add isl_schedule_insert_context
[isl.git] / isl_schedule_node.c
blobae2ceca4009758adf7f2647e3c26ae0d497a3a29
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/set.h>
14 #include <isl_schedule_band.h>
15 #include <isl_schedule_private.h>
16 #include <isl_schedule_node_private.h>
18 /* Create a new schedule node in the given schedule, point at the given
19 * tree with given ancestors and child positions.
20 * "child_pos" may be NULL if there are no ancestors.
22 __isl_give isl_schedule_node *isl_schedule_node_alloc(
23 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree,
24 __isl_take isl_schedule_tree_list *ancestors, int *child_pos)
26 isl_ctx *ctx;
27 isl_schedule_node *node;
28 int i, n;
30 if (!schedule || !tree || !ancestors)
31 goto error;
32 n = isl_schedule_tree_list_n_schedule_tree(ancestors);
33 if (n > 0 && !child_pos)
34 goto error;
35 ctx = isl_schedule_get_ctx(schedule);
36 node = isl_calloc_type(ctx, isl_schedule_node);
37 if (!node)
38 goto error;
39 node->ref = 1;
40 node->schedule = schedule;
41 node->tree = tree;
42 node->ancestors = ancestors;
43 node->child_pos = isl_alloc_array(ctx, int, n);
44 if (n && !node->child_pos)
45 return isl_schedule_node_free(node);
46 for (i = 0; i < n; ++i)
47 node->child_pos[i] = child_pos[i];
49 return node;
50 error:
51 isl_schedule_free(schedule);
52 isl_schedule_tree_free(tree);
53 isl_schedule_tree_list_free(ancestors);
54 return NULL;
57 /* Return a pointer to the root of a schedule tree with as single
58 * node a domain node with the given domain.
60 __isl_give isl_schedule_node *isl_schedule_node_from_domain(
61 __isl_take isl_union_set *domain)
63 isl_schedule *schedule;
64 isl_schedule_node *node;
66 schedule = isl_schedule_from_domain(domain);
67 node = isl_schedule_get_root(schedule);
68 isl_schedule_free(schedule);
70 return node;
73 /* Return the isl_ctx to which "node" belongs.
75 isl_ctx *isl_schedule_node_get_ctx(__isl_keep isl_schedule_node *node)
77 return node ? isl_schedule_get_ctx(node->schedule) : NULL;
80 /* Return a pointer to the leaf of the schedule into which "node" points.
82 * Even though these leaves are not reference counted, we still
83 * indicate that this function does not return a copy.
85 __isl_keep isl_schedule_tree *isl_schedule_node_peek_leaf(
86 __isl_keep isl_schedule_node *node)
88 return node ? isl_schedule_peek_leaf(node->schedule) : NULL;
91 /* Return a pointer to the leaf of the schedule into which "node" points.
93 * Even though these leaves are not reference counted, we still
94 * return a "copy" of the leaf here such that it can still be "freed"
95 * by the user.
97 __isl_give isl_schedule_tree *isl_schedule_node_get_leaf(
98 __isl_keep isl_schedule_node *node)
100 return isl_schedule_tree_copy(isl_schedule_node_peek_leaf(node));
103 /* Return the type of the node or isl_schedule_node_error on error.
105 enum isl_schedule_node_type isl_schedule_node_get_type(
106 __isl_keep isl_schedule_node *node)
108 return node ? isl_schedule_tree_get_type(node->tree)
109 : isl_schedule_node_error;
112 /* Return the type of the parent of "node" or isl_schedule_node_error on error.
114 enum isl_schedule_node_type isl_schedule_node_get_parent_type(
115 __isl_keep isl_schedule_node *node)
117 int pos;
118 int has_parent;
119 isl_schedule_tree *parent;
120 enum isl_schedule_node_type type;
122 if (!node)
123 return isl_schedule_node_error;
124 has_parent = isl_schedule_node_has_parent(node);
125 if (has_parent < 0)
126 return isl_schedule_node_error;
127 if (!has_parent)
128 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
129 "node has no parent", return isl_schedule_node_error);
131 pos = isl_schedule_tree_list_n_schedule_tree(node->ancestors) - 1;
132 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors, pos);
133 type = isl_schedule_tree_get_type(parent);
134 isl_schedule_tree_free(parent);
136 return type;
139 /* Return a copy of the subtree that this node points to.
141 __isl_give isl_schedule_tree *isl_schedule_node_get_tree(
142 __isl_keep isl_schedule_node *node)
144 if (!node)
145 return NULL;
147 return isl_schedule_tree_copy(node->tree);
150 /* Return a copy of the schedule into which "node" points.
152 __isl_give isl_schedule *isl_schedule_node_get_schedule(
153 __isl_keep isl_schedule_node *node)
155 if (!node)
156 return NULL;
157 return isl_schedule_copy(node->schedule);
160 /* Return a fresh copy of "node".
162 __isl_take isl_schedule_node *isl_schedule_node_dup(
163 __isl_keep isl_schedule_node *node)
165 if (!node)
166 return NULL;
168 return isl_schedule_node_alloc(isl_schedule_copy(node->schedule),
169 isl_schedule_tree_copy(node->tree),
170 isl_schedule_tree_list_copy(node->ancestors),
171 node->child_pos);
174 /* Return an isl_schedule_node that is equal to "node" and that has only
175 * a single reference.
177 __isl_give isl_schedule_node *isl_schedule_node_cow(
178 __isl_take isl_schedule_node *node)
180 if (!node)
181 return NULL;
183 if (node->ref == 1)
184 return node;
185 node->ref--;
186 return isl_schedule_node_dup(node);
189 /* Return a new reference to "node".
191 __isl_give isl_schedule_node *isl_schedule_node_copy(
192 __isl_keep isl_schedule_node *node)
194 if (!node)
195 return NULL;
197 node->ref++;
198 return node;
201 /* Free "node" and return NULL.
203 * Since the node may point to a leaf of its schedule, which
204 * point to a field inside the schedule, we need to make sure
205 * we free the tree before freeing the schedule.
207 __isl_null isl_schedule_node *isl_schedule_node_free(
208 __isl_take isl_schedule_node *node)
210 if (!node)
211 return NULL;
212 if (--node->ref > 0)
213 return NULL;
215 isl_schedule_tree_list_free(node->ancestors);
216 free(node->child_pos);
217 isl_schedule_tree_free(node->tree);
218 isl_schedule_free(node->schedule);
219 free(node);
221 return NULL;
224 /* Do "node1" and "node2" point to the same position in the same
225 * schedule?
227 int isl_schedule_node_is_equal(__isl_keep isl_schedule_node *node1,
228 __isl_keep isl_schedule_node *node2)
230 int i, n1, n2;
232 if (!node1 || !node2)
233 return -1;
234 if (node1 == node2)
235 return 1;
236 if (node1->schedule != node2->schedule)
237 return 0;
239 n1 = isl_schedule_node_get_tree_depth(node1);
240 n2 = isl_schedule_node_get_tree_depth(node2);
241 if (n1 != n2)
242 return 0;
243 for (i = 0; i < n1; ++i)
244 if (node1->child_pos[i] != node2->child_pos[i])
245 return 0;
247 return 1;
250 /* Return the number of outer schedule dimensions of "node"
251 * in its schedule tree.
253 * Return -1 on error.
255 int isl_schedule_node_get_schedule_depth(__isl_keep isl_schedule_node *node)
257 int i, n;
258 int depth = 0;
260 if (!node)
261 return -1;
263 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
264 for (i = n - 1; i >= 0; --i) {
265 isl_schedule_tree *tree;
267 tree = isl_schedule_tree_list_get_schedule_tree(
268 node->ancestors, i);
269 if (!tree)
270 return -1;
271 if (tree->type == isl_schedule_node_band)
272 depth += isl_schedule_tree_band_n_member(tree);
273 isl_schedule_tree_free(tree);
276 return depth;
279 /* Internal data structure for
280 * isl_schedule_node_get_prefix_schedule_union_pw_multi_aff
282 * "initialized" is set if the filter field has been initialized.
283 * "universe_filter" is set if we are only collecting the universes of filters
284 * "collect_prefix" is set if we are collecting prefixes.
285 * "filter" collects all outer filters and is NULL until "initialized" is set.
286 * "prefix" collects all outer band partial schedules (if "collect_prefix"
287 * is set). If it is used, then it is initialized by the caller
288 * of collect_filter_prefix to a zero-dimensional function.
290 struct isl_schedule_node_get_filter_prefix_data {
291 int initialized;
292 int universe_filter;
293 int collect_prefix;
294 isl_union_set *filter;
295 isl_multi_union_pw_aff *prefix;
298 /* Update "data" based on the tree node "tree" in case "data" has
299 * not been initialized yet.
301 * Return 0 on success and -1 on error.
303 * If "tree" is a filter, then we set data->filter to this filter
304 * (or its universe).
305 * If "tree" is a domain, then this means we have reached the root
306 * of the schedule tree without being able to extract any information.
307 * We therefore initialize data->filter to the universe of the domain.
308 * If "tree" is a band with at least one member, then we set data->filter
309 * to the universe of the schedule domain and replace the zero-dimensional
310 * data->prefix by the band schedule (if data->collect_prefix is set).
312 static int collect_filter_prefix_init(__isl_keep isl_schedule_tree *tree,
313 struct isl_schedule_node_get_filter_prefix_data *data)
315 enum isl_schedule_node_type type;
316 isl_multi_union_pw_aff *mupa;
317 isl_union_set *filter;
319 type = isl_schedule_tree_get_type(tree);
320 switch (type) {
321 case isl_schedule_node_error:
322 return -1;
323 case isl_schedule_node_context:
324 case isl_schedule_node_leaf:
325 case isl_schedule_node_sequence:
326 case isl_schedule_node_set:
327 return 0;
328 case isl_schedule_node_domain:
329 filter = isl_schedule_tree_domain_get_domain(tree);
330 filter = isl_union_set_universe(filter);
331 data->filter = filter;
332 break;
333 case isl_schedule_node_band:
334 if (isl_schedule_tree_band_n_member(tree) == 0)
335 return 0;
336 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
337 if (data->collect_prefix) {
338 isl_multi_union_pw_aff_free(data->prefix);
339 mupa = isl_multi_union_pw_aff_reset_tuple_id(mupa,
340 isl_dim_set);
341 data->prefix = isl_multi_union_pw_aff_copy(mupa);
343 filter = isl_multi_union_pw_aff_domain(mupa);
344 filter = isl_union_set_universe(filter);
345 data->filter = filter;
346 break;
347 case isl_schedule_node_filter:
348 filter = isl_schedule_tree_filter_get_filter(tree);
349 if (data->universe_filter)
350 filter = isl_union_set_universe(filter);
351 data->filter = filter;
352 break;
355 if ((data->collect_prefix && !data->prefix) || !data->filter)
356 return -1;
358 data->initialized = 1;
360 return 0;
363 /* Update "data" based on the tree node "tree" in case "data" has
364 * already been initialized.
366 * Return 0 on success and -1 on error.
368 * If "tree" is a filter, then we intersect data->filter with this filter
369 * (or its universe).
370 * If "tree" is a band with at least one member and data->collect_prefix
371 * is set, then we extend data->prefix with the band schedule.
373 static int collect_filter_prefix_update(__isl_keep isl_schedule_tree *tree,
374 struct isl_schedule_node_get_filter_prefix_data *data)
376 enum isl_schedule_node_type type;
377 isl_multi_union_pw_aff *mupa;
378 isl_union_set *filter;
380 type = isl_schedule_tree_get_type(tree);
381 switch (type) {
382 case isl_schedule_node_error:
383 return -1;
384 case isl_schedule_node_context:
385 case isl_schedule_node_domain:
386 case isl_schedule_node_leaf:
387 case isl_schedule_node_sequence:
388 case isl_schedule_node_set:
389 break;
390 case isl_schedule_node_band:
391 if (isl_schedule_tree_band_n_member(tree) == 0)
392 break;
393 if (!data->collect_prefix)
394 break;
395 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
396 data->prefix = isl_multi_union_pw_aff_flat_range_product(mupa,
397 data->prefix);
398 if (!data->prefix)
399 return -1;
400 break;
401 case isl_schedule_node_filter:
402 filter = isl_schedule_tree_filter_get_filter(tree);
403 if (data->universe_filter)
404 filter = isl_union_set_universe(filter);
405 data->filter = isl_union_set_intersect(data->filter, filter);
406 if (!data->filter)
407 return -1;
408 break;
411 return 0;
414 /* Collect filter and/or prefix information from the elements
415 * in "list" (which represent the ancestors of a node).
416 * Store the results in "data".
418 * Return 0 on success and -1 on error.
420 * We traverse the list from innermost ancestor (last element)
421 * to outermost ancestor (first element), calling collect_filter_prefix_init
422 * on each node as long as we have not been able to extract any information
423 * yet and collect_filter_prefix_update afterwards.
424 * On successful return, data->initialized will be set since the outermost
425 * ancestor is a domain node, which always results in an initialization.
427 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
428 struct isl_schedule_node_get_filter_prefix_data *data)
430 int i, n;
432 data->initialized = 0;
433 data->filter = NULL;
435 if (!list)
436 return -1;
438 n = isl_schedule_tree_list_n_schedule_tree(list);
439 for (i = n - 1; i >= 0; --i) {
440 isl_schedule_tree *tree;
441 int r;
443 tree = isl_schedule_tree_list_get_schedule_tree(list, i);
444 if (!tree)
445 return -1;
446 if (!data->initialized)
447 r = collect_filter_prefix_init(tree, data);
448 else
449 r = collect_filter_prefix_update(tree, data);
450 isl_schedule_tree_free(tree);
451 if (r < 0)
452 return -1;
455 return 0;
458 /* Return the concatenation of the partial schedules of all outer band
459 * nodes of "node" interesected with all outer filters
460 * as an isl_union_pw_multi_aff.
462 * If "node" is pointing at the root of the schedule tree, then
463 * there are no domain elements reaching the current node, so
464 * we return an empty result.
466 * We collect all the filters and partial schedules in collect_filter_prefix.
467 * The partial schedules are collected as an isl_multi_union_pw_aff.
468 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
469 * contain any domain information, so we construct the isl_union_pw_multi_aff
470 * result as a zero-dimensional function on the collected filter.
471 * Otherwise, we convert the isl_multi_union_pw_aff to
472 * an isl_multi_union_pw_aff and intersect the domain with the filter.
474 __isl_give isl_union_pw_multi_aff *
475 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
476 __isl_keep isl_schedule_node *node)
478 isl_space *space;
479 isl_union_pw_multi_aff *prefix;
480 struct isl_schedule_node_get_filter_prefix_data data;
482 if (!node)
483 return NULL;
485 space = isl_schedule_get_space(node->schedule);
486 if (node->tree == node->schedule->root)
487 return isl_union_pw_multi_aff_empty(space);
489 space = isl_space_set_from_params(space);
490 data.universe_filter = 0;
491 data.collect_prefix = 1;
492 data.prefix = isl_multi_union_pw_aff_zero(space);
494 if (collect_filter_prefix(node->ancestors, &data) < 0)
495 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
497 if (data.prefix &&
498 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
499 isl_multi_union_pw_aff_free(data.prefix);
500 prefix = isl_union_pw_multi_aff_from_domain(data.filter);
501 } else {
502 prefix =
503 isl_union_pw_multi_aff_from_multi_union_pw_aff(data.prefix);
504 prefix = isl_union_pw_multi_aff_intersect_domain(prefix,
505 data.filter);
508 return prefix;
511 /* Return the concatenation of the partial schedules of all outer band
512 * nodes of "node" interesected with all outer filters
513 * as an isl_union_map.
515 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_union_map(
516 __isl_keep isl_schedule_node *node)
518 isl_union_pw_multi_aff *upma;
520 upma = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
521 return isl_union_map_from_union_pw_multi_aff(upma);
524 /* Return the union of universe sets of the domain elements that reach "node".
526 * If "node" is pointing at the root of the schedule tree, then
527 * there are no domain elements reaching the current node, so
528 * we return an empty result.
530 * Otherwise, we collect the universes of all filters reaching the node
531 * in collect_filter_prefix.
533 __isl_give isl_union_set *isl_schedule_node_get_universe_domain(
534 __isl_keep isl_schedule_node *node)
536 struct isl_schedule_node_get_filter_prefix_data data;
538 if (!node)
539 return NULL;
541 if (node->tree == node->schedule->root) {
542 isl_space *space;
544 space = isl_schedule_get_space(node->schedule);
545 return isl_union_set_empty(space);
548 data.universe_filter = 1;
549 data.collect_prefix = 0;
550 data.prefix = NULL;
552 if (collect_filter_prefix(node->ancestors, &data) < 0)
553 data.filter = isl_union_set_free(data.filter);
555 return data.filter;
558 /* Return the subtree schedule of "node".
560 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
561 * trees that do not contain any schedule information, we first
562 * move down to the first relevant descendant and handle leaves ourselves.
564 __isl_give isl_union_map *isl_schedule_node_get_subtree_schedule_union_map(
565 __isl_keep isl_schedule_node *node)
567 isl_schedule_tree *tree, *leaf;
568 isl_union_map *umap;
570 tree = isl_schedule_node_get_tree(node);
571 leaf = isl_schedule_node_peek_leaf(node);
572 tree = isl_schedule_tree_first_schedule_descendant(tree, leaf);
573 if (!tree)
574 return NULL;
575 if (tree == leaf) {
576 isl_union_set *domain;
577 domain = isl_schedule_node_get_universe_domain(node);
578 isl_schedule_tree_free(tree);
579 return isl_union_map_from_domain(domain);
582 umap = isl_schedule_tree_get_subtree_schedule_union_map(tree);
583 isl_schedule_tree_free(tree);
584 return umap;
587 /* Return the number of ancestors of "node" in its schedule tree.
589 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node *node)
591 if (!node)
592 return -1;
593 return isl_schedule_tree_list_n_schedule_tree(node->ancestors);
596 /* Does "node" have a parent?
598 * That is, does it point to any node of the schedule other than the root?
600 int isl_schedule_node_has_parent(__isl_keep isl_schedule_node *node)
602 if (!node)
603 return -1;
604 if (!node->ancestors)
605 return -1;
607 return isl_schedule_tree_list_n_schedule_tree(node->ancestors) != 0;
610 /* Return the position of "node" among the children of its parent.
612 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node *node)
614 int n;
615 int has_parent;
617 if (!node)
618 return -1;
619 has_parent = isl_schedule_node_has_parent(node);
620 if (has_parent < 0)
621 return -1;
622 if (!has_parent)
623 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
624 "node has no parent", return -1);
626 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
627 return node->child_pos[n - 1];
630 /* Does the parent (if any) of "node" have any children with a smaller child
631 * position than this one?
633 int isl_schedule_node_has_previous_sibling(__isl_keep isl_schedule_node *node)
635 int n;
636 int has_parent;
638 if (!node)
639 return -1;
640 has_parent = isl_schedule_node_has_parent(node);
641 if (has_parent < 0 || !has_parent)
642 return has_parent;
644 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
646 return node->child_pos[n - 1] > 0;
649 /* Does the parent (if any) of "node" have any children with a greater child
650 * position than this one?
652 int isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node *node)
654 int n, n_child;
655 int has_parent;
656 isl_schedule_tree *tree;
658 if (!node)
659 return -1;
660 has_parent = isl_schedule_node_has_parent(node);
661 if (has_parent < 0 || !has_parent)
662 return has_parent;
664 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
665 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n - 1);
666 if (!tree)
667 return -1;
668 n_child = isl_schedule_tree_list_n_schedule_tree(tree->children);
669 isl_schedule_tree_free(tree);
671 return node->child_pos[n - 1] + 1 < n_child;
674 /* Does "node" have any children?
676 * Any node other than the leaf nodes is considered to have at least
677 * one child, even if the corresponding isl_schedule_tree does not
678 * have any children.
680 int isl_schedule_node_has_children(__isl_keep isl_schedule_node *node)
682 if (!node)
683 return -1;
684 return !isl_schedule_tree_is_leaf(node->tree);
687 /* Return the number of children of "node"?
689 * Any node other than the leaf nodes is considered to have at least
690 * one child, even if the corresponding isl_schedule_tree does not
691 * have any children. That is, the number of children of "node" is
692 * only zero if its tree is the explicit empty tree. Otherwise,
693 * if the isl_schedule_tree has any children, then it is equal
694 * to the number of children of "node". If it has zero children,
695 * then "node" still has a leaf node as child.
697 int isl_schedule_node_n_children(__isl_keep isl_schedule_node *node)
699 int n;
701 if (!node)
702 return -1;
704 if (isl_schedule_tree_is_leaf(node->tree))
705 return 0;
707 n = isl_schedule_tree_n_children(node->tree);
708 if (n == 0)
709 return 1;
711 return n;
714 /* Move the "node" pointer to the ancestor of the given generation
715 * of the node it currently points to, where generation 0 is the node
716 * itself and generation 1 is its parent.
718 __isl_give isl_schedule_node *isl_schedule_node_ancestor(
719 __isl_take isl_schedule_node *node, int generation)
721 int n;
722 isl_schedule_tree *tree;
724 if (!node)
725 return NULL;
726 if (generation == 0)
727 return node;
728 n = isl_schedule_node_get_tree_depth(node);
729 if (n < 0)
730 return isl_schedule_node_free(node);
731 if (generation < 0 || generation > n)
732 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
733 "generation out of bounds",
734 return isl_schedule_node_free(node));
735 node = isl_schedule_node_cow(node);
736 if (!node)
737 return NULL;
739 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
740 n - generation);
741 isl_schedule_tree_free(node->tree);
742 node->tree = tree;
743 node->ancestors = isl_schedule_tree_list_drop(node->ancestors,
744 n - generation, generation);
745 if (!node->ancestors || !node->tree)
746 return isl_schedule_node_free(node);
748 return node;
751 /* Move the "node" pointer to the parent of the node it currently points to.
753 __isl_give isl_schedule_node *isl_schedule_node_parent(
754 __isl_take isl_schedule_node *node)
756 if (!node)
757 return NULL;
758 if (!isl_schedule_node_has_parent(node))
759 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
760 "node has no parent",
761 return isl_schedule_node_free(node));
762 return isl_schedule_node_ancestor(node, 1);
765 /* Move the "node" pointer to the root of its schedule tree.
767 __isl_give isl_schedule_node *isl_schedule_node_root(
768 __isl_take isl_schedule_node *node)
770 int n;
772 if (!node)
773 return NULL;
774 n = isl_schedule_node_get_tree_depth(node);
775 if (n < 0)
776 return isl_schedule_node_free(node);
777 return isl_schedule_node_ancestor(node, n);
780 /* Move the "node" pointer to the child at position "pos" of the node
781 * it currently points to.
783 __isl_give isl_schedule_node *isl_schedule_node_child(
784 __isl_take isl_schedule_node *node, int pos)
786 int n;
787 isl_ctx *ctx;
788 isl_schedule_tree *tree;
789 int *child_pos;
791 node = isl_schedule_node_cow(node);
792 if (!node)
793 return NULL;
794 if (!isl_schedule_node_has_children(node))
795 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
796 "node has no children",
797 return isl_schedule_node_free(node));
799 ctx = isl_schedule_node_get_ctx(node);
800 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
801 child_pos = isl_realloc_array(ctx, node->child_pos, int, n + 1);
802 if (!child_pos)
803 return isl_schedule_node_free(node);
804 node->child_pos = child_pos;
805 node->child_pos[n] = pos;
807 node->ancestors = isl_schedule_tree_list_add(node->ancestors,
808 isl_schedule_tree_copy(node->tree));
809 tree = node->tree;
810 if (isl_schedule_tree_has_children(tree))
811 tree = isl_schedule_tree_get_child(tree, pos);
812 else
813 tree = isl_schedule_node_get_leaf(node);
814 isl_schedule_tree_free(node->tree);
815 node->tree = tree;
817 if (!node->tree || !node->ancestors)
818 return isl_schedule_node_free(node);
820 return node;
823 /* Move the "node" pointer to the first child of the node
824 * it currently points to.
826 __isl_give isl_schedule_node *isl_schedule_node_first_child(
827 __isl_take isl_schedule_node *node)
829 return isl_schedule_node_child(node, 0);
832 /* Move the "node" pointer to the child of this node's parent in
833 * the previous child position.
835 __isl_give isl_schedule_node *isl_schedule_node_previous_sibling(
836 __isl_take isl_schedule_node *node)
838 int n;
839 isl_schedule_tree *parent, *tree;
841 node = isl_schedule_node_cow(node);
842 if (!node)
843 return NULL;
844 if (!isl_schedule_node_has_previous_sibling(node))
845 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
846 "node has no previous sibling",
847 return isl_schedule_node_free(node));
849 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
850 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
851 n - 1);
852 if (!parent)
853 return isl_schedule_node_free(node);
854 node->child_pos[n - 1]--;
855 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
856 node->child_pos[n - 1]);
857 isl_schedule_tree_free(parent);
858 if (!tree)
859 return isl_schedule_node_free(node);
860 isl_schedule_tree_free(node->tree);
861 node->tree = tree;
863 return node;
866 /* Move the "node" pointer to the child of this node's parent in
867 * the next child position.
869 __isl_give isl_schedule_node *isl_schedule_node_next_sibling(
870 __isl_take isl_schedule_node *node)
872 int n;
873 isl_schedule_tree *parent, *tree;
875 node = isl_schedule_node_cow(node);
876 if (!node)
877 return NULL;
878 if (!isl_schedule_node_has_next_sibling(node))
879 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
880 "node has no next sibling",
881 return isl_schedule_node_free(node));
883 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
884 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
885 n - 1);
886 if (!parent)
887 return isl_schedule_node_free(node);
888 node->child_pos[n - 1]++;
889 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
890 node->child_pos[n - 1]);
891 isl_schedule_tree_free(parent);
892 if (!tree)
893 return isl_schedule_node_free(node);
894 isl_schedule_tree_free(node->tree);
895 node->tree = tree;
897 return node;
900 /* Return a copy to the child at position "pos" of "node".
902 __isl_give isl_schedule_node *isl_schedule_node_get_child(
903 __isl_keep isl_schedule_node *node, int pos)
905 return isl_schedule_node_child(isl_schedule_node_copy(node), pos);
908 /* Traverse the descendant of "node" in depth-first order, including
909 * "node" itself. Call "enter" whenever a node is entered and "leave"
910 * whenever a node is left. The callback "enter" is responsible
911 * for moving to the deepest initial subtree of its argument that
912 * should be traversed.
914 static __isl_give isl_schedule_node *traverse(
915 __isl_take isl_schedule_node *node,
916 __isl_give isl_schedule_node *(*enter)(
917 __isl_take isl_schedule_node *node, void *user),
918 __isl_give isl_schedule_node *(*leave)(
919 __isl_take isl_schedule_node *node, void *user),
920 void *user)
922 int depth;
924 if (!node)
925 return NULL;
927 depth = isl_schedule_node_get_tree_depth(node);
928 do {
929 node = enter(node, user);
930 node = leave(node, user);
931 while (node && isl_schedule_node_get_tree_depth(node) > depth &&
932 !isl_schedule_node_has_next_sibling(node)) {
933 node = isl_schedule_node_parent(node);
934 node = leave(node, user);
936 if (node && isl_schedule_node_get_tree_depth(node) > depth)
937 node = isl_schedule_node_next_sibling(node);
938 } while (node && isl_schedule_node_get_tree_depth(node) > depth);
940 return node;
943 /* Internal data structure for isl_schedule_node_foreach_descendant.
945 * "fn" is the user-specified callback function.
946 * "user" is the user-specified argument for the callback.
948 struct isl_schedule_node_preorder_data {
949 int (*fn)(__isl_keep isl_schedule_node *node, void *user);
950 void *user;
953 /* Callback for "traverse" to enter a node and to move
954 * to the deepest initial subtree that should be traversed
955 * for use in a preorder visit.
957 * If the user callback returns a negative value, then we abort
958 * the traversal. If this callback returns zero, then we skip
959 * the subtree rooted at the current node. Otherwise, we move
960 * down to the first child and repeat the process until a leaf
961 * is reached.
963 static __isl_give isl_schedule_node *preorder_enter(
964 __isl_take isl_schedule_node *node, void *user)
966 struct isl_schedule_node_preorder_data *data = user;
968 if (!node)
969 return NULL;
971 do {
972 int r;
974 r = data->fn(node, data->user);
975 if (r < 0)
976 return isl_schedule_node_free(node);
977 if (r == 0)
978 return node;
979 } while (isl_schedule_node_has_children(node) &&
980 (node = isl_schedule_node_first_child(node)) != NULL);
982 return node;
985 /* Callback for "traverse" to leave a node
986 * for use in a preorder visit.
987 * Since we already visited the node when we entered it,
988 * we do not need to do anything here.
990 static __isl_give isl_schedule_node *preorder_leave(
991 __isl_take isl_schedule_node *node, void *user)
993 return node;
996 /* Traverse the descendants of "node" (including the node itself)
997 * in depth first preorder.
999 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1000 * If "fn" returns 0 on any of the nodes, then the subtree rooted
1001 * at that node is skipped.
1003 * Return 0 on success and -1 on failure.
1005 int isl_schedule_node_foreach_descendant(__isl_keep isl_schedule_node *node,
1006 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
1008 struct isl_schedule_node_preorder_data data = { fn, user };
1010 node = isl_schedule_node_copy(node);
1011 node = traverse(node, &preorder_enter, &preorder_leave, &data);
1012 isl_schedule_node_free(node);
1014 return node ? 0 : -1;
1017 /* Internal data structure for isl_schedule_node_map_descendant.
1019 * "fn" is the user-specified callback function.
1020 * "user" is the user-specified argument for the callback.
1022 struct isl_schedule_node_postorder_data {
1023 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1024 void *user);
1025 void *user;
1028 /* Callback for "traverse" to enter a node and to move
1029 * to the deepest initial subtree that should be traversed
1030 * for use in a postorder visit.
1032 * Since we are performing a postorder visit, we only need
1033 * to move to the deepest initial leaf here.
1035 static __isl_give isl_schedule_node *postorder_enter(
1036 __isl_take isl_schedule_node *node, void *user)
1038 while (node && isl_schedule_node_has_children(node))
1039 node = isl_schedule_node_first_child(node);
1041 return node;
1044 /* Callback for "traverse" to leave a node
1045 * for use in a postorder visit.
1047 * Since we are performing a postorder visit, we need
1048 * to call the user callback here.
1050 static __isl_give isl_schedule_node *postorder_leave(
1051 __isl_take isl_schedule_node *node, void *user)
1053 struct isl_schedule_node_postorder_data *data = user;
1055 return data->fn(node, data->user);
1058 /* Traverse the descendants of "node" (including the node itself)
1059 * in depth first postorder, allowing the user to modify the visited node.
1060 * The traversal continues from the node returned by the callback function.
1061 * It is the responsibility of the user to ensure that this does not
1062 * lead to an infinite loop. It is safest to always return a pointer
1063 * to the same position (same ancestors and child positions) as the input node.
1065 __isl_give isl_schedule_node *isl_schedule_node_map_descendant(
1066 __isl_take isl_schedule_node *node,
1067 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1068 void *user), void *user)
1070 struct isl_schedule_node_postorder_data data = { fn, user };
1072 return traverse(node, &postorder_enter, &postorder_leave, &data);
1075 /* Traverse the ancestors of "node" from the root down to and including
1076 * the parent of "node", calling "fn" on each of them.
1078 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1080 * Return 0 on success and -1 on failure.
1082 int isl_schedule_node_foreach_ancestor_top_down(
1083 __isl_keep isl_schedule_node *node,
1084 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
1086 int i, n;
1088 if (!node)
1089 return -1;
1091 n = isl_schedule_node_get_tree_depth(node);
1092 for (i = 0; i < n; ++i) {
1093 isl_schedule_node *ancestor;
1094 int r;
1096 ancestor = isl_schedule_node_copy(node);
1097 ancestor = isl_schedule_node_ancestor(ancestor, n - i);
1098 r = fn(ancestor, user);
1099 isl_schedule_node_free(ancestor);
1100 if (r < 0)
1101 return -1;
1104 return 0;
1107 /* Is any node in the subtree rooted at "node" anchored?
1108 * That is, do any of these nodes reference the outer band nodes?
1110 int isl_schedule_node_is_subtree_anchored(__isl_keep isl_schedule_node *node)
1112 if (!node)
1113 return -1;
1114 return isl_schedule_tree_is_subtree_anchored(node->tree);
1117 /* Return the number of members in the given band node.
1119 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node *node)
1121 return node ? isl_schedule_tree_band_n_member(node->tree) : 0;
1124 /* Is the band member at position "pos" of the band node "node"
1125 * marked coincident?
1127 int isl_schedule_node_band_member_get_coincident(
1128 __isl_keep isl_schedule_node *node, int pos)
1130 if (!node)
1131 return -1;
1132 return isl_schedule_tree_band_member_get_coincident(node->tree, pos);
1135 /* Mark the band member at position "pos" the band node "node"
1136 * as being coincident or not according to "coincident".
1138 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_coincident(
1139 __isl_take isl_schedule_node *node, int pos, int coincident)
1141 int c;
1142 isl_schedule_tree *tree;
1144 if (!node)
1145 return NULL;
1146 c = isl_schedule_node_band_member_get_coincident(node, pos);
1147 if (c == coincident)
1148 return node;
1150 tree = isl_schedule_tree_copy(node->tree);
1151 tree = isl_schedule_tree_band_member_set_coincident(tree, pos,
1152 coincident);
1153 node = isl_schedule_node_graft_tree(node, tree);
1155 return node;
1158 /* Is the band node "node" marked permutable?
1160 int isl_schedule_node_band_get_permutable(__isl_keep isl_schedule_node *node)
1162 if (!node)
1163 return -1;
1165 return isl_schedule_tree_band_get_permutable(node->tree);
1168 /* Mark the band node "node" permutable or not according to "permutable"?
1170 __isl_give isl_schedule_node *isl_schedule_node_band_set_permutable(
1171 __isl_take isl_schedule_node *node, int permutable)
1173 isl_schedule_tree *tree;
1175 if (!node)
1176 return NULL;
1177 if (isl_schedule_node_band_get_permutable(node) == permutable)
1178 return node;
1180 tree = isl_schedule_tree_copy(node->tree);
1181 tree = isl_schedule_tree_band_set_permutable(tree, permutable);
1182 node = isl_schedule_node_graft_tree(node, tree);
1184 return node;
1187 /* Return the schedule space of the band node.
1189 __isl_give isl_space *isl_schedule_node_band_get_space(
1190 __isl_keep isl_schedule_node *node)
1192 if (!node)
1193 return NULL;
1195 return isl_schedule_tree_band_get_space(node->tree);
1198 /* Return the schedule of the band node in isolation.
1200 __isl_give isl_multi_union_pw_aff *isl_schedule_node_band_get_partial_schedule(
1201 __isl_keep isl_schedule_node *node)
1203 if (!node)
1204 return NULL;
1206 return isl_schedule_tree_band_get_partial_schedule(node->tree);
1209 /* Return the schedule of the band node in isolation in the form of
1210 * an isl_union_map.
1212 * If the band does not have any members, then we construct a universe map
1213 * with the universe of the domain elements reaching the node as domain.
1214 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1215 * convert that to an isl_union_map.
1217 __isl_give isl_union_map *isl_schedule_node_band_get_partial_schedule_union_map(
1218 __isl_keep isl_schedule_node *node)
1220 isl_multi_union_pw_aff *mupa;
1222 if (!node)
1223 return NULL;
1225 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
1226 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1227 "not a band node", return NULL);
1228 if (isl_schedule_node_band_n_member(node) == 0) {
1229 isl_union_set *domain;
1231 domain = isl_schedule_node_get_universe_domain(node);
1232 return isl_union_map_from_domain(domain);
1235 mupa = isl_schedule_node_band_get_partial_schedule(node);
1236 return isl_union_map_from_multi_union_pw_aff(mupa);
1239 /* Return the loop AST generation type for the band member of band node "node"
1240 * at position "pos".
1242 enum isl_ast_loop_type isl_schedule_node_band_member_get_ast_loop_type(
1243 __isl_keep isl_schedule_node *node, int pos)
1245 if (!node)
1246 return isl_ast_loop_error;
1248 return isl_schedule_tree_band_member_get_ast_loop_type(node->tree, pos);
1251 /* Set the loop AST generation type for the band member of band node "node"
1252 * at position "pos" to "type".
1254 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_ast_loop_type(
1255 __isl_take isl_schedule_node *node, int pos,
1256 enum isl_ast_loop_type type)
1258 isl_schedule_tree *tree;
1260 if (!node)
1261 return NULL;
1263 tree = isl_schedule_tree_copy(node->tree);
1264 tree = isl_schedule_tree_band_member_set_ast_loop_type(tree, pos, type);
1265 return isl_schedule_node_graft_tree(node, tree);
1268 /* Return the loop AST generation type for the band member of band node "node"
1269 * at position "pos" for the isolated part.
1271 enum isl_ast_loop_type isl_schedule_node_band_member_get_isolate_ast_loop_type(
1272 __isl_keep isl_schedule_node *node, int pos)
1274 if (!node)
1275 return isl_ast_loop_error;
1277 return isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1278 node->tree, pos);
1281 /* Set the loop AST generation type for the band member of band node "node"
1282 * at position "pos" for the isolated part to "type".
1284 __isl_give isl_schedule_node *
1285 isl_schedule_node_band_member_set_isolate_ast_loop_type(
1286 __isl_take isl_schedule_node *node, int pos,
1287 enum isl_ast_loop_type type)
1289 isl_schedule_tree *tree;
1291 if (!node)
1292 return NULL;
1294 tree = isl_schedule_tree_copy(node->tree);
1295 tree = isl_schedule_tree_band_member_set_isolate_ast_loop_type(tree,
1296 pos, type);
1297 return isl_schedule_node_graft_tree(node, tree);
1300 /* Return the AST build options associated to band node "node".
1302 __isl_give isl_union_set *isl_schedule_node_band_get_ast_build_options(
1303 __isl_keep isl_schedule_node *node)
1305 if (!node)
1306 return NULL;
1308 return isl_schedule_tree_band_get_ast_build_options(node->tree);
1311 /* Replace the AST build options associated to band node "node" by "options".
1313 __isl_give isl_schedule_node *isl_schedule_node_band_set_ast_build_options(
1314 __isl_take isl_schedule_node *node, __isl_take isl_union_set *options)
1316 isl_schedule_tree *tree;
1318 if (!node || !options)
1319 goto error;
1321 tree = isl_schedule_tree_copy(node->tree);
1322 tree = isl_schedule_tree_band_set_ast_build_options(tree, options);
1323 return isl_schedule_node_graft_tree(node, tree);
1324 error:
1325 isl_schedule_node_free(node);
1326 isl_union_set_free(options);
1327 return NULL;
1330 /* Make sure that that spaces of "node" and "mv" are the same.
1331 * Return -1 on error, reporting the error to the user.
1333 static int check_space_multi_val(__isl_keep isl_schedule_node *node,
1334 __isl_keep isl_multi_val *mv)
1336 isl_space *node_space, *mv_space;
1337 int equal;
1339 node_space = isl_schedule_node_band_get_space(node);
1340 mv_space = isl_multi_val_get_space(mv);
1341 equal = isl_space_tuple_is_equal(node_space, isl_dim_set,
1342 mv_space, isl_dim_set);
1343 isl_space_free(mv_space);
1344 isl_space_free(node_space);
1345 if (equal < 0)
1346 return -1;
1347 if (!equal)
1348 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1349 "spaces don't match", return -1);
1351 return 0;
1354 /* Multiply the partial schedule of the band node "node"
1355 * with the factors in "mv".
1357 __isl_give isl_schedule_node *isl_schedule_node_band_scale(
1358 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1360 isl_schedule_tree *tree;
1361 int anchored;
1363 if (!node || !mv)
1364 goto error;
1365 if (check_space_multi_val(node, mv) < 0)
1366 goto error;
1367 anchored = isl_schedule_node_is_subtree_anchored(node);
1368 if (anchored < 0)
1369 goto error;
1370 if (anchored)
1371 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1372 "cannot scale band node with anchored subtree",
1373 goto error);
1375 tree = isl_schedule_node_get_tree(node);
1376 tree = isl_schedule_tree_band_scale(tree, mv);
1377 return isl_schedule_node_graft_tree(node, tree);
1378 error:
1379 isl_multi_val_free(mv);
1380 isl_schedule_node_free(node);
1381 return NULL;
1384 /* Divide the partial schedule of the band node "node"
1385 * by the factors in "mv".
1387 __isl_give isl_schedule_node *isl_schedule_node_band_scale_down(
1388 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1390 isl_schedule_tree *tree;
1391 int anchored;
1393 if (!node || !mv)
1394 goto error;
1395 if (check_space_multi_val(node, mv) < 0)
1396 goto error;
1397 anchored = isl_schedule_node_is_subtree_anchored(node);
1398 if (anchored < 0)
1399 goto error;
1400 if (anchored)
1401 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1402 "cannot scale down band node with anchored subtree",
1403 goto error);
1405 tree = isl_schedule_node_get_tree(node);
1406 tree = isl_schedule_tree_band_scale_down(tree, mv);
1407 return isl_schedule_node_graft_tree(node, tree);
1408 error:
1409 isl_multi_val_free(mv);
1410 isl_schedule_node_free(node);
1411 return NULL;
1414 /* Tile "node" with tile sizes "sizes".
1416 * The current node is replaced by two nested nodes corresponding
1417 * to the tile dimensions and the point dimensions.
1419 * Return a pointer to the outer (tile) node.
1421 * If any of the descendants of "node" depend on the set of outer band nodes,
1422 * then we refuse to tile the node.
1424 * If the scale tile loops option is set, then the tile loops
1425 * are scaled by the tile sizes. If the shift point loops option is set,
1426 * then the point loops are shifted to start at zero.
1427 * In particular, these options affect the tile and point loop schedules
1428 * as follows
1430 * scale shift original tile point
1432 * 0 0 i floor(i/s) i
1433 * 1 0 i s * floor(i/s) i
1434 * 0 1 i floor(i/s) i - s * floor(i/s)
1435 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1437 __isl_give isl_schedule_node *isl_schedule_node_band_tile(
1438 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
1440 isl_schedule_tree *tree;
1441 int anchored;
1443 if (!node || !sizes)
1444 goto error;
1445 anchored = isl_schedule_node_is_subtree_anchored(node);
1446 if (anchored < 0)
1447 goto error;
1448 if (anchored)
1449 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1450 "cannot tile band node with anchored subtree",
1451 goto error);
1453 if (check_space_multi_val(node, sizes) < 0)
1454 goto error;
1456 tree = isl_schedule_node_get_tree(node);
1457 tree = isl_schedule_tree_band_tile(tree, sizes);
1458 return isl_schedule_node_graft_tree(node, tree);
1459 error:
1460 isl_multi_val_free(sizes);
1461 isl_schedule_node_free(node);
1462 return NULL;
1465 /* Move the band node "node" down to all the leaves in the subtree
1466 * rooted at "node".
1467 * Return a pointer to the node in the resulting tree that is in the same
1468 * position as the node pointed to by "node" in the original tree.
1470 * If the node only has a leaf child, then nothing needs to be done.
1471 * Otherwise, the child of the node is removed and the result is
1472 * appended to all the leaves in the subtree rooted at the original child.
1473 * The original node is then replaced by the result of this operation.
1475 * If any of the nodes in the subtree rooted at "node" depend on
1476 * the set of outer band nodes then we refuse to sink the band node.
1478 __isl_give isl_schedule_node *isl_schedule_node_band_sink(
1479 __isl_take isl_schedule_node *node)
1481 enum isl_schedule_node_type type;
1482 isl_schedule_tree *tree, *child;
1483 int anchored;
1485 if (!node)
1486 return NULL;
1488 type = isl_schedule_node_get_type(node);
1489 if (type != isl_schedule_node_band)
1490 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1491 "not a band node", isl_schedule_node_free(node));
1492 anchored = isl_schedule_node_is_subtree_anchored(node);
1493 if (anchored < 0)
1494 return isl_schedule_node_free(node);
1495 if (anchored)
1496 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1497 "cannot sink band node in anchored subtree",
1498 isl_schedule_node_free(node));
1499 if (isl_schedule_tree_n_children(node->tree) == 0)
1500 return node;
1502 tree = isl_schedule_node_get_tree(node);
1503 child = isl_schedule_tree_get_child(tree, 0);
1504 tree = isl_schedule_tree_reset_children(tree);
1505 tree = isl_schedule_tree_append_to_leaves(child, tree);
1507 return isl_schedule_node_graft_tree(node, tree);
1510 /* Split "node" into two nested band nodes, one with the first "pos"
1511 * dimensions and one with the remaining dimensions.
1512 * The schedules of the two band nodes live in anonymous spaces.
1514 __isl_give isl_schedule_node *isl_schedule_node_band_split(
1515 __isl_take isl_schedule_node *node, int pos)
1517 isl_schedule_tree *tree;
1519 tree = isl_schedule_node_get_tree(node);
1520 tree = isl_schedule_tree_band_split(tree, pos);
1521 return isl_schedule_node_graft_tree(node, tree);
1524 /* Return the context of the context node "node".
1526 __isl_give isl_set *isl_schedule_node_context_get_context(
1527 __isl_keep isl_schedule_node *node)
1529 if (!node)
1530 return NULL;
1532 return isl_schedule_tree_context_get_context(node->tree);
1535 /* Return the domain of the domain node "node".
1537 __isl_give isl_union_set *isl_schedule_node_domain_get_domain(
1538 __isl_keep isl_schedule_node *node)
1540 if (!node)
1541 return NULL;
1543 return isl_schedule_tree_domain_get_domain(node->tree);
1546 /* Return the filter of the filter node "node".
1548 __isl_give isl_union_set *isl_schedule_node_filter_get_filter(
1549 __isl_keep isl_schedule_node *node)
1551 if (!node)
1552 return NULL;
1554 return isl_schedule_tree_filter_get_filter(node->tree);
1557 /* Replace the filter of filter node "node" by "filter".
1559 __isl_give isl_schedule_node *isl_schedule_node_filter_set_filter(
1560 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
1562 isl_schedule_tree *tree;
1564 if (!node || !filter)
1565 goto error;
1567 tree = isl_schedule_tree_copy(node->tree);
1568 tree = isl_schedule_tree_filter_set_filter(tree, filter);
1569 return isl_schedule_node_graft_tree(node, tree);
1570 error:
1571 isl_schedule_node_free(node);
1572 isl_union_set_free(filter);
1573 return NULL;
1576 /* Update the ancestors of "node" to point to the tree that "node"
1577 * now points to.
1578 * That is, replace the child in the original parent that corresponds
1579 * to the current tree position by node->tree and continue updating
1580 * the ancestors in the same way until the root is reached.
1582 * If "node" originally points to a leaf of the schedule tree, then make sure
1583 * that in the end it points to a leaf in the updated schedule tree.
1585 static __isl_give isl_schedule_node *update_ancestors(
1586 __isl_take isl_schedule_node *node)
1588 int i, n;
1589 int is_leaf;
1590 isl_ctx *ctx;
1591 isl_schedule_tree *tree;
1593 node = isl_schedule_node_cow(node);
1594 if (!node)
1595 return NULL;
1597 ctx = isl_schedule_node_get_ctx(node);
1598 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1599 tree = isl_schedule_tree_copy(node->tree);
1601 for (i = n - 1; i >= 0; --i) {
1602 isl_schedule_tree *parent;
1604 parent = isl_schedule_tree_list_get_schedule_tree(
1605 node->ancestors, i);
1606 parent = isl_schedule_tree_replace_child(parent,
1607 node->child_pos[i], tree);
1608 node->ancestors = isl_schedule_tree_list_set_schedule_tree(
1609 node->ancestors, i, isl_schedule_tree_copy(parent));
1611 tree = parent;
1614 is_leaf = isl_schedule_tree_is_leaf(node->tree);
1615 node->schedule = isl_schedule_set_root(node->schedule, tree);
1616 if (is_leaf) {
1617 isl_schedule_tree_free(node->tree);
1618 node->tree = isl_schedule_node_get_leaf(node);
1621 if (!node->schedule || !node->ancestors)
1622 return isl_schedule_node_free(node);
1624 return node;
1627 /* Replace the subtree that "pos" points to by "tree", updating
1628 * the ancestors to maintain a consistent state.
1630 __isl_give isl_schedule_node *isl_schedule_node_graft_tree(
1631 __isl_take isl_schedule_node *pos, __isl_take isl_schedule_tree *tree)
1633 if (!tree || !pos)
1634 goto error;
1635 if (pos->tree == tree) {
1636 isl_schedule_tree_free(tree);
1637 return pos;
1640 pos = isl_schedule_node_cow(pos);
1641 if (!pos)
1642 goto error;
1644 isl_schedule_tree_free(pos->tree);
1645 pos->tree = tree;
1647 return update_ancestors(pos);
1648 error:
1649 isl_schedule_node_free(pos);
1650 isl_schedule_tree_free(tree);
1651 return NULL;
1654 /* Make sure we can insert a node between "node" and its parent.
1655 * Return -1 on error, reporting the reason why we cannot insert a node.
1657 static int check_insert(__isl_keep isl_schedule_node *node)
1659 int has_parent;
1660 enum isl_schedule_node_type type;
1662 has_parent = isl_schedule_node_has_parent(node);
1663 if (has_parent < 0)
1664 return -1;
1665 if (!has_parent)
1666 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1667 "cannot insert node outside of root", return -1);
1669 type = isl_schedule_node_get_parent_type(node);
1670 if (type == isl_schedule_node_error)
1671 return -1;
1672 if (type == isl_schedule_node_set || type == isl_schedule_node_sequence)
1673 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1674 "cannot insert node between set or sequence node "
1675 "and its filter children", return -1);
1677 return 0;
1680 /* Insert a band node with partial schedule "mupa" between "node" and
1681 * its parent.
1682 * Return a pointer to the new band node.
1684 * If any of the nodes in the subtree rooted at "node" depend on
1685 * the set of outer band nodes then we refuse to insert the band node.
1687 __isl_give isl_schedule_node *isl_schedule_node_insert_partial_schedule(
1688 __isl_take isl_schedule_node *node,
1689 __isl_take isl_multi_union_pw_aff *mupa)
1691 int anchored;
1692 isl_schedule_band *band;
1693 isl_schedule_tree *tree;
1695 if (check_insert(node) < 0)
1696 node = isl_schedule_node_free(node);
1697 anchored = isl_schedule_node_is_subtree_anchored(node);
1698 if (anchored < 0)
1699 goto error;
1700 if (anchored)
1701 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1702 "cannot insert band node in anchored subtree",
1703 goto error);
1705 tree = isl_schedule_node_get_tree(node);
1706 band = isl_schedule_band_from_multi_union_pw_aff(mupa);
1707 tree = isl_schedule_tree_insert_band(tree, band);
1708 node = isl_schedule_node_graft_tree(node, tree);
1710 return node;
1711 error:
1712 isl_schedule_node_free(node);
1713 isl_multi_union_pw_aff_free(mupa);
1714 return NULL;
1717 /* Insert a context node with context "context" between "node" and its parent.
1718 * Return a pointer to the new context node.
1720 __isl_give isl_schedule_node *isl_schedule_node_insert_context(
1721 __isl_take isl_schedule_node *node, __isl_take isl_set *context)
1723 isl_schedule_tree *tree;
1725 if (check_insert(node) < 0)
1726 node = isl_schedule_node_free(node);
1728 tree = isl_schedule_node_get_tree(node);
1729 tree = isl_schedule_tree_insert_context(tree, context);
1730 node = isl_schedule_node_graft_tree(node, tree);
1732 return node;
1735 /* Insert a filter node with filter "filter" between "node" and its parent.
1736 * Return a pointer to the new filter node.
1738 __isl_give isl_schedule_node *isl_schedule_node_insert_filter(
1739 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
1741 isl_schedule_tree *tree;
1743 if (check_insert(node) < 0)
1744 node = isl_schedule_node_free(node);
1746 tree = isl_schedule_node_get_tree(node);
1747 tree = isl_schedule_tree_insert_filter(tree, filter);
1748 node = isl_schedule_node_graft_tree(node, tree);
1750 return node;
1753 /* Attach the current subtree of "node" to a sequence of filter tree nodes
1754 * with filters described by "filters", attach this sequence
1755 * of filter tree nodes as children to a new tree of type "type" and
1756 * replace the original subtree of "node" by this new tree.
1758 static __isl_give isl_schedule_node *isl_schedule_node_insert_children(
1759 __isl_take isl_schedule_node *node,
1760 enum isl_schedule_node_type type,
1761 __isl_take isl_union_set_list *filters)
1763 int i, n;
1764 isl_ctx *ctx;
1765 isl_schedule_tree *tree;
1766 isl_schedule_tree_list *list;
1768 if (check_insert(node) < 0)
1769 node = isl_schedule_node_free(node);
1771 if (!node || !filters)
1772 goto error;
1774 ctx = isl_schedule_node_get_ctx(node);
1775 n = isl_union_set_list_n_union_set(filters);
1776 list = isl_schedule_tree_list_alloc(ctx, n);
1777 for (i = 0; i < n; ++i) {
1778 isl_schedule_tree *tree;
1779 isl_union_set *filter;
1781 tree = isl_schedule_node_get_tree(node);
1782 filter = isl_union_set_list_get_union_set(filters, i);
1783 tree = isl_schedule_tree_insert_filter(tree, filter);
1784 list = isl_schedule_tree_list_add(list, tree);
1786 tree = isl_schedule_tree_from_children(type, list);
1787 node = isl_schedule_node_graft_tree(node, tree);
1789 isl_union_set_list_free(filters);
1790 return node;
1791 error:
1792 isl_union_set_list_free(filters);
1793 isl_schedule_node_free(node);
1794 return NULL;
1797 /* Insert a sequence node with child filters "filters" between "node" and
1798 * its parent. That is, the tree that "node" points to is attached
1799 * to each of the child nodes of the filter nodes.
1800 * Return a pointer to the new sequence node.
1802 __isl_give isl_schedule_node *isl_schedule_node_insert_sequence(
1803 __isl_take isl_schedule_node *node,
1804 __isl_take isl_union_set_list *filters)
1806 return isl_schedule_node_insert_children(node,
1807 isl_schedule_node_sequence, filters);
1810 /* Insert a set node with child filters "filters" between "node" and
1811 * its parent. That is, the tree that "node" points to is attached
1812 * to each of the child nodes of the filter nodes.
1813 * Return a pointer to the new set node.
1815 __isl_give isl_schedule_node *isl_schedule_node_insert_set(
1816 __isl_take isl_schedule_node *node,
1817 __isl_take isl_union_set_list *filters)
1819 return isl_schedule_node_insert_children(node,
1820 isl_schedule_node_set, filters);
1823 /* Remove "node" from its schedule tree and return a pointer
1824 * to the leaf at the same position in the updated schedule tree.
1826 * It is not allowed to remove the root of a schedule tree or
1827 * a child of a set or sequence node.
1829 __isl_give isl_schedule_node *isl_schedule_node_cut(
1830 __isl_take isl_schedule_node *node)
1832 isl_schedule_tree *leaf;
1833 enum isl_schedule_node_type parent_type;
1835 if (!node)
1836 return NULL;
1837 if (!isl_schedule_node_has_parent(node))
1838 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1839 "cannot cut root", return isl_schedule_node_free(node));
1841 parent_type = isl_schedule_node_get_parent_type(node);
1842 if (parent_type == isl_schedule_node_set ||
1843 parent_type == isl_schedule_node_sequence)
1844 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1845 "cannot cut child of set or sequence",
1846 return isl_schedule_node_free(node));
1848 leaf = isl_schedule_node_get_leaf(node);
1849 return isl_schedule_node_graft_tree(node, leaf);
1852 /* Remove a single node from the schedule tree, attaching the child
1853 * of "node" directly to its parent.
1854 * Return a pointer to this former child or to the leaf the position
1855 * of the original node if there was no child.
1856 * It is not allowed to remove the root of a schedule tree,
1857 * a set or sequence node, a child of a set or sequence node or
1858 * a band node with an anchored subtree.
1860 __isl_give isl_schedule_node *isl_schedule_node_delete(
1861 __isl_take isl_schedule_node *node)
1863 int n;
1864 isl_schedule_tree *tree;
1865 enum isl_schedule_node_type type;
1867 if (!node)
1868 return NULL;
1870 if (isl_schedule_node_get_tree_depth(node) == 0)
1871 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1872 "cannot delete root node",
1873 return isl_schedule_node_free(node));
1874 n = isl_schedule_node_n_children(node);
1875 if (n != 1)
1876 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1877 "can only delete node with a single child",
1878 return isl_schedule_node_free(node));
1879 type = isl_schedule_node_get_parent_type(node);
1880 if (type == isl_schedule_node_sequence || type == isl_schedule_node_set)
1881 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1882 "cannot delete child of set or sequence",
1883 return isl_schedule_node_free(node));
1884 if (isl_schedule_node_get_type(node) == isl_schedule_node_band) {
1885 int anchored;
1887 anchored = isl_schedule_node_is_subtree_anchored(node);
1888 if (anchored < 0)
1889 return isl_schedule_node_free(node);
1890 if (anchored)
1891 isl_die(isl_schedule_node_get_ctx(node),
1892 isl_error_invalid,
1893 "cannot delete band node with anchored subtree",
1894 return isl_schedule_node_free(node));
1897 tree = isl_schedule_node_get_tree(node);
1898 if (!tree || isl_schedule_tree_has_children(tree)) {
1899 tree = isl_schedule_tree_child(tree, 0);
1900 } else {
1901 isl_schedule_tree_free(tree);
1902 tree = isl_schedule_node_get_leaf(node);
1904 node = isl_schedule_node_graft_tree(node, tree);
1906 return node;
1909 /* Compute the gist of the given band node with respect to "context".
1911 __isl_give isl_schedule_node *isl_schedule_node_band_gist(
1912 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
1914 isl_schedule_tree *tree;
1916 tree = isl_schedule_node_get_tree(node);
1917 tree = isl_schedule_tree_band_gist(tree, context);
1918 return isl_schedule_node_graft_tree(node, tree);
1921 /* Internal data structure for isl_schedule_node_gist.
1922 * "filters" contains an element for each outer filter node
1923 * with respect to the current position, each representing
1924 * the intersection of the previous element and the filter on the filter node.
1925 * The first element in the original context passed to isl_schedule_node_gist.
1927 struct isl_node_gist_data {
1928 isl_union_set_list *filters;
1931 /* Can we finish gisting at this node?
1932 * That is, is the filter on the current filter node a subset of
1933 * the original context passed to isl_schedule_node_gist?
1935 static int gist_done(__isl_keep isl_schedule_node *node,
1936 struct isl_node_gist_data *data)
1938 isl_union_set *filter, *outer;
1939 int subset;
1941 filter = isl_schedule_node_filter_get_filter(node);
1942 outer = isl_union_set_list_get_union_set(data->filters, 0);
1943 subset = isl_union_set_is_subset(filter, outer);
1944 isl_union_set_free(outer);
1945 isl_union_set_free(filter);
1947 return subset;
1950 /* Callback for "traverse" to enter a node and to move
1951 * to the deepest initial subtree that should be traversed
1952 * by isl_schedule_node_gist.
1954 * The "filters" list is extended by one element each time
1955 * we come across a filter node by the result of intersecting
1956 * the last element in the list with the filter on the filter node.
1958 * If the filter on the current filter node is a subset of
1959 * the original context passed to isl_schedule_node_gist,
1960 * then there is no need to go into its subtree since it cannot
1961 * be further simplified by the context. The "filters" list is
1962 * still extended for consistency, but the actual value of the
1963 * added element is immaterial since it will not be used.
1965 * Otherwise, the filter on the current filter node is replaced by
1966 * the gist of the original filter with respect to the intersection
1967 * of the original context with the intermediate filters.
1969 * If the new element in the "filters" list is empty, then no elements
1970 * can reach the descendants of the current filter node. The subtree
1971 * underneath the filter node is therefore removed.
1973 static __isl_give isl_schedule_node *gist_enter(
1974 __isl_take isl_schedule_node *node, void *user)
1976 struct isl_node_gist_data *data = user;
1978 do {
1979 isl_union_set *filter, *inner;
1980 int done, empty;
1981 int n;
1983 switch (isl_schedule_node_get_type(node)) {
1984 case isl_schedule_node_error:
1985 return isl_schedule_node_free(node);
1986 case isl_schedule_node_band:
1987 case isl_schedule_node_context:
1988 case isl_schedule_node_domain:
1989 case isl_schedule_node_leaf:
1990 case isl_schedule_node_sequence:
1991 case isl_schedule_node_set:
1992 continue;
1993 case isl_schedule_node_filter:
1994 break;
1996 done = gist_done(node, data);
1997 filter = isl_schedule_node_filter_get_filter(node);
1998 if (done < 0 || done) {
1999 data->filters = isl_union_set_list_add(data->filters,
2000 filter);
2001 if (done < 0)
2002 return isl_schedule_node_free(node);
2003 return node;
2005 n = isl_union_set_list_n_union_set(data->filters);
2006 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
2007 filter = isl_union_set_gist(filter, isl_union_set_copy(inner));
2008 node = isl_schedule_node_filter_set_filter(node,
2009 isl_union_set_copy(filter));
2010 filter = isl_union_set_intersect(filter, inner);
2011 empty = isl_union_set_is_empty(filter);
2012 data->filters = isl_union_set_list_add(data->filters, filter);
2013 if (empty < 0)
2014 return isl_schedule_node_free(node);
2015 if (!empty)
2016 continue;
2017 node = isl_schedule_node_child(node, 0);
2018 node = isl_schedule_node_cut(node);
2019 node = isl_schedule_node_parent(node);
2020 return node;
2021 } while (isl_schedule_node_has_children(node) &&
2022 (node = isl_schedule_node_first_child(node)) != NULL);
2024 return node;
2027 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
2029 * In particular, if the current node is a filter node, then we remove
2030 * the element on the "filters" list that was added when we entered
2031 * the node. There is no need to compute any gist here, since we
2032 * already did that when we entered the node.
2034 * If the current node is a band node, then we compute the gist of
2035 * the band node with respect to the intersection of the original context
2036 * and the intermediate filters.
2038 * If the current node is a sequence or set node, then some of
2039 * the filter children may have become empty and so they are removed.
2040 * If only one child is left, then the set or sequence node along with
2041 * the single remaining child filter is removed. The filter can be
2042 * removed because the filters on a sequence or set node are supposed
2043 * to partition the incoming domain instances.
2044 * In principle, it should then be impossible for there to be zero
2045 * remaining children, but should this happen, we replace the entire
2046 * subtree with an empty filter.
2048 static __isl_give isl_schedule_node *gist_leave(
2049 __isl_take isl_schedule_node *node, void *user)
2051 struct isl_node_gist_data *data = user;
2052 isl_schedule_tree *tree;
2053 int i, n;
2054 isl_union_set *filter;
2056 switch (isl_schedule_node_get_type(node)) {
2057 case isl_schedule_node_error:
2058 return isl_schedule_node_free(node);
2059 case isl_schedule_node_filter:
2060 n = isl_union_set_list_n_union_set(data->filters);
2061 data->filters = isl_union_set_list_drop(data->filters,
2062 n - 1, 1);
2063 break;
2064 case isl_schedule_node_band:
2065 n = isl_union_set_list_n_union_set(data->filters);
2066 filter = isl_union_set_list_get_union_set(data->filters, n - 1);
2067 node = isl_schedule_node_band_gist(node, filter);
2068 break;
2069 case isl_schedule_node_set:
2070 case isl_schedule_node_sequence:
2071 tree = isl_schedule_node_get_tree(node);
2072 n = isl_schedule_tree_n_children(tree);
2073 for (i = n - 1; i >= 0; --i) {
2074 isl_schedule_tree *child;
2075 isl_union_set *filter;
2076 int empty;
2078 child = isl_schedule_tree_get_child(tree, i);
2079 filter = isl_schedule_tree_filter_get_filter(child);
2080 empty = isl_union_set_is_empty(filter);
2081 isl_union_set_free(filter);
2082 isl_schedule_tree_free(child);
2083 if (empty < 0)
2084 tree = isl_schedule_tree_free(tree);
2085 else if (empty)
2086 tree = isl_schedule_tree_drop_child(tree, i);
2088 n = isl_schedule_tree_n_children(tree);
2089 node = isl_schedule_node_graft_tree(node, tree);
2090 if (n == 1) {
2091 node = isl_schedule_node_delete(node);
2092 node = isl_schedule_node_delete(node);
2093 } else if (n == 0) {
2094 isl_space *space;
2096 filter =
2097 isl_union_set_list_get_union_set(data->filters, 0);
2098 space = isl_union_set_get_space(filter);
2099 isl_union_set_free(filter);
2100 filter = isl_union_set_empty(space);
2101 node = isl_schedule_node_cut(node);
2102 node = isl_schedule_node_insert_filter(node, filter);
2104 break;
2105 case isl_schedule_node_context:
2106 case isl_schedule_node_domain:
2107 case isl_schedule_node_leaf:
2108 break;
2111 return node;
2114 /* Compute the gist of the subtree at "node" with respect to
2115 * the reaching domain elements in "context".
2116 * In particular, compute the gist of all band and filter nodes
2117 * in the subtree with respect to "context". Children of set or sequence
2118 * nodes that end up with an empty filter are removed completely.
2120 * We keep track of the intersection of "context" with all outer filters
2121 * of the current node within the subtree in the final element of "filters".
2122 * Initially, this list contains the single element "context" and it is
2123 * extended or shortened each time we enter or leave a filter node.
2125 __isl_give isl_schedule_node *isl_schedule_node_gist(
2126 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
2128 struct isl_node_gist_data data;
2130 data.filters = isl_union_set_list_from_union_set(context);
2131 node = traverse(node, &gist_enter, &gist_leave, &data);
2132 isl_union_set_list_free(data.filters);
2133 return node;
2136 /* Intersect the domain of domain node "node" with "domain".
2138 * If the domain of "node" is already a subset of "domain",
2139 * then nothing needs to be changed.
2141 * Otherwise, we replace the domain of the domain node by the intersection
2142 * and simplify the subtree rooted at "node" with respect to this intersection.
2144 __isl_give isl_schedule_node *isl_schedule_node_domain_intersect_domain(
2145 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain)
2147 isl_schedule_tree *tree;
2148 isl_union_set *uset;
2149 int is_subset;
2151 if (!node || !domain)
2152 goto error;
2154 uset = isl_schedule_tree_domain_get_domain(node->tree);
2155 is_subset = isl_union_set_is_subset(uset, domain);
2156 isl_union_set_free(uset);
2157 if (is_subset < 0)
2158 goto error;
2159 if (is_subset) {
2160 isl_union_set_free(domain);
2161 return node;
2164 tree = isl_schedule_tree_copy(node->tree);
2165 uset = isl_schedule_tree_domain_get_domain(tree);
2166 uset = isl_union_set_intersect(uset, domain);
2167 tree = isl_schedule_tree_domain_set_domain(tree,
2168 isl_union_set_copy(uset));
2169 node = isl_schedule_node_graft_tree(node, tree);
2171 node = isl_schedule_node_child(node, 0);
2172 node = isl_schedule_node_gist(node, uset);
2173 node = isl_schedule_node_parent(node);
2175 return node;
2176 error:
2177 isl_schedule_node_free(node);
2178 isl_union_set_free(domain);
2179 return NULL;
2182 /* Reset the user pointer on all identifiers of parameters and tuples
2183 * in the schedule node "node".
2185 __isl_give isl_schedule_node *isl_schedule_node_reset_user(
2186 __isl_take isl_schedule_node *node)
2188 isl_schedule_tree *tree;
2190 tree = isl_schedule_node_get_tree(node);
2191 tree = isl_schedule_tree_reset_user(tree);
2192 node = isl_schedule_node_graft_tree(node, tree);
2194 return node;
2197 /* Align the parameters of the schedule node "node" to those of "space".
2199 __isl_give isl_schedule_node *isl_schedule_node_align_params(
2200 __isl_take isl_schedule_node *node, __isl_take isl_space *space)
2202 isl_schedule_tree *tree;
2204 tree = isl_schedule_node_get_tree(node);
2205 tree = isl_schedule_tree_align_params(tree, space);
2206 node = isl_schedule_node_graft_tree(node, tree);
2208 return node;
2211 /* Compute the pullback of schedule node "node"
2212 * by the function represented by "upma".
2213 * In other words, plug in "upma" in the iteration domains
2214 * of schedule node "node".
2216 * Note that this is only a helper function for
2217 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
2218 * this function should not be called on a single node without also
2219 * calling it on all the other nodes.
2221 __isl_give isl_schedule_node *isl_schedule_node_pullback_union_pw_multi_aff(
2222 __isl_take isl_schedule_node *node,
2223 __isl_take isl_union_pw_multi_aff *upma)
2225 isl_schedule_tree *tree;
2227 tree = isl_schedule_node_get_tree(node);
2228 tree = isl_schedule_tree_pullback_union_pw_multi_aff(tree, upma);
2229 node = isl_schedule_node_graft_tree(node, tree);
2231 return node;
2234 /* Return the position of the subtree containing "node" among the children
2235 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
2236 * In particular, both nodes should point to the same schedule tree.
2238 * Return -1 on error.
2240 int isl_schedule_node_get_ancestor_child_position(
2241 __isl_keep isl_schedule_node *node,
2242 __isl_keep isl_schedule_node *ancestor)
2244 int n1, n2;
2245 isl_schedule_tree *tree;
2247 if (!node || !ancestor)
2248 return -1;
2250 if (node->schedule != ancestor->schedule)
2251 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2252 "not a descendant", return -1);
2254 n1 = isl_schedule_node_get_tree_depth(ancestor);
2255 n2 = isl_schedule_node_get_tree_depth(node);
2257 if (n1 >= n2)
2258 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2259 "not a descendant", return -1);
2260 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n1);
2261 isl_schedule_tree_free(tree);
2262 if (tree != ancestor->tree)
2263 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2264 "not a descendant", return -1);
2266 return node->child_pos[n1];
2269 /* Given two nodes that point to the same schedule tree, return their
2270 * closest shared ancestor.
2272 * Since the two nodes point to the same schedule, they share at least
2273 * one ancestor, the root of the schedule. We move down from the root
2274 * to the first ancestor where the respective children have a different
2275 * child position. This is the requested ancestor.
2276 * If there is no ancestor where the children have a different position,
2277 * then one node is an ancestor of the other and then this node is
2278 * the requested ancestor.
2280 __isl_give isl_schedule_node *isl_schedule_node_get_shared_ancestor(
2281 __isl_keep isl_schedule_node *node1,
2282 __isl_keep isl_schedule_node *node2)
2284 int i, n1, n2;
2286 if (!node1 || !node2)
2287 return NULL;
2288 if (node1->schedule != node2->schedule)
2289 isl_die(isl_schedule_node_get_ctx(node1), isl_error_invalid,
2290 "not part of same schedule", return NULL);
2291 n1 = isl_schedule_node_get_tree_depth(node1);
2292 n2 = isl_schedule_node_get_tree_depth(node2);
2293 if (n2 < n1)
2294 return isl_schedule_node_get_shared_ancestor(node2, node1);
2295 if (n1 == 0)
2296 return isl_schedule_node_copy(node1);
2297 if (isl_schedule_node_is_equal(node1, node2))
2298 return isl_schedule_node_copy(node1);
2300 for (i = 0; i < n1; ++i)
2301 if (node1->child_pos[i] != node2->child_pos[i])
2302 break;
2304 node1 = isl_schedule_node_copy(node1);
2305 return isl_schedule_node_ancestor(node1, n1 - i);
2308 /* Print "node" to "p".
2310 __isl_give isl_printer *isl_printer_print_schedule_node(
2311 __isl_take isl_printer *p, __isl_keep isl_schedule_node *node)
2313 if (!node)
2314 return isl_printer_free(p);
2315 return isl_printer_print_schedule_tree_mark(p, node->schedule->root,
2316 isl_schedule_tree_list_n_schedule_tree(node->ancestors),
2317 node->child_pos);
2320 void isl_schedule_node_dump(__isl_keep isl_schedule_node *node)
2322 isl_ctx *ctx;
2323 isl_printer *printer;
2325 if (!node)
2326 return;
2328 ctx = isl_schedule_node_get_ctx(node);
2329 printer = isl_printer_to_file(ctx, stderr);
2330 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
2331 printer = isl_printer_print_schedule_node(printer, node);
2333 isl_printer_free(printer);