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.
89 void isl_access_info_free(__isl_take isl_access_info
*acc
)
95 isl_map_free(acc
->sink
.map
);
96 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
97 isl_map_free(acc
->source
[i
].map
);
101 /* Add another source to an isl_access_info structure, making
102 * sure the "must" sources are placed before the "may" sources.
103 * This function may be called at most max_source times on a
104 * given isl_access_info structure, with max_source as specified
105 * in the call to isl_access_info_alloc that constructed the structure.
107 __isl_give isl_access_info
*isl_access_info_add_source(
108 __isl_take isl_access_info
*acc
, __isl_take isl_map
*source
,
109 int must
, void *source_user
)
113 isl_assert(acc
->sink
.map
->ctx
,
114 acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
118 acc
->source
[acc
->n_must
+ acc
->n_may
] =
119 acc
->source
[acc
->n_must
];
120 acc
->source
[acc
->n_must
].map
= source
;
121 acc
->source
[acc
->n_must
].data
= source_user
;
122 acc
->source
[acc
->n_must
].must
= 1;
125 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
126 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
127 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
133 isl_map_free(source
);
134 isl_access_info_free(acc
);
138 /* A temporary structure used while sorting the accesses in an isl_access_info.
140 struct isl_access_sort_info
{
141 struct isl_map
*source_map
;
143 struct isl_access_info
*acc
;
146 /* Return -n, 0 or n (with n a positive value), depending on whether
147 * the source access identified by p1 should be sorted before, together
148 * or after that identified by p2.
150 * If p1 and p2 share a different number of levels with the sink,
151 * then the one with the lowest number of shared levels should be
153 * If they both share no levels, then the order is irrelevant.
154 * Otherwise, if p1 appears before p2, then it should be sorted first.
156 static int access_sort_cmp(const void *p1
, const void *p2
)
158 const struct isl_access_sort_info
*i1
, *i2
;
160 i1
= (const struct isl_access_sort_info
*) p1
;
161 i2
= (const struct isl_access_sort_info
*) p2
;
163 level1
= i1
->acc
->level_before(i1
->source_data
, i1
->acc
->sink
.data
);
164 level2
= i2
->acc
->level_before(i2
->source_data
, i2
->acc
->sink
.data
);
166 if (level1
!= level2
|| !level1
)
167 return level1
- level2
;
169 level1
= i1
->acc
->level_before(i1
->source_data
, i2
->source_data
);
171 return (level1
% 2) ? -1 : 1;
174 /* Sort the must source accesses in order of increasing number of shared
175 * levels with the sink access.
176 * Source accesses with the same number of shared levels are sorted
177 * in their textual order.
179 static __isl_give isl_access_info
*isl_access_info_sort_sources(
180 __isl_take isl_access_info
*acc
)
183 struct isl_access_sort_info
*array
;
187 if (acc
->n_must
<= 1)
190 array
= isl_alloc_array(acc
->sink
.map
->ctx
,
191 struct isl_access_sort_info
, acc
->n_must
);
195 for (i
= 0; i
< acc
->n_must
; ++i
) {
196 array
[i
].source_map
= acc
->source
[i
].map
;
197 array
[i
].source_data
= acc
->source
[i
].data
;
201 qsort(array
, acc
->n_must
, sizeof(struct isl_access_sort_info
),
204 for (i
= 0; i
< acc
->n_must
; ++i
) {
205 acc
->source
[i
].map
= array
[i
].source_map
;
206 acc
->source
[i
].data
= array
[i
].source_data
;
213 isl_access_info_free(acc
);
217 /* Initialize an empty isl_flow structure corresponding to a given
218 * isl_access_info structure.
219 * For each must access, two dependences are created (initialized
220 * to the empty relation), one for the resulting must dependences
221 * and one for the resulting may dependences. May accesses can
222 * only lead to may dependences, so only one dependence is created
224 * This function is private as isl_flow structures are only supposed
225 * to be created by isl_access_info_compute_flow.
227 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
231 struct isl_flow
*dep
;
236 ctx
= acc
->sink
.map
->ctx
;
237 dep
= isl_calloc_type(ctx
, struct isl_flow
);
241 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
,
242 2 * acc
->n_must
+ acc
->n_may
);
246 dep
->n_source
= 2 * acc
->n_must
+ acc
->n_may
;
247 for (i
= 0; i
< acc
->n_must
; ++i
) {
249 dim
= isl_dim_join(isl_map_get_dim(acc
->source
[i
].map
),
250 isl_dim_reverse(isl_map_get_dim(acc
->sink
.map
)));
251 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
252 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
253 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
254 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
255 dep
->dep
[2 * i
].must
= 1;
256 dep
->dep
[2 * i
+ 1].must
= 0;
257 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
260 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
262 dim
= isl_dim_join(isl_map_get_dim(acc
->source
[i
].map
),
263 isl_dim_reverse(isl_map_get_dim(acc
->sink
.map
)));
264 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
265 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
266 dep
->dep
[acc
->n_must
+ i
].must
= 0;
267 if (!dep
->dep
[acc
->n_must
+ i
].map
)
277 /* Iterate over all sources and for each resulting flow dependence
278 * that is not empty, call the user specfied function.
279 * The second argument in this function call identifies the source,
280 * while the third argument correspond to the final argument of
281 * the isl_flow_foreach call.
283 int isl_flow_foreach(__isl_keep isl_flow
*deps
,
284 int (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
, void *user
),
292 for (i
= 0; i
< deps
->n_source
; ++i
) {
293 if (isl_map_fast_is_empty(deps
->dep
[i
].map
))
295 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
296 deps
->dep
[i
].data
, user
) < 0)
303 /* Return a copy of the subset of the sink for which no source could be found.
305 __isl_give isl_set
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
311 return isl_set_copy(deps
->must_no_source
);
313 return isl_set_copy(deps
->may_no_source
);
316 void isl_flow_free(__isl_take isl_flow
*deps
)
322 isl_set_free(deps
->must_no_source
);
323 isl_set_free(deps
->may_no_source
);
325 for (i
= 0; i
< deps
->n_source
; ++i
)
326 isl_map_free(deps
->dep
[i
].map
);
332 /* Return a map that enforces that the domain iteration occurs after
333 * the range iteration at the given level.
334 * If level is odd, then the domain iteration should occur after
335 * the target iteration in their shared level/2 outermost loops.
336 * In this case we simply need to enforce that these outermost
337 * loop iterations are the same.
338 * If level is even, then the loop iterator of the domain should
339 * be greater than the loop iterator of the range at the last
340 * of the level/2 shared loops, i.e., loop level/2 - 1.
342 static __isl_give isl_map
*after_at_level(struct isl_dim
*dim
, int level
)
344 struct isl_basic_map
*bmap
;
347 bmap
= isl_basic_map_equal(dim
, level
/2);
349 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
351 return isl_map_from_basic_map(bmap
);
354 /* Compute the last iteration of must source j that precedes the sink
355 * at the given level for sink iterations in set_C.
356 * The subset of set_C for which no such iteration can be found is returned
359 static struct isl_map
*last_source(struct isl_access_info
*acc
,
360 struct isl_set
*set_C
,
361 int j
, int level
, struct isl_set
**empty
)
363 struct isl_map
*read_map
;
364 struct isl_map
*write_map
;
365 struct isl_map
*dep_map
;
366 struct isl_map
*after
;
367 struct isl_map
*result
;
369 read_map
= isl_map_copy(acc
->sink
.map
);
370 write_map
= isl_map_copy(acc
->source
[j
].map
);
371 write_map
= isl_map_reverse(write_map
);
372 dep_map
= isl_map_apply_range(read_map
, write_map
);
373 after
= after_at_level(isl_map_get_dim(dep_map
), level
);
374 dep_map
= isl_map_intersect(dep_map
, after
);
375 result
= isl_map_partial_lexmax(dep_map
, set_C
, empty
);
376 result
= isl_map_reverse(result
);
381 /* For a given mapping between iterations of must source j and iterations
382 * of the sink, compute the last iteration of must source k preceding
383 * the sink at level before_level for any of the sink iterations,
384 * but following the corresponding iteration of must source j at level
387 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
388 struct isl_map
*old_map
,
389 int j
, int before_level
,
390 int k
, int after_level
,
391 struct isl_set
**empty
)
394 struct isl_set
*set_C
;
395 struct isl_map
*read_map
;
396 struct isl_map
*write_map
;
397 struct isl_map
*dep_map
;
398 struct isl_map
*after_write
;
399 struct isl_map
*before_read
;
400 struct isl_map
*result
;
402 set_C
= isl_map_range(isl_map_copy(old_map
));
403 read_map
= isl_map_copy(acc
->sink
.map
);
404 write_map
= isl_map_copy(acc
->source
[k
].map
);
406 write_map
= isl_map_reverse(write_map
);
407 dep_map
= isl_map_apply_range(read_map
, write_map
);
408 dim
= isl_dim_join(isl_map_get_dim(acc
->source
[k
].map
),
409 isl_dim_reverse(isl_map_get_dim(acc
->source
[j
].map
)));
410 after_write
= after_at_level(dim
, after_level
);
411 after_write
= isl_map_apply_range(after_write
, old_map
);
412 after_write
= isl_map_reverse(after_write
);
413 dep_map
= isl_map_intersect(dep_map
, after_write
);
414 before_read
= after_at_level(isl_map_get_dim(dep_map
), before_level
);
415 dep_map
= isl_map_intersect(dep_map
, before_read
);
416 result
= isl_map_partial_lexmax(dep_map
, set_C
, empty
);
417 result
= isl_map_reverse(result
);
422 /* Given a shared_level between two accesses, return 1 if the
423 * the first can precede the second at the requested target_level.
424 * If the target level is odd, i.e., refers to a statement level
425 * dimension, then first needs to precede second at the requested
426 * level, i.e., shared_level must be equal to target_level.
427 * If the target level is odd, then the two loops should share
428 * at least the requested number of outer loops.
430 static int can_precede_at_level(int shared_level
, int target_level
)
432 if (shared_level
< target_level
)
434 if ((target_level
% 2) && shared_level
> target_level
)
439 /* Given a possible flow dependence temp_rel[j] between source j and the sink
440 * at level sink_level, remove those elements for which
441 * there is an iteration of another source k < j that is closer to the sink.
442 * The flow dependences temp_rel[k] are updated with the improved sources.
443 * Any improved source needs to precede the sink at the same level
444 * and needs to follow source j at the same or a deeper level.
445 * The lower this level, the later the execution date of source k.
446 * We therefore consider lower levels first.
448 * If temp_rel[j] is empty, then there can be no improvement and
449 * we return immediately.
451 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
452 struct isl_map
**temp_rel
, int j
, int sink_level
)
455 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
457 if (isl_map_fast_is_empty(temp_rel
[j
]))
460 for (k
= j
- 1; k
>= 0; --k
) {
462 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
463 if (!can_precede_at_level(plevel
, sink_level
))
466 plevel2
= acc
->level_before(acc
->source
[j
].data
,
467 acc
->source
[k
].data
);
469 for (level
= sink_level
; level
<= depth
; ++level
) {
471 struct isl_set
*trest
;
472 struct isl_map
*copy
;
474 if (!can_precede_at_level(plevel2
, level
))
477 copy
= isl_map_copy(temp_rel
[j
]);
478 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
480 if (isl_map_fast_is_empty(T
)) {
485 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
486 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
493 /* Compute all iterations of may source j that precedes the sink at the given
494 * level for sink iterations in set_C.
496 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
497 __isl_take isl_set
*set_C
, int j
, int level
)
504 read_map
= isl_map_copy(acc
->sink
.map
);
505 read_map
= isl_map_intersect_domain(read_map
, set_C
);
506 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
507 write_map
= isl_map_reverse(write_map
);
508 dep_map
= isl_map_apply_range(read_map
, write_map
);
509 after
= after_at_level(isl_map_get_dim(dep_map
), level
);
510 dep_map
= isl_map_intersect(dep_map
, after
);
512 return isl_map_reverse(dep_map
);
515 /* For a given mapping between iterations of must source k and iterations
516 * of the sink, compute the all iteration of may source j preceding
517 * the sink at level before_level for any of the sink iterations,
518 * but following the corresponding iteration of must source k at level
521 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
522 __isl_keep isl_map
*old_map
,
523 int j
, int before_level
, int k
, int after_level
)
530 isl_map
*after_write
;
531 isl_map
*before_read
;
533 set_C
= isl_map_range(isl_map_copy(old_map
));
534 read_map
= isl_map_copy(acc
->sink
.map
);
535 read_map
= isl_map_intersect_domain(read_map
, set_C
);
536 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
538 write_map
= isl_map_reverse(write_map
);
539 dep_map
= isl_map_apply_range(read_map
, write_map
);
540 dim
= isl_dim_join(isl_map_get_dim(acc
->source
[acc
->n_must
+ j
].map
),
541 isl_dim_reverse(isl_map_get_dim(acc
->source
[k
].map
)));
542 after_write
= after_at_level(dim
, after_level
);
543 after_write
= isl_map_apply_range(after_write
, old_map
);
544 after_write
= isl_map_reverse(after_write
);
545 dep_map
= isl_map_intersect(dep_map
, after_write
);
546 before_read
= after_at_level(isl_map_get_dim(dep_map
), before_level
);
547 dep_map
= isl_map_intersect(dep_map
, before_read
);
548 return isl_map_reverse(dep_map
);
551 /* Given the must and may dependence relations for the must accesses
552 * for level sink_level, check if there are any accesses of may access j
553 * that occur in between and return their union.
554 * If some of these accesses are intermediate with respect to
555 * (previously thought to be) must dependences, then these
556 * must dependences are turned into may dependences.
558 static __isl_give isl_map
*all_intermediate_sources(
559 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
560 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
561 int j
, int sink_level
)
564 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
567 for (k
= 0; k
< acc
->n_must
; ++k
) {
570 if (isl_map_fast_is_empty(may_rel
[k
]) &&
571 isl_map_fast_is_empty(must_rel
[k
]))
574 plevel
= acc
->level_before(acc
->source
[k
].data
,
575 acc
->source
[acc
->n_must
+ j
].data
);
577 for (level
= sink_level
; level
<= depth
; ++level
) {
582 if (!can_precede_at_level(plevel
, level
))
585 copy
= isl_map_copy(may_rel
[k
]);
586 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
587 map
= isl_map_union(map
, T
);
589 copy
= isl_map_copy(must_rel
[k
]);
590 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
591 ran
= isl_map_range(isl_map_copy(T
));
592 map
= isl_map_union(map
, T
);
593 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
594 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
596 T
= isl_map_from_domain_and_range(
598 isl_dim_domain(isl_map_get_dim(must_rel
[k
]))),
600 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
607 /* Compute dependences for the case where all accesses are "may"
608 * accesses, which boils down to computing memory based dependences.
609 * The generic algorithm would also work in this case, but it would
610 * be overkill to use it.
612 static __isl_give isl_flow
*compute_mem_based_dependences(
613 __isl_take isl_access_info
*acc
)
620 res
= isl_flow_alloc(acc
);
624 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
625 maydo
= isl_set_copy(mustdo
);
627 for (i
= 0; i
< acc
->n_may
; ++i
) {
634 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
635 is_before
= plevel
& 1;
638 dim
= isl_map_get_dim(res
->dep
[i
].map
);
640 before
= isl_map_lex_le_first(dim
, plevel
);
642 before
= isl_map_lex_lt_first(dim
, plevel
);
643 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
644 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
645 dep
= isl_map_intersect(dep
, before
);
646 mustdo
= isl_set_subtract(mustdo
,
647 isl_map_range(isl_map_copy(dep
)));
648 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
651 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
652 res
->must_no_source
= mustdo
;
654 isl_access_info_free(acc
);
658 isl_access_info_free(acc
);
662 /* Compute dependences for the case where there is at least one
665 * The core algorithm considers all levels in which a source may precede
666 * the sink, where a level may either be a statement level or a loop level.
667 * The outermost statement level is 1, the first loop level is 2, etc...
668 * The algorithm basically does the following:
669 * for all levels l of the read access from innermost to outermost
670 * for all sources w that may precede the sink access at that level
671 * compute the last iteration of the source that precedes the sink access
673 * add result to possible last accesses at level l of source w
674 * for all sources w2 that we haven't considered yet at this level that may
675 * also precede the sink access
676 * for all levels l2 of w from l to innermost
677 * for all possible last accesses dep of w at l
678 * compute last iteration of w2 between the source and sink
680 * add result to possible last accesses at level l of write w2
681 * and replace possible last accesses dep by the remainder
684 * The above algorithm is applied to the must access. During the course
685 * of the algorithm, we keep track of sink iterations that still
686 * need to be considered. These iterations are split into those that
687 * haven't been matched to any source access (mustdo) and those that have only
688 * been matched to may accesses (maydo).
689 * At the end of each level, we also consider the may accesses.
690 * In particular, we consider may accesses that precede the remaining
691 * sink iterations, moving elements from mustdo to maydo when appropriate,
692 * and may accesses that occur between a must source and a sink of any
693 * dependences found at the current level, turning must dependences into
694 * may dependences when appropriate.
697 static __isl_give isl_flow
*compute_val_based_dependences(
698 __isl_take isl_access_info
*acc
)
702 isl_set
*mustdo
= NULL
;
703 isl_set
*maydo
= NULL
;
706 isl_map
**must_rel
= NULL
;
707 isl_map
**may_rel
= NULL
;
709 acc
= isl_access_info_sort_sources(acc
);
713 res
= isl_flow_alloc(acc
);
716 ctx
= acc
->sink
.map
->ctx
;
718 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
719 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
720 maydo
= isl_set_empty_like(mustdo
);
721 if (!mustdo
|| !maydo
)
723 if (isl_set_fast_is_empty(mustdo
))
726 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
727 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
728 if (!must_rel
|| !may_rel
)
731 for (level
= depth
; level
>= 1; --level
) {
732 for (j
= acc
->n_must
-1; j
>=0; --j
) {
733 must_rel
[j
] = isl_map_empty_like(res
->dep
[j
].map
);
734 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
737 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
739 struct isl_set
*rest
;
742 plevel
= acc
->level_before(acc
->source
[j
].data
,
744 if (!can_precede_at_level(plevel
, level
))
747 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
748 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
751 intermediate_sources(acc
, must_rel
, j
, level
);
753 T
= last_source(acc
, maydo
, j
, level
, &rest
);
754 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
757 intermediate_sources(acc
, may_rel
, j
, level
);
759 if (isl_set_fast_is_empty(mustdo
) &&
760 isl_set_fast_is_empty(maydo
))
763 for (j
= j
- 1; j
>= 0; --j
) {
766 plevel
= acc
->level_before(acc
->source
[j
].data
,
768 if (!can_precede_at_level(plevel
, level
))
771 intermediate_sources(acc
, must_rel
, j
, level
);
772 intermediate_sources(acc
, may_rel
, j
, level
);
775 for (j
= 0; j
< acc
->n_may
; ++j
) {
780 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
782 if (!can_precede_at_level(plevel
, level
))
785 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
786 res
->dep
[2 * acc
->n_must
+ j
].map
=
787 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
788 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
789 ran
= isl_map_range(isl_map_copy(T
));
790 res
->dep
[2 * acc
->n_must
+ j
].map
=
791 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
792 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
793 maydo
= isl_set_union_disjoint(maydo
, ran
);
795 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
796 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
798 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
801 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
802 res
->dep
[2 * j
].map
=
803 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
805 res
->dep
[2 * j
+ 1].map
=
806 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
810 if (isl_set_fast_is_empty(mustdo
) &&
811 isl_set_fast_is_empty(maydo
))
818 res
->must_no_source
= mustdo
;
819 res
->may_no_source
= maydo
;
820 isl_access_info_free(acc
);
823 isl_access_info_free(acc
);
825 isl_set_free(mustdo
);
832 /* Given a "sink" access, a list of n "source" accesses,
833 * compute for each iteration of the sink access
834 * and for each element accessed by that iteration,
835 * the source access in the list that last accessed the
836 * element accessed by the sink access before this sink access.
837 * Each access is given as a map from the loop iterators
838 * to the array indices.
839 * The result is a list of n relations between source and sink
840 * iterations and a subset of the domain of the sink access,
841 * corresponding to those iterations that access an element
842 * not previously accessed.
844 * To deal with multi-valued sink access relations, the sink iteration
845 * domain is first extended with dimensions that correspond to the data
846 * space. After the computation is finished, these extra dimensions are
847 * projected out again.
849 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
852 struct isl_flow
*res
;
853 isl_map
*domain_map
= NULL
;
858 domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
859 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
863 if (acc
->n_must
== 0)
864 res
= compute_mem_based_dependences(acc
);
866 res
= compute_val_based_dependences(acc
);
870 for (j
= 0; j
< res
->n_source
; ++j
) {
871 res
->dep
[j
].map
= isl_map_apply_range(res
->dep
[j
].map
,
872 isl_map_copy(domain_map
));
873 if (!res
->dep
[j
].map
)
876 res
->must_no_source
= isl_set_apply(res
->must_no_source
,
877 isl_map_copy(domain_map
));
878 res
->may_no_source
= isl_set_apply(res
->may_no_source
,
879 isl_map_copy(domain_map
));
880 if (!res
->must_no_source
|| !res
->may_no_source
)
883 isl_map_free(domain_map
);
886 isl_map_free(domain_map
);
887 isl_access_info_free(acc
);
890 isl_map_free(domain_map
);
896 struct isl_compute_flow_data
{
897 isl_union_map
*must_source
;
898 isl_union_map
*may_source
;
899 isl_union_map
*must_dep
;
900 isl_union_map
*may_dep
;
901 isl_union_set
*must_no_source
;
902 isl_union_set
*may_no_source
;
908 isl_dim
**source_dim
;
909 isl_access_info
*accesses
;
912 static int count_matching_array(__isl_take isl_map
*map
, void *user
)
916 struct isl_compute_flow_data
*data
;
918 data
= (struct isl_compute_flow_data
*)user
;
920 dim
= isl_dim_range(isl_map_get_dim(map
));
922 eq
= isl_dim_equal(dim
, data
->dim
);
935 static int collect_matching_array(__isl_take isl_map
*map
, void *user
)
939 struct isl_compute_flow_data
*data
;
941 data
= (struct isl_compute_flow_data
*)user
;
943 dim
= isl_dim_range(isl_map_get_dim(map
));
945 eq
= isl_dim_equal(dim
, data
->dim
);
956 dim
= isl_dim_unwrap(isl_dim_domain(isl_map_get_dim(map
)));
957 data
->source_dim
[data
->count
] = dim
;
959 data
->accesses
= isl_access_info_add_source(data
->accesses
,
960 map
, data
->must
, dim
);
970 static int before(void *first
, void *second
)
972 isl_dim
*dim1
= first
;
973 isl_dim
*dim2
= second
;
976 n1
= isl_dim_size(dim1
, isl_dim_in
);
977 n2
= isl_dim_size(dim2
, isl_dim_in
);
982 return 2 * n1
+ (dim1
< dim2
);
985 /* Given a sink access, look for all the source accesses that access
986 * the same array and perform dataflow analysis on them using
987 * isl_access_info_compute_flow.
989 static int compute_flow(__isl_take isl_map
*map
, void *user
)
993 struct isl_compute_flow_data
*data
;
996 data
= (struct isl_compute_flow_data
*)user
;
998 ctx
= isl_map_get_ctx(map
);
1000 data
->accesses
= NULL
;
1001 data
->sink_dim
= NULL
;
1002 data
->source_dim
= NULL
;
1004 data
->dim
= isl_dim_range(isl_map_get_dim(map
));
1006 if (isl_union_map_foreach_map(data
->must_source
,
1007 &count_matching_array
, data
) < 0)
1009 if (isl_union_map_foreach_map(data
->may_source
,
1010 &count_matching_array
, data
) < 0)
1013 data
->sink_dim
= isl_dim_unwrap(isl_dim_domain(isl_map_get_dim(map
)));
1014 data
->source_dim
= isl_calloc_array(ctx
, isl_dim
*, data
->count
);
1016 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
1017 data
->sink_dim
, &before
, data
->count
);
1020 if (isl_union_map_foreach_map(data
->must_source
,
1021 &collect_matching_array
, data
) < 0)
1024 if (isl_union_map_foreach_map(data
->may_source
,
1025 &collect_matching_array
, data
) < 0)
1028 flow
= isl_access_info_compute_flow(data
->accesses
);
1029 data
->accesses
= NULL
;
1034 data
->must_no_source
= isl_union_set_union(data
->must_no_source
,
1035 isl_union_set_from_set(isl_set_copy(flow
->must_no_source
)));
1036 data
->may_no_source
= isl_union_set_union(data
->may_no_source
,
1037 isl_union_set_from_set(isl_set_copy(flow
->may_no_source
)));
1039 for (i
= 0; i
< flow
->n_source
; ++i
) {
1041 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
1042 if (flow
->dep
[i
].must
)
1043 data
->must_dep
= isl_union_map_union(data
->must_dep
, dep
);
1045 data
->may_dep
= isl_union_map_union(data
->may_dep
, dep
);
1048 isl_flow_free(flow
);
1050 isl_dim_free(data
->sink_dim
);
1051 if (data
->source_dim
) {
1052 for (i
= 0; i
< data
->count
; ++i
)
1053 isl_dim_free(data
->source_dim
[i
]);
1054 free(data
->source_dim
);
1056 isl_dim_free(data
->dim
);
1061 isl_access_info_free(data
->accesses
);
1062 isl_dim_free(data
->sink_dim
);
1063 if (data
->source_dim
) {
1064 for (i
= 0; i
< data
->count
; ++i
)
1065 isl_dim_free(data
->source_dim
[i
]);
1066 free(data
->source_dim
);
1068 isl_dim_free(data
->dim
);
1074 /* Given a collection of "sink" and "source" accesses,
1075 * compute for each iteration of a sink access
1076 * and for each element accessed by that iteration,
1077 * the source access in the list that last accessed the
1078 * element accessed by the sink access before this sink access.
1079 * Each access is given as a map from the loop iterators
1080 * to the array indices.
1081 * The result is a relations between source and sink
1082 * iterations and a subset of the domain of the sink accesses,
1083 * corresponding to those iterations that access an element
1084 * not previously accessed.
1086 * We first prepend the schedule dimensions to the domain
1087 * of the accesses so that we can easily compare their relative order.
1088 * Then we consider each sink access individually in compute_flow.
1090 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
1091 __isl_take isl_union_map
*must_source
,
1092 __isl_take isl_union_map
*may_source
,
1093 __isl_take isl_union_map
*schedule
,
1094 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
1095 __isl_give isl_union_set
**must_no_source
,
1096 __isl_give isl_union_set
**may_no_source
)
1099 isl_union_map
*range_map
= NULL
;
1100 struct isl_compute_flow_data data
;
1102 sink
= isl_union_map_align_params(sink
,
1103 isl_union_map_get_dim(must_source
));
1104 sink
= isl_union_map_align_params(sink
,
1105 isl_union_map_get_dim(may_source
));
1106 sink
= isl_union_map_align_params(sink
,
1107 isl_union_map_get_dim(schedule
));
1108 dim
= isl_union_map_get_dim(sink
);
1109 must_source
= isl_union_map_align_params(must_source
, isl_dim_copy(dim
));
1110 may_source
= isl_union_map_align_params(may_source
, isl_dim_copy(dim
));
1111 schedule
= isl_union_map_align_params(schedule
, isl_dim_copy(dim
));
1113 schedule
= isl_union_map_reverse(schedule
);
1114 range_map
= isl_union_map_range_map(schedule
);
1115 schedule
= isl_union_map_reverse(isl_union_map_copy(range_map
));
1116 sink
= isl_union_map_apply_domain(sink
, isl_union_map_copy(schedule
));
1117 must_source
= isl_union_map_apply_domain(must_source
,
1118 isl_union_map_copy(schedule
));
1119 may_source
= isl_union_map_apply_domain(may_source
, schedule
);
1121 data
.must_source
= must_source
;
1122 data
.may_source
= may_source
;
1123 data
.must_dep
= must_dep
?
1124 isl_union_map_empty(isl_dim_copy(dim
)) : NULL
;
1125 data
.may_dep
= may_dep
? isl_union_map_empty(isl_dim_copy(dim
)) : NULL
;
1126 data
.must_no_source
= must_no_source
?
1127 isl_union_set_empty(isl_dim_copy(dim
)) : NULL
;
1128 data
.may_no_source
= may_no_source
?
1129 isl_union_set_empty(isl_dim_copy(dim
)) : NULL
;
1133 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
1136 isl_union_map_free(sink
);
1137 isl_union_map_free(must_source
);
1138 isl_union_map_free(may_source
);
1141 data
.must_dep
= isl_union_map_apply_domain(data
.must_dep
,
1142 isl_union_map_copy(range_map
));
1143 data
.must_dep
= isl_union_map_apply_range(data
.must_dep
,
1144 isl_union_map_copy(range_map
));
1145 *must_dep
= data
.must_dep
;
1148 data
.may_dep
= isl_union_map_apply_domain(data
.may_dep
,
1149 isl_union_map_copy(range_map
));
1150 data
.may_dep
= isl_union_map_apply_range(data
.may_dep
,
1151 isl_union_map_copy(range_map
));
1152 *may_dep
= data
.may_dep
;
1154 if (must_no_source
) {
1155 data
.must_no_source
= isl_union_set_apply(data
.must_no_source
,
1156 isl_union_map_copy(range_map
));
1157 *must_no_source
= data
.must_no_source
;
1159 if (may_no_source
) {
1160 data
.may_no_source
= isl_union_set_apply(data
.may_no_source
,
1161 isl_union_map_copy(range_map
));
1162 *may_no_source
= data
.may_no_source
;
1165 isl_union_map_free(range_map
);
1169 isl_union_map_free(range_map
);
1170 isl_union_map_free(sink
);
1171 isl_union_map_free(must_source
);
1172 isl_union_map_free(may_source
);
1173 isl_union_map_free(data
.must_dep
);
1174 isl_union_map_free(data
.may_dep
);
1175 isl_union_set_free(data
.must_no_source
);
1176 isl_union_set_free(data
.may_no_source
);
1183 *must_no_source
= NULL
;
1185 *may_no_source
= NULL
;