add isl_schedule_node_band_scale_down
[isl.git] / isl_schedule_tree.c
blob5a0858b50bde6dbaa65acab5e2f7df659f1e9cad
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 space of the band tree root.
548 __isl_give isl_space *isl_schedule_tree_band_get_space(
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_space(tree->band);
561 /* Return the schedule of the band tree root in isolation.
563 __isl_give isl_multi_union_pw_aff *isl_schedule_tree_band_get_partial_schedule(
564 __isl_keep isl_schedule_tree *tree)
566 if (!tree)
567 return NULL;
569 if (tree->type != isl_schedule_node_band)
570 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
571 "not a band node", return NULL);
573 return isl_schedule_band_get_partial_schedule(tree->band);
576 /* Return the domain of the domain tree root.
578 __isl_give isl_union_set *isl_schedule_tree_domain_get_domain(
579 __isl_keep isl_schedule_tree *tree)
581 if (!tree)
582 return NULL;
584 if (tree->type != isl_schedule_node_domain)
585 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
586 "not a domain node", return NULL);
588 return isl_union_set_copy(tree->domain);
591 /* Replace the domain of domain tree root "tree" by "domain".
593 __isl_give isl_schedule_tree *isl_schedule_tree_domain_set_domain(
594 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *domain)
596 tree = isl_schedule_tree_cow(tree);
597 if (!tree || !domain)
598 goto error;
600 if (tree->type != isl_schedule_node_domain)
601 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
602 "not a domain node", goto error);
604 isl_union_set_free(tree->domain);
605 tree->domain = domain;
607 return tree;
608 error:
609 isl_schedule_tree_free(tree);
610 isl_union_set_free(domain);
611 return NULL;
614 /* Return the filter of the filter tree root.
616 __isl_give isl_union_set *isl_schedule_tree_filter_get_filter(
617 __isl_keep isl_schedule_tree *tree)
619 if (!tree)
620 return NULL;
622 if (tree->type != isl_schedule_node_filter)
623 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
624 "not a filter node", return NULL);
626 return isl_union_set_copy(tree->filter);
629 /* Replace the filter of the filter tree root by "filter".
631 __isl_give isl_schedule_tree *isl_schedule_tree_filter_set_filter(
632 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter)
634 tree = isl_schedule_tree_cow(tree);
635 if (!tree || !filter)
636 goto error;
638 if (tree->type != isl_schedule_node_filter)
639 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
640 "not a filter node", return NULL);
642 isl_union_set_free(tree->filter);
643 tree->filter = filter;
645 return tree;
646 error:
647 isl_schedule_tree_free(tree);
648 isl_union_set_free(filter);
649 return NULL;
652 /* Set dim to the range dimension of "map" and abort the search.
654 static int set_range_dim(__isl_take isl_map *map, void *user)
656 int *dim = user;
658 *dim = isl_map_dim(map, isl_dim_out);
659 isl_map_free(map);
661 return -1;
664 /* Return the dimension of the range of "umap".
665 * "umap" is assumed not to be empty and
666 * all maps inside "umap" are assumed to have the same range.
668 * We extract the range dimension from the first map in "umap".
670 static int range_dim(__isl_keep isl_union_map *umap)
672 int dim = -1;
674 if (!umap)
675 return -1;
676 if (isl_union_map_n_map(umap) == 0)
677 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,
678 "unexpected empty input", return -1);
680 isl_union_map_foreach_map(umap, &set_range_dim, &dim);
682 return dim;
685 /* Append an "extra" number of zeros to the range of "umap" and
686 * return the result.
688 static __isl_give isl_union_map *append_range(__isl_take isl_union_map *umap,
689 int extra)
691 isl_union_set *dom;
692 isl_space *space;
693 isl_multi_val *mv;
694 isl_union_pw_multi_aff *suffix;
695 isl_union_map *universe;
696 isl_union_map *suffix_umap;
698 universe = isl_union_map_universe(isl_union_map_copy(umap));
699 dom = isl_union_map_domain(universe);
700 space = isl_union_set_get_space(dom);
701 space = isl_space_set_from_params(space);
702 space = isl_space_add_dims(space, isl_dim_set, extra);
703 mv = isl_multi_val_zero(space);
705 suffix = isl_union_pw_multi_aff_multi_val_on_domain(dom, mv);
706 suffix_umap = isl_union_map_from_union_pw_multi_aff(suffix);
707 umap = isl_union_map_flat_range_product(umap, suffix_umap);
709 return umap;
712 /* Move down to the first descendant of "tree" that contains any schedule
713 * information or return "leaf" if there is no such descendant.
715 __isl_give isl_schedule_tree *isl_schedule_tree_first_schedule_descendant(
716 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_tree *leaf)
718 while (isl_schedule_tree_get_type(tree) == isl_schedule_node_band &&
719 isl_schedule_tree_band_n_member(tree) == 0) {
720 if (!isl_schedule_tree_has_children(tree)) {
721 isl_schedule_tree_free(tree);
722 return isl_schedule_tree_copy(leaf);
724 tree = isl_schedule_tree_child(tree, 0);
727 return tree;
730 static __isl_give isl_union_map *subtree_schedule_extend(
731 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer);
733 /* Extend the schedule map "outer" with the subtree schedule
734 * of the (single) child of "tree", if any.
736 * If "tree" does not have any descendants (apart from those that
737 * do not carry any schedule information), then we simply return "outer".
738 * Otherwise, we extend the schedule map "outer" with the subtree schedule
739 * of the single child.
741 static __isl_give isl_union_map *subtree_schedule_extend_child(
742 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
744 isl_schedule_tree *child;
745 isl_union_map *res;
747 if (!tree)
748 return isl_union_map_free(outer);
749 if (!isl_schedule_tree_has_children(tree))
750 return outer;
751 child = isl_schedule_tree_get_child(tree, 0);
752 if (!child)
753 return isl_union_map_free(outer);
754 res = subtree_schedule_extend(child, outer);
755 isl_schedule_tree_free(child);
756 return res;
759 /* Extract the parameter space from one of the children of "tree",
760 * which are assumed to be filters.
762 static __isl_give isl_space *extract_space_from_filter_child(
763 __isl_keep isl_schedule_tree *tree)
765 isl_space *space;
766 isl_union_set *dom;
767 isl_schedule_tree *child;
769 child = isl_schedule_tree_list_get_schedule_tree(tree->children, 0);
770 dom = isl_schedule_tree_filter_get_filter(child);
771 space = isl_union_set_get_space(dom);
772 isl_union_set_free(dom);
773 isl_schedule_tree_free(child);
775 return space;
778 /* Extend the schedule map "outer" with the subtree schedule
779 * of a set or sequence node.
781 * The schedule for the set or sequence node itself is composed of
782 * pieces of the form
784 * filter -> []
786 * or
788 * filter -> [index]
790 * The first form is used if there is only a single child or
791 * if the current node is a set node and the schedule_separate_components
792 * option is not set.
794 * Each of the pieces above is extended with the subtree schedule of
795 * the child of the corresponding filter, if any, padded with zeros
796 * to ensure that all pieces have the same range dimension.
798 static __isl_give isl_union_map *subtree_schedule_extend_from_children(
799 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
801 int i, n;
802 int dim;
803 int separate;
804 isl_ctx *ctx;
805 isl_val *v = NULL;
806 isl_multi_val *mv;
807 isl_space *space;
808 isl_union_map *umap;
810 if (!tree)
811 return NULL;
813 ctx = isl_schedule_tree_get_ctx(tree);
814 if (!tree->children)
815 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
816 "missing children", return NULL);
817 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
818 if (n == 0)
819 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
820 "missing children", return NULL);
822 separate = n > 1 && (tree->type == isl_schedule_node_sequence ||
823 isl_options_get_schedule_separate_components(ctx));
825 space = extract_space_from_filter_child(tree);
827 umap = isl_union_map_empty(isl_space_copy(space));
828 space = isl_space_set_from_params(space);
829 if (separate) {
830 space = isl_space_add_dims(space, isl_dim_set, 1);
831 v = isl_val_zero(ctx);
833 mv = isl_multi_val_zero(space);
835 dim = isl_multi_val_dim(mv, isl_dim_set);
836 for (i = 0; i < n; ++i) {
837 isl_union_pw_multi_aff *upma;
838 isl_union_map *umap_i;
839 isl_union_set *dom;
840 isl_schedule_tree *child;
841 int dim_i;
842 int empty;
844 child = isl_schedule_tree_list_get_schedule_tree(
845 tree->children, i);
846 dom = isl_schedule_tree_filter_get_filter(child);
848 if (separate) {
849 mv = isl_multi_val_set_val(mv, 0, isl_val_copy(v));
850 v = isl_val_add_ui(v, 1);
852 upma = isl_union_pw_multi_aff_multi_val_on_domain(dom,
853 isl_multi_val_copy(mv));
854 umap_i = isl_union_map_from_union_pw_multi_aff(upma);
855 umap_i = isl_union_map_flat_range_product(
856 isl_union_map_copy(outer), umap_i);
857 umap_i = subtree_schedule_extend_child(child, umap_i);
858 isl_schedule_tree_free(child);
860 empty = isl_union_map_is_empty(umap_i);
861 if (empty < 0)
862 umap_i = isl_union_map_free(umap_i);
863 else if (empty) {
864 isl_union_map_free(umap_i);
865 continue;
868 dim_i = range_dim(umap_i);
869 if (dim_i < 0) {
870 umap = isl_union_map_free(umap);
871 } else if (dim < dim_i) {
872 umap = append_range(umap, dim_i - dim);
873 dim = dim_i;
874 } else if (dim_i < dim) {
875 umap_i = append_range(umap_i, dim - dim_i);
877 umap = isl_union_map_union(umap, umap_i);
880 isl_val_free(v);
881 isl_multi_val_free(mv);
882 isl_union_map_free(outer);
884 return umap;
887 /* Extend the schedule map "outer" with the subtree schedule of "tree".
889 * If the root of the tree is a set or a sequence, then we extend
890 * the schedule map in subtree_schedule_extend_from_children.
891 * Otherwise, we extend the schedule map with the partial schedule
892 * corresponding to the root of the tree and then continue with
893 * the single child of this root.
895 static __isl_give isl_union_map *subtree_schedule_extend(
896 __isl_keep isl_schedule_tree *tree, __isl_take isl_union_map *outer)
898 isl_multi_union_pw_aff *mupa;
899 isl_union_map *umap;
900 isl_union_set *domain;
902 if (!tree)
903 return NULL;
905 switch (tree->type) {
906 case isl_schedule_node_error:
907 return isl_union_map_free(outer);
908 case isl_schedule_node_band:
909 if (isl_schedule_tree_band_n_member(tree) == 0)
910 return subtree_schedule_extend_child(tree, outer);
911 mupa = isl_schedule_band_get_partial_schedule(tree->band);
912 umap = isl_union_map_from_multi_union_pw_aff(mupa);
913 outer = isl_union_map_flat_range_product(outer, umap);
914 umap = subtree_schedule_extend_child(tree, outer);
915 break;
916 case isl_schedule_node_domain:
917 domain = isl_schedule_tree_domain_get_domain(tree);
918 umap = isl_union_map_from_domain(domain);
919 outer = isl_union_map_flat_range_product(outer, umap);
920 umap = subtree_schedule_extend_child(tree, outer);
921 break;
922 case isl_schedule_node_filter:
923 domain = isl_schedule_tree_filter_get_filter(tree);
924 umap = isl_union_map_from_domain(domain);
925 outer = isl_union_map_flat_range_product(outer, umap);
926 umap = subtree_schedule_extend_child(tree, outer);
927 break;
928 case isl_schedule_node_leaf:
929 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
930 "leaf node should be handled by caller", return NULL);
931 case isl_schedule_node_set:
932 case isl_schedule_node_sequence:
933 umap = subtree_schedule_extend_from_children(tree, outer);
934 break;
937 return umap;
940 static __isl_give isl_union_set *initial_domain(
941 __isl_keep isl_schedule_tree *tree);
943 /* Extract a universe domain from the children of the tree root "tree",
944 * which is a set or sequence, meaning that its children are filters.
945 * In particular, return the union of the universes of the filters.
947 static __isl_give isl_union_set *initial_domain_from_children(
948 __isl_keep isl_schedule_tree *tree)
950 int i, n;
951 isl_space *space;
952 isl_union_set *domain;
954 if (!tree->children)
955 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
956 "missing children", return NULL);
957 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
958 if (n == 0)
959 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
960 "missing children", return NULL);
962 space = extract_space_from_filter_child(tree);
963 domain = isl_union_set_empty(space);
965 for (i = 0; i < n; ++i) {
966 isl_schedule_tree *child;
967 isl_union_set *domain_i;
969 child = isl_schedule_tree_get_child(tree, i);
970 domain_i = initial_domain(child);
971 domain = isl_union_set_union(domain, domain_i);
972 isl_schedule_tree_free(child);
975 return domain;
978 /* Extract a universe domain from the tree root "tree".
979 * The caller is responsible for making sure that this node
980 * would not be skipped by isl_schedule_tree_first_schedule_descendant
981 * and that it is not a leaf node.
983 static __isl_give isl_union_set *initial_domain(
984 __isl_keep isl_schedule_tree *tree)
986 isl_multi_union_pw_aff *mupa;
987 isl_union_set *domain;
989 if (!tree)
990 return NULL;
992 switch (tree->type) {
993 case isl_schedule_node_error:
994 return NULL;
995 case isl_schedule_node_band:
996 if (isl_schedule_tree_band_n_member(tree) == 0)
997 isl_die(isl_schedule_tree_get_ctx(tree),
998 isl_error_internal,
999 "0D band should be handled by caller",
1000 return NULL);
1001 mupa = isl_schedule_band_get_partial_schedule(tree->band);
1002 domain = isl_multi_union_pw_aff_domain(mupa);
1003 domain = isl_union_set_universe(domain);
1004 break;
1005 case isl_schedule_node_domain:
1006 domain = isl_schedule_tree_domain_get_domain(tree);
1007 domain = isl_union_set_universe(domain);
1008 break;
1009 case isl_schedule_node_filter:
1010 domain = isl_schedule_tree_filter_get_filter(tree);
1011 domain = isl_union_set_universe(domain);
1012 break;
1013 case isl_schedule_node_leaf:
1014 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
1015 "leaf node should be handled by caller", return NULL);
1016 case isl_schedule_node_set:
1017 case isl_schedule_node_sequence:
1018 domain = initial_domain_from_children(tree);
1019 break;
1022 return domain;
1025 /* Return the subtree schedule of a node that contains some schedule
1026 * information, i.e., a node that would not be skipped by
1027 * isl_schedule_tree_first_schedule_descendant and that is not a leaf.
1029 * We start with an initial zero-dimensional subtree schedule based
1030 * on the domain information in the root node and then extend it
1031 * based on the schedule information in the root node and its descendants.
1033 __isl_give isl_union_map *isl_schedule_tree_get_subtree_schedule_union_map(
1034 __isl_keep isl_schedule_tree *tree)
1036 isl_union_set *domain;
1037 isl_union_map *umap;
1039 domain = initial_domain(tree);
1040 umap = isl_union_map_from_domain(domain);
1041 return subtree_schedule_extend(tree, umap);
1044 /* Multiply the partial schedule of the band root node of "tree"
1045 * with the factors in "mv".
1047 __isl_give isl_schedule_tree *isl_schedule_tree_band_scale(
1048 __isl_take isl_schedule_tree *tree, __isl_take isl_multi_val *mv)
1050 if (!tree || !mv)
1051 goto error;
1052 if (tree->type != isl_schedule_node_band)
1053 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1054 "not a band node", goto error);
1056 tree = isl_schedule_tree_cow(tree);
1057 if (!tree)
1058 goto error;
1060 tree->band = isl_schedule_band_scale(tree->band, mv);
1061 if (!tree->band)
1062 return isl_schedule_tree_free(tree);
1064 return tree;
1065 error:
1066 isl_schedule_tree_free(tree);
1067 isl_multi_val_free(mv);
1068 return NULL;
1071 /* Divide the partial schedule of the band root node of "tree"
1072 * by the factors in "mv".
1074 __isl_give isl_schedule_tree *isl_schedule_tree_band_scale_down(
1075 __isl_take isl_schedule_tree *tree, __isl_take isl_multi_val *mv)
1077 if (!tree || !mv)
1078 goto error;
1079 if (tree->type != isl_schedule_node_band)
1080 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1081 "not a band node", goto error);
1083 tree = isl_schedule_tree_cow(tree);
1084 if (!tree)
1085 goto error;
1087 tree->band = isl_schedule_band_scale_down(tree->band, mv);
1088 if (!tree->band)
1089 return isl_schedule_tree_free(tree);
1091 return tree;
1092 error:
1093 isl_schedule_tree_free(tree);
1094 isl_multi_val_free(mv);
1095 return NULL;
1098 /* Tile the band root node of "tree" with tile sizes "sizes".
1100 * We duplicate the band node, change the schedule of one of them
1101 * to the tile schedule and the other to the point schedule and then
1102 * attach the point band as a child to the tile band.
1104 __isl_give isl_schedule_tree *isl_schedule_tree_band_tile(
1105 __isl_take isl_schedule_tree *tree, __isl_take isl_multi_val *sizes)
1107 isl_schedule_tree *child = NULL;
1109 if (!tree || !sizes)
1110 goto error;
1111 if (tree->type != isl_schedule_node_band)
1112 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1113 "not a band node", goto error);
1115 child = isl_schedule_tree_copy(tree);
1116 tree = isl_schedule_tree_cow(tree);
1117 child = isl_schedule_tree_cow(child);
1118 if (!tree || !child)
1119 goto error;
1121 tree->band = isl_schedule_band_tile(tree->band,
1122 isl_multi_val_copy(sizes));
1123 if (!tree->band)
1124 goto error;
1125 child->band = isl_schedule_band_point(child->band, tree->band, sizes);
1126 if (!child->band)
1127 child = isl_schedule_tree_free(child);
1129 tree = isl_schedule_tree_replace_child(tree, 0, child);
1131 return tree;
1132 error:
1133 isl_schedule_tree_free(child);
1134 isl_schedule_tree_free(tree);
1135 isl_multi_val_free(sizes);
1136 return NULL;
1139 /* Split the band root node of "tree" into two nested band nodes,
1140 * one with the first "pos" dimensions and
1141 * one with the remaining dimensions.
1143 __isl_give isl_schedule_tree *isl_schedule_tree_band_split(
1144 __isl_take isl_schedule_tree *tree, int pos)
1146 int n;
1147 isl_schedule_tree *child;
1149 if (!tree)
1150 return NULL;
1151 if (tree->type != isl_schedule_node_band)
1152 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1153 "not a band node", return isl_schedule_tree_free(tree));
1155 n = isl_schedule_tree_band_n_member(tree);
1156 if (pos < 0 || pos > n)
1157 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
1158 "position out of bounds",
1159 return isl_schedule_tree_free(tree));
1161 child = isl_schedule_tree_copy(tree);
1162 tree = isl_schedule_tree_cow(tree);
1163 child = isl_schedule_tree_cow(child);
1164 if (!tree || !child)
1165 goto error;
1167 child->band = isl_schedule_band_drop(child->band, 0, pos);
1168 tree->band = isl_schedule_band_drop(tree->band, pos, n - pos);
1169 if (!child->band || !tree->band)
1170 goto error;
1172 tree = isl_schedule_tree_replace_child(tree, 0, child);
1174 return tree;
1175 error:
1176 isl_schedule_tree_free(child);
1177 isl_schedule_tree_free(tree);
1178 return NULL;
1181 /* Are any members in "band" marked coincident?
1183 static int any_coincident(__isl_keep isl_schedule_band *band)
1185 int i, n;
1187 n = isl_schedule_band_n_member(band);
1188 for (i = 0; i < n; ++i)
1189 if (isl_schedule_band_member_get_coincident(band, i))
1190 return 1;
1192 return 0;
1195 /* Print the band node "band" to "p".
1197 * The permutable and coincident properties are only printed if they
1198 * are different from the defaults.
1199 * The coincident property is always printed in YAML flow style.
1201 static __isl_give isl_printer *print_tree_band(__isl_take isl_printer *p,
1202 __isl_keep isl_schedule_band *band)
1204 p = isl_printer_print_str(p, "schedule");
1205 p = isl_printer_yaml_next(p);
1206 p = isl_printer_print_str(p, "\"");
1207 p = isl_printer_print_multi_union_pw_aff(p, band->mupa);
1208 p = isl_printer_print_str(p, "\"");
1209 if (isl_schedule_band_get_permutable(band)) {
1210 p = isl_printer_yaml_next(p);
1211 p = isl_printer_print_str(p, "permutable");
1212 p = isl_printer_yaml_next(p);
1213 p = isl_printer_print_int(p, 1);
1215 if (any_coincident(band)) {
1216 int i, n;
1217 int style;
1219 p = isl_printer_yaml_next(p);
1220 p = isl_printer_print_str(p, "coincident");
1221 p = isl_printer_yaml_next(p);
1222 style = isl_printer_get_yaml_style(p);
1223 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1224 p = isl_printer_yaml_start_sequence(p);
1225 n = isl_schedule_band_n_member(band);
1226 for (i = 0; i < n; ++i) {
1227 p = isl_printer_print_int(p,
1228 isl_schedule_band_member_get_coincident(band, i));
1229 p = isl_printer_yaml_next(p);
1231 p = isl_printer_yaml_end_sequence(p);
1232 p = isl_printer_set_yaml_style(p, style);
1235 return p;
1238 /* Print "tree" to "p".
1240 * If "n_ancestor" is non-negative, then "child_pos" contains the child
1241 * positions of a descendant of the current node that should be marked
1242 * (by the comment "YOU ARE HERE"). In particular, if "n_ancestor"
1243 * is zero, then the current node should be marked.
1244 * The marking is only printed in YAML block format.
1246 * Implicit leaf nodes are not printed, except if they correspond
1247 * to the node that should be marked.
1249 __isl_give isl_printer *isl_printer_print_schedule_tree_mark(
1250 __isl_take isl_printer *p, __isl_keep isl_schedule_tree *tree,
1251 int n_ancestor, int *child_pos)
1253 int i, n;
1254 int sequence = 0;
1255 int block;
1257 block = isl_printer_get_yaml_style(p) == ISL_YAML_STYLE_BLOCK;
1259 p = isl_printer_yaml_start_mapping(p);
1260 if (n_ancestor == 0 && block) {
1261 p = isl_printer_print_str(p, "# YOU ARE HERE");
1262 p = isl_printer_end_line(p);
1263 p = isl_printer_start_line(p);
1265 switch (tree->type) {
1266 case isl_schedule_node_error:
1267 p = isl_printer_print_str(p, "ERROR");
1268 break;
1269 case isl_schedule_node_leaf:
1270 p = isl_printer_print_str(p, "leaf");
1271 break;
1272 case isl_schedule_node_sequence:
1273 p = isl_printer_print_str(p, "sequence");
1274 sequence = 1;
1275 break;
1276 case isl_schedule_node_set:
1277 p = isl_printer_print_str(p, "set");
1278 sequence = 1;
1279 break;
1280 case isl_schedule_node_domain:
1281 p = isl_printer_print_str(p, "domain");
1282 p = isl_printer_yaml_next(p);
1283 p = isl_printer_print_str(p, "\"");
1284 p = isl_printer_print_union_set(p, tree->domain);
1285 p = isl_printer_print_str(p, "\"");
1286 break;
1287 case isl_schedule_node_filter:
1288 p = isl_printer_print_str(p, "filter");
1289 p = isl_printer_yaml_next(p);
1290 p = isl_printer_print_str(p, "\"");
1291 p = isl_printer_print_union_set(p, tree->filter);
1292 p = isl_printer_print_str(p, "\"");
1293 break;
1294 case isl_schedule_node_band:
1295 p = print_tree_band(p, tree->band);
1296 break;
1298 p = isl_printer_yaml_next(p);
1300 if (!tree->children) {
1301 if (n_ancestor > 0 && block) {
1302 isl_schedule_tree *leaf;
1304 p = isl_printer_print_str(p, "child");
1305 p = isl_printer_yaml_next(p);
1306 leaf = isl_schedule_tree_leaf(isl_printer_get_ctx(p));
1307 p = isl_printer_print_schedule_tree_mark(p,
1308 leaf, 0, NULL);
1309 isl_schedule_tree_free(leaf);
1310 p = isl_printer_yaml_next(p);
1312 return isl_printer_yaml_end_mapping(p);
1315 if (sequence) {
1316 p = isl_printer_yaml_start_sequence(p);
1317 } else {
1318 p = isl_printer_print_str(p, "child");
1319 p = isl_printer_yaml_next(p);
1322 n = isl_schedule_tree_list_n_schedule_tree(tree->children);
1323 for (i = 0; i < n; ++i) {
1324 isl_schedule_tree *t;
1326 t = isl_schedule_tree_get_child(tree, i);
1327 if (n_ancestor > 0 && child_pos[0] == i)
1328 p = isl_printer_print_schedule_tree_mark(p, t,
1329 n_ancestor - 1, child_pos + 1);
1330 else
1331 p = isl_printer_print_schedule_tree_mark(p, t,
1332 -1, NULL);
1333 isl_schedule_tree_free(t);
1335 p = isl_printer_yaml_next(p);
1338 if (sequence)
1339 p = isl_printer_yaml_end_sequence(p);
1340 p = isl_printer_yaml_end_mapping(p);
1342 return p;
1345 /* Print "tree" to "p".
1347 __isl_give isl_printer *isl_printer_print_schedule_tree(
1348 __isl_take isl_printer *p, __isl_keep isl_schedule_tree *tree)
1350 return isl_printer_print_schedule_tree_mark(p, tree, -1, NULL);
1353 void isl_schedule_tree_dump(__isl_keep isl_schedule_tree *tree)
1355 isl_ctx *ctx;
1356 isl_printer *printer;
1358 if (!tree)
1359 return;
1361 ctx = isl_schedule_tree_get_ctx(tree);
1362 printer = isl_printer_to_file(ctx, stderr);
1363 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
1364 printer = isl_printer_print_schedule_tree(printer, tree);
1366 isl_printer_free(printer);