2 * Copyright 2013-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege,
9 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
10 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
11 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl/space.h>
18 #include <isl_schedule_band.h>
19 #include <isl_schedule_private.h>
20 #include <isl_schedule_node_private.h>
22 /* Create a new schedule node in the given schedule, point at the given
23 * tree with given ancestors and child positions.
24 * "child_pos" may be NULL if there are no ancestors.
26 __isl_give isl_schedule_node
*isl_schedule_node_alloc(
27 __isl_take isl_schedule
*schedule
, __isl_take isl_schedule_tree
*tree
,
28 __isl_take isl_schedule_tree_list
*ancestors
, int *child_pos
)
31 isl_schedule_node
*node
;
34 if (!schedule
|| !tree
|| !ancestors
)
36 n
= isl_schedule_tree_list_n_schedule_tree(ancestors
);
37 if (n
> 0 && !child_pos
)
39 ctx
= isl_schedule_get_ctx(schedule
);
40 node
= isl_calloc_type(ctx
, isl_schedule_node
);
44 node
->schedule
= schedule
;
46 node
->ancestors
= ancestors
;
47 node
->child_pos
= isl_alloc_array(ctx
, int, n
);
48 if (n
&& !node
->child_pos
)
49 return isl_schedule_node_free(node
);
50 for (i
= 0; i
< n
; ++i
)
51 node
->child_pos
[i
] = child_pos
[i
];
55 isl_schedule_free(schedule
);
56 isl_schedule_tree_free(tree
);
57 isl_schedule_tree_list_free(ancestors
);
61 /* Return a pointer to the root of a schedule tree with as single
62 * node a domain node with the given domain.
64 __isl_give isl_schedule_node
*isl_schedule_node_from_domain(
65 __isl_take isl_union_set
*domain
)
67 isl_schedule
*schedule
;
68 isl_schedule_node
*node
;
70 schedule
= isl_schedule_from_domain(domain
);
71 node
= isl_schedule_get_root(schedule
);
72 isl_schedule_free(schedule
);
77 /* Return a pointer to the root of a schedule tree with as single
78 * node a extension node with the given extension.
80 __isl_give isl_schedule_node
*isl_schedule_node_from_extension(
81 __isl_take isl_union_map
*extension
)
84 isl_schedule
*schedule
;
85 isl_schedule_tree
*tree
;
86 isl_schedule_node
*node
;
91 ctx
= isl_union_map_get_ctx(extension
);
92 tree
= isl_schedule_tree_from_extension(extension
);
93 schedule
= isl_schedule_from_schedule_tree(ctx
, tree
);
94 node
= isl_schedule_get_root(schedule
);
95 isl_schedule_free(schedule
);
100 /* Return the isl_ctx to which "node" belongs.
102 isl_ctx
*isl_schedule_node_get_ctx(__isl_keep isl_schedule_node
*node
)
104 return node
? isl_schedule_get_ctx(node
->schedule
) : NULL
;
107 /* Return a pointer to the leaf of the schedule into which "node" points.
109 __isl_keep isl_schedule_tree
*isl_schedule_node_peek_leaf(
110 __isl_keep isl_schedule_node
*node
)
112 return node
? isl_schedule_peek_leaf(node
->schedule
) : NULL
;
115 /* Return a copy of the leaf of the schedule into which "node" points.
117 __isl_give isl_schedule_tree
*isl_schedule_node_get_leaf(
118 __isl_keep isl_schedule_node
*node
)
120 return isl_schedule_tree_copy(isl_schedule_node_peek_leaf(node
));
123 /* Return the type of the node or isl_schedule_node_error on error.
125 enum isl_schedule_node_type
isl_schedule_node_get_type(
126 __isl_keep isl_schedule_node
*node
)
128 return node
? isl_schedule_tree_get_type(node
->tree
)
129 : isl_schedule_node_error
;
132 /* Return the type of the parent of "node" or isl_schedule_node_error on error.
134 enum isl_schedule_node_type
isl_schedule_node_get_parent_type(
135 __isl_keep isl_schedule_node
*node
)
139 isl_schedule_tree
*parent
;
140 enum isl_schedule_node_type type
;
143 return isl_schedule_node_error
;
144 has_parent
= isl_schedule_node_has_parent(node
);
146 return isl_schedule_node_error
;
148 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
149 "node has no parent", return isl_schedule_node_error
);
151 pos
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
) - 1;
152 parent
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, pos
);
153 type
= isl_schedule_tree_get_type(parent
);
154 isl_schedule_tree_free(parent
);
159 /* Return a copy of the subtree that this node points to.
161 __isl_give isl_schedule_tree
*isl_schedule_node_get_tree(
162 __isl_keep isl_schedule_node
*node
)
167 return isl_schedule_tree_copy(node
->tree
);
170 /* Return a copy of the schedule into which "node" points.
172 __isl_give isl_schedule
*isl_schedule_node_get_schedule(
173 __isl_keep isl_schedule_node
*node
)
177 return isl_schedule_copy(node
->schedule
);
180 /* Return a fresh copy of "node".
182 __isl_take isl_schedule_node
*isl_schedule_node_dup(
183 __isl_keep isl_schedule_node
*node
)
188 return isl_schedule_node_alloc(isl_schedule_copy(node
->schedule
),
189 isl_schedule_tree_copy(node
->tree
),
190 isl_schedule_tree_list_copy(node
->ancestors
),
194 /* Return an isl_schedule_node that is equal to "node" and that has only
195 * a single reference.
197 __isl_give isl_schedule_node
*isl_schedule_node_cow(
198 __isl_take isl_schedule_node
*node
)
206 return isl_schedule_node_dup(node
);
209 /* Return a new reference to "node".
211 __isl_give isl_schedule_node
*isl_schedule_node_copy(
212 __isl_keep isl_schedule_node
*node
)
221 /* Free "node" and return NULL.
223 __isl_null isl_schedule_node
*isl_schedule_node_free(
224 __isl_take isl_schedule_node
*node
)
231 isl_schedule_tree_list_free(node
->ancestors
);
232 free(node
->child_pos
);
233 isl_schedule_tree_free(node
->tree
);
234 isl_schedule_free(node
->schedule
);
240 /* Do "node1" and "node2" point to the same position in the same
243 isl_bool
isl_schedule_node_is_equal(__isl_keep isl_schedule_node
*node1
,
244 __isl_keep isl_schedule_node
*node2
)
248 if (!node1
|| !node2
)
249 return isl_bool_error
;
251 return isl_bool_true
;
252 if (node1
->schedule
!= node2
->schedule
)
253 return isl_bool_false
;
255 n1
= isl_schedule_node_get_tree_depth(node1
);
256 n2
= isl_schedule_node_get_tree_depth(node2
);
258 return isl_bool_false
;
259 for (i
= 0; i
< n1
; ++i
)
260 if (node1
->child_pos
[i
] != node2
->child_pos
[i
])
261 return isl_bool_false
;
263 return isl_bool_true
;
266 /* Return the number of outer schedule dimensions of "node"
267 * in its schedule tree.
269 * Return -1 on error.
271 int isl_schedule_node_get_schedule_depth(__isl_keep isl_schedule_node
*node
)
279 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
280 for (i
= n
- 1; i
>= 0; --i
) {
281 isl_schedule_tree
*tree
;
283 tree
= isl_schedule_tree_list_get_schedule_tree(
287 if (tree
->type
== isl_schedule_node_band
)
288 depth
+= isl_schedule_tree_band_n_member(tree
);
289 isl_schedule_tree_free(tree
);
295 /* Internal data structure for
296 * isl_schedule_node_get_prefix_schedule_union_pw_multi_aff
298 * "initialized" is set if the filter field has been initialized.
299 * If "universe_domain" is not set, then the collected filter is intersected
300 * with the domain of the root domain node.
301 * "universe_filter" is set if we are only collecting the universes of filters
302 * "collect_prefix" is set if we are collecting prefixes.
303 * "filter" collects all outer filters and is NULL until "initialized" is set.
304 * "prefix" collects all outer band partial schedules (if "collect_prefix"
305 * is set). If it is used, then it is initialized by the caller
306 * of collect_filter_prefix to a zero-dimensional function.
308 struct isl_schedule_node_get_filter_prefix_data
{
313 isl_union_set
*filter
;
314 isl_multi_union_pw_aff
*prefix
;
317 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list
*list
,
318 int n
, struct isl_schedule_node_get_filter_prefix_data
*data
);
320 /* Update the filter and prefix information in "data" based on the first "n"
321 * elements in "list" and the expansion tree root "tree".
323 * We first collect the information from the elements in "list",
324 * initializing the filter based on the domain of the expansion.
325 * Then we map the results to the expanded space and combined them
326 * with the results already in "data".
328 static int collect_filter_prefix_expansion(__isl_take isl_schedule_tree
*tree
,
329 __isl_keep isl_schedule_tree_list
*list
, int n
,
330 struct isl_schedule_node_get_filter_prefix_data
*data
)
332 struct isl_schedule_node_get_filter_prefix_data contracted
;
333 isl_union_pw_multi_aff
*c
;
334 isl_union_map
*exp
, *universe
;
335 isl_union_set
*filter
;
337 c
= isl_schedule_tree_expansion_get_contraction(tree
);
338 exp
= isl_schedule_tree_expansion_get_expansion(tree
);
340 contracted
.initialized
= 1;
341 contracted
.universe_domain
= data
->universe_domain
;
342 contracted
.universe_filter
= data
->universe_filter
;
343 contracted
.collect_prefix
= data
->collect_prefix
;
344 universe
= isl_union_map_universe(isl_union_map_copy(exp
));
345 filter
= isl_union_map_domain(universe
);
346 if (data
->collect_prefix
) {
347 isl_space
*space
= isl_union_set_get_space(filter
);
348 space
= isl_space_set_from_params(space
);
349 contracted
.prefix
= isl_multi_union_pw_aff_zero(space
);
351 contracted
.filter
= filter
;
353 if (collect_filter_prefix(list
, n
, &contracted
) < 0)
354 contracted
.filter
= isl_union_set_free(contracted
.filter
);
355 if (data
->collect_prefix
) {
356 isl_multi_union_pw_aff
*prefix
;
358 prefix
= contracted
.prefix
;
360 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
361 isl_union_pw_multi_aff_copy(c
));
362 data
->prefix
= isl_multi_union_pw_aff_flat_range_product(
363 prefix
, data
->prefix
);
365 filter
= contracted
.filter
;
366 if (data
->universe_domain
)
367 filter
= isl_union_set_preimage_union_pw_multi_aff(filter
,
368 isl_union_pw_multi_aff_copy(c
));
370 filter
= isl_union_set_apply(filter
, isl_union_map_copy(exp
));
371 if (!data
->initialized
)
372 data
->filter
= filter
;
374 data
->filter
= isl_union_set_intersect(filter
, data
->filter
);
375 data
->initialized
= 1;
377 isl_union_pw_multi_aff_free(c
);
378 isl_union_map_free(exp
);
379 isl_schedule_tree_free(tree
);
384 /* Update the filter information in "data" based on the first "n"
385 * elements in "list" and the extension tree root "tree", in case
386 * data->universe_domain is set and data->collect_prefix is not.
388 * We collect the universe domain of the elements in "list" and
389 * add it to the universe range of the extension (intersected
390 * with the already collected filter, if any).
392 static int collect_universe_domain_extension(__isl_take isl_schedule_tree
*tree
,
393 __isl_keep isl_schedule_tree_list
*list
, int n
,
394 struct isl_schedule_node_get_filter_prefix_data
*data
)
396 struct isl_schedule_node_get_filter_prefix_data data_outer
;
397 isl_union_map
*extension
;
398 isl_union_set
*filter
;
400 data_outer
.initialized
= 0;
401 data_outer
.universe_domain
= 1;
402 data_outer
.universe_filter
= data
->universe_filter
;
403 data_outer
.collect_prefix
= 0;
404 data_outer
.filter
= NULL
;
405 data_outer
.prefix
= NULL
;
407 if (collect_filter_prefix(list
, n
, &data_outer
) < 0)
408 data_outer
.filter
= isl_union_set_free(data_outer
.filter
);
410 extension
= isl_schedule_tree_extension_get_extension(tree
);
411 extension
= isl_union_map_universe(extension
);
412 filter
= isl_union_map_range(extension
);
413 if (data_outer
.initialized
)
414 filter
= isl_union_set_union(filter
, data_outer
.filter
);
415 if (data
->initialized
)
416 filter
= isl_union_set_intersect(filter
, data
->filter
);
418 data
->filter
= filter
;
420 isl_schedule_tree_free(tree
);
425 /* Update "data" based on the tree node "tree" in case "data" has
426 * not been initialized yet.
428 * Return 0 on success and -1 on error.
430 * If "tree" is a filter, then we set data->filter to this filter
432 * If "tree" is a domain, then this means we have reached the root
433 * of the schedule tree without being able to extract any information.
434 * We therefore initialize data->filter to the universe of the domain,
435 * or the domain itself if data->universe_domain is not set.
436 * If "tree" is a band with at least one member, then we set data->filter
437 * to the universe of the schedule domain and replace the zero-dimensional
438 * data->prefix by the band schedule (if data->collect_prefix is set).
440 static int collect_filter_prefix_init(__isl_keep isl_schedule_tree
*tree
,
441 struct isl_schedule_node_get_filter_prefix_data
*data
)
443 enum isl_schedule_node_type type
;
444 isl_multi_union_pw_aff
*mupa
;
445 isl_union_set
*filter
;
447 type
= isl_schedule_tree_get_type(tree
);
449 case isl_schedule_node_error
:
451 case isl_schedule_node_expansion
:
452 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
453 "should be handled by caller", return -1);
454 case isl_schedule_node_extension
:
455 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
456 "cannot handle extension nodes", return -1);
457 case isl_schedule_node_context
:
458 case isl_schedule_node_leaf
:
459 case isl_schedule_node_guard
:
460 case isl_schedule_node_mark
:
461 case isl_schedule_node_sequence
:
462 case isl_schedule_node_set
:
464 case isl_schedule_node_domain
:
465 filter
= isl_schedule_tree_domain_get_domain(tree
);
466 if (data
->universe_domain
)
467 filter
= isl_union_set_universe(filter
);
468 data
->filter
= filter
;
470 case isl_schedule_node_band
:
471 if (isl_schedule_tree_band_n_member(tree
) == 0)
473 mupa
= isl_schedule_tree_band_get_partial_schedule(tree
);
474 if (data
->collect_prefix
) {
475 isl_multi_union_pw_aff_free(data
->prefix
);
476 mupa
= isl_multi_union_pw_aff_reset_tuple_id(mupa
,
478 data
->prefix
= isl_multi_union_pw_aff_copy(mupa
);
480 filter
= isl_multi_union_pw_aff_domain(mupa
);
481 filter
= isl_union_set_universe(filter
);
482 data
->filter
= filter
;
484 case isl_schedule_node_filter
:
485 filter
= isl_schedule_tree_filter_get_filter(tree
);
486 if (data
->universe_filter
)
487 filter
= isl_union_set_universe(filter
);
488 data
->filter
= filter
;
492 if ((data
->collect_prefix
&& !data
->prefix
) || !data
->filter
)
495 data
->initialized
= 1;
500 /* Update "data" based on the tree node "tree" in case "data" has
501 * already been initialized.
503 * Return 0 on success and -1 on error.
505 * If "tree" is a domain and data->universe_domain is not set, then
506 * intersect data->filter with the domain.
507 * If "tree" is a filter, then we intersect data->filter with this filter
509 * If "tree" is a band with at least one member and data->collect_prefix
510 * is set, then we extend data->prefix with the band schedule.
511 * If "tree" is an extension, then we make sure that we are not collecting
512 * information on any extended domain elements.
514 static int collect_filter_prefix_update(__isl_keep isl_schedule_tree
*tree
,
515 struct isl_schedule_node_get_filter_prefix_data
*data
)
517 enum isl_schedule_node_type type
;
518 isl_multi_union_pw_aff
*mupa
;
519 isl_union_set
*filter
;
520 isl_union_map
*extension
;
523 type
= isl_schedule_tree_get_type(tree
);
525 case isl_schedule_node_error
:
527 case isl_schedule_node_expansion
:
528 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
529 "should be handled by caller", return -1);
530 case isl_schedule_node_extension
:
531 extension
= isl_schedule_tree_extension_get_extension(tree
);
532 extension
= isl_union_map_intersect_range(extension
,
533 isl_union_set_copy(data
->filter
));
534 empty
= isl_union_map_is_empty(extension
);
535 isl_union_map_free(extension
);
540 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
541 "cannot handle extension nodes", return -1);
542 case isl_schedule_node_context
:
543 case isl_schedule_node_leaf
:
544 case isl_schedule_node_guard
:
545 case isl_schedule_node_mark
:
546 case isl_schedule_node_sequence
:
547 case isl_schedule_node_set
:
549 case isl_schedule_node_domain
:
550 if (data
->universe_domain
)
552 filter
= isl_schedule_tree_domain_get_domain(tree
);
553 data
->filter
= isl_union_set_intersect(data
->filter
, filter
);
555 case isl_schedule_node_band
:
556 if (isl_schedule_tree_band_n_member(tree
) == 0)
558 if (!data
->collect_prefix
)
560 mupa
= isl_schedule_tree_band_get_partial_schedule(tree
);
561 data
->prefix
= isl_multi_union_pw_aff_flat_range_product(mupa
,
566 case isl_schedule_node_filter
:
567 filter
= isl_schedule_tree_filter_get_filter(tree
);
568 if (data
->universe_filter
)
569 filter
= isl_union_set_universe(filter
);
570 data
->filter
= isl_union_set_intersect(data
->filter
, filter
);
579 /* Collect filter and/or prefix information from the first "n"
580 * elements in "list" (which represent the ancestors of a node).
581 * Store the results in "data".
583 * Extension nodes are only supported if they do not affect the outcome,
584 * i.e., if we are collecting information on non-extended domain elements,
585 * or if we are collecting the universe domain (without prefix).
587 * Return 0 on success and -1 on error.
589 * We traverse the list from innermost ancestor (last element)
590 * to outermost ancestor (first element), calling collect_filter_prefix_init
591 * on each node as long as we have not been able to extract any information
592 * yet and collect_filter_prefix_update afterwards.
593 * If we come across an expansion node, then we interrupt the traversal
594 * and call collect_filter_prefix_expansion to restart the traversal
595 * over the remaining ancestors and to combine the results with those
596 * that have already been collected.
597 * If we come across an extension node and we are only computing
598 * the universe domain, then we interrupt the traversal and call
599 * collect_universe_domain_extension to restart the traversal
600 * over the remaining ancestors and to combine the results with those
601 * that have already been collected.
602 * On successful return, data->initialized will be set since the outermost
603 * ancestor is a domain node, which always results in an initialization.
605 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list
*list
,
606 int n
, struct isl_schedule_node_get_filter_prefix_data
*data
)
613 for (i
= n
- 1; i
>= 0; --i
) {
614 isl_schedule_tree
*tree
;
615 enum isl_schedule_node_type type
;
618 tree
= isl_schedule_tree_list_get_schedule_tree(list
, i
);
621 type
= isl_schedule_tree_get_type(tree
);
622 if (type
== isl_schedule_node_expansion
)
623 return collect_filter_prefix_expansion(tree
, list
, i
,
625 if (type
== isl_schedule_node_extension
&&
626 data
->universe_domain
&& !data
->collect_prefix
)
627 return collect_universe_domain_extension(tree
, list
, i
,
629 if (!data
->initialized
)
630 r
= collect_filter_prefix_init(tree
, data
);
632 r
= collect_filter_prefix_update(tree
, data
);
633 isl_schedule_tree_free(tree
);
641 /* Return the concatenation of the partial schedules of all outer band
642 * nodes of "node" interesected with all outer filters
643 * as an isl_multi_union_pw_aff.
644 * None of the ancestors of "node" may be an extension node, unless
645 * there is also a filter ancestor that filters out all the extended
648 * If "node" is pointing at the root of the schedule tree, then
649 * there are no domain elements reaching the current node, so
650 * we return an empty result.
652 * We collect all the filters and partial schedules in collect_filter_prefix
653 * and intersect the domain of the combined schedule with the combined filter.
655 __isl_give isl_multi_union_pw_aff
*
656 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(
657 __isl_keep isl_schedule_node
*node
)
661 struct isl_schedule_node_get_filter_prefix_data data
;
666 space
= isl_schedule_get_space(node
->schedule
);
667 space
= isl_space_set_from_params(space
);
668 if (node
->tree
== node
->schedule
->root
)
669 return isl_multi_union_pw_aff_zero(space
);
671 data
.initialized
= 0;
672 data
.universe_domain
= 1;
673 data
.universe_filter
= 0;
674 data
.collect_prefix
= 1;
676 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
678 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
679 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
680 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
682 data
.prefix
= isl_multi_union_pw_aff_intersect_domain(data
.prefix
,
688 /* Return the concatenation of the partial schedules of all outer band
689 * nodes of "node" interesected with all outer filters
690 * as an isl_union_pw_multi_aff.
691 * None of the ancestors of "node" may be an extension node, unless
692 * there is also a filter ancestor that filters out all the extended
695 * If "node" is pointing at the root of the schedule tree, then
696 * there are no domain elements reaching the current node, so
697 * we return an empty result.
699 * We collect all the filters and partial schedules in collect_filter_prefix.
700 * The partial schedules are collected as an isl_multi_union_pw_aff.
701 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
702 * contain any domain information, so we construct the isl_union_pw_multi_aff
703 * result as a zero-dimensional function on the collected filter.
704 * Otherwise, we convert the isl_multi_union_pw_aff to
705 * an isl_multi_union_pw_aff and intersect the domain with the filter.
707 __isl_give isl_union_pw_multi_aff
*
708 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
709 __isl_keep isl_schedule_node
*node
)
713 isl_union_pw_multi_aff
*prefix
;
714 struct isl_schedule_node_get_filter_prefix_data data
;
719 space
= isl_schedule_get_space(node
->schedule
);
720 if (node
->tree
== node
->schedule
->root
)
721 return isl_union_pw_multi_aff_empty(space
);
723 space
= isl_space_set_from_params(space
);
724 data
.initialized
= 0;
725 data
.universe_domain
= 1;
726 data
.universe_filter
= 0;
727 data
.collect_prefix
= 1;
729 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
731 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
732 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
733 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
736 isl_multi_union_pw_aff_dim(data
.prefix
, isl_dim_set
) == 0) {
737 isl_multi_union_pw_aff_free(data
.prefix
);
738 prefix
= isl_union_pw_multi_aff_from_domain(data
.filter
);
741 isl_union_pw_multi_aff_from_multi_union_pw_aff(data
.prefix
);
742 prefix
= isl_union_pw_multi_aff_intersect_domain(prefix
,
749 /* Return the concatenation of the partial schedules of all outer band
750 * nodes of "node" interesected with all outer filters
751 * as an isl_union_map.
753 __isl_give isl_union_map
*isl_schedule_node_get_prefix_schedule_union_map(
754 __isl_keep isl_schedule_node
*node
)
756 isl_union_pw_multi_aff
*upma
;
758 upma
= isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
759 return isl_union_map_from_union_pw_multi_aff(upma
);
762 /* Return the concatenation of the partial schedules of all outer band
763 * nodes of "node" intersected with all outer domain constraints.
764 * None of the ancestors of "node" may be an extension node, unless
765 * there is also a filter ancestor that filters out all the extended
768 * Essentially, this function intersects the domain of the output
769 * of isl_schedule_node_get_prefix_schedule_union_map with the output
770 * of isl_schedule_node_get_domain, except that it only traverses
771 * the ancestors of "node" once.
773 __isl_give isl_union_map
*isl_schedule_node_get_prefix_schedule_relation(
774 __isl_keep isl_schedule_node
*node
)
778 isl_union_map
*prefix
;
779 struct isl_schedule_node_get_filter_prefix_data data
;
784 space
= isl_schedule_get_space(node
->schedule
);
785 if (node
->tree
== node
->schedule
->root
)
786 return isl_union_map_empty(space
);
788 space
= isl_space_set_from_params(space
);
789 data
.initialized
= 0;
790 data
.universe_domain
= 0;
791 data
.universe_filter
= 0;
792 data
.collect_prefix
= 1;
794 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
796 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
797 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
798 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
801 isl_multi_union_pw_aff_dim(data
.prefix
, isl_dim_set
) == 0) {
802 isl_multi_union_pw_aff_free(data
.prefix
);
803 prefix
= isl_union_map_from_domain(data
.filter
);
805 prefix
= isl_union_map_from_multi_union_pw_aff(data
.prefix
);
806 prefix
= isl_union_map_intersect_domain(prefix
, data
.filter
);
812 /* Return the domain elements that reach "node".
814 * If "node" is pointing at the root of the schedule tree, then
815 * there are no domain elements reaching the current node, so
816 * we return an empty result.
817 * None of the ancestors of "node" may be an extension node, unless
818 * there is also a filter ancestor that filters out all the extended
821 * Otherwise, we collect all filters reaching the node,
822 * intersected with the root domain in collect_filter_prefix.
824 __isl_give isl_union_set
*isl_schedule_node_get_domain(
825 __isl_keep isl_schedule_node
*node
)
828 struct isl_schedule_node_get_filter_prefix_data data
;
833 if (node
->tree
== node
->schedule
->root
) {
836 space
= isl_schedule_get_space(node
->schedule
);
837 return isl_union_set_empty(space
);
840 data
.initialized
= 0;
841 data
.universe_domain
= 0;
842 data
.universe_filter
= 0;
843 data
.collect_prefix
= 0;
847 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
848 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
849 data
.filter
= isl_union_set_free(data
.filter
);
854 /* Return the union of universe sets of the domain elements that reach "node".
856 * If "node" is pointing at the root of the schedule tree, then
857 * there are no domain elements reaching the current node, so
858 * we return an empty result.
860 * Otherwise, we collect the universes of all filters reaching the node
861 * in collect_filter_prefix.
863 __isl_give isl_union_set
*isl_schedule_node_get_universe_domain(
864 __isl_keep isl_schedule_node
*node
)
867 struct isl_schedule_node_get_filter_prefix_data data
;
872 if (node
->tree
== node
->schedule
->root
) {
875 space
= isl_schedule_get_space(node
->schedule
);
876 return isl_union_set_empty(space
);
879 data
.initialized
= 0;
880 data
.universe_domain
= 1;
881 data
.universe_filter
= 1;
882 data
.collect_prefix
= 0;
886 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
887 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
888 data
.filter
= isl_union_set_free(data
.filter
);
893 /* Return the subtree schedule of "node".
895 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
896 * trees that do not contain any schedule information, we first
897 * move down to the first relevant descendant and handle leaves ourselves.
899 * If the subtree rooted at "node" contains any expansion nodes, then
900 * the returned subtree schedule is formulated in terms of the expanded
902 * The subtree is not allowed to contain any extension nodes.
904 __isl_give isl_union_map
*isl_schedule_node_get_subtree_schedule_union_map(
905 __isl_keep isl_schedule_node
*node
)
907 isl_schedule_tree
*tree
, *leaf
;
910 tree
= isl_schedule_node_get_tree(node
);
911 leaf
= isl_schedule_node_peek_leaf(node
);
912 tree
= isl_schedule_tree_first_schedule_descendant(tree
, leaf
);
916 isl_union_set
*domain
;
917 domain
= isl_schedule_node_get_universe_domain(node
);
918 isl_schedule_tree_free(tree
);
919 return isl_union_map_from_domain(domain
);
922 umap
= isl_schedule_tree_get_subtree_schedule_union_map(tree
);
923 isl_schedule_tree_free(tree
);
927 /* Return the number of ancestors of "node" in its schedule tree.
929 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node
*node
)
933 return isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
936 /* Does "node" have a parent?
938 * That is, does it point to any node of the schedule other than the root?
940 isl_bool
isl_schedule_node_has_parent(__isl_keep isl_schedule_node
*node
)
944 depth
= isl_schedule_node_get_tree_depth(node
);
946 return isl_bool_error
;
950 /* Return the position of "node" among the children of its parent.
952 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node
*node
)
959 has_parent
= isl_schedule_node_has_parent(node
);
963 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
964 "node has no parent", return -1);
966 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
967 return node
->child_pos
[n
- 1];
970 /* Does the parent (if any) of "node" have any children with a smaller child
971 * position than this one?
973 isl_bool
isl_schedule_node_has_previous_sibling(
974 __isl_keep isl_schedule_node
*node
)
980 return isl_bool_error
;
981 has_parent
= isl_schedule_node_has_parent(node
);
982 if (has_parent
< 0 || !has_parent
)
985 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
987 return node
->child_pos
[n
- 1] > 0;
990 /* Does the parent (if any) of "node" have any children with a greater child
991 * position than this one?
993 isl_bool
isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node
*node
)
997 isl_schedule_tree
*tree
;
1000 return isl_bool_error
;
1001 has_parent
= isl_schedule_node_has_parent(node
);
1002 if (has_parent
< 0 || !has_parent
)
1005 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1006 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, n
- 1);
1007 n_child
= isl_schedule_tree_n_children(tree
);
1008 isl_schedule_tree_free(tree
);
1010 return isl_bool_error
;
1012 return node
->child_pos
[n
- 1] + 1 < n_child
;
1015 /* Does "node" have any children?
1017 * Any node other than the leaf nodes is considered to have at least
1018 * one child, even if the corresponding isl_schedule_tree does not
1019 * have any children.
1021 isl_bool
isl_schedule_node_has_children(__isl_keep isl_schedule_node
*node
)
1024 return isl_bool_error
;
1025 return !isl_schedule_tree_is_leaf(node
->tree
);
1028 /* Return the number of children of "node"?
1030 * Any node other than the leaf nodes is considered to have at least
1031 * one child, even if the corresponding isl_schedule_tree does not
1032 * have any children. That is, the number of children of "node" is
1033 * only zero if its tree is the explicit empty tree. Otherwise,
1034 * if the isl_schedule_tree has any children, then it is equal
1035 * to the number of children of "node". If it has zero children,
1036 * then "node" still has a leaf node as child.
1038 int isl_schedule_node_n_children(__isl_keep isl_schedule_node
*node
)
1045 if (isl_schedule_tree_is_leaf(node
->tree
))
1048 n
= isl_schedule_tree_n_children(node
->tree
);
1055 /* Move the "node" pointer to the ancestor of the given generation
1056 * of the node it currently points to, where generation 0 is the node
1057 * itself and generation 1 is its parent.
1059 __isl_give isl_schedule_node
*isl_schedule_node_ancestor(
1060 __isl_take isl_schedule_node
*node
, int generation
)
1063 isl_schedule_tree
*tree
;
1067 if (generation
== 0)
1069 n
= isl_schedule_node_get_tree_depth(node
);
1071 return isl_schedule_node_free(node
);
1072 if (generation
< 0 || generation
> n
)
1073 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1074 "generation out of bounds",
1075 return isl_schedule_node_free(node
));
1076 node
= isl_schedule_node_cow(node
);
1080 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1082 isl_schedule_tree_free(node
->tree
);
1084 node
->ancestors
= isl_schedule_tree_list_drop(node
->ancestors
,
1085 n
- generation
, generation
);
1086 if (!node
->ancestors
|| !node
->tree
)
1087 return isl_schedule_node_free(node
);
1092 /* Move the "node" pointer to the parent of the node it currently points to.
1094 __isl_give isl_schedule_node
*isl_schedule_node_parent(
1095 __isl_take isl_schedule_node
*node
)
1099 if (!isl_schedule_node_has_parent(node
))
1100 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1101 "node has no parent",
1102 return isl_schedule_node_free(node
));
1103 return isl_schedule_node_ancestor(node
, 1);
1106 /* Move the "node" pointer to the root of its schedule tree.
1108 __isl_give isl_schedule_node
*isl_schedule_node_root(
1109 __isl_take isl_schedule_node
*node
)
1115 n
= isl_schedule_node_get_tree_depth(node
);
1117 return isl_schedule_node_free(node
);
1118 return isl_schedule_node_ancestor(node
, n
);
1121 /* Move the "node" pointer to the child at position "pos" of the node
1122 * it currently points to.
1124 __isl_give isl_schedule_node
*isl_schedule_node_child(
1125 __isl_take isl_schedule_node
*node
, int pos
)
1129 isl_schedule_tree
*tree
;
1132 node
= isl_schedule_node_cow(node
);
1135 if (!isl_schedule_node_has_children(node
))
1136 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1137 "node has no children",
1138 return isl_schedule_node_free(node
));
1140 ctx
= isl_schedule_node_get_ctx(node
);
1141 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1142 child_pos
= isl_realloc_array(ctx
, node
->child_pos
, int, n
+ 1);
1144 return isl_schedule_node_free(node
);
1145 node
->child_pos
= child_pos
;
1146 node
->child_pos
[n
] = pos
;
1148 node
->ancestors
= isl_schedule_tree_list_add(node
->ancestors
,
1149 isl_schedule_tree_copy(node
->tree
));
1151 if (isl_schedule_tree_has_children(tree
))
1152 tree
= isl_schedule_tree_get_child(tree
, pos
);
1154 tree
= isl_schedule_node_get_leaf(node
);
1155 isl_schedule_tree_free(node
->tree
);
1158 if (!node
->tree
|| !node
->ancestors
)
1159 return isl_schedule_node_free(node
);
1164 /* Move the "node" pointer to the first child of the node
1165 * it currently points to.
1167 __isl_give isl_schedule_node
*isl_schedule_node_first_child(
1168 __isl_take isl_schedule_node
*node
)
1170 return isl_schedule_node_child(node
, 0);
1173 /* Move the "node" pointer to the child of this node's parent in
1174 * the previous child position.
1176 __isl_give isl_schedule_node
*isl_schedule_node_previous_sibling(
1177 __isl_take isl_schedule_node
*node
)
1180 isl_schedule_tree
*parent
, *tree
;
1182 node
= isl_schedule_node_cow(node
);
1185 if (!isl_schedule_node_has_previous_sibling(node
))
1186 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1187 "node has no previous sibling",
1188 return isl_schedule_node_free(node
));
1190 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1191 parent
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1194 return isl_schedule_node_free(node
);
1195 node
->child_pos
[n
- 1]--;
1196 tree
= isl_schedule_tree_list_get_schedule_tree(parent
->children
,
1197 node
->child_pos
[n
- 1]);
1198 isl_schedule_tree_free(parent
);
1200 return isl_schedule_node_free(node
);
1201 isl_schedule_tree_free(node
->tree
);
1207 /* Move the "node" pointer to the child of this node's parent in
1208 * the next child position.
1210 __isl_give isl_schedule_node
*isl_schedule_node_next_sibling(
1211 __isl_take isl_schedule_node
*node
)
1214 isl_schedule_tree
*parent
, *tree
;
1216 node
= isl_schedule_node_cow(node
);
1219 if (!isl_schedule_node_has_next_sibling(node
))
1220 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1221 "node has no next sibling",
1222 return isl_schedule_node_free(node
));
1224 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1225 parent
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1228 return isl_schedule_node_free(node
);
1229 node
->child_pos
[n
- 1]++;
1230 tree
= isl_schedule_tree_list_get_schedule_tree(parent
->children
,
1231 node
->child_pos
[n
- 1]);
1232 isl_schedule_tree_free(parent
);
1234 return isl_schedule_node_free(node
);
1235 isl_schedule_tree_free(node
->tree
);
1241 /* Return a copy to the child at position "pos" of "node".
1243 __isl_give isl_schedule_node
*isl_schedule_node_get_child(
1244 __isl_keep isl_schedule_node
*node
, int pos
)
1246 return isl_schedule_node_child(isl_schedule_node_copy(node
), pos
);
1249 /* Traverse the descendant of "node" in depth-first order, including
1250 * "node" itself. Call "enter" whenever a node is entered and "leave"
1251 * whenever a node is left. The callback "enter" is responsible
1252 * for moving to the deepest initial subtree of its argument that
1253 * should be traversed.
1255 static __isl_give isl_schedule_node
*traverse(
1256 __isl_take isl_schedule_node
*node
,
1257 __isl_give isl_schedule_node
*(*enter
)(
1258 __isl_take isl_schedule_node
*node
, void *user
),
1259 __isl_give isl_schedule_node
*(*leave
)(
1260 __isl_take isl_schedule_node
*node
, void *user
),
1268 depth
= isl_schedule_node_get_tree_depth(node
);
1270 node
= enter(node
, user
);
1271 node
= leave(node
, user
);
1272 while (node
&& isl_schedule_node_get_tree_depth(node
) > depth
&&
1273 !isl_schedule_node_has_next_sibling(node
)) {
1274 node
= isl_schedule_node_parent(node
);
1275 node
= leave(node
, user
);
1277 if (node
&& isl_schedule_node_get_tree_depth(node
) > depth
)
1278 node
= isl_schedule_node_next_sibling(node
);
1279 } while (node
&& isl_schedule_node_get_tree_depth(node
) > depth
);
1284 /* Internal data structure for isl_schedule_node_foreach_descendant_top_down.
1286 * "fn" is the user-specified callback function.
1287 * "user" is the user-specified argument for the callback.
1289 struct isl_schedule_node_preorder_data
{
1290 isl_bool (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
);
1294 /* Callback for "traverse" to enter a node and to move
1295 * to the deepest initial subtree that should be traversed
1296 * for use in a preorder visit.
1298 * If the user callback returns a negative value, then we abort
1299 * the traversal. If this callback returns zero, then we skip
1300 * the subtree rooted at the current node. Otherwise, we move
1301 * down to the first child and repeat the process until a leaf
1304 static __isl_give isl_schedule_node
*preorder_enter(
1305 __isl_take isl_schedule_node
*node
, void *user
)
1307 struct isl_schedule_node_preorder_data
*data
= user
;
1315 r
= data
->fn(node
, data
->user
);
1317 return isl_schedule_node_free(node
);
1318 if (r
== isl_bool_false
)
1320 } while (isl_schedule_node_has_children(node
) &&
1321 (node
= isl_schedule_node_first_child(node
)) != NULL
);
1326 /* Callback for "traverse" to leave a node
1327 * for use in a preorder visit.
1328 * Since we already visited the node when we entered it,
1329 * we do not need to do anything here.
1331 static __isl_give isl_schedule_node
*preorder_leave(
1332 __isl_take isl_schedule_node
*node
, void *user
)
1337 /* Traverse the descendants of "node" (including the node itself)
1338 * in depth first preorder.
1340 * If "fn" returns isl_bool_error on any of the nodes,
1341 * then the traversal is aborted.
1342 * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1343 * at that node is skipped.
1345 * Return isl_stat_ok on success and isl_stat_error on failure.
1347 isl_stat
isl_schedule_node_foreach_descendant_top_down(
1348 __isl_keep isl_schedule_node
*node
,
1349 isl_bool (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
),
1352 struct isl_schedule_node_preorder_data data
= { fn
, user
};
1354 node
= isl_schedule_node_copy(node
);
1355 node
= traverse(node
, &preorder_enter
, &preorder_leave
, &data
);
1356 isl_schedule_node_free(node
);
1358 return node
? isl_stat_ok
: isl_stat_error
;
1361 /* Internal data structure for isl_schedule_node_every_descendant.
1363 * "test" is the user-specified callback function.
1364 * "user" is the user-specified callback function argument.
1366 * "failed" is initialized to 0 and set to 1 if "test" fails
1369 struct isl_union_map_every_data
{
1370 isl_bool (*test
)(__isl_keep isl_schedule_node
*node
, void *user
);
1375 /* isl_schedule_node_foreach_descendant_top_down callback
1376 * that sets data->failed if data->test returns false and
1377 * subsequently aborts the traversal.
1379 static isl_bool
call_every(__isl_keep isl_schedule_node
*node
, void *user
)
1381 struct isl_union_map_every_data
*data
= user
;
1384 r
= data
->test(node
, data
->user
);
1386 return isl_bool_error
;
1388 return isl_bool_true
;
1390 return isl_bool_error
;
1393 /* Does "test" succeed on every descendant of "node" (including "node" itself)?
1395 isl_bool
isl_schedule_node_every_descendant(__isl_keep isl_schedule_node
*node
,
1396 isl_bool (*test
)(__isl_keep isl_schedule_node
*node
, void *user
),
1399 struct isl_union_map_every_data data
= { test
, user
, 0 };
1402 r
= isl_schedule_node_foreach_descendant_top_down(node
, &call_every
,
1405 return isl_bool_true
;
1407 return isl_bool_false
;
1408 return isl_bool_error
;
1411 /* Internal data structure for isl_schedule_node_map_descendant_bottom_up.
1413 * "fn" is the user-specified callback function.
1414 * "user" is the user-specified argument for the callback.
1416 struct isl_schedule_node_postorder_data
{
1417 __isl_give isl_schedule_node
*(*fn
)(__isl_take isl_schedule_node
*node
,
1422 /* Callback for "traverse" to enter a node and to move
1423 * to the deepest initial subtree that should be traversed
1424 * for use in a postorder visit.
1426 * Since we are performing a postorder visit, we only need
1427 * to move to the deepest initial leaf here.
1429 static __isl_give isl_schedule_node
*postorder_enter(
1430 __isl_take isl_schedule_node
*node
, void *user
)
1432 while (node
&& isl_schedule_node_has_children(node
))
1433 node
= isl_schedule_node_first_child(node
);
1438 /* Callback for "traverse" to leave a node
1439 * for use in a postorder visit.
1441 * Since we are performing a postorder visit, we need
1442 * to call the user callback here.
1444 static __isl_give isl_schedule_node
*postorder_leave(
1445 __isl_take isl_schedule_node
*node
, void *user
)
1447 struct isl_schedule_node_postorder_data
*data
= user
;
1449 return data
->fn(node
, data
->user
);
1452 /* Traverse the descendants of "node" (including the node itself)
1453 * in depth first postorder, allowing the user to modify the visited node.
1454 * The traversal continues from the node returned by the callback function.
1455 * It is the responsibility of the user to ensure that this does not
1456 * lead to an infinite loop. It is safest to always return a pointer
1457 * to the same position (same ancestors and child positions) as the input node.
1459 __isl_give isl_schedule_node
*isl_schedule_node_map_descendant_bottom_up(
1460 __isl_take isl_schedule_node
*node
,
1461 __isl_give isl_schedule_node
*(*fn
)(__isl_take isl_schedule_node
*node
,
1462 void *user
), void *user
)
1464 struct isl_schedule_node_postorder_data data
= { fn
, user
};
1466 return traverse(node
, &postorder_enter
, &postorder_leave
, &data
);
1469 /* Traverse the ancestors of "node" from the root down to and including
1470 * the parent of "node", calling "fn" on each of them.
1472 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1474 * Return 0 on success and -1 on failure.
1476 isl_stat
isl_schedule_node_foreach_ancestor_top_down(
1477 __isl_keep isl_schedule_node
*node
,
1478 isl_stat (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
),
1484 return isl_stat_error
;
1486 n
= isl_schedule_node_get_tree_depth(node
);
1487 for (i
= 0; i
< n
; ++i
) {
1488 isl_schedule_node
*ancestor
;
1491 ancestor
= isl_schedule_node_copy(node
);
1492 ancestor
= isl_schedule_node_ancestor(ancestor
, n
- i
);
1493 r
= fn(ancestor
, user
);
1494 isl_schedule_node_free(ancestor
);
1496 return isl_stat_error
;
1502 /* Is any node in the subtree rooted at "node" anchored?
1503 * That is, do any of these nodes reference the outer band nodes?
1505 isl_bool
isl_schedule_node_is_subtree_anchored(
1506 __isl_keep isl_schedule_node
*node
)
1509 return isl_bool_error
;
1510 return isl_schedule_tree_is_subtree_anchored(node
->tree
);
1513 /* Return the number of members in the given band node.
1515 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node
*node
)
1517 return node
? isl_schedule_tree_band_n_member(node
->tree
) : 0;
1520 /* Is the band member at position "pos" of the band node "node"
1521 * marked coincident?
1523 isl_bool
isl_schedule_node_band_member_get_coincident(
1524 __isl_keep isl_schedule_node
*node
, int pos
)
1527 return isl_bool_error
;
1528 return isl_schedule_tree_band_member_get_coincident(node
->tree
, pos
);
1531 /* Mark the band member at position "pos" the band node "node"
1532 * as being coincident or not according to "coincident".
1534 __isl_give isl_schedule_node
*isl_schedule_node_band_member_set_coincident(
1535 __isl_take isl_schedule_node
*node
, int pos
, int coincident
)
1538 isl_schedule_tree
*tree
;
1542 c
= isl_schedule_node_band_member_get_coincident(node
, pos
);
1543 if (c
== coincident
)
1546 tree
= isl_schedule_tree_copy(node
->tree
);
1547 tree
= isl_schedule_tree_band_member_set_coincident(tree
, pos
,
1549 node
= isl_schedule_node_graft_tree(node
, tree
);
1554 /* Is the band node "node" marked permutable?
1556 isl_bool
isl_schedule_node_band_get_permutable(
1557 __isl_keep isl_schedule_node
*node
)
1560 return isl_bool_error
;
1562 return isl_schedule_tree_band_get_permutable(node
->tree
);
1565 /* Mark the band node "node" permutable or not according to "permutable"?
1567 __isl_give isl_schedule_node
*isl_schedule_node_band_set_permutable(
1568 __isl_take isl_schedule_node
*node
, int permutable
)
1570 isl_schedule_tree
*tree
;
1574 if (isl_schedule_node_band_get_permutable(node
) == permutable
)
1577 tree
= isl_schedule_tree_copy(node
->tree
);
1578 tree
= isl_schedule_tree_band_set_permutable(tree
, permutable
);
1579 node
= isl_schedule_node_graft_tree(node
, tree
);
1584 /* Return the schedule space of the band node.
1586 __isl_give isl_space
*isl_schedule_node_band_get_space(
1587 __isl_keep isl_schedule_node
*node
)
1592 return isl_schedule_tree_band_get_space(node
->tree
);
1595 /* Return the schedule of the band node in isolation.
1597 __isl_give isl_multi_union_pw_aff
*isl_schedule_node_band_get_partial_schedule(
1598 __isl_keep isl_schedule_node
*node
)
1603 return isl_schedule_tree_band_get_partial_schedule(node
->tree
);
1606 /* Return the schedule of the band node in isolation in the form of
1609 * If the band does not have any members, then we construct a universe map
1610 * with the universe of the domain elements reaching the node as domain.
1611 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1612 * convert that to an isl_union_map.
1614 __isl_give isl_union_map
*isl_schedule_node_band_get_partial_schedule_union_map(
1615 __isl_keep isl_schedule_node
*node
)
1617 isl_multi_union_pw_aff
*mupa
;
1622 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
1623 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1624 "not a band node", return NULL
);
1625 if (isl_schedule_node_band_n_member(node
) == 0) {
1626 isl_union_set
*domain
;
1628 domain
= isl_schedule_node_get_universe_domain(node
);
1629 return isl_union_map_from_domain(domain
);
1632 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
1633 return isl_union_map_from_multi_union_pw_aff(mupa
);
1636 /* Return the loop AST generation type for the band member of band node "node"
1637 * at position "pos".
1639 enum isl_ast_loop_type
isl_schedule_node_band_member_get_ast_loop_type(
1640 __isl_keep isl_schedule_node
*node
, int pos
)
1643 return isl_ast_loop_error
;
1645 return isl_schedule_tree_band_member_get_ast_loop_type(node
->tree
, pos
);
1648 /* Set the loop AST generation type for the band member of band node "node"
1649 * at position "pos" to "type".
1651 __isl_give isl_schedule_node
*isl_schedule_node_band_member_set_ast_loop_type(
1652 __isl_take isl_schedule_node
*node
, int pos
,
1653 enum isl_ast_loop_type type
)
1655 isl_schedule_tree
*tree
;
1660 tree
= isl_schedule_tree_copy(node
->tree
);
1661 tree
= isl_schedule_tree_band_member_set_ast_loop_type(tree
, pos
, type
);
1662 return isl_schedule_node_graft_tree(node
, tree
);
1665 /* Return the loop AST generation type for the band member of band node "node"
1666 * at position "pos" for the isolated part.
1668 enum isl_ast_loop_type
isl_schedule_node_band_member_get_isolate_ast_loop_type(
1669 __isl_keep isl_schedule_node
*node
, int pos
)
1672 return isl_ast_loop_error
;
1674 return isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1678 /* Set the loop AST generation type for the band member of band node "node"
1679 * at position "pos" for the isolated part to "type".
1681 __isl_give isl_schedule_node
*
1682 isl_schedule_node_band_member_set_isolate_ast_loop_type(
1683 __isl_take isl_schedule_node
*node
, int pos
,
1684 enum isl_ast_loop_type type
)
1686 isl_schedule_tree
*tree
;
1691 tree
= isl_schedule_tree_copy(node
->tree
);
1692 tree
= isl_schedule_tree_band_member_set_isolate_ast_loop_type(tree
,
1694 return isl_schedule_node_graft_tree(node
, tree
);
1697 /* Return the AST build options associated to band node "node".
1699 __isl_give isl_union_set
*isl_schedule_node_band_get_ast_build_options(
1700 __isl_keep isl_schedule_node
*node
)
1705 return isl_schedule_tree_band_get_ast_build_options(node
->tree
);
1708 /* Replace the AST build options associated to band node "node" by "options".
1710 __isl_give isl_schedule_node
*isl_schedule_node_band_set_ast_build_options(
1711 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*options
)
1713 isl_schedule_tree
*tree
;
1715 if (!node
|| !options
)
1718 tree
= isl_schedule_tree_copy(node
->tree
);
1719 tree
= isl_schedule_tree_band_set_ast_build_options(tree
, options
);
1720 return isl_schedule_node_graft_tree(node
, tree
);
1722 isl_schedule_node_free(node
);
1723 isl_union_set_free(options
);
1727 /* Return the "isolate" option associated to band node "node".
1729 __isl_give isl_set
*isl_schedule_node_band_get_ast_isolate_option(
1730 __isl_keep isl_schedule_node
*node
)
1737 depth
= isl_schedule_node_get_schedule_depth(node
);
1738 return isl_schedule_tree_band_get_ast_isolate_option(node
->tree
, depth
);
1741 /* Make sure that that spaces of "node" and "mv" are the same.
1742 * Return -1 on error, reporting the error to the user.
1744 static int check_space_multi_val(__isl_keep isl_schedule_node
*node
,
1745 __isl_keep isl_multi_val
*mv
)
1747 isl_space
*node_space
, *mv_space
;
1750 node_space
= isl_schedule_node_band_get_space(node
);
1751 mv_space
= isl_multi_val_get_space(mv
);
1752 equal
= isl_space_tuple_is_equal(node_space
, isl_dim_set
,
1753 mv_space
, isl_dim_set
);
1754 isl_space_free(mv_space
);
1755 isl_space_free(node_space
);
1759 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1760 "spaces don't match", return -1);
1765 /* Multiply the partial schedule of the band node "node"
1766 * with the factors in "mv".
1768 __isl_give isl_schedule_node
*isl_schedule_node_band_scale(
1769 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1771 isl_schedule_tree
*tree
;
1776 if (check_space_multi_val(node
, mv
) < 0)
1778 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1782 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1783 "cannot scale band node with anchored subtree",
1786 tree
= isl_schedule_node_get_tree(node
);
1787 tree
= isl_schedule_tree_band_scale(tree
, mv
);
1788 return isl_schedule_node_graft_tree(node
, tree
);
1790 isl_multi_val_free(mv
);
1791 isl_schedule_node_free(node
);
1795 /* Divide the partial schedule of the band node "node"
1796 * by the factors in "mv".
1798 __isl_give isl_schedule_node
*isl_schedule_node_band_scale_down(
1799 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1801 isl_schedule_tree
*tree
;
1806 if (check_space_multi_val(node
, mv
) < 0)
1808 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1812 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1813 "cannot scale down band node with anchored subtree",
1816 tree
= isl_schedule_node_get_tree(node
);
1817 tree
= isl_schedule_tree_band_scale_down(tree
, mv
);
1818 return isl_schedule_node_graft_tree(node
, tree
);
1820 isl_multi_val_free(mv
);
1821 isl_schedule_node_free(node
);
1825 /* Reduce the partial schedule of the band node "node"
1826 * modulo the factors in "mv".
1828 __isl_give isl_schedule_node
*isl_schedule_node_band_mod(
1829 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1831 isl_schedule_tree
*tree
;
1836 if (check_space_multi_val(node
, mv
) < 0)
1838 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1842 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1843 "cannot perform mod on band node with anchored subtree",
1846 tree
= isl_schedule_node_get_tree(node
);
1847 tree
= isl_schedule_tree_band_mod(tree
, mv
);
1848 return isl_schedule_node_graft_tree(node
, tree
);
1850 isl_multi_val_free(mv
);
1851 isl_schedule_node_free(node
);
1855 /* Make sure that that spaces of "node" and "mupa" are the same.
1856 * Return isl_stat_error on error, reporting the error to the user.
1858 static isl_stat
check_space_multi_union_pw_aff(
1859 __isl_keep isl_schedule_node
*node
,
1860 __isl_keep isl_multi_union_pw_aff
*mupa
)
1862 isl_space
*node_space
, *mupa_space
;
1865 node_space
= isl_schedule_node_band_get_space(node
);
1866 mupa_space
= isl_multi_union_pw_aff_get_space(mupa
);
1867 equal
= isl_space_tuple_is_equal(node_space
, isl_dim_set
,
1868 mupa_space
, isl_dim_set
);
1869 isl_space_free(mupa_space
);
1870 isl_space_free(node_space
);
1872 return isl_stat_error
;
1874 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1875 "spaces don't match", return isl_stat_error
);
1880 /* Shift the partial schedule of the band node "node" by "shift".
1882 __isl_give isl_schedule_node
*isl_schedule_node_band_shift(
1883 __isl_take isl_schedule_node
*node
,
1884 __isl_take isl_multi_union_pw_aff
*shift
)
1886 isl_schedule_tree
*tree
;
1889 if (!node
|| !shift
)
1891 if (check_space_multi_union_pw_aff(node
, shift
) < 0)
1893 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1897 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1898 "cannot shift band node with anchored subtree",
1901 tree
= isl_schedule_node_get_tree(node
);
1902 tree
= isl_schedule_tree_band_shift(tree
, shift
);
1903 return isl_schedule_node_graft_tree(node
, tree
);
1905 isl_multi_union_pw_aff_free(shift
);
1906 isl_schedule_node_free(node
);
1910 /* Tile "node" with tile sizes "sizes".
1912 * The current node is replaced by two nested nodes corresponding
1913 * to the tile dimensions and the point dimensions.
1915 * Return a pointer to the outer (tile) node.
1917 * If any of the descendants of "node" depend on the set of outer band nodes,
1918 * then we refuse to tile the node.
1920 * If the scale tile loops option is set, then the tile loops
1921 * are scaled by the tile sizes. If the shift point loops option is set,
1922 * then the point loops are shifted to start at zero.
1923 * In particular, these options affect the tile and point loop schedules
1926 * scale shift original tile point
1928 * 0 0 i floor(i/s) i
1929 * 1 0 i s * floor(i/s) i
1930 * 0 1 i floor(i/s) i - s * floor(i/s)
1931 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1933 __isl_give isl_schedule_node
*isl_schedule_node_band_tile(
1934 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
1936 isl_schedule_tree
*tree
;
1939 if (!node
|| !sizes
)
1941 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1945 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1946 "cannot tile band node with anchored subtree",
1949 if (check_space_multi_val(node
, sizes
) < 0)
1952 tree
= isl_schedule_node_get_tree(node
);
1953 tree
= isl_schedule_tree_band_tile(tree
, sizes
);
1954 return isl_schedule_node_graft_tree(node
, tree
);
1956 isl_multi_val_free(sizes
);
1957 isl_schedule_node_free(node
);
1961 /* Move the band node "node" down to all the leaves in the subtree
1963 * Return a pointer to the node in the resulting tree that is in the same
1964 * position as the node pointed to by "node" in the original tree.
1966 * If the node only has a leaf child, then nothing needs to be done.
1967 * Otherwise, the child of the node is removed and the result is
1968 * appended to all the leaves in the subtree rooted at the original child.
1969 * Since the node is moved to the leaves, it needs to be expanded
1970 * according to the expansion, if any, defined by that subtree.
1971 * In the end, the original node is replaced by the result of
1972 * attaching copies of the expanded node to the leaves.
1974 * If any of the nodes in the subtree rooted at "node" depend on
1975 * the set of outer band nodes then we refuse to sink the band node.
1977 __isl_give isl_schedule_node
*isl_schedule_node_band_sink(
1978 __isl_take isl_schedule_node
*node
)
1980 enum isl_schedule_node_type type
;
1981 isl_schedule_tree
*tree
, *child
;
1982 isl_union_pw_multi_aff
*contraction
;
1988 type
= isl_schedule_node_get_type(node
);
1989 if (type
!= isl_schedule_node_band
)
1990 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1991 "not a band node", return isl_schedule_node_free(node
));
1992 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1994 return isl_schedule_node_free(node
);
1996 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1997 "cannot sink band node in anchored subtree",
1998 return isl_schedule_node_free(node
));
1999 if (isl_schedule_tree_n_children(node
->tree
) == 0)
2002 contraction
= isl_schedule_node_get_subtree_contraction(node
);
2004 tree
= isl_schedule_node_get_tree(node
);
2005 child
= isl_schedule_tree_get_child(tree
, 0);
2006 tree
= isl_schedule_tree_reset_children(tree
);
2007 tree
= isl_schedule_tree_pullback_union_pw_multi_aff(tree
, contraction
);
2008 tree
= isl_schedule_tree_append_to_leaves(child
, tree
);
2010 return isl_schedule_node_graft_tree(node
, tree
);
2013 /* Split "node" into two nested band nodes, one with the first "pos"
2014 * dimensions and one with the remaining dimensions.
2015 * The schedules of the two band nodes live in anonymous spaces.
2016 * The loop AST generation type options and the isolate option
2017 * are split over the two band nodes.
2019 __isl_give isl_schedule_node
*isl_schedule_node_band_split(
2020 __isl_take isl_schedule_node
*node
, int pos
)
2023 isl_schedule_tree
*tree
;
2025 depth
= isl_schedule_node_get_schedule_depth(node
);
2026 tree
= isl_schedule_node_get_tree(node
);
2027 tree
= isl_schedule_tree_band_split(tree
, pos
, depth
);
2028 return isl_schedule_node_graft_tree(node
, tree
);
2031 /* Return the context of the context node "node".
2033 __isl_give isl_set
*isl_schedule_node_context_get_context(
2034 __isl_keep isl_schedule_node
*node
)
2039 return isl_schedule_tree_context_get_context(node
->tree
);
2042 /* Return the domain of the domain node "node".
2044 __isl_give isl_union_set
*isl_schedule_node_domain_get_domain(
2045 __isl_keep isl_schedule_node
*node
)
2050 return isl_schedule_tree_domain_get_domain(node
->tree
);
2053 /* Return the expansion map of expansion node "node".
2055 __isl_give isl_union_map
*isl_schedule_node_expansion_get_expansion(
2056 __isl_keep isl_schedule_node
*node
)
2061 return isl_schedule_tree_expansion_get_expansion(node
->tree
);
2064 /* Return the contraction of expansion node "node".
2066 __isl_give isl_union_pw_multi_aff
*isl_schedule_node_expansion_get_contraction(
2067 __isl_keep isl_schedule_node
*node
)
2072 return isl_schedule_tree_expansion_get_contraction(node
->tree
);
2075 /* Replace the contraction and the expansion of the expansion node "node"
2076 * by "contraction" and "expansion".
2078 __isl_give isl_schedule_node
*
2079 isl_schedule_node_expansion_set_contraction_and_expansion(
2080 __isl_take isl_schedule_node
*node
,
2081 __isl_take isl_union_pw_multi_aff
*contraction
,
2082 __isl_take isl_union_map
*expansion
)
2084 isl_schedule_tree
*tree
;
2086 if (!node
|| !contraction
|| !expansion
)
2089 tree
= isl_schedule_tree_copy(node
->tree
);
2090 tree
= isl_schedule_tree_expansion_set_contraction_and_expansion(tree
,
2091 contraction
, expansion
);
2092 return isl_schedule_node_graft_tree(node
, tree
);
2094 isl_schedule_node_free(node
);
2095 isl_union_pw_multi_aff_free(contraction
);
2096 isl_union_map_free(expansion
);
2100 /* Return the extension of the extension node "node".
2102 __isl_give isl_union_map
*isl_schedule_node_extension_get_extension(
2103 __isl_keep isl_schedule_node
*node
)
2108 return isl_schedule_tree_extension_get_extension(node
->tree
);
2111 /* Replace the extension of extension node "node" by "extension".
2113 __isl_give isl_schedule_node
*isl_schedule_node_extension_set_extension(
2114 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
2116 isl_schedule_tree
*tree
;
2118 if (!node
|| !extension
)
2121 tree
= isl_schedule_tree_copy(node
->tree
);
2122 tree
= isl_schedule_tree_extension_set_extension(tree
, extension
);
2123 return isl_schedule_node_graft_tree(node
, tree
);
2125 isl_schedule_node_free(node
);
2126 isl_union_map_free(extension
);
2130 /* Return the filter of the filter node "node".
2132 __isl_give isl_union_set
*isl_schedule_node_filter_get_filter(
2133 __isl_keep isl_schedule_node
*node
)
2138 return isl_schedule_tree_filter_get_filter(node
->tree
);
2141 /* Replace the filter of filter node "node" by "filter".
2143 __isl_give isl_schedule_node
*isl_schedule_node_filter_set_filter(
2144 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2146 isl_schedule_tree
*tree
;
2148 if (!node
|| !filter
)
2151 tree
= isl_schedule_tree_copy(node
->tree
);
2152 tree
= isl_schedule_tree_filter_set_filter(tree
, filter
);
2153 return isl_schedule_node_graft_tree(node
, tree
);
2155 isl_schedule_node_free(node
);
2156 isl_union_set_free(filter
);
2160 /* Intersect the filter of filter node "node" with "filter".
2162 * If the filter of the node is already a subset of "filter",
2163 * then leave the node unchanged.
2165 __isl_give isl_schedule_node
*isl_schedule_node_filter_intersect_filter(
2166 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2168 isl_union_set
*node_filter
= NULL
;
2171 if (!node
|| !filter
)
2174 node_filter
= isl_schedule_node_filter_get_filter(node
);
2175 subset
= isl_union_set_is_subset(node_filter
, filter
);
2179 isl_union_set_free(node_filter
);
2180 isl_union_set_free(filter
);
2183 node_filter
= isl_union_set_intersect(node_filter
, filter
);
2184 node
= isl_schedule_node_filter_set_filter(node
, node_filter
);
2187 isl_schedule_node_free(node
);
2188 isl_union_set_free(node_filter
);
2189 isl_union_set_free(filter
);
2193 /* Return the guard of the guard node "node".
2195 __isl_give isl_set
*isl_schedule_node_guard_get_guard(
2196 __isl_keep isl_schedule_node
*node
)
2201 return isl_schedule_tree_guard_get_guard(node
->tree
);
2204 /* Return the mark identifier of the mark node "node".
2206 __isl_give isl_id
*isl_schedule_node_mark_get_id(
2207 __isl_keep isl_schedule_node
*node
)
2212 return isl_schedule_tree_mark_get_id(node
->tree
);
2215 /* Replace the child at position "pos" of the sequence node "node"
2216 * by the children of sequence root node of "tree".
2218 __isl_give isl_schedule_node
*isl_schedule_node_sequence_splice(
2219 __isl_take isl_schedule_node
*node
, int pos
,
2220 __isl_take isl_schedule_tree
*tree
)
2222 isl_schedule_tree
*node_tree
;
2226 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2227 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2228 "not a sequence node", goto error
);
2229 if (isl_schedule_tree_get_type(tree
) != isl_schedule_node_sequence
)
2230 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2231 "not a sequence node", goto error
);
2232 node_tree
= isl_schedule_node_get_tree(node
);
2233 node_tree
= isl_schedule_tree_sequence_splice(node_tree
, pos
, tree
);
2234 node
= isl_schedule_node_graft_tree(node
, node_tree
);
2238 isl_schedule_node_free(node
);
2239 isl_schedule_tree_free(tree
);
2243 /* Given a sequence node "node", with a child at position "pos" that
2244 * is also a sequence node, attach the children of that node directly
2245 * as children of "node" at that position, replacing the original child.
2247 * The filters of these children are intersected with the filter
2248 * of the child at position "pos".
2250 __isl_give isl_schedule_node
*isl_schedule_node_sequence_splice_child(
2251 __isl_take isl_schedule_node
*node
, int pos
)
2254 isl_union_set
*filter
;
2255 isl_schedule_node
*child
;
2256 isl_schedule_tree
*tree
;
2260 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2261 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2262 "not a sequence node",
2263 return isl_schedule_node_free(node
));
2264 node
= isl_schedule_node_child(node
, pos
);
2265 node
= isl_schedule_node_child(node
, 0);
2266 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2267 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2268 "not a sequence node",
2269 return isl_schedule_node_free(node
));
2270 child
= isl_schedule_node_copy(node
);
2271 node
= isl_schedule_node_parent(node
);
2272 filter
= isl_schedule_node_filter_get_filter(node
);
2273 n
= isl_schedule_node_n_children(child
);
2274 for (i
= 0; i
< n
; ++i
) {
2275 child
= isl_schedule_node_child(child
, i
);
2276 child
= isl_schedule_node_filter_intersect_filter(child
,
2277 isl_union_set_copy(filter
));
2278 child
= isl_schedule_node_parent(child
);
2280 isl_union_set_free(filter
);
2281 tree
= isl_schedule_node_get_tree(child
);
2282 isl_schedule_node_free(child
);
2283 node
= isl_schedule_node_parent(node
);
2284 node
= isl_schedule_node_sequence_splice(node
, pos
, tree
);
2289 /* Update the ancestors of "node" to point to the tree that "node"
2291 * That is, replace the child in the original parent that corresponds
2292 * to the current tree position by node->tree and continue updating
2293 * the ancestors in the same way until the root is reached.
2295 * If "fn" is not NULL, then it is called on each ancestor as we move up
2296 * the tree so that it can modify the ancestor before it is added
2297 * to the list of ancestors of the modified node.
2298 * The additional "pos" argument records the position
2299 * of the "tree" argument in the original schedule tree.
2301 * If "node" originally points to a leaf of the schedule tree, then make sure
2302 * that in the end it points to a leaf in the updated schedule tree.
2304 static __isl_give isl_schedule_node
*update_ancestors(
2305 __isl_take isl_schedule_node
*node
,
2306 __isl_give isl_schedule_tree
*(*fn
)(__isl_take isl_schedule_tree
*tree
,
2307 __isl_keep isl_schedule_node
*pos
, void *user
), void *user
)
2311 isl_schedule_tree
*tree
;
2312 isl_schedule_node
*pos
= NULL
;
2315 pos
= isl_schedule_node_copy(node
);
2317 node
= isl_schedule_node_cow(node
);
2319 return isl_schedule_node_free(pos
);
2321 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
2322 tree
= isl_schedule_tree_copy(node
->tree
);
2324 for (i
= n
- 1; i
>= 0; --i
) {
2325 isl_schedule_tree
*parent
;
2327 parent
= isl_schedule_tree_list_get_schedule_tree(
2328 node
->ancestors
, i
);
2329 parent
= isl_schedule_tree_replace_child(parent
,
2330 node
->child_pos
[i
], tree
);
2332 pos
= isl_schedule_node_parent(pos
);
2333 parent
= fn(parent
, pos
, user
);
2335 node
->ancestors
= isl_schedule_tree_list_set_schedule_tree(
2336 node
->ancestors
, i
, isl_schedule_tree_copy(parent
));
2342 isl_schedule_node_free(pos
);
2344 is_leaf
= isl_schedule_tree_is_leaf(node
->tree
);
2345 node
->schedule
= isl_schedule_set_root(node
->schedule
, tree
);
2347 isl_schedule_tree_free(node
->tree
);
2348 node
->tree
= isl_schedule_node_get_leaf(node
);
2351 if (!node
->schedule
|| !node
->ancestors
)
2352 return isl_schedule_node_free(node
);
2357 /* Replace the subtree that "pos" points to by "tree", updating
2358 * the ancestors to maintain a consistent state.
2360 __isl_give isl_schedule_node
*isl_schedule_node_graft_tree(
2361 __isl_take isl_schedule_node
*pos
, __isl_take isl_schedule_tree
*tree
)
2365 if (pos
->tree
== tree
) {
2366 isl_schedule_tree_free(tree
);
2370 pos
= isl_schedule_node_cow(pos
);
2374 isl_schedule_tree_free(pos
->tree
);
2377 return update_ancestors(pos
, NULL
, NULL
);
2379 isl_schedule_node_free(pos
);
2380 isl_schedule_tree_free(tree
);
2384 /* Make sure we can insert a node between "node" and its parent.
2385 * Return -1 on error, reporting the reason why we cannot insert a node.
2387 static int check_insert(__isl_keep isl_schedule_node
*node
)
2390 enum isl_schedule_node_type type
;
2392 has_parent
= isl_schedule_node_has_parent(node
);
2396 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2397 "cannot insert node outside of root", return -1);
2399 type
= isl_schedule_node_get_parent_type(node
);
2400 if (type
== isl_schedule_node_error
)
2402 if (type
== isl_schedule_node_set
|| type
== isl_schedule_node_sequence
)
2403 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2404 "cannot insert node between set or sequence node "
2405 "and its filter children", return -1);
2410 /* Insert a band node with partial schedule "mupa" between "node" and
2412 * Return a pointer to the new band node.
2414 * If any of the nodes in the subtree rooted at "node" depend on
2415 * the set of outer band nodes then we refuse to insert the band node.
2417 __isl_give isl_schedule_node
*isl_schedule_node_insert_partial_schedule(
2418 __isl_take isl_schedule_node
*node
,
2419 __isl_take isl_multi_union_pw_aff
*mupa
)
2422 isl_schedule_band
*band
;
2423 isl_schedule_tree
*tree
;
2425 if (check_insert(node
) < 0)
2426 node
= isl_schedule_node_free(node
);
2427 anchored
= isl_schedule_node_is_subtree_anchored(node
);
2431 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2432 "cannot insert band node in anchored subtree",
2435 tree
= isl_schedule_node_get_tree(node
);
2436 band
= isl_schedule_band_from_multi_union_pw_aff(mupa
);
2437 tree
= isl_schedule_tree_insert_band(tree
, band
);
2438 node
= isl_schedule_node_graft_tree(node
, tree
);
2442 isl_schedule_node_free(node
);
2443 isl_multi_union_pw_aff_free(mupa
);
2447 /* Insert a context node with context "context" between "node" and its parent.
2448 * Return a pointer to the new context node.
2450 __isl_give isl_schedule_node
*isl_schedule_node_insert_context(
2451 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*context
)
2453 isl_schedule_tree
*tree
;
2455 if (check_insert(node
) < 0)
2456 node
= isl_schedule_node_free(node
);
2458 tree
= isl_schedule_node_get_tree(node
);
2459 tree
= isl_schedule_tree_insert_context(tree
, context
);
2460 node
= isl_schedule_node_graft_tree(node
, tree
);
2465 /* Insert an expansion node with the given "contraction" and "expansion"
2466 * between "node" and its parent.
2467 * Return a pointer to the new expansion node.
2469 * Typically the domain and range spaces of the expansion are different.
2470 * This means that only one of them can refer to the current domain space
2471 * in a consistent tree. It is up to the caller to ensure that the tree
2472 * returns to a consistent state.
2474 __isl_give isl_schedule_node
*isl_schedule_node_insert_expansion(
2475 __isl_take isl_schedule_node
*node
,
2476 __isl_take isl_union_pw_multi_aff
*contraction
,
2477 __isl_take isl_union_map
*expansion
)
2479 isl_schedule_tree
*tree
;
2481 if (check_insert(node
) < 0)
2482 node
= isl_schedule_node_free(node
);
2484 tree
= isl_schedule_node_get_tree(node
);
2485 tree
= isl_schedule_tree_insert_expansion(tree
, contraction
, expansion
);
2486 node
= isl_schedule_node_graft_tree(node
, tree
);
2491 /* Insert an extension node with extension "extension" between "node" and
2493 * Return a pointer to the new extension node.
2495 __isl_give isl_schedule_node
*isl_schedule_node_insert_extension(
2496 __isl_take isl_schedule_node
*node
,
2497 __isl_take isl_union_map
*extension
)
2499 isl_schedule_tree
*tree
;
2501 tree
= isl_schedule_node_get_tree(node
);
2502 tree
= isl_schedule_tree_insert_extension(tree
, extension
);
2503 node
= isl_schedule_node_graft_tree(node
, tree
);
2508 /* Insert a filter node with filter "filter" between "node" and its parent.
2509 * Return a pointer to the new filter node.
2511 __isl_give isl_schedule_node
*isl_schedule_node_insert_filter(
2512 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2514 isl_schedule_tree
*tree
;
2516 if (check_insert(node
) < 0)
2517 node
= isl_schedule_node_free(node
);
2519 tree
= isl_schedule_node_get_tree(node
);
2520 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
2521 node
= isl_schedule_node_graft_tree(node
, tree
);
2526 /* Insert a guard node with guard "guard" between "node" and its parent.
2527 * Return a pointer to the new guard node.
2529 __isl_give isl_schedule_node
*isl_schedule_node_insert_guard(
2530 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*guard
)
2532 isl_schedule_tree
*tree
;
2534 if (check_insert(node
) < 0)
2535 node
= isl_schedule_node_free(node
);
2537 tree
= isl_schedule_node_get_tree(node
);
2538 tree
= isl_schedule_tree_insert_guard(tree
, guard
);
2539 node
= isl_schedule_node_graft_tree(node
, tree
);
2544 /* Insert a mark node with mark identifier "mark" between "node" and
2546 * Return a pointer to the new mark node.
2548 __isl_give isl_schedule_node
*isl_schedule_node_insert_mark(
2549 __isl_take isl_schedule_node
*node
, __isl_take isl_id
*mark
)
2551 isl_schedule_tree
*tree
;
2553 if (check_insert(node
) < 0)
2554 node
= isl_schedule_node_free(node
);
2556 tree
= isl_schedule_node_get_tree(node
);
2557 tree
= isl_schedule_tree_insert_mark(tree
, mark
);
2558 node
= isl_schedule_node_graft_tree(node
, tree
);
2563 /* Attach the current subtree of "node" to a sequence of filter tree nodes
2564 * with filters described by "filters", attach this sequence
2565 * of filter tree nodes as children to a new tree of type "type" and
2566 * replace the original subtree of "node" by this new tree.
2567 * Each copy of the original subtree is simplified with respect
2568 * to the corresponding filter.
2570 static __isl_give isl_schedule_node
*isl_schedule_node_insert_children(
2571 __isl_take isl_schedule_node
*node
,
2572 enum isl_schedule_node_type type
,
2573 __isl_take isl_union_set_list
*filters
)
2577 isl_schedule_tree
*tree
;
2578 isl_schedule_tree_list
*list
;
2580 if (check_insert(node
) < 0)
2581 node
= isl_schedule_node_free(node
);
2583 if (!node
|| !filters
)
2586 ctx
= isl_schedule_node_get_ctx(node
);
2587 n
= isl_union_set_list_n_union_set(filters
);
2588 list
= isl_schedule_tree_list_alloc(ctx
, n
);
2589 for (i
= 0; i
< n
; ++i
) {
2590 isl_schedule_node
*node_i
;
2591 isl_schedule_tree
*tree
;
2592 isl_union_set
*filter
;
2594 filter
= isl_union_set_list_get_union_set(filters
, i
);
2595 node_i
= isl_schedule_node_copy(node
);
2596 node_i
= isl_schedule_node_gist(node_i
,
2597 isl_union_set_copy(filter
));
2598 tree
= isl_schedule_node_get_tree(node_i
);
2599 isl_schedule_node_free(node_i
);
2600 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
2601 list
= isl_schedule_tree_list_add(list
, tree
);
2603 tree
= isl_schedule_tree_from_children(type
, list
);
2604 node
= isl_schedule_node_graft_tree(node
, tree
);
2606 isl_union_set_list_free(filters
);
2609 isl_union_set_list_free(filters
);
2610 isl_schedule_node_free(node
);
2614 /* Insert a sequence node with child filters "filters" between "node" and
2615 * its parent. That is, the tree that "node" points to is attached
2616 * to each of the child nodes of the filter nodes.
2617 * Return a pointer to the new sequence node.
2619 __isl_give isl_schedule_node
*isl_schedule_node_insert_sequence(
2620 __isl_take isl_schedule_node
*node
,
2621 __isl_take isl_union_set_list
*filters
)
2623 return isl_schedule_node_insert_children(node
,
2624 isl_schedule_node_sequence
, filters
);
2627 /* Insert a set node with child filters "filters" between "node" and
2628 * its parent. That is, the tree that "node" points to is attached
2629 * to each of the child nodes of the filter nodes.
2630 * Return a pointer to the new set node.
2632 __isl_give isl_schedule_node
*isl_schedule_node_insert_set(
2633 __isl_take isl_schedule_node
*node
,
2634 __isl_take isl_union_set_list
*filters
)
2636 return isl_schedule_node_insert_children(node
,
2637 isl_schedule_node_set
, filters
);
2640 /* Remove "node" from its schedule tree and return a pointer
2641 * to the leaf at the same position in the updated schedule tree.
2643 * It is not allowed to remove the root of a schedule tree or
2644 * a child of a set or sequence node.
2646 __isl_give isl_schedule_node
*isl_schedule_node_cut(
2647 __isl_take isl_schedule_node
*node
)
2649 isl_schedule_tree
*leaf
;
2650 enum isl_schedule_node_type parent_type
;
2654 if (!isl_schedule_node_has_parent(node
))
2655 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2656 "cannot cut root", return isl_schedule_node_free(node
));
2658 parent_type
= isl_schedule_node_get_parent_type(node
);
2659 if (parent_type
== isl_schedule_node_set
||
2660 parent_type
== isl_schedule_node_sequence
)
2661 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2662 "cannot cut child of set or sequence",
2663 return isl_schedule_node_free(node
));
2665 leaf
= isl_schedule_node_get_leaf(node
);
2666 return isl_schedule_node_graft_tree(node
, leaf
);
2669 /* Remove a single node from the schedule tree, attaching the child
2670 * of "node" directly to its parent.
2671 * Return a pointer to this former child or to the leaf the position
2672 * of the original node if there was no child.
2673 * It is not allowed to remove the root of a schedule tree,
2674 * a set or sequence node, a child of a set or sequence node or
2675 * a band node with an anchored subtree.
2677 __isl_give isl_schedule_node
*isl_schedule_node_delete(
2678 __isl_take isl_schedule_node
*node
)
2681 isl_schedule_tree
*tree
;
2682 enum isl_schedule_node_type type
;
2687 if (isl_schedule_node_get_tree_depth(node
) == 0)
2688 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2689 "cannot delete root node",
2690 return isl_schedule_node_free(node
));
2691 n
= isl_schedule_node_n_children(node
);
2693 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2694 "can only delete node with a single child",
2695 return isl_schedule_node_free(node
));
2696 type
= isl_schedule_node_get_parent_type(node
);
2697 if (type
== isl_schedule_node_sequence
|| type
== isl_schedule_node_set
)
2698 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2699 "cannot delete child of set or sequence",
2700 return isl_schedule_node_free(node
));
2701 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
) {
2704 anchored
= isl_schedule_node_is_subtree_anchored(node
);
2706 return isl_schedule_node_free(node
);
2708 isl_die(isl_schedule_node_get_ctx(node
),
2710 "cannot delete band node with anchored subtree",
2711 return isl_schedule_node_free(node
));
2714 tree
= isl_schedule_node_get_tree(node
);
2715 if (!tree
|| isl_schedule_tree_has_children(tree
)) {
2716 tree
= isl_schedule_tree_child(tree
, 0);
2718 isl_schedule_tree_free(tree
);
2719 tree
= isl_schedule_node_get_leaf(node
);
2721 node
= isl_schedule_node_graft_tree(node
, tree
);
2726 /* Internal data structure for the group_ancestor callback.
2728 * If "finished" is set, then we no longer need to modify
2729 * any further ancestors.
2731 * "contraction" and "expansion" represent the expansion
2732 * that reflects the grouping.
2734 * "domain" contains the domain elements that reach the position
2735 * where the grouping is performed. That is, it is the range
2736 * of the resulting expansion.
2737 * "domain_universe" is the universe of "domain".
2738 * "group" is the set of group elements, i.e., the domain
2739 * of the resulting expansion.
2740 * "group_universe" is the universe of "group".
2742 * "sched" is the schedule for the group elements, in pratice
2743 * an identity mapping on "group_universe".
2744 * "dim" is the dimension of "sched".
2746 struct isl_schedule_group_data
{
2749 isl_union_map
*expansion
;
2750 isl_union_pw_multi_aff
*contraction
;
2752 isl_union_set
*domain
;
2753 isl_union_set
*domain_universe
;
2754 isl_union_set
*group
;
2755 isl_union_set
*group_universe
;
2758 isl_multi_aff
*sched
;
2761 /* Is domain covered by data->domain within data->domain_universe?
2763 static isl_bool
locally_covered_by_domain(__isl_keep isl_union_set
*domain
,
2764 struct isl_schedule_group_data
*data
)
2767 isl_union_set
*test
;
2769 test
= isl_union_set_copy(domain
);
2770 test
= isl_union_set_intersect(test
,
2771 isl_union_set_copy(data
->domain_universe
));
2772 is_subset
= isl_union_set_is_subset(test
, data
->domain
);
2773 isl_union_set_free(test
);
2778 /* Update the band tree root "tree" to refer to the group instances
2779 * in data->group rather than the original domain elements in data->domain.
2780 * "pos" is the position in the original schedule tree where the modified
2781 * "tree" will be attached.
2783 * Add the part of the identity schedule on the group instances data->sched
2784 * that corresponds to this band node to the band schedule.
2785 * If the domain elements that reach the node and that are part
2786 * of data->domain_universe are all elements of data->domain (and therefore
2787 * replaced by the group instances) then this data->domain_universe
2788 * is removed from the domain of the band schedule.
2790 static __isl_give isl_schedule_tree
*group_band(
2791 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2792 struct isl_schedule_group_data
*data
)
2794 isl_union_set
*domain
;
2796 isl_multi_union_pw_aff
*mupa
, *partial
;
2797 isl_bool is_covered
;
2801 domain
= isl_schedule_node_get_domain(pos
);
2802 is_covered
= locally_covered_by_domain(domain
, data
);
2803 if (is_covered
>= 0 && is_covered
) {
2804 domain
= isl_union_set_universe(domain
);
2805 domain
= isl_union_set_subtract(domain
,
2806 isl_union_set_copy(data
->domain_universe
));
2807 tree
= isl_schedule_tree_band_intersect_domain(tree
, domain
);
2809 isl_union_set_free(domain
);
2811 return isl_schedule_tree_free(tree
);
2812 depth
= isl_schedule_node_get_schedule_depth(pos
);
2813 n
= isl_schedule_tree_band_n_member(tree
);
2814 ma
= isl_multi_aff_copy(data
->sched
);
2815 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, 0, depth
);
2816 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, n
, data
->dim
- depth
- n
);
2817 mupa
= isl_multi_union_pw_aff_from_multi_aff(ma
);
2818 partial
= isl_schedule_tree_band_get_partial_schedule(tree
);
2819 has_id
= isl_multi_union_pw_aff_has_tuple_id(partial
, isl_dim_set
);
2821 partial
= isl_multi_union_pw_aff_free(partial
);
2822 } else if (has_id
) {
2824 id
= isl_multi_union_pw_aff_get_tuple_id(partial
, isl_dim_set
);
2825 mupa
= isl_multi_union_pw_aff_set_tuple_id(mupa
,
2828 partial
= isl_multi_union_pw_aff_union_add(partial
, mupa
);
2829 tree
= isl_schedule_tree_band_set_partial_schedule(tree
, partial
);
2834 /* Drop the parameters in "uset" that are not also in "space".
2835 * "n" is the number of parameters in "space".
2837 static __isl_give isl_union_set
*union_set_drop_extra_params(
2838 __isl_take isl_union_set
*uset
, __isl_keep isl_space
*space
, int n
)
2842 uset
= isl_union_set_align_params(uset
, isl_space_copy(space
));
2843 n2
= isl_union_set_dim(uset
, isl_dim_param
);
2844 uset
= isl_union_set_project_out(uset
, isl_dim_param
, n
, n2
- n
);
2849 /* Update the context tree root "tree" to refer to the group instances
2850 * in data->group rather than the original domain elements in data->domain.
2851 * "pos" is the position in the original schedule tree where the modified
2852 * "tree" will be attached.
2854 * We do not actually need to update "tree" since a context node only
2855 * refers to the schedule space. However, we may need to update "data"
2856 * to not refer to any parameters introduced by the context node.
2858 static __isl_give isl_schedule_tree
*group_context(
2859 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2860 struct isl_schedule_group_data
*data
)
2863 isl_union_set
*domain
;
2867 if (isl_schedule_node_get_tree_depth(pos
) == 1)
2870 domain
= isl_schedule_node_get_universe_domain(pos
);
2871 space
= isl_union_set_get_space(domain
);
2872 isl_union_set_free(domain
);
2874 n1
= isl_space_dim(space
, isl_dim_param
);
2875 data
->expansion
= isl_union_map_align_params(data
->expansion
, space
);
2876 n2
= isl_union_map_dim(data
->expansion
, isl_dim_param
);
2878 if (!data
->expansion
)
2879 return isl_schedule_tree_free(tree
);
2883 involves
= isl_union_map_involves_dims(data
->expansion
,
2884 isl_dim_param
, n1
, n2
- n1
);
2886 return isl_schedule_tree_free(tree
);
2888 isl_die(isl_schedule_node_get_ctx(pos
), isl_error_invalid
,
2889 "grouping cannot only refer to global parameters",
2890 return isl_schedule_tree_free(tree
));
2892 data
->expansion
= isl_union_map_project_out(data
->expansion
,
2893 isl_dim_param
, n1
, n2
- n1
);
2894 space
= isl_union_map_get_space(data
->expansion
);
2896 data
->contraction
= isl_union_pw_multi_aff_align_params(
2897 data
->contraction
, isl_space_copy(space
));
2898 n2
= isl_union_pw_multi_aff_dim(data
->contraction
, isl_dim_param
);
2899 data
->contraction
= isl_union_pw_multi_aff_drop_dims(data
->contraction
,
2900 isl_dim_param
, n1
, n2
- n1
);
2902 data
->domain
= union_set_drop_extra_params(data
->domain
, space
, n1
);
2903 data
->domain_universe
=
2904 union_set_drop_extra_params(data
->domain_universe
, space
, n1
);
2905 data
->group
= union_set_drop_extra_params(data
->group
, space
, n1
);
2906 data
->group_universe
=
2907 union_set_drop_extra_params(data
->group_universe
, space
, n1
);
2909 data
->sched
= isl_multi_aff_align_params(data
->sched
,
2910 isl_space_copy(space
));
2911 n2
= isl_multi_aff_dim(data
->sched
, isl_dim_param
);
2912 data
->sched
= isl_multi_aff_drop_dims(data
->sched
,
2913 isl_dim_param
, n1
, n2
- n1
);
2915 isl_space_free(space
);
2920 /* Update the domain tree root "tree" to refer to the group instances
2921 * in data->group rather than the original domain elements in data->domain.
2922 * "pos" is the position in the original schedule tree where the modified
2923 * "tree" will be attached.
2925 * We first double-check that all grouped domain elements are actually
2926 * part of the root domain and then replace those elements by the group
2929 static __isl_give isl_schedule_tree
*group_domain(
2930 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2931 struct isl_schedule_group_data
*data
)
2933 isl_union_set
*domain
;
2936 domain
= isl_schedule_tree_domain_get_domain(tree
);
2937 is_subset
= isl_union_set_is_subset(data
->domain
, domain
);
2938 isl_union_set_free(domain
);
2940 return isl_schedule_tree_free(tree
);
2942 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
2943 "grouped domain should be part of outer domain",
2944 return isl_schedule_tree_free(tree
));
2945 domain
= isl_schedule_tree_domain_get_domain(tree
);
2946 domain
= isl_union_set_subtract(domain
,
2947 isl_union_set_copy(data
->domain
));
2948 domain
= isl_union_set_union(domain
, isl_union_set_copy(data
->group
));
2949 tree
= isl_schedule_tree_domain_set_domain(tree
, domain
);
2954 /* Update the expansion tree root "tree" to refer to the group instances
2955 * in data->group rather than the original domain elements in data->domain.
2956 * "pos" is the position in the original schedule tree where the modified
2957 * "tree" will be attached.
2959 * Let G_1 -> D_1 be the expansion of "tree" and G_2 -> D_2 the newly
2960 * introduced expansion in a descendant of "tree".
2961 * We first double-check that D_2 is a subset of D_1.
2962 * Then we remove D_2 from the range of G_1 -> D_1 and add the mapping
2963 * G_1 -> D_1 . D_2 -> G_2.
2964 * Simmilarly, we restrict the domain of the contraction to the universe
2965 * of the range of the updated expansion and add G_2 -> D_2 . D_1 -> G_1,
2966 * attempting to remove the domain constraints of this additional part.
2968 static __isl_give isl_schedule_tree
*group_expansion(
2969 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2970 struct isl_schedule_group_data
*data
)
2972 isl_union_set
*domain
;
2973 isl_union_map
*expansion
, *umap
;
2974 isl_union_pw_multi_aff
*contraction
, *upma
;
2977 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2978 domain
= isl_union_map_range(expansion
);
2979 is_subset
= isl_union_set_is_subset(data
->domain
, domain
);
2980 isl_union_set_free(domain
);
2982 return isl_schedule_tree_free(tree
);
2984 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
2985 "grouped domain should be part "
2986 "of outer expansion domain",
2987 return isl_schedule_tree_free(tree
));
2988 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2989 umap
= isl_union_map_from_union_pw_multi_aff(
2990 isl_union_pw_multi_aff_copy(data
->contraction
));
2991 umap
= isl_union_map_apply_range(expansion
, umap
);
2992 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2993 expansion
= isl_union_map_subtract_range(expansion
,
2994 isl_union_set_copy(data
->domain
));
2995 expansion
= isl_union_map_union(expansion
, umap
);
2996 umap
= isl_union_map_universe(isl_union_map_copy(expansion
));
2997 domain
= isl_union_map_range(umap
);
2998 contraction
= isl_schedule_tree_expansion_get_contraction(tree
);
2999 umap
= isl_union_map_from_union_pw_multi_aff(contraction
);
3000 umap
= isl_union_map_apply_range(isl_union_map_copy(data
->expansion
),
3002 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
3003 contraction
= isl_schedule_tree_expansion_get_contraction(tree
);
3004 contraction
= isl_union_pw_multi_aff_intersect_domain(contraction
,
3006 domain
= isl_union_pw_multi_aff_domain(
3007 isl_union_pw_multi_aff_copy(upma
));
3008 upma
= isl_union_pw_multi_aff_gist(upma
, domain
);
3009 contraction
= isl_union_pw_multi_aff_union_add(contraction
, upma
);
3010 tree
= isl_schedule_tree_expansion_set_contraction_and_expansion(tree
,
3011 contraction
, expansion
);
3016 /* Update the tree root "tree" to refer to the group instances
3017 * in data->group rather than the original domain elements in data->domain.
3018 * "pos" is the position in the original schedule tree where the modified
3019 * "tree" will be attached.
3021 * If we have come across a domain or expansion node before (data->finished
3022 * is set), then we no longer need perform any modifications.
3024 * If "tree" is a filter, then we add data->group_universe to the filter.
3025 * We also remove data->domain_universe from the filter if all the domain
3026 * elements in this universe that reach the filter node are part of
3027 * the elements that are being grouped by data->expansion.
3028 * If "tree" is a band, domain or expansion, then it is handled
3029 * in a separate function.
3031 static __isl_give isl_schedule_tree
*group_ancestor(
3032 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
3035 struct isl_schedule_group_data
*data
= user
;
3036 isl_union_set
*domain
;
3037 isl_bool is_covered
;
3040 return isl_schedule_tree_free(tree
);
3045 switch (isl_schedule_tree_get_type(tree
)) {
3046 case isl_schedule_node_error
:
3047 return isl_schedule_tree_free(tree
);
3048 case isl_schedule_node_extension
:
3049 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_unsupported
,
3050 "grouping not allowed in extended tree",
3051 return isl_schedule_tree_free(tree
));
3052 case isl_schedule_node_band
:
3053 tree
= group_band(tree
, pos
, data
);
3055 case isl_schedule_node_context
:
3056 tree
= group_context(tree
, pos
, data
);
3058 case isl_schedule_node_domain
:
3059 tree
= group_domain(tree
, pos
, data
);
3062 case isl_schedule_node_filter
:
3063 domain
= isl_schedule_node_get_domain(pos
);
3064 is_covered
= locally_covered_by_domain(domain
, data
);
3065 isl_union_set_free(domain
);
3067 return isl_schedule_tree_free(tree
);
3068 domain
= isl_schedule_tree_filter_get_filter(tree
);
3070 domain
= isl_union_set_subtract(domain
,
3071 isl_union_set_copy(data
->domain_universe
));
3072 domain
= isl_union_set_union(domain
,
3073 isl_union_set_copy(data
->group_universe
));
3074 tree
= isl_schedule_tree_filter_set_filter(tree
, domain
);
3076 case isl_schedule_node_expansion
:
3077 tree
= group_expansion(tree
, pos
, data
);
3080 case isl_schedule_node_leaf
:
3081 case isl_schedule_node_guard
:
3082 case isl_schedule_node_mark
:
3083 case isl_schedule_node_sequence
:
3084 case isl_schedule_node_set
:
3091 /* Group the domain elements that reach "node" into instances
3092 * of a single statement with identifier "group_id".
3093 * In particular, group the domain elements according to their
3096 * That is, introduce an expansion node with as contraction
3097 * the prefix schedule (with the target space replaced by "group_id")
3098 * and as expansion the inverse of this contraction (with its range
3099 * intersected with the domain elements that reach "node").
3100 * The outer nodes are then modified to refer to the group instances
3101 * instead of the original domain elements.
3103 * No instance of "group_id" is allowed to reach "node" prior
3105 * No ancestor of "node" is allowed to be an extension node.
3107 * Return a pointer to original node in tree, i.e., the child
3108 * of the newly introduced expansion node.
3110 __isl_give isl_schedule_node
*isl_schedule_node_group(
3111 __isl_take isl_schedule_node
*node
, __isl_take isl_id
*group_id
)
3113 struct isl_schedule_group_data data
= { 0 };
3115 isl_union_set
*domain
;
3116 isl_union_pw_multi_aff
*contraction
;
3117 isl_union_map
*expansion
;
3120 if (!node
|| !group_id
)
3122 if (check_insert(node
) < 0)
3125 domain
= isl_schedule_node_get_domain(node
);
3126 data
.domain
= isl_union_set_copy(domain
);
3127 data
.domain_universe
= isl_union_set_copy(domain
);
3128 data
.domain_universe
= isl_union_set_universe(data
.domain_universe
);
3130 data
.dim
= isl_schedule_node_get_schedule_depth(node
);
3131 if (data
.dim
== 0) {
3134 isl_union_set
*group
;
3135 isl_union_map
*univ
;
3137 ctx
= isl_schedule_node_get_ctx(node
);
3138 space
= isl_space_set_alloc(ctx
, 0, 0);
3139 space
= isl_space_set_tuple_id(space
, isl_dim_set
, group_id
);
3140 set
= isl_set_universe(isl_space_copy(space
));
3141 group
= isl_union_set_from_set(set
);
3142 expansion
= isl_union_map_from_domain_and_range(domain
, group
);
3143 univ
= isl_union_map_universe(isl_union_map_copy(expansion
));
3144 contraction
= isl_union_pw_multi_aff_from_union_map(univ
);
3145 expansion
= isl_union_map_reverse(expansion
);
3147 isl_multi_union_pw_aff
*prefix
;
3148 isl_union_set
*univ
;
3151 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
3152 prefix
= isl_multi_union_pw_aff_set_tuple_id(prefix
,
3153 isl_dim_set
, group_id
);
3154 space
= isl_multi_union_pw_aff_get_space(prefix
);
3155 contraction
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
3157 univ
= isl_union_set_universe(isl_union_set_copy(domain
));
3159 isl_union_pw_multi_aff_intersect_domain(contraction
, univ
);
3160 expansion
= isl_union_map_from_union_pw_multi_aff(
3161 isl_union_pw_multi_aff_copy(contraction
));
3162 expansion
= isl_union_map_reverse(expansion
);
3163 expansion
= isl_union_map_intersect_range(expansion
, domain
);
3165 space
= isl_space_map_from_set(space
);
3166 data
.sched
= isl_multi_aff_identity(space
);
3167 data
.group
= isl_union_map_domain(isl_union_map_copy(expansion
));
3168 data
.group
= isl_union_set_coalesce(data
.group
);
3169 data
.group_universe
= isl_union_set_copy(data
.group
);
3170 data
.group_universe
= isl_union_set_universe(data
.group_universe
);
3171 data
.expansion
= isl_union_map_copy(expansion
);
3172 data
.contraction
= isl_union_pw_multi_aff_copy(contraction
);
3173 node
= isl_schedule_node_insert_expansion(node
, contraction
, expansion
);
3175 disjoint
= isl_union_set_is_disjoint(data
.domain_universe
,
3176 data
.group_universe
);
3178 node
= update_ancestors(node
, &group_ancestor
, &data
);
3180 isl_union_set_free(data
.domain
);
3181 isl_union_set_free(data
.domain_universe
);
3182 isl_union_set_free(data
.group
);
3183 isl_union_set_free(data
.group_universe
);
3184 isl_multi_aff_free(data
.sched
);
3185 isl_union_map_free(data
.expansion
);
3186 isl_union_pw_multi_aff_free(data
.contraction
);
3188 node
= isl_schedule_node_child(node
, 0);
3190 if (!node
|| disjoint
< 0)
3191 return isl_schedule_node_free(node
);
3193 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
3194 "group instances already reach node",
3195 return isl_schedule_node_free(node
));
3199 isl_schedule_node_free(node
);
3200 isl_id_free(group_id
);
3204 /* Compute the gist of the given band node with respect to "context".
3206 __isl_give isl_schedule_node
*isl_schedule_node_band_gist(
3207 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*context
)
3209 isl_schedule_tree
*tree
;
3211 tree
= isl_schedule_node_get_tree(node
);
3212 tree
= isl_schedule_tree_band_gist(tree
, context
);
3213 return isl_schedule_node_graft_tree(node
, tree
);
3216 /* Internal data structure for isl_schedule_node_gist.
3217 * "n_expansion" is the number of outer expansion nodes
3218 * with respect to the current position
3219 * "filters" contains an element for each outer filter, expansion or
3220 * extension node with respect to the current position, each representing
3221 * the intersection of the previous element and the filter on the filter node
3222 * or the expansion/extension of the previous element.
3223 * The first element in the original context passed to isl_schedule_node_gist.
3225 struct isl_node_gist_data
{
3227 isl_union_set_list
*filters
;
3230 /* Enter the expansion node "node" during a isl_schedule_node_gist traversal.
3232 * In particular, add an extra element to data->filters containing
3233 * the expansion of the previous element and replace the expansion
3234 * and contraction on "node" by the gist with respect to these filters.
3235 * Also keep track of the fact that we have entered another expansion.
3237 static __isl_give isl_schedule_node
*gist_enter_expansion(
3238 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3241 isl_union_set
*inner
;
3242 isl_union_map
*expansion
;
3243 isl_union_pw_multi_aff
*contraction
;
3245 data
->n_expansion
++;
3247 n
= isl_union_set_list_n_union_set(data
->filters
);
3248 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3249 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3250 inner
= isl_union_set_apply(inner
, expansion
);
3252 contraction
= isl_schedule_node_expansion_get_contraction(node
);
3253 contraction
= isl_union_pw_multi_aff_gist(contraction
,
3254 isl_union_set_copy(inner
));
3256 data
->filters
= isl_union_set_list_add(data
->filters
, inner
);
3258 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3259 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3260 expansion
= isl_union_map_gist_domain(expansion
, inner
);
3261 node
= isl_schedule_node_expansion_set_contraction_and_expansion(node
,
3262 contraction
, expansion
);
3267 /* Leave the expansion node "node" during a isl_schedule_node_gist traversal.
3269 * In particular, remove the element in data->filters that was added by
3270 * gist_enter_expansion and decrement the number of outer expansions.
3272 * The expansion has already been simplified in gist_enter_expansion.
3273 * If this simplification results in an identity expansion, then
3274 * it is removed here.
3276 static __isl_give isl_schedule_node
*gist_leave_expansion(
3277 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3281 isl_union_map
*expansion
;
3283 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3284 identity
= isl_union_map_is_identity(expansion
);
3285 isl_union_map_free(expansion
);
3288 node
= isl_schedule_node_free(node
);
3290 node
= isl_schedule_node_delete(node
);
3292 n
= isl_union_set_list_n_union_set(data
->filters
);
3293 data
->filters
= isl_union_set_list_drop(data
->filters
, n
- 1, 1);
3295 data
->n_expansion
--;
3300 /* Enter the extension node "node" during a isl_schedule_node_gist traversal.
3302 * In particular, add an extra element to data->filters containing
3303 * the union of the previous element with the additional domain elements
3304 * introduced by the extension.
3306 static __isl_give isl_schedule_node
*gist_enter_extension(
3307 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3310 isl_union_set
*inner
, *extra
;
3311 isl_union_map
*extension
;
3313 n
= isl_union_set_list_n_union_set(data
->filters
);
3314 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3315 extension
= isl_schedule_node_extension_get_extension(node
);
3316 extra
= isl_union_map_range(extension
);
3317 inner
= isl_union_set_union(inner
, extra
);
3319 data
->filters
= isl_union_set_list_add(data
->filters
, inner
);
3324 /* Can we finish gisting at this node?
3325 * That is, is the filter on the current filter node a subset of
3326 * the original context passed to isl_schedule_node_gist?
3327 * If we have gone through any expansions, then we cannot perform
3328 * this test since the current domain elements are incomparable
3329 * to the domain elements in the original context.
3331 static isl_bool
gist_done(__isl_keep isl_schedule_node
*node
,
3332 struct isl_node_gist_data
*data
)
3334 isl_union_set
*filter
, *outer
;
3337 if (data
->n_expansion
!= 0)
3338 return isl_bool_false
;
3340 filter
= isl_schedule_node_filter_get_filter(node
);
3341 outer
= isl_union_set_list_get_union_set(data
->filters
, 0);
3342 subset
= isl_union_set_is_subset(filter
, outer
);
3343 isl_union_set_free(outer
);
3344 isl_union_set_free(filter
);
3349 /* Callback for "traverse" to enter a node and to move
3350 * to the deepest initial subtree that should be traversed
3351 * by isl_schedule_node_gist.
3353 * The "filters" list is extended by one element each time
3354 * we come across a filter node by the result of intersecting
3355 * the last element in the list with the filter on the filter node.
3357 * If the filter on the current filter node is a subset of
3358 * the original context passed to isl_schedule_node_gist,
3359 * then there is no need to go into its subtree since it cannot
3360 * be further simplified by the context. The "filters" list is
3361 * still extended for consistency, but the actual value of the
3362 * added element is immaterial since it will not be used.
3364 * Otherwise, the filter on the current filter node is replaced by
3365 * the gist of the original filter with respect to the intersection
3366 * of the original context with the intermediate filters.
3368 * If the new element in the "filters" list is empty, then no elements
3369 * can reach the descendants of the current filter node. The subtree
3370 * underneath the filter node is therefore removed.
3372 * Each expansion node we come across is handled by
3373 * gist_enter_expansion.
3375 * Each extension node we come across is handled by
3376 * gist_enter_extension.
3378 static __isl_give isl_schedule_node
*gist_enter(
3379 __isl_take isl_schedule_node
*node
, void *user
)
3381 struct isl_node_gist_data
*data
= user
;
3384 isl_union_set
*filter
, *inner
;
3385 isl_bool done
, empty
;
3388 switch (isl_schedule_node_get_type(node
)) {
3389 case isl_schedule_node_error
:
3390 return isl_schedule_node_free(node
);
3391 case isl_schedule_node_expansion
:
3392 node
= gist_enter_expansion(node
, data
);
3394 case isl_schedule_node_extension
:
3395 node
= gist_enter_extension(node
, data
);
3397 case isl_schedule_node_band
:
3398 case isl_schedule_node_context
:
3399 case isl_schedule_node_domain
:
3400 case isl_schedule_node_guard
:
3401 case isl_schedule_node_leaf
:
3402 case isl_schedule_node_mark
:
3403 case isl_schedule_node_sequence
:
3404 case isl_schedule_node_set
:
3406 case isl_schedule_node_filter
:
3409 done
= gist_done(node
, data
);
3410 filter
= isl_schedule_node_filter_get_filter(node
);
3411 if (done
< 0 || done
) {
3412 data
->filters
= isl_union_set_list_add(data
->filters
,
3415 return isl_schedule_node_free(node
);
3418 n
= isl_union_set_list_n_union_set(data
->filters
);
3419 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3420 filter
= isl_union_set_gist(filter
, isl_union_set_copy(inner
));
3421 node
= isl_schedule_node_filter_set_filter(node
,
3422 isl_union_set_copy(filter
));
3423 filter
= isl_union_set_intersect(filter
, inner
);
3424 empty
= isl_union_set_is_empty(filter
);
3425 data
->filters
= isl_union_set_list_add(data
->filters
, filter
);
3427 return isl_schedule_node_free(node
);
3430 node
= isl_schedule_node_child(node
, 0);
3431 node
= isl_schedule_node_cut(node
);
3432 node
= isl_schedule_node_parent(node
);
3434 } while (isl_schedule_node_has_children(node
) &&
3435 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3440 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
3442 * In particular, if the current node is a filter node, then we remove
3443 * the element on the "filters" list that was added when we entered
3444 * the node. There is no need to compute any gist here, since we
3445 * already did that when we entered the node.
3447 * Expansion nodes are handled by gist_leave_expansion.
3449 * If the current node is an extension, then remove the element
3450 * in data->filters that was added by gist_enter_extension.
3452 * If the current node is a band node, then we compute the gist of
3453 * the band node with respect to the intersection of the original context
3454 * and the intermediate filters.
3456 * If the current node is a sequence or set node, then some of
3457 * the filter children may have become empty and so they are removed.
3458 * If only one child is left, then the set or sequence node along with
3459 * the single remaining child filter is removed. The filter can be
3460 * removed because the filters on a sequence or set node are supposed
3461 * to partition the incoming domain instances.
3462 * In principle, it should then be impossible for there to be zero
3463 * remaining children, but should this happen, we replace the entire
3464 * subtree with an empty filter.
3466 static __isl_give isl_schedule_node
*gist_leave(
3467 __isl_take isl_schedule_node
*node
, void *user
)
3469 struct isl_node_gist_data
*data
= user
;
3470 isl_schedule_tree
*tree
;
3472 isl_union_set
*filter
;
3474 switch (isl_schedule_node_get_type(node
)) {
3475 case isl_schedule_node_error
:
3476 return isl_schedule_node_free(node
);
3477 case isl_schedule_node_expansion
:
3478 node
= gist_leave_expansion(node
, data
);
3480 case isl_schedule_node_extension
:
3481 case isl_schedule_node_filter
:
3482 n
= isl_union_set_list_n_union_set(data
->filters
);
3483 data
->filters
= isl_union_set_list_drop(data
->filters
,
3486 case isl_schedule_node_band
:
3487 n
= isl_union_set_list_n_union_set(data
->filters
);
3488 filter
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3489 node
= isl_schedule_node_band_gist(node
, filter
);
3491 case isl_schedule_node_set
:
3492 case isl_schedule_node_sequence
:
3493 tree
= isl_schedule_node_get_tree(node
);
3494 n
= isl_schedule_tree_n_children(tree
);
3495 for (i
= n
- 1; i
>= 0; --i
) {
3496 isl_schedule_tree
*child
;
3497 isl_union_set
*filter
;
3500 child
= isl_schedule_tree_get_child(tree
, i
);
3501 filter
= isl_schedule_tree_filter_get_filter(child
);
3502 empty
= isl_union_set_is_empty(filter
);
3503 isl_union_set_free(filter
);
3504 isl_schedule_tree_free(child
);
3506 tree
= isl_schedule_tree_free(tree
);
3508 tree
= isl_schedule_tree_drop_child(tree
, i
);
3510 n
= isl_schedule_tree_n_children(tree
);
3511 node
= isl_schedule_node_graft_tree(node
, tree
);
3513 node
= isl_schedule_node_delete(node
);
3514 node
= isl_schedule_node_delete(node
);
3515 } else if (n
== 0) {
3519 isl_union_set_list_get_union_set(data
->filters
, 0);
3520 space
= isl_union_set_get_space(filter
);
3521 isl_union_set_free(filter
);
3522 filter
= isl_union_set_empty(space
);
3523 node
= isl_schedule_node_cut(node
);
3524 node
= isl_schedule_node_insert_filter(node
, filter
);
3527 case isl_schedule_node_context
:
3528 case isl_schedule_node_domain
:
3529 case isl_schedule_node_guard
:
3530 case isl_schedule_node_leaf
:
3531 case isl_schedule_node_mark
:
3538 /* Compute the gist of the subtree at "node" with respect to
3539 * the reaching domain elements in "context".
3540 * In particular, compute the gist of all band and filter nodes
3541 * in the subtree with respect to "context". Children of set or sequence
3542 * nodes that end up with an empty filter are removed completely.
3544 * We keep track of the intersection of "context" with all outer filters
3545 * of the current node within the subtree in the final element of "filters".
3546 * Initially, this list contains the single element "context" and it is
3547 * extended or shortened each time we enter or leave a filter node.
3549 __isl_give isl_schedule_node
*isl_schedule_node_gist(
3550 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*context
)
3552 struct isl_node_gist_data data
;
3554 data
.n_expansion
= 0;
3555 data
.filters
= isl_union_set_list_from_union_set(context
);
3556 node
= traverse(node
, &gist_enter
, &gist_leave
, &data
);
3557 isl_union_set_list_free(data
.filters
);
3561 /* Intersect the domain of domain node "node" with "domain".
3563 * If the domain of "node" is already a subset of "domain",
3564 * then nothing needs to be changed.
3566 * Otherwise, we replace the domain of the domain node by the intersection
3567 * and simplify the subtree rooted at "node" with respect to this intersection.
3569 __isl_give isl_schedule_node
*isl_schedule_node_domain_intersect_domain(
3570 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
)
3572 isl_schedule_tree
*tree
;
3573 isl_union_set
*uset
;
3576 if (!node
|| !domain
)
3579 uset
= isl_schedule_tree_domain_get_domain(node
->tree
);
3580 is_subset
= isl_union_set_is_subset(uset
, domain
);
3581 isl_union_set_free(uset
);
3585 isl_union_set_free(domain
);
3589 tree
= isl_schedule_tree_copy(node
->tree
);
3590 uset
= isl_schedule_tree_domain_get_domain(tree
);
3591 uset
= isl_union_set_intersect(uset
, domain
);
3592 tree
= isl_schedule_tree_domain_set_domain(tree
,
3593 isl_union_set_copy(uset
));
3594 node
= isl_schedule_node_graft_tree(node
, tree
);
3596 node
= isl_schedule_node_child(node
, 0);
3597 node
= isl_schedule_node_gist(node
, uset
);
3598 node
= isl_schedule_node_parent(node
);
3602 isl_schedule_node_free(node
);
3603 isl_union_set_free(domain
);
3607 /* Replace the domain of domain node "node" with the gist
3608 * of the original domain with respect to the parameter domain "context".
3610 __isl_give isl_schedule_node
*isl_schedule_node_domain_gist_params(
3611 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*context
)
3613 isl_union_set
*domain
;
3614 isl_schedule_tree
*tree
;
3616 if (!node
|| !context
)
3619 tree
= isl_schedule_tree_copy(node
->tree
);
3620 domain
= isl_schedule_tree_domain_get_domain(node
->tree
);
3621 domain
= isl_union_set_gist_params(domain
, context
);
3622 tree
= isl_schedule_tree_domain_set_domain(tree
, domain
);
3623 node
= isl_schedule_node_graft_tree(node
, tree
);
3627 isl_schedule_node_free(node
);
3628 isl_set_free(context
);
3632 /* Internal data structure for isl_schedule_node_get_subtree_expansion.
3633 * "expansions" contains a list of accumulated expansions
3634 * for each outer expansion, set or sequence node. The first element
3635 * in the list is an identity mapping on the reaching domain elements.
3636 * "res" collects the results.
3638 struct isl_subtree_expansion_data
{
3639 isl_union_map_list
*expansions
;
3643 /* Callback for "traverse" to enter a node and to move
3644 * to the deepest initial subtree that should be traversed
3645 * by isl_schedule_node_get_subtree_expansion.
3647 * Whenever we come across an expansion node, the last element
3648 * of data->expansions is combined with the expansion
3649 * on the expansion node.
3651 * Whenever we come across a filter node that is the child
3652 * of a set or sequence node, data->expansions is extended
3653 * with a new element that restricts the previous element
3654 * to the elements selected by the filter.
3655 * The previous element can then be reused while backtracking.
3657 static __isl_give isl_schedule_node
*subtree_expansion_enter(
3658 __isl_take isl_schedule_node
*node
, void *user
)
3660 struct isl_subtree_expansion_data
*data
= user
;
3663 enum isl_schedule_node_type type
;
3664 isl_union_set
*filter
;
3665 isl_union_map
*inner
, *expansion
;
3668 switch (isl_schedule_node_get_type(node
)) {
3669 case isl_schedule_node_error
:
3670 return isl_schedule_node_free(node
);
3671 case isl_schedule_node_filter
:
3672 type
= isl_schedule_node_get_parent_type(node
);
3673 if (type
!= isl_schedule_node_set
&&
3674 type
!= isl_schedule_node_sequence
)
3676 filter
= isl_schedule_node_filter_get_filter(node
);
3677 n
= isl_union_map_list_n_union_map(data
->expansions
);
3679 isl_union_map_list_get_union_map(data
->expansions
,
3681 inner
= isl_union_map_intersect_range(inner
, filter
);
3683 isl_union_map_list_add(data
->expansions
, inner
);
3685 case isl_schedule_node_expansion
:
3686 n
= isl_union_map_list_n_union_map(data
->expansions
);
3688 isl_schedule_node_expansion_get_expansion(node
);
3690 isl_union_map_list_get_union_map(data
->expansions
,
3692 inner
= isl_union_map_apply_range(inner
, expansion
);
3694 isl_union_map_list_set_union_map(data
->expansions
,
3697 case isl_schedule_node_band
:
3698 case isl_schedule_node_context
:
3699 case isl_schedule_node_domain
:
3700 case isl_schedule_node_extension
:
3701 case isl_schedule_node_guard
:
3702 case isl_schedule_node_leaf
:
3703 case isl_schedule_node_mark
:
3704 case isl_schedule_node_sequence
:
3705 case isl_schedule_node_set
:
3708 } while (isl_schedule_node_has_children(node
) &&
3709 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3714 /* Callback for "traverse" to leave a node for
3715 * isl_schedule_node_get_subtree_expansion.
3717 * If we come across a filter node that is the child
3718 * of a set or sequence node, then we remove the element
3719 * of data->expansions that was added in subtree_expansion_enter.
3721 * If we reach a leaf node, then the accumulated expansion is
3722 * added to data->res.
3724 static __isl_give isl_schedule_node
*subtree_expansion_leave(
3725 __isl_take isl_schedule_node
*node
, void *user
)
3727 struct isl_subtree_expansion_data
*data
= user
;
3729 isl_union_map
*inner
;
3730 enum isl_schedule_node_type type
;
3732 switch (isl_schedule_node_get_type(node
)) {
3733 case isl_schedule_node_error
:
3734 return isl_schedule_node_free(node
);
3735 case isl_schedule_node_filter
:
3736 type
= isl_schedule_node_get_parent_type(node
);
3737 if (type
!= isl_schedule_node_set
&&
3738 type
!= isl_schedule_node_sequence
)
3740 n
= isl_union_map_list_n_union_map(data
->expansions
);
3741 data
->expansions
= isl_union_map_list_drop(data
->expansions
,
3744 case isl_schedule_node_leaf
:
3745 n
= isl_union_map_list_n_union_map(data
->expansions
);
3746 inner
= isl_union_map_list_get_union_map(data
->expansions
,
3748 data
->res
= isl_union_map_union(data
->res
, inner
);
3750 case isl_schedule_node_band
:
3751 case isl_schedule_node_context
:
3752 case isl_schedule_node_domain
:
3753 case isl_schedule_node_expansion
:
3754 case isl_schedule_node_extension
:
3755 case isl_schedule_node_guard
:
3756 case isl_schedule_node_mark
:
3757 case isl_schedule_node_sequence
:
3758 case isl_schedule_node_set
:
3765 /* Return a mapping from the domain elements that reach "node"
3766 * to the corresponding domain elements in the leaves of the subtree
3767 * rooted at "node" obtained by composing the intermediate expansions.
3769 * We start out with an identity mapping between the domain elements
3770 * that reach "node" and compose it with all the expansions
3771 * on a path from "node" to a leaf while traversing the subtree.
3772 * Within the children of an a sequence or set node, the
3773 * accumulated expansion is restricted to the elements selected
3774 * by the filter child.
3776 __isl_give isl_union_map
*isl_schedule_node_get_subtree_expansion(
3777 __isl_keep isl_schedule_node
*node
)
3779 struct isl_subtree_expansion_data data
;
3781 isl_union_set
*domain
;
3782 isl_union_map
*expansion
;
3787 domain
= isl_schedule_node_get_universe_domain(node
);
3788 space
= isl_union_set_get_space(domain
);
3789 expansion
= isl_union_set_identity(domain
);
3790 data
.res
= isl_union_map_empty(space
);
3791 data
.expansions
= isl_union_map_list_from_union_map(expansion
);
3793 node
= isl_schedule_node_copy(node
);
3794 node
= traverse(node
, &subtree_expansion_enter
,
3795 &subtree_expansion_leave
, &data
);
3797 data
.res
= isl_union_map_free(data
.res
);
3798 isl_schedule_node_free(node
);
3800 isl_union_map_list_free(data
.expansions
);
3805 /* Internal data structure for isl_schedule_node_get_subtree_contraction.
3806 * "contractions" contains a list of accumulated contractions
3807 * for each outer expansion, set or sequence node. The first element
3808 * in the list is an identity mapping on the reaching domain elements.
3809 * "res" collects the results.
3811 struct isl_subtree_contraction_data
{
3812 isl_union_pw_multi_aff_list
*contractions
;
3813 isl_union_pw_multi_aff
*res
;
3816 /* Callback for "traverse" to enter a node and to move
3817 * to the deepest initial subtree that should be traversed
3818 * by isl_schedule_node_get_subtree_contraction.
3820 * Whenever we come across an expansion node, the last element
3821 * of data->contractions is combined with the contraction
3822 * on the expansion node.
3824 * Whenever we come across a filter node that is the child
3825 * of a set or sequence node, data->contractions is extended
3826 * with a new element that restricts the previous element
3827 * to the elements selected by the filter.
3828 * The previous element can then be reused while backtracking.
3830 static __isl_give isl_schedule_node
*subtree_contraction_enter(
3831 __isl_take isl_schedule_node
*node
, void *user
)
3833 struct isl_subtree_contraction_data
*data
= user
;
3836 enum isl_schedule_node_type type
;
3837 isl_union_set
*filter
;
3838 isl_union_pw_multi_aff
*inner
, *contraction
;
3841 switch (isl_schedule_node_get_type(node
)) {
3842 case isl_schedule_node_error
:
3843 return isl_schedule_node_free(node
);
3844 case isl_schedule_node_filter
:
3845 type
= isl_schedule_node_get_parent_type(node
);
3846 if (type
!= isl_schedule_node_set
&&
3847 type
!= isl_schedule_node_sequence
)
3849 filter
= isl_schedule_node_filter_get_filter(node
);
3850 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3851 data
->contractions
);
3853 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3854 data
->contractions
, n
- 1);
3855 inner
= isl_union_pw_multi_aff_intersect_domain(inner
,
3857 data
->contractions
=
3858 isl_union_pw_multi_aff_list_add(data
->contractions
,
3861 case isl_schedule_node_expansion
:
3862 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3863 data
->contractions
);
3865 isl_schedule_node_expansion_get_contraction(node
);
3867 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3868 data
->contractions
, n
- 1);
3870 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3871 inner
, contraction
);
3872 data
->contractions
=
3873 isl_union_pw_multi_aff_list_set_union_pw_multi_aff(
3874 data
->contractions
, n
- 1, inner
);
3876 case isl_schedule_node_band
:
3877 case isl_schedule_node_context
:
3878 case isl_schedule_node_domain
:
3879 case isl_schedule_node_extension
:
3880 case isl_schedule_node_guard
:
3881 case isl_schedule_node_leaf
:
3882 case isl_schedule_node_mark
:
3883 case isl_schedule_node_sequence
:
3884 case isl_schedule_node_set
:
3887 } while (isl_schedule_node_has_children(node
) &&
3888 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3893 /* Callback for "traverse" to leave a node for
3894 * isl_schedule_node_get_subtree_contraction.
3896 * If we come across a filter node that is the child
3897 * of a set or sequence node, then we remove the element
3898 * of data->contractions that was added in subtree_contraction_enter.
3900 * If we reach a leaf node, then the accumulated contraction is
3901 * added to data->res.
3903 static __isl_give isl_schedule_node
*subtree_contraction_leave(
3904 __isl_take isl_schedule_node
*node
, void *user
)
3906 struct isl_subtree_contraction_data
*data
= user
;
3908 isl_union_pw_multi_aff
*inner
;
3909 enum isl_schedule_node_type type
;
3911 switch (isl_schedule_node_get_type(node
)) {
3912 case isl_schedule_node_error
:
3913 return isl_schedule_node_free(node
);
3914 case isl_schedule_node_filter
:
3915 type
= isl_schedule_node_get_parent_type(node
);
3916 if (type
!= isl_schedule_node_set
&&
3917 type
!= isl_schedule_node_sequence
)
3919 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3920 data
->contractions
);
3921 data
->contractions
=
3922 isl_union_pw_multi_aff_list_drop(data
->contractions
,
3925 case isl_schedule_node_leaf
:
3926 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3927 data
->contractions
);
3928 inner
= isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3929 data
->contractions
, n
- 1);
3930 data
->res
= isl_union_pw_multi_aff_union_add(data
->res
, inner
);
3932 case isl_schedule_node_band
:
3933 case isl_schedule_node_context
:
3934 case isl_schedule_node_domain
:
3935 case isl_schedule_node_expansion
:
3936 case isl_schedule_node_extension
:
3937 case isl_schedule_node_guard
:
3938 case isl_schedule_node_mark
:
3939 case isl_schedule_node_sequence
:
3940 case isl_schedule_node_set
:
3947 /* Return a mapping from the domain elements in the leaves of the subtree
3948 * rooted at "node" to the corresponding domain elements that reach "node"
3949 * obtained by composing the intermediate contractions.
3951 * We start out with an identity mapping between the domain elements
3952 * that reach "node" and compose it with all the contractions
3953 * on a path from "node" to a leaf while traversing the subtree.
3954 * Within the children of an a sequence or set node, the
3955 * accumulated contraction is restricted to the elements selected
3956 * by the filter child.
3958 __isl_give isl_union_pw_multi_aff
*isl_schedule_node_get_subtree_contraction(
3959 __isl_keep isl_schedule_node
*node
)
3961 struct isl_subtree_contraction_data data
;
3963 isl_union_set
*domain
;
3964 isl_union_pw_multi_aff
*contraction
;
3969 domain
= isl_schedule_node_get_universe_domain(node
);
3970 space
= isl_union_set_get_space(domain
);
3971 contraction
= isl_union_set_identity_union_pw_multi_aff(domain
);
3972 data
.res
= isl_union_pw_multi_aff_empty(space
);
3974 isl_union_pw_multi_aff_list_from_union_pw_multi_aff(contraction
);
3976 node
= isl_schedule_node_copy(node
);
3977 node
= traverse(node
, &subtree_contraction_enter
,
3978 &subtree_contraction_leave
, &data
);
3980 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
3981 isl_schedule_node_free(node
);
3983 isl_union_pw_multi_aff_list_free(data
.contractions
);
3988 /* Do the nearest "n" ancestors of "node" have the types given in "types"
3989 * (starting at the parent of "node")?
3991 static isl_bool
has_ancestors(__isl_keep isl_schedule_node
*node
,
3992 int n
, enum isl_schedule_node_type
*types
)
3997 return isl_bool_error
;
3999 n_ancestor
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
4001 return isl_bool_false
;
4003 for (i
= 0; i
< n
; ++i
) {
4004 isl_schedule_tree
*tree
;
4007 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
4008 n_ancestor
- 1 - i
);
4010 return isl_bool_error
;
4011 correct_type
= isl_schedule_tree_get_type(tree
) == types
[i
];
4012 isl_schedule_tree_free(tree
);
4014 return isl_bool_false
;
4017 return isl_bool_true
;
4020 /* Given a node "node" that appears in an extension (i.e., it is the child
4021 * of a filter in a sequence inside an extension node), are the spaces
4022 * of the extension specified by "extension" disjoint from those
4023 * of both the original extension and the domain elements that reach
4024 * that original extension?
4026 static int is_disjoint_extension(__isl_keep isl_schedule_node
*node
,
4027 __isl_keep isl_union_map
*extension
)
4030 isl_union_set
*domain
;
4033 node
= isl_schedule_node_copy(node
);
4034 node
= isl_schedule_node_parent(node
);
4035 node
= isl_schedule_node_parent(node
);
4036 node
= isl_schedule_node_parent(node
);
4037 old
= isl_schedule_node_extension_get_extension(node
);
4038 domain
= isl_schedule_node_get_universe_domain(node
);
4039 isl_schedule_node_free(node
);
4040 old
= isl_union_map_universe(old
);
4041 domain
= isl_union_set_union(domain
, isl_union_map_range(old
));
4042 extension
= isl_union_map_copy(extension
);
4043 extension
= isl_union_map_intersect_range(extension
, domain
);
4044 empty
= isl_union_map_is_empty(extension
);
4045 isl_union_map_free(extension
);
4050 /* Given a node "node" that is governed by an extension node, extend
4051 * that extension node with "extension".
4053 * In particular, "node" is the child of a filter in a sequence that
4054 * is in turn a child of an extension node. Extend that extension node
4057 * Return a pointer to the parent of the original node (i.e., a filter).
4059 static __isl_give isl_schedule_node
*extend_extension(
4060 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
4064 isl_union_map
*node_extension
;
4066 node
= isl_schedule_node_parent(node
);
4067 pos
= isl_schedule_node_get_child_position(node
);
4068 node
= isl_schedule_node_parent(node
);
4069 node
= isl_schedule_node_parent(node
);
4070 node_extension
= isl_schedule_node_extension_get_extension(node
);
4071 disjoint
= isl_union_map_is_disjoint(extension
, node_extension
);
4072 extension
= isl_union_map_union(extension
, node_extension
);
4073 node
= isl_schedule_node_extension_set_extension(node
, extension
);
4074 node
= isl_schedule_node_child(node
, 0);
4075 node
= isl_schedule_node_child(node
, pos
);
4078 return isl_schedule_node_free(node
);
4082 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4083 "extension domain should be disjoint from earlier "
4084 "extensions", return isl_schedule_node_free(node
));
4089 /* Return the universe of "uset" if this universe is disjoint from "ref".
4090 * Otherwise, return "uset".
4092 * Also check if "uset" itself is disjoint from "ref", reporting
4093 * an error if it is not.
4095 static __isl_give isl_union_set
*replace_by_universe_if_disjoint(
4096 __isl_take isl_union_set
*uset
, __isl_keep isl_union_set
*ref
)
4099 isl_union_set
*universe
;
4101 disjoint
= isl_union_set_is_disjoint(uset
, ref
);
4103 return isl_union_set_free(uset
);
4105 isl_die(isl_union_set_get_ctx(uset
), isl_error_invalid
,
4106 "extension domain should be disjoint from "
4107 "current domain", return isl_union_set_free(uset
));
4109 universe
= isl_union_set_universe(isl_union_set_copy(uset
));
4110 disjoint
= isl_union_set_is_disjoint(universe
, ref
);
4111 if (disjoint
>= 0 && disjoint
) {
4112 isl_union_set_free(uset
);
4115 isl_union_set_free(universe
);
4118 return isl_union_set_free(uset
);
4122 /* Insert an extension node on top of "node" with extension "extension".
4123 * In addition, insert a filter that separates node from the extension
4124 * between the extension node and "node".
4125 * Return a pointer to the inserted filter node.
4127 * If "node" already appears in an extension (i.e., if it is the child
4128 * of a filter in a sequence inside an extension node), then extend that
4129 * extension with "extension" instead.
4130 * In this case, a pointer to the original filter node is returned.
4131 * Note that if some of the elements in the new extension live in the
4132 * same space as those of the original extension or the domain elements
4133 * reaching the original extension, then we insert a new extension anyway.
4134 * Otherwise, we would have to adjust the filters in the sequence child
4135 * of the extension to ensure that the elements in the new extension
4138 static __isl_give isl_schedule_node
*insert_extension(
4139 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
4141 enum isl_schedule_node_type ancestors
[] =
4142 { isl_schedule_node_filter
, isl_schedule_node_sequence
,
4143 isl_schedule_node_extension
};
4144 isl_union_set
*domain
;
4145 isl_union_set
*filter
;
4148 in_ext
= has_ancestors(node
, 3, ancestors
);
4154 disjoint
= is_disjoint_extension(node
, extension
);
4158 return extend_extension(node
, extension
);
4161 filter
= isl_schedule_node_get_domain(node
);
4162 domain
= isl_union_map_range(isl_union_map_copy(extension
));
4163 filter
= replace_by_universe_if_disjoint(filter
, domain
);
4164 isl_union_set_free(domain
);
4166 node
= isl_schedule_node_insert_filter(node
, filter
);
4167 node
= isl_schedule_node_insert_extension(node
, extension
);
4168 node
= isl_schedule_node_child(node
, 0);
4171 isl_schedule_node_free(node
);
4172 isl_union_map_free(extension
);
4176 /* Replace the subtree that "node" points to by "tree" (which has
4177 * a sequence root with two children), except if the parent of "node"
4178 * is a sequence as well, in which case "tree" is spliced at the position
4179 * of "node" in its parent.
4180 * Return a pointer to the child of the "tree_pos" (filter) child of "tree"
4181 * in the updated schedule tree.
4183 static __isl_give isl_schedule_node
*graft_or_splice(
4184 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_tree
*tree
,
4189 if (isl_schedule_node_get_parent_type(node
) ==
4190 isl_schedule_node_sequence
) {
4191 pos
= isl_schedule_node_get_child_position(node
);
4192 node
= isl_schedule_node_parent(node
);
4193 node
= isl_schedule_node_sequence_splice(node
, pos
, tree
);
4196 node
= isl_schedule_node_graft_tree(node
, tree
);
4198 node
= isl_schedule_node_child(node
, pos
+ tree_pos
);
4199 node
= isl_schedule_node_child(node
, 0);
4204 /* Insert a node "graft" into the schedule tree of "node" such that it
4205 * is executed before (if "before" is set) or after (if "before" is not set)
4206 * the node that "node" points to.
4207 * The root of "graft" is an extension node.
4208 * Return a pointer to the node that "node" pointed to.
4210 * We first insert an extension node on top of "node" (or extend
4211 * the extension node if there already is one), with a filter on "node"
4212 * separating it from the extension.
4213 * We then insert a filter in the graft to separate it from the original
4214 * domain elements and combine the original and new tree in a sequence.
4215 * If we have extended an extension node, then the children of this
4216 * sequence are spliced in the sequence of the extended extension
4217 * at the position where "node" appears in the original extension.
4218 * Otherwise, the sequence pair is attached to the new extension node.
4220 static __isl_give isl_schedule_node
*graft_extension(
4221 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
,
4224 isl_union_map
*extension
;
4225 isl_union_set
*graft_domain
;
4226 isl_union_set
*node_domain
;
4227 isl_schedule_tree
*tree
, *tree_graft
;
4229 extension
= isl_schedule_node_extension_get_extension(graft
);
4230 graft_domain
= isl_union_map_range(isl_union_map_copy(extension
));
4231 node_domain
= isl_schedule_node_get_universe_domain(node
);
4232 node
= insert_extension(node
, extension
);
4234 graft_domain
= replace_by_universe_if_disjoint(graft_domain
,
4236 isl_union_set_free(node_domain
);
4238 tree
= isl_schedule_node_get_tree(node
);
4239 if (!isl_schedule_node_has_children(graft
)) {
4240 tree_graft
= isl_schedule_tree_from_filter(graft_domain
);
4242 graft
= isl_schedule_node_child(graft
, 0);
4243 tree_graft
= isl_schedule_node_get_tree(graft
);
4244 tree_graft
= isl_schedule_tree_insert_filter(tree_graft
,
4248 tree
= isl_schedule_tree_sequence_pair(tree_graft
, tree
);
4250 tree
= isl_schedule_tree_sequence_pair(tree
, tree_graft
);
4251 node
= graft_or_splice(node
, tree
, before
);
4253 isl_schedule_node_free(graft
);
4258 /* Replace the root domain node of "node" by an extension node suitable
4259 * for insertion at "pos".
4260 * That is, create an extension node that maps the outer band nodes
4261 * at "pos" to the domain of the root node of "node" and attach
4262 * the child of this root node to the extension node.
4264 static __isl_give isl_schedule_node
*extension_from_domain(
4265 __isl_take isl_schedule_node
*node
, __isl_keep isl_schedule_node
*pos
)
4267 isl_union_set
*universe
;
4268 isl_union_set
*domain
;
4273 isl_schedule_node
*res
;
4274 isl_schedule_tree
*tree
;
4276 anchored
= isl_schedule_node_is_subtree_anchored(node
);
4278 return isl_schedule_node_free(node
);
4280 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
4281 "cannot graft anchored tree with domain root",
4282 return isl_schedule_node_free(node
));
4284 depth
= isl_schedule_node_get_schedule_depth(pos
);
4285 domain
= isl_schedule_node_domain_get_domain(node
);
4286 space
= isl_union_set_get_space(domain
);
4287 space
= isl_space_set_from_params(space
);
4288 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
4289 universe
= isl_union_set_from_set(isl_set_universe(space
));
4290 ext
= isl_union_map_from_domain_and_range(universe
, domain
);
4291 res
= isl_schedule_node_from_extension(ext
);
4292 node
= isl_schedule_node_child(node
, 0);
4294 return isl_schedule_node_free(res
);
4295 if (!isl_schedule_tree_is_leaf(node
->tree
)) {
4296 tree
= isl_schedule_node_get_tree(node
);
4297 res
= isl_schedule_node_child(res
, 0);
4298 res
= isl_schedule_node_graft_tree(res
, tree
);
4299 res
= isl_schedule_node_parent(res
);
4301 isl_schedule_node_free(node
);
4306 /* Insert a node "graft" into the schedule tree of "node" such that it
4307 * is executed before (if "before" is set) or after (if "before" is not set)
4308 * the node that "node" points to.
4309 * The root of "graft" may be either a domain or an extension node.
4310 * In the latter case, the domain of the extension needs to correspond
4311 * to the outer band nodes of "node".
4312 * The elements of the domain or the range of the extension may not
4313 * intersect with the domain elements that reach "node".
4314 * The schedule tree of "graft" may not be anchored.
4316 * The schedule tree of "node" is modified to include an extension node
4317 * corresponding to the root node of "graft" as a child of the original
4318 * parent of "node". The original node that "node" points to and the
4319 * child of the root node of "graft" are attached to this extension node
4320 * through a sequence, with appropriate filters and with the child
4321 * of "graft" appearing before or after the original "node".
4323 * If "node" already appears inside a sequence that is the child of
4324 * an extension node and if the spaces of the new domain elements
4325 * do not overlap with those of the original domain elements,
4326 * then that extension node is extended with the new extension
4327 * rather than introducing a new segment of extension and sequence nodes.
4329 * Return a pointer to the same node in the modified tree that
4330 * "node" pointed to in the original tree.
4332 static __isl_give isl_schedule_node
*isl_schedule_node_graft_before_or_after(
4333 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
,
4336 if (!node
|| !graft
)
4338 if (check_insert(node
) < 0)
4341 if (isl_schedule_node_get_type(graft
) == isl_schedule_node_domain
)
4342 graft
= extension_from_domain(graft
, node
);
4346 if (isl_schedule_node_get_type(graft
) != isl_schedule_node_extension
)
4347 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4348 "expecting domain or extension as root of graft",
4351 return graft_extension(node
, graft
, before
);
4353 isl_schedule_node_free(node
);
4354 isl_schedule_node_free(graft
);
4358 /* Insert a node "graft" into the schedule tree of "node" such that it
4359 * is executed before the node that "node" points to.
4360 * The root of "graft" may be either a domain or an extension node.
4361 * In the latter case, the domain of the extension needs to correspond
4362 * to the outer band nodes of "node".
4363 * The elements of the domain or the range of the extension may not
4364 * intersect with the domain elements that reach "node".
4365 * The schedule tree of "graft" may not be anchored.
4367 * Return a pointer to the same node in the modified tree that
4368 * "node" pointed to in the original tree.
4370 __isl_give isl_schedule_node
*isl_schedule_node_graft_before(
4371 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
)
4373 return isl_schedule_node_graft_before_or_after(node
, graft
, 1);
4376 /* Insert a node "graft" into the schedule tree of "node" such that it
4377 * is executed after the node that "node" points to.
4378 * The root of "graft" may be either a domain or an extension node.
4379 * In the latter case, the domain of the extension needs to correspond
4380 * to the outer band nodes of "node".
4381 * The elements of the domain or the range of the extension may not
4382 * intersect with the domain elements that reach "node".
4383 * The schedule tree of "graft" may not be anchored.
4385 * Return a pointer to the same node in the modified tree that
4386 * "node" pointed to in the original tree.
4388 __isl_give isl_schedule_node
*isl_schedule_node_graft_after(
4389 __isl_take isl_schedule_node
*node
,
4390 __isl_take isl_schedule_node
*graft
)
4392 return isl_schedule_node_graft_before_or_after(node
, graft
, 0);
4395 /* Split the domain elements that reach "node" into those that satisfy
4396 * "filter" and those that do not. Arrange for the first subset to be
4397 * executed before or after the second subset, depending on the value
4399 * Return a pointer to the tree corresponding to the second subset,
4400 * except when this subset is empty in which case the original pointer
4402 * If both subsets are non-empty, then a sequence node is introduced
4403 * to impose the order. If the grandparent of the original node was
4404 * itself a sequence, then the original child is replaced by two children
4405 * in this sequence instead.
4406 * The children in the sequence are copies of the original subtree,
4407 * simplified with respect to their filters.
4409 static __isl_give isl_schedule_node
*isl_schedule_node_order_before_or_after(
4410 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
,
4413 enum isl_schedule_node_type ancestors
[] =
4414 { isl_schedule_node_filter
, isl_schedule_node_sequence
};
4415 isl_union_set
*node_domain
, *node_filter
= NULL
, *parent_filter
;
4416 isl_schedule_node
*node2
;
4417 isl_schedule_tree
*tree1
, *tree2
;
4418 isl_bool empty1
, empty2
;
4421 if (!node
|| !filter
)
4423 if (check_insert(node
) < 0)
4426 in_seq
= has_ancestors(node
, 2, ancestors
);
4429 node_domain
= isl_schedule_node_get_domain(node
);
4430 filter
= isl_union_set_gist(filter
, isl_union_set_copy(node_domain
));
4431 node_filter
= isl_union_set_copy(node_domain
);
4432 node_filter
= isl_union_set_subtract(node_filter
,
4433 isl_union_set_copy(filter
));
4434 node_filter
= isl_union_set_gist(node_filter
, node_domain
);
4435 empty1
= isl_union_set_is_empty(filter
);
4436 empty2
= isl_union_set_is_empty(node_filter
);
4437 if (empty1
< 0 || empty2
< 0)
4439 if (empty1
|| empty2
) {
4440 isl_union_set_free(filter
);
4441 isl_union_set_free(node_filter
);
4446 node
= isl_schedule_node_parent(node
);
4447 parent_filter
= isl_schedule_node_filter_get_filter(node
);
4448 node_filter
= isl_union_set_intersect(node_filter
,
4449 isl_union_set_copy(parent_filter
));
4450 filter
= isl_union_set_intersect(filter
, parent_filter
);
4453 node2
= isl_schedule_node_copy(node
);
4454 node
= isl_schedule_node_gist(node
, isl_union_set_copy(node_filter
));
4455 node2
= isl_schedule_node_gist(node2
, isl_union_set_copy(filter
));
4456 tree1
= isl_schedule_node_get_tree(node
);
4457 tree2
= isl_schedule_node_get_tree(node2
);
4458 tree1
= isl_schedule_tree_insert_filter(tree1
, node_filter
);
4459 tree2
= isl_schedule_tree_insert_filter(tree2
, filter
);
4460 isl_schedule_node_free(node2
);
4463 tree1
= isl_schedule_tree_sequence_pair(tree2
, tree1
);
4464 node
= graft_or_splice(node
, tree1
, 1);
4466 tree1
= isl_schedule_tree_sequence_pair(tree1
, tree2
);
4467 node
= graft_or_splice(node
, tree1
, 0);
4472 isl_schedule_node_free(node
);
4473 isl_union_set_free(filter
);
4474 isl_union_set_free(node_filter
);
4478 /* Split the domain elements that reach "node" into those that satisfy
4479 * "filter" and those that do not. Arrange for the first subset to be
4480 * executed before the second subset.
4481 * Return a pointer to the tree corresponding to the second subset,
4482 * except when this subset is empty in which case the original pointer
4485 __isl_give isl_schedule_node
*isl_schedule_node_order_before(
4486 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
4488 return isl_schedule_node_order_before_or_after(node
, filter
, 1);
4491 /* Split the domain elements that reach "node" into those that satisfy
4492 * "filter" and those that do not. Arrange for the first subset to be
4493 * executed after the second subset.
4494 * Return a pointer to the tree corresponding to the second subset,
4495 * except when this subset is empty in which case the original pointer
4498 __isl_give isl_schedule_node
*isl_schedule_node_order_after(
4499 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
4501 return isl_schedule_node_order_before_or_after(node
, filter
, 0);
4504 /* Reset the user pointer on all identifiers of parameters and tuples
4505 * in the schedule node "node".
4507 __isl_give isl_schedule_node
*isl_schedule_node_reset_user(
4508 __isl_take isl_schedule_node
*node
)
4510 isl_schedule_tree
*tree
;
4512 tree
= isl_schedule_node_get_tree(node
);
4513 tree
= isl_schedule_tree_reset_user(tree
);
4514 node
= isl_schedule_node_graft_tree(node
, tree
);
4519 /* Align the parameters of the schedule node "node" to those of "space".
4521 __isl_give isl_schedule_node
*isl_schedule_node_align_params(
4522 __isl_take isl_schedule_node
*node
, __isl_take isl_space
*space
)
4524 isl_schedule_tree
*tree
;
4526 tree
= isl_schedule_node_get_tree(node
);
4527 tree
= isl_schedule_tree_align_params(tree
, space
);
4528 node
= isl_schedule_node_graft_tree(node
, tree
);
4533 /* Compute the pullback of schedule node "node"
4534 * by the function represented by "upma".
4535 * In other words, plug in "upma" in the iteration domains
4536 * of schedule node "node".
4537 * We currently do not handle expansion nodes.
4539 * Note that this is only a helper function for
4540 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
4541 * this function should not be called on a single node without also
4542 * calling it on all the other nodes.
4544 __isl_give isl_schedule_node
*isl_schedule_node_pullback_union_pw_multi_aff(
4545 __isl_take isl_schedule_node
*node
,
4546 __isl_take isl_union_pw_multi_aff
*upma
)
4548 isl_schedule_tree
*tree
;
4550 tree
= isl_schedule_node_get_tree(node
);
4551 tree
= isl_schedule_tree_pullback_union_pw_multi_aff(tree
, upma
);
4552 node
= isl_schedule_node_graft_tree(node
, tree
);
4557 /* Internal data structure for isl_schedule_node_expand.
4558 * "tree" is the tree that needs to be plugged in in all the leaves.
4559 * "domain" is the set of domain elements in the original leaves
4560 * to which the tree applies.
4562 struct isl_schedule_expand_data
{
4563 isl_schedule_tree
*tree
;
4564 isl_union_set
*domain
;
4567 /* If "node" is a leaf, then plug in data->tree, simplifying it
4568 * within its new context.
4570 * If there are any domain elements at the leaf where the tree
4571 * should not be plugged in (i.e., there are elements not in data->domain)
4572 * then first extend the tree to only apply to the elements in data->domain
4573 * by constructing a set node that selects data->tree for elements
4574 * in data->domain and a leaf for the other elements.
4576 static __isl_give isl_schedule_node
*expand(__isl_take isl_schedule_node
*node
,
4579 struct isl_schedule_expand_data
*data
= user
;
4580 isl_schedule_tree
*tree
, *leaf
;
4581 isl_union_set
*domain
, *left
;
4584 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
4587 domain
= isl_schedule_node_get_domain(node
);
4588 tree
= isl_schedule_tree_copy(data
->tree
);
4590 left
= isl_union_set_copy(domain
);
4591 left
= isl_union_set_subtract(left
, isl_union_set_copy(data
->domain
));
4592 empty
= isl_union_set_is_empty(left
);
4593 if (empty
>= 0 && !empty
) {
4594 leaf
= isl_schedule_node_get_leaf(node
);
4595 leaf
= isl_schedule_tree_insert_filter(leaf
, left
);
4596 left
= isl_union_set_copy(data
->domain
);
4597 tree
= isl_schedule_tree_insert_filter(tree
, left
);
4598 tree
= isl_schedule_tree_set_pair(tree
, leaf
);
4601 node
= isl_schedule_node_free(node
);
4602 isl_union_set_free(left
);
4605 node
= isl_schedule_node_graft_tree(node
, tree
);
4606 node
= isl_schedule_node_gist(node
, domain
);
4611 /* Expand the tree rooted at "node" by extending all leaves
4612 * with an expansion node with as child "tree".
4613 * The expansion is determined by "contraction" and "domain".
4614 * That is, the elements of "domain" are contracted according
4615 * to "contraction". The expansion relation is then the inverse
4616 * of "contraction" with its range intersected with "domain".
4618 * Insert the appropriate expansion node on top of "tree" and
4619 * then plug in the result in all leaves of "node".
4621 __isl_give isl_schedule_node
*isl_schedule_node_expand(
4622 __isl_take isl_schedule_node
*node
,
4623 __isl_take isl_union_pw_multi_aff
*contraction
,
4624 __isl_take isl_union_set
*domain
,
4625 __isl_take isl_schedule_tree
*tree
)
4627 struct isl_schedule_expand_data data
;
4628 isl_union_map
*expansion
;
4629 isl_union_pw_multi_aff
*copy
;
4631 if (!node
|| !contraction
|| !tree
)
4632 node
= isl_schedule_node_free(node
);
4634 copy
= isl_union_pw_multi_aff_copy(contraction
);
4635 expansion
= isl_union_map_from_union_pw_multi_aff(copy
);
4636 expansion
= isl_union_map_reverse(expansion
);
4637 expansion
= isl_union_map_intersect_range(expansion
, domain
);
4638 data
.domain
= isl_union_map_domain(isl_union_map_copy(expansion
));
4640 tree
= isl_schedule_tree_insert_expansion(tree
, contraction
, expansion
);
4643 node
= isl_schedule_node_map_descendant_bottom_up(node
, &expand
, &data
);
4644 isl_union_set_free(data
.domain
);
4645 isl_schedule_tree_free(data
.tree
);
4649 /* Return the position of the subtree containing "node" among the children
4650 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
4651 * In particular, both nodes should point to the same schedule tree.
4653 * Return -1 on error.
4655 int isl_schedule_node_get_ancestor_child_position(
4656 __isl_keep isl_schedule_node
*node
,
4657 __isl_keep isl_schedule_node
*ancestor
)
4660 isl_schedule_tree
*tree
;
4662 if (!node
|| !ancestor
)
4665 if (node
->schedule
!= ancestor
->schedule
)
4666 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4667 "not a descendant", return -1);
4669 n1
= isl_schedule_node_get_tree_depth(ancestor
);
4670 n2
= isl_schedule_node_get_tree_depth(node
);
4673 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4674 "not a descendant", return -1);
4675 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, n1
);
4676 isl_schedule_tree_free(tree
);
4677 if (tree
!= ancestor
->tree
)
4678 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4679 "not a descendant", return -1);
4681 return node
->child_pos
[n1
];
4684 /* Given two nodes that point to the same schedule tree, return their
4685 * closest shared ancestor.
4687 * Since the two nodes point to the same schedule, they share at least
4688 * one ancestor, the root of the schedule. We move down from the root
4689 * to the first ancestor where the respective children have a different
4690 * child position. This is the requested ancestor.
4691 * If there is no ancestor where the children have a different position,
4692 * then one node is an ancestor of the other and then this node is
4693 * the requested ancestor.
4695 __isl_give isl_schedule_node
*isl_schedule_node_get_shared_ancestor(
4696 __isl_keep isl_schedule_node
*node1
,
4697 __isl_keep isl_schedule_node
*node2
)
4701 if (!node1
|| !node2
)
4703 if (node1
->schedule
!= node2
->schedule
)
4704 isl_die(isl_schedule_node_get_ctx(node1
), isl_error_invalid
,
4705 "not part of same schedule", return NULL
);
4706 n1
= isl_schedule_node_get_tree_depth(node1
);
4707 n2
= isl_schedule_node_get_tree_depth(node2
);
4709 return isl_schedule_node_get_shared_ancestor(node2
, node1
);
4711 return isl_schedule_node_copy(node1
);
4712 if (isl_schedule_node_is_equal(node1
, node2
))
4713 return isl_schedule_node_copy(node1
);
4715 for (i
= 0; i
< n1
; ++i
)
4716 if (node1
->child_pos
[i
] != node2
->child_pos
[i
])
4719 node1
= isl_schedule_node_copy(node1
);
4720 return isl_schedule_node_ancestor(node1
, n1
- i
);
4723 /* Print "node" to "p".
4725 __isl_give isl_printer
*isl_printer_print_schedule_node(
4726 __isl_take isl_printer
*p
, __isl_keep isl_schedule_node
*node
)
4729 return isl_printer_free(p
);
4730 return isl_printer_print_schedule_tree_mark(p
, node
->schedule
->root
,
4731 isl_schedule_tree_list_n_schedule_tree(node
->ancestors
),
4735 void isl_schedule_node_dump(__isl_keep isl_schedule_node
*node
)
4738 isl_printer
*printer
;
4743 ctx
= isl_schedule_node_get_ctx(node
);
4744 printer
= isl_printer_to_file(ctx
, stderr
);
4745 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
4746 printer
= isl_printer_print_schedule_node(printer
, node
);
4748 isl_printer_free(printer
);
4751 /* Return a string representation of "node".
4752 * Print the schedule node in block format as it would otherwise
4753 * look identical to the entire schedule.
4755 __isl_give
char *isl_schedule_node_to_str(__isl_keep isl_schedule_node
*node
)
4757 isl_printer
*printer
;
4763 printer
= isl_printer_to_str(isl_schedule_node_get_ctx(node
));
4764 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
4765 printer
= isl_printer_print_schedule_node(printer
, node
);
4766 s
= isl_printer_get_str(printer
);
4767 isl_printer_free(printer
);