isl_basic_map_remove_redundancies: sort constraints
[isl.git] / isl_schedule_tree.c
blob4a5b67e23583f1b3d5b89a9d321cfe4424f12ce3
1 /*
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
13 #include <isl/map.h>
14 #include <isl_schedule_band.h>
15 #include <isl_schedule_private.h>
17 #undef EL
18 #define EL isl_schedule_tree
20 #include <isl_list_templ.h>
22 #undef BASE
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
36 * the children.
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)
47 return NULL;
49 tree = isl_calloc_type(ctx, isl_schedule_tree);
50 if (!tree)
51 return NULL;
53 tree->ref = 1;
54 tree->ctx = ctx;
55 isl_ctx_ref(ctx);
56 tree->type = type;
57 tree->anchored = 0;
59 return 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)
67 isl_ctx *ctx;
68 isl_schedule_tree *dup;
70 if (!tree)
71 return NULL;
73 ctx = isl_schedule_tree_get_ctx(tree);
74 dup = isl_schedule_tree_alloc(ctx, tree->type);
75 if (!dup)
76 return NULL;
78 switch (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);
85 if (!dup->band)
86 return isl_schedule_tree_free(dup);
87 break;
88 case isl_schedule_node_context:
89 dup->context = isl_set_copy(tree->context);
90 if (!dup->context)
91 return isl_schedule_tree_free(dup);
92 break;
93 case isl_schedule_node_domain:
94 dup->domain = isl_union_set_copy(tree->domain);
95 if (!dup->domain)
96 return isl_schedule_tree_free(dup);
97 break;
98 case isl_schedule_node_expansion:
99 dup->contraction =
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);
104 break;
105 case isl_schedule_node_extension:
106 dup->extension = isl_union_map_copy(tree->extension);
107 if (!dup->extension)
108 return isl_schedule_tree_free(dup);
109 break;
110 case isl_schedule_node_filter:
111 dup->filter = isl_union_set_copy(tree->filter);
112 if (!dup->filter)
113 return isl_schedule_tree_free(dup);
114 break;
115 case isl_schedule_node_guard:
116 dup->guard = isl_set_copy(tree->guard);
117 if (!dup->guard)
118 return isl_schedule_tree_free(dup);
119 break;
120 case isl_schedule_node_mark:
121 dup->mark = isl_id_copy(tree->mark);
122 if (!dup->mark)
123 return isl_schedule_tree_free(dup);
124 break;
125 case isl_schedule_node_leaf:
126 case isl_schedule_node_sequence:
127 case isl_schedule_node_set:
128 break;
131 if (tree->children) {
132 dup->children = isl_schedule_tree_list_copy(tree->children);
133 if (!dup->children)
134 return isl_schedule_tree_free(dup);
136 dup->anchored = tree->anchored;
138 return dup;
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)
147 if (!tree)
148 return NULL;
150 if (tree->ref == 1)
151 return tree;
152 tree->ref--;
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)
161 if (!tree)
162 return NULL;
164 tree->ref++;
165 return tree;
168 /* Free "tree" and return NULL.
170 __isl_null isl_schedule_tree *isl_schedule_tree_free(
171 __isl_take isl_schedule_tree *tree)
173 if (!tree)
174 return NULL;
175 if (--tree->ref > 0)
176 return NULL;
178 switch (tree->type) {
179 case isl_schedule_node_band:
180 isl_schedule_band_free(tree->band);
181 break;
182 case isl_schedule_node_context:
183 isl_set_free(tree->context);
184 break;
185 case isl_schedule_node_domain:
186 isl_union_set_free(tree->domain);
187 break;
188 case isl_schedule_node_expansion:
189 isl_union_pw_multi_aff_free(tree->contraction);
190 isl_union_map_free(tree->expansion);
191 break;
192 case isl_schedule_node_extension:
193 isl_union_map_free(tree->extension);
194 break;
195 case isl_schedule_node_filter:
196 isl_union_set_free(tree->filter);
197 break;
198 case isl_schedule_node_guard:
199 isl_set_free(tree->guard);
200 break;
201 case isl_schedule_node_mark:
202 isl_id_free(tree->mark);
203 break;
204 case isl_schedule_node_sequence:
205 case isl_schedule_node_set:
206 case isl_schedule_node_error:
207 case isl_schedule_node_leaf:
208 break;
210 isl_schedule_tree_list_free(tree->children);
211 isl_ctx_deref(tree->ctx);
212 free(tree);
214 return NULL;
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"
225 * with no children.
227 __isl_give isl_schedule_tree *isl_schedule_tree_from_band(
228 __isl_take isl_schedule_band *band)
230 isl_ctx *ctx;
231 isl_schedule_tree *tree;
233 if (!band)
234 return NULL;
236 ctx = isl_schedule_band_get_ctx(band);
237 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_band);
238 if (!tree)
239 goto error;
241 tree->band = band;
242 tree->anchored = isl_schedule_band_is_anchored(band);
244 return tree;
245 error:
246 isl_schedule_band_free(band);
247 return NULL;
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)
257 isl_ctx *ctx;
258 isl_schedule_tree *tree;
260 if (!context)
261 return NULL;
263 ctx = isl_set_get_ctx(context);
264 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_context);
265 if (!tree)
266 goto error;
268 tree->context = context;
269 tree->anchored = 1;
271 return tree;
272 error:
273 isl_set_free(context);
274 return NULL;
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)
282 isl_ctx *ctx;
283 isl_schedule_tree *tree;
285 if (!domain)
286 return NULL;
288 ctx = isl_union_set_get_ctx(domain);
289 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_domain);
290 if (!tree)
291 goto error;
293 tree->domain = domain;
295 return tree;
296 error:
297 isl_union_set_free(domain);
298 return NULL;
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)
308 isl_ctx *ctx;
309 isl_schedule_tree *tree;
311 if (!contraction || !expansion)
312 goto error;
314 ctx = isl_union_map_get_ctx(expansion);
315 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_expansion);
316 if (!tree)
317 goto error;
319 tree->contraction = contraction;
320 tree->expansion = expansion;
322 return tree;
323 error:
324 isl_union_pw_multi_aff_free(contraction);
325 isl_union_map_free(expansion);
326 return NULL;
329 /* Create a new extension schedule tree with the given extension and
330 * no children.
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)
337 isl_ctx *ctx;
338 isl_schedule_tree *tree;
340 if (!extension)
341 return NULL;
343 ctx = isl_union_map_get_ctx(extension);
344 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_extension);
345 if (!tree)
346 goto error;
348 tree->extension = extension;
349 tree->anchored = 1;
351 return tree;
352 error:
353 isl_union_map_free(extension);
354 return NULL;
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)
362 isl_ctx *ctx;
363 isl_schedule_tree *tree;
365 if (!filter)
366 return NULL;
368 ctx = isl_union_set_get_ctx(filter);
369 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_filter);
370 if (!tree)
371 goto error;
373 tree->filter = filter;
375 return tree;
376 error:
377 isl_union_set_free(filter);
378 return NULL;
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)
388 isl_ctx *ctx;
389 isl_schedule_tree *tree;
391 if (!guard)
392 return NULL;
394 ctx = isl_set_get_ctx(guard);
395 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_guard);
396 if (!tree)
397 goto error;
399 tree->guard = guard;
400 tree->anchored = 1;
402 return tree;
403 error:
404 isl_set_free(guard);
405 return NULL;
408 /* Create a new mark schedule tree with the given mark identifier and
409 * no children.
411 __isl_give isl_schedule_tree *isl_schedule_tree_from_mark(
412 __isl_take isl_id *mark)
414 isl_ctx *ctx;
415 isl_schedule_tree *tree;
417 if (!mark)
418 return NULL;
420 ctx = isl_id_get_ctx(mark);
421 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_mark);
422 if (!tree)
423 goto error;
425 tree->mark = mark;
427 return tree;
428 error:
429 isl_id_free(mark);
430 return NULL;
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
443 * schedule tree?
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)
449 if (!tree)
450 return -1;
452 switch (isl_schedule_tree_get_type(tree)) {
453 case isl_schedule_node_error:
454 return -1;
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:
460 return 1;
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:
468 return 0;
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)
484 int i, n;
485 int anchored;
487 if (!tree)
488 return NULL;
490 anchored = isl_schedule_tree_is_anchored(tree);
491 if (anchored < 0)
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);
499 if (!child)
500 return isl_schedule_tree_free(tree);
501 anchored = child->anchored;
502 isl_schedule_tree_free(child);
505 if (anchored == tree->anchored)
506 return tree;
507 tree = isl_schedule_tree_cow(tree);
508 if (!tree)
509 return NULL;
510 tree->anchored = anchored;
511 return tree;
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)
521 isl_ctx *ctx;
522 isl_schedule_tree *tree;
524 if (!list)
525 return NULL;
527 ctx = isl_schedule_tree_list_get_ctx(list);
528 tree = isl_schedule_tree_alloc(ctx, type);
529 if (!tree)
530 goto error;
532 tree->children = list;
533 tree = isl_schedule_tree_update_anchored(tree);
535 return tree;
536 error:
537 isl_schedule_tree_list_free(list);
538 return NULL;
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)
550 isl_ctx *ctx;
551 isl_schedule_tree_list *list;
553 if (!tree1 || !tree2)
554 goto error;
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);
560 } else {
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);
570 } else {
571 list = isl_schedule_tree_list_add(list, tree2);
574 return isl_schedule_tree_from_children(type, list);
575 error:
576 isl_schedule_tree_free(tree1);
577 isl_schedule_tree_free(tree2);
578 return NULL;
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,
591 tree1, tree2);
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
614 * on 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)
627 isl_bool equal;
628 int i, n;
630 if (!tree1 || !tree2)
631 return isl_bool_error;
632 if (tree1 == tree2)
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,
640 tree2->band);
641 break;
642 case isl_schedule_node_context:
643 equal = isl_set_is_equal(tree1->context, tree2->context);
644 break;
645 case isl_schedule_node_domain:
646 equal = isl_union_set_is_equal(tree1->domain, tree2->domain);
647 break;
648 case isl_schedule_node_expansion:
649 equal = isl_union_map_is_equal(tree1->expansion,
650 tree2->expansion);
651 if (equal >= 0 && equal)
652 equal = isl_union_pw_multi_aff_plain_is_equal(
653 tree1->contraction, tree2->contraction);
654 break;
655 case isl_schedule_node_extension:
656 equal = isl_union_map_is_equal(tree1->extension,
657 tree2->extension);
658 break;
659 case isl_schedule_node_filter:
660 equal = isl_union_set_is_equal(tree1->filter, tree2->filter);
661 break;
662 case isl_schedule_node_guard:
663 equal = isl_set_is_equal(tree1->guard, tree2->guard);
664 break;
665 case isl_schedule_node_mark:
666 equal = tree1->mark == tree2->mark;
667 break;
668 case isl_schedule_node_leaf:
669 case isl_schedule_node_sequence:
670 case isl_schedule_node_set:
671 equal = isl_bool_true;
672 break;
673 case isl_schedule_node_error:
674 equal = isl_bool_error;
675 break;
678 if (equal < 0 || !equal)
679 return 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)
694 return 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)
704 if (!tree)
705 return -1;
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)
714 if (!tree)
715 return -1;
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)
725 if (!tree)
726 return NULL;
727 if (!tree->children)
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
734 * free "tree".
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);
743 return child;
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);
752 if (!tree)
753 return NULL;
754 tree->children = isl_schedule_tree_list_free(tree->children);
755 return tree;
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)
764 int n;
766 tree = isl_schedule_tree_cow(tree);
767 if (!tree)
768 return NULL;
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));
779 if (n == 1)
780 return isl_schedule_tree_reset_children(tree);
782 tree->children = isl_schedule_tree_list_drop(tree->children, pos, 1);
783 if (!tree->children)
784 return isl_schedule_tree_free(tree);
786 return 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);
802 if (!tree || !child)
803 goto error;
805 if (isl_schedule_tree_is_leaf(child)) {
806 isl_schedule_tree_free(child);
807 if (!tree->children && pos == 0)
808 return tree;
809 if (isl_schedule_tree_n_children(tree) != 1)
810 isl_die(isl_schedule_tree_get_ctx(tree),
811 isl_error_internal,
812 "can only replace single child by leaf",
813 goto error);
814 return isl_schedule_tree_reset_children(tree);
817 if (!tree->children && pos == 0)
818 tree->children =
819 isl_schedule_tree_list_from_schedule_tree(child);
820 else
821 tree->children = isl_schedule_tree_list_set_schedule_tree(
822 tree->children, pos, child);
824 if (!tree->children)
825 return isl_schedule_tree_free(tree);
826 tree = isl_schedule_tree_update_anchored(tree);
828 return tree;
829 error:
830 isl_schedule_tree_free(tree);
831 isl_schedule_tree_free(child);
832 return NULL;
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)
843 goto error;
844 isl_schedule_tree_list_free(tree->children);
845 tree->children = children;
846 return tree;
847 error:
848 isl_schedule_tree_free(tree);
849 isl_schedule_tree_list_free(children);
850 return NULL;
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);
931 return tree;
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)
944 int i, n;
946 if (!tree || !filter)
947 goto error;
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);
960 return tree;
961 error:
962 isl_union_set_free(filter);
963 isl_schedule_tree_free(tree);
964 return NULL;
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
980 * single child.
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)
995 if (!tree)
996 return 0;
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)
1011 if (!tree)
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)
1027 if (!tree)
1028 return NULL;
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) ==
1033 coincident)
1034 return tree;
1035 tree = isl_schedule_tree_cow(tree);
1036 if (!tree)
1037 return NULL;
1039 tree->band = isl_schedule_band_member_set_coincident(tree->band, pos,
1040 coincident);
1041 if (!tree->band)
1042 return isl_schedule_tree_free(tree);
1043 return 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)
1051 if (!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)
1066 if (!tree)
1067 return NULL;
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)
1072 return tree;
1073 tree = isl_schedule_tree_cow(tree);
1074 if (!tree)
1075 return NULL;
1077 tree->band = isl_schedule_band_set_permutable(tree->band, permutable);
1078 if (!tree->band)
1079 return isl_schedule_tree_free(tree);
1080 return 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)
1088 if (!tree)
1089 return NULL;
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
1099 * with "domain".
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)
1105 goto error;
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);
1112 if (!tree->band)
1113 return isl_schedule_tree_free(tree);
1115 return tree;
1116 error:
1117 isl_schedule_tree_free(tree);
1118 isl_union_set_free(domain);
1119 return NULL;
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)
1127 if (!tree)
1128 return NULL;
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)
1145 goto error;
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,
1151 schedule);
1153 return tree;
1154 error:
1155 isl_schedule_tree_free(tree);
1156 isl_multi_union_pw_aff_free(schedule);
1157 return NULL;
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)
1166 if (!tree)
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);
1184 if (!tree)
1185 return NULL;
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,
1192 pos, type);
1193 if (!tree->band)
1194 return isl_schedule_tree_free(tree);
1196 return 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)
1205 if (!tree)
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,
1213 pos);
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);
1225 if (!tree)
1226 return NULL;
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);
1234 if (!tree->band)
1235 return isl_schedule_tree_free(tree);
1237 return 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)
1245 if (!tree)
1246 return NULL;
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
1257 * changes.
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)
1262 int was_anchored;
1264 tree = isl_schedule_tree_cow(tree);
1265 if (!tree || !options)
1266 goto error;
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,
1274 options);
1275 if (!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);
1280 return tree;
1281 error:
1282 isl_schedule_tree_free(tree);
1283 isl_union_set_free(options);
1284 return NULL;
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)
1292 if (!tree)
1293 return NULL;
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)
1307 if (!tree)
1308 return NULL;
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)
1324 goto error;
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;
1333 return tree;
1334 error:
1335 isl_schedule_tree_free(tree);
1336 isl_union_set_free(domain);
1337 return NULL;
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)
1345 if (!tree)
1346 return NULL;
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)
1360 if (!tree)
1361 return NULL;
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)
1381 goto error;
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;
1392 return tree;
1393 error:
1394 isl_schedule_tree_free(tree);
1395 isl_union_pw_multi_aff_free(contraction);
1396 isl_union_map_free(expansion);
1397 return NULL;
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)
1405 if (!tree)
1406 return NULL;
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)
1422 goto error;
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;
1430 return tree;
1431 error:
1432 isl_schedule_tree_free(tree);
1433 isl_union_map_free(extension);
1434 return NULL;
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)
1442 if (!tree)
1443 return NULL;
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)
1459 goto error;
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;
1468 return tree;
1469 error:
1470 isl_schedule_tree_free(tree);
1471 isl_union_set_free(filter);
1472 return NULL;
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)
1480 if (!tree)
1481 return NULL;
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)
1495 if (!tree)
1496 return NULL;
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)
1509 int *dim = user;
1511 *dim = isl_map_dim(map, isl_dim_out);
1512 isl_map_free(map);
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)
1525 int dim = -1;
1527 if (!umap)
1528 return -1;
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);
1535 return 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,
1542 int extra)
1544 isl_union_set *dom;
1545 isl_space *space;
1546 isl_multi_val *mv;
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);
1562 return 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);
1581 switch (type) {
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:
1587 return 1;
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:
1596 return 0;
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);
1617 return tree;
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;
1635 isl_union_map *res;
1637 if (!tree)
1638 return isl_union_map_free(outer);
1639 if (!isl_schedule_tree_has_children(tree))
1640 return outer;
1641 child = isl_schedule_tree_get_child(tree, 0);
1642 if (!child)
1643 return isl_union_map_free(outer);
1644 res = subtree_schedule_extend(child, outer);
1645 isl_schedule_tree_free(child);
1646 return res;
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)
1655 isl_space *space;
1656 isl_union_set *dom;
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);
1665 return space;
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
1674 * filter -> []
1676 * or
1678 * filter -> [index]
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)
1691 int i, n;
1692 int dim;
1693 int separate;
1694 isl_ctx *ctx;
1695 isl_val *v = NULL;
1696 isl_multi_val *mv;
1697 isl_space *space;
1698 isl_union_map *umap;
1700 if (!tree)
1701 return NULL;
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);
1708 if (n == 0)
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);
1719 if (separate) {
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;
1729 isl_union_set *dom;
1730 isl_schedule_tree *child;
1731 int dim_i;
1732 int empty;
1734 child = isl_schedule_tree_list_get_schedule_tree(
1735 tree->children, i);
1736 dom = isl_schedule_tree_filter_get_filter(child);
1738 if (separate) {
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);
1751 if (empty < 0)
1752 umap_i = isl_union_map_free(umap_i);
1753 else if (empty) {
1754 isl_union_map_free(umap_i);
1755 continue;
1758 dim_i = range_dim(umap_i);
1759 if (dim_i < 0) {
1760 umap = isl_union_map_free(umap);
1761 } else if (dim < dim_i) {
1762 umap = append_range(umap, dim_i - dim);
1763 dim = dim_i;
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);
1770 isl_val_free(v);
1771 isl_multi_val_free(mv);
1772 isl_union_map_free(outer);
1774 return umap;
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;
1794 if (!tree)
1795 return NULL;
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);
1816 break;
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);
1822 break;
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);
1827 break;
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);
1833 break;
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);
1840 break;
1843 return umap;
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)
1856 int i, n;
1857 isl_space *space;
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);
1864 if (n == 0)
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);
1881 return domain;
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;
1894 isl_union_map *exp;
1896 if (!tree)
1897 return NULL;
1899 switch (tree->type) {
1900 case isl_schedule_node_error:
1901 return NULL;
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",
1905 return NULL);
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",
1909 return NULL);
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",
1913 return NULL);
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),
1921 isl_error_internal,
1922 "0D band should be handled by caller",
1923 return NULL);
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);
1927 break;
1928 case isl_schedule_node_domain:
1929 domain = isl_schedule_tree_domain_get_domain(tree);
1930 domain = isl_union_set_universe(domain);
1931 break;
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);
1936 break;
1937 case isl_schedule_node_filter:
1938 domain = isl_schedule_tree_filter_get_filter(tree);
1939 domain = isl_union_set_universe(domain);
1940 break;
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);
1947 break;
1950 return domain;
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)
1982 if (!tree || !mv)
1983 goto error;
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);
1989 if (!tree)
1990 goto error;
1992 tree->band = isl_schedule_band_scale(tree->band, mv);
1993 if (!tree->band)
1994 return isl_schedule_tree_free(tree);
1996 return tree;
1997 error:
1998 isl_schedule_tree_free(tree);
1999 isl_multi_val_free(mv);
2000 return NULL;
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)
2009 if (!tree || !mv)
2010 goto error;
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);
2016 if (!tree)
2017 goto error;
2019 tree->band = isl_schedule_band_scale_down(tree->band, mv);
2020 if (!tree->band)
2021 return isl_schedule_tree_free(tree);
2023 return tree;
2024 error:
2025 isl_schedule_tree_free(tree);
2026 isl_multi_val_free(mv);
2027 return NULL;
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)
2036 if (!tree || !mv)
2037 goto error;
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);
2043 if (!tree)
2044 goto error;
2046 tree->band = isl_schedule_band_mod(tree->band, mv);
2047 if (!tree->band)
2048 return isl_schedule_tree_free(tree);
2050 return tree;
2051 error:
2052 isl_schedule_tree_free(tree);
2053 isl_multi_val_free(mv);
2054 return NULL;
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)
2064 goto error;
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);
2070 if (!tree)
2071 goto error;
2073 tree->band = isl_schedule_band_shift(tree->band, shift);
2074 if (!tree->band)
2075 return isl_schedule_tree_free(tree);
2077 return tree;
2078 error:
2079 isl_schedule_tree_free(tree);
2080 isl_multi_union_pw_aff_free(shift);
2081 return NULL;
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)
2091 int n;
2092 isl_schedule_tree_list *list1, *list2;
2094 tree = isl_schedule_tree_cow(tree);
2095 if (!tree || !child)
2096 goto error;
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,
2119 list1);
2120 error:
2121 isl_schedule_tree_free(tree);
2122 isl_schedule_tree_free(child);
2123 return NULL;
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)
2138 goto error;
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)
2147 goto error;
2149 tree->band = isl_schedule_band_tile(tree->band,
2150 isl_multi_val_copy(sizes));
2151 if (!tree->band)
2152 goto error;
2153 child->band = isl_schedule_band_point(child->band, tree->band, sizes);
2154 if (!child->band)
2155 child = isl_schedule_tree_free(child);
2157 tree = isl_schedule_tree_replace_child(tree, 0, child);
2159 return tree;
2160 error:
2161 isl_schedule_tree_free(child);
2162 isl_schedule_tree_free(tree);
2163 isl_multi_val_free(sizes);
2164 return NULL;
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)
2174 int n;
2175 isl_schedule_tree *child;
2177 if (!tree)
2178 return NULL;
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)
2193 goto error;
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)
2198 goto error;
2200 tree = isl_schedule_tree_replace_child(tree, 0, child);
2202 return tree;
2203 error:
2204 isl_schedule_tree_free(child);
2205 isl_schedule_tree_free(tree);
2206 return NULL;
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)
2219 int i, n;
2221 if (!tree1 || !tree2)
2222 goto error;
2223 n = isl_schedule_tree_n_children(tree1);
2224 if (n == 0) {
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);
2228 return tree1;
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);
2240 return tree1;
2241 error:
2242 isl_schedule_tree_free(tree1);
2243 isl_schedule_tree_free(tree2);
2244 return NULL;
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))
2254 return tree;
2256 tree = isl_schedule_tree_cow(tree);
2257 if (!tree)
2258 return NULL;
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);
2265 if (!tree->band)
2266 return isl_schedule_tree_free(tree);
2267 break;
2268 case isl_schedule_node_context:
2269 tree->context = isl_set_reset_user(tree->context);
2270 if (!tree->context)
2271 return isl_schedule_tree_free(tree);
2272 break;
2273 case isl_schedule_node_domain:
2274 tree->domain = isl_union_set_reset_user(tree->domain);
2275 if (!tree->domain)
2276 return isl_schedule_tree_free(tree);
2277 break;
2278 case isl_schedule_node_expansion:
2279 tree->contraction =
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);
2284 break;
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);
2289 break;
2290 case isl_schedule_node_filter:
2291 tree->filter = isl_union_set_reset_user(tree->filter);
2292 if (!tree->filter)
2293 return isl_schedule_tree_free(tree);
2294 break;
2295 case isl_schedule_node_guard:
2296 tree->guard = isl_set_reset_user(tree->guard);
2297 if (!tree->guard)
2298 return isl_schedule_tree_free(tree);
2299 break;
2300 case isl_schedule_node_leaf:
2301 case isl_schedule_node_mark:
2302 case isl_schedule_node_sequence:
2303 case isl_schedule_node_set:
2304 break;
2307 return tree;
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)
2315 if (!space)
2316 goto error;
2318 if (isl_schedule_tree_is_leaf(tree)) {
2319 isl_space_free(space);
2320 return tree;
2323 tree = isl_schedule_tree_cow(tree);
2324 if (!tree)
2325 goto error;
2327 switch (tree->type) {
2328 case isl_schedule_node_error:
2329 goto error;
2330 case isl_schedule_node_band:
2331 tree->band = isl_schedule_band_align_params(tree->band, space);
2332 if (!tree->band)
2333 return isl_schedule_tree_free(tree);
2334 break;
2335 case isl_schedule_node_context:
2336 tree->context = isl_set_align_params(tree->context, space);
2337 if (!tree->context)
2338 return isl_schedule_tree_free(tree);
2339 break;
2340 case isl_schedule_node_domain:
2341 tree->domain = isl_union_set_align_params(tree->domain, space);
2342 if (!tree->domain)
2343 return isl_schedule_tree_free(tree);
2344 break;
2345 case isl_schedule_node_expansion:
2346 tree->contraction =
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,
2350 space);
2351 if (!tree->contraction || !tree->expansion)
2352 return isl_schedule_tree_free(tree);
2353 break;
2354 case isl_schedule_node_extension:
2355 tree->extension = isl_union_map_align_params(tree->extension,
2356 space);
2357 if (!tree->extension)
2358 return isl_schedule_tree_free(tree);
2359 break;
2360 case isl_schedule_node_filter:
2361 tree->filter = isl_union_set_align_params(tree->filter, space);
2362 if (!tree->filter)
2363 return isl_schedule_tree_free(tree);
2364 break;
2365 case isl_schedule_node_guard:
2366 tree->guard = isl_set_align_params(tree->guard, space);
2367 if (!tree->guard)
2368 return isl_schedule_tree_free(tree);
2369 break;
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);
2375 break;
2378 return tree;
2379 error:
2380 isl_space_free(space);
2381 isl_schedule_tree_free(tree);
2382 return NULL;
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)
2391 if (!tree)
2392 return -1;
2394 switch (tree->type) {
2395 case isl_schedule_node_error:
2396 return -1;
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:
2402 return 1;
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:
2409 return 0;
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)
2429 int involves;
2431 if (!tree || !upma)
2432 goto error;
2434 involves = involves_iteration_domain(tree);
2435 if (involves < 0)
2436 goto error;
2437 if (!involves) {
2438 isl_union_pw_multi_aff_free(upma);
2439 return tree;
2442 tree = isl_schedule_tree_cow(tree);
2443 if (!tree)
2444 goto error;
2446 if (tree->type == isl_schedule_node_band) {
2447 tree->band = isl_schedule_band_pullback_union_pw_multi_aff(
2448 tree->band, upma);
2449 if (!tree->band)
2450 return isl_schedule_tree_free(tree);
2451 } else if (tree->type == isl_schedule_node_domain) {
2452 tree->domain =
2453 isl_union_set_preimage_union_pw_multi_aff(tree->domain,
2454 upma);
2455 if (!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) {
2461 tree->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) {
2467 tree->filter =
2468 isl_union_set_preimage_union_pw_multi_aff(tree->filter,
2469 upma);
2470 if (!tree->filter)
2471 return isl_schedule_tree_free(tree);
2474 return tree;
2475 error:
2476 isl_union_pw_multi_aff_free(upma);
2477 isl_schedule_tree_free(tree);
2478 return NULL;
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)
2486 if (!tree)
2487 return NULL;
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);
2492 if (!tree)
2493 goto error;
2495 tree->band = isl_schedule_band_gist(tree->band, context);
2496 if (!tree->band)
2497 return isl_schedule_tree_free(tree);
2498 return tree;
2499 error:
2500 isl_union_set_free(context);
2501 isl_schedule_tree_free(tree);
2502 return NULL;
2505 /* Are any members in "band" marked coincident?
2507 static int any_coincident(__isl_keep isl_schedule_band *band)
2509 int i, n;
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))
2514 return 1;
2516 return 0;
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;
2529 int empty;
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)) {
2543 int i, n;
2544 int style;
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);
2563 if (empty < 0)
2564 p = isl_printer_free(p);
2565 if (!empty) {
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);
2575 return p;
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)
2593 int i, n;
2594 int sequence = 0;
2595 int block;
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");
2608 break;
2609 case isl_schedule_node_leaf:
2610 p = isl_printer_print_str(p, "leaf");
2611 break;
2612 case isl_schedule_node_sequence:
2613 p = isl_printer_print_str(p, "sequence");
2614 sequence = 1;
2615 break;
2616 case isl_schedule_node_set:
2617 p = isl_printer_print_str(p, "set");
2618 sequence = 1;
2619 break;
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, "\"");
2626 break;
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, "\"");
2633 break;
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, "\"");
2646 break;
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, "\"");
2653 break;
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, "\"");
2660 break;
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, "\"");
2667 break;
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, "\"");
2674 break;
2675 case isl_schedule_node_band:
2676 p = print_tree_band(p, tree->band);
2677 break;
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,
2689 leaf, 0, NULL);
2690 isl_schedule_tree_free(leaf);
2691 p = isl_printer_yaml_next(p);
2693 return isl_printer_yaml_end_mapping(p);
2696 if (sequence) {
2697 p = isl_printer_yaml_start_sequence(p);
2698 } else {
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);
2711 else
2712 p = isl_printer_print_schedule_tree_mark(p, t,
2713 -1, NULL);
2714 isl_schedule_tree_free(t);
2716 p = isl_printer_yaml_next(p);
2719 if (sequence)
2720 p = isl_printer_yaml_end_sequence(p);
2721 p = isl_printer_yaml_end_mapping(p);
2723 return 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)
2736 isl_ctx *ctx;
2737 isl_printer *printer;
2739 if (!tree)
2740 return;
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);