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 "schedule_map".
1289 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1290 __isl_take isl_union_access_info
*access
,
1291 __isl_take isl_union_map
*schedule_map
)
1293 if (!access
|| !schedule_map
)
1296 isl_union_map_free(access
->schedule_map
);
1297 access
->schedule_map
= schedule_map
;
1301 isl_union_access_info_free(access
);
1302 isl_union_map_free(schedule_map
);
1306 /* Update the fields of "access" such that they all have the same parameters.
1308 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1309 __isl_take isl_union_access_info
*access
)
1316 space
= isl_union_map_get_space(access
->sink
);
1317 space
= isl_space_align_params(space
,
1318 isl_union_map_get_space(access
->must_source
));
1319 space
= isl_space_align_params(space
,
1320 isl_union_map_get_space(access
->may_source
));
1321 space
= isl_space_align_params(space
,
1322 isl_union_map_get_space(access
->schedule_map
));
1323 access
->sink
= isl_union_map_align_params(access
->sink
,
1324 isl_space_copy(space
));
1325 access
->must_source
= isl_union_map_align_params(access
->must_source
,
1326 isl_space_copy(space
));
1327 access
->may_source
= isl_union_map_align_params(access
->may_source
,
1328 isl_space_copy(space
));
1329 access
->schedule_map
= isl_union_map_align_params(access
->schedule_map
,
1332 if (!access
->sink
|| !access
->must_source
||
1333 !access
->may_source
|| !access
->schedule_map
)
1334 return isl_union_access_info_free(access
);
1339 /* Prepend the schedule dimensions to the iteration domains.
1341 * That is, if the schedule is of the form
1345 * while the access relations are of the form
1349 * then the updated access relations are of the form
1353 * The schedule map is also replaced by the map
1357 * that is used during the internal computation.
1358 * Neither the original schedule map nor this updated schedule map
1359 * are used after the call to this function.
1361 static __isl_give isl_union_access_info
*
1362 isl_union_access_info_introduce_schedule(
1363 __isl_take isl_union_access_info
*access
)
1370 sm
= isl_union_map_reverse(access
->schedule_map
);
1371 sm
= isl_union_map_range_map(sm
);
1372 access
->sink
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1374 access
->may_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1375 access
->may_source
);
1376 access
->must_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1377 access
->must_source
);
1378 access
->schedule_map
= sm
;
1380 if (!access
->sink
|| !access
->must_source
||
1381 !access
->may_source
|| !access
->schedule_map
)
1382 return isl_union_access_info_free(access
);
1387 /* This structure epresents the result of a dependence analysis computation.
1389 * "must_dep" represents the definite dependences.
1390 * "may_dep" represents the non-definite dependences.
1391 * "must_no_source" represents the subset of the sink accesses for which
1392 * definitely no source was found.
1393 * "may_no_source" represents the subset of the sink accesses for which
1394 * possibly, but not definitely, no source was found.
1396 struct isl_union_flow
{
1397 isl_union_map
*must_dep
;
1398 isl_union_map
*may_dep
;
1399 isl_union_map
*must_no_source
;
1400 isl_union_map
*may_no_source
;
1403 /* Free "flow" and return NULL.
1405 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
1409 isl_union_map_free(flow
->must_dep
);
1410 isl_union_map_free(flow
->may_dep
);
1411 isl_union_map_free(flow
->must_no_source
);
1412 isl_union_map_free(flow
->may_no_source
);
1417 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
1422 fprintf(stderr
, "must dependences: ");
1423 isl_union_map_dump(flow
->must_dep
);
1424 fprintf(stderr
, "may dependences: ");
1425 isl_union_map_dump(flow
->may_dep
);
1426 fprintf(stderr
, "must no source: ");
1427 isl_union_map_dump(flow
->must_no_source
);
1428 fprintf(stderr
, "may no source: ");
1429 isl_union_map_dump(flow
->may_no_source
);
1432 /* Return the definite dependences in "flow".
1434 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
1435 __isl_keep isl_union_flow
*flow
)
1439 return isl_union_map_copy(flow
->must_dep
);
1442 /* Return the possible dependences in "flow", including the definite
1445 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
1446 __isl_keep isl_union_flow
*flow
)
1450 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1451 isl_union_map_copy(flow
->may_dep
));
1454 /* Return the non-definite dependences in "flow".
1456 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
1457 __isl_keep isl_union_flow
*flow
)
1461 return isl_union_map_copy(flow
->may_dep
);
1464 /* Return the subset of the sink accesses for which definitely
1465 * no source was found.
1467 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
1468 __isl_keep isl_union_flow
*flow
)
1472 return isl_union_map_copy(flow
->must_no_source
);
1475 /* Return the subset of the sink accesses for which possibly
1476 * no source was found, including those for which definitely
1477 * no source was found.
1479 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
1480 __isl_keep isl_union_flow
*flow
)
1484 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
1485 isl_union_map_copy(flow
->may_no_source
));
1488 /* Return the subset of the sink accesses for which possibly, but not
1489 * definitely, no source was found.
1491 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
1492 __isl_keep isl_union_flow
*flow
)
1496 return isl_union_map_copy(flow
->may_no_source
);
1499 /* Create a new isl_union_flow object, initialized with empty
1500 * dependence relations and sink subsets.
1502 static __isl_give isl_union_flow
*isl_union_flow_alloc(
1503 __isl_take isl_space
*space
)
1506 isl_union_map
*empty
;
1507 isl_union_flow
*flow
;
1511 ctx
= isl_space_get_ctx(space
);
1512 flow
= isl_alloc_type(ctx
, isl_union_flow
);
1516 empty
= isl_union_map_empty(space
);
1517 flow
->must_dep
= isl_union_map_copy(empty
);
1518 flow
->may_dep
= isl_union_map_copy(empty
);
1519 flow
->must_no_source
= isl_union_map_copy(empty
);
1520 flow
->may_no_source
= empty
;
1522 if (!flow
->must_dep
|| !flow
->may_dep
||
1523 !flow
->must_no_source
|| !flow
->may_no_source
)
1524 return isl_union_flow_free(flow
);
1528 isl_space_free(space
);
1532 /* Drop the schedule dimensions from the iteration domains in "flow".
1533 * In particular, the schedule dimensions have been prepended
1534 * to the iteration domains prior to the dependence analysis by
1535 * replacing the iteration domain D, by the wrapped map [S -> D].
1536 * Replace these wrapped maps by the original D.
1538 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
1539 __isl_take isl_union_flow
*flow
)
1544 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
1545 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
1546 flow
->must_no_source
=
1547 isl_union_map_domain_factor_range(flow
->must_no_source
);
1548 flow
->may_no_source
=
1549 isl_union_map_domain_factor_range(flow
->may_no_source
);
1551 if (!flow
->must_dep
|| !flow
->may_dep
||
1552 !flow
->must_no_source
|| !flow
->may_no_source
)
1553 return isl_union_flow_free(flow
);
1558 struct isl_compute_flow_data
{
1559 isl_union_map
*must_source
;
1560 isl_union_map
*may_source
;
1561 isl_union_flow
*flow
;
1566 struct isl_sched_info
*sink_info
;
1567 struct isl_sched_info
**source_info
;
1568 isl_access_info
*accesses
;
1571 static int count_matching_array(__isl_take isl_map
*map
, void *user
)
1575 struct isl_compute_flow_data
*data
;
1577 data
= (struct isl_compute_flow_data
*)user
;
1579 dim
= isl_space_range(isl_map_get_space(map
));
1581 eq
= isl_space_is_equal(dim
, data
->dim
);
1583 isl_space_free(dim
);
1594 static int collect_matching_array(__isl_take isl_map
*map
, void *user
)
1598 struct isl_sched_info
*info
;
1599 struct isl_compute_flow_data
*data
;
1601 data
= (struct isl_compute_flow_data
*)user
;
1603 dim
= isl_space_range(isl_map_get_space(map
));
1605 eq
= isl_space_is_equal(dim
, data
->dim
);
1607 isl_space_free(dim
);
1616 info
= sched_info_alloc(map
);
1617 data
->source_info
[data
->count
] = info
;
1619 data
->accesses
= isl_access_info_add_source(data
->accesses
,
1620 map
, data
->must
, info
);
1630 /* Determine the shared nesting level and the "textual order" of
1631 * the given accesses.
1633 * We first determine the minimal schedule dimension for both accesses.
1635 * If among those dimensions, we can find one where both have a fixed
1636 * value and if moreover those values are different, then the previous
1637 * dimension is the last shared nesting level and the textual order
1638 * is determined based on the order of the fixed values.
1639 * If no such fixed values can be found, then we set the shared
1640 * nesting level to the minimal schedule dimension, with no textual ordering.
1642 static int before(void *first
, void *second
)
1644 struct isl_sched_info
*info1
= first
;
1645 struct isl_sched_info
*info2
= second
;
1649 n1
= isl_vec_size(info1
->cst
);
1650 n2
= isl_vec_size(info2
->cst
);
1655 for (i
= 0; i
< n1
; ++i
) {
1659 if (!info1
->is_cst
[i
])
1661 if (!info2
->is_cst
[i
])
1663 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
1667 r
= 2 * i
+ (cmp
< 0);
1675 /* Given a sink access, look for all the source accesses that access
1676 * the same array and perform dataflow analysis on them using
1677 * isl_access_info_compute_flow.
1679 static int compute_flow(__isl_take isl_map
*map
, void *user
)
1683 struct isl_compute_flow_data
*data
;
1687 data
= (struct isl_compute_flow_data
*)user
;
1690 ctx
= isl_map_get_ctx(map
);
1692 data
->accesses
= NULL
;
1693 data
->sink_info
= NULL
;
1694 data
->source_info
= NULL
;
1696 data
->dim
= isl_space_range(isl_map_get_space(map
));
1698 if (isl_union_map_foreach_map(data
->must_source
,
1699 &count_matching_array
, data
) < 0)
1701 if (isl_union_map_foreach_map(data
->may_source
,
1702 &count_matching_array
, data
) < 0)
1705 data
->sink_info
= sched_info_alloc(map
);
1706 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
1709 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
1710 data
->sink_info
, &before
, data
->count
);
1711 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
1716 if (isl_union_map_foreach_map(data
->must_source
,
1717 &collect_matching_array
, data
) < 0)
1720 if (isl_union_map_foreach_map(data
->may_source
,
1721 &collect_matching_array
, data
) < 0)
1724 flow
= isl_access_info_compute_flow(data
->accesses
);
1725 data
->accesses
= NULL
;
1730 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
1731 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
1732 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
1733 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
1735 for (i
= 0; i
< flow
->n_source
; ++i
) {
1737 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
1738 if (flow
->dep
[i
].must
)
1739 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
1741 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
1744 isl_flow_free(flow
);
1746 sched_info_free(data
->sink_info
);
1747 if (data
->source_info
) {
1748 for (i
= 0; i
< data
->count
; ++i
)
1749 sched_info_free(data
->source_info
[i
]);
1750 free(data
->source_info
);
1752 isl_space_free(data
->dim
);
1757 isl_access_info_free(data
->accesses
);
1758 sched_info_free(data
->sink_info
);
1759 if (data
->source_info
) {
1760 for (i
= 0; i
< data
->count
; ++i
)
1761 sched_info_free(data
->source_info
[i
]);
1762 free(data
->source_info
);
1764 isl_space_free(data
->dim
);
1770 /* Remove the must accesses from the may accesses.
1772 * A must access always trumps a may access, so there is no need
1773 * for a must access to also be considered as a may access. Doing so
1774 * would only cost extra computations only to find out that
1775 * the duplicated may access does not make any difference.
1777 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
1778 __isl_take isl_union_access_info
*access
)
1782 access
->may_source
= isl_union_map_subtract(access
->may_source
,
1783 isl_union_map_copy(access
->must_source
));
1784 if (!access
->may_source
)
1785 return isl_union_access_info_free(access
);
1790 /* Given a description of the "sink" accesses, the "source" accesses and
1791 * a schedule, compute for each instance of a sink access
1792 * and for each element accessed by that instance,
1793 * the possible or definite source accesses that last accessed the
1794 * element accessed by the sink access before this sink access
1795 * in the sense that there is no intermediate definite source access.
1797 * The must_no_source and may_no_source elements of the result
1798 * are subsets of access->sink. The elements must_dep and may_dep
1799 * map domain elements of access->{may,must)_source to
1800 * domain elements of access->sink.
1802 * We first prepend the schedule dimensions to the domain
1803 * of the accesses so that we can easily compare their relative order.
1804 * Then we consider each sink access individually in compute_flow.
1806 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
1807 __isl_take isl_union_access_info
*access
)
1809 struct isl_compute_flow_data data
;
1811 access
= isl_union_access_info_normalize(access
);
1812 access
= isl_union_access_info_align_params(access
);
1813 access
= isl_union_access_info_introduce_schedule(access
);
1817 data
.must_source
= access
->must_source
;
1818 data
.may_source
= access
->may_source
;
1820 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
1822 if (isl_union_map_foreach_map(access
->sink
, &compute_flow
, &data
) < 0)
1825 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
1827 isl_union_access_info_free(access
);
1830 isl_union_access_info_free(access
);
1831 isl_union_flow_free(data
.flow
);
1835 /* Given a collection of "sink" and "source" accesses,
1836 * compute for each iteration of a sink access
1837 * and for each element accessed by that iteration,
1838 * the source access in the list that last accessed the
1839 * element accessed by the sink access before this sink access.
1840 * Each access is given as a map from the loop iterators
1841 * to the array indices.
1842 * The result is a relations between source and sink
1843 * iterations and a subset of the domain of the sink accesses,
1844 * corresponding to those iterations that access an element
1845 * not previously accessed.
1847 * We first prepend the schedule dimensions to the domain
1848 * of the accesses so that we can easily compare their relative order.
1849 * Then we consider each sink access individually in compute_flow.
1851 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
1852 __isl_take isl_union_map
*must_source
,
1853 __isl_take isl_union_map
*may_source
,
1854 __isl_take isl_union_map
*schedule
,
1855 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
1856 __isl_give isl_union_map
**must_no_source
,
1857 __isl_give isl_union_map
**may_no_source
)
1860 struct isl_compute_flow_data data
;
1862 sink
= isl_union_map_align_params(sink
,
1863 isl_union_map_get_space(must_source
));
1864 sink
= isl_union_map_align_params(sink
,
1865 isl_union_map_get_space(may_source
));
1866 sink
= isl_union_map_align_params(sink
,
1867 isl_union_map_get_space(schedule
));
1868 space
= isl_union_map_get_space(sink
);
1869 must_source
= isl_union_map_align_params(must_source
,
1870 isl_space_copy(space
));
1871 may_source
= isl_union_map_align_params(may_source
,
1872 isl_space_copy(space
));
1873 schedule
= isl_union_map_align_params(schedule
, isl_space_copy(space
));
1875 schedule
= isl_union_map_reverse(schedule
);
1876 schedule
= isl_union_map_reverse(isl_union_map_range_map(schedule
));
1877 sink
= isl_union_map_apply_domain(sink
, isl_union_map_copy(schedule
));
1878 must_source
= isl_union_map_apply_domain(must_source
,
1879 isl_union_map_copy(schedule
));
1880 may_source
= isl_union_map_apply_domain(may_source
, schedule
);
1882 data
.must_source
= must_source
;
1883 data
.may_source
= may_source
;
1884 data
.flow
= isl_union_flow_alloc(space
);
1886 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
1889 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
1893 isl_union_map_free(sink
);
1894 isl_union_map_free(must_source
);
1895 isl_union_map_free(may_source
);
1898 *must_dep
= isl_union_flow_get_must_dependence(data
.flow
);
1900 *may_dep
= isl_union_flow_get_non_must_dependence(data
.flow
);
1902 *must_no_source
= isl_union_flow_get_must_no_source(data
.flow
);
1905 isl_union_flow_get_non_must_no_source(data
.flow
);
1907 isl_union_flow_free(data
.flow
);
1911 isl_union_map_free(sink
);
1912 isl_union_map_free(must_source
);
1913 isl_union_map_free(may_source
);
1914 isl_union_flow_free(data
.flow
);
1921 *must_no_source
= NULL
;
1923 *may_no_source
= NULL
;