2 * Copyright 2013-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
14 #include <isl_schedule_band.h>
15 #include <isl_schedule_private.h>
18 #define EL isl_schedule_tree
20 #include <isl_list_templ.h>
23 #define BASE schedule_tree
25 #include <isl_list_templ.c>
27 /* Is "tree" the leaf of a schedule tree?
29 int isl_schedule_tree_is_leaf(__isl_keep isl_schedule_tree
*tree
)
31 return isl_schedule_tree_get_type(tree
) == isl_schedule_node_leaf
;
34 /* Create a new schedule tree of type "type".
35 * The caller is responsible for filling in the type specific fields and
38 * By default, the single node tree does not have any anchored nodes.
39 * The caller is responsible for updating the anchored field if needed.
41 static __isl_give isl_schedule_tree
*isl_schedule_tree_alloc(isl_ctx
*ctx
,
42 enum isl_schedule_node_type type
)
44 isl_schedule_tree
*tree
;
46 if (type
== isl_schedule_node_error
)
49 tree
= isl_calloc_type(ctx
, isl_schedule_tree
);
62 /* Return a fresh copy of "tree".
64 __isl_take isl_schedule_tree
*isl_schedule_tree_dup(
65 __isl_keep isl_schedule_tree
*tree
)
68 isl_schedule_tree
*dup
;
73 ctx
= isl_schedule_tree_get_ctx(tree
);
74 dup
= isl_schedule_tree_alloc(ctx
, tree
->type
);
79 case isl_schedule_node_error
:
80 isl_die(ctx
, isl_error_internal
,
81 "allocation should have failed",
82 isl_schedule_tree_free(dup
));
83 case isl_schedule_node_band
:
84 dup
->band
= isl_schedule_band_copy(tree
->band
);
86 return isl_schedule_tree_free(dup
);
88 case isl_schedule_node_context
:
89 dup
->context
= isl_set_copy(tree
->context
);
91 return isl_schedule_tree_free(dup
);
93 case isl_schedule_node_domain
:
94 dup
->domain
= isl_union_set_copy(tree
->domain
);
96 return isl_schedule_tree_free(dup
);
98 case isl_schedule_node_expansion
:
100 isl_union_pw_multi_aff_copy(tree
->contraction
);
101 dup
->expansion
= isl_union_map_copy(tree
->expansion
);
102 if (!dup
->contraction
|| !dup
->expansion
)
103 return isl_schedule_tree_free(dup
);
105 case isl_schedule_node_extension
:
106 dup
->extension
= isl_union_map_copy(tree
->extension
);
108 return isl_schedule_tree_free(dup
);
110 case isl_schedule_node_filter
:
111 dup
->filter
= isl_union_set_copy(tree
->filter
);
113 return isl_schedule_tree_free(dup
);
115 case isl_schedule_node_guard
:
116 dup
->guard
= isl_set_copy(tree
->guard
);
118 return isl_schedule_tree_free(dup
);
120 case isl_schedule_node_mark
:
121 dup
->mark
= isl_id_copy(tree
->mark
);
123 return isl_schedule_tree_free(dup
);
125 case isl_schedule_node_leaf
:
126 case isl_schedule_node_sequence
:
127 case isl_schedule_node_set
:
131 if (tree
->children
) {
132 dup
->children
= isl_schedule_tree_list_copy(tree
->children
);
134 return isl_schedule_tree_free(dup
);
136 dup
->anchored
= tree
->anchored
;
141 /* Return an isl_schedule_tree that is equal to "tree" and that has only
142 * a single reference.
144 * This function is called before a tree is modified.
145 * A static tree (with negative reference count) should never be modified,
146 * so it is not allowed to call this function on a static tree.
148 __isl_give isl_schedule_tree
*isl_schedule_tree_cow(
149 __isl_take isl_schedule_tree
*tree
)
155 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
156 "static trees cannot be modified",
157 return isl_schedule_tree_free(tree
));
162 return isl_schedule_tree_dup(tree
);
165 /* Return a new reference to "tree".
167 * A static tree (with negative reference count) does not keep track
168 * of the number of references and should not be modified.
170 __isl_give isl_schedule_tree
*isl_schedule_tree_copy(
171 __isl_keep isl_schedule_tree
*tree
)
183 /* Free "tree" and return NULL.
185 __isl_null isl_schedule_tree
*isl_schedule_tree_free(
186 __isl_take isl_schedule_tree
*tree
)
195 switch (tree
->type
) {
196 case isl_schedule_node_band
:
197 isl_schedule_band_free(tree
->band
);
199 case isl_schedule_node_context
:
200 isl_set_free(tree
->context
);
202 case isl_schedule_node_domain
:
203 isl_union_set_free(tree
->domain
);
205 case isl_schedule_node_expansion
:
206 isl_union_pw_multi_aff_free(tree
->contraction
);
207 isl_union_map_free(tree
->expansion
);
209 case isl_schedule_node_extension
:
210 isl_union_map_free(tree
->extension
);
212 case isl_schedule_node_filter
:
213 isl_union_set_free(tree
->filter
);
215 case isl_schedule_node_guard
:
216 isl_set_free(tree
->guard
);
218 case isl_schedule_node_mark
:
219 isl_id_free(tree
->mark
);
221 case isl_schedule_node_sequence
:
222 case isl_schedule_node_set
:
223 case isl_schedule_node_error
:
224 case isl_schedule_node_leaf
:
227 isl_schedule_tree_list_free(tree
->children
);
228 isl_ctx_deref(tree
->ctx
);
234 /* Create and return a new leaf schedule tree.
236 __isl_give isl_schedule_tree
*isl_schedule_tree_leaf(isl_ctx
*ctx
)
238 return isl_schedule_tree_alloc(ctx
, isl_schedule_node_leaf
);
241 /* Create a new band schedule tree referring to "band"
244 __isl_give isl_schedule_tree
*isl_schedule_tree_from_band(
245 __isl_take isl_schedule_band
*band
)
248 isl_schedule_tree
*tree
;
253 ctx
= isl_schedule_band_get_ctx(band
);
254 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_band
);
259 tree
->anchored
= isl_schedule_band_is_anchored(band
);
263 isl_schedule_band_free(band
);
267 /* Create a new context schedule tree with the given context and no children.
268 * Since the context references the outer schedule dimension,
269 * the tree is anchored.
271 __isl_give isl_schedule_tree
*isl_schedule_tree_from_context(
272 __isl_take isl_set
*context
)
275 isl_schedule_tree
*tree
;
280 ctx
= isl_set_get_ctx(context
);
281 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_context
);
285 tree
->context
= context
;
290 isl_set_free(context
);
294 /* Create a new domain schedule tree with the given domain and no children.
296 __isl_give isl_schedule_tree
*isl_schedule_tree_from_domain(
297 __isl_take isl_union_set
*domain
)
300 isl_schedule_tree
*tree
;
305 ctx
= isl_union_set_get_ctx(domain
);
306 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_domain
);
310 tree
->domain
= domain
;
314 isl_union_set_free(domain
);
318 /* Create a new expansion schedule tree with the given contraction and
319 * expansion and no children.
321 __isl_give isl_schedule_tree
*isl_schedule_tree_from_expansion(
322 __isl_take isl_union_pw_multi_aff
*contraction
,
323 __isl_take isl_union_map
*expansion
)
326 isl_schedule_tree
*tree
;
328 if (!contraction
|| !expansion
)
331 ctx
= isl_union_map_get_ctx(expansion
);
332 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_expansion
);
336 tree
->contraction
= contraction
;
337 tree
->expansion
= expansion
;
341 isl_union_pw_multi_aff_free(contraction
);
342 isl_union_map_free(expansion
);
346 /* Create a new extension schedule tree with the given extension and
348 * Since the domain of the extension refers to the outer schedule dimension,
349 * the tree is anchored.
351 __isl_give isl_schedule_tree
*isl_schedule_tree_from_extension(
352 __isl_take isl_union_map
*extension
)
355 isl_schedule_tree
*tree
;
360 ctx
= isl_union_map_get_ctx(extension
);
361 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_extension
);
365 tree
->extension
= extension
;
370 isl_union_map_free(extension
);
374 /* Create a new filter schedule tree with the given filter and no children.
376 __isl_give isl_schedule_tree
*isl_schedule_tree_from_filter(
377 __isl_take isl_union_set
*filter
)
380 isl_schedule_tree
*tree
;
385 ctx
= isl_union_set_get_ctx(filter
);
386 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_filter
);
390 tree
->filter
= filter
;
394 isl_union_set_free(filter
);
398 /* Create a new guard schedule tree with the given guard and no children.
399 * Since the guard references the outer schedule dimension,
400 * the tree is anchored.
402 __isl_give isl_schedule_tree
*isl_schedule_tree_from_guard(
403 __isl_take isl_set
*guard
)
406 isl_schedule_tree
*tree
;
411 ctx
= isl_set_get_ctx(guard
);
412 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_guard
);
425 /* Create a new mark schedule tree with the given mark identifier and
428 __isl_give isl_schedule_tree
*isl_schedule_tree_from_mark(
429 __isl_take isl_id
*mark
)
432 isl_schedule_tree
*tree
;
437 ctx
= isl_id_get_ctx(mark
);
438 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_mark
);
450 /* Does "tree" have any node that depends on its position
451 * in the complete schedule tree?
453 int isl_schedule_tree_is_subtree_anchored(__isl_keep isl_schedule_tree
*tree
)
455 return tree
? tree
->anchored
: -1;
458 /* Does the root node of "tree" depend on its position in the complete
460 * Band nodes may be anchored depending on the associated AST build options.
461 * Context, extension and guard nodes are always anchored.
463 int isl_schedule_tree_is_anchored(__isl_keep isl_schedule_tree
*tree
)
468 switch (isl_schedule_tree_get_type(tree
)) {
469 case isl_schedule_node_error
:
471 case isl_schedule_node_band
:
472 return isl_schedule_band_is_anchored(tree
->band
);
473 case isl_schedule_node_context
:
474 case isl_schedule_node_extension
:
475 case isl_schedule_node_guard
:
477 case isl_schedule_node_domain
:
478 case isl_schedule_node_expansion
:
479 case isl_schedule_node_filter
:
480 case isl_schedule_node_leaf
:
481 case isl_schedule_node_mark
:
482 case isl_schedule_node_sequence
:
483 case isl_schedule_node_set
:
488 /* Update the anchored field of "tree" based on whether the root node
489 * itself in anchored and the anchored fields of the children.
491 * This function should be called whenever the children of a tree node
492 * are changed or the anchoredness of the tree root itself changes.
494 __isl_give isl_schedule_tree
*isl_schedule_tree_update_anchored(
495 __isl_take isl_schedule_tree
*tree
)
503 anchored
= isl_schedule_tree_is_anchored(tree
);
505 return isl_schedule_tree_free(tree
);
507 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
508 for (i
= 0; !anchored
&& i
< n
; ++i
) {
509 isl_schedule_tree
*child
;
511 child
= isl_schedule_tree_get_child(tree
, i
);
513 return isl_schedule_tree_free(tree
);
514 anchored
= child
->anchored
;
515 isl_schedule_tree_free(child
);
518 if (anchored
== tree
->anchored
)
520 tree
= isl_schedule_tree_cow(tree
);
523 tree
->anchored
= anchored
;
527 /* Create a new tree of the given type (isl_schedule_node_sequence or
528 * isl_schedule_node_set) with the given children.
530 __isl_give isl_schedule_tree
*isl_schedule_tree_from_children(
531 enum isl_schedule_node_type type
,
532 __isl_take isl_schedule_tree_list
*list
)
535 isl_schedule_tree
*tree
;
540 ctx
= isl_schedule_tree_list_get_ctx(list
);
541 tree
= isl_schedule_tree_alloc(ctx
, type
);
545 tree
->children
= list
;
546 tree
= isl_schedule_tree_update_anchored(tree
);
550 isl_schedule_tree_list_free(list
);
554 /* Construct a tree with a root node of type "type" and as children
555 * "tree1" and "tree2".
556 * If the root of one (or both) of the input trees is itself of type "type",
557 * then the tree is replaced by its children.
559 __isl_give isl_schedule_tree
*isl_schedule_tree_from_pair(
560 enum isl_schedule_node_type type
, __isl_take isl_schedule_tree
*tree1
,
561 __isl_take isl_schedule_tree
*tree2
)
564 isl_schedule_tree_list
*list
;
566 if (!tree1
|| !tree2
)
569 ctx
= isl_schedule_tree_get_ctx(tree1
);
570 if (isl_schedule_tree_get_type(tree1
) == type
) {
571 list
= isl_schedule_tree_list_copy(tree1
->children
);
572 isl_schedule_tree_free(tree1
);
574 list
= isl_schedule_tree_list_alloc(ctx
, 2);
575 list
= isl_schedule_tree_list_add(list
, tree1
);
577 if (isl_schedule_tree_get_type(tree2
) == type
) {
578 isl_schedule_tree_list
*children
;
580 children
= isl_schedule_tree_list_copy(tree2
->children
);
581 list
= isl_schedule_tree_list_concat(list
, children
);
582 isl_schedule_tree_free(tree2
);
584 list
= isl_schedule_tree_list_add(list
, tree2
);
587 return isl_schedule_tree_from_children(type
, list
);
589 isl_schedule_tree_free(tree1
);
590 isl_schedule_tree_free(tree2
);
594 /* Construct a tree with a sequence root node and as children
595 * "tree1" and "tree2".
596 * If the root of one (or both) of the input trees is itself a sequence,
597 * then the tree is replaced by its children.
599 __isl_give isl_schedule_tree
*isl_schedule_tree_sequence_pair(
600 __isl_take isl_schedule_tree
*tree1
,
601 __isl_take isl_schedule_tree
*tree2
)
603 return isl_schedule_tree_from_pair(isl_schedule_node_sequence
,
607 /* Return the isl_ctx to which "tree" belongs.
609 isl_ctx
*isl_schedule_tree_get_ctx(__isl_keep isl_schedule_tree
*tree
)
611 return tree
? tree
->ctx
: NULL
;
614 /* Return the type of the root of the tree or isl_schedule_node_error
617 enum isl_schedule_node_type
isl_schedule_tree_get_type(
618 __isl_keep isl_schedule_tree
*tree
)
620 return tree
? tree
->type
: isl_schedule_node_error
;
623 /* Are "tree1" and "tree2" obviously equal to each other?
625 int isl_schedule_tree_plain_is_equal(__isl_keep isl_schedule_tree
*tree1
,
626 __isl_keep isl_schedule_tree
*tree2
)
631 if (!tree1
|| !tree2
)
635 if (tree1
->type
!= tree2
->type
)
638 switch (tree1
->type
) {
639 case isl_schedule_node_band
:
640 equal
= isl_schedule_band_plain_is_equal(tree1
->band
,
643 case isl_schedule_node_context
:
644 equal
= isl_set_is_equal(tree1
->context
, tree2
->context
);
646 case isl_schedule_node_domain
:
647 equal
= isl_union_set_is_equal(tree1
->domain
, tree2
->domain
);
649 case isl_schedule_node_expansion
:
650 equal
= isl_union_map_is_equal(tree1
->expansion
,
652 if (equal
>= 0 && equal
)
653 equal
= isl_union_pw_multi_aff_plain_is_equal(
654 tree1
->contraction
, tree2
->contraction
);
656 case isl_schedule_node_extension
:
657 equal
= isl_union_map_is_equal(tree1
->extension
,
660 case isl_schedule_node_filter
:
661 equal
= isl_union_set_is_equal(tree1
->filter
, tree2
->filter
);
663 case isl_schedule_node_guard
:
664 equal
= isl_set_is_equal(tree1
->guard
, tree2
->guard
);
666 case isl_schedule_node_mark
:
667 equal
= tree1
->mark
== tree2
->mark
;
669 case isl_schedule_node_leaf
:
670 case isl_schedule_node_sequence
:
671 case isl_schedule_node_set
:
674 case isl_schedule_node_error
:
679 if (equal
< 0 || !equal
)
682 n
= isl_schedule_tree_n_children(tree1
);
683 if (n
!= isl_schedule_tree_n_children(tree2
))
685 for (i
= 0; i
< n
; ++i
) {
686 isl_schedule_tree
*child1
, *child2
;
688 child1
= isl_schedule_tree_get_child(tree1
, i
);
689 child2
= isl_schedule_tree_get_child(tree2
, i
);
690 equal
= isl_schedule_tree_plain_is_equal(child1
, child2
);
691 isl_schedule_tree_free(child1
);
692 isl_schedule_tree_free(child2
);
694 if (equal
< 0 || !equal
)
701 /* Does "tree" have any children, other than an implicit leaf.
703 int isl_schedule_tree_has_children(__isl_keep isl_schedule_tree
*tree
)
708 return tree
->children
!= NULL
;
711 /* Return the number of children of "tree", excluding implicit leaves.
713 int isl_schedule_tree_n_children(__isl_keep isl_schedule_tree
*tree
)
718 return isl_schedule_tree_list_n_schedule_tree(tree
->children
);
721 /* Return a copy of the (explicit) child at position "pos" of "tree".
723 __isl_give isl_schedule_tree
*isl_schedule_tree_get_child(
724 __isl_keep isl_schedule_tree
*tree
, int pos
)
729 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
730 "schedule tree has no explicit children", return NULL
);
731 return isl_schedule_tree_list_get_schedule_tree(tree
->children
, pos
);
734 /* Return a copy of the (explicit) child at position "pos" of "tree" and
737 __isl_give isl_schedule_tree
*isl_schedule_tree_child(
738 __isl_take isl_schedule_tree
*tree
, int pos
)
740 isl_schedule_tree
*child
;
742 child
= isl_schedule_tree_get_child(tree
, pos
);
743 isl_schedule_tree_free(tree
);
747 /* Remove all (explicit) children from "tree".
749 __isl_give isl_schedule_tree
*isl_schedule_tree_reset_children(
750 __isl_take isl_schedule_tree
*tree
)
752 tree
= isl_schedule_tree_cow(tree
);
755 tree
->children
= isl_schedule_tree_list_free(tree
->children
);
759 /* Remove the child at position "pos" from the children of "tree".
760 * If there was only one child to begin with, then remove all children.
762 __isl_give isl_schedule_tree
*isl_schedule_tree_drop_child(
763 __isl_take isl_schedule_tree
*tree
, int pos
)
767 tree
= isl_schedule_tree_cow(tree
);
771 if (!isl_schedule_tree_has_children(tree
))
772 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
773 "tree does not have any explicit children",
774 return isl_schedule_tree_free(tree
));
775 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
776 if (pos
< 0 || pos
>= n
)
777 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
778 "position out of bounds",
779 return isl_schedule_tree_free(tree
));
781 return isl_schedule_tree_reset_children(tree
);
783 tree
->children
= isl_schedule_tree_list_drop(tree
->children
, pos
, 1);
785 return isl_schedule_tree_free(tree
);
790 /* Replace the child at position "pos" of "tree" by "child".
792 * If the new child is a leaf, then it is not explicitly
793 * recorded in the list of children. Instead, the list of children
794 * (which is assumed to have only one element) is removed.
795 * Note that the children of set and sequence nodes are always
796 * filters, so they cannot be replaced by empty trees.
798 __isl_give isl_schedule_tree
*isl_schedule_tree_replace_child(
799 __isl_take isl_schedule_tree
*tree
, int pos
,
800 __isl_take isl_schedule_tree
*child
)
802 tree
= isl_schedule_tree_cow(tree
);
806 if (isl_schedule_tree_is_leaf(child
)) {
807 isl_schedule_tree_free(child
);
808 if (!tree
->children
&& pos
== 0)
810 if (isl_schedule_tree_n_children(tree
) != 1)
811 isl_die(isl_schedule_tree_get_ctx(tree
),
813 "can only replace single child by leaf",
815 return isl_schedule_tree_reset_children(tree
);
818 if (!tree
->children
&& pos
== 0)
820 isl_schedule_tree_list_from_schedule_tree(child
);
822 tree
->children
= isl_schedule_tree_list_set_schedule_tree(
823 tree
->children
, pos
, child
);
826 return isl_schedule_tree_free(tree
);
827 tree
= isl_schedule_tree_update_anchored(tree
);
831 isl_schedule_tree_free(tree
);
832 isl_schedule_tree_free(child
);
836 /* Replace the (explicit) children of "tree" by "children"?
838 __isl_give isl_schedule_tree
*isl_schedule_tree_set_children(
839 __isl_take isl_schedule_tree
*tree
,
840 __isl_take isl_schedule_tree_list
*children
)
842 tree
= isl_schedule_tree_cow(tree
);
843 if (!tree
|| !children
)
845 isl_schedule_tree_list_free(tree
->children
);
846 tree
->children
= children
;
849 isl_schedule_tree_free(tree
);
850 isl_schedule_tree_list_free(children
);
854 /* Create a new band schedule tree referring to "band"
855 * with "tree" as single child.
857 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_band(
858 __isl_take isl_schedule_tree
*tree
, __isl_take isl_schedule_band
*band
)
860 isl_schedule_tree
*res
;
862 res
= isl_schedule_tree_from_band(band
);
863 return isl_schedule_tree_replace_child(res
, 0, tree
);
866 /* Create a new context schedule tree with the given context and
867 * with "tree" as single child.
869 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_context(
870 __isl_take isl_schedule_tree
*tree
, __isl_take isl_set
*context
)
872 isl_schedule_tree
*res
;
874 res
= isl_schedule_tree_from_context(context
);
875 return isl_schedule_tree_replace_child(res
, 0, tree
);
878 /* Create a new domain schedule tree with the given domain and
879 * with "tree" as single child.
881 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_domain(
882 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*domain
)
884 isl_schedule_tree
*res
;
886 res
= isl_schedule_tree_from_domain(domain
);
887 return isl_schedule_tree_replace_child(res
, 0, tree
);
890 /* Create a new expansion schedule tree with the given contraction and
891 * expansion and with "tree" as single child.
893 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_expansion(
894 __isl_take isl_schedule_tree
*tree
,
895 __isl_take isl_union_pw_multi_aff
*contraction
,
896 __isl_take isl_union_map
*expansion
)
898 isl_schedule_tree
*res
;
900 res
= isl_schedule_tree_from_expansion(contraction
, expansion
);
901 return isl_schedule_tree_replace_child(res
, 0, tree
);
904 /* Create a new extension schedule tree with the given extension and
905 * with "tree" as single child.
907 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_extension(
908 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_map
*extension
)
910 isl_schedule_tree
*res
;
912 res
= isl_schedule_tree_from_extension(extension
);
913 return isl_schedule_tree_replace_child(res
, 0, tree
);
916 /* Create a new filter schedule tree with the given filter and single child.
918 * If the root of "tree" is itself a filter node, then the two
919 * filter nodes are merged into one node.
921 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_filter(
922 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
)
924 isl_schedule_tree
*res
;
926 if (isl_schedule_tree_get_type(tree
) == isl_schedule_node_filter
) {
927 isl_union_set
*tree_filter
;
929 tree_filter
= isl_schedule_tree_filter_get_filter(tree
);
930 tree_filter
= isl_union_set_intersect(tree_filter
, filter
);
931 tree
= isl_schedule_tree_filter_set_filter(tree
, tree_filter
);
935 res
= isl_schedule_tree_from_filter(filter
);
936 return isl_schedule_tree_replace_child(res
, 0, tree
);
939 /* Insert a filter node with filter set "filter"
940 * in each of the children of "tree".
942 __isl_give isl_schedule_tree
*isl_schedule_tree_children_insert_filter(
943 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
)
947 if (!tree
|| !filter
)
950 n
= isl_schedule_tree_n_children(tree
);
951 for (i
= 0; i
< n
; ++i
) {
952 isl_schedule_tree
*child
;
954 child
= isl_schedule_tree_get_child(tree
, i
);
955 child
= isl_schedule_tree_insert_filter(child
,
956 isl_union_set_copy(filter
));
957 tree
= isl_schedule_tree_replace_child(tree
, i
, child
);
960 isl_union_set_free(filter
);
963 isl_union_set_free(filter
);
964 isl_schedule_tree_free(tree
);
968 /* Create a new guard schedule tree with the given guard and
969 * with "tree" as single child.
971 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_guard(
972 __isl_take isl_schedule_tree
*tree
, __isl_take isl_set
*guard
)
974 isl_schedule_tree
*res
;
976 res
= isl_schedule_tree_from_guard(guard
);
977 return isl_schedule_tree_replace_child(res
, 0, tree
);
980 /* Create a new mark schedule tree with the given mark identifier and
983 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_mark(
984 __isl_take isl_schedule_tree
*tree
, __isl_take isl_id
*mark
)
986 isl_schedule_tree
*res
;
988 res
= isl_schedule_tree_from_mark(mark
);
989 return isl_schedule_tree_replace_child(res
, 0, tree
);
992 /* Return the number of members in the band tree root.
994 unsigned isl_schedule_tree_band_n_member(__isl_keep isl_schedule_tree
*tree
)
999 if (tree
->type
!= isl_schedule_node_band
)
1000 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1001 "not a band node", return 0);
1003 return isl_schedule_band_n_member(tree
->band
);
1006 /* Is the band member at position "pos" of the band tree root
1007 * marked coincident?
1009 int isl_schedule_tree_band_member_get_coincident(
1010 __isl_keep isl_schedule_tree
*tree
, int pos
)
1015 if (tree
->type
!= isl_schedule_node_band
)
1016 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1017 "not a band node", return -1);
1019 return isl_schedule_band_member_get_coincident(tree
->band
, pos
);
1022 /* Mark the given band member as being coincident or not
1023 * according to "coincident".
1025 __isl_give isl_schedule_tree
*isl_schedule_tree_band_member_set_coincident(
1026 __isl_take isl_schedule_tree
*tree
, int pos
, int coincident
)
1030 if (tree
->type
!= isl_schedule_node_band
)
1031 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1032 "not a band node", return isl_schedule_tree_free(tree
));
1033 if (isl_schedule_tree_band_member_get_coincident(tree
, pos
) ==
1036 tree
= isl_schedule_tree_cow(tree
);
1040 tree
->band
= isl_schedule_band_member_set_coincident(tree
->band
, pos
,
1043 return isl_schedule_tree_free(tree
);
1047 /* Is the band tree root marked permutable?
1049 int isl_schedule_tree_band_get_permutable(__isl_keep isl_schedule_tree
*tree
)
1054 if (tree
->type
!= isl_schedule_node_band
)
1055 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1056 "not a band node", return -1);
1058 return isl_schedule_band_get_permutable(tree
->band
);
1061 /* Mark the band tree root permutable or not according to "permutable"?
1063 __isl_give isl_schedule_tree
*isl_schedule_tree_band_set_permutable(
1064 __isl_take isl_schedule_tree
*tree
, int permutable
)
1068 if (tree
->type
!= isl_schedule_node_band
)
1069 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1070 "not a band node", return isl_schedule_tree_free(tree
));
1071 if (isl_schedule_tree_band_get_permutable(tree
) == permutable
)
1073 tree
= isl_schedule_tree_cow(tree
);
1077 tree
->band
= isl_schedule_band_set_permutable(tree
->band
, permutable
);
1079 return isl_schedule_tree_free(tree
);
1083 /* Return the schedule space of the band tree root.
1085 __isl_give isl_space
*isl_schedule_tree_band_get_space(
1086 __isl_keep isl_schedule_tree
*tree
)
1091 if (tree
->type
!= isl_schedule_node_band
)
1092 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1093 "not a band node", return NULL
);
1095 return isl_schedule_band_get_space(tree
->band
);
1098 /* Intersect the domain of the band schedule of the band tree root
1101 __isl_give isl_schedule_tree
*isl_schedule_tree_band_intersect_domain(
1102 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*domain
)
1104 if (!tree
|| !domain
)
1107 if (tree
->type
!= isl_schedule_node_band
)
1108 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1109 "not a band node", goto error
);
1111 tree
->band
= isl_schedule_band_intersect_domain(tree
->band
, domain
);
1113 return isl_schedule_tree_free(tree
);
1117 isl_schedule_tree_free(tree
);
1118 isl_union_set_free(domain
);
1122 /* Return the schedule of the band tree root in isolation.
1124 __isl_give isl_multi_union_pw_aff
*isl_schedule_tree_band_get_partial_schedule(
1125 __isl_keep isl_schedule_tree
*tree
)
1130 if (tree
->type
!= isl_schedule_node_band
)
1131 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1132 "not a band node", return NULL
);
1134 return isl_schedule_band_get_partial_schedule(tree
->band
);
1137 /* Replace the schedule of the band tree root by "schedule".
1139 __isl_give isl_schedule_tree
*isl_schedule_tree_band_set_partial_schedule(
1140 __isl_take isl_schedule_tree
*tree
,
1141 __isl_take isl_multi_union_pw_aff
*schedule
)
1143 tree
= isl_schedule_tree_cow(tree
);
1144 if (!tree
|| !schedule
)
1147 if (tree
->type
!= isl_schedule_node_band
)
1148 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1149 "not a band node", return NULL
);
1150 tree
->band
= isl_schedule_band_set_partial_schedule(tree
->band
,
1155 isl_schedule_tree_free(tree
);
1156 isl_multi_union_pw_aff_free(schedule
);
1160 /* Return the loop AST generation type for the band member
1161 * of the band tree root at position "pos".
1163 enum isl_ast_loop_type
isl_schedule_tree_band_member_get_ast_loop_type(
1164 __isl_keep isl_schedule_tree
*tree
, int pos
)
1167 return isl_ast_loop_error
;
1169 if (tree
->type
!= isl_schedule_node_band
)
1170 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1171 "not a band node", return isl_ast_loop_error
);
1173 return isl_schedule_band_member_get_ast_loop_type(tree
->band
, pos
);
1176 /* Set the loop AST generation type for the band member of the band tree root
1177 * at position "pos" to "type".
1179 __isl_give isl_schedule_tree
*isl_schedule_tree_band_member_set_ast_loop_type(
1180 __isl_take isl_schedule_tree
*tree
, int pos
,
1181 enum isl_ast_loop_type type
)
1183 tree
= isl_schedule_tree_cow(tree
);
1187 if (tree
->type
!= isl_schedule_node_band
)
1188 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1189 "not a band node", return isl_schedule_tree_free(tree
));
1191 tree
->band
= isl_schedule_band_member_set_ast_loop_type(tree
->band
,
1194 return isl_schedule_tree_free(tree
);
1199 /* Return the loop AST generation type for the band member
1200 * of the band tree root at position "pos" for the isolated part.
1202 enum isl_ast_loop_type
isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1203 __isl_keep isl_schedule_tree
*tree
, int pos
)
1206 return isl_ast_loop_error
;
1208 if (tree
->type
!= isl_schedule_node_band
)
1209 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1210 "not a band node", return isl_ast_loop_error
);
1212 return isl_schedule_band_member_get_isolate_ast_loop_type(tree
->band
,
1216 /* Set the loop AST generation type for the band member of the band tree root
1217 * at position "pos" for the isolated part to "type".
1219 __isl_give isl_schedule_tree
*
1220 isl_schedule_tree_band_member_set_isolate_ast_loop_type(
1221 __isl_take isl_schedule_tree
*tree
, int pos
,
1222 enum isl_ast_loop_type type
)
1224 tree
= isl_schedule_tree_cow(tree
);
1228 if (tree
->type
!= isl_schedule_node_band
)
1229 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1230 "not a band node", return isl_schedule_tree_free(tree
));
1232 tree
->band
= isl_schedule_band_member_set_isolate_ast_loop_type(
1233 tree
->band
, pos
, type
);
1235 return isl_schedule_tree_free(tree
);
1240 /* Return the AST build options associated to the band tree root.
1242 __isl_give isl_union_set
*isl_schedule_tree_band_get_ast_build_options(
1243 __isl_keep isl_schedule_tree
*tree
)
1248 if (tree
->type
!= isl_schedule_node_band
)
1249 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1250 "not a band node", return NULL
);
1252 return isl_schedule_band_get_ast_build_options(tree
->band
);
1255 /* Replace the AST build options associated to band tree root by "options".
1256 * Updated the anchored field if the anchoredness of the root node itself
1259 __isl_give isl_schedule_tree
*isl_schedule_tree_band_set_ast_build_options(
1260 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*options
)
1264 tree
= isl_schedule_tree_cow(tree
);
1265 if (!tree
|| !options
)
1268 if (tree
->type
!= isl_schedule_node_band
)
1269 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1270 "not a band node", goto error
);
1272 was_anchored
= isl_schedule_tree_is_anchored(tree
);
1273 tree
->band
= isl_schedule_band_set_ast_build_options(tree
->band
,
1276 return isl_schedule_tree_free(tree
);
1277 if (isl_schedule_tree_is_anchored(tree
) != was_anchored
)
1278 tree
= isl_schedule_tree_update_anchored(tree
);
1282 isl_schedule_tree_free(tree
);
1283 isl_union_set_free(options
);
1287 /* Return the context of the context tree root.
1289 __isl_give isl_set
*isl_schedule_tree_context_get_context(
1290 __isl_keep isl_schedule_tree
*tree
)
1295 if (tree
->type
!= isl_schedule_node_context
)
1296 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1297 "not a context node", return NULL
);
1299 return isl_set_copy(tree
->context
);
1302 /* Return the domain of the domain tree root.
1304 __isl_give isl_union_set
*isl_schedule_tree_domain_get_domain(
1305 __isl_keep isl_schedule_tree
*tree
)
1310 if (tree
->type
!= isl_schedule_node_domain
)
1311 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1312 "not a domain node", return NULL
);
1314 return isl_union_set_copy(tree
->domain
);
1317 /* Replace the domain of domain tree root "tree" by "domain".
1319 __isl_give isl_schedule_tree
*isl_schedule_tree_domain_set_domain(
1320 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*domain
)
1322 tree
= isl_schedule_tree_cow(tree
);
1323 if (!tree
|| !domain
)
1326 if (tree
->type
!= isl_schedule_node_domain
)
1327 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1328 "not a domain node", goto error
);
1330 isl_union_set_free(tree
->domain
);
1331 tree
->domain
= domain
;
1335 isl_schedule_tree_free(tree
);
1336 isl_union_set_free(domain
);
1340 /* Return the contraction of the expansion tree root.
1342 __isl_give isl_union_pw_multi_aff
*isl_schedule_tree_expansion_get_contraction(
1343 __isl_keep isl_schedule_tree
*tree
)
1348 if (tree
->type
!= isl_schedule_node_expansion
)
1349 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1350 "not an expansion node", return NULL
);
1352 return isl_union_pw_multi_aff_copy(tree
->contraction
);
1355 /* Return the expansion of the expansion tree root.
1357 __isl_give isl_union_map
*isl_schedule_tree_expansion_get_expansion(
1358 __isl_keep isl_schedule_tree
*tree
)
1363 if (tree
->type
!= isl_schedule_node_expansion
)
1364 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1365 "not an expansion node", return NULL
);
1367 return isl_union_map_copy(tree
->expansion
);
1370 /* Replace the contraction and the expansion of the expansion tree root "tree"
1371 * by "contraction" and "expansion".
1373 __isl_give isl_schedule_tree
*
1374 isl_schedule_tree_expansion_set_contraction_and_expansion(
1375 __isl_take isl_schedule_tree
*tree
,
1376 __isl_take isl_union_pw_multi_aff
*contraction
,
1377 __isl_take isl_union_map
*expansion
)
1379 tree
= isl_schedule_tree_cow(tree
);
1380 if (!tree
|| !contraction
|| !expansion
)
1383 if (tree
->type
!= isl_schedule_node_expansion
)
1384 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1385 "not an expansion node", return NULL
);
1387 isl_union_pw_multi_aff_free(tree
->contraction
);
1388 tree
->contraction
= contraction
;
1389 isl_union_map_free(tree
->expansion
);
1390 tree
->expansion
= expansion
;
1394 isl_schedule_tree_free(tree
);
1395 isl_union_pw_multi_aff_free(contraction
);
1396 isl_union_map_free(expansion
);
1400 /* Return the extension of the extension tree root.
1402 __isl_give isl_union_map
*isl_schedule_tree_extension_get_extension(
1403 __isl_take isl_schedule_tree
*tree
)
1408 if (tree
->type
!= isl_schedule_node_extension
)
1409 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1410 "not an extension node", return NULL
);
1412 return isl_union_map_copy(tree
->extension
);
1415 /* Replace the extension of extension tree root "tree" by "extension".
1417 __isl_give isl_schedule_tree
*isl_schedule_tree_extension_set_extension(
1418 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_map
*extension
)
1420 tree
= isl_schedule_tree_cow(tree
);
1421 if (!tree
|| !extension
)
1424 if (tree
->type
!= isl_schedule_node_extension
)
1425 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1426 "not an extension node", return NULL
);
1427 isl_union_map_free(tree
->extension
);
1428 tree
->extension
= extension
;
1432 isl_schedule_tree_free(tree
);
1433 isl_union_map_free(extension
);
1437 /* Return the filter of the filter tree root.
1439 __isl_give isl_union_set
*isl_schedule_tree_filter_get_filter(
1440 __isl_keep isl_schedule_tree
*tree
)
1445 if (tree
->type
!= isl_schedule_node_filter
)
1446 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1447 "not a filter node", return NULL
);
1449 return isl_union_set_copy(tree
->filter
);
1452 /* Replace the filter of the filter tree root by "filter".
1454 __isl_give isl_schedule_tree
*isl_schedule_tree_filter_set_filter(
1455 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
)
1457 tree
= isl_schedule_tree_cow(tree
);
1458 if (!tree
|| !filter
)
1461 if (tree
->type
!= isl_schedule_node_filter
)
1462 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1463 "not a filter node", return NULL
);
1465 isl_union_set_free(tree
->filter
);
1466 tree
->filter
= filter
;
1470 isl_schedule_tree_free(tree
);
1471 isl_union_set_free(filter
);
1475 /* Return the guard of the guard tree root.
1477 __isl_give isl_set
*isl_schedule_tree_guard_get_guard(
1478 __isl_take isl_schedule_tree
*tree
)
1483 if (tree
->type
!= isl_schedule_node_guard
)
1484 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1485 "not a guard node", return NULL
);
1487 return isl_set_copy(tree
->guard
);
1490 /* Return the mark identifier of the mark tree root "tree".
1492 __isl_give isl_id
*isl_schedule_tree_mark_get_id(
1493 __isl_keep isl_schedule_tree
*tree
)
1498 if (tree
->type
!= isl_schedule_node_mark
)
1499 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1500 "not a mark node", return NULL
);
1502 return isl_id_copy(tree
->mark
);
1505 /* Set dim to the range dimension of "map" and abort the search.
1507 static int set_range_dim(__isl_take isl_map
*map
, void *user
)
1511 *dim
= isl_map_dim(map
, isl_dim_out
);
1517 /* Return the dimension of the range of "umap".
1518 * "umap" is assumed not to be empty and
1519 * all maps inside "umap" are assumed to have the same range.
1521 * We extract the range dimension from the first map in "umap".
1523 static int range_dim(__isl_keep isl_union_map
*umap
)
1529 if (isl_union_map_n_map(umap
) == 0)
1530 isl_die(isl_union_map_get_ctx(umap
), isl_error_internal
,
1531 "unexpected empty input", return -1);
1533 isl_union_map_foreach_map(umap
, &set_range_dim
, &dim
);
1538 /* Append an "extra" number of zeros to the range of "umap" and
1539 * return the result.
1541 static __isl_give isl_union_map
*append_range(__isl_take isl_union_map
*umap
,
1547 isl_union_pw_multi_aff
*suffix
;
1548 isl_union_map
*universe
;
1549 isl_union_map
*suffix_umap
;
1551 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
1552 dom
= isl_union_map_domain(universe
);
1553 space
= isl_union_set_get_space(dom
);
1554 space
= isl_space_set_from_params(space
);
1555 space
= isl_space_add_dims(space
, isl_dim_set
, extra
);
1556 mv
= isl_multi_val_zero(space
);
1558 suffix
= isl_union_pw_multi_aff_multi_val_on_domain(dom
, mv
);
1559 suffix_umap
= isl_union_map_from_union_pw_multi_aff(suffix
);
1560 umap
= isl_union_map_flat_range_product(umap
, suffix_umap
);
1565 /* Should we skip the root of "tree" while looking for the first
1566 * descendant with schedule information?
1567 * That is, is it impossible to derive any information about
1568 * the iteration domain from this node?
1570 * We do not want to skip leaf or error nodes because there is
1571 * no point in looking any deeper from these nodes.
1572 * We can only extract partial iteration domain information
1573 * from an extension node, but extension nodes are not supported
1574 * by the caller and it will error out on them.
1576 static int domain_less(__isl_keep isl_schedule_tree
*tree
)
1578 enum isl_schedule_node_type type
;
1580 type
= isl_schedule_tree_get_type(tree
);
1582 case isl_schedule_node_band
:
1583 return isl_schedule_tree_band_n_member(tree
) == 0;
1584 case isl_schedule_node_context
:
1585 case isl_schedule_node_guard
:
1586 case isl_schedule_node_mark
:
1588 case isl_schedule_node_leaf
:
1589 case isl_schedule_node_error
:
1590 case isl_schedule_node_domain
:
1591 case isl_schedule_node_expansion
:
1592 case isl_schedule_node_extension
:
1593 case isl_schedule_node_filter
:
1594 case isl_schedule_node_set
:
1595 case isl_schedule_node_sequence
:
1600 /* Move down to the first descendant of "tree" that contains any schedule
1601 * information or return "leaf" if there is no such descendant.
1603 __isl_give isl_schedule_tree
*isl_schedule_tree_first_schedule_descendant(
1604 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_tree
*leaf
)
1606 while (domain_less(tree
)) {
1607 if (!isl_schedule_tree_has_children(tree
)) {
1608 isl_schedule_tree_free(tree
);
1609 return isl_schedule_tree_copy(leaf
);
1611 tree
= isl_schedule_tree_child(tree
, 0);
1617 static __isl_give isl_union_map
*subtree_schedule_extend(
1618 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
);
1620 /* Extend the schedule map "outer" with the subtree schedule
1621 * of the (single) child of "tree", if any.
1623 * If "tree" does not have any descendants (apart from those that
1624 * do not carry any schedule information), then we simply return "outer".
1625 * Otherwise, we extend the schedule map "outer" with the subtree schedule
1626 * of the single child.
1628 static __isl_give isl_union_map
*subtree_schedule_extend_child(
1629 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
1631 isl_schedule_tree
*child
;
1635 return isl_union_map_free(outer
);
1636 if (!isl_schedule_tree_has_children(tree
))
1638 child
= isl_schedule_tree_get_child(tree
, 0);
1640 return isl_union_map_free(outer
);
1641 res
= subtree_schedule_extend(child
, outer
);
1642 isl_schedule_tree_free(child
);
1646 /* Extract the parameter space from one of the children of "tree",
1647 * which are assumed to be filters.
1649 static __isl_give isl_space
*extract_space_from_filter_child(
1650 __isl_keep isl_schedule_tree
*tree
)
1654 isl_schedule_tree
*child
;
1656 child
= isl_schedule_tree_list_get_schedule_tree(tree
->children
, 0);
1657 dom
= isl_schedule_tree_filter_get_filter(child
);
1658 space
= isl_union_set_get_space(dom
);
1659 isl_union_set_free(dom
);
1660 isl_schedule_tree_free(child
);
1665 /* Extend the schedule map "outer" with the subtree schedule
1666 * of a set or sequence node.
1668 * The schedule for the set or sequence node itself is composed of
1669 * pieces of the form
1677 * The first form is used if there is only a single child or
1678 * if the current node is a set node and the schedule_separate_components
1679 * option is not set.
1681 * Each of the pieces above is extended with the subtree schedule of
1682 * the child of the corresponding filter, if any, padded with zeros
1683 * to ensure that all pieces have the same range dimension.
1685 static __isl_give isl_union_map
*subtree_schedule_extend_from_children(
1686 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
1695 isl_union_map
*umap
;
1700 ctx
= isl_schedule_tree_get_ctx(tree
);
1701 if (!tree
->children
)
1702 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1703 "missing children", return NULL
);
1704 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
1706 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1707 "missing children", return NULL
);
1709 separate
= n
> 1 && (tree
->type
== isl_schedule_node_sequence
||
1710 isl_options_get_schedule_separate_components(ctx
));
1712 space
= extract_space_from_filter_child(tree
);
1714 umap
= isl_union_map_empty(isl_space_copy(space
));
1715 space
= isl_space_set_from_params(space
);
1717 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1718 v
= isl_val_zero(ctx
);
1720 mv
= isl_multi_val_zero(space
);
1722 dim
= isl_multi_val_dim(mv
, isl_dim_set
);
1723 for (i
= 0; i
< n
; ++i
) {
1724 isl_union_pw_multi_aff
*upma
;
1725 isl_union_map
*umap_i
;
1727 isl_schedule_tree
*child
;
1731 child
= isl_schedule_tree_list_get_schedule_tree(
1733 dom
= isl_schedule_tree_filter_get_filter(child
);
1736 mv
= isl_multi_val_set_val(mv
, 0, isl_val_copy(v
));
1737 v
= isl_val_add_ui(v
, 1);
1739 upma
= isl_union_pw_multi_aff_multi_val_on_domain(dom
,
1740 isl_multi_val_copy(mv
));
1741 umap_i
= isl_union_map_from_union_pw_multi_aff(upma
);
1742 umap_i
= isl_union_map_flat_range_product(
1743 isl_union_map_copy(outer
), umap_i
);
1744 umap_i
= subtree_schedule_extend_child(child
, umap_i
);
1745 isl_schedule_tree_free(child
);
1747 empty
= isl_union_map_is_empty(umap_i
);
1749 umap_i
= isl_union_map_free(umap_i
);
1751 isl_union_map_free(umap_i
);
1755 dim_i
= range_dim(umap_i
);
1757 umap
= isl_union_map_free(umap
);
1758 } else if (dim
< dim_i
) {
1759 umap
= append_range(umap
, dim_i
- dim
);
1761 } else if (dim_i
< dim
) {
1762 umap_i
= append_range(umap_i
, dim
- dim_i
);
1764 umap
= isl_union_map_union(umap
, umap_i
);
1768 isl_multi_val_free(mv
);
1769 isl_union_map_free(outer
);
1774 /* Extend the schedule map "outer" with the subtree schedule of "tree".
1776 * If the root of the tree is a set or a sequence, then we extend
1777 * the schedule map in subtree_schedule_extend_from_children.
1778 * Otherwise, we extend the schedule map with the partial schedule
1779 * corresponding to the root of the tree and then continue with
1780 * the single child of this root.
1781 * In the special case of an expansion, the schedule map is "extended"
1782 * by applying the expansion to the domain of the schedule map.
1784 static __isl_give isl_union_map
*subtree_schedule_extend(
1785 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
1787 isl_multi_union_pw_aff
*mupa
;
1788 isl_union_map
*umap
;
1789 isl_union_set
*domain
;
1794 switch (tree
->type
) {
1795 case isl_schedule_node_error
:
1796 return isl_union_map_free(outer
);
1797 case isl_schedule_node_extension
:
1798 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1799 "cannot construct subtree schedule of tree "
1800 "with extension nodes",
1801 return isl_union_map_free(outer
));
1802 case isl_schedule_node_context
:
1803 case isl_schedule_node_guard
:
1804 case isl_schedule_node_mark
:
1805 return subtree_schedule_extend_child(tree
, outer
);
1806 case isl_schedule_node_band
:
1807 if (isl_schedule_tree_band_n_member(tree
) == 0)
1808 return subtree_schedule_extend_child(tree
, outer
);
1809 mupa
= isl_schedule_band_get_partial_schedule(tree
->band
);
1810 umap
= isl_union_map_from_multi_union_pw_aff(mupa
);
1811 outer
= isl_union_map_flat_range_product(outer
, umap
);
1812 umap
= subtree_schedule_extend_child(tree
, outer
);
1814 case isl_schedule_node_domain
:
1815 domain
= isl_schedule_tree_domain_get_domain(tree
);
1816 umap
= isl_union_map_from_domain(domain
);
1817 outer
= isl_union_map_flat_range_product(outer
, umap
);
1818 umap
= subtree_schedule_extend_child(tree
, outer
);
1820 case isl_schedule_node_expansion
:
1821 umap
= isl_schedule_tree_expansion_get_expansion(tree
);
1822 outer
= isl_union_map_apply_domain(outer
, umap
);
1823 umap
= subtree_schedule_extend_child(tree
, outer
);
1825 case isl_schedule_node_filter
:
1826 domain
= isl_schedule_tree_filter_get_filter(tree
);
1827 umap
= isl_union_map_from_domain(domain
);
1828 outer
= isl_union_map_flat_range_product(outer
, umap
);
1829 umap
= subtree_schedule_extend_child(tree
, outer
);
1831 case isl_schedule_node_leaf
:
1832 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1833 "leaf node should be handled by caller", return NULL
);
1834 case isl_schedule_node_set
:
1835 case isl_schedule_node_sequence
:
1836 umap
= subtree_schedule_extend_from_children(tree
, outer
);
1843 static __isl_give isl_union_set
*initial_domain(
1844 __isl_keep isl_schedule_tree
*tree
);
1846 /* Extract a universe domain from the children of the tree root "tree",
1847 * which is a set or sequence, meaning that its children are filters.
1848 * In particular, return the union of the universes of the filters.
1850 static __isl_give isl_union_set
*initial_domain_from_children(
1851 __isl_keep isl_schedule_tree
*tree
)
1855 isl_union_set
*domain
;
1857 if (!tree
->children
)
1858 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1859 "missing children", return NULL
);
1860 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
1862 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1863 "missing children", return NULL
);
1865 space
= extract_space_from_filter_child(tree
);
1866 domain
= isl_union_set_empty(space
);
1868 for (i
= 0; i
< n
; ++i
) {
1869 isl_schedule_tree
*child
;
1870 isl_union_set
*domain_i
;
1872 child
= isl_schedule_tree_get_child(tree
, i
);
1873 domain_i
= initial_domain(child
);
1874 domain
= isl_union_set_union(domain
, domain_i
);
1875 isl_schedule_tree_free(child
);
1881 /* Extract a universe domain from the tree root "tree".
1882 * The caller is responsible for making sure that this node
1883 * would not be skipped by isl_schedule_tree_first_schedule_descendant
1884 * and that it is not a leaf node.
1886 static __isl_give isl_union_set
*initial_domain(
1887 __isl_keep isl_schedule_tree
*tree
)
1889 isl_multi_union_pw_aff
*mupa
;
1890 isl_union_set
*domain
;
1896 switch (tree
->type
) {
1897 case isl_schedule_node_error
:
1899 case isl_schedule_node_context
:
1900 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1901 "context node should be handled by caller",
1903 case isl_schedule_node_guard
:
1904 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1905 "guard node should be handled by caller",
1907 case isl_schedule_node_mark
:
1908 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1909 "mark node should be handled by caller",
1911 case isl_schedule_node_extension
:
1912 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1913 "cannot construct subtree schedule of tree "
1914 "with extension nodes", return NULL
);
1915 case isl_schedule_node_band
:
1916 if (isl_schedule_tree_band_n_member(tree
) == 0)
1917 isl_die(isl_schedule_tree_get_ctx(tree
),
1919 "0D band should be handled by caller",
1921 mupa
= isl_schedule_band_get_partial_schedule(tree
->band
);
1922 domain
= isl_multi_union_pw_aff_domain(mupa
);
1923 domain
= isl_union_set_universe(domain
);
1925 case isl_schedule_node_domain
:
1926 domain
= isl_schedule_tree_domain_get_domain(tree
);
1927 domain
= isl_union_set_universe(domain
);
1929 case isl_schedule_node_expansion
:
1930 exp
= isl_schedule_tree_expansion_get_expansion(tree
);
1931 exp
= isl_union_map_universe(exp
);
1932 domain
= isl_union_map_domain(exp
);
1934 case isl_schedule_node_filter
:
1935 domain
= isl_schedule_tree_filter_get_filter(tree
);
1936 domain
= isl_union_set_universe(domain
);
1938 case isl_schedule_node_leaf
:
1939 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1940 "leaf node should be handled by caller", return NULL
);
1941 case isl_schedule_node_set
:
1942 case isl_schedule_node_sequence
:
1943 domain
= initial_domain_from_children(tree
);
1950 /* Return the subtree schedule of a node that contains some schedule
1951 * information, i.e., a node that would not be skipped by
1952 * isl_schedule_tree_first_schedule_descendant and that is not a leaf.
1954 * If the tree contains any expansions, then the returned subtree
1955 * schedule is formulated in terms of the expanded domains.
1956 * The tree is not allowed to contain any extension nodes.
1958 * We start with an initial zero-dimensional subtree schedule based
1959 * on the domain information in the root node and then extend it
1960 * based on the schedule information in the root node and its descendants.
1962 __isl_give isl_union_map
*isl_schedule_tree_get_subtree_schedule_union_map(
1963 __isl_keep isl_schedule_tree
*tree
)
1965 isl_union_set
*domain
;
1966 isl_union_map
*umap
;
1968 domain
= initial_domain(tree
);
1969 umap
= isl_union_map_from_domain(domain
);
1970 return subtree_schedule_extend(tree
, umap
);
1973 /* Multiply the partial schedule of the band root node of "tree"
1974 * with the factors in "mv".
1976 __isl_give isl_schedule_tree
*isl_schedule_tree_band_scale(
1977 __isl_take isl_schedule_tree
*tree
, __isl_take isl_multi_val
*mv
)
1981 if (tree
->type
!= isl_schedule_node_band
)
1982 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1983 "not a band node", goto error
);
1985 tree
= isl_schedule_tree_cow(tree
);
1989 tree
->band
= isl_schedule_band_scale(tree
->band
, mv
);
1991 return isl_schedule_tree_free(tree
);
1995 isl_schedule_tree_free(tree
);
1996 isl_multi_val_free(mv
);
2000 /* Divide the partial schedule of the band root node of "tree"
2001 * by the factors in "mv".
2003 __isl_give isl_schedule_tree
*isl_schedule_tree_band_scale_down(
2004 __isl_take isl_schedule_tree
*tree
, __isl_take isl_multi_val
*mv
)
2008 if (tree
->type
!= isl_schedule_node_band
)
2009 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2010 "not a band node", goto error
);
2012 tree
= isl_schedule_tree_cow(tree
);
2016 tree
->band
= isl_schedule_band_scale_down(tree
->band
, mv
);
2018 return isl_schedule_tree_free(tree
);
2022 isl_schedule_tree_free(tree
);
2023 isl_multi_val_free(mv
);
2027 /* Given two trees with sequence roots, replace the child at position
2028 * "pos" of "tree" with the children of "child".
2030 __isl_give isl_schedule_tree
*isl_schedule_tree_sequence_splice(
2031 __isl_take isl_schedule_tree
*tree
, int pos
,
2032 __isl_take isl_schedule_tree
*child
)
2035 isl_schedule_tree_list
*list1
, *list2
;
2037 tree
= isl_schedule_tree_cow(tree
);
2038 if (!tree
|| !child
)
2040 if (isl_schedule_tree_get_type(tree
) != isl_schedule_node_sequence
)
2041 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2042 "not a sequence node", goto error
);
2043 n
= isl_schedule_tree_n_children(tree
);
2044 if (pos
< 0 || pos
>= n
)
2045 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2046 "position out of bounds", goto error
);
2047 if (isl_schedule_tree_get_type(child
) != isl_schedule_node_sequence
)
2048 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2049 "not a sequence node", goto error
);
2051 list1
= isl_schedule_tree_list_copy(tree
->children
);
2052 list1
= isl_schedule_tree_list_drop(list1
, pos
, n
- pos
);
2053 list2
= isl_schedule_tree_list_copy(tree
->children
);
2054 list2
= isl_schedule_tree_list_drop(list2
, 0, pos
+ 1);
2055 list1
= isl_schedule_tree_list_concat(list1
,
2056 isl_schedule_tree_list_copy(child
->children
));
2057 list1
= isl_schedule_tree_list_concat(list1
, list2
);
2059 isl_schedule_tree_free(tree
);
2060 isl_schedule_tree_free(child
);
2061 return isl_schedule_tree_from_children(isl_schedule_node_sequence
,
2064 isl_schedule_tree_free(tree
);
2065 isl_schedule_tree_free(child
);
2069 /* Tile the band root node of "tree" with tile sizes "sizes".
2071 * We duplicate the band node, change the schedule of one of them
2072 * to the tile schedule and the other to the point schedule and then
2073 * attach the point band as a child to the tile band.
2075 __isl_give isl_schedule_tree
*isl_schedule_tree_band_tile(
2076 __isl_take isl_schedule_tree
*tree
, __isl_take isl_multi_val
*sizes
)
2078 isl_schedule_tree
*child
= NULL
;
2080 if (!tree
|| !sizes
)
2082 if (tree
->type
!= isl_schedule_node_band
)
2083 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2084 "not a band node", goto error
);
2086 child
= isl_schedule_tree_copy(tree
);
2087 tree
= isl_schedule_tree_cow(tree
);
2088 child
= isl_schedule_tree_cow(child
);
2089 if (!tree
|| !child
)
2092 tree
->band
= isl_schedule_band_tile(tree
->band
,
2093 isl_multi_val_copy(sizes
));
2096 child
->band
= isl_schedule_band_point(child
->band
, tree
->band
, sizes
);
2098 child
= isl_schedule_tree_free(child
);
2100 tree
= isl_schedule_tree_replace_child(tree
, 0, child
);
2104 isl_schedule_tree_free(child
);
2105 isl_schedule_tree_free(tree
);
2106 isl_multi_val_free(sizes
);
2110 /* Split the band root node of "tree" into two nested band nodes,
2111 * one with the first "pos" dimensions and
2112 * one with the remaining dimensions.
2114 __isl_give isl_schedule_tree
*isl_schedule_tree_band_split(
2115 __isl_take isl_schedule_tree
*tree
, int pos
)
2118 isl_schedule_tree
*child
;
2122 if (tree
->type
!= isl_schedule_node_band
)
2123 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2124 "not a band node", return isl_schedule_tree_free(tree
));
2126 n
= isl_schedule_tree_band_n_member(tree
);
2127 if (pos
< 0 || pos
> n
)
2128 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2129 "position out of bounds",
2130 return isl_schedule_tree_free(tree
));
2132 child
= isl_schedule_tree_copy(tree
);
2133 tree
= isl_schedule_tree_cow(tree
);
2134 child
= isl_schedule_tree_cow(child
);
2135 if (!tree
|| !child
)
2138 child
->band
= isl_schedule_band_drop(child
->band
, 0, pos
);
2139 tree
->band
= isl_schedule_band_drop(tree
->band
, pos
, n
- pos
);
2140 if (!child
->band
|| !tree
->band
)
2143 tree
= isl_schedule_tree_replace_child(tree
, 0, child
);
2147 isl_schedule_tree_free(child
);
2148 isl_schedule_tree_free(tree
);
2152 /* Attach "tree2" at each of the leaves of "tree1".
2154 * If "tree1" does not have any explicit children, then make "tree2"
2155 * its single child. Otherwise, attach "tree2" to the leaves of
2156 * each of the children of "tree1".
2158 __isl_give isl_schedule_tree
*isl_schedule_tree_append_to_leaves(
2159 __isl_take isl_schedule_tree
*tree1
,
2160 __isl_take isl_schedule_tree
*tree2
)
2164 if (!tree1
|| !tree2
)
2166 n
= isl_schedule_tree_n_children(tree1
);
2168 isl_schedule_tree_list
*list
;
2169 list
= isl_schedule_tree_list_from_schedule_tree(tree2
);
2170 tree1
= isl_schedule_tree_set_children(tree1
, list
);
2173 for (i
= 0; i
< n
; ++i
) {
2174 isl_schedule_tree
*child
;
2176 child
= isl_schedule_tree_get_child(tree1
, i
);
2177 child
= isl_schedule_tree_append_to_leaves(child
,
2178 isl_schedule_tree_copy(tree2
));
2179 tree1
= isl_schedule_tree_replace_child(tree1
, i
, child
);
2182 isl_schedule_tree_free(tree2
);
2185 isl_schedule_tree_free(tree1
);
2186 isl_schedule_tree_free(tree2
);
2190 /* Reset the user pointer on all identifiers of parameters and tuples
2191 * in the root of "tree".
2193 __isl_give isl_schedule_tree
*isl_schedule_tree_reset_user(
2194 __isl_take isl_schedule_tree
*tree
)
2196 if (isl_schedule_tree_is_leaf(tree
))
2199 tree
= isl_schedule_tree_cow(tree
);
2203 switch (tree
->type
) {
2204 case isl_schedule_node_error
:
2205 return isl_schedule_tree_free(tree
);
2206 case isl_schedule_node_band
:
2207 tree
->band
= isl_schedule_band_reset_user(tree
->band
);
2209 return isl_schedule_tree_free(tree
);
2211 case isl_schedule_node_context
:
2212 tree
->context
= isl_set_reset_user(tree
->context
);
2214 return isl_schedule_tree_free(tree
);
2216 case isl_schedule_node_domain
:
2217 tree
->domain
= isl_union_set_reset_user(tree
->domain
);
2219 return isl_schedule_tree_free(tree
);
2221 case isl_schedule_node_expansion
:
2223 isl_union_pw_multi_aff_reset_user(tree
->contraction
);
2224 tree
->expansion
= isl_union_map_reset_user(tree
->expansion
);
2225 if (!tree
->contraction
|| !tree
->expansion
)
2226 return isl_schedule_tree_free(tree
);
2228 case isl_schedule_node_extension
:
2229 tree
->extension
= isl_union_map_reset_user(tree
->extension
);
2230 if (!tree
->extension
)
2231 return isl_schedule_tree_free(tree
);
2233 case isl_schedule_node_filter
:
2234 tree
->filter
= isl_union_set_reset_user(tree
->filter
);
2236 return isl_schedule_tree_free(tree
);
2238 case isl_schedule_node_guard
:
2239 tree
->guard
= isl_set_reset_user(tree
->guard
);
2241 return isl_schedule_tree_free(tree
);
2243 case isl_schedule_node_leaf
:
2244 case isl_schedule_node_mark
:
2245 case isl_schedule_node_sequence
:
2246 case isl_schedule_node_set
:
2253 /* Align the parameters of the root of "tree" to those of "space".
2255 __isl_give isl_schedule_tree
*isl_schedule_tree_align_params(
2256 __isl_take isl_schedule_tree
*tree
, __isl_take isl_space
*space
)
2261 if (isl_schedule_tree_is_leaf(tree
)) {
2262 isl_space_free(space
);
2266 tree
= isl_schedule_tree_cow(tree
);
2270 switch (tree
->type
) {
2271 case isl_schedule_node_error
:
2273 case isl_schedule_node_band
:
2274 tree
->band
= isl_schedule_band_align_params(tree
->band
, space
);
2276 return isl_schedule_tree_free(tree
);
2278 case isl_schedule_node_context
:
2279 tree
->context
= isl_set_align_params(tree
->context
, space
);
2281 return isl_schedule_tree_free(tree
);
2283 case isl_schedule_node_domain
:
2284 tree
->domain
= isl_union_set_align_params(tree
->domain
, space
);
2286 return isl_schedule_tree_free(tree
);
2288 case isl_schedule_node_expansion
:
2290 isl_union_pw_multi_aff_align_params(tree
->contraction
,
2291 isl_space_copy(space
));
2292 tree
->expansion
= isl_union_map_align_params(tree
->expansion
,
2294 if (!tree
->contraction
|| !tree
->expansion
)
2295 return isl_schedule_tree_free(tree
);
2297 case isl_schedule_node_extension
:
2298 tree
->extension
= isl_union_map_align_params(tree
->extension
,
2300 if (!tree
->extension
)
2301 return isl_schedule_tree_free(tree
);
2303 case isl_schedule_node_filter
:
2304 tree
->filter
= isl_union_set_align_params(tree
->filter
, space
);
2306 return isl_schedule_tree_free(tree
);
2308 case isl_schedule_node_guard
:
2309 tree
->guard
= isl_set_align_params(tree
->guard
, space
);
2311 return isl_schedule_tree_free(tree
);
2313 case isl_schedule_node_leaf
:
2314 case isl_schedule_node_mark
:
2315 case isl_schedule_node_sequence
:
2316 case isl_schedule_node_set
:
2317 isl_space_free(space
);
2323 isl_space_free(space
);
2324 isl_schedule_tree_free(tree
);
2328 /* Does "tree" involve the iteration domain?
2329 * That is, does it need to be modified
2330 * by isl_schedule_tree_pullback_union_pw_multi_aff?
2332 static int involves_iteration_domain(__isl_keep isl_schedule_tree
*tree
)
2337 switch (tree
->type
) {
2338 case isl_schedule_node_error
:
2340 case isl_schedule_node_band
:
2341 case isl_schedule_node_domain
:
2342 case isl_schedule_node_expansion
:
2343 case isl_schedule_node_extension
:
2344 case isl_schedule_node_filter
:
2346 case isl_schedule_node_context
:
2347 case isl_schedule_node_leaf
:
2348 case isl_schedule_node_guard
:
2349 case isl_schedule_node_mark
:
2350 case isl_schedule_node_sequence
:
2351 case isl_schedule_node_set
:
2356 /* Compute the pullback of the root node of "tree" by the function
2357 * represented by "upma".
2358 * In other words, plug in "upma" in the iteration domains of
2359 * the root node of "tree".
2360 * We currently do not handle expansion nodes.
2362 * We first check if the root node involves any iteration domains.
2363 * If so, we handle the specific cases.
2365 __isl_give isl_schedule_tree
*isl_schedule_tree_pullback_union_pw_multi_aff(
2366 __isl_take isl_schedule_tree
*tree
,
2367 __isl_take isl_union_pw_multi_aff
*upma
)
2374 involves
= involves_iteration_domain(tree
);
2378 isl_union_pw_multi_aff_free(upma
);
2382 tree
= isl_schedule_tree_cow(tree
);
2386 if (tree
->type
== isl_schedule_node_band
) {
2387 tree
->band
= isl_schedule_band_pullback_union_pw_multi_aff(
2390 return isl_schedule_tree_free(tree
);
2391 } else if (tree
->type
== isl_schedule_node_domain
) {
2393 isl_union_set_preimage_union_pw_multi_aff(tree
->domain
,
2396 return isl_schedule_tree_free(tree
);
2397 } else if (tree
->type
== isl_schedule_node_expansion
) {
2398 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_unsupported
,
2399 "cannot pullback expansion node", goto error
);
2400 } else if (tree
->type
== isl_schedule_node_extension
) {
2402 isl_union_map_preimage_range_union_pw_multi_aff(
2403 tree
->extension
, upma
);
2404 if (!tree
->extension
)
2405 return isl_schedule_tree_free(tree
);
2406 } else if (tree
->type
== isl_schedule_node_filter
) {
2408 isl_union_set_preimage_union_pw_multi_aff(tree
->filter
,
2411 return isl_schedule_tree_free(tree
);
2416 isl_union_pw_multi_aff_free(upma
);
2417 isl_schedule_tree_free(tree
);
2421 /* Compute the gist of the band tree root with respect to "context".
2423 __isl_give isl_schedule_tree
*isl_schedule_tree_band_gist(
2424 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*context
)
2428 if (tree
->type
!= isl_schedule_node_band
)
2429 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2430 "not a band node", goto error
);
2431 tree
= isl_schedule_tree_cow(tree
);
2435 tree
->band
= isl_schedule_band_gist(tree
->band
, context
);
2437 return isl_schedule_tree_free(tree
);
2440 isl_union_set_free(context
);
2441 isl_schedule_tree_free(tree
);
2445 /* Are any members in "band" marked coincident?
2447 static int any_coincident(__isl_keep isl_schedule_band
*band
)
2451 n
= isl_schedule_band_n_member(band
);
2452 for (i
= 0; i
< n
; ++i
)
2453 if (isl_schedule_band_member_get_coincident(band
, i
))
2459 /* Print the band node "band" to "p".
2461 * The permutable and coincident properties are only printed if they
2462 * are different from the defaults.
2463 * The coincident property is always printed in YAML flow style.
2465 static __isl_give isl_printer
*print_tree_band(__isl_take isl_printer
*p
,
2466 __isl_keep isl_schedule_band
*band
)
2468 isl_union_set
*options
;
2471 p
= isl_printer_print_str(p
, "schedule");
2472 p
= isl_printer_yaml_next(p
);
2473 p
= isl_printer_print_str(p
, "\"");
2474 p
= isl_printer_print_multi_union_pw_aff(p
, band
->mupa
);
2475 p
= isl_printer_print_str(p
, "\"");
2476 if (isl_schedule_band_get_permutable(band
)) {
2477 p
= isl_printer_yaml_next(p
);
2478 p
= isl_printer_print_str(p
, "permutable");
2479 p
= isl_printer_yaml_next(p
);
2480 p
= isl_printer_print_int(p
, 1);
2482 if (any_coincident(band
)) {
2486 p
= isl_printer_yaml_next(p
);
2487 p
= isl_printer_print_str(p
, "coincident");
2488 p
= isl_printer_yaml_next(p
);
2489 style
= isl_printer_get_yaml_style(p
);
2490 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2491 p
= isl_printer_yaml_start_sequence(p
);
2492 n
= isl_schedule_band_n_member(band
);
2493 for (i
= 0; i
< n
; ++i
) {
2494 p
= isl_printer_print_int(p
,
2495 isl_schedule_band_member_get_coincident(band
, i
));
2496 p
= isl_printer_yaml_next(p
);
2498 p
= isl_printer_yaml_end_sequence(p
);
2499 p
= isl_printer_set_yaml_style(p
, style
);
2501 options
= isl_schedule_band_get_ast_build_options(band
);
2502 empty
= isl_union_set_is_empty(options
);
2504 p
= isl_printer_free(p
);
2506 p
= isl_printer_yaml_next(p
);
2507 p
= isl_printer_print_str(p
, "options");
2508 p
= isl_printer_yaml_next(p
);
2509 p
= isl_printer_print_str(p
, "\"");
2510 p
= isl_printer_print_union_set(p
, options
);
2511 p
= isl_printer_print_str(p
, "\"");
2513 isl_union_set_free(options
);
2518 /* Print "tree" to "p".
2520 * If "n_ancestor" is non-negative, then "child_pos" contains the child
2521 * positions of a descendant of the current node that should be marked
2522 * (by the comment "YOU ARE HERE"). In particular, if "n_ancestor"
2523 * is zero, then the current node should be marked.
2524 * The marking is only printed in YAML block format.
2526 * Implicit leaf nodes are not printed, except if they correspond
2527 * to the node that should be marked.
2529 __isl_give isl_printer
*isl_printer_print_schedule_tree_mark(
2530 __isl_take isl_printer
*p
, __isl_keep isl_schedule_tree
*tree
,
2531 int n_ancestor
, int *child_pos
)
2537 block
= isl_printer_get_yaml_style(p
) == ISL_YAML_STYLE_BLOCK
;
2539 p
= isl_printer_yaml_start_mapping(p
);
2540 if (n_ancestor
== 0 && block
) {
2541 p
= isl_printer_print_str(p
, "# YOU ARE HERE");
2542 p
= isl_printer_end_line(p
);
2543 p
= isl_printer_start_line(p
);
2545 switch (tree
->type
) {
2546 case isl_schedule_node_error
:
2547 p
= isl_printer_print_str(p
, "ERROR");
2549 case isl_schedule_node_leaf
:
2550 p
= isl_printer_print_str(p
, "leaf");
2552 case isl_schedule_node_sequence
:
2553 p
= isl_printer_print_str(p
, "sequence");
2556 case isl_schedule_node_set
:
2557 p
= isl_printer_print_str(p
, "set");
2560 case isl_schedule_node_context
:
2561 p
= isl_printer_print_str(p
, "context");
2562 p
= isl_printer_yaml_next(p
);
2563 p
= isl_printer_print_str(p
, "\"");
2564 p
= isl_printer_print_set(p
, tree
->context
);
2565 p
= isl_printer_print_str(p
, "\"");
2567 case isl_schedule_node_domain
:
2568 p
= isl_printer_print_str(p
, "domain");
2569 p
= isl_printer_yaml_next(p
);
2570 p
= isl_printer_print_str(p
, "\"");
2571 p
= isl_printer_print_union_set(p
, tree
->domain
);
2572 p
= isl_printer_print_str(p
, "\"");
2574 case isl_schedule_node_expansion
:
2575 p
= isl_printer_print_str(p
, "contraction");
2576 p
= isl_printer_yaml_next(p
);
2577 p
= isl_printer_print_str(p
, "\"");
2578 p
= isl_printer_print_union_pw_multi_aff(p
, tree
->contraction
);
2579 p
= isl_printer_print_str(p
, "\"");
2580 p
= isl_printer_yaml_next(p
);
2581 p
= isl_printer_print_str(p
, "expansion");
2582 p
= isl_printer_yaml_next(p
);
2583 p
= isl_printer_print_str(p
, "\"");
2584 p
= isl_printer_print_union_map(p
, tree
->expansion
);
2585 p
= isl_printer_print_str(p
, "\"");
2587 case isl_schedule_node_extension
:
2588 p
= isl_printer_print_str(p
, "extension");
2589 p
= isl_printer_yaml_next(p
);
2590 p
= isl_printer_print_str(p
, "\"");
2591 p
= isl_printer_print_union_map(p
, tree
->extension
);
2592 p
= isl_printer_print_str(p
, "\"");
2594 case isl_schedule_node_filter
:
2595 p
= isl_printer_print_str(p
, "filter");
2596 p
= isl_printer_yaml_next(p
);
2597 p
= isl_printer_print_str(p
, "\"");
2598 p
= isl_printer_print_union_set(p
, tree
->filter
);
2599 p
= isl_printer_print_str(p
, "\"");
2601 case isl_schedule_node_guard
:
2602 p
= isl_printer_print_str(p
, "guard");
2603 p
= isl_printer_yaml_next(p
);
2604 p
= isl_printer_print_str(p
, "\"");
2605 p
= isl_printer_print_set(p
, tree
->guard
);
2606 p
= isl_printer_print_str(p
, "\"");
2608 case isl_schedule_node_mark
:
2609 p
= isl_printer_print_str(p
, "mark");
2610 p
= isl_printer_yaml_next(p
);
2611 p
= isl_printer_print_str(p
, "\"");
2612 p
= isl_printer_print_str(p
, isl_id_get_name(tree
->mark
));
2613 p
= isl_printer_print_str(p
, "\"");
2615 case isl_schedule_node_band
:
2616 p
= print_tree_band(p
, tree
->band
);
2619 p
= isl_printer_yaml_next(p
);
2621 if (!tree
->children
) {
2622 if (n_ancestor
> 0 && block
) {
2623 isl_schedule_tree
*leaf
;
2625 p
= isl_printer_print_str(p
, "child");
2626 p
= isl_printer_yaml_next(p
);
2627 leaf
= isl_schedule_tree_leaf(isl_printer_get_ctx(p
));
2628 p
= isl_printer_print_schedule_tree_mark(p
,
2630 isl_schedule_tree_free(leaf
);
2631 p
= isl_printer_yaml_next(p
);
2633 return isl_printer_yaml_end_mapping(p
);
2637 p
= isl_printer_yaml_start_sequence(p
);
2639 p
= isl_printer_print_str(p
, "child");
2640 p
= isl_printer_yaml_next(p
);
2643 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
2644 for (i
= 0; i
< n
; ++i
) {
2645 isl_schedule_tree
*t
;
2647 t
= isl_schedule_tree_get_child(tree
, i
);
2648 if (n_ancestor
> 0 && child_pos
[0] == i
)
2649 p
= isl_printer_print_schedule_tree_mark(p
, t
,
2650 n_ancestor
- 1, child_pos
+ 1);
2652 p
= isl_printer_print_schedule_tree_mark(p
, t
,
2654 isl_schedule_tree_free(t
);
2656 p
= isl_printer_yaml_next(p
);
2660 p
= isl_printer_yaml_end_sequence(p
);
2661 p
= isl_printer_yaml_end_mapping(p
);
2666 /* Print "tree" to "p".
2668 __isl_give isl_printer
*isl_printer_print_schedule_tree(
2669 __isl_take isl_printer
*p
, __isl_keep isl_schedule_tree
*tree
)
2671 return isl_printer_print_schedule_tree_mark(p
, tree
, -1, NULL
);
2674 void isl_schedule_tree_dump(__isl_keep isl_schedule_tree
*tree
)
2677 isl_printer
*printer
;
2682 ctx
= isl_schedule_tree_get_ctx(tree
);
2683 printer
= isl_printer_to_file(ctx
, stderr
);
2684 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
2685 printer
= isl_printer_print_schedule_tree(printer
, tree
);
2687 isl_printer_free(printer
);