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 all iterations 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 /* The different types of access relations that isl_union_access_info
1225 * "isl_access_sink" represents the sink accesses.
1226 * "isl_access_must_source" represents the definite source accesses.
1227 * "isl_access_may_source" represents the possible source accesses.
1229 * isl_access_sink is sometimes treated differently and
1230 * should therefore appear first.
1232 enum isl_access_type
{
1234 isl_access_must_source
,
1235 isl_access_may_source
,
1239 /* This structure represents the input for a dependence analysis computation.
1241 * "access" contains the access relations.
1243 * "schedule" or "schedule_map" represents the execution order.
1244 * Exactly one of these fields should be NULL. The other field
1245 * determines the execution order.
1247 * The domains of these four maps refer to the same iteration spaces(s).
1248 * The ranges of the first three maps also refer to the same data space(s).
1250 * After a call to isl_union_access_info_introduce_schedule,
1251 * the "schedule_map" field no longer contains useful information.
1253 struct isl_union_access_info
{
1254 isl_union_map
*access
[isl_access_end
];
1256 isl_schedule
*schedule
;
1257 isl_union_map
*schedule_map
;
1260 /* Free "access" and return NULL.
1262 __isl_null isl_union_access_info
*isl_union_access_info_free(
1263 __isl_take isl_union_access_info
*access
)
1265 enum isl_access_type i
;
1270 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1271 isl_union_map_free(access
->access
[i
]);
1272 isl_schedule_free(access
->schedule
);
1273 isl_union_map_free(access
->schedule_map
);
1279 /* Return the isl_ctx to which "access" belongs.
1281 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1285 return isl_union_map_get_ctx(access
->access
[isl_access_sink
]);
1288 /* Create a new isl_union_access_info with the given sink accesses and
1289 * and no other accesses or schedule information.
1291 * By default, we use the schedule field of the isl_union_access_info,
1292 * but this may be overridden by a call
1293 * to isl_union_access_info_set_schedule_map.
1295 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1296 __isl_take isl_union_map
*sink
)
1300 isl_union_map
*empty
;
1301 isl_union_access_info
*access
;
1302 enum isl_access_type i
;
1306 ctx
= isl_union_map_get_ctx(sink
);
1307 access
= isl_alloc_type(ctx
, isl_union_access_info
);
1311 space
= isl_union_map_get_space(sink
);
1312 empty
= isl_union_map_empty(isl_space_copy(space
));
1313 access
->access
[isl_access_sink
] = sink
;
1314 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1315 access
->access
[i
] = isl_union_map_copy(empty
);
1316 isl_union_map_free(empty
);
1317 access
->schedule
= isl_schedule_empty(space
);
1318 access
->schedule_map
= NULL
;
1320 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1321 if (!access
->access
[i
])
1322 return isl_union_access_info_free(access
);
1323 if (!access
->schedule
)
1324 return isl_union_access_info_free(access
);
1328 isl_union_map_free(sink
);
1332 /* Replace the access relation of type "type" of "info" by "access".
1334 static __isl_give isl_union_access_info
*isl_union_access_info_set(
1335 __isl_take isl_union_access_info
*info
,
1336 enum isl_access_type type
, __isl_take isl_union_map
*access
)
1338 if (!info
|| !access
)
1341 isl_union_map_free(info
->access
[type
]);
1342 info
->access
[type
] = access
;
1346 isl_union_access_info_free(info
);
1347 isl_union_map_free(access
);
1351 /* Replace the definite source accesses of "access" by "must_source".
1353 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1354 __isl_take isl_union_access_info
*access
,
1355 __isl_take isl_union_map
*must_source
)
1357 return isl_union_access_info_set(access
, isl_access_must_source
,
1361 /* Replace the possible source accesses of "access" by "may_source".
1363 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1364 __isl_take isl_union_access_info
*access
,
1365 __isl_take isl_union_map
*may_source
)
1367 return isl_union_access_info_set(access
, isl_access_may_source
,
1371 /* Replace the schedule of "access" by "schedule".
1372 * Also free the schedule_map in case it was set last.
1374 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1375 __isl_take isl_union_access_info
*access
,
1376 __isl_take isl_schedule
*schedule
)
1378 if (!access
|| !schedule
)
1381 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1382 isl_schedule_free(access
->schedule
);
1383 access
->schedule
= schedule
;
1387 isl_union_access_info_free(access
);
1388 isl_schedule_free(schedule
);
1392 /* Replace the schedule map of "access" by "schedule_map".
1393 * Also free the schedule in case it was set last.
1395 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1396 __isl_take isl_union_access_info
*access
,
1397 __isl_take isl_union_map
*schedule_map
)
1399 if (!access
|| !schedule_map
)
1402 isl_union_map_free(access
->schedule_map
);
1403 access
->schedule
= isl_schedule_free(access
->schedule
);
1404 access
->schedule_map
= schedule_map
;
1408 isl_union_access_info_free(access
);
1409 isl_union_map_free(schedule_map
);
1413 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1414 __isl_keep isl_union_access_info
*access
)
1416 isl_union_access_info
*copy
;
1417 enum isl_access_type i
;
1421 copy
= isl_union_access_info_from_sink(
1422 isl_union_map_copy(access
->access
[isl_access_sink
]));
1423 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1424 copy
= isl_union_access_info_set(copy
, i
,
1425 isl_union_map_copy(access
->access
[i
]));
1426 if (access
->schedule
)
1427 copy
= isl_union_access_info_set_schedule(copy
,
1428 isl_schedule_copy(access
->schedule
));
1430 copy
= isl_union_access_info_set_schedule_map(copy
,
1431 isl_union_map_copy(access
->schedule_map
));
1436 /* Print a key-value pair of a YAML mapping to "p",
1437 * with key "name" and value "umap".
1439 static __isl_give isl_printer
*print_union_map_field(__isl_take isl_printer
*p
,
1440 const char *name
, __isl_keep isl_union_map
*umap
)
1442 p
= isl_printer_print_str(p
, name
);
1443 p
= isl_printer_yaml_next(p
);
1444 p
= isl_printer_print_str(p
, "\"");
1445 p
= isl_printer_print_union_map(p
, umap
);
1446 p
= isl_printer_print_str(p
, "\"");
1447 p
= isl_printer_yaml_next(p
);
1452 /* Print the information contained in "access" to "p".
1453 * The information is printed as a YAML document.
1455 __isl_give isl_printer
*isl_printer_print_union_access_info(
1456 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1459 return isl_printer_free(p
);
1461 p
= isl_printer_yaml_start_mapping(p
);
1462 p
= print_union_map_field(p
, "sink", access
->access
[isl_access_sink
]);
1463 p
= print_union_map_field(p
, "must_source",
1464 access
->access
[isl_access_must_source
]);
1465 p
= print_union_map_field(p
, "may_source",
1466 access
->access
[isl_access_may_source
]);
1467 if (access
->schedule
) {
1468 p
= isl_printer_print_str(p
, "schedule");
1469 p
= isl_printer_yaml_next(p
);
1470 p
= isl_printer_print_schedule(p
, access
->schedule
);
1471 p
= isl_printer_yaml_next(p
);
1473 p
= print_union_map_field(p
, "schedule_map",
1474 access
->schedule_map
);
1476 p
= isl_printer_yaml_end_mapping(p
);
1481 /* Return a string representation of the information in "access".
1482 * The information is printed in flow format.
1484 __isl_give
char *isl_union_access_info_to_str(
1485 __isl_keep isl_union_access_info
*access
)
1493 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1494 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1495 p
= isl_printer_print_union_access_info(p
, access
);
1496 s
= isl_printer_get_str(p
);
1497 isl_printer_free(p
);
1502 /* Update the fields of "access" such that they all have the same parameters,
1503 * keeping in mind that the schedule_map field may be NULL and ignoring
1504 * the schedule field.
1506 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1507 __isl_take isl_union_access_info
*access
)
1510 enum isl_access_type i
;
1515 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
1516 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1517 space
= isl_space_align_params(space
,
1518 isl_union_map_get_space(access
->access
[i
]));
1519 if (access
->schedule_map
)
1520 space
= isl_space_align_params(space
,
1521 isl_union_map_get_space(access
->schedule_map
));
1522 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1524 isl_union_map_align_params(access
->access
[i
],
1525 isl_space_copy(space
));
1526 if (!access
->schedule_map
) {
1527 isl_space_free(space
);
1529 access
->schedule_map
=
1530 isl_union_map_align_params(access
->schedule_map
, space
);
1531 if (!access
->schedule_map
)
1532 return isl_union_access_info_free(access
);
1535 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1536 if (!access
->access
[i
])
1537 return isl_union_access_info_free(access
);
1542 /* Prepend the schedule dimensions to the iteration domains.
1544 * That is, if the schedule is of the form
1548 * while the access relations are of the form
1552 * then the updated access relations are of the form
1556 * The schedule map is also replaced by the map
1560 * that is used during the internal computation.
1561 * Neither the original schedule map nor this updated schedule map
1562 * are used after the call to this function.
1564 static __isl_give isl_union_access_info
*
1565 isl_union_access_info_introduce_schedule(
1566 __isl_take isl_union_access_info
*access
)
1569 enum isl_access_type i
;
1574 sm
= isl_union_map_reverse(access
->schedule_map
);
1575 sm
= isl_union_map_range_map(sm
);
1576 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1578 isl_union_map_apply_range(isl_union_map_copy(sm
),
1580 access
->schedule_map
= sm
;
1582 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1583 if (!access
->access
[i
])
1584 return isl_union_access_info_free(access
);
1585 if (!access
->schedule_map
)
1586 return isl_union_access_info_free(access
);
1591 /* This structure represents the result of a dependence analysis computation.
1593 * "must_dep" represents the full definite dependences
1594 * "may_dep" represents the full non-definite dependences.
1595 * Both are of the form
1597 * [Source] -> [[Sink -> Data]]
1599 * (after the schedule dimensions have been projected out).
1600 * "must_no_source" represents the subset of the sink accesses for which
1601 * definitely no source was found.
1602 * "may_no_source" represents the subset of the sink accesses for which
1603 * possibly, but not definitely, no source was found.
1605 struct isl_union_flow
{
1606 isl_union_map
*must_dep
;
1607 isl_union_map
*may_dep
;
1608 isl_union_map
*must_no_source
;
1609 isl_union_map
*may_no_source
;
1612 /* Return the isl_ctx to which "flow" belongs.
1614 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
1616 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
1619 /* Free "flow" and return NULL.
1621 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
1625 isl_union_map_free(flow
->must_dep
);
1626 isl_union_map_free(flow
->may_dep
);
1627 isl_union_map_free(flow
->must_no_source
);
1628 isl_union_map_free(flow
->may_no_source
);
1633 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
1638 fprintf(stderr
, "must dependences: ");
1639 isl_union_map_dump(flow
->must_dep
);
1640 fprintf(stderr
, "may dependences: ");
1641 isl_union_map_dump(flow
->may_dep
);
1642 fprintf(stderr
, "must no source: ");
1643 isl_union_map_dump(flow
->must_no_source
);
1644 fprintf(stderr
, "may no source: ");
1645 isl_union_map_dump(flow
->may_no_source
);
1648 /* Return the full definite dependences in "flow", with accessed elements.
1650 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
1651 __isl_keep isl_union_flow
*flow
)
1655 return isl_union_map_copy(flow
->must_dep
);
1658 /* Return the full possible dependences in "flow", including the definite
1659 * dependences, with accessed elements.
1661 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
1662 __isl_keep isl_union_flow
*flow
)
1666 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1667 isl_union_map_copy(flow
->may_dep
));
1670 /* Return the definite dependences in "flow", without the accessed elements.
1672 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
1673 __isl_keep isl_union_flow
*flow
)
1679 dep
= isl_union_map_copy(flow
->must_dep
);
1680 return isl_union_map_range_factor_domain(dep
);
1683 /* Return the possible dependences in "flow", including the definite
1684 * dependences, without the accessed elements.
1686 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
1687 __isl_keep isl_union_flow
*flow
)
1693 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1694 isl_union_map_copy(flow
->may_dep
));
1695 return isl_union_map_range_factor_domain(dep
);
1698 /* Return the non-definite dependences in "flow".
1700 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
1701 __isl_keep isl_union_flow
*flow
)
1705 return isl_union_map_copy(flow
->may_dep
);
1708 /* Return the subset of the sink accesses for which definitely
1709 * no source was found.
1711 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
1712 __isl_keep isl_union_flow
*flow
)
1716 return isl_union_map_copy(flow
->must_no_source
);
1719 /* Return the subset of the sink accesses for which possibly
1720 * no source was found, including those for which definitely
1721 * no source was found.
1723 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
1724 __isl_keep isl_union_flow
*flow
)
1728 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
1729 isl_union_map_copy(flow
->may_no_source
));
1732 /* Return the subset of the sink accesses for which possibly, but not
1733 * definitely, no source was found.
1735 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
1736 __isl_keep isl_union_flow
*flow
)
1740 return isl_union_map_copy(flow
->may_no_source
);
1743 /* Create a new isl_union_flow object, initialized with empty
1744 * dependence relations and sink subsets.
1746 static __isl_give isl_union_flow
*isl_union_flow_alloc(
1747 __isl_take isl_space
*space
)
1750 isl_union_map
*empty
;
1751 isl_union_flow
*flow
;
1755 ctx
= isl_space_get_ctx(space
);
1756 flow
= isl_alloc_type(ctx
, isl_union_flow
);
1760 empty
= isl_union_map_empty(space
);
1761 flow
->must_dep
= isl_union_map_copy(empty
);
1762 flow
->may_dep
= isl_union_map_copy(empty
);
1763 flow
->must_no_source
= isl_union_map_copy(empty
);
1764 flow
->may_no_source
= empty
;
1766 if (!flow
->must_dep
|| !flow
->may_dep
||
1767 !flow
->must_no_source
|| !flow
->may_no_source
)
1768 return isl_union_flow_free(flow
);
1772 isl_space_free(space
);
1776 /* Copy this isl_union_flow object.
1778 __isl_give isl_union_flow
*isl_union_flow_copy(__isl_keep isl_union_flow
*flow
)
1780 isl_union_flow
*copy
;
1785 copy
= isl_union_flow_alloc(isl_union_map_get_space(flow
->must_dep
));
1790 copy
->must_dep
= isl_union_map_union(copy
->must_dep
,
1791 isl_union_map_copy(flow
->must_dep
));
1792 copy
->may_dep
= isl_union_map_union(copy
->may_dep
,
1793 isl_union_map_copy(flow
->may_dep
));
1794 copy
->must_no_source
= isl_union_map_union(copy
->must_no_source
,
1795 isl_union_map_copy(flow
->must_no_source
));
1796 copy
->may_no_source
= isl_union_map_union(copy
->may_no_source
,
1797 isl_union_map_copy(flow
->may_no_source
));
1799 if (!copy
->must_dep
|| !copy
->may_dep
||
1800 !copy
->must_no_source
|| !copy
->may_no_source
)
1801 return isl_union_flow_free(copy
);
1806 /* Drop the schedule dimensions from the iteration domains in "flow".
1807 * In particular, the schedule dimensions have been prepended
1808 * to the iteration domains prior to the dependence analysis by
1809 * replacing the iteration domain D, by the wrapped map [S -> D].
1810 * Replace these wrapped maps by the original D.
1812 * In particular, the dependences computed by access_info_compute_flow_core
1815 * [S -> D] -> [[S' -> D'] -> A]
1817 * The schedule dimensions are projected out by first currying the range,
1820 * [S -> D] -> [S' -> [D' -> A]]
1822 * and then computing the factor range
1826 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
1827 __isl_take isl_union_flow
*flow
)
1832 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
1833 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
1834 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
1835 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
1836 flow
->must_no_source
=
1837 isl_union_map_domain_factor_range(flow
->must_no_source
);
1838 flow
->may_no_source
=
1839 isl_union_map_domain_factor_range(flow
->may_no_source
);
1841 if (!flow
->must_dep
|| !flow
->may_dep
||
1842 !flow
->must_no_source
|| !flow
->may_no_source
)
1843 return isl_union_flow_free(flow
);
1848 struct isl_compute_flow_data
{
1849 isl_union_map
*must_source
;
1850 isl_union_map
*may_source
;
1851 isl_union_flow
*flow
;
1856 struct isl_sched_info
*sink_info
;
1857 struct isl_sched_info
**source_info
;
1858 isl_access_info
*accesses
;
1861 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
1865 struct isl_compute_flow_data
*data
;
1867 data
= (struct isl_compute_flow_data
*)user
;
1869 dim
= isl_space_range(isl_map_get_space(map
));
1871 eq
= isl_space_is_equal(dim
, data
->dim
);
1873 isl_space_free(dim
);
1877 return isl_stat_error
;
1884 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
1888 struct isl_sched_info
*info
;
1889 struct isl_compute_flow_data
*data
;
1891 data
= (struct isl_compute_flow_data
*)user
;
1893 dim
= isl_space_range(isl_map_get_space(map
));
1895 eq
= isl_space_is_equal(dim
, data
->dim
);
1897 isl_space_free(dim
);
1906 info
= sched_info_alloc(map
);
1907 data
->source_info
[data
->count
] = info
;
1909 data
->accesses
= isl_access_info_add_source(data
->accesses
,
1910 map
, data
->must
, info
);
1917 return isl_stat_error
;
1920 /* Determine the shared nesting level and the "textual order" of
1921 * the given accesses.
1923 * We first determine the minimal schedule dimension for both accesses.
1925 * If among those dimensions, we can find one where both have a fixed
1926 * value and if moreover those values are different, then the previous
1927 * dimension is the last shared nesting level and the textual order
1928 * is determined based on the order of the fixed values.
1929 * If no such fixed values can be found, then we set the shared
1930 * nesting level to the minimal schedule dimension, with no textual ordering.
1932 static int before(void *first
, void *second
)
1934 struct isl_sched_info
*info1
= first
;
1935 struct isl_sched_info
*info2
= second
;
1939 n1
= isl_vec_size(info1
->cst
);
1940 n2
= isl_vec_size(info2
->cst
);
1945 for (i
= 0; i
< n1
; ++i
) {
1949 if (!info1
->is_cst
[i
])
1951 if (!info2
->is_cst
[i
])
1953 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
1957 r
= 2 * i
+ (cmp
< 0);
1965 /* Given a sink access, look for all the source accesses that access
1966 * the same array and perform dataflow analysis on them using
1967 * isl_access_info_compute_flow_core.
1969 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
1973 struct isl_compute_flow_data
*data
;
1977 data
= (struct isl_compute_flow_data
*)user
;
1980 ctx
= isl_map_get_ctx(map
);
1982 data
->accesses
= NULL
;
1983 data
->sink_info
= NULL
;
1984 data
->source_info
= NULL
;
1986 data
->dim
= isl_space_range(isl_map_get_space(map
));
1988 if (isl_union_map_foreach_map(data
->must_source
,
1989 &count_matching_array
, data
) < 0)
1991 if (isl_union_map_foreach_map(data
->may_source
,
1992 &count_matching_array
, data
) < 0)
1995 data
->sink_info
= sched_info_alloc(map
);
1996 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
1999 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
2000 data
->sink_info
, &before
, data
->count
);
2001 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
2006 if (isl_union_map_foreach_map(data
->must_source
,
2007 &collect_matching_array
, data
) < 0)
2010 if (isl_union_map_foreach_map(data
->may_source
,
2011 &collect_matching_array
, data
) < 0)
2014 flow
= access_info_compute_flow_core(data
->accesses
);
2015 data
->accesses
= NULL
;
2020 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
2021 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
2022 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
2023 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
2025 for (i
= 0; i
< flow
->n_source
; ++i
) {
2027 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
2028 if (flow
->dep
[i
].must
)
2029 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
2031 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
2034 isl_flow_free(flow
);
2036 sched_info_free(data
->sink_info
);
2037 if (data
->source_info
) {
2038 for (i
= 0; i
< data
->count
; ++i
)
2039 sched_info_free(data
->source_info
[i
]);
2040 free(data
->source_info
);
2042 isl_space_free(data
->dim
);
2047 isl_access_info_free(data
->accesses
);
2048 sched_info_free(data
->sink_info
);
2049 if (data
->source_info
) {
2050 for (i
= 0; i
< data
->count
; ++i
)
2051 sched_info_free(data
->source_info
[i
]);
2052 free(data
->source_info
);
2054 isl_space_free(data
->dim
);
2057 return isl_stat_error
;
2060 /* Remove the must accesses from the may accesses.
2062 * A must access always trumps a may access, so there is no need
2063 * for a must access to also be considered as a may access. Doing so
2064 * would only cost extra computations only to find out that
2065 * the duplicated may access does not make any difference.
2067 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2068 __isl_take isl_union_access_info
*access
)
2072 access
->access
[isl_access_may_source
] =
2073 isl_union_map_subtract(access
->access
[isl_access_may_source
],
2074 isl_union_map_copy(access
->access
[isl_access_must_source
]));
2075 if (!access
->access
[isl_access_may_source
])
2076 return isl_union_access_info_free(access
);
2081 /* Given a description of the "sink" accesses, the "source" accesses and
2082 * a schedule, compute for each instance of a sink access
2083 * and for each element accessed by that instance,
2084 * the possible or definite source accesses that last accessed the
2085 * element accessed by the sink access before this sink access
2086 * in the sense that there is no intermediate definite source access.
2088 * The must_no_source and may_no_source elements of the result
2089 * are subsets of access->sink. The elements must_dep and may_dep
2090 * map domain elements of access->{may,must)_source to
2091 * domain elements of access->sink.
2093 * This function is used when only the schedule map representation
2096 * We first prepend the schedule dimensions to the domain
2097 * of the accesses so that we can easily compare their relative order.
2098 * Then we consider each sink access individually in compute_flow.
2100 static __isl_give isl_union_flow
*compute_flow_union_map(
2101 __isl_take isl_union_access_info
*access
)
2103 struct isl_compute_flow_data data
;
2104 isl_union_map
*sink
;
2106 access
= isl_union_access_info_align_params(access
);
2107 access
= isl_union_access_info_introduce_schedule(access
);
2111 data
.must_source
= access
->access
[isl_access_must_source
];
2112 data
.may_source
= access
->access
[isl_access_may_source
];
2114 sink
= access
->access
[isl_access_sink
];
2115 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(sink
));
2117 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
2120 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2122 isl_union_access_info_free(access
);
2125 isl_union_access_info_free(access
);
2126 isl_union_flow_free(data
.flow
);
2130 /* A schedule access relation.
2132 * The access relation "access" is of the form [S -> D] -> A,
2133 * where S corresponds to the prefix schedule at "node".
2134 * "must" is only relevant for source accesses and indicates
2135 * whether the access is a must source or a may source.
2137 struct isl_scheduled_access
{
2140 isl_schedule_node
*node
;
2143 /* Data structure for keeping track of individual scheduled sink and source
2144 * accesses when computing dependence analysis based on a schedule tree.
2146 * "n_sink" is the number of used entries in "sink"
2147 * "n_source" is the number of used entries in "source"
2149 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2150 * to keep track of the current node and
2151 * of what extract_sink_source needs to do.
2153 struct isl_compute_flow_schedule_data
{
2154 isl_union_access_info
*access
;
2159 struct isl_scheduled_access
*sink
;
2160 struct isl_scheduled_access
*source
;
2164 isl_schedule_node
*node
;
2167 /* Align the parameters of all sinks with all sources.
2169 * If there are no sinks or no sources, then no alignment is needed.
2171 static void isl_compute_flow_schedule_data_align_params(
2172 struct isl_compute_flow_schedule_data
*data
)
2177 if (data
->n_sink
== 0 || data
->n_source
== 0)
2180 space
= isl_map_get_space(data
->sink
[0].access
);
2182 for (i
= 1; i
< data
->n_sink
; ++i
)
2183 space
= isl_space_align_params(space
,
2184 isl_map_get_space(data
->sink
[i
].access
));
2185 for (i
= 0; i
< data
->n_source
; ++i
)
2186 space
= isl_space_align_params(space
,
2187 isl_map_get_space(data
->source
[i
].access
));
2189 for (i
= 0; i
< data
->n_sink
; ++i
)
2190 data
->sink
[i
].access
=
2191 isl_map_align_params(data
->sink
[i
].access
,
2192 isl_space_copy(space
));
2193 for (i
= 0; i
< data
->n_source
; ++i
)
2194 data
->source
[i
].access
=
2195 isl_map_align_params(data
->source
[i
].access
,
2196 isl_space_copy(space
));
2198 isl_space_free(space
);
2201 /* Free all the memory referenced from "data".
2202 * Do not free "data" itself as it may be allocated on the stack.
2204 static void isl_compute_flow_schedule_data_clear(
2205 struct isl_compute_flow_schedule_data
*data
)
2212 for (i
= 0; i
< data
->n_sink
; ++i
) {
2213 isl_map_free(data
->sink
[i
].access
);
2214 isl_schedule_node_free(data
->sink
[i
].node
);
2217 for (i
= 0; i
< data
->n_source
; ++i
) {
2218 isl_map_free(data
->source
[i
].access
);
2219 isl_schedule_node_free(data
->source
[i
].node
);
2225 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2226 * (an upper bound on) the number of sinks and sources.
2228 * Sinks and sources are only extracted at leaves of the tree,
2229 * so we skip the node if it is not a leaf.
2230 * Otherwise we increment data->n_sink and data->n_source with
2231 * the number of spaces in the sink and source access domains
2232 * that reach this node.
2234 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2237 struct isl_compute_flow_schedule_data
*data
= user
;
2238 isl_union_set
*domain
;
2239 isl_union_map
*umap
;
2240 isl_bool r
= isl_bool_false
;
2242 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2243 return isl_bool_true
;
2245 domain
= isl_schedule_node_get_universe_domain(node
);
2247 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2248 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2249 data
->n_sink
+= isl_union_map_n_map(umap
);
2250 isl_union_map_free(umap
);
2254 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2255 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2256 data
->n_source
+= isl_union_map_n_map(umap
);
2257 isl_union_map_free(umap
);
2261 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2262 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2263 data
->n_source
+= isl_union_map_n_map(umap
);
2264 isl_union_map_free(umap
);
2268 isl_union_set_free(domain
);
2273 /* Add a single scheduled sink or source (depending on data->set_sink)
2274 * with scheduled access relation "map", must property data->must and
2275 * schedule node data->node to the list of sinks or sources.
2277 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2279 struct isl_compute_flow_schedule_data
*data
= user
;
2280 struct isl_scheduled_access
*access
;
2283 access
= data
->sink
+ data
->n_sink
++;
2285 access
= data
->source
+ data
->n_source
++;
2287 access
->access
= map
;
2288 access
->must
= data
->must
;
2289 access
->node
= isl_schedule_node_copy(data
->node
);
2294 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2295 * individual scheduled source and sink accesses (taking into account
2296 * the domain of the schedule).
2298 * We only collect accesses at the leaves of the schedule tree.
2299 * We prepend the schedule dimensions at the leaf to the iteration
2300 * domains of the source and sink accesses and then extract
2301 * the individual accesses (per space).
2303 * In particular, if the prefix schedule at the node is of the form
2307 * while the access relations are of the form
2311 * then the updated access relations are of the form
2315 * Note that S consists of a single space such that introducing S
2316 * in the access relations does not increase the number of spaces.
2318 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2321 struct isl_compute_flow_schedule_data
*data
= user
;
2322 isl_union_map
*prefix
;
2323 isl_union_map
*umap
;
2324 isl_bool r
= isl_bool_false
;
2326 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2327 return isl_bool_true
;
2331 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2332 prefix
= isl_union_map_reverse(prefix
);
2333 prefix
= isl_union_map_range_map(prefix
);
2336 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2337 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2338 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2340 isl_union_map_free(umap
);
2344 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2345 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2346 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2348 isl_union_map_free(umap
);
2352 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2353 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2354 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2356 isl_union_map_free(umap
);
2358 isl_union_map_free(prefix
);
2363 /* isl_access_info_compute_flow callback for determining whether
2364 * the shared nesting level and the ordering within that level
2365 * for two scheduled accesses for use in compute_single_flow.
2367 * The tokens passed to this function refer to the leaves
2368 * in the schedule tree where the accesses take place.
2370 * If n is the shared number of loops, then we need to return
2371 * "2 * n + 1" if "first" precedes "second" inside the innermost
2372 * shared loop and "2 * n" otherwise.
2374 * The innermost shared ancestor may be the leaves themselves
2375 * if the accesses take place in the same leaf. Otherwise,
2376 * it is either a set node or a sequence node. Only in the case
2377 * of a sequence node do we consider one access to precede the other.
2379 static int before_node(void *first
, void *second
)
2381 isl_schedule_node
*node1
= first
;
2382 isl_schedule_node
*node2
= second
;
2383 isl_schedule_node
*shared
;
2387 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2391 depth
= isl_schedule_node_get_schedule_depth(shared
);
2392 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2395 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2397 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2399 before
= pos1
< pos2
;
2402 isl_schedule_node_free(shared
);
2404 return 2 * depth
+ before
;
2407 /* Add the scheduled sources from "data" that access
2408 * the same data space as "sink" to "access".
2410 static __isl_give isl_access_info
*add_matching_sources(
2411 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2412 struct isl_compute_flow_schedule_data
*data
)
2417 space
= isl_space_range(isl_map_get_space(sink
->access
));
2418 for (i
= 0; i
< data
->n_source
; ++i
) {
2419 struct isl_scheduled_access
*source
;
2420 isl_space
*source_space
;
2423 source
= &data
->source
[i
];
2424 source_space
= isl_map_get_space(source
->access
);
2425 source_space
= isl_space_range(source_space
);
2426 eq
= isl_space_is_equal(space
, source_space
);
2427 isl_space_free(source_space
);
2434 access
= isl_access_info_add_source(access
,
2435 isl_map_copy(source
->access
), source
->must
, source
->node
);
2438 isl_space_free(space
);
2441 isl_space_free(space
);
2442 isl_access_info_free(access
);
2446 /* Given a scheduled sink access relation "sink", compute the corresponding
2447 * dependences on the sources in "data" and add the computed dependences
2450 * The dependences computed by access_info_compute_flow_core are of the form
2452 * [S -> I] -> [[S' -> I'] -> A]
2454 * The schedule dimensions are projected out by first currying the range,
2457 * [S -> I] -> [S' -> [I' -> A]]
2459 * and then computing the factor range
2463 static __isl_give isl_union_flow
*compute_single_flow(
2464 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
2465 struct isl_compute_flow_schedule_data
*data
)
2468 isl_access_info
*access
;
2475 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
2476 &before_node
, data
->n_source
);
2477 access
= add_matching_sources(access
, sink
, data
);
2479 flow
= access_info_compute_flow_core(access
);
2481 return isl_union_flow_free(uf
);
2483 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
2484 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
2485 isl_union_map_from_map(map
));
2486 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
2487 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
2488 isl_union_map_from_map(map
));
2490 for (i
= 0; i
< flow
->n_source
; ++i
) {
2493 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
2494 map
= isl_map_factor_range(map
);
2495 dep
= isl_union_map_from_map(map
);
2496 if (flow
->dep
[i
].must
)
2497 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
2499 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
2502 isl_flow_free(flow
);
2507 /* Given a description of the "sink" accesses, the "source" accesses and
2508 * a schedule, compute for each instance of a sink access
2509 * and for each element accessed by that instance,
2510 * the possible or definite source accesses that last accessed the
2511 * element accessed by the sink access before this sink access
2512 * in the sense that there is no intermediate definite source access.
2513 * Only consider dependences between statement instances that belong
2514 * to the domain of the schedule.
2516 * The must_no_source and may_no_source elements of the result
2517 * are subsets of access->sink. The elements must_dep and may_dep
2518 * map domain elements of access->{may,must)_source to
2519 * domain elements of access->sink.
2521 * This function is used when a schedule tree representation
2524 * We extract the individual scheduled source and sink access relations
2525 * (taking into account the domain of the schedule) and
2526 * then compute dependences for each scheduled sink individually.
2528 static __isl_give isl_union_flow
*compute_flow_schedule(
2529 __isl_take isl_union_access_info
*access
)
2531 struct isl_compute_flow_schedule_data data
= { access
};
2535 isl_union_flow
*flow
;
2537 ctx
= isl_union_access_info_get_ctx(access
);
2541 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2542 &count_sink_source
, &data
) < 0)
2545 n
= data
.n_sink
+ data
.n_source
;
2546 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
2547 if (n
&& !data
.sink
)
2549 data
.source
= data
.sink
+ data
.n_sink
;
2553 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2554 &collect_sink_source
, &data
) < 0)
2557 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
2558 flow
= isl_union_flow_alloc(space
);
2560 isl_compute_flow_schedule_data_align_params(&data
);
2562 for (i
= 0; i
< data
.n_sink
; ++i
)
2563 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
2565 isl_compute_flow_schedule_data_clear(&data
);
2567 isl_union_access_info_free(access
);
2570 isl_union_access_info_free(access
);
2571 isl_compute_flow_schedule_data_clear(&data
);
2575 /* Given a description of the "sink" accesses, the "source" accesses and
2576 * a schedule, compute for each instance of a sink access
2577 * and for each element accessed by that instance,
2578 * the possible or definite source accesses that last accessed the
2579 * element accessed by the sink access before this sink access
2580 * in the sense that there is no intermediate definite source access.
2582 * The must_no_source and may_no_source elements of the result
2583 * are subsets of access->sink. The elements must_dep and may_dep
2584 * map domain elements of access->{may,must)_source to
2585 * domain elements of access->sink.
2587 * We check whether the schedule is available as a schedule tree
2588 * or a schedule map and call the corresponding function to perform
2591 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
2592 __isl_take isl_union_access_info
*access
)
2594 access
= isl_union_access_info_normalize(access
);
2597 if (access
->schedule
)
2598 return compute_flow_schedule(access
);
2600 return compute_flow_union_map(access
);
2603 /* Print the information contained in "flow" to "p".
2604 * The information is printed as a YAML document.
2606 __isl_give isl_printer
*isl_printer_print_union_flow(
2607 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
2609 isl_union_map
*umap
;
2612 return isl_printer_free(p
);
2614 p
= isl_printer_yaml_start_mapping(p
);
2615 umap
= isl_union_flow_get_full_must_dependence(flow
);
2616 p
= print_union_map_field(p
, "must_dependence", umap
);
2617 isl_union_map_free(umap
);
2618 umap
= isl_union_flow_get_full_may_dependence(flow
);
2619 p
= print_union_map_field(p
, "may_dependence", umap
);
2620 isl_union_map_free(umap
);
2621 p
= print_union_map_field(p
, "must_no_source", flow
->must_no_source
);
2622 umap
= isl_union_flow_get_may_no_source(flow
);
2623 p
= print_union_map_field(p
, "may_no_source", umap
);
2624 isl_union_map_free(umap
);
2625 p
= isl_printer_yaml_end_mapping(p
);
2630 /* Return a string representation of the information in "flow".
2631 * The information is printed in flow format.
2633 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
2641 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
2642 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2643 p
= isl_printer_print_union_flow(p
, flow
);
2644 s
= isl_printer_get_str(p
);
2645 isl_printer_free(p
);
2650 /* Given a collection of "sink" and "source" accesses,
2651 * compute for each iteration of a sink access
2652 * and for each element accessed by that iteration,
2653 * the source access in the list that last accessed the
2654 * element accessed by the sink access before this sink access.
2655 * Each access is given as a map from the loop iterators
2656 * to the array indices.
2657 * The result is a relations between source and sink
2658 * iterations and a subset of the domain of the sink accesses,
2659 * corresponding to those iterations that access an element
2660 * not previously accessed.
2662 * We collect the inputs in an isl_union_access_info object,
2663 * call isl_union_access_info_compute_flow and extract
2664 * the outputs from the result.
2666 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
2667 __isl_take isl_union_map
*must_source
,
2668 __isl_take isl_union_map
*may_source
,
2669 __isl_take isl_union_map
*schedule
,
2670 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
2671 __isl_give isl_union_map
**must_no_source
,
2672 __isl_give isl_union_map
**may_no_source
)
2674 isl_union_access_info
*access
;
2675 isl_union_flow
*flow
;
2677 access
= isl_union_access_info_from_sink(sink
);
2678 access
= isl_union_access_info_set_must_source(access
, must_source
);
2679 access
= isl_union_access_info_set_may_source(access
, may_source
);
2680 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
2681 flow
= isl_union_access_info_compute_flow(access
);
2684 *must_dep
= isl_union_flow_get_must_dependence(flow
);
2686 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
2688 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
2690 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
2692 isl_union_flow_free(flow
);
2694 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
2695 (must_no_source
&& !*must_no_source
) ||
2696 (may_no_source
&& !*may_no_source
))
2702 *must_dep
= isl_union_map_free(*must_dep
);
2704 *may_dep
= isl_union_map_free(*may_dep
);
2706 *must_no_source
= isl_union_map_free(*must_no_source
);
2708 *may_no_source
= isl_union_map_free(*may_no_source
);