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
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
)
27 isl_schedule_node
*node
;
30 if (!schedule
|| !tree
|| !ancestors
)
32 n
= isl_schedule_tree_list_n_schedule_tree(ancestors
);
33 if (n
> 0 && !child_pos
)
35 ctx
= isl_schedule_get_ctx(schedule
);
36 node
= isl_calloc_type(ctx
, isl_schedule_node
);
40 node
->schedule
= schedule
;
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
];
51 isl_schedule_free(schedule
);
52 isl_schedule_tree_free(tree
);
53 isl_schedule_tree_list_free(ancestors
);
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
);
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"
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
)
119 isl_schedule_tree
*parent
;
120 enum isl_schedule_node_type type
;
123 return isl_schedule_node_error
;
124 has_parent
= isl_schedule_node_has_parent(node
);
126 return isl_schedule_node_error
;
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
);
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
)
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
)
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
)
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
),
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
)
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
)
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
)
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
);
224 /* Do "node1" and "node2" point to the same position in the same
227 int isl_schedule_node_is_equal(__isl_keep isl_schedule_node
*node1
,
228 __isl_keep isl_schedule_node
*node2
)
232 if (!node1
|| !node2
)
236 if (node1
->schedule
!= node2
->schedule
)
239 n1
= isl_schedule_node_get_tree_depth(node1
);
240 n2
= isl_schedule_node_get_tree_depth(node2
);
243 for (i
= 0; i
< n1
; ++i
)
244 if (node1
->child_pos
[i
] != node2
->child_pos
[i
])
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
)
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(
271 if (tree
->type
== isl_schedule_node_band
)
272 depth
+= isl_schedule_tree_band_n_member(tree
);
273 isl_schedule_tree_free(tree
);
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
{
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
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
);
321 case isl_schedule_node_error
:
323 case isl_schedule_node_leaf
:
324 case isl_schedule_node_sequence
:
325 case isl_schedule_node_set
:
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
;
332 case isl_schedule_node_band
:
333 if (isl_schedule_tree_band_n_member(tree
) == 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
,
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
;
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
;
354 if ((data
->collect_prefix
&& !data
->prefix
) || !data
->filter
)
357 data
->initialized
= 1;
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
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
);
381 case isl_schedule_node_error
:
383 case isl_schedule_node_domain
:
384 case isl_schedule_node_leaf
:
385 case isl_schedule_node_sequence
:
386 case isl_schedule_node_set
:
388 case isl_schedule_node_band
:
389 if (isl_schedule_tree_band_n_member(tree
) == 0)
391 if (!data
->collect_prefix
)
393 mupa
= isl_schedule_tree_band_get_partial_schedule(tree
);
394 data
->prefix
= isl_multi_union_pw_aff_flat_range_product(mupa
,
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
);
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
)
430 data
->initialized
= 0;
436 n
= isl_schedule_tree_list_n_schedule_tree(list
);
437 for (i
= n
- 1; i
>= 0; --i
) {
438 isl_schedule_tree
*tree
;
441 tree
= isl_schedule_tree_list_get_schedule_tree(list
, i
);
444 if (!data
->initialized
)
445 r
= collect_filter_prefix_init(tree
, data
);
447 r
= collect_filter_prefix_update(tree
, data
);
448 isl_schedule_tree_free(tree
);
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
)
477 isl_union_pw_multi_aff
*prefix
;
478 struct isl_schedule_node_get_filter_prefix_data data
;
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
);
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
);
501 isl_union_pw_multi_aff_from_multi_union_pw_aff(data
.prefix
);
502 prefix
= isl_union_pw_multi_aff_intersect_domain(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
;
539 if (node
->tree
== node
->schedule
->root
) {
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;
550 if (collect_filter_prefix(node
->ancestors
, &data
) < 0)
551 data
.filter
= isl_union_set_free(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
;
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
);
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
);
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
)
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
)
602 if (!node
->ancestors
)
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
)
617 has_parent
= isl_schedule_node_has_parent(node
);
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
)
638 has_parent
= isl_schedule_node_has_parent(node
);
639 if (has_parent
< 0 || !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
)
654 isl_schedule_tree
*tree
;
658 has_parent
= isl_schedule_node_has_parent(node
);
659 if (has_parent
< 0 || !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);
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
678 int isl_schedule_node_has_children(__isl_keep isl_schedule_node
*node
)
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
)
702 if (isl_schedule_tree_is_leaf(node
->tree
))
705 n
= isl_schedule_tree_n_children(node
->tree
);
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
)
720 isl_schedule_tree
*tree
;
726 n
= isl_schedule_node_get_tree_depth(node
);
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
);
737 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
739 isl_schedule_tree_free(node
->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
);
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
)
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
)
772 n
= isl_schedule_node_get_tree_depth(node
);
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
)
786 isl_schedule_tree
*tree
;
789 node
= isl_schedule_node_cow(node
);
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);
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
));
808 if (isl_schedule_tree_has_children(tree
))
809 tree
= isl_schedule_tree_get_child(tree
, pos
);
811 tree
= isl_schedule_node_get_leaf(node
);
812 isl_schedule_tree_free(node
->tree
);
815 if (!node
->tree
|| !node
->ancestors
)
816 return isl_schedule_node_free(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
)
837 isl_schedule_tree
*parent
, *tree
;
839 node
= isl_schedule_node_cow(node
);
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
,
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
);
857 return isl_schedule_node_free(node
);
858 isl_schedule_tree_free(node
->tree
);
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
)
871 isl_schedule_tree
*parent
, *tree
;
873 node
= isl_schedule_node_cow(node
);
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
,
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
);
891 return isl_schedule_node_free(node
);
892 isl_schedule_tree_free(node
->tree
);
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
),
925 depth
= isl_schedule_node_get_tree_depth(node
);
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
);
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
);
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
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
;
972 r
= data
->fn(node
, data
->user
);
974 return isl_schedule_node_free(node
);
977 } while (isl_schedule_node_has_children(node
) &&
978 (node
= isl_schedule_node_first_child(node
)) != NULL
);
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
)
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
,
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
);
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
)
1089 n
= isl_schedule_node_get_tree_depth(node
);
1090 for (i
= 0; i
< n
; ++i
) {
1091 isl_schedule_node
*ancestor
;
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
);
1105 /* Is any node in the subtree rooted at "node" anchored?
1106 * That is, do any of these nodes reference the outer band nodes?
1108 int isl_schedule_node_is_subtree_anchored(__isl_keep isl_schedule_node
*node
)
1112 return isl_schedule_tree_is_subtree_anchored(node
->tree
);
1115 /* Return the number of members in the given band node.
1117 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node
*node
)
1119 return node
? isl_schedule_tree_band_n_member(node
->tree
) : 0;
1122 /* Is the band member at position "pos" of the band node "node"
1123 * marked coincident?
1125 int isl_schedule_node_band_member_get_coincident(
1126 __isl_keep isl_schedule_node
*node
, int pos
)
1130 return isl_schedule_tree_band_member_get_coincident(node
->tree
, pos
);
1133 /* Mark the band member at position "pos" the band node "node"
1134 * as being coincident or not according to "coincident".
1136 __isl_give isl_schedule_node
*isl_schedule_node_band_member_set_coincident(
1137 __isl_take isl_schedule_node
*node
, int pos
, int coincident
)
1140 isl_schedule_tree
*tree
;
1144 c
= isl_schedule_node_band_member_get_coincident(node
, pos
);
1145 if (c
== coincident
)
1148 tree
= isl_schedule_tree_copy(node
->tree
);
1149 tree
= isl_schedule_tree_band_member_set_coincident(tree
, pos
,
1151 node
= isl_schedule_node_graft_tree(node
, tree
);
1156 /* Is the band node "node" marked permutable?
1158 int isl_schedule_node_band_get_permutable(__isl_keep isl_schedule_node
*node
)
1163 return isl_schedule_tree_band_get_permutable(node
->tree
);
1166 /* Mark the band node "node" permutable or not according to "permutable"?
1168 __isl_give isl_schedule_node
*isl_schedule_node_band_set_permutable(
1169 __isl_take isl_schedule_node
*node
, int permutable
)
1171 isl_schedule_tree
*tree
;
1175 if (isl_schedule_node_band_get_permutable(node
) == permutable
)
1178 tree
= isl_schedule_tree_copy(node
->tree
);
1179 tree
= isl_schedule_tree_band_set_permutable(tree
, permutable
);
1180 node
= isl_schedule_node_graft_tree(node
, tree
);
1185 /* Return the schedule space of the band node.
1187 __isl_give isl_space
*isl_schedule_node_band_get_space(
1188 __isl_keep isl_schedule_node
*node
)
1193 return isl_schedule_tree_band_get_space(node
->tree
);
1196 /* Return the schedule of the band node in isolation.
1198 __isl_give isl_multi_union_pw_aff
*isl_schedule_node_band_get_partial_schedule(
1199 __isl_keep isl_schedule_node
*node
)
1204 return isl_schedule_tree_band_get_partial_schedule(node
->tree
);
1207 /* Return the schedule of the band node in isolation in the form of
1210 * If the band does not have any members, then we construct a universe map
1211 * with the universe of the domain elements reaching the node as domain.
1212 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1213 * convert that to an isl_union_map.
1215 __isl_give isl_union_map
*isl_schedule_node_band_get_partial_schedule_union_map(
1216 __isl_keep isl_schedule_node
*node
)
1218 isl_multi_union_pw_aff
*mupa
;
1223 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
1224 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1225 "not a band node", return NULL
);
1226 if (isl_schedule_node_band_n_member(node
) == 0) {
1227 isl_union_set
*domain
;
1229 domain
= isl_schedule_node_get_universe_domain(node
);
1230 return isl_union_map_from_domain(domain
);
1233 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
1234 return isl_union_map_from_multi_union_pw_aff(mupa
);
1237 /* Return the loop AST generation type for the band member of band node "node"
1238 * at position "pos".
1240 enum isl_ast_loop_type
isl_schedule_node_band_member_get_ast_loop_type(
1241 __isl_keep isl_schedule_node
*node
, int pos
)
1244 return isl_ast_loop_error
;
1246 return isl_schedule_tree_band_member_get_ast_loop_type(node
->tree
, pos
);
1249 /* Set the loop AST generation type for the band member of band node "node"
1250 * at position "pos" to "type".
1252 __isl_give isl_schedule_node
*isl_schedule_node_band_member_set_ast_loop_type(
1253 __isl_take isl_schedule_node
*node
, int pos
,
1254 enum isl_ast_loop_type type
)
1256 isl_schedule_tree
*tree
;
1261 tree
= isl_schedule_tree_copy(node
->tree
);
1262 tree
= isl_schedule_tree_band_member_set_ast_loop_type(tree
, pos
, type
);
1263 return isl_schedule_node_graft_tree(node
, tree
);
1266 /* Return the loop AST generation type for the band member of band node "node"
1267 * at position "pos" for the isolated part.
1269 enum isl_ast_loop_type
isl_schedule_node_band_member_get_isolate_ast_loop_type(
1270 __isl_keep isl_schedule_node
*node
, int pos
)
1273 return isl_ast_loop_error
;
1275 return isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1279 /* Set the loop AST generation type for the band member of band node "node"
1280 * at position "pos" for the isolated part to "type".
1282 __isl_give isl_schedule_node
*
1283 isl_schedule_node_band_member_set_isolate_ast_loop_type(
1284 __isl_take isl_schedule_node
*node
, int pos
,
1285 enum isl_ast_loop_type type
)
1287 isl_schedule_tree
*tree
;
1292 tree
= isl_schedule_tree_copy(node
->tree
);
1293 tree
= isl_schedule_tree_band_member_set_isolate_ast_loop_type(tree
,
1295 return isl_schedule_node_graft_tree(node
, tree
);
1298 /* Return the AST build options associated to band node "node".
1300 __isl_give isl_union_set
*isl_schedule_node_band_get_ast_build_options(
1301 __isl_keep isl_schedule_node
*node
)
1306 return isl_schedule_tree_band_get_ast_build_options(node
->tree
);
1309 /* Replace the AST build options associated to band node "node" by "options".
1311 __isl_give isl_schedule_node
*isl_schedule_node_band_set_ast_build_options(
1312 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*options
)
1314 isl_schedule_tree
*tree
;
1316 if (!node
|| !options
)
1319 tree
= isl_schedule_tree_copy(node
->tree
);
1320 tree
= isl_schedule_tree_band_set_ast_build_options(tree
, options
);
1321 return isl_schedule_node_graft_tree(node
, tree
);
1323 isl_schedule_node_free(node
);
1324 isl_union_set_free(options
);
1328 /* Make sure that that spaces of "node" and "mv" are the same.
1329 * Return -1 on error, reporting the error to the user.
1331 static int check_space_multi_val(__isl_keep isl_schedule_node
*node
,
1332 __isl_keep isl_multi_val
*mv
)
1334 isl_space
*node_space
, *mv_space
;
1337 node_space
= isl_schedule_node_band_get_space(node
);
1338 mv_space
= isl_multi_val_get_space(mv
);
1339 equal
= isl_space_tuple_is_equal(node_space
, isl_dim_set
,
1340 mv_space
, isl_dim_set
);
1341 isl_space_free(mv_space
);
1342 isl_space_free(node_space
);
1346 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1347 "spaces don't match", return -1);
1352 /* Multiply the partial schedule of the band node "node"
1353 * with the factors in "mv".
1355 __isl_give isl_schedule_node
*isl_schedule_node_band_scale(
1356 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1358 isl_schedule_tree
*tree
;
1363 if (check_space_multi_val(node
, mv
) < 0)
1365 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1369 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1370 "cannot scale band node with anchored subtree",
1373 tree
= isl_schedule_node_get_tree(node
);
1374 tree
= isl_schedule_tree_band_scale(tree
, mv
);
1375 return isl_schedule_node_graft_tree(node
, tree
);
1377 isl_multi_val_free(mv
);
1378 isl_schedule_node_free(node
);
1382 /* Divide the partial schedule of the band node "node"
1383 * by the factors in "mv".
1385 __isl_give isl_schedule_node
*isl_schedule_node_band_scale_down(
1386 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1388 isl_schedule_tree
*tree
;
1393 if (check_space_multi_val(node
, mv
) < 0)
1395 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1399 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1400 "cannot scale down band node with anchored subtree",
1403 tree
= isl_schedule_node_get_tree(node
);
1404 tree
= isl_schedule_tree_band_scale_down(tree
, mv
);
1405 return isl_schedule_node_graft_tree(node
, tree
);
1407 isl_multi_val_free(mv
);
1408 isl_schedule_node_free(node
);
1412 /* Tile "node" with tile sizes "sizes".
1414 * The current node is replaced by two nested nodes corresponding
1415 * to the tile dimensions and the point dimensions.
1417 * Return a pointer to the outer (tile) node.
1419 * If any of the descendants of "node" depend on the set of outer band nodes,
1420 * then we refuse to tile the node.
1422 * If the scale tile loops option is set, then the tile loops
1423 * are scaled by the tile sizes. If the shift point loops option is set,
1424 * then the point loops are shifted to start at zero.
1425 * In particular, these options affect the tile and point loop schedules
1428 * scale shift original tile point
1430 * 0 0 i floor(i/s) i
1431 * 1 0 i s * floor(i/s) i
1432 * 0 1 i floor(i/s) i - s * floor(i/s)
1433 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1435 __isl_give isl_schedule_node
*isl_schedule_node_band_tile(
1436 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
1438 isl_schedule_tree
*tree
;
1441 if (!node
|| !sizes
)
1443 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1447 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1448 "cannot tile band node with anchored subtree",
1451 if (check_space_multi_val(node
, sizes
) < 0)
1454 tree
= isl_schedule_node_get_tree(node
);
1455 tree
= isl_schedule_tree_band_tile(tree
, sizes
);
1456 return isl_schedule_node_graft_tree(node
, tree
);
1458 isl_multi_val_free(sizes
);
1459 isl_schedule_node_free(node
);
1463 /* Move the band node "node" down to all the leaves in the subtree
1465 * Return a pointer to the node in the resulting tree that is in the same
1466 * position as the node pointed to by "node" in the original tree.
1468 * If the node only has a leaf child, then nothing needs to be done.
1469 * Otherwise, the child of the node is removed and the result is
1470 * appended to all the leaves in the subtree rooted at the original child.
1471 * The original node is then replaced by the result of this operation.
1473 * If any of the nodes in the subtree rooted at "node" depend on
1474 * the set of outer band nodes then we refuse to sink the band node.
1476 __isl_give isl_schedule_node
*isl_schedule_node_band_sink(
1477 __isl_take isl_schedule_node
*node
)
1479 enum isl_schedule_node_type type
;
1480 isl_schedule_tree
*tree
, *child
;
1486 type
= isl_schedule_node_get_type(node
);
1487 if (type
!= isl_schedule_node_band
)
1488 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1489 "not a band node", isl_schedule_node_free(node
));
1490 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1492 return isl_schedule_node_free(node
);
1494 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1495 "cannot sink band node in anchored subtree",
1496 isl_schedule_node_free(node
));
1497 if (isl_schedule_tree_n_children(node
->tree
) == 0)
1500 tree
= isl_schedule_node_get_tree(node
);
1501 child
= isl_schedule_tree_get_child(tree
, 0);
1502 tree
= isl_schedule_tree_reset_children(tree
);
1503 tree
= isl_schedule_tree_append_to_leaves(child
, tree
);
1505 return isl_schedule_node_graft_tree(node
, tree
);
1508 /* Split "node" into two nested band nodes, one with the first "pos"
1509 * dimensions and one with the remaining dimensions.
1510 * The schedules of the two band nodes live in anonymous spaces.
1512 __isl_give isl_schedule_node
*isl_schedule_node_band_split(
1513 __isl_take isl_schedule_node
*node
, int pos
)
1515 isl_schedule_tree
*tree
;
1517 tree
= isl_schedule_node_get_tree(node
);
1518 tree
= isl_schedule_tree_band_split(tree
, pos
);
1519 return isl_schedule_node_graft_tree(node
, tree
);
1522 /* Return the domain of the domain node "node".
1524 __isl_give isl_union_set
*isl_schedule_node_domain_get_domain(
1525 __isl_keep isl_schedule_node
*node
)
1530 return isl_schedule_tree_domain_get_domain(node
->tree
);
1533 /* Return the filter of the filter node "node".
1535 __isl_give isl_union_set
*isl_schedule_node_filter_get_filter(
1536 __isl_keep isl_schedule_node
*node
)
1541 return isl_schedule_tree_filter_get_filter(node
->tree
);
1544 /* Replace the filter of filter node "node" by "filter".
1546 __isl_give isl_schedule_node
*isl_schedule_node_filter_set_filter(
1547 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
1549 isl_schedule_tree
*tree
;
1551 if (!node
|| !filter
)
1554 tree
= isl_schedule_tree_copy(node
->tree
);
1555 tree
= isl_schedule_tree_filter_set_filter(tree
, filter
);
1556 return isl_schedule_node_graft_tree(node
, tree
);
1558 isl_schedule_node_free(node
);
1559 isl_union_set_free(filter
);
1563 /* Update the ancestors of "node" to point to the tree that "node"
1565 * That is, replace the child in the original parent that corresponds
1566 * to the current tree position by node->tree and continue updating
1567 * the ancestors in the same way until the root is reached.
1569 * If "node" originally points to a leaf of the schedule tree, then make sure
1570 * that in the end it points to a leaf in the updated schedule tree.
1572 static __isl_give isl_schedule_node
*update_ancestors(
1573 __isl_take isl_schedule_node
*node
)
1578 isl_schedule_tree
*tree
;
1580 node
= isl_schedule_node_cow(node
);
1584 ctx
= isl_schedule_node_get_ctx(node
);
1585 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1586 tree
= isl_schedule_tree_copy(node
->tree
);
1588 for (i
= n
- 1; i
>= 0; --i
) {
1589 isl_schedule_tree
*parent
;
1591 parent
= isl_schedule_tree_list_get_schedule_tree(
1592 node
->ancestors
, i
);
1593 parent
= isl_schedule_tree_replace_child(parent
,
1594 node
->child_pos
[i
], tree
);
1595 node
->ancestors
= isl_schedule_tree_list_set_schedule_tree(
1596 node
->ancestors
, i
, isl_schedule_tree_copy(parent
));
1601 is_leaf
= isl_schedule_tree_is_leaf(node
->tree
);
1602 node
->schedule
= isl_schedule_set_root(node
->schedule
, tree
);
1604 isl_schedule_tree_free(node
->tree
);
1605 node
->tree
= isl_schedule_node_get_leaf(node
);
1608 if (!node
->schedule
|| !node
->ancestors
)
1609 return isl_schedule_node_free(node
);
1614 /* Replace the subtree that "pos" points to by "tree", updating
1615 * the ancestors to maintain a consistent state.
1617 __isl_give isl_schedule_node
*isl_schedule_node_graft_tree(
1618 __isl_take isl_schedule_node
*pos
, __isl_take isl_schedule_tree
*tree
)
1622 if (pos
->tree
== tree
) {
1623 isl_schedule_tree_free(tree
);
1627 pos
= isl_schedule_node_cow(pos
);
1631 isl_schedule_tree_free(pos
->tree
);
1634 return update_ancestors(pos
);
1636 isl_schedule_node_free(pos
);
1637 isl_schedule_tree_free(tree
);
1641 /* Make sure we can insert a node between "node" and its parent.
1642 * Return -1 on error, reporting the reason why we cannot insert a node.
1644 static int check_insert(__isl_keep isl_schedule_node
*node
)
1647 enum isl_schedule_node_type type
;
1649 has_parent
= isl_schedule_node_has_parent(node
);
1653 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1654 "cannot insert node outside of root", return -1);
1656 type
= isl_schedule_node_get_parent_type(node
);
1657 if (type
== isl_schedule_node_error
)
1659 if (type
== isl_schedule_node_set
|| type
== isl_schedule_node_sequence
)
1660 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1661 "cannot insert node between set or sequence node "
1662 "and its filter children", return -1);
1667 /* Insert a band node with partial schedule "mupa" between "node" and
1669 * Return a pointer to the new band node.
1671 * If any of the nodes in the subtree rooted at "node" depend on
1672 * the set of outer band nodes then we refuse to insert the band node.
1674 __isl_give isl_schedule_node
*isl_schedule_node_insert_partial_schedule(
1675 __isl_take isl_schedule_node
*node
,
1676 __isl_take isl_multi_union_pw_aff
*mupa
)
1679 isl_schedule_band
*band
;
1680 isl_schedule_tree
*tree
;
1682 if (check_insert(node
) < 0)
1683 node
= isl_schedule_node_free(node
);
1684 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1688 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1689 "cannot insert band node in anchored subtree",
1692 tree
= isl_schedule_node_get_tree(node
);
1693 band
= isl_schedule_band_from_multi_union_pw_aff(mupa
);
1694 tree
= isl_schedule_tree_insert_band(tree
, band
);
1695 node
= isl_schedule_node_graft_tree(node
, tree
);
1699 isl_schedule_node_free(node
);
1700 isl_multi_union_pw_aff_free(mupa
);
1704 /* Insert a filter node with filter "filter" between "node" and its parent.
1705 * Return a pointer to the new filter node.
1707 __isl_give isl_schedule_node
*isl_schedule_node_insert_filter(
1708 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
1710 isl_schedule_tree
*tree
;
1712 if (check_insert(node
) < 0)
1713 node
= isl_schedule_node_free(node
);
1715 tree
= isl_schedule_node_get_tree(node
);
1716 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
1717 node
= isl_schedule_node_graft_tree(node
, tree
);
1722 /* Attach the current subtree of "node" to a sequence of filter tree nodes
1723 * with filters described by "filters", attach this sequence
1724 * of filter tree nodes as children to a new tree of type "type" and
1725 * replace the original subtree of "node" by this new tree.
1727 static __isl_give isl_schedule_node
*isl_schedule_node_insert_children(
1728 __isl_take isl_schedule_node
*node
,
1729 enum isl_schedule_node_type type
,
1730 __isl_take isl_union_set_list
*filters
)
1734 isl_schedule_tree
*tree
;
1735 isl_schedule_tree_list
*list
;
1737 if (check_insert(node
) < 0)
1738 node
= isl_schedule_node_free(node
);
1740 if (!node
|| !filters
)
1743 ctx
= isl_schedule_node_get_ctx(node
);
1744 n
= isl_union_set_list_n_union_set(filters
);
1745 list
= isl_schedule_tree_list_alloc(ctx
, n
);
1746 for (i
= 0; i
< n
; ++i
) {
1747 isl_schedule_tree
*tree
;
1748 isl_union_set
*filter
;
1750 tree
= isl_schedule_node_get_tree(node
);
1751 filter
= isl_union_set_list_get_union_set(filters
, i
);
1752 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
1753 list
= isl_schedule_tree_list_add(list
, tree
);
1755 tree
= isl_schedule_tree_from_children(type
, list
);
1756 node
= isl_schedule_node_graft_tree(node
, tree
);
1758 isl_union_set_list_free(filters
);
1761 isl_union_set_list_free(filters
);
1762 isl_schedule_node_free(node
);
1766 /* Insert a sequence node with child filters "filters" between "node" and
1767 * its parent. That is, the tree that "node" points to is attached
1768 * to each of the child nodes of the filter nodes.
1769 * Return a pointer to the new sequence node.
1771 __isl_give isl_schedule_node
*isl_schedule_node_insert_sequence(
1772 __isl_take isl_schedule_node
*node
,
1773 __isl_take isl_union_set_list
*filters
)
1775 return isl_schedule_node_insert_children(node
,
1776 isl_schedule_node_sequence
, filters
);
1779 /* Insert a set node with child filters "filters" between "node" and
1780 * its parent. That is, the tree that "node" points to is attached
1781 * to each of the child nodes of the filter nodes.
1782 * Return a pointer to the new set node.
1784 __isl_give isl_schedule_node
*isl_schedule_node_insert_set(
1785 __isl_take isl_schedule_node
*node
,
1786 __isl_take isl_union_set_list
*filters
)
1788 return isl_schedule_node_insert_children(node
,
1789 isl_schedule_node_set
, filters
);
1792 /* Remove "node" from its schedule tree and return a pointer
1793 * to the leaf at the same position in the updated schedule tree.
1795 * It is not allowed to remove the root of a schedule tree or
1796 * a child of a set or sequence node.
1798 __isl_give isl_schedule_node
*isl_schedule_node_cut(
1799 __isl_take isl_schedule_node
*node
)
1801 isl_schedule_tree
*leaf
;
1802 enum isl_schedule_node_type parent_type
;
1806 if (!isl_schedule_node_has_parent(node
))
1807 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1808 "cannot cut root", return isl_schedule_node_free(node
));
1810 parent_type
= isl_schedule_node_get_parent_type(node
);
1811 if (parent_type
== isl_schedule_node_set
||
1812 parent_type
== isl_schedule_node_sequence
)
1813 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1814 "cannot cut child of set or sequence",
1815 return isl_schedule_node_free(node
));
1817 leaf
= isl_schedule_node_get_leaf(node
);
1818 return isl_schedule_node_graft_tree(node
, leaf
);
1821 /* Remove a single node from the schedule tree, attaching the child
1822 * of "node" directly to its parent.
1823 * Return a pointer to this former child or to the leaf the position
1824 * of the original node if there was no child.
1825 * It is not allowed to remove the root of a schedule tree,
1826 * a set or sequence node, a child of a set or sequence node or
1827 * a band node with an anchored subtree.
1829 __isl_give isl_schedule_node
*isl_schedule_node_delete(
1830 __isl_take isl_schedule_node
*node
)
1833 isl_schedule_tree
*tree
;
1834 enum isl_schedule_node_type type
;
1839 if (isl_schedule_node_get_tree_depth(node
) == 0)
1840 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1841 "cannot delete root node",
1842 return isl_schedule_node_free(node
));
1843 n
= isl_schedule_node_n_children(node
);
1845 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1846 "can only delete node with a single child",
1847 return isl_schedule_node_free(node
));
1848 type
= isl_schedule_node_get_parent_type(node
);
1849 if (type
== isl_schedule_node_sequence
|| type
== isl_schedule_node_set
)
1850 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1851 "cannot delete child of set or sequence",
1852 return isl_schedule_node_free(node
));
1853 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
) {
1856 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1858 return isl_schedule_node_free(node
);
1860 isl_die(isl_schedule_node_get_ctx(node
),
1862 "cannot delete band node with anchored subtree",
1863 return isl_schedule_node_free(node
));
1866 tree
= isl_schedule_node_get_tree(node
);
1867 if (!tree
|| isl_schedule_tree_has_children(tree
)) {
1868 tree
= isl_schedule_tree_child(tree
, 0);
1870 isl_schedule_tree_free(tree
);
1871 tree
= isl_schedule_node_get_leaf(node
);
1873 node
= isl_schedule_node_graft_tree(node
, tree
);
1878 /* Compute the gist of the given band node with respect to "context".
1880 __isl_give isl_schedule_node
*isl_schedule_node_band_gist(
1881 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*context
)
1883 isl_schedule_tree
*tree
;
1885 tree
= isl_schedule_node_get_tree(node
);
1886 tree
= isl_schedule_tree_band_gist(tree
, context
);
1887 return isl_schedule_node_graft_tree(node
, tree
);
1890 /* Internal data structure for isl_schedule_node_gist.
1891 * "filters" contains an element for each outer filter node
1892 * with respect to the current position, each representing
1893 * the intersection of the previous element and the filter on the filter node.
1894 * The first element in the original context passed to isl_schedule_node_gist.
1896 struct isl_node_gist_data
{
1897 isl_union_set_list
*filters
;
1900 /* Can we finish gisting at this node?
1901 * That is, is the filter on the current filter node a subset of
1902 * the original context passed to isl_schedule_node_gist?
1904 static int gist_done(__isl_keep isl_schedule_node
*node
,
1905 struct isl_node_gist_data
*data
)
1907 isl_union_set
*filter
, *outer
;
1910 filter
= isl_schedule_node_filter_get_filter(node
);
1911 outer
= isl_union_set_list_get_union_set(data
->filters
, 0);
1912 subset
= isl_union_set_is_subset(filter
, outer
);
1913 isl_union_set_free(outer
);
1914 isl_union_set_free(filter
);
1919 /* Callback for "traverse" to enter a node and to move
1920 * to the deepest initial subtree that should be traversed
1921 * by isl_schedule_node_gist.
1923 * The "filters" list is extended by one element each time
1924 * we come across a filter node by the result of intersecting
1925 * the last element in the list with the filter on the filter node.
1927 * If the filter on the current filter node is a subset of
1928 * the original context passed to isl_schedule_node_gist,
1929 * then there is no need to go into its subtree since it cannot
1930 * be further simplified by the context. The "filters" list is
1931 * still extended for consistency, but the actual value of the
1932 * added element is immaterial since it will not be used.
1934 * Otherwise, the filter on the current filter node is replaced by
1935 * the gist of the original filter with respect to the intersection
1936 * of the original context with the intermediate filters.
1938 * If the new element in the "filters" list is empty, then no elements
1939 * can reach the descendants of the current filter node. The subtree
1940 * underneath the filter node is therefore removed.
1942 static __isl_give isl_schedule_node
*gist_enter(
1943 __isl_take isl_schedule_node
*node
, void *user
)
1945 struct isl_node_gist_data
*data
= user
;
1948 isl_union_set
*filter
, *inner
;
1952 switch (isl_schedule_node_get_type(node
)) {
1953 case isl_schedule_node_error
:
1954 return isl_schedule_node_free(node
);
1955 case isl_schedule_node_band
:
1956 case isl_schedule_node_domain
:
1957 case isl_schedule_node_leaf
:
1958 case isl_schedule_node_sequence
:
1959 case isl_schedule_node_set
:
1961 case isl_schedule_node_filter
:
1964 done
= gist_done(node
, data
);
1965 filter
= isl_schedule_node_filter_get_filter(node
);
1966 if (done
< 0 || done
) {
1967 data
->filters
= isl_union_set_list_add(data
->filters
,
1970 return isl_schedule_node_free(node
);
1973 n
= isl_union_set_list_n_union_set(data
->filters
);
1974 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
1975 filter
= isl_union_set_gist(filter
, isl_union_set_copy(inner
));
1976 node
= isl_schedule_node_filter_set_filter(node
,
1977 isl_union_set_copy(filter
));
1978 filter
= isl_union_set_intersect(filter
, inner
);
1979 empty
= isl_union_set_is_empty(filter
);
1980 data
->filters
= isl_union_set_list_add(data
->filters
, filter
);
1982 return isl_schedule_node_free(node
);
1985 node
= isl_schedule_node_child(node
, 0);
1986 node
= isl_schedule_node_cut(node
);
1987 node
= isl_schedule_node_parent(node
);
1989 } while (isl_schedule_node_has_children(node
) &&
1990 (node
= isl_schedule_node_first_child(node
)) != NULL
);
1995 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
1997 * In particular, if the current node is a filter node, then we remove
1998 * the element on the "filters" list that was added when we entered
1999 * the node. There is no need to compute any gist here, since we
2000 * already did that when we entered the node.
2002 * If the current node is a band node, then we compute the gist of
2003 * the band node with respect to the intersection of the original context
2004 * and the intermediate filters.
2006 * If the current node is a sequence or set node, then some of
2007 * the filter children may have become empty and so they are removed.
2008 * If only one child is left, then the set or sequence node along with
2009 * the single remaining child filter is removed. The filter can be
2010 * removed because the filters on a sequence or set node are supposed
2011 * to partition the incoming domain instances.
2012 * In principle, it should then be impossible for there to be zero
2013 * remaining children, but should this happen, we replace the entire
2014 * subtree with an empty filter.
2016 static __isl_give isl_schedule_node
*gist_leave(
2017 __isl_take isl_schedule_node
*node
, void *user
)
2019 struct isl_node_gist_data
*data
= user
;
2020 isl_schedule_tree
*tree
;
2022 isl_union_set
*filter
;
2024 switch (isl_schedule_node_get_type(node
)) {
2025 case isl_schedule_node_error
:
2026 return isl_schedule_node_free(node
);
2027 case isl_schedule_node_filter
:
2028 n
= isl_union_set_list_n_union_set(data
->filters
);
2029 data
->filters
= isl_union_set_list_drop(data
->filters
,
2032 case isl_schedule_node_band
:
2033 n
= isl_union_set_list_n_union_set(data
->filters
);
2034 filter
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
2035 node
= isl_schedule_node_band_gist(node
, filter
);
2037 case isl_schedule_node_set
:
2038 case isl_schedule_node_sequence
:
2039 tree
= isl_schedule_node_get_tree(node
);
2040 n
= isl_schedule_tree_n_children(tree
);
2041 for (i
= n
- 1; i
>= 0; --i
) {
2042 isl_schedule_tree
*child
;
2043 isl_union_set
*filter
;
2046 child
= isl_schedule_tree_get_child(tree
, i
);
2047 filter
= isl_schedule_tree_filter_get_filter(child
);
2048 empty
= isl_union_set_is_empty(filter
);
2049 isl_union_set_free(filter
);
2050 isl_schedule_tree_free(child
);
2052 tree
= isl_schedule_tree_free(tree
);
2054 tree
= isl_schedule_tree_drop_child(tree
, i
);
2056 n
= isl_schedule_tree_n_children(tree
);
2057 node
= isl_schedule_node_graft_tree(node
, tree
);
2059 node
= isl_schedule_node_delete(node
);
2060 node
= isl_schedule_node_delete(node
);
2061 } else if (n
== 0) {
2065 isl_union_set_list_get_union_set(data
->filters
, 0);
2066 space
= isl_union_set_get_space(filter
);
2067 isl_union_set_free(filter
);
2068 filter
= isl_union_set_empty(space
);
2069 node
= isl_schedule_node_cut(node
);
2070 node
= isl_schedule_node_insert_filter(node
, filter
);
2073 case isl_schedule_node_domain
:
2074 case isl_schedule_node_leaf
:
2081 /* Compute the gist of the subtree at "node" with respect to
2082 * the reaching domain elements in "context".
2083 * In particular, compute the gist of all band and filter nodes
2084 * in the subtree with respect to "context". Children of set or sequence
2085 * nodes that end up with an empty filter are removed completely.
2087 * We keep track of the intersection of "context" with all outer filters
2088 * of the current node within the subtree in the final element of "filters".
2089 * Initially, this list contains the single element "context" and it is
2090 * extended or shortened each time we enter or leave a filter node.
2092 __isl_give isl_schedule_node
*isl_schedule_node_gist(
2093 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*context
)
2095 struct isl_node_gist_data data
;
2097 data
.filters
= isl_union_set_list_from_union_set(context
);
2098 node
= traverse(node
, &gist_enter
, &gist_leave
, &data
);
2099 isl_union_set_list_free(data
.filters
);
2103 /* Intersect the domain of domain node "node" with "domain".
2105 * If the domain of "node" is already a subset of "domain",
2106 * then nothing needs to be changed.
2108 * Otherwise, we replace the domain of the domain node by the intersection
2109 * and simplify the subtree rooted at "node" with respect to this intersection.
2111 __isl_give isl_schedule_node
*isl_schedule_node_domain_intersect_domain(
2112 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
)
2114 isl_schedule_tree
*tree
;
2115 isl_union_set
*uset
;
2118 if (!node
|| !domain
)
2121 uset
= isl_schedule_tree_domain_get_domain(node
->tree
);
2122 is_subset
= isl_union_set_is_subset(uset
, domain
);
2123 isl_union_set_free(uset
);
2127 isl_union_set_free(domain
);
2131 tree
= isl_schedule_tree_copy(node
->tree
);
2132 uset
= isl_schedule_tree_domain_get_domain(tree
);
2133 uset
= isl_union_set_intersect(uset
, domain
);
2134 tree
= isl_schedule_tree_domain_set_domain(tree
,
2135 isl_union_set_copy(uset
));
2136 node
= isl_schedule_node_graft_tree(node
, tree
);
2138 node
= isl_schedule_node_child(node
, 0);
2139 node
= isl_schedule_node_gist(node
, uset
);
2140 node
= isl_schedule_node_parent(node
);
2144 isl_schedule_node_free(node
);
2145 isl_union_set_free(domain
);
2149 /* Reset the user pointer on all identifiers of parameters and tuples
2150 * in the schedule node "node".
2152 __isl_give isl_schedule_node
*isl_schedule_node_reset_user(
2153 __isl_take isl_schedule_node
*node
)
2155 isl_schedule_tree
*tree
;
2157 tree
= isl_schedule_node_get_tree(node
);
2158 tree
= isl_schedule_tree_reset_user(tree
);
2159 node
= isl_schedule_node_graft_tree(node
, tree
);
2164 /* Align the parameters of the schedule node "node" to those of "space".
2166 __isl_give isl_schedule_node
*isl_schedule_node_align_params(
2167 __isl_take isl_schedule_node
*node
, __isl_take isl_space
*space
)
2169 isl_schedule_tree
*tree
;
2171 tree
= isl_schedule_node_get_tree(node
);
2172 tree
= isl_schedule_tree_align_params(tree
, space
);
2173 node
= isl_schedule_node_graft_tree(node
, tree
);
2178 /* Compute the pullback of schedule node "node"
2179 * by the function represented by "upma".
2180 * In other words, plug in "upma" in the iteration domains
2181 * of schedule node "node".
2183 * Note that this is only a helper function for
2184 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
2185 * this function should not be called on a single node without also
2186 * calling it on all the other nodes.
2188 __isl_give isl_schedule_node
*isl_schedule_node_pullback_union_pw_multi_aff(
2189 __isl_take isl_schedule_node
*node
,
2190 __isl_take isl_union_pw_multi_aff
*upma
)
2192 isl_schedule_tree
*tree
;
2194 tree
= isl_schedule_node_get_tree(node
);
2195 tree
= isl_schedule_tree_pullback_union_pw_multi_aff(tree
, upma
);
2196 node
= isl_schedule_node_graft_tree(node
, tree
);
2201 /* Return the position of the subtree containing "node" among the children
2202 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
2203 * In particular, both nodes should point to the same schedule tree.
2205 * Return -1 on error.
2207 int isl_schedule_node_get_ancestor_child_position(
2208 __isl_keep isl_schedule_node
*node
,
2209 __isl_keep isl_schedule_node
*ancestor
)
2212 isl_schedule_tree
*tree
;
2214 if (!node
|| !ancestor
)
2217 if (node
->schedule
!= ancestor
->schedule
)
2218 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2219 "not a descendant", return -1);
2221 n1
= isl_schedule_node_get_tree_depth(ancestor
);
2222 n2
= isl_schedule_node_get_tree_depth(node
);
2225 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2226 "not a descendant", return -1);
2227 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, n1
);
2228 isl_schedule_tree_free(tree
);
2229 if (tree
!= ancestor
->tree
)
2230 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2231 "not a descendant", return -1);
2233 return node
->child_pos
[n1
];
2236 /* Given two nodes that point to the same schedule tree, return their
2237 * closest shared ancestor.
2239 * Since the two nodes point to the same schedule, they share at least
2240 * one ancestor, the root of the schedule. We move down from the root
2241 * to the first ancestor where the respective children have a different
2242 * child position. This is the requested ancestor.
2243 * If there is no ancestor where the children have a different position,
2244 * then one node is an ancestor of the other and then this node is
2245 * the requested ancestor.
2247 __isl_give isl_schedule_node
*isl_schedule_node_get_shared_ancestor(
2248 __isl_keep isl_schedule_node
*node1
,
2249 __isl_keep isl_schedule_node
*node2
)
2253 if (!node1
|| !node2
)
2255 if (node1
->schedule
!= node2
->schedule
)
2256 isl_die(isl_schedule_node_get_ctx(node1
), isl_error_invalid
,
2257 "not part of same schedule", return NULL
);
2258 n1
= isl_schedule_node_get_tree_depth(node1
);
2259 n2
= isl_schedule_node_get_tree_depth(node2
);
2261 return isl_schedule_node_get_shared_ancestor(node2
, node1
);
2263 return isl_schedule_node_copy(node1
);
2264 if (isl_schedule_node_is_equal(node1
, node2
))
2265 return isl_schedule_node_copy(node1
);
2267 for (i
= 0; i
< n1
; ++i
)
2268 if (node1
->child_pos
[i
] != node2
->child_pos
[i
])
2271 node1
= isl_schedule_node_copy(node1
);
2272 return isl_schedule_node_ancestor(node1
, n1
- i
);
2275 /* Print "node" to "p".
2277 __isl_give isl_printer
*isl_printer_print_schedule_node(
2278 __isl_take isl_printer
*p
, __isl_keep isl_schedule_node
*node
)
2281 return isl_printer_free(p
);
2282 return isl_printer_print_schedule_tree_mark(p
, node
->schedule
->root
,
2283 isl_schedule_tree_list_n_schedule_tree(node
->ancestors
),
2287 void isl_schedule_node_dump(__isl_keep isl_schedule_node
*node
)
2290 isl_printer
*printer
;
2295 ctx
= isl_schedule_node_get_ctx(node
);
2296 printer
= isl_printer_to_file(ctx
, stderr
);
2297 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
2298 printer
= isl_printer_print_schedule_node(printer
, node
);
2300 isl_printer_free(printer
);