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 __isl_give isl_schedule_tree
*isl_schedule_tree_cow(
145 __isl_take isl_schedule_tree
*tree
)
153 return isl_schedule_tree_dup(tree
);
156 /* Return a new reference to "tree".
158 __isl_give isl_schedule_tree
*isl_schedule_tree_copy(
159 __isl_keep isl_schedule_tree
*tree
)
168 /* Free "tree" and return NULL.
170 __isl_null isl_schedule_tree
*isl_schedule_tree_free(
171 __isl_take isl_schedule_tree
*tree
)
178 switch (tree
->type
) {
179 case isl_schedule_node_band
:
180 isl_schedule_band_free(tree
->band
);
182 case isl_schedule_node_context
:
183 isl_set_free(tree
->context
);
185 case isl_schedule_node_domain
:
186 isl_union_set_free(tree
->domain
);
188 case isl_schedule_node_expansion
:
189 isl_union_pw_multi_aff_free(tree
->contraction
);
190 isl_union_map_free(tree
->expansion
);
192 case isl_schedule_node_extension
:
193 isl_union_map_free(tree
->extension
);
195 case isl_schedule_node_filter
:
196 isl_union_set_free(tree
->filter
);
198 case isl_schedule_node_guard
:
199 isl_set_free(tree
->guard
);
201 case isl_schedule_node_mark
:
202 isl_id_free(tree
->mark
);
204 case isl_schedule_node_sequence
:
205 case isl_schedule_node_set
:
206 case isl_schedule_node_error
:
207 case isl_schedule_node_leaf
:
210 isl_schedule_tree_list_free(tree
->children
);
211 isl_ctx_deref(tree
->ctx
);
217 /* Create and return a new leaf schedule tree.
219 __isl_give isl_schedule_tree
*isl_schedule_tree_leaf(isl_ctx
*ctx
)
221 return isl_schedule_tree_alloc(ctx
, isl_schedule_node_leaf
);
224 /* Create a new band schedule tree referring to "band"
227 __isl_give isl_schedule_tree
*isl_schedule_tree_from_band(
228 __isl_take isl_schedule_band
*band
)
231 isl_schedule_tree
*tree
;
236 ctx
= isl_schedule_band_get_ctx(band
);
237 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_band
);
242 tree
->anchored
= isl_schedule_band_is_anchored(band
);
246 isl_schedule_band_free(band
);
250 /* Create a new context schedule tree with the given context and no children.
251 * Since the context references the outer schedule dimension,
252 * the tree is anchored.
254 __isl_give isl_schedule_tree
*isl_schedule_tree_from_context(
255 __isl_take isl_set
*context
)
258 isl_schedule_tree
*tree
;
263 ctx
= isl_set_get_ctx(context
);
264 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_context
);
268 tree
->context
= context
;
273 isl_set_free(context
);
277 /* Create a new domain schedule tree with the given domain and no children.
279 __isl_give isl_schedule_tree
*isl_schedule_tree_from_domain(
280 __isl_take isl_union_set
*domain
)
283 isl_schedule_tree
*tree
;
288 ctx
= isl_union_set_get_ctx(domain
);
289 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_domain
);
293 tree
->domain
= domain
;
297 isl_union_set_free(domain
);
301 /* Create a new expansion schedule tree with the given contraction and
302 * expansion and no children.
304 __isl_give isl_schedule_tree
*isl_schedule_tree_from_expansion(
305 __isl_take isl_union_pw_multi_aff
*contraction
,
306 __isl_take isl_union_map
*expansion
)
309 isl_schedule_tree
*tree
;
311 if (!contraction
|| !expansion
)
314 ctx
= isl_union_map_get_ctx(expansion
);
315 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_expansion
);
319 tree
->contraction
= contraction
;
320 tree
->expansion
= expansion
;
324 isl_union_pw_multi_aff_free(contraction
);
325 isl_union_map_free(expansion
);
329 /* Create a new extension schedule tree with the given extension and
331 * Since the domain of the extension refers to the outer schedule dimension,
332 * the tree is anchored.
334 __isl_give isl_schedule_tree
*isl_schedule_tree_from_extension(
335 __isl_take isl_union_map
*extension
)
338 isl_schedule_tree
*tree
;
343 ctx
= isl_union_map_get_ctx(extension
);
344 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_extension
);
348 tree
->extension
= extension
;
353 isl_union_map_free(extension
);
357 /* Create a new filter schedule tree with the given filter and no children.
359 __isl_give isl_schedule_tree
*isl_schedule_tree_from_filter(
360 __isl_take isl_union_set
*filter
)
363 isl_schedule_tree
*tree
;
368 ctx
= isl_union_set_get_ctx(filter
);
369 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_filter
);
373 tree
->filter
= filter
;
377 isl_union_set_free(filter
);
381 /* Create a new guard schedule tree with the given guard and no children.
382 * Since the guard references the outer schedule dimension,
383 * the tree is anchored.
385 __isl_give isl_schedule_tree
*isl_schedule_tree_from_guard(
386 __isl_take isl_set
*guard
)
389 isl_schedule_tree
*tree
;
394 ctx
= isl_set_get_ctx(guard
);
395 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_guard
);
408 /* Create a new mark schedule tree with the given mark identifier and
411 __isl_give isl_schedule_tree
*isl_schedule_tree_from_mark(
412 __isl_take isl_id
*mark
)
415 isl_schedule_tree
*tree
;
420 ctx
= isl_id_get_ctx(mark
);
421 tree
= isl_schedule_tree_alloc(ctx
, isl_schedule_node_mark
);
433 /* Does "tree" have any node that depends on its position
434 * in the complete schedule tree?
436 isl_bool
isl_schedule_tree_is_subtree_anchored(
437 __isl_keep isl_schedule_tree
*tree
)
439 return tree
? tree
->anchored
: isl_bool_error
;
442 /* Does the root node of "tree" depend on its position in the complete
444 * Band nodes may be anchored depending on the associated AST build options.
445 * Context, extension and guard nodes are always anchored.
447 int isl_schedule_tree_is_anchored(__isl_keep isl_schedule_tree
*tree
)
452 switch (isl_schedule_tree_get_type(tree
)) {
453 case isl_schedule_node_error
:
455 case isl_schedule_node_band
:
456 return isl_schedule_band_is_anchored(tree
->band
);
457 case isl_schedule_node_context
:
458 case isl_schedule_node_extension
:
459 case isl_schedule_node_guard
:
461 case isl_schedule_node_domain
:
462 case isl_schedule_node_expansion
:
463 case isl_schedule_node_filter
:
464 case isl_schedule_node_leaf
:
465 case isl_schedule_node_mark
:
466 case isl_schedule_node_sequence
:
467 case isl_schedule_node_set
:
471 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
472 "unhandled case", return -1);
475 /* Update the anchored field of "tree" based on whether the root node
476 * itself in anchored and the anchored fields of the children.
478 * This function should be called whenever the children of a tree node
479 * are changed or the anchoredness of the tree root itself changes.
481 __isl_give isl_schedule_tree
*isl_schedule_tree_update_anchored(
482 __isl_take isl_schedule_tree
*tree
)
490 anchored
= isl_schedule_tree_is_anchored(tree
);
492 return isl_schedule_tree_free(tree
);
494 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
495 for (i
= 0; !anchored
&& i
< n
; ++i
) {
496 isl_schedule_tree
*child
;
498 child
= isl_schedule_tree_get_child(tree
, i
);
500 return isl_schedule_tree_free(tree
);
501 anchored
= child
->anchored
;
502 isl_schedule_tree_free(child
);
505 if (anchored
== tree
->anchored
)
507 tree
= isl_schedule_tree_cow(tree
);
510 tree
->anchored
= anchored
;
514 /* Create a new tree of the given type (isl_schedule_node_sequence or
515 * isl_schedule_node_set) with the given children.
517 __isl_give isl_schedule_tree
*isl_schedule_tree_from_children(
518 enum isl_schedule_node_type type
,
519 __isl_take isl_schedule_tree_list
*list
)
522 isl_schedule_tree
*tree
;
527 ctx
= isl_schedule_tree_list_get_ctx(list
);
528 tree
= isl_schedule_tree_alloc(ctx
, type
);
532 tree
->children
= list
;
533 tree
= isl_schedule_tree_update_anchored(tree
);
537 isl_schedule_tree_list_free(list
);
541 /* Construct a tree with a root node of type "type" and as children
542 * "tree1" and "tree2".
543 * If the root of one (or both) of the input trees is itself of type "type",
544 * then the tree is replaced by its children.
546 __isl_give isl_schedule_tree
*isl_schedule_tree_from_pair(
547 enum isl_schedule_node_type type
, __isl_take isl_schedule_tree
*tree1
,
548 __isl_take isl_schedule_tree
*tree2
)
551 isl_schedule_tree_list
*list
;
553 if (!tree1
|| !tree2
)
556 ctx
= isl_schedule_tree_get_ctx(tree1
);
557 if (isl_schedule_tree_get_type(tree1
) == type
) {
558 list
= isl_schedule_tree_list_copy(tree1
->children
);
559 isl_schedule_tree_free(tree1
);
561 list
= isl_schedule_tree_list_alloc(ctx
, 2);
562 list
= isl_schedule_tree_list_add(list
, tree1
);
564 if (isl_schedule_tree_get_type(tree2
) == type
) {
565 isl_schedule_tree_list
*children
;
567 children
= isl_schedule_tree_list_copy(tree2
->children
);
568 list
= isl_schedule_tree_list_concat(list
, children
);
569 isl_schedule_tree_free(tree2
);
571 list
= isl_schedule_tree_list_add(list
, tree2
);
574 return isl_schedule_tree_from_children(type
, list
);
576 isl_schedule_tree_free(tree1
);
577 isl_schedule_tree_free(tree2
);
581 /* Construct a tree with a sequence root node and as children
582 * "tree1" and "tree2".
583 * If the root of one (or both) of the input trees is itself a sequence,
584 * then the tree is replaced by its children.
586 __isl_give isl_schedule_tree
*isl_schedule_tree_sequence_pair(
587 __isl_take isl_schedule_tree
*tree1
,
588 __isl_take isl_schedule_tree
*tree2
)
590 return isl_schedule_tree_from_pair(isl_schedule_node_sequence
,
594 /* Construct a tree with a set root node and as children
595 * "tree1" and "tree2".
596 * If the root of one (or both) of the input trees is itself a set,
597 * then the tree is replaced by its children.
599 __isl_give isl_schedule_tree
*isl_schedule_tree_set_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_set
, tree1
, tree2
);
606 /* Return the isl_ctx to which "tree" belongs.
608 isl_ctx
*isl_schedule_tree_get_ctx(__isl_keep isl_schedule_tree
*tree
)
610 return tree
? tree
->ctx
: NULL
;
613 /* Return the type of the root of the tree or isl_schedule_node_error
616 enum isl_schedule_node_type
isl_schedule_tree_get_type(
617 __isl_keep isl_schedule_tree
*tree
)
619 return tree
? tree
->type
: isl_schedule_node_error
;
622 /* Are "tree1" and "tree2" obviously equal to each other?
624 isl_bool
isl_schedule_tree_plain_is_equal(__isl_keep isl_schedule_tree
*tree1
,
625 __isl_keep isl_schedule_tree
*tree2
)
630 if (!tree1
|| !tree2
)
631 return isl_bool_error
;
633 return isl_bool_true
;
634 if (tree1
->type
!= tree2
->type
)
635 return isl_bool_false
;
637 switch (tree1
->type
) {
638 case isl_schedule_node_band
:
639 equal
= isl_schedule_band_plain_is_equal(tree1
->band
,
642 case isl_schedule_node_context
:
643 equal
= isl_set_is_equal(tree1
->context
, tree2
->context
);
645 case isl_schedule_node_domain
:
646 equal
= isl_union_set_is_equal(tree1
->domain
, tree2
->domain
);
648 case isl_schedule_node_expansion
:
649 equal
= isl_union_map_is_equal(tree1
->expansion
,
651 if (equal
>= 0 && equal
)
652 equal
= isl_union_pw_multi_aff_plain_is_equal(
653 tree1
->contraction
, tree2
->contraction
);
655 case isl_schedule_node_extension
:
656 equal
= isl_union_map_is_equal(tree1
->extension
,
659 case isl_schedule_node_filter
:
660 equal
= isl_union_set_is_equal(tree1
->filter
, tree2
->filter
);
662 case isl_schedule_node_guard
:
663 equal
= isl_set_is_equal(tree1
->guard
, tree2
->guard
);
665 case isl_schedule_node_mark
:
666 equal
= tree1
->mark
== tree2
->mark
;
668 case isl_schedule_node_leaf
:
669 case isl_schedule_node_sequence
:
670 case isl_schedule_node_set
:
671 equal
= isl_bool_true
;
673 case isl_schedule_node_error
:
674 equal
= isl_bool_error
;
678 if (equal
< 0 || !equal
)
681 n
= isl_schedule_tree_n_children(tree1
);
682 if (n
!= isl_schedule_tree_n_children(tree2
))
683 return isl_bool_false
;
684 for (i
= 0; i
< n
; ++i
) {
685 isl_schedule_tree
*child1
, *child2
;
687 child1
= isl_schedule_tree_get_child(tree1
, i
);
688 child2
= isl_schedule_tree_get_child(tree2
, i
);
689 equal
= isl_schedule_tree_plain_is_equal(child1
, child2
);
690 isl_schedule_tree_free(child1
);
691 isl_schedule_tree_free(child2
);
693 if (equal
< 0 || !equal
)
697 return isl_bool_true
;
700 /* Does "tree" have any children, other than an implicit leaf.
702 int isl_schedule_tree_has_children(__isl_keep isl_schedule_tree
*tree
)
707 return tree
->children
!= NULL
;
710 /* Return the number of children of "tree", excluding implicit leaves.
712 int isl_schedule_tree_n_children(__isl_keep isl_schedule_tree
*tree
)
717 return isl_schedule_tree_list_n_schedule_tree(tree
->children
);
720 /* Return a copy of the (explicit) child at position "pos" of "tree".
722 __isl_give isl_schedule_tree
*isl_schedule_tree_get_child(
723 __isl_keep isl_schedule_tree
*tree
, int pos
)
728 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
729 "schedule tree has no explicit children", return NULL
);
730 return isl_schedule_tree_list_get_schedule_tree(tree
->children
, pos
);
733 /* Return a copy of the (explicit) child at position "pos" of "tree" and
736 __isl_give isl_schedule_tree
*isl_schedule_tree_child(
737 __isl_take isl_schedule_tree
*tree
, int pos
)
739 isl_schedule_tree
*child
;
741 child
= isl_schedule_tree_get_child(tree
, pos
);
742 isl_schedule_tree_free(tree
);
746 /* Remove all (explicit) children from "tree".
748 __isl_give isl_schedule_tree
*isl_schedule_tree_reset_children(
749 __isl_take isl_schedule_tree
*tree
)
751 tree
= isl_schedule_tree_cow(tree
);
754 tree
->children
= isl_schedule_tree_list_free(tree
->children
);
758 /* Remove the child at position "pos" from the children of "tree".
759 * If there was only one child to begin with, then remove all children.
761 __isl_give isl_schedule_tree
*isl_schedule_tree_drop_child(
762 __isl_take isl_schedule_tree
*tree
, int pos
)
766 tree
= isl_schedule_tree_cow(tree
);
770 if (!isl_schedule_tree_has_children(tree
))
771 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
772 "tree does not have any explicit children",
773 return isl_schedule_tree_free(tree
));
774 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
775 if (pos
< 0 || pos
>= n
)
776 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
777 "position out of bounds",
778 return isl_schedule_tree_free(tree
));
780 return isl_schedule_tree_reset_children(tree
);
782 tree
->children
= isl_schedule_tree_list_drop(tree
->children
, pos
, 1);
784 return isl_schedule_tree_free(tree
);
789 /* Replace the child at position "pos" of "tree" by "child".
791 * If the new child is a leaf, then it is not explicitly
792 * recorded in the list of children. Instead, the list of children
793 * (which is assumed to have only one element) is removed.
794 * Note that the children of set and sequence nodes are always
795 * filters, so they cannot be replaced by empty trees.
797 __isl_give isl_schedule_tree
*isl_schedule_tree_replace_child(
798 __isl_take isl_schedule_tree
*tree
, int pos
,
799 __isl_take isl_schedule_tree
*child
)
801 tree
= isl_schedule_tree_cow(tree
);
805 if (isl_schedule_tree_is_leaf(child
)) {
806 isl_schedule_tree_free(child
);
807 if (!tree
->children
&& pos
== 0)
809 if (isl_schedule_tree_n_children(tree
) != 1)
810 isl_die(isl_schedule_tree_get_ctx(tree
),
812 "can only replace single child by leaf",
814 return isl_schedule_tree_reset_children(tree
);
817 if (!tree
->children
&& pos
== 0)
819 isl_schedule_tree_list_from_schedule_tree(child
);
821 tree
->children
= isl_schedule_tree_list_set_schedule_tree(
822 tree
->children
, pos
, child
);
825 return isl_schedule_tree_free(tree
);
826 tree
= isl_schedule_tree_update_anchored(tree
);
830 isl_schedule_tree_free(tree
);
831 isl_schedule_tree_free(child
);
835 /* Replace the (explicit) children of "tree" by "children"?
837 __isl_give isl_schedule_tree
*isl_schedule_tree_set_children(
838 __isl_take isl_schedule_tree
*tree
,
839 __isl_take isl_schedule_tree_list
*children
)
841 tree
= isl_schedule_tree_cow(tree
);
842 if (!tree
|| !children
)
844 isl_schedule_tree_list_free(tree
->children
);
845 tree
->children
= children
;
848 isl_schedule_tree_free(tree
);
849 isl_schedule_tree_list_free(children
);
853 /* Create a new band schedule tree referring to "band"
854 * with "tree" as single child.
856 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_band(
857 __isl_take isl_schedule_tree
*tree
, __isl_take isl_schedule_band
*band
)
859 isl_schedule_tree
*res
;
861 res
= isl_schedule_tree_from_band(band
);
862 return isl_schedule_tree_replace_child(res
, 0, tree
);
865 /* Create a new context schedule tree with the given context and
866 * with "tree" as single child.
868 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_context(
869 __isl_take isl_schedule_tree
*tree
, __isl_take isl_set
*context
)
871 isl_schedule_tree
*res
;
873 res
= isl_schedule_tree_from_context(context
);
874 return isl_schedule_tree_replace_child(res
, 0, tree
);
877 /* Create a new domain schedule tree with the given domain and
878 * with "tree" as single child.
880 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_domain(
881 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*domain
)
883 isl_schedule_tree
*res
;
885 res
= isl_schedule_tree_from_domain(domain
);
886 return isl_schedule_tree_replace_child(res
, 0, tree
);
889 /* Create a new expansion schedule tree with the given contraction and
890 * expansion and with "tree" as single child.
892 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_expansion(
893 __isl_take isl_schedule_tree
*tree
,
894 __isl_take isl_union_pw_multi_aff
*contraction
,
895 __isl_take isl_union_map
*expansion
)
897 isl_schedule_tree
*res
;
899 res
= isl_schedule_tree_from_expansion(contraction
, expansion
);
900 return isl_schedule_tree_replace_child(res
, 0, tree
);
903 /* Create a new extension schedule tree with the given extension and
904 * with "tree" as single child.
906 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_extension(
907 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_map
*extension
)
909 isl_schedule_tree
*res
;
911 res
= isl_schedule_tree_from_extension(extension
);
912 return isl_schedule_tree_replace_child(res
, 0, tree
);
915 /* Create a new filter schedule tree with the given filter and single child.
917 * If the root of "tree" is itself a filter node, then the two
918 * filter nodes are merged into one node.
920 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_filter(
921 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
)
923 isl_schedule_tree
*res
;
925 if (isl_schedule_tree_get_type(tree
) == isl_schedule_node_filter
) {
926 isl_union_set
*tree_filter
;
928 tree_filter
= isl_schedule_tree_filter_get_filter(tree
);
929 tree_filter
= isl_union_set_intersect(tree_filter
, filter
);
930 tree
= isl_schedule_tree_filter_set_filter(tree
, tree_filter
);
934 res
= isl_schedule_tree_from_filter(filter
);
935 return isl_schedule_tree_replace_child(res
, 0, tree
);
938 /* Insert a filter node with filter set "filter"
939 * in each of the children of "tree".
941 __isl_give isl_schedule_tree
*isl_schedule_tree_children_insert_filter(
942 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
)
946 if (!tree
|| !filter
)
949 n
= isl_schedule_tree_n_children(tree
);
950 for (i
= 0; i
< n
; ++i
) {
951 isl_schedule_tree
*child
;
953 child
= isl_schedule_tree_get_child(tree
, i
);
954 child
= isl_schedule_tree_insert_filter(child
,
955 isl_union_set_copy(filter
));
956 tree
= isl_schedule_tree_replace_child(tree
, i
, child
);
959 isl_union_set_free(filter
);
962 isl_union_set_free(filter
);
963 isl_schedule_tree_free(tree
);
967 /* Create a new guard schedule tree with the given guard and
968 * with "tree" as single child.
970 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_guard(
971 __isl_take isl_schedule_tree
*tree
, __isl_take isl_set
*guard
)
973 isl_schedule_tree
*res
;
975 res
= isl_schedule_tree_from_guard(guard
);
976 return isl_schedule_tree_replace_child(res
, 0, tree
);
979 /* Create a new mark schedule tree with the given mark identifier and
982 __isl_give isl_schedule_tree
*isl_schedule_tree_insert_mark(
983 __isl_take isl_schedule_tree
*tree
, __isl_take isl_id
*mark
)
985 isl_schedule_tree
*res
;
987 res
= isl_schedule_tree_from_mark(mark
);
988 return isl_schedule_tree_replace_child(res
, 0, tree
);
991 /* Return the number of members in the band tree root.
993 unsigned isl_schedule_tree_band_n_member(__isl_keep isl_schedule_tree
*tree
)
998 if (tree
->type
!= isl_schedule_node_band
)
999 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1000 "not a band node", return 0);
1002 return isl_schedule_band_n_member(tree
->band
);
1005 /* Is the band member at position "pos" of the band tree root
1006 * marked coincident?
1008 isl_bool
isl_schedule_tree_band_member_get_coincident(
1009 __isl_keep isl_schedule_tree
*tree
, int pos
)
1012 return isl_bool_error
;
1014 if (tree
->type
!= isl_schedule_node_band
)
1015 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1016 "not a band node", return isl_bool_error
);
1018 return isl_schedule_band_member_get_coincident(tree
->band
, pos
);
1021 /* Mark the given band member as being coincident or not
1022 * according to "coincident".
1024 __isl_give isl_schedule_tree
*isl_schedule_tree_band_member_set_coincident(
1025 __isl_take isl_schedule_tree
*tree
, int pos
, int coincident
)
1029 if (tree
->type
!= isl_schedule_node_band
)
1030 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1031 "not a band node", return isl_schedule_tree_free(tree
));
1032 if (isl_schedule_tree_band_member_get_coincident(tree
, pos
) ==
1035 tree
= isl_schedule_tree_cow(tree
);
1039 tree
->band
= isl_schedule_band_member_set_coincident(tree
->band
, pos
,
1042 return isl_schedule_tree_free(tree
);
1046 /* Is the band tree root marked permutable?
1048 isl_bool
isl_schedule_tree_band_get_permutable(
1049 __isl_keep isl_schedule_tree
*tree
)
1052 return isl_bool_error
;
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 isl_bool_error
);
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 isl_stat
set_range_dim(__isl_take isl_map
*map
, void *user
)
1511 *dim
= isl_map_dim(map
, isl_dim_out
);
1514 return isl_stat_error
;
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
:
1599 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1600 "unhandled case", return 0);
1603 /* Move down to the first descendant of "tree" that contains any schedule
1604 * information or return "leaf" if there is no such descendant.
1606 __isl_give isl_schedule_tree
*isl_schedule_tree_first_schedule_descendant(
1607 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_tree
*leaf
)
1609 while (domain_less(tree
)) {
1610 if (!isl_schedule_tree_has_children(tree
)) {
1611 isl_schedule_tree_free(tree
);
1612 return isl_schedule_tree_copy(leaf
);
1614 tree
= isl_schedule_tree_child(tree
, 0);
1620 static __isl_give isl_union_map
*subtree_schedule_extend(
1621 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
);
1623 /* Extend the schedule map "outer" with the subtree schedule
1624 * of the (single) child of "tree", if any.
1626 * If "tree" does not have any descendants (apart from those that
1627 * do not carry any schedule information), then we simply return "outer".
1628 * Otherwise, we extend the schedule map "outer" with the subtree schedule
1629 * of the single child.
1631 static __isl_give isl_union_map
*subtree_schedule_extend_child(
1632 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
1634 isl_schedule_tree
*child
;
1638 return isl_union_map_free(outer
);
1639 if (!isl_schedule_tree_has_children(tree
))
1641 child
= isl_schedule_tree_get_child(tree
, 0);
1643 return isl_union_map_free(outer
);
1644 res
= subtree_schedule_extend(child
, outer
);
1645 isl_schedule_tree_free(child
);
1649 /* Extract the parameter space from one of the children of "tree",
1650 * which are assumed to be filters.
1652 static __isl_give isl_space
*extract_space_from_filter_child(
1653 __isl_keep isl_schedule_tree
*tree
)
1657 isl_schedule_tree
*child
;
1659 child
= isl_schedule_tree_list_get_schedule_tree(tree
->children
, 0);
1660 dom
= isl_schedule_tree_filter_get_filter(child
);
1661 space
= isl_union_set_get_space(dom
);
1662 isl_union_set_free(dom
);
1663 isl_schedule_tree_free(child
);
1668 /* Extend the schedule map "outer" with the subtree schedule
1669 * of a set or sequence node.
1671 * The schedule for the set or sequence node itself is composed of
1672 * pieces of the form
1680 * The first form is used if there is only a single child or
1681 * if the current node is a set node and the schedule_separate_components
1682 * option is not set.
1684 * Each of the pieces above is extended with the subtree schedule of
1685 * the child of the corresponding filter, if any, padded with zeros
1686 * to ensure that all pieces have the same range dimension.
1688 static __isl_give isl_union_map
*subtree_schedule_extend_from_children(
1689 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
1698 isl_union_map
*umap
;
1703 ctx
= isl_schedule_tree_get_ctx(tree
);
1704 if (!tree
->children
)
1705 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1706 "missing children", return NULL
);
1707 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
1709 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1710 "missing children", return NULL
);
1712 separate
= n
> 1 && (tree
->type
== isl_schedule_node_sequence
||
1713 isl_options_get_schedule_separate_components(ctx
));
1715 space
= extract_space_from_filter_child(tree
);
1717 umap
= isl_union_map_empty(isl_space_copy(space
));
1718 space
= isl_space_set_from_params(space
);
1720 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1721 v
= isl_val_zero(ctx
);
1723 mv
= isl_multi_val_zero(space
);
1725 dim
= isl_multi_val_dim(mv
, isl_dim_set
);
1726 for (i
= 0; i
< n
; ++i
) {
1727 isl_union_pw_multi_aff
*upma
;
1728 isl_union_map
*umap_i
;
1730 isl_schedule_tree
*child
;
1734 child
= isl_schedule_tree_list_get_schedule_tree(
1736 dom
= isl_schedule_tree_filter_get_filter(child
);
1739 mv
= isl_multi_val_set_val(mv
, 0, isl_val_copy(v
));
1740 v
= isl_val_add_ui(v
, 1);
1742 upma
= isl_union_pw_multi_aff_multi_val_on_domain(dom
,
1743 isl_multi_val_copy(mv
));
1744 umap_i
= isl_union_map_from_union_pw_multi_aff(upma
);
1745 umap_i
= isl_union_map_flat_range_product(
1746 isl_union_map_copy(outer
), umap_i
);
1747 umap_i
= subtree_schedule_extend_child(child
, umap_i
);
1748 isl_schedule_tree_free(child
);
1750 empty
= isl_union_map_is_empty(umap_i
);
1752 umap_i
= isl_union_map_free(umap_i
);
1754 isl_union_map_free(umap_i
);
1758 dim_i
= range_dim(umap_i
);
1760 umap
= isl_union_map_free(umap
);
1761 } else if (dim
< dim_i
) {
1762 umap
= append_range(umap
, dim_i
- dim
);
1764 } else if (dim_i
< dim
) {
1765 umap_i
= append_range(umap_i
, dim
- dim_i
);
1767 umap
= isl_union_map_union(umap
, umap_i
);
1771 isl_multi_val_free(mv
);
1772 isl_union_map_free(outer
);
1777 /* Extend the schedule map "outer" with the subtree schedule of "tree".
1779 * If the root of the tree is a set or a sequence, then we extend
1780 * the schedule map in subtree_schedule_extend_from_children.
1781 * Otherwise, we extend the schedule map with the partial schedule
1782 * corresponding to the root of the tree and then continue with
1783 * the single child of this root.
1784 * In the special case of an expansion, the schedule map is "extended"
1785 * by applying the expansion to the domain of the schedule map.
1787 static __isl_give isl_union_map
*subtree_schedule_extend(
1788 __isl_keep isl_schedule_tree
*tree
, __isl_take isl_union_map
*outer
)
1790 isl_multi_union_pw_aff
*mupa
;
1791 isl_union_map
*umap
;
1792 isl_union_set
*domain
;
1797 switch (tree
->type
) {
1798 case isl_schedule_node_error
:
1799 return isl_union_map_free(outer
);
1800 case isl_schedule_node_extension
:
1801 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1802 "cannot construct subtree schedule of tree "
1803 "with extension nodes",
1804 return isl_union_map_free(outer
));
1805 case isl_schedule_node_context
:
1806 case isl_schedule_node_guard
:
1807 case isl_schedule_node_mark
:
1808 return subtree_schedule_extend_child(tree
, outer
);
1809 case isl_schedule_node_band
:
1810 if (isl_schedule_tree_band_n_member(tree
) == 0)
1811 return subtree_schedule_extend_child(tree
, outer
);
1812 mupa
= isl_schedule_band_get_partial_schedule(tree
->band
);
1813 umap
= isl_union_map_from_multi_union_pw_aff(mupa
);
1814 outer
= isl_union_map_flat_range_product(outer
, umap
);
1815 umap
= subtree_schedule_extend_child(tree
, outer
);
1817 case isl_schedule_node_domain
:
1818 domain
= isl_schedule_tree_domain_get_domain(tree
);
1819 umap
= isl_union_map_from_domain(domain
);
1820 outer
= isl_union_map_flat_range_product(outer
, umap
);
1821 umap
= subtree_schedule_extend_child(tree
, outer
);
1823 case isl_schedule_node_expansion
:
1824 umap
= isl_schedule_tree_expansion_get_expansion(tree
);
1825 outer
= isl_union_map_apply_domain(outer
, umap
);
1826 umap
= subtree_schedule_extend_child(tree
, outer
);
1828 case isl_schedule_node_filter
:
1829 domain
= isl_schedule_tree_filter_get_filter(tree
);
1830 umap
= isl_union_map_from_domain(domain
);
1831 outer
= isl_union_map_flat_range_product(outer
, umap
);
1832 umap
= subtree_schedule_extend_child(tree
, outer
);
1834 case isl_schedule_node_leaf
:
1835 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1836 "leaf node should be handled by caller", return NULL
);
1837 case isl_schedule_node_set
:
1838 case isl_schedule_node_sequence
:
1839 umap
= subtree_schedule_extend_from_children(tree
, outer
);
1846 static __isl_give isl_union_set
*initial_domain(
1847 __isl_keep isl_schedule_tree
*tree
);
1849 /* Extract a universe domain from the children of the tree root "tree",
1850 * which is a set or sequence, meaning that its children are filters.
1851 * In particular, return the union of the universes of the filters.
1853 static __isl_give isl_union_set
*initial_domain_from_children(
1854 __isl_keep isl_schedule_tree
*tree
)
1858 isl_union_set
*domain
;
1860 if (!tree
->children
)
1861 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1862 "missing children", return NULL
);
1863 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
1865 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1866 "missing children", return NULL
);
1868 space
= extract_space_from_filter_child(tree
);
1869 domain
= isl_union_set_empty(space
);
1871 for (i
= 0; i
< n
; ++i
) {
1872 isl_schedule_tree
*child
;
1873 isl_union_set
*domain_i
;
1875 child
= isl_schedule_tree_get_child(tree
, i
);
1876 domain_i
= initial_domain(child
);
1877 domain
= isl_union_set_union(domain
, domain_i
);
1878 isl_schedule_tree_free(child
);
1884 /* Extract a universe domain from the tree root "tree".
1885 * The caller is responsible for making sure that this node
1886 * would not be skipped by isl_schedule_tree_first_schedule_descendant
1887 * and that it is not a leaf node.
1889 static __isl_give isl_union_set
*initial_domain(
1890 __isl_keep isl_schedule_tree
*tree
)
1892 isl_multi_union_pw_aff
*mupa
;
1893 isl_union_set
*domain
;
1899 switch (tree
->type
) {
1900 case isl_schedule_node_error
:
1902 case isl_schedule_node_context
:
1903 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1904 "context node should be handled by caller",
1906 case isl_schedule_node_guard
:
1907 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1908 "guard node should be handled by caller",
1910 case isl_schedule_node_mark
:
1911 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1912 "mark node should be handled by caller",
1914 case isl_schedule_node_extension
:
1915 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1916 "cannot construct subtree schedule of tree "
1917 "with extension nodes", return NULL
);
1918 case isl_schedule_node_band
:
1919 if (isl_schedule_tree_band_n_member(tree
) == 0)
1920 isl_die(isl_schedule_tree_get_ctx(tree
),
1922 "0D band should be handled by caller",
1924 mupa
= isl_schedule_band_get_partial_schedule(tree
->band
);
1925 domain
= isl_multi_union_pw_aff_domain(mupa
);
1926 domain
= isl_union_set_universe(domain
);
1928 case isl_schedule_node_domain
:
1929 domain
= isl_schedule_tree_domain_get_domain(tree
);
1930 domain
= isl_union_set_universe(domain
);
1932 case isl_schedule_node_expansion
:
1933 exp
= isl_schedule_tree_expansion_get_expansion(tree
);
1934 exp
= isl_union_map_universe(exp
);
1935 domain
= isl_union_map_domain(exp
);
1937 case isl_schedule_node_filter
:
1938 domain
= isl_schedule_tree_filter_get_filter(tree
);
1939 domain
= isl_union_set_universe(domain
);
1941 case isl_schedule_node_leaf
:
1942 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
1943 "leaf node should be handled by caller", return NULL
);
1944 case isl_schedule_node_set
:
1945 case isl_schedule_node_sequence
:
1946 domain
= initial_domain_from_children(tree
);
1953 /* Return the subtree schedule of a node that contains some schedule
1954 * information, i.e., a node that would not be skipped by
1955 * isl_schedule_tree_first_schedule_descendant and that is not a leaf.
1957 * If the tree contains any expansions, then the returned subtree
1958 * schedule is formulated in terms of the expanded domains.
1959 * The tree is not allowed to contain any extension nodes.
1961 * We start with an initial zero-dimensional subtree schedule based
1962 * on the domain information in the root node and then extend it
1963 * based on the schedule information in the root node and its descendants.
1965 __isl_give isl_union_map
*isl_schedule_tree_get_subtree_schedule_union_map(
1966 __isl_keep isl_schedule_tree
*tree
)
1968 isl_union_set
*domain
;
1969 isl_union_map
*umap
;
1971 domain
= initial_domain(tree
);
1972 umap
= isl_union_map_from_domain(domain
);
1973 return subtree_schedule_extend(tree
, umap
);
1976 /* Multiply the partial schedule of the band root node of "tree"
1977 * with the factors in "mv".
1979 __isl_give isl_schedule_tree
*isl_schedule_tree_band_scale(
1980 __isl_take isl_schedule_tree
*tree
, __isl_take isl_multi_val
*mv
)
1984 if (tree
->type
!= isl_schedule_node_band
)
1985 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
1986 "not a band node", goto error
);
1988 tree
= isl_schedule_tree_cow(tree
);
1992 tree
->band
= isl_schedule_band_scale(tree
->band
, mv
);
1994 return isl_schedule_tree_free(tree
);
1998 isl_schedule_tree_free(tree
);
1999 isl_multi_val_free(mv
);
2003 /* Divide the partial schedule of the band root node of "tree"
2004 * by the factors in "mv".
2006 __isl_give isl_schedule_tree
*isl_schedule_tree_band_scale_down(
2007 __isl_take isl_schedule_tree
*tree
, __isl_take isl_multi_val
*mv
)
2011 if (tree
->type
!= isl_schedule_node_band
)
2012 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2013 "not a band node", goto error
);
2015 tree
= isl_schedule_tree_cow(tree
);
2019 tree
->band
= isl_schedule_band_scale_down(tree
->band
, mv
);
2021 return isl_schedule_tree_free(tree
);
2025 isl_schedule_tree_free(tree
);
2026 isl_multi_val_free(mv
);
2030 /* Reduce the partial schedule of the band root node of "tree"
2031 * modulo the factors in "mv".
2033 __isl_give isl_schedule_tree
*isl_schedule_tree_band_mod(
2034 __isl_take isl_schedule_tree
*tree
, __isl_take isl_multi_val
*mv
)
2038 if (tree
->type
!= isl_schedule_node_band
)
2039 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2040 "not a band node", goto error
);
2042 tree
= isl_schedule_tree_cow(tree
);
2046 tree
->band
= isl_schedule_band_mod(tree
->band
, mv
);
2048 return isl_schedule_tree_free(tree
);
2052 isl_schedule_tree_free(tree
);
2053 isl_multi_val_free(mv
);
2057 /* Shift the partial schedule of the band root node of "tree" by "shift".
2059 __isl_give isl_schedule_tree
*isl_schedule_tree_band_shift(
2060 __isl_take isl_schedule_tree
*tree
,
2061 __isl_take isl_multi_union_pw_aff
*shift
)
2063 if (!tree
|| !shift
)
2065 if (tree
->type
!= isl_schedule_node_band
)
2066 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2067 "not a band node", goto error
);
2069 tree
= isl_schedule_tree_cow(tree
);
2073 tree
->band
= isl_schedule_band_shift(tree
->band
, shift
);
2075 return isl_schedule_tree_free(tree
);
2079 isl_schedule_tree_free(tree
);
2080 isl_multi_union_pw_aff_free(shift
);
2084 /* Given two trees with sequence roots, replace the child at position
2085 * "pos" of "tree" with the children of "child".
2087 __isl_give isl_schedule_tree
*isl_schedule_tree_sequence_splice(
2088 __isl_take isl_schedule_tree
*tree
, int pos
,
2089 __isl_take isl_schedule_tree
*child
)
2092 isl_schedule_tree_list
*list1
, *list2
;
2094 tree
= isl_schedule_tree_cow(tree
);
2095 if (!tree
|| !child
)
2097 if (isl_schedule_tree_get_type(tree
) != isl_schedule_node_sequence
)
2098 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2099 "not a sequence node", goto error
);
2100 n
= isl_schedule_tree_n_children(tree
);
2101 if (pos
< 0 || pos
>= n
)
2102 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2103 "position out of bounds", goto error
);
2104 if (isl_schedule_tree_get_type(child
) != isl_schedule_node_sequence
)
2105 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2106 "not a sequence node", goto error
);
2108 list1
= isl_schedule_tree_list_copy(tree
->children
);
2109 list1
= isl_schedule_tree_list_drop(list1
, pos
, n
- pos
);
2110 list2
= isl_schedule_tree_list_copy(tree
->children
);
2111 list2
= isl_schedule_tree_list_drop(list2
, 0, pos
+ 1);
2112 list1
= isl_schedule_tree_list_concat(list1
,
2113 isl_schedule_tree_list_copy(child
->children
));
2114 list1
= isl_schedule_tree_list_concat(list1
, list2
);
2116 isl_schedule_tree_free(tree
);
2117 isl_schedule_tree_free(child
);
2118 return isl_schedule_tree_from_children(isl_schedule_node_sequence
,
2121 isl_schedule_tree_free(tree
);
2122 isl_schedule_tree_free(child
);
2126 /* Tile the band root node of "tree" with tile sizes "sizes".
2128 * We duplicate the band node, change the schedule of one of them
2129 * to the tile schedule and the other to the point schedule and then
2130 * attach the point band as a child to the tile band.
2132 __isl_give isl_schedule_tree
*isl_schedule_tree_band_tile(
2133 __isl_take isl_schedule_tree
*tree
, __isl_take isl_multi_val
*sizes
)
2135 isl_schedule_tree
*child
= NULL
;
2137 if (!tree
|| !sizes
)
2139 if (tree
->type
!= isl_schedule_node_band
)
2140 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2141 "not a band node", goto error
);
2143 child
= isl_schedule_tree_copy(tree
);
2144 tree
= isl_schedule_tree_cow(tree
);
2145 child
= isl_schedule_tree_cow(child
);
2146 if (!tree
|| !child
)
2149 tree
->band
= isl_schedule_band_tile(tree
->band
,
2150 isl_multi_val_copy(sizes
));
2153 child
->band
= isl_schedule_band_point(child
->band
, tree
->band
, sizes
);
2155 child
= isl_schedule_tree_free(child
);
2157 tree
= isl_schedule_tree_replace_child(tree
, 0, child
);
2161 isl_schedule_tree_free(child
);
2162 isl_schedule_tree_free(tree
);
2163 isl_multi_val_free(sizes
);
2167 /* Split the band root node of "tree" into two nested band nodes,
2168 * one with the first "pos" dimensions and
2169 * one with the remaining dimensions.
2171 __isl_give isl_schedule_tree
*isl_schedule_tree_band_split(
2172 __isl_take isl_schedule_tree
*tree
, int pos
)
2175 isl_schedule_tree
*child
;
2179 if (tree
->type
!= isl_schedule_node_band
)
2180 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2181 "not a band node", return isl_schedule_tree_free(tree
));
2183 n
= isl_schedule_tree_band_n_member(tree
);
2184 if (pos
< 0 || pos
> n
)
2185 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2186 "position out of bounds",
2187 return isl_schedule_tree_free(tree
));
2189 child
= isl_schedule_tree_copy(tree
);
2190 tree
= isl_schedule_tree_cow(tree
);
2191 child
= isl_schedule_tree_cow(child
);
2192 if (!tree
|| !child
)
2195 child
->band
= isl_schedule_band_drop(child
->band
, 0, pos
);
2196 tree
->band
= isl_schedule_band_drop(tree
->band
, pos
, n
- pos
);
2197 if (!child
->band
|| !tree
->band
)
2200 tree
= isl_schedule_tree_replace_child(tree
, 0, child
);
2204 isl_schedule_tree_free(child
);
2205 isl_schedule_tree_free(tree
);
2209 /* Attach "tree2" at each of the leaves of "tree1".
2211 * If "tree1" does not have any explicit children, then make "tree2"
2212 * its single child. Otherwise, attach "tree2" to the leaves of
2213 * each of the children of "tree1".
2215 __isl_give isl_schedule_tree
*isl_schedule_tree_append_to_leaves(
2216 __isl_take isl_schedule_tree
*tree1
,
2217 __isl_take isl_schedule_tree
*tree2
)
2221 if (!tree1
|| !tree2
)
2223 n
= isl_schedule_tree_n_children(tree1
);
2225 isl_schedule_tree_list
*list
;
2226 list
= isl_schedule_tree_list_from_schedule_tree(tree2
);
2227 tree1
= isl_schedule_tree_set_children(tree1
, list
);
2230 for (i
= 0; i
< n
; ++i
) {
2231 isl_schedule_tree
*child
;
2233 child
= isl_schedule_tree_get_child(tree1
, i
);
2234 child
= isl_schedule_tree_append_to_leaves(child
,
2235 isl_schedule_tree_copy(tree2
));
2236 tree1
= isl_schedule_tree_replace_child(tree1
, i
, child
);
2239 isl_schedule_tree_free(tree2
);
2242 isl_schedule_tree_free(tree1
);
2243 isl_schedule_tree_free(tree2
);
2247 /* Reset the user pointer on all identifiers of parameters and tuples
2248 * in the root of "tree".
2250 __isl_give isl_schedule_tree
*isl_schedule_tree_reset_user(
2251 __isl_take isl_schedule_tree
*tree
)
2253 if (isl_schedule_tree_is_leaf(tree
))
2256 tree
= isl_schedule_tree_cow(tree
);
2260 switch (tree
->type
) {
2261 case isl_schedule_node_error
:
2262 return isl_schedule_tree_free(tree
);
2263 case isl_schedule_node_band
:
2264 tree
->band
= isl_schedule_band_reset_user(tree
->band
);
2266 return isl_schedule_tree_free(tree
);
2268 case isl_schedule_node_context
:
2269 tree
->context
= isl_set_reset_user(tree
->context
);
2271 return isl_schedule_tree_free(tree
);
2273 case isl_schedule_node_domain
:
2274 tree
->domain
= isl_union_set_reset_user(tree
->domain
);
2276 return isl_schedule_tree_free(tree
);
2278 case isl_schedule_node_expansion
:
2280 isl_union_pw_multi_aff_reset_user(tree
->contraction
);
2281 tree
->expansion
= isl_union_map_reset_user(tree
->expansion
);
2282 if (!tree
->contraction
|| !tree
->expansion
)
2283 return isl_schedule_tree_free(tree
);
2285 case isl_schedule_node_extension
:
2286 tree
->extension
= isl_union_map_reset_user(tree
->extension
);
2287 if (!tree
->extension
)
2288 return isl_schedule_tree_free(tree
);
2290 case isl_schedule_node_filter
:
2291 tree
->filter
= isl_union_set_reset_user(tree
->filter
);
2293 return isl_schedule_tree_free(tree
);
2295 case isl_schedule_node_guard
:
2296 tree
->guard
= isl_set_reset_user(tree
->guard
);
2298 return isl_schedule_tree_free(tree
);
2300 case isl_schedule_node_leaf
:
2301 case isl_schedule_node_mark
:
2302 case isl_schedule_node_sequence
:
2303 case isl_schedule_node_set
:
2310 /* Align the parameters of the root of "tree" to those of "space".
2312 __isl_give isl_schedule_tree
*isl_schedule_tree_align_params(
2313 __isl_take isl_schedule_tree
*tree
, __isl_take isl_space
*space
)
2318 if (isl_schedule_tree_is_leaf(tree
)) {
2319 isl_space_free(space
);
2323 tree
= isl_schedule_tree_cow(tree
);
2327 switch (tree
->type
) {
2328 case isl_schedule_node_error
:
2330 case isl_schedule_node_band
:
2331 tree
->band
= isl_schedule_band_align_params(tree
->band
, space
);
2333 return isl_schedule_tree_free(tree
);
2335 case isl_schedule_node_context
:
2336 tree
->context
= isl_set_align_params(tree
->context
, space
);
2338 return isl_schedule_tree_free(tree
);
2340 case isl_schedule_node_domain
:
2341 tree
->domain
= isl_union_set_align_params(tree
->domain
, space
);
2343 return isl_schedule_tree_free(tree
);
2345 case isl_schedule_node_expansion
:
2347 isl_union_pw_multi_aff_align_params(tree
->contraction
,
2348 isl_space_copy(space
));
2349 tree
->expansion
= isl_union_map_align_params(tree
->expansion
,
2351 if (!tree
->contraction
|| !tree
->expansion
)
2352 return isl_schedule_tree_free(tree
);
2354 case isl_schedule_node_extension
:
2355 tree
->extension
= isl_union_map_align_params(tree
->extension
,
2357 if (!tree
->extension
)
2358 return isl_schedule_tree_free(tree
);
2360 case isl_schedule_node_filter
:
2361 tree
->filter
= isl_union_set_align_params(tree
->filter
, space
);
2363 return isl_schedule_tree_free(tree
);
2365 case isl_schedule_node_guard
:
2366 tree
->guard
= isl_set_align_params(tree
->guard
, space
);
2368 return isl_schedule_tree_free(tree
);
2370 case isl_schedule_node_leaf
:
2371 case isl_schedule_node_mark
:
2372 case isl_schedule_node_sequence
:
2373 case isl_schedule_node_set
:
2374 isl_space_free(space
);
2380 isl_space_free(space
);
2381 isl_schedule_tree_free(tree
);
2385 /* Does "tree" involve the iteration domain?
2386 * That is, does it need to be modified
2387 * by isl_schedule_tree_pullback_union_pw_multi_aff?
2389 static int involves_iteration_domain(__isl_keep isl_schedule_tree
*tree
)
2394 switch (tree
->type
) {
2395 case isl_schedule_node_error
:
2397 case isl_schedule_node_band
:
2398 case isl_schedule_node_domain
:
2399 case isl_schedule_node_expansion
:
2400 case isl_schedule_node_extension
:
2401 case isl_schedule_node_filter
:
2403 case isl_schedule_node_context
:
2404 case isl_schedule_node_leaf
:
2405 case isl_schedule_node_guard
:
2406 case isl_schedule_node_mark
:
2407 case isl_schedule_node_sequence
:
2408 case isl_schedule_node_set
:
2412 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
2413 "unhandled case", return -1);
2416 /* Compute the pullback of the root node of "tree" by the function
2417 * represented by "upma".
2418 * In other words, plug in "upma" in the iteration domains of
2419 * the root node of "tree".
2420 * We currently do not handle expansion nodes.
2422 * We first check if the root node involves any iteration domains.
2423 * If so, we handle the specific cases.
2425 __isl_give isl_schedule_tree
*isl_schedule_tree_pullback_union_pw_multi_aff(
2426 __isl_take isl_schedule_tree
*tree
,
2427 __isl_take isl_union_pw_multi_aff
*upma
)
2434 involves
= involves_iteration_domain(tree
);
2438 isl_union_pw_multi_aff_free(upma
);
2442 tree
= isl_schedule_tree_cow(tree
);
2446 if (tree
->type
== isl_schedule_node_band
) {
2447 tree
->band
= isl_schedule_band_pullback_union_pw_multi_aff(
2450 return isl_schedule_tree_free(tree
);
2451 } else if (tree
->type
== isl_schedule_node_domain
) {
2453 isl_union_set_preimage_union_pw_multi_aff(tree
->domain
,
2456 return isl_schedule_tree_free(tree
);
2457 } else if (tree
->type
== isl_schedule_node_expansion
) {
2458 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_unsupported
,
2459 "cannot pullback expansion node", goto error
);
2460 } else if (tree
->type
== isl_schedule_node_extension
) {
2462 isl_union_map_preimage_range_union_pw_multi_aff(
2463 tree
->extension
, upma
);
2464 if (!tree
->extension
)
2465 return isl_schedule_tree_free(tree
);
2466 } else if (tree
->type
== isl_schedule_node_filter
) {
2468 isl_union_set_preimage_union_pw_multi_aff(tree
->filter
,
2471 return isl_schedule_tree_free(tree
);
2476 isl_union_pw_multi_aff_free(upma
);
2477 isl_schedule_tree_free(tree
);
2481 /* Compute the gist of the band tree root with respect to "context".
2483 __isl_give isl_schedule_tree
*isl_schedule_tree_band_gist(
2484 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*context
)
2488 if (tree
->type
!= isl_schedule_node_band
)
2489 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
2490 "not a band node", goto error
);
2491 tree
= isl_schedule_tree_cow(tree
);
2495 tree
->band
= isl_schedule_band_gist(tree
->band
, context
);
2497 return isl_schedule_tree_free(tree
);
2500 isl_union_set_free(context
);
2501 isl_schedule_tree_free(tree
);
2505 /* Are any members in "band" marked coincident?
2507 static int any_coincident(__isl_keep isl_schedule_band
*band
)
2511 n
= isl_schedule_band_n_member(band
);
2512 for (i
= 0; i
< n
; ++i
)
2513 if (isl_schedule_band_member_get_coincident(band
, i
))
2519 /* Print the band node "band" to "p".
2521 * The permutable and coincident properties are only printed if they
2522 * are different from the defaults.
2523 * The coincident property is always printed in YAML flow style.
2525 static __isl_give isl_printer
*print_tree_band(__isl_take isl_printer
*p
,
2526 __isl_keep isl_schedule_band
*band
)
2528 isl_union_set
*options
;
2531 p
= isl_printer_print_str(p
, "schedule");
2532 p
= isl_printer_yaml_next(p
);
2533 p
= isl_printer_print_str(p
, "\"");
2534 p
= isl_printer_print_multi_union_pw_aff(p
, band
->mupa
);
2535 p
= isl_printer_print_str(p
, "\"");
2536 if (isl_schedule_band_get_permutable(band
)) {
2537 p
= isl_printer_yaml_next(p
);
2538 p
= isl_printer_print_str(p
, "permutable");
2539 p
= isl_printer_yaml_next(p
);
2540 p
= isl_printer_print_int(p
, 1);
2542 if (any_coincident(band
)) {
2546 p
= isl_printer_yaml_next(p
);
2547 p
= isl_printer_print_str(p
, "coincident");
2548 p
= isl_printer_yaml_next(p
);
2549 style
= isl_printer_get_yaml_style(p
);
2550 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2551 p
= isl_printer_yaml_start_sequence(p
);
2552 n
= isl_schedule_band_n_member(band
);
2553 for (i
= 0; i
< n
; ++i
) {
2554 p
= isl_printer_print_int(p
,
2555 isl_schedule_band_member_get_coincident(band
, i
));
2556 p
= isl_printer_yaml_next(p
);
2558 p
= isl_printer_yaml_end_sequence(p
);
2559 p
= isl_printer_set_yaml_style(p
, style
);
2561 options
= isl_schedule_band_get_ast_build_options(band
);
2562 empty
= isl_union_set_is_empty(options
);
2564 p
= isl_printer_free(p
);
2566 p
= isl_printer_yaml_next(p
);
2567 p
= isl_printer_print_str(p
, "options");
2568 p
= isl_printer_yaml_next(p
);
2569 p
= isl_printer_print_str(p
, "\"");
2570 p
= isl_printer_print_union_set(p
, options
);
2571 p
= isl_printer_print_str(p
, "\"");
2573 isl_union_set_free(options
);
2578 /* Print "tree" to "p".
2580 * If "n_ancestor" is non-negative, then "child_pos" contains the child
2581 * positions of a descendant of the current node that should be marked
2582 * (by the comment "YOU ARE HERE"). In particular, if "n_ancestor"
2583 * is zero, then the current node should be marked.
2584 * The marking is only printed in YAML block format.
2586 * Implicit leaf nodes are not printed, except if they correspond
2587 * to the node that should be marked.
2589 __isl_give isl_printer
*isl_printer_print_schedule_tree_mark(
2590 __isl_take isl_printer
*p
, __isl_keep isl_schedule_tree
*tree
,
2591 int n_ancestor
, int *child_pos
)
2597 block
= isl_printer_get_yaml_style(p
) == ISL_YAML_STYLE_BLOCK
;
2599 p
= isl_printer_yaml_start_mapping(p
);
2600 if (n_ancestor
== 0 && block
) {
2601 p
= isl_printer_print_str(p
, "# YOU ARE HERE");
2602 p
= isl_printer_end_line(p
);
2603 p
= isl_printer_start_line(p
);
2605 switch (tree
->type
) {
2606 case isl_schedule_node_error
:
2607 p
= isl_printer_print_str(p
, "ERROR");
2609 case isl_schedule_node_leaf
:
2610 p
= isl_printer_print_str(p
, "leaf");
2612 case isl_schedule_node_sequence
:
2613 p
= isl_printer_print_str(p
, "sequence");
2616 case isl_schedule_node_set
:
2617 p
= isl_printer_print_str(p
, "set");
2620 case isl_schedule_node_context
:
2621 p
= isl_printer_print_str(p
, "context");
2622 p
= isl_printer_yaml_next(p
);
2623 p
= isl_printer_print_str(p
, "\"");
2624 p
= isl_printer_print_set(p
, tree
->context
);
2625 p
= isl_printer_print_str(p
, "\"");
2627 case isl_schedule_node_domain
:
2628 p
= isl_printer_print_str(p
, "domain");
2629 p
= isl_printer_yaml_next(p
);
2630 p
= isl_printer_print_str(p
, "\"");
2631 p
= isl_printer_print_union_set(p
, tree
->domain
);
2632 p
= isl_printer_print_str(p
, "\"");
2634 case isl_schedule_node_expansion
:
2635 p
= isl_printer_print_str(p
, "contraction");
2636 p
= isl_printer_yaml_next(p
);
2637 p
= isl_printer_print_str(p
, "\"");
2638 p
= isl_printer_print_union_pw_multi_aff(p
, tree
->contraction
);
2639 p
= isl_printer_print_str(p
, "\"");
2640 p
= isl_printer_yaml_next(p
);
2641 p
= isl_printer_print_str(p
, "expansion");
2642 p
= isl_printer_yaml_next(p
);
2643 p
= isl_printer_print_str(p
, "\"");
2644 p
= isl_printer_print_union_map(p
, tree
->expansion
);
2645 p
= isl_printer_print_str(p
, "\"");
2647 case isl_schedule_node_extension
:
2648 p
= isl_printer_print_str(p
, "extension");
2649 p
= isl_printer_yaml_next(p
);
2650 p
= isl_printer_print_str(p
, "\"");
2651 p
= isl_printer_print_union_map(p
, tree
->extension
);
2652 p
= isl_printer_print_str(p
, "\"");
2654 case isl_schedule_node_filter
:
2655 p
= isl_printer_print_str(p
, "filter");
2656 p
= isl_printer_yaml_next(p
);
2657 p
= isl_printer_print_str(p
, "\"");
2658 p
= isl_printer_print_union_set(p
, tree
->filter
);
2659 p
= isl_printer_print_str(p
, "\"");
2661 case isl_schedule_node_guard
:
2662 p
= isl_printer_print_str(p
, "guard");
2663 p
= isl_printer_yaml_next(p
);
2664 p
= isl_printer_print_str(p
, "\"");
2665 p
= isl_printer_print_set(p
, tree
->guard
);
2666 p
= isl_printer_print_str(p
, "\"");
2668 case isl_schedule_node_mark
:
2669 p
= isl_printer_print_str(p
, "mark");
2670 p
= isl_printer_yaml_next(p
);
2671 p
= isl_printer_print_str(p
, "\"");
2672 p
= isl_printer_print_str(p
, isl_id_get_name(tree
->mark
));
2673 p
= isl_printer_print_str(p
, "\"");
2675 case isl_schedule_node_band
:
2676 p
= print_tree_band(p
, tree
->band
);
2679 p
= isl_printer_yaml_next(p
);
2681 if (!tree
->children
) {
2682 if (n_ancestor
> 0 && block
) {
2683 isl_schedule_tree
*leaf
;
2685 p
= isl_printer_print_str(p
, "child");
2686 p
= isl_printer_yaml_next(p
);
2687 leaf
= isl_schedule_tree_leaf(isl_printer_get_ctx(p
));
2688 p
= isl_printer_print_schedule_tree_mark(p
,
2690 isl_schedule_tree_free(leaf
);
2691 p
= isl_printer_yaml_next(p
);
2693 return isl_printer_yaml_end_mapping(p
);
2697 p
= isl_printer_yaml_start_sequence(p
);
2699 p
= isl_printer_print_str(p
, "child");
2700 p
= isl_printer_yaml_next(p
);
2703 n
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
2704 for (i
= 0; i
< n
; ++i
) {
2705 isl_schedule_tree
*t
;
2707 t
= isl_schedule_tree_get_child(tree
, i
);
2708 if (n_ancestor
> 0 && child_pos
[0] == i
)
2709 p
= isl_printer_print_schedule_tree_mark(p
, t
,
2710 n_ancestor
- 1, child_pos
+ 1);
2712 p
= isl_printer_print_schedule_tree_mark(p
, t
,
2714 isl_schedule_tree_free(t
);
2716 p
= isl_printer_yaml_next(p
);
2720 p
= isl_printer_yaml_end_sequence(p
);
2721 p
= isl_printer_yaml_end_mapping(p
);
2726 /* Print "tree" to "p".
2728 __isl_give isl_printer
*isl_printer_print_schedule_tree(
2729 __isl_take isl_printer
*p
, __isl_keep isl_schedule_tree
*tree
)
2731 return isl_printer_print_schedule_tree_mark(p
, tree
, -1, NULL
);
2734 void isl_schedule_tree_dump(__isl_keep isl_schedule_tree
*tree
)
2737 isl_printer
*printer
;
2742 ctx
= isl_schedule_tree_get_ctx(tree
);
2743 printer
= isl_printer_to_file(ctx
, stderr
);
2744 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
2745 printer
= isl_printer_print_schedule_tree(printer
, tree
);
2747 isl_printer_free(printer
);