Coverage Report

Created: 2017-06-28 17:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_flow.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2005-2007 Universiteit Leiden
3
 * Copyright 2008-2009 Katholieke Universiteit Leuven
4
 * Copyright 2010      INRIA Saclay
5
 * Copyright 2012      Universiteit Leiden
6
 * Copyright 2014      Ecole Normale Superieure
7
 *
8
 * Use of this software is governed by the MIT license
9
 *
10
 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
11
 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
12
 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
13
 * B-3001 Leuven, Belgium
14
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
16
 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
17
 */
18
19
#include <isl/set.h>
20
#include <isl/map.h>
21
#include <isl/union_set.h>
22
#include <isl/union_map.h>
23
#include <isl/flow.h>
24
#include <isl/schedule_node.h>
25
#include <isl_sort.h>
26
27
enum isl_restriction_type {
28
  isl_restriction_type_empty,
29
  isl_restriction_type_none,
30
  isl_restriction_type_input,
31
  isl_restriction_type_output
32
};
33
34
struct isl_restriction {
35
  enum isl_restriction_type type;
36
37
  isl_set *source;
38
  isl_set *sink;
39
};
40
41
/* Create a restriction of the given type.
42
 */
43
static __isl_give isl_restriction *isl_restriction_alloc(
44
  __isl_take isl_map *source_map, enum isl_restriction_type type)
45
0
{
46
0
  isl_ctx *ctx;
47
0
  isl_restriction *restr;
48
0
49
0
  if (!source_map)
50
0
    return NULL;
51
0
52
0
  ctx = isl_map_get_ctx(source_map);
53
0
  restr = isl_calloc_type(ctx, struct isl_restriction);
54
0
  if (!restr)
55
0
    goto error;
56
0
57
0
  restr->type = type;
58
0
59
0
  isl_map_free(source_map);
60
0
  return restr;
61
0
error:
62
0
  isl_map_free(source_map);
63
0
  return NULL;
64
0
}
65
66
/* Create a restriction that doesn't restrict anything.
67
 */
68
__isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
69
0
{
70
0
  return isl_restriction_alloc(source_map, isl_restriction_type_none);
71
0
}
72
73
/* Create a restriction that removes everything.
74
 */
75
__isl_give isl_restriction *isl_restriction_empty(
76
  __isl_take isl_map *source_map)
77
0
{
78
0
  return isl_restriction_alloc(source_map, isl_restriction_type_empty);
79
0
}
80
81
/* Create a restriction on the input of the maximization problem
82
 * based on the given source and sink restrictions.
83
 */
84
__isl_give isl_restriction *isl_restriction_input(
85
  __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
86
0
{
87
0
  isl_ctx *ctx;
88
0
  isl_restriction *restr;
89
0
90
0
  if (
!source_restr || 0
!sink_restr0
)
91
0
    goto error;
92
0
93
0
  ctx = isl_set_get_ctx(source_restr);
94
0
  restr = isl_calloc_type(ctx, struct isl_restriction);
95
0
  if (!restr)
96
0
    goto error;
97
0
98
0
  restr->type = isl_restriction_type_input;
99
0
  restr->source = source_restr;
100
0
  restr->sink = sink_restr;
101
0
102
0
  return restr;
103
0
error:
104
0
  isl_set_free(source_restr);
105
0
  isl_set_free(sink_restr);
106
0
  return NULL;
107
0
}
108
109
/* Create a restriction on the output of the maximization problem
110
 * based on the given source restriction.
111
 */
112
__isl_give isl_restriction *isl_restriction_output(
113
  __isl_take isl_set *source_restr)
114
0
{
115
0
  isl_ctx *ctx;
116
0
  isl_restriction *restr;
117
0
118
0
  if (!source_restr)
119
0
    return NULL;
120
0
121
0
  ctx = isl_set_get_ctx(source_restr);
122
0
  restr = isl_calloc_type(ctx, struct isl_restriction);
123
0
  if (!restr)
124
0
    goto error;
125
0
126
0
  restr->type = isl_restriction_type_output;
127
0
  restr->source = source_restr;
128
0
129
0
  return restr;
130
0
error:
131
0
  isl_set_free(source_restr);
132
0
  return NULL;
133
0
}
134
135
__isl_null isl_restriction *isl_restriction_free(
136
  __isl_take isl_restriction *restr)
137
0
{
138
0
  if (!restr)
139
0
    return NULL;
140
0
141
0
  isl_set_free(restr->source);
142
0
  isl_set_free(restr->sink);
143
0
  free(restr);
144
0
  return NULL;
145
0
}
146
147
isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
148
0
{
149
0
  return restr ? isl_set_get_ctx(restr->source) : NULL;
150
0
}
151
152
/* A private structure to keep track of a mapping together with
153
 * a user-specified identifier and a boolean indicating whether
154
 * the map represents a must or may access/dependence.
155
 */
156
struct isl_labeled_map {
157
  struct isl_map  *map;
158
  void    *data;
159
  int   must;
160
};
161
162
/* A structure containing the input for dependence analysis:
163
 * - a sink
164
 * - n_must + n_may (<= max_source) sources
165
 * - a function for determining the relative order of sources and sink
166
 * The must sources are placed before the may sources.
167
 *
168
 * domain_map is an auxiliary map that maps the sink access relation
169
 * to the domain of this access relation.
170
 * This field is only needed when restrict_fn is set and
171
 * the field itself is set by isl_access_info_compute_flow.
172
 *
173
 * restrict_fn is a callback that (if not NULL) will be called
174
 * right before any lexicographical maximization.
175
 */
176
struct isl_access_info {
177
  isl_map       *domain_map;
178
  struct isl_labeled_map    sink;
179
  isl_access_level_before   level_before;
180
181
  isl_access_restrict   restrict_fn;
182
  void        *restrict_user;
183
184
  int           max_source;
185
  int           n_must;
186
  int           n_may;
187
  struct isl_labeled_map    source[1];
188
};
189
190
/* A structure containing the output of dependence analysis:
191
 * - n_source dependences
192
 * - a wrapped subset of the sink for which definitely no source could be found
193
 * - a wrapped subset of the sink for which possibly no source could be found
194
 */
195
struct isl_flow {
196
  isl_set     *must_no_source;
197
  isl_set     *may_no_source;
198
  int     n_source;
199
  struct isl_labeled_map  *dep;
200
};
201
202
/* Construct an isl_access_info structure and fill it up with
203
 * the given data.  The number of sources is set to 0.
204
 */
205
__isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
206
  void *sink_user, isl_access_level_before fn, int max_source)
207
3.40k
{
208
3.40k
  isl_ctx *ctx;
209
3.40k
  struct isl_access_info *acc;
210
3.40k
211
3.40k
  if (!sink)
212
0
    return NULL;
213
3.40k
214
3.40k
  ctx = isl_map_get_ctx(sink);
215
3.40k
  isl_assert(ctx, max_source >= 0, goto error);
216
3.40k
217
3.40k
  
acc = 3.40k
isl_calloc3.40k
(ctx, struct isl_access_info,
218
3.40k
      sizeof(struct isl_access_info) +
219
3.40k
      (max_source - 1) * sizeof(struct isl_labeled_map));
220
3.40k
  if (!acc)
221
0
    goto error;
222
3.40k
223
3.40k
  acc->sink.map = sink;
224
3.40k
  acc->sink.data = sink_user;
225
3.40k
  acc->level_before = fn;
226
3.40k
  acc->max_source = max_source;
227
3.40k
  acc->n_must = 0;
228
3.40k
  acc->n_may = 0;
229
3.40k
230
3.40k
  return acc;
231
0
error:
232
0
  isl_map_free(sink);
233
0
  return NULL;
234
3.40k
}
235
236
/* Free the given isl_access_info structure.
237
 */
238
__isl_null isl_access_info *isl_access_info_free(
239
  __isl_take isl_access_info *acc)
240
3.40k
{
241
3.40k
  int i;
242
3.40k
243
3.40k
  if (!acc)
244
0
    return NULL;
245
3.40k
  isl_map_free(acc->domain_map);
246
3.40k
  isl_map_free(acc->sink.map);
247
9.28k
  for (i = 0; 
i < acc->n_must + acc->n_may9.28k
;
++i5.87k
)
248
5.87k
    isl_map_free(acc->source[i].map);
249
3.40k
  free(acc);
250
3.40k
  return NULL;
251
3.40k
}
252
253
isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
254
0
{
255
0
  return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
256
0
}
257
258
__isl_give isl_access_info *isl_access_info_set_restrict(
259
  __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
260
0
{
261
0
  if (!acc)
262
0
    return NULL;
263
0
  acc->restrict_fn = fn;
264
0
  acc->restrict_user = user;
265
0
  return acc;
266
0
}
267
268
/* Add another source to an isl_access_info structure, making
269
 * sure the "must" sources are placed before the "may" sources.
270
 * This function may be called at most max_source times on a
271
 * given isl_access_info structure, with max_source as specified
272
 * in the call to isl_access_info_alloc that constructed the structure.
273
 */
274
__isl_give isl_access_info *isl_access_info_add_source(
275
  __isl_take isl_access_info *acc, __isl_take isl_map *source,
276
  int must, void *source_user)
277
5.87k
{
278
5.87k
  isl_ctx *ctx;
279
5.87k
280
5.87k
  if (!acc)
281
0
    goto error;
282
5.87k
  ctx = isl_map_get_ctx(acc->sink.map);
283
5.87k
  isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
284
5.87k
  
285
5.87k
  
if (5.87k
must5.87k
)
{5.30k
286
5.30k
    if (acc->n_may)
287
121
      acc->source[acc->n_must + acc->n_may] =
288
121
        acc->source[acc->n_must];
289
5.30k
    acc->source[acc->n_must].map = source;
290
5.30k
    acc->source[acc->n_must].data = source_user;
291
5.30k
    acc->source[acc->n_must].must = 1;
292
5.30k
    acc->n_must++;
293
569
  } else {
294
569
    acc->source[acc->n_must + acc->n_may].map = source;
295
569
    acc->source[acc->n_must + acc->n_may].data = source_user;
296
569
    acc->source[acc->n_must + acc->n_may].must = 0;
297
569
    acc->n_may++;
298
569
  }
299
5.87k
300
5.87k
  return acc;
301
0
error:
302
0
  isl_map_free(source);
303
0
  isl_access_info_free(acc);
304
0
  return NULL;
305
5.87k
}
306
307
/* Return -n, 0 or n (with n a positive value), depending on whether
308
 * the source access identified by p1 should be sorted before, together
309
 * or after that identified by p2.
310
 *
311
 * If p1 appears before p2, then it should be sorted first.
312
 * For more generic initial schedules, it is possible that neither
313
 * p1 nor p2 appears before the other, or at least not in any obvious way.
314
 * We therefore also check if p2 appears before p1, in which case p2
315
 * should be sorted first.
316
 * If not, we try to order the two statements based on the description
317
 * of the iteration domains.  This results in an arbitrary, but fairly
318
 * stable ordering.
319
 */
320
static int access_sort_cmp(const void *p1, const void *p2, void *user)
321
3.38k
{
322
3.38k
  isl_access_info *acc = user;
323
3.38k
  const struct isl_labeled_map *i1, *i2;
324
3.38k
  int level1, level2;
325
3.38k
  uint32_t h1, h2;
326
3.38k
  i1 = (const struct isl_labeled_map *) p1;
327
3.38k
  i2 = (const struct isl_labeled_map *) p2;
328
3.38k
329
3.38k
  level1 = acc->level_before(i1->data, i2->data);
330
3.38k
  if (level1 % 2)
331
3.33k
    return -1;
332
3.38k
333
51
  level2 = acc->level_before(i2->data, i1->data);
334
51
  if (level2 % 2)
335
50
    return 1;
336
51
337
1
  h1 = isl_map_get_hash(i1->map);
338
1
  h2 = isl_map_get_hash(i2->map);
339
1
  return h1 > h2 ? 
11
:
h1 < h2 ? 0
-10
:
00
;
340
51
}
341
342
/* Sort the must source accesses in their textual order.
343
 */
344
static __isl_give isl_access_info *isl_access_info_sort_sources(
345
  __isl_take isl_access_info *acc)
346
3.12k
{
347
3.12k
  if (!acc)
348
0
    return NULL;
349
3.12k
  
if (3.12k
acc->n_must <= 13.12k
)
350
2.02k
    return acc;
351
3.12k
352
1.10k
  
if (1.10k
isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),1.10k
353
1.10k
        access_sort_cmp, acc) < 0)
354
0
    return isl_access_info_free(acc);
355
1.10k
356
1.10k
  return acc;
357
1.10k
}
358
359
/* Align the parameters of the two spaces if needed and then call
360
 * isl_space_join.
361
 */
362
static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
363
  __isl_take isl_space *right)
364
6.01k
{
365
6.01k
  isl_bool equal_params;
366
6.01k
367
6.01k
  equal_params = isl_space_has_equal_params(left, right);
368
6.01k
  if (equal_params < 0)
369
0
    goto error;
370
6.01k
  
if (6.01k
equal_params6.01k
)
371
6.01k
    return isl_space_join(left, right);
372
6.01k
373
0
  left = isl_space_align_params(left, isl_space_copy(right));
374
0
  right = isl_space_align_params(right, isl_space_copy(left));
375
0
  return isl_space_join(left, right);
376
0
error:
377
0
  isl_space_free(left);
378
0
  isl_space_free(right);
379
0
  return NULL;
380
6.01k
}
381
382
/* Initialize an empty isl_flow structure corresponding to a given
383
 * isl_access_info structure.
384
 * For each must access, two dependences are created (initialized
385
 * to the empty relation), one for the resulting must dependences
386
 * and one for the resulting may dependences.  May accesses can
387
 * only lead to may dependences, so only one dependence is created
388
 * for each of them.
389
 * This function is private as isl_flow structures are only supposed
390
 * to be created by isl_access_info_compute_flow.
391
 */
