isl_range.c: has_sign: drop unused variable
[isl.git] / isl_schedule_node.c
blob38f65be650d3743916227862450fdadd3fe7609c
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_leaf:
324 case isl_schedule_node_sequence:
325 case isl_schedule_node_set:
326 return 0;
327 case isl_schedule_node_domain:
328 filter = isl_schedule_tree_domain_get_domain(tree);
329 filter = isl_union_set_universe(filter);
330 data->filter = filter;
331 break;
332 case isl_schedule_node_band:
333 if (isl_schedule_tree_band_n_member(tree) == 0)
334 return 0;
335 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
336 if (data->collect_prefix) {
337 isl_multi_union_pw_aff_free(data->prefix);
338 mupa = isl_multi_union_pw_aff_reset_tuple_id(mupa,
339 isl_dim_set);
340 data->prefix = isl_multi_union_pw_aff_copy(mupa);
342 filter = isl_multi_union_pw_aff_domain(mupa);
343 filter = isl_union_set_universe(filter);
344 data->filter = filter;
345 break;
346 case isl_schedule_node_filter:
347 filter = isl_schedule_tree_filter_get_filter(tree);
348 if (data->universe_filter)
349 filter = isl_union_set_universe(filter);
350 data->filter = filter;
351 break;
354 if ((data->collect_prefix && !data->prefix) || !data->filter)
355 return -1;
357 data->initialized = 1;
359 return 0;
362 /* Update "data" based on the tree node "tree" in case "data" has
363 * already been initialized.
365 * Return 0 on success and -1 on error.
367 * If "tree" is a filter, then we intersect data->filter with this filter
368 * (or its universe).
369 * If "tree" is a band with at least one member and data->collect_prefix
370 * is set, then we extend data->prefix with the band schedule.
372 static int collect_filter_prefix_update(__isl_keep isl_schedule_tree *tree,
373 struct isl_schedule_node_get_filter_prefix_data *data)
375 enum isl_schedule_node_type type;
376 isl_multi_union_pw_aff *mupa;
377 isl_union_set *filter;
379 type = isl_schedule_tree_get_type(tree);
380 switch (type) {
381 case isl_schedule_node_error:
382 return -1;
383 case isl_schedule_node_domain:
384 case isl_schedule_node_leaf:
385 case isl_schedule_node_sequence:
386 case isl_schedule_node_set:
387 break;
388 case isl_schedule_node_band:
389 if (isl_schedule_tree_band_n_member(tree) == 0)
390 break;
391 if (!data->collect_prefix)
392 break;
393 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
394 data->prefix = isl_multi_union_pw_aff_flat_range_product(mupa,
395 data->prefix);
396 if (!data->prefix)
397 return -1;
398 break;
399 case isl_schedule_node_filter:
400 filter = isl_schedule_tree_filter_get_filter(tree);
401 if (data->universe_filter)
402 filter = isl_union_set_universe(filter);
403 data->filter = isl_union_set_intersect(data->filter, filter);
404 if (!data->filter)
405 return -1;
406 break;
409 return 0;
412 /* Collect filter and/or prefix information from the elements
413 * in "list" (which represent the ancestors of a node).
414 * Store the results in "data".
416 * Return 0 on success and -1 on error.
418 * We traverse the list from innermost ancestor (last element)
419 * to outermost ancestor (first element), calling collect_filter_prefix_init
420 * on each node as long as we have not been able to extract any information
421 * yet and collect_filter_prefix_update afterwards.
422 * On successful return, data->initialized will be set since the outermost
423 * ancestor is a domain node, which always results in an initialization.
425 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
426 struct isl_schedule_node_get_filter_prefix_data *data)
428 int i, n;
430 data->initialized = 0;
431 data->filter = NULL;
433 if (!list)
434 return -1;
436 n = isl_schedule_tree_list_n_schedule_tree(list);
437 for (i = n - 1; i >= 0; --i) {
438 isl_schedule_tree *tree;
439 int r;
441 tree = isl_schedule_tree_list_get_schedule_tree(list, i);
442 if (!tree)
443 return -1;
444 if (!data->initialized)
445 r = collect_filter_prefix_init(tree, data);
446 else
447 r = collect_filter_prefix_update(tree, data);
448 isl_schedule_tree_free(tree);
449 if (r < 0)
450 return -1;
453 return 0;
456 /* Return the concatenation of the partial schedules of all outer band
457 * nodes of "node" interesected with all outer filters
458 * as an isl_union_pw_multi_aff.
460 * If "node" is pointing at the root of the schedule tree, then
461 * there are no domain elements reaching the current node, so
462 * we return an empty result.
464 * We collect all the filters and partial schedules in collect_filter_prefix.
465 * The partial schedules are collected as an isl_multi_union_pw_aff.
466 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
467 * contain any domain information, so we construct the isl_union_pw_multi_aff
468 * result as a zero-dimensional function on the collected filter.
469 * Otherwise, we convert the isl_multi_union_pw_aff to
470 * an isl_multi_union_pw_aff and intersect the domain with the filter.
472 __isl_give isl_union_pw_multi_aff *
473 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
474 __isl_keep isl_schedule_node *node)
476 isl_space *space;
477 isl_union_pw_multi_aff *prefix;
478 struct isl_schedule_node_get_filter_prefix_data data;
480 if (!node)
481 return NULL;
483 space = isl_schedule_get_space(node->schedule);
484 if (node->tree == node->schedule->root)
485 return isl_union_pw_multi_aff_empty(space);
487 space = isl_space_set_from_params(space);
488 data.universe_filter = 0;
489 data.collect_prefix = 1;
490 data.prefix = isl_multi_union_pw_aff_zero(space);
492 if (collect_filter_prefix(node->ancestors, &data) < 0)
493 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
495 if (data.prefix &&
496 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
497 isl_multi_union_pw_aff_free(data.prefix);
498 prefix = isl_union_pw_multi_aff_from_domain(data.filter);
499 } else {
500 prefix =
501 isl_union_pw_multi_aff_from_multi_union_pw_aff(data.prefix);
502 prefix = isl_union_pw_multi_aff_intersect_domain(prefix,
503 data.filter);
506 return prefix;
509 /* Return the concatenation of the partial schedules of all outer band
510 * nodes of "node" interesected with all outer filters
511 * as an isl_union_map.
513 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_union_map(
514 __isl_keep isl_schedule_node *node)
516 isl_union_pw_multi_aff *upma;
518 upma = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
519 return isl_union_map_from_union_pw_multi_aff(upma);
522 /* Return the union of universe sets of the domain elements that reach "node".
524 * If "node" is pointing at the root of the schedule tree, then
525 * there are no domain elements reaching the current node, so
526 * we return an empty result.
528 * Otherwise, we collect the universes of all filters reaching the node
529 * in collect_filter_prefix.
531 __isl_give isl_union_set *isl_schedule_node_get_universe_domain(
532 __isl_keep isl_schedule_node *node)
534 struct isl_schedule_node_get_filter_prefix_data data;
536 if (!node)
537 return NULL;
539 if (node->tree == node->schedule->root) {
540 isl_space *space;
542 space = isl_schedule_get_space(node->schedule);
543 return isl_union_set_empty(space);
546 data.universe_filter = 1;
547 data.collect_prefix = 0;
548 data.prefix = NULL;
550 if (collect_filter_prefix(node->ancestors, &data) < 0)
551 data.filter = isl_union_set_free(data.filter);
553 return data.filter;
556 /* Return the subtree schedule of "node".
558 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
559 * trees that do not contain any schedule information, we first
560 * move down to the first relevant descendant and handle leaves ourselves.
562 __isl_give isl_union_map *isl_schedule_node_get_subtree_schedule_union_map(
563 __isl_keep isl_schedule_node *node)
565 isl_schedule_tree *tree, *leaf;
566 isl_union_map *umap;
568 tree = isl_schedule_node_get_tree(node);
569 leaf = isl_schedule_node_peek_leaf(node);
570 tree = isl_schedule_tree_first_schedule_descendant(tree, leaf);
571 if (!tree)
572 return NULL;
573 if (tree == leaf) {
574 isl_union_set *domain;
575 domain = isl_schedule_node_get_universe_domain(node);
576 isl_schedule_tree_free(tree);
577 return isl_union_map_from_domain(domain);
580 umap = isl_schedule_tree_get_subtree_schedule_union_map(tree);
581 isl_schedule_tree_free(tree);
582 return umap;
585 /* Return the number of ancestors of "node" in its schedule tree.
587 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node *node)
589 if (!node)
590 return -1;
591 return isl_schedule_tree_list_n_schedule_tree(node->ancestors);
594 /* Does "node" have a parent?
596 * That is, does it point to any node of the schedule other than the root?
598 int isl_schedule_node_has_parent(__isl_keep isl_schedule_node *node)
600 if (!node)
601 return -1;
602 if (!node->ancestors)
603 return -1;
605 return isl_schedule_tree_list_n_schedule_tree(node->ancestors) != 0;
608 /* Return the position of "node" among the children of its parent.
610 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node *node)
612 int n;
613 int has_parent;
615 if (!node)
616 return -1;
617 has_parent = isl_schedule_node_has_parent(node);
618 if (has_parent < 0)
619 return -1;
620 if (!has_parent)
621 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
622 "node has no parent", return -1);
624 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
625 return node->child_pos[n - 1];
628 /* Does the parent (if any) of "node" have any children with a smaller child
629 * position than this one?
631 int isl_schedule_node_has_previous_sibling(__isl_keep isl_schedule_node *node)
633 int n;
634 int has_parent;
636 if (!node)
637 return -1;
638 has_parent = isl_schedule_node_has_parent(node);
639 if (has_parent < 0 || !has_parent)
640 return has_parent;
642 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
644 return node->child_pos[n - 1] > 0;
647 /* Does the parent (if any) of "node" have any children with a greater child
648 * position than this one?
650 int isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node *node)
652 int n, n_child;
653 int has_parent;
654 isl_schedule_tree *tree;
656 if (!node)
657 return -1;
658 has_parent = isl_schedule_node_has_parent(node);
659 if (has_parent < 0 || !has_parent)
660 return has_parent;
662 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
663 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n - 1);
664 if (!tree)
665 return -1;
666 n_child = isl_schedule_tree_list_n_schedule_tree(tree->children);
667 isl_schedule_tree_free(tree);
669 return node->child_pos[n - 1] + 1 < n_child;
672 /* Does "node" have any children?
674 * Any node other than the leaf nodes is considered to have at least
675 * one child, even if the corresponding isl_schedule_tree does not
676 * have any children.
678 int isl_schedule_node_has_children(__isl_keep isl_schedule_node *node)
680 if (!node)
681 return -1;
682 return !isl_schedule_tree_is_leaf(node->tree);
685 /* Return the number of children of "node"?
687 * Any node other than the leaf nodes is considered to have at least
688 * one child, even if the corresponding isl_schedule_tree does not
689 * have any children. That is, the number of children of "node" is
690 * only zero if its tree is the explicit empty tree. Otherwise,
691 * if the isl_schedule_tree has any children, then it is equal
692 * to the number of children of "node". If it has zero children,
693 * then "node" still has a leaf node as child.
695 int isl_schedule_node_n_children(__isl_keep isl_schedule_node *node)
697 int n;
699 if (!node)
700 return -1;
702 if (isl_schedule_tree_is_leaf(node->tree))
703 return 0;
705 n = isl_schedule_tree_n_children(node->tree);
706 if (n == 0)
707 return 1;
709 return n;
712 /* Move the "node" pointer to the ancestor of the given generation
713 * of the node it currently points to, where generation 0 is the node
714 * itself and generation 1 is its parent.
716 __isl_give isl_schedule_node *isl_schedule_node_ancestor(
717 __isl_take isl_schedule_node *node, int generation)
719 int n;
720 isl_schedule_tree *tree;
722 if (!node)
723 return NULL;
724 if (generation == 0)
725 return node;
726 n = isl_schedule_node_get_tree_depth(node);
727 if (n < 0)
728 return isl_schedule_node_free(node);
729 if (generation < 0 || generation > n)
730 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
731 "generation out of bounds",
732 return isl_schedule_node_free(node));
733 node = isl_schedule_node_cow(node);
734 if (!node)
735 return NULL;
737 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
738 n - generation);
739 isl_schedule_tree_free(node->tree);
740 node->tree = tree;
741 node->ancestors = isl_schedule_tree_list_drop(node->ancestors,
742 n - generation, generation);
743 if (!node->ancestors || !node->tree)
744 return isl_schedule_node_free(node);
746 return node;
749 /* Move the "node" pointer to the parent of the node it currently points to.
751 __isl_give isl_schedule_node *isl_schedule_node_parent(
752 __isl_take isl_schedule_node *node)
754 if (!node)
755 return NULL;
756 if (!isl_schedule_node_has_parent(node))
757 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
758 "node has no parent",
759 return isl_schedule_node_free(node));
760 return isl_schedule_node_ancestor(node, 1);
763 /* Move the "node" pointer to the root of its schedule tree.
765 __isl_give isl_schedule_node *isl_schedule_node_root(
766 __isl_take isl_schedule_node *node)
768 int n;
770 if (!node)
771 return NULL;
772 n = isl_schedule_node_get_tree_depth(node);
773 if (n < 0)
774 return isl_schedule_node_free(node);
775 return isl_schedule_node_ancestor(node, n);
778 /* Move the "node" pointer to the child at position "pos" of the node
779 * it currently points to.
781 __isl_give isl_schedule_node *isl_schedule_node_child(
782 __isl_take isl_schedule_node *node, int pos)
784 int n;
785 isl_ctx *ctx;
786 isl_schedule_tree *tree;
787 int *child_pos;
789 node = isl_schedule_node_cow(node);
790 if (!node)
791 return NULL;
792 if (!isl_schedule_node_has_children(node))
793 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
794 "node has no children",
795 return isl_schedule_node_free(node));
797 ctx = isl_schedule_node_get_ctx(node);
798 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
799 child_pos = isl_realloc_array(ctx, node->child_pos, int, n + 1);
800 if (!child_pos)
801 return isl_schedule_node_free(node);
802 node->child_pos = child_pos;
803 node->child_pos[n] = pos;
805 node->ancestors = isl_schedule_tree_list_add(node->ancestors,
806 isl_schedule_tree_copy(node->tree));
807 tree = node->tree;
808 if (isl_schedule_tree_has_children(tree))
809 tree = isl_schedule_tree_get_child(tree, pos);
810 else
811 tree = isl_schedule_node_get_leaf(node);
812 isl_schedule_tree_free(node->tree);
813 node->tree = tree;
815 if (!node->tree || !node->ancestors)
816 return isl_schedule_node_free(node);
818 return node;
821 /* Move the "node" pointer to the first child of the node
822 * it currently points to.
824 __isl_give isl_schedule_node *isl_schedule_node_first_child(
825 __isl_take isl_schedule_node *node)
827 return isl_schedule_node_child(node, 0);
830 /* Move the "node" pointer to the child of this node's parent in
831 * the previous child position.
833 __isl_give isl_schedule_node *isl_schedule_node_previous_sibling(
834 __isl_take isl_schedule_node *node)
836 int n;
837 isl_schedule_tree *parent, *tree;
839 node = isl_schedule_node_cow(node);
840 if (!node)
841 return NULL;
842 if (!isl_schedule_node_has_previous_sibling(node))
843 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
844 "node has no previous sibling",
845 return isl_schedule_node_free(node));
847 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
848 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
849 n - 1);
850 if (!parent)
851 return isl_schedule_node_free(node);
852 node->child_pos[n - 1]--;
853 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
854 node->child_pos[n - 1]);
855 isl_schedule_tree_free(parent);
856 if (!tree)
857 return isl_schedule_node_free(node);
858 isl_schedule_tree_free(node->tree);
859 node->tree = tree;
861 return node;
864 /* Move the "node" pointer to the child of this node's parent in
865 * the next child position.
867 __isl_give isl_schedule_node *isl_schedule_node_next_sibling(
868 __isl_take isl_schedule_node *node)
870 int n;
871 isl_schedule_tree *parent, *tree;
873 node = isl_schedule_node_cow(node);
874 if (!node)
875 return NULL;
876 if (!isl_schedule_node_has_next_sibling(node))
877 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
878 "node has no next sibling",
879 return isl_schedule_node_free(node));
881 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
882 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
883 n - 1);
884 if (!parent)
885 return isl_schedule_node_free(node);
886 node->child_pos[n - 1]++;
887 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
888 node->child_pos[n - 1]);
889 isl_schedule_tree_free(parent);
890 if (!tree)
891 return isl_schedule_node_free(node);
892 isl_schedule_tree_free(node->tree);
893 node->tree = tree;
895 return node;
898 /* Return a copy to the child at position "pos" of "node".
900 __isl_give isl_schedule_node *isl_schedule_node_get_child(
901 __isl_keep isl_schedule_node *node, int pos)
903 return isl_schedule_node_child(isl_schedule_node_copy(node), pos);
906 /* Traverse the descendant of "node" in depth-first order, including
907 * "node" itself. Call "enter" whenever a node is entered and "leave"
908 * whenever a node is left. The callback "enter" is responsible
909 * for moving to the deepest initial subtree of its argument that
910 * should be traversed.
912 static __isl_give isl_schedule_node *traverse(
913 __isl_take isl_schedule_node *node,
914 __isl_give isl_schedule_node *(*enter)(
915 __isl_take isl_schedule_node *node, void *user),
916 __isl_give isl_schedule_node *(*leave)(
917 __isl_take isl_schedule_node *node, void *user),
918 void *user)
920 int depth;
922 if (!node)
923 return NULL;
925 depth = isl_schedule_node_get_tree_depth(node);
926 do {
927 node = enter(node, user);
928 node = leave(node, user);
929 while (node && isl_schedule_node_get_tree_depth(node) > depth &&
930 !isl_schedule_node_has_next_sibling(node)) {
931 node = isl_schedule_node_parent(node);
932 node = leave(node, user);
934 if (node && isl_schedule_node_get_tree_depth(node) > depth)
935 node = isl_schedule_node_next_sibling(node);
936 } while (node && isl_schedule_node_get_tree_depth(node) > depth);
938 return node;
941 /* Internal data structure for isl_schedule_node_foreach_descendant.
943 * "fn" is the user-specified callback function.
944 * "user" is the user-specified argument for the callback.
946 struct isl_schedule_node_preorder_data {
947 int (*fn)(__isl_keep isl_schedule_node *node, void *user);
948 void *user;
951 /* Callback for "traverse" to enter a node and to move
952 * to the deepest initial subtree that should be traversed
953 * for use in a preorder visit.
955 * If the user callback returns a negative value, then we abort
956 * the traversal. If this callback returns zero, then we skip
957 * the subtree rooted at the current node. Otherwise, we move
958 * down to the first child and repeat the process until a leaf
959 * is reached.
961 static __isl_give isl_schedule_node *preorder_enter(
962 __isl_take isl_schedule_node *node, void *user)
964 struct isl_schedule_node_preorder_data *data = user;
966 if (!node)
967 return NULL;
969 do {
970 int r;
972 r = data->fn(node, data->user);
973 if (r < 0)
974 return isl_schedule_node_free(node);
975 if (r == 0)
976 return node;
977 } while (isl_schedule_node_has_children(node) &&
978 (node = isl_schedule_node_first_child(node)) != NULL);
980 return node;
983 /* Callback for "traverse" to leave a node
984 * for use in a preorder visit.
985 * Since we already visited the node when we entered it,
986 * we do not need to do anything here.
988 static __isl_give isl_schedule_node *preorder_leave(
989 __isl_take isl_schedule_node *node, void *user)
991 return node;
994 /* Traverse the descendants of "node" (including the node itself)
995 * in depth first preorder.
997 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
998 * If "fn" returns 0 on any of the nodes, then the subtree rooted
999 * at that node is skipped.
1001 * Return 0 on success and -1 on failure.
1003 int isl_schedule_node_foreach_descendant(__isl_keep isl_schedule_node *node,
1004 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
1006 struct isl_schedule_node_preorder_data data = { fn, user };
1008 node = isl_schedule_node_copy(node);
1009 node = traverse(node, &preorder_enter, &preorder_leave, &data);
1010 isl_schedule_node_free(node);
1012 return node ? 0 : -1;
1015 /* Internal data structure for isl_schedule_node_map_descendant.
1017 * "fn" is the user-specified callback function.
1018 * "user" is the user-specified argument for the callback.
1020 struct isl_schedule_node_postorder_data {
1021 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1022 void *user);
1023 void *user;
1026 /* Callback for "traverse" to enter a node and to move
1027 * to the deepest initial subtree that should be traversed
1028 * for use in a postorder visit.
1030 * Since we are performing a postorder visit, we only need
1031 * to move to the deepest initial leaf here.
1033 static __isl_give isl_schedule_node *postorder_enter(
1034 __isl_take isl_schedule_node *node, void *user)
1036 while (node && isl_schedule_node_has_children(node))
1037 node = isl_schedule_node_first_child(node);
1039 return node;
1042 /* Callback for "traverse" to leave a node
1043 * for use in a postorder visit.
1045 * Since we are performing a postorder visit, we need
1046 * to call the user callback here.
1048 static __isl_give isl_schedule_node *postorder_leave(
1049 __isl_take isl_schedule_node *node, void *user)
1051 struct isl_schedule_node_postorder_data *data = user;
1053 return data->fn(node, data->user);
1056 /* Traverse the descendants of "node" (including the node itself)
1057 * in depth first postorder, allowing the user to modify the visited node.
1058 * The traversal continues from the node returned by the callback function.
1059 * It is the responsibility of the user to ensure that this does not
1060 * lead to an infinite loop. It is safest to always return a pointer
1061 * to the same position (same ancestors and child positions) as the input node.
1063 __isl_give isl_schedule_node *isl_schedule_node_map_descendant(
1064 __isl_take isl_schedule_node *node,
1065 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1066 void *user), void *user)
1068 struct isl_schedule_node_postorder_data data = { fn, user };
1070 return traverse(node, &postorder_enter, &postorder_leave, &data);
1073 /* Traverse the ancestors of "node" from the root down to and including
1074 * the parent of "node", calling "fn" on each of them.
1076 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1078 * Return 0 on success and -1 on failure.
1080 int isl_schedule_node_foreach_ancestor_top_down(
1081 __isl_keep isl_schedule_node *node,
1082 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
1084 int i, n;
1086 if (!node)
1087 return -1;
1089 n = isl_schedule_node_get_tree_depth(node);
1090 for (i = 0; i < n; ++i) {
1091 isl_schedule_node *ancestor;
1092 int r;
1094 ancestor = isl_schedule_node_copy(node);
1095 ancestor = isl_schedule_node_ancestor(ancestor, n - i);
1096 r = fn(ancestor, user);
1097 isl_schedule_node_free(ancestor);
1098 if (r < 0)
1099 return -1;
1102 return 0;
1105 /* Return the number of members in the given band node.
1107 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node *node)
1109 return node ? isl_schedule_tree_band_n_member(node->tree) : 0;
1112 /* Is the band member at position "pos" of the band node "node"
1113 * marked coincident?
1115 int isl_schedule_node_band_member_get_coincident(
1116 __isl_keep isl_schedule_node *node, int pos)
1118 if (!node)
1119 return -1;
1120 return isl_schedule_tree_band_member_get_coincident(node->tree, pos);
1123 /* Mark the band member at position "pos" the band node "node"
1124 * as being coincident or not according to "coincident".
1126 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_coincident(
1127 __isl_take isl_schedule_node *node, int pos, int coincident)
1129 int c;
1130 isl_schedule_tree *tree;
1132 if (!node)
1133 return NULL;
1134 c = isl_schedule_node_band_member_get_coincident(node, pos);
1135 if (c == coincident)
1136 return node;
1138 tree = isl_schedule_tree_copy(node->tree);
1139 tree = isl_schedule_tree_band_member_set_coincident(tree, pos,
1140 coincident);
1141 node = isl_schedule_node_graft_tree(node, tree);
1143 return node;
1146 /* Is the band node "node" marked permutable?
1148 int isl_schedule_node_band_get_permutable(__isl_keep isl_schedule_node *node)
1150 if (!node)
1151 return -1;
1153 return isl_schedule_tree_band_get_permutable(node->tree);
1156 /* Mark the band node "node" permutable or not according to "permutable"?
1158 __isl_give isl_schedule_node *isl_schedule_node_band_set_permutable(
1159 __isl_take isl_schedule_node *node, int permutable)
1161 isl_schedule_tree *tree;
1163 if (!node)
1164 return NULL;
1165 if (isl_schedule_node_band_get_permutable(node) == permutable)
1166 return node;
1168 tree = isl_schedule_tree_copy(node->tree);
1169 tree = isl_schedule_tree_band_set_permutable(tree, permutable);
1170 node = isl_schedule_node_graft_tree(node, tree);
1172 return node;
1175 /* Return the schedule space of the band node.
1177 __isl_give isl_space *isl_schedule_node_band_get_space(
1178 __isl_keep isl_schedule_node *node)
1180 if (!node)
1181 return NULL;
1183 return isl_schedule_tree_band_get_space(node->tree);
1186 /* Return the schedule of the band node in isolation.
1188 __isl_give isl_multi_union_pw_aff *isl_schedule_node_band_get_partial_schedule(
1189 __isl_keep isl_schedule_node *node)
1191 if (!node)
1192 return NULL;
1194 return isl_schedule_tree_band_get_partial_schedule(node->tree);
1197 /* Return the schedule of the band node in isolation in the form of
1198 * an isl_union_map.
1200 * If the band does not have any members, then we construct a universe map
1201 * with the universe of the domain elements reaching the node as domain.
1202 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1203 * convert that to an isl_union_map.
1205 __isl_give isl_union_map *isl_schedule_node_band_get_partial_schedule_union_map(
1206 __isl_keep isl_schedule_node *node)
1208 isl_multi_union_pw_aff *mupa;
1210 if (!node)
1211 return NULL;
1213 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
1214 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1215 "not a band node", return NULL);
1216 if (isl_schedule_node_band_n_member(node) == 0) {
1217 isl_union_set *domain;
1219 domain = isl_schedule_node_get_universe_domain(node);
1220 return isl_union_map_from_domain(domain);
1223 mupa = isl_schedule_node_band_get_partial_schedule(node);
1224 return isl_union_map_from_multi_union_pw_aff(mupa);
1227 /* Make sure that that spaces of "node" and "mv" are the same.
1228 * Return -1 on error, reporting the error to the user.
1230 static int check_space_multi_val(__isl_keep isl_schedule_node *node,
1231 __isl_keep isl_multi_val *mv)
1233 isl_space *node_space, *mv_space;
1234 int equal;
1236 node_space = isl_schedule_node_band_get_space(node);
1237 mv_space = isl_multi_val_get_space(mv);
1238 equal = isl_space_tuple_is_equal(node_space, isl_dim_set,
1239 mv_space, isl_dim_set);
1240 isl_space_free(mv_space);
1241 isl_space_free(node_space);
1242 if (equal < 0)
1243 return -1;
1244 if (!equal)
1245 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1246 "spaces don't match", return -1);
1248 return 0;
1251 /* Multiply the partial schedule of the band node "node"
1252 * with the factors in "mv".
1254 __isl_give isl_schedule_node *isl_schedule_node_band_scale(
1255 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1257 isl_schedule_tree *tree;
1259 if (!node || !mv)
1260 goto error;
1261 if (check_space_multi_val(node, mv) < 0)
1262 goto error;
1264 tree = isl_schedule_node_get_tree(node);
1265 tree = isl_schedule_tree_band_scale(tree, mv);
1266 return isl_schedule_node_graft_tree(node, tree);
1267 error:
1268 isl_multi_val_free(mv);
1269 isl_schedule_node_free(node);
1270 return NULL;
1273 /* Divide the partial schedule of the band node "node"
1274 * by the factors in "mv".
1276 __isl_give isl_schedule_node *isl_schedule_node_band_scale_down(
1277 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1279 isl_schedule_tree *tree;
1281 if (!node || !mv)
1282 goto error;
1283 if (check_space_multi_val(node, mv) < 0)
1284 goto error;
1286 tree = isl_schedule_node_get_tree(node);
1287 tree = isl_schedule_tree_band_scale_down(tree, mv);
1288 return isl_schedule_node_graft_tree(node, tree);
1289 error:
1290 isl_multi_val_free(mv);
1291 isl_schedule_node_free(node);
1292 return NULL;
1295 /* Tile "node" with tile sizes "sizes".
1297 * The current node is replaced by two nested nodes corresponding
1298 * to the tile dimensions and the point dimensions.
1300 * Return a pointer to the outer (tile) node.
1302 * If the scale tile loops option is set, then the tile loops
1303 * are scaled by the tile sizes. If the shift point loops option is set,
1304 * then the point loops are shifted to start at zero.
1305 * In particular, these options affect the tile and point loop schedules
1306 * as follows
1308 * scale shift original tile point
1310 * 0 0 i floor(i/s) i
1311 * 1 0 i s * floor(i/s) i
1312 * 0 1 i floor(i/s) i - s * floor(i/s)
1313 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1315 __isl_give isl_schedule_node *isl_schedule_node_band_tile(
1316 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
1318 isl_schedule_tree *tree;
1320 if (!node || !sizes)
1321 goto error;
1323 if (check_space_multi_val(node, sizes) < 0)
1324 goto error;
1326 tree = isl_schedule_node_get_tree(node);
1327 tree = isl_schedule_tree_band_tile(tree, sizes);
1328 return isl_schedule_node_graft_tree(node, tree);
1329 error:
1330 isl_multi_val_free(sizes);
1331 isl_schedule_node_free(node);
1332 return NULL;
1335 /* Move the band node "node" down to all the leaves in the subtree
1336 * rooted at "node".
1337 * Return a pointer to the node in the resulting tree that is in the same
1338 * position as the node pointed to by "node" in the original tree.
1340 * If the node only has a leaf child, then nothing needs to be done.
1341 * Otherwise, the child of the node is removed and the result is
1342 * appended to all the leaves in the subtree rooted at the original child.
1343 * The original node is then replaced by the result of this operation.
1345 __isl_give isl_schedule_node *isl_schedule_node_band_sink(
1346 __isl_take isl_schedule_node *node)
1348 enum isl_schedule_node_type type;
1349 isl_schedule_tree *tree, *child;
1351 if (!node)
1352 return NULL;
1354 type = isl_schedule_node_get_type(node);
1355 if (type != isl_schedule_node_band)
1356 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1357 "not a band node", isl_schedule_node_free(node));
1358 if (isl_schedule_tree_n_children(node->tree) == 0)
1359 return node;
1361 tree = isl_schedule_node_get_tree(node);
1362 child = isl_schedule_tree_get_child(tree, 0);
1363 tree = isl_schedule_tree_reset_children(tree);
1364 tree = isl_schedule_tree_append_to_leaves(child, tree);
1366 return isl_schedule_node_graft_tree(node, tree);
1369 /* Split "node" into two nested band nodes, one with the first "pos"
1370 * dimensions and one with the remaining dimensions.
1371 * The schedules of the two band nodes live in anonymous spaces.
1373 __isl_give isl_schedule_node *isl_schedule_node_band_split(
1374 __isl_take isl_schedule_node *node, int pos)
1376 isl_schedule_tree *tree;
1378 tree = isl_schedule_node_get_tree(node);
1379 tree = isl_schedule_tree_band_split(tree, pos);
1380 return isl_schedule_node_graft_tree(node, tree);
1383 /* Return the domain of the domain node "node".
1385 __isl_give isl_union_set *isl_schedule_node_domain_get_domain(
1386 __isl_keep isl_schedule_node *node)
1388 if (!node)
1389 return NULL;
1391 return isl_schedule_tree_domain_get_domain(node->tree);
1394 /* Return the filter of the filter node "node".
1396 __isl_give isl_union_set *isl_schedule_node_filter_get_filter(
1397 __isl_keep isl_schedule_node *node)
1399 if (!node)
1400 return NULL;
1402 return isl_schedule_tree_filter_get_filter(node->tree);
1405 /* Replace the filter of filter node "node" by "filter".
1407 __isl_give isl_schedule_node *isl_schedule_node_filter_set_filter(
1408 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
1410 isl_schedule_tree *tree;
1412 if (!node || !filter)
1413 goto error;
1415 tree = isl_schedule_tree_copy(node->tree);
1416 tree = isl_schedule_tree_filter_set_filter(tree, filter);
1417 return isl_schedule_node_graft_tree(node, tree);
1418 error:
1419 isl_schedule_node_free(node);
1420 isl_union_set_free(filter);
1421 return NULL;
1424 /* Update the ancestors of "node" to point to the tree that "node"
1425 * now points to.
1426 * That is, replace the child in the original parent that corresponds
1427 * to the current tree position by node->tree and continue updating
1428 * the ancestors in the same way until the root is reached.
1430 * If "node" originally points to a leaf of the schedule tree, then make sure
1431 * that in the end it points to a leaf in the updated schedule tree.
1433 static __isl_give isl_schedule_node *update_ancestors(
1434 __isl_take isl_schedule_node *node)
1436 int i, n;
1437 int is_leaf;
1438 isl_ctx *ctx;
1439 isl_schedule_tree *tree;
1441 node = isl_schedule_node_cow(node);
1442 if (!node)
1443 return NULL;
1445 ctx = isl_schedule_node_get_ctx(node);
1446 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1447 tree = isl_schedule_tree_copy(node->tree);
1449 for (i = n - 1; i >= 0; --i) {
1450 isl_schedule_tree *parent;
1452 parent = isl_schedule_tree_list_get_schedule_tree(
1453 node->ancestors, i);
1454 parent = isl_schedule_tree_replace_child(parent,
1455 node->child_pos[i], tree);
1456 node->ancestors = isl_schedule_tree_list_set_schedule_tree(
1457 node->ancestors, i, isl_schedule_tree_copy(parent));
1459 tree = parent;
1462 is_leaf = isl_schedule_tree_is_leaf(node->tree);
1463 node->schedule = isl_schedule_set_root(node->schedule, tree);
1464 if (is_leaf) {
1465 isl_schedule_tree_free(node->tree);
1466 node->tree = isl_schedule_node_get_leaf(node);
1469 if (!node->schedule || !node->ancestors)
1470 return isl_schedule_node_free(node);
1472 return node;
1475 /* Replace the subtree that "pos" points to by "tree", updating
1476 * the ancestors to maintain a consistent state.
1478 __isl_give isl_schedule_node *isl_schedule_node_graft_tree(
1479 __isl_take isl_schedule_node *pos, __isl_take isl_schedule_tree *tree)
1481 if (!tree || !pos)
1482 goto error;
1483 if (pos->tree == tree) {
1484 isl_schedule_tree_free(tree);
1485 return pos;
1488 pos = isl_schedule_node_cow(pos);
1489 if (!pos)
1490 goto error;
1492 isl_schedule_tree_free(pos->tree);
1493 pos->tree = tree;
1495 return update_ancestors(pos);
1496 error:
1497 isl_schedule_node_free(pos);
1498 isl_schedule_tree_free(tree);
1499 return NULL;
1502 /* Make sure we can insert a node between "node" and its parent.
1503 * Return -1 on error, reporting the reason why we cannot insert a node.
1505 static int check_insert(__isl_keep isl_schedule_node *node)
1507 int has_parent;
1508 enum isl_schedule_node_type type;
1510 has_parent = isl_schedule_node_has_parent(node);
1511 if (has_parent < 0)
1512 return -1;
1513 if (!has_parent)
1514 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1515 "cannot insert node outside of root", return -1);
1517 type = isl_schedule_node_get_parent_type(node);
1518 if (type == isl_schedule_node_error)
1519 return -1;
1520 if (type == isl_schedule_node_set || type == isl_schedule_node_sequence)
1521 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1522 "cannot insert node between set or sequence node "
1523 "and its filter children", return -1);
1525 return 0;
1528 /* Insert a band node with partial schedule "mupa" between "node" and
1529 * its parent.
1530 * Return a pointer to the new band node.
1532 __isl_give isl_schedule_node *isl_schedule_node_insert_partial_schedule(
1533 __isl_take isl_schedule_node *node,
1534 __isl_take isl_multi_union_pw_aff *mupa)
1536 isl_schedule_band *band;
1537 isl_schedule_tree *tree;
1539 if (check_insert(node) < 0)
1540 node = isl_schedule_node_free(node);
1542 tree = isl_schedule_node_get_tree(node);
1543 band = isl_schedule_band_from_multi_union_pw_aff(mupa);
1544 tree = isl_schedule_tree_insert_band(tree, band);
1545 node = isl_schedule_node_graft_tree(node, tree);
1547 return node;
1550 /* Insert a filter node with filter "filter" between "node" and its parent.
1551 * Return a pointer to the new filter node.
1553 __isl_give isl_schedule_node *isl_schedule_node_insert_filter(
1554 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
1556 isl_schedule_tree *tree;
1558 if (check_insert(node) < 0)
1559 node = isl_schedule_node_free(node);
1561 tree = isl_schedule_node_get_tree(node);
1562 tree = isl_schedule_tree_insert_filter(tree, filter);
1563 node = isl_schedule_node_graft_tree(node, tree);
1565 return node;
1568 /* Attach the current subtree of "node" to a sequence of filter tree nodes
1569 * with filters described by "filters", attach this sequence
1570 * of filter tree nodes as children to a new tree of type "type" and
1571 * replace the original subtree of "node" by this new tree.
1573 static __isl_give isl_schedule_node *isl_schedule_node_insert_children(
1574 __isl_take isl_schedule_node *node,
1575 enum isl_schedule_node_type type,
1576 __isl_take isl_union_set_list *filters)
1578 int i, n;
1579 isl_ctx *ctx;
1580 isl_schedule_tree *tree;
1581 isl_schedule_tree_list *list;
1583 if (check_insert(node) < 0)
1584 node = isl_schedule_node_free(node);
1586 if (!node || !filters)
1587 goto error;
1589 ctx = isl_schedule_node_get_ctx(node);
1590 n = isl_union_set_list_n_union_set(filters);
1591 list = isl_schedule_tree_list_alloc(ctx, n);
1592 for (i = 0; i < n; ++i) {
1593 isl_schedule_tree *tree;
1594 isl_union_set *filter;
1596 tree = isl_schedule_node_get_tree(node);
1597 filter = isl_union_set_list_get_union_set(filters, i);
1598 tree = isl_schedule_tree_insert_filter(tree, filter);
1599 list = isl_schedule_tree_list_add(list, tree);
1601 tree = isl_schedule_tree_from_children(type, list);
1602 node = isl_schedule_node_graft_tree(node, tree);
1604 isl_union_set_list_free(filters);
1605 return node;
1606 error:
1607 isl_union_set_list_free(filters);
1608 isl_schedule_node_free(node);
1609 return NULL;
1612 /* Insert a sequence node with child filters "filters" between "node" and
1613 * its parent. That is, the tree that "node" points to is attached
1614 * to each of the child nodes of the filter nodes.
1615 * Return a pointer to the new sequence node.
1617 __isl_give isl_schedule_node *isl_schedule_node_insert_sequence(
1618 __isl_take isl_schedule_node *node,
1619 __isl_take isl_union_set_list *filters)
1621 return isl_schedule_node_insert_children(node,
1622 isl_schedule_node_sequence, filters);
1625 /* Insert a set node with child filters "filters" between "node" and
1626 * its parent. That is, the tree that "node" points to is attached
1627 * to each of the child nodes of the filter nodes.
1628 * Return a pointer to the new set node.
1630 __isl_give isl_schedule_node *isl_schedule_node_insert_set(
1631 __isl_take isl_schedule_node *node,
1632 __isl_take isl_union_set_list *filters)
1634 return isl_schedule_node_insert_children(node,
1635 isl_schedule_node_set, filters);
1638 /* Remove "node" from its schedule tree and return a pointer
1639 * to the leaf at the same position in the updated schedule tree.
1641 * It is not allowed to remove the root of a schedule tree or
1642 * a child of a set or sequence node.
1644 __isl_give isl_schedule_node *isl_schedule_node_cut(
1645 __isl_take isl_schedule_node *node)
1647 isl_schedule_tree *leaf;
1648 enum isl_schedule_node_type parent_type;
1650 if (!node)
1651 return NULL;
1652 if (!isl_schedule_node_has_parent(node))
1653 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1654 "cannot cut root", return isl_schedule_node_free(node));
1656 parent_type = isl_schedule_node_get_parent_type(node);
1657 if (parent_type == isl_schedule_node_set ||
1658 parent_type == isl_schedule_node_sequence)
1659 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1660 "cannot cut child of set or sequence",
1661 return isl_schedule_node_free(node));
1663 leaf = isl_schedule_node_get_leaf(node);
1664 return isl_schedule_node_graft_tree(node, leaf);
1667 /* Remove a single node from the schedule tree, attaching the child
1668 * of "node" directly to its parent.
1669 * Return a pointer to this former child or to the leaf the position
1670 * of the original node if there was no child.
1671 * It is not allowed to remove the root of a schedule tree,
1672 * a set or sequence node or a child of a set or sequence node.
1674 __isl_give isl_schedule_node *isl_schedule_node_delete(
1675 __isl_take isl_schedule_node *node)
1677 int n;
1678 isl_schedule_tree *tree;
1679 enum isl_schedule_node_type type;
1681 if (!node)
1682 return NULL;
1684 if (isl_schedule_node_get_tree_depth(node) == 0)
1685 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1686 "cannot delete root node",
1687 return isl_schedule_node_free(node));
1688 n = isl_schedule_node_n_children(node);
1689 if (n != 1)
1690 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1691 "can only delete node with a single child",
1692 return isl_schedule_node_free(node));
1693 type = isl_schedule_node_get_parent_type(node);
1694 if (type == isl_schedule_node_sequence || type == isl_schedule_node_set)
1695 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1696 "cannot delete child of set or sequence",
1697 return isl_schedule_node_free(node));
1699 tree = isl_schedule_node_get_tree(node);
1700 if (!tree || isl_schedule_tree_has_children(tree)) {
1701 tree = isl_schedule_tree_child(tree, 0);
1702 } else {
1703 isl_schedule_tree_free(tree);
1704 tree = isl_schedule_node_get_leaf(node);
1706 node = isl_schedule_node_graft_tree(node, tree);
1708 return node;
1711 /* Compute the gist of the given band node with respect to "context".
1713 __isl_give isl_schedule_node *isl_schedule_node_band_gist(
1714 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
1716 isl_schedule_tree *tree;
1718 tree = isl_schedule_node_get_tree(node);
1719 tree = isl_schedule_tree_band_gist(tree, context);
1720 return isl_schedule_node_graft_tree(node, tree);
1723 /* Internal data structure for isl_schedule_node_gist.
1724 * "filters" contains an element for each outer filter node
1725 * with respect to the current position, each representing
1726 * the intersection of the previous element and the filter on the filter node.
1727 * The first element in the original context passed to isl_schedule_node_gist.
1729 struct isl_node_gist_data {
1730 isl_union_set_list *filters;
1733 /* Can we finish gisting at this node?
1734 * That is, is the filter on the current filter node a subset of
1735 * the original context passed to isl_schedule_node_gist?
1737 static int gist_done(__isl_keep isl_schedule_node *node,
1738 struct isl_node_gist_data *data)
1740 isl_union_set *filter, *outer;
1741 int subset;
1743 filter = isl_schedule_node_filter_get_filter(node);
1744 outer = isl_union_set_list_get_union_set(data->filters, 0);
1745 subset = isl_union_set_is_subset(filter, outer);
1746 isl_union_set_free(outer);
1747 isl_union_set_free(filter);
1749 return subset;
1752 /* Callback for "traverse" to enter a node and to move
1753 * to the deepest initial subtree that should be traversed
1754 * by isl_schedule_node_gist.
1756 * The "filters" list is extended by one element each time
1757 * we come across a filter node by the result of intersecting
1758 * the last element in the list with the filter on the filter node.
1760 * If the filter on the current filter node is a subset of
1761 * the original context passed to isl_schedule_node_gist,
1762 * then there is no need to go into its subtree since it cannot
1763 * be further simplified by the context. The "filters" list is
1764 * still extended for consistency, but the actual value of the
1765 * added element is immaterial since it will not be used.
1767 * Otherwise, the filter on the current filter node is replaced by
1768 * the gist of the original filter with respect to the intersection
1769 * of the original context with the intermediate filters.
1771 * If the new element in the "filters" list is empty, then no elements
1772 * can reach the descendants of the current filter node. The subtree
1773 * underneath the filter node is therefore removed.
1775 static __isl_give isl_schedule_node *gist_enter(
1776 __isl_take isl_schedule_node *node, void *user)
1778 struct isl_node_gist_data *data = user;
1780 do {
1781 isl_union_set *filter, *inner;
1782 int done, empty;
1783 int n;
1785 switch (isl_schedule_node_get_type(node)) {
1786 case isl_schedule_node_error:
1787 return isl_schedule_node_free(node);
1788 case isl_schedule_node_band:
1789 case isl_schedule_node_domain:
1790 case isl_schedule_node_leaf:
1791 case isl_schedule_node_sequence:
1792 case isl_schedule_node_set:
1793 continue;
1794 case isl_schedule_node_filter:
1795 break;
1797 done = gist_done(node, data);
1798 filter = isl_schedule_node_filter_get_filter(node);
1799 if (done < 0 || done) {
1800 data->filters = isl_union_set_list_add(data->filters,
1801 filter);
1802 if (done < 0)
1803 return isl_schedule_node_free(node);
1804 return node;
1806 n = isl_union_set_list_n_union_set(data->filters);
1807 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
1808 filter = isl_union_set_gist(filter, isl_union_set_copy(inner));
1809 node = isl_schedule_node_filter_set_filter(node,
1810 isl_union_set_copy(filter));
1811 filter = isl_union_set_intersect(filter, inner);
1812 empty = isl_union_set_is_empty(filter);
1813 data->filters = isl_union_set_list_add(data->filters, filter);
1814 if (empty < 0)
1815 return isl_schedule_node_free(node);
1816 if (!empty)
1817 continue;
1818 node = isl_schedule_node_child(node, 0);
1819 node = isl_schedule_node_cut(node);
1820 node = isl_schedule_node_parent(node);
1821 return node;
1822 } while (isl_schedule_node_has_children(node) &&
1823 (node = isl_schedule_node_first_child(node)) != NULL);
1825 return node;
1828 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
1830 * In particular, if the current node is a filter node, then we remove
1831 * the element on the "filters" list that was added when we entered
1832 * the node. There is no need to compute any gist here, since we
1833 * already did that when we entered the node.
1835 * If the current node is a band node, then we compute the gist of
1836 * the band node with respect to the intersection of the original context
1837 * and the intermediate filters.
1839 * If the current node is a sequence or set node, then some of
1840 * the filter children may have become empty and so they are removed.
1841 * If only one child is left, then the set or sequence node along with
1842 * the single remaining child filter is removed. The filter can be
1843 * removed because the filters on a sequence or set node are supposed
1844 * to partition the incoming domain instances.
1845 * In principle, it should then be impossible for there to be zero
1846 * remaining children, but should this happen, we replace the entire
1847 * subtree with an empty filter.
1849 static __isl_give isl_schedule_node *gist_leave(
1850 __isl_take isl_schedule_node *node, void *user)
1852 struct isl_node_gist_data *data = user;
1853 isl_schedule_tree *tree;
1854 int i, n;
1855 isl_union_set *filter;
1857 switch (isl_schedule_node_get_type(node)) {
1858 case isl_schedule_node_error:
1859 return isl_schedule_node_free(node);
1860 case isl_schedule_node_filter:
1861 n = isl_union_set_list_n_union_set(data->filters);
1862 data->filters = isl_union_set_list_drop(data->filters,
1863 n - 1, 1);
1864 break;
1865 case isl_schedule_node_band:
1866 n = isl_union_set_list_n_union_set(data->filters);
1867 filter = isl_union_set_list_get_union_set(data->filters, n - 1);
1868 node = isl_schedule_node_band_gist(node, filter);
1869 break;
1870 case isl_schedule_node_set:
1871 case isl_schedule_node_sequence:
1872 tree = isl_schedule_node_get_tree(node);
1873 n = isl_schedule_tree_n_children(tree);
1874 for (i = n - 1; i >= 0; --i) {
1875 isl_schedule_tree *child;
1876 isl_union_set *filter;
1877 int empty;
1879 child = isl_schedule_tree_get_child(tree, i);
1880 filter = isl_schedule_tree_filter_get_filter(child);
1881 empty = isl_union_set_is_empty(filter);
1882 isl_union_set_free(filter);
1883 isl_schedule_tree_free(child);
1884 if (empty < 0)
1885 tree = isl_schedule_tree_free(tree);
1886 else if (empty)
1887 tree = isl_schedule_tree_drop_child(tree, i);
1889 n = isl_schedule_tree_n_children(tree);
1890 node = isl_schedule_node_graft_tree(node, tree);
1891 if (n == 1) {
1892 node = isl_schedule_node_delete(node);
1893 node = isl_schedule_node_delete(node);
1894 } else if (n == 0) {
1895 isl_space *space;
1897 filter =
1898 isl_union_set_list_get_union_set(data->filters, 0);
1899 space = isl_union_set_get_space(filter);
1900 isl_union_set_free(filter);
1901 filter = isl_union_set_empty(space);
1902 node = isl_schedule_node_cut(node);
1903 node = isl_schedule_node_insert_filter(node, filter);
1905 break;
1906 case isl_schedule_node_domain:
1907 case isl_schedule_node_leaf:
1908 break;
1911 return node;
1914 /* Compute the gist of the subtree at "node" with respect to
1915 * the reaching domain elements in "context".
1916 * In particular, compute the gist of all band and filter nodes
1917 * in the subtree with respect to "context". Children of set or sequence
1918 * nodes that end up with an empty filter are removed completely.
1920 * We keep track of the intersection of "context" with all outer filters
1921 * of the current node within the subtree in the final element of "filters".
1922 * Initially, this list contains the single element "context" and it is
1923 * extended or shortened each time we enter or leave a filter node.
1925 __isl_give isl_schedule_node *isl_schedule_node_gist(
1926 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
1928 struct isl_node_gist_data data;
1930 data.filters = isl_union_set_list_from_union_set(context);
1931 node = traverse(node, &gist_enter, &gist_leave, &data);
1932 isl_union_set_list_free(data.filters);
1933 return node;
1936 /* Intersect the domain of domain node "node" with "domain".
1938 * If the domain of "node" is already a subset of "domain",
1939 * then nothing needs to be changed.
1941 * Otherwise, we replace the domain of the domain node by the intersection
1942 * and simplify the subtree rooted at "node" with respect to this intersection.
1944 __isl_give isl_schedule_node *isl_schedule_node_domain_intersect_domain(
1945 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain)
1947 isl_schedule_tree *tree;
1948 isl_union_set *uset;
1949 int is_subset;
1951 if (!node || !domain)
1952 goto error;
1954 uset = isl_schedule_tree_domain_get_domain(node->tree);
1955 is_subset = isl_union_set_is_subset(uset, domain);
1956 isl_union_set_free(uset);
1957 if (is_subset < 0)
1958 goto error;
1959 if (is_subset) {
1960 isl_union_set_free(domain);
1961 return node;
1964 tree = isl_schedule_tree_copy(node->tree);
1965 uset = isl_schedule_tree_domain_get_domain(tree);
1966 uset = isl_union_set_intersect(uset, domain);
1967 tree = isl_schedule_tree_domain_set_domain(tree,
1968 isl_union_set_copy(uset));
1969 node = isl_schedule_node_graft_tree(node, tree);
1971 node = isl_schedule_node_child(node, 0);
1972 node = isl_schedule_node_gist(node, uset);
1973 node = isl_schedule_node_parent(node);
1975 return node;
1976 error:
1977 isl_schedule_node_free(node);
1978 isl_union_set_free(domain);
1979 return NULL;
1982 /* Reset the user pointer on all identifiers of parameters and tuples
1983 * in the schedule node "node".
1985 __isl_give isl_schedule_node *isl_schedule_node_reset_user(
1986 __isl_take isl_schedule_node *node)
1988 isl_schedule_tree *tree;
1990 tree = isl_schedule_node_get_tree(node);
1991 tree = isl_schedule_tree_reset_user(tree);
1992 node = isl_schedule_node_graft_tree(node, tree);
1994 return node;
1997 /* Align the parameters of the schedule node "node" to those of "space".
1999 __isl_give isl_schedule_node *isl_schedule_node_align_params(
2000 __isl_take isl_schedule_node *node, __isl_take isl_space *space)
2002 isl_schedule_tree *tree;
2004 tree = isl_schedule_node_get_tree(node);
2005 tree = isl_schedule_tree_align_params(tree, space);
2006 node = isl_schedule_node_graft_tree(node, tree);
2008 return node;
2011 /* Compute the pullback of schedule node "node"
2012 * by the function represented by "upma".
2013 * In other words, plug in "upma" in the iteration domains
2014 * of schedule node "node".
2016 * Note that this is only a helper function for
2017 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
2018 * this function should not be called on a single node without also
2019 * calling it on all the other nodes.
2021 __isl_give isl_schedule_node *isl_schedule_node_pullback_union_pw_multi_aff(
2022 __isl_take isl_schedule_node *node,
2023 __isl_take isl_union_pw_multi_aff *upma)
2025 isl_schedule_tree *tree;
2027 tree = isl_schedule_node_get_tree(node);
2028 tree = isl_schedule_tree_pullback_union_pw_multi_aff(tree, upma);
2029 node = isl_schedule_node_graft_tree(node, tree);
2031 return node;
2034 /* Return the position of the subtree containing "node" among the children
2035 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
2036 * In particular, both nodes should point to the same schedule tree.
2038 * Return -1 on error.
2040 int isl_schedule_node_get_ancestor_child_position(
2041 __isl_keep isl_schedule_node *node,
2042 __isl_keep isl_schedule_node *ancestor)
2044 int n1, n2;
2045 isl_schedule_tree *tree;
2047 if (!node || !ancestor)
2048 return -1;
2050 if (node->schedule != ancestor->schedule)
2051 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2052 "not a descendant", return -1);
2054 n1 = isl_schedule_node_get_tree_depth(ancestor);
2055 n2 = isl_schedule_node_get_tree_depth(node);
2057 if (n1 >= n2)
2058 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2059 "not a descendant", return -1);
2060 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n1);
2061 isl_schedule_tree_free(tree);
2062 if (tree != ancestor->tree)
2063 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2064 "not a descendant", return -1);
2066 return node->child_pos[n1];
2069 /* Given two nodes that point to the same schedule tree, return their
2070 * closest shared ancestor.
2072 * Since the two nodes point to the same schedule, they share at least
2073 * one ancestor, the root of the schedule. We move down from the root
2074 * to the first ancestor where the respective children have a different
2075 * child position. This is the requested ancestor.
2076 * If there is no ancestor where the children have a different position,
2077 * then one node is an ancestor of the other and then this node is
2078 * the requested ancestor.
2080 __isl_give isl_schedule_node *isl_schedule_node_get_shared_ancestor(
2081 __isl_keep isl_schedule_node *node1,
2082 __isl_keep isl_schedule_node *node2)
2084 int i, n1, n2;
2086 if (!node1 || !node2)
2087 return NULL;
2088 if (node1->schedule != node2->schedule)
2089 isl_die(isl_schedule_node_get_ctx(node1), isl_error_invalid,
2090 "not part of same schedule", return NULL);
2091 n1 = isl_schedule_node_get_tree_depth(node1);
2092 n2 = isl_schedule_node_get_tree_depth(node2);
2093 if (n2 < n1)
2094 return isl_schedule_node_get_shared_ancestor(node2, node1);
2095 if (n1 == 0)
2096 return isl_schedule_node_copy(node1);
2097 if (isl_schedule_node_is_equal(node1, node2))
2098 return isl_schedule_node_copy(node1);
2100 for (i = 0; i < n1; ++i)
2101 if (node1->child_pos[i] != node2->child_pos[i])
2102 break;
2104 node1 = isl_schedule_node_copy(node1);
2105 return isl_schedule_node_ancestor(node1, n1 - i);
2108 /* Print "node" to "p".
2110 __isl_give isl_printer *isl_printer_print_schedule_node(
2111 __isl_take isl_printer *p, __isl_keep isl_schedule_node *node)
2113 if (!node)
2114 return isl_printer_free(p);
2115 return isl_printer_print_schedule_tree_mark(p, node->schedule->root,
2116 isl_schedule_tree_list_n_schedule_tree(node->ancestors),
2117 node->child_pos);
2120 void isl_schedule_node_dump(__isl_keep isl_schedule_node *node)
2122 isl_ctx *ctx;
2123 isl_printer *printer;
2125 if (!node)
2126 return;
2128 ctx = isl_schedule_node_get_ctx(node);
2129 printer = isl_printer_to_file(ctx, stderr);
2130 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
2131 printer = isl_printer_print_schedule_node(printer, node);
2133 isl_printer_free(printer);