2 * Copyright 2005-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
6 * Use of this software is governed by the GNU LGPLv2.1 license
8 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
9 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
10 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
11 * B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
18 /* A private structure to keep track of a mapping together with
19 * a user-specified identifier and a boolean indicating whether
20 * the map represents a must or may access/dependence.
22 struct isl_labeled_map
{
28 /* A structure containing the input for dependence analysis:
30 * - n_must + n_may (<= max_source) sources
31 * - a function for determining the relative order of sources and sink
32 * The must sources are placed before the may sources.
34 struct isl_access_info
{
35 struct isl_labeled_map sink
;
36 isl_access_level_before level_before
;
40 struct isl_labeled_map source
[1];
43 /* A structure containing the output of dependence analysis:
44 * - n_source dependences
45 * - a subset of the sink for which definitely no source could be found
46 * - a subset of the sink for which possibly no source could be found
49 isl_set
*must_no_source
;
50 isl_set
*may_no_source
;
52 struct isl_labeled_map
*dep
;
55 /* Construct an isl_access_info structure and fill it up with
56 * the given data. The number of sources is set to 0.
58 __isl_give isl_access_info
*isl_access_info_alloc(__isl_take isl_map
*sink
,
59 void *sink_user
, isl_access_level_before fn
, int max_source
)
61 struct isl_access_info
*acc
;
66 isl_assert(sink
->ctx
, max_source
>= 0, goto error
);
68 acc
= isl_alloc(sink
->ctx
, struct isl_access_info
,
69 sizeof(struct isl_access_info
) +
70 (max_source
- 1) * sizeof(struct isl_labeled_map
));
75 acc
->sink
.data
= sink_user
;
76 acc
->level_before
= fn
;
77 acc
->max_source
= max_source
;
87 /* Free the given isl_access_info structure.
88 * This function is static because the user is expected to call
89 * isl_access_info_compute_flow on any isl_access_info structure
92 static void isl_access_info_free(__isl_take isl_access_info
*acc
)
98 isl_map_free(acc
->sink
.map
);
99 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
100 isl_map_free(acc
->source
[i
].map
);
104 /* Add another source to an isl_access_info structure, making
105 * sure the "must" sources are placed before the "may" sources.
106 * This function may be called at most max_source times on a
107 * given isl_access_info structure, with max_source as specified
108 * in the call to isl_access_info_alloc that constructed the structure.
110 __isl_give isl_access_info
*isl_access_info_add_source(
111 __isl_take isl_access_info
*acc
, __isl_take isl_map
*source
,
112 int must
, void *source_user
)
116 isl_assert(acc
->sink
.map
->ctx
,
117 acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
121 acc
->source
[acc
->n_must
+ acc
->n_may
] =
122 acc
->source
[acc
->n_must
];
123 acc
->source
[acc
->n_must
].map
= source
;
124 acc
->source
[acc
->n_must
].data
= source_user
;
125 acc
->source
[acc
->n_must
].must
= 1;
128 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
129 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
130 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
136 isl_map_free(source
);
137 isl_access_info_free(acc
);
141 /* A temporary structure used while sorting the accesses in an isl_access_info.
143 struct isl_access_sort_info
{
144 struct isl_map
*source_map
;
146 struct isl_access_info
*acc
;
149 /* Return -n, 0 or n (with n a positive value), depending on whether
150 * the source access identified by p1 should be sorted before, together
151 * or after that identified by p2.
153 * If p1 and p2 share a different number of levels with the sink,
154 * then the one with the lowest number of shared levels should be
156 * If they both share no levels, then the order is irrelevant.
157 * Otherwise, if p1 appears before p2, then it should be sorted first.
159 static int access_sort_cmp(const void *p1
, const void *p2
)
161 const struct isl_access_sort_info
*i1
, *i2
;
163 i1
= (const struct isl_access_sort_info
*) p1
;
164 i2
= (const struct isl_access_sort_info
*) p2
;
166 level1
= i1
->acc
->level_before(i1
->source_data
, i1
->acc
->sink
.data
);
167 level2
= i2
->acc
->level_before(i2
->source_data
, i2
->acc
->sink
.data
);
169 if (level1
!= level2
|| !level1
)
170 return level1
- level2
;
172 level1
= i1
->acc
->level_before(i1
->source_data
, i2
->source_data
);
174 return (level1
% 2) ? -1 : 1;
177 /* Sort the must source accesses in order of increasing number of shared
178 * levels with the sink access.
179 * Source accesses with the same number of shared levels are sorted
180 * in their textual order.
182 static __isl_give isl_access_info
*isl_access_info_sort_sources(
183 __isl_take isl_access_info
*acc
)
186 struct isl_access_sort_info
*array
;
190 if (acc
->n_must
<= 1)
193 array
= isl_alloc_array(acc
->sink
.map
->ctx
,
194 struct isl_access_sort_info
, acc
->n_must
);
198 for (i
= 0; i
< acc
->n_must
; ++i
) {
199 array
[i
].source_map
= acc
->source
[i
].map
;
200 array
[i
].source_data
= acc
->source
[i
].data
;
204 qsort(array
, acc
->n_must
, sizeof(struct isl_access_sort_info
),
207 for (i
= 0; i
< acc
->n_must
; ++i
) {
208 acc
->source
[i
].map
= array
[i
].source_map
;
209 acc
->source
[i
].data
= array
[i
].source_data
;
216 isl_access_info_free(acc
);
220 /* Initialize an empty isl_flow structure corresponding to a given
221 * isl_access_info structure.
222 * For each must access, two dependences are created (initialized
223 * to the empty relation), one for the resulting must dependences
224 * and one for the resulting may dependences. May accesses can
225 * only lead to may dependences, so only one dependence is created
227 * This function is private as isl_flow structures are only supposed
228 * to be created by isl_access_info_compute_flow.
230 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
234 struct isl_flow
*dep
;
239 ctx
= acc
->sink
.map
->ctx
;
240 dep
= isl_calloc_type(ctx
, struct isl_flow
);
244 dep
->dep
= isl_alloc_array(ctx
, struct isl_labeled_map
,
245 2 * acc
->n_must
+ acc
->n_may
);
249 dep
->n_source
= 2 * acc
->n_must
+ acc
->n_may
;
250 for (i
= 0; i
< acc
->n_must
; ++i
) {
252 dim
= isl_dim_join(isl_dim_copy(acc
->source
[i
].map
->dim
),
253 isl_dim_reverse(isl_dim_copy(acc
->sink
.map
->dim
)));
254 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
255 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
256 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
257 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
258 dep
->dep
[2 * i
].must
= 1;
259 dep
->dep
[2 * i
+ 1].must
= 0;
261 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
263 dim
= isl_dim_join(isl_dim_copy(acc
->source
[i
].map
->dim
),
264 isl_dim_reverse(isl_dim_copy(acc
->sink
.map
->dim
)));
265 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
266 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
267 dep
->dep
[acc
->n_must
+ i
].must
= 0;
276 /* Iterate over all sources and for each resulting flow dependence
277 * that is not empty, call the user specfied function.
278 * The second argument in this function call identifies the source,
279 * while the third argument correspond to the final argument of
280 * the isl_flow_foreach call.
282 int isl_flow_foreach(__isl_keep isl_flow
*deps
,
283 int (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
, void *user
),
291 for (i
= 0; i
< deps
->n_source
; ++i
) {
292 if (isl_map_fast_is_empty(deps
->dep
[i
].map
))
294 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
295 deps
->dep
[i
].data
, user
) < 0)
302 /* Return a copy of the subset of the sink for which no source could be found.
304 __isl_give isl_set
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
310 return isl_set_copy(deps
->must_no_source
);
312 return isl_set_copy(deps
->may_no_source
);
315 void isl_flow_free(__isl_take isl_flow
*deps
)
321 isl_set_free(deps
->must_no_source
);
322 isl_set_free(deps
->may_no_source
);
324 for (i
= 0; i
< deps
->n_source
; ++i
)
325 isl_map_free(deps
->dep
[i
].map
);
331 /* Return a map that enforces that the domain iteration occurs after
332 * the range iteration at the given level.
333 * If level is odd, then the domain iteration should occur after
334 * the target iteration in their shared level/2 outermost loops.
335 * In this case we simply need to enforce that these outermost
336 * loop iterations are the same.
337 * If level is even, then the loop iterator of the domain should
338 * be greater than the loop iterator of the range at the last
339 * of the level/2 shared loops, i.e., loop level/2 - 1.
341 static __isl_give isl_map
*after_at_level(struct isl_dim
*dim
, int level
)
343 struct isl_basic_map
*bmap
;
346 bmap
= isl_basic_map_equal(dim
, level
/2);
348 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
350 return isl_map_from_basic_map(bmap
);
353 /* Compute the last iteration of must source j that precedes the sink
354 * at the given level for sink iterations in set_C.
355 * The subset of set_C for which no such iteration can be found is returned
358 static struct isl_map
*last_source(struct isl_access_info
*acc
,
359 struct isl_set
*set_C
,
360 int j
, int level
, struct isl_set
**empty
)
362 struct isl_map
*read_map
;
363 struct isl_map
*write_map
;
364 struct isl_map
*dep_map
;
365 struct isl_map
*after
;
366 struct isl_map
*result
;
368 read_map
= isl_map_copy(acc
->sink
.map
);
369 write_map
= isl_map_copy(acc
->source
[j
].map
);
370 write_map
= isl_map_reverse(write_map
);
371 dep_map
= isl_map_apply_range(read_map
, write_map
);
372 after
= after_at_level(isl_dim_copy(dep_map
->dim
), level
);
373 dep_map
= isl_map_intersect(dep_map
, after
);
374 result
= isl_map_partial_lexmax(dep_map
, set_C
, empty
);
375 result
= isl_map_reverse(result
);
380 /* For a given mapping between iterations of must source j and iterations
381 * of the sink, compute the last iteration of must source k preceding
382 * the sink at level before_level for any of the sink iterations,
383 * but following the corresponding iteration of must source j at level
386 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
387 struct isl_map
*old_map
,
388 int j
, int before_level
,
389 int k
, int after_level
,
390 struct isl_set
**empty
)
393 struct isl_set
*set_C
;
394 struct isl_map
*read_map
;
395 struct isl_map
*write_map
;
396 struct isl_map
*dep_map
;
397 struct isl_map
*after_write
;
398 struct isl_map
*before_read
;
399 struct isl_map
*result
;
401 set_C
= isl_map_range(isl_map_copy(old_map
));
402 read_map
= isl_map_copy(acc
->sink
.map
);
403 write_map
= isl_map_copy(acc
->source
[k
].map
);
405 write_map
= isl_map_reverse(write_map
);
406 dep_map
= isl_map_apply_range(read_map
, write_map
);
407 dim
= isl_dim_join(isl_dim_copy(acc
->source
[k
].map
->dim
),
408 isl_dim_reverse(isl_dim_copy(acc
->source
[j
].map
->dim
)));
409 after_write
= after_at_level(dim
, after_level
);
410 after_write
= isl_map_apply_range(after_write
, old_map
);
411 after_write
= isl_map_reverse(after_write
);
412 dep_map
= isl_map_intersect(dep_map
, after_write
);
413 before_read
= after_at_level(isl_dim_copy(dep_map
->dim
), before_level
);
414 dep_map
= isl_map_intersect(dep_map
, before_read
);
415 result
= isl_map_partial_lexmax(dep_map
, set_C
, empty
);
416 result
= isl_map_reverse(result
);
421 /* Given a shared_level between two accesses, return 1 if the
422 * the first can precede the second at the requested target_level.
423 * If the target level is odd, i.e., refers to a statement level
424 * dimension, then first needs to precede second at the requested
425 * level, i.e., shared_level must be equal to target_level.
426 * If the target level is odd, then the two loops should share
427 * at least the requested number of outer loops.
429 static int can_precede_at_level(int shared_level
, int target_level
)
431 if (shared_level
< target_level
)
433 if ((target_level
% 2) && shared_level
> target_level
)
438 /* Given a possible flow dependence temp_rel[j] between source j and the sink
439 * at level sink_level, remove those elements for which
440 * there is an iteration of another source k < j that is closer to the sink.
441 * The flow dependences temp_rel[k] are updated with the improved sources.
442 * Any improved source needs to precede the sink at the same level
443 * and needs to follow source j at the same or a deeper level.
444 * The lower this level, the later the execution date of source k.
445 * We therefore consider lower levels first.
447 * If temp_rel[j] is empty, then there can be no improvement and
448 * we return immediately.
450 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
451 struct isl_map
**temp_rel
, int j
, int sink_level
)
454 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
456 if (isl_map_fast_is_empty(temp_rel
[j
]))
459 for (k
= j
- 1; k
>= 0; --k
) {
461 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
462 if (!can_precede_at_level(plevel
, sink_level
))
465 plevel2
= acc
->level_before(acc
->source
[j
].data
,
466 acc
->source
[k
].data
);
468 for (level
= sink_level
; level
<= depth
; ++level
) {
470 struct isl_set
*trest
;
471 struct isl_map
*copy
;
473 if (!can_precede_at_level(plevel2
, level
))
476 copy
= isl_map_copy(temp_rel
[j
]);
477 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
479 if (isl_map_fast_is_empty(T
)) {
484 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
485 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
492 /* Compute all iterations of may source j that precedes the sink at the given
493 * level for sink iterations in set_C.
495 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
496 __isl_take isl_set
*set_C
, int j
, int level
)
503 read_map
= isl_map_copy(acc
->sink
.map
);
504 read_map
= isl_map_intersect_domain(read_map
, set_C
);
505 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
506 write_map
= isl_map_reverse(write_map
);
507 dep_map
= isl_map_apply_range(read_map
, write_map
);
508 after
= after_at_level(isl_dim_copy(dep_map
->dim
), level
);
509 dep_map
= isl_map_intersect(dep_map
, after
);
511 return isl_map_reverse(dep_map
);
514 /* For a given mapping between iterations of must source k and iterations
515 * of the sink, compute the all iteration of may source j preceding
516 * the sink at level before_level for any of the sink iterations,
517 * but following the corresponding iteration of must source k at level
520 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
521 __isl_keep isl_map
*old_map
,
522 int j
, int before_level
, int k
, int after_level
)
529 isl_map
*after_write
;
530 isl_map
*before_read
;
532 set_C
= isl_map_range(isl_map_copy(old_map
));
533 read_map
= isl_map_copy(acc
->sink
.map
);
534 read_map
= isl_map_intersect_domain(read_map
, set_C
);
535 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
537 write_map
= isl_map_reverse(write_map
);
538 dep_map
= isl_map_apply_range(read_map
, write_map
);
539 dim
= isl_dim_join(isl_dim_copy(acc
->source
[acc
->n_must
+ j
].map
->dim
),
540 isl_dim_reverse(isl_dim_copy(acc
->source
[k
].map
->dim
)));
541 after_write
= after_at_level(dim
, after_level
);
542 after_write
= isl_map_apply_range(after_write
, old_map
);
543 after_write
= isl_map_reverse(after_write
);
544 dep_map
= isl_map_intersect(dep_map
, after_write
);
545 before_read
= after_at_level(isl_dim_copy(dep_map
->dim
), before_level
);
546 dep_map
= isl_map_intersect(dep_map
, before_read
);
547 return isl_map_reverse(dep_map
);
550 /* Given the must and may dependence relations for the must accesses
551 * for level sink_level, check if there are any accesses of may access j
552 * that occur in between and return their union.
553 * If some of these accesses are intermediate with respect to
554 * (previously thought to be) must dependences, then these
555 * must dependences are turned into may dependences.
557 static __isl_give isl_map
*all_intermediate_sources(
558 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
559 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
560 int j
, int sink_level
)
563 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
566 for (k
= 0; k
< acc
->n_must
; ++k
) {
569 if (isl_map_fast_is_empty(may_rel
[k
]) &&
570 isl_map_fast_is_empty(must_rel
[k
]))
573 plevel
= acc
->level_before(acc
->source
[k
].data
,
574 acc
->source
[acc
->n_must
+ j
].data
);
576 for (level
= sink_level
; level
<= depth
; ++level
) {
581 if (!can_precede_at_level(plevel
, level
))
584 copy
= isl_map_copy(may_rel
[k
]);
585 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
586 map
= isl_map_union(map
, T
);
588 copy
= isl_map_copy(must_rel
[k
]);
589 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
590 ran
= isl_map_range(isl_map_copy(T
));
591 map
= isl_map_union(map
, T
);
592 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
593 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
595 T
= isl_map_from_domain_and_range(
597 isl_dim_domain(isl_map_get_dim(must_rel
[k
]))),
599 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
606 /* Compute dependences for the case where all accesses are "may"
607 * accesses, which boils down to computing memory based dependences.
608 * The generic algorithm would also work in this case, but it would
609 * be overkill to use it.
611 static __isl_give isl_flow
*compute_mem_based_dependences(
612 __isl_take isl_access_info
*acc
)
619 res
= isl_flow_alloc(acc
);
623 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
624 maydo
= isl_set_copy(mustdo
);
626 for (i
= 0; i
< acc
->n_may
; ++i
) {
633 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
634 is_before
= plevel
& 1;
637 dim
= isl_map_get_dim(res
->dep
[i
].map
);
639 before
= isl_map_lex_le_first(dim
, plevel
);
641 before
= isl_map_lex_lt_first(dim
, plevel
);
642 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
643 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
644 dep
= isl_map_intersect(dep
, before
);
645 mustdo
= isl_set_subtract(mustdo
,
646 isl_map_range(isl_map_copy(dep
)));
647 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
650 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
651 res
->must_no_source
= mustdo
;
653 isl_access_info_free(acc
);
657 isl_access_info_free(acc
);
661 /* Compute dependences for the case where there is at least one
664 * The core algorithm considers all levels in which a source may precede
665 * the sink, where a level may either be a statement level or a loop level.
666 * The outermost statement level is 1, the first loop level is 2, etc...
667 * The algorithm basically does the following:
668 * for all levels l of the read access from innermost to outermost
669 * for all sources w that may precede the sink access at that level
670 * compute the last iteration of the source that precedes the sink access
672 * add result to possible last accesses at level l of source w
673 * for all sources w2 that we haven't considered yet at this level that may
674 * also precede the sink access
675 * for all levels l2 of w from l to innermost
676 * for all possible last accesses dep of w at l
677 * compute last iteration of w2 between the source and sink
679 * add result to possible last accesses at level l of write w2
680 * and replace possible last accesses dep by the remainder
683 * The above algorithm is applied to the must access. During the course
684 * of the algorithm, we keep track of sink iterations that still
685 * need to be considered. These iterations are split into those that
686 * haven't been matched to any source access (mustdo) and those that have only
687 * been matched to may accesses (maydo).
688 * At the end of each level, we also consider the may accesses.
689 * In particular, we consider may accesses that precede the remaining
690 * sink iterations, moving elements from mustdo to maydo when appropriate,
691 * and may accesses that occur between a must source and a sink of any
692 * dependences found at the current level, turning must dependences into
693 * may dependences when appropriate.
696 static __isl_give isl_flow
*compute_val_based_dependences(
697 __isl_take isl_access_info
*acc
)
708 acc
= isl_access_info_sort_sources(acc
);
712 res
= isl_flow_alloc(acc
);
715 ctx
= acc
->sink
.map
->ctx
;
717 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
718 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
719 maydo
= isl_set_empty_like(mustdo
);
720 if (isl_set_fast_is_empty(mustdo
))
723 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
724 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
726 for (level
= depth
; level
>= 1; --level
) {
727 for (j
= acc
->n_must
-1; j
>=0; --j
) {
728 must_rel
[j
] = isl_map_empty_like(res
->dep
[j
].map
);
729 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
732 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
734 struct isl_set
*rest
;
737 plevel
= acc
->level_before(acc
->source
[j
].data
,
739 if (!can_precede_at_level(plevel
, level
))
742 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
743 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
746 intermediate_sources(acc
, must_rel
, j
, level
);
748 T
= last_source(acc
, maydo
, j
, level
, &rest
);
749 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
752 intermediate_sources(acc
, may_rel
, j
, level
);
754 if (isl_set_fast_is_empty(mustdo
) &&
755 isl_set_fast_is_empty(maydo
))
758 for (j
= j
- 1; j
>= 0; --j
) {
761 plevel
= acc
->level_before(acc
->source
[j
].data
,
763 if (!can_precede_at_level(plevel
, level
))
766 intermediate_sources(acc
, must_rel
, j
, level
);
767 intermediate_sources(acc
, may_rel
, j
, level
);
770 for (j
= 0; j
< acc
->n_may
; ++j
) {
775 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
777 if (!can_precede_at_level(plevel
, level
))
780 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
781 res
->dep
[2 * acc
->n_must
+ j
].map
=
782 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
783 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
784 ran
= isl_map_range(isl_map_copy(T
));
785 res
->dep
[2 * acc
->n_must
+ j
].map
=
786 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
787 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
788 maydo
= isl_set_union_disjoint(maydo
, ran
);
790 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
791 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
793 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
796 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
797 res
->dep
[2 * j
].map
=
798 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
800 res
->dep
[2 * j
+ 1].map
=
801 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
805 if (isl_set_fast_is_empty(mustdo
) &&
806 isl_set_fast_is_empty(maydo
))
813 res
->must_no_source
= mustdo
;
814 res
->may_no_source
= maydo
;
815 isl_access_info_free(acc
);
818 isl_access_info_free(acc
);
822 /* Given a "sink" access, a list of n "source" accesses,
823 * compute for each iteration of the sink access
824 * and for each element accessed by that iteration,
825 * the source access in the list that last accessed the
826 * element accessed by the sink access before this sink access.
827 * Each access is given as a map from the loop iterators
828 * to the array indices.
829 * The result is a list of n relations between source and sink
830 * iterations and a subset of the domain of the sink access,
831 * corresponding to those iterations that access an element
832 * not previously accessed.
834 * To deal with multi-valued sink access relations, the sink iteration
835 * domain is first extended with dimensions that correspond to the data
836 * space. After the computation is finished, these extra dimensions are
837 * projected out again.
839 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
842 struct isl_flow
*res
;
851 n_sink
= isl_map_dim(acc
->sink
.map
, isl_dim_in
);
852 n_data
= isl_map_dim(acc
->sink
.map
, isl_dim_out
);
853 dim
= isl_dim_range(isl_map_get_dim(acc
->sink
.map
));
854 id
= isl_map_identity(dim
);
855 id
= isl_map_insert(id
, isl_dim_in
, 0, n_sink
);
856 acc
->sink
.map
= isl_map_insert(acc
->sink
.map
, isl_dim_in
,
858 acc
->sink
.map
= isl_map_intersect(acc
->sink
.map
, id
);
860 if (acc
->n_must
== 0)
861 res
= compute_mem_based_dependences(acc
);
863 res
= compute_val_based_dependences(acc
);
865 for (j
= 0; j
< res
->n_source
; ++j
)
866 res
->dep
[j
].map
= isl_map_project_out(res
->dep
[j
].map
,
867 isl_dim_out
, n_sink
, n_data
);
868 res
->must_no_source
= isl_set_project_out(res
->must_no_source
, isl_dim_set
, n_sink
, n_data
);
869 res
->may_no_source
= isl_set_project_out(res
->may_no_source
, isl_dim_set
, n_sink
, n_data
);