392
static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
393
3.40k
{
394
3.40k
  int i, n;
395
3.40k
  struct isl_ctx *ctx;
396
3.40k
  struct isl_flow *dep;
397
3.40k
398
3.40k
  if (!acc)
399
0
    return NULL;
400
3.40k
401
3.40k
  ctx = isl_map_get_ctx(acc->sink.map);
402
3.40k
  dep = isl_calloc_type(ctx, struct isl_flow);
403
3.40k
  if (!dep)
404
0
    return NULL;
405
3.40k
406
3.40k
  n = 2 * acc->n_must + acc->n_may;
407
3.40k
  dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
408
3.40k
  if (
n && 3.40k
!dep->dep3.18k
)
409
0
    goto error;
410
3.40k
411
3.40k
  dep->n_source = n;
412
8.71k
  for (i = 0; 
i < acc->n_must8.71k
;
++i5.30k
)
{5.30k
413
5.30k
    isl_space *dim;
414
5.30k
    dim = space_align_and_join(
415
5.30k
      isl_map_get_space(acc->source[i].map),
416
5.30k
      isl_space_reverse(isl_map_get_space(acc->sink.map)));
417
5.30k
    dep->dep[2 * i].map = isl_map_empty(dim);
418
5.30k
    dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
419
5.30k
    dep->dep[2 * i].data = acc->source[i].data;
420
5.30k
    dep->dep[2 * i + 1].data = acc->source[i].data;
421
5.30k
    dep->dep[2 * i].must = 1;
422
5.30k
    dep->dep[2 * i + 1].must = 0;
423
5.30k
    if (
!dep->dep[2 * i].map || 5.30k
!dep->dep[2 * i + 1].map5.30k
)
424
0
      goto error;
425
5.30k
  }
426
3.97k
  
for (i = acc->n_must; 3.40k
i < acc->n_must + acc->n_may3.97k
;
++i569
)
{569
427
569
    isl_space *dim;
428
569
    dim = space_align_and_join(
429
569
      isl_map_get_space(acc->source[i].map),
430
569
      isl_space_reverse(isl_map_get_space(acc->sink.map)));
431
569
    dep->dep[acc->n_must + i].map = isl_map_empty(dim);
432
569
    dep->dep[acc->n_must + i].data = acc->source[i].data;
433
569
    dep->dep[acc->n_must + i].must = 0;
434
569
    if (!dep->dep[acc->n_must + i].map)
435
0
      goto error;
436
569
  }
437
3.40k
438
3.40k
  return dep;
439
0
error:
440
0
  isl_flow_free(dep);
441
0
  return NULL;
442
3.40k
}
443
444
/* Iterate over all sources and for each resulting flow dependence
445
 * that is not empty, call the user specfied function.
446
 * The second argument in this function call identifies the source,
447
 * while the third argument correspond to the final argument of
448
 * the isl_flow_foreach call.
449
 */
450
isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
451
  isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
452
    void *user),
453
  void *user)
454
6
{
455
6
  int i;
456
6
457
6
  if (!deps)
458
0
    return isl_stat_error;
459
6
460
21
  
for (i = 0; 6
i < deps->n_source21
;
++i15
)
{15
461
15
    if (isl_map_plain_is_empty(deps->dep[i].map))
462
3
      continue;
463
12
    
if (12
fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,12
464
12
        deps->dep[i].data, user) < 0)
465
0
      return isl_stat_error;
466
12
  }
467
6
468
6
  return isl_stat_ok;
469
6
}
470
471
/* Return a copy of the subset of the sink for which no source could be found.
472
 */
473
__isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
474
6.80k
{
475
6.80k
  if (!deps)
476
0
    return NULL;
477
6.80k
  
478
6.80k
  
if (6.80k
must6.80k
)
479
3.40k
    return isl_set_unwrap(isl_set_copy(deps->must_no_source));
480
6.80k
  else
481
3.40k
    return isl_set_unwrap(isl_set_copy(deps->may_no_source));
482
6.80k
}
483
484
void isl_flow_free(__isl_take isl_flow *deps)
485
3.40k
{
486
3.40k
  int i;
487
3.40k
488
3.40k
  if (!deps)
489
0
    return;
490
3.40k
  isl_set_free(deps->must_no_source);
491
3.40k
  isl_set_free(deps->may_no_source);
492
3.40k
  if (
deps->dep3.40k
)
{3.40k
493
14.5k
    for (i = 0; 
i < deps->n_source14.5k
;
++i11.1k
)
494
11.1k
      isl_map_free(deps->dep[i].map);
495
3.40k
    free(deps->dep);
496
3.40k
  }
497
3.40k
  free(deps);
498
3.40k
}
499
500
isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
501
0
{
502
0
  return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
503
0
}
504
505
/* Return a map that enforces that the domain iteration occurs after
506
 * the range iteration at the given level.
507
 * If level is odd, then the domain iteration should occur after
508
 * the target iteration in their shared level/2 outermost loops.
509
 * In this case we simply need to enforce that these outermost
510
 * loop iterations are the same.
511
 * If level is even, then the loop iterator of the domain should
512
 * be greater than the loop iterator of the range at the last
513
 * of the level/2 shared loops, i.e., loop level/2 - 1.
514
 */
515
static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
516
12.7k
{
517
12.7k
  struct isl_basic_map *bmap;
518
12.7k
519
12.7k
  if (level % 2)
520
2.14k
    bmap = isl_basic_map_equal(dim, level/2);
521
12.7k
  else
522
10.6k
    bmap = isl_basic_map_more_at(dim, level/2 - 1);
523
12.7k
524
12.7k
  return isl_map_from_basic_map(bmap);
525
12.7k
}
526
527
/* Compute the partial lexicographic maximum of "dep" on domain "sink",
528
 * but first check if the user has set acc->restrict_fn and if so
529
 * update either the input or the output of the maximization problem
530
 * with respect to the resulting restriction.
531
 *
532
 * Since the user expects a mapping from sink iterations to source iterations,
533
 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
534
 * to accessed array elements, we first need to project out the accessed
535
 * sink array elements by applying acc->domain_map.
536
 * Similarly, the sink restriction specified by the user needs to be
537
 * converted back to the wrapped map.
538
 */
539
static __isl_give isl_map *restricted_partial_lexmax(
540
  __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
541
  int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
542
9.90k
{
543
9.90k
  isl_map *source_map;
544
9.90k
  isl_restriction *restr;
545
9.90k
  isl_set *sink_domain;
546
9.90k
  isl_set *sink_restr;
547
9.90k
  isl_map *res;
548
9.90k
549
9.90k
  if (!acc->restrict_fn)
550
9.90k
    return isl_map_partial_lexmax(dep, sink, empty);
551
9.90k
552
0
  source_map = isl_map_copy(dep);
553
0
  source_map = isl_map_apply_domain(source_map,
554
0
              isl_map_copy(acc->domain_map));
555
0
  sink_domain = isl_set_copy(sink);
556
0
  sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
557
0
  restr = acc->restrict_fn(source_map, sink_domain,
558
0
        acc->source[source].data, acc->restrict_user);
559
0
  isl_set_free(sink_domain);
560
0
  isl_map_free(source_map);
561
0
562
0
  if (!restr)
563
0
    goto error;
564
0
  
if (0
restr->type == isl_restriction_type_input0
)
{0
565
0
    dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
566
0
    sink_restr = isl_set_copy(restr->sink);
567
0
    sink_restr = isl_set_apply(sink_restr,
568
0
        isl_map_reverse(isl_map_copy(acc->domain_map)));
569
0
    sink = isl_set_intersect(sink, sink_restr);
570
0
  } else 
if (0
restr->type == isl_restriction_type_empty0
)
{0
571
0
    isl_space *space = isl_map_get_space(dep);
572
0
    isl_map_free(dep);
573
0
    dep = isl_map_empty(space);
574
0
  }
575
0
576
0
  res = isl_map_partial_lexmax(dep, sink, empty);
577
0
578
0
  if (restr->type == isl_restriction_type_output)
579
0
    res = isl_map_intersect_range(res, isl_set_copy(restr->source));
580
0
581
0
  isl_restriction_free(restr);
582
0
  return res;
583
0
error:
584
0
  isl_map_free(dep);
585
0
  isl_set_free(sink);
586
0
  *empty = NULL;
587
0
  return NULL;
588
0
}
589
590
/* Compute the last iteration of must source j that precedes the sink
591
 * at the given level for sink iterations in set_C.
592
 * The subset of set_C for which no such iteration can be found is returned
593
 * in *empty.
594
 */
595
static struct isl_map *last_source(struct isl_access_info *acc, 
596
            struct isl_set *set_C,
597
            int j, int level, struct isl_set **empty)
598
9.76k
{
599
9.76k
  struct isl_map *read_map;
600
9.76k
  struct isl_map *write_map;
601
9.76k
  struct isl_map *dep_map;
602
9.76k
  struct isl_map *after;
603
9.76k
  struct isl_map *result;
604
9.76k
605
9.76k
  read_map = isl_map_copy(acc->sink.map);
606
9.76k
  write_map = isl_map_copy(acc->source[j].map);
607
9.76k
  write_map = isl_map_reverse(write_map);
608
9.76k
  dep_map = isl_map_apply_range(read_map, write_map);
609
9.76k
  after = after_at_level(isl_map_get_space(dep_map), level);
610
9.76k
  dep_map = isl_map_intersect(dep_map, after);
611
9.76k
  result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
612
9.76k
  result = isl_map_reverse(result);
613
9.76k
614
9.76k
  return result;
615
9.76k
}
616
617
/* For a given mapping between iterations of must source j and iterations
618
 * of the sink, compute the last iteration of must source k preceding
619
 * the sink at level before_level for any of the sink iterations,
620
 * but following the corresponding iteration of must source j at level
621
 * after_level.
622
 */
623
static struct isl_map *last_later_source(struct isl_access_info *acc,
624
           struct isl_map *old_map,
625
           int j, int before_level,
626
           int k, int after_level,
627
           struct isl_set **empty)
628
143
{
629
143
  isl_space *dim;
630
143
  struct isl_set *set_C;
631
143
  struct isl_map *read_map;
632
143
  struct isl_map *write_map;
633
143
  struct isl_map *dep_map;
634
143
  struct isl_map *after_write;
635
143
  struct isl_map *before_read;
636
143
  struct isl_map *result;
637
143
638
143
  set_C = isl_map_range(isl_map_copy(old_map));
639
143
  read_map = isl_map_copy(acc->sink.map);
640
143
  write_map = isl_map_copy(acc->source[k].map);
641
143
642
143
  write_map = isl_map_reverse(write_map);
643
143
  dep_map = isl_map_apply_range(read_map, write_map);
644
143
  dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
645
143
        isl_space_reverse(isl_map_get_space(acc->source[j].map)));
646
143
  after_write = after_at_level(dim, after_level);
647
143
  after_write = isl_map_apply_range(after_write, old_map);
648
143
  after_write = isl_map_reverse(after_write);
649
143
  dep_map = isl_map_intersect(dep_map, after_write);
650
143
  before_read = after_at_level(isl_map_get_space(dep_map), before_level);
651
143
  dep_map = isl_map_intersect(dep_map, before_read);
652
143
  result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
653
143
  result = isl_map_reverse(result);
654
143
655
143
  return result;
656
143
}
657
658
/* Given a shared_level between two accesses, return 1 if the
659
 * the first can precede the second at the requested target_level.
660
 * If the target level is odd, i.e., refers to a statement level
661
 * dimension, then first needs to precede second at the requested
662
 * level, i.e., shared_level must be equal to target_level.
663
 * If the target level is odd, then the two loops should share
664
 * at least the requested number of outer loops.
665
 */
666
static int can_precede_at_level(int shared_level, int target_level)
667
57.9k
{
668
57.9k
  if (shared_level < target_level)
669
46.5k
    return 0;
670
11.4k
  
if (11.4k
(target_level % 2) && 11.4k
shared_level > target_level6.41k
)
671
4.56k
    return 0;
672
6.92k
  return 1;
673
11.4k
}
674
675
/* Given a possible flow dependence temp_rel[j] between source j and the sink
676
 * at level sink_level, remove those elements for which
677
 * there is an iteration of another source k < j that is closer to the sink.
678
 * The flow dependences temp_rel[k] are updated with the improved sources.
679
 * Any improved source needs to precede the sink at the same level
680
 * and needs to follow source j at the same or a deeper level.
681
 * The lower this level, the later the execution date of source k.
682
 * We therefore consider lower levels first.
683
 *
684
 * If temp_rel[j] is empty, then there can be no improvement and
685
 * we return immediately.
686
 */
687
static int intermediate_sources(__isl_keep isl_access_info *acc,
688
  struct isl_map **temp_rel, int j, int sink_level)
