2 * Copyright 2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
11 #include <isl_schedule_band.h>
12 #include <isl_schedule_private.h>
15 #define EL isl_schedule_tree
17 #include <isl_list_templ.h>
20 #define BASE schedule_tree
22 #include <isl_list_templ.c>
24 /* Is "tree" the leaf of a schedule tree?
26 int isl_schedule_tree_is_leaf(__isl_keep isl_schedule_tree
*tree
)
28 return isl_schedule_tree_get_type(tree
) == isl_schedule_node_leaf
;
31 /* Create a new schedule tree of type "type".
32 * The caller is responsible for filling in the type specific fields and
35 static __isl_give isl_schedule_tree
*isl_schedule_tree_alloc(isl_ctx
*ctx
,
36 enum isl_schedule_node_type type
)
38 isl_schedule_tree
*tree
;
40 if (type
== isl_schedule_node_error
)
43 tree
= isl_calloc_type(ctx
, isl_schedule_tree
);
55 /* Return a fresh copy of "tree".
57 __isl_take isl_schedule_tree
*isl_schedule_tree_dup(
58 __isl_keep isl_schedule_tree
*tree
)
61 isl_schedule_tree
*dup
;
66 ctx
= isl_schedule_tree_get_ctx(tree
);
67 dup
= isl_schedule_tree_alloc(ctx
, tree
->type
);
72 case isl_schedule_node_error
:
73 isl_die(ctx
, isl_error_internal
,
74 "allocation should have failed",
75 isl_schedule_tree_free(dup
));
76 case isl_schedule_node_band
:
77 dup
->band
= isl_schedule_band_copy(tree
->band
);
79 return isl_schedule_tree_free(dup
);
81 case isl_schedule_node_domain
:
82 dup
->domain
= isl_union_set_copy(tree
->domain
);
84 return isl_schedule_tree_free(dup
);
86 case isl_schedule_node_filter
:
87 dup
->filter
= isl_union_set_copy(tree
->filter
);
89 return isl_schedule_tree_free(dup
);
91 case isl_schedule_node_leaf
:
92 case isl_schedule_node_sequence
:
93 case isl_schedule_node_set
:
98 dup
->children
= isl_schedule_tree_list_copy(tree
->children
);
100 return isl_schedule_tree_free(dup
);
106 /* Return an isl_schedule_tree that is equal to "tree" and that has only
107 * a single reference.
109 * This function is called before a tree is modified.
110 * A static tree (with negative reference count) should never be modified,
111 * so it is not allowed to call this function on a static tree.
113 __isl_give isl_schedule_tree
*isl_schedule_tree_cow(
114 __isl_take isl_schedule_tree
*tree
)
120 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
121 "static trees cannot be modified",
122 return isl_schedule_tree_free(tree
));
127 return isl_schedule_tree_dup(tree
);
130 /* Return a new reference to "tree".
132 * A static tree (with negative reference count) does not keep track
133 * of the number of references and should not be modified.
135 __isl_give isl_schedule_tree
*isl_schedule_tree_copy(
136 __isl_keep isl_schedule_tree
*tree
)
148 /* Free "tree" and return NULL.
150 __isl_null isl_schedule_tree
*isl_schedule_tree_free(
151 __isl_take isl_schedule_tree
*tree
)
160 switch (tree
->type
) {
161 case isl_schedule_node_band
:
162 isl_schedule_band_free(tree
->band
);
164 case isl_schedule_node_domain
:
165 isl_union_set_free(tree
->domain
);
167 case isl_schedule_node_filter
:
168 isl_union_set_free(tree
->filter
);
170 case isl_schedule_node_sequence
:
171 case isl_schedule_node_set
:
172 case isl_schedule_node_error
:
173 case isl_schedule_node_leaf
:
176 isl_schedule_tree_list_free(tree
->children
);
177 isl_ctx_deref(tree
->ctx
);
183 /* Create and return a new leaf schedule tree.
185 __isl_give isl_schedule_tree
*isl_schedule_tree_leaf(isl_ctx
*ctx
)
187 return isl_schedule_tree_alloc(ctx
, isl_schedule_node_leaf
);
190 /* Create a new band schedule tree referring to "band"
193 __isl_give isl_schedule_tree
*isl_schedule_tree_from_band(
194 __isl_take isl_schedule_band
*band
)
197 isl_schedule_tree
*tree
;
202 ctx
= isl_schedule_band_get_ctx(band
);
203 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_band
);
211 isl_schedule_band_free(band
);
215 /* Create a new domain schedule tree with the given domain and no children.
217 __isl_give isl_schedule_tree
*isl_schedule_tree_from_domain(
218 __isl_take isl_union_set
*domain
)
221 isl_schedule_tree
*tree
;
226 ctx
= isl_union_set_get_ctx(domain
);
227 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_domain
);
231 tree
->domain
= domain
;
235 isl_union_set_free(domain
);
239 /* Create a new filter schedule tree with the given filter and no children.
241 __isl_give isl_schedule_tree
*isl_schedule_tree_from_filter(
242 __isl_take isl_union_set
*filter
)
245 isl_schedule_tree
*tree
;
250 ctx
= isl_union_set_get_ctx(filter
);
251 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_filter
);
255 tree
->filter
= filter
;
259 isl_union_set_free(filter
);
263 /* Create a new tree of the given type (isl_schedule_node_sequence or
264 * isl_schedule_node_set) with the given children.
266 __isl_give isl_schedule_tree
*isl_schedule_tree_from_children(
267 enum isl_schedule_node_type type
,
268 __isl_take isl_schedule_tree_list
*list
)
271 isl_schedule_tree
*tree
;
276 ctx
= isl_schedule_tree_list_get_ctx(list
);
277 tree
= isl_schedule_tree_alloc(ctx
, type
);
281 tree
->children
= list
;
285 isl_schedule_tree_list_free(list
);
289 /* Return the isl_ctx to which "tree" belongs.
291 isl_ctx
*isl_schedule_tree_get_ctx(__isl_keep isl_schedule_tree
*tree
)
293 return tree
? tree
->ctx
: NULL
;
296 /* Return the type of the root of the tree or isl_schedule_node_error
299 enum isl_schedule_node_type
isl_schedule_tree_get_type(
300 __isl_keep isl_schedule_tree
*tree
)
302 return tree
? tree
->type
: isl_schedule_node_error
;
305 /* Does "tree" have any children, other than an implicit leaf.
307 int isl_schedule_tree_has_children(__isl_keep isl_schedule_tree
*tree
)
312 return tree
->children
!= NULL
;
315 /* Return the number of children of "tree", excluding implicit leaves.
317 int isl_schedule_tree_n_children(__isl_keep isl_schedule_tree
*tree
)
322 return isl_schedule_tree_list_n_schedule_tree(tree
->children
);
325 /* Return a copy of the (explicit) child at position "pos" of "tree".
327 __isl_give isl_schedule_tree
*isl_schedule_tree_get_child(
328 __isl_keep isl_schedule_tree
*tree
, int pos
)
333 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
334 "schedule tree has no explicit children", return NULL
);
335 return isl_schedule_tree_list_get_schedule_tree(tree
->children
, pos
);
338 /* Return a copy of the (explicit) child at position "pos" of "tree" and
341 __isl_give isl_schedule_tree
*isl_schedule_tree_child(
342 __isl_take isl_schedule_tree
*tree
, int pos
)
344 isl_schedule_tree
*child
;
346 child
= isl_schedule_tree_get_child(tree
, pos
);
347 isl_schedule_tree_free(tree
);
351 /* Remove all (explicit) children from "tree".
353 __isl_give isl_schedule_tree
*isl_schedule_tree_reset_children(
354 __isl_take isl_schedule_tree
*tree
)
356 tree
= isl_schedule_tree_cow(tree
);
359 tree
->children
= isl_schedule_tree_list_free(tree
->children
);
363 /* Replace the child at position "pos" of "tree" by "child".
365 * If the new child is a leaf, then it is not explicitly
366 * recorded in the list of children. Instead, the list of children
367 * (which is assumed to have only one element) is removed.
368 * Note that the children of set and sequence nodes are always
369 * filters, so they cannot be replaced by empty trees.
371 __isl_give isl_schedule_tree
*isl_schedule_tree_replace_child(
372 __isl_take isl_schedule_tree
*tree
, int pos
,
373 __isl_take isl_schedule_tree
*child
)
375 tree
= isl_schedule_tree_cow(tree
);
379 if (isl_schedule_tree_is_leaf(child
)) {
380 isl_schedule_tree_free(child
);
381 if (!tree
->children
&& pos
== 0)
383 if (isl_schedule_tree_n_children(tree
) != 1)
384 isl_die(isl_schedule_tree_get_ctx(tree
),
386 "can only replace single child by leaf",
388 return isl_schedule_tree_reset_children(tree
);
391 if (!tree
->children
&& pos
== 0)
393 isl_schedule_tree_list_from_schedule_tree(child
);
395 tree
->children
= isl_schedule_tree_list_set_schedule_tree(
396 tree
->children
, pos
, child
);
399 return isl_schedule_tree_free(tree
);
403 isl_schedule_tree_free(tree
);
404 isl_schedule_tree_free(child
);
408 /* Create a new band schedule tree referring to "band"
409 * with "tree" as single child.
411 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_band(
412 __isl_take isl_schedule_tree
*tree
, __isl_take isl_schedule_band
*band
)
414 isl_schedule_tree
*res
;
416 res
= isl_schedule_tree_from_band(band
);
417 return isl_schedule_tree_replace_child(res
, 0, tree
);
420 /* Create a new domain schedule tree with the given domain and
421 * with "tree" as single child.
423 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_domain(
424 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*domain
)
426 isl_schedule_tree
*res
;
428 res
= isl_schedule_tree_from_domain(domain
);
429 return isl_schedule_tree_replace_child(res
, 0, tree
);
432 /* Create a new filter schedule tree with the given filter and single child.
434 * If the root of "tree" is itself a filter node, then the two
435 * filter nodes are merged into one node.
437 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_filter(
438 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
)
440 isl_schedule_tree
*res
;
442 if (isl_schedule_tree_get_type(tree
) == isl_schedule_node_filter
) {
443 isl_union_set
*tree_filter
;
445 tree_filter
= isl_schedule_tree_filter_get_filter(tree
);
446 tree_filter
= isl_union_set_intersect(tree_filter
, filter
);
447 tree
= isl_schedule_tree_filter_set_filter(tree
, tree_filter
);
451 res
= isl_schedule_tree_from_filter(filter
);
452 return isl_schedule_tree_replace_child(res
, 0, tree
);
455 /* Return the number of members in the band tree root.
457 unsigned isl_schedule_tree_band_n_member(__isl_keep isl_schedule_tree
*tree
)
462 if (tree
->type
!= isl_schedule_node_band
)
463 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
464 "not a band node", return 0);
466 return isl_schedule_band_n_member(tree
->band
);
469 /* Is the band member at position "pos" of the band tree root
472 int isl_schedule_tree_band_member_get_coincident(
473 __isl_keep isl_schedule_tree
*tree
, int pos
)
478 if (tree
->type
!= isl_schedule_node_band
)
479 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
480 "not a band node", return -1);
482 return isl_schedule_band_member_get_coincident(tree
->band
, pos
);
485 /* Mark the given band member as being coincident or not
486 * according to "coincident".
488 __isl_give isl_schedule_tree
*isl_schedule_tree_band_member_set_coincident(
489 __isl_take isl_schedule_tree
*tree
, int pos
, int coincident
)
493 if (tree
->type
!= isl_schedule_node_band
)
494 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
495 "not a band node", return isl_schedule_tree_free(tree
));
496 if (isl_schedule_tree_band_member_get_coincident(tree
, pos
) ==
499 tree
= isl_schedule_tree_cow(tree
);
503 tree
->band
= isl_schedule_band_member_set_coincident(tree
->band
, pos
,
506 return isl_schedule_tree_free(tree
);
510 /* Is the band tree root marked permutable?
512 int isl_schedule_tree_band_get_permutable(__isl_keep isl_schedule_tree
*tree
)
517 if (tree
->type
!= isl_schedule_node_band
)
518 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
519 "not a band node", return -1);
521 return isl_schedule_band_get_permutable(tree
->band
);
524 /* Mark the band tree root permutable or not according to "permutable"?
526 __isl_give isl_schedule_tree
*isl_schedule_tree_band_set_permutable(
527 __isl_take isl_schedule_tree
*tree
, int permutable
)
531 if (tree
->type
!= isl_schedule_node_band
)
532 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
533 "not a band node", return isl_schedule_tree_free(tree
));
534 if (isl_schedule_tree_band_get_permutable(tree
) == permutable
)
536 tree
= isl_schedule_tree_cow(tree
);
540 tree
->band
= isl_schedule_band_set_permutable(tree
->band
, permutable
);
542 return isl_schedule_tree_free(tree
);
546 /* Return the schedule of the band tree root in isolation.
548 __isl_give isl_multi_union_pw_aff
*isl_schedule_tree_band_get_partial_schedule(
549 __isl_keep isl_schedule_tree
*tree
)
554 if (tree
->type
!= isl_schedule_node_band
)
555 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
556 "not a band node", return NULL
);
558 return isl_schedule_band_get_partial_schedule(tree
->band
);
561 /* Return the domain of the domain tree root.
563 __isl_give isl_union_set
*isl_schedule_tree_domain_get_domain(
564 __isl_keep isl_schedule_tree
*tree
)
569 if (tree
->type
!= isl_schedule_node_domain
)
570 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
571 "not a domain node", return NULL
);
573 return isl_union_set_copy(tree
->domain
);
576 /* Replace the domain of domain tree root "tree" by "domain".
578 __isl_give isl_schedule_tree
*isl_schedule_tree_domain_set_domain(
579 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*domain
)
581 tree
= isl_schedule_tree_cow(tree
);
582 if (!tree
|| !domain
)
585 if (tree
->type
!= isl_schedule_node_domain
)
586 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
587 "not a domain node", goto error
);
589 isl_union_set_free(tree
->domain
);
590 tree
->domain
= domain
;
594 isl_schedule_tree_free(tree
);
595 isl_union_set_free(domain
);
599 /* Return the filter of the filter tree root.
601 __isl_give isl_union_set
*isl_schedule_tree_filter_get_filter(
602 __isl_keep isl_schedule_tree
*tree
)
607 if (tree
->type
!= isl_schedule_node_filter
)
608 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
609 "not a filter node", return NULL
);
611 return isl_union_set_copy(tree
->filter
);
614 /* Replace the filter of the filter tree root by "filter".
616 __isl_give isl_schedule_tree
*isl_schedule_tree_filter_set_filter(
617 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
)
619 tree
= isl_schedule_tree_cow(tree
);
620 if (!tree
|| !filter
)
623 if (tree
->type
!= isl_schedule_node_filter
)
624 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
625 "not a filter node", return NULL
);
627 isl_union_set_free(tree
->filter
);
628 tree
->filter
= filter
;
632 isl_schedule_tree_free(tree
);
633 isl_union_set_free(filter
);
637 /* Set dim to the range dimension of "map" and abort the search.
639 static int set_range_dim(__isl_take isl_map
*map
, void *user
)
643 *dim
= isl_map_dim(map
, isl_dim_out
);
649 /* Return the dimension of the range of "umap".
650 * "umap" is assumed not to be empty and
651 * all maps inside "umap" are assumed to have the same range.
653 * We extract the range dimension from the first map in "umap".
655 static int range_dim(__isl_keep isl_union_map
*umap
)
661 if (isl_union_map_n_map(umap
) == 0)
662 isl_die(isl_union_map_get_ctx(umap
), isl_error_internal
,
663 "unexpected empty input", return -1);
665 isl_union_map_foreach_map(umap
, &set_range_dim
, &dim
);
670 /* Append an "extra" number of zeros to the range of "umap" and
673 static __isl_give isl_union_map
*append_range(__isl_take isl_union_map
*umap
,
679 isl_union_pw_multi_aff
*suffix
;
680 isl_union_map
*universe
;
681 isl_union_map
*suffix_umap
;
683 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
684 dom
= isl_union_map_domain(universe
);
685 space
= isl_union_set_get_space(dom
);
686 space
= isl_space_set_from_params(space
);
687 space
= isl_space_add_dims(space
, isl_dim_set
, extra
);
688 mv
= isl_multi_val_zero(space
);
690 suffix
= isl_union_pw_multi_aff_multi_val_on_domain(dom
, mv
);
691 suffix_umap
= isl_union_map_from_union_pw_multi_aff(suffix
);
692 umap
= isl_union_map_flat_range_product(umap
, suffix_umap
);
697 /* Move down to the first descendant of "tree" that contains any schedule
698 * information or return "leaf" if there is no such descendant.
700 __isl_give isl_schedule_tree
*isl_schedule_tree_first_schedule_descendant(
701 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_tree
*leaf
)
703 while (isl_schedule_tree_get_type(tree
) == isl_schedule_node_band
&&
704 isl_schedule_tree_band_n_member(tree
) == 0) {
705 if (!isl_schedule_tree_has_children(tree
)) {
706 isl_schedule_tree_free(tree
);
707 return isl_schedule_tree_copy(leaf
);
709 tree
= isl_schedule_tree_child(tree
, 0);
715 static __isl_give isl_union_map
*subtree_schedule_extend(
716 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
);
718 /* Extend the schedule map "outer" with the subtree schedule
719 * of the (single) child of "tree", if any.
721 * If "tree" does not have any descendants (apart from those that
722 * do not carry any schedule information), then we simply return "outer".
723 * Otherwise, we extend the schedule map "outer" with the subtree schedule
724 * of the single child.
726 static __isl_give isl_union_map
*subtree_schedule_extend_child(
727 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
729 isl_schedule_tree
*child
;
733 return isl_union_map_free(outer
);
734 if (!isl_schedule_tree_has_children(tree
))
736 child
= isl_schedule_tree_get_child(tree
, 0);
738 return isl_union_map_free(outer
);
739 res
= subtree_schedule_extend(child
, outer
);
740 isl_schedule_tree_free(child
);
744 /* Extract the parameter space from one of the children of "tree",
745 * which are assumed to be filters.
747 static __isl_give isl_space
*extract_space_from_filter_child(
748 __isl_keep isl_schedule_tree
*tree
)
752 isl_schedule_tree
*child
;
754 child
= isl_schedule_tree_list_get_schedule_tree(tree
->children
, 0);
755 dom
= isl_schedule_tree_filter_get_filter(child
);
756 space
= isl_union_set_get_space(dom
);
757 isl_union_set_free(dom
);
758 isl_schedule_tree_free(child
);
763 /* Extend the schedule map "outer" with the subtree schedule
764 * of a set or sequence node.
766 * The schedule for the set or sequence node itself is composed of
775 * The first form is used if there is only a single child or
776 * if the current node is a set node and the schedule_separate_components
779 * Each of the pieces above is extended with the subtree schedule of
780 * the child of the corresponding filter, if any, padded with zeros
781 * to ensure that all pieces have the same range dimension.
783 static __isl_give isl_union_map
*subtree_schedule_extend_from_children(
784 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
798 ctx
= isl_schedule_tree_get_ctx(tree
);
800 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
801 "missing children", return NULL
);
802 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
804 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
805 "missing children", return NULL
);
807 separate
= n
> 1 && (tree
->type
== isl_schedule_node_sequence
||
808 isl_options_get_schedule_separate_components(ctx
));
810 space
= extract_space_from_filter_child(tree
);
812 umap
= isl_union_map_empty(isl_space_copy(space
));
813 space
= isl_space_set_from_params(space
);
815 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
816 v
= isl_val_zero(ctx
);
818 mv
= isl_multi_val_zero(space
);
820 dim
= isl_multi_val_dim(mv
, isl_dim_set
);
821 for (i
= 0; i
< n
; ++i
) {
822 isl_union_pw_multi_aff
*upma
;
823 isl_union_map
*umap_i
;
825 isl_schedule_tree
*child
;
829 child
= isl_schedule_tree_list_get_schedule_tree(
831 dom
= isl_schedule_tree_filter_get_filter(child
);
834 mv
= isl_multi_val_set_val(mv
, 0, isl_val_copy(v
));
835 v
= isl_val_add_ui(v
, 1);
837 upma
= isl_union_pw_multi_aff_multi_val_on_domain(dom
,
838 isl_multi_val_copy(mv
));
839 umap_i
= isl_union_map_from_union_pw_multi_aff(upma
);
840 umap_i
= isl_union_map_flat_range_product(
841 isl_union_map_copy(outer
), umap_i
);
842 umap_i
= subtree_schedule_extend_child(child
, umap_i
);
843 isl_schedule_tree_free(child
);
845 empty
= isl_union_map_is_empty(umap_i
);
847 umap_i
= isl_union_map_free(umap_i
);
849 isl_union_map_free(umap_i
);
853 dim_i
= range_dim(umap_i
);
855 umap
= isl_union_map_free(umap
);
856 } else if (dim
< dim_i
) {
857 umap
= append_range(umap
, dim_i
- dim
);
859 } else if (dim_i
< dim
) {
860 umap_i
= append_range(umap_i
, dim
- dim_i
);
862 umap
= isl_union_map_union(umap
, umap_i
);
866 isl_multi_val_free(mv
);
867 isl_union_map_free(outer
);
872 /* Extend the schedule map "outer" with the subtree schedule of "tree".
874 * If the root of the tree is a set or a sequence, then we extend
875 * the schedule map in subtree_schedule_extend_from_children.
876 * Otherwise, we extend the schedule map with the partial schedule
877 * corresponding to the root of the tree and then continue with
878 * the single child of this root.
880 static __isl_give isl_union_map
*subtree_schedule_extend(
881 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
883 isl_multi_union_pw_aff
*mupa
;
885 isl_union_set
*domain
;
890 switch (tree
->type
) {
891 case isl_schedule_node_error
:
892 return isl_union_map_free(outer
);
893 case isl_schedule_node_band
:
894 if (isl_schedule_tree_band_n_member(tree
) == 0)
895 return subtree_schedule_extend_child(tree
, outer
);
896 mupa
= isl_schedule_band_get_partial_schedule(tree
->band
);
897 umap
= isl_union_map_from_multi_union_pw_aff(mupa
);
898 outer
= isl_union_map_flat_range_product(outer
, umap
);
899 umap
= subtree_schedule_extend_child(tree
, outer
);
901 case isl_schedule_node_domain
:
902 domain
= isl_schedule_tree_domain_get_domain(tree
);
903 umap
= isl_union_map_from_domain(domain
);
904 outer
= isl_union_map_flat_range_product(outer
, umap
);
905 umap
= subtree_schedule_extend_child(tree
, outer
);
907 case isl_schedule_node_filter
:
908 domain
= isl_schedule_tree_filter_get_filter(tree
);
909 umap
= isl_union_map_from_domain(domain
);
910 outer
= isl_union_map_flat_range_product(outer
, umap
);
911 umap
= subtree_schedule_extend_child(tree
, outer
);
913 case isl_schedule_node_leaf
:
914 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
915 "leaf node should be handled by caller", return NULL
);
916 case isl_schedule_node_set
:
917 case isl_schedule_node_sequence
:
918 umap
= subtree_schedule_extend_from_children(tree
, outer
);
925 static __isl_give isl_union_set
*initial_domain(
926 __isl_keep isl_schedule_tree
*tree
);
928 /* Extract a universe domain from the children of the tree root "tree",
929 * which is a set or sequence, meaning that its children are filters.
930 * In particular, return the union of the universes of the filters.
932 static __isl_give isl_union_set
*initial_domain_from_children(
933 __isl_keep isl_schedule_tree
*tree
)
937 isl_union_set
*domain
;
940 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
941 "missing children", return NULL
);
942 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
944 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
945 "missing children", return NULL
);
947 space
= extract_space_from_filter_child(tree
);
948 domain
= isl_union_set_empty(space
);
950 for (i
= 0; i
< n
; ++i
) {
951 isl_schedule_tree
*child
;
952 isl_union_set
*domain_i
;
954 child
= isl_schedule_tree_get_child(tree
, i
);
955 domain_i
= initial_domain(child
);
956 domain
= isl_union_set_union(domain
, domain_i
);
957 isl_schedule_tree_free(child
);
963 /* Extract a universe domain from the tree root "tree".
964 * The caller is responsible for making sure that this node
965 * would not be skipped by isl_schedule_tree_first_schedule_descendant
966 * and that it is not a leaf node.
968 static __isl_give isl_union_set
*initial_domain(
969 __isl_keep isl_schedule_tree
*tree
)
971 isl_multi_union_pw_aff
*mupa
;
972 isl_union_set
*domain
;
977 switch (tree
->type
) {
978 case isl_schedule_node_error
:
980 case isl_schedule_node_band
:
981 if (isl_schedule_tree_band_n_member(tree
) == 0)
982 isl_die(isl_schedule_tree_get_ctx(tree
),
984 "0D band should be handled by caller",
986 mupa
= isl_schedule_band_get_partial_schedule(tree
->band
);
987 domain
= isl_multi_union_pw_aff_domain(mupa
);
988 domain
= isl_union_set_universe(domain
);
990 case isl_schedule_node_domain
:
991 domain
= isl_schedule_tree_domain_get_domain(tree
);
992 domain
= isl_union_set_universe(domain
);
994 case isl_schedule_node_filter
:
995 domain
= isl_schedule_tree_filter_get_filter(tree
);
996 domain
= isl_union_set_universe(domain
);
998 case isl_schedule_node_leaf
:
999 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1000 "leaf node should be handled by caller", return NULL
);
1001 case isl_schedule_node_set
:
1002 case isl_schedule_node_sequence
:
1003 domain
= initial_domain_from_children(tree
);
1010 /* Return the subtree schedule of a node that contains some schedule
1011 * information, i.e., a node that would not be skipped by
1012 * isl_schedule_tree_first_schedule_descendant and that is not a leaf.
1014 * We start with an initial zero-dimensional subtree schedule based
1015 * on the domain information in the root node and then extend it
1016 * based on the schedule information in the root node and its descendants.
1018 __isl_give isl_union_map
*isl_schedule_tree_get_subtree_schedule_union_map(
1019 __isl_keep isl_schedule_tree
*tree
)
1021 isl_union_set
*domain
;
1022 isl_union_map
*umap
;
1024 domain
= initial_domain(tree
);
1025 umap
= isl_union_map_from_domain(domain
);
1026 return subtree_schedule_extend(tree
, umap
);
1029 /* Are any members in "band" marked coincident?
1031 static int any_coincident(__isl_keep isl_schedule_band
*band
)
1035 n
= isl_schedule_band_n_member(band
);
1036 for (i
= 0; i
< n
; ++i
)
1037 if (isl_schedule_band_member_get_coincident(band
, i
))
1043 /* Print the band node "band" to "p".
1045 * The permutable and coincident properties are only printed if they
1046 * are different from the defaults.
1047 * The coincident property is always printed in YAML flow style.
1049 static __isl_give isl_printer
*print_tree_band(__isl_take isl_printer
*p
,
1050 __isl_keep isl_schedule_band
*band
)
1052 p
= isl_printer_print_str(p
, "schedule");
1053 p
= isl_printer_yaml_next(p
);
1054 p
= isl_printer_print_str(p
, "\"");
1055 p
= isl_printer_print_multi_union_pw_aff(p
, band
->mupa
);
1056 p
= isl_printer_print_str(p
, "\"");
1057 if (isl_schedule_band_get_permutable(band
)) {
1058 p
= isl_printer_yaml_next(p
);
1059 p
= isl_printer_print_str(p
, "permutable");
1060 p
= isl_printer_yaml_next(p
);
1061 p
= isl_printer_print_int(p
, 1);
1063 if (any_coincident(band
)) {
1067 p
= isl_printer_yaml_next(p
);
1068 p
= isl_printer_print_str(p
, "coincident");
1069 p
= isl_printer_yaml_next(p
);
1070 style
= isl_printer_get_yaml_style(p
);
1071 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1072 p
= isl_printer_yaml_start_sequence(p
);
1073 n
= isl_schedule_band_n_member(band
);
1074 for (i
= 0; i
< n
; ++i
) {
1075 p
= isl_printer_print_int(p
,
1076 isl_schedule_band_member_get_coincident(band
, i
));
1077 p
= isl_printer_yaml_next(p
);
1079 p
= isl_printer_yaml_end_sequence(p
);
1080 p
= isl_printer_set_yaml_style(p
, style
);
1086 /* Print "tree" to "p".
1088 * If "n_ancestor" is non-negative, then "child_pos" contains the child
1089 * positions of a descendant of the current node that should be marked
1090 * (by the comment "YOU ARE HERE"). In particular, if "n_ancestor"
1091 * is zero, then the current node should be marked.
1092 * The marking is only printed in YAML block format.
1094 * Implicit leaf nodes are not printed, except if they correspond
1095 * to the node that should be marked.
1097 __isl_give isl_printer
*isl_printer_print_schedule_tree_mark(
1098 __isl_take isl_printer
*p
, __isl_keep isl_schedule_tree
*tree
,
1099 int n_ancestor
, int *child_pos
)
1105 block
= isl_printer_get_yaml_style(p
) == ISL_YAML_STYLE_BLOCK
;
1107 p
= isl_printer_yaml_start_mapping(p
);
1108 if (n_ancestor
== 0 && block
) {
1109 p
= isl_printer_print_str(p
, "# YOU ARE HERE");
1110 p
= isl_printer_end_line(p
);
1111 p
= isl_printer_start_line(p
);
1113 switch (tree
->type
) {
1114 case isl_schedule_node_error
:
1115 p
= isl_printer_print_str(p
, "ERROR");
1117 case isl_schedule_node_leaf
:
1118 p
= isl_printer_print_str(p
, "leaf");
1120 case isl_schedule_node_sequence
:
1121 p
= isl_printer_print_str(p
, "sequence");
1124 case isl_schedule_node_set
:
1125 p
= isl_printer_print_str(p
, "set");
1128 case isl_schedule_node_domain
:
1129 p
= isl_printer_print_str(p
, "domain");
1130 p
= isl_printer_yaml_next(p
);
1131 p
= isl_printer_print_str(p
, "\"");
1132 p
= isl_printer_print_union_set(p
, tree
->domain
);
1133 p
= isl_printer_print_str(p
, "\"");
1135 case isl_schedule_node_filter
:
1136 p
= isl_printer_print_str(p
, "filter");
1137 p
= isl_printer_yaml_next(p
);
1138 p
= isl_printer_print_str(p
, "\"");
1139 p
= isl_printer_print_union_set(p
, tree
->filter
);
1140 p
= isl_printer_print_str(p
, "\"");
1142 case isl_schedule_node_band
:
1143 p
= print_tree_band(p
, tree
->band
);
1146 p
= isl_printer_yaml_next(p
);
1148 if (!tree
->children
) {
1149 if (n_ancestor
> 0 && block
) {
1150 isl_schedule_tree
*leaf
;
1152 p
= isl_printer_print_str(p
, "child");
1153 p
= isl_printer_yaml_next(p
);
1154 leaf
= isl_schedule_tree_leaf(isl_printer_get_ctx(p
));
1155 p
= isl_printer_print_schedule_tree_mark(p
,
1157 isl_schedule_tree_free(leaf
);
1158 p
= isl_printer_yaml_next(p
);
1160 return isl_printer_yaml_end_mapping(p
);
1164 p
= isl_printer_yaml_start_sequence(p
);
1166 p
= isl_printer_print_str(p
, "child");
1167 p
= isl_printer_yaml_next(p
);
1170 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
1171 for (i
= 0; i
< n
; ++i
) {
1172 isl_schedule_tree
*t
;
1174 t
= isl_schedule_tree_get_child(tree
, i
);
1175 if (n_ancestor
> 0 && child_pos
[0] == i
)
1176 p
= isl_printer_print_schedule_tree_mark(p
, t
,
1177 n_ancestor
- 1, child_pos
+ 1);
1179 p
= isl_printer_print_schedule_tree_mark(p
, t
,
1181 isl_schedule_tree_free(t
);
1183 p
= isl_printer_yaml_next(p
);
1187 p
= isl_printer_yaml_end_sequence(p
);
1188 p
= isl_printer_yaml_end_mapping(p
);
1193 /* Print "tree" to "p".
1195 __isl_give isl_printer
*isl_printer_print_schedule_tree(
1196 __isl_take isl_printer
*p
, __isl_keep isl_schedule_tree
*tree
)
1198 return isl_printer_print_schedule_tree_mark(p
, tree
, -1, NULL
);
1201 void isl_schedule_tree_dump(__isl_keep isl_schedule_tree
*tree
)
1204 isl_printer
*printer
;
1209 ctx
= isl_schedule_tree_get_ctx(tree
);
1210 printer
= isl_printer_to_file(ctx
, stderr
);
1211 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
1212 printer
= isl_printer_print_schedule_tree(printer
, tree
);
1214 isl_printer_free(printer
);