isl_range.c: has_sign: drop unused variable
[isl.git] / isl_schedule_tree.c
blobf675f64bffd39f56a2080cdd25e77aa6e9d9374b
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 static __isl_give isl_schedule_tree *isl_schedule_tree_alloc(isl_ctx *ctx,
39 enum isl_schedule_node_type type)
41 isl_schedule_tree *tree;
43 if (type == isl_schedule_node_error)
44 return NULL;
46 tree = isl_calloc_type(ctx, isl_schedule_tree);
47 if (!tree)
48 return NULL;
50 tree->ref = 1;
51 tree->ctx = ctx;
52 isl_ctx_ref(ctx);
53 tree->type = type;
55 return tree;
58 /* Return a fresh copy of "tree".
60 __isl_take isl_schedule_tree *isl_schedule_tree_dup(
61 __isl_keep isl_schedule_tree *tree)
63 isl_ctx *ctx;
64 isl_schedule_tree *dup;
66 if (!tree)
67 return NULL;
69 ctx = isl_schedule_tree_get_ctx(tree);
70 dup = isl_schedule_tree_alloc(ctx, tree->type);
71 if (!dup)
72 return NULL;
74 switch (tree->type) {
75 case isl_schedule_node_error:
76 isl_die(ctx, isl_error_internal,
77 "allocation should have failed",
78 isl_schedule_tree_free(dup));
79 case isl_schedule_node_band:
80 dup->band = isl_schedule_band_copy(tree->band);
81 if (!dup->band)
82 return isl_schedule_tree_free(dup);
83 break;
84 case isl_schedule_node_domain:
85 dup->domain = isl_union_set_copy(tree->domain);
86 if (!dup->domain)
87 return isl_schedule_tree_free(dup);
88 break;
89 case isl_schedule_node_filter:
90 dup->filter = isl_union_set_copy(tree->filter);
91 if (!dup->filter)
92 return isl_schedule_tree_free(dup);
93 break;
94 case isl_schedule_node_leaf:
95 case isl_schedule_node_sequence:
96 case isl_schedule_node_set:
97 break;
100 if (tree->children) {
101 dup->children = isl_schedule_tree_list_copy(tree->children);
102 if (!dup->children)
103 return isl_schedule_tree_free(dup);
106 return dup;
109 /* Return an isl_schedule_tree that is equal to "tree" and that has only
110 * a single reference.
112 * This function is called before a tree is modified.
113 * A static tree (with negative reference count) should never be modified,
114 * so it is not allowed to call this function on a static tree.
116 __isl_give isl_schedule_tree *isl_schedule_tree_cow(
117 __isl_take isl_schedule_tree *tree)
119 if (!tree)
120 return NULL;
122 if (tree->ref < 0)
123 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
124 "static trees cannot be modified",
125 return isl_schedule_tree_free(tree));
127 if (tree->ref == 1)
128 return tree;
129 tree->ref--;
130 return isl_schedule_tree_dup(tree);
133 /* Return a new reference to "tree".
135 * A static tree (with negative reference count) does not keep track
136 * of the number of references and should not be modified.
138 __isl_give isl_schedule_tree *isl_schedule_tree_copy(
139 __isl_keep isl_schedule_tree *tree)
141 if (!tree)
142 return NULL;
144 if (tree->ref < 0)
145 return tree;
147 tree->ref++;
148 return tree;
151 /* Free "tree" and return NULL.
153 __isl_null isl_schedule_tree *isl_schedule_tree_free(
154 __isl_take isl_schedule_tree *tree)
156 if (!tree)
157 return NULL;
158 if (tree->ref < 0)
159 return NULL;
160 if (--tree->ref > 0)
161 return NULL;
163 switch (tree->type) {
164 case isl_schedule_node_band:
165 isl_schedule_band_free(tree->band);
166 break;
167 case isl_schedule_node_domain:
168 isl_union_set_free(tree->domain);
169 break;
170 case isl_schedule_node_filter:
171 isl_union_set_free(tree->filter);
172 break;
173 case isl_schedule_node_sequence:
174 case isl_schedule_node_set:
175 case isl_schedule_node_error:
176 case isl_schedule_node_leaf:
177 break;
179 isl_schedule_tree_list_free(tree->children);
180 isl_ctx_deref(tree->ctx);
181 free(tree);
183 return NULL;
186 /* Create and return a new leaf schedule tree.
188 __isl_give isl_schedule_tree *isl_schedule_tree_leaf(isl_ctx *ctx)
190 return isl_schedule_tree_alloc(ctx, isl_schedule_node_leaf);
193 /* Create a new band schedule tree referring to "band"
194 * with no children.
196 __isl_give isl_schedule_tree *isl_schedule_tree_from_band(
197 __isl_take isl_schedule_band *band)
199 isl_ctx *ctx;
200 isl_schedule_tree *tree;
202 if (!band)
203 return NULL;
205 ctx = isl_schedule_band_get_ctx(band);
206 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_band);
207 if (!tree)
208 goto error;
210 tree->band = band;
212 return tree;
213 error:
214 isl_schedule_band_free(band);
215 return NULL;
218 /* Create a new domain schedule tree with the given domain and no children.
220 __isl_give isl_schedule_tree *isl_schedule_tree_from_domain(
221 __isl_take isl_union_set *domain)
223 isl_ctx *ctx;
224 isl_schedule_tree *tree;
226 if (!domain)
227 return NULL;
229 ctx = isl_union_set_get_ctx(domain);
230 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_domain);
231 if (!tree)
232 goto error;
234 tree->domain = domain;
236 return tree;
237 error:
238 isl_union_set_free(domain);
239 return NULL;
242 /* Create a new filter schedule tree with the given filter and no children.
244 __isl_give isl_schedule_tree *isl_schedule_tree_from_filter(
245 __isl_take isl_union_set *filter)
247 isl_ctx *ctx;
248 isl_schedule_tree *tree;
250 if (!filter)
251 return NULL;
253 ctx = isl_union_set_get_ctx(filter);
254 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_filter);
255 if (!tree)
256 goto error;
258 tree->filter = filter;
260 return tree;
261 error:
262 isl_union_set_free(filter);
263 return NULL;
266 /* Create a new tree of the given type (isl_schedule_node_sequence or
267 * isl_schedule_node_set) with the given children.
269 __isl_give isl_schedule_tree *isl_schedule_tree_from_children(
270 enum isl_schedule_node_type type,
271 __isl_take isl_schedule_tree_list *list)
273 isl_ctx *ctx;
274 isl_schedule_tree *tree;
276 if (!list)
277 return NULL;
279 ctx = isl_schedule_tree_list_get_ctx(list);
280 tree = isl_schedule_tree_alloc(ctx, type);
281 if (!tree)
282 goto error;
284 tree->children = list;
286 return tree;
287 error:
288 isl_schedule_tree_list_free(list);
289 return NULL;
292 /* Construct a tree with a root node of type "type" and as children
293 * "tree1" and "tree2".
294 * If the root of one (or both) of the input trees is itself of type "type",
295 * then the tree is replaced by its children.
297 __isl_give isl_schedule_tree *isl_schedule_tree_from_pair(
298 enum isl_schedule_node_type type, __isl_take isl_schedule_tree *tree1,
299 __isl_take isl_schedule_tree *tree2)
301 isl_ctx *ctx;
302 isl_schedule_tree_list *list;
304 if (!tree1 || !tree2)
305 goto error;
307 ctx = isl_schedule_tree_get_ctx(tree1);
308 if (isl_schedule_tree_get_type(tree1) == type) {
309 list = isl_schedule_tree_list_copy(tree1->children);
310 isl_schedule_tree_free(tree1);
311 } else {
312 list = isl_schedule_tree_list_alloc(ctx, 2);
313 list = isl_schedule_tree_list_add(list, tree1);
315 if (isl_schedule_tree_get_type(tree2) == type) {
316 isl_schedule_tree_list *children;
318 children = isl_schedule_tree_list_copy(tree2->children);
319 list = isl_schedule_tree_list_concat(list, children);
320 isl_schedule_tree_free(tree2);
321 } else {
322 list = isl_schedule_tree_list_add(list, tree2);
325 return isl_schedule_tree_from_children(type, list);
326 error:
327 isl_schedule_tree_free(tree1);
328 isl_schedule_tree_free(tree2);
329 return NULL;
332 /* Return the isl_ctx to which "tree" belongs.
334 isl_ctx *isl_schedule_tree_get_ctx(__isl_keep isl_schedule_tree *tree)
336 return tree ? tree->ctx : NULL;
339 /* Return the type of the root of the tree or isl_schedule_node_error
340 * on error.
342 enum isl_schedule_node_type isl_schedule_tree_get_type(
343 __isl_keep isl_schedule_tree *tree)
345 return tree ? tree->type : isl_schedule_node_error;
348 /* Are "tree1" and "tree2" obviously equal to each other?
350 int isl_schedule_tree_plain_is_equal(__isl_keep isl_schedule_tree *tree1,
351 __isl_keep isl_schedule_tree *tree2)
353 int equal;
354 int i, n;
356 if (!tree1 || !tree2)
357 return -1;
358 if (tree1 == tree2)
359 return 1;
360 if (tree1->type != tree2->type)
361 return 0;
363 switch (tree1->type) {
364 case isl_schedule_node_band:
365 equal = isl_schedule_band_plain_is_equal(tree1->band,
366 tree2->band);
367 break;
368 case isl_schedule_node_domain:
369 equal = isl_union_set_is_equal(tree1->domain, tree2->domain);
370 break;
371 case isl_schedule_node_filter:
372 equal = isl_union_set_is_equal(tree1->filter, tree2->filter);
373 break;
374 case isl_schedule_node_leaf:
375 case isl_schedule_node_sequence:
376 case isl_schedule_node_set:
377 equal = 1;
378 break;
379 case isl_schedule_node_error:
380 equal = -1;
381 break;
384 if (equal < 0 || !equal)
385 return equal;
387 n = isl_schedule_tree_n_children(tree1);
388 if (n != isl_schedule_tree_n_children(tree2))
389 return 0;
390 for (i = 0; i < n; ++i) {
391 isl_schedule_tree *child1, *child2;
393 child1 = isl_schedule_tree_get_child(tree1, i);
394 child2 = isl_schedule_tree_get_child(tree2, i);
395 equal = isl_schedule_tree_plain_is_equal(child1, child2);
396 isl_schedule_tree_free(child1);
397 isl_schedule_tree_free(child2);
399 if (equal < 0 || !equal)
400 return equal;
403 return 1;
406 /* Does "tree" have any children, other than an implicit leaf.
408 int isl_schedule_tree_has_children(__isl_keep isl_schedule_tree *tree)
410 if (!tree)
411 return -1;
413 return tree->children != NULL;
416 /* Return the number of children of "tree", excluding implicit leaves.
418 int isl_schedule_tree_n_children(__isl_keep isl_schedule_tree *tree)
420 if (!tree)
421 return -1;
423 return isl_schedule_tree_list_n_schedule_tree(tree->children);
426 /* Return a copy of the (explicit) child at position "pos" of "tree".
428 __isl_give isl_schedule_tree *isl_schedule_tree_get_child(
429 __isl_keep isl_schedule_tree *tree, int pos)
431 if (!tree)
432 return NULL;
433 if (!tree->children)
434 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
435 "schedule tree has no explicit children", return NULL);
436 return isl_schedule_tree_list_get_schedule_tree(tree->children, pos);
439 /* Return a copy of the (explicit) child at position "pos" of "tree" and
440 * free "tree".
442 __isl_give isl_schedule_tree *isl_schedule_tree_child(
443 __isl_take isl_schedule_tree *tree, int pos)
445 isl_schedule_tree *child;
447 child = isl_schedule_tree_get_child(tree, pos);
448 isl_schedule_tree_free(tree);
449 return child;
452 /* Remove all (explicit) children from "tree".
454 __isl_give isl_schedule_tree *isl_schedule_tree_reset_children(
455 __isl_take isl_schedule_tree *tree)
457 tree = isl_schedule_tree_cow(tree);
458 if (!tree)
459 return NULL;
460 tree->children = isl_schedule_tree_list_free(tree->children);
461 return tree;
464 /* Remove the child at position "pos" from the children of "tree".
465 * If there was only one child to begin with, then remove all children.
467 __isl_give isl_schedule_tree *isl_schedule_tree_drop_child(
468 __isl_take isl_schedule_tree *tree, int pos)
470 int n;
472 tree = isl_schedule_tree_cow(tree);
473 if (!tree)
474 return NULL;
476 if (!isl_schedule_tree_has_children(tree))
477 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
478 "tree does not have any explicit children",
479 return isl_schedule_tree_free(tree));
480 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
481 if (pos < 0 || pos >= n)
482 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
483 "position out of bounds",
484 return isl_schedule_tree_free(tree));
485 if (n == 1)
486 return isl_schedule_tree_reset_children(tree);
488 tree->children = isl_schedule_tree_list_drop(tree->children, pos, 1);
489 if (!tree->children)
490 return isl_schedule_tree_free(tree);
492 return tree;
495 /* Replace the child at position "pos" of "tree" by "child".
497 * If the new child is a leaf, then it is not explicitly
498 * recorded in the list of children. Instead, the list of children
499 * (which is assumed to have only one element) is removed.
500 * Note that the children of set and sequence nodes are always
501 * filters, so they cannot be replaced by empty trees.
503 __isl_give isl_schedule_tree *isl_schedule_tree_replace_child(
504 __isl_take isl_schedule_tree *tree, int pos,
505 __isl_take isl_schedule_tree *child)
507 tree = isl_schedule_tree_cow(tree);
508 if (!tree || !child)
509 goto error;
511 if (isl_schedule_tree_is_leaf(child)) {
512 isl_schedule_tree_free(child);
513 if (!tree->children && pos == 0)
514 return tree;
515 if (isl_schedule_tree_n_children(tree) != 1)
516 isl_die(isl_schedule_tree_get_ctx(tree),
517 isl_error_internal,
518 "can only replace single child by leaf",
519 goto error);
520 return isl_schedule_tree_reset_children(tree);
523 if (!tree->children && pos == 0)
524 tree->children =
525 isl_schedule_tree_list_from_schedule_tree(child);
526 else
527 tree->children = isl_schedule_tree_list_set_schedule_tree(
528 tree->children, pos, child);
530 if (!tree->children)
531 return isl_schedule_tree_free(tree);
533 return tree;
534 error:
535 isl_schedule_tree_free(tree);
536 isl_schedule_tree_free(child);
537 return NULL;
540 /* Replace the (explicit) children of "tree" by "children"?
542 __isl_give isl_schedule_tree *isl_schedule_tree_set_children(
543 __isl_take isl_schedule_tree *tree,
544 __isl_take isl_schedule_tree_list *children)
546 tree = isl_schedule_tree_cow(tree);
547 if (!tree || !children)
548 goto error;
549 isl_schedule_tree_list_free(tree->children);
550 tree->children = children;
551 return tree;
552 error:
553 isl_schedule_tree_free(tree);
554 isl_schedule_tree_list_free(children);
555 return NULL;
558 /* Create a new band schedule tree referring to "band"
559 * with "tree" as single child.
561 __isl_give isl_schedule_tree *isl_schedule_tree_insert_band(
562 __isl_take isl_schedule_tree *tree, __isl_take isl_schedule_band *band)
564 isl_schedule_tree *res;
566 res = isl_schedule_tree_from_band(band);
567 return isl_schedule_tree_replace_child(res, 0, tree);
570 /* Create a new domain schedule tree with the given domain and
571 * with "tree" as single child.
573 __isl_give isl_schedule_tree *isl_schedule_tree_insert_domain(
574 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *domain)
576 isl_schedule_tree *res;
578 res = isl_schedule_tree_from_domain(domain);
579 return isl_schedule_tree_replace_child(res, 0, tree);
582 /* Create a new filter schedule tree with the given filter and single child.
584 * If the root of "tree" is itself a filter node, then the two
585 * filter nodes are merged into one node.
587 __isl_give isl_schedule_tree *isl_schedule_tree_insert_filter(
588 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter)
590 isl_schedule_tree *res;
592 if (isl_schedule_tree_get_type(tree) == isl_schedule_node_filter) {
593 isl_union_set *tree_filter;
595 tree_filter = isl_schedule_tree_filter_get_filter(tree);
596 tree_filter = isl_union_set_intersect(tree_filter, filter);
597 tree = isl_schedule_tree_filter_set_filter(tree, tree_filter);
598 return tree;
601 res = isl_schedule_tree_from_filter(filter);
602 return isl_schedule_tree_replace_child(res, 0, tree);
605 /* Insert a filter node with filter set "filter"
606 * in each of the children of "tree".
608 __isl_give isl_schedule_tree *isl_schedule_tree_children_insert_filter(
609 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter)
611 int i, n;
613 if (!tree || !filter)
614 goto error;
616 n = isl_schedule_tree_n_children(tree);
617 for (i = 0; i < n; ++i) {
618 isl_schedule_tree *child;
620 child = isl_schedule_tree_get_child(tree, i);
621 child = isl_schedule_tree_insert_filter(child,
622 isl_union_set_copy(filter));
623 tree = isl_schedule_tree_replace_child(tree, i, child);
626 isl_union_set_free(filter);
627 return tree;
628 error:
629 isl_union_set_free(filter);
630 isl_schedule_tree_free(tree);
631 return NULL;
634 /* Return the number of members in the band tree root.
636 unsigned isl_schedule_tree_band_n_member(__isl_keep isl_schedule_tree *tree)
638 if (!tree)
639 return 0;
641 if (tree->type != isl_schedule_node_band)
642 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
643 "not a band node", return 0);
645 return isl_schedule_band_n_member(tree->band);
648 /* Is the band member at position "pos" of the band tree root
649 * marked coincident?
651 int isl_schedule_tree_band_member_get_coincident(
652 __isl_keep isl_schedule_tree *tree, int pos)
654 if (!tree)
655 return -1;
657 if (tree->type != isl_schedule_node_band)
658 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
659 "not a band node", return -1);
661 return isl_schedule_band_member_get_coincident(tree->band, pos);
664 /* Mark the given band member as being coincident or not
665 * according to "coincident".
667 __isl_give isl_schedule_tree *isl_schedule_tree_band_member_set_coincident(
668 __isl_take isl_schedule_tree *tree, int pos, int coincident)
670 if (!tree)
671 return NULL;
672 if (tree->type != isl_schedule_node_band)
673 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
674 "not a band node", return isl_schedule_tree_free(tree));
675 if (isl_schedule_tree_band_member_get_coincident(tree, pos) ==
676 coincident)
677 return tree;
678 tree = isl_schedule_tree_cow(tree);
679 if (!tree)
680 return NULL;
682 tree->band = isl_schedule_band_member_set_coincident(tree->band, pos,
683 coincident);
684 if (!tree->band)
685 return isl_schedule_tree_free(tree);
686 return tree;
689 /* Is the band tree root marked permutable?
691 int isl_schedule_tree_band_get_permutable(__isl_keep isl_schedule_tree *tree)
693 if (!tree)
694 return -1;
696 if (tree->type != isl_schedule_node_band)
697 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
698 "not a band node", return -1);
700 return isl_schedule_band_get_permutable(tree->band);
703 /* Mark the band tree root permutable or not according to "permutable"?
705 __isl_give isl_schedule_tree *isl_schedule_tree_band_set_permutable(
706 __isl_take isl_schedule_tree *tree, int permutable)
708 if (!tree)
709 return NULL;
710 if (tree->type != isl_schedule_node_band)
711 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
712 "not a band node", return isl_schedule_tree_free(tree));
713 if (isl_schedule_tree_band_get_permutable(tree) == permutable)
714 return tree;
715 tree = isl_schedule_tree_cow(tree);
716 if (!tree)
717 return NULL;
719 tree->band = isl_schedule_band_set_permutable(tree->band, permutable);
720 if (!tree->band)
721 return isl_schedule_tree_free(tree);
722 return tree;
725 /* Return the schedule space of the band tree root.
727 __isl_give isl_space *isl_schedule_tree_band_get_space(
728 __isl_keep isl_schedule_tree *tree)
730 if (!tree)
731 return NULL;
733 if (tree->type != isl_schedule_node_band)
734 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
735 "not a band node", return NULL);
737 return isl_schedule_band_get_space(tree->band);
740 /* Return the schedule of the band tree root in isolation.
742 __isl_give isl_multi_union_pw_aff *isl_schedule_tree_band_get_partial_schedule(
743 __isl_keep isl_schedule_tree *tree)
745 if (!tree)
746 return NULL;
748 if (tree->type != isl_schedule_node_band)
749 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
750 "not a band node", return NULL);
752 return isl_schedule_band_get_partial_schedule(tree->band);
755 /* Return the domain of the domain tree root.
757 __isl_give isl_union_set *isl_schedule_tree_domain_get_domain(
758 __isl_keep isl_schedule_tree *tree)
760 if (!tree)
761 return NULL;
763 if (tree->type != isl_schedule_node_domain)
764 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
765 "not a domain node", return NULL);
767 return isl_union_set_copy(tree->domain);
770 /* Replace the domain of domain tree root "tree" by "domain".
772 __isl_give isl_schedule_tree *isl_schedule_tree_domain_set_domain(
773 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *domain)
775 tree = isl_schedule_tree_cow(tree);
776 if (!tree || !domain)
777 goto error;
779 if (tree->type != isl_schedule_node_domain)
780 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
781 "not a domain node", goto error);
783 isl_union_set_free(tree->domain);
784 tree->domain = domain;
786 return tree;
787 error:
788 isl_schedule_tree_free(tree);
789 isl_union_set_free(domain);
790 return NULL;
793 /* Return the filter of the filter tree root.
795 __isl_give isl_union_set *isl_schedule_tree_filter_get_filter(
796 __isl_keep isl_schedule_tree *tree)
798 if (!tree)
799 return NULL;
801 if (tree->type != isl_schedule_node_filter)
802 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
803 "not a filter node", return NULL);
805 return isl_union_set_copy(tree->filter);
808 /* Replace the filter of the filter tree root by "filter".
810 __isl_give isl_schedule_tree *isl_schedule_tree_filter_set_filter(
811 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter)
813 tree = isl_schedule_tree_cow(tree);
814 if (!tree || !filter)
815 goto error;
817 if (tree->type != isl_schedule_node_filter)
818 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
819 "not a filter node", return NULL);
821 isl_union_set_free(tree->filter);
822 tree->filter = filter;
824 return tree;
825 error:
826 isl_schedule_tree_free(tree);
827 isl_union_set_free(filter);
828 return NULL;
831 /* Set dim to the range dimension of "map" and abort the search.
833 static int set_range_dim(__isl_take isl_map *map, void *user)
835 int *dim = user;
837 *dim = isl_map_dim(map, isl_dim_out);
838 isl_map_free(map);
840 return -1;
843 /* Return the dimension of the range of "umap".
844 * "umap" is assumed not to be empty and
845 * all maps inside "umap" are assumed to have the same range.
847 * We extract the range dimension from the first map in "umap".
849 static int range_dim(__isl_keep isl_union_map *umap)
851 int dim = -1;
853 if (!umap)
854 return -1;
855 if (isl_union_map_n_map(umap) == 0)
856 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,
857 "unexpected empty input", return -1);
859 isl_union_map_foreach_map(umap, &set_range_dim, &dim);
861 return dim;
864 /* Append an "extra" number of zeros to the range of "umap" and
865 * return the result.
867 static __isl_give isl_union_map *append_range(__isl_take isl_union_map *umap,
868 int extra)
870 isl_union_set *dom;
871 isl_space *space;
872 isl_multi_val *mv;
873 isl_union_pw_multi_aff *suffix;
874 isl_union_map *universe;
875 isl_union_map *suffix_umap;
877 universe = isl_union_map_universe(isl_union_map_copy(umap));
878 dom = isl_union_map_domain(universe);
879 space = isl_union_set_get_space(dom);
880 space = isl_space_set_from_params(space);
881 space = isl_space_add_dims(space, isl_dim_set, extra);
882 mv = isl_multi_val_zero(space);
884 suffix = isl_union_pw_multi_aff_multi_val_on_domain(dom, mv);
885 suffix_umap = isl_union_map_from_union_pw_multi_aff(suffix);
886 umap = isl_union_map_flat_range_product(umap, suffix_umap);
888 return umap;
891 /* Move down to the first descendant of "tree" that contains any schedule
892 * information or return "leaf" if there is no such descendant.
894 __isl_give isl_schedule_tree *isl_schedule_tree_first_schedule_descendant(
895 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_tree *leaf)
897 while (isl_schedule_tree_get_type(tree) == isl_schedule_node_band &&
898 isl_schedule_tree_band_n_member(tree) == 0) {
899 if (!isl_schedule_tree_has_children(tree)) {
900 isl_schedule_tree_free(tree);
901 return isl_schedule_tree_copy(leaf);
903 tree = isl_schedule_tree_child(tree, 0);
906 return tree;
909 static __isl_give isl_union_map *subtree_schedule_extend(
910 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer);
912 /* Extend the schedule map "outer" with the subtree schedule
913 * of the (single) child of "tree", if any.
915 * If "tree" does not have any descendants (apart from those that
916 * do not carry any schedule information), then we simply return "outer".
917 * Otherwise, we extend the schedule map "outer" with the subtree schedule
918 * of the single child.
920 static __isl_give isl_union_map *subtree_schedule_extend_child(
921 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
923 isl_schedule_tree *child;
924 isl_union_map *res;
926 if (!tree)
927 return isl_union_map_free(outer);
928 if (!isl_schedule_tree_has_children(tree))
929 return outer;
930 child = isl_schedule_tree_get_child(tree, 0);
931 if (!child)
932 return isl_union_map_free(outer);
933 res = subtree_schedule_extend(child, outer);
934 isl_schedule_tree_free(child);
935 return res;
938 /* Extract the parameter space from one of the children of "tree",
939 * which are assumed to be filters.
941 static __isl_give isl_space *extract_space_from_filter_child(
942 __isl_keep isl_schedule_tree *tree)
944 isl_space *space;
945 isl_union_set *dom;
946 isl_schedule_tree *child;
948 child = isl_schedule_tree_list_get_schedule_tree(tree->children, 0);
949 dom = isl_schedule_tree_filter_get_filter(child);
950 space = isl_union_set_get_space(dom);
951 isl_union_set_free(dom);
952 isl_schedule_tree_free(child);
954 return space;
957 /* Extend the schedule map "outer" with the subtree schedule
958 * of a set or sequence node.
960 * The schedule for the set or sequence node itself is composed of
961 * pieces of the form
963 * filter -> []
965 * or
967 * filter -> [index]
969 * The first form is used if there is only a single child or
970 * if the current node is a set node and the schedule_separate_components
971 * option is not set.
973 * Each of the pieces above is extended with the subtree schedule of
974 * the child of the corresponding filter, if any, padded with zeros
975 * to ensure that all pieces have the same range dimension.
977 static __isl_give isl_union_map *subtree_schedule_extend_from_children(
978 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
980 int i, n;
981 int dim;
982 int separate;
983 isl_ctx *ctx;
984 isl_val *v = NULL;
985 isl_multi_val *mv;
986 isl_space *space;
987 isl_union_map *umap;
989 if (!tree)
990 return NULL;
992 ctx = isl_schedule_tree_get_ctx(tree);
993 if (!tree->children)
994 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
995 "missing children", return NULL);
996 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
997 if (n == 0)
998 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
999 "missing children", return NULL);
1001 separate = n > 1 && (tree->type == isl_schedule_node_sequence ||
1002 isl_options_get_schedule_separate_components(ctx));
1004 space = extract_space_from_filter_child(tree);
1006 umap = isl_union_map_empty(isl_space_copy(space));
1007 space = isl_space_set_from_params(space);
1008 if (separate) {
1009 space = isl_space_add_dims(space, isl_dim_set, 1);
1010 v = isl_val_zero(ctx);
1012 mv = isl_multi_val_zero(space);
1014 dim = isl_multi_val_dim(mv, isl_dim_set);
1015 for (i = 0; i < n; ++i) {
1016 isl_union_pw_multi_aff *upma;
1017 isl_union_map *umap_i;
1018 isl_union_set *dom;
1019 isl_schedule_tree *child;
1020 int dim_i;
1021 int empty;
1023 child = isl_schedule_tree_list_get_schedule_tree(
1024 tree->children, i);
1025 dom = isl_schedule_tree_filter_get_filter(child);
1027 if (separate) {
1028 mv = isl_multi_val_set_val(mv, 0, isl_val_copy(v));
1029 v = isl_val_add_ui(v, 1);
1031 upma = isl_union_pw_multi_aff_multi_val_on_domain(dom,
1032 isl_multi_val_copy(mv));
1033 umap_i = isl_union_map_from_union_pw_multi_aff(upma);
1034 umap_i = isl_union_map_flat_range_product(
1035 isl_union_map_copy(outer), umap_i);
1036 umap_i = subtree_schedule_extend_child(child, umap_i);
1037 isl_schedule_tree_free(child);
1039 empty = isl_union_map_is_empty(umap_i);
1040 if (empty < 0)
1041 umap_i = isl_union_map_free(umap_i);
1042 else if (empty) {
1043 isl_union_map_free(umap_i);
1044 continue;
1047 dim_i = range_dim(umap_i);
1048 if (dim_i < 0) {
1049 umap = isl_union_map_free(umap);
1050 } else if (dim < dim_i) {
1051 umap = append_range(umap, dim_i - dim);
1052 dim = dim_i;
1053 } else if (dim_i < dim) {
1054 umap_i = append_range(umap_i, dim - dim_i);
1056 umap = isl_union_map_union(umap, umap_i);
1059 isl_val_free(v);
1060 isl_multi_val_free(mv);
1061 isl_union_map_free(outer);
1063 return umap;
1066 /* Extend the schedule map "outer" with the subtree schedule of "tree".
1068 * If the root of the tree is a set or a sequence, then we extend
1069 * the schedule map in subtree_schedule_extend_from_children.
1070 * Otherwise, we extend the schedule map with the partial schedule
1071 * corresponding to the root of the tree and then continue with
1072 * the single child of this root.
1074 static __isl_give isl_union_map *subtree_schedule_extend(
1075 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
1077 isl_multi_union_pw_aff *mupa;
1078 isl_union_map *umap;
1079 isl_union_set *domain;
1081 if (!tree)
1082 return NULL;
1084 switch (tree->type) {
1085 case isl_schedule_node_error:
1086 return isl_union_map_free(outer);
1087 case isl_schedule_node_band:
1088 if (isl_schedule_tree_band_n_member(tree) == 0)
1089 return subtree_schedule_extend_child(tree, outer);
1090 mupa = isl_schedule_band_get_partial_schedule(tree->band);
1091 umap = isl_union_map_from_multi_union_pw_aff(mupa);
1092 outer = isl_union_map_flat_range_product(outer, umap);
1093 umap = subtree_schedule_extend_child(tree, outer);
1094 break;
1095 case isl_schedule_node_domain:
1096 domain = isl_schedule_tree_domain_get_domain(tree);
1097 umap = isl_union_map_from_domain(domain);
1098 outer = isl_union_map_flat_range_product(outer, umap);
1099 umap = subtree_schedule_extend_child(tree, outer);
1100 break;
1101 case isl_schedule_node_filter:
1102 domain = isl_schedule_tree_filter_get_filter(tree);
1103 umap = isl_union_map_from_domain(domain);
1104 outer = isl_union_map_flat_range_product(outer, umap);
1105 umap = subtree_schedule_extend_child(tree, outer);
1106 break;
1107 case isl_schedule_node_leaf:
1108 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
1109 "leaf node should be handled by caller", return NULL);
1110 case isl_schedule_node_set:
1111 case isl_schedule_node_sequence:
1112 umap = subtree_schedule_extend_from_children(tree, outer);
1113 break;
1116 return umap;
1119 static __isl_give isl_union_set *initial_domain(
1120 __isl_keep isl_schedule_tree *tree);
1122 /* Extract a universe domain from the children of the tree root "tree",
1123 * which is a set or sequence, meaning that its children are filters.
1124 * In particular, return the union of the universes of the filters.
1126 static __isl_give isl_union_set *initial_domain_from_children(
1127 __isl_keep isl_schedule_tree *tree)
1129 int i, n;
1130 isl_space *space;
1131 isl_union_set *domain;
1133 if (!tree->children)
1134 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
1135 "missing children", return NULL);
1136 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
1137 if (n == 0)
1138 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
1139 "missing children", return NULL);
1141 space = extract_space_from_filter_child(tree);
1142 domain = isl_union_set_empty(space);
1144 for (i = 0; i < n; ++i) {
1145 isl_schedule_tree *child;
1146 isl_union_set *domain_i;
1148 child = isl_schedule_tree_get_child(tree, i);
1149 domain_i = initial_domain(child);
1150 domain = isl_union_set_union(domain, domain_i);
1151 isl_schedule_tree_free(child);
1154 return domain;
1157 /* Extract a universe domain from the tree root "tree".
1158 * The caller is responsible for making sure that this node
1159 * would not be skipped by isl_schedule_tree_first_schedule_descendant
1160 * and that it is not a leaf node.
1162 static __isl_give isl_union_set *initial_domain(
1163 __isl_keep isl_schedule_tree *tree)
1165 isl_multi_union_pw_aff *mupa;
1166 isl_union_set *domain;
1168 if (!tree)
1169 return NULL;
1171 switch (tree->type) {
1172 case isl_schedule_node_error:
1173 return NULL;
1174 case isl_schedule_node_band:
1175 if (isl_schedule_tree_band_n_member(tree) == 0)
1176 isl_die(isl_schedule_tree_get_ctx(tree),
1177 isl_error_internal,
1178 "0D band should be handled by caller",
1179 return NULL);
1180 mupa = isl_schedule_band_get_partial_schedule(tree->band);
1181 domain = isl_multi_union_pw_aff_domain(mupa);
1182 domain = isl_union_set_universe(domain);
1183 break;
1184 case isl_schedule_node_domain:
1185 domain = isl_schedule_tree_domain_get_domain(tree);
1186 domain = isl_union_set_universe(domain);
1187 break;
1188 case isl_schedule_node_filter:
1189 domain = isl_schedule_tree_filter_get_filter(tree);
1190 domain = isl_union_set_universe(domain);
1191 break;
1192 case isl_schedule_node_leaf:
1193 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
1194 "leaf node should be handled by caller", return NULL);
1195 case isl_schedule_node_set:
1196 case isl_schedule_node_sequence:
1197 domain = initial_domain_from_children(tree);
1198 break;
1201 return domain;
1204 /* Return the subtree schedule of a node that contains some schedule
1205 * information, i.e., a node that would not be skipped by
1206 * isl_schedule_tree_first_schedule_descendant and that is not a leaf.
1208 * We start with an initial zero-dimensional subtree schedule based
1209 * on the domain information in the root node and then extend it
1210 * based on the schedule information in the root node and its descendants.
1212 __isl_give isl_union_map *isl_schedule_tree_get_subtree_schedule_union_map(
1213 __isl_keep isl_schedule_tree *tree)
1215 isl_union_set *domain;
1216 isl_union_map *umap;
1218 domain = initial_domain(tree);
1219 umap = isl_union_map_from_domain(domain);
1220 return subtree_schedule_extend(tree, umap);
1223 /* Multiply the partial schedule of the band root node of "tree"
1224 * with the factors in "mv".
1226 __isl_give isl_schedule_tree *isl_schedule_tree_band_scale(
1227 __isl_take isl_schedule_tree *tree, __isl_take isl_multi_val *mv)
1229 if (!tree || !mv)
1230 goto error;
1231 if (tree->type != isl_schedule_node_band)
1232 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1233 "not a band node", goto error);
1235 tree = isl_schedule_tree_cow(tree);
1236 if (!tree)
1237 goto error;
1239 tree->band = isl_schedule_band_scale(tree->band, mv);
1240 if (!tree->band)
1241 return isl_schedule_tree_free(tree);
1243 return tree;
1244 error:
1245 isl_schedule_tree_free(tree);
1246 isl_multi_val_free(mv);
1247 return NULL;
1250 /* Divide the partial schedule of the band root node of "tree"
1251 * by the factors in "mv".
1253 __isl_give isl_schedule_tree *isl_schedule_tree_band_scale_down(
1254 __isl_take isl_schedule_tree *tree, __isl_take isl_multi_val *mv)
1256 if (!tree || !mv)
1257 goto error;
1258 if (tree->type != isl_schedule_node_band)
1259 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1260 "not a band node", goto error);
1262 tree = isl_schedule_tree_cow(tree);
1263 if (!tree)
1264 goto error;
1266 tree->band = isl_schedule_band_scale_down(tree->band, mv);
1267 if (!tree->band)
1268 return isl_schedule_tree_free(tree);
1270 return tree;
1271 error:
1272 isl_schedule_tree_free(tree);
1273 isl_multi_val_free(mv);
1274 return NULL;
1277 /* Tile the band root node of "tree" with tile sizes "sizes".
1279 * We duplicate the band node, change the schedule of one of them
1280 * to the tile schedule and the other to the point schedule and then
1281 * attach the point band as a child to the tile band.
1283 __isl_give isl_schedule_tree *isl_schedule_tree_band_tile(
1284 __isl_take isl_schedule_tree *tree, __isl_take isl_multi_val *sizes)
1286 isl_schedule_tree *child = NULL;
1288 if (!tree || !sizes)
1289 goto error;
1290 if (tree->type != isl_schedule_node_band)
1291 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1292 "not a band node", goto error);
1294 child = isl_schedule_tree_copy(tree);
1295 tree = isl_schedule_tree_cow(tree);
1296 child = isl_schedule_tree_cow(child);
1297 if (!tree || !child)
1298 goto error;
1300 tree->band = isl_schedule_band_tile(tree->band,
1301 isl_multi_val_copy(sizes));
1302 if (!tree->band)
1303 goto error;
1304 child->band = isl_schedule_band_point(child->band, tree->band, sizes);
1305 if (!child->band)
1306 child = isl_schedule_tree_free(child);
1308 tree = isl_schedule_tree_replace_child(tree, 0, child);
1310 return tree;
1311 error:
1312 isl_schedule_tree_free(child);
1313 isl_schedule_tree_free(tree);
1314 isl_multi_val_free(sizes);
1315 return NULL;
1318 /* Split the band root node of "tree" into two nested band nodes,
1319 * one with the first "pos" dimensions and
1320 * one with the remaining dimensions.
1322 __isl_give isl_schedule_tree *isl_schedule_tree_band_split(
1323 __isl_take isl_schedule_tree *tree, int pos)
1325 int n;
1326 isl_schedule_tree *child;
1328 if (!tree)
1329 return NULL;
1330 if (tree->type != isl_schedule_node_band)
1331 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1332 "not a band node", return isl_schedule_tree_free(tree));
1334 n = isl_schedule_tree_band_n_member(tree);
1335 if (pos < 0 || pos > n)
1336 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1337 "position out of bounds",
1338 return isl_schedule_tree_free(tree));
1340 child = isl_schedule_tree_copy(tree);
1341 tree = isl_schedule_tree_cow(tree);
1342 child = isl_schedule_tree_cow(child);
1343 if (!tree || !child)
1344 goto error;
1346 child->band = isl_schedule_band_drop(child->band, 0, pos);
1347 tree->band = isl_schedule_band_drop(tree->band, pos, n - pos);
1348 if (!child->band || !tree->band)
1349 goto error;
1351 tree = isl_schedule_tree_replace_child(tree, 0, child);
1353 return tree;
1354 error:
1355 isl_schedule_tree_free(child);
1356 isl_schedule_tree_free(tree);
1357 return NULL;
1360 /* Attach "tree2" at each of the leaves of "tree1".
1362 * If "tree1" does not have any explicit children, then make "tree2"
1363 * its single child. Otherwise, attach "tree2" to the leaves of
1364 * each of the children of "tree1".
1366 __isl_give isl_schedule_tree *isl_schedule_tree_append_to_leaves(
1367 __isl_take isl_schedule_tree *tree1,
1368 __isl_take isl_schedule_tree *tree2)
1370 int i, n;
1372 if (!tree1 || !tree2)
1373 goto error;
1374 n = isl_schedule_tree_n_children(tree1);
1375 if (n == 0) {
1376 isl_schedule_tree_list *list;
1377 list = isl_schedule_tree_list_from_schedule_tree(tree2);
1378 tree1 = isl_schedule_tree_set_children(tree1, list);
1379 return tree1;
1381 for (i = 0; i < n; ++i) {
1382 isl_schedule_tree *child;
1384 child = isl_schedule_tree_get_child(tree1, i);
1385 child = isl_schedule_tree_append_to_leaves(child,
1386 isl_schedule_tree_copy(tree2));
1387 tree1 = isl_schedule_tree_replace_child(tree1, i, child);
1390 isl_schedule_tree_free(tree2);
1391 return tree1;
1392 error:
1393 isl_schedule_tree_free(tree1);
1394 isl_schedule_tree_free(tree2);
1395 return NULL;
1398 /* Reset the user pointer on all identifiers of parameters and tuples
1399 * in the root of "tree".
1401 __isl_give isl_schedule_tree *isl_schedule_tree_reset_user(
1402 __isl_take isl_schedule_tree *tree)
1404 if (isl_schedule_tree_is_leaf(tree))
1405 return tree;
1407 tree = isl_schedule_tree_cow(tree);
1408 if (!tree)
1409 return NULL;
1411 switch (tree->type) {
1412 case isl_schedule_node_error:
1413 return isl_schedule_tree_free(tree);
1414 case isl_schedule_node_band:
1415 tree->band = isl_schedule_band_reset_user(tree->band);
1416 if (!tree->band)
1417 return isl_schedule_tree_free(tree);
1418 break;
1419 case isl_schedule_node_domain:
1420 tree->domain = isl_union_set_reset_user(tree->domain);
1421 if (!tree->domain)
1422 return isl_schedule_tree_free(tree);
1423 break;
1424 case isl_schedule_node_filter:
1425 tree->filter = isl_union_set_reset_user(tree->filter);
1426 if (!tree->filter)
1427 return isl_schedule_tree_free(tree);
1428 break;
1429 case isl_schedule_node_leaf:
1430 case isl_schedule_node_sequence:
1431 case isl_schedule_node_set:
1432 break;
1435 return tree;
1438 /* Align the parameters of the root of "tree" to those of "space".
1440 __isl_give isl_schedule_tree *isl_schedule_tree_align_params(
1441 __isl_take isl_schedule_tree *tree, __isl_take isl_space *space)
1443 if (!space)
1444 goto error;
1446 if (isl_schedule_tree_is_leaf(tree)) {
1447 isl_space_free(space);
1448 return tree;
1451 tree = isl_schedule_tree_cow(tree);
1452 if (!tree)
1453 goto error;
1455 switch (tree->type) {
1456 case isl_schedule_node_error:
1457 goto error;
1458 case isl_schedule_node_band:
1459 tree->band = isl_schedule_band_align_params(tree->band, space);
1460 if (!tree->band)
1461 return isl_schedule_tree_free(tree);
1462 break;
1463 case isl_schedule_node_domain:
1464 tree->domain = isl_union_set_align_params(tree->domain, space);
1465 if (!tree->domain)
1466 return isl_schedule_tree_free(tree);
1467 break;
1468 case isl_schedule_node_filter:
1469 tree->filter = isl_union_set_align_params(tree->filter, space);
1470 if (!tree->filter)
1471 return isl_schedule_tree_free(tree);
1472 break;
1473 case isl_schedule_node_leaf:
1474 case isl_schedule_node_sequence:
1475 case isl_schedule_node_set:
1476 isl_space_free(space);
1477 break;
1480 return tree;
1481 error:
1482 isl_space_free(space);
1483 isl_schedule_tree_free(tree);
1484 return NULL;
1487 /* Does "tree" involve the iteration domain?
1488 * That is, does it need to be modified
1489 * by isl_schedule_tree_pullback_union_pw_multi_aff?
1491 static int involves_iteration_domain(__isl_keep isl_schedule_tree *tree)
1493 if (!tree)
1494 return -1;
1496 switch (tree->type) {
1497 case isl_schedule_node_error:
1498 return -1;
1499 case isl_schedule_node_band:
1500 case isl_schedule_node_domain:
1501 case isl_schedule_node_filter:
1502 return 1;
1503 case isl_schedule_node_leaf:
1504 case isl_schedule_node_sequence:
1505 case isl_schedule_node_set:
1506 return 0;
1510 /* Compute the pullback of the root node of "tree" by the function
1511 * represented by "upma".
1512 * In other words, plug in "upma" in the iteration domains of
1513 * the root node of "tree".
1515 * We first check if the root node involves any iteration domains.
1516 * If so, we handle the specific cases.
1518 __isl_give isl_schedule_tree *isl_schedule_tree_pullback_union_pw_multi_aff(
1519 __isl_take isl_schedule_tree *tree,
1520 __isl_take isl_union_pw_multi_aff *upma)
1522 int involves;
1524 if (!tree || !upma)
1525 goto error;
1527 involves = involves_iteration_domain(tree);
1528 if (involves < 0)
1529 goto error;
1530 if (!involves) {
1531 isl_union_pw_multi_aff_free(upma);
1532 return tree;
1535 tree = isl_schedule_tree_cow(tree);
1536 if (!tree)
1537 goto error;
1539 if (tree->type == isl_schedule_node_band) {
1540 tree->band = isl_schedule_band_pullback_union_pw_multi_aff(
1541 tree->band, upma);
1542 if (!tree->band)
1543 return isl_schedule_tree_free(tree);
1544 } else if (tree->type == isl_schedule_node_domain) {
1545 tree->domain =
1546 isl_union_set_preimage_union_pw_multi_aff(tree->domain,
1547 upma);
1548 if (!tree->domain)
1549 return isl_schedule_tree_free(tree);
1550 } else if (tree->type == isl_schedule_node_filter) {
1551 tree->filter =
1552 isl_union_set_preimage_union_pw_multi_aff(tree->filter,
1553 upma);
1554 if (!tree->filter)
1555 return isl_schedule_tree_free(tree);
1558 return tree;
1559 error:
1560 isl_union_pw_multi_aff_free(upma);
1561 isl_schedule_tree_free(tree);
1562 return NULL;
1565 /* Compute the gist of the band tree root with respect to "context".
1567 __isl_give isl_schedule_tree *isl_schedule_tree_band_gist(
1568 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *context)
1570 if (!tree)
1571 return NULL;
1572 if (tree->type != isl_schedule_node_band)
1573 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1574 "not a band node", goto error);
1575 tree = isl_schedule_tree_cow(tree);
1576 if (!tree)
1577 goto error;
1579 tree->band = isl_schedule_band_gist(tree->band, context);
1580 if (!tree->band)
1581 return isl_schedule_tree_free(tree);
1582 return tree;
1583 error:
1584 isl_union_set_free(context);
1585 isl_schedule_tree_free(tree);
1586 return NULL;
1589 /* Are any members in "band" marked coincident?
1591 static int any_coincident(__isl_keep isl_schedule_band *band)
1593 int i, n;
1595 n = isl_schedule_band_n_member(band);
1596 for (i = 0; i < n; ++i)
1597 if (isl_schedule_band_member_get_coincident(band, i))
1598 return 1;
1600 return 0;
1603 /* Print the band node "band" to "p".
1605 * The permutable and coincident properties are only printed if they
1606 * are different from the defaults.
1607 * The coincident property is always printed in YAML flow style.
1609 static __isl_give isl_printer *print_tree_band(__isl_take isl_printer *p,
1610 __isl_keep isl_schedule_band *band)
1612 p = isl_printer_print_str(p, "schedule");
1613 p = isl_printer_yaml_next(p);
1614 p = isl_printer_print_str(p, "\"");
1615 p = isl_printer_print_multi_union_pw_aff(p, band->mupa);
1616 p = isl_printer_print_str(p, "\"");
1617 if (isl_schedule_band_get_permutable(band)) {
1618 p = isl_printer_yaml_next(p);
1619 p = isl_printer_print_str(p, "permutable");
1620 p = isl_printer_yaml_next(p);
1621 p = isl_printer_print_int(p, 1);
1623 if (any_coincident(band)) {
1624 int i, n;
1625 int style;
1627 p = isl_printer_yaml_next(p);
1628 p = isl_printer_print_str(p, "coincident");
1629 p = isl_printer_yaml_next(p);
1630 style = isl_printer_get_yaml_style(p);
1631 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1632 p = isl_printer_yaml_start_sequence(p);
1633 n = isl_schedule_band_n_member(band);
1634 for (i = 0; i < n; ++i) {
1635 p = isl_printer_print_int(p,
1636 isl_schedule_band_member_get_coincident(band, i));
1637 p = isl_printer_yaml_next(p);
1639 p = isl_printer_yaml_end_sequence(p);
1640 p = isl_printer_set_yaml_style(p, style);
1643 return p;
1646 /* Print "tree" to "p".
1648 * If "n_ancestor" is non-negative, then "child_pos" contains the child
1649 * positions of a descendant of the current node that should be marked
1650 * (by the comment "YOU ARE HERE"). In particular, if "n_ancestor"
1651 * is zero, then the current node should be marked.
1652 * The marking is only printed in YAML block format.
1654 * Implicit leaf nodes are not printed, except if they correspond
1655 * to the node that should be marked.
1657 __isl_give isl_printer *isl_printer_print_schedule_tree_mark(
1658 __isl_take isl_printer *p, __isl_keep isl_schedule_tree *tree,
1659 int n_ancestor, int *child_pos)
1661 int i, n;
1662 int sequence = 0;
1663 int block;
1665 block = isl_printer_get_yaml_style(p) == ISL_YAML_STYLE_BLOCK;
1667 p = isl_printer_yaml_start_mapping(p);
1668 if (n_ancestor == 0 && block) {
1669 p = isl_printer_print_str(p, "# YOU ARE HERE");
1670 p = isl_printer_end_line(p);
1671 p = isl_printer_start_line(p);
1673 switch (tree->type) {
1674 case isl_schedule_node_error:
1675 p = isl_printer_print_str(p, "ERROR");
1676 break;
1677 case isl_schedule_node_leaf:
1678 p = isl_printer_print_str(p, "leaf");
1679 break;
1680 case isl_schedule_node_sequence:
1681 p = isl_printer_print_str(p, "sequence");
1682 sequence = 1;
1683 break;
1684 case isl_schedule_node_set:
1685 p = isl_printer_print_str(p, "set");
1686 sequence = 1;
1687 break;
1688 case isl_schedule_node_domain:
1689 p = isl_printer_print_str(p, "domain");
1690 p = isl_printer_yaml_next(p);
1691 p = isl_printer_print_str(p, "\"");
1692 p = isl_printer_print_union_set(p, tree->domain);
1693 p = isl_printer_print_str(p, "\"");
1694 break;
1695 case isl_schedule_node_filter:
1696 p = isl_printer_print_str(p, "filter");
1697 p = isl_printer_yaml_next(p);
1698 p = isl_printer_print_str(p, "\"");
1699 p = isl_printer_print_union_set(p, tree->filter);
1700 p = isl_printer_print_str(p, "\"");
1701 break;
1702 case isl_schedule_node_band:
1703 p = print_tree_band(p, tree->band);
1704 break;
1706 p = isl_printer_yaml_next(p);
1708 if (!tree->children) {
1709 if (n_ancestor > 0 && block) {
1710 isl_schedule_tree *leaf;
1712 p = isl_printer_print_str(p, "child");
1713 p = isl_printer_yaml_next(p);
1714 leaf = isl_schedule_tree_leaf(isl_printer_get_ctx(p));
1715 p = isl_printer_print_schedule_tree_mark(p,
1716 leaf, 0, NULL);
1717 isl_schedule_tree_free(leaf);
1718 p = isl_printer_yaml_next(p);
1720 return isl_printer_yaml_end_mapping(p);
1723 if (sequence) {
1724 p = isl_printer_yaml_start_sequence(p);
1725 } else {
1726 p = isl_printer_print_str(p, "child");
1727 p = isl_printer_yaml_next(p);
1730 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
1731 for (i = 0; i < n; ++i) {
1732 isl_schedule_tree *t;
1734 t = isl_schedule_tree_get_child(tree, i);
1735 if (n_ancestor > 0 && child_pos[0] == i)
1736 p = isl_printer_print_schedule_tree_mark(p, t,
1737 n_ancestor - 1, child_pos + 1);
1738 else
1739 p = isl_printer_print_schedule_tree_mark(p, t,
1740 -1, NULL);
1741 isl_schedule_tree_free(t);
1743 p = isl_printer_yaml_next(p);
1746 if (sequence)
1747 p = isl_printer_yaml_end_sequence(p);
1748 p = isl_printer_yaml_end_mapping(p);
1750 return p;
1753 /* Print "tree" to "p".
1755 __isl_give isl_printer *isl_printer_print_schedule_tree(
1756 __isl_take isl_printer *p, __isl_keep isl_schedule_tree *tree)
1758 return isl_printer_print_schedule_tree_mark(p, tree, -1, NULL);
1761 void isl_schedule_tree_dump(__isl_keep isl_schedule_tree *tree)
1763 isl_ctx *ctx;
1764 isl_printer *printer;
1766 if (!tree)
1767 return;
1769 ctx = isl_schedule_tree_get_ctx(tree);
1770 printer = isl_printer_to_file(ctx, stderr);
1771 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
1772 printer = isl_printer_print_schedule_tree(printer, tree);
1774 isl_printer_free(printer);