689
10.5k
{
690
10.5k
  int k, level;
691
10.5k
  int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
692
10.5k
693
10.5k
  if (isl_map_plain_is_empty(temp_rel[j]))
694
8.65k
    return 0;
695
10.5k
696
3.29k
  
for (k = j - 1; 1.84k
k >= 03.29k
;
--k1.44k
)
{1.44k
697
1.44k
    int plevel, plevel2;
698
1.44k
    plevel = acc->level_before(acc->source[k].data, acc->sink.data);
699
1.44k
    if (!can_precede_at_level(plevel, sink_level))
700
885
      continue;
701
1.44k
702
563
    plevel2 = acc->level_before(acc->source[j].data,
703
563
            acc->source[k].data);
704
563
705
5.70k
    for (level = sink_level; 
level <= depth5.70k
;
++level5.13k
)
{5.13k
706
5.13k
      struct isl_map *T;
707
5.13k
      struct isl_set *trest;
708
5.13k
      struct isl_map *copy;
709
5.13k
710
5.13k
      if (!can_precede_at_level(plevel2, level))
711
4.99k
        continue;
712
5.13k
713
143
      copy = isl_map_copy(temp_rel[j]);
714
143
      T = last_later_source(acc, copy, j, sink_level, k,
715
143
                level, &trest);
716
143
      if (
isl_map_plain_is_empty(T)143
)
{127
717
127
        isl_set_free(trest);
718
127
        isl_map_free(T);
719
127
        continue;
720
127
      }
721
16
      temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
722
16
      temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
723
16
    }
724
563
  }
725
1.84k
726
1.84k
  return 0;
727
10.5k
}
728
729
/* Compute all iterations of may source j that precedes the sink at the given
730
 * level for sink iterations in set_C.
731
 */
732
static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
733
            __isl_take isl_set *set_C, int j, int level)
734
1.12k
{
735
1.12k
  isl_map *read_map;
736
1.12k
  isl_map *write_map;
737
1.12k
  isl_map *dep_map;
738
1.12k
  isl_map *after;
739
1.12k
740
1.12k
  read_map = isl_map_copy(acc->sink.map);
741
1.12k
  read_map = isl_map_intersect_domain(read_map, set_C);
742
1.12k
  write_map = isl_map_copy(acc->source[acc->n_must + j].map);
743
1.12k
  write_map = isl_map_reverse(write_map);
744
1.12k
  dep_map = isl_map_apply_range(read_map, write_map);
745
1.12k
  after = after_at_level(isl_map_get_space(dep_map), level);
746
1.12k
  dep_map = isl_map_intersect(dep_map, after);
747
1.12k
748
1.12k
  return isl_map_reverse(dep_map);
749
1.12k
}
750
751
/* For a given mapping between iterations of must source k and iterations
752
 * of the sink, compute all iterations of may source j preceding
753
 * the sink at level before_level for any of the sink iterations,
754
 * but following the corresponding iteration of must source k at level
755
 * after_level.
756
 */
757
static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
758
  __isl_take isl_map *old_map,
759
  int j, int before_level, int k, int after_level)
760
800
{
761
800
  isl_space *dim;
762
800
  isl_set *set_C;
763
800
  isl_map *read_map;
764
800
  isl_map *write_map;
765
800
  isl_map *dep_map;
766
800
  isl_map *after_write;
767
800
  isl_map *before_read;
768
800
769
800
  set_C = isl_map_range(isl_map_copy(old_map));
770
800
  read_map = isl_map_copy(acc->sink.map);
771
800
  read_map = isl_map_intersect_domain(read_map, set_C);
772
800
  write_map = isl_map_copy(acc->source[acc->n_must + j].map);
773
800
774
800
  write_map = isl_map_reverse(write_map);
775
800
  dep_map = isl_map_apply_range(read_map, write_map);
776
800
  dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
777
800
        isl_space_reverse(isl_map_get_space(acc->source[k].map)));
778
800
  after_write = after_at_level(dim, after_level);
779
800
  after_write = isl_map_apply_range(after_write, old_map);
780
800
  after_write = isl_map_reverse(after_write);
781
800
  dep_map = isl_map_intersect(dep_map, after_write);
782
800
  before_read = after_at_level(isl_map_get_space(dep_map), before_level);
783
800
  dep_map = isl_map_intersect(dep_map, before_read);
784
800
  return isl_map_reverse(dep_map);
785
800
}
786
787
/* Given the must and may dependence relations for the must accesses
788
 * for level sink_level, check if there are any accesses of may access j
789
 * that occur in between and return their union.
790
 * If some of these accesses are intermediate with respect to
791
 * (previously thought to be) must dependences, then these
792
 * must dependences are turned into may dependences.
793
 */
794
static __isl_give isl_map *all_intermediate_sources(
795
  __isl_keep isl_access_info *acc, __isl_take isl_map *map,
796
  struct isl_map **must_rel, struct isl_map **may_rel,
797
  int j, int sink_level)
798
564
{
799
564
  int k, level;
800
564
  int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
801
564
          isl_dim_in) + 1;
802
564
803
1.31k
  for (k = 0; 
k < acc->n_must1.31k
;
++k746
)
{746
804
746
    int plevel;
805
746
806
746
    if (isl_map_plain_is_empty(may_rel[k]) &&
807
702
        isl_map_plain_is_empty(must_rel[k]))
808
512
      continue;
809
746
810
234
    plevel = acc->level_before(acc->source[k].data,
811
234
          acc->source[acc->n_must + j].data);
812
234
813
1.51k
    for (level = sink_level; 
level <= depth1.51k
;
++level1.28k
)
{1.28k
814
1.28k
      isl_map *T;
815
1.28k
      isl_map *copy;
816
1.28k
      isl_set *ran;
817
1.28k
818
1.28k
      if (!can_precede_at_level(plevel, level))
819
885
        continue;
820
1.28k
821
400
      copy = isl_map_copy(may_rel[k]);
822
400
      T = all_later_sources(acc, copy, j, sink_level, k, level);
823
400
      map = isl_map_union(map, T);
824
400
825
400
      copy = isl_map_copy(must_rel[k]);
826
400
      T = all_later_sources(acc, copy, j, sink_level, k, level);
827
400
      ran = isl_map_range(isl_map_copy(T));
828
400
      map = isl_map_union(map, T);
829
400
      may_rel[k] = isl_map_union_disjoint(may_rel[k],
830
400
          isl_map_intersect_range(isl_map_copy(must_rel[k]),
831
400
                isl_set_copy(ran)));
832
400
      T = isl_map_from_domain_and_range(
833
400
          isl_set_universe(
834
400
        isl_space_domain(isl_map_get_space(must_rel[k]))),
835
400
          ran);
836
400
      must_rel[k] = isl_map_subtract(must_rel[k], T);
837
400
    }
838
234
  }
839
564
840
564
  return map;
841
564
}
842
843
/* Compute dependences for the case where all accesses are "may"
844
 * accesses, which boils down to computing memory based dependences.
845
 * The generic algorithm would also work in this case, but it would
846
 * be overkill to use it.
847
 */
848
static __isl_give isl_flow *compute_mem_based_dependences(
849
  __isl_keep isl_access_info *acc)
850
283
{
851
283
  int i;
852
283
  isl_set *mustdo;
853
283
  isl_set *maydo;
854
283
  isl_flow *res;
855
283
856
283
  res = isl_flow_alloc(acc);
857
283
  if (!res)
858
0
    return NULL;
859
283
860
283
  mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
861
283
  maydo = isl_set_copy(mustdo);
862
283
863
362
  for (i = 0; 
i < acc->n_may362
;
++i79
)
{79
864
79
    int plevel;
865
79
    int is_before;
866
79
    isl_space *dim;
867
79
    isl_map *before;
868
79
    isl_map *dep;
869
79
870
79
    plevel = acc->level_before(acc->source[i].data, acc->sink.data);
871
79
    is_before = plevel & 1;
872
79
    plevel >>= 1;
873
79
874
79
    dim = isl_map_get_space(res->dep[i].map);
875
79
    if (is_before)
876
10
      before = isl_map_lex_le_first(dim, plevel);
877
79
    else
878
69
      before = isl_map_lex_lt_first(dim, plevel);
879
79
    dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
880
79
      isl_map_reverse(isl_map_copy(acc->sink.map)));
881
79
    dep = isl_map_intersect(dep, before);
882
79
    mustdo = isl_set_subtract(mustdo,
883
79
              isl_map_range(isl_map_copy(dep)));
884
79
    res->dep[i].map = isl_map_union(res->dep[i].map, dep);
885
79
  }
886
283
887
283
  res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
888
283
  res->must_no_source = mustdo;
889
283
890
283
  return res;
891
283
}
892
893
/* Compute dependences for the case where there is at least one
894
 * "must" access.
895
 *
896
 * The core algorithm considers all levels in which a source may precede
897
 * the sink, where a level may either be a statement level or a loop level.
898
 * The outermost statement level is 1, the first loop level is 2, etc...
899
 * The algorithm basically does the following:
900
 * for all levels l of the read access from innermost to outermost
901
 *  for all sources w that may precede the sink access at that level
902
 *      compute the last iteration of the source that precedes the sink access
903
 *              at that level
904
 *      add result to possible last accesses at level l of source w
905
 *      for all sources w2 that we haven't considered yet at this level that may
906
 *              also precede the sink access
907
 *    for all levels l2 of w from l to innermost
908
 *        for all possible last accesses dep of w at l
909
 *      compute last iteration of w2 between the source and sink
910
 *                of dep
911
 *      add result to possible last accesses at level l of write w2
912
 *      and replace possible last accesses dep by the remainder
913
 *
914
 *
915
 * The above algorithm is applied to the must access.  During the course
916
 * of the algorithm, we keep track of sink iterations that still
917
 * need to be considered.  These iterations are split into those that
918
 * haven't been matched to any source access (mustdo) and those that have only
919
 * been matched to may accesses (maydo).
920
 * At the end of each level, we also consider the may accesses.
921
 * In particular, we consider may accesses that precede the remaining
922
 * sink iterations, moving elements from mustdo to maydo when appropriate,
923
 * and may accesses that occur between a must source and a sink of any 
924
 * dependences found at the current level, turning must dependences into
925
 * may dependences when appropriate.
926
 * 
927
 */
928
static __isl_give isl_flow *compute_val_based_dependences(
929
  __isl_keep isl_access_info *acc)
