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
8 * Use of this software is governed by the MIT license
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
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
24 #include <isl/schedule_node.h>
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
34 struct isl_restriction
{
35 enum isl_restriction_type type
;
41 /* Create a restriction of the given type.
43 static __isl_give isl_restriction
*isl_restriction_alloc(
44 __isl_take isl_map
*source_map
, enum isl_restriction_type type
)
47 isl_restriction
*restr
;
52 ctx
= isl_map_get_ctx(source_map
);
53 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
59 isl_map_free(source_map
);
62 isl_map_free(source_map
);
66 /* Create a restriction that doesn't restrict anything.
68 __isl_give isl_restriction
*isl_restriction_none(__isl_take isl_map
*source_map
)
70 return isl_restriction_alloc(source_map
, isl_restriction_type_none
);
73 /* Create a restriction that removes everything.
75 __isl_give isl_restriction
*isl_restriction_empty(
76 __isl_take isl_map
*source_map
)
78 return isl_restriction_alloc(source_map
, isl_restriction_type_empty
);
81 /* Create a restriction on the input of the maximization problem
82 * based on the given source and sink restrictions.
84 __isl_give isl_restriction
*isl_restriction_input(
85 __isl_take isl_set
*source_restr
, __isl_take isl_set
*sink_restr
)
88 isl_restriction
*restr
;
90 if (!source_restr
|| !sink_restr
)
93 ctx
= isl_set_get_ctx(source_restr
);
94 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
98 restr
->type
= isl_restriction_type_input
;
99 restr
->source
= source_restr
;
100 restr
->sink
= sink_restr
;
104 isl_set_free(source_restr
);
105 isl_set_free(sink_restr
);
109 /* Create a restriction on the output of the maximization problem
110 * based on the given source restriction.
112 __isl_give isl_restriction
*isl_restriction_output(
113 __isl_take isl_set
*source_restr
)
116 isl_restriction
*restr
;
121 ctx
= isl_set_get_ctx(source_restr
);
122 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
126 restr
->type
= isl_restriction_type_output
;
127 restr
->source
= source_restr
;
131 isl_set_free(source_restr
);
135 __isl_null isl_restriction
*isl_restriction_free(
136 __isl_take isl_restriction
*restr
)
141 isl_set_free(restr
->source
);
142 isl_set_free(restr
->sink
);
147 isl_ctx
*isl_restriction_get_ctx(__isl_keep isl_restriction
*restr
)
149 return restr
? isl_set_get_ctx(restr
->source
) : NULL
;
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.
156 struct isl_labeled_map
{
162 /* A structure containing the input for dependence analysis:
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.
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.
173 * restrict_fn is a callback that (if not NULL) will be called
174 * right before any lexicographical maximization.
176 struct isl_access_info
{
178 struct isl_labeled_map sink
;
179 isl_access_level_before level_before
;
181 isl_access_restrict restrict_fn
;
187 struct isl_labeled_map source
[1];
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
196 isl_set
*must_no_source
;
197 isl_set
*may_no_source
;
199 struct isl_labeled_map
*dep
;
202 /* Construct an isl_access_info structure and fill it up with
203 * the given data. The number of sources is set to 0.
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
)
209 struct isl_access_info
*acc
;
214 ctx
= isl_map_get_ctx(sink
);
215 isl_assert(ctx
, max_source
>= 0, goto error
);
217 acc
= isl_calloc(ctx
, struct isl_access_info
,
218 sizeof(struct isl_access_info
) +
219 (max_source
- 1) * sizeof(struct isl_labeled_map
));
223 acc
->sink
.map
= sink
;
224 acc
->sink
.data
= sink_user
;
225 acc
->level_before
= fn
;
226 acc
->max_source
= max_source
;
236 /* Free the given isl_access_info structure.
238 __isl_null isl_access_info
*isl_access_info_free(
239 __isl_take isl_access_info
*acc
)
245 isl_map_free(acc
->domain_map
);
246 isl_map_free(acc
->sink
.map
);
247 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
248 isl_map_free(acc
->source
[i
].map
);
253 isl_ctx
*isl_access_info_get_ctx(__isl_keep isl_access_info
*acc
)
255 return acc
? isl_map_get_ctx(acc
->sink
.map
) : NULL
;
258 __isl_give isl_access_info
*isl_access_info_set_restrict(
259 __isl_take isl_access_info
*acc
, isl_access_restrict fn
, void *user
)
263 acc
->restrict_fn
= fn
;
264 acc
->restrict_user
= user
;
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.
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
)
282 ctx
= isl_map_get_ctx(acc
->sink
.map
);
283 isl_assert(ctx
, acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
287 acc
->source
[acc
->n_must
+ acc
->n_may
] =
288 acc
->source
[acc
->n_must
];
289 acc
->source
[acc
->n_must
].map
= source
;
290 acc
->source
[acc
->n_must
].data
= source_user
;
291 acc
->source
[acc
->n_must
].must
= 1;
294 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
295 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
296 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
302 isl_map_free(source
);
303 isl_access_info_free(acc
);
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.
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
320 static int access_sort_cmp(const void *p1
, const void *p2
, void *user
)
322 isl_access_info
*acc
= user
;
323 const struct isl_labeled_map
*i1
, *i2
;
326 i1
= (const struct isl_labeled_map
*) p1
;
327 i2
= (const struct isl_labeled_map
*) p2
;
329 level1
= acc
->level_before(i1
->data
, i2
->data
);
333 level2
= acc
->level_before(i2
->data
, i1
->data
);
337 h1
= isl_map_get_hash(i1
->map
);
338 h2
= isl_map_get_hash(i2
->map
);
339 return h1
> h2
? 1 : h1
< h2
? -1 : 0;
342 /* Sort the must source accesses in their textual order.
344 static __isl_give isl_access_info
*isl_access_info_sort_sources(
345 __isl_take isl_access_info
*acc
)
349 if (acc
->n_must
<= 1)
352 if (isl_sort(acc
->source
, acc
->n_must
, sizeof(struct isl_labeled_map
),
353 access_sort_cmp
, acc
) < 0)
354 return isl_access_info_free(acc
);
359 /* Align the parameters of the two spaces if needed and then call
362 static __isl_give isl_space
*space_align_and_join(__isl_take isl_space
*left
,
363 __isl_take isl_space
*right
)
365 if (isl_space_match(left
, isl_dim_param
, right
, isl_dim_param
))
366 return isl_space_join(left
, right
);
368 left
= isl_space_align_params(left
, isl_space_copy(right
));
369 right
= isl_space_align_params(right
, isl_space_copy(left
));
370 return isl_space_join(left
, right
);
373 /* Initialize an empty isl_flow structure corresponding to a given
374 * isl_access_info structure.
375 * For each must access, two dependences are created (initialized
376 * to the empty relation), one for the resulting must dependences
377 * and one for the resulting may dependences. May accesses can
378 * only lead to may dependences, so only one dependence is created
380 * This function is private as isl_flow structures are only supposed
381 * to be created by isl_access_info_compute_flow.
383 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
387 struct isl_flow
*dep
;
392 ctx
= isl_map_get_ctx(acc
->sink
.map
);
393 dep
= isl_calloc_type(ctx
, struct isl_flow
);
397 n
= 2 * acc
->n_must
+ acc
->n_may
;
398 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
, n
);
403 for (i
= 0; i
< acc
->n_must
; ++i
) {
405 dim
= space_align_and_join(
406 isl_map_get_space(acc
->source
[i
].map
),
407 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
408 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
409 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
410 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
411 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
412 dep
->dep
[2 * i
].must
= 1;
413 dep
->dep
[2 * i
+ 1].must
= 0;
414 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
417 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
419 dim
= space_align_and_join(
420 isl_map_get_space(acc
->source
[i
].map
),
421 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
422 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
423 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
424 dep
->dep
[acc
->n_must
+ i
].must
= 0;
425 if (!dep
->dep
[acc
->n_must
+ i
].map
)
435 /* Iterate over all sources and for each resulting flow dependence
436 * that is not empty, call the user specfied function.
437 * The second argument in this function call identifies the source,
438 * while the third argument correspond to the final argument of
439 * the isl_flow_foreach call.
441 isl_stat
isl_flow_foreach(__isl_keep isl_flow
*deps
,
442 isl_stat (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
,
449 return isl_stat_error
;
451 for (i
= 0; i
< deps
->n_source
; ++i
) {
452 if (isl_map_plain_is_empty(deps
->dep
[i
].map
))
454 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
455 deps
->dep
[i
].data
, user
) < 0)
456 return isl_stat_error
;
462 /* Return a copy of the subset of the sink for which no source could be found.
464 __isl_give isl_map
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
470 return isl_set_unwrap(isl_set_copy(deps
->must_no_source
));
472 return isl_set_unwrap(isl_set_copy(deps
->may_no_source
));
475 void isl_flow_free(__isl_take isl_flow
*deps
)
481 isl_set_free(deps
->must_no_source
);
482 isl_set_free(deps
->may_no_source
);
484 for (i
= 0; i
< deps
->n_source
; ++i
)
485 isl_map_free(deps
->dep
[i
].map
);
491 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
493 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
496 /* Return a map that enforces that the domain iteration occurs after
497 * the range iteration at the given level.
498 * If level is odd, then the domain iteration should occur after
499 * the target iteration in their shared level/2 outermost loops.
500 * In this case we simply need to enforce that these outermost
501 * loop iterations are the same.
502 * If level is even, then the loop iterator of the domain should
503 * be greater than the loop iterator of the range at the last
504 * of the level/2 shared loops, i.e., loop level/2 - 1.
506 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
508 struct isl_basic_map
*bmap
;
511 bmap
= isl_basic_map_equal(dim
, level
/2);
513 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
515 return isl_map_from_basic_map(bmap
);
518 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
519 * but first check if the user has set acc->restrict_fn and if so
520 * update either the input or the output of the maximization problem
521 * with respect to the resulting restriction.
523 * Since the user expects a mapping from sink iterations to source iterations,
524 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
525 * to accessed array elements, we first need to project out the accessed
526 * sink array elements by applying acc->domain_map.
527 * Similarly, the sink restriction specified by the user needs to be
528 * converted back to the wrapped map.
530 static __isl_give isl_map
*restricted_partial_lexmax(
531 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
532 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
535 isl_restriction
*restr
;
536 isl_set
*sink_domain
;
540 if (!acc
->restrict_fn
)
541 return isl_map_partial_lexmax(dep
, sink
, empty
);
543 source_map
= isl_map_copy(dep
);
544 source_map
= isl_map_apply_domain(source_map
,
545 isl_map_copy(acc
->domain_map
));
546 sink_domain
= isl_set_copy(sink
);
547 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
548 restr
= acc
->restrict_fn(source_map
, sink_domain
,
549 acc
->source
[source
].data
, acc
->restrict_user
);
550 isl_set_free(sink_domain
);
551 isl_map_free(source_map
);
555 if (restr
->type
== isl_restriction_type_input
) {
556 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
557 sink_restr
= isl_set_copy(restr
->sink
);
558 sink_restr
= isl_set_apply(sink_restr
,
559 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
560 sink
= isl_set_intersect(sink
, sink_restr
);
561 } else if (restr
->type
== isl_restriction_type_empty
) {
562 isl_space
*space
= isl_map_get_space(dep
);
564 dep
= isl_map_empty(space
);
567 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
569 if (restr
->type
== isl_restriction_type_output
)
570 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
572 isl_restriction_free(restr
);
581 /* Compute the last iteration of must source j that precedes the sink
582 * at the given level for sink iterations in set_C.
583 * The subset of set_C for which no such iteration can be found is returned
586 static struct isl_map
*last_source(struct isl_access_info
*acc
,
587 struct isl_set
*set_C
,
588 int j
, int level
, struct isl_set
**empty
)
590 struct isl_map
*read_map
;
591 struct isl_map
*write_map
;
592 struct isl_map
*dep_map
;
593 struct isl_map
*after
;
594 struct isl_map
*result
;
596 read_map
= isl_map_copy(acc
->sink
.map
);
597 write_map
= isl_map_copy(acc
->source
[j
].map
);
598 write_map
= isl_map_reverse(write_map
);
599 dep_map
= isl_map_apply_range(read_map
, write_map
);
600 after
= after_at_level(isl_map_get_space(dep_map
), level
);
601 dep_map
= isl_map_intersect(dep_map
, after
);
602 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
603 result
= isl_map_reverse(result
);
608 /* For a given mapping between iterations of must source j and iterations
609 * of the sink, compute the last iteration of must source k preceding
610 * the sink at level before_level for any of the sink iterations,
611 * but following the corresponding iteration of must source j at level
614 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
615 struct isl_map
*old_map
,
616 int j
, int before_level
,
617 int k
, int after_level
,
618 struct isl_set
**empty
)
621 struct isl_set
*set_C
;
622 struct isl_map
*read_map
;
623 struct isl_map
*write_map
;
624 struct isl_map
*dep_map
;
625 struct isl_map
*after_write
;
626 struct isl_map
*before_read
;
627 struct isl_map
*result
;
629 set_C
= isl_map_range(isl_map_copy(old_map
));
630 read_map
= isl_map_copy(acc
->sink
.map
);
631 write_map
= isl_map_copy(acc
->source
[k
].map
);
633 write_map
= isl_map_reverse(write_map
);
634 dep_map
= isl_map_apply_range(read_map
, write_map
);
635 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
636 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
637 after_write
= after_at_level(dim
, after_level
);
638 after_write
= isl_map_apply_range(after_write
, old_map
);
639 after_write
= isl_map_reverse(after_write
);
640 dep_map
= isl_map_intersect(dep_map
, after_write
);
641 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
642 dep_map
= isl_map_intersect(dep_map
, before_read
);
643 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
644 result
= isl_map_reverse(result
);
649 /* Given a shared_level between two accesses, return 1 if the
650 * the first can precede the second at the requested target_level.
651 * If the target level is odd, i.e., refers to a statement level
652 * dimension, then first needs to precede second at the requested
653 * level, i.e., shared_level must be equal to target_level.
654 * If the target level is odd, then the two loops should share
655 * at least the requested number of outer loops.
657 static int can_precede_at_level(int shared_level
, int target_level
)
659 if (shared_level
< target_level
)
661 if ((target_level
% 2) && shared_level
> target_level
)
666 /* Given a possible flow dependence temp_rel[j] between source j and the sink
667 * at level sink_level, remove those elements for which
668 * there is an iteration of another source k < j that is closer to the sink.
669 * The flow dependences temp_rel[k] are updated with the improved sources.
670 * Any improved source needs to precede the sink at the same level
671 * and needs to follow source j at the same or a deeper level.
672 * The lower this level, the later the execution date of source k.
673 * We therefore consider lower levels first.
675 * If temp_rel[j] is empty, then there can be no improvement and
676 * we return immediately.
678 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
679 struct isl_map
**temp_rel
, int j
, int sink_level
)
682 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
684 if (isl_map_plain_is_empty(temp_rel
[j
]))
687 for (k
= j
- 1; k
>= 0; --k
) {
689 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
690 if (!can_precede_at_level(plevel
, sink_level
))
693 plevel2
= acc
->level_before(acc
->source
[j
].data
,
694 acc
->source
[k
].data
);
696 for (level
= sink_level
; level
<= depth
; ++level
) {
698 struct isl_set
*trest
;
699 struct isl_map
*copy
;
701 if (!can_precede_at_level(plevel2
, level
))
704 copy
= isl_map_copy(temp_rel
[j
]);
705 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
707 if (isl_map_plain_is_empty(T
)) {
712 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
713 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
720 /* Compute all iterations of may source j that precedes the sink at the given
721 * level for sink iterations in set_C.
723 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
724 __isl_take isl_set
*set_C
, int j
, int level
)
731 read_map
= isl_map_copy(acc
->sink
.map
);
732 read_map
= isl_map_intersect_domain(read_map
, set_C
);
733 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
734 write_map
= isl_map_reverse(write_map
);
735 dep_map
= isl_map_apply_range(read_map
, write_map
);
736 after
= after_at_level(isl_map_get_space(dep_map
), level
);
737 dep_map
= isl_map_intersect(dep_map
, after
);
739 return isl_map_reverse(dep_map
);
742 /* For a given mapping between iterations of must source k and iterations
743 * of the sink, compute the all iteration of may source j preceding
744 * the sink at level before_level for any of the sink iterations,
745 * but following the corresponding iteration of must source k at level
748 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
749 __isl_take isl_map
*old_map
,
750 int j
, int before_level
, int k
, int after_level
)
757 isl_map
*after_write
;
758 isl_map
*before_read
;
760 set_C
= isl_map_range(isl_map_copy(old_map
));
761 read_map
= isl_map_copy(acc
->sink
.map
);
762 read_map
= isl_map_intersect_domain(read_map
, set_C
);
763 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
765 write_map
= isl_map_reverse(write_map
);
766 dep_map
= isl_map_apply_range(read_map
, write_map
);
767 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
768 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
769 after_write
= after_at_level(dim
, after_level
);
770 after_write
= isl_map_apply_range(after_write
, old_map
);
771 after_write
= isl_map_reverse(after_write
);
772 dep_map
= isl_map_intersect(dep_map
, after_write
);
773 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
774 dep_map
= isl_map_intersect(dep_map
, before_read
);
775 return isl_map_reverse(dep_map
);
778 /* Given the must and may dependence relations for the must accesses
779 * for level sink_level, check if there are any accesses of may access j
780 * that occur in between and return their union.
781 * If some of these accesses are intermediate with respect to
782 * (previously thought to be) must dependences, then these
783 * must dependences are turned into may dependences.
785 static __isl_give isl_map
*all_intermediate_sources(
786 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
787 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
788 int j
, int sink_level
)
791 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
794 for (k
= 0; k
< acc
->n_must
; ++k
) {
797 if (isl_map_plain_is_empty(may_rel
[k
]) &&
798 isl_map_plain_is_empty(must_rel
[k
]))
801 plevel
= acc
->level_before(acc
->source
[k
].data
,
802 acc
->source
[acc
->n_must
+ j
].data
);
804 for (level
= sink_level
; level
<= depth
; ++level
) {
809 if (!can_precede_at_level(plevel
, level
))
812 copy
= isl_map_copy(may_rel
[k
]);
813 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
814 map
= isl_map_union(map
, T
);
816 copy
= isl_map_copy(must_rel
[k
]);
817 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
818 ran
= isl_map_range(isl_map_copy(T
));
819 map
= isl_map_union(map
, T
);
820 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
821 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
823 T
= isl_map_from_domain_and_range(
825 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
827 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
834 /* Compute dependences for the case where all accesses are "may"
835 * accesses, which boils down to computing memory based dependences.
836 * The generic algorithm would also work in this case, but it would
837 * be overkill to use it.
839 static __isl_give isl_flow
*compute_mem_based_dependences(
840 __isl_keep isl_access_info
*acc
)
847 res
= isl_flow_alloc(acc
);
851 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
852 maydo
= isl_set_copy(mustdo
);
854 for (i
= 0; i
< acc
->n_may
; ++i
) {
861 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
862 is_before
= plevel
& 1;
865 dim
= isl_map_get_space(res
->dep
[i
].map
);
867 before
= isl_map_lex_le_first(dim
, plevel
);
869 before
= isl_map_lex_lt_first(dim
, plevel
);
870 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
871 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
872 dep
= isl_map_intersect(dep
, before
);
873 mustdo
= isl_set_subtract(mustdo
,
874 isl_map_range(isl_map_copy(dep
)));
875 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
878 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
879 res
->must_no_source
= mustdo
;
884 /* Compute dependences for the case where there is at least one
887 * The core algorithm considers all levels in which a source may precede
888 * the sink, where a level may either be a statement level or a loop level.
889 * The outermost statement level is 1, the first loop level is 2, etc...
890 * The algorithm basically does the following:
891 * for all levels l of the read access from innermost to outermost
892 * for all sources w that may precede the sink access at that level
893 * compute the last iteration of the source that precedes the sink access
895 * add result to possible last accesses at level l of source w
896 * for all sources w2 that we haven't considered yet at this level that may
897 * also precede the sink access
898 * for all levels l2 of w from l to innermost
899 * for all possible last accesses dep of w at l
900 * compute last iteration of w2 between the source and sink
902 * add result to possible last accesses at level l of write w2
903 * and replace possible last accesses dep by the remainder
906 * The above algorithm is applied to the must access. During the course
907 * of the algorithm, we keep track of sink iterations that still
908 * need to be considered. These iterations are split into those that
909 * haven't been matched to any source access (mustdo) and those that have only
910 * been matched to may accesses (maydo).
911 * At the end of each level, we also consider the may accesses.
912 * In particular, we consider may accesses that precede the remaining
913 * sink iterations, moving elements from mustdo to maydo when appropriate,
914 * and may accesses that occur between a must source and a sink of any
915 * dependences found at the current level, turning must dependences into
916 * may dependences when appropriate.
919 static __isl_give isl_flow
*compute_val_based_dependences(
920 __isl_keep isl_access_info
*acc
)
924 isl_set
*mustdo
= NULL
;
925 isl_set
*maydo
= NULL
;
928 isl_map
**must_rel
= NULL
;
929 isl_map
**may_rel
= NULL
;
934 res
= isl_flow_alloc(acc
);
937 ctx
= isl_map_get_ctx(acc
->sink
.map
);
939 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
940 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
941 maydo
= isl_set_empty(isl_set_get_space(mustdo
));
942 if (!mustdo
|| !maydo
)
944 if (isl_set_plain_is_empty(mustdo
))
947 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
948 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
949 if (!must_rel
|| !may_rel
)
952 for (level
= depth
; level
>= 1; --level
) {
953 for (j
= acc
->n_must
-1; j
>=0; --j
) {
955 space
= isl_map_get_space(res
->dep
[2 * j
].map
);
956 must_rel
[j
] = isl_map_empty(space
);
957 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
960 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
962 struct isl_set
*rest
;
965 plevel
= acc
->level_before(acc
->source
[j
].data
,
967 if (!can_precede_at_level(plevel
, level
))
970 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
971 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
974 intermediate_sources(acc
, must_rel
, j
, level
);
976 T
= last_source(acc
, maydo
, j
, level
, &rest
);
977 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
980 intermediate_sources(acc
, may_rel
, j
, level
);
982 if (isl_set_plain_is_empty(mustdo
) &&
983 isl_set_plain_is_empty(maydo
))
986 for (j
= j
- 1; j
>= 0; --j
) {
989 plevel
= acc
->level_before(acc
->source
[j
].data
,
991 if (!can_precede_at_level(plevel
, level
))
994 intermediate_sources(acc
, must_rel
, j
, level
);
995 intermediate_sources(acc
, may_rel
, j
, level
);
998 for (j
= 0; j
< acc
->n_may
; ++j
) {
1003 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1005 if (!can_precede_at_level(plevel
, level
))
1008 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1009 res
->dep
[2 * acc
->n_must
+ j
].map
=
1010 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1011 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1012 ran
= isl_map_range(isl_map_copy(T
));
1013 res
->dep
[2 * acc
->n_must
+ j
].map
=
1014 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1015 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1016 maydo
= isl_set_union_disjoint(maydo
, ran
);
1018 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1019 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1021 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1024 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1025 res
->dep
[2 * j
].map
=
1026 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1028 res
->dep
[2 * j
+ 1].map
=
1029 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1033 if (isl_set_plain_is_empty(mustdo
) &&
1034 isl_set_plain_is_empty(maydo
))
1041 res
->must_no_source
= mustdo
;
1042 res
->may_no_source
= maydo
;
1046 isl_set_free(mustdo
);
1047 isl_set_free(maydo
);
1053 /* Given a "sink" access, a list of n "source" accesses,
1054 * compute for each iteration of the sink access
1055 * and for each element accessed by that iteration,
1056 * the source access in the list that last accessed the
1057 * element accessed by the sink access before this sink access.
1058 * Each access is given as a map from the loop iterators
1059 * to the array indices.
1060 * The result is a list of n relations between source and sink
1061 * iterations and a subset of the domain of the sink access,
1062 * corresponding to those iterations that access an element
1063 * not previously accessed.
1065 * To deal with multi-valued sink access relations, the sink iteration
1066 * domain is first extended with dimensions that correspond to the data
1067 * space. However, these extra dimensions are not projected out again.
1068 * It is up to the caller to decide whether these dimensions should be kept.
1070 static __isl_give isl_flow
*access_info_compute_flow_core(
1071 __isl_take isl_access_info
*acc
)
1073 struct isl_flow
*res
= NULL
;
1078 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1082 if (acc
->n_must
== 0)
1083 res
= compute_mem_based_dependences(acc
);
1085 acc
= isl_access_info_sort_sources(acc
);
1086 res
= compute_val_based_dependences(acc
);
1088 isl_access_info_free(acc
);
1091 if (!res
->must_no_source
|| !res
->may_no_source
)
1099 /* Given a "sink" access, a list of n "source" accesses,
1100 * compute for each iteration of the sink access
1101 * and for each element accessed by that iteration,
1102 * the source access in the list that last accessed the
1103 * element accessed by the sink access before this sink access.
1104 * Each access is given as a map from the loop iterators
1105 * to the array indices.
1106 * The result is a list of n relations between source and sink
1107 * iterations and a subset of the domain of the sink access,
1108 * corresponding to those iterations that access an element
1109 * not previously accessed.
1111 * To deal with multi-valued sink access relations,
1112 * access_info_compute_flow_core extends the sink iteration domain
1113 * with dimensions that correspond to the data space. These extra dimensions
1114 * are projected out from the result of access_info_compute_flow_core.
1116 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1119 struct isl_flow
*res
;
1124 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1125 res
= access_info_compute_flow_core(acc
);
1127 for (j
= 0; j
< res
->n_source
; ++j
) {
1128 res
->dep
[j
].map
= isl_map_range_factor_domain(res
->dep
[j
].map
);
1129 if (!res
->dep
[j
].map
)
1140 /* Keep track of some information about a schedule for a given
1141 * access. In particular, keep track of which dimensions
1142 * have a constant value and of the actual constant values.
1144 struct isl_sched_info
{
1149 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1153 isl_vec_free(info
->cst
);
1158 /* Extract information on the constant dimensions of the schedule
1159 * for a given access. The "map" is of the form
1163 * with S the schedule domain, D the iteration domain and A the data domain.
1165 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1166 __isl_keep isl_map
*map
)
1170 struct isl_sched_info
*info
;
1176 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1179 n
= isl_space_dim(dim
, isl_dim_in
);
1180 isl_space_free(dim
);
1182 ctx
= isl_map_get_ctx(map
);
1183 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1186 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1187 info
->cst
= isl_vec_alloc(ctx
, n
);
1188 if (n
&& (!info
->is_cst
|| !info
->cst
))
1191 for (i
= 0; i
< n
; ++i
) {
1194 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1197 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1198 if (info
->is_cst
[i
])
1199 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1206 sched_info_free(info
);
1210 /* This structure represents the input for a dependence analysis computation.
1212 * "sink" represents the sink accesses.
1213 * "must_source" represents the definite source accesses.
1214 * "may_source" represents the possible source accesses.
1216 * "schedule" or "schedule_map" represents the execution order.
1217 * Exactly one of these fields should be NULL. The other field
1218 * determines the execution order.
1220 * The domains of these four maps refer to the same iteration spaces(s).
1221 * The ranges of the first three maps also refer to the same data space(s).
1223 * After a call to isl_union_access_info_introduce_schedule,
1224 * the "schedule_map" field no longer contains useful information.
1226 struct isl_union_access_info
{
1227 isl_union_map
*sink
;
1228 isl_union_map
*must_source
;
1229 isl_union_map
*may_source
;
1231 isl_schedule
*schedule
;
1232 isl_union_map
*schedule_map
;
1235 /* Free "access" and return NULL.
1237 __isl_null isl_union_access_info
*isl_union_access_info_free(
1238 __isl_take isl_union_access_info
*access
)
1243 isl_union_map_free(access
->sink
);
1244 isl_union_map_free(access
->must_source
);
1245 isl_union_map_free(access
->may_source
);
1246 isl_schedule_free(access
->schedule
);
1247 isl_union_map_free(access
->schedule_map
);
1253 /* Return the isl_ctx to which "access" belongs.
1255 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1257 return access
? isl_union_map_get_ctx(access
->sink
) : NULL
;
1260 /* Create a new isl_union_access_info with the given sink accesses and
1261 * and no source accesses or schedule information.
1263 * By default, we use the schedule field of the isl_union_access_info,
1264 * but this may be overridden by a call
1265 * to isl_union_access_info_set_schedule_map.
1267 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1268 __isl_take isl_union_map
*sink
)
1272 isl_union_map
*empty
;
1273 isl_union_access_info
*access
;
1277 ctx
= isl_union_map_get_ctx(sink
);
1278 access
= isl_alloc_type(ctx
, isl_union_access_info
);
1282 space
= isl_union_map_get_space(sink
);
1283 empty
= isl_union_map_empty(isl_space_copy(space
));
1284 access
->sink
= sink
;
1285 access
->must_source
= isl_union_map_copy(empty
);
1286 access
->may_source
= empty
;
1287 access
->schedule
= isl_schedule_empty(space
);
1288 access
->schedule_map
= NULL
;
1290 if (!access
->sink
|| !access
->must_source
||
1291 !access
->may_source
|| !access
->schedule
)
1292 return isl_union_access_info_free(access
);
1296 isl_union_map_free(sink
);
1300 /* Replace the definite source accesses of "access" by "must_source".
1302 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1303 __isl_take isl_union_access_info
*access
,
1304 __isl_take isl_union_map
*must_source
)
1306 if (!access
|| !must_source
)
1309 isl_union_map_free(access
->must_source
);
1310 access
->must_source
= must_source
;
1314 isl_union_access_info_free(access
);
1315 isl_union_map_free(must_source
);
1319 /* Replace the possible source accesses of "access" by "may_source".
1321 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1322 __isl_take isl_union_access_info
*access
,
1323 __isl_take isl_union_map
*may_source
)
1325 if (!access
|| !may_source
)
1328 isl_union_map_free(access
->may_source
);
1329 access
->may_source
= may_source
;
1333 isl_union_access_info_free(access
);
1334 isl_union_map_free(may_source
);
1338 /* Replace the schedule of "access" by "schedule".
1339 * Also free the schedule_map in case it was set last.
1341 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1342 __isl_take isl_union_access_info
*access
,
1343 __isl_take isl_schedule
*schedule
)
1345 if (!access
|| !schedule
)
1348 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1349 isl_schedule_free(access
->schedule
);
1350 access
->schedule
= schedule
;
1354 isl_union_access_info_free(access
);
1355 isl_schedule_free(schedule
);
1359 /* Replace the schedule map of "access" by "schedule_map".
1360 * Also free the schedule in case it was set last.
1362 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1363 __isl_take isl_union_access_info
*access
,
1364 __isl_take isl_union_map
*schedule_map
)
1366 if (!access
|| !schedule_map
)
1369 isl_union_map_free(access
->schedule_map
);
1370 access
->schedule
= isl_schedule_free(access
->schedule
);
1371 access
->schedule_map
= schedule_map
;
1375 isl_union_access_info_free(access
);
1376 isl_union_map_free(schedule_map
);
1380 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1381 __isl_keep isl_union_access_info
*access
)
1383 isl_union_access_info
*copy
;
1387 copy
= isl_union_access_info_from_sink(
1388 isl_union_map_copy(access
->sink
));
1389 copy
= isl_union_access_info_set_must_source(copy
,
1390 isl_union_map_copy(access
->must_source
));
1391 copy
= isl_union_access_info_set_may_source(copy
,
1392 isl_union_map_copy(access
->may_source
));
1393 if (access
->schedule
)
1394 copy
= isl_union_access_info_set_schedule(copy
,
1395 isl_schedule_copy(access
->schedule
));
1397 copy
= isl_union_access_info_set_schedule_map(copy
,
1398 isl_union_map_copy(access
->schedule_map
));
1403 /* Update the fields of "access" such that they all have the same parameters,
1404 * keeping in mind that the schedule_map field may be NULL and ignoring
1405 * the schedule field.
1407 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1408 __isl_take isl_union_access_info
*access
)
1415 space
= isl_union_map_get_space(access
->sink
);
1416 space
= isl_space_align_params(space
,
1417 isl_union_map_get_space(access
->must_source
));
1418 space
= isl_space_align_params(space
,
1419 isl_union_map_get_space(access
->may_source
));
1420 if (access
->schedule_map
)
1421 space
= isl_space_align_params(space
,
1422 isl_union_map_get_space(access
->schedule_map
));
1423 access
->sink
= isl_union_map_align_params(access
->sink
,
1424 isl_space_copy(space
));
1425 access
->must_source
= isl_union_map_align_params(access
->must_source
,
1426 isl_space_copy(space
));
1427 access
->may_source
= isl_union_map_align_params(access
->may_source
,
1428 isl_space_copy(space
));
1429 if (!access
->schedule_map
) {
1430 isl_space_free(space
);
1432 access
->schedule_map
=
1433 isl_union_map_align_params(access
->schedule_map
, space
);
1434 if (!access
->schedule_map
)
1435 return isl_union_access_info_free(access
);
1438 if (!access
->sink
|| !access
->must_source
|| !access
->may_source
)
1439 return isl_union_access_info_free(access
);
1444 /* Prepend the schedule dimensions to the iteration domains.
1446 * That is, if the schedule is of the form
1450 * while the access relations are of the form
1454 * then the updated access relations are of the form
1458 * The schedule map is also replaced by the map
1462 * that is used during the internal computation.
1463 * Neither the original schedule map nor this updated schedule map
1464 * are used after the call to this function.
1466 static __isl_give isl_union_access_info
*
1467 isl_union_access_info_introduce_schedule(
1468 __isl_take isl_union_access_info
*access
)
1475 sm
= isl_union_map_reverse(access
->schedule_map
);
1476 sm
= isl_union_map_range_map(sm
);
1477 access
->sink
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1479 access
->may_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1480 access
->may_source
);
1481 access
->must_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1482 access
->must_source
);
1483 access
->schedule_map
= sm
;
1485 if (!access
->sink
|| !access
->must_source
||
1486 !access
->may_source
|| !access
->schedule_map
)
1487 return isl_union_access_info_free(access
);
1492 /* This structure represents the result of a dependence analysis computation.
1494 * "must_dep" represents the full definite dependences
1495 * "may_dep" represents the full non-definite dependences.
1496 * Both are of the form
1498 * [Source] -> [[Sink -> Data]]
1500 * (after the schedule dimensions have been projected out).
1501 * "must_no_source" represents the subset of the sink accesses for which
1502 * definitely no source was found.
1503 * "may_no_source" represents the subset of the sink accesses for which
1504 * possibly, but not definitely, no source was found.
1506 struct isl_union_flow
{
1507 isl_union_map
*must_dep
;
1508 isl_union_map
*may_dep
;
1509 isl_union_map
*must_no_source
;
1510 isl_union_map
*may_no_source
;
1513 /* Return the isl_ctx to which "flow" belongs.
1515 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
1517 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
1520 /* Free "flow" and return NULL.
1522 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
1526 isl_union_map_free(flow
->must_dep
);
1527 isl_union_map_free(flow
->may_dep
);
1528 isl_union_map_free(flow
->must_no_source
);
1529 isl_union_map_free(flow
->may_no_source
);
1534 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
1539 fprintf(stderr
, "must dependences: ");
1540 isl_union_map_dump(flow
->must_dep
);
1541 fprintf(stderr
, "may dependences: ");
1542 isl_union_map_dump(flow
->may_dep
);
1543 fprintf(stderr
, "must no source: ");
1544 isl_union_map_dump(flow
->must_no_source
);
1545 fprintf(stderr
, "may no source: ");
1546 isl_union_map_dump(flow
->may_no_source
);
1549 /* Return the full definite dependences in "flow", with accessed elements.
1551 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
1552 __isl_keep isl_union_flow
*flow
)
1556 return isl_union_map_copy(flow
->must_dep
);
1559 /* Return the full possible dependences in "flow", including the definite
1560 * dependences, with accessed elements.
1562 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
1563 __isl_keep isl_union_flow
*flow
)
1567 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1568 isl_union_map_copy(flow
->may_dep
));
1571 /* Return the definite dependences in "flow", without the accessed elements.
1573 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
1574 __isl_keep isl_union_flow
*flow
)
1580 dep
= isl_union_map_copy(flow
->must_dep
);
1581 return isl_union_map_range_factor_domain(dep
);
1584 /* Return the possible dependences in "flow", including the definite
1585 * dependences, without the accessed elements.
1587 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
1588 __isl_keep isl_union_flow
*flow
)
1594 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1595 isl_union_map_copy(flow
->may_dep
));
1596 return isl_union_map_range_factor_domain(dep
);
1599 /* Return the non-definite dependences in "flow".
1601 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
1602 __isl_keep isl_union_flow
*flow
)
1606 return isl_union_map_copy(flow
->may_dep
);
1609 /* Return the subset of the sink accesses for which definitely
1610 * no source was found.
1612 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
1613 __isl_keep isl_union_flow
*flow
)
1617 return isl_union_map_copy(flow
->must_no_source
);
1620 /* Return the subset of the sink accesses for which possibly
1621 * no source was found, including those for which definitely
1622 * no source was found.
1624 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
1625 __isl_keep isl_union_flow
*flow
)
1629 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
1630 isl_union_map_copy(flow
->may_no_source
));
1633 /* Return the subset of the sink accesses for which possibly, but not
1634 * definitely, no source was found.
1636 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
1637 __isl_keep isl_union_flow
*flow
)
1641 return isl_union_map_copy(flow
->may_no_source
);
1644 /* Create a new isl_union_flow object, initialized with empty
1645 * dependence relations and sink subsets.
1647 static __isl_give isl_union_flow
*isl_union_flow_alloc(
1648 __isl_take isl_space
*space
)
1651 isl_union_map
*empty
;
1652 isl_union_flow
*flow
;
1656 ctx
= isl_space_get_ctx(space
);
1657 flow
= isl_alloc_type(ctx
, isl_union_flow
);
1661 empty
= isl_union_map_empty(space
);
1662 flow
->must_dep
= isl_union_map_copy(empty
);
1663 flow
->may_dep
= isl_union_map_copy(empty
);
1664 flow
->must_no_source
= isl_union_map_copy(empty
);
1665 flow
->may_no_source
= empty
;
1667 if (!flow
->must_dep
|| !flow
->may_dep
||
1668 !flow
->must_no_source
|| !flow
->may_no_source
)
1669 return isl_union_flow_free(flow
);
1673 isl_space_free(space
);
1677 /* Drop the schedule dimensions from the iteration domains in "flow".
1678 * In particular, the schedule dimensions have been prepended
1679 * to the iteration domains prior to the dependence analysis by
1680 * replacing the iteration domain D, by the wrapped map [S -> D].
1681 * Replace these wrapped maps by the original D.
1683 * In particular, the dependences computed by access_info_compute_flow_core
1686 * [S -> D] -> [[S' -> D'] -> A]
1688 * The schedule dimensions are projected out by first currying the range,
1691 * [S -> D] -> [S' -> [D' -> A]]
1693 * and then computing the factor range
1697 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
1698 __isl_take isl_union_flow
*flow
)
1703 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
1704 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
1705 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
1706 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
1707 flow
->must_no_source
=
1708 isl_union_map_domain_factor_range(flow
->must_no_source
);
1709 flow
->may_no_source
=
1710 isl_union_map_domain_factor_range(flow
->may_no_source
);
1712 if (!flow
->must_dep
|| !flow
->may_dep
||
1713 !flow
->must_no_source
|| !flow
->may_no_source
)
1714 return isl_union_flow_free(flow
);
1719 struct isl_compute_flow_data
{
1720 isl_union_map
*must_source
;
1721 isl_union_map
*may_source
;
1722 isl_union_flow
*flow
;
1727 struct isl_sched_info
*sink_info
;
1728 struct isl_sched_info
**source_info
;
1729 isl_access_info
*accesses
;
1732 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
1736 struct isl_compute_flow_data
*data
;
1738 data
= (struct isl_compute_flow_data
*)user
;
1740 dim
= isl_space_range(isl_map_get_space(map
));
1742 eq
= isl_space_is_equal(dim
, data
->dim
);
1744 isl_space_free(dim
);
1748 return isl_stat_error
;
1755 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
1759 struct isl_sched_info
*info
;
1760 struct isl_compute_flow_data
*data
;
1762 data
= (struct isl_compute_flow_data
*)user
;
1764 dim
= isl_space_range(isl_map_get_space(map
));
1766 eq
= isl_space_is_equal(dim
, data
->dim
);
1768 isl_space_free(dim
);
1777 info
= sched_info_alloc(map
);
1778 data
->source_info
[data
->count
] = info
;
1780 data
->accesses
= isl_access_info_add_source(data
->accesses
,
1781 map
, data
->must
, info
);
1788 return isl_stat_error
;
1791 /* Determine the shared nesting level and the "textual order" of
1792 * the given accesses.
1794 * We first determine the minimal schedule dimension for both accesses.
1796 * If among those dimensions, we can find one where both have a fixed
1797 * value and if moreover those values are different, then the previous
1798 * dimension is the last shared nesting level and the textual order
1799 * is determined based on the order of the fixed values.
1800 * If no such fixed values can be found, then we set the shared
1801 * nesting level to the minimal schedule dimension, with no textual ordering.
1803 static int before(void *first
, void *second
)
1805 struct isl_sched_info
*info1
= first
;
1806 struct isl_sched_info
*info2
= second
;
1810 n1
= isl_vec_size(info1
->cst
);
1811 n2
= isl_vec_size(info2
->cst
);
1816 for (i
= 0; i
< n1
; ++i
) {
1820 if (!info1
->is_cst
[i
])
1822 if (!info2
->is_cst
[i
])
1824 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
1828 r
= 2 * i
+ (cmp
< 0);
1836 /* Given a sink access, look for all the source accesses that access
1837 * the same array and perform dataflow analysis on them using
1838 * isl_access_info_compute_flow_core.
1840 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
1844 struct isl_compute_flow_data
*data
;
1848 data
= (struct isl_compute_flow_data
*)user
;
1851 ctx
= isl_map_get_ctx(map
);
1853 data
->accesses
= NULL
;
1854 data
->sink_info
= NULL
;
1855 data
->source_info
= NULL
;
1857 data
->dim
= isl_space_range(isl_map_get_space(map
));
1859 if (isl_union_map_foreach_map(data
->must_source
,
1860 &count_matching_array
, data
) < 0)
1862 if (isl_union_map_foreach_map(data
->may_source
,
1863 &count_matching_array
, data
) < 0)
1866 data
->sink_info
= sched_info_alloc(map
);
1867 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
1870 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
1871 data
->sink_info
, &before
, data
->count
);
1872 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
1877 if (isl_union_map_foreach_map(data
->must_source
,
1878 &collect_matching_array
, data
) < 0)
1881 if (isl_union_map_foreach_map(data
->may_source
,
1882 &collect_matching_array
, data
) < 0)
1885 flow
= access_info_compute_flow_core(data
->accesses
);
1886 data
->accesses
= NULL
;
1891 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
1892 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
1893 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
1894 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
1896 for (i
= 0; i
< flow
->n_source
; ++i
) {
1898 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
1899 if (flow
->dep
[i
].must
)
1900 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
1902 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
1905 isl_flow_free(flow
);
1907 sched_info_free(data
->sink_info
);
1908 if (data
->source_info
) {
1909 for (i
= 0; i
< data
->count
; ++i
)
1910 sched_info_free(data
->source_info
[i
]);
1911 free(data
->source_info
);
1913 isl_space_free(data
->dim
);
1918 isl_access_info_free(data
->accesses
);
1919 sched_info_free(data
->sink_info
);
1920 if (data
->source_info
) {
1921 for (i
= 0; i
< data
->count
; ++i
)
1922 sched_info_free(data
->source_info
[i
]);
1923 free(data
->source_info
);
1925 isl_space_free(data
->dim
);
1928 return isl_stat_error
;
1931 /* Remove the must accesses from the may accesses.
1933 * A must access always trumps a may access, so there is no need
1934 * for a must access to also be considered as a may access. Doing so
1935 * would only cost extra computations only to find out that
1936 * the duplicated may access does not make any difference.
1938 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
1939 __isl_take isl_union_access_info
*access
)
1943 access
->may_source
= isl_union_map_subtract(access
->may_source
,
1944 isl_union_map_copy(access
->must_source
));
1945 if (!access
->may_source
)
1946 return isl_union_access_info_free(access
);
1951 /* Given a description of the "sink" accesses, the "source" accesses and
1952 * a schedule, compute for each instance of a sink access
1953 * and for each element accessed by that instance,
1954 * the possible or definite source accesses that last accessed the
1955 * element accessed by the sink access before this sink access
1956 * in the sense that there is no intermediate definite source access.
1958 * The must_no_source and may_no_source elements of the result
1959 * are subsets of access->sink. The elements must_dep and may_dep
1960 * map domain elements of access->{may,must)_source to
1961 * domain elements of access->sink.
1963 * This function is used when only the schedule map representation
1966 * We first prepend the schedule dimensions to the domain
1967 * of the accesses so that we can easily compare their relative order.
1968 * Then we consider each sink access individually in compute_flow.
1970 static __isl_give isl_union_flow
*compute_flow_union_map(
1971 __isl_take isl_union_access_info
*access
)
1973 struct isl_compute_flow_data data
;
1975 access
= isl_union_access_info_align_params(access
);
1976 access
= isl_union_access_info_introduce_schedule(access
);
1980 data
.must_source
= access
->must_source
;
1981 data
.may_source
= access
->may_source
;
1983 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
1985 if (isl_union_map_foreach_map(access
->sink
, &compute_flow
, &data
) < 0)
1988 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
1990 isl_union_access_info_free(access
);
1993 isl_union_access_info_free(access
);
1994 isl_union_flow_free(data
.flow
);
1998 /* A schedule access relation.
2000 * The access relation "access" is of the form [S -> D] -> A,
2001 * where S corresponds to the prefix schedule at "node".
2002 * "must" is only relevant for source accesses and indicates
2003 * whether the access is a must source or a may source.
2005 struct isl_scheduled_access
{
2008 isl_schedule_node
*node
;
2011 /* Data structure for keeping track of individual scheduled sink and source
2012 * accesses when computing dependence analysis based on a schedule tree.
2014 * "n_sink" is the number of used entries in "sink"
2015 * "n_source" is the number of used entries in "source"
2017 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2018 * to keep track of the current node and
2019 * of what extract_sink_source needs to do.
2021 struct isl_compute_flow_schedule_data
{
2022 isl_union_access_info
*access
;
2027 struct isl_scheduled_access
*sink
;
2028 struct isl_scheduled_access
*source
;
2032 isl_schedule_node
*node
;
2035 /* Align the parameters of all sinks with all sources.
2037 * If there are no sinks or no sources, then no alignment is needed.
2039 static void isl_compute_flow_schedule_data_align_params(
2040 struct isl_compute_flow_schedule_data
*data
)
2045 if (data
->n_sink
== 0 || data
->n_source
== 0)
2048 space
= isl_map_get_space(data
->sink
[0].access
);
2050 for (i
= 1; i
< data
->n_sink
; ++i
)
2051 space
= isl_space_align_params(space
,
2052 isl_map_get_space(data
->sink
[i
].access
));
2053 for (i
= 0; i
< data
->n_source
; ++i
)
2054 space
= isl_space_align_params(space
,
2055 isl_map_get_space(data
->source
[i
].access
));
2057 for (i
= 0; i
< data
->n_sink
; ++i
)
2058 data
->sink
[i
].access
=
2059 isl_map_align_params(data
->sink
[i
].access
,
2060 isl_space_copy(space
));
2061 for (i
= 0; i
< data
->n_source
; ++i
)
2062 data
->source
[i
].access
=
2063 isl_map_align_params(data
->source
[i
].access
,
2064 isl_space_copy(space
));
2066 isl_space_free(space
);
2069 /* Free all the memory referenced from "data".
2070 * Do not free "data" itself as it may be allocated on the stack.
2072 static void isl_compute_flow_schedule_data_clear(
2073 struct isl_compute_flow_schedule_data
*data
)
2080 for (i
= 0; i
< data
->n_sink
; ++i
) {
2081 isl_map_free(data
->sink
[i
].access
);
2082 isl_schedule_node_free(data
->sink
[i
].node
);
2085 for (i
= 0; i
< data
->n_source
; ++i
) {
2086 isl_map_free(data
->source
[i
].access
);
2087 isl_schedule_node_free(data
->source
[i
].node
);
2093 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2094 * (an upper bound on) the number of sinks and sources.
2096 * Sinks and sources are only extracted at leaves of the tree,
2097 * so we skip the node if it is not a leaf.
2098 * Otherwise we increment data->n_sink and data->n_source with
2099 * the number of spaces in the sink and source access domains
2100 * that reach this node.
2102 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2105 struct isl_compute_flow_schedule_data
*data
= user
;
2106 isl_union_set
*domain
;
2107 isl_union_map
*umap
;
2108 isl_bool r
= isl_bool_false
;
2110 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2111 return isl_bool_true
;
2113 domain
= isl_schedule_node_get_universe_domain(node
);
2115 umap
= isl_union_map_copy(data
->access
->sink
);
2116 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2117 data
->n_sink
+= isl_union_map_n_map(umap
);
2118 isl_union_map_free(umap
);
2122 umap
= isl_union_map_copy(data
->access
->must_source
);
2123 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2124 data
->n_source
+= isl_union_map_n_map(umap
);
2125 isl_union_map_free(umap
);
2129 umap
= isl_union_map_copy(data
->access
->may_source
);
2130 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2131 data
->n_source
+= isl_union_map_n_map(umap
);
2132 isl_union_map_free(umap
);
2136 isl_union_set_free(domain
);
2141 /* Add a single scheduled sink or source (depending on data->set_sink)
2142 * with scheduled access relation "map", must property data->must and
2143 * schedule node data->node to the list of sinks or sources.
2145 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2147 struct isl_compute_flow_schedule_data
*data
= user
;
2148 struct isl_scheduled_access
*access
;
2151 access
= data
->sink
+ data
->n_sink
++;
2153 access
= data
->source
+ data
->n_source
++;
2155 access
->access
= map
;
2156 access
->must
= data
->must
;
2157 access
->node
= isl_schedule_node_copy(data
->node
);
2162 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2163 * individual scheduled source and sink accesses (taking into account
2164 * the domain of the schedule).
2166 * We only collect accesses at the leaves of the schedule tree.
2167 * We prepend the schedule dimensions at the leaf to the iteration
2168 * domains of the source and sink accesses and then extract
2169 * the individual accesses (per space).
2171 * In particular, if the prefix schedule at the node is of the form
2175 * while the access relations are of the form
2179 * then the updated access relations are of the form
2183 * Note that S consists of a single space such that introducing S
2184 * in the access relations does not increase the number of spaces.
2186 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2189 struct isl_compute_flow_schedule_data
*data
= user
;
2190 isl_union_map
*prefix
;
2191 isl_union_map
*umap
;
2192 isl_bool r
= isl_bool_false
;
2194 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2195 return isl_bool_true
;
2199 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2200 prefix
= isl_union_map_reverse(prefix
);
2201 prefix
= isl_union_map_range_map(prefix
);
2204 umap
= isl_union_map_copy(data
->access
->sink
);
2205 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2206 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2208 isl_union_map_free(umap
);
2212 umap
= isl_union_map_copy(data
->access
->must_source
);
2213 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2214 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2216 isl_union_map_free(umap
);
2220 umap
= isl_union_map_copy(data
->access
->may_source
);
2221 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2222 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2224 isl_union_map_free(umap
);
2226 isl_union_map_free(prefix
);
2231 /* isl_access_info_compute_flow callback for determining whether
2232 * the shared nesting level and the ordering within that level
2233 * for two scheduled accesses for use in compute_single_flow.
2235 * The tokens passed to this function refer to the leaves
2236 * in the schedule tree where the accesses take place.
2238 * If n is the shared number of loops, then we need to return
2239 * "2 * n + 1" if "first" precedes "second" inside the innermost
2240 * shared loop and "2 * n" otherwise.
2242 * The innermost shared ancestor may be the leaves themselves
2243 * if the accesses take place in the same leaf. Otherwise,
2244 * it is either a set node or a sequence node. Only in the case
2245 * of a sequence node do we consider one access to precede the other.
2247 static int before_node(void *first
, void *second
)
2249 isl_schedule_node
*node1
= first
;
2250 isl_schedule_node
*node2
= second
;
2251 isl_schedule_node
*shared
;
2255 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2259 depth
= isl_schedule_node_get_schedule_depth(shared
);
2260 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2263 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2265 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2267 before
= pos1
< pos2
;
2270 isl_schedule_node_free(shared
);
2272 return 2 * depth
+ before
;
2275 /* Add the scheduled sources from "data" that access
2276 * the same data space as "sink" to "access".
2278 static __isl_give isl_access_info
*add_matching_sources(
2279 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2280 struct isl_compute_flow_schedule_data
*data
)
2285 space
= isl_space_range(isl_map_get_space(sink
->access
));
2286 for (i
= 0; i
< data
->n_source
; ++i
) {
2287 struct isl_scheduled_access
*source
;
2288 isl_space
*source_space
;
2291 source
= &data
->source
[i
];
2292 source_space
= isl_map_get_space(source
->access
);
2293 source_space
= isl_space_range(source_space
);
2294 eq
= isl_space_is_equal(space
, source_space
);
2295 isl_space_free(source_space
);
2302 access
= isl_access_info_add_source(access
,
2303 isl_map_copy(source
->access
), source
->must
, source
->node
);
2306 isl_space_free(space
);
2309 isl_space_free(space
);
2310 isl_access_info_free(access
);
2314 /* Given a scheduled sink access relation "sink", compute the corresponding
2315 * dependences on the sources in "data" and add the computed dependences
2318 * The dependences computed by access_info_compute_flow_core are of the form
2320 * [S -> I] -> [[S' -> I'] -> A]
2322 * The schedule dimensions are projected out by first currying the range,
2325 * [S -> I] -> [S' -> [I' -> A]]
2327 * and then computing the factor range
2331 static __isl_give isl_union_flow
*compute_single_flow(
2332 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
2333 struct isl_compute_flow_schedule_data
*data
)
2336 isl_access_info
*access
;
2343 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
2344 &before_node
, data
->n_source
);
2345 access
= add_matching_sources(access
, sink
, data
);
2347 flow
= access_info_compute_flow_core(access
);
2349 return isl_union_flow_free(uf
);
2351 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
2352 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
2353 isl_union_map_from_map(map
));
2354 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
2355 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
2356 isl_union_map_from_map(map
));
2358 for (i
= 0; i
< flow
->n_source
; ++i
) {
2361 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
2362 map
= isl_map_factor_range(map
);
2363 dep
= isl_union_map_from_map(map
);
2364 if (flow
->dep
[i
].must
)
2365 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
2367 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
2370 isl_flow_free(flow
);
2375 /* Given a description of the "sink" accesses, the "source" accesses and
2376 * a schedule, compute for each instance of a sink access
2377 * and for each element accessed by that instance,
2378 * the possible or definite source accesses that last accessed the
2379 * element accessed by the sink access before this sink access
2380 * in the sense that there is no intermediate definite source access.
2381 * Only consider dependences between statement instances that belong
2382 * to the domain of the schedule.
2384 * The must_no_source and may_no_source elements of the result
2385 * are subsets of access->sink. The elements must_dep and may_dep
2386 * map domain elements of access->{may,must)_source to
2387 * domain elements of access->sink.
2389 * This function is used when a schedule tree representation
2392 * We extract the individual scheduled source and sink access relations
2393 * (taking into account the domain of the schedule) and
2394 * then compute dependences for each scheduled sink individually.
2396 static __isl_give isl_union_flow
*compute_flow_schedule(
2397 __isl_take isl_union_access_info
*access
)
2399 struct isl_compute_flow_schedule_data data
= { access
};
2402 isl_union_flow
*flow
;
2404 ctx
= isl_union_access_info_get_ctx(access
);
2408 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2409 &count_sink_source
, &data
) < 0)
2412 n
= data
.n_sink
+ data
.n_source
;
2413 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
2414 if (n
&& !data
.sink
)
2416 data
.source
= data
.sink
+ data
.n_sink
;
2420 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2421 &collect_sink_source
, &data
) < 0)
2424 flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
2426 isl_compute_flow_schedule_data_align_params(&data
);
2428 for (i
= 0; i
< data
.n_sink
; ++i
)
2429 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
2431 isl_compute_flow_schedule_data_clear(&data
);
2433 isl_union_access_info_free(access
);
2436 isl_union_access_info_free(access
);
2437 isl_compute_flow_schedule_data_clear(&data
);
2441 /* Given a description of the "sink" accesses, the "source" accesses and
2442 * a schedule, compute for each instance of a sink access
2443 * and for each element accessed by that instance,
2444 * the possible or definite source accesses that last accessed the
2445 * element accessed by the sink access before this sink access
2446 * in the sense that there is no intermediate definite source access.
2448 * The must_no_source and may_no_source elements of the result
2449 * are subsets of access->sink. The elements must_dep and may_dep
2450 * map domain elements of access->{may,must)_source to
2451 * domain elements of access->sink.
2453 * We check whether the schedule is available as a schedule tree
2454 * or a schedule map and call the correpsonding function to perform
2457 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
2458 __isl_take isl_union_access_info
*access
)
2460 access
= isl_union_access_info_normalize(access
);
2463 if (access
->schedule
)
2464 return compute_flow_schedule(access
);
2466 return compute_flow_union_map(access
);
2469 /* Given a collection of "sink" and "source" accesses,
2470 * compute for each iteration of a sink access
2471 * and for each element accessed by that iteration,
2472 * the source access in the list that last accessed the
2473 * element accessed by the sink access before this sink access.
2474 * Each access is given as a map from the loop iterators
2475 * to the array indices.
2476 * The result is a relations between source and sink
2477 * iterations and a subset of the domain of the sink accesses,
2478 * corresponding to those iterations that access an element
2479 * not previously accessed.
2481 * We collect the inputs in an isl_union_access_info object,
2482 * call isl_union_access_info_compute_flow and extract
2483 * the outputs from the result.
2485 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
2486 __isl_take isl_union_map
*must_source
,
2487 __isl_take isl_union_map
*may_source
,
2488 __isl_take isl_union_map
*schedule
,
2489 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
2490 __isl_give isl_union_map
**must_no_source
,
2491 __isl_give isl_union_map
**may_no_source
)
2493 isl_union_access_info
*access
;
2494 isl_union_flow
*flow
;
2496 access
= isl_union_access_info_from_sink(sink
);
2497 access
= isl_union_access_info_set_must_source(access
, must_source
);
2498 access
= isl_union_access_info_set_may_source(access
, may_source
);
2499 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
2500 flow
= isl_union_access_info_compute_flow(access
);
2503 *must_dep
= isl_union_flow_get_must_dependence(flow
);
2505 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
2507 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
2509 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
2511 isl_union_flow_free(flow
);
2513 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
2514 (must_no_source
&& !*must_no_source
) ||
2515 (may_no_source
&& !*may_no_source
))
2521 *must_dep
= isl_union_map_free(*must_dep
);
2523 *may_dep
= isl_union_map_free(*may_dep
);
2525 *must_no_source
= isl_union_map_free(*must_no_source
);
2527 *may_no_source
= isl_union_map_free(*may_no_source
);