isl_test: avoid use of band forests
[isl.git] / isl_schedule_tree.c
blob0c045ef74e5f2bbcc8796da2e889bde23e8badd6
1 /*
2 * Copyright 2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
10 #include <isl/map.h>
11 #include <isl_schedule_band.h>
12 #include <isl_schedule_private.h>
14 #undef EL
15 #define EL isl_schedule_tree
17 #include <isl_list_templ.h>
19 #undef BASE
20 #define BASE schedule_tree
22 #include <isl_list_templ.c>
24 /* Is "tree" the leaf of a schedule tree?
26 int isl_schedule_tree_is_leaf(__isl_keep isl_schedule_tree *tree)
28 return isl_schedule_tree_get_type(tree) == isl_schedule_node_leaf;
31 /* Create a new schedule tree of type "type".
32 * The caller is responsible for filling in the type specific fields and
33 * the children.
35 static __isl_give isl_schedule_tree *isl_schedule_tree_alloc(isl_ctx *ctx,
36 enum isl_schedule_node_type type)
38 isl_schedule_tree *tree;
40 if (type == isl_schedule_node_error)
41 return NULL;
43 tree = isl_calloc_type(ctx, isl_schedule_tree);
44 if (!tree)
45 return NULL;
47 tree->ref = 1;
48 tree->ctx = ctx;
49 isl_ctx_ref(ctx);
50 tree->type = type;
52 return tree;
55 /* Return a fresh copy of "tree".
57 __isl_take isl_schedule_tree *isl_schedule_tree_dup(
58 __isl_keep isl_schedule_tree *tree)
60 isl_ctx *ctx;
61 isl_schedule_tree *dup;
63 if (!tree)
64 return NULL;
66 ctx = isl_schedule_tree_get_ctx(tree);
67 dup = isl_schedule_tree_alloc(ctx, tree->type);
68 if (!dup)
69 return NULL;
71 switch (tree->type) {
72 case isl_schedule_node_error:
73 isl_die(ctx, isl_error_internal,
74 "allocation should have failed",
75 isl_schedule_tree_free(dup));
76 case isl_schedule_node_band:
77 dup->band = isl_schedule_band_copy(tree->band);
78 if (!dup->band)
79 return isl_schedule_tree_free(dup);
80 break;
81 case isl_schedule_node_domain:
82 dup->domain = isl_union_set_copy(tree->domain);
83 if (!dup->domain)
84 return isl_schedule_tree_free(dup);
85 break;
86 case isl_schedule_node_filter:
87 dup->filter = isl_union_set_copy(tree->filter);
88 if (!dup->filter)
89 return isl_schedule_tree_free(dup);
90 break;
91 case isl_schedule_node_leaf:
92 case isl_schedule_node_sequence:
93 case isl_schedule_node_set:
94 break;
97 if (tree->children) {
98 dup->children = isl_schedule_tree_list_copy(tree->children);
99 if (!dup->children)
100 return isl_schedule_tree_free(dup);
103 return dup;
106 /* Return an isl_schedule_tree that is equal to "tree" and that has only
107 * a single reference.
109 * This function is called before a tree is modified.
110 * A static tree (with negative reference count) should never be modified,
111 * so it is not allowed to call this function on a static tree.
113 __isl_give isl_schedule_tree *isl_schedule_tree_cow(
114 __isl_take isl_schedule_tree *tree)
116 if (!tree)
117 return NULL;
119 if (tree->ref < 0)
120 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
121 "static trees cannot be modified",
122 return isl_schedule_tree_free(tree));
124 if (tree->ref == 1)
125 return tree;
126 tree->ref--;
127 return isl_schedule_tree_dup(tree);
130 /* Return a new reference to "tree".
132 * A static tree (with negative reference count) does not keep track
133 * of the number of references and should not be modified.
135 __isl_give isl_schedule_tree *isl_schedule_tree_copy(
136 __isl_keep isl_schedule_tree *tree)
138 if (!tree)
139 return NULL;
141 if (tree->ref < 0)
142 return tree;
144 tree->ref++;
145 return tree;
148 /* Free "tree" and return NULL.
150 __isl_null isl_schedule_tree *isl_schedule_tree_free(
151 __isl_take isl_schedule_tree *tree)
153 if (!tree)
154 return NULL;
155 if (tree->ref < 0)
156 return NULL;
157 if (--tree->ref > 0)
158 return NULL;
160 switch (tree->type) {
161 case isl_schedule_node_band:
162 isl_schedule_band_free(tree->band);
163 break;
164 case isl_schedule_node_domain:
165 isl_union_set_free(tree->domain);
166 break;
167 case isl_schedule_node_filter:
168 isl_union_set_free(tree->filter);
169 break;
170 case isl_schedule_node_sequence:
171 case isl_schedule_node_set:
172 case isl_schedule_node_error:
173 case isl_schedule_node_leaf:
174 break;
176 isl_schedule_tree_list_free(tree->children);
177 isl_ctx_deref(tree->ctx);
178 free(tree);
180 return NULL;
183 /* Create and return a new leaf schedule tree.
185 __isl_give isl_schedule_tree *isl_schedule_tree_leaf(isl_ctx *ctx)
187 return isl_schedule_tree_alloc(ctx, isl_schedule_node_leaf);
190 /* Create a new band schedule tree referring to "band"
191 * with no children.
193 __isl_give isl_schedule_tree *isl_schedule_tree_from_band(
194 __isl_take isl_schedule_band *band)
196 isl_ctx *ctx;
197 isl_schedule_tree *tree;
199 if (!band)
200 return NULL;
202 ctx = isl_schedule_band_get_ctx(band);
203 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_band);
204 if (!tree)
205 goto error;
207 tree->band = band;
209 return tree;
210 error:
211 isl_schedule_band_free(band);
212 return NULL;
215 /* Create a new domain schedule tree with the given domain and no children.
217 __isl_give isl_schedule_tree *isl_schedule_tree_from_domain(
218 __isl_take isl_union_set *domain)
220 isl_ctx *ctx;
221 isl_schedule_tree *tree;
223 if (!domain)
224 return NULL;
226 ctx = isl_union_set_get_ctx(domain);
227 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_domain);
228 if (!tree)
229 goto error;
231 tree->domain = domain;
233 return tree;
234 error:
235 isl_union_set_free(domain);
236 return NULL;
239 /* Create a new filter schedule tree with the given filter and no children.
241 __isl_give isl_schedule_tree *isl_schedule_tree_from_filter(
242 __isl_take isl_union_set *filter)
244 isl_ctx *ctx;
245 isl_schedule_tree *tree;
247 if (!filter)
248 return NULL;
250 ctx = isl_union_set_get_ctx(filter);
251 tree = isl_schedule_tree_alloc(ctx, isl_schedule_node_filter);
252 if (!tree)
253 goto error;
255 tree->filter = filter;
257 return tree;
258 error:
259 isl_union_set_free(filter);
260 return NULL;
263 /* Create a new tree of the given type (isl_schedule_node_sequence or
264 * isl_schedule_node_set) with the given children.
266 __isl_give isl_schedule_tree *isl_schedule_tree_from_children(
267 enum isl_schedule_node_type type,
268 __isl_take isl_schedule_tree_list *list)
270 isl_ctx *ctx;
271 isl_schedule_tree *tree;
273 if (!list)
274 return NULL;
276 ctx = isl_schedule_tree_list_get_ctx(list);
277 tree = isl_schedule_tree_alloc(ctx, type);
278 if (!tree)
279 goto error;
281 tree->children = list;
283 return tree;
284 error:
285 isl_schedule_tree_list_free(list);
286 return NULL;
289 /* Return the isl_ctx to which "tree" belongs.
291 isl_ctx *isl_schedule_tree_get_ctx(__isl_keep isl_schedule_tree *tree)
293 return tree ? tree->ctx : NULL;
296 /* Return the type of the root of the tree or isl_schedule_node_error
297 * on error.
299 enum isl_schedule_node_type isl_schedule_tree_get_type(
300 __isl_keep isl_schedule_tree *tree)
302 return tree ? tree->type : isl_schedule_node_error;
305 /* Does "tree" have any children, other than an implicit leaf.
307 int isl_schedule_tree_has_children(__isl_keep isl_schedule_tree *tree)
309 if (!tree)
310 return -1;
312 return tree->children != NULL;
315 /* Return the number of children of "tree", excluding implicit leaves.
317 int isl_schedule_tree_n_children(__isl_keep isl_schedule_tree *tree)
319 if (!tree)
320 return -1;
322 return isl_schedule_tree_list_n_schedule_tree(tree->children);
325 /* Return a copy of the (explicit) child at position "pos" of "tree".
327 __isl_give isl_schedule_tree *isl_schedule_tree_get_child(
328 __isl_keep isl_schedule_tree *tree, int pos)
330 if (!tree)
331 return NULL;
332 if (!tree->children)
333 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
334 "schedule tree has no explicit children", return NULL);
335 return isl_schedule_tree_list_get_schedule_tree(tree->children, pos);
338 /* Return a copy of the (explicit) child at position "pos" of "tree" and
339 * free "tree".
341 __isl_give isl_schedule_tree *isl_schedule_tree_child(
342 __isl_take isl_schedule_tree *tree, int pos)
344 isl_schedule_tree *child;
346 child = isl_schedule_tree_get_child(tree, pos);
347 isl_schedule_tree_free(tree);
348 return child;
351 /* Remove all (explicit) children from "tree".
353 __isl_give isl_schedule_tree *isl_schedule_tree_reset_children(
354 __isl_take isl_schedule_tree *tree)
356 tree = isl_schedule_tree_cow(tree);
357 if (!tree)
358 return NULL;
359 tree->children = isl_schedule_tree_list_free(tree->children);
360 return tree;
363 /* Replace the child at position "pos" of "tree" by "child".
365 * If the new child is a leaf, then it is not explicitly
366 * recorded in the list of children. Instead, the list of children
367 * (which is assumed to have only one element) is removed.
368 * Note that the children of set and sequence nodes are always
369 * filters, so they cannot be replaced by empty trees.
371 __isl_give isl_schedule_tree *isl_schedule_tree_replace_child(
372 __isl_take isl_schedule_tree *tree, int pos,
373 __isl_take isl_schedule_tree *child)
375 tree = isl_schedule_tree_cow(tree);
376 if (!tree || !child)
377 goto error;
379 if (isl_schedule_tree_is_leaf(child)) {
380 isl_schedule_tree_free(child);
381 if (!tree->children && pos == 0)
382 return tree;
383 if (isl_schedule_tree_n_children(tree) != 1)
384 isl_die(isl_schedule_tree_get_ctx(tree),
385 isl_error_internal,
386 "can only replace single child by leaf",
387 goto error);
388 return isl_schedule_tree_reset_children(tree);
391 if (!tree->children && pos == 0)
392 tree->children =
393 isl_schedule_tree_list_from_schedule_tree(child);
394 else
395 tree->children = isl_schedule_tree_list_set_schedule_tree(
396 tree->children, pos, child);
398 if (!tree->children)
399 return isl_schedule_tree_free(tree);
401 return tree;
402 error:
403 isl_schedule_tree_free(tree);
404 isl_schedule_tree_free(child);
405 return NULL;
408 /* Create a new band schedule tree referring to "band"
409 * with "tree" as single child.
411 __isl_give isl_schedule_tree *isl_schedule_tree_insert_band(
412 __isl_take isl_schedule_tree *tree, __isl_take isl_schedule_band *band)
414 isl_schedule_tree *res;
416 res = isl_schedule_tree_from_band(band);
417 return isl_schedule_tree_replace_child(res, 0, tree);
420 /* Create a new domain schedule tree with the given domain and
421 * with "tree" as single child.
423 __isl_give isl_schedule_tree *isl_schedule_tree_insert_domain(
424 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *domain)
426 isl_schedule_tree *res;
428 res = isl_schedule_tree_from_domain(domain);
429 return isl_schedule_tree_replace_child(res, 0, tree);
432 /* Create a new filter schedule tree with the given filter and single child.
434 * If the root of "tree" is itself a filter node, then the two
435 * filter nodes are merged into one node.
437 __isl_give isl_schedule_tree *isl_schedule_tree_insert_filter(
438 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter)
440 isl_schedule_tree *res;
442 if (isl_schedule_tree_get_type(tree) == isl_schedule_node_filter) {
443 isl_union_set *tree_filter;
445 tree_filter = isl_schedule_tree_filter_get_filter(tree);
446 tree_filter = isl_union_set_intersect(tree_filter, filter);
447 tree = isl_schedule_tree_filter_set_filter(tree, tree_filter);
448 return tree;
451 res = isl_schedule_tree_from_filter(filter);
452 return isl_schedule_tree_replace_child(res, 0, tree);
455 /* Return the number of members in the band tree root.
457 unsigned isl_schedule_tree_band_n_member(__isl_keep isl_schedule_tree *tree)
459 if (!tree)
460 return 0;
462 if (tree->type != isl_schedule_node_band)
463 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
464 "not a band node", return 0);
466 return isl_schedule_band_n_member(tree->band);
469 /* Is the band member at position "pos" of the band tree root
470 * marked coincident?
472 int isl_schedule_tree_band_member_get_coincident(
473 __isl_keep isl_schedule_tree *tree, int pos)
475 if (!tree)
476 return -1;
478 if (tree->type != isl_schedule_node_band)
479 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
480 "not a band node", return -1);
482 return isl_schedule_band_member_get_coincident(tree->band, pos);
485 /* Mark the given band member as being coincident or not
486 * according to "coincident".
488 __isl_give isl_schedule_tree *isl_schedule_tree_band_member_set_coincident(
489 __isl_take isl_schedule_tree *tree, int pos, int coincident)
491 if (!tree)
492 return NULL;
493 if (tree->type != isl_schedule_node_band)
494 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
495 "not a band node", return isl_schedule_tree_free(tree));
496 if (isl_schedule_tree_band_member_get_coincident(tree, pos) ==
497 coincident)
498 return tree;
499 tree = isl_schedule_tree_cow(tree);
500 if (!tree)
501 return NULL;
503 tree->band = isl_schedule_band_member_set_coincident(tree->band, pos,
504 coincident);
505 if (!tree->band)
506 return isl_schedule_tree_free(tree);
507 return tree;
510 /* Is the band tree root marked permutable?
512 int isl_schedule_tree_band_get_permutable(__isl_keep isl_schedule_tree *tree)
514 if (!tree)
515 return -1;
517 if (tree->type != isl_schedule_node_band)
518 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
519 "not a band node", return -1);
521 return isl_schedule_band_get_permutable(tree->band);
524 /* Mark the band tree root permutable or not according to "permutable"?
526 __isl_give isl_schedule_tree *isl_schedule_tree_band_set_permutable(
527 __isl_take isl_schedule_tree *tree, int permutable)
529 if (!tree)
530 return NULL;
531 if (tree->type != isl_schedule_node_band)
532 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
533 "not a band node", return isl_schedule_tree_free(tree));
534 if (isl_schedule_tree_band_get_permutable(tree) == permutable)
535 return tree;
536 tree = isl_schedule_tree_cow(tree);
537 if (!tree)
538 return NULL;
540 tree->band = isl_schedule_band_set_permutable(tree->band, permutable);
541 if (!tree->band)
542 return isl_schedule_tree_free(tree);
543 return tree;
546 /* Return the schedule of the band tree root in isolation.
548 __isl_give isl_multi_union_pw_aff *isl_schedule_tree_band_get_partial_schedule(
549 __isl_keep isl_schedule_tree *tree)
551 if (!tree)
552 return NULL;
554 if (tree->type != isl_schedule_node_band)
555 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
556 "not a band node", return NULL);
558 return isl_schedule_band_get_partial_schedule(tree->band);
561 /* Return the domain of the domain tree root.
563 __isl_give isl_union_set *isl_schedule_tree_domain_get_domain(
564 __isl_keep isl_schedule_tree *tree)
566 if (!tree)
567 return NULL;
569 if (tree->type != isl_schedule_node_domain)
570 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
571 "not a domain node", return NULL);
573 return isl_union_set_copy(tree->domain);
576 /* Replace the domain of domain tree root "tree" by "domain".
578 __isl_give isl_schedule_tree *isl_schedule_tree_domain_set_domain(
579 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *domain)
581 tree = isl_schedule_tree_cow(tree);
582 if (!tree || !domain)
583 goto error;
585 if (tree->type != isl_schedule_node_domain)
586 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
587 "not a domain node", goto error);
589 isl_union_set_free(tree->domain);
590 tree->domain = domain;
592 return tree;
593 error:
594 isl_schedule_tree_free(tree);
595 isl_union_set_free(domain);
596 return NULL;
599 /* Return the filter of the filter tree root.
601 __isl_give isl_union_set *isl_schedule_tree_filter_get_filter(
602 __isl_keep isl_schedule_tree *tree)
604 if (!tree)
605 return NULL;
607 if (tree->type != isl_schedule_node_filter)
608 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
609 "not a filter node", return NULL);
611 return isl_union_set_copy(tree->filter);
614 /* Replace the filter of the filter tree root by "filter".
616 __isl_give isl_schedule_tree *isl_schedule_tree_filter_set_filter(
617 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter)
619 tree = isl_schedule_tree_cow(tree);
620 if (!tree || !filter)
621 goto error;
623 if (tree->type != isl_schedule_node_filter)
624 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
625 "not a filter node", return NULL);
627 isl_union_set_free(tree->filter);
628 tree->filter = filter;
630 return tree;
631 error:
632 isl_schedule_tree_free(tree);
633 isl_union_set_free(filter);
634 return NULL;
637 /* Set dim to the range dimension of "map" and abort the search.
639 static int set_range_dim(__isl_take isl_map *map, void *user)
641 int *dim = user;
643 *dim = isl_map_dim(map, isl_dim_out);
644 isl_map_free(map);
646 return -1;
649 /* Return the dimension of the range of "umap".
650 * "umap" is assumed not to be empty and
651 * all maps inside "umap" are assumed to have the same range.
653 * We extract the range dimension from the first map in "umap".
655 static int range_dim(__isl_keep isl_union_map *umap)
657 int dim = -1;
659 if (!umap)
660 return -1;
661 if (isl_union_map_n_map(umap) == 0)
662 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,
663 "unexpected empty input", return -1);
665 isl_union_map_foreach_map(umap, &set_range_dim, &dim);
667 return dim;
670 /* Append an "extra" number of zeros to the range of "umap" and
671 * return the result.
673 static __isl_give isl_union_map *append_range(__isl_take isl_union_map *umap,
674 int extra)
676 isl_union_set *dom;
677 isl_space *space;
678 isl_multi_val *mv;
679 isl_union_pw_multi_aff *suffix;
680 isl_union_map *universe;
681 isl_union_map *suffix_umap;
683 universe = isl_union_map_universe(isl_union_map_copy(umap));
684 dom = isl_union_map_domain(universe);
685 space = isl_union_set_get_space(dom);
686 space = isl_space_set_from_params(space);
687 space = isl_space_add_dims(space, isl_dim_set, extra);
688 mv = isl_multi_val_zero(space);
690 suffix = isl_union_pw_multi_aff_multi_val_on_domain(dom, mv);
691 suffix_umap = isl_union_map_from_union_pw_multi_aff(suffix);
692 umap = isl_union_map_flat_range_product(umap, suffix_umap);
694 return umap;
697 /* Move down to the first descendant of "tree" that contains any schedule
698 * information or return "leaf" if there is no such descendant.
700 __isl_give isl_schedule_tree *isl_schedule_tree_first_schedule_descendant(
701 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_tree *leaf)
703 while (isl_schedule_tree_get_type(tree) == isl_schedule_node_band &&
704 isl_schedule_tree_band_n_member(tree) == 0) {
705 if (!isl_schedule_tree_has_children(tree)) {
706 isl_schedule_tree_free(tree);
707 return isl_schedule_tree_copy(leaf);
709 tree = isl_schedule_tree_child(tree, 0);
712 return tree;
715 static __isl_give isl_union_map *subtree_schedule_extend(
716 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer);
718 /* Extend the schedule map "outer" with the subtree schedule
719 * of the (single) child of "tree", if any.
721 * If "tree" does not have any descendants (apart from those that
722 * do not carry any schedule information), then we simply return "outer".
723 * Otherwise, we extend the schedule map "outer" with the subtree schedule
724 * of the single child.
726 static __isl_give isl_union_map *subtree_schedule_extend_child(
727 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
729 isl_schedule_tree *child;
730 isl_union_map *res;
732 if (!tree)
733 return isl_union_map_free(outer);
734 if (!isl_schedule_tree_has_children(tree))
735 return outer;
736 child = isl_schedule_tree_get_child(tree, 0);
737 if (!child)
738 return isl_union_map_free(outer);
739 res = subtree_schedule_extend(child, outer);
740 isl_schedule_tree_free(child);
741 return res;
744 /* Extract the parameter space from one of the children of "tree",
745 * which are assumed to be filters.
747 static __isl_give isl_space *extract_space_from_filter_child(
748 __isl_keep isl_schedule_tree *tree)
750 isl_space *space;
751 isl_union_set *dom;
752 isl_schedule_tree *child;
754 child = isl_schedule_tree_list_get_schedule_tree(tree->children, 0);
755 dom = isl_schedule_tree_filter_get_filter(child);
756 space = isl_union_set_get_space(dom);
757 isl_union_set_free(dom);
758 isl_schedule_tree_free(child);
760 return space;
763 /* Extend the schedule map "outer" with the subtree schedule
764 * of a set or sequence node.
766 * The schedule for the set or sequence node itself is composed of
767 * pieces of the form
769 * filter -> []
771 * or
773 * filter -> [index]
775 * The first form is used if there is only a single child or
776 * if the current node is a set node and the schedule_separate_components
777 * option is not set.
779 * Each of the pieces above is extended with the subtree schedule of
780 * the child of the corresponding filter, if any, padded with zeros
781 * to ensure that all pieces have the same range dimension.
783 static __isl_give isl_union_map *subtree_schedule_extend_from_children(
784 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
786 int i, n;
787 int dim;
788 int separate;
789 isl_ctx *ctx;
790 isl_val *v = NULL;
791 isl_multi_val *mv;
792 isl_space *space;
793 isl_union_map *umap;
795 if (!tree)
796 return NULL;
798 ctx = isl_schedule_tree_get_ctx(tree);
799 if (!tree->children)
800 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
801 "missing children", return NULL);
802 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
803 if (n == 0)
804 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
805 "missing children", return NULL);
807 separate = n > 1 && (tree->type == isl_schedule_node_sequence ||
808 isl_options_get_schedule_separate_components(ctx));
810 space = extract_space_from_filter_child(tree);
812 umap = isl_union_map_empty(isl_space_copy(space));
813 space = isl_space_set_from_params(space);
814 if (separate) {
815 space = isl_space_add_dims(space, isl_dim_set, 1);
816 v = isl_val_zero(ctx);
818 mv = isl_multi_val_zero(space);
820 dim = isl_multi_val_dim(mv, isl_dim_set);
821 for (i = 0; i < n; ++i) {
822 isl_union_pw_multi_aff *upma;
823 isl_union_map *umap_i;
824 isl_union_set *dom;
825 isl_schedule_tree *child;
826 int dim_i;
827 int empty;
829 child = isl_schedule_tree_list_get_schedule_tree(
830 tree->children, i);
831 dom = isl_schedule_tree_filter_get_filter(child);
833 if (separate) {
834 mv = isl_multi_val_set_val(mv, 0, isl_val_copy(v));
835 v = isl_val_add_ui(v, 1);
837 upma = isl_union_pw_multi_aff_multi_val_on_domain(dom,
838 isl_multi_val_copy(mv));
839 umap_i = isl_union_map_from_union_pw_multi_aff(upma);
840 umap_i = isl_union_map_flat_range_product(
841 isl_union_map_copy(outer), umap_i);
842 umap_i = subtree_schedule_extend_child(child, umap_i);
843 isl_schedule_tree_free(child);
845 empty = isl_union_map_is_empty(umap_i);
846 if (empty < 0)
847 umap_i = isl_union_map_free(umap_i);
848 else if (empty) {
849 isl_union_map_free(umap_i);
850 continue;
853 dim_i = range_dim(umap_i);
854 if (dim_i < 0) {
855 umap = isl_union_map_free(umap);
856 } else if (dim < dim_i) {
857 umap = append_range(umap, dim_i - dim);
858 dim = dim_i;
859 } else if (dim_i < dim) {
860 umap_i = append_range(umap_i, dim - dim_i);
862 umap = isl_union_map_union(umap, umap_i);
865 isl_val_free(v);
866 isl_multi_val_free(mv);
867 isl_union_map_free(outer);
869 return umap;
872 /* Extend the schedule map "outer" with the subtree schedule of "tree".
874 * If the root of the tree is a set or a sequence, then we extend
875 * the schedule map in subtree_schedule_extend_from_children.
876 * Otherwise, we extend the schedule map with the partial schedule
877 * corresponding to the root of the tree and then continue with
878 * the single child of this root.
880 static __isl_give isl_union_map *subtree_schedule_extend(
881 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
883 isl_multi_union_pw_aff *mupa;
884 isl_union_map *umap;
885 isl_union_set *domain;
887 if (!tree)
888 return NULL;
890 switch (tree->type) {
891 case isl_schedule_node_error:
892 return isl_union_map_free(outer);
893 case isl_schedule_node_band:
894 if (isl_schedule_tree_band_n_member(tree) == 0)
895 return subtree_schedule_extend_child(tree, outer);
896 mupa = isl_schedule_band_get_partial_schedule(tree->band);
897 umap = isl_union_map_from_multi_union_pw_aff(mupa);
898 outer = isl_union_map_flat_range_product(outer, umap);
899 umap = subtree_schedule_extend_child(tree, outer);
900 break;
901 case isl_schedule_node_domain:
902 domain = isl_schedule_tree_domain_get_domain(tree);
903 umap = isl_union_map_from_domain(domain);
904 outer = isl_union_map_flat_range_product(outer, umap);
905 umap = subtree_schedule_extend_child(tree, outer);
906 break;
907 case isl_schedule_node_filter:
908 domain = isl_schedule_tree_filter_get_filter(tree);
909 umap = isl_union_map_from_domain(domain);
910 outer = isl_union_map_flat_range_product(outer, umap);
911 umap = subtree_schedule_extend_child(tree, outer);
912 break;
913 case isl_schedule_node_leaf:
914 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
915 "leaf node should be handled by caller", return NULL);
916 case isl_schedule_node_set:
917 case isl_schedule_node_sequence:
918 umap = subtree_schedule_extend_from_children(tree, outer);
919 break;
922 return umap;
925 static __isl_give isl_union_set *initial_domain(
926 __isl_keep isl_schedule_tree *tree);
928 /* Extract a universe domain from the children of the tree root "tree",
929 * which is a set or sequence, meaning that its children are filters.
930 * In particular, return the union of the universes of the filters.
932 static __isl_give isl_union_set *initial_domain_from_children(
933 __isl_keep isl_schedule_tree *tree)
935 int i, n;
936 isl_space *space;
937 isl_union_set *domain;
939 if (!tree->children)
940 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
941 "missing children", return NULL);
942 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
943 if (n == 0)
944 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
945 "missing children", return NULL);
947 space = extract_space_from_filter_child(tree);
948 domain = isl_union_set_empty(space);
950 for (i = 0; i < n; ++i) {
951 isl_schedule_tree *child;
952 isl_union_set *domain_i;
954 child = isl_schedule_tree_get_child(tree, i);
955 domain_i = initial_domain(child);
956 domain = isl_union_set_union(domain, domain_i);
957 isl_schedule_tree_free(child);
960 return domain;
963 /* Extract a universe domain from the tree root "tree".
964 * The caller is responsible for making sure that this node
965 * would not be skipped by isl_schedule_tree_first_schedule_descendant
966 * and that it is not a leaf node.
968 static __isl_give isl_union_set *initial_domain(
969 __isl_keep isl_schedule_tree *tree)
971 isl_multi_union_pw_aff *mupa;
972 isl_union_set *domain;
974 if (!tree)
975 return NULL;
977 switch (tree->type) {
978 case isl_schedule_node_error:
979 return NULL;
980 case isl_schedule_node_band:
981 if (isl_schedule_tree_band_n_member(tree) == 0)
982 isl_die(isl_schedule_tree_get_ctx(tree),
983 isl_error_internal,
984 "0D band should be handled by caller",
985 return NULL);
986 mupa = isl_schedule_band_get_partial_schedule(tree->band);
987 domain = isl_multi_union_pw_aff_domain(mupa);
988 domain = isl_union_set_universe(domain);
989 break;
990 case isl_schedule_node_domain:
991 domain = isl_schedule_tree_domain_get_domain(tree);
992 domain = isl_union_set_universe(domain);
993 break;
994 case isl_schedule_node_filter:
995 domain = isl_schedule_tree_filter_get_filter(tree);
996 domain = isl_union_set_universe(domain);
997 break;
998 case isl_schedule_node_leaf:
999 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
1000 "leaf node should be handled by caller", return NULL);
1001 case isl_schedule_node_set:
1002 case isl_schedule_node_sequence:
1003 domain = initial_domain_from_children(tree);
1004 break;
1007 return domain;
1010 /* Return the subtree schedule of a node that contains some schedule
1011 * information, i.e., a node that would not be skipped by
1012 * isl_schedule_tree_first_schedule_descendant and that is not a leaf.
1014 * We start with an initial zero-dimensional subtree schedule based
1015 * on the domain information in the root node and then extend it
1016 * based on the schedule information in the root node and its descendants.
1018 __isl_give isl_union_map *isl_schedule_tree_get_subtree_schedule_union_map(
1019 __isl_keep isl_schedule_tree *tree)
1021 isl_union_set *domain;
1022 isl_union_map *umap;
1024 domain = initial_domain(tree);
1025 umap = isl_union_map_from_domain(domain);
1026 return subtree_schedule_extend(tree, umap);
1029 /* Are any members in "band" marked coincident?
1031 static int any_coincident(__isl_keep isl_schedule_band *band)
1033 int i, n;
1035 n = isl_schedule_band_n_member(band);
1036 for (i = 0; i < n; ++i)
1037 if (isl_schedule_band_member_get_coincident(band, i))
1038 return 1;
1040 return 0;
1043 /* Print the band node "band" to "p".
1045 * The permutable and coincident properties are only printed if they
1046 * are different from the defaults.
1047 * The coincident property is always printed in YAML flow style.
1049 static __isl_give isl_printer *print_tree_band(__isl_take isl_printer *p,
1050 __isl_keep isl_schedule_band *band)
1052 p = isl_printer_print_str(p, "schedule");
1053 p = isl_printer_yaml_next(p);
1054 p = isl_printer_print_str(p, "\"");
1055 p = isl_printer_print_multi_union_pw_aff(p, band->mupa);
1056 p = isl_printer_print_str(p, "\"");
1057 if (isl_schedule_band_get_permutable(band)) {
1058 p = isl_printer_yaml_next(p);
1059 p = isl_printer_print_str(p, "permutable");
1060 p = isl_printer_yaml_next(p);
1061 p = isl_printer_print_int(p, 1);
1063 if (any_coincident(band)) {
1064 int i, n;
1065 int style;
1067 p = isl_printer_yaml_next(p);
1068 p = isl_printer_print_str(p, "coincident");
1069 p = isl_printer_yaml_next(p);
1070 style = isl_printer_get_yaml_style(p);
1071 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1072 p = isl_printer_yaml_start_sequence(p);
1073 n = isl_schedule_band_n_member(band);
1074 for (i = 0; i < n; ++i) {
1075 p = isl_printer_print_int(p,
1076 isl_schedule_band_member_get_coincident(band, i));
1077 p = isl_printer_yaml_next(p);
1079 p = isl_printer_yaml_end_sequence(p);
1080 p = isl_printer_set_yaml_style(p, style);
1083 return p;
1086 /* Print "tree" to "p".
1088 * If "n_ancestor" is non-negative, then "child_pos" contains the child
1089 * positions of a descendant of the current node that should be marked
1090 * (by the comment "YOU ARE HERE"). In particular, if "n_ancestor"
1091 * is zero, then the current node should be marked.
1092 * The marking is only printed in YAML block format.
1094 * Implicit leaf nodes are not printed, except if they correspond
1095 * to the node that should be marked.
1097 __isl_give isl_printer *isl_printer_print_schedule_tree_mark(
1098 __isl_take isl_printer *p, __isl_keep isl_schedule_tree *tree,
1099 int n_ancestor, int *child_pos)
1101 int i, n;
1102 int sequence = 0;
1103 int block;
1105 block = isl_printer_get_yaml_style(p) == ISL_YAML_STYLE_BLOCK;
1107 p = isl_printer_yaml_start_mapping(p);
1108 if (n_ancestor == 0 && block) {
1109 p = isl_printer_print_str(p, "# YOU ARE HERE");
1110 p = isl_printer_end_line(p);
1111 p = isl_printer_start_line(p);
1113 switch (tree->type) {
1114 case isl_schedule_node_error:
1115 p = isl_printer_print_str(p, "ERROR");
1116 break;
1117 case isl_schedule_node_leaf:
1118 p = isl_printer_print_str(p, "leaf");
1119 break;
1120 case isl_schedule_node_sequence:
1121 p = isl_printer_print_str(p, "sequence");
1122 sequence = 1;
1123 break;
1124 case isl_schedule_node_set:
1125 p = isl_printer_print_str(p, "set");
1126 sequence = 1;
1127 break;
1128 case isl_schedule_node_domain:
1129 p = isl_printer_print_str(p, "domain");
1130 p = isl_printer_yaml_next(p);
1131 p = isl_printer_print_str(p, "\"");
1132 p = isl_printer_print_union_set(p, tree->domain);
1133 p = isl_printer_print_str(p, "\"");
1134 break;
1135 case isl_schedule_node_filter:
1136 p = isl_printer_print_str(p, "filter");
1137 p = isl_printer_yaml_next(p);
1138 p = isl_printer_print_str(p, "\"");
1139 p = isl_printer_print_union_set(p, tree->filter);
1140 p = isl_printer_print_str(p, "\"");
1141 break;
1142 case isl_schedule_node_band:
1143 p = print_tree_band(p, tree->band);
1144 break;
1146 p = isl_printer_yaml_next(p);
1148 if (!tree->children) {
1149 if (n_ancestor > 0 && block) {
1150 isl_schedule_tree *leaf;
1152 p = isl_printer_print_str(p, "child");
1153 p = isl_printer_yaml_next(p);
1154 leaf = isl_schedule_tree_leaf(isl_printer_get_ctx(p));
1155 p = isl_printer_print_schedule_tree_mark(p,
1156 leaf, 0, NULL);
1157 isl_schedule_tree_free(leaf);
1158 p = isl_printer_yaml_next(p);
1160 return isl_printer_yaml_end_mapping(p);
1163 if (sequence) {
1164 p = isl_printer_yaml_start_sequence(p);
1165 } else {
1166 p = isl_printer_print_str(p, "child");
1167 p = isl_printer_yaml_next(p);
1170 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
1171 for (i = 0; i < n; ++i) {
1172 isl_schedule_tree *t;
1174 t = isl_schedule_tree_get_child(tree, i);
1175 if (n_ancestor > 0 && child_pos[0] == i)
1176 p = isl_printer_print_schedule_tree_mark(p, t,
1177 n_ancestor - 1, child_pos + 1);
1178 else
1179 p = isl_printer_print_schedule_tree_mark(p, t,
1180 -1, NULL);
1181 isl_schedule_tree_free(t);
1183 p = isl_printer_yaml_next(p);
1186 if (sequence)
1187 p = isl_printer_yaml_end_sequence(p);
1188 p = isl_printer_yaml_end_mapping(p);
1190 return p;
1193 /* Print "tree" to "p".
1195 __isl_give isl_printer *isl_printer_print_schedule_tree(
1196 __isl_take isl_printer *p, __isl_keep isl_schedule_tree *tree)
1198 return isl_printer_print_schedule_tree_mark(p, tree, -1, NULL);
1201 void isl_schedule_tree_dump(__isl_keep isl_schedule_tree *tree)
1203 isl_ctx *ctx;
1204 isl_printer *printer;
1206 if (!tree)
1207 return;
1209 ctx = isl_schedule_tree_get_ctx(tree);
1210 printer = isl_printer_to_file(ctx, stderr);
1211 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
1212 printer = isl_printer_print_schedule_tree(printer, tree);
1214 isl_printer_free(printer);