930
3.12k
{
931
3.12k
  isl_ctx *ctx;
932
3.12k
  isl_flow *res;
933
3.12k
  isl_set *mustdo = NULL;
934
3.12k
  isl_set *maydo = NULL;
935
3.12k
  int level, j;
936
3.12k
  int depth;
937
3.12k
  isl_map **must_rel = NULL;
938
3.12k
  isl_map **may_rel = NULL;
939
3.12k
940
3.12k
  if (!acc)
941
0
    return NULL;
942
3.12k
943
3.12k
  res = isl_flow_alloc(acc);
944
3.12k
  if (!res)
945
0
    goto error;
946
3.12k
  ctx = isl_map_get_ctx(acc->sink.map);
947
3.12k
948
3.12k
  depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
949
3.12k
  mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
950
3.12k
  maydo = isl_set_empty(isl_set_get_space(mustdo));
951
3.12k
  if (
!mustdo || 3.12k
!maydo3.12k
)
952
0
    goto error;
953
3.12k
  
if (3.12k
isl_set_plain_is_empty(mustdo)3.12k
)
954
0
    goto done;
955
3.12k
956
3.12k
  
must_rel = 3.12k
isl_alloc_array3.12k
(ctx, struct isl_map *, acc->n_must);
957
3.12k
  may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
958
3.12k
  if (
!must_rel || 3.12k
!may_rel3.12k
)
959
0
    goto error;
960
3.12k
961
27.5k
  
for (level = depth; 3.12k
level >= 127.5k
;
--level24.4k
)
{24.8k
962
71.1k
    for (j = acc->n_must-1; 
j >=071.1k
;
--j46.2k
)
{46.2k
963
46.2k
      isl_space *space;
964
46.2k
      space = isl_map_get_space(res->dep[2 * j].map);
965
46.2k
      must_rel[j] = isl_map_empty(space);
966
46.2k
      may_rel[j] = isl_map_copy(must_rel[j]);
967
46.2k
    }
968
24.8k
969
70.3k
    for (j = acc->n_must - 1; 
j >= 070.3k
;
--j45.4k
)
{45.9k
970
45.9k
      struct isl_map *T;
971
45.9k
      struct isl_set *rest;
972
45.9k
      int plevel;
973
45.9k
974
45.9k
      plevel = acc->level_before(acc->source[j].data,
975
45.9k
                 acc->sink.data);
976
45.9k
      if (!can_precede_at_level(plevel, level))
977
41.0k
        continue;
978
45.9k
979
4.88k
      T = last_source(acc, mustdo, j, level, &rest);
980
4.88k
      must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
981
4.88k
      mustdo = rest;
982
4.88k
983
4.88k
      intermediate_sources(acc, must_rel, j, level);
984
4.88k
985
4.88k
      T = last_source(acc, maydo, j, level, &rest);
986
4.88k
      may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
987
4.88k
      maydo = rest;
988
4.88k
989
4.88k
      intermediate_sources(acc, may_rel, j, level);
990
4.88k
991
4.88k
      if (isl_set_plain_is_empty(mustdo) &&
992
501
          isl_set_plain_is_empty(maydo))
993
463
        break;
994
4.88k
    }
995
25.2k
    for (j = j - 1; 
j >= 025.2k
;
--j370
)
{370
996
370
      int plevel;
997
370
998
370
      plevel = acc->level_before(acc->source[j].data,
999
370
                 acc->sink.data);
1000
370
      if (!can_precede_at_level(plevel, level))
1001
1
        continue;
1002
370
1003
369
      intermediate_sources(acc, must_rel, j, level);
1004
369
      intermediate_sources(acc, may_rel, j, level);
1005
369
    }
1006
24.8k
1007
28.7k
    for (j = 0; 
j < acc->n_may28.7k
;
++j3.84k
)
{3.84k
1008
3.84k
      int plevel;
1009
3.84k
      isl_map *T;
1010
3.84k
      isl_set *ran;
1011
3.84k
1012
3.84k
      plevel = acc->level_before(acc->source[acc->n_must + j].data,
1013
3.84k
                 acc->sink.data);
1014
3.84k
      if (!can_precede_at_level(plevel, level))
1015
3.28k
        continue;
1016
3.84k
1017
564
      T = all_sources(acc, isl_set_copy(maydo), j, level);
1018
564
      res->dep[2 * acc->n_must + j].map =
1019
564
          isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1020
564
      T = all_sources(acc, isl_set_copy(mustdo), j, level);
1021
564
      ran = isl_map_range(isl_map_copy(T));
1022
564
      res->dep[2 * acc->n_must + j].map =
1023
564
          isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1024
564
      mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1025
564
      maydo = isl_set_union_disjoint(maydo, ran);
1026
564
1027
564
      T = res->dep[2 * acc->n_must + j].map;
1028
564
      T = all_intermediate_sources(acc, T, must_rel, may_rel,
1029
564
              j, level);
1030
564
      res->dep[2 * acc->n_must + j].map = T;
1031
564
    }
1032
24.8k
1033
71.1k
    for (j = acc->n_must - 1; 
j >= 071.1k
;
--j46.2k
)
{46.2k
1034
46.2k
      res->dep[2 * j].map =
1035
46.2k
        isl_map_union_disjoint(res->dep[2 * j].map,
1036
46.2k
                   must_rel[j]);
1037
46.2k
      res->dep[2 * j + 1].map =
1038
46.2k
        isl_map_union_disjoint(res->dep[2 * j + 1].map,
1039
46.2k
                   may_rel[j]);
1040
46.2k
    }
1041
24.8k
1042
24.8k
    if (isl_set_plain_is_empty(mustdo) &&
1043
573
        isl_set_plain_is_empty(maydo))
1044
463
      break;
1045
24.8k
  }
1046
3.12k
1047
3.12k
  free(must_rel);
1048
3.12k
  free(may_rel);
1049
3.12k
done:
1050
3.12k
  res->must_no_source = mustdo;
1051
3.12k
  res->may_no_source = maydo;
1052
3.12k
  return res;
1053
0
error:
1054
0
  isl_flow_free(res);
1055
0
  isl_set_free(mustdo);
1056
0
  isl_set_free(maydo);
1057
0
  free(must_rel);
1058
0
  free(may_rel);
1059
0
  return NULL;
1060
3.12k
}
1061
1062
/* Given a "sink" access, a list of n "source" accesses,
1063
 * compute for each iteration of the sink access
1064
 * and for each element accessed by that iteration,
1065
 * the source access in the list that last accessed the
1066
 * element accessed by the sink access before this sink access.
1067
 * Each access is given as a map from the loop iterators
1068
 * to the array indices.
1069
 * The result is a list of n relations between source and sink
1070
 * iterations and a subset of the domain of the sink access,
1071
 * corresponding to those iterations that access an element
1072
 * not previously accessed.
1073
 *
1074
 * To deal with multi-valued sink access relations, the sink iteration
1075
 * domain is first extended with dimensions that correspond to the data
1076
 * space.  However, these extra dimensions are not projected out again.
1077
 * It is up to the caller to decide whether these dimensions should be kept.
1078
 */
1079
static __isl_give isl_flow *access_info_compute_flow_core(
1080
  __isl_take isl_access_info *acc)
1081
3.40k
{
1082
3.40k
  struct isl_flow *res = NULL;
1083
3.40k
1084
3.40k
  if (!acc)
1085
0
    return NULL;
1086
3.40k
1087
3.40k
  acc->sink.map = isl_map_range_map(acc->sink.map);
1088
3.40k
  if (!acc->sink.map)
1089
0
    goto error;
1090
3.40k
1091
3.40k
  
if (3.40k
acc->n_must == 03.40k
)
1092
283
    res = compute_mem_based_dependences(acc);
1093
3.12k
  else {
1094
3.12k
    acc = isl_access_info_sort_sources(acc);
1095
3.12k
    res = compute_val_based_dependences(acc);
1096
3.12k
  }
1097
3.40k
  acc = isl_access_info_free(acc);
1098
3.40k
  if (!res)
1099
0
    return NULL;
1100
3.40k
  
if (3.40k
!res->must_no_source || 3.40k
!res->may_no_source3.40k
)
1101
0
    goto error;
1102
3.40k
  return res;
1103
0
error:
1104
0
  isl_access_info_free(acc);
1105
0
  isl_flow_free(res);
1106
0
  return NULL;
1107
3.40k
}
1108
1109
/* Given a "sink" access, a list of n "source" accesses,
1110
 * compute for each iteration of the sink access
1111
 * and for each element accessed by that iteration,
1112
 * the source access in the list that last accessed the
1113
 * element accessed by the sink access before this sink access.
1114
 * Each access is given as a map from the loop iterators
1115
 * to the array indices.
1116
 * The result is a list of n relations between source and sink
1117
 * iterations and a subset of the domain of the sink access,
1118
 * corresponding to those iterations that access an element
1119
 * not previously accessed.
1120
 *
1121
 * To deal with multi-valued sink access relations,
1122
 * access_info_compute_flow_core extends the sink iteration domain
1123
 * with dimensions that correspond to the data space.  These extra dimensions
1124
 * are projected out from the result of access_info_compute_flow_core.
1125
 */
1126
__isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1127
6
{
1128
6
  int j;
1129
6
  struct isl_flow *res;
1130
6
1131
6
  if (!acc)
1132
0
    return NULL;
1133
6
1134
6
  acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1135
6
  res = access_info_compute_flow_core(acc);
1136
6
  if (!res)
1137
0
    return NULL;
1138
6
1139
21
  
for (j = 0; 6
j < res->n_source21
;
++j15
)
{15
1140
15
    res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1141
15
    if (!res->dep[j].map)
1142
0
      goto error;
1143
15
  }
1144
6
1145
6
  return res;
1146
0
error:
1147
0
  isl_flow_free(res);
1148
0
  return NULL;
1149
6
}
1150
1151
1152
/* Keep track of some information about a schedule for a given
1153
 * access.  In particular, keep track of which dimensions
1154
 * have a constant value and of the actual constant values.
1155
 */
1156
struct isl_sched_info {
1157
  int *is_cst;
1158
  isl_vec *cst;
1159
};
1160
1161
static void sched_info_free(__isl_take struct isl_sched_info *info)
1162
400
{
1163
400
  if (!info)
1164
0
    return;
1165
400
  isl_vec_free(info->cst);
1166
400
  free(info->is_cst);
1167
400
  free(info);
1168
400
}
1169
1170
/* Extract information on the constant dimensions of the schedule
1171
 * for a given access.  The "map" is of the form
1172
 *
1173
 *  [S -> D] -> A
1174
 *
1175
 * with S the schedule domain, D the iteration domain and A the data domain.
1176
 */
1177
static __isl_give struct isl_sched_info *sched_info_alloc(
1178
  __isl_keep isl_map *map)
1179
400
{
1180
400
  isl_ctx *ctx;
1181
400
  isl_space *dim;
1182
400
  struct isl_sched_info *info;
1183
400
  int i, n;
1184
400
1185
400
  if (!map)
1186
0
    return NULL;
1187
400
1188
400
  dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1189
400
  if (!dim)
1190
0
    return NULL;
1191
400
  n = isl_space_dim(dim, isl_dim_in);
1192
400
  isl_space_free(dim);
1193
400
1194
400
  ctx = isl_map_get_ctx(map);
1195
400
  info = isl_alloc_type(ctx, struct isl_sched_info);
1196
400
  if (!info)
1197
0
    return NULL;
1198
400
  
info->is_cst = 400
isl_alloc_array400
(ctx, int, n);
1199
400
  info->cst = isl_vec_alloc(ctx, n);
1200
400
  if (
n && 400
(!info->is_cst || 400
!info->cst400
))
1201
0
    goto error;
1202
400
1203
1.68k
  
for (i = 0; 400
i < n1.68k
;
++i1.28k
)
{1.28k
1204
1.28k
    isl_val *v;
1205
1.28k
1206
1.28k
    v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1207
1.28k
    if (!v)
1208
0
      goto error;
1209
1.28k
    info->is_cst[i] = !isl_val_is_nan(v);
1210
1.28k
    if (info->is_cst[i])
1211
797
      info->cst = isl_vec_set_element_val(info->cst, i, v);
1212
1.28k
    else
1213
487
      isl_val_free(v);
1214
1.28k
  }
1215
400
1216
400
  return info;
1217
0
error:
1218
0
  sched_info_free(info);
1219
0
  return NULL;
1220
400
}
1221
1222
/* This structure represents the input for a dependence analysis computation.
1223
 *
1224
 * "sink" represents the sink accesses.
1225
 * "must_source" represents the definite source accesses.
1226
 * "may_source" represents the possible source accesses.
1227
 *
1228
 * "schedule" or "schedule_map" represents the execution order.
1229
 * Exactly one of these fields should be NULL.  The other field
1230
 * determines the execution order.
1231
 *
1232
 * The domains of these four maps refer to the same iteration spaces(s).
1233
 * The ranges of the first three maps also refer to the same data space(s).
1234
 *
1235
 * After a call to isl_union_access_info_introduce_schedule,
1236
 * the "schedule_map" field no longer contains useful information.
1237
 */
1238
struct isl_union_access_info {
1239
  isl_union_map *sink;
1240
  isl_union_map *must_source;
1241
  isl_union_map *may_source;
1242
1243
  isl_schedule *schedule;
1244
  isl_union_map *schedule_map;
1245
};
1246
1247
/* Free "access" and return NULL.
1248
 */
1249
__isl_null isl_union_access_info *isl_union_access_info_free(
1250
  __isl_take isl_union_access_info *access)
1251
2.27k
{
1252
2.27k
  if (!access)
1253
55
    return NULL;
1254
2.27k
1255
2.22k
  isl_union_map_free(access->sink);
1256
2.22k
  isl_union_map_free(access->must_source);
1257
2.22k
  isl_union_map_free(access->may_source);
1258
2.22k
  isl_schedule_free(access->schedule);
1259
2.22k
  isl_union_map_free(access->schedule_map);
1260
2.22k
  free(access);
1261
2.22k
1262
2.22k
  return NULL;
1263
2.27k
}
1264
1265
/* Return the isl_ctx to which "access" belongs.
1266
 */
1267
isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1268
2.14k
{
1269
2.14k
  return access ? isl_union_map_get_ctx(access->sink) : NULL;
1270
2.14k
}
1271
1272
/* Create a new isl_union_access_info with the given sink accesses and
1273
 * and no source accesses or schedule information.
1274
 *
1275
 * By default, we use the schedule field of the isl_union_access_info,
1276
 * but this may be overridden by a call
1277
 * to isl_union_access_info_set_schedule_map.
1278
 */
1279
__isl_give isl_union_access_info *isl_union_access_info_from_sink(
1280
  __isl_take isl_union_map *sink)
1281
2.24k
{
1282
2.24k
  isl_ctx *ctx;
1283
2.24k
  isl_space *space;
1284
2.24k
  isl_union_map *empty;
1285
2.24k
  isl_union_access_info *access;
1286
2.24k
1287
2.24k
  if (!sink)
1288
15
    return NULL;
1289
2.22k
  ctx = isl_union_map_get_ctx(sink);
1290
2.22k
  access = isl_alloc_type(ctx, isl_union_access_info);
1291
2.22k
  if (!access)
1292
5
    goto error;
1293
2.22k
1294
2.22k
  space = isl_union_map_get_space(sink);
1295
2.22k
  empty = isl_union_map_empty(isl_space_copy(space));
1296
2.22k
  access->sink = sink;
1297
2.22k
  access->must_source = isl_union_map_copy(empty);
1298
2.22k
  access->may_source = empty;
1299
2.22k
  access->schedule = isl_schedule_empty(space);
1300
2.22k
  access->schedule_map = NULL;
1301
2.22k
1302
2.22k
  if (
!access->sink || 2.22k
!access->must_source2.22k
||
1303
2.22k
      
!access->may_source2.22k
||
!access->schedule2.22k
)
1304
0
    return isl_union_access_info_free(access);
1305
2.22k
1306
2.22k
  return access;
1307
5
error:
1308
5
  isl_union_map_free(sink);
1309
5
  return NULL;
1310
2.22k
}
1311
1312
/* Replace the definite source accesses of "access" by "must_source".
1313
 */
1314
__isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1315
  __isl_take isl_union_access_info *access,
1316
  __isl_take isl_union_map *must_source)
1317
2.22k
{
1318
2.22k
  if (
!access || 2.22k
!must_source2.20k
)
1319
15
    goto error;
1320
2.22k
1321
2.20k
  isl_union_map_free(access->must_source);
1322
2.20k
  access->must_source = must_source;
1323
2.20k
1324
2.20k
  return access;
1325
15
error:
1326
15
  isl_union_access_info_free(access);
1327
15
  isl_union_map_free(must_source);
1328
15
  return NULL;
1329
2.22k
}
1330
1331
/* Replace the possible source accesses of "access" by "may_source".
1332
 */
