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
15 #include <isl_schedule_band.h>
16 #include <isl_schedule_private.h>
17 #include <isl_schedule_node_private.h>
19 /* Create a new schedule node in the given schedule, point at the given
20 * tree with given ancestors and child positions.
21 * "child_pos" may be NULL if there are no ancestors.
23 __isl_give isl_schedule_node
*isl_schedule_node_alloc(
24 __isl_take isl_schedule
*schedule
, __isl_take isl_schedule_tree
*tree
,
25 __isl_take isl_schedule_tree_list
*ancestors
, int *child_pos
)
28 isl_schedule_node
*node
;
31 if (!schedule
|| !tree
|| !ancestors
)
33 n
= isl_schedule_tree_list_n_schedule_tree(ancestors
);
34 if (n
> 0 && !child_pos
)
36 ctx
= isl_schedule_get_ctx(schedule
);
37 node
= isl_calloc_type(ctx
, isl_schedule_node
);
41 node
->schedule
= schedule
;
43 node
->ancestors
= ancestors
;
44 node
->child_pos
= isl_alloc_array(ctx
, int, n
);
45 if (n
&& !node
->child_pos
)
46 return isl_schedule_node_free(node
);
47 for (i
= 0; i
< n
; ++i
)
48 node
->child_pos
[i
] = child_pos
[i
];
52 isl_schedule_free(schedule
);
53 isl_schedule_tree_free(tree
);
54 isl_schedule_tree_list_free(ancestors
);
58 /* Return a pointer to the root of a schedule tree with as single
59 * node a domain node with the given domain.
61 __isl_give isl_schedule_node
*isl_schedule_node_from_domain(
62 __isl_take isl_union_set
*domain
)
64 isl_schedule
*schedule
;
65 isl_schedule_node
*node
;
67 schedule
= isl_schedule_from_domain(domain
);
68 node
= isl_schedule_get_root(schedule
);
69 isl_schedule_free(schedule
);
74 /* Return a pointer to the root of a schedule tree with as single
75 * node a extension node with the given extension.
77 __isl_give isl_schedule_node
*isl_schedule_node_from_extension(
78 __isl_take isl_union_map
*extension
)
81 isl_schedule
*schedule
;
82 isl_schedule_tree
*tree
;
83 isl_schedule_node
*node
;
88 ctx
= isl_union_map_get_ctx(extension
);
89 tree
= isl_schedule_tree_from_extension(extension
);
90 schedule
= isl_schedule_from_schedule_tree(ctx
, tree
);
91 node
= isl_schedule_get_root(schedule
);
92 isl_schedule_free(schedule
);
97 /* Return the isl_ctx to which "node" belongs.
99 isl_ctx
*isl_schedule_node_get_ctx(__isl_keep isl_schedule_node
*node
)
101 return node
? isl_schedule_get_ctx(node
->schedule
) : NULL
;
104 /* Return a pointer to the leaf of the schedule into which "node" points.
106 __isl_keep isl_schedule_tree
*isl_schedule_node_peek_leaf(
107 __isl_keep isl_schedule_node
*node
)
109 return node
? isl_schedule_peek_leaf(node
->schedule
) : NULL
;
112 /* Return a copy of the leaf of the schedule into which "node" points.
114 __isl_give isl_schedule_tree
*isl_schedule_node_get_leaf(
115 __isl_keep isl_schedule_node
*node
)
117 return isl_schedule_tree_copy(isl_schedule_node_peek_leaf(node
));
120 /* Return the type of the node or isl_schedule_node_error on error.
122 enum isl_schedule_node_type
isl_schedule_node_get_type(
123 __isl_keep isl_schedule_node
*node
)
125 return node
? isl_schedule_tree_get_type(node
->tree
)
126 : isl_schedule_node_error
;
129 /* Return the type of the parent of "node" or isl_schedule_node_error on error.
131 enum isl_schedule_node_type
isl_schedule_node_get_parent_type(
132 __isl_keep isl_schedule_node
*node
)
136 isl_schedule_tree
*parent
;
137 enum isl_schedule_node_type type
;
140 return isl_schedule_node_error
;
141 has_parent
= isl_schedule_node_has_parent(node
);
143 return isl_schedule_node_error
;
145 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
146 "node has no parent", return isl_schedule_node_error
);
148 pos
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
) - 1;
149 parent
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, pos
);
150 type
= isl_schedule_tree_get_type(parent
);
151 isl_schedule_tree_free(parent
);
156 /* Return a copy of the subtree that this node points to.
158 __isl_give isl_schedule_tree
*isl_schedule_node_get_tree(
159 __isl_keep isl_schedule_node
*node
)
164 return isl_schedule_tree_copy(node
->tree
);
167 /* Return a copy of the schedule into which "node" points.
169 __isl_give isl_schedule
*isl_schedule_node_get_schedule(
170 __isl_keep isl_schedule_node
*node
)
174 return isl_schedule_copy(node
->schedule
);
177 /* Return a fresh copy of "node".
179 __isl_take isl_schedule_node
*isl_schedule_node_dup(
180 __isl_keep isl_schedule_node
*node
)
185 return isl_schedule_node_alloc(isl_schedule_copy(node
->schedule
),
186 isl_schedule_tree_copy(node
->tree
),
187 isl_schedule_tree_list_copy(node
->ancestors
),
191 /* Return an isl_schedule_node that is equal to "node" and that has only
192 * a single reference.
194 __isl_give isl_schedule_node
*isl_schedule_node_cow(
195 __isl_take isl_schedule_node
*node
)
203 return isl_schedule_node_dup(node
);
206 /* Return a new reference to "node".
208 __isl_give isl_schedule_node
*isl_schedule_node_copy(
209 __isl_keep isl_schedule_node
*node
)
218 /* Free "node" and return NULL.
220 __isl_null isl_schedule_node
*isl_schedule_node_free(
221 __isl_take isl_schedule_node
*node
)
228 isl_schedule_tree_list_free(node
->ancestors
);
229 free(node
->child_pos
);
230 isl_schedule_tree_free(node
->tree
);
231 isl_schedule_free(node
->schedule
);
237 /* Do "node1" and "node2" point to the same position in the same
240 isl_bool
isl_schedule_node_is_equal(__isl_keep isl_schedule_node
*node1
,
241 __isl_keep isl_schedule_node
*node2
)
245 if (!node1
|| !node2
)
246 return isl_bool_error
;
248 return isl_bool_true
;
249 if (node1
->schedule
!= node2
->schedule
)
250 return isl_bool_false
;
252 n1
= isl_schedule_node_get_tree_depth(node1
);
253 n2
= isl_schedule_node_get_tree_depth(node2
);
255 return isl_bool_false
;
256 for (i
= 0; i
< n1
; ++i
)
257 if (node1
->child_pos
[i
] != node2
->child_pos
[i
])
258 return isl_bool_false
;
260 return isl_bool_true
;
263 /* Return the number of outer schedule dimensions of "node"
264 * in its schedule tree.
266 * Return -1 on error.
268 int isl_schedule_node_get_schedule_depth(__isl_keep isl_schedule_node
*node
)
276 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
277 for (i
= n
- 1; i
>= 0; --i
) {
278 isl_schedule_tree
*tree
;
280 tree
= isl_schedule_tree_list_get_schedule_tree(
284 if (tree
->type
== isl_schedule_node_band
)
285 depth
+= isl_schedule_tree_band_n_member(tree
);
286 isl_schedule_tree_free(tree
);
292 /* Internal data structure for
293 * isl_schedule_node_get_prefix_schedule_union_pw_multi_aff
295 * "initialized" is set if the filter field has been initialized.
296 * If "universe_domain" is not set, then the collected filter is intersected
297 * with the the domain of the root domain node.
298 * "universe_filter" is set if we are only collecting the universes of filters
299 * "collect_prefix" is set if we are collecting prefixes.
300 * "filter" collects all outer filters and is NULL until "initialized" is set.
301 * "prefix" collects all outer band partial schedules (if "collect_prefix"
302 * is set). If it is used, then it is initialized by the caller
303 * of collect_filter_prefix to a zero-dimensional function.
305 struct isl_schedule_node_get_filter_prefix_data
{
310 isl_union_set
*filter
;
311 isl_multi_union_pw_aff
*prefix
;
314 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list
*list
,
315 int n
, struct isl_schedule_node_get_filter_prefix_data
*data
);
317 /* Update the filter and prefix information in "data" based on the first "n"
318 * elements in "list" and the expansion tree root "tree".
320 * We first collect the information from the elements in "list",
321 * initializing the filter based on the domain of the expansion.
322 * Then we map the results to the expanded space and combined them
323 * with the results already in "data".
325 static int collect_filter_prefix_expansion(__isl_take isl_schedule_tree
*tree
,
326 __isl_keep isl_schedule_tree_list
*list
, int n
,
327 struct isl_schedule_node_get_filter_prefix_data
*data
)
329 struct isl_schedule_node_get_filter_prefix_data contracted
;
330 isl_union_pw_multi_aff
*c
;
331 isl_union_map
*exp
, *universe
;
332 isl_union_set
*filter
;
334 c
= isl_schedule_tree_expansion_get_contraction(tree
);
335 exp
= isl_schedule_tree_expansion_get_expansion(tree
);
337 contracted
.initialized
= 1;
338 contracted
.universe_domain
= data
->universe_domain
;
339 contracted
.universe_filter
= data
->universe_filter
;
340 contracted
.collect_prefix
= data
->collect_prefix
;
341 universe
= isl_union_map_universe(isl_union_map_copy(exp
));
342 filter
= isl_union_map_domain(universe
);
343 if (data
->collect_prefix
) {
344 isl_space
*space
= isl_union_set_get_space(filter
);
345 space
= isl_space_set_from_params(space
);
346 contracted
.prefix
= isl_multi_union_pw_aff_zero(space
);
348 contracted
.filter
= filter
;
350 if (collect_filter_prefix(list
, n
, &contracted
) < 0)
351 contracted
.filter
= isl_union_set_free(contracted
.filter
);
352 if (data
->collect_prefix
) {
353 isl_multi_union_pw_aff
*prefix
;
355 prefix
= contracted
.prefix
;
357 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
358 isl_union_pw_multi_aff_copy(c
));
359 data
->prefix
= isl_multi_union_pw_aff_flat_range_product(
360 prefix
, data
->prefix
);
362 filter
= contracted
.filter
;
363 if (data
->universe_domain
)
364 filter
= isl_union_set_preimage_union_pw_multi_aff(filter
,
365 isl_union_pw_multi_aff_copy(c
));
367 filter
= isl_union_set_apply(filter
, isl_union_map_copy(exp
));
368 if (!data
->initialized
)
369 data
->filter
= filter
;
371 data
->filter
= isl_union_set_intersect(filter
, data
->filter
);
372 data
->initialized
= 1;
374 isl_union_pw_multi_aff_free(c
);
375 isl_union_map_free(exp
);
376 isl_schedule_tree_free(tree
);
381 /* Update the filter information in "data" based on the first "n"
382 * elements in "list" and the extension tree root "tree", in case
383 * data->universe_domain is set and data->collect_prefix is not.
385 * We collect the universe domain of the elements in "list" and
386 * add it to the universe range of the extension (intersected
387 * with the already collected filter, if any).
389 static int collect_universe_domain_extension(__isl_take isl_schedule_tree
*tree
,
390 __isl_keep isl_schedule_tree_list
*list
, int n
,
391 struct isl_schedule_node_get_filter_prefix_data
*data
)
393 struct isl_schedule_node_get_filter_prefix_data data_outer
;
394 isl_union_map
*extension
;
395 isl_union_set
*filter
;
397 data_outer
.initialized
= 0;
398 data_outer
.universe_domain
= 1;
399 data_outer
.universe_filter
= data
->universe_filter
;
400 data_outer
.collect_prefix
= 0;
401 data_outer
.filter
= NULL
;
402 data_outer
.prefix
= NULL
;
404 if (collect_filter_prefix(list
, n
, &data_outer
) < 0)
405 data_outer
.filter
= isl_union_set_free(data_outer
.filter
);
407 extension
= isl_schedule_tree_extension_get_extension(tree
);
408 extension
= isl_union_map_universe(extension
);
409 filter
= isl_union_map_range(extension
);
410 if (data_outer
.initialized
)
411 filter
= isl_union_set_union(filter
, data_outer
.filter
);
412 if (data
->initialized
)
413 filter
= isl_union_set_intersect(filter
, data
->filter
);
415 data
->filter
= filter
;
417 isl_schedule_tree_free(tree
);
422 /* Update "data" based on the tree node "tree" in case "data" has
423 * not been initialized yet.
425 * Return 0 on success and -1 on error.
427 * If "tree" is a filter, then we set data->filter to this filter
429 * If "tree" is a domain, then this means we have reached the root
430 * of the schedule tree without being able to extract any information.
431 * We therefore initialize data->filter to the universe of the domain,
432 * or the domain itself if data->universe_domain is not set.
433 * If "tree" is a band with at least one member, then we set data->filter
434 * to the universe of the schedule domain and replace the zero-dimensional
435 * data->prefix by the band schedule (if data->collect_prefix is set).
437 static int collect_filter_prefix_init(__isl_keep isl_schedule_tree
*tree
,
438 struct isl_schedule_node_get_filter_prefix_data
*data
)
440 enum isl_schedule_node_type type
;
441 isl_multi_union_pw_aff
*mupa
;
442 isl_union_set
*filter
;
444 type
= isl_schedule_tree_get_type(tree
);
446 case isl_schedule_node_error
:
448 case isl_schedule_node_expansion
:
449 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
450 "should be handled by caller", return -1);
451 case isl_schedule_node_extension
:
452 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
453 "cannot handle extension nodes", return -1);
454 case isl_schedule_node_context
:
455 case isl_schedule_node_leaf
:
456 case isl_schedule_node_guard
:
457 case isl_schedule_node_mark
:
458 case isl_schedule_node_sequence
:
459 case isl_schedule_node_set
:
461 case isl_schedule_node_domain
:
462 filter
= isl_schedule_tree_domain_get_domain(tree
);
463 if (data
->universe_domain
)
464 filter
= isl_union_set_universe(filter
);
465 data
->filter
= filter
;
467 case isl_schedule_node_band
:
468 if (isl_schedule_tree_band_n_member(tree
) == 0)
470 mupa
= isl_schedule_tree_band_get_partial_schedule(tree
);
471 if (data
->collect_prefix
) {
472 isl_multi_union_pw_aff_free(data
->prefix
);
473 mupa
= isl_multi_union_pw_aff_reset_tuple_id(mupa
,
475 data
->prefix
= isl_multi_union_pw_aff_copy(mupa
);
477 filter
= isl_multi_union_pw_aff_domain(mupa
);
478 filter
= isl_union_set_universe(filter
);
479 data
->filter
= filter
;
481 case isl_schedule_node_filter
:
482 filter
= isl_schedule_tree_filter_get_filter(tree
);
483 if (data
->universe_filter
)
484 filter
= isl_union_set_universe(filter
);
485 data
->filter
= filter
;
489 if ((data
->collect_prefix
&& !data
->prefix
) || !data
->filter
)
492 data
->initialized
= 1;
497 /* Update "data" based on the tree node "tree" in case "data" has
498 * already been initialized.
500 * Return 0 on success and -1 on error.
502 * If "tree" is a domain and data->universe_domain is not set, then
503 * intersect data->filter with the domain.
504 * If "tree" is a filter, then we intersect data->filter with this filter
506 * If "tree" is a band with at least one member and data->collect_prefix
507 * is set, then we extend data->prefix with the band schedule.
508 * If "tree" is an extension, then we make sure that we are not collecting
509 * information on any extended domain elements.
511 static int collect_filter_prefix_update(__isl_keep isl_schedule_tree
*tree
,
512 struct isl_schedule_node_get_filter_prefix_data
*data
)
514 enum isl_schedule_node_type type
;
515 isl_multi_union_pw_aff
*mupa
;
516 isl_union_set
*filter
;
517 isl_union_map
*extension
;
520 type
= isl_schedule_tree_get_type(tree
);
522 case isl_schedule_node_error
:
524 case isl_schedule_node_expansion
:
525 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
526 "should be handled by caller", return -1);
527 case isl_schedule_node_extension
:
528 extension
= isl_schedule_tree_extension_get_extension(tree
);
529 extension
= isl_union_map_intersect_range(extension
,
530 isl_union_set_copy(data
->filter
));
531 empty
= isl_union_map_is_empty(extension
);
532 isl_union_map_free(extension
);
537 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
538 "cannot handle extension nodes", return -1);
539 case isl_schedule_node_context
:
540 case isl_schedule_node_leaf
:
541 case isl_schedule_node_guard
:
542 case isl_schedule_node_mark
:
543 case isl_schedule_node_sequence
:
544 case isl_schedule_node_set
:
546 case isl_schedule_node_domain
:
547 if (data
->universe_domain
)
549 filter
= isl_schedule_tree_domain_get_domain(tree
);
550 data
->filter
= isl_union_set_intersect(data
->filter
, filter
);
552 case isl_schedule_node_band
:
553 if (isl_schedule_tree_band_n_member(tree
) == 0)
555 if (!data
->collect_prefix
)
557 mupa
= isl_schedule_tree_band_get_partial_schedule(tree
);
558 data
->prefix
= isl_multi_union_pw_aff_flat_range_product(mupa
,
563 case isl_schedule_node_filter
:
564 filter
= isl_schedule_tree_filter_get_filter(tree
);
565 if (data
->universe_filter
)
566 filter
= isl_union_set_universe(filter
);
567 data
->filter
= isl_union_set_intersect(data
->filter
, filter
);
576 /* Collect filter and/or prefix information from the first "n"
577 * elements in "list" (which represent the ancestors of a node).
578 * Store the results in "data".
580 * Extension nodes are only supported if they do not affect the outcome,
581 * i.e., if we are collecting information on non-extended domain elements,
582 * or if we are collecting the universe domain (without prefix).
584 * Return 0 on success and -1 on error.
586 * We traverse the list from innermost ancestor (last element)
587 * to outermost ancestor (first element), calling collect_filter_prefix_init
588 * on each node as long as we have not been able to extract any information
589 * yet and collect_filter_prefix_update afterwards.
590 * If we come across an expansion node, then we interrupt the traversal
591 * and call collect_filter_prefix_expansion to restart the traversal
592 * over the remaining ancestors and to combine the results with those
593 * that have already been collected.
594 * If we come across an extension node and we are only computing
595 * the universe domain, then we interrupt the traversal and call
596 * collect_universe_domain_extension to restart the traversal
597 * over the remaining ancestors and to combine the results with those
598 * that have already been collected.
599 * On successful return, data->initialized will be set since the outermost
600 * ancestor is a domain node, which always results in an initialization.
602 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list
*list
,
603 int n
, struct isl_schedule_node_get_filter_prefix_data
*data
)
610 for (i
= n
- 1; i
>= 0; --i
) {
611 isl_schedule_tree
*tree
;
612 enum isl_schedule_node_type type
;
615 tree
= isl_schedule_tree_list_get_schedule_tree(list
, i
);
618 type
= isl_schedule_tree_get_type(tree
);
619 if (type
== isl_schedule_node_expansion
)
620 return collect_filter_prefix_expansion(tree
, list
, i
,
622 if (type
== isl_schedule_node_extension
&&
623 data
->universe_domain
&& !data
->collect_prefix
)
624 return collect_universe_domain_extension(tree
, list
, i
,
626 if (!data
->initialized
)
627 r
= collect_filter_prefix_init(tree
, data
);
629 r
= collect_filter_prefix_update(tree
, data
);
630 isl_schedule_tree_free(tree
);
638 /* Return the concatenation of the partial schedules of all outer band
639 * nodes of "node" interesected with all outer filters
640 * as an isl_multi_union_pw_aff.
641 * None of the ancestors of "node" may be an extension node, unless
642 * there is also a filter ancestor that filters out all the extended
645 * If "node" is pointing at the root of the schedule tree, then
646 * there are no domain elements reaching the current node, so
647 * we return an empty result.
649 * We collect all the filters and partial schedules in collect_filter_prefix
650 * and intersect the domain of the combined schedule with the combined filter.
652 __isl_give isl_multi_union_pw_aff
*
653 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(
654 __isl_keep isl_schedule_node
*node
)
658 struct isl_schedule_node_get_filter_prefix_data data
;
663 space
= isl_schedule_get_space(node
->schedule
);
664 space
= isl_space_set_from_params(space
);
665 if (node
->tree
== node
->schedule
->root
)
666 return isl_multi_union_pw_aff_zero(space
);
668 data
.initialized
= 0;
669 data
.universe_domain
= 1;
670 data
.universe_filter
= 0;
671 data
.collect_prefix
= 1;
673 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
675 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
676 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
677 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
679 data
.prefix
= isl_multi_union_pw_aff_intersect_domain(data
.prefix
,
685 /* Return the concatenation of the partial schedules of all outer band
686 * nodes of "node" interesected with all outer filters
687 * as an isl_union_pw_multi_aff.
688 * None of the ancestors of "node" may be an extension node, unless
689 * there is also a filter ancestor that filters out all the extended
692 * If "node" is pointing at the root of the schedule tree, then
693 * there are no domain elements reaching the current node, so
694 * we return an empty result.
696 * We collect all the filters and partial schedules in collect_filter_prefix.
697 * The partial schedules are collected as an isl_multi_union_pw_aff.
698 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
699 * contain any domain information, so we construct the isl_union_pw_multi_aff
700 * result as a zero-dimensional function on the collected filter.
701 * Otherwise, we convert the isl_multi_union_pw_aff to
702 * an isl_multi_union_pw_aff and intersect the domain with the filter.
704 __isl_give isl_union_pw_multi_aff
*
705 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
706 __isl_keep isl_schedule_node
*node
)
710 isl_union_pw_multi_aff
*prefix
;
711 struct isl_schedule_node_get_filter_prefix_data data
;
716 space
= isl_schedule_get_space(node
->schedule
);
717 if (node
->tree
== node
->schedule
->root
)
718 return isl_union_pw_multi_aff_empty(space
);
720 space
= isl_space_set_from_params(space
);
721 data
.initialized
= 0;
722 data
.universe_domain
= 1;
723 data
.universe_filter
= 0;
724 data
.collect_prefix
= 1;
726 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
728 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
729 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
730 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
733 isl_multi_union_pw_aff_dim(data
.prefix
, isl_dim_set
) == 0) {
734 isl_multi_union_pw_aff_free(data
.prefix
);
735 prefix
= isl_union_pw_multi_aff_from_domain(data
.filter
);
738 isl_union_pw_multi_aff_from_multi_union_pw_aff(data
.prefix
);
739 prefix
= isl_union_pw_multi_aff_intersect_domain(prefix
,
746 /* Return the concatenation of the partial schedules of all outer band
747 * nodes of "node" interesected with all outer filters
748 * as an isl_union_map.
750 __isl_give isl_union_map
*isl_schedule_node_get_prefix_schedule_union_map(
751 __isl_keep isl_schedule_node
*node
)
753 isl_union_pw_multi_aff
*upma
;
755 upma
= isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
756 return isl_union_map_from_union_pw_multi_aff(upma
);
759 /* Return the concatenation of the partial schedules of all outer band
760 * nodes of "node" intersected with all outer domain constraints.
761 * None of the ancestors of "node" may be an extension node, unless
762 * there is also a filter ancestor that filters out all the extended
765 * Essentially, this function intersects the domain of the output
766 * of isl_schedule_node_get_prefix_schedule_union_map with the output
767 * of isl_schedule_node_get_domain, except that it only traverses
768 * the ancestors of "node" once.
770 __isl_give isl_union_map
*isl_schedule_node_get_prefix_schedule_relation(
771 __isl_keep isl_schedule_node
*node
)
775 isl_union_map
*prefix
;
776 struct isl_schedule_node_get_filter_prefix_data data
;
781 space
= isl_schedule_get_space(node
->schedule
);
782 if (node
->tree
== node
->schedule
->root
)
783 return isl_union_map_empty(space
);
785 space
= isl_space_set_from_params(space
);
786 data
.initialized
= 0;
787 data
.universe_domain
= 0;
788 data
.universe_filter
= 0;
789 data
.collect_prefix
= 1;
791 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
793 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
794 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
795 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
798 isl_multi_union_pw_aff_dim(data
.prefix
, isl_dim_set
) == 0) {
799 isl_multi_union_pw_aff_free(data
.prefix
);
800 prefix
= isl_union_map_from_domain(data
.filter
);
802 prefix
= isl_union_map_from_multi_union_pw_aff(data
.prefix
);
803 prefix
= isl_union_map_intersect_domain(prefix
, data
.filter
);
809 /* Return the domain elements that reach "node".
811 * If "node" is pointing at the root of the schedule tree, then
812 * there are no domain elements reaching the current node, so
813 * we return an empty result.
814 * None of the ancestors of "node" may be an extension node, unless
815 * there is also a filter ancestor that filters out all the extended
818 * Otherwise, we collect all filters reaching the node,
819 * intersected with the root domain in collect_filter_prefix.
821 __isl_give isl_union_set
*isl_schedule_node_get_domain(
822 __isl_keep isl_schedule_node
*node
)
825 struct isl_schedule_node_get_filter_prefix_data data
;
830 if (node
->tree
== node
->schedule
->root
) {
833 space
= isl_schedule_get_space(node
->schedule
);
834 return isl_union_set_empty(space
);
837 data
.initialized
= 0;
838 data
.universe_domain
= 0;
839 data
.universe_filter
= 0;
840 data
.collect_prefix
= 0;
844 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
845 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
846 data
.filter
= isl_union_set_free(data
.filter
);
851 /* Return the union of universe sets of the domain elements that reach "node".
853 * If "node" is pointing at the root of the schedule tree, then
854 * there are no domain elements reaching the current node, so
855 * we return an empty result.
857 * Otherwise, we collect the universes of all filters reaching the node
858 * in collect_filter_prefix.
860 __isl_give isl_union_set
*isl_schedule_node_get_universe_domain(
861 __isl_keep isl_schedule_node
*node
)
864 struct isl_schedule_node_get_filter_prefix_data data
;
869 if (node
->tree
== node
->schedule
->root
) {
872 space
= isl_schedule_get_space(node
->schedule
);
873 return isl_union_set_empty(space
);
876 data
.initialized
= 0;
877 data
.universe_domain
= 1;
878 data
.universe_filter
= 1;
879 data
.collect_prefix
= 0;
883 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
884 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
885 data
.filter
= isl_union_set_free(data
.filter
);
890 /* Return the subtree schedule of "node".
892 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
893 * trees that do not contain any schedule information, we first
894 * move down to the first relevant descendant and handle leaves ourselves.
896 * If the subtree rooted at "node" contains any expansion nodes, then
897 * the returned subtree schedule is formulated in terms of the expanded
899 * The subtree is not allowed to contain any extension nodes.
901 __isl_give isl_union_map
*isl_schedule_node_get_subtree_schedule_union_map(
902 __isl_keep isl_schedule_node
*node
)
904 isl_schedule_tree
*tree
, *leaf
;
907 tree
= isl_schedule_node_get_tree(node
);
908 leaf
= isl_schedule_node_peek_leaf(node
);
909 tree
= isl_schedule_tree_first_schedule_descendant(tree
, leaf
);
913 isl_union_set
*domain
;
914 domain
= isl_schedule_node_get_universe_domain(node
);
915 isl_schedule_tree_free(tree
);
916 return isl_union_map_from_domain(domain
);
919 umap
= isl_schedule_tree_get_subtree_schedule_union_map(tree
);
920 isl_schedule_tree_free(tree
);
924 /* Return the number of ancestors of "node" in its schedule tree.
926 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node
*node
)
930 return isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
933 /* Does "node" have a parent?
935 * That is, does it point to any node of the schedule other than the root?
937 isl_bool
isl_schedule_node_has_parent(__isl_keep isl_schedule_node
*node
)
940 return isl_bool_error
;
941 if (!node
->ancestors
)
942 return isl_bool_error
;
944 return isl_schedule_tree_list_n_schedule_tree(node
->ancestors
) != 0;
947 /* Return the position of "node" among the children of its parent.
949 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node
*node
)
956 has_parent
= isl_schedule_node_has_parent(node
);
960 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
961 "node has no parent", return -1);
963 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
964 return node
->child_pos
[n
- 1];
967 /* Does the parent (if any) of "node" have any children with a smaller child
968 * position than this one?
970 isl_bool
isl_schedule_node_has_previous_sibling(
971 __isl_keep isl_schedule_node
*node
)
977 return isl_bool_error
;
978 has_parent
= isl_schedule_node_has_parent(node
);
979 if (has_parent
< 0 || !has_parent
)
982 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
984 return node
->child_pos
[n
- 1] > 0;
987 /* Does the parent (if any) of "node" have any children with a greater child
988 * position than this one?
990 isl_bool
isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node
*node
)
994 isl_schedule_tree
*tree
;
997 return isl_bool_error
;
998 has_parent
= isl_schedule_node_has_parent(node
);
999 if (has_parent
< 0 || !has_parent
)
1002 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1003 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, n
- 1);
1005 return isl_bool_error
;
1006 n_child
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
1007 isl_schedule_tree_free(tree
);
1009 return node
->child_pos
[n
- 1] + 1 < n_child
;
1012 /* Does "node" have any children?
1014 * Any node other than the leaf nodes is considered to have at least
1015 * one child, even if the corresponding isl_schedule_tree does not
1016 * have any children.
1018 isl_bool
isl_schedule_node_has_children(__isl_keep isl_schedule_node
*node
)
1021 return isl_bool_error
;
1022 return !isl_schedule_tree_is_leaf(node
->tree
);
1025 /* Return the number of children of "node"?
1027 * Any node other than the leaf nodes is considered to have at least
1028 * one child, even if the corresponding isl_schedule_tree does not
1029 * have any children. That is, the number of children of "node" is
1030 * only zero if its tree is the explicit empty tree. Otherwise,
1031 * if the isl_schedule_tree has any children, then it is equal
1032 * to the number of children of "node". If it has zero children,
1033 * then "node" still has a leaf node as child.
1035 int isl_schedule_node_n_children(__isl_keep isl_schedule_node
*node
)
1042 if (isl_schedule_tree_is_leaf(node
->tree
))
1045 n
= isl_schedule_tree_n_children(node
->tree
);
1052 /* Move the "node" pointer to the ancestor of the given generation
1053 * of the node it currently points to, where generation 0 is the node
1054 * itself and generation 1 is its parent.
1056 __isl_give isl_schedule_node
*isl_schedule_node_ancestor(
1057 __isl_take isl_schedule_node
*node
, int generation
)
1060 isl_schedule_tree
*tree
;
1064 if (generation
== 0)
1066 n
= isl_schedule_node_get_tree_depth(node
);
1068 return isl_schedule_node_free(node
);
1069 if (generation
< 0 || generation
> n
)
1070 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1071 "generation out of bounds",
1072 return isl_schedule_node_free(node
));
1073 node
= isl_schedule_node_cow(node
);
1077 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1079 isl_schedule_tree_free(node
->tree
);
1081 node
->ancestors
= isl_schedule_tree_list_drop(node
->ancestors
,
1082 n
- generation
, generation
);
1083 if (!node
->ancestors
|| !node
->tree
)
1084 return isl_schedule_node_free(node
);
1089 /* Move the "node" pointer to the parent of the node it currently points to.
1091 __isl_give isl_schedule_node
*isl_schedule_node_parent(
1092 __isl_take isl_schedule_node
*node
)
1096 if (!isl_schedule_node_has_parent(node
))
1097 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1098 "node has no parent",
1099 return isl_schedule_node_free(node
));
1100 return isl_schedule_node_ancestor(node
, 1);
1103 /* Move the "node" pointer to the root of its schedule tree.
1105 __isl_give isl_schedule_node
*isl_schedule_node_root(
1106 __isl_take isl_schedule_node
*node
)
1112 n
= isl_schedule_node_get_tree_depth(node
);
1114 return isl_schedule_node_free(node
);
1115 return isl_schedule_node_ancestor(node
, n
);
1118 /* Move the "node" pointer to the child at position "pos" of the node
1119 * it currently points to.
1121 __isl_give isl_schedule_node
*isl_schedule_node_child(
1122 __isl_take isl_schedule_node
*node
, int pos
)
1126 isl_schedule_tree
*tree
;
1129 node
= isl_schedule_node_cow(node
);
1132 if (!isl_schedule_node_has_children(node
))
1133 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1134 "node has no children",
1135 return isl_schedule_node_free(node
));
1137 ctx
= isl_schedule_node_get_ctx(node
);
1138 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1139 child_pos
= isl_realloc_array(ctx
, node
->child_pos
, int, n
+ 1);
1141 return isl_schedule_node_free(node
);
1142 node
->child_pos
= child_pos
;
1143 node
->child_pos
[n
] = pos
;
1145 node
->ancestors
= isl_schedule_tree_list_add(node
->ancestors
,
1146 isl_schedule_tree_copy(node
->tree
));
1148 if (isl_schedule_tree_has_children(tree
))
1149 tree
= isl_schedule_tree_get_child(tree
, pos
);
1151 tree
= isl_schedule_node_get_leaf(node
);
1152 isl_schedule_tree_free(node
->tree
);
1155 if (!node
->tree
|| !node
->ancestors
)
1156 return isl_schedule_node_free(node
);
1161 /* Move the "node" pointer to the first child of the node
1162 * it currently points to.
1164 __isl_give isl_schedule_node
*isl_schedule_node_first_child(
1165 __isl_take isl_schedule_node
*node
)
1167 return isl_schedule_node_child(node
, 0);
1170 /* Move the "node" pointer to the child of this node's parent in
1171 * the previous child position.
1173 __isl_give isl_schedule_node
*isl_schedule_node_previous_sibling(
1174 __isl_take isl_schedule_node
*node
)
1177 isl_schedule_tree
*parent
, *tree
;
1179 node
= isl_schedule_node_cow(node
);
1182 if (!isl_schedule_node_has_previous_sibling(node
))
1183 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1184 "node has no previous sibling",
1185 return isl_schedule_node_free(node
));
1187 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1188 parent
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1191 return isl_schedule_node_free(node
);
1192 node
->child_pos
[n
- 1]--;
1193 tree
= isl_schedule_tree_list_get_schedule_tree(parent
->children
,
1194 node
->child_pos
[n
- 1]);
1195 isl_schedule_tree_free(parent
);
1197 return isl_schedule_node_free(node
);
1198 isl_schedule_tree_free(node
->tree
);
1204 /* Move the "node" pointer to the child of this node's parent in
1205 * the next child position.
1207 __isl_give isl_schedule_node
*isl_schedule_node_next_sibling(
1208 __isl_take isl_schedule_node
*node
)
1211 isl_schedule_tree
*parent
, *tree
;
1213 node
= isl_schedule_node_cow(node
);
1216 if (!isl_schedule_node_has_next_sibling(node
))
1217 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1218 "node has no next sibling",
1219 return isl_schedule_node_free(node
));
1221 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1222 parent
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1225 return isl_schedule_node_free(node
);
1226 node
->child_pos
[n
- 1]++;
1227 tree
= isl_schedule_tree_list_get_schedule_tree(parent
->children
,
1228 node
->child_pos
[n
- 1]);
1229 isl_schedule_tree_free(parent
);
1231 return isl_schedule_node_free(node
);
1232 isl_schedule_tree_free(node
->tree
);
1238 /* Return a copy to the child at position "pos" of "node".
1240 __isl_give isl_schedule_node
*isl_schedule_node_get_child(
1241 __isl_keep isl_schedule_node
*node
, int pos
)
1243 return isl_schedule_node_child(isl_schedule_node_copy(node
), pos
);
1246 /* Traverse the descendant of "node" in depth-first order, including
1247 * "node" itself. Call "enter" whenever a node is entered and "leave"
1248 * whenever a node is left. The callback "enter" is responsible
1249 * for moving to the deepest initial subtree of its argument that
1250 * should be traversed.
1252 static __isl_give isl_schedule_node
*traverse(
1253 __isl_take isl_schedule_node
*node
,
1254 __isl_give isl_schedule_node
*(*enter
)(
1255 __isl_take isl_schedule_node
*node
, void *user
),
1256 __isl_give isl_schedule_node
*(*leave
)(
1257 __isl_take isl_schedule_node
*node
, void *user
),
1265 depth
= isl_schedule_node_get_tree_depth(node
);
1267 node
= enter(node
, user
);
1268 node
= leave(node
, user
);
1269 while (node
&& isl_schedule_node_get_tree_depth(node
) > depth
&&
1270 !isl_schedule_node_has_next_sibling(node
)) {
1271 node
= isl_schedule_node_parent(node
);
1272 node
= leave(node
, user
);
1274 if (node
&& isl_schedule_node_get_tree_depth(node
) > depth
)
1275 node
= isl_schedule_node_next_sibling(node
);
1276 } while (node
&& isl_schedule_node_get_tree_depth(node
) > depth
);
1281 /* Internal data structure for isl_schedule_node_foreach_descendant_top_down.
1283 * "fn" is the user-specified callback function.
1284 * "user" is the user-specified argument for the callback.
1286 struct isl_schedule_node_preorder_data
{
1287 isl_bool (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
);
1291 /* Callback for "traverse" to enter a node and to move
1292 * to the deepest initial subtree that should be traversed
1293 * for use in a preorder visit.
1295 * If the user callback returns a negative value, then we abort
1296 * the traversal. If this callback returns zero, then we skip
1297 * the subtree rooted at the current node. Otherwise, we move
1298 * down to the first child and repeat the process until a leaf
1301 static __isl_give isl_schedule_node
*preorder_enter(
1302 __isl_take isl_schedule_node
*node
, void *user
)
1304 struct isl_schedule_node_preorder_data
*data
= user
;
1312 r
= data
->fn(node
, data
->user
);
1314 return isl_schedule_node_free(node
);
1315 if (r
== isl_bool_false
)
1317 } while (isl_schedule_node_has_children(node
) &&
1318 (node
= isl_schedule_node_first_child(node
)) != NULL
);
1323 /* Callback for "traverse" to leave a node
1324 * for use in a preorder visit.
1325 * Since we already visited the node when we entered it,
1326 * we do not need to do anything here.
1328 static __isl_give isl_schedule_node
*preorder_leave(
1329 __isl_take isl_schedule_node
*node
, void *user
)
1334 /* Traverse the descendants of "node" (including the node itself)
1335 * in depth first preorder.
1337 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1338 * If "fn" returns 0 on any of the nodes, then the subtree rooted
1339 * at that node is skipped.
1341 * Return 0 on success and -1 on failure.
1343 isl_stat
isl_schedule_node_foreach_descendant_top_down(
1344 __isl_keep isl_schedule_node
*node
,
1345 isl_bool (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
),
1348 struct isl_schedule_node_preorder_data data
= { fn
, user
};
1350 node
= isl_schedule_node_copy(node
);
1351 node
= traverse(node
, &preorder_enter
, &preorder_leave
, &data
);
1352 isl_schedule_node_free(node
);
1354 return node
? isl_stat_ok
: isl_stat_error
;
1357 /* Internal data structure for isl_schedule_node_map_descendant_bottom_up.
1359 * "fn" is the user-specified callback function.
1360 * "user" is the user-specified argument for the callback.
1362 struct isl_schedule_node_postorder_data
{
1363 __isl_give isl_schedule_node
*(*fn
)(__isl_take isl_schedule_node
*node
,
1368 /* Callback for "traverse" to enter a node and to move
1369 * to the deepest initial subtree that should be traversed
1370 * for use in a postorder visit.
1372 * Since we are performing a postorder visit, we only need
1373 * to move to the deepest initial leaf here.
1375 static __isl_give isl_schedule_node
*postorder_enter(
1376 __isl_take isl_schedule_node
*node
, void *user
)
1378 while (node
&& isl_schedule_node_has_children(node
))
1379 node
= isl_schedule_node_first_child(node
);
1384 /* Callback for "traverse" to leave a node
1385 * for use in a postorder visit.
1387 * Since we are performing a postorder visit, we need
1388 * to call the user callback here.
1390 static __isl_give isl_schedule_node
*postorder_leave(
1391 __isl_take isl_schedule_node
*node
, void *user
)
1393 struct isl_schedule_node_postorder_data
*data
= user
;
1395 return data
->fn(node
, data
->user
);
1398 /* Traverse the descendants of "node" (including the node itself)
1399 * in depth first postorder, allowing the user to modify the visited node.
1400 * The traversal continues from the node returned by the callback function.
1401 * It is the responsibility of the user to ensure that this does not
1402 * lead to an infinite loop. It is safest to always return a pointer
1403 * to the same position (same ancestors and child positions) as the input node.
1405 __isl_give isl_schedule_node
*isl_schedule_node_map_descendant_bottom_up(
1406 __isl_take isl_schedule_node
*node
,
1407 __isl_give isl_schedule_node
*(*fn
)(__isl_take isl_schedule_node
*node
,
1408 void *user
), void *user
)
1410 struct isl_schedule_node_postorder_data data
= { fn
, user
};
1412 return traverse(node
, &postorder_enter
, &postorder_leave
, &data
);
1415 /* Traverse the ancestors of "node" from the root down to and including
1416 * the parent of "node", calling "fn" on each of them.
1418 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1420 * Return 0 on success and -1 on failure.
1422 isl_stat
isl_schedule_node_foreach_ancestor_top_down(
1423 __isl_keep isl_schedule_node
*node
,
1424 isl_stat (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
),
1430 return isl_stat_error
;
1432 n
= isl_schedule_node_get_tree_depth(node
);
1433 for (i
= 0; i
< n
; ++i
) {
1434 isl_schedule_node
*ancestor
;
1437 ancestor
= isl_schedule_node_copy(node
);
1438 ancestor
= isl_schedule_node_ancestor(ancestor
, n
- i
);
1439 r
= fn(ancestor
, user
);
1440 isl_schedule_node_free(ancestor
);
1442 return isl_stat_error
;
1448 /* Is any node in the subtree rooted at "node" anchored?
1449 * That is, do any of these nodes reference the outer band nodes?
1451 isl_bool
isl_schedule_node_is_subtree_anchored(
1452 __isl_keep isl_schedule_node
*node
)
1455 return isl_bool_error
;
1456 return isl_schedule_tree_is_subtree_anchored(node
->tree
);
1459 /* Return the number of members in the given band node.
1461 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node
*node
)
1463 return node
? isl_schedule_tree_band_n_member(node
->tree
) : 0;
1466 /* Is the band member at position "pos" of the band node "node"
1467 * marked coincident?
1469 isl_bool
isl_schedule_node_band_member_get_coincident(
1470 __isl_keep isl_schedule_node
*node
, int pos
)
1473 return isl_bool_error
;
1474 return isl_schedule_tree_band_member_get_coincident(node
->tree
, pos
);
1477 /* Mark the band member at position "pos" the band node "node"
1478 * as being coincident or not according to "coincident".
1480 __isl_give isl_schedule_node
*isl_schedule_node_band_member_set_coincident(
1481 __isl_take isl_schedule_node
*node
, int pos
, int coincident
)
1484 isl_schedule_tree
*tree
;
1488 c
= isl_schedule_node_band_member_get_coincident(node
, pos
);
1489 if (c
== coincident
)
1492 tree
= isl_schedule_tree_copy(node
->tree
);
1493 tree
= isl_schedule_tree_band_member_set_coincident(tree
, pos
,
1495 node
= isl_schedule_node_graft_tree(node
, tree
);
1500 /* Is the band node "node" marked permutable?
1502 isl_bool
isl_schedule_node_band_get_permutable(
1503 __isl_keep isl_schedule_node
*node
)
1506 return isl_bool_error
;
1508 return isl_schedule_tree_band_get_permutable(node
->tree
);
1511 /* Mark the band node "node" permutable or not according to "permutable"?
1513 __isl_give isl_schedule_node
*isl_schedule_node_band_set_permutable(
1514 __isl_take isl_schedule_node
*node
, int permutable
)
1516 isl_schedule_tree
*tree
;
1520 if (isl_schedule_node_band_get_permutable(node
) == permutable
)
1523 tree
= isl_schedule_tree_copy(node
->tree
);
1524 tree
= isl_schedule_tree_band_set_permutable(tree
, permutable
);
1525 node
= isl_schedule_node_graft_tree(node
, tree
);
1530 /* Return the schedule space of the band node.
1532 __isl_give isl_space
*isl_schedule_node_band_get_space(
1533 __isl_keep isl_schedule_node
*node
)
1538 return isl_schedule_tree_band_get_space(node
->tree
);
1541 /* Return the schedule of the band node in isolation.
1543 __isl_give isl_multi_union_pw_aff
*isl_schedule_node_band_get_partial_schedule(
1544 __isl_keep isl_schedule_node
*node
)
1549 return isl_schedule_tree_band_get_partial_schedule(node
->tree
);
1552 /* Return the schedule of the band node in isolation in the form of
1555 * If the band does not have any members, then we construct a universe map
1556 * with the universe of the domain elements reaching the node as domain.
1557 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1558 * convert that to an isl_union_map.
1560 __isl_give isl_union_map
*isl_schedule_node_band_get_partial_schedule_union_map(
1561 __isl_keep isl_schedule_node
*node
)
1563 isl_multi_union_pw_aff
*mupa
;
1568 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
1569 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1570 "not a band node", return NULL
);
1571 if (isl_schedule_node_band_n_member(node
) == 0) {
1572 isl_union_set
*domain
;
1574 domain
= isl_schedule_node_get_universe_domain(node
);
1575 return isl_union_map_from_domain(domain
);
1578 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
1579 return isl_union_map_from_multi_union_pw_aff(mupa
);
1582 /* Return the loop AST generation type for the band member of band node "node"
1583 * at position "pos".
1585 enum isl_ast_loop_type
isl_schedule_node_band_member_get_ast_loop_type(
1586 __isl_keep isl_schedule_node
*node
, int pos
)
1589 return isl_ast_loop_error
;
1591 return isl_schedule_tree_band_member_get_ast_loop_type(node
->tree
, pos
);
1594 /* Set the loop AST generation type for the band member of band node "node"
1595 * at position "pos" to "type".
1597 __isl_give isl_schedule_node
*isl_schedule_node_band_member_set_ast_loop_type(
1598 __isl_take isl_schedule_node
*node
, int pos
,
1599 enum isl_ast_loop_type type
)
1601 isl_schedule_tree
*tree
;
1606 tree
= isl_schedule_tree_copy(node
->tree
);
1607 tree
= isl_schedule_tree_band_member_set_ast_loop_type(tree
, pos
, type
);
1608 return isl_schedule_node_graft_tree(node
, tree
);
1611 /* Return the loop AST generation type for the band member of band node "node"
1612 * at position "pos" for the isolated part.
1614 enum isl_ast_loop_type
isl_schedule_node_band_member_get_isolate_ast_loop_type(
1615 __isl_keep isl_schedule_node
*node
, int pos
)
1618 return isl_ast_loop_error
;
1620 return isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1624 /* Set the loop AST generation type for the band member of band node "node"
1625 * at position "pos" for the isolated part to "type".
1627 __isl_give isl_schedule_node
*
1628 isl_schedule_node_band_member_set_isolate_ast_loop_type(
1629 __isl_take isl_schedule_node
*node
, int pos
,
1630 enum isl_ast_loop_type type
)
1632 isl_schedule_tree
*tree
;
1637 tree
= isl_schedule_tree_copy(node
->tree
);
1638 tree
= isl_schedule_tree_band_member_set_isolate_ast_loop_type(tree
,
1640 return isl_schedule_node_graft_tree(node
, tree
);
1643 /* Return the AST build options associated to band node "node".
1645 __isl_give isl_union_set
*isl_schedule_node_band_get_ast_build_options(
1646 __isl_keep isl_schedule_node
*node
)
1651 return isl_schedule_tree_band_get_ast_build_options(node
->tree
);
1654 /* Replace the AST build options associated to band node "node" by "options".
1656 __isl_give isl_schedule_node
*isl_schedule_node_band_set_ast_build_options(
1657 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*options
)
1659 isl_schedule_tree
*tree
;
1661 if (!node
|| !options
)
1664 tree
= isl_schedule_tree_copy(node
->tree
);
1665 tree
= isl_schedule_tree_band_set_ast_build_options(tree
, options
);
1666 return isl_schedule_node_graft_tree(node
, tree
);
1668 isl_schedule_node_free(node
);
1669 isl_union_set_free(options
);
1673 /* Make sure that that spaces of "node" and "mv" are the same.
1674 * Return -1 on error, reporting the error to the user.
1676 static int check_space_multi_val(__isl_keep isl_schedule_node
*node
,
1677 __isl_keep isl_multi_val
*mv
)
1679 isl_space
*node_space
, *mv_space
;
1682 node_space
= isl_schedule_node_band_get_space(node
);
1683 mv_space
= isl_multi_val_get_space(mv
);
1684 equal
= isl_space_tuple_is_equal(node_space
, isl_dim_set
,
1685 mv_space
, isl_dim_set
);
1686 isl_space_free(mv_space
);
1687 isl_space_free(node_space
);
1691 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1692 "spaces don't match", return -1);
1697 /* Multiply the partial schedule of the band node "node"
1698 * with the factors in "mv".
1700 __isl_give isl_schedule_node
*isl_schedule_node_band_scale(
1701 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1703 isl_schedule_tree
*tree
;
1708 if (check_space_multi_val(node
, mv
) < 0)
1710 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1714 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1715 "cannot scale band node with anchored subtree",
1718 tree
= isl_schedule_node_get_tree(node
);
1719 tree
= isl_schedule_tree_band_scale(tree
, mv
);
1720 return isl_schedule_node_graft_tree(node
, tree
);
1722 isl_multi_val_free(mv
);
1723 isl_schedule_node_free(node
);
1727 /* Divide the partial schedule of the band node "node"
1728 * by the factors in "mv".
1730 __isl_give isl_schedule_node
*isl_schedule_node_band_scale_down(
1731 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1733 isl_schedule_tree
*tree
;
1738 if (check_space_multi_val(node
, mv
) < 0)
1740 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1744 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1745 "cannot scale down band node with anchored subtree",
1748 tree
= isl_schedule_node_get_tree(node
);
1749 tree
= isl_schedule_tree_band_scale_down(tree
, mv
);
1750 return isl_schedule_node_graft_tree(node
, tree
);
1752 isl_multi_val_free(mv
);
1753 isl_schedule_node_free(node
);
1757 /* Reduce the partial schedule of the band node "node"
1758 * modulo the factors in "mv".
1760 __isl_give isl_schedule_node
*isl_schedule_node_band_mod(
1761 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1763 isl_schedule_tree
*tree
;
1768 if (check_space_multi_val(node
, mv
) < 0)
1770 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1774 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1775 "cannot perform mod on band node with anchored subtree",
1778 tree
= isl_schedule_node_get_tree(node
);
1779 tree
= isl_schedule_tree_band_mod(tree
, mv
);
1780 return isl_schedule_node_graft_tree(node
, tree
);
1782 isl_multi_val_free(mv
);
1783 isl_schedule_node_free(node
);
1787 /* Make sure that that spaces of "node" and "mupa" are the same.
1788 * Return isl_stat_error on error, reporting the error to the user.
1790 static isl_stat
check_space_multi_union_pw_aff(
1791 __isl_keep isl_schedule_node
*node
,
1792 __isl_keep isl_multi_union_pw_aff
*mupa
)
1794 isl_space
*node_space
, *mupa_space
;
1797 node_space
= isl_schedule_node_band_get_space(node
);
1798 mupa_space
= isl_multi_union_pw_aff_get_space(mupa
);
1799 equal
= isl_space_tuple_is_equal(node_space
, isl_dim_set
,
1800 mupa_space
, isl_dim_set
);
1801 isl_space_free(mupa_space
);
1802 isl_space_free(node_space
);
1804 return isl_stat_error
;
1806 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1807 "spaces don't match", return isl_stat_error
);
1812 /* Shift the partial schedule of the band node "node" by "shift".
1814 __isl_give isl_schedule_node
*isl_schedule_node_band_shift(
1815 __isl_take isl_schedule_node
*node
,
1816 __isl_take isl_multi_union_pw_aff
*shift
)
1818 isl_schedule_tree
*tree
;
1821 if (!node
|| !shift
)
1823 if (check_space_multi_union_pw_aff(node
, shift
) < 0)
1825 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1829 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1830 "cannot shift band node with anchored subtree",
1833 tree
= isl_schedule_node_get_tree(node
);
1834 tree
= isl_schedule_tree_band_shift(tree
, shift
);
1835 return isl_schedule_node_graft_tree(node
, tree
);
1837 isl_multi_union_pw_aff_free(shift
);
1838 isl_schedule_node_free(node
);
1842 /* Tile "node" with tile sizes "sizes".
1844 * The current node is replaced by two nested nodes corresponding
1845 * to the tile dimensions and the point dimensions.
1847 * Return a pointer to the outer (tile) node.
1849 * If any of the descendants of "node" depend on the set of outer band nodes,
1850 * then we refuse to tile the node.
1852 * If the scale tile loops option is set, then the tile loops
1853 * are scaled by the tile sizes. If the shift point loops option is set,
1854 * then the point loops are shifted to start at zero.
1855 * In particular, these options affect the tile and point loop schedules
1858 * scale shift original tile point
1860 * 0 0 i floor(i/s) i
1861 * 1 0 i s * floor(i/s) i
1862 * 0 1 i floor(i/s) i - s * floor(i/s)
1863 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1865 __isl_give isl_schedule_node
*isl_schedule_node_band_tile(
1866 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
1868 isl_schedule_tree
*tree
;
1871 if (!node
|| !sizes
)
1873 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1877 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1878 "cannot tile band node with anchored subtree",
1881 if (check_space_multi_val(node
, sizes
) < 0)
1884 tree
= isl_schedule_node_get_tree(node
);
1885 tree
= isl_schedule_tree_band_tile(tree
, sizes
);
1886 return isl_schedule_node_graft_tree(node
, tree
);
1888 isl_multi_val_free(sizes
);
1889 isl_schedule_node_free(node
);
1893 /* Move the band node "node" down to all the leaves in the subtree
1895 * Return a pointer to the node in the resulting tree that is in the same
1896 * position as the node pointed to by "node" in the original tree.
1898 * If the node only has a leaf child, then nothing needs to be done.
1899 * Otherwise, the child of the node is removed and the result is
1900 * appended to all the leaves in the subtree rooted at the original child.
1901 * Since the node is moved to the leaves, it needs to be expanded
1902 * according to the expansion, if any, defined by that subtree.
1903 * In the end, the original node is replaced by the result of
1904 * attaching copies of the expanded node to the leaves.
1906 * If any of the nodes in the subtree rooted at "node" depend on
1907 * the set of outer band nodes then we refuse to sink the band node.
1909 __isl_give isl_schedule_node
*isl_schedule_node_band_sink(
1910 __isl_take isl_schedule_node
*node
)
1912 enum isl_schedule_node_type type
;
1913 isl_schedule_tree
*tree
, *child
;
1914 isl_union_pw_multi_aff
*contraction
;
1920 type
= isl_schedule_node_get_type(node
);
1921 if (type
!= isl_schedule_node_band
)
1922 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1923 "not a band node", isl_schedule_node_free(node
));
1924 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1926 return isl_schedule_node_free(node
);
1928 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1929 "cannot sink band node in anchored subtree",
1930 isl_schedule_node_free(node
));
1931 if (isl_schedule_tree_n_children(node
->tree
) == 0)
1934 contraction
= isl_schedule_node_get_subtree_contraction(node
);
1936 tree
= isl_schedule_node_get_tree(node
);
1937 child
= isl_schedule_tree_get_child(tree
, 0);
1938 tree
= isl_schedule_tree_reset_children(tree
);
1939 tree
= isl_schedule_tree_pullback_union_pw_multi_aff(tree
, contraction
);
1940 tree
= isl_schedule_tree_append_to_leaves(child
, tree
);
1942 return isl_schedule_node_graft_tree(node
, tree
);
1945 /* Split "node" into two nested band nodes, one with the first "pos"
1946 * dimensions and one with the remaining dimensions.
1947 * The schedules of the two band nodes live in anonymous spaces.
1949 __isl_give isl_schedule_node
*isl_schedule_node_band_split(
1950 __isl_take isl_schedule_node
*node
, int pos
)
1952 isl_schedule_tree
*tree
;
1954 tree
= isl_schedule_node_get_tree(node
);
1955 tree
= isl_schedule_tree_band_split(tree
, pos
);
1956 return isl_schedule_node_graft_tree(node
, tree
);
1959 /* Return the context of the context node "node".
1961 __isl_give isl_set
*isl_schedule_node_context_get_context(
1962 __isl_keep isl_schedule_node
*node
)
1967 return isl_schedule_tree_context_get_context(node
->tree
);
1970 /* Return the domain of the domain node "node".
1972 __isl_give isl_union_set
*isl_schedule_node_domain_get_domain(
1973 __isl_keep isl_schedule_node
*node
)
1978 return isl_schedule_tree_domain_get_domain(node
->tree
);
1981 /* Return the expansion map of expansion node "node".
1983 __isl_give isl_union_map
*isl_schedule_node_expansion_get_expansion(
1984 __isl_keep isl_schedule_node
*node
)
1989 return isl_schedule_tree_expansion_get_expansion(node
->tree
);
1992 /* Return the contraction of expansion node "node".
1994 __isl_give isl_union_pw_multi_aff
*isl_schedule_node_expansion_get_contraction(
1995 __isl_keep isl_schedule_node
*node
)
2000 return isl_schedule_tree_expansion_get_contraction(node
->tree
);
2003 /* Replace the contraction and the expansion of the expansion node "node"
2004 * by "contraction" and "expansion".
2006 __isl_give isl_schedule_node
*
2007 isl_schedule_node_expansion_set_contraction_and_expansion(
2008 __isl_take isl_schedule_node
*node
,
2009 __isl_take isl_union_pw_multi_aff
*contraction
,
2010 __isl_take isl_union_map
*expansion
)
2012 isl_schedule_tree
*tree
;
2014 if (!node
|| !contraction
|| !expansion
)
2017 tree
= isl_schedule_tree_copy(node
->tree
);
2018 tree
= isl_schedule_tree_expansion_set_contraction_and_expansion(tree
,
2019 contraction
, expansion
);
2020 return isl_schedule_node_graft_tree(node
, tree
);
2022 isl_schedule_node_free(node
);
2023 isl_union_pw_multi_aff_free(contraction
);
2024 isl_union_map_free(expansion
);
2028 /* Return the extension of the extension node "node".
2030 __isl_give isl_union_map
*isl_schedule_node_extension_get_extension(
2031 __isl_keep isl_schedule_node
*node
)
2036 return isl_schedule_tree_extension_get_extension(node
->tree
);
2039 /* Replace the extension of extension node "node" by "extension".
2041 __isl_give isl_schedule_node
*isl_schedule_node_extension_set_extension(
2042 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
2044 isl_schedule_tree
*tree
;
2046 if (!node
|| !extension
)
2049 tree
= isl_schedule_tree_copy(node
->tree
);
2050 tree
= isl_schedule_tree_extension_set_extension(tree
, extension
);
2051 return isl_schedule_node_graft_tree(node
, tree
);
2053 isl_schedule_node_free(node
);
2054 isl_union_map_free(extension
);
2058 /* Return the filter of the filter node "node".
2060 __isl_give isl_union_set
*isl_schedule_node_filter_get_filter(
2061 __isl_keep isl_schedule_node
*node
)
2066 return isl_schedule_tree_filter_get_filter(node
->tree
);
2069 /* Replace the filter of filter node "node" by "filter".
2071 __isl_give isl_schedule_node
*isl_schedule_node_filter_set_filter(
2072 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2074 isl_schedule_tree
*tree
;
2076 if (!node
|| !filter
)
2079 tree
= isl_schedule_tree_copy(node
->tree
);
2080 tree
= isl_schedule_tree_filter_set_filter(tree
, filter
);
2081 return isl_schedule_node_graft_tree(node
, tree
);
2083 isl_schedule_node_free(node
);
2084 isl_union_set_free(filter
);
2088 /* Intersect the filter of filter node "node" with "filter".
2090 * If the filter of the node is already a subset of "filter",
2091 * then leave the node unchanged.
2093 __isl_give isl_schedule_node
*isl_schedule_node_filter_intersect_filter(
2094 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2096 isl_union_set
*node_filter
= NULL
;
2099 if (!node
|| !filter
)
2102 node_filter
= isl_schedule_node_filter_get_filter(node
);
2103 subset
= isl_union_set_is_subset(node_filter
, filter
);
2107 isl_union_set_free(node_filter
);
2108 isl_union_set_free(filter
);
2111 node_filter
= isl_union_set_intersect(node_filter
, filter
);
2112 node
= isl_schedule_node_filter_set_filter(node
, node_filter
);
2115 isl_schedule_node_free(node
);
2116 isl_union_set_free(node_filter
);
2117 isl_union_set_free(filter
);
2121 /* Return the guard of the guard node "node".
2123 __isl_give isl_set
*isl_schedule_node_guard_get_guard(
2124 __isl_keep isl_schedule_node
*node
)
2129 return isl_schedule_tree_guard_get_guard(node
->tree
);
2132 /* Return the mark identifier of the mark node "node".
2134 __isl_give isl_id
*isl_schedule_node_mark_get_id(
2135 __isl_keep isl_schedule_node
*node
)
2140 return isl_schedule_tree_mark_get_id(node
->tree
);
2143 /* Replace the child at position "pos" of the sequence node "node"
2144 * by the children of sequence root node of "tree".
2146 __isl_give isl_schedule_node
*isl_schedule_node_sequence_splice(
2147 __isl_take isl_schedule_node
*node
, int pos
,
2148 __isl_take isl_schedule_tree
*tree
)
2150 isl_schedule_tree
*node_tree
;
2154 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2155 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2156 "not a sequence node", goto error
);
2157 if (isl_schedule_tree_get_type(tree
) != isl_schedule_node_sequence
)
2158 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2159 "not a sequence node", goto error
);
2160 node_tree
= isl_schedule_node_get_tree(node
);
2161 node_tree
= isl_schedule_tree_sequence_splice(node_tree
, pos
, tree
);
2162 node
= isl_schedule_node_graft_tree(node
, node_tree
);
2166 isl_schedule_node_free(node
);
2167 isl_schedule_tree_free(tree
);
2171 /* Given a sequence node "node", with a child at position "pos" that
2172 * is also a sequence node, attach the children of that node directly
2173 * as children of "node" at that position, replacing the original child.
2175 * The filters of these children are intersected with the filter
2176 * of the child at position "pos".
2178 __isl_give isl_schedule_node
*isl_schedule_node_sequence_splice_child(
2179 __isl_take isl_schedule_node
*node
, int pos
)
2182 isl_union_set
*filter
;
2183 isl_schedule_node
*child
;
2184 isl_schedule_tree
*tree
;
2188 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2189 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2190 "not a sequence node", isl_schedule_node_free(node
));
2191 node
= isl_schedule_node_child(node
, pos
);
2192 node
= isl_schedule_node_child(node
, 0);
2193 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2194 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2195 "not a sequence node", isl_schedule_node_free(node
));
2196 child
= isl_schedule_node_copy(node
);
2197 node
= isl_schedule_node_parent(node
);
2198 filter
= isl_schedule_node_filter_get_filter(node
);
2199 n
= isl_schedule_node_n_children(child
);
2200 for (i
= 0; i
< n
; ++i
) {
2201 child
= isl_schedule_node_child(child
, i
);
2202 child
= isl_schedule_node_filter_intersect_filter(child
,
2203 isl_union_set_copy(filter
));
2204 child
= isl_schedule_node_parent(child
);
2206 isl_union_set_free(filter
);
2207 tree
= isl_schedule_node_get_tree(child
);
2208 isl_schedule_node_free(child
);
2209 node
= isl_schedule_node_parent(node
);
2210 node
= isl_schedule_node_sequence_splice(node
, pos
, tree
);
2215 /* Update the ancestors of "node" to point to the tree that "node"
2217 * That is, replace the child in the original parent that corresponds
2218 * to the current tree position by node->tree and continue updating
2219 * the ancestors in the same way until the root is reached.
2221 * If "fn" is not NULL, then it is called on each ancestor as we move up
2222 * the tree so that it can modify the ancestor before it is added
2223 * to the list of ancestors of the modified node.
2224 * The additional "pos" argument records the position
2225 * of the "tree" argument in the original schedule tree.
2227 * If "node" originally points to a leaf of the schedule tree, then make sure
2228 * that in the end it points to a leaf in the updated schedule tree.
2230 static __isl_give isl_schedule_node
*update_ancestors(
2231 __isl_take isl_schedule_node
*node
,
2232 __isl_give isl_schedule_tree
*(*fn
)(__isl_take isl_schedule_tree
*tree
,
2233 __isl_keep isl_schedule_node
*pos
, void *user
), void *user
)
2238 isl_schedule_tree
*tree
;
2239 isl_schedule_node
*pos
= NULL
;
2242 pos
= isl_schedule_node_copy(node
);
2244 node
= isl_schedule_node_cow(node
);
2246 return isl_schedule_node_free(pos
);
2248 ctx
= isl_schedule_node_get_ctx(node
);
2249 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
2250 tree
= isl_schedule_tree_copy(node
->tree
);
2252 for (i
= n
- 1; i
>= 0; --i
) {
2253 isl_schedule_tree
*parent
;
2255 parent
= isl_schedule_tree_list_get_schedule_tree(
2256 node
->ancestors
, i
);
2257 parent
= isl_schedule_tree_replace_child(parent
,
2258 node
->child_pos
[i
], tree
);
2260 pos
= isl_schedule_node_parent(pos
);
2261 parent
= fn(parent
, pos
, user
);
2263 node
->ancestors
= isl_schedule_tree_list_set_schedule_tree(
2264 node
->ancestors
, i
, isl_schedule_tree_copy(parent
));
2270 isl_schedule_node_free(pos
);
2272 is_leaf
= isl_schedule_tree_is_leaf(node
->tree
);
2273 node
->schedule
= isl_schedule_set_root(node
->schedule
, tree
);
2275 isl_schedule_tree_free(node
->tree
);
2276 node
->tree
= isl_schedule_node_get_leaf(node
);
2279 if (!node
->schedule
|| !node
->ancestors
)
2280 return isl_schedule_node_free(node
);
2285 /* Replace the subtree that "pos" points to by "tree", updating
2286 * the ancestors to maintain a consistent state.
2288 __isl_give isl_schedule_node
*isl_schedule_node_graft_tree(
2289 __isl_take isl_schedule_node
*pos
, __isl_take isl_schedule_tree
*tree
)
2293 if (pos
->tree
== tree
) {
2294 isl_schedule_tree_free(tree
);
2298 pos
= isl_schedule_node_cow(pos
);
2302 isl_schedule_tree_free(pos
->tree
);
2305 return update_ancestors(pos
, NULL
, NULL
);
2307 isl_schedule_node_free(pos
);
2308 isl_schedule_tree_free(tree
);
2312 /* Make sure we can insert a node between "node" and its parent.
2313 * Return -1 on error, reporting the reason why we cannot insert a node.
2315 static int check_insert(__isl_keep isl_schedule_node
*node
)
2318 enum isl_schedule_node_type type
;
2320 has_parent
= isl_schedule_node_has_parent(node
);
2324 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2325 "cannot insert node outside of root", return -1);
2327 type
= isl_schedule_node_get_parent_type(node
);
2328 if (type
== isl_schedule_node_error
)
2330 if (type
== isl_schedule_node_set
|| type
== isl_schedule_node_sequence
)
2331 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2332 "cannot insert node between set or sequence node "
2333 "and its filter children", return -1);
2338 /* Insert a band node with partial schedule "mupa" between "node" and
2340 * Return a pointer to the new band node.
2342 * If any of the nodes in the subtree rooted at "node" depend on
2343 * the set of outer band nodes then we refuse to insert the band node.
2345 __isl_give isl_schedule_node
*isl_schedule_node_insert_partial_schedule(
2346 __isl_take isl_schedule_node
*node
,
2347 __isl_take isl_multi_union_pw_aff
*mupa
)
2350 isl_schedule_band
*band
;
2351 isl_schedule_tree
*tree
;
2353 if (check_insert(node
) < 0)
2354 node
= isl_schedule_node_free(node
);
2355 anchored
= isl_schedule_node_is_subtree_anchored(node
);
2359 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2360 "cannot insert band node in anchored subtree",
2363 tree
= isl_schedule_node_get_tree(node
);
2364 band
= isl_schedule_band_from_multi_union_pw_aff(mupa
);
2365 tree
= isl_schedule_tree_insert_band(tree
, band
);
2366 node
= isl_schedule_node_graft_tree(node
, tree
);
2370 isl_schedule_node_free(node
);
2371 isl_multi_union_pw_aff_free(mupa
);
2375 /* Insert a context node with context "context" between "node" and its parent.
2376 * Return a pointer to the new context node.
2378 __isl_give isl_schedule_node
*isl_schedule_node_insert_context(
2379 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*context
)
2381 isl_schedule_tree
*tree
;
2383 if (check_insert(node
) < 0)
2384 node
= isl_schedule_node_free(node
);
2386 tree
= isl_schedule_node_get_tree(node
);
2387 tree
= isl_schedule_tree_insert_context(tree
, context
);
2388 node
= isl_schedule_node_graft_tree(node
, tree
);
2393 /* Insert an expansion node with the given "contraction" and "expansion"
2394 * between "node" and its parent.
2395 * Return a pointer to the new expansion node.
2397 * Typically the domain and range spaces of the expansion are different.
2398 * This means that only one of them can refer to the current domain space
2399 * in a consistent tree. It is up to the caller to ensure that the tree
2400 * returns to a consistent state.
2402 __isl_give isl_schedule_node
*isl_schedule_node_insert_expansion(
2403 __isl_take isl_schedule_node
*node
,
2404 __isl_take isl_union_pw_multi_aff
*contraction
,
2405 __isl_take isl_union_map
*expansion
)
2407 isl_schedule_tree
*tree
;
2409 if (check_insert(node
) < 0)
2410 node
= isl_schedule_node_free(node
);
2412 tree
= isl_schedule_node_get_tree(node
);
2413 tree
= isl_schedule_tree_insert_expansion(tree
, contraction
, expansion
);
2414 node
= isl_schedule_node_graft_tree(node
, tree
);
2419 /* Insert an extension node with extension "extension" between "node" and
2421 * Return a pointer to the new extension node.
2423 __isl_give isl_schedule_node
*isl_schedule_node_insert_extension(
2424 __isl_take isl_schedule_node
*node
,
2425 __isl_take isl_union_map
*extension
)
2427 isl_schedule_tree
*tree
;
2429 tree
= isl_schedule_node_get_tree(node
);
2430 tree
= isl_schedule_tree_insert_extension(tree
, extension
);
2431 node
= isl_schedule_node_graft_tree(node
, tree
);
2436 /* Insert a filter node with filter "filter" between "node" and its parent.
2437 * Return a pointer to the new filter node.
2439 __isl_give isl_schedule_node
*isl_schedule_node_insert_filter(
2440 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2442 isl_schedule_tree
*tree
;
2444 if (check_insert(node
) < 0)
2445 node
= isl_schedule_node_free(node
);
2447 tree
= isl_schedule_node_get_tree(node
);
2448 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
2449 node
= isl_schedule_node_graft_tree(node
, tree
);
2454 /* Insert a guard node with guard "guard" between "node" and its parent.
2455 * Return a pointer to the new guard node.
2457 __isl_give isl_schedule_node
*isl_schedule_node_insert_guard(
2458 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*guard
)
2460 isl_schedule_tree
*tree
;
2462 if (check_insert(node
) < 0)
2463 node
= isl_schedule_node_free(node
);
2465 tree
= isl_schedule_node_get_tree(node
);
2466 tree
= isl_schedule_tree_insert_guard(tree
, guard
);
2467 node
= isl_schedule_node_graft_tree(node
, tree
);
2472 /* Insert a mark node with mark identifier "mark" between "node" and
2474 * Return a pointer to the new mark node.
2476 __isl_give isl_schedule_node
*isl_schedule_node_insert_mark(
2477 __isl_take isl_schedule_node
*node
, __isl_take isl_id
*mark
)
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_mark(tree
, mark
);
2486 node
= isl_schedule_node_graft_tree(node
, tree
);
2491 /* Attach the current subtree of "node" to a sequence of filter tree nodes
2492 * with filters described by "filters", attach this sequence
2493 * of filter tree nodes as children to a new tree of type "type" and
2494 * replace the original subtree of "node" by this new tree.
2495 * Each copy of the original subtree is simplified with respect
2496 * to the corresponding filter.
2498 static __isl_give isl_schedule_node
*isl_schedule_node_insert_children(
2499 __isl_take isl_schedule_node
*node
,
2500 enum isl_schedule_node_type type
,
2501 __isl_take isl_union_set_list
*filters
)
2505 isl_schedule_tree
*tree
;
2506 isl_schedule_tree_list
*list
;
2508 if (check_insert(node
) < 0)
2509 node
= isl_schedule_node_free(node
);
2511 if (!node
|| !filters
)
2514 ctx
= isl_schedule_node_get_ctx(node
);
2515 n
= isl_union_set_list_n_union_set(filters
);
2516 list
= isl_schedule_tree_list_alloc(ctx
, n
);
2517 for (i
= 0; i
< n
; ++i
) {
2518 isl_schedule_node
*node_i
;
2519 isl_schedule_tree
*tree
;
2520 isl_union_set
*filter
;
2522 filter
= isl_union_set_list_get_union_set(filters
, i
);
2523 node_i
= isl_schedule_node_copy(node
);
2524 node_i
= isl_schedule_node_gist(node_i
,
2525 isl_union_set_copy(filter
));
2526 tree
= isl_schedule_node_get_tree(node_i
);
2527 isl_schedule_node_free(node_i
);
2528 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
2529 list
= isl_schedule_tree_list_add(list
, tree
);
2531 tree
= isl_schedule_tree_from_children(type
, list
);
2532 node
= isl_schedule_node_graft_tree(node
, tree
);
2534 isl_union_set_list_free(filters
);
2537 isl_union_set_list_free(filters
);
2538 isl_schedule_node_free(node
);
2542 /* Insert a sequence node with child filters "filters" between "node" and
2543 * its parent. That is, the tree that "node" points to is attached
2544 * to each of the child nodes of the filter nodes.
2545 * Return a pointer to the new sequence node.
2547 __isl_give isl_schedule_node
*isl_schedule_node_insert_sequence(
2548 __isl_take isl_schedule_node
*node
,
2549 __isl_take isl_union_set_list
*filters
)
2551 return isl_schedule_node_insert_children(node
,
2552 isl_schedule_node_sequence
, filters
);
2555 /* Insert a set node with child filters "filters" between "node" and
2556 * its parent. That is, the tree that "node" points to is attached
2557 * to each of the child nodes of the filter nodes.
2558 * Return a pointer to the new set node.
2560 __isl_give isl_schedule_node
*isl_schedule_node_insert_set(
2561 __isl_take isl_schedule_node
*node
,
2562 __isl_take isl_union_set_list
*filters
)
2564 return isl_schedule_node_insert_children(node
,
2565 isl_schedule_node_set
, filters
);
2568 /* Remove "node" from its schedule tree and return a pointer
2569 * to the leaf at the same position in the updated schedule tree.
2571 * It is not allowed to remove the root of a schedule tree or
2572 * a child of a set or sequence node.
2574 __isl_give isl_schedule_node
*isl_schedule_node_cut(
2575 __isl_take isl_schedule_node
*node
)
2577 isl_schedule_tree
*leaf
;
2578 enum isl_schedule_node_type parent_type
;
2582 if (!isl_schedule_node_has_parent(node
))
2583 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2584 "cannot cut root", return isl_schedule_node_free(node
));
2586 parent_type
= isl_schedule_node_get_parent_type(node
);
2587 if (parent_type
== isl_schedule_node_set
||
2588 parent_type
== isl_schedule_node_sequence
)
2589 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2590 "cannot cut child of set or sequence",
2591 return isl_schedule_node_free(node
));
2593 leaf
= isl_schedule_node_get_leaf(node
);
2594 return isl_schedule_node_graft_tree(node
, leaf
);
2597 /* Remove a single node from the schedule tree, attaching the child
2598 * of "node" directly to its parent.
2599 * Return a pointer to this former child or to the leaf the position
2600 * of the original node if there was no child.
2601 * It is not allowed to remove the root of a schedule tree,
2602 * a set or sequence node, a child of a set or sequence node or
2603 * a band node with an anchored subtree.
2605 __isl_give isl_schedule_node
*isl_schedule_node_delete(
2606 __isl_take isl_schedule_node
*node
)
2609 isl_schedule_tree
*tree
;
2610 enum isl_schedule_node_type type
;
2615 if (isl_schedule_node_get_tree_depth(node
) == 0)
2616 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2617 "cannot delete root node",
2618 return isl_schedule_node_free(node
));
2619 n
= isl_schedule_node_n_children(node
);
2621 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2622 "can only delete node with a single child",
2623 return isl_schedule_node_free(node
));
2624 type
= isl_schedule_node_get_parent_type(node
);
2625 if (type
== isl_schedule_node_sequence
|| type
== isl_schedule_node_set
)
2626 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2627 "cannot delete child of set or sequence",
2628 return isl_schedule_node_free(node
));
2629 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
) {
2632 anchored
= isl_schedule_node_is_subtree_anchored(node
);
2634 return isl_schedule_node_free(node
);
2636 isl_die(isl_schedule_node_get_ctx(node
),
2638 "cannot delete band node with anchored subtree",
2639 return isl_schedule_node_free(node
));
2642 tree
= isl_schedule_node_get_tree(node
);
2643 if (!tree
|| isl_schedule_tree_has_children(tree
)) {
2644 tree
= isl_schedule_tree_child(tree
, 0);
2646 isl_schedule_tree_free(tree
);
2647 tree
= isl_schedule_node_get_leaf(node
);
2649 node
= isl_schedule_node_graft_tree(node
, tree
);
2654 /* Internal data structure for the group_ancestor callback.
2656 * If "finished" is set, then we no longer need to modify
2657 * any further ancestors.
2659 * "contraction" and "expansion" represent the expansion
2660 * that reflects the grouping.
2662 * "domain" contains the domain elements that reach the position
2663 * where the grouping is performed. That is, it is the range
2664 * of the resulting expansion.
2665 * "domain_universe" is the universe of "domain".
2666 * "group" is the set of group elements, i.e., the domain
2667 * of the resulting expansion.
2668 * "group_universe" is the universe of "group".
2670 * "sched" is the schedule for the group elements, in pratice
2671 * an identity mapping on "group_universe".
2672 * "dim" is the dimension of "sched".
2674 struct isl_schedule_group_data
{
2677 isl_union_map
*expansion
;
2678 isl_union_pw_multi_aff
*contraction
;
2680 isl_union_set
*domain
;
2681 isl_union_set
*domain_universe
;
2682 isl_union_set
*group
;
2683 isl_union_set
*group_universe
;
2686 isl_multi_aff
*sched
;
2689 /* Is domain covered by data->domain within data->domain_universe?
2691 static int locally_covered_by_domain(__isl_keep isl_union_set
*domain
,
2692 struct isl_schedule_group_data
*data
)
2695 isl_union_set
*test
;
2697 test
= isl_union_set_copy(domain
);
2698 test
= isl_union_set_intersect(test
,
2699 isl_union_set_copy(data
->domain_universe
));
2700 is_subset
= isl_union_set_is_subset(test
, data
->domain
);
2701 isl_union_set_free(test
);
2706 /* Update the band tree root "tree" to refer to the group instances
2707 * in data->group rather than the original domain elements in data->domain.
2708 * "pos" is the position in the original schedule tree where the modified
2709 * "tree" will be attached.
2711 * Add the part of the identity schedule on the group instances data->sched
2712 * that corresponds to this band node to the band schedule.
2713 * If the domain elements that reach the node and that are part
2714 * of data->domain_universe are all elements of data->domain (and therefore
2715 * replaced by the group instances) then this data->domain_universe
2716 * is removed from the domain of the band schedule.
2718 static __isl_give isl_schedule_tree
*group_band(
2719 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2720 struct isl_schedule_group_data
*data
)
2722 isl_union_set
*domain
;
2724 isl_multi_union_pw_aff
*mupa
, *partial
;
2726 int depth
, n
, has_id
;
2728 domain
= isl_schedule_node_get_domain(pos
);
2729 is_covered
= locally_covered_by_domain(domain
, data
);
2730 if (is_covered
>= 0 && is_covered
) {
2731 domain
= isl_union_set_universe(domain
);
2732 domain
= isl_union_set_subtract(domain
,
2733 isl_union_set_copy(data
->domain_universe
));
2734 tree
= isl_schedule_tree_band_intersect_domain(tree
, domain
);
2736 isl_union_set_free(domain
);
2738 return isl_schedule_tree_free(tree
);
2739 depth
= isl_schedule_node_get_schedule_depth(pos
);
2740 n
= isl_schedule_tree_band_n_member(tree
);
2741 ma
= isl_multi_aff_copy(data
->sched
);
2742 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, 0, depth
);
2743 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, n
, data
->dim
- depth
- n
);
2744 mupa
= isl_multi_union_pw_aff_from_multi_aff(ma
);
2745 partial
= isl_schedule_tree_band_get_partial_schedule(tree
);
2746 has_id
= isl_multi_union_pw_aff_has_tuple_id(partial
, isl_dim_set
);
2748 partial
= isl_multi_union_pw_aff_free(partial
);
2749 } else if (has_id
) {
2751 id
= isl_multi_union_pw_aff_get_tuple_id(partial
, isl_dim_set
);
2752 mupa
= isl_multi_union_pw_aff_set_tuple_id(mupa
,
2755 partial
= isl_multi_union_pw_aff_union_add(partial
, mupa
);
2756 tree
= isl_schedule_tree_band_set_partial_schedule(tree
, partial
);
2761 /* Drop the parameters in "uset" that are not also in "space".
2762 * "n" is the number of parameters in "space".
2764 static __isl_give isl_union_set
*union_set_drop_extra_params(
2765 __isl_take isl_union_set
*uset
, __isl_keep isl_space
*space
, int n
)
2769 uset
= isl_union_set_align_params(uset
, isl_space_copy(space
));
2770 n2
= isl_union_set_dim(uset
, isl_dim_param
);
2771 uset
= isl_union_set_project_out(uset
, isl_dim_param
, n
, n2
- n
);
2776 /* Update the context tree root "tree" to refer to the group instances
2777 * in data->group rather than the original domain elements in data->domain.
2778 * "pos" is the position in the original schedule tree where the modified
2779 * "tree" will be attached.
2781 * We do not actually need to update "tree" since a context node only
2782 * refers to the schedule space. However, we may need to update "data"
2783 * to not refer to any parameters introduced by the context node.
2785 static __isl_give isl_schedule_tree
*group_context(
2786 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2787 struct isl_schedule_group_data
*data
)
2790 isl_union_set
*domain
;
2794 if (isl_schedule_node_get_tree_depth(pos
) == 1)
2797 domain
= isl_schedule_node_get_universe_domain(pos
);
2798 space
= isl_union_set_get_space(domain
);
2799 isl_union_set_free(domain
);
2801 n1
= isl_space_dim(space
, isl_dim_param
);
2802 data
->expansion
= isl_union_map_align_params(data
->expansion
, space
);
2803 n2
= isl_union_map_dim(data
->expansion
, isl_dim_param
);
2805 if (!data
->expansion
)
2806 return isl_schedule_tree_free(tree
);
2810 involves
= isl_union_map_involves_dims(data
->expansion
,
2811 isl_dim_param
, n1
, n2
- n1
);
2813 return isl_schedule_tree_free(tree
);
2815 isl_die(isl_schedule_node_get_ctx(pos
), isl_error_invalid
,
2816 "grouping cannot only refer to global parameters",
2817 return isl_schedule_tree_free(tree
));
2819 data
->expansion
= isl_union_map_project_out(data
->expansion
,
2820 isl_dim_param
, n1
, n2
- n1
);
2821 space
= isl_union_map_get_space(data
->expansion
);
2823 data
->contraction
= isl_union_pw_multi_aff_align_params(
2824 data
->contraction
, isl_space_copy(space
));
2825 n2
= isl_union_pw_multi_aff_dim(data
->contraction
, isl_dim_param
);
2826 data
->contraction
= isl_union_pw_multi_aff_drop_dims(data
->contraction
,
2827 isl_dim_param
, n1
, n2
- n1
);
2829 data
->domain
= union_set_drop_extra_params(data
->domain
, space
, n1
);
2830 data
->domain_universe
=
2831 union_set_drop_extra_params(data
->domain_universe
, space
, n1
);
2832 data
->group
= union_set_drop_extra_params(data
->group
, space
, n1
);
2833 data
->group_universe
=
2834 union_set_drop_extra_params(data
->group_universe
, space
, n1
);
2836 data
->sched
= isl_multi_aff_align_params(data
->sched
,
2837 isl_space_copy(space
));
2838 n2
= isl_multi_aff_dim(data
->sched
, isl_dim_param
);
2839 data
->sched
= isl_multi_aff_drop_dims(data
->sched
,
2840 isl_dim_param
, n1
, n2
- n1
);
2842 isl_space_free(space
);
2847 /* Update the domain tree root "tree" to refer to the group instances
2848 * in data->group rather than the original domain elements in data->domain.
2849 * "pos" is the position in the original schedule tree where the modified
2850 * "tree" will be attached.
2852 * We first double-check that all grouped domain elements are actually
2853 * part of the root domain and then replace those elements by the group
2856 static __isl_give isl_schedule_tree
*group_domain(
2857 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2858 struct isl_schedule_group_data
*data
)
2860 isl_union_set
*domain
;
2863 domain
= isl_schedule_tree_domain_get_domain(tree
);
2864 is_subset
= isl_union_set_is_subset(data
->domain
, domain
);
2865 isl_union_set_free(domain
);
2867 return isl_schedule_tree_free(tree
);
2869 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
2870 "grouped domain should be part of outer domain",
2871 return isl_schedule_tree_free(tree
));
2872 domain
= isl_schedule_tree_domain_get_domain(tree
);
2873 domain
= isl_union_set_subtract(domain
,
2874 isl_union_set_copy(data
->domain
));
2875 domain
= isl_union_set_union(domain
, isl_union_set_copy(data
->group
));
2876 tree
= isl_schedule_tree_domain_set_domain(tree
, domain
);
2881 /* Update the expansion tree root "tree" to refer to the group instances
2882 * in data->group rather than the original domain elements in data->domain.
2883 * "pos" is the position in the original schedule tree where the modified
2884 * "tree" will be attached.
2886 * Let G_1 -> D_1 be the expansion of "tree" and G_2 -> D_2 the newly
2887 * introduced expansion in a descendant of "tree".
2888 * We first double-check that D_2 is a subset of D_1.
2889 * Then we remove D_2 from the range of G_1 -> D_1 and add the mapping
2890 * G_1 -> D_1 . D_2 -> G_2.
2891 * Simmilarly, we restrict the domain of the contraction to the universe
2892 * of the range of the updated expansion and add G_2 -> D_2 . D_1 -> G_1,
2893 * attempting to remove the domain constraints of this additional part.
2895 static __isl_give isl_schedule_tree
*group_expansion(
2896 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2897 struct isl_schedule_group_data
*data
)
2899 isl_union_set
*domain
;
2900 isl_union_map
*expansion
, *umap
;
2901 isl_union_pw_multi_aff
*contraction
, *upma
;
2904 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2905 domain
= isl_union_map_range(expansion
);
2906 is_subset
= isl_union_set_is_subset(data
->domain
, domain
);
2907 isl_union_set_free(domain
);
2909 return isl_schedule_tree_free(tree
);
2911 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
2912 "grouped domain should be part "
2913 "of outer expansion domain",
2914 return isl_schedule_tree_free(tree
));
2915 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2916 umap
= isl_union_map_from_union_pw_multi_aff(
2917 isl_union_pw_multi_aff_copy(data
->contraction
));
2918 umap
= isl_union_map_apply_range(expansion
, umap
);
2919 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2920 expansion
= isl_union_map_subtract_range(expansion
,
2921 isl_union_set_copy(data
->domain
));
2922 expansion
= isl_union_map_union(expansion
, umap
);
2923 umap
= isl_union_map_universe(isl_union_map_copy(expansion
));
2924 domain
= isl_union_map_range(umap
);
2925 contraction
= isl_schedule_tree_expansion_get_contraction(tree
);
2926 umap
= isl_union_map_from_union_pw_multi_aff(contraction
);
2927 umap
= isl_union_map_apply_range(isl_union_map_copy(data
->expansion
),
2929 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
2930 contraction
= isl_schedule_tree_expansion_get_contraction(tree
);
2931 contraction
= isl_union_pw_multi_aff_intersect_domain(contraction
,
2933 domain
= isl_union_pw_multi_aff_domain(
2934 isl_union_pw_multi_aff_copy(upma
));
2935 upma
= isl_union_pw_multi_aff_gist(upma
, domain
);
2936 contraction
= isl_union_pw_multi_aff_union_add(contraction
, upma
);
2937 tree
= isl_schedule_tree_expansion_set_contraction_and_expansion(tree
,
2938 contraction
, expansion
);
2943 /* Update the tree root "tree" to refer to the group instances
2944 * in data->group rather than the original domain elements in data->domain.
2945 * "pos" is the position in the original schedule tree where the modified
2946 * "tree" will be attached.
2948 * If we have come across a domain or expansion node before (data->finished
2949 * is set), then we no longer need perform any modifications.
2951 * If "tree" is a filter, then we add data->group_universe to the filter.
2952 * We also remove data->domain_universe from the filter if all the domain
2953 * elements in this universe that reach the filter node are part of
2954 * the elements that are being grouped by data->expansion.
2955 * If "tree" is a band, domain or expansion, then it is handled
2956 * in a separate function.
2958 static __isl_give isl_schedule_tree
*group_ancestor(
2959 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2962 struct isl_schedule_group_data
*data
= user
;
2963 isl_union_set
*domain
;
2967 return isl_schedule_tree_free(tree
);
2972 switch (isl_schedule_tree_get_type(tree
)) {
2973 case isl_schedule_node_error
:
2974 return isl_schedule_tree_free(tree
);
2975 case isl_schedule_node_extension
:
2976 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_unsupported
,
2977 "grouping not allowed in extended tree",
2978 return isl_schedule_tree_free(tree
));
2979 case isl_schedule_node_band
:
2980 tree
= group_band(tree
, pos
, data
);
2982 case isl_schedule_node_context
:
2983 tree
= group_context(tree
, pos
, data
);
2985 case isl_schedule_node_domain
:
2986 tree
= group_domain(tree
, pos
, data
);
2989 case isl_schedule_node_filter
:
2990 domain
= isl_schedule_node_get_domain(pos
);
2991 is_covered
= locally_covered_by_domain(domain
, data
);
2992 isl_union_set_free(domain
);
2994 return isl_schedule_tree_free(tree
);
2995 domain
= isl_schedule_tree_filter_get_filter(tree
);
2997 domain
= isl_union_set_subtract(domain
,
2998 isl_union_set_copy(data
->domain_universe
));
2999 domain
= isl_union_set_union(domain
,
3000 isl_union_set_copy(data
->group_universe
));
3001 tree
= isl_schedule_tree_filter_set_filter(tree
, domain
);
3003 case isl_schedule_node_expansion
:
3004 tree
= group_expansion(tree
, pos
, data
);
3007 case isl_schedule_node_leaf
:
3008 case isl_schedule_node_guard
:
3009 case isl_schedule_node_mark
:
3010 case isl_schedule_node_sequence
:
3011 case isl_schedule_node_set
:
3018 /* Group the domain elements that reach "node" into instances
3019 * of a single statement with identifier "group_id".
3020 * In particular, group the domain elements according to their
3023 * That is, introduce an expansion node with as contraction
3024 * the prefix schedule (with the target space replaced by "group_id")
3025 * and as expansion the inverse of this contraction (with its range
3026 * intersected with the domain elements that reach "node").
3027 * The outer nodes are then modified to refer to the group instances
3028 * instead of the original domain elements.
3030 * No instance of "group_id" is allowed to reach "node" prior
3032 * No ancestor of "node" is allowed to be an extension node.
3034 * Return a pointer to original node in tree, i.e., the child
3035 * of the newly introduced expansion node.
3037 __isl_give isl_schedule_node
*isl_schedule_node_group(
3038 __isl_take isl_schedule_node
*node
, __isl_take isl_id
*group_id
)
3040 struct isl_schedule_group_data data
= { 0 };
3042 isl_union_set
*domain
;
3043 isl_union_pw_multi_aff
*contraction
;
3044 isl_union_map
*expansion
;
3047 if (!node
|| !group_id
)
3049 if (check_insert(node
) < 0)
3052 domain
= isl_schedule_node_get_domain(node
);
3053 data
.domain
= isl_union_set_copy(domain
);
3054 data
.domain_universe
= isl_union_set_copy(domain
);
3055 data
.domain_universe
= isl_union_set_universe(data
.domain_universe
);
3057 data
.dim
= isl_schedule_node_get_schedule_depth(node
);
3058 if (data
.dim
== 0) {
3061 isl_union_set
*group
;
3062 isl_union_map
*univ
;
3064 ctx
= isl_schedule_node_get_ctx(node
);
3065 space
= isl_space_set_alloc(ctx
, 0, 0);
3066 space
= isl_space_set_tuple_id(space
, isl_dim_set
, group_id
);
3067 set
= isl_set_universe(isl_space_copy(space
));
3068 group
= isl_union_set_from_set(set
);
3069 expansion
= isl_union_map_from_domain_and_range(domain
, group
);
3070 univ
= isl_union_map_universe(isl_union_map_copy(expansion
));
3071 contraction
= isl_union_pw_multi_aff_from_union_map(univ
);
3072 expansion
= isl_union_map_reverse(expansion
);
3074 isl_multi_union_pw_aff
*prefix
;
3075 isl_union_set
*univ
;
3078 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
3079 prefix
= isl_multi_union_pw_aff_set_tuple_id(prefix
,
3080 isl_dim_set
, group_id
);
3081 space
= isl_multi_union_pw_aff_get_space(prefix
);
3082 contraction
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
3084 univ
= isl_union_set_universe(isl_union_set_copy(domain
));
3086 isl_union_pw_multi_aff_intersect_domain(contraction
, univ
);
3087 expansion
= isl_union_map_from_union_pw_multi_aff(
3088 isl_union_pw_multi_aff_copy(contraction
));
3089 expansion
= isl_union_map_reverse(expansion
);
3090 expansion
= isl_union_map_intersect_range(expansion
, domain
);
3092 space
= isl_space_map_from_set(space
);
3093 data
.sched
= isl_multi_aff_identity(space
);
3094 data
.group
= isl_union_map_domain(isl_union_map_copy(expansion
));
3095 data
.group
= isl_union_set_coalesce(data
.group
);
3096 data
.group_universe
= isl_union_set_copy(data
.group
);
3097 data
.group_universe
= isl_union_set_universe(data
.group_universe
);
3098 data
.expansion
= isl_union_map_copy(expansion
);
3099 data
.contraction
= isl_union_pw_multi_aff_copy(contraction
);
3100 node
= isl_schedule_node_insert_expansion(node
, contraction
, expansion
);
3102 disjoint
= isl_union_set_is_disjoint(data
.domain_universe
,
3103 data
.group_universe
);
3105 node
= update_ancestors(node
, &group_ancestor
, &data
);
3107 isl_union_set_free(data
.domain
);
3108 isl_union_set_free(data
.domain_universe
);
3109 isl_union_set_free(data
.group
);
3110 isl_union_set_free(data
.group_universe
);
3111 isl_multi_aff_free(data
.sched
);
3112 isl_union_map_free(data
.expansion
);
3113 isl_union_pw_multi_aff_free(data
.contraction
);
3115 node
= isl_schedule_node_child(node
, 0);
3117 if (!node
|| disjoint
< 0)
3118 return isl_schedule_node_free(node
);
3120 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
3121 "group instances already reach node",
3122 isl_schedule_node_free(node
));
3126 isl_schedule_node_free(node
);
3127 isl_id_free(group_id
);
3131 /* Compute the gist of the given band node with respect to "context".
3133 __isl_give isl_schedule_node
*isl_schedule_node_band_gist(
3134 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*context
)
3136 isl_schedule_tree
*tree
;
3138 tree
= isl_schedule_node_get_tree(node
);
3139 tree
= isl_schedule_tree_band_gist(tree
, context
);
3140 return isl_schedule_node_graft_tree(node
, tree
);
3143 /* Internal data structure for isl_schedule_node_gist.
3144 * "n_expansion" is the number of outer expansion nodes
3145 * with respect to the current position
3146 * "filters" contains an element for each outer filter, expansion or
3147 * extension node with respect to the current position, each representing
3148 * the intersection of the previous element and the filter on the filter node
3149 * or the expansion/extension of the previous element.
3150 * The first element in the original context passed to isl_schedule_node_gist.
3152 struct isl_node_gist_data
{
3154 isl_union_set_list
*filters
;
3157 /* Enter the expansion node "node" during a isl_schedule_node_gist traversal.
3159 * In particular, add an extra element to data->filters containing
3160 * the expansion of the previous element and replace the expansion
3161 * and contraction on "node" by the gist with respect to these filters.
3162 * Also keep track of the fact that we have entered another expansion.
3164 static __isl_give isl_schedule_node
*gist_enter_expansion(
3165 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3168 isl_union_set
*inner
;
3169 isl_union_map
*expansion
;
3170 isl_union_pw_multi_aff
*contraction
;
3172 data
->n_expansion
++;
3174 n
= isl_union_set_list_n_union_set(data
->filters
);
3175 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3176 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3177 inner
= isl_union_set_apply(inner
, expansion
);
3179 contraction
= isl_schedule_node_expansion_get_contraction(node
);
3180 contraction
= isl_union_pw_multi_aff_gist(contraction
,
3181 isl_union_set_copy(inner
));
3183 data
->filters
= isl_union_set_list_add(data
->filters
, inner
);
3185 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3186 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3187 expansion
= isl_union_map_gist_domain(expansion
, inner
);
3188 node
= isl_schedule_node_expansion_set_contraction_and_expansion(node
,
3189 contraction
, expansion
);
3194 /* Leave the expansion node "node" during a isl_schedule_node_gist traversal.
3196 * In particular, remove the element in data->filters that was added by
3197 * gist_enter_expansion and decrement the number of outer expansions.
3199 * The expansion has already been simplified in gist_enter_expansion.
3200 * If this simplification results in an identity expansion, then
3201 * it is removed here.
3203 static __isl_give isl_schedule_node
*gist_leave_expansion(
3204 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3208 isl_union_map
*expansion
;
3210 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3211 identity
= isl_union_map_is_identity(expansion
);
3212 isl_union_map_free(expansion
);
3215 node
= isl_schedule_node_free(node
);
3217 node
= isl_schedule_node_delete(node
);
3219 n
= isl_union_set_list_n_union_set(data
->filters
);
3220 data
->filters
= isl_union_set_list_drop(data
->filters
, n
- 1, 1);
3222 data
->n_expansion
--;
3227 /* Enter the extension node "node" during a isl_schedule_node_gist traversal.
3229 * In particular, add an extra element to data->filters containing
3230 * the union of the previous element with the additional domain elements
3231 * introduced by the extension.
3233 static __isl_give isl_schedule_node
*gist_enter_extension(
3234 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3237 isl_union_set
*inner
, *extra
;
3238 isl_union_map
*extension
;
3240 n
= isl_union_set_list_n_union_set(data
->filters
);
3241 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3242 extension
= isl_schedule_node_extension_get_extension(node
);
3243 extra
= isl_union_map_range(extension
);
3244 inner
= isl_union_set_union(inner
, extra
);
3246 data
->filters
= isl_union_set_list_add(data
->filters
, inner
);
3251 /* Can we finish gisting at this node?
3252 * That is, is the filter on the current filter node a subset of
3253 * the original context passed to isl_schedule_node_gist?
3254 * If we have gone through any expansions, then we cannot perform
3255 * this test since the current domain elements are incomparable
3256 * to the domain elements in the original context.
3258 static int gist_done(__isl_keep isl_schedule_node
*node
,
3259 struct isl_node_gist_data
*data
)
3261 isl_union_set
*filter
, *outer
;
3264 if (data
->n_expansion
!= 0)
3267 filter
= isl_schedule_node_filter_get_filter(node
);
3268 outer
= isl_union_set_list_get_union_set(data
->filters
, 0);
3269 subset
= isl_union_set_is_subset(filter
, outer
);
3270 isl_union_set_free(outer
);
3271 isl_union_set_free(filter
);
3276 /* Callback for "traverse" to enter a node and to move
3277 * to the deepest initial subtree that should be traversed
3278 * by isl_schedule_node_gist.
3280 * The "filters" list is extended by one element each time
3281 * we come across a filter node by the result of intersecting
3282 * the last element in the list with the filter on the filter node.
3284 * If the filter on the current filter node is a subset of
3285 * the original context passed to isl_schedule_node_gist,
3286 * then there is no need to go into its subtree since it cannot
3287 * be further simplified by the context. The "filters" list is
3288 * still extended for consistency, but the actual value of the
3289 * added element is immaterial since it will not be used.
3291 * Otherwise, the filter on the current filter node is replaced by
3292 * the gist of the original filter with respect to the intersection
3293 * of the original context with the intermediate filters.
3295 * If the new element in the "filters" list is empty, then no elements
3296 * can reach the descendants of the current filter node. The subtree
3297 * underneath the filter node is therefore removed.
3299 * Each expansion node we come across is handled by
3300 * gist_enter_expansion.
3302 * Each extension node we come across is handled by
3303 * gist_enter_extension.
3305 static __isl_give isl_schedule_node
*gist_enter(
3306 __isl_take isl_schedule_node
*node
, void *user
)
3308 struct isl_node_gist_data
*data
= user
;
3311 isl_union_set
*filter
, *inner
;
3315 switch (isl_schedule_node_get_type(node
)) {
3316 case isl_schedule_node_error
:
3317 return isl_schedule_node_free(node
);
3318 case isl_schedule_node_expansion
:
3319 node
= gist_enter_expansion(node
, data
);
3321 case isl_schedule_node_extension
:
3322 node
= gist_enter_extension(node
, data
);
3324 case isl_schedule_node_band
:
3325 case isl_schedule_node_context
:
3326 case isl_schedule_node_domain
:
3327 case isl_schedule_node_guard
:
3328 case isl_schedule_node_leaf
:
3329 case isl_schedule_node_mark
:
3330 case isl_schedule_node_sequence
:
3331 case isl_schedule_node_set
:
3333 case isl_schedule_node_filter
:
3336 done
= gist_done(node
, data
);
3337 filter
= isl_schedule_node_filter_get_filter(node
);
3338 if (done
< 0 || done
) {
3339 data
->filters
= isl_union_set_list_add(data
->filters
,
3342 return isl_schedule_node_free(node
);
3345 n
= isl_union_set_list_n_union_set(data
->filters
);
3346 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3347 filter
= isl_union_set_gist(filter
, isl_union_set_copy(inner
));
3348 node
= isl_schedule_node_filter_set_filter(node
,
3349 isl_union_set_copy(filter
));
3350 filter
= isl_union_set_intersect(filter
, inner
);
3351 empty
= isl_union_set_is_empty(filter
);
3352 data
->filters
= isl_union_set_list_add(data
->filters
, filter
);
3354 return isl_schedule_node_free(node
);
3357 node
= isl_schedule_node_child(node
, 0);
3358 node
= isl_schedule_node_cut(node
);
3359 node
= isl_schedule_node_parent(node
);
3361 } while (isl_schedule_node_has_children(node
) &&
3362 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3367 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
3369 * In particular, if the current node is a filter node, then we remove
3370 * the element on the "filters" list that was added when we entered
3371 * the node. There is no need to compute any gist here, since we
3372 * already did that when we entered the node.
3374 * Expansion nodes are handled by gist_leave_expansion.
3376 * If the current node is an extension, then remove the element
3377 * in data->filters that was added by gist_enter_extension.
3379 * If the current node is a band node, then we compute the gist of
3380 * the band node with respect to the intersection of the original context
3381 * and the intermediate filters.
3383 * If the current node is a sequence or set node, then some of
3384 * the filter children may have become empty and so they are removed.
3385 * If only one child is left, then the set or sequence node along with
3386 * the single remaining child filter is removed. The filter can be
3387 * removed because the filters on a sequence or set node are supposed
3388 * to partition the incoming domain instances.
3389 * In principle, it should then be impossible for there to be zero
3390 * remaining children, but should this happen, we replace the entire
3391 * subtree with an empty filter.
3393 static __isl_give isl_schedule_node
*gist_leave(
3394 __isl_take isl_schedule_node
*node
, void *user
)
3396 struct isl_node_gist_data
*data
= user
;
3397 isl_schedule_tree
*tree
;
3399 isl_union_set
*filter
;
3401 switch (isl_schedule_node_get_type(node
)) {
3402 case isl_schedule_node_error
:
3403 return isl_schedule_node_free(node
);
3404 case isl_schedule_node_expansion
:
3405 node
= gist_leave_expansion(node
, data
);
3407 case isl_schedule_node_extension
:
3408 case isl_schedule_node_filter
:
3409 n
= isl_union_set_list_n_union_set(data
->filters
);
3410 data
->filters
= isl_union_set_list_drop(data
->filters
,
3413 case isl_schedule_node_band
:
3414 n
= isl_union_set_list_n_union_set(data
->filters
);
3415 filter
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3416 node
= isl_schedule_node_band_gist(node
, filter
);
3418 case isl_schedule_node_set
:
3419 case isl_schedule_node_sequence
:
3420 tree
= isl_schedule_node_get_tree(node
);
3421 n
= isl_schedule_tree_n_children(tree
);
3422 for (i
= n
- 1; i
>= 0; --i
) {
3423 isl_schedule_tree
*child
;
3424 isl_union_set
*filter
;
3427 child
= isl_schedule_tree_get_child(tree
, i
);
3428 filter
= isl_schedule_tree_filter_get_filter(child
);
3429 empty
= isl_union_set_is_empty(filter
);
3430 isl_union_set_free(filter
);
3431 isl_schedule_tree_free(child
);
3433 tree
= isl_schedule_tree_free(tree
);
3435 tree
= isl_schedule_tree_drop_child(tree
, i
);
3437 n
= isl_schedule_tree_n_children(tree
);
3438 node
= isl_schedule_node_graft_tree(node
, tree
);
3440 node
= isl_schedule_node_delete(node
);
3441 node
= isl_schedule_node_delete(node
);
3442 } else if (n
== 0) {
3446 isl_union_set_list_get_union_set(data
->filters
, 0);
3447 space
= isl_union_set_get_space(filter
);
3448 isl_union_set_free(filter
);
3449 filter
= isl_union_set_empty(space
);
3450 node
= isl_schedule_node_cut(node
);
3451 node
= isl_schedule_node_insert_filter(node
, filter
);
3454 case isl_schedule_node_context
:
3455 case isl_schedule_node_domain
:
3456 case isl_schedule_node_guard
:
3457 case isl_schedule_node_leaf
:
3458 case isl_schedule_node_mark
:
3465 /* Compute the gist of the subtree at "node" with respect to
3466 * the reaching domain elements in "context".
3467 * In particular, compute the gist of all band and filter nodes
3468 * in the subtree with respect to "context". Children of set or sequence
3469 * nodes that end up with an empty filter are removed completely.
3471 * We keep track of the intersection of "context" with all outer filters
3472 * of the current node within the subtree in the final element of "filters".
3473 * Initially, this list contains the single element "context" and it is
3474 * extended or shortened each time we enter or leave a filter node.
3476 __isl_give isl_schedule_node
*isl_schedule_node_gist(
3477 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*context
)
3479 struct isl_node_gist_data data
;
3481 data
.n_expansion
= 0;
3482 data
.filters
= isl_union_set_list_from_union_set(context
);
3483 node
= traverse(node
, &gist_enter
, &gist_leave
, &data
);
3484 isl_union_set_list_free(data
.filters
);
3488 /* Intersect the domain of domain node "node" with "domain".
3490 * If the domain of "node" is already a subset of "domain",
3491 * then nothing needs to be changed.
3493 * Otherwise, we replace the domain of the domain node by the intersection
3494 * and simplify the subtree rooted at "node" with respect to this intersection.
3496 __isl_give isl_schedule_node
*isl_schedule_node_domain_intersect_domain(
3497 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
)
3499 isl_schedule_tree
*tree
;
3500 isl_union_set
*uset
;
3503 if (!node
|| !domain
)
3506 uset
= isl_schedule_tree_domain_get_domain(node
->tree
);
3507 is_subset
= isl_union_set_is_subset(uset
, domain
);
3508 isl_union_set_free(uset
);
3512 isl_union_set_free(domain
);
3516 tree
= isl_schedule_tree_copy(node
->tree
);
3517 uset
= isl_schedule_tree_domain_get_domain(tree
);
3518 uset
= isl_union_set_intersect(uset
, domain
);
3519 tree
= isl_schedule_tree_domain_set_domain(tree
,
3520 isl_union_set_copy(uset
));
3521 node
= isl_schedule_node_graft_tree(node
, tree
);
3523 node
= isl_schedule_node_child(node
, 0);
3524 node
= isl_schedule_node_gist(node
, uset
);
3525 node
= isl_schedule_node_parent(node
);
3529 isl_schedule_node_free(node
);
3530 isl_union_set_free(domain
);
3534 /* Replace the domain of domain node "node" with the gist
3535 * of the original domain with respect to the parameter domain "context".
3537 __isl_give isl_schedule_node
*isl_schedule_node_domain_gist_params(
3538 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*context
)
3540 isl_union_set
*domain
;
3541 isl_schedule_tree
*tree
;
3543 if (!node
|| !context
)
3546 tree
= isl_schedule_tree_copy(node
->tree
);
3547 domain
= isl_schedule_tree_domain_get_domain(node
->tree
);
3548 domain
= isl_union_set_gist_params(domain
, context
);
3549 tree
= isl_schedule_tree_domain_set_domain(tree
, domain
);
3550 node
= isl_schedule_node_graft_tree(node
, tree
);
3554 isl_schedule_node_free(node
);
3555 isl_set_free(context
);
3559 /* Internal data structure for isl_schedule_node_get_subtree_expansion.
3560 * "expansions" contains a list of accumulated expansions
3561 * for each outer expansion, set or sequence node. The first element
3562 * in the list is an identity mapping on the reaching domain elements.
3563 * "res" collects the results.
3565 struct isl_subtree_expansion_data
{
3566 isl_union_map_list
*expansions
;
3570 /* Callback for "traverse" to enter a node and to move
3571 * to the deepest initial subtree that should be traversed
3572 * by isl_schedule_node_get_subtree_expansion.
3574 * Whenever we come across an expansion node, the last element
3575 * of data->expansions is combined with the expansion
3576 * on the expansion node.
3578 * Whenever we come across a filter node that is the child
3579 * of a set or sequence node, data->expansions is extended
3580 * with a new element that restricts the previous element
3581 * to the elements selected by the filter.
3582 * The previous element can then be reused while backtracking.
3584 static __isl_give isl_schedule_node
*subtree_expansion_enter(
3585 __isl_take isl_schedule_node
*node
, void *user
)
3587 struct isl_subtree_expansion_data
*data
= user
;
3590 enum isl_schedule_node_type type
;
3591 isl_union_set
*filter
;
3592 isl_union_map
*inner
, *expansion
;
3595 switch (isl_schedule_node_get_type(node
)) {
3596 case isl_schedule_node_error
:
3597 return isl_schedule_node_free(node
);
3598 case isl_schedule_node_filter
:
3599 type
= isl_schedule_node_get_parent_type(node
);
3600 if (type
!= isl_schedule_node_set
&&
3601 type
!= isl_schedule_node_sequence
)
3603 filter
= isl_schedule_node_filter_get_filter(node
);
3604 n
= isl_union_map_list_n_union_map(data
->expansions
);
3606 isl_union_map_list_get_union_map(data
->expansions
,
3608 inner
= isl_union_map_intersect_range(inner
, filter
);
3610 isl_union_map_list_add(data
->expansions
, inner
);
3612 case isl_schedule_node_expansion
:
3613 n
= isl_union_map_list_n_union_map(data
->expansions
);
3615 isl_schedule_node_expansion_get_expansion(node
);
3617 isl_union_map_list_get_union_map(data
->expansions
,
3619 inner
= isl_union_map_apply_range(inner
, expansion
);
3621 isl_union_map_list_set_union_map(data
->expansions
,
3624 case isl_schedule_node_band
:
3625 case isl_schedule_node_context
:
3626 case isl_schedule_node_domain
:
3627 case isl_schedule_node_extension
:
3628 case isl_schedule_node_guard
:
3629 case isl_schedule_node_leaf
:
3630 case isl_schedule_node_mark
:
3631 case isl_schedule_node_sequence
:
3632 case isl_schedule_node_set
:
3635 } while (isl_schedule_node_has_children(node
) &&
3636 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3641 /* Callback for "traverse" to leave a node for
3642 * isl_schedule_node_get_subtree_expansion.
3644 * If we come across a filter node that is the child
3645 * of a set or sequence node, then we remove the element
3646 * of data->expansions that was added in subtree_expansion_enter.
3648 * If we reach a leaf node, then the accumulated expansion is
3649 * added to data->res.
3651 static __isl_give isl_schedule_node
*subtree_expansion_leave(
3652 __isl_take isl_schedule_node
*node
, void *user
)
3654 struct isl_subtree_expansion_data
*data
= user
;
3656 isl_union_map
*inner
;
3657 enum isl_schedule_node_type type
;
3659 switch (isl_schedule_node_get_type(node
)) {
3660 case isl_schedule_node_error
:
3661 return isl_schedule_node_free(node
);
3662 case isl_schedule_node_filter
:
3663 type
= isl_schedule_node_get_parent_type(node
);
3664 if (type
!= isl_schedule_node_set
&&
3665 type
!= isl_schedule_node_sequence
)
3667 n
= isl_union_map_list_n_union_map(data
->expansions
);
3668 data
->expansions
= isl_union_map_list_drop(data
->expansions
,
3671 case isl_schedule_node_leaf
:
3672 n
= isl_union_map_list_n_union_map(data
->expansions
);
3673 inner
= isl_union_map_list_get_union_map(data
->expansions
,
3675 data
->res
= isl_union_map_union(data
->res
, inner
);
3677 case isl_schedule_node_band
:
3678 case isl_schedule_node_context
:
3679 case isl_schedule_node_domain
:
3680 case isl_schedule_node_expansion
:
3681 case isl_schedule_node_extension
:
3682 case isl_schedule_node_guard
:
3683 case isl_schedule_node_mark
:
3684 case isl_schedule_node_sequence
:
3685 case isl_schedule_node_set
:
3692 /* Return a mapping from the domain elements that reach "node"
3693 * to the corresponding domain elements in the leaves of the subtree
3694 * rooted at "node" obtained by composing the intermediate expansions.
3696 * We start out with an identity mapping between the domain elements
3697 * that reach "node" and compose it with all the expansions
3698 * on a path from "node" to a leaf while traversing the subtree.
3699 * Within the children of an a sequence or set node, the
3700 * accumulated expansion is restricted to the elements selected
3701 * by the filter child.
3703 __isl_give isl_union_map
*isl_schedule_node_get_subtree_expansion(
3704 __isl_keep isl_schedule_node
*node
)
3706 struct isl_subtree_expansion_data data
;
3708 isl_union_set
*domain
;
3709 isl_union_map
*expansion
;
3714 domain
= isl_schedule_node_get_universe_domain(node
);
3715 space
= isl_union_set_get_space(domain
);
3716 expansion
= isl_union_set_identity(domain
);
3717 data
.res
= isl_union_map_empty(space
);
3718 data
.expansions
= isl_union_map_list_from_union_map(expansion
);
3720 node
= isl_schedule_node_copy(node
);
3721 node
= traverse(node
, &subtree_expansion_enter
,
3722 &subtree_expansion_leave
, &data
);
3724 data
.res
= isl_union_map_free(data
.res
);
3725 isl_schedule_node_free(node
);
3727 isl_union_map_list_free(data
.expansions
);
3732 /* Internal data structure for isl_schedule_node_get_subtree_contraction.
3733 * "contractions" contains a list of accumulated contractions
3734 * for each outer expansion, set or sequence node. The first element
3735 * in the list is an identity mapping on the reaching domain elements.
3736 * "res" collects the results.
3738 struct isl_subtree_contraction_data
{
3739 isl_union_pw_multi_aff_list
*contractions
;
3740 isl_union_pw_multi_aff
*res
;
3743 /* Callback for "traverse" to enter a node and to move
3744 * to the deepest initial subtree that should be traversed
3745 * by isl_schedule_node_get_subtree_contraction.
3747 * Whenever we come across an expansion node, the last element
3748 * of data->contractions is combined with the contraction
3749 * on the expansion node.
3751 * Whenever we come across a filter node that is the child
3752 * of a set or sequence node, data->contractions is extended
3753 * with a new element that restricts the previous element
3754 * to the elements selected by the filter.
3755 * The previous element can then be reused while backtracking.
3757 static __isl_give isl_schedule_node
*subtree_contraction_enter(
3758 __isl_take isl_schedule_node
*node
, void *user
)
3760 struct isl_subtree_contraction_data
*data
= user
;
3763 enum isl_schedule_node_type type
;
3764 isl_union_set
*filter
;
3765 isl_union_pw_multi_aff
*inner
, *contraction
;
3768 switch (isl_schedule_node_get_type(node
)) {
3769 case isl_schedule_node_error
:
3770 return isl_schedule_node_free(node
);
3771 case isl_schedule_node_filter
:
3772 type
= isl_schedule_node_get_parent_type(node
);
3773 if (type
!= isl_schedule_node_set
&&
3774 type
!= isl_schedule_node_sequence
)
3776 filter
= isl_schedule_node_filter_get_filter(node
);
3777 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3778 data
->contractions
);
3780 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3781 data
->contractions
, n
- 1);
3782 inner
= isl_union_pw_multi_aff_intersect_domain(inner
,
3784 data
->contractions
=
3785 isl_union_pw_multi_aff_list_add(data
->contractions
,
3788 case isl_schedule_node_expansion
:
3789 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3790 data
->contractions
);
3792 isl_schedule_node_expansion_get_contraction(node
);
3794 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3795 data
->contractions
, n
- 1);
3797 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3798 inner
, contraction
);
3799 data
->contractions
=
3800 isl_union_pw_multi_aff_list_set_union_pw_multi_aff(
3801 data
->contractions
, n
- 1, inner
);
3803 case isl_schedule_node_band
:
3804 case isl_schedule_node_context
:
3805 case isl_schedule_node_domain
:
3806 case isl_schedule_node_extension
:
3807 case isl_schedule_node_guard
:
3808 case isl_schedule_node_leaf
:
3809 case isl_schedule_node_mark
:
3810 case isl_schedule_node_sequence
:
3811 case isl_schedule_node_set
:
3814 } while (isl_schedule_node_has_children(node
) &&
3815 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3820 /* Callback for "traverse" to leave a node for
3821 * isl_schedule_node_get_subtree_contraction.
3823 * If we come across a filter node that is the child
3824 * of a set or sequence node, then we remove the element
3825 * of data->contractions that was added in subtree_contraction_enter.
3827 * If we reach a leaf node, then the accumulated contraction is
3828 * added to data->res.
3830 static __isl_give isl_schedule_node
*subtree_contraction_leave(
3831 __isl_take isl_schedule_node
*node
, void *user
)
3833 struct isl_subtree_contraction_data
*data
= user
;
3835 isl_union_pw_multi_aff
*inner
;
3836 enum isl_schedule_node_type type
;
3838 switch (isl_schedule_node_get_type(node
)) {
3839 case isl_schedule_node_error
:
3840 return isl_schedule_node_free(node
);
3841 case isl_schedule_node_filter
:
3842 type
= isl_schedule_node_get_parent_type(node
);
3843 if (type
!= isl_schedule_node_set
&&
3844 type
!= isl_schedule_node_sequence
)
3846 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3847 data
->contractions
);
3848 data
->contractions
=
3849 isl_union_pw_multi_aff_list_drop(data
->contractions
,
3852 case isl_schedule_node_leaf
:
3853 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3854 data
->contractions
);
3855 inner
= isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3856 data
->contractions
, n
- 1);
3857 data
->res
= isl_union_pw_multi_aff_union_add(data
->res
, inner
);
3859 case isl_schedule_node_band
:
3860 case isl_schedule_node_context
:
3861 case isl_schedule_node_domain
:
3862 case isl_schedule_node_expansion
:
3863 case isl_schedule_node_extension
:
3864 case isl_schedule_node_guard
:
3865 case isl_schedule_node_mark
:
3866 case isl_schedule_node_sequence
:
3867 case isl_schedule_node_set
:
3874 /* Return a mapping from the domain elements in the leaves of the subtree
3875 * rooted at "node" to the corresponding domain elements that reach "node"
3876 * obtained by composing the intermediate contractions.
3878 * We start out with an identity mapping between the domain elements
3879 * that reach "node" and compose it with all the contractions
3880 * on a path from "node" to a leaf while traversing the subtree.
3881 * Within the children of an a sequence or set node, the
3882 * accumulated contraction is restricted to the elements selected
3883 * by the filter child.
3885 __isl_give isl_union_pw_multi_aff
*isl_schedule_node_get_subtree_contraction(
3886 __isl_keep isl_schedule_node
*node
)
3888 struct isl_subtree_contraction_data data
;
3890 isl_union_set
*domain
;
3891 isl_union_pw_multi_aff
*contraction
;
3896 domain
= isl_schedule_node_get_universe_domain(node
);
3897 space
= isl_union_set_get_space(domain
);
3898 contraction
= isl_union_set_identity_union_pw_multi_aff(domain
);
3899 data
.res
= isl_union_pw_multi_aff_empty(space
);
3901 isl_union_pw_multi_aff_list_from_union_pw_multi_aff(contraction
);
3903 node
= isl_schedule_node_copy(node
);
3904 node
= traverse(node
, &subtree_contraction_enter
,
3905 &subtree_contraction_leave
, &data
);
3907 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
3908 isl_schedule_node_free(node
);
3910 isl_union_pw_multi_aff_list_free(data
.contractions
);
3915 /* Do the nearest "n" ancestors of "node" have the types given in "types"
3916 * (starting at the parent of "node")?
3918 static int has_ancestors(__isl_keep isl_schedule_node
*node
,
3919 int n
, enum isl_schedule_node_type
*types
)
3926 n_ancestor
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
3930 for (i
= 0; i
< n
; ++i
) {
3931 isl_schedule_tree
*tree
;
3934 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
3935 n_ancestor
- 1 - i
);
3938 correct_type
= isl_schedule_tree_get_type(tree
) == types
[i
];
3939 isl_schedule_tree_free(tree
);
3947 /* Given a node "node" that appears in an extension (i.e., it is the child
3948 * of a filter in a sequence inside an extension node), are the spaces
3949 * of the extension specified by "extension" disjoint from those
3950 * of both the original extension and the domain elements that reach
3951 * that original extension?
3953 static int is_disjoint_extension(__isl_keep isl_schedule_node
*node
,
3954 __isl_keep isl_union_map
*extension
)
3957 isl_union_set
*domain
;
3960 node
= isl_schedule_node_copy(node
);
3961 node
= isl_schedule_node_parent(node
);
3962 node
= isl_schedule_node_parent(node
);
3963 node
= isl_schedule_node_parent(node
);
3964 old
= isl_schedule_node_extension_get_extension(node
);
3965 domain
= isl_schedule_node_get_universe_domain(node
);
3966 isl_schedule_node_free(node
);
3967 old
= isl_union_map_universe(old
);
3968 domain
= isl_union_set_union(domain
, isl_union_map_range(old
));
3969 extension
= isl_union_map_copy(extension
);
3970 extension
= isl_union_map_intersect_range(extension
, domain
);
3971 empty
= isl_union_map_is_empty(extension
);
3972 isl_union_map_free(extension
);
3977 /* Given a node "node" that is governed by an extension node, extend
3978 * that extension node with "extension".
3980 * In particular, "node" is the child of a filter in a sequence that
3981 * is in turn a child of an extension node. Extend that extension node
3984 * Return a pointer to the parent of the original node (i.e., a filter).
3986 static __isl_give isl_schedule_node
*extend_extension(
3987 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
3991 isl_union_map
*node_extension
;
3993 node
= isl_schedule_node_parent(node
);
3994 pos
= isl_schedule_node_get_child_position(node
);
3995 node
= isl_schedule_node_parent(node
);
3996 node
= isl_schedule_node_parent(node
);
3997 node_extension
= isl_schedule_node_extension_get_extension(node
);
3998 disjoint
= isl_union_map_is_disjoint(extension
, node_extension
);
3999 extension
= isl_union_map_union(extension
, node_extension
);
4000 node
= isl_schedule_node_extension_set_extension(node
, extension
);
4001 node
= isl_schedule_node_child(node
, 0);
4002 node
= isl_schedule_node_child(node
, pos
);
4005 return isl_schedule_node_free(node
);
4009 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4010 "extension domain should be disjoint from earlier "
4011 "extensions", return isl_schedule_node_free(node
));
4016 /* Return the universe of "uset" if this universe is disjoint from "ref".
4017 * Otherwise, return "uset".
4019 * Also check if "uset" itself is disjoint from "ref", reporting
4020 * an error if it is not.
4022 static __isl_give isl_union_set
*replace_by_universe_if_disjoint(
4023 __isl_take isl_union_set
*uset
, __isl_keep isl_union_set
*ref
)
4026 isl_union_set
*universe
;
4028 disjoint
= isl_union_set_is_disjoint(uset
, ref
);
4030 return isl_union_set_free(uset
);
4032 isl_die(isl_union_set_get_ctx(uset
), isl_error_invalid
,
4033 "extension domain should be disjoint from "
4034 "current domain", return isl_union_set_free(uset
));
4036 universe
= isl_union_set_universe(isl_union_set_copy(uset
));
4037 disjoint
= isl_union_set_is_disjoint(universe
, ref
);
4038 if (disjoint
>= 0 && disjoint
) {
4039 isl_union_set_free(uset
);
4042 isl_union_set_free(universe
);
4045 return isl_union_set_free(uset
);
4049 /* Insert an extension node on top of "node" with extension "extension".
4050 * In addition, insert a filter that separates node from the extension
4051 * between the extension node and "node".
4052 * Return a pointer to the inserted filter node.
4054 * If "node" already appears in an extension (i.e., if it is the child
4055 * of a filter in a sequence inside an extension node), then extend that
4056 * extension with "extension" instead.
4057 * In this case, a pointer to the original filter node is returned.
4058 * Note that if some of the elements in the new extension live in the
4059 * same space as those of the original extension or the domain elements
4060 * reaching the original extension, then we insert a new extension anyway.
4061 * Otherwise, we would have to adjust the filters in the sequence child
4062 * of the extension to ensure that the elements in the new extension
4065 static __isl_give isl_schedule_node
*insert_extension(
4066 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
4068 enum isl_schedule_node_type ancestors
[] =
4069 { isl_schedule_node_filter
, isl_schedule_node_sequence
,
4070 isl_schedule_node_extension
};
4071 isl_union_set
*domain
;
4072 isl_union_set
*filter
;
4075 in_ext
= has_ancestors(node
, 3, ancestors
);
4081 disjoint
= is_disjoint_extension(node
, extension
);
4085 return extend_extension(node
, extension
);
4088 filter
= isl_schedule_node_get_domain(node
);
4089 domain
= isl_union_map_range(isl_union_map_copy(extension
));
4090 filter
= replace_by_universe_if_disjoint(filter
, domain
);
4091 isl_union_set_free(domain
);
4093 node
= isl_schedule_node_insert_filter(node
, filter
);
4094 node
= isl_schedule_node_insert_extension(node
, extension
);
4095 node
= isl_schedule_node_child(node
, 0);
4098 isl_schedule_node_free(node
);
4099 isl_union_map_free(extension
);
4103 /* Replace the subtree that "node" points to by "tree" (which has
4104 * a sequence root with two children), except if the parent of "node"
4105 * is a sequence as well, in which case "tree" is spliced at the position
4106 * of "node" in its parent.
4107 * Return a pointer to the child of the "tree_pos" (filter) child of "tree"
4108 * in the updated schedule tree.
4110 static __isl_give isl_schedule_node
*graft_or_splice(
4111 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_tree
*tree
,
4116 if (isl_schedule_node_get_parent_type(node
) ==
4117 isl_schedule_node_sequence
) {
4118 pos
= isl_schedule_node_get_child_position(node
);
4119 node
= isl_schedule_node_parent(node
);
4120 node
= isl_schedule_node_sequence_splice(node
, pos
, tree
);
4123 node
= isl_schedule_node_graft_tree(node
, tree
);
4125 node
= isl_schedule_node_child(node
, pos
+ tree_pos
);
4126 node
= isl_schedule_node_child(node
, 0);
4131 /* Insert a node "graft" into the schedule tree of "node" such that it
4132 * is executed before (if "before" is set) or after (if "before" is not set)
4133 * the node that "node" points to.
4134 * The root of "graft" is an extension node.
4135 * Return a pointer to the node that "node" pointed to.
4137 * We first insert an extension node on top of "node" (or extend
4138 * the extension node if there already is one), with a filter on "node"
4139 * separating it from the extension.
4140 * We then insert a filter in the graft to separate it from the original
4141 * domain elements and combine the original and new tree in a sequence.
4142 * If we have extended an extension node, then the children of this
4143 * sequence are spliced in the sequence of the extended extension
4144 * at the position where "node" appears in the original extension.
4145 * Otherwise, the sequence pair is attached to the new extension node.
4147 static __isl_give isl_schedule_node
*graft_extension(
4148 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
,
4151 isl_union_map
*extension
;
4152 isl_union_set
*graft_domain
;
4153 isl_union_set
*node_domain
;
4154 isl_schedule_tree
*tree
, *tree_graft
;
4156 extension
= isl_schedule_node_extension_get_extension(graft
);
4157 graft_domain
= isl_union_map_range(isl_union_map_copy(extension
));
4158 node_domain
= isl_schedule_node_get_universe_domain(node
);
4159 node
= insert_extension(node
, extension
);
4161 graft_domain
= replace_by_universe_if_disjoint(graft_domain
,
4163 isl_union_set_free(node_domain
);
4165 tree
= isl_schedule_node_get_tree(node
);
4166 if (!isl_schedule_node_has_children(graft
)) {
4167 tree_graft
= isl_schedule_tree_from_filter(graft_domain
);
4169 graft
= isl_schedule_node_child(graft
, 0);
4170 tree_graft
= isl_schedule_node_get_tree(graft
);
4171 tree_graft
= isl_schedule_tree_insert_filter(tree_graft
,
4175 tree
= isl_schedule_tree_sequence_pair(tree_graft
, tree
);
4177 tree
= isl_schedule_tree_sequence_pair(tree
, tree_graft
);
4178 node
= graft_or_splice(node
, tree
, before
);
4180 isl_schedule_node_free(graft
);
4185 /* Replace the root domain node of "node" by an extension node suitable
4186 * for insertion at "pos".
4187 * That is, create an extension node that maps the outer band nodes
4188 * at "pos" to the domain of the root node of "node" and attach
4189 * the child of this root node to the extension node.
4191 static __isl_give isl_schedule_node
*extension_from_domain(
4192 __isl_take isl_schedule_node
*node
, __isl_keep isl_schedule_node
*pos
)
4194 isl_union_set
*universe
;
4195 isl_union_set
*domain
;
4200 isl_schedule_node
*res
;
4201 isl_schedule_tree
*tree
;
4203 anchored
= isl_schedule_node_is_subtree_anchored(node
);
4205 return isl_schedule_node_free(node
);
4207 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
4208 "cannot graft anchored tree with domain root",
4209 return isl_schedule_node_free(node
));
4211 depth
= isl_schedule_node_get_schedule_depth(pos
);
4212 domain
= isl_schedule_node_domain_get_domain(node
);
4213 space
= isl_union_set_get_space(domain
);
4214 space
= isl_space_set_from_params(space
);
4215 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
4216 universe
= isl_union_set_from_set(isl_set_universe(space
));
4217 ext
= isl_union_map_from_domain_and_range(universe
, domain
);
4218 res
= isl_schedule_node_from_extension(ext
);
4219 node
= isl_schedule_node_child(node
, 0);
4221 return isl_schedule_node_free(res
);
4222 if (!isl_schedule_tree_is_leaf(node
->tree
)) {
4223 tree
= isl_schedule_node_get_tree(node
);
4224 res
= isl_schedule_node_child(res
, 0);
4225 res
= isl_schedule_node_graft_tree(res
, tree
);
4226 res
= isl_schedule_node_parent(res
);
4228 isl_schedule_node_free(node
);
4233 /* Insert a node "graft" into the schedule tree of "node" such that it
4234 * is executed before (if "before" is set) or after (if "before" is not set)
4235 * the node that "node" points to.
4236 * The root of "graft" may be either a domain or an extension node.
4237 * In the latter case, the domain of the extension needs to correspond
4238 * to the outer band nodes of "node".
4239 * The elements of the domain or the range of the extension may not
4240 * intersect with the domain elements that reach "node".
4241 * The schedule tree of "graft" may not be anchored.
4243 * The schedule tree of "node" is modified to include an extension node
4244 * corresponding to the root node of "graft" as a child of the original
4245 * parent of "node". The original node that "node" points to and the
4246 * child of the root node of "graft" are attached to this extension node
4247 * through a sequence, with appropriate filters and with the child
4248 * of "graft" appearing before or after the original "node".
4250 * If "node" already appears inside a sequence that is the child of
4251 * an extension node and if the spaces of the new domain elements
4252 * do not overlap with those of the original domain elements,
4253 * then that extension node is extended with the new extension
4254 * rather than introducing a new segment of extension and sequence nodes.
4256 * Return a pointer to the same node in the modified tree that
4257 * "node" pointed to in the original tree.
4259 static __isl_give isl_schedule_node
*isl_schedule_node_graft_before_or_after(
4260 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
,
4263 if (!node
|| !graft
)
4265 if (check_insert(node
) < 0)
4268 if (isl_schedule_node_get_type(graft
) == isl_schedule_node_domain
)
4269 graft
= extension_from_domain(graft
, node
);
4271 if (isl_schedule_node_get_type(graft
) != isl_schedule_node_extension
)
4272 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4273 "expecting domain or extension as root of graft",
4276 return graft_extension(node
, graft
, before
);
4278 isl_schedule_node_free(node
);
4279 isl_schedule_node_free(graft
);
4283 /* Insert a node "graft" into the schedule tree of "node" such that it
4284 * is executed before the node that "node" points to.
4285 * The root of "graft" may be either a domain or an extension node.
4286 * In the latter case, the domain of the extension needs to correspond
4287 * to the outer band nodes of "node".
4288 * The elements of the domain or the range of the extension may not
4289 * intersect with the domain elements that reach "node".
4290 * The schedule tree of "graft" may not be anchored.
4292 * Return a pointer to the same node in the modified tree that
4293 * "node" pointed to in the original tree.
4295 __isl_give isl_schedule_node
*isl_schedule_node_graft_before(
4296 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
)
4298 return isl_schedule_node_graft_before_or_after(node
, graft
, 1);
4301 /* Insert a node "graft" into the schedule tree of "node" such that it
4302 * is executed after the node that "node" points to.
4303 * The root of "graft" may be either a domain or an extension node.
4304 * In the latter case, the domain of the extension needs to correspond
4305 * to the outer band nodes of "node".
4306 * The elements of the domain or the range of the extension may not
4307 * intersect with the domain elements that reach "node".
4308 * The schedule tree of "graft" may not be anchored.
4310 * Return a pointer to the same node in the modified tree that
4311 * "node" pointed to in the original tree.
4313 __isl_give isl_schedule_node
*isl_schedule_node_graft_after(
4314 __isl_take isl_schedule_node
*node
,
4315 __isl_take isl_schedule_node
*graft
)
4317 return isl_schedule_node_graft_before_or_after(node
, graft
, 0);
4320 /* Split the domain elements that reach "node" into those that satisfy
4321 * "filter" and those that do not. Arrange for the first subset to be
4322 * executed before or after the second subset, depending on the value
4324 * Return a pointer to the tree corresponding to the second subset,
4325 * except when this subset is empty in which case the original pointer
4327 * If both subsets are non-empty, then a sequence node is introduced
4328 * to impose the order. If the grandparent of the original node was
4329 * itself a sequence, then the original child is replaced by two children
4330 * in this sequence instead.
4331 * The children in the sequence are copies of the original subtree,
4332 * simplified with respect to their filters.
4334 static __isl_give isl_schedule_node
*isl_schedule_node_order_before_or_after(
4335 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
,
4338 enum isl_schedule_node_type ancestors
[] =
4339 { isl_schedule_node_filter
, isl_schedule_node_sequence
};
4340 isl_union_set
*node_domain
, *node_filter
= NULL
, *parent_filter
;
4341 isl_schedule_node
*node2
;
4342 isl_schedule_tree
*tree1
, *tree2
;
4346 if (!node
|| !filter
)
4348 if (check_insert(node
) < 0)
4351 in_seq
= has_ancestors(node
, 2, ancestors
);
4354 node_domain
= isl_schedule_node_get_domain(node
);
4355 filter
= isl_union_set_gist(filter
, isl_union_set_copy(node_domain
));
4356 node_filter
= isl_union_set_copy(node_domain
);
4357 node_filter
= isl_union_set_subtract(node_filter
,
4358 isl_union_set_copy(filter
));
4359 node_filter
= isl_union_set_gist(node_filter
, node_domain
);
4360 empty1
= isl_union_set_is_empty(filter
);
4361 empty2
= isl_union_set_is_empty(node_filter
);
4362 if (empty1
< 0 || empty2
< 0)
4364 if (empty1
|| empty2
) {
4365 isl_union_set_free(filter
);
4366 isl_union_set_free(node_filter
);
4371 node
= isl_schedule_node_parent(node
);
4372 parent_filter
= isl_schedule_node_filter_get_filter(node
);
4373 node_filter
= isl_union_set_intersect(node_filter
,
4374 isl_union_set_copy(parent_filter
));
4375 filter
= isl_union_set_intersect(filter
, parent_filter
);
4378 node2
= isl_schedule_node_copy(node
);
4379 node
= isl_schedule_node_gist(node
, isl_union_set_copy(node_filter
));
4380 node2
= isl_schedule_node_gist(node2
, isl_union_set_copy(filter
));
4381 tree1
= isl_schedule_node_get_tree(node
);
4382 tree2
= isl_schedule_node_get_tree(node2
);
4383 tree1
= isl_schedule_tree_insert_filter(tree1
, node_filter
);
4384 tree2
= isl_schedule_tree_insert_filter(tree2
, filter
);
4385 isl_schedule_node_free(node2
);
4388 tree1
= isl_schedule_tree_sequence_pair(tree2
, tree1
);
4389 node
= graft_or_splice(node
, tree1
, 1);
4391 tree1
= isl_schedule_tree_sequence_pair(tree1
, tree2
);
4392 node
= graft_or_splice(node
, tree1
, 0);
4397 isl_schedule_node_free(node
);
4398 isl_union_set_free(filter
);
4399 isl_union_set_free(node_filter
);
4403 /* Split the domain elements that reach "node" into those that satisfy
4404 * "filter" and those that do not. Arrange for the first subset to be
4405 * executed before the second subset.
4406 * Return a pointer to the tree corresponding to the second subset,
4407 * except when this subset is empty in which case the original pointer
4410 __isl_give isl_schedule_node
*isl_schedule_node_order_before(
4411 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
4413 return isl_schedule_node_order_before_or_after(node
, filter
, 1);
4416 /* Split the domain elements that reach "node" into those that satisfy
4417 * "filter" and those that do not. Arrange for the first subset to be
4418 * executed after the second subset.
4419 * Return a pointer to the tree corresponding to the second subset,
4420 * except when this subset is empty in which case the original pointer
4423 __isl_give isl_schedule_node
*isl_schedule_node_order_after(
4424 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
4426 return isl_schedule_node_order_before_or_after(node
, filter
, 0);
4429 /* Reset the user pointer on all identifiers of parameters and tuples
4430 * in the schedule node "node".
4432 __isl_give isl_schedule_node
*isl_schedule_node_reset_user(
4433 __isl_take isl_schedule_node
*node
)
4435 isl_schedule_tree
*tree
;
4437 tree
= isl_schedule_node_get_tree(node
);
4438 tree
= isl_schedule_tree_reset_user(tree
);
4439 node
= isl_schedule_node_graft_tree(node
, tree
);
4444 /* Align the parameters of the schedule node "node" to those of "space".
4446 __isl_give isl_schedule_node
*isl_schedule_node_align_params(
4447 __isl_take isl_schedule_node
*node
, __isl_take isl_space
*space
)
4449 isl_schedule_tree
*tree
;
4451 tree
= isl_schedule_node_get_tree(node
);
4452 tree
= isl_schedule_tree_align_params(tree
, space
);
4453 node
= isl_schedule_node_graft_tree(node
, tree
);
4458 /* Compute the pullback of schedule node "node"
4459 * by the function represented by "upma".
4460 * In other words, plug in "upma" in the iteration domains
4461 * of schedule node "node".
4462 * We currently do not handle expansion nodes.
4464 * Note that this is only a helper function for
4465 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
4466 * this function should not be called on a single node without also
4467 * calling it on all the other nodes.
4469 __isl_give isl_schedule_node
*isl_schedule_node_pullback_union_pw_multi_aff(
4470 __isl_take isl_schedule_node
*node
,
4471 __isl_take isl_union_pw_multi_aff
*upma
)
4473 isl_schedule_tree
*tree
;
4475 tree
= isl_schedule_node_get_tree(node
);
4476 tree
= isl_schedule_tree_pullback_union_pw_multi_aff(tree
, upma
);
4477 node
= isl_schedule_node_graft_tree(node
, tree
);
4482 /* Internal data structure for isl_schedule_node_expand.
4483 * "tree" is the tree that needs to be plugged in in all the leaves.
4484 * "domain" is the set of domain elements in the original leaves
4485 * to which the tree applies.
4487 struct isl_schedule_expand_data
{
4488 isl_schedule_tree
*tree
;
4489 isl_union_set
*domain
;
4492 /* If "node" is a leaf, then plug in data->tree, simplifying it
4493 * within its new context.
4495 * If there are any domain elements at the leaf where the tree
4496 * should not be plugged in (i.e., there are elements not in data->domain)
4497 * then first extend the tree to only apply to the elements in data->domain
4498 * by constructing a set node that selects data->tree for elements
4499 * in data->domain and a leaf for the other elements.
4501 static __isl_give isl_schedule_node
*expand(__isl_take isl_schedule_node
*node
,
4504 struct isl_schedule_expand_data
*data
= user
;
4505 isl_schedule_tree
*tree
, *leaf
;
4506 isl_union_set
*domain
, *left
;
4509 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
4512 domain
= isl_schedule_node_get_domain(node
);
4513 tree
= isl_schedule_tree_copy(data
->tree
);
4515 left
= isl_union_set_copy(domain
);
4516 left
= isl_union_set_subtract(left
, isl_union_set_copy(data
->domain
));
4517 empty
= isl_union_set_is_empty(left
);
4518 if (empty
>= 0 && !empty
) {
4519 leaf
= isl_schedule_node_get_leaf(node
);
4520 leaf
= isl_schedule_tree_insert_filter(leaf
, left
);
4521 left
= isl_union_set_copy(data
->domain
);
4522 tree
= isl_schedule_tree_insert_filter(tree
, left
);
4523 tree
= isl_schedule_tree_set_pair(tree
, leaf
);
4526 node
= isl_schedule_node_free(node
);
4527 isl_union_set_free(left
);
4530 node
= isl_schedule_node_graft_tree(node
, tree
);
4531 node
= isl_schedule_node_gist(node
, domain
);
4536 /* Expand the tree rooted at "node" by extending all leaves
4537 * with an expansion node with as child "tree".
4538 * The expansion is determined by "contraction" and "domain".
4539 * That is, the elements of "domain" are contracted according
4540 * to "contraction". The expansion relation is then the inverse
4541 * of "contraction" with its range intersected with "domain".
4543 * Insert the appropriate expansion node on top of "tree" and
4544 * then plug in the result in all leaves of "node".
4546 __isl_give isl_schedule_node
*isl_schedule_node_expand(
4547 __isl_take isl_schedule_node
*node
,
4548 __isl_take isl_union_pw_multi_aff
*contraction
,
4549 __isl_take isl_union_set
*domain
,
4550 __isl_take isl_schedule_tree
*tree
)
4552 struct isl_schedule_expand_data data
;
4553 isl_union_map
*expansion
;
4554 isl_union_pw_multi_aff
*copy
;
4556 if (!node
|| !contraction
|| !tree
)
4557 node
= isl_schedule_node_free(node
);
4559 copy
= isl_union_pw_multi_aff_copy(contraction
);
4560 expansion
= isl_union_map_from_union_pw_multi_aff(copy
);
4561 expansion
= isl_union_map_reverse(expansion
);
4562 expansion
= isl_union_map_intersect_range(expansion
, domain
);
4563 data
.domain
= isl_union_map_domain(isl_union_map_copy(expansion
));
4565 tree
= isl_schedule_tree_insert_expansion(tree
, contraction
, expansion
);
4568 node
= isl_schedule_node_map_descendant_bottom_up(node
, &expand
, &data
);
4569 isl_union_set_free(data
.domain
);
4570 isl_schedule_tree_free(data
.tree
);
4574 /* Return the position of the subtree containing "node" among the children
4575 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
4576 * In particular, both nodes should point to the same schedule tree.
4578 * Return -1 on error.
4580 int isl_schedule_node_get_ancestor_child_position(
4581 __isl_keep isl_schedule_node
*node
,
4582 __isl_keep isl_schedule_node
*ancestor
)
4585 isl_schedule_tree
*tree
;
4587 if (!node
|| !ancestor
)
4590 if (node
->schedule
!= ancestor
->schedule
)
4591 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4592 "not a descendant", return -1);
4594 n1
= isl_schedule_node_get_tree_depth(ancestor
);
4595 n2
= isl_schedule_node_get_tree_depth(node
);
4598 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4599 "not a descendant", return -1);
4600 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, n1
);
4601 isl_schedule_tree_free(tree
);
4602 if (tree
!= ancestor
->tree
)
4603 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4604 "not a descendant", return -1);
4606 return node
->child_pos
[n1
];
4609 /* Given two nodes that point to the same schedule tree, return their
4610 * closest shared ancestor.
4612 * Since the two nodes point to the same schedule, they share at least
4613 * one ancestor, the root of the schedule. We move down from the root
4614 * to the first ancestor where the respective children have a different
4615 * child position. This is the requested ancestor.
4616 * If there is no ancestor where the children have a different position,
4617 * then one node is an ancestor of the other and then this node is
4618 * the requested ancestor.
4620 __isl_give isl_schedule_node
*isl_schedule_node_get_shared_ancestor(
4621 __isl_keep isl_schedule_node
*node1
,
4622 __isl_keep isl_schedule_node
*node2
)
4626 if (!node1
|| !node2
)
4628 if (node1
->schedule
!= node2
->schedule
)
4629 isl_die(isl_schedule_node_get_ctx(node1
), isl_error_invalid
,
4630 "not part of same schedule", return NULL
);
4631 n1
= isl_schedule_node_get_tree_depth(node1
);
4632 n2
= isl_schedule_node_get_tree_depth(node2
);
4634 return isl_schedule_node_get_shared_ancestor(node2
, node1
);
4636 return isl_schedule_node_copy(node1
);
4637 if (isl_schedule_node_is_equal(node1
, node2
))
4638 return isl_schedule_node_copy(node1
);
4640 for (i
= 0; i
< n1
; ++i
)
4641 if (node1
->child_pos
[i
] != node2
->child_pos
[i
])
4644 node1
= isl_schedule_node_copy(node1
);
4645 return isl_schedule_node_ancestor(node1
, n1
- i
);
4648 /* Print "node" to "p".
4650 __isl_give isl_printer
*isl_printer_print_schedule_node(
4651 __isl_take isl_printer
*p
, __isl_keep isl_schedule_node
*node
)
4654 return isl_printer_free(p
);
4655 return isl_printer_print_schedule_tree_mark(p
, node
->schedule
->root
,
4656 isl_schedule_tree_list_n_schedule_tree(node
->ancestors
),
4660 void isl_schedule_node_dump(__isl_keep isl_schedule_node
*node
)
4663 isl_printer
*printer
;
4668 ctx
= isl_schedule_node_get_ctx(node
);
4669 printer
= isl_printer_to_file(ctx
, stderr
);
4670 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
4671 printer
= isl_printer_print_schedule_node(printer
, node
);
4673 isl_printer_free(printer
);
4676 /* Return a string representation of "node".
4677 * Print the schedule node in block format as it would otherwise
4678 * look identical to the entire schedule.
4680 __isl_give
char *isl_schedule_node_to_str(__isl_keep isl_schedule_node
*node
)
4682 isl_printer
*printer
;
4688 printer
= isl_printer_to_str(isl_schedule_node_get_ctx(node
));
4689 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
4690 printer
= isl_printer_print_schedule_node(printer
, node
);
4691 s
= isl_printer_get_str(printer
);
4692 isl_printer_free(printer
);