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
24 enum isl_restriction_type
{
25 isl_restriction_type_empty
,
26 isl_restriction_type_none
,
27 isl_restriction_type_input
,
28 isl_restriction_type_output
31 struct isl_restriction
{
32 enum isl_restriction_type type
;
38 /* Create a restriction of the given type.
40 static __isl_give isl_restriction
*isl_restriction_alloc(
41 __isl_take isl_map
*source_map
, enum isl_restriction_type type
)
44 isl_restriction
*restr
;
49 ctx
= isl_map_get_ctx(source_map
);
50 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
56 isl_map_free(source_map
);
59 isl_map_free(source_map
);
63 /* Create a restriction that doesn't restrict anything.
65 __isl_give isl_restriction
*isl_restriction_none(__isl_take isl_map
*source_map
)
67 return isl_restriction_alloc(source_map
, isl_restriction_type_none
);
70 /* Create a restriction that removes everything.
72 __isl_give isl_restriction
*isl_restriction_empty(
73 __isl_take isl_map
*source_map
)
75 return isl_restriction_alloc(source_map
, isl_restriction_type_empty
);
78 /* Create a restriction on the input of the maximization problem
79 * based on the given source and sink restrictions.
81 __isl_give isl_restriction
*isl_restriction_input(
82 __isl_take isl_set
*source_restr
, __isl_take isl_set
*sink_restr
)
85 isl_restriction
*restr
;
87 if (!source_restr
|| !sink_restr
)
90 ctx
= isl_set_get_ctx(source_restr
);
91 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
95 restr
->type
= isl_restriction_type_input
;
96 restr
->source
= source_restr
;
97 restr
->sink
= sink_restr
;
101 isl_set_free(source_restr
);
102 isl_set_free(sink_restr
);
106 /* Create a restriction on the output of the maximization problem
107 * based on the given source restriction.
109 __isl_give isl_restriction
*isl_restriction_output(
110 __isl_take isl_set
*source_restr
)
113 isl_restriction
*restr
;
118 ctx
= isl_set_get_ctx(source_restr
);
119 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
123 restr
->type
= isl_restriction_type_output
;
124 restr
->source
= source_restr
;
128 isl_set_free(source_restr
);
132 __isl_null isl_restriction
*isl_restriction_free(
133 __isl_take isl_restriction
*restr
)
138 isl_set_free(restr
->source
);
139 isl_set_free(restr
->sink
);
144 isl_ctx
*isl_restriction_get_ctx(__isl_keep isl_restriction
*restr
)
146 return restr
? isl_set_get_ctx(restr
->source
) : NULL
;
149 /* A private structure to keep track of a mapping together with
150 * a user-specified identifier and a boolean indicating whether
151 * the map represents a must or may access/dependence.
153 struct isl_labeled_map
{
159 /* A structure containing the input for dependence analysis:
161 * - n_must + n_may (<= max_source) sources
162 * - a function for determining the relative order of sources and sink
163 * The must sources are placed before the may sources.
165 * domain_map is an auxiliary map that maps the sink access relation
166 * to the domain of this access relation.
168 * restrict_fn is a callback that (if not NULL) will be called
169 * right before any lexicographical maximization.
171 struct isl_access_info
{
173 struct isl_labeled_map sink
;
174 isl_access_level_before level_before
;
176 isl_access_restrict restrict_fn
;
182 struct isl_labeled_map source
[1];
185 /* A structure containing the output of dependence analysis:
186 * - n_source dependences
187 * - a wrapped subset of the sink for which definitely no source could be found
188 * - a wrapped subset of the sink for which possibly no source could be found
191 isl_set
*must_no_source
;
192 isl_set
*may_no_source
;
194 struct isl_labeled_map
*dep
;
197 /* Construct an isl_access_info structure and fill it up with
198 * the given data. The number of sources is set to 0.
200 __isl_give isl_access_info
*isl_access_info_alloc(__isl_take isl_map
*sink
,
201 void *sink_user
, isl_access_level_before fn
, int max_source
)
204 struct isl_access_info
*acc
;
209 ctx
= isl_map_get_ctx(sink
);
210 isl_assert(ctx
, max_source
>= 0, goto error
);
212 acc
= isl_calloc(ctx
, struct isl_access_info
,
213 sizeof(struct isl_access_info
) +
214 (max_source
- 1) * sizeof(struct isl_labeled_map
));
218 acc
->sink
.map
= sink
;
219 acc
->sink
.data
= sink_user
;
220 acc
->level_before
= fn
;
221 acc
->max_source
= max_source
;
231 /* Free the given isl_access_info structure.
233 __isl_null isl_access_info
*isl_access_info_free(
234 __isl_take isl_access_info
*acc
)
240 isl_map_free(acc
->domain_map
);
241 isl_map_free(acc
->sink
.map
);
242 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
243 isl_map_free(acc
->source
[i
].map
);
248 isl_ctx
*isl_access_info_get_ctx(__isl_keep isl_access_info
*acc
)
250 return acc
? isl_map_get_ctx(acc
->sink
.map
) : NULL
;
253 __isl_give isl_access_info
*isl_access_info_set_restrict(
254 __isl_take isl_access_info
*acc
, isl_access_restrict fn
, void *user
)
258 acc
->restrict_fn
= fn
;
259 acc
->restrict_user
= user
;
263 /* Add another source to an isl_access_info structure, making
264 * sure the "must" sources are placed before the "may" sources.
265 * This function may be called at most max_source times on a
266 * given isl_access_info structure, with max_source as specified
267 * in the call to isl_access_info_alloc that constructed the structure.
269 __isl_give isl_access_info
*isl_access_info_add_source(
270 __isl_take isl_access_info
*acc
, __isl_take isl_map
*source
,
271 int must
, void *source_user
)
277 ctx
= isl_map_get_ctx(acc
->sink
.map
);
278 isl_assert(ctx
, acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
282 acc
->source
[acc
->n_must
+ acc
->n_may
] =
283 acc
->source
[acc
->n_must
];
284 acc
->source
[acc
->n_must
].map
= source
;
285 acc
->source
[acc
->n_must
].data
= source_user
;
286 acc
->source
[acc
->n_must
].must
= 1;
289 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
290 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
291 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
297 isl_map_free(source
);
298 isl_access_info_free(acc
);
302 /* Return -n, 0 or n (with n a positive value), depending on whether
303 * the source access identified by p1 should be sorted before, together
304 * or after that identified by p2.
306 * If p1 appears before p2, then it should be sorted first.
307 * For more generic initial schedules, it is possible that neither
308 * p1 nor p2 appears before the other, or at least not in any obvious way.
309 * We therefore also check if p2 appears before p1, in which case p2
310 * should be sorted first.
311 * If not, we try to order the two statements based on the description
312 * of the iteration domains. This results in an arbitrary, but fairly
315 static int access_sort_cmp(const void *p1
, const void *p2
, void *user
)
317 isl_access_info
*acc
= user
;
318 const struct isl_labeled_map
*i1
, *i2
;
321 i1
= (const struct isl_labeled_map
*) p1
;
322 i2
= (const struct isl_labeled_map
*) p2
;
324 level1
= acc
->level_before(i1
->data
, i2
->data
);
328 level2
= acc
->level_before(i2
->data
, i1
->data
);
332 h1
= isl_map_get_hash(i1
->map
);
333 h2
= isl_map_get_hash(i2
->map
);
334 return h1
> h2
? 1 : h1
< h2
? -1 : 0;
337 /* Sort the must source accesses in their textual order.
339 static __isl_give isl_access_info
*isl_access_info_sort_sources(
340 __isl_take isl_access_info
*acc
)
344 if (acc
->n_must
<= 1)
347 if (isl_sort(acc
->source
, acc
->n_must
, sizeof(struct isl_labeled_map
),
348 access_sort_cmp
, acc
) < 0)
349 return isl_access_info_free(acc
);
354 /* Align the parameters of the two spaces if needed and then call
357 static __isl_give isl_space
*space_align_and_join(__isl_take isl_space
*left
,
358 __isl_take isl_space
*right
)
360 if (isl_space_match(left
, isl_dim_param
, right
, isl_dim_param
))
361 return isl_space_join(left
, right
);
363 left
= isl_space_align_params(left
, isl_space_copy(right
));
364 right
= isl_space_align_params(right
, isl_space_copy(left
));
365 return isl_space_join(left
, right
);
368 /* Initialize an empty isl_flow structure corresponding to a given
369 * isl_access_info structure.
370 * For each must access, two dependences are created (initialized
371 * to the empty relation), one for the resulting must dependences
372 * and one for the resulting may dependences. May accesses can
373 * only lead to may dependences, so only one dependence is created
375 * This function is private as isl_flow structures are only supposed
376 * to be created by isl_access_info_compute_flow.
378 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
382 struct isl_flow
*dep
;
387 ctx
= isl_map_get_ctx(acc
->sink
.map
);
388 dep
= isl_calloc_type(ctx
, struct isl_flow
);
392 n
= 2 * acc
->n_must
+ acc
->n_may
;
393 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
, n
);
398 for (i
= 0; i
< acc
->n_must
; ++i
) {
400 dim
= space_align_and_join(
401 isl_map_get_space(acc
->source
[i
].map
),
402 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
403 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
404 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
405 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
406 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
407 dep
->dep
[2 * i
].must
= 1;
408 dep
->dep
[2 * i
+ 1].must
= 0;
409 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
412 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
414 dim
= space_align_and_join(
415 isl_map_get_space(acc
->source
[i
].map
),
416 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
417 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
418 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
419 dep
->dep
[acc
->n_must
+ i
].must
= 0;
420 if (!dep
->dep
[acc
->n_must
+ i
].map
)
430 /* Iterate over all sources and for each resulting flow dependence
431 * that is not empty, call the user specfied function.
432 * The second argument in this function call identifies the source,
433 * while the third argument correspond to the final argument of
434 * the isl_flow_foreach call.
436 int isl_flow_foreach(__isl_keep isl_flow
*deps
,
437 int (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
, void *user
),
445 for (i
= 0; i
< deps
->n_source
; ++i
) {
446 if (isl_map_plain_is_empty(deps
->dep
[i
].map
))
448 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
449 deps
->dep
[i
].data
, user
) < 0)
456 /* Return a copy of the subset of the sink for which no source could be found.
458 __isl_give isl_map
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
464 return isl_set_unwrap(isl_set_copy(deps
->must_no_source
));
466 return isl_set_unwrap(isl_set_copy(deps
->may_no_source
));
469 void isl_flow_free(__isl_take isl_flow
*deps
)
475 isl_set_free(deps
->must_no_source
);
476 isl_set_free(deps
->may_no_source
);
478 for (i
= 0; i
< deps
->n_source
; ++i
)
479 isl_map_free(deps
->dep
[i
].map
);
485 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
487 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
490 /* Return a map that enforces that the domain iteration occurs after
491 * the range iteration at the given level.
492 * If level is odd, then the domain iteration should occur after
493 * the target iteration in their shared level/2 outermost loops.
494 * In this case we simply need to enforce that these outermost
495 * loop iterations are the same.
496 * If level is even, then the loop iterator of the domain should
497 * be greater than the loop iterator of the range at the last
498 * of the level/2 shared loops, i.e., loop level/2 - 1.
500 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
502 struct isl_basic_map
*bmap
;
505 bmap
= isl_basic_map_equal(dim
, level
/2);
507 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
509 return isl_map_from_basic_map(bmap
);
512 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
513 * but first check if the user has set acc->restrict_fn and if so
514 * update either the input or the output of the maximization problem
515 * with respect to the resulting restriction.
517 * Since the user expects a mapping from sink iterations to source iterations,
518 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
519 * to accessed array elements, we first need to project out the accessed
520 * sink array elements by applying acc->domain_map.
521 * Similarly, the sink restriction specified by the user needs to be
522 * converted back to the wrapped map.
524 static __isl_give isl_map
*restricted_partial_lexmax(
525 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
526 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
529 isl_restriction
*restr
;
530 isl_set
*sink_domain
;
534 if (!acc
->restrict_fn
)
535 return isl_map_partial_lexmax(dep
, sink
, empty
);
537 source_map
= isl_map_copy(dep
);
538 source_map
= isl_map_apply_domain(source_map
,
539 isl_map_copy(acc
->domain_map
));
540 sink_domain
= isl_set_copy(sink
);
541 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
542 restr
= acc
->restrict_fn(source_map
, sink_domain
,
543 acc
->source
[source
].data
, acc
->restrict_user
);
544 isl_set_free(sink_domain
);
545 isl_map_free(source_map
);
549 if (restr
->type
== isl_restriction_type_input
) {
550 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
551 sink_restr
= isl_set_copy(restr
->sink
);
552 sink_restr
= isl_set_apply(sink_restr
,
553 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
554 sink
= isl_set_intersect(sink
, sink_restr
);
555 } else if (restr
->type
== isl_restriction_type_empty
) {
556 isl_space
*space
= isl_map_get_space(dep
);
558 dep
= isl_map_empty(space
);
561 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
563 if (restr
->type
== isl_restriction_type_output
)
564 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
566 isl_restriction_free(restr
);
575 /* Compute the last iteration of must source j that precedes the sink
576 * at the given level for sink iterations in set_C.
577 * The subset of set_C for which no such iteration can be found is returned
580 static struct isl_map
*last_source(struct isl_access_info
*acc
,
581 struct isl_set
*set_C
,
582 int j
, int level
, struct isl_set
**empty
)
584 struct isl_map
*read_map
;
585 struct isl_map
*write_map
;
586 struct isl_map
*dep_map
;
587 struct isl_map
*after
;
588 struct isl_map
*result
;
590 read_map
= isl_map_copy(acc
->sink
.map
);
591 write_map
= isl_map_copy(acc
->source
[j
].map
);
592 write_map
= isl_map_reverse(write_map
);
593 dep_map
= isl_map_apply_range(read_map
, write_map
);
594 after
= after_at_level(isl_map_get_space(dep_map
), level
);
595 dep_map
= isl_map_intersect(dep_map
, after
);
596 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
597 result
= isl_map_reverse(result
);
602 /* For a given mapping between iterations of must source j and iterations
603 * of the sink, compute the last iteration of must source k preceding
604 * the sink at level before_level for any of the sink iterations,
605 * but following the corresponding iteration of must source j at level
608 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
609 struct isl_map
*old_map
,
610 int j
, int before_level
,
611 int k
, int after_level
,
612 struct isl_set
**empty
)
615 struct isl_set
*set_C
;
616 struct isl_map
*read_map
;
617 struct isl_map
*write_map
;
618 struct isl_map
*dep_map
;
619 struct isl_map
*after_write
;
620 struct isl_map
*before_read
;
621 struct isl_map
*result
;
623 set_C
= isl_map_range(isl_map_copy(old_map
));
624 read_map
= isl_map_copy(acc
->sink
.map
);
625 write_map
= isl_map_copy(acc
->source
[k
].map
);
627 write_map
= isl_map_reverse(write_map
);
628 dep_map
= isl_map_apply_range(read_map
, write_map
);
629 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
630 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
631 after_write
= after_at_level(dim
, after_level
);
632 after_write
= isl_map_apply_range(after_write
, old_map
);
633 after_write
= isl_map_reverse(after_write
);
634 dep_map
= isl_map_intersect(dep_map
, after_write
);
635 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
636 dep_map
= isl_map_intersect(dep_map
, before_read
);
637 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
638 result
= isl_map_reverse(result
);
643 /* Given a shared_level between two accesses, return 1 if the
644 * the first can precede the second at the requested target_level.
645 * If the target level is odd, i.e., refers to a statement level
646 * dimension, then first needs to precede second at the requested
647 * level, i.e., shared_level must be equal to target_level.
648 * If the target level is odd, then the two loops should share
649 * at least the requested number of outer loops.
651 static int can_precede_at_level(int shared_level
, int target_level
)
653 if (shared_level
< target_level
)
655 if ((target_level
% 2) && shared_level
> target_level
)
660 /* Given a possible flow dependence temp_rel[j] between source j and the sink
661 * at level sink_level, remove those elements for which
662 * there is an iteration of another source k < j that is closer to the sink.
663 * The flow dependences temp_rel[k] are updated with the improved sources.
664 * Any improved source needs to precede the sink at the same level
665 * and needs to follow source j at the same or a deeper level.
666 * The lower this level, the later the execution date of source k.
667 * We therefore consider lower levels first.
669 * If temp_rel[j] is empty, then there can be no improvement and
670 * we return immediately.
672 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
673 struct isl_map
**temp_rel
, int j
, int sink_level
)
676 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
678 if (isl_map_plain_is_empty(temp_rel
[j
]))
681 for (k
= j
- 1; k
>= 0; --k
) {
683 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
684 if (!can_precede_at_level(plevel
, sink_level
))
687 plevel2
= acc
->level_before(acc
->source
[j
].data
,
688 acc
->source
[k
].data
);
690 for (level
= sink_level
; level
<= depth
; ++level
) {
692 struct isl_set
*trest
;
693 struct isl_map
*copy
;
695 if (!can_precede_at_level(plevel2
, level
))
698 copy
= isl_map_copy(temp_rel
[j
]);
699 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
701 if (isl_map_plain_is_empty(T
)) {
706 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
707 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
714 /* Compute all iterations of may source j that precedes the sink at the given
715 * level for sink iterations in set_C.
717 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
718 __isl_take isl_set
*set_C
, int j
, int level
)
725 read_map
= isl_map_copy(acc
->sink
.map
);
726 read_map
= isl_map_intersect_domain(read_map
, set_C
);
727 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
728 write_map
= isl_map_reverse(write_map
);
729 dep_map
= isl_map_apply_range(read_map
, write_map
);
730 after
= after_at_level(isl_map_get_space(dep_map
), level
);
731 dep_map
= isl_map_intersect(dep_map
, after
);
733 return isl_map_reverse(dep_map
);
736 /* For a given mapping between iterations of must source k and iterations
737 * of the sink, compute the all iteration of may source j preceding
738 * the sink at level before_level for any of the sink iterations,
739 * but following the corresponding iteration of must source k at level
742 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
743 __isl_take isl_map
*old_map
,
744 int j
, int before_level
, int k
, int after_level
)
751 isl_map
*after_write
;
752 isl_map
*before_read
;
754 set_C
= isl_map_range(isl_map_copy(old_map
));
755 read_map
= isl_map_copy(acc
->sink
.map
);
756 read_map
= isl_map_intersect_domain(read_map
, set_C
);
757 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
759 write_map
= isl_map_reverse(write_map
);
760 dep_map
= isl_map_apply_range(read_map
, write_map
);
761 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
762 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
763 after_write
= after_at_level(dim
, after_level
);
764 after_write
= isl_map_apply_range(after_write
, old_map
);
765 after_write
= isl_map_reverse(after_write
);
766 dep_map
= isl_map_intersect(dep_map
, after_write
);
767 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
768 dep_map
= isl_map_intersect(dep_map
, before_read
);
769 return isl_map_reverse(dep_map
);
772 /* Given the must and may dependence relations for the must accesses
773 * for level sink_level, check if there are any accesses of may access j
774 * that occur in between and return their union.
775 * If some of these accesses are intermediate with respect to
776 * (previously thought to be) must dependences, then these
777 * must dependences are turned into may dependences.
779 static __isl_give isl_map
*all_intermediate_sources(
780 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
781 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
782 int j
, int sink_level
)
785 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
788 for (k
= 0; k
< acc
->n_must
; ++k
) {
791 if (isl_map_plain_is_empty(may_rel
[k
]) &&
792 isl_map_plain_is_empty(must_rel
[k
]))
795 plevel
= acc
->level_before(acc
->source
[k
].data
,
796 acc
->source
[acc
->n_must
+ j
].data
);
798 for (level
= sink_level
; level
<= depth
; ++level
) {
803 if (!can_precede_at_level(plevel
, level
))
806 copy
= isl_map_copy(may_rel
[k
]);
807 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
808 map
= isl_map_union(map
, T
);
810 copy
= isl_map_copy(must_rel
[k
]);
811 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
812 ran
= isl_map_range(isl_map_copy(T
));
813 map
= isl_map_union(map
, T
);
814 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
815 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
817 T
= isl_map_from_domain_and_range(
819 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
821 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
828 /* Compute dependences for the case where all accesses are "may"
829 * accesses, which boils down to computing memory based dependences.
830 * The generic algorithm would also work in this case, but it would
831 * be overkill to use it.
833 static __isl_give isl_flow
*compute_mem_based_dependences(
834 __isl_keep isl_access_info
*acc
)
841 res
= isl_flow_alloc(acc
);
845 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
846 maydo
= isl_set_copy(mustdo
);
848 for (i
= 0; i
< acc
->n_may
; ++i
) {
855 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
856 is_before
= plevel
& 1;
859 dim
= isl_map_get_space(res
->dep
[i
].map
);
861 before
= isl_map_lex_le_first(dim
, plevel
);
863 before
= isl_map_lex_lt_first(dim
, plevel
);
864 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
865 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
866 dep
= isl_map_intersect(dep
, before
);
867 mustdo
= isl_set_subtract(mustdo
,
868 isl_map_range(isl_map_copy(dep
)));
869 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
872 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
873 res
->must_no_source
= mustdo
;
878 /* Compute dependences for the case where there is at least one
881 * The core algorithm considers all levels in which a source may precede
882 * the sink, where a level may either be a statement level or a loop level.
883 * The outermost statement level is 1, the first loop level is 2, etc...
884 * The algorithm basically does the following:
885 * for all levels l of the read access from innermost to outermost
886 * for all sources w that may precede the sink access at that level
887 * compute the last iteration of the source that precedes the sink access
889 * add result to possible last accesses at level l of source w
890 * for all sources w2 that we haven't considered yet at this level that may
891 * also precede the sink access
892 * for all levels l2 of w from l to innermost
893 * for all possible last accesses dep of w at l
894 * compute last iteration of w2 between the source and sink
896 * add result to possible last accesses at level l of write w2
897 * and replace possible last accesses dep by the remainder
900 * The above algorithm is applied to the must access. During the course
901 * of the algorithm, we keep track of sink iterations that still
902 * need to be considered. These iterations are split into those that
903 * haven't been matched to any source access (mustdo) and those that have only
904 * been matched to may accesses (maydo).
905 * At the end of each level, we also consider the may accesses.
906 * In particular, we consider may accesses that precede the remaining
907 * sink iterations, moving elements from mustdo to maydo when appropriate,
908 * and may accesses that occur between a must source and a sink of any
909 * dependences found at the current level, turning must dependences into
910 * may dependences when appropriate.
913 static __isl_give isl_flow
*compute_val_based_dependences(
914 __isl_keep isl_access_info
*acc
)
918 isl_set
*mustdo
= NULL
;
919 isl_set
*maydo
= NULL
;
922 isl_map
**must_rel
= NULL
;
923 isl_map
**may_rel
= NULL
;
928 res
= isl_flow_alloc(acc
);
931 ctx
= isl_map_get_ctx(acc
->sink
.map
);
933 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
934 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
935 maydo
= isl_set_empty_like(mustdo
);
936 if (!mustdo
|| !maydo
)
938 if (isl_set_plain_is_empty(mustdo
))
941 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
942 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
943 if (!must_rel
|| !may_rel
)
946 for (level
= depth
; level
>= 1; --level
) {
947 for (j
= acc
->n_must
-1; j
>=0; --j
) {
948 must_rel
[j
] = isl_map_empty_like(res
->dep
[2 * j
].map
);
949 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
952 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
954 struct isl_set
*rest
;
957 plevel
= acc
->level_before(acc
->source
[j
].data
,
959 if (!can_precede_at_level(plevel
, level
))
962 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
963 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
966 intermediate_sources(acc
, must_rel
, j
, level
);
968 T
= last_source(acc
, maydo
, j
, level
, &rest
);
969 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
972 intermediate_sources(acc
, may_rel
, j
, level
);
974 if (isl_set_plain_is_empty(mustdo
) &&
975 isl_set_plain_is_empty(maydo
))
978 for (j
= j
- 1; j
>= 0; --j
) {
981 plevel
= acc
->level_before(acc
->source
[j
].data
,
983 if (!can_precede_at_level(plevel
, level
))
986 intermediate_sources(acc
, must_rel
, j
, level
);
987 intermediate_sources(acc
, may_rel
, j
, level
);
990 for (j
= 0; j
< acc
->n_may
; ++j
) {
995 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
997 if (!can_precede_at_level(plevel
, level
))
1000 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1001 res
->dep
[2 * acc
->n_must
+ j
].map
=
1002 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1003 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1004 ran
= isl_map_range(isl_map_copy(T
));
1005 res
->dep
[2 * acc
->n_must
+ j
].map
=
1006 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1007 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1008 maydo
= isl_set_union_disjoint(maydo
, ran
);
1010 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1011 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1013 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1016 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1017 res
->dep
[2 * j
].map
=
1018 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1020 res
->dep
[2 * j
+ 1].map
=
1021 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1025 if (isl_set_plain_is_empty(mustdo
) &&
1026 isl_set_plain_is_empty(maydo
))
1033 res
->must_no_source
= mustdo
;
1034 res
->may_no_source
= maydo
;
1038 isl_set_free(mustdo
);
1039 isl_set_free(maydo
);
1045 /* Given a "sink" access, a list of n "source" accesses,
1046 * compute for each iteration of the sink access
1047 * and for each element accessed by that iteration,
1048 * the source access in the list that last accessed the
1049 * element accessed by the sink access before this sink access.
1050 * Each access is given as a map from the loop iterators
1051 * to the array indices.
1052 * The result is a list of n relations between source and sink
1053 * iterations and a subset of the domain of the sink access,
1054 * corresponding to those iterations that access an element
1055 * not previously accessed.
1057 * To deal with multi-valued sink access relations, the sink iteration
1058 * domain is first extended with dimensions that correspond to the data
1059 * space. After the computation is finished, these extra dimensions are
1060 * projected out again.
1062 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1065 struct isl_flow
*res
= NULL
;
1070 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1071 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1075 if (acc
->n_must
== 0)
1076 res
= compute_mem_based_dependences(acc
);
1078 acc
= isl_access_info_sort_sources(acc
);
1079 res
= compute_val_based_dependences(acc
);
1084 for (j
= 0; j
< res
->n_source
; ++j
) {
1085 res
->dep
[j
].map
= isl_map_apply_range(res
->dep
[j
].map
,
1086 isl_map_copy(acc
->domain_map
));
1087 if (!res
->dep
[j
].map
)
1090 if (!res
->must_no_source
|| !res
->may_no_source
)
1093 isl_access_info_free(acc
);
1096 isl_access_info_free(acc
);
1102 /* Keep track of some information about a schedule for a given
1103 * access. In particular, keep track of which dimensions
1104 * have a constant value and of the actual constant values.
1106 struct isl_sched_info
{
1111 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1115 isl_vec_free(info
->cst
);
1120 /* Extract information on the constant dimensions of the schedule
1121 * for a given access. The "map" is of the form
1125 * with S the schedule domain, D the iteration domain and A the data domain.
1127 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1128 __isl_keep isl_map
*map
)
1132 struct isl_sched_info
*info
;
1138 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1141 n
= isl_space_dim(dim
, isl_dim_in
);
1142 isl_space_free(dim
);
1144 ctx
= isl_map_get_ctx(map
);
1145 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1148 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1149 info
->cst
= isl_vec_alloc(ctx
, n
);
1150 if (n
&& (!info
->is_cst
|| !info
->cst
))
1153 for (i
= 0; i
< n
; ++i
) {
1156 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1159 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1160 if (info
->is_cst
[i
])
1161 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1168 sched_info_free(info
);
1172 /* This structure represents the input for a dependence analysis computation.
1174 * "sink" represents the sink accesses.
1175 * "must_source" represents the definite source accesses.
1176 * "may_source" represents the possible source accesses.
1177 * "schedule_map" represents the execution order.
1179 * The domains of these four maps refer to the same iteration spaces(s).
1180 * The ranges of the first three maps also refer to the same data space(s).
1182 * After a call to isl_union_access_info_introduce_schedule,
1183 * the "schedule_map" field no longer contains useful information.
1185 struct isl_union_access_info
{
1186 isl_union_map
*sink
;
1187 isl_union_map
*must_source
;
1188 isl_union_map
*may_source
;
1189 isl_union_map
*schedule_map
;
1192 /* Free "access" and return NULL.
1194 __isl_null isl_union_access_info
*isl_union_access_info_free(
1195 __isl_take isl_union_access_info
*access
)
1200 isl_union_map_free(access
->sink
);
1201 isl_union_map_free(access
->must_source
);
1202 isl_union_map_free(access
->may_source
);
1203 isl_union_map_free(access
->schedule_map
);
1209 /* Return the isl_ctx to which "access" belongs.
1211 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1213 return access
? isl_union_map_get_ctx(access
->sink
) : NULL
;
1216 /* Create a new isl_union_access_info with the given sink accesses and
1217 * and no source accesses or schedule information.
1219 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1220 __isl_take isl_union_map
*sink
)
1223 isl_union_map
*empty
;
1224 isl_union_access_info
*access
;
1228 ctx
= isl_union_map_get_ctx(sink
);
1229 access
= isl_alloc_type(ctx
, isl_union_access_info
);
1233 empty
= isl_union_map_empty(isl_union_map_get_space(sink
));
1234 access
->sink
= sink
;
1235 access
->must_source
= isl_union_map_copy(empty
);
1236 access
->may_source
= isl_union_map_copy(empty
);
1237 access
->schedule_map
= empty
;
1239 if (!access
->sink
|| !access
->must_source
||
1240 !access
->may_source
|| !access
->schedule_map
)
1241 return isl_union_access_info_free(access
);
1245 isl_union_map_free(sink
);
1249 /* Replace the definite source accesses of "access" by "must_source".
1251 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1252 __isl_take isl_union_access_info
*access
,
1253 __isl_take isl_union_map
*must_source
)
1255 if (!access
|| !must_source
)
1258 isl_union_map_free(access
->must_source
);
1259 access
->must_source
= must_source
;
1263 isl_union_access_info_free(access
);
1264 isl_union_map_free(must_source
);
1268 /* Replace the possible source accesses of "access" by "may_source".
1270 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1271 __isl_take isl_union_access_info
*access
,
1272 __isl_take isl_union_map
*may_source
)
1274 if (!access
|| !may_source
)
1277 isl_union_map_free(access
->may_source
);
1278 access
->may_source
= may_source
;
1282 isl_union_access_info_free(access
);
1283 isl_union_map_free(may_source
);
1287 /* Replace the schedule map of "access" by the schedule map
1288 * derived from "schedule".
1290 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1291 __isl_take isl_union_access_info
*access
,
1292 __isl_take isl_schedule
*schedule
)
1294 isl_union_map
*schedule_map
;
1296 schedule_map
= isl_schedule_get_map(schedule
);
1297 isl_schedule_free(schedule
);
1299 return isl_union_access_info_set_schedule_map(access
, schedule_map
);
1302 /* Replace the schedule map of "access" by "schedule_map".
1304 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1305 __isl_take isl_union_access_info
*access
,
1306 __isl_take isl_union_map
*schedule_map
)
1308 if (!access
|| !schedule_map
)
1311 isl_union_map_free(access
->schedule_map
);
1312 access
->schedule_map
= schedule_map
;
1316 isl_union_access_info_free(access
);
1317 isl_union_map_free(schedule_map
);
1321 /* Update the fields of "access" such that they all have the same parameters.
1323 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1324 __isl_take isl_union_access_info
*access
)
1331 space
= isl_union_map_get_space(access
->sink
);
1332 space
= isl_space_align_params(space
,
1333 isl_union_map_get_space(access
->must_source
));
1334 space
= isl_space_align_params(space
,
1335 isl_union_map_get_space(access
->may_source
));
1336 space
= isl_space_align_params(space
,
1337 isl_union_map_get_space(access
->schedule_map
));
1338 access
->sink
= isl_union_map_align_params(access
->sink
,
1339 isl_space_copy(space
));
1340 access
->must_source
= isl_union_map_align_params(access
->must_source
,
1341 isl_space_copy(space
));
1342 access
->may_source
= isl_union_map_align_params(access
->may_source
,
1343 isl_space_copy(space
));
1344 access
->schedule_map
= isl_union_map_align_params(access
->schedule_map
,
1347 if (!access
->sink
|| !access
->must_source
||
1348 !access
->may_source
|| !access
->schedule_map
)
1349 return isl_union_access_info_free(access
);
1354 /* Prepend the schedule dimensions to the iteration domains.
1356 * That is, if the schedule is of the form
1360 * while the access relations are of the form
1364 * then the updated access relations are of the form
1368 * The schedule map is also replaced by the map
1372 * that is used during the internal computation.
1373 * Neither the original schedule map nor this updated schedule map
1374 * are used after the call to this function.
1376 static __isl_give isl_union_access_info
*
1377 isl_union_access_info_introduce_schedule(
1378 __isl_take isl_union_access_info
*access
)
1385 sm
= isl_union_map_reverse(access
->schedule_map
);
1386 sm
= isl_union_map_range_map(sm
);
1387 access
->sink
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1389 access
->may_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1390 access
->may_source
);
1391 access
->must_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1392 access
->must_source
);
1393 access
->schedule_map
= sm
;
1395 if (!access
->sink
|| !access
->must_source
||
1396 !access
->may_source
|| !access
->schedule_map
)
1397 return isl_union_access_info_free(access
);
1402 /* This structure epresents the result of a dependence analysis computation.
1404 * "must_dep" represents the definite dependences.
1405 * "may_dep" represents the non-definite dependences.
1406 * "must_no_source" represents the subset of the sink accesses for which
1407 * definitely no source was found.
1408 * "may_no_source" represents the subset of the sink accesses for which
1409 * possibly, but not definitely, no source was found.
1411 struct isl_union_flow
{
1412 isl_union_map
*must_dep
;
1413 isl_union_map
*may_dep
;
1414 isl_union_map
*must_no_source
;
1415 isl_union_map
*may_no_source
;
1418 /* Free "flow" and return NULL.
1420 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
1424 isl_union_map_free(flow
->must_dep
);
1425 isl_union_map_free(flow
->may_dep
);
1426 isl_union_map_free(flow
->must_no_source
);
1427 isl_union_map_free(flow
->may_no_source
);
1432 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
1437 fprintf(stderr
, "must dependences: ");
1438 isl_union_map_dump(flow
->must_dep
);
1439 fprintf(stderr
, "may dependences: ");
1440 isl_union_map_dump(flow
->may_dep
);
1441 fprintf(stderr
, "must no source: ");
1442 isl_union_map_dump(flow
->must_no_source
);
1443 fprintf(stderr
, "may no source: ");
1444 isl_union_map_dump(flow
->may_no_source
);
1447 /* Return the definite dependences in "flow".
1449 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
1450 __isl_keep isl_union_flow
*flow
)
1454 return isl_union_map_copy(flow
->must_dep
);
1457 /* Return the possible dependences in "flow", including the definite
1460 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
1461 __isl_keep isl_union_flow
*flow
)
1465 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1466 isl_union_map_copy(flow
->may_dep
));
1469 /* Return the non-definite dependences in "flow".
1471 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
1472 __isl_keep isl_union_flow
*flow
)
1476 return isl_union_map_copy(flow
->may_dep
);
1479 /* Return the subset of the sink accesses for which definitely
1480 * no source was found.
1482 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
1483 __isl_keep isl_union_flow
*flow
)
1487 return isl_union_map_copy(flow
->must_no_source
);
1490 /* Return the subset of the sink accesses for which possibly
1491 * no source was found, including those for which definitely
1492 * no source was found.
1494 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
1495 __isl_keep isl_union_flow
*flow
)
1499 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
1500 isl_union_map_copy(flow
->may_no_source
));
1503 /* Return the subset of the sink accesses for which possibly, but not
1504 * definitely, no source was found.
1506 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
1507 __isl_keep isl_union_flow
*flow
)
1511 return isl_union_map_copy(flow
->may_no_source
);
1514 /* Create a new isl_union_flow object, initialized with empty
1515 * dependence relations and sink subsets.
1517 static __isl_give isl_union_flow
*isl_union_flow_alloc(
1518 __isl_take isl_space
*space
)
1521 isl_union_map
*empty
;
1522 isl_union_flow
*flow
;
1526 ctx
= isl_space_get_ctx(space
);
1527 flow
= isl_alloc_type(ctx
, isl_union_flow
);
1531 empty
= isl_union_map_empty(space
);
1532 flow
->must_dep
= isl_union_map_copy(empty
);
1533 flow
->may_dep
= isl_union_map_copy(empty
);
1534 flow
->must_no_source
= isl_union_map_copy(empty
);
1535 flow
->may_no_source
= empty
;
1537 if (!flow
->must_dep
|| !flow
->may_dep
||
1538 !flow
->must_no_source
|| !flow
->may_no_source
)
1539 return isl_union_flow_free(flow
);
1543 isl_space_free(space
);
1547 /* Drop the schedule dimensions from the iteration domains in "flow".
1548 * In particular, the schedule dimensions have been prepended
1549 * to the iteration domains prior to the dependence analysis by
1550 * replacing the iteration domain D, by the wrapped map [S -> D].
1551 * Replace these wrapped maps by the original D.
1553 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
1554 __isl_take isl_union_flow
*flow
)
1559 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
1560 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
1561 flow
->must_no_source
=
1562 isl_union_map_domain_factor_range(flow
->must_no_source
);
1563 flow
->may_no_source
=
1564 isl_union_map_domain_factor_range(flow
->may_no_source
);
1566 if (!flow
->must_dep
|| !flow
->may_dep
||
1567 !flow
->must_no_source
|| !flow
->may_no_source
)
1568 return isl_union_flow_free(flow
);
1573 struct isl_compute_flow_data
{
1574 isl_union_map
*must_source
;
1575 isl_union_map
*may_source
;
1576 isl_union_flow
*flow
;
1581 struct isl_sched_info
*sink_info
;
1582 struct isl_sched_info
**source_info
;
1583 isl_access_info
*accesses
;
1586 static int count_matching_array(__isl_take isl_map
*map
, void *user
)
1590 struct isl_compute_flow_data
*data
;
1592 data
= (struct isl_compute_flow_data
*)user
;
1594 dim
= isl_space_range(isl_map_get_space(map
));
1596 eq
= isl_space_is_equal(dim
, data
->dim
);
1598 isl_space_free(dim
);
1609 static int collect_matching_array(__isl_take isl_map
*map
, void *user
)
1613 struct isl_sched_info
*info
;
1614 struct isl_compute_flow_data
*data
;
1616 data
= (struct isl_compute_flow_data
*)user
;
1618 dim
= isl_space_range(isl_map_get_space(map
));
1620 eq
= isl_space_is_equal(dim
, data
->dim
);
1622 isl_space_free(dim
);
1631 info
= sched_info_alloc(map
);
1632 data
->source_info
[data
->count
] = info
;
1634 data
->accesses
= isl_access_info_add_source(data
->accesses
,
1635 map
, data
->must
, info
);
1645 /* Determine the shared nesting level and the "textual order" of
1646 * the given accesses.
1648 * We first determine the minimal schedule dimension for both accesses.
1650 * If among those dimensions, we can find one where both have a fixed
1651 * value and if moreover those values are different, then the previous
1652 * dimension is the last shared nesting level and the textual order
1653 * is determined based on the order of the fixed values.
1654 * If no such fixed values can be found, then we set the shared
1655 * nesting level to the minimal schedule dimension, with no textual ordering.
1657 static int before(void *first
, void *second
)
1659 struct isl_sched_info
*info1
= first
;
1660 struct isl_sched_info
*info2
= second
;
1664 n1
= isl_vec_size(info1
->cst
);
1665 n2
= isl_vec_size(info2
->cst
);
1670 for (i
= 0; i
< n1
; ++i
) {
1674 if (!info1
->is_cst
[i
])
1676 if (!info2
->is_cst
[i
])
1678 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
1682 r
= 2 * i
+ (cmp
< 0);
1690 /* Given a sink access, look for all the source accesses that access
1691 * the same array and perform dataflow analysis on them using
1692 * isl_access_info_compute_flow.
1694 static int compute_flow(__isl_take isl_map
*map
, void *user
)
1698 struct isl_compute_flow_data
*data
;
1702 data
= (struct isl_compute_flow_data
*)user
;
1705 ctx
= isl_map_get_ctx(map
);
1707 data
->accesses
= NULL
;
1708 data
->sink_info
= NULL
;
1709 data
->source_info
= NULL
;
1711 data
->dim
= isl_space_range(isl_map_get_space(map
));
1713 if (isl_union_map_foreach_map(data
->must_source
,
1714 &count_matching_array
, data
) < 0)
1716 if (isl_union_map_foreach_map(data
->may_source
,
1717 &count_matching_array
, data
) < 0)
1720 data
->sink_info
= sched_info_alloc(map
);
1721 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
1724 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
1725 data
->sink_info
, &before
, data
->count
);
1726 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
1731 if (isl_union_map_foreach_map(data
->must_source
,
1732 &collect_matching_array
, data
) < 0)
1735 if (isl_union_map_foreach_map(data
->may_source
,
1736 &collect_matching_array
, data
) < 0)
1739 flow
= isl_access_info_compute_flow(data
->accesses
);
1740 data
->accesses
= NULL
;
1745 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
1746 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
1747 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
1748 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
1750 for (i
= 0; i
< flow
->n_source
; ++i
) {
1752 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
1753 if (flow
->dep
[i
].must
)
1754 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
1756 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
1759 isl_flow_free(flow
);
1761 sched_info_free(data
->sink_info
);
1762 if (data
->source_info
) {
1763 for (i
= 0; i
< data
->count
; ++i
)
1764 sched_info_free(data
->source_info
[i
]);
1765 free(data
->source_info
);
1767 isl_space_free(data
->dim
);
1772 isl_access_info_free(data
->accesses
);
1773 sched_info_free(data
->sink_info
);
1774 if (data
->source_info
) {
1775 for (i
= 0; i
< data
->count
; ++i
)
1776 sched_info_free(data
->source_info
[i
]);
1777 free(data
->source_info
);
1779 isl_space_free(data
->dim
);
1785 /* Remove the must accesses from the may accesses.
1787 * A must access always trumps a may access, so there is no need
1788 * for a must access to also be considered as a may access. Doing so
1789 * would only cost extra computations only to find out that
1790 * the duplicated may access does not make any difference.
1792 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
1793 __isl_take isl_union_access_info
*access
)
1797 access
->may_source
= isl_union_map_subtract(access
->may_source
,
1798 isl_union_map_copy(access
->must_source
));
1799 if (!access
->may_source
)
1800 return isl_union_access_info_free(access
);
1805 /* Given a description of the "sink" accesses, the "source" accesses and
1806 * a schedule, compute for each instance of a sink access
1807 * and for each element accessed by that instance,
1808 * the possible or definite source accesses that last accessed the
1809 * element accessed by the sink access before this sink access
1810 * in the sense that there is no intermediate definite source access.
1812 * The must_no_source and may_no_source elements of the result
1813 * are subsets of access->sink. The elements must_dep and may_dep
1814 * map domain elements of access->{may,must)_source to
1815 * domain elements of access->sink.
1817 * We first prepend the schedule dimensions to the domain
1818 * of the accesses so that we can easily compare their relative order.
1819 * Then we consider each sink access individually in compute_flow.
1821 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
1822 __isl_take isl_union_access_info
*access
)
1824 struct isl_compute_flow_data data
;
1826 access
= isl_union_access_info_normalize(access
);
1827 access
= isl_union_access_info_align_params(access
);
1828 access
= isl_union_access_info_introduce_schedule(access
);
1832 data
.must_source
= access
->must_source
;
1833 data
.may_source
= access
->may_source
;
1835 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
1837 if (isl_union_map_foreach_map(access
->sink
, &compute_flow
, &data
) < 0)
1840 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
1842 isl_union_access_info_free(access
);
1845 isl_union_access_info_free(access
);
1846 isl_union_flow_free(data
.flow
);
1850 /* Given a collection of "sink" and "source" accesses,
1851 * compute for each iteration of a sink access
1852 * and for each element accessed by that iteration,
1853 * the source access in the list that last accessed the
1854 * element accessed by the sink access before this sink access.
1855 * Each access is given as a map from the loop iterators
1856 * to the array indices.
1857 * The result is a relations between source and sink
1858 * iterations and a subset of the domain of the sink accesses,
1859 * corresponding to those iterations that access an element
1860 * not previously accessed.
1862 * We collect the inputs in an isl_union_access_info object,
1863 * call isl_union_access_info_compute_flow and extract
1864 * the outputs from the result.
1866 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
1867 __isl_take isl_union_map
*must_source
,
1868 __isl_take isl_union_map
*may_source
,
1869 __isl_take isl_union_map
*schedule
,
1870 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
1871 __isl_give isl_union_map
**must_no_source
,
1872 __isl_give isl_union_map
**may_no_source
)
1874 isl_union_access_info
*access
;
1875 isl_union_flow
*flow
;
1877 access
= isl_union_access_info_from_sink(sink
);
1878 access
= isl_union_access_info_set_must_source(access
, must_source
);
1879 access
= isl_union_access_info_set_may_source(access
, may_source
);
1880 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
1881 flow
= isl_union_access_info_compute_flow(access
);
1884 *must_dep
= isl_union_flow_get_must_dependence(flow
);
1886 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
1888 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
1890 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
1892 isl_union_flow_free(flow
);
1894 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
1895 (must_no_source
&& !*must_no_source
) ||
1896 (may_no_source
&& !*may_no_source
))
1902 *must_dep
= isl_union_map_free(*must_dep
);
1904 *may_dep
= isl_union_map_free(*may_dep
);
1906 *must_no_source
= isl_union_map_free(*must_no_source
);
1908 *may_no_source
= isl_union_map_free(*may_no_source
);