1333
__isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1334
  __isl_take isl_union_access_info *access,
1335
  __isl_take isl_union_map *may_source)
1336
2.24k
{
1337
2.24k
  if (
!access || 2.24k
!may_source2.22k
)
1338
20
    goto error;
1339
2.24k
1340
2.22k
  isl_union_map_free(access->may_source);
1341
2.22k
  access->may_source = may_source;
1342
2.22k
1343
2.22k
  return access;
1344
20
error:
1345
20
  isl_union_access_info_free(access);
1346
20
  isl_union_map_free(may_source);
1347
20
  return NULL;
1348
2.24k
}
1349
1350
/* Replace the schedule of "access" by "schedule".
1351
 * Also free the schedule_map in case it was set last.
1352
 */
1353
__isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1354
  __isl_take isl_union_access_info *access,
1355
  __isl_take isl_schedule *schedule)
1356
2.16k
{
1357
2.16k
  if (
!access || 2.16k
!schedule2.14k
)
1358
20
    goto error;
1359
2.16k
1360
2.14k
  access->schedule_map = isl_union_map_free(access->schedule_map);
1361
2.14k
  isl_schedule_free(access->schedule);
1362
2.14k
  access->schedule = schedule;
1363
2.14k
1364
2.14k
  return access;
1365
20
error:
1366
20
  isl_union_access_info_free(access);
1367
20
  isl_schedule_free(schedule);
1368
20
  return NULL;
1369
2.16k
}
1370
1371
/* Replace the schedule map of "access" by "schedule_map".
1372
 * Also free the schedule in case it was set last.
1373
 */
1374
__isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1375
  __isl_take isl_union_access_info *access,
1376
  __isl_take isl_union_map *schedule_map)
1377
81
{
1378
81
  if (
!access || 81
!schedule_map81
)
1379
0
    goto error;
1380
81
1381
81
  isl_union_map_free(access->schedule_map);
1382
81
  access->schedule = isl_schedule_free(access->schedule);
1383
81
  access->schedule_map = schedule_map;
1384
81
1385
81
  return access;
1386
0
error:
1387
0
  isl_union_access_info_free(access);
1388
0
  isl_union_map_free(schedule_map);
1389
0
  return NULL;
1390
81
}
1391
1392
__isl_give isl_union_access_info *isl_union_access_info_copy(
1393
  __isl_keep isl_union_access_info *access)
1394
0
{
1395
0
  isl_union_access_info *copy;
1396
0
1397
0
  if (!access)
1398
0
    return NULL;
1399
0
  copy = isl_union_access_info_from_sink(
1400
0
        isl_union_map_copy(access->sink));
1401
0
  copy = isl_union_access_info_set_must_source(copy,
1402
0
        isl_union_map_copy(access->must_source));
1403
0
  copy = isl_union_access_info_set_may_source(copy,
1404
0
        isl_union_map_copy(access->may_source));
1405
0
  if (access->schedule)
1406
0
    copy = isl_union_access_info_set_schedule(copy,
1407
0
        isl_schedule_copy(access->schedule));
1408
0
  else
1409
0
    copy = isl_union_access_info_set_schedule_map(copy,
1410
0
        isl_union_map_copy(access->schedule_map));
1411
0
1412
0
  return copy;
1413
0
}
1414
1415
/* Print a key-value pair of a YAML mapping to "p",
1416
 * with key "name" and value "umap".
1417
 */
1418
static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1419
  const char *name, __isl_keep isl_union_map *umap)
1420
0
{
1421
0
  p = isl_printer_print_str(p, name);
1422
0
  p = isl_printer_yaml_next(p);
1423
0
  p = isl_printer_print_str(p, "\"");
1424
0
  p = isl_printer_print_union_map(p, umap);
1425
0
  p = isl_printer_print_str(p, "\"");
1426
0
  p = isl_printer_yaml_next(p);
1427
0
1428
0
  return p;
1429
0
}
1430
1431
/* Print the information contained in "access" to "p".
1432
 * The information is printed as a YAML document.
1433
 */
1434
__isl_give isl_printer *isl_printer_print_union_access_info(
1435
  __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1436
0
{
1437
0
  if (!access)
1438
0
    return isl_printer_free(p);
1439
0
1440
0
  p = isl_printer_yaml_start_mapping(p);
1441
0
  p = print_union_map_field(p, "sink", access->sink);
1442
0
  p = print_union_map_field(p, "must_source", access->must_source);
1443
0
  p = print_union_map_field(p, "may_source", access->may_source);
1444
0
  if (
access->schedule0
)
{0
1445
0
    p = isl_printer_print_str(p, "schedule");
1446
0
    p = isl_printer_yaml_next(p);
1447
0
    p = isl_printer_print_schedule(p, access->schedule);
1448
0
    p = isl_printer_yaml_next(p);
1449
0
  } else {
1450
0
    p = print_union_map_field(p, "schedule_map",
1451
0
            access->schedule_map);
1452
0
  }
1453
0
  p = isl_printer_yaml_end_mapping(p);
1454
0
1455
0
  return p;
1456
0
}
1457
1458
/* Return a string representation of the information in "access".
1459
 * The information is printed in flow format.
1460
 */
1461
__isl_give char *isl_union_access_info_to_str(
1462
  __isl_keep isl_union_access_info *access)
1463
0
{
1464
0
  isl_printer *p;
1465
0
  char *s;
1466
0
1467
0
  if (!access)
1468
0
    return NULL;
1469
0
1470
0
  p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1471
0
  p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1472
0
  p = isl_printer_print_union_access_info(p, access);
1473
0
  s = isl_printer_get_str(p);
1474
0
  isl_printer_free(p);
1475
0
1476
0
  return s;
1477
0
}
1478
1479
/* Update the fields of "access" such that they all have the same parameters,
1480
 * keeping in mind that the schedule_map field may be NULL and ignoring
1481
 * the schedule field.
1482
 */
1483
static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1484
  __isl_take isl_union_access_info *access)
1485
81
{
1486
81
  isl_space *space;
1487
81
1488
81
  if (!access)
1489
0
    return NULL;
1490
81
1491
81
  space = isl_union_map_get_space(access->sink);
1492
81
  space = isl_space_align_params(space,
1493
81
        isl_union_map_get_space(access->must_source));
1494
81
  space = isl_space_align_params(space,
1495
81
        isl_union_map_get_space(access->may_source));
1496
81
  if (access->schedule_map)
1497
81
    space = isl_space_align_params(space,
1498
81
        isl_union_map_get_space(access->schedule_map));
1499
81
  access->sink = isl_union_map_align_params(access->sink,
1500
81
              isl_space_copy(space));
1501
81
  access->must_source = isl_union_map_align_params(access->must_source,
1502
81
              isl_space_copy(space));
1503
81
  access->may_source = isl_union_map_align_params(access->may_source,
1504
81
              isl_space_copy(space));
1505
81
  if (
!access->schedule_map81
)
{0
1506
0
    isl_space_free(space);
1507
81
  } else {
1508
81
    access->schedule_map =
1509
81
        isl_union_map_align_params(access->schedule_map, space);
1510
81
    if (!access->schedule_map)
1511
0
      return isl_union_access_info_free(access);
1512
81
  }
1513
81
1514
81
  
if (81
!access->sink || 81
!access->must_source81
||
!access->may_source81
)
1515
0
    return isl_union_access_info_free(access);
1516
81
1517
81
  return access;
1518
81
}
1519
1520
/* Prepend the schedule dimensions to the iteration domains.
1521
 *
1522
 * That is, if the schedule is of the form
1523
 *
1524
 *  D -> S
1525
 *
1526
 * while the access relations are of the form
1527
 *
1528
 *  D -> A
1529
 *
1530
 * then the updated access relations are of the form
1531
 *
1532
 *  [S -> D] -> A
1533
 *
1534
 * The schedule map is also replaced by the map
1535
 *
1536
 *  [S -> D] -> D
1537
 *
1538
 * that is used during the internal computation.
1539
 * Neither the original schedule map nor this updated schedule map
1540
 * are used after the call to this function.
1541
 */
1542
static __isl_give isl_union_access_info *
1543
isl_union_access_info_introduce_schedule(
1544
  __isl_take isl_union_access_info *access)
1545
81
{
1546
81
  isl_union_map *sm;
1547
81
1548
81
  if (!access)
1549
0
    return NULL;
1550
81
1551
81
  sm = isl_union_map_reverse(access->schedule_map);
1552
81
  sm = isl_union_map_range_map(sm);
1553
81
  access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1554
81
            access->sink);
1555
81
  access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1556
81
            access->may_source);
1557
81
  access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1558
81
            access->must_source);
1559
81
  access->schedule_map = sm;
1560
81
1561
81
  if (
!access->sink || 81
!access->must_source81
||
1562
81
      
!access->may_source81
||
!access->schedule_map81
)
1563
0
    return isl_union_access_info_free(access);
1564
81
1565
81
  return access;
1566
81
}
1567
1568
/* This structure represents the result of a dependence analysis computation.
1569
 *
1570
 * "must_dep" represents the full definite dependences
1571
 * "may_dep" represents the full non-definite dependences.
1572
 * Both are of the form
1573
 *
1574
 *  [Source] -> [[Sink -> Data]]
1575
 *
1576
 * (after the schedule dimensions have been projected out).
1577
 * "must_no_source" represents the subset of the sink accesses for which
1578
 * definitely no source was found.
1579
 * "may_no_source" represents the subset of the sink accesses for which
1580
 * possibly, but not definitely, no source was found.
1581
 */
1582
struct isl_union_flow {
1583
  isl_union_map *must_dep;
1584
  isl_union_map *may_dep;
1585
  isl_union_map *must_no_source;
1586
  isl_union_map *may_no_source;
1587
};
1588
1589
/* Return the isl_ctx to which "flow" belongs.
1590
 */
1591
isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1592
0
{
1593
0
  return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1594
0
}
1595
1596
/* Free "flow" and return NULL.
1597
 */
1598
__isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1599
2.24k
{
1600
2.24k
  if (!flow)
1601
20
    return NULL;
1602
2.22k
  isl_union_map_free(flow->must_dep);
1603
2.22k
  isl_union_map_free(flow->may_dep);
1604
2.22k
  isl_union_map_free(flow->must_no_source);
1605
2.22k
  isl_union_map_free(flow->may_no_source);
1606
2.22k
  free(flow);
1607
2.22k
  return NULL;
1608
2.24k
}
1609
1610
void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1611
0
{
1612
0
  if (!flow)
1613
0
    return;
1614
0
1615
0
  fprintf(stderr, "must dependences: ");
1616
0
  isl_union_map_dump(flow->must_dep);
1617
0
  fprintf(stderr, "may dependences: ");
1618
0
  isl_union_map_dump(flow->may_dep);
1619
0
  fprintf(stderr, "must no source: ");
1620
0
  isl_union_map_dump(flow->must_no_source);
1621
0
  fprintf(stderr, "may no source: ");
1622
0
  isl_union_map_dump(flow->may_no_source);
1623
0
}
1624
1625
/* Return the full definite dependences in "flow", with accessed elements.
1626
 */
1627
__isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1628
  __isl_keep isl_union_flow *flow)
1629
0
{
1630
0
  if (!flow)
1631
0
    return NULL;
1632
0
  return isl_union_map_copy(flow->must_dep);
1633
0
}
1634
1635
/* Return the full possible dependences in "flow", including the definite
1636
 * dependences, with accessed elements.
1637
 */
1638
__isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1639
  __isl_keep isl_union_flow *flow)
1640
534
{
1641
534
  if (!flow)
1642
5
    return NULL;
1643
529
  return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1644
529
            isl_union_map_copy(flow->may_dep));
1645
534
}
1646
1647
/* Return the definite dependences in "flow", without the accessed elements.
1648
 */
1649
__isl_give isl_union_map *isl_union_flow_get_must_dependence(
1650
  __isl_keep isl_union_flow *flow)
1651
623
{
1652
623
  isl_union_map *dep;
1653
623
1654
623
  if (!flow)
1655
5
    return NULL;
1656
618
  dep = isl_union_map_copy(flow->must_dep);
1657
618
  return isl_union_map_range_factor_domain(dep);
1658
623
}
1659
1660
/* Return the possible dependences in "flow", including the definite
1661
 * dependences, without the accessed elements.
1662
 */
1663
__isl_give isl_union_map *isl_union_flow_get_may_dependence(
1664
  __isl_keep isl_union_flow *flow)
1665
1.08k
{
1666
1.08k
  isl_union_map *dep;
1667
1.08k
1668
1.08k
  if (!flow)
1669
10
    return NULL;
1670
1.07k
  dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1671
1.07k
            isl_union_map_copy(flow->may_dep));
1672
1.07k
  return isl_union_map_range_factor_domain(dep);
1673
1.08k
}
1674
1675
/* Return the non-definite dependences in "flow".
1676
 */
1677
static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1678
  __isl_keep isl_union_flow *flow)
1679
41
{
1680
41
  if (!flow)
1681
0
    return NULL;
1682
41
  return isl_union_map_copy(flow->may_dep);
1683
41
}
1684
1685
/* Return the subset of the sink accesses for which definitely
1686
 * no source was found.
1687
 */
