2 * Copyright 2005-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
5 * Copyright 2012 Universiteit Leiden
6 * Copyright 2014 Ecole Normale Superieure
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
12 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
13 * B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
24 #include <isl/schedule_node.h>
27 enum isl_restriction_type
{
28 isl_restriction_type_empty
,
29 isl_restriction_type_none
,
30 isl_restriction_type_input
,
31 isl_restriction_type_output
34 struct isl_restriction
{
35 enum isl_restriction_type type
;
41 /* Create a restriction of the given type.
43 static __isl_give isl_restriction
*isl_restriction_alloc(
44 __isl_take isl_map
*source_map
, enum isl_restriction_type type
)
47 isl_restriction
*restr
;
52 ctx
= isl_map_get_ctx(source_map
);
53 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
59 isl_map_free(source_map
);
62 isl_map_free(source_map
);
66 /* Create a restriction that doesn't restrict anything.
68 __isl_give isl_restriction
*isl_restriction_none(__isl_take isl_map
*source_map
)
70 return isl_restriction_alloc(source_map
, isl_restriction_type_none
);
73 /* Create a restriction that removes everything.
75 __isl_give isl_restriction
*isl_restriction_empty(
76 __isl_take isl_map
*source_map
)
78 return isl_restriction_alloc(source_map
, isl_restriction_type_empty
);
81 /* Create a restriction on the input of the maximization problem
82 * based on the given source and sink restrictions.
84 __isl_give isl_restriction
*isl_restriction_input(
85 __isl_take isl_set
*source_restr
, __isl_take isl_set
*sink_restr
)
88 isl_restriction
*restr
;
90 if (!source_restr
|| !sink_restr
)
93 ctx
= isl_set_get_ctx(source_restr
);
94 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
98 restr
->type
= isl_restriction_type_input
;
99 restr
->source
= source_restr
;
100 restr
->sink
= sink_restr
;
104 isl_set_free(source_restr
);
105 isl_set_free(sink_restr
);
109 /* Create a restriction on the output of the maximization problem
110 * based on the given source restriction.
112 __isl_give isl_restriction
*isl_restriction_output(
113 __isl_take isl_set
*source_restr
)
116 isl_restriction
*restr
;
121 ctx
= isl_set_get_ctx(source_restr
);
122 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
126 restr
->type
= isl_restriction_type_output
;
127 restr
->source
= source_restr
;
131 isl_set_free(source_restr
);
135 __isl_null isl_restriction
*isl_restriction_free(
136 __isl_take isl_restriction
*restr
)
141 isl_set_free(restr
->source
);
142 isl_set_free(restr
->sink
);
147 isl_ctx
*isl_restriction_get_ctx(__isl_keep isl_restriction
*restr
)
149 return restr
? isl_set_get_ctx(restr
->source
) : NULL
;
152 /* A private structure to keep track of a mapping together with
153 * a user-specified identifier and a boolean indicating whether
154 * the map represents a must or may access/dependence.
156 struct isl_labeled_map
{
162 /* A structure containing the input for dependence analysis:
164 * - n_must + n_may (<= max_source) sources
165 * - a function for determining the relative order of sources and sink
166 * The must sources are placed before the may sources.
168 * domain_map is an auxiliary map that maps the sink access relation
169 * to the domain of this access relation.
170 * This field is only needed when restrict_fn is set and
171 * the field itself is set by isl_access_info_compute_flow.
173 * restrict_fn is a callback that (if not NULL) will be called
174 * right before any lexicographical maximization.
176 struct isl_access_info
{
178 struct isl_labeled_map sink
;
179 isl_access_level_before level_before
;
181 isl_access_restrict restrict_fn
;
187 struct isl_labeled_map source
[1];
190 /* A structure containing the output of dependence analysis:
191 * - n_source dependences
192 * - a wrapped subset of the sink for which definitely no source could be found
193 * - a wrapped subset of the sink for which possibly no source could be found
196 isl_set
*must_no_source
;
197 isl_set
*may_no_source
;
199 struct isl_labeled_map
*dep
;
202 /* Construct an isl_access_info structure and fill it up with
203 * the given data. The number of sources is set to 0.
205 __isl_give isl_access_info
*isl_access_info_alloc(__isl_take isl_map
*sink
,
206 void *sink_user
, isl_access_level_before fn
, int max_source
)
209 struct isl_access_info
*acc
;
214 ctx
= isl_map_get_ctx(sink
);
215 isl_assert(ctx
, max_source
>= 0, goto error
);
217 acc
= isl_calloc(ctx
, struct isl_access_info
,
218 sizeof(struct isl_access_info
) +
219 (max_source
- 1) * sizeof(struct isl_labeled_map
));
223 acc
->sink
.map
= sink
;
224 acc
->sink
.data
= sink_user
;
225 acc
->level_before
= fn
;
226 acc
->max_source
= max_source
;
236 /* Free the given isl_access_info structure.
238 __isl_null isl_access_info
*isl_access_info_free(
239 __isl_take isl_access_info
*acc
)
245 isl_map_free(acc
->domain_map
);
246 isl_map_free(acc
->sink
.map
);
247 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
248 isl_map_free(acc
->source
[i
].map
);
253 isl_ctx
*isl_access_info_get_ctx(__isl_keep isl_access_info
*acc
)
255 return acc
? isl_map_get_ctx(acc
->sink
.map
) : NULL
;
258 __isl_give isl_access_info
*isl_access_info_set_restrict(
259 __isl_take isl_access_info
*acc
, isl_access_restrict fn
, void *user
)
263 acc
->restrict_fn
= fn
;
264 acc
->restrict_user
= user
;
268 /* Add another source to an isl_access_info structure, making
269 * sure the "must" sources are placed before the "may" sources.
270 * This function may be called at most max_source times on a
271 * given isl_access_info structure, with max_source as specified
272 * in the call to isl_access_info_alloc that constructed the structure.
274 __isl_give isl_access_info
*isl_access_info_add_source(
275 __isl_take isl_access_info
*acc
, __isl_take isl_map
*source
,
276 int must
, void *source_user
)
282 ctx
= isl_map_get_ctx(acc
->sink
.map
);
283 isl_assert(ctx
, acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
287 acc
->source
[acc
->n_must
+ acc
->n_may
] =
288 acc
->source
[acc
->n_must
];
289 acc
->source
[acc
->n_must
].map
= source
;
290 acc
->source
[acc
->n_must
].data
= source_user
;
291 acc
->source
[acc
->n_must
].must
= 1;
294 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
295 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
296 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
302 isl_map_free(source
);
303 isl_access_info_free(acc
);
307 /* Return -n, 0 or n (with n a positive value), depending on whether
308 * the source access identified by p1 should be sorted before, together
309 * or after that identified by p2.
311 * If p1 appears before p2, then it should be sorted first.
312 * For more generic initial schedules, it is possible that neither
313 * p1 nor p2 appears before the other, or at least not in any obvious way.
314 * We therefore also check if p2 appears before p1, in which case p2
315 * should be sorted first.
316 * If not, we try to order the two statements based on the description
317 * of the iteration domains. This results in an arbitrary, but fairly
320 static int access_sort_cmp(const void *p1
, const void *p2
, void *user
)
322 isl_access_info
*acc
= user
;
323 const struct isl_labeled_map
*i1
, *i2
;
326 i1
= (const struct isl_labeled_map
*) p1
;
327 i2
= (const struct isl_labeled_map
*) p2
;
329 level1
= acc
->level_before(i1
->data
, i2
->data
);
333 level2
= acc
->level_before(i2
->data
, i1
->data
);
337 h1
= isl_map_get_hash(i1
->map
);
338 h2
= isl_map_get_hash(i2
->map
);
339 return h1
> h2
? 1 : h1
< h2
? -1 : 0;
342 /* Sort the must source accesses in their textual order.
344 static __isl_give isl_access_info
*isl_access_info_sort_sources(
345 __isl_take isl_access_info
*acc
)
349 if (acc
->n_must
<= 1)
352 if (isl_sort(acc
->source
, acc
->n_must
, sizeof(struct isl_labeled_map
),
353 access_sort_cmp
, acc
) < 0)
354 return isl_access_info_free(acc
);
359 /* Align the parameters of the two spaces if needed and then call
362 static __isl_give isl_space
*space_align_and_join(__isl_take isl_space
*left
,
363 __isl_take isl_space
*right
)
365 isl_bool equal_params
;
367 equal_params
= isl_space_has_equal_params(left
, right
);
368 if (equal_params
< 0)
371 return isl_space_join(left
, right
);
373 left
= isl_space_align_params(left
, isl_space_copy(right
));
374 right
= isl_space_align_params(right
, isl_space_copy(left
));
375 return isl_space_join(left
, right
);
377 isl_space_free(left
);
378 isl_space_free(right
);
382 /* Initialize an empty isl_flow structure corresponding to a given
383 * isl_access_info structure.
384 * For each must access, two dependences are created (initialized
385 * to the empty relation), one for the resulting must dependences
386 * and one for the resulting may dependences. May accesses can
387 * only lead to may dependences, so only one dependence is created
389 * This function is private as isl_flow structures are only supposed
390 * to be created by isl_access_info_compute_flow.
392 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
396 struct isl_flow
*dep
;
401 ctx
= isl_map_get_ctx(acc
->sink
.map
);
402 dep
= isl_calloc_type(ctx
, struct isl_flow
);
406 n
= 2 * acc
->n_must
+ acc
->n_may
;
407 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
, n
);
412 for (i
= 0; i
< acc
->n_must
; ++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
[2 * i
].map
= isl_map_empty(dim
);
418 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
419 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
420 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
421 dep
->dep
[2 * i
].must
= 1;
422 dep
->dep
[2 * i
+ 1].must
= 0;
423 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
426 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
428 dim
= space_align_and_join(
429 isl_map_get_space(acc
->source
[i
].map
),
430 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
431 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
432 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
433 dep
->dep
[acc
->n_must
+ i
].must
= 0;
434 if (!dep
->dep
[acc
->n_must
+ i
].map
)
444 /* Iterate over all sources and for each resulting flow dependence
445 * that is not empty, call the user specfied function.
446 * The second argument in this function call identifies the source,
447 * while the third argument correspond to the final argument of
448 * the isl_flow_foreach call.
450 isl_stat
isl_flow_foreach(__isl_keep isl_flow
*deps
,
451 isl_stat (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
,
458 return isl_stat_error
;
460 for (i
= 0; i
< deps
->n_source
; ++i
) {
461 if (isl_map_plain_is_empty(deps
->dep
[i
].map
))
463 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
464 deps
->dep
[i
].data
, user
) < 0)
465 return isl_stat_error
;
471 /* Return a copy of the subset of the sink for which no source could be found.
473 __isl_give isl_map
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
479 return isl_set_unwrap(isl_set_copy(deps
->must_no_source
));
481 return isl_set_unwrap(isl_set_copy(deps
->may_no_source
));
484 void isl_flow_free(__isl_take isl_flow
*deps
)
490 isl_set_free(deps
->must_no_source
);
491 isl_set_free(deps
->may_no_source
);
493 for (i
= 0; i
< deps
->n_source
; ++i
)
494 isl_map_free(deps
->dep
[i
].map
);
500 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
502 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
505 /* Return a map that enforces that the domain iteration occurs after
506 * the range iteration at the given level.
507 * If level is odd, then the domain iteration should occur after
508 * the target iteration in their shared level/2 outermost loops.
509 * In this case we simply need to enforce that these outermost
510 * loop iterations are the same.
511 * If level is even, then the loop iterator of the domain should
512 * be greater than the loop iterator of the range at the last
513 * of the level/2 shared loops, i.e., loop level/2 - 1.
515 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
517 struct isl_basic_map
*bmap
;
520 bmap
= isl_basic_map_equal(dim
, level
/2);
522 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
524 return isl_map_from_basic_map(bmap
);
527 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
528 * but first check if the user has set acc->restrict_fn and if so
529 * update either the input or the output of the maximization problem
530 * with respect to the resulting restriction.
532 * Since the user expects a mapping from sink iterations to source iterations,
533 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
534 * to accessed array elements, we first need to project out the accessed
535 * sink array elements by applying acc->domain_map.
536 * Similarly, the sink restriction specified by the user needs to be
537 * converted back to the wrapped map.
539 static __isl_give isl_map
*restricted_partial_lexmax(
540 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
541 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
544 isl_restriction
*restr
;
545 isl_set
*sink_domain
;
549 if (!acc
->restrict_fn
)
550 return isl_map_partial_lexmax(dep
, sink
, empty
);
552 source_map
= isl_map_copy(dep
);
553 source_map
= isl_map_apply_domain(source_map
,
554 isl_map_copy(acc
->domain_map
));
555 sink_domain
= isl_set_copy(sink
);
556 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
557 restr
= acc
->restrict_fn(source_map
, sink_domain
,
558 acc
->source
[source
].data
, acc
->restrict_user
);
559 isl_set_free(sink_domain
);
560 isl_map_free(source_map
);
564 if (restr
->type
== isl_restriction_type_input
) {
565 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
566 sink_restr
= isl_set_copy(restr
->sink
);
567 sink_restr
= isl_set_apply(sink_restr
,
568 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
569 sink
= isl_set_intersect(sink
, sink_restr
);
570 } else if (restr
->type
== isl_restriction_type_empty
) {
571 isl_space
*space
= isl_map_get_space(dep
);
573 dep
= isl_map_empty(space
);
576 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
578 if (restr
->type
== isl_restriction_type_output
)
579 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
581 isl_restriction_free(restr
);
590 /* Compute the last iteration of must source j that precedes the sink
591 * at the given level for sink iterations in set_C.
592 * The subset of set_C for which no such iteration can be found is returned
595 static struct isl_map
*last_source(struct isl_access_info
*acc
,
596 struct isl_set
*set_C
,
597 int j
, int level
, struct isl_set
**empty
)
599 struct isl_map
*read_map
;
600 struct isl_map
*write_map
;
601 struct isl_map
*dep_map
;
602 struct isl_map
*after
;
603 struct isl_map
*result
;
605 read_map
= isl_map_copy(acc
->sink
.map
);
606 write_map
= isl_map_copy(acc
->source
[j
].map
);
607 write_map
= isl_map_reverse(write_map
);
608 dep_map
= isl_map_apply_range(read_map
, write_map
);
609 after
= after_at_level(isl_map_get_space(dep_map
), level
);
610 dep_map
= isl_map_intersect(dep_map
, after
);
611 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
612 result
= isl_map_reverse(result
);
617 /* For a given mapping between iterations of must source j and iterations
618 * of the sink, compute the last iteration of must source k preceding
619 * the sink at level before_level for any of the sink iterations,
620 * but following the corresponding iteration of must source j at level
623 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
624 struct isl_map
*old_map
,
625 int j
, int before_level
,
626 int k
, int after_level
,
627 struct isl_set
**empty
)
630 struct isl_set
*set_C
;
631 struct isl_map
*read_map
;
632 struct isl_map
*write_map
;
633 struct isl_map
*dep_map
;
634 struct isl_map
*after_write
;
635 struct isl_map
*before_read
;
636 struct isl_map
*result
;
638 set_C
= isl_map_range(isl_map_copy(old_map
));
639 read_map
= isl_map_copy(acc
->sink
.map
);
640 write_map
= isl_map_copy(acc
->source
[k
].map
);
642 write_map
= isl_map_reverse(write_map
);
643 dep_map
= isl_map_apply_range(read_map
, write_map
);
644 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
645 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
646 after_write
= after_at_level(dim
, after_level
);
647 after_write
= isl_map_apply_range(after_write
, old_map
);
648 after_write
= isl_map_reverse(after_write
);
649 dep_map
= isl_map_intersect(dep_map
, after_write
);
650 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
651 dep_map
= isl_map_intersect(dep_map
, before_read
);
652 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
653 result
= isl_map_reverse(result
);
658 /* Given a shared_level between two accesses, return 1 if the
659 * the first can precede the second at the requested target_level.
660 * If the target level is odd, i.e., refers to a statement level
661 * dimension, then first needs to precede second at the requested
662 * level, i.e., shared_level must be equal to target_level.
663 * If the target level is odd, then the two loops should share
664 * at least the requested number of outer loops.
666 static int can_precede_at_level(int shared_level
, int target_level
)
668 if (shared_level
< target_level
)
670 if ((target_level
% 2) && shared_level
> target_level
)
675 /* Given a possible flow dependence temp_rel[j] between source j and the sink
676 * at level sink_level, remove those elements for which
677 * there is an iteration of another source k < j that is closer to the sink.
678 * The flow dependences temp_rel[k] are updated with the improved sources.
679 * Any improved source needs to precede the sink at the same level
680 * and needs to follow source j at the same or a deeper level.
681 * The lower this level, the later the execution date of source k.
682 * We therefore consider lower levels first.
684 * If temp_rel[j] is empty, then there can be no improvement and
685 * we return immediately.
687 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
688 struct isl_map
**temp_rel
, int j
, int sink_level
)
691 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
693 if (isl_map_plain_is_empty(temp_rel
[j
]))
696 for (k
= j
- 1; k
>= 0; --k
) {
698 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
699 if (!can_precede_at_level(plevel
, sink_level
))
702 plevel2
= acc
->level_before(acc
->source
[j
].data
,
703 acc
->source
[k
].data
);
705 for (level
= sink_level
; level
<= depth
; ++level
) {
707 struct isl_set
*trest
;
708 struct isl_map
*copy
;
710 if (!can_precede_at_level(plevel2
, level
))
713 copy
= isl_map_copy(temp_rel
[j
]);
714 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
716 if (isl_map_plain_is_empty(T
)) {
721 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
722 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
729 /* Compute all iterations of may source j that precedes the sink at the given
730 * level for sink iterations in set_C.
732 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
733 __isl_take isl_set
*set_C
, int j
, int level
)
740 read_map
= isl_map_copy(acc
->sink
.map
);
741 read_map
= isl_map_intersect_domain(read_map
, set_C
);
742 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
743 write_map
= isl_map_reverse(write_map
);
744 dep_map
= isl_map_apply_range(read_map
, write_map
);
745 after
= after_at_level(isl_map_get_space(dep_map
), level
);
746 dep_map
= isl_map_intersect(dep_map
, after
);
748 return isl_map_reverse(dep_map
);
751 /* For a given mapping between iterations of must source k and iterations
752 * of the sink, compute the all iteration of may source j preceding
753 * the sink at level before_level for any of the sink iterations,
754 * but following the corresponding iteration of must source k at level
757 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
758 __isl_take isl_map
*old_map
,
759 int j
, int before_level
, int k
, int after_level
)
766 isl_map
*after_write
;
767 isl_map
*before_read
;
769 set_C
= isl_map_range(isl_map_copy(old_map
));
770 read_map
= isl_map_copy(acc
->sink
.map
);
771 read_map
= isl_map_intersect_domain(read_map
, set_C
);
772 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
774 write_map
= isl_map_reverse(write_map
);
775 dep_map
= isl_map_apply_range(read_map
, write_map
);
776 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
777 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
778 after_write
= after_at_level(dim
, after_level
);
779 after_write
= isl_map_apply_range(after_write
, old_map
);
780 after_write
= isl_map_reverse(after_write
);
781 dep_map
= isl_map_intersect(dep_map
, after_write
);
782 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
783 dep_map
= isl_map_intersect(dep_map
, before_read
);
784 return isl_map_reverse(dep_map
);
787 /* Given the must and may dependence relations for the must accesses
788 * for level sink_level, check if there are any accesses of may access j
789 * that occur in between and return their union.
790 * If some of these accesses are intermediate with respect to
791 * (previously thought to be) must dependences, then these
792 * must dependences are turned into may dependences.
794 static __isl_give isl_map
*all_intermediate_sources(
795 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
796 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
797 int j
, int sink_level
)
800 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
803 for (k
= 0; k
< acc
->n_must
; ++k
) {
806 if (isl_map_plain_is_empty(may_rel
[k
]) &&
807 isl_map_plain_is_empty(must_rel
[k
]))
810 plevel
= acc
->level_before(acc
->source
[k
].data
,
811 acc
->source
[acc
->n_must
+ j
].data
);
813 for (level
= sink_level
; level
<= depth
; ++level
) {
818 if (!can_precede_at_level(plevel
, level
))
821 copy
= isl_map_copy(may_rel
[k
]);
822 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
823 map
= isl_map_union(map
, T
);
825 copy
= isl_map_copy(must_rel
[k
]);
826 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
827 ran
= isl_map_range(isl_map_copy(T
));
828 map
= isl_map_union(map
, T
);
829 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
830 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
832 T
= isl_map_from_domain_and_range(
834 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
836 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
843 /* Compute dependences for the case where all accesses are "may"
844 * accesses, which boils down to computing memory based dependences.
845 * The generic algorithm would also work in this case, but it would
846 * be overkill to use it.
848 static __isl_give isl_flow
*compute_mem_based_dependences(
849 __isl_keep isl_access_info
*acc
)
856 res
= isl_flow_alloc(acc
);
860 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
861 maydo
= isl_set_copy(mustdo
);
863 for (i
= 0; i
< acc
->n_may
; ++i
) {
870 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
871 is_before
= plevel
& 1;
874 dim
= isl_map_get_space(res
->dep
[i
].map
);
876 before
= isl_map_lex_le_first(dim
, plevel
);
878 before
= isl_map_lex_lt_first(dim
, plevel
);
879 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
880 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
881 dep
= isl_map_intersect(dep
, before
);
882 mustdo
= isl_set_subtract(mustdo
,
883 isl_map_range(isl_map_copy(dep
)));
884 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
887 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
888 res
->must_no_source
= mustdo
;
893 /* Compute dependences for the case where there is at least one
896 * The core algorithm considers all levels in which a source may precede
897 * the sink, where a level may either be a statement level or a loop level.
898 * The outermost statement level is 1, the first loop level is 2, etc...
899 * The algorithm basically does the following:
900 * for all levels l of the read access from innermost to outermost
901 * for all sources w that may precede the sink access at that level
902 * compute the last iteration of the source that precedes the sink access
904 * add result to possible last accesses at level l of source w
905 * for all sources w2 that we haven't considered yet at this level that may
906 * also precede the sink access
907 * for all levels l2 of w from l to innermost
908 * for all possible last accesses dep of w at l
909 * compute last iteration of w2 between the source and sink
911 * add result to possible last accesses at level l of write w2
912 * and replace possible last accesses dep by the remainder
915 * The above algorithm is applied to the must access. During the course
916 * of the algorithm, we keep track of sink iterations that still
917 * need to be considered. These iterations are split into those that
918 * haven't been matched to any source access (mustdo) and those that have only
919 * been matched to may accesses (maydo).
920 * At the end of each level, we also consider the may accesses.
921 * In particular, we consider may accesses that precede the remaining
922 * sink iterations, moving elements from mustdo to maydo when appropriate,
923 * and may accesses that occur between a must source and a sink of any
924 * dependences found at the current level, turning must dependences into
925 * may dependences when appropriate.
928 static __isl_give isl_flow
*compute_val_based_dependences(
929 __isl_keep isl_access_info
*acc
)
933 isl_set
*mustdo
= NULL
;
934 isl_set
*maydo
= NULL
;
937 isl_map
**must_rel
= NULL
;
938 isl_map
**may_rel
= NULL
;
943 res
= isl_flow_alloc(acc
);
946 ctx
= isl_map_get_ctx(acc
->sink
.map
);
948 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
949 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
950 maydo
= isl_set_empty(isl_set_get_space(mustdo
));
951 if (!mustdo
|| !maydo
)
953 if (isl_set_plain_is_empty(mustdo
))
956 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
957 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
958 if (!must_rel
|| !may_rel
)
961 for (level
= depth
; level
>= 1; --level
) {
962 for (j
= acc
->n_must
-1; j
>=0; --j
) {
964 space
= isl_map_get_space(res
->dep
[2 * j
].map
);
965 must_rel
[j
] = isl_map_empty(space
);
966 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
969 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
971 struct isl_set
*rest
;
974 plevel
= acc
->level_before(acc
->source
[j
].data
,
976 if (!can_precede_at_level(plevel
, level
))
979 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
980 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
983 intermediate_sources(acc
, must_rel
, j
, level
);
985 T
= last_source(acc
, maydo
, j
, level
, &rest
);
986 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
989 intermediate_sources(acc
, may_rel
, j
, level
);
991 if (isl_set_plain_is_empty(mustdo
) &&
992 isl_set_plain_is_empty(maydo
))
995 for (j
= j
- 1; j
>= 0; --j
) {
998 plevel
= acc
->level_before(acc
->source
[j
].data
,
1000 if (!can_precede_at_level(plevel
, level
))
1003 intermediate_sources(acc
, must_rel
, j
, level
);
1004 intermediate_sources(acc
, may_rel
, j
, level
);
1007 for (j
= 0; j
< acc
->n_may
; ++j
) {
1012 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1014 if (!can_precede_at_level(plevel
, level
))
1017 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1018 res
->dep
[2 * acc
->n_must
+ j
].map
=
1019 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1020 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1021 ran
= isl_map_range(isl_map_copy(T
));
1022 res
->dep
[2 * acc
->n_must
+ j
].map
=
1023 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1024 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1025 maydo
= isl_set_union_disjoint(maydo
, ran
);
1027 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1028 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1030 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1033 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1034 res
->dep
[2 * j
].map
=
1035 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1037 res
->dep
[2 * j
+ 1].map
=
1038 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1042 if (isl_set_plain_is_empty(mustdo
) &&
1043 isl_set_plain_is_empty(maydo
))
1050 res
->must_no_source
= mustdo
;
1051 res
->may_no_source
= maydo
;
1055 isl_set_free(mustdo
);
1056 isl_set_free(maydo
);
1062 /* Given a "sink" access, a list of n "source" accesses,
1063 * compute for each iteration of the sink access
1064 * and for each element accessed by that iteration,
1065 * the source access in the list that last accessed the
1066 * element accessed by the sink access before this sink access.
1067 * Each access is given as a map from the loop iterators
1068 * to the array indices.
1069 * The result is a list of n relations between source and sink
1070 * iterations and a subset of the domain of the sink access,
1071 * corresponding to those iterations that access an element
1072 * not previously accessed.
1074 * To deal with multi-valued sink access relations, the sink iteration
1075 * domain is first extended with dimensions that correspond to the data
1076 * space. However, these extra dimensions are not projected out again.
1077 * It is up to the caller to decide whether these dimensions should be kept.
1079 static __isl_give isl_flow
*access_info_compute_flow_core(
1080 __isl_take isl_access_info
*acc
)
1082 struct isl_flow
*res
= NULL
;
1087 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1091 if (acc
->n_must
== 0)
1092 res
= compute_mem_based_dependences(acc
);
1094 acc
= isl_access_info_sort_sources(acc
);
1095 res
= compute_val_based_dependences(acc
);
1097 acc
= isl_access_info_free(acc
);
1100 if (!res
->must_no_source
|| !res
->may_no_source
)
1104 isl_access_info_free(acc
);
1109 /* Given a "sink" access, a list of n "source" accesses,
1110 * compute for each iteration of the sink access
1111 * and for each element accessed by that iteration,
1112 * the source access in the list that last accessed the
1113 * element accessed by the sink access before this sink access.
1114 * Each access is given as a map from the loop iterators
1115 * to the array indices.
1116 * The result is a list of n relations between source and sink
1117 * iterations and a subset of the domain of the sink access,
1118 * corresponding to those iterations that access an element
1119 * not previously accessed.
1121 * To deal with multi-valued sink access relations,
1122 * access_info_compute_flow_core extends the sink iteration domain
1123 * with dimensions that correspond to the data space. These extra dimensions
1124 * are projected out from the result of access_info_compute_flow_core.
1126 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1129 struct isl_flow
*res
;
1134 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1135 res
= access_info_compute_flow_core(acc
);
1139 for (j
= 0; j
< res
->n_source
; ++j
) {
1140 res
->dep
[j
].map
= isl_map_range_factor_domain(res
->dep
[j
].map
);
1141 if (!res
->dep
[j
].map
)
1152 /* Keep track of some information about a schedule for a given
1153 * access. In particular, keep track of which dimensions
1154 * have a constant value and of the actual constant values.
1156 struct isl_sched_info
{
1161 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1165 isl_vec_free(info
->cst
);
1170 /* Extract information on the constant dimensions of the schedule
1171 * for a given access. The "map" is of the form
1175 * with S the schedule domain, D the iteration domain and A the data domain.
1177 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1178 __isl_keep isl_map
*map
)
1182 struct isl_sched_info
*info
;
1188 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1191 n
= isl_space_dim(dim
, isl_dim_in
);
1192 isl_space_free(dim
);
1194 ctx
= isl_map_get_ctx(map
);
1195 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1198 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1199 info
->cst
= isl_vec_alloc(ctx
, n
);
1200 if (n
&& (!info
->is_cst
|| !info
->cst
))
1203 for (i
= 0; i
< n
; ++i
) {
1206 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1209 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1210 if (info
->is_cst
[i
])
1211 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1218 sched_info_free(info
);
1222 /* This structure represents the input for a dependence analysis computation.
1224 * "sink" represents the sink accesses.
1225 * "must_source" represents the definite source accesses.
1226 * "may_source" represents the possible source accesses.
1228 * "schedule" or "schedule_map" represents the execution order.
1229 * Exactly one of these fields should be NULL. The other field
1230 * determines the execution order.
1232 * The domains of these four maps refer to the same iteration spaces(s).
1233 * The ranges of the first three maps also refer to the same data space(s).
1235 * After a call to isl_union_access_info_introduce_schedule,
1236 * the "schedule_map" field no longer contains useful information.
1238 struct isl_union_access_info
{
1239 isl_union_map
*sink
;
1240 isl_union_map
*must_source
;
1241 isl_union_map
*may_source
;
1243 isl_schedule
*schedule
;
1244 isl_union_map
*schedule_map
;
1247 /* Free "access" and return NULL.
1249 __isl_null isl_union_access_info
*isl_union_access_info_free(
1250 __isl_take isl_union_access_info
*access
)
1255 isl_union_map_free(access
->sink
);
1256 isl_union_map_free(access
->must_source
);
1257 isl_union_map_free(access
->may_source
);
1258 isl_schedule_free(access
->schedule
);
1259 isl_union_map_free(access
->schedule_map
);
1265 /* Return the isl_ctx to which "access" belongs.
1267 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1269 return access
? isl_union_map_get_ctx(access
->sink
) : NULL
;
1272 /* Create a new isl_union_access_info with the given sink accesses and
1273 * and no source accesses or schedule information.
1275 * By default, we use the schedule field of the isl_union_access_info,
1276 * but this may be overridden by a call
1277 * to isl_union_access_info_set_schedule_map.
1279 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1280 __isl_take isl_union_map
*sink
)
1284 isl_union_map
*empty
;
1285 isl_union_access_info
*access
;
1289 ctx
= isl_union_map_get_ctx(sink
);
1290 access
= isl_alloc_type(ctx
, isl_union_access_info
);
1294 space
= isl_union_map_get_space(sink
);
1295 empty
= isl_union_map_empty(isl_space_copy(space
));
1296 access
->sink
= sink
;
1297 access
->must_source
= isl_union_map_copy(empty
);
1298 access
->may_source
= empty
;
1299 access
->schedule
= isl_schedule_empty(space
);
1300 access
->schedule_map
= NULL
;
1302 if (!access
->sink
|| !access
->must_source
||
1303 !access
->may_source
|| !access
->schedule
)
1304 return isl_union_access_info_free(access
);
1308 isl_union_map_free(sink
);
1312 /* Replace the definite source accesses of "access" by "must_source".
1314 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1315 __isl_take isl_union_access_info
*access
,
1316 __isl_take isl_union_map
*must_source
)
1318 if (!access
|| !must_source
)
1321 isl_union_map_free(access
->must_source
);
1322 access
->must_source
= must_source
;
1326 isl_union_access_info_free(access
);
1327 isl_union_map_free(must_source
);
1331 /* Replace the possible source accesses of "access" by "may_source".
1333 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1334 __isl_take isl_union_access_info
*access
,
1335 __isl_take isl_union_map
*may_source
)
1337 if (!access
|| !may_source
)
1340 isl_union_map_free(access
->may_source
);
1341 access
->may_source
= may_source
;
1345 isl_union_access_info_free(access
);
1346 isl_union_map_free(may_source
);
1350 /* Replace the schedule of "access" by "schedule".
1351 * Also free the schedule_map in case it was set last.
1353 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1354 __isl_take isl_union_access_info
*access
,
1355 __isl_take isl_schedule
*schedule
)
1357 if (!access
|| !schedule
)
1360 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1361 isl_schedule_free(access
->schedule
);
1362 access
->schedule
= schedule
;
1366 isl_union_access_info_free(access
);
1367 isl_schedule_free(schedule
);
1371 /* Replace the schedule map of "access" by "schedule_map".
1372 * Also free the schedule in case it was set last.
1374 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1375 __isl_take isl_union_access_info
*access
,
1376 __isl_take isl_union_map
*schedule_map
)
1378 if (!access
|| !schedule_map
)
1381 isl_union_map_free(access
->schedule_map
);
1382 access
->schedule
= isl_schedule_free(access
->schedule
);
1383 access
->schedule_map
= schedule_map
;
1387 isl_union_access_info_free(access
);
1388 isl_union_map_free(schedule_map
);
1392 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1393 __isl_keep isl_union_access_info
*access
)
1395 isl_union_access_info
*copy
;
1399 copy
= isl_union_access_info_from_sink(
1400 isl_union_map_copy(access
->sink
));
1401 copy
= isl_union_access_info_set_must_source(copy
,
1402 isl_union_map_copy(access
->must_source
));
1403 copy
= isl_union_access_info_set_may_source(copy
,
1404 isl_union_map_copy(access
->may_source
));
1405 if (access
->schedule
)
1406 copy
= isl_union_access_info_set_schedule(copy
,
1407 isl_schedule_copy(access
->schedule
));
1409 copy
= isl_union_access_info_set_schedule_map(copy
,
1410 isl_union_map_copy(access
->schedule_map
));
1415 /* Print a key-value pair of a YAML mapping to "p",
1416 * with key "name" and value "umap".
1418 static __isl_give isl_printer
*print_union_map_field(__isl_take isl_printer
*p
,
1419 const char *name
, __isl_keep isl_union_map
*umap
)
1421 p
= isl_printer_print_str(p
, name
);
1422 p
= isl_printer_yaml_next(p
);
1423 p
= isl_printer_print_str(p
, "\"");
1424 p
= isl_printer_print_union_map(p
, umap
);
1425 p
= isl_printer_print_str(p
, "\"");
1426 p
= isl_printer_yaml_next(p
);
1431 /* Print the information contained in "access" to "p".
1432 * The information is printed as a YAML document.
1434 __isl_give isl_printer
*isl_printer_print_union_access_info(
1435 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1438 return isl_printer_free(p
);
1440 p
= isl_printer_yaml_start_mapping(p
);
1441 p
= print_union_map_field(p
, "sink", access
->sink
);
1442 p
= print_union_map_field(p
, "must_source", access
->must_source
);
1443 p
= print_union_map_field(p
, "may_source", access
->may_source
);
1444 if (access
->schedule
) {
1445 p
= isl_printer_print_str(p
, "schedule");
1446 p
= isl_printer_yaml_next(p
);
1447 p
= isl_printer_print_schedule(p
, access
->schedule
);
1448 p
= isl_printer_yaml_next(p
);
1450 p
= print_union_map_field(p
, "schedule_map",
1451 access
->schedule_map
);
1453 p
= isl_printer_yaml_end_mapping(p
);
1458 /* Return a string representation of the information in "access".
1459 * The information is printed in flow format.
1461 __isl_give
char *isl_union_access_info_to_str(
1462 __isl_keep isl_union_access_info
*access
)
1470 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1471 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1472 p
= isl_printer_print_union_access_info(p
, access
);
1473 s
= isl_printer_get_str(p
);
1474 isl_printer_free(p
);
1479 /* Update the fields of "access" such that they all have the same parameters,
1480 * keeping in mind that the schedule_map field may be NULL and ignoring
1481 * the schedule field.
1483 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1484 __isl_take isl_union_access_info
*access
)
1491 space
= isl_union_map_get_space(access
->sink
);
1492 space
= isl_space_align_params(space
,
1493 isl_union_map_get_space(access
->must_source
));
1494 space
= isl_space_align_params(space
,
1495 isl_union_map_get_space(access
->may_source
));
1496 if (access
->schedule_map
)
1497 space
= isl_space_align_params(space
,
1498 isl_union_map_get_space(access
->schedule_map
));
1499 access
->sink
= isl_union_map_align_params(access
->sink
,
1500 isl_space_copy(space
));
1501 access
->must_source
= isl_union_map_align_params(access
->must_source
,
1502 isl_space_copy(space
));
1503 access
->may_source
= isl_union_map_align_params(access
->may_source
,
1504 isl_space_copy(space
));
1505 if (!access
->schedule_map
) {
1506 isl_space_free(space
);
1508 access
->schedule_map
=
1509 isl_union_map_align_params(access
->schedule_map
, space
);
1510 if (!access
->schedule_map
)
1511 return isl_union_access_info_free(access
);
1514 if (!access
->sink
|| !access
->must_source
|| !access
->may_source
)
1515 return isl_union_access_info_free(access
);
1520 /* Prepend the schedule dimensions to the iteration domains.
1522 * That is, if the schedule is of the form
1526 * while the access relations are of the form
1530 * then the updated access relations are of the form
1534 * The schedule map is also replaced by the map
1538 * that is used during the internal computation.
1539 * Neither the original schedule map nor this updated schedule map
1540 * are used after the call to this function.
1542 static __isl_give isl_union_access_info
*
1543 isl_union_access_info_introduce_schedule(
1544 __isl_take isl_union_access_info
*access
)
1551 sm
= isl_union_map_reverse(access
->schedule_map
);
1552 sm
= isl_union_map_range_map(sm
);
1553 access
->sink
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1555 access
->may_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1556 access
->may_source
);
1557 access
->must_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1558 access
->must_source
);
1559 access
->schedule_map
= sm
;
1561 if (!access
->sink
|| !access
->must_source
||
1562 !access
->may_source
|| !access
->schedule_map
)
1563 return isl_union_access_info_free(access
);
1568 /* This structure represents the result of a dependence analysis computation.
1570 * "must_dep" represents the full definite dependences
1571 * "may_dep" represents the full non-definite dependences.
1572 * Both are of the form
1574 * [Source] -> [[Sink -> Data]]
1576 * (after the schedule dimensions have been projected out).
1577 * "must_no_source" represents the subset of the sink accesses for which
1578 * definitely no source was found.
1579 * "may_no_source" represents the subset of the sink accesses for which
1580 * possibly, but not definitely, no source was found.
1582 struct isl_union_flow
{
1583 isl_union_map
*must_dep
;
1584 isl_union_map
*may_dep
;
1585 isl_union_map
*must_no_source
;
1586 isl_union_map
*may_no_source
;
1589 /* Return the isl_ctx to which "flow" belongs.
1591 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
1593 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
1596 /* Free "flow" and return NULL.
1598 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
1602 isl_union_map_free(flow
->must_dep
);
1603 isl_union_map_free(flow
->may_dep
);
1604 isl_union_map_free(flow
->must_no_source
);
1605 isl_union_map_free(flow
->may_no_source
);
1610 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
1615 fprintf(stderr
, "must dependences: ");
1616 isl_union_map_dump(flow
->must_dep
);
1617 fprintf(stderr
, "may dependences: ");
1618 isl_union_map_dump(flow
->may_dep
);
1619 fprintf(stderr
, "must no source: ");
1620 isl_union_map_dump(flow
->must_no_source
);
1621 fprintf(stderr
, "may no source: ");
1622 isl_union_map_dump(flow
->may_no_source
);
1625 /* Return the full definite dependences in "flow", with accessed elements.
1627 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
1628 __isl_keep isl_union_flow
*flow
)
1632 return isl_union_map_copy(flow
->must_dep
);
1635 /* Return the full possible dependences in "flow", including the definite
1636 * dependences, with accessed elements.
1638 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
1639 __isl_keep isl_union_flow
*flow
)
1643 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1644 isl_union_map_copy(flow
->may_dep
));
1647 /* Return the definite dependences in "flow", without the accessed elements.
1649 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
1650 __isl_keep isl_union_flow
*flow
)
1656 dep
= isl_union_map_copy(flow
->must_dep
);
1657 return isl_union_map_range_factor_domain(dep
);
1660 /* Return the possible dependences in "flow", including the definite
1661 * dependences, without the accessed elements.
1663 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
1664 __isl_keep isl_union_flow
*flow
)
1670 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1671 isl_union_map_copy(flow
->may_dep
));
1672 return isl_union_map_range_factor_domain(dep
);
1675 /* Return the non-definite dependences in "flow".
1677 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
1678 __isl_keep isl_union_flow
*flow
)
1682 return isl_union_map_copy(flow
->may_dep
);
1685 /* Return the subset of the sink accesses for which definitely
1686 * no source was found.
1688 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
1689 __isl_keep isl_union_flow
*flow
)
1693 return isl_union_map_copy(flow
->must_no_source
);
1696 /* Return the subset of the sink accesses for which possibly
1697 * no source was found, including those for which definitely
1698 * no source was found.
1700 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
1701 __isl_keep isl_union_flow
*flow
)
1705 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
1706 isl_union_map_copy(flow
->may_no_source
));
1709 /* Return the subset of the sink accesses for which possibly, but not
1710 * definitely, no source was found.
1712 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
1713 __isl_keep isl_union_flow
*flow
)
1717 return isl_union_map_copy(flow
->may_no_source
);
1720 /* Create a new isl_union_flow object, initialized with empty
1721 * dependence relations and sink subsets.
1723 static __isl_give isl_union_flow
*isl_union_flow_alloc(
1724 __isl_take isl_space
*space
)
1727 isl_union_map
*empty
;
1728 isl_union_flow
*flow
;
1732 ctx
= isl_space_get_ctx(space
);
1733 flow
= isl_alloc_type(ctx
, isl_union_flow
);
1737 empty
= isl_union_map_empty(space
);
1738 flow
->must_dep
= isl_union_map_copy(empty
);
1739 flow
->may_dep
= isl_union_map_copy(empty
);
1740 flow
->must_no_source
= isl_union_map_copy(empty
);
1741 flow
->may_no_source
= empty
;
1743 if (!flow
->must_dep
|| !flow
->may_dep
||
1744 !flow
->must_no_source
|| !flow
->may_no_source
)
1745 return isl_union_flow_free(flow
);
1749 isl_space_free(space
);
1753 /* Copy this isl_union_flow object.
1755 __isl_give isl_union_flow
*isl_union_flow_copy(__isl_keep isl_union_flow
*flow
)
1757 isl_union_flow
*copy
;
1762 copy
= isl_union_flow_alloc(isl_union_map_get_space(flow
->must_dep
));
1767 copy
->must_dep
= isl_union_map_union(copy
->must_dep
,
1768 isl_union_map_copy(flow
->must_dep
));
1769 copy
->may_dep
= isl_union_map_union(copy
->may_dep
,
1770 isl_union_map_copy(flow
->may_dep
));
1771 copy
->must_no_source
= isl_union_map_union(copy
->must_no_source
,
1772 isl_union_map_copy(flow
->must_no_source
));
1773 copy
->may_no_source
= isl_union_map_union(copy
->may_no_source
,
1774 isl_union_map_copy(flow
->may_no_source
));
1776 if (!copy
->must_dep
|| !copy
->may_dep
||
1777 !copy
->must_no_source
|| !copy
->may_no_source
)
1778 return isl_union_flow_free(copy
);
1783 /* Drop the schedule dimensions from the iteration domains in "flow".
1784 * In particular, the schedule dimensions have been prepended
1785 * to the iteration domains prior to the dependence analysis by
1786 * replacing the iteration domain D, by the wrapped map [S -> D].
1787 * Replace these wrapped maps by the original D.
1789 * In particular, the dependences computed by access_info_compute_flow_core
1792 * [S -> D] -> [[S' -> D'] -> A]
1794 * The schedule dimensions are projected out by first currying the range,
1797 * [S -> D] -> [S' -> [D' -> A]]
1799 * and then computing the factor range
1803 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
1804 __isl_take isl_union_flow
*flow
)
1809 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
1810 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
1811 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
1812 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
1813 flow
->must_no_source
=
1814 isl_union_map_domain_factor_range(flow
->must_no_source
);
1815 flow
->may_no_source
=
1816 isl_union_map_domain_factor_range(flow
->may_no_source
);
1818 if (!flow
->must_dep
|| !flow
->may_dep
||
1819 !flow
->must_no_source
|| !flow
->may_no_source
)
1820 return isl_union_flow_free(flow
);
1825 struct isl_compute_flow_data
{
1826 isl_union_map
*must_source
;
1827 isl_union_map
*may_source
;
1828 isl_union_flow
*flow
;
1833 struct isl_sched_info
*sink_info
;
1834 struct isl_sched_info
**source_info
;
1835 isl_access_info
*accesses
;
1838 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
1842 struct isl_compute_flow_data
*data
;
1844 data
= (struct isl_compute_flow_data
*)user
;
1846 dim
= isl_space_range(isl_map_get_space(map
));
1848 eq
= isl_space_is_equal(dim
, data
->dim
);
1850 isl_space_free(dim
);
1854 return isl_stat_error
;
1861 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
1865 struct isl_sched_info
*info
;
1866 struct isl_compute_flow_data
*data
;
1868 data
= (struct isl_compute_flow_data
*)user
;
1870 dim
= isl_space_range(isl_map_get_space(map
));
1872 eq
= isl_space_is_equal(dim
, data
->dim
);
1874 isl_space_free(dim
);
1883 info
= sched_info_alloc(map
);
1884 data
->source_info
[data
->count
] = info
;
1886 data
->accesses
= isl_access_info_add_source(data
->accesses
,
1887 map
, data
->must
, info
);
1894 return isl_stat_error
;
1897 /* Determine the shared nesting level and the "textual order" of
1898 * the given accesses.
1900 * We first determine the minimal schedule dimension for both accesses.
1902 * If among those dimensions, we can find one where both have a fixed
1903 * value and if moreover those values are different, then the previous
1904 * dimension is the last shared nesting level and the textual order
1905 * is determined based on the order of the fixed values.
1906 * If no such fixed values can be found, then we set the shared
1907 * nesting level to the minimal schedule dimension, with no textual ordering.
1909 static int before(void *first
, void *second
)
1911 struct isl_sched_info
*info1
= first
;
1912 struct isl_sched_info
*info2
= second
;
1916 n1
= isl_vec_size(info1
->cst
);
1917 n2
= isl_vec_size(info2
->cst
);
1922 for (i
= 0; i
< n1
; ++i
) {
1926 if (!info1
->is_cst
[i
])
1928 if (!info2
->is_cst
[i
])
1930 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
1934 r
= 2 * i
+ (cmp
< 0);
1942 /* Given a sink access, look for all the source accesses that access
1943 * the same array and perform dataflow analysis on them using
1944 * isl_access_info_compute_flow_core.
1946 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
1950 struct isl_compute_flow_data
*data
;
1954 data
= (struct isl_compute_flow_data
*)user
;
1957 ctx
= isl_map_get_ctx(map
);
1959 data
->accesses
= NULL
;
1960 data
->sink_info
= NULL
;
1961 data
->source_info
= NULL
;
1963 data
->dim
= isl_space_range(isl_map_get_space(map
));
1965 if (isl_union_map_foreach_map(data
->must_source
,
1966 &count_matching_array
, data
) < 0)
1968 if (isl_union_map_foreach_map(data
->may_source
,
1969 &count_matching_array
, data
) < 0)
1972 data
->sink_info
= sched_info_alloc(map
);
1973 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
1976 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
1977 data
->sink_info
, &before
, data
->count
);
1978 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
1983 if (isl_union_map_foreach_map(data
->must_source
,
1984 &collect_matching_array
, data
) < 0)
1987 if (isl_union_map_foreach_map(data
->may_source
,
1988 &collect_matching_array
, data
) < 0)
1991 flow
= access_info_compute_flow_core(data
->accesses
);
1992 data
->accesses
= NULL
;
1997 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
1998 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
1999 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
2000 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
2002 for (i
= 0; i
< flow
->n_source
; ++i
) {
2004 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
2005 if (flow
->dep
[i
].must
)
2006 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
2008 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
2011 isl_flow_free(flow
);
2013 sched_info_free(data
->sink_info
);
2014 if (data
->source_info
) {
2015 for (i
= 0; i
< data
->count
; ++i
)
2016 sched_info_free(data
->source_info
[i
]);
2017 free(data
->source_info
);
2019 isl_space_free(data
->dim
);
2024 isl_access_info_free(data
->accesses
);
2025 sched_info_free(data
->sink_info
);
2026 if (data
->source_info
) {
2027 for (i
= 0; i
< data
->count
; ++i
)
2028 sched_info_free(data
->source_info
[i
]);
2029 free(data
->source_info
);
2031 isl_space_free(data
->dim
);
2034 return isl_stat_error
;
2037 /* Remove the must accesses from the may accesses.
2039 * A must access always trumps a may access, so there is no need
2040 * for a must access to also be considered as a may access. Doing so
2041 * would only cost extra computations only to find out that
2042 * the duplicated may access does not make any difference.
2044 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2045 __isl_take isl_union_access_info
*access
)
2049 access
->may_source
= isl_union_map_subtract(access
->may_source
,
2050 isl_union_map_copy(access
->must_source
));
2051 if (!access
->may_source
)
2052 return isl_union_access_info_free(access
);
2057 /* Given a description of the "sink" accesses, the "source" accesses and
2058 * a schedule, compute for each instance of a sink access
2059 * and for each element accessed by that instance,
2060 * the possible or definite source accesses that last accessed the
2061 * element accessed by the sink access before this sink access
2062 * in the sense that there is no intermediate definite source access.
2064 * The must_no_source and may_no_source elements of the result
2065 * are subsets of access->sink. The elements must_dep and may_dep
2066 * map domain elements of access->{may,must)_source to
2067 * domain elements of access->sink.
2069 * This function is used when only the schedule map representation
2072 * We first prepend the schedule dimensions to the domain
2073 * of the accesses so that we can easily compare their relative order.
2074 * Then we consider each sink access individually in compute_flow.
2076 static __isl_give isl_union_flow
*compute_flow_union_map(
2077 __isl_take isl_union_access_info
*access
)
2079 struct isl_compute_flow_data data
;
2081 access
= isl_union_access_info_align_params(access
);
2082 access
= isl_union_access_info_introduce_schedule(access
);
2086 data
.must_source
= access
->must_source
;
2087 data
.may_source
= access
->may_source
;
2089 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
2091 if (isl_union_map_foreach_map(access
->sink
, &compute_flow
, &data
) < 0)
2094 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2096 isl_union_access_info_free(access
);
2099 isl_union_access_info_free(access
);
2100 isl_union_flow_free(data
.flow
);
2104 /* A schedule access relation.
2106 * The access relation "access" is of the form [S -> D] -> A,
2107 * where S corresponds to the prefix schedule at "node".
2108 * "must" is only relevant for source accesses and indicates
2109 * whether the access is a must source or a may source.
2111 struct isl_scheduled_access
{
2114 isl_schedule_node
*node
;
2117 /* Data structure for keeping track of individual scheduled sink and source
2118 * accesses when computing dependence analysis based on a schedule tree.
2120 * "n_sink" is the number of used entries in "sink"
2121 * "n_source" is the number of used entries in "source"
2123 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2124 * to keep track of the current node and
2125 * of what extract_sink_source needs to do.
2127 struct isl_compute_flow_schedule_data
{
2128 isl_union_access_info
*access
;
2133 struct isl_scheduled_access
*sink
;
2134 struct isl_scheduled_access
*source
;
2138 isl_schedule_node
*node
;
2141 /* Align the parameters of all sinks with all sources.
2143 * If there are no sinks or no sources, then no alignment is needed.
2145 static void isl_compute_flow_schedule_data_align_params(
2146 struct isl_compute_flow_schedule_data
*data
)
2151 if (data
->n_sink
== 0 || data
->n_source
== 0)
2154 space
= isl_map_get_space(data
->sink
[0].access
);
2156 for (i
= 1; i
< data
->n_sink
; ++i
)
2157 space
= isl_space_align_params(space
,
2158 isl_map_get_space(data
->sink
[i
].access
));
2159 for (i
= 0; i
< data
->n_source
; ++i
)
2160 space
= isl_space_align_params(space
,
2161 isl_map_get_space(data
->source
[i
].access
));
2163 for (i
= 0; i
< data
->n_sink
; ++i
)
2164 data
->sink
[i
].access
=
2165 isl_map_align_params(data
->sink
[i
].access
,
2166 isl_space_copy(space
));
2167 for (i
= 0; i
< data
->n_source
; ++i
)
2168 data
->source
[i
].access
=
2169 isl_map_align_params(data
->source
[i
].access
,
2170 isl_space_copy(space
));
2172 isl_space_free(space
);
2175 /* Free all the memory referenced from "data".
2176 * Do not free "data" itself as it may be allocated on the stack.
2178 static void isl_compute_flow_schedule_data_clear(
2179 struct isl_compute_flow_schedule_data
*data
)
2186 for (i
= 0; i
< data
->n_sink
; ++i
) {
2187 isl_map_free(data
->sink
[i
].access
);
2188 isl_schedule_node_free(data
->sink
[i
].node
);
2191 for (i
= 0; i
< data
->n_source
; ++i
) {
2192 isl_map_free(data
->source
[i
].access
);
2193 isl_schedule_node_free(data
->source
[i
].node
);
2199 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2200 * (an upper bound on) the number of sinks and sources.
2202 * Sinks and sources are only extracted at leaves of the tree,
2203 * so we skip the node if it is not a leaf.
2204 * Otherwise we increment data->n_sink and data->n_source with
2205 * the number of spaces in the sink and source access domains
2206 * that reach this node.
2208 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2211 struct isl_compute_flow_schedule_data
*data
= user
;
2212 isl_union_set
*domain
;
2213 isl_union_map
*umap
;
2214 isl_bool r
= isl_bool_false
;
2216 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2217 return isl_bool_true
;
2219 domain
= isl_schedule_node_get_universe_domain(node
);
2221 umap
= isl_union_map_copy(data
->access
->sink
);
2222 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2223 data
->n_sink
+= isl_union_map_n_map(umap
);
2224 isl_union_map_free(umap
);
2228 umap
= isl_union_map_copy(data
->access
->must_source
);
2229 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2230 data
->n_source
+= isl_union_map_n_map(umap
);
2231 isl_union_map_free(umap
);
2235 umap
= isl_union_map_copy(data
->access
->may_source
);
2236 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2237 data
->n_source
+= isl_union_map_n_map(umap
);
2238 isl_union_map_free(umap
);
2242 isl_union_set_free(domain
);
2247 /* Add a single scheduled sink or source (depending on data->set_sink)
2248 * with scheduled access relation "map", must property data->must and
2249 * schedule node data->node to the list of sinks or sources.
2251 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2253 struct isl_compute_flow_schedule_data
*data
= user
;
2254 struct isl_scheduled_access
*access
;
2257 access
= data
->sink
+ data
->n_sink
++;
2259 access
= data
->source
+ data
->n_source
++;
2261 access
->access
= map
;
2262 access
->must
= data
->must
;
2263 access
->node
= isl_schedule_node_copy(data
->node
);
2268 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2269 * individual scheduled source and sink accesses (taking into account
2270 * the domain of the schedule).
2272 * We only collect accesses at the leaves of the schedule tree.
2273 * We prepend the schedule dimensions at the leaf to the iteration
2274 * domains of the source and sink accesses and then extract
2275 * the individual accesses (per space).
2277 * In particular, if the prefix schedule at the node is of the form
2281 * while the access relations are of the form
2285 * then the updated access relations are of the form
2289 * Note that S consists of a single space such that introducing S
2290 * in the access relations does not increase the number of spaces.
2292 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2295 struct isl_compute_flow_schedule_data
*data
= user
;
2296 isl_union_map
*prefix
;
2297 isl_union_map
*umap
;
2298 isl_bool r
= isl_bool_false
;
2300 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2301 return isl_bool_true
;
2305 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2306 prefix
= isl_union_map_reverse(prefix
);
2307 prefix
= isl_union_map_range_map(prefix
);
2310 umap
= isl_union_map_copy(data
->access
->sink
);
2311 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2312 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2314 isl_union_map_free(umap
);
2318 umap
= isl_union_map_copy(data
->access
->must_source
);
2319 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2320 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2322 isl_union_map_free(umap
);
2326 umap
= isl_union_map_copy(data
->access
->may_source
);
2327 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2328 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2330 isl_union_map_free(umap
);
2332 isl_union_map_free(prefix
);
2337 /* isl_access_info_compute_flow callback for determining whether
2338 * the shared nesting level and the ordering within that level
2339 * for two scheduled accesses for use in compute_single_flow.
2341 * The tokens passed to this function refer to the leaves
2342 * in the schedule tree where the accesses take place.
2344 * If n is the shared number of loops, then we need to return
2345 * "2 * n + 1" if "first" precedes "second" inside the innermost
2346 * shared loop and "2 * n" otherwise.
2348 * The innermost shared ancestor may be the leaves themselves
2349 * if the accesses take place in the same leaf. Otherwise,
2350 * it is either a set node or a sequence node. Only in the case
2351 * of a sequence node do we consider one access to precede the other.
2353 static int before_node(void *first
, void *second
)
2355 isl_schedule_node
*node1
= first
;
2356 isl_schedule_node
*node2
= second
;
2357 isl_schedule_node
*shared
;
2361 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2365 depth
= isl_schedule_node_get_schedule_depth(shared
);
2366 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2369 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2371 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2373 before
= pos1
< pos2
;
2376 isl_schedule_node_free(shared
);
2378 return 2 * depth
+ before
;
2381 /* Add the scheduled sources from "data" that access
2382 * the same data space as "sink" to "access".
2384 static __isl_give isl_access_info
*add_matching_sources(
2385 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2386 struct isl_compute_flow_schedule_data
*data
)
2391 space
= isl_space_range(isl_map_get_space(sink
->access
));
2392 for (i
= 0; i
< data
->n_source
; ++i
) {
2393 struct isl_scheduled_access
*source
;
2394 isl_space
*source_space
;
2397 source
= &data
->source
[i
];
2398 source_space
= isl_map_get_space(source
->access
);
2399 source_space
= isl_space_range(source_space
);
2400 eq
= isl_space_is_equal(space
, source_space
);
2401 isl_space_free(source_space
);
2408 access
= isl_access_info_add_source(access
,
2409 isl_map_copy(source
->access
), source
->must
, source
->node
);
2412 isl_space_free(space
);
2415 isl_space_free(space
);
2416 isl_access_info_free(access
);
2420 /* Given a scheduled sink access relation "sink", compute the corresponding
2421 * dependences on the sources in "data" and add the computed dependences
2424 * The dependences computed by access_info_compute_flow_core are of the form
2426 * [S -> I] -> [[S' -> I'] -> A]
2428 * The schedule dimensions are projected out by first currying the range,
2431 * [S -> I] -> [S' -> [I' -> A]]
2433 * and then computing the factor range
2437 static __isl_give isl_union_flow
*compute_single_flow(
2438 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
2439 struct isl_compute_flow_schedule_data
*data
)
2442 isl_access_info
*access
;
2449 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
2450 &before_node
, data
->n_source
);
2451 access
= add_matching_sources(access
, sink
, data
);
2453 flow
= access_info_compute_flow_core(access
);
2455 return isl_union_flow_free(uf
);
2457 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
2458 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
2459 isl_union_map_from_map(map
));
2460 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
2461 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
2462 isl_union_map_from_map(map
));
2464 for (i
= 0; i
< flow
->n_source
; ++i
) {
2467 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
2468 map
= isl_map_factor_range(map
);
2469 dep
= isl_union_map_from_map(map
);
2470 if (flow
->dep
[i
].must
)
2471 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
2473 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
2476 isl_flow_free(flow
);
2481 /* Given a description of the "sink" accesses, the "source" accesses and
2482 * a schedule, compute for each instance of a sink access
2483 * and for each element accessed by that instance,
2484 * the possible or definite source accesses that last accessed the
2485 * element accessed by the sink access before this sink access
2486 * in the sense that there is no intermediate definite source access.
2487 * Only consider dependences between statement instances that belong
2488 * to the domain of the schedule.
2490 * The must_no_source and may_no_source elements of the result
2491 * are subsets of access->sink. The elements must_dep and may_dep
2492 * map domain elements of access->{may,must)_source to
2493 * domain elements of access->sink.
2495 * This function is used when a schedule tree representation
2498 * We extract the individual scheduled source and sink access relations
2499 * (taking into account the domain of the schedule) and
2500 * then compute dependences for each scheduled sink individually.
2502 static __isl_give isl_union_flow
*compute_flow_schedule(
2503 __isl_take isl_union_access_info
*access
)
2505 struct isl_compute_flow_schedule_data data
= { access
};
2508 isl_union_flow
*flow
;
2510 ctx
= isl_union_access_info_get_ctx(access
);
2514 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2515 &count_sink_source
, &data
) < 0)
2518 n
= data
.n_sink
+ data
.n_source
;
2519 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
2520 if (n
&& !data
.sink
)
2522 data
.source
= data
.sink
+ data
.n_sink
;
2526 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2527 &collect_sink_source
, &data
) < 0)
2530 flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
2532 isl_compute_flow_schedule_data_align_params(&data
);
2534 for (i
= 0; i
< data
.n_sink
; ++i
)
2535 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
2537 isl_compute_flow_schedule_data_clear(&data
);
2539 isl_union_access_info_free(access
);
2542 isl_union_access_info_free(access
);
2543 isl_compute_flow_schedule_data_clear(&data
);
2547 /* Given a description of the "sink" accesses, the "source" accesses and
2548 * a schedule, compute for each instance of a sink access
2549 * and for each element accessed by that instance,
2550 * the possible or definite source accesses that last accessed the
2551 * element accessed by the sink access before this sink access
2552 * in the sense that there is no intermediate definite source access.
2554 * The must_no_source and may_no_source elements of the result
2555 * are subsets of access->sink. The elements must_dep and may_dep
2556 * map domain elements of access->{may,must)_source to
2557 * domain elements of access->sink.
2559 * We check whether the schedule is available as a schedule tree
2560 * or a schedule map and call the correpsonding function to perform
2563 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
2564 __isl_take isl_union_access_info
*access
)
2566 access
= isl_union_access_info_normalize(access
);
2569 if (access
->schedule
)
2570 return compute_flow_schedule(access
);
2572 return compute_flow_union_map(access
);
2575 /* Print the information contained in "flow" to "p".
2576 * The information is printed as a YAML document.
2578 __isl_give isl_printer
*isl_printer_print_union_flow(
2579 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
2581 isl_union_map
*umap
;
2584 return isl_printer_free(p
);
2586 p
= isl_printer_yaml_start_mapping(p
);
2587 p
= print_union_map_field(p
, "must_dependence", flow
->must_dep
);
2588 umap
= isl_union_flow_get_may_dependence(flow
);
2589 p
= print_union_map_field(p
, "may_dependence", umap
);
2590 isl_union_map_free(umap
);
2591 p
= print_union_map_field(p
, "must_no_source", flow
->must_no_source
);
2592 umap
= isl_union_flow_get_may_no_source(flow
);
2593 p
= print_union_map_field(p
, "may_no_source", umap
);
2594 isl_union_map_free(umap
);
2595 p
= isl_printer_yaml_end_mapping(p
);
2600 /* Return a string representation of the information in "flow".
2601 * The information is printed in flow format.
2603 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
2611 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
2612 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2613 p
= isl_printer_print_union_flow(p
, flow
);
2614 s
= isl_printer_get_str(p
);
2615 isl_printer_free(p
);
2620 /* Given a collection of "sink" and "source" accesses,
2621 * compute for each iteration of a sink access
2622 * and for each element accessed by that iteration,
2623 * the source access in the list that last accessed the
2624 * element accessed by the sink access before this sink access.
2625 * Each access is given as a map from the loop iterators
2626 * to the array indices.
2627 * The result is a relations between source and sink
2628 * iterations and a subset of the domain of the sink accesses,
2629 * corresponding to those iterations that access an element
2630 * not previously accessed.
2632 * We collect the inputs in an isl_union_access_info object,
2633 * call isl_union_access_info_compute_flow and extract
2634 * the outputs from the result.
2636 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
2637 __isl_take isl_union_map
*must_source
,
2638 __isl_take isl_union_map
*may_source
,
2639 __isl_take isl_union_map
*schedule
,
2640 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
2641 __isl_give isl_union_map
**must_no_source
,
2642 __isl_give isl_union_map
**may_no_source
)
2644 isl_union_access_info
*access
;
2645 isl_union_flow
*flow
;
2647 access
= isl_union_access_info_from_sink(sink
);
2648 access
= isl_union_access_info_set_must_source(access
, must_source
);
2649 access
= isl_union_access_info_set_may_source(access
, may_source
);
2650 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
2651 flow
= isl_union_access_info_compute_flow(access
);
2654 *must_dep
= isl_union_flow_get_must_dependence(flow
);
2656 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
2658 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
2660 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
2662 isl_union_flow_free(flow
);
2664 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
2665 (must_no_source
&& !*must_no_source
) ||
2666 (may_no_source
&& !*may_no_source
))
2672 *must_dep
= isl_union_map_free(*must_dep
);
2674 *may_dep
= isl_union_map_free(*may_dep
);
2676 *must_no_source
= isl_union_map_free(*must_no_source
);
2678 *may_no_source
= isl_union_map_free(*may_no_source
);