add isl_schedule_node_root
[isl.git] / isl_schedule_node.c
blob04445228388ecae90907ce3e6ad4a301738a19a8
1 /*
2 * Copyright 2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
10 #include <isl/set.h>
11 #include <isl_schedule_band.h>
12 #include <isl_schedule_private.h>
13 #include <isl_schedule_node_private.h>
15 /* Create a new schedule node in the given schedule, point at the given
16 * tree with given ancestors and child positions.
17 * "child_pos" may be NULL if there are no ancestors.
19 __isl_give isl_schedule_node *isl_schedule_node_alloc(
20 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree,
21 __isl_take isl_schedule_tree_list *ancestors, int *child_pos)
23 isl_ctx *ctx;
24 isl_schedule_node *node;
25 int i, n;
27 if (!schedule || !tree || !ancestors)
28 goto error;
29 n = isl_schedule_tree_list_n_schedule_tree(ancestors);
30 if (n > 0 && !child_pos)
31 goto error;
32 ctx = isl_schedule_get_ctx(schedule);
33 node = isl_calloc_type(ctx, isl_schedule_node);
34 if (!node)
35 goto error;
36 node->ref = 1;
37 node->schedule = schedule;
38 node->tree = tree;
39 node->ancestors = ancestors;
40 node->child_pos = isl_alloc_array(ctx, int, n);
41 if (n && !node->child_pos)
42 return isl_schedule_node_free(node);
43 for (i = 0; i < n; ++i)
44 node->child_pos[i] = child_pos[i];
46 return node;
47 error:
48 isl_schedule_free(schedule);
49 isl_schedule_tree_free(tree);
50 isl_schedule_tree_list_free(ancestors);
51 return NULL;
54 /* Return a pointer to the root of a schedule tree with as single
55 * node a domain node with the given domain.
57 __isl_give isl_schedule_node *isl_schedule_node_from_domain(
58 __isl_take isl_union_set *domain)
60 isl_schedule *schedule;
61 isl_schedule_node *node;
63 schedule = isl_schedule_from_domain(domain);
64 node = isl_schedule_get_root(schedule);
65 isl_schedule_free(schedule);
67 return node;
70 /* Return the isl_ctx to which "node" belongs.
72 isl_ctx *isl_schedule_node_get_ctx(__isl_keep isl_schedule_node *node)
74 return node ? isl_schedule_get_ctx(node->schedule) : NULL;
77 /* Return a pointer to the leaf of the schedule into which "node" points.
79 * Even though these leaves are not reference counted, we still
80 * indicate that this function does not return a copy.
82 __isl_keep isl_schedule_tree *isl_schedule_node_peek_leaf(
83 __isl_keep isl_schedule_node *node)
85 return node ? isl_schedule_peek_leaf(node->schedule) : NULL;
88 /* Return a pointer to the leaf of the schedule into which "node" points.
90 * Even though these leaves are not reference counted, we still
91 * return a "copy" of the leaf here such that it can still be "freed"
92 * by the user.
94 __isl_give isl_schedule_tree *isl_schedule_node_get_leaf(
95 __isl_keep isl_schedule_node *node)
97 return isl_schedule_tree_copy(isl_schedule_node_peek_leaf(node));
100 /* Return the type of the node or isl_schedule_node_error on error.
102 enum isl_schedule_node_type isl_schedule_node_get_type(
103 __isl_keep isl_schedule_node *node)
105 return node ? isl_schedule_tree_get_type(node->tree)
106 : isl_schedule_node_error;
109 /* Return the type of the parent of "node" or isl_schedule_node_error on error.
111 enum isl_schedule_node_type isl_schedule_node_get_parent_type(
112 __isl_keep isl_schedule_node *node)
114 int pos;
115 int has_parent;
116 isl_schedule_tree *parent;
117 enum isl_schedule_node_type type;
119 if (!node)
120 return isl_schedule_node_error;
121 has_parent = isl_schedule_node_has_parent(node);
122 if (has_parent < 0)
123 return isl_schedule_node_error;
124 if (!has_parent)
125 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
126 "node has no parent", return isl_schedule_node_error);
128 pos = isl_schedule_tree_list_n_schedule_tree(node->ancestors) - 1;
129 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors, pos);
130 type = isl_schedule_tree_get_type(parent);
131 isl_schedule_tree_free(parent);
133 return type;
136 /* Return a copy of the subtree that this node points to.
138 __isl_give isl_schedule_tree *isl_schedule_node_get_tree(
139 __isl_keep isl_schedule_node *node)
141 if (!node)
142 return NULL;
144 return isl_schedule_tree_copy(node->tree);
147 /* Return a copy of the schedule into which "node" points.
149 __isl_give isl_schedule *isl_schedule_node_get_schedule(
150 __isl_keep isl_schedule_node *node)
152 if (!node)
153 return NULL;
154 return isl_schedule_copy(node->schedule);
157 /* Return a fresh copy of "node".
159 __isl_take isl_schedule_node *isl_schedule_node_dup(
160 __isl_keep isl_schedule_node *node)
162 if (!node)
163 return NULL;
165 return isl_schedule_node_alloc(isl_schedule_copy(node->schedule),
166 isl_schedule_tree_copy(node->tree),
167 isl_schedule_tree_list_copy(node->ancestors),
168 node->child_pos);
171 /* Return an isl_schedule_node that is equal to "node" and that has only
172 * a single reference.
174 __isl_give isl_schedule_node *isl_schedule_node_cow(
175 __isl_take isl_schedule_node *node)
177 if (!node)
178 return NULL;
180 if (node->ref == 1)
181 return node;
182 node->ref--;
183 return isl_schedule_node_dup(node);
186 /* Return a new reference to "node".
188 __isl_give isl_schedule_node *isl_schedule_node_copy(
189 __isl_keep isl_schedule_node *node)
191 if (!node)
192 return NULL;
194 node->ref++;
195 return node;
198 /* Free "node" and return NULL.
200 * Since the node may point to a leaf of its schedule, which
201 * point to a field inside the schedule, we need to make sure
202 * we free the tree before freeing the schedule.
204 __isl_null isl_schedule_node *isl_schedule_node_free(
205 __isl_take isl_schedule_node *node)
207 if (!node)
208 return NULL;
209 if (--node->ref > 0)
210 return NULL;
212 isl_schedule_tree_list_free(node->ancestors);
213 free(node->child_pos);
214 isl_schedule_tree_free(node->tree);
215 isl_schedule_free(node->schedule);
216 free(node);
218 return NULL;
221 /* Do "node1" and "node2" point to the same position in the same
222 * schedule?
224 int isl_schedule_node_is_equal(__isl_keep isl_schedule_node *node1,
225 __isl_keep isl_schedule_node *node2)
227 int i, n1, n2;
229 if (!node1 || !node2)
230 return -1;
231 if (node1 == node2)
232 return 1;
233 if (node1->schedule != node2->schedule)
234 return 0;
236 n1 = isl_schedule_node_get_tree_depth(node1);
237 n2 = isl_schedule_node_get_tree_depth(node2);
238 if (n1 != n2)
239 return 0;
240 for (i = 0; i < n1; ++i)
241 if (node1->child_pos[i] != node2->child_pos[i])
242 return 0;
244 return 1;
247 /* Internal data structure for
248 * isl_schedule_node_get_prefix_schedule_union_pw_multi_aff
250 * "initialized" is set if the filter field has been initialized.
251 * "universe_filter" is set if we are only collecting the universes of filters
252 * "collect_prefix" is set if we are collecting prefixes.
253 * "filter" collects all outer filters and is NULL until "initialized" is set.
254 * "prefix" collects all outer band partial schedules (if "collect_prefix"
255 * is set). If it is used, then it is initialized by the caller
256 * of collect_filter_prefix to a zero-dimensional function.
258 struct isl_schedule_node_get_filter_prefix_data {
259 int initialized;
260 int universe_filter;
261 int collect_prefix;
262 isl_union_set *filter;
263 isl_multi_union_pw_aff *prefix;
266 /* Update "data" based on the tree node "tree" in case "data" has
267 * not been initialized yet.
269 * Return 0 on success and -1 on error.
271 * If "tree" is a filter, then we set data->filter to this filter
272 * (or its universe).
273 * If "tree" is a domain, then this means we have reached the root
274 * of the schedule tree without being able to extract any information.
275 * We therefore initialize data->filter to the universe of the domain.
276 * If "tree" is a band with at least one member, then we set data->filter
277 * to the universe of the schedule domain and replace the zero-dimensional
278 * data->prefix by the band schedule (if data->collect_prefix is set).
280 static int collect_filter_prefix_init(__isl_keep isl_schedule_tree *tree,
281 struct isl_schedule_node_get_filter_prefix_data *data)
283 enum isl_schedule_node_type type;
284 isl_multi_union_pw_aff *mupa;
285 isl_union_set *filter;
287 type = isl_schedule_tree_get_type(tree);
288 switch (type) {
289 case isl_schedule_node_error:
290 return -1;
291 case isl_schedule_node_leaf:
292 case isl_schedule_node_sequence:
293 case isl_schedule_node_set:
294 return 0;
295 case isl_schedule_node_domain:
296 filter = isl_schedule_tree_domain_get_domain(tree);
297 filter = isl_union_set_universe(filter);
298 data->filter = filter;
299 break;
300 case isl_schedule_node_band:
301 if (isl_schedule_tree_band_n_member(tree) == 0)
302 return 0;
303 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
304 if (data->collect_prefix) {
305 isl_multi_union_pw_aff_free(data->prefix);
306 mupa = isl_multi_union_pw_aff_reset_tuple_id(mupa,
307 isl_dim_set);
308 data->prefix = isl_multi_union_pw_aff_copy(mupa);
310 filter = isl_multi_union_pw_aff_domain(mupa);
311 filter = isl_union_set_universe(filter);
312 data->filter = filter;
313 break;
314 case isl_schedule_node_filter:
315 filter = isl_schedule_tree_filter_get_filter(tree);
316 if (data->universe_filter)
317 filter = isl_union_set_universe(filter);
318 data->filter = filter;
319 break;
322 if ((data->collect_prefix && !data->prefix) || !data->filter)
323 return -1;
325 data->initialized = 1;
327 return 0;
330 /* Update "data" based on the tree node "tree" in case "data" has
331 * already been initialized.
333 * Return 0 on success and -1 on error.
335 * If "tree" is a filter, then we intersect data->filter with this filter
336 * (or its universe).
337 * If "tree" is a band with at least one member and data->collect_prefix
338 * is set, then we extend data->prefix with the band schedule.
340 static int collect_filter_prefix_update(__isl_keep isl_schedule_tree *tree,
341 struct isl_schedule_node_get_filter_prefix_data *data)
343 enum isl_schedule_node_type type;
344 isl_multi_union_pw_aff *mupa;
345 isl_union_set *filter;
347 type = isl_schedule_tree_get_type(tree);
348 switch (type) {
349 case isl_schedule_node_error:
350 return -1;
351 case isl_schedule_node_domain:
352 case isl_schedule_node_leaf:
353 case isl_schedule_node_sequence:
354 case isl_schedule_node_set:
355 break;
356 case isl_schedule_node_band:
357 if (isl_schedule_tree_band_n_member(tree) == 0)
358 break;
359 if (!data->collect_prefix)
360 break;
361 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
362 data->prefix = isl_multi_union_pw_aff_flat_range_product(mupa,
363 data->prefix);
364 if (!data->prefix)
365 return -1;
366 break;
367 case isl_schedule_node_filter:
368 filter = isl_schedule_tree_filter_get_filter(tree);
369 if (data->universe_filter)
370 filter = isl_union_set_universe(filter);
371 data->filter = isl_union_set_intersect(data->filter, filter);
372 if (!data->filter)
373 return -1;
374 break;
377 return 0;
380 /* Collect filter and/or prefix information from the elements
381 * in "list" (which represent the ancestors of a node).
382 * Store the results in "data".
384 * Return 0 on success and -1 on error.
386 * We traverse the list from innermost ancestor (last element)
387 * to outermost ancestor (first element), calling collect_filter_prefix_init
388 * on each node as long as we have not been able to extract any information
389 * yet and collect_filter_prefix_update afterwards.
390 * On successful return, data->initialized will be set since the outermost
391 * ancestor is a domain node, which always results in an initialization.
393 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
394 struct isl_schedule_node_get_filter_prefix_data *data)
396 int i, n;
398 data->initialized = 0;
399 data->filter = NULL;
401 if (!list)
402 return -1;
404 n = isl_schedule_tree_list_n_schedule_tree(list);
405 for (i = n - 1; i >= 0; --i) {
406 isl_schedule_tree *tree;
407 int r;
409 tree = isl_schedule_tree_list_get_schedule_tree(list, i);
410 if (!tree)
411 return -1;
412 if (!data->initialized)
413 r = collect_filter_prefix_init(tree, data);
414 else
415 r = collect_filter_prefix_update(tree, data);
416 isl_schedule_tree_free(tree);
417 if (r < 0)
418 return -1;
421 return 0;
424 /* Return the concatenation of the partial schedules of all outer band
425 * nodes of "node" interesected with all outer filters
426 * as an isl_union_pw_multi_aff.
428 * If "node" is pointing at the root of the schedule tree, then
429 * there are no domain elements reaching the current node, so
430 * we return an empty result.
432 * We collect all the filters and partial schedules in collect_filter_prefix.
433 * The partial schedules are collected as an isl_multi_union_pw_aff.
434 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
435 * contain any domain information, so we construct the isl_union_pw_multi_aff
436 * result as a zero-dimensional function on the collected filter.
437 * Otherwise, we convert the isl_multi_union_pw_aff to
438 * an isl_multi_union_pw_aff and intersect the domain with the filter.
440 __isl_give isl_union_pw_multi_aff *
441 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
442 __isl_keep isl_schedule_node *node)
444 isl_space *space;
445 isl_union_pw_multi_aff *prefix;
446 struct isl_schedule_node_get_filter_prefix_data data;
448 if (!node)
449 return NULL;
451 space = isl_schedule_get_space(node->schedule);
452 if (node->tree == node->schedule->root)
453 return isl_union_pw_multi_aff_empty(space);
455 space = isl_space_set_from_params(space);
456 data.universe_filter = 0;
457 data.collect_prefix = 1;
458 data.prefix = isl_multi_union_pw_aff_zero(space);
460 if (collect_filter_prefix(node->ancestors, &data) < 0)
461 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
463 if (data.prefix &&
464 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
465 isl_multi_union_pw_aff_free(data.prefix);
466 prefix = isl_union_pw_multi_aff_from_domain(data.filter);
467 } else {
468 prefix =
469 isl_union_pw_multi_aff_from_multi_union_pw_aff(data.prefix);
470 prefix = isl_union_pw_multi_aff_intersect_domain(prefix,
471 data.filter);
474 return prefix;
477 /* Return the concatenation of the partial schedules of all outer band
478 * nodes of "node" interesected with all outer filters
479 * as an isl_union_map.
481 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_union_map(
482 __isl_keep isl_schedule_node *node)
484 isl_union_pw_multi_aff *upma;
486 upma = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
487 return isl_union_map_from_union_pw_multi_aff(upma);
490 /* Return the union of universe sets of the domain elements that reach "node".
492 * If "node" is pointing at the root of the schedule tree, then
493 * there are no domain elements reaching the current node, so
494 * we return an empty result.
496 * Otherwise, we collect the universes of all filters reaching the node
497 * in collect_filter_prefix.
499 __isl_give isl_union_set *isl_schedule_node_get_universe_domain(
500 __isl_keep isl_schedule_node *node)
502 struct isl_schedule_node_get_filter_prefix_data data;
504 if (!node)
505 return NULL;
507 if (node->tree == node->schedule->root) {
508 isl_space *space;
510 space = isl_schedule_get_space(node->schedule);
511 return isl_union_set_empty(space);
514 data.universe_filter = 1;
515 data.collect_prefix = 0;
516 data.prefix = NULL;
518 if (collect_filter_prefix(node->ancestors, &data) < 0)
519 data.filter = isl_union_set_free(data.filter);
521 return data.filter;
524 /* Return the subtree schedule of "node".
526 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
527 * trees that do not contain any schedule information, we first
528 * move down to the first relevant descendant and handle leaves ourselves.
530 __isl_give isl_union_map *isl_schedule_node_get_subtree_schedule_union_map(
531 __isl_keep isl_schedule_node *node)
533 isl_schedule_tree *tree, *leaf;
534 isl_union_map *umap;
536 tree = isl_schedule_node_get_tree(node);
537 leaf = isl_schedule_node_peek_leaf(node);
538 tree = isl_schedule_tree_first_schedule_descendant(tree, leaf);
539 if (!tree)
540 return NULL;
541 if (tree == leaf) {
542 isl_union_set *domain;
543 domain = isl_schedule_node_get_universe_domain(node);
544 isl_schedule_tree_free(tree);
545 return isl_union_map_from_domain(domain);
548 umap = isl_schedule_tree_get_subtree_schedule_union_map(tree);
549 isl_schedule_tree_free(tree);
550 return umap;
553 /* Return the number of ancestors of "node" in its schedule tree.
555 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node *node)
557 if (!node)
558 return -1;
559 return isl_schedule_tree_list_n_schedule_tree(node->ancestors);
562 /* Does "node" have a parent?
564 * That is, does it point to any node of the schedule other than the root?
566 int isl_schedule_node_has_parent(__isl_keep isl_schedule_node *node)
568 if (!node)
569 return -1;
570 if (!node->ancestors)
571 return -1;
573 return isl_schedule_tree_list_n_schedule_tree(node->ancestors) != 0;
576 /* Return the position of "node" among the children of its parent.
578 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node *node)
580 int n;
581 int has_parent;
583 if (!node)
584 return -1;
585 has_parent = isl_schedule_node_has_parent(node);
586 if (has_parent < 0)
587 return -1;
588 if (!has_parent)
589 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
590 "node has no parent", return -1);
592 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
593 return node->child_pos[n - 1];
596 /* Does the parent (if any) of "node" have any children with a smaller child
597 * position than this one?
599 int isl_schedule_node_has_previous_sibling(__isl_keep isl_schedule_node *node)
601 int n;
602 int has_parent;
604 if (!node)
605 return -1;
606 has_parent = isl_schedule_node_has_parent(node);
607 if (has_parent < 0 || !has_parent)
608 return has_parent;
610 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
612 return node->child_pos[n - 1] > 0;
615 /* Does the parent (if any) of "node" have any children with a greater child
616 * position than this one?
618 int isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node *node)
620 int n, n_child;
621 int has_parent;
622 isl_schedule_tree *tree;
624 if (!node)
625 return -1;
626 has_parent = isl_schedule_node_has_parent(node);
627 if (has_parent < 0 || !has_parent)
628 return has_parent;
630 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
631 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n - 1);
632 if (!tree)
633 return -1;
634 n_child = isl_schedule_tree_list_n_schedule_tree(tree->children);
635 isl_schedule_tree_free(tree);
637 return node->child_pos[n - 1] + 1 < n_child;
640 /* Does "node" have any children?
642 * Any node other than the leaf nodes is considered to have at least
643 * one child, even if the corresponding isl_schedule_tree does not
644 * have any children.
646 int isl_schedule_node_has_children(__isl_keep isl_schedule_node *node)
648 if (!node)
649 return -1;
650 return !isl_schedule_tree_is_leaf(node->tree);
653 /* Return the number of children of "node"?
655 * Any node other than the leaf nodes is considered to have at least
656 * one child, even if the corresponding isl_schedule_tree does not
657 * have any children. That is, the number of children of "node" is
658 * only zero if its tree is the explicit empty tree. Otherwise,
659 * if the isl_schedule_tree has any children, then it is equal
660 * to the number of children of "node". If it has zero children,
661 * then "node" still has a leaf node as child.
663 int isl_schedule_node_n_children(__isl_keep isl_schedule_node *node)
665 int n;
667 if (!node)
668 return -1;
670 if (isl_schedule_tree_is_leaf(node->tree))
671 return 0;
673 n = isl_schedule_tree_n_children(node->tree);
674 if (n == 0)
675 return 1;
677 return n;
680 /* Move the "node" pointer to the ancestor of the given generation
681 * of the node it currently points to, where generation 0 is the node
682 * itself and generation 1 is its parent.
684 __isl_give isl_schedule_node *isl_schedule_node_ancestor(
685 __isl_take isl_schedule_node *node, int generation)
687 int n;
688 isl_schedule_tree *tree;
690 if (!node)
691 return NULL;
692 if (generation == 0)
693 return node;
694 n = isl_schedule_node_get_tree_depth(node);
695 if (n < 0)
696 return isl_schedule_node_free(node);
697 if (generation < 0 || generation > n)
698 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
699 "generation out of bounds",
700 return isl_schedule_node_free(node));
701 node = isl_schedule_node_cow(node);
702 if (!node)
703 return NULL;
705 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
706 n - generation);
707 isl_schedule_tree_free(node->tree);
708 node->tree = tree;
709 node->ancestors = isl_schedule_tree_list_drop(node->ancestors,
710 n - generation, generation);
711 if (!node->ancestors || !node->tree)
712 return isl_schedule_node_free(node);
714 return node;
717 /* Move the "node" pointer to the parent of the node it currently points to.
719 __isl_give isl_schedule_node *isl_schedule_node_parent(
720 __isl_take isl_schedule_node *node)
722 if (!node)
723 return NULL;
724 if (!isl_schedule_node_has_parent(node))
725 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
726 "node has no parent",
727 return isl_schedule_node_free(node));
728 return isl_schedule_node_ancestor(node, 1);
731 /* Move the "node" pointer to the root of its schedule tree.
733 __isl_give isl_schedule_node *isl_schedule_node_root(
734 __isl_take isl_schedule_node *node)
736 int n;
738 if (!node)
739 return NULL;
740 n = isl_schedule_node_get_tree_depth(node);
741 if (n < 0)
742 return isl_schedule_node_free(node);
743 return isl_schedule_node_ancestor(node, n);
746 /* Move the "node" pointer to the child at position "pos" of the node
747 * it currently points to.
749 __isl_give isl_schedule_node *isl_schedule_node_child(
750 __isl_take isl_schedule_node *node, int pos)
752 int n;
753 isl_ctx *ctx;
754 isl_schedule_tree *tree;
755 int *child_pos;
757 node = isl_schedule_node_cow(node);
758 if (!node)
759 return NULL;
760 if (!isl_schedule_node_has_children(node))
761 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
762 "node has no children",
763 return isl_schedule_node_free(node));
765 ctx = isl_schedule_node_get_ctx(node);
766 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
767 child_pos = isl_realloc_array(ctx, node->child_pos, int, n + 1);
768 if (!child_pos)
769 return isl_schedule_node_free(node);
770 node->child_pos = child_pos;
771 node->child_pos[n] = pos;
773 node->ancestors = isl_schedule_tree_list_add(node->ancestors,
774 isl_schedule_tree_copy(node->tree));
775 tree = node->tree;
776 if (isl_schedule_tree_has_children(tree))
777 tree = isl_schedule_tree_get_child(tree, pos);
778 else
779 tree = isl_schedule_node_get_leaf(node);
780 isl_schedule_tree_free(node->tree);
781 node->tree = tree;
783 if (!node->tree || !node->ancestors)
784 return isl_schedule_node_free(node);
786 return node;
789 /* Move the "node" pointer to the first child of the node
790 * it currently points to.
792 __isl_give isl_schedule_node *isl_schedule_node_first_child(
793 __isl_take isl_schedule_node *node)
795 return isl_schedule_node_child(node, 0);
798 /* Move the "node" pointer to the child of this node's parent in
799 * the previous child position.
801 __isl_give isl_schedule_node *isl_schedule_node_previous_sibling(
802 __isl_take isl_schedule_node *node)
804 int n;
805 isl_schedule_tree *parent, *tree;
807 node = isl_schedule_node_cow(node);
808 if (!node)
809 return NULL;
810 if (!isl_schedule_node_has_previous_sibling(node))
811 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
812 "node has no previous sibling",
813 return isl_schedule_node_free(node));
815 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
816 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
817 n - 1);
818 if (!parent)
819 return isl_schedule_node_free(node);
820 node->child_pos[n - 1]--;
821 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
822 node->child_pos[n - 1]);
823 isl_schedule_tree_free(parent);
824 if (!tree)
825 return isl_schedule_node_free(node);
826 isl_schedule_tree_free(node->tree);
827 node->tree = tree;
829 return node;
832 /* Move the "node" pointer to the child of this node's parent in
833 * the next child position.
835 __isl_give isl_schedule_node *isl_schedule_node_next_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_next_sibling(node))
845 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
846 "node has no next 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 /* Return a copy to the child at position "pos" of "node".
868 __isl_give isl_schedule_node *isl_schedule_node_get_child(
869 __isl_keep isl_schedule_node *node, int pos)
871 return isl_schedule_node_child(isl_schedule_node_copy(node), pos);
874 /* Traverse the descendant of "node" in depth-first order, including
875 * "node" itself. Call "enter" whenever a node is entered and "leave"
876 * whenever a node is left. The callback "enter" is responsible
877 * for moving to the deepest initial subtree of its argument that
878 * should be traversed.
880 static __isl_give isl_schedule_node *traverse(
881 __isl_take isl_schedule_node *node,
882 __isl_give isl_schedule_node *(*enter)(
883 __isl_take isl_schedule_node *node, void *user),
884 __isl_give isl_schedule_node *(*leave)(
885 __isl_take isl_schedule_node *node, void *user),
886 void *user)
888 int depth;
890 if (!node)
891 return NULL;
893 depth = isl_schedule_node_get_tree_depth(node);
894 do {
895 node = enter(node, user);
896 node = leave(node, user);
897 while (node && isl_schedule_node_get_tree_depth(node) > depth &&
898 !isl_schedule_node_has_next_sibling(node)) {
899 node = isl_schedule_node_parent(node);
900 node = leave(node, user);
902 if (node && isl_schedule_node_get_tree_depth(node) > depth)
903 node = isl_schedule_node_next_sibling(node);
904 } while (node && isl_schedule_node_get_tree_depth(node) > depth);
906 return node;
909 /* Internal data structure for isl_schedule_node_foreach_descendant.
911 * "fn" is the user-specified callback function.
912 * "user" is the user-specified argument for the callback.
914 struct isl_schedule_node_preorder_data {
915 int (*fn)(__isl_keep isl_schedule_node *node, void *user);
916 void *user;
919 /* Callback for "traverse" to enter a node and to move
920 * to the deepest initial subtree that should be traversed
921 * for use in a preorder visit.
923 * If the user callback returns a negative value, then we abort
924 * the traversal. If this callback returns zero, then we skip
925 * the subtree rooted at the current node. Otherwise, we move
926 * down to the first child and repeat the process until a leaf
927 * is reached.
929 static __isl_give isl_schedule_node *preorder_enter(
930 __isl_take isl_schedule_node *node, void *user)
932 struct isl_schedule_node_preorder_data *data = user;
934 if (!node)
935 return NULL;
937 do {
938 int r;
940 r = data->fn(node, data->user);
941 if (r < 0)
942 return isl_schedule_node_free(node);
943 if (r == 0)
944 return node;
945 } while (isl_schedule_node_has_children(node) &&
946 (node = isl_schedule_node_first_child(node)) != NULL);
948 return node;
951 /* Callback for "traverse" to leave a node
952 * for use in a preorder visit.
953 * Since we already visited the node when we entered it,
954 * we do not need to do anything here.
956 static __isl_give isl_schedule_node *preorder_leave(
957 __isl_take isl_schedule_node *node, void *user)
959 return node;
962 /* Traverse the descendants of "node" (including the node itself)
963 * in depth first preorder.
965 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
966 * If "fn" returns 0 on any of the nodes, then the subtree rooted
967 * at that node is skipped.
969 * Return 0 on success and -1 on failure.
971 int isl_schedule_node_foreach_descendant(__isl_keep isl_schedule_node *node,
972 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
974 struct isl_schedule_node_preorder_data data = { fn, user };
976 node = isl_schedule_node_copy(node);
977 node = traverse(node, &preorder_enter, &preorder_leave, &data);
978 isl_schedule_node_free(node);
980 return node ? 0 : -1;
983 /* Internal data structure for isl_schedule_node_map_descendant.
985 * "fn" is the user-specified callback function.
986 * "user" is the user-specified argument for the callback.
988 struct isl_schedule_node_postorder_data {
989 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
990 void *user);
991 void *user;
994 /* Callback for "traverse" to enter a node and to move
995 * to the deepest initial subtree that should be traversed
996 * for use in a postorder visit.
998 * Since we are performing a postorder visit, we only need
999 * to move to the deepest initial leaf here.
1001 static __isl_give isl_schedule_node *postorder_enter(
1002 __isl_take isl_schedule_node *node, void *user)
1004 while (node && isl_schedule_node_has_children(node))
1005 node = isl_schedule_node_first_child(node);
1007 return node;
1010 /* Callback for "traverse" to leave a node
1011 * for use in a postorder visit.
1013 * Since we are performing a postorder visit, we need
1014 * to call the user callback here.
1016 static __isl_give isl_schedule_node *postorder_leave(
1017 __isl_take isl_schedule_node *node, void *user)
1019 struct isl_schedule_node_postorder_data *data = user;
1021 return data->fn(node, data->user);
1024 /* Traverse the descendants of "node" (including the node itself)
1025 * in depth first postorder, allowing the user to modify the visited node.
1026 * The traversal continues from the node returned by the callback function.
1027 * It is the responsibility of the user to ensure that this does not
1028 * lead to an infinite loop. It is safest to always return a pointer
1029 * to the same position (same ancestors and child positions) as the input node.
1031 __isl_give isl_schedule_node *isl_schedule_node_map_descendant(
1032 __isl_take isl_schedule_node *node,
1033 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1034 void *user), void *user)
1036 struct isl_schedule_node_postorder_data data = { fn, user };
1038 return traverse(node, &postorder_enter, &postorder_leave, &data);
1041 /* Return the number of members in the given band node.
1043 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node *node)
1045 return node ? isl_schedule_tree_band_n_member(node->tree) : 0;
1048 /* Is the band member at position "pos" of the band node "node"
1049 * marked coincident?
1051 int isl_schedule_node_band_member_get_coincident(
1052 __isl_keep isl_schedule_node *node, int pos)
1054 if (!node)
1055 return -1;
1056 return isl_schedule_tree_band_member_get_coincident(node->tree, pos);
1059 /* Mark the band member at position "pos" the band node "node"
1060 * as being coincident or not according to "coincident".
1062 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_coincident(
1063 __isl_take isl_schedule_node *node, int pos, int coincident)
1065 int c;
1066 isl_schedule_tree *tree;
1068 if (!node)
1069 return NULL;
1070 c = isl_schedule_node_band_member_get_coincident(node, pos);
1071 if (c == coincident)
1072 return node;
1074 tree = isl_schedule_tree_copy(node->tree);
1075 tree = isl_schedule_tree_band_member_set_coincident(tree, pos,
1076 coincident);
1077 node = isl_schedule_node_graft_tree(node, tree);
1079 return node;
1082 /* Is the band node "node" marked permutable?
1084 int isl_schedule_node_band_get_permutable(__isl_keep isl_schedule_node *node)
1086 if (!node)
1087 return -1;
1089 return isl_schedule_tree_band_get_permutable(node->tree);
1092 /* Mark the band node "node" permutable or not according to "permutable"?
1094 __isl_give isl_schedule_node *isl_schedule_node_band_set_permutable(
1095 __isl_take isl_schedule_node *node, int permutable)
1097 isl_schedule_tree *tree;
1099 if (!node)
1100 return NULL;
1101 if (isl_schedule_node_band_get_permutable(node) == permutable)
1102 return node;
1104 tree = isl_schedule_tree_copy(node->tree);
1105 tree = isl_schedule_tree_band_set_permutable(tree, permutable);
1106 node = isl_schedule_node_graft_tree(node, tree);
1108 return node;
1111 /* Return the schedule space of the band node.
1113 __isl_give isl_space *isl_schedule_node_band_get_space(
1114 __isl_keep isl_schedule_node *node)
1116 if (!node)
1117 return NULL;
1119 return isl_schedule_tree_band_get_space(node->tree);
1122 /* Return the schedule of the band node in isolation.
1124 __isl_give isl_multi_union_pw_aff *isl_schedule_node_band_get_partial_schedule(
1125 __isl_keep isl_schedule_node *node)
1127 if (!node)
1128 return NULL;
1130 return isl_schedule_tree_band_get_partial_schedule(node->tree);
1133 /* Return the schedule of the band node in isolation in the form of
1134 * an isl_union_map.
1136 * If the band does not have any members, then we construct a universe map
1137 * with the universe of the domain elements reaching the node as domain.
1138 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1139 * convert that to an isl_union_map.
1141 __isl_give isl_union_map *isl_schedule_node_band_get_partial_schedule_union_map(
1142 __isl_keep isl_schedule_node *node)
1144 isl_multi_union_pw_aff *mupa;
1146 if (!node)
1147 return NULL;
1149 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
1150 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1151 "not a band node", return NULL);
1152 if (isl_schedule_node_band_n_member(node) == 0) {
1153 isl_union_set *domain;
1155 domain = isl_schedule_node_get_universe_domain(node);
1156 return isl_union_map_from_domain(domain);
1159 mupa = isl_schedule_node_band_get_partial_schedule(node);
1160 return isl_union_map_from_multi_union_pw_aff(mupa);
1163 /* Make sure that that spaces of "node" and "mv" are the same.
1164 * Return -1 on error, reporting the error to the user.
1166 static int check_space_multi_val(__isl_keep isl_schedule_node *node,
1167 __isl_keep isl_multi_val *mv)
1169 isl_space *node_space, *mv_space;
1170 int equal;
1172 node_space = isl_schedule_node_band_get_space(node);
1173 mv_space = isl_multi_val_get_space(mv);
1174 equal = isl_space_tuple_is_equal(node_space, isl_dim_set,
1175 mv_space, isl_dim_set);
1176 isl_space_free(mv_space);
1177 isl_space_free(node_space);
1178 if (equal < 0)
1179 return -1;
1180 if (!equal)
1181 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1182 "spaces don't match", return -1);
1184 return 0;
1187 /* Multiply the partial schedule of the band node "node"
1188 * with the factors in "mv".
1190 __isl_give isl_schedule_node *isl_schedule_node_band_scale(
1191 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1193 isl_schedule_tree *tree;
1195 if (!node || !mv)
1196 goto error;
1197 if (check_space_multi_val(node, mv) < 0)
1198 goto error;
1200 tree = isl_schedule_node_get_tree(node);
1201 tree = isl_schedule_tree_band_scale(tree, mv);
1202 return isl_schedule_node_graft_tree(node, tree);
1203 error:
1204 isl_multi_val_free(mv);
1205 isl_schedule_node_free(node);
1206 return NULL;
1209 /* Divide the partial schedule of the band node "node"
1210 * by the factors in "mv".
1212 __isl_give isl_schedule_node *isl_schedule_node_band_scale_down(
1213 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1215 isl_schedule_tree *tree;
1217 if (!node || !mv)
1218 goto error;
1219 if (check_space_multi_val(node, mv) < 0)
1220 goto error;
1222 tree = isl_schedule_node_get_tree(node);
1223 tree = isl_schedule_tree_band_scale_down(tree, mv);
1224 return isl_schedule_node_graft_tree(node, tree);
1225 error:
1226 isl_multi_val_free(mv);
1227 isl_schedule_node_free(node);
1228 return NULL;
1231 /* Tile "node" with tile sizes "sizes".
1233 * The current node is replaced by two nested nodes corresponding
1234 * to the tile dimensions and the point dimensions.
1236 * Return a pointer to the outer (tile) node.
1238 * If the scale tile loops option is set, then the tile loops
1239 * are scaled by the tile sizes. If the shift point loops option is set,
1240 * then the point loops are shifted to start at zero.
1241 * In particular, these options affect the tile and point loop schedules
1242 * as follows
1244 * scale shift original tile point
1246 * 0 0 i floor(i/s) i
1247 * 1 0 i s * floor(i/s) i
1248 * 0 1 i floor(i/s) i - s * floor(i/s)
1249 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1251 __isl_give isl_schedule_node *isl_schedule_node_band_tile(
1252 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
1254 isl_schedule_tree *tree;
1256 if (!node || !sizes)
1257 goto error;
1259 if (check_space_multi_val(node, sizes) < 0)
1260 goto error;
1262 tree = isl_schedule_node_get_tree(node);
1263 tree = isl_schedule_tree_band_tile(tree, sizes);
1264 return isl_schedule_node_graft_tree(node, tree);
1265 error:
1266 isl_multi_val_free(sizes);
1267 isl_schedule_node_free(node);
1268 return NULL;
1271 /* Move the band node "node" down to all the leaves in the subtree
1272 * rooted at "node".
1273 * Return a pointer to the node in the resulting tree that is in the same
1274 * position as the node pointed to by "node" in the original tree.
1276 * If the node only has a leaf child, then nothing needs to be done.
1277 * Otherwise, the child of the node is removed and the result is
1278 * appended to all the leaves in the subtree rooted at the original child.
1279 * The original node is then replaced by the result of this operation.
1281 __isl_give isl_schedule_node *isl_schedule_node_band_sink(
1282 __isl_take isl_schedule_node *node)
1284 enum isl_schedule_node_type type;
1285 isl_schedule_tree *tree, *child;
1287 if (!node)
1288 return NULL;
1290 type = isl_schedule_node_get_type(node);
1291 if (type != isl_schedule_node_band)
1292 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1293 "not a band node", isl_schedule_node_free(node));
1294 if (isl_schedule_tree_n_children(node->tree) == 0)
1295 return node;
1297 tree = isl_schedule_node_get_tree(node);
1298 child = isl_schedule_tree_get_child(tree, 0);
1299 tree = isl_schedule_tree_reset_children(tree);
1300 tree = isl_schedule_tree_append_to_leaves(child, tree);
1302 return isl_schedule_node_graft_tree(node, tree);
1305 /* Split "node" into two nested band nodes, one with the first "pos"
1306 * dimensions and one with the remaining dimensions.
1307 * The schedules of the two band nodes live in anonymous spaces.
1309 __isl_give isl_schedule_node *isl_schedule_node_band_split(
1310 __isl_take isl_schedule_node *node, int pos)
1312 isl_schedule_tree *tree;
1314 tree = isl_schedule_node_get_tree(node);
1315 tree = isl_schedule_tree_band_split(tree, pos);
1316 return isl_schedule_node_graft_tree(node, tree);
1319 /* Return the domain of the domain node "node".
1321 __isl_give isl_union_set *isl_schedule_node_domain_get_domain(
1322 __isl_keep isl_schedule_node *node)
1324 if (!node)
1325 return NULL;
1327 return isl_schedule_tree_domain_get_domain(node->tree);
1330 /* Return the filter of the filter node "node".
1332 __isl_give isl_union_set *isl_schedule_node_filter_get_filter(
1333 __isl_keep isl_schedule_node *node)
1335 if (!node)
1336 return NULL;
1338 return isl_schedule_tree_filter_get_filter(node->tree);
1341 /* Replace the filter of filter node "node" by "filter".
1343 __isl_give isl_schedule_node *isl_schedule_node_filter_set_filter(
1344 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
1346 isl_schedule_tree *tree;
1348 if (!node || !filter)
1349 goto error;
1351 tree = isl_schedule_tree_copy(node->tree);
1352 tree = isl_schedule_tree_filter_set_filter(tree, filter);
1353 return isl_schedule_node_graft_tree(node, tree);
1354 error:
1355 isl_schedule_node_free(node);
1356 isl_union_set_free(filter);
1357 return NULL;
1360 /* Update the ancestors of "node" to point to the tree that "node"
1361 * now points to.
1362 * That is, replace the child in the original parent that corresponds
1363 * to the current tree position by node->tree and continue updating
1364 * the ancestors in the same way until the root is reached.
1366 * If "node" originally points to a leaf of the schedule tree, then make sure
1367 * that in the end it points to a leaf in the updated schedule tree.
1369 static __isl_give isl_schedule_node *update_ancestors(
1370 __isl_take isl_schedule_node *node)
1372 int i, n;
1373 int is_leaf;
1374 isl_ctx *ctx;
1375 isl_schedule_tree *tree;
1377 node = isl_schedule_node_cow(node);
1378 if (!node)
1379 return NULL;
1381 ctx = isl_schedule_node_get_ctx(node);
1382 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1383 tree = isl_schedule_tree_copy(node->tree);
1385 for (i = n - 1; i >= 0; --i) {
1386 isl_schedule_tree *parent;
1388 parent = isl_schedule_tree_list_get_schedule_tree(
1389 node->ancestors, i);
1390 parent = isl_schedule_tree_replace_child(parent,
1391 node->child_pos[i], tree);
1392 node->ancestors = isl_schedule_tree_list_set_schedule_tree(
1393 node->ancestors, i, isl_schedule_tree_copy(parent));
1395 tree = parent;
1398 is_leaf = isl_schedule_tree_is_leaf(node->tree);
1399 node->schedule = isl_schedule_set_root(node->schedule, tree);
1400 if (is_leaf) {
1401 isl_schedule_tree_free(node->tree);
1402 node->tree = isl_schedule_node_get_leaf(node);
1405 if (!node->schedule || !node->ancestors)
1406 return isl_schedule_node_free(node);
1408 return node;
1411 /* Replace the subtree that "pos" points to by "tree", updating
1412 * the ancestors to maintain a consistent state.
1414 __isl_give isl_schedule_node *isl_schedule_node_graft_tree(
1415 __isl_take isl_schedule_node *pos, __isl_take isl_schedule_tree *tree)
1417 if (!tree || !pos)
1418 goto error;
1419 if (pos->tree == tree) {
1420 isl_schedule_tree_free(tree);
1421 return pos;
1424 pos = isl_schedule_node_cow(pos);
1425 if (!pos)
1426 goto error;
1428 isl_schedule_tree_free(pos->tree);
1429 pos->tree = tree;
1431 return update_ancestors(pos);
1432 error:
1433 isl_schedule_node_free(pos);
1434 isl_schedule_tree_free(tree);
1435 return NULL;
1438 /* Make sure we can insert a node between "node" and its parent.
1439 * Return -1 on error, reporting the reason why we cannot insert a node.
1441 static int check_insert(__isl_keep isl_schedule_node *node)
1443 int has_parent;
1444 enum isl_schedule_node_type type;
1446 has_parent = isl_schedule_node_has_parent(node);
1447 if (has_parent < 0)
1448 return -1;
1449 if (!has_parent)
1450 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1451 "cannot insert node outside of root", return -1);
1453 type = isl_schedule_node_get_parent_type(node);
1454 if (type == isl_schedule_node_error)
1455 return -1;
1456 if (type == isl_schedule_node_set || type == isl_schedule_node_sequence)
1457 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1458 "cannot insert node between set or sequence node "
1459 "and its filter children", return -1);
1461 return 0;
1464 /* Insert a band node with partial schedule "mupa" between "node" and
1465 * its parent.
1466 * Return a pointer to the new band node.
1468 __isl_give isl_schedule_node *isl_schedule_node_insert_partial_schedule(
1469 __isl_take isl_schedule_node *node,
1470 __isl_take isl_multi_union_pw_aff *mupa)
1472 isl_schedule_band *band;
1473 isl_schedule_tree *tree;
1475 if (check_insert(node) < 0)
1476 node = isl_schedule_node_free(node);
1478 tree = isl_schedule_node_get_tree(node);
1479 band = isl_schedule_band_from_multi_union_pw_aff(mupa);
1480 tree = isl_schedule_tree_insert_band(tree, band);
1481 node = isl_schedule_node_graft_tree(node, tree);
1483 return node;
1486 /* Insert a filter node with filter "filter" between "node" and its parent.
1487 * Return a pointer to the new filter node.
1489 __isl_give isl_schedule_node *isl_schedule_node_insert_filter(
1490 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
1492 isl_schedule_tree *tree;
1494 if (check_insert(node) < 0)
1495 node = isl_schedule_node_free(node);
1497 tree = isl_schedule_node_get_tree(node);
1498 tree = isl_schedule_tree_insert_filter(tree, filter);
1499 node = isl_schedule_node_graft_tree(node, tree);
1501 return node;
1504 /* Attach the current subtree of "node" to a sequence of filter tree nodes
1505 * with filters described by "filters", attach this sequence
1506 * of filter tree nodes as children to a new tree of type "type" and
1507 * replace the original subtree of "node" by this new tree.
1509 static __isl_give isl_schedule_node *isl_schedule_node_insert_children(
1510 __isl_take isl_schedule_node *node,
1511 enum isl_schedule_node_type type,
1512 __isl_take isl_union_set_list *filters)
1514 int i, n;
1515 isl_ctx *ctx;
1516 isl_schedule_tree *tree;
1517 isl_schedule_tree_list *list;
1519 if (check_insert(node) < 0)
1520 node = isl_schedule_node_free(node);
1522 if (!node || !filters)
1523 goto error;
1525 ctx = isl_schedule_node_get_ctx(node);
1526 n = isl_union_set_list_n_union_set(filters);
1527 list = isl_schedule_tree_list_alloc(ctx, n);
1528 for (i = 0; i < n; ++i) {
1529 isl_schedule_tree *tree;
1530 isl_union_set *filter;
1532 tree = isl_schedule_node_get_tree(node);
1533 filter = isl_union_set_list_get_union_set(filters, i);
1534 tree = isl_schedule_tree_insert_filter(tree, filter);
1535 list = isl_schedule_tree_list_add(list, tree);
1537 tree = isl_schedule_tree_from_children(type, list);
1538 node = isl_schedule_node_graft_tree(node, tree);
1540 isl_union_set_list_free(filters);
1541 return node;
1542 error:
1543 isl_union_set_list_free(filters);
1544 isl_schedule_node_free(node);
1545 return NULL;
1548 /* Insert a sequence node with child filters "filters" between "node" and
1549 * its parent. That is, the tree that "node" points to is attached
1550 * to each of the child nodes of the filter nodes.
1551 * Return a pointer to the new sequence node.
1553 __isl_give isl_schedule_node *isl_schedule_node_insert_sequence(
1554 __isl_take isl_schedule_node *node,
1555 __isl_take isl_union_set_list *filters)
1557 return isl_schedule_node_insert_children(node,
1558 isl_schedule_node_sequence, filters);
1561 /* Insert a set node with child filters "filters" between "node" and
1562 * its parent. That is, the tree that "node" points to is attached
1563 * to each of the child nodes of the filter nodes.
1564 * Return a pointer to the new set node.
1566 __isl_give isl_schedule_node *isl_schedule_node_insert_set(
1567 __isl_take isl_schedule_node *node,
1568 __isl_take isl_union_set_list *filters)
1570 return isl_schedule_node_insert_children(node,
1571 isl_schedule_node_set, filters);
1574 /* Reset the user pointer on all identifiers of parameters and tuples
1575 * in the schedule node "node".
1577 __isl_give isl_schedule_node *isl_schedule_node_reset_user(
1578 __isl_take isl_schedule_node *node)
1580 isl_schedule_tree *tree;
1582 tree = isl_schedule_node_get_tree(node);
1583 tree = isl_schedule_tree_reset_user(tree);
1584 node = isl_schedule_node_graft_tree(node, tree);
1586 return node;
1589 /* Align the parameters of the schedule node "node" to those of "space".
1591 __isl_give isl_schedule_node *isl_schedule_node_align_params(
1592 __isl_take isl_schedule_node *node, __isl_take isl_space *space)
1594 isl_schedule_tree *tree;
1596 tree = isl_schedule_node_get_tree(node);
1597 tree = isl_schedule_tree_align_params(tree, space);
1598 node = isl_schedule_node_graft_tree(node, tree);
1600 return node;
1603 /* Print "node" to "p".
1605 __isl_give isl_printer *isl_printer_print_schedule_node(
1606 __isl_take isl_printer *p, __isl_keep isl_schedule_node *node)
1608 if (!node)
1609 return isl_printer_free(p);
1610 return isl_printer_print_schedule_tree_mark(p, node->schedule->root,
1611 isl_schedule_tree_list_n_schedule_tree(node->ancestors),
1612 node->child_pos);
1615 void isl_schedule_node_dump(__isl_keep isl_schedule_node *node)
1617 isl_ctx *ctx;
1618 isl_printer *printer;
1620 if (!node)
1621 return;
1623 ctx = isl_schedule_node_get_ctx(node);
1624 printer = isl_printer_to_file(ctx, stderr);
1625 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
1626 printer = isl_printer_print_schedule_node(printer, node);
1628 isl_printer_free(printer);