1688
__isl_give isl_union_map *isl_union_flow_get_must_no_source(
1689
  __isl_keep isl_union_flow *flow)
1690
0
{
1691
0
  if (!flow)
1692
0
    return NULL;
1693
0
  return isl_union_map_copy(flow->must_no_source);
1694
0
}
1695
1696
/* Return the subset of the sink accesses for which possibly
1697
 * no source was found, including those for which definitely
1698
 * no source was found.
1699
 */
1700
__isl_give isl_union_map *isl_union_flow_get_may_no_source(
1701
  __isl_keep isl_union_flow *flow)
1702
0
{
1703
0
  if (!flow)
1704
0
    return NULL;
1705
0
  return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1706
0
            isl_union_map_copy(flow->may_no_source));
1707
0
}
1708
1709
/* Return the subset of the sink accesses for which possibly, but not
1710
 * definitely, no source was found.
1711
 */
1712
static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1713
  __isl_keep isl_union_flow *flow)
1714
0
{
1715
0
  if (!flow)
1716
0
    return NULL;
1717
0
  return isl_union_map_copy(flow->may_no_source);
1718
0
}
1719
1720
/* Create a new isl_union_flow object, initialized with empty
1721
 * dependence relations and sink subsets.
1722
 */
1723
static __isl_give isl_union_flow *isl_union_flow_alloc(
1724
  __isl_take isl_space *space)
1725
2.22k
{
1726
2.22k
  isl_ctx *ctx;
1727
2.22k
  isl_union_map *empty;
1728
2.22k
  isl_union_flow *flow;
1729
2.22k
1730
2.22k
  if (!space)
1731
0
    return NULL;
1732
2.22k
  ctx = isl_space_get_ctx(space);
1733
2.22k
  flow = isl_alloc_type(ctx, isl_union_flow);
1734
2.22k
  if (!flow)
1735
0
    goto error;
1736
2.22k
1737
2.22k
  empty = isl_union_map_empty(space);
1738
2.22k
  flow->must_dep = isl_union_map_copy(empty);
1739
2.22k
  flow->may_dep = isl_union_map_copy(empty);
1740
2.22k
  flow->must_no_source = isl_union_map_copy(empty);
1741
2.22k
  flow->may_no_source = empty;
1742
2.22k
1743
2.22k
  if (
!flow->must_dep || 2.22k
!flow->may_dep2.22k
||
1744
2.22k
      
!flow->must_no_source2.22k
||
!flow->may_no_source2.22k
)
1745
0
    return isl_union_flow_free(flow);
1746
2.22k
1747
2.22k
  return flow;
1748
0
error:
1749
0
  isl_space_free(space);
1750
0
  return NULL;
1751
2.22k
}
1752
1753
/* Copy this isl_union_flow object.
1754
 */
1755
__isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
1756
0
{
1757
0
  isl_union_flow *copy;
1758
0
1759
0
  if (!flow)
1760
0
    return NULL;
1761
0
1762
0
  copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
1763
0
1764
0
  if (!copy)
1765
0
    return NULL;
1766
0
1767
0
  copy->must_dep = isl_union_map_union(copy->must_dep,
1768
0
    isl_union_map_copy(flow->must_dep));
1769
0
  copy->may_dep = isl_union_map_union(copy->may_dep,
1770
0
    isl_union_map_copy(flow->may_dep));
1771
0
  copy->must_no_source = isl_union_map_union(copy->must_no_source,
1772
0
    isl_union_map_copy(flow->must_no_source));
1773
0
  copy->may_no_source = isl_union_map_union(copy->may_no_source,
1774
0
    isl_union_map_copy(flow->may_no_source));
1775
0
1776
0
  if (
!copy->must_dep || 0
!copy->may_dep0
||
1777
0
      
!copy->must_no_source0
||
!copy->may_no_source0
)
1778
0
    return isl_union_flow_free(copy);
1779
0
1780
0
  return copy;
1781
0
}
1782
1783
/* Drop the schedule dimensions from the iteration domains in "flow".
1784
 * In particular, the schedule dimensions have been prepended
1785
 * to the iteration domains prior to the dependence analysis by
1786
 * replacing the iteration domain D, by the wrapped map [S -> D].
1787
 * Replace these wrapped maps by the original D.
1788
 *
1789
 * In particular, the dependences computed by access_info_compute_flow_core
1790
 * are of the form
1791
 *
1792
 *  [S -> D] -> [[S' -> D'] -> A]
1793
 *
1794
 * The schedule dimensions are projected out by first currying the range,
1795
 * resulting in
1796
 *
1797
 *  [S -> D] -> [S' -> [D' -> A]]
1798
 *
1799
 * and then computing the factor range
1800
 *
1801
 *  D -> [D' -> A]
1802
 */
1803
static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1804
  __isl_take isl_union_flow *flow)
1805
81
{
1806
81
  if (!flow)
1807
0
    return NULL;
1808
81
1809
81
  flow->must_dep = isl_union_map_range_curry(flow->must_dep);
1810
81
  flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1811
81
  flow->may_dep = isl_union_map_range_curry(flow->may_dep);
1812
81
  flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1813
81
  flow->must_no_source =
1814
81
    isl_union_map_domain_factor_range(flow->must_no_source);
1815
81
  flow->may_no_source =
1816
81
    isl_union_map_domain_factor_range(flow->may_no_source);
1817
81
1818
81
  if (
!flow->must_dep || 81
!flow->may_dep81
||
1819
81
      
!flow->must_no_source81
||
!flow->may_no_source81
)
1820
0
    return isl_union_flow_free(flow);
1821
81
1822
81
  return flow;
1823
81
}
1824
1825
struct isl_compute_flow_data {
1826
  isl_union_map *must_source;
1827
  isl_union_map *may_source;
1828
  isl_union_flow *flow;
1829
1830
  int count;
1831
  int must;
1832
  isl_space *dim;
1833
  struct isl_sched_info *sink_info;
1834
  struct isl_sched_info **source_info;
1835
  isl_access_info *accesses;
1836
};
1837
1838
static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1839
380
{
1840
380
  int eq;
1841
380
  isl_space *dim;
1842
380
  struct isl_compute_flow_data *data;
1843
380
1844
380
  data = (struct isl_compute_flow_data *)user;
1845
380
1846
380
  dim = isl_space_range(isl_map_get_space(map));
1847
380
1848
380
  eq = isl_space_is_equal(dim, data->dim);
1849
380
1850
380
  isl_space_free(dim);
1851
380
  isl_map_free(map);
1852
380
1853
380
  if (eq < 0)
1854
0
    return isl_stat_error;
1855
380
  
if (380
eq380
)
1856
274
    data->count++;
1857
380
1858
380
  return isl_stat_ok;
1859
380
}
1860
1861
static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1862
380
{
1863
380
  int eq;
1864
380
  isl_space *dim;
1865
380
  struct isl_sched_info *info;
1866
380
  struct isl_compute_flow_data *data;
1867
380
1868
380
  data = (struct isl_compute_flow_data *)user;
1869
380
1870
380
  dim = isl_space_range(isl_map_get_space(map));
1871
380
1872
380
  eq = isl_space_is_equal(dim, data->dim);
1873
380
1874
380
  isl_space_free(dim);
1875
380
1876
380
  if (eq < 0)
1877
0
    goto error;
1878
380
  
if (380
!eq380
)
{106
1879
106
    isl_map_free(map);
1880
106
    return isl_stat_ok;
1881
106
  }
1882
380
1883
274
  info = sched_info_alloc(map);
1884
274
  data->source_info[data->count] = info;
1885
274
1886
274
  data->accesses = isl_access_info_add_source(data->accesses,
1887
274
                map, data->must, info);
1888
274
1889
274
  data->count++;
1890
274
1891
274
  return isl_stat_ok;
1892
0
error:
1893
0
  isl_map_free(map);
1894
0
  return isl_stat_error;
1895
380
}
1896
1897
/* Determine the shared nesting level and the "textual order" of
1898
 * the given accesses.
1899
 *
1900
 * We first determine the minimal schedule dimension for both accesses.
1901
 *
1902
 * If among those dimensions, we can find one where both have a fixed
1903
 * value and if moreover those values are different, then the previous
1904
 * dimension is the last shared nesting level and the textual order
1905
 * is determined based on the order of the fixed values.
1906
 * If no such fixed values can be found, then we set the shared
1907
 * nesting level to the minimal schedule dimension, with no textual ordering.
1908
 */
1909
static int before(void *first, void *second)
1910
3.59k
{
1911
3.59k
  struct isl_sched_info *info1 = first;
1912
3.59k
  struct isl_sched_info *info2 = second;
1913
3.59k
  int n1, n2;
1914
3.59k
  int i;
1915
3.59k
1916
3.59k
  n1 = isl_vec_size(info1->cst);
1917
3.59k
  n2 = isl_vec_size(info2->cst);
1918
3.59k
1919
3.59k
  if (n2 < n1)
1920
160
    n1 = n2;
1921
3.59k
1922
10.5k
  for (i = 0; 
i < n110.5k
;
++i6.95k
)
{8.94k
1923
8.94k
    int r;
1924
8.94k
    int cmp;
1925
8.94k
1926
8.94k
    if (!info1->is_cst[i])
1927
3.11k
      continue;
1928
5.82k
    
if (5.82k
!info2->is_cst[i]5.82k
)
1929
0
      continue;
1930
5.82k
    cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1931
5.82k
    if (cmp == 0)
1932
3.83k
      continue;
1933
5.82k
1934
1.98k
    r = 2 * i + (cmp < 0);
1935
1.98k
1936
1.98k
    return r;
1937
5.82k
  }
1938
3.59k
1939
1.61k
  return 2 * n1;
1940
3.59k
}
1941
1942
/* Given a sink access, look for all the source accesses that access
1943
 * the same array and perform dataflow analysis on them using
1944
 * isl_access_info_compute_flow_core.
1945
 */
1946
static isl_stat compute_flow(__isl_take isl_map *map, void *user)
1947
126
{
1948
126
  int i;
1949
126
  isl_ctx *ctx;
1950
126
  struct isl_compute_flow_data *data;
1951
126
  isl_flow *flow;
1952
126
  isl_union_flow *df;
1953
126
1954
126
  data = (struct isl_compute_flow_data *)user;
1955
126
  df = data->flow;
1956
126
1957
126
  ctx = isl_map_get_ctx(map);
1958
126
1959
126
  data->accesses = NULL;
1960
126
  data->sink_info = NULL;
1961
126
  data->source_info = NULL;
1962
126
  data->count = 0;
1963
126
  data->dim = isl_space_range(isl_map_get_space(map));
1964
126
1965
126
  if (isl_union_map_foreach_map(data->must_source,
1966
126
          &count_matching_array, data) < 0)
1967
0
    goto error;
1968
126
  
if (126
isl_union_map_foreach_map(data->may_source,126
1969
126
          &count_matching_array, data) < 0)
1970
0
    goto error;
1971
126
1972
126
  data->sink_info = sched_info_alloc(map);
1973
126
  data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1974
126
               data->count);
1975
126
1976
126
  data->accesses = isl_access_info_alloc(isl_map_copy(map),
1977
126
        data->sink_info, &before, data->count);
1978
126
  if (
!data->sink_info || 126
(data->count && 126
!data->source_info122
) ||
1979
126
      !data->accesses)
1980
0
    goto error;
1981
126
  data->count = 0;
1982
126
  data->must = 1;
1983
126
  if (isl_union_map_foreach_map(data->must_source,
1984
126
          &collect_matching_array, data) < 0)
1985
0
    goto error;
1986
126
  data->must = 0;
1987
126
  if (isl_union_map_foreach_map(data->may_source,
1988
126
          &collect_matching_array, data) < 0)
1989
0
    goto error;
1990
126
1991
126
  flow = access_info_compute_flow_core(data->accesses);
1992
126
  data->accesses = NULL;
1993
126
1994
126
  if (!flow)
1995
0
    goto error;
1996
126
1997
126
  df->must_no_source = isl_union_map_union(df->must_no_source,
1998
126
        isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1999
126
  df->may_no_source = isl_union_map_union(df->may_no_source,
2000
126
        isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2001
126
2002
598
  for (i = 0; 
i < flow->n_source598
;
++i472
)
{472
2003
472
    isl_union_map *dep;
2004
472
    dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2005
472
    if (flow->dep[i].must)
2006
198
      df->must_dep = isl_union_map_union(df->must_dep, dep);
2007
472
    else
2008
274
      df->may_dep = isl_union_map_union(df->may_dep, dep);
2009
472
  }
2010
126
2011
126
  isl_flow_free(flow);
2012
126
2013
126
  sched_info_free(data->sink_info);
2014
126
  if (
data->source_info126
)
{126
2015
400
    for (i = 0; 
i < data->count400
;
++i274
)
2016
274
      sched_info_free(data->source_info[i]);
2017
126
    free(data->source_info);
2018
126
  }
2019
126
  isl_space_free(data->dim);
2020
126
  isl_map_free(map);
2021
126
2022
126
  return isl_stat_ok;
2023
0
error:
2024
0
  isl_access_info_free(data->accesses);
2025
0
  sched_info_free(data->sink_info);
2026
0
  if (
data->source_info0
)
{0
2027
0
    for (i = 0; 
i < data->count0
;
++i0
)
2028
0
      sched_info_free(data->source_info[i]);
2029
0
    free(data->source_info);
2030
0
  }
2031
0
  isl_space_free(data->dim);
2032
0
  isl_map_free(map);
2033
0
2034
0
  return isl_stat_error;
2035
126
}
2036
2037
/* Remove the must accesses from the may accesses.
2038
 *
2039
 * A must access always trumps a may access, so there is no need
2040
 * for a must access to also be considered as a may access.  Doing so
2041
 * would only cost extra computations only to find out that
2042
 * the duplicated may access does not make any difference.
2043
 */
2044
static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2045
  __isl_take isl_union_access_info *access)
2046
2.24k
{
2047
2.24k
  if (!access)
2048
20
    return NULL;
2049
2.22k
  access->may_source = isl_union_map_subtract(access->may_source,
2050
2.22k
            isl_union_map_copy(access->must_source));
2051
2.22k
  if (!access->may_source)
2052
0
    return isl_union_access_info_free(access);
2053
2.22k
2054
2.22k
  return access;
2055
2.22k
}
2056
2057
/* Given a description of the "sink" accesses, the "source" accesses and
2058
 * a schedule, compute for each instance of a sink access
2059
 * and for each element accessed by that instance,
2060
 * the possible or definite source accesses that last accessed the
2061
 * element accessed by the sink access before this sink access
2062
 * in the sense that there is no intermediate definite source access.
2063
 *
2064
 * The must_no_source and may_no_source elements of the result
2065
 * are subsets of access->sink.  The elements must_dep and may_dep
2066
 * map domain elements of access->{may,must)_source to
2067
 * domain elements of access->sink.
2068
 *
2069
 * This function is used when only the schedule map representation
2070
 * is available.
2071
 *
2072
 * We first prepend the schedule dimensions to the domain
2073
 * of the accesses so that we can easily compare their relative order.
2074
 * Then we consider each sink access individually in compute_flow.
2075
 */
2076
static __isl_give isl_union_flow *compute_flow_union_map(
2077
  __isl_take isl_union_access_info *access)
2078
81
{
2079
81
  struct isl_compute_flow_data data;
2080
81
2081
81
  access = isl_union_access_info_align_params(access);
2082
81
  access = isl_union_access_info_introduce_schedule(access);
2083
81
  if (!access)
2084
0
    return NULL;
2085
81
2086
81
  data.must_source = access->must_source;
2087
81
  data.may_source = access->may_source;
2088
81
2089
81
  data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2090
81
2091
81
  if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
2092
0
    goto error;
2093
81
2094
81
  data.flow = isl_union_flow_drop_schedule(data.flow);
2095
81
2096
81
  isl_union_access_info_free(access);
2097
81
  return data.flow;
2098
0
error:
2099
0
  isl_union_access_info_free(access);
2100
0
  isl_union_flow_free(data.flow);
2101
0
  return NULL;
2102
81
}
2103
2104
/* A schedule access relation.
2105
 *
2106
 * The access relation "access" is of the form [S -> D] -> A,
2107
 * where S corresponds to the prefix schedule at "node".
2108
 * "must" is only relevant for source accesses and indicates
2109
 * whether the access is a must source or a may source.
2110
 */
2111
struct isl_scheduled_access {
2112
  isl_map *access;
2113
  int must;
2114
  isl_schedule_node *node;
2115
};
2116
2117
/* Data structure for keeping track of individual scheduled sink and source
2118
 * accesses when computing dependence analysis based on a schedule tree.
2119
 *
2120
 * "n_sink" is the number of used entries in "sink"
2121
 * "n_source" is the number of used entries in "source"
2122
 *
2123
 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2124
 * to keep track of the current node and
2125
 * of what extract_sink_source needs to do.
2126
 */
2127
struct isl_compute_flow_schedule_data {
2128
  isl_union_access_info *access;
2129
2130
  int n_sink;
2131
  int n_source;
2132
2133
  struct isl_scheduled_access *sink;
2134
  struct isl_scheduled_access *source;
2135
2136
  int set_sink;
2137
  int must;
2138
  isl_schedule_node *node;
2139
};
2140
2141
/* Align the parameters of all sinks with all sources.
2142
 *
2143
 * If there are no sinks or no sources, then no alignment is needed.
2144
 */
2145
static void isl_compute_flow_schedule_data_align_params(
2146
  struct isl_compute_flow_schedule_data *data)
2147
2.14k
{
2148
2.14k
  int i;
2149
2.14k
  isl_space *space;
2150
2.14k
2151
2.14k
  if (
data->n_sink == 0 || 2.14k
data->n_source == 01.94k
)
2152
205
    return;
2153
2.14k
2154
1.93k
  space = isl_map_get_space(data->sink[0].access);
2155
1.93k
2156
3.26k
  for (i = 1; 
i < data->n_sink3.26k
;
++i1.33k
)
2157
1.33k
    space = isl_space_align_params(space,
2158
1.33k
        isl_map_get_space(data->sink[i].access));
2159
5.96k
  for (i = 0; 
i < data->n_source5.96k
;
++i4.02k
)
2160
4.02k
    space = isl_space_align_params(space,
2161
4.02k
        isl_map_get_space(data->source[i].access));
2162
1.93k
2163
5.20k
  for (i = 0; 
i < data->n_sink5.20k
;
++i3.26k
)
2164
3.26k
    data->sink[i].access =
2165
3.26k
      isl_map_align_params(data->sink[i].access,
2166
3.26k
              isl_space_copy(space));
2167
5.96k
  for (i = 0; 
i < data->n_source5.96k
;
++i4.02k
)
2168
4.02k
    data->source[i].access =
2169
4.02k
      isl_map_align_params(data->source[i].access,
2170
4.02k
              isl_space_copy(space));
2171
1.93k
2172
1.93k
  isl_space_free(space);
2173
1.93k
}
2174
2175
/* Free all the memory referenced from "data".
2176
 * Do not free "data" itself as it may be allocated on the stack.
2177
 */
2178
static void isl_compute_flow_schedule_data_clear(
2179
  struct isl_compute_flow_schedule_data *data)
2180
2.14k
{
2181
2.14k
  int i;
2182
2.14k
2183
2.14k
  if (!data->sink)
2184
0
    return;
2185
2.14k
2186
5.41k
  
for (i = 0; 2.14k
i < data->n_sink5.41k
;
++i3.27k
)
{3.27k
2187
3.27k
    isl_map_free(data->sink[i].access);
2188
3.27k
    isl_schedule_node_free(data->sink[i].node);
2189
3.27k
  }
2190
2.14k
2191
6.41k
  for (i = 0; 
i < data->n_source6.41k
;
++i4.27k
)
{4.27k
2192
4.27k
    isl_map_free(data->source[i].access);
2193
4.27k
    isl_schedule_node_free(data->source[i].node);
2194
4.27k
  }
2195
2.14k
2196
2.14k
  free(data->sink);
2197
2.14k
}
2198
2199
/* isl_schedule_foreach_schedule_node_top_down callback for counting
2200
 * (an upper bound on) the number of sinks and sources.
2201
 *
2202
 * Sinks and sources are only extracted at leaves of the tree,
2203
 * so we skip the node if it is not a leaf.
2204
 * Otherwise we increment data->n_sink and data->n_source with
2205
 * the number of spaces in the sink and source access domains
2206
 * that reach this node.
2207
 */
2208
static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2209
  void *user)
2210
10.9k
{
2211
10.9k
  struct isl_compute_flow_schedule_data *data = user;
2212
10.9k
  isl_union_set *domain;
2213
10.9k
  isl_union_map *umap;
2214
10.9k
  isl_bool r = isl_bool_false;
2215
10.9k
2216
10.9k
  if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2217
7.71k
    return isl_bool_true;
2218
10.9k
2219
3.22k
  domain = isl_schedule_node_get_universe_domain(node);
2220
3.22k
2221
3.22k
  umap = isl_union_map_copy(data->access->sink);
2222
3.22k
  umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2223
3.22k
  data->n_sink += isl_union_map_n_map(umap);
2224
3.22k
  isl_union_map_free(umap);
2225
3.22k
  if (!umap)
2226
0
    r = isl_bool_error;
2227
3.22k
2228
3.22k
  umap = isl_union_map_copy(data->access->must_source);
2229
3.22k
  umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2230
3.22k
  data->n_source += isl_union_map_n_map(umap);
2231
3.22k
  isl_union_map_free(umap);
2232
3.22k
  if (!umap)
2233
0
    r = isl_bool_error;
2234
3.22k
2235
3.22k
  umap = isl_union_map_copy(data->access->may_source);
2236
3.22k
  umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2237
3.22k
  data->n_source += isl_union_map_n_map(umap);
2238
3.22k
  isl_union_map_free(umap);
2239
3.22k
  if (!umap)
2240
0
    r = isl_bool_error;
2241
3.22k
2242
3.22k
  isl_union_set_free(domain);
2243
3.22k
2244
3.22k
  return r;
2245
10.9k
}
2246
2247
/* Add a single scheduled sink or source (depending on data->set_sink)
2248
 * with scheduled access relation "map", must property data->must and
2249
 * schedule node data->node to the list of sinks or sources.
2250
 */
2251
static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2252
7.55k
{
2253
7.55k
  struct isl_compute_flow_schedule_data *data = user;
2254
7.55k
  struct isl_scheduled_access *access;
2255
7.55k
2256
7.55k
  if (data->set_sink)
2257
3.27k
    access = data->sink + data->n_sink++;
2258
7.55k
  else
2259
4.27k
    access = data->source + data->n_source++;
2260
7.55k
2261
7.55k
  access->access = map;
2262
7.55k
  access->must = data->must;
2263
7.55k
  access->node = isl_schedule_node_copy(data->node);
2264
7.55k
2265
7.55k
  return isl_stat_ok;
2266
7.55k
}
2267
2268
/* isl_schedule_foreach_schedule_node_top_down callback for collecting
2269
 * individual scheduled source and sink accesses (taking into account
2270
 * the domain of the schedule).
2271
 *
2272
 * We only collect accesses at the leaves of the schedule tree.
2273
 * We prepend the schedule dimensions at the leaf to the iteration
2274
 * domains of the source and sink accesses and then extract
2275
 * the individual accesses (per space).
2276
 *
2277
 * In particular, if the prefix schedule at the node is of the form
2278
 *
2279
 *  D -> S
2280
 *
2281
 * while the access relations are of the form
2282
 *
2283
 *  D -> A
2284
 *
2285
 * then the updated access relations are of the form
2286
 *
2287
 *  [S -> D] -> A
2288
 *
2289
 * Note that S consists of a single space such that introducing S
2290
 * in the access relations does not increase the number of spaces.
2291
 */
2292
static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2293
  void *user)
2294
10.9k
{
2295
10.9k
  struct isl_compute_flow_schedule_data *data = user;
2296
10.9k
  isl_union_map *prefix;
2297
10.9k
  isl_union_map *umap;
2298
10.9k
  isl_bool r = isl_bool_false;
2299
10.9k
2300
10.9k
  if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2301
7.71k
    return isl_bool_true;
2302
10.9k
2303
3.22k
  data->node = node;
2304
3.22k
2305
3.22k
  prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2306
3.22k
  prefix = isl_union_map_reverse(prefix);
2307
3.22k
  prefix = isl_union_map_range_map(prefix);
2308
3.22k
2309
3.22k
  data->set_sink = 1;
2310
3.22k
  umap = isl_union_map_copy(data->access->sink);
2311
3.22k
  umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2312
3.22k
  if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2313
0
    r = isl_bool_error;
2314
3.22k
  isl_union_map_free(umap);
2315
3.22k
2316
3.22k
  data->set_sink = 0;
2317
3.22k
  data->must = 1;
2318
3.22k
  umap = isl_union_map_copy(data->access->must_source);
2319
3.22k
  umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2320
3.22k
  if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2321
0
    r = isl_bool_error;
2322
3.22k
  isl_union_map_free(umap);
2323
3.22k
2324
3.22k
  data->set_sink = 0;
2325
3.22k
  data->must = 0;
2326
3.22k
  umap = isl_union_map_copy(data->access->may_source);
2327
3.22k
  umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2328
3.22k
  if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2329
0
    r = isl_bool_error;
2330
3.22k
  isl_union_map_free(umap);
2331
3.22k
2332
3.22k
  isl_union_map_free(prefix);
2333
3.22k
2334
3.22k
  return r;
2335
10.9k
}
2336
2337
/* isl_access_info_compute_flow callback for determining whether
2338
 * the shared nesting level and the ordering within that level
2339
 * for two scheduled accesses for use in compute_single_flow.
2340
 *
2341
 * The tokens passed to this function refer to the leaves
2342
 * in the schedule tree where the accesses take place.
2343
 *
2344
 * If n is the shared number of loops, then we need to return
2345
 * "2 * n + 1" if "first" precedes "second" inside the innermost
2346
 * shared loop and "2 * n" otherwise.
2347
 *
2348
 * The innermost shared ancestor may be the leaves themselves
2349
 * if the accesses take place in the same leaf.  Otherwise,
2350
 * it is either a set node or a sequence node.  Only in the case
2351
 * of a sequence node do we consider one access to precede the other.
2352
 */
2353
static int before_node(void *first, void *second)
2354
52.2k
{
2355
52.2k
  isl_schedule_node *node1 = first;
2356
52.2k
  isl_schedule_node *node2 = second;
2357
52.2k
  isl_schedule_node *shared;
2358
52.2k
  int depth;
2359
52.2k
  int before = 0;
2360
52.2k
2361
52.2k
  shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2362
52.2k
  if (!shared)
2363
0
    return -1;
2364
52.2k
2365
52.2k
  depth = isl_schedule_node_get_schedule_depth(shared);
2366
52.2k
  if (
isl_schedule_node_get_type(shared) == isl_schedule_node_sequence52.2k
)
{28.0k
2367
28.0k
    int pos1, pos2;
2368
28.0k
2369
28.0k
    pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2370
28.0k
                    shared);
2371
28.0k
    pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2372
28.0k
                    shared);
2373
28.0k
    before = pos1 < pos2;
2374
28.0k
  }
2375
52.2k
2376
52.2k
  isl_schedule_node_free(shared);
2377
52.2k
2378
52.2k
  return 2 * depth + before;
2379
52.2k
}
2380
2381
/* Add the scheduled sources from "data" that access
2382
 * the same data space as "sink" to "access".
2383
 */
2384
static __isl_give isl_access_info *add_matching_sources(
2385
  __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2386
  struct isl_compute_flow_schedule_data *data)
2387
3.27k
{
2388
3.27k
  int i;
2389
3.27k
  isl_space *space;
2390
3.27k
2391
3.27k
  space = isl_space_range(isl_map_get_space(sink->access));
2392
13.5k
  for (i = 0; 
i < data->n_source13.5k
;
++i10.2k
)
{10.2k
2393
10.2k
    struct isl_scheduled_access *source;
2394
10.2k
    isl_space *source_space;
2395
10.2k
    int eq;
2396
10.2k
2397
10.2k
    source = &data->source[i];
2398
10.2k
    source_space = isl_map_get_space(source->access);
2399
10.2k
    source_space = isl_space_range(source_space);
2400
10.2k
    eq = isl_space_is_equal(space, source_space);
2401
10.2k
    isl_space_free(source_space);
2402
10.2k
2403
10.2k
    if (!eq)
2404
4.66k
      continue;
2405
5.59k
    
if (5.59k
eq < 05.59k
)
2406
0
      goto error;
2407
5.59k
2408
5.59k
    access = isl_access_info_add_source(access,
2409
5.59k
        isl_map_copy(source->access), source->must, source->node);
2410
5.59k
  }
2411
3.27k
2412
3.27k
  isl_space_free(space);
2413
3.27k
  return access;
2414
0
error:
2415
0
  isl_space_free(space);
2416
0
  isl_access_info_free(access);
2417
0
  return NULL;
2418
3.27k
}
2419
2420
/* Given a scheduled sink access relation "sink", compute the corresponding
2421
 * dependences on the sources in "data" and add the computed dependences
2422
 * to "uf".
2423
 *
2424
 * The dependences computed by access_info_compute_flow_core are of the form
2425
 *
2426
 *  [S -> I] -> [[S' -> I'] -> A]
2427
 *
2428
 * The schedule dimensions are projected out by first currying the range,
2429
 * resulting in
2430
 *
2431
 *  [S -> I] -> [S' -> [I' -> A]]
2432
 *
2433
 * and then computing the factor range
2434
 *
2435
 *  I -> [I' -> A]
2436
 */
2437
static __isl_give isl_union_flow *compute_single_flow(
2438
  __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2439
  struct isl_compute_flow_schedule_data *data)
2440
3.27k
{
2441
3.27k
  int i;
2442
3.27k
  isl_access_info *access;
2443
3.27k
  isl_flow *flow;
2444
3.27k
  isl_map *map;
2445
3.27k
2446
3.27k
  if (!uf)
2447
0
    return NULL;
2448
3.27k
2449
3.27k
  access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2450
3.27k
          &before_node, data->n_source);
2451
3.27k
  access = add_matching_sources(access, sink, data);
2452
3.27k
2453
3.27k
  flow = access_info_compute_flow_core(access);
2454
3.27k
  if (!flow)
2455
0
    return isl_union_flow_free(uf);
2456
3.27k
2457
3.27k
  map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2458
3.27k
  uf->must_no_source = isl_union_map_union(uf->must_no_source,
2459
3.27k
            isl_union_map_from_map(map));
2460
3.27k
  map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2461
3.27k
  uf->may_no_source = isl_union_map_union(uf->may_no_source,
2462
3.27k
            isl_union_map_from_map(map));
2463
3.27k
2464
13.9k
  for (i = 0; 
i < flow->n_source13.9k
;
++i10.6k
)
{10.6k
2465
10.6k
    isl_union_map *dep;
2466
10.6k
2467
10.6k
    map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2468
10.6k
    map = isl_map_factor_range(map);
2469
10.6k
    dep = isl_union_map_from_map(map);
2470
10.6k
    if (flow->dep[i].must)
2471
5.10k
      uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2472
10.6k
    else
2473
5.59k
      uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2474
10.6k
  }
2475
3.27k
2476
3.27k
  isl_flow_free(flow);
2477
3.27k
2478
3.27k
  return uf;
2479
3.27k
}
2480
2481
/* Given a description of the "sink" accesses, the "source" accesses and
2482
 * a schedule, compute for each instance of a sink access
2483
 * and for each element accessed by that instance,
2484
 * the possible or definite source accesses that last accessed the
2485
 * element accessed by the sink access before this sink access
2486
 * in the sense that there is no intermediate definite source access.
2487
 * Only consider dependences between statement instances that belong
2488
 * to the domain of the schedule.
2489
 *
2490
 * The must_no_source and may_no_source elements of the result
2491
 * are subsets of access->sink.  The elements must_dep and may_dep
2492
 * map domain elements of access->{may,must)_source to
2493
 * domain elements of access->sink.
2494
 *
2495
 * This function is used when a schedule tree representation
2496
 * is available.
2497
 *
2498
 * We extract the individual scheduled source and sink access relations
2499
 * (taking into account the domain of the schedule) and
2500
 * then compute dependences for each scheduled sink individually.
2501
 */
2502
static __isl_give isl_union_flow *compute_flow_schedule(
2503
  __isl_take isl_union_access_info *access)
2504
2.14k
{
2505
2.14k
  struct isl_compute_flow_schedule_data data = { access };
2506
2.14k
  int i, n;
2507
2.14k
  isl_ctx *ctx;
2508
2.14k
  isl_union_flow *flow;
2509
2.14k
2510
2.14k
  ctx = isl_union_access_info_get_ctx(access);
2511
2.14k
2512
2.14k
  data.n_sink = 0;
2513
2.14k
  data.n_source = 0;
2514
2.14k
  if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2515
2.14k
            &count_sink_source, &data) < 0)
2516
0
    goto error;
2517
2.14k
2518
2.14k
  n = data.n_sink + data.n_source;
2519
2.14k
  data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2520
2.14k
  if (
n && 2.14k
!data.sink2.14k
)
2521
0
    goto error;
2522
2.14k
  data.source = data.sink + data.n_sink;
2523
2.14k
2524
2.14k
  data.n_sink = 0;
2525
2.14k
  data.n_source = 0;
2526
2.14k
  if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2527
2.14k
              &collect_sink_source, &data) < 0)
2528
0
    goto error;
2529
2.14k
2530
2.14k
  flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2531
2.14k
2532
2.14k
  isl_compute_flow_schedule_data_align_params(&data);
2533
2.14k
2534
5.41k
  for (i = 0; 
i < data.n_sink5.41k
;
++i3.27k
)
2535
3.27k
    flow = compute_single_flow(flow, &data.sink[i], &data);
2536
2.14k
2537
2.14k
  isl_compute_flow_schedule_data_clear(&data);
2538
2.14k
2539
2.14k
  isl_union_access_info_free(access);
2540
2.14k
  return flow;
2541
0
error:
2542
0
  isl_union_access_info_free(access);
2543
0
  isl_compute_flow_schedule_data_clear(&data);
2544
0
  return NULL;
2545
2.14k
}
2546
2547
/* Given a description of the "sink" accesses, the "source" accesses and
2548
 * a schedule, compute for each instance of a sink access
2549
 * and for each element accessed by that instance,
2550
 * the possible or definite source accesses that last accessed the
2551
 * element accessed by the sink access before this sink access
2552
 * in the sense that there is no intermediate definite source access.
2553
 *
2554
 * The must_no_source and may_no_source elements of the result
2555
 * are subsets of access->sink.  The elements must_dep and may_dep
2556
 * map domain elements of access->{may,must)_source to
2557
 * domain elements of access->sink.
2558
 *
2559
 * We check whether the schedule is available as a schedule tree
2560
 * or a schedule map and call the corresponding function to perform
2561
 * the analysis.
2562
 */
2563
__isl_give isl_union_flow *isl_union_access_info_compute_flow(
2564
  __isl_take isl_union_access_info *access)
2565
2.24k
{
2566
2.24k
  access = isl_union_access_info_normalize(access);
2567
2.24k
  if (!access)
2568
20
    return NULL;
2569
2.22k
  
if (2.22k
access->schedule2.22k
)
2570
2.14k
    return compute_flow_schedule(access);
2571
2.22k
  else
2572
81
    return compute_flow_union_map(access);
2573
2.22k
}
2574
2575
/* Print the information contained in "flow" to "p".
2576
 * The information is printed as a YAML document.
2577
 */
2578
__isl_give isl_printer *isl_printer_print_union_flow(
2579
  __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
2580
0
{
2581
0
  isl_union_map *umap;
2582
0
2583
0
  if (!flow)
2584
0
    return isl_printer_free(p);
2585
0
2586
0
  p = isl_printer_yaml_start_mapping(p);
2587
0
  p = print_union_map_field(p, "must_dependence", flow->must_dep);
2588
0
  umap = isl_union_flow_get_may_dependence(flow);
2589
0
  p = print_union_map_field(p, "may_dependence", umap);
2590
0
  isl_union_map_free(umap);
2591
0
  p = print_union_map_field(p, "must_no_source", flow->must_no_source);
2592
0
  umap = isl_union_flow_get_may_no_source(flow);
2593
0
  p = print_union_map_field(p, "may_no_source", umap);
2594
0
  isl_union_map_free(umap);
2595
0
  p = isl_printer_yaml_end_mapping(p);
2596
0
2597
0
  return p;
2598
0
}
2599
2600
/* Return a string representation of the information in "flow".
2601
 * The information is printed in flow format.
2602
 */
2603
__isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
2604
0
{
2605
0
  isl_printer *p;
2606
0
  char *s;
2607
0
2608
0
  if (!flow)
2609
0
    return NULL;
2610
0
2611
0
  p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
2612
0
  p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2613
0
  p = isl_printer_print_union_flow(p, flow);
2614
0
  s = isl_printer_get_str(p);
2615
0
  isl_printer_free(p);
2616
0
2617
0
  return s;
2618
0
}
2619
2620
/* Given a collection of "sink" and "source" accesses,
2621
 * compute for each iteration of a sink access
2622
 * and for each element accessed by that iteration,
2623
 * the source access in the list that last accessed the
2624
 * element accessed by the sink access before this sink access.
2625
 * Each access is given as a map from the loop iterators
2626
 * to the array indices.
2627
 * The result is a relations between source and sink
2628
 * iterations and a subset of the domain of the sink accesses,
2629
 * corresponding to those iterations that access an element
2630
 * not previously accessed.
2631
 *
2632
 * We collect the inputs in an isl_union_access_info object,
2633
 * call isl_union_access_info_compute_flow and extract
2634
 * the outputs from the result.
2635
 */
2636
int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2637
  __isl_take isl_union_map *must_source,
2638
  __isl_take isl_union_map *may_source,
2639
  __isl_take isl_union_map *schedule,
2640
  __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2641
  __isl_give isl_union_map **must_no_source,
2642
  __isl_give isl_union_map **may_no_source)
2643
81
{
2644
81
  isl_union_access_info *access;
2645
81
  isl_union_flow *flow;
2646
81
2647
81
  access = isl_union_access_info_from_sink(sink);
2648
81
  access = isl_union_access_info_set_must_source(access, must_source);
2649
81
  access = isl_union_access_info_set_may_source(access, may_source);
2650
81
  access = isl_union_access_info_set_schedule_map(access, schedule);
2651
81
  flow = isl_union_access_info_compute_flow(access);
2652
81
2653
81
  if (must_dep)
2654
81
    *must_dep = isl_union_flow_get_must_dependence(flow);
2655
81
  if (may_dep)
2656
41
    *may_dep = isl_union_flow_get_non_must_dependence(flow);
2657
81
  if (must_no_source)
2658
0
    *must_no_source = isl_union_flow_get_must_no_source(flow);
2659
81
  if (may_no_source)
2660
0
    *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2661
81
2662
81
  isl_union_flow_free(flow);
2663
81
2664
81
  if (
(must_dep && 81
!*must_dep81
) ||
(may_dep && 81
!*may_dep41
) ||
2665
81
      
(must_no_source && 81
!*must_no_source0
) ||
2666
81
      
(may_no_source && 81
!*may_no_source0
))
2667
0
    goto error;
2668
81
2669
81
  return 0;
2670
0
error:
2671
0
  if (must_dep)
2672
0
    *must_dep = isl_union_map_free(*must_dep);
2673
0
  if (may_dep)
2674
0
    *may_dep = isl_union_map_free(*may_dep);
2675
0
  if (must_no_source)
2676
0
    *must_no_source = isl_union_map_free(*must_no_source);
2677
0
  if (may_no_source)
2678
0
    *may_no_source = isl_union_map_free(*may_no_source);
2679
0
  return -1;
2680
81
}