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 * Since the node may point to a leaf of its schedule, which
221 * point to a field inside the schedule, we need to make sure
222 * we free the tree before freeing the schedule.
224 __isl_null isl_schedule_node
*isl_schedule_node_free(
225 __isl_take isl_schedule_node
*node
)
232 isl_schedule_tree_list_free(node
->ancestors
);
233 free(node
->child_pos
);
234 isl_schedule_tree_free(node
->tree
);
235 isl_schedule_free(node
->schedule
);
241 /* Do "node1" and "node2" point to the same position in the same
244 isl_bool
isl_schedule_node_is_equal(__isl_keep isl_schedule_node
*node1
,
245 __isl_keep isl_schedule_node
*node2
)
249 if (!node1
|| !node2
)
250 return isl_bool_error
;
252 return isl_bool_true
;
253 if (node1
->schedule
!= node2
->schedule
)
254 return isl_bool_false
;
256 n1
= isl_schedule_node_get_tree_depth(node1
);
257 n2
= isl_schedule_node_get_tree_depth(node2
);
259 return isl_bool_false
;
260 for (i
= 0; i
< n1
; ++i
)
261 if (node1
->child_pos
[i
] != node2
->child_pos
[i
])
262 return isl_bool_false
;
264 return isl_bool_true
;
267 /* Return the number of outer schedule dimensions of "node"
268 * in its schedule tree.
270 * Return -1 on error.
272 int isl_schedule_node_get_schedule_depth(__isl_keep isl_schedule_node
*node
)
280 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
281 for (i
= n
- 1; i
>= 0; --i
) {
282 isl_schedule_tree
*tree
;
284 tree
= isl_schedule_tree_list_get_schedule_tree(
288 if (tree
->type
== isl_schedule_node_band
)
289 depth
+= isl_schedule_tree_band_n_member(tree
);
290 isl_schedule_tree_free(tree
);
296 /* Internal data structure for
297 * isl_schedule_node_get_prefix_schedule_union_pw_multi_aff
299 * "initialized" is set if the filter field has been initialized.
300 * If "universe_domain" is not set, then the collected filter is intersected
301 * with the the domain of the root domain node.
302 * "universe_filter" is set if we are only collecting the universes of filters
303 * "collect_prefix" is set if we are collecting prefixes.
304 * "filter" collects all outer filters and is NULL until "initialized" is set.
305 * "prefix" collects all outer band partial schedules (if "collect_prefix"
306 * is set). If it is used, then it is initialized by the caller
307 * of collect_filter_prefix to a zero-dimensional function.
309 struct isl_schedule_node_get_filter_prefix_data
{
314 isl_union_set
*filter
;
315 isl_multi_union_pw_aff
*prefix
;
318 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list
*list
,
319 int n
, struct isl_schedule_node_get_filter_prefix_data
*data
);
321 /* Update the filter and prefix information in "data" based on the first "n"
322 * elements in "list" and the expansion tree root "tree".
324 * We first collect the information from the elements in "list",
325 * initializing the filter based on the domain of the expansion.
326 * Then we map the results to the expanded space and combined them
327 * with the results already in "data".
329 static int collect_filter_prefix_expansion(__isl_take isl_schedule_tree
*tree
,
330 __isl_keep isl_schedule_tree_list
*list
, int n
,
331 struct isl_schedule_node_get_filter_prefix_data
*data
)
333 struct isl_schedule_node_get_filter_prefix_data contracted
;
334 isl_union_pw_multi_aff
*c
;
335 isl_union_map
*exp
, *universe
;
336 isl_union_set
*filter
;
338 c
= isl_schedule_tree_expansion_get_contraction(tree
);
339 exp
= isl_schedule_tree_expansion_get_expansion(tree
);
341 contracted
.initialized
= 1;
342 contracted
.universe_domain
= data
->universe_domain
;
343 contracted
.universe_filter
= data
->universe_filter
;
344 contracted
.collect_prefix
= data
->collect_prefix
;
345 universe
= isl_union_map_universe(isl_union_map_copy(exp
));
346 filter
= isl_union_map_domain(universe
);
347 if (data
->collect_prefix
) {
348 isl_space
*space
= isl_union_set_get_space(filter
);
349 space
= isl_space_set_from_params(space
);
350 contracted
.prefix
= isl_multi_union_pw_aff_zero(space
);
352 contracted
.filter
= filter
;
354 if (collect_filter_prefix(list
, n
, &contracted
) < 0)
355 contracted
.filter
= isl_union_set_free(contracted
.filter
);
356 if (data
->collect_prefix
) {
357 isl_multi_union_pw_aff
*prefix
;
359 prefix
= contracted
.prefix
;
361 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
362 isl_union_pw_multi_aff_copy(c
));
363 data
->prefix
= isl_multi_union_pw_aff_flat_range_product(
364 prefix
, data
->prefix
);
366 filter
= contracted
.filter
;
367 if (data
->universe_domain
)
368 filter
= isl_union_set_preimage_union_pw_multi_aff(filter
,
369 isl_union_pw_multi_aff_copy(c
));
371 filter
= isl_union_set_apply(filter
, isl_union_map_copy(exp
));
372 if (!data
->initialized
)
373 data
->filter
= filter
;
375 data
->filter
= isl_union_set_intersect(filter
, data
->filter
);
376 data
->initialized
= 1;
378 isl_union_pw_multi_aff_free(c
);
379 isl_union_map_free(exp
);
380 isl_schedule_tree_free(tree
);
385 /* Update the filter information in "data" based on the first "n"
386 * elements in "list" and the extension tree root "tree", in case
387 * data->universe_domain is set and data->collect_prefix is not.
389 * We collect the universe domain of the elements in "list" and
390 * add it to the universe range of the extension (intersected
391 * with the already collected filter, if any).
393 static int collect_universe_domain_extension(__isl_take isl_schedule_tree
*tree
,
394 __isl_keep isl_schedule_tree_list
*list
, int n
,
395 struct isl_schedule_node_get_filter_prefix_data
*data
)
397 struct isl_schedule_node_get_filter_prefix_data data_outer
;
398 isl_union_map
*extension
;
399 isl_union_set
*filter
;
401 data_outer
.initialized
= 0;
402 data_outer
.universe_domain
= 1;
403 data_outer
.universe_filter
= data
->universe_filter
;
404 data_outer
.collect_prefix
= 0;
405 data_outer
.filter
= NULL
;
406 data_outer
.prefix
= NULL
;
408 if (collect_filter_prefix(list
, n
, &data_outer
) < 0)
409 data_outer
.filter
= isl_union_set_free(data_outer
.filter
);
411 extension
= isl_schedule_tree_extension_get_extension(tree
);
412 extension
= isl_union_map_universe(extension
);
413 filter
= isl_union_map_range(extension
);
414 if (data_outer
.initialized
)
415 filter
= isl_union_set_union(filter
, data_outer
.filter
);
416 if (data
->initialized
)
417 filter
= isl_union_set_intersect(filter
, data
->filter
);
419 data
->filter
= filter
;
421 isl_schedule_tree_free(tree
);
426 /* Update "data" based on the tree node "tree" in case "data" has
427 * not been initialized yet.
429 * Return 0 on success and -1 on error.
431 * If "tree" is a filter, then we set data->filter to this filter
433 * If "tree" is a domain, then this means we have reached the root
434 * of the schedule tree without being able to extract any information.
435 * We therefore initialize data->filter to the universe of the domain,
436 * or the domain itself if data->universe_domain is not set.
437 * If "tree" is a band with at least one member, then we set data->filter
438 * to the universe of the schedule domain and replace the zero-dimensional
439 * data->prefix by the band schedule (if data->collect_prefix is set).
441 static int collect_filter_prefix_init(__isl_keep isl_schedule_tree
*tree
,
442 struct isl_schedule_node_get_filter_prefix_data
*data
)
444 enum isl_schedule_node_type type
;
445 isl_multi_union_pw_aff
*mupa
;
446 isl_union_set
*filter
;
448 type
= isl_schedule_tree_get_type(tree
);
450 case isl_schedule_node_error
:
452 case isl_schedule_node_expansion
:
453 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
454 "should be handled by caller", return -1);
455 case isl_schedule_node_extension
:
456 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
457 "cannot handle extension nodes", return -1);
458 case isl_schedule_node_context
:
459 case isl_schedule_node_leaf
:
460 case isl_schedule_node_guard
:
461 case isl_schedule_node_mark
:
462 case isl_schedule_node_sequence
:
463 case isl_schedule_node_set
:
465 case isl_schedule_node_domain
:
466 filter
= isl_schedule_tree_domain_get_domain(tree
);
467 if (data
->universe_domain
)
468 filter
= isl_union_set_universe(filter
);
469 data
->filter
= filter
;
471 case isl_schedule_node_band
:
472 if (isl_schedule_tree_band_n_member(tree
) == 0)
474 mupa
= isl_schedule_tree_band_get_partial_schedule(tree
);
475 if (data
->collect_prefix
) {
476 isl_multi_union_pw_aff_free(data
->prefix
);
477 mupa
= isl_multi_union_pw_aff_reset_tuple_id(mupa
,
479 data
->prefix
= isl_multi_union_pw_aff_copy(mupa
);
481 filter
= isl_multi_union_pw_aff_domain(mupa
);
482 filter
= isl_union_set_universe(filter
);
483 data
->filter
= filter
;
485 case isl_schedule_node_filter
:
486 filter
= isl_schedule_tree_filter_get_filter(tree
);
487 if (data
->universe_filter
)
488 filter
= isl_union_set_universe(filter
);
489 data
->filter
= filter
;
493 if ((data
->collect_prefix
&& !data
->prefix
) || !data
->filter
)
496 data
->initialized
= 1;
501 /* Update "data" based on the tree node "tree" in case "data" has
502 * already been initialized.
504 * Return 0 on success and -1 on error.
506 * If "tree" is a domain and data->universe_domain is not set, then
507 * intersect data->filter with the domain.
508 * If "tree" is a filter, then we intersect data->filter with this filter
510 * If "tree" is a band with at least one member and data->collect_prefix
511 * is set, then we extend data->prefix with the band schedule.
512 * If "tree" is an extension, then we make sure that we are not collecting
513 * information on any extended domain elements.
515 static int collect_filter_prefix_update(__isl_keep isl_schedule_tree
*tree
,
516 struct isl_schedule_node_get_filter_prefix_data
*data
)
518 enum isl_schedule_node_type type
;
519 isl_multi_union_pw_aff
*mupa
;
520 isl_union_set
*filter
;
521 isl_union_map
*extension
;
524 type
= isl_schedule_tree_get_type(tree
);
526 case isl_schedule_node_error
:
528 case isl_schedule_node_expansion
:
529 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
530 "should be handled by caller", return -1);
531 case isl_schedule_node_extension
:
532 extension
= isl_schedule_tree_extension_get_extension(tree
);
533 extension
= isl_union_map_intersect_range(extension
,
534 isl_union_set_copy(data
->filter
));
535 empty
= isl_union_map_is_empty(extension
);
536 isl_union_map_free(extension
);
541 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_invalid
,
542 "cannot handle extension nodes", return -1);
543 case isl_schedule_node_context
:
544 case isl_schedule_node_leaf
:
545 case isl_schedule_node_guard
:
546 case isl_schedule_node_mark
:
547 case isl_schedule_node_sequence
:
548 case isl_schedule_node_set
:
550 case isl_schedule_node_domain
:
551 if (data
->universe_domain
)
553 filter
= isl_schedule_tree_domain_get_domain(tree
);
554 data
->filter
= isl_union_set_intersect(data
->filter
, filter
);
556 case isl_schedule_node_band
:
557 if (isl_schedule_tree_band_n_member(tree
) == 0)
559 if (!data
->collect_prefix
)
561 mupa
= isl_schedule_tree_band_get_partial_schedule(tree
);
562 data
->prefix
= isl_multi_union_pw_aff_flat_range_product(mupa
,
567 case isl_schedule_node_filter
:
568 filter
= isl_schedule_tree_filter_get_filter(tree
);
569 if (data
->universe_filter
)
570 filter
= isl_union_set_universe(filter
);
571 data
->filter
= isl_union_set_intersect(data
->filter
, filter
);
580 /* Collect filter and/or prefix information from the first "n"
581 * elements in "list" (which represent the ancestors of a node).
582 * Store the results in "data".
584 * Extension nodes are only supported if they do not affect the outcome,
585 * i.e., if we are collecting information on non-extended domain elements,
586 * or if we are collecting the universe domain (without prefix).
588 * Return 0 on success and -1 on error.
590 * We traverse the list from innermost ancestor (last element)
591 * to outermost ancestor (first element), calling collect_filter_prefix_init
592 * on each node as long as we have not been able to extract any information
593 * yet and collect_filter_prefix_update afterwards.
594 * If we come across an expansion node, then we interrupt the traversal
595 * and call collect_filter_prefix_expansion to restart the traversal
596 * over the remaining ancestors and to combine the results with those
597 * that have already been collected.
598 * If we come across an extension node and we are only computing
599 * the universe domain, then we interrupt the traversal and call
600 * collect_universe_domain_extension to restart the traversal
601 * over the remaining ancestors and to combine the results with those
602 * that have already been collected.
603 * On successful return, data->initialized will be set since the outermost
604 * ancestor is a domain node, which always results in an initialization.
606 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list
*list
,
607 int n
, struct isl_schedule_node_get_filter_prefix_data
*data
)
614 for (i
= n
- 1; i
>= 0; --i
) {
615 isl_schedule_tree
*tree
;
616 enum isl_schedule_node_type type
;
619 tree
= isl_schedule_tree_list_get_schedule_tree(list
, i
);
622 type
= isl_schedule_tree_get_type(tree
);
623 if (type
== isl_schedule_node_expansion
)
624 return collect_filter_prefix_expansion(tree
, list
, i
,
626 if (type
== isl_schedule_node_extension
&&
627 data
->universe_domain
&& !data
->collect_prefix
)
628 return collect_universe_domain_extension(tree
, list
, i
,
630 if (!data
->initialized
)
631 r
= collect_filter_prefix_init(tree
, data
);
633 r
= collect_filter_prefix_update(tree
, data
);
634 isl_schedule_tree_free(tree
);
642 /* Return the concatenation of the partial schedules of all outer band
643 * nodes of "node" interesected with all outer filters
644 * as an isl_multi_union_pw_aff.
645 * None of the ancestors of "node" may be an extension node, unless
646 * there is also a filter ancestor that filters out all the extended
649 * If "node" is pointing at the root of the schedule tree, then
650 * there are no domain elements reaching the current node, so
651 * we return an empty result.
653 * We collect all the filters and partial schedules in collect_filter_prefix
654 * and intersect the domain of the combined schedule with the combined filter.
656 __isl_give isl_multi_union_pw_aff
*
657 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(
658 __isl_keep isl_schedule_node
*node
)
662 struct isl_schedule_node_get_filter_prefix_data data
;
667 space
= isl_schedule_get_space(node
->schedule
);
668 space
= isl_space_set_from_params(space
);
669 if (node
->tree
== node
->schedule
->root
)
670 return isl_multi_union_pw_aff_zero(space
);
672 data
.initialized
= 0;
673 data
.universe_domain
= 1;
674 data
.universe_filter
= 0;
675 data
.collect_prefix
= 1;
677 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
679 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
680 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
681 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
683 data
.prefix
= isl_multi_union_pw_aff_intersect_domain(data
.prefix
,
689 /* Return the concatenation of the partial schedules of all outer band
690 * nodes of "node" interesected with all outer filters
691 * as an isl_union_pw_multi_aff.
692 * None of the ancestors of "node" may be an extension node, unless
693 * there is also a filter ancestor that filters out all the extended
696 * If "node" is pointing at the root of the schedule tree, then
697 * there are no domain elements reaching the current node, so
698 * we return an empty result.
700 * We collect all the filters and partial schedules in collect_filter_prefix.
701 * The partial schedules are collected as an isl_multi_union_pw_aff.
702 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
703 * contain any domain information, so we construct the isl_union_pw_multi_aff
704 * result as a zero-dimensional function on the collected filter.
705 * Otherwise, we convert the isl_multi_union_pw_aff to
706 * an isl_multi_union_pw_aff and intersect the domain with the filter.
708 __isl_give isl_union_pw_multi_aff
*
709 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
710 __isl_keep isl_schedule_node
*node
)
714 isl_union_pw_multi_aff
*prefix
;
715 struct isl_schedule_node_get_filter_prefix_data data
;
720 space
= isl_schedule_get_space(node
->schedule
);
721 if (node
->tree
== node
->schedule
->root
)
722 return isl_union_pw_multi_aff_empty(space
);
724 space
= isl_space_set_from_params(space
);
725 data
.initialized
= 0;
726 data
.universe_domain
= 1;
727 data
.universe_filter
= 0;
728 data
.collect_prefix
= 1;
730 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
732 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
733 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
734 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
737 isl_multi_union_pw_aff_dim(data
.prefix
, isl_dim_set
) == 0) {
738 isl_multi_union_pw_aff_free(data
.prefix
);
739 prefix
= isl_union_pw_multi_aff_from_domain(data
.filter
);
742 isl_union_pw_multi_aff_from_multi_union_pw_aff(data
.prefix
);
743 prefix
= isl_union_pw_multi_aff_intersect_domain(prefix
,
750 /* Return the concatenation of the partial schedules of all outer band
751 * nodes of "node" interesected with all outer filters
752 * as an isl_union_map.
754 __isl_give isl_union_map
*isl_schedule_node_get_prefix_schedule_union_map(
755 __isl_keep isl_schedule_node
*node
)
757 isl_union_pw_multi_aff
*upma
;
759 upma
= isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
760 return isl_union_map_from_union_pw_multi_aff(upma
);
763 /* Return the concatenation of the partial schedules of all outer band
764 * nodes of "node" intersected with all outer domain constraints.
765 * None of the ancestors of "node" may be an extension node, unless
766 * there is also a filter ancestor that filters out all the extended
769 * Essentially, this function intersects the domain of the output
770 * of isl_schedule_node_get_prefix_schedule_union_map with the output
771 * of isl_schedule_node_get_domain, except that it only traverses
772 * the ancestors of "node" once.
774 __isl_give isl_union_map
*isl_schedule_node_get_prefix_schedule_relation(
775 __isl_keep isl_schedule_node
*node
)
779 isl_union_map
*prefix
;
780 struct isl_schedule_node_get_filter_prefix_data data
;
785 space
= isl_schedule_get_space(node
->schedule
);
786 if (node
->tree
== node
->schedule
->root
)
787 return isl_union_map_empty(space
);
789 space
= isl_space_set_from_params(space
);
790 data
.initialized
= 0;
791 data
.universe_domain
= 0;
792 data
.universe_filter
= 0;
793 data
.collect_prefix
= 1;
795 data
.prefix
= isl_multi_union_pw_aff_zero(space
);
797 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
798 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
799 data
.prefix
= isl_multi_union_pw_aff_free(data
.prefix
);
802 isl_multi_union_pw_aff_dim(data
.prefix
, isl_dim_set
) == 0) {
803 isl_multi_union_pw_aff_free(data
.prefix
);
804 prefix
= isl_union_map_from_domain(data
.filter
);
806 prefix
= isl_union_map_from_multi_union_pw_aff(data
.prefix
);
807 prefix
= isl_union_map_intersect_domain(prefix
, data
.filter
);
813 /* Return the domain elements that reach "node".
815 * If "node" is pointing at the root of the schedule tree, then
816 * there are no domain elements reaching the current node, so
817 * we return an empty result.
818 * None of the ancestors of "node" may be an extension node, unless
819 * there is also a filter ancestor that filters out all the extended
822 * Otherwise, we collect all filters reaching the node,
823 * intersected with the root domain in collect_filter_prefix.
825 __isl_give isl_union_set
*isl_schedule_node_get_domain(
826 __isl_keep isl_schedule_node
*node
)
829 struct isl_schedule_node_get_filter_prefix_data data
;
834 if (node
->tree
== node
->schedule
->root
) {
837 space
= isl_schedule_get_space(node
->schedule
);
838 return isl_union_set_empty(space
);
841 data
.initialized
= 0;
842 data
.universe_domain
= 0;
843 data
.universe_filter
= 0;
844 data
.collect_prefix
= 0;
848 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
849 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
850 data
.filter
= isl_union_set_free(data
.filter
);
855 /* Return the union of universe sets of the domain elements that reach "node".
857 * If "node" is pointing at the root of the schedule tree, then
858 * there are no domain elements reaching the current node, so
859 * we return an empty result.
861 * Otherwise, we collect the universes of all filters reaching the node
862 * in collect_filter_prefix.
864 __isl_give isl_union_set
*isl_schedule_node_get_universe_domain(
865 __isl_keep isl_schedule_node
*node
)
868 struct isl_schedule_node_get_filter_prefix_data data
;
873 if (node
->tree
== node
->schedule
->root
) {
876 space
= isl_schedule_get_space(node
->schedule
);
877 return isl_union_set_empty(space
);
880 data
.initialized
= 0;
881 data
.universe_domain
= 1;
882 data
.universe_filter
= 1;
883 data
.collect_prefix
= 0;
887 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
888 if (collect_filter_prefix(node
->ancestors
, n
, &data
) < 0)
889 data
.filter
= isl_union_set_free(data
.filter
);
894 /* Return the subtree schedule of "node".
896 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
897 * trees that do not contain any schedule information, we first
898 * move down to the first relevant descendant and handle leaves ourselves.
900 * If the subtree rooted at "node" contains any expansion nodes, then
901 * the returned subtree schedule is formulated in terms of the expanded
903 * The subtree is not allowed to contain any extension nodes.
905 __isl_give isl_union_map
*isl_schedule_node_get_subtree_schedule_union_map(
906 __isl_keep isl_schedule_node
*node
)
908 isl_schedule_tree
*tree
, *leaf
;
911 tree
= isl_schedule_node_get_tree(node
);
912 leaf
= isl_schedule_node_peek_leaf(node
);
913 tree
= isl_schedule_tree_first_schedule_descendant(tree
, leaf
);
917 isl_union_set
*domain
;
918 domain
= isl_schedule_node_get_universe_domain(node
);
919 isl_schedule_tree_free(tree
);
920 return isl_union_map_from_domain(domain
);
923 umap
= isl_schedule_tree_get_subtree_schedule_union_map(tree
);
924 isl_schedule_tree_free(tree
);
928 /* Return the number of ancestors of "node" in its schedule tree.
930 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node
*node
)
934 return isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
937 /* Does "node" have a parent?
939 * That is, does it point to any node of the schedule other than the root?
941 isl_bool
isl_schedule_node_has_parent(__isl_keep isl_schedule_node
*node
)
944 return isl_bool_error
;
945 if (!node
->ancestors
)
946 return isl_bool_error
;
948 return isl_schedule_tree_list_n_schedule_tree(node
->ancestors
) != 0;
951 /* Return the position of "node" among the children of its parent.
953 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node
*node
)
960 has_parent
= isl_schedule_node_has_parent(node
);
964 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
965 "node has no parent", return -1);
967 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
968 return node
->child_pos
[n
- 1];
971 /* Does the parent (if any) of "node" have any children with a smaller child
972 * position than this one?
974 isl_bool
isl_schedule_node_has_previous_sibling(
975 __isl_keep isl_schedule_node
*node
)
981 return isl_bool_error
;
982 has_parent
= isl_schedule_node_has_parent(node
);
983 if (has_parent
< 0 || !has_parent
)
986 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
988 return node
->child_pos
[n
- 1] > 0;
991 /* Does the parent (if any) of "node" have any children with a greater child
992 * position than this one?
994 isl_bool
isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node
*node
)
998 isl_schedule_tree
*tree
;
1001 return isl_bool_error
;
1002 has_parent
= isl_schedule_node_has_parent(node
);
1003 if (has_parent
< 0 || !has_parent
)
1006 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1007 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, n
- 1);
1009 return isl_bool_error
;
1010 n_child
= isl_schedule_tree_list_n_schedule_tree(tree
->children
);
1011 isl_schedule_tree_free(tree
);
1013 return node
->child_pos
[n
- 1] + 1 < n_child
;
1016 /* Does "node" have any children?
1018 * Any node other than the leaf nodes is considered to have at least
1019 * one child, even if the corresponding isl_schedule_tree does not
1020 * have any children.
1022 isl_bool
isl_schedule_node_has_children(__isl_keep isl_schedule_node
*node
)
1025 return isl_bool_error
;
1026 return !isl_schedule_tree_is_leaf(node
->tree
);
1029 /* Return the number of children of "node"?
1031 * Any node other than the leaf nodes is considered to have at least
1032 * one child, even if the corresponding isl_schedule_tree does not
1033 * have any children. That is, the number of children of "node" is
1034 * only zero if its tree is the explicit empty tree. Otherwise,
1035 * if the isl_schedule_tree has any children, then it is equal
1036 * to the number of children of "node". If it has zero children,
1037 * then "node" still has a leaf node as child.
1039 int isl_schedule_node_n_children(__isl_keep isl_schedule_node
*node
)
1046 if (isl_schedule_tree_is_leaf(node
->tree
))
1049 n
= isl_schedule_tree_n_children(node
->tree
);
1056 /* Move the "node" pointer to the ancestor of the given generation
1057 * of the node it currently points to, where generation 0 is the node
1058 * itself and generation 1 is its parent.
1060 __isl_give isl_schedule_node
*isl_schedule_node_ancestor(
1061 __isl_take isl_schedule_node
*node
, int generation
)
1064 isl_schedule_tree
*tree
;
1068 if (generation
== 0)
1070 n
= isl_schedule_node_get_tree_depth(node
);
1072 return isl_schedule_node_free(node
);
1073 if (generation
< 0 || generation
> n
)
1074 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1075 "generation out of bounds",
1076 return isl_schedule_node_free(node
));
1077 node
= isl_schedule_node_cow(node
);
1081 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1083 isl_schedule_tree_free(node
->tree
);
1085 node
->ancestors
= isl_schedule_tree_list_drop(node
->ancestors
,
1086 n
- generation
, generation
);
1087 if (!node
->ancestors
|| !node
->tree
)
1088 return isl_schedule_node_free(node
);
1093 /* Move the "node" pointer to the parent of the node it currently points to.
1095 __isl_give isl_schedule_node
*isl_schedule_node_parent(
1096 __isl_take isl_schedule_node
*node
)
1100 if (!isl_schedule_node_has_parent(node
))
1101 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1102 "node has no parent",
1103 return isl_schedule_node_free(node
));
1104 return isl_schedule_node_ancestor(node
, 1);
1107 /* Move the "node" pointer to the root of its schedule tree.
1109 __isl_give isl_schedule_node
*isl_schedule_node_root(
1110 __isl_take isl_schedule_node
*node
)
1116 n
= isl_schedule_node_get_tree_depth(node
);
1118 return isl_schedule_node_free(node
);
1119 return isl_schedule_node_ancestor(node
, n
);
1122 /* Move the "node" pointer to the child at position "pos" of the node
1123 * it currently points to.
1125 __isl_give isl_schedule_node
*isl_schedule_node_child(
1126 __isl_take isl_schedule_node
*node
, int pos
)
1130 isl_schedule_tree
*tree
;
1133 node
= isl_schedule_node_cow(node
);
1136 if (!isl_schedule_node_has_children(node
))
1137 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1138 "node has no children",
1139 return isl_schedule_node_free(node
));
1141 ctx
= isl_schedule_node_get_ctx(node
);
1142 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1143 child_pos
= isl_realloc_array(ctx
, node
->child_pos
, int, n
+ 1);
1145 return isl_schedule_node_free(node
);
1146 node
->child_pos
= child_pos
;
1147 node
->child_pos
[n
] = pos
;
1149 node
->ancestors
= isl_schedule_tree_list_add(node
->ancestors
,
1150 isl_schedule_tree_copy(node
->tree
));
1152 if (isl_schedule_tree_has_children(tree
))
1153 tree
= isl_schedule_tree_get_child(tree
, pos
);
1155 tree
= isl_schedule_node_get_leaf(node
);
1156 isl_schedule_tree_free(node
->tree
);
1159 if (!node
->tree
|| !node
->ancestors
)
1160 return isl_schedule_node_free(node
);
1165 /* Move the "node" pointer to the first child of the node
1166 * it currently points to.
1168 __isl_give isl_schedule_node
*isl_schedule_node_first_child(
1169 __isl_take isl_schedule_node
*node
)
1171 return isl_schedule_node_child(node
, 0);
1174 /* Move the "node" pointer to the child of this node's parent in
1175 * the previous child position.
1177 __isl_give isl_schedule_node
*isl_schedule_node_previous_sibling(
1178 __isl_take isl_schedule_node
*node
)
1181 isl_schedule_tree
*parent
, *tree
;
1183 node
= isl_schedule_node_cow(node
);
1186 if (!isl_schedule_node_has_previous_sibling(node
))
1187 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1188 "node has no previous sibling",
1189 return isl_schedule_node_free(node
));
1191 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1192 parent
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1195 return isl_schedule_node_free(node
);
1196 node
->child_pos
[n
- 1]--;
1197 tree
= isl_schedule_tree_list_get_schedule_tree(parent
->children
,
1198 node
->child_pos
[n
- 1]);
1199 isl_schedule_tree_free(parent
);
1201 return isl_schedule_node_free(node
);
1202 isl_schedule_tree_free(node
->tree
);
1208 /* Move the "node" pointer to the child of this node's parent in
1209 * the next child position.
1211 __isl_give isl_schedule_node
*isl_schedule_node_next_sibling(
1212 __isl_take isl_schedule_node
*node
)
1215 isl_schedule_tree
*parent
, *tree
;
1217 node
= isl_schedule_node_cow(node
);
1220 if (!isl_schedule_node_has_next_sibling(node
))
1221 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1222 "node has no next sibling",
1223 return isl_schedule_node_free(node
));
1225 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
1226 parent
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
1229 return isl_schedule_node_free(node
);
1230 node
->child_pos
[n
- 1]++;
1231 tree
= isl_schedule_tree_list_get_schedule_tree(parent
->children
,
1232 node
->child_pos
[n
- 1]);
1233 isl_schedule_tree_free(parent
);
1235 return isl_schedule_node_free(node
);
1236 isl_schedule_tree_free(node
->tree
);
1242 /* Return a copy to the child at position "pos" of "node".
1244 __isl_give isl_schedule_node
*isl_schedule_node_get_child(
1245 __isl_keep isl_schedule_node
*node
, int pos
)
1247 return isl_schedule_node_child(isl_schedule_node_copy(node
), pos
);
1250 /* Traverse the descendant of "node" in depth-first order, including
1251 * "node" itself. Call "enter" whenever a node is entered and "leave"
1252 * whenever a node is left. The callback "enter" is responsible
1253 * for moving to the deepest initial subtree of its argument that
1254 * should be traversed.
1256 static __isl_give isl_schedule_node
*traverse(
1257 __isl_take isl_schedule_node
*node
,
1258 __isl_give isl_schedule_node
*(*enter
)(
1259 __isl_take isl_schedule_node
*node
, void *user
),
1260 __isl_give isl_schedule_node
*(*leave
)(
1261 __isl_take isl_schedule_node
*node
, void *user
),
1269 depth
= isl_schedule_node_get_tree_depth(node
);
1271 node
= enter(node
, user
);
1272 node
= leave(node
, user
);
1273 while (node
&& isl_schedule_node_get_tree_depth(node
) > depth
&&
1274 !isl_schedule_node_has_next_sibling(node
)) {
1275 node
= isl_schedule_node_parent(node
);
1276 node
= leave(node
, user
);
1278 if (node
&& isl_schedule_node_get_tree_depth(node
) > depth
)
1279 node
= isl_schedule_node_next_sibling(node
);
1280 } while (node
&& isl_schedule_node_get_tree_depth(node
) > depth
);
1285 /* Internal data structure for isl_schedule_node_foreach_descendant_top_down.
1287 * "fn" is the user-specified callback function.
1288 * "user" is the user-specified argument for the callback.
1290 struct isl_schedule_node_preorder_data
{
1291 isl_bool (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
);
1295 /* Callback for "traverse" to enter a node and to move
1296 * to the deepest initial subtree that should be traversed
1297 * for use in a preorder visit.
1299 * If the user callback returns a negative value, then we abort
1300 * the traversal. If this callback returns zero, then we skip
1301 * the subtree rooted at the current node. Otherwise, we move
1302 * down to the first child and repeat the process until a leaf
1305 static __isl_give isl_schedule_node
*preorder_enter(
1306 __isl_take isl_schedule_node
*node
, void *user
)
1308 struct isl_schedule_node_preorder_data
*data
= user
;
1316 r
= data
->fn(node
, data
->user
);
1318 return isl_schedule_node_free(node
);
1319 if (r
== isl_bool_false
)
1321 } while (isl_schedule_node_has_children(node
) &&
1322 (node
= isl_schedule_node_first_child(node
)) != NULL
);
1327 /* Callback for "traverse" to leave a node
1328 * for use in a preorder visit.
1329 * Since we already visited the node when we entered it,
1330 * we do not need to do anything here.
1332 static __isl_give isl_schedule_node
*preorder_leave(
1333 __isl_take isl_schedule_node
*node
, void *user
)
1338 /* Traverse the descendants of "node" (including the node itself)
1339 * in depth first preorder.
1341 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1342 * If "fn" returns 0 on any of the nodes, then the subtree rooted
1343 * at that node is skipped.
1345 * Return 0 on success and -1 on failure.
1347 isl_stat
isl_schedule_node_foreach_descendant_top_down(
1348 __isl_keep isl_schedule_node
*node
,
1349 isl_bool (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
),
1352 struct isl_schedule_node_preorder_data data
= { fn
, user
};
1354 node
= isl_schedule_node_copy(node
);
1355 node
= traverse(node
, &preorder_enter
, &preorder_leave
, &data
);
1356 isl_schedule_node_free(node
);
1358 return node
? isl_stat_ok
: isl_stat_error
;
1361 /* Internal data structure for isl_schedule_node_map_descendant_bottom_up.
1363 * "fn" is the user-specified callback function.
1364 * "user" is the user-specified argument for the callback.
1366 struct isl_schedule_node_postorder_data
{
1367 __isl_give isl_schedule_node
*(*fn
)(__isl_take isl_schedule_node
*node
,
1372 /* Callback for "traverse" to enter a node and to move
1373 * to the deepest initial subtree that should be traversed
1374 * for use in a postorder visit.
1376 * Since we are performing a postorder visit, we only need
1377 * to move to the deepest initial leaf here.
1379 static __isl_give isl_schedule_node
*postorder_enter(
1380 __isl_take isl_schedule_node
*node
, void *user
)
1382 while (node
&& isl_schedule_node_has_children(node
))
1383 node
= isl_schedule_node_first_child(node
);
1388 /* Callback for "traverse" to leave a node
1389 * for use in a postorder visit.
1391 * Since we are performing a postorder visit, we need
1392 * to call the user callback here.
1394 static __isl_give isl_schedule_node
*postorder_leave(
1395 __isl_take isl_schedule_node
*node
, void *user
)
1397 struct isl_schedule_node_postorder_data
*data
= user
;
1399 return data
->fn(node
, data
->user
);
1402 /* Traverse the descendants of "node" (including the node itself)
1403 * in depth first postorder, allowing the user to modify the visited node.
1404 * The traversal continues from the node returned by the callback function.
1405 * It is the responsibility of the user to ensure that this does not
1406 * lead to an infinite loop. It is safest to always return a pointer
1407 * to the same position (same ancestors and child positions) as the input node.
1409 __isl_give isl_schedule_node
*isl_schedule_node_map_descendant_bottom_up(
1410 __isl_take isl_schedule_node
*node
,
1411 __isl_give isl_schedule_node
*(*fn
)(__isl_take isl_schedule_node
*node
,
1412 void *user
), void *user
)
1414 struct isl_schedule_node_postorder_data data
= { fn
, user
};
1416 return traverse(node
, &postorder_enter
, &postorder_leave
, &data
);
1419 /* Traverse the ancestors of "node" from the root down to and including
1420 * the parent of "node", calling "fn" on each of them.
1422 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1424 * Return 0 on success and -1 on failure.
1426 isl_stat
isl_schedule_node_foreach_ancestor_top_down(
1427 __isl_keep isl_schedule_node
*node
,
1428 isl_stat (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
),
1434 return isl_stat_error
;
1436 n
= isl_schedule_node_get_tree_depth(node
);
1437 for (i
= 0; i
< n
; ++i
) {
1438 isl_schedule_node
*ancestor
;
1441 ancestor
= isl_schedule_node_copy(node
);
1442 ancestor
= isl_schedule_node_ancestor(ancestor
, n
- i
);
1443 r
= fn(ancestor
, user
);
1444 isl_schedule_node_free(ancestor
);
1446 return isl_stat_error
;
1452 /* Is any node in the subtree rooted at "node" anchored?
1453 * That is, do any of these nodes reference the outer band nodes?
1455 isl_bool
isl_schedule_node_is_subtree_anchored(
1456 __isl_keep isl_schedule_node
*node
)
1459 return isl_bool_error
;
1460 return isl_schedule_tree_is_subtree_anchored(node
->tree
);
1463 /* Return the number of members in the given band node.
1465 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node
*node
)
1467 return node
? isl_schedule_tree_band_n_member(node
->tree
) : 0;
1470 /* Is the band member at position "pos" of the band node "node"
1471 * marked coincident?
1473 isl_bool
isl_schedule_node_band_member_get_coincident(
1474 __isl_keep isl_schedule_node
*node
, int pos
)
1477 return isl_bool_error
;
1478 return isl_schedule_tree_band_member_get_coincident(node
->tree
, pos
);
1481 /* Mark the band member at position "pos" the band node "node"
1482 * as being coincident or not according to "coincident".
1484 __isl_give isl_schedule_node
*isl_schedule_node_band_member_set_coincident(
1485 __isl_take isl_schedule_node
*node
, int pos
, int coincident
)
1488 isl_schedule_tree
*tree
;
1492 c
= isl_schedule_node_band_member_get_coincident(node
, pos
);
1493 if (c
== coincident
)
1496 tree
= isl_schedule_tree_copy(node
->tree
);
1497 tree
= isl_schedule_tree_band_member_set_coincident(tree
, pos
,
1499 node
= isl_schedule_node_graft_tree(node
, tree
);
1504 /* Is the band node "node" marked permutable?
1506 isl_bool
isl_schedule_node_band_get_permutable(
1507 __isl_keep isl_schedule_node
*node
)
1510 return isl_bool_error
;
1512 return isl_schedule_tree_band_get_permutable(node
->tree
);
1515 /* Mark the band node "node" permutable or not according to "permutable"?
1517 __isl_give isl_schedule_node
*isl_schedule_node_band_set_permutable(
1518 __isl_take isl_schedule_node
*node
, int permutable
)
1520 isl_schedule_tree
*tree
;
1524 if (isl_schedule_node_band_get_permutable(node
) == permutable
)
1527 tree
= isl_schedule_tree_copy(node
->tree
);
1528 tree
= isl_schedule_tree_band_set_permutable(tree
, permutable
);
1529 node
= isl_schedule_node_graft_tree(node
, tree
);
1534 /* Return the schedule space of the band node.
1536 __isl_give isl_space
*isl_schedule_node_band_get_space(
1537 __isl_keep isl_schedule_node
*node
)
1542 return isl_schedule_tree_band_get_space(node
->tree
);
1545 /* Return the schedule of the band node in isolation.
1547 __isl_give isl_multi_union_pw_aff
*isl_schedule_node_band_get_partial_schedule(
1548 __isl_keep isl_schedule_node
*node
)
1553 return isl_schedule_tree_band_get_partial_schedule(node
->tree
);
1556 /* Return the schedule of the band node in isolation in the form of
1559 * If the band does not have any members, then we construct a universe map
1560 * with the universe of the domain elements reaching the node as domain.
1561 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1562 * convert that to an isl_union_map.
1564 __isl_give isl_union_map
*isl_schedule_node_band_get_partial_schedule_union_map(
1565 __isl_keep isl_schedule_node
*node
)
1567 isl_multi_union_pw_aff
*mupa
;
1572 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
1573 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1574 "not a band node", return NULL
);
1575 if (isl_schedule_node_band_n_member(node
) == 0) {
1576 isl_union_set
*domain
;
1578 domain
= isl_schedule_node_get_universe_domain(node
);
1579 return isl_union_map_from_domain(domain
);
1582 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
1583 return isl_union_map_from_multi_union_pw_aff(mupa
);
1586 /* Return the loop AST generation type for the band member of band node "node"
1587 * at position "pos".
1589 enum isl_ast_loop_type
isl_schedule_node_band_member_get_ast_loop_type(
1590 __isl_keep isl_schedule_node
*node
, int pos
)
1593 return isl_ast_loop_error
;
1595 return isl_schedule_tree_band_member_get_ast_loop_type(node
->tree
, pos
);
1598 /* Set the loop AST generation type for the band member of band node "node"
1599 * at position "pos" to "type".
1601 __isl_give isl_schedule_node
*isl_schedule_node_band_member_set_ast_loop_type(
1602 __isl_take isl_schedule_node
*node
, int pos
,
1603 enum isl_ast_loop_type type
)
1605 isl_schedule_tree
*tree
;
1610 tree
= isl_schedule_tree_copy(node
->tree
);
1611 tree
= isl_schedule_tree_band_member_set_ast_loop_type(tree
, pos
, type
);
1612 return isl_schedule_node_graft_tree(node
, tree
);
1615 /* Return the loop AST generation type for the band member of band node "node"
1616 * at position "pos" for the isolated part.
1618 enum isl_ast_loop_type
isl_schedule_node_band_member_get_isolate_ast_loop_type(
1619 __isl_keep isl_schedule_node
*node
, int pos
)
1622 return isl_ast_loop_error
;
1624 return isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1628 /* Set the loop AST generation type for the band member of band node "node"
1629 * at position "pos" for the isolated part to "type".
1631 __isl_give isl_schedule_node
*
1632 isl_schedule_node_band_member_set_isolate_ast_loop_type(
1633 __isl_take isl_schedule_node
*node
, int pos
,
1634 enum isl_ast_loop_type type
)
1636 isl_schedule_tree
*tree
;
1641 tree
= isl_schedule_tree_copy(node
->tree
);
1642 tree
= isl_schedule_tree_band_member_set_isolate_ast_loop_type(tree
,
1644 return isl_schedule_node_graft_tree(node
, tree
);
1647 /* Return the AST build options associated to band node "node".
1649 __isl_give isl_union_set
*isl_schedule_node_band_get_ast_build_options(
1650 __isl_keep isl_schedule_node
*node
)
1655 return isl_schedule_tree_band_get_ast_build_options(node
->tree
);
1658 /* Replace the AST build options associated to band node "node" by "options".
1660 __isl_give isl_schedule_node
*isl_schedule_node_band_set_ast_build_options(
1661 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*options
)
1663 isl_schedule_tree
*tree
;
1665 if (!node
|| !options
)
1668 tree
= isl_schedule_tree_copy(node
->tree
);
1669 tree
= isl_schedule_tree_band_set_ast_build_options(tree
, options
);
1670 return isl_schedule_node_graft_tree(node
, tree
);
1672 isl_schedule_node_free(node
);
1673 isl_union_set_free(options
);
1677 /* Make sure that that spaces of "node" and "mv" are the same.
1678 * Return -1 on error, reporting the error to the user.
1680 static int check_space_multi_val(__isl_keep isl_schedule_node
*node
,
1681 __isl_keep isl_multi_val
*mv
)
1683 isl_space
*node_space
, *mv_space
;
1686 node_space
= isl_schedule_node_band_get_space(node
);
1687 mv_space
= isl_multi_val_get_space(mv
);
1688 equal
= isl_space_tuple_is_equal(node_space
, isl_dim_set
,
1689 mv_space
, isl_dim_set
);
1690 isl_space_free(mv_space
);
1691 isl_space_free(node_space
);
1695 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1696 "spaces don't match", return -1);
1701 /* Multiply the partial schedule of the band node "node"
1702 * with the factors in "mv".
1704 __isl_give isl_schedule_node
*isl_schedule_node_band_scale(
1705 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1707 isl_schedule_tree
*tree
;
1712 if (check_space_multi_val(node
, mv
) < 0)
1714 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1718 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1719 "cannot scale band node with anchored subtree",
1722 tree
= isl_schedule_node_get_tree(node
);
1723 tree
= isl_schedule_tree_band_scale(tree
, mv
);
1724 return isl_schedule_node_graft_tree(node
, tree
);
1726 isl_multi_val_free(mv
);
1727 isl_schedule_node_free(node
);
1731 /* Divide the partial schedule of the band node "node"
1732 * by the factors in "mv".
1734 __isl_give isl_schedule_node
*isl_schedule_node_band_scale_down(
1735 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1737 isl_schedule_tree
*tree
;
1742 if (check_space_multi_val(node
, mv
) < 0)
1744 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1748 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1749 "cannot scale down band node with anchored subtree",
1752 tree
= isl_schedule_node_get_tree(node
);
1753 tree
= isl_schedule_tree_band_scale_down(tree
, mv
);
1754 return isl_schedule_node_graft_tree(node
, tree
);
1756 isl_multi_val_free(mv
);
1757 isl_schedule_node_free(node
);
1761 /* Reduce the partial schedule of the band node "node"
1762 * modulo the factors in "mv".
1764 __isl_give isl_schedule_node
*isl_schedule_node_band_mod(
1765 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*mv
)
1767 isl_schedule_tree
*tree
;
1772 if (check_space_multi_val(node
, mv
) < 0)
1774 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1778 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1779 "cannot perform mod on band node with anchored subtree",
1782 tree
= isl_schedule_node_get_tree(node
);
1783 tree
= isl_schedule_tree_band_mod(tree
, mv
);
1784 return isl_schedule_node_graft_tree(node
, tree
);
1786 isl_multi_val_free(mv
);
1787 isl_schedule_node_free(node
);
1791 /* Make sure that that spaces of "node" and "mupa" are the same.
1792 * Return isl_stat_error on error, reporting the error to the user.
1794 static isl_stat
check_space_multi_union_pw_aff(
1795 __isl_keep isl_schedule_node
*node
,
1796 __isl_keep isl_multi_union_pw_aff
*mupa
)
1798 isl_space
*node_space
, *mupa_space
;
1801 node_space
= isl_schedule_node_band_get_space(node
);
1802 mupa_space
= isl_multi_union_pw_aff_get_space(mupa
);
1803 equal
= isl_space_tuple_is_equal(node_space
, isl_dim_set
,
1804 mupa_space
, isl_dim_set
);
1805 isl_space_free(mupa_space
);
1806 isl_space_free(node_space
);
1808 return isl_stat_error
;
1810 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1811 "spaces don't match", return isl_stat_error
);
1816 /* Shift the partial schedule of the band node "node" by "shift".
1818 __isl_give isl_schedule_node
*isl_schedule_node_band_shift(
1819 __isl_take isl_schedule_node
*node
,
1820 __isl_take isl_multi_union_pw_aff
*shift
)
1822 isl_schedule_tree
*tree
;
1825 if (!node
|| !shift
)
1827 if (check_space_multi_union_pw_aff(node
, shift
) < 0)
1829 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1833 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1834 "cannot shift band node with anchored subtree",
1837 tree
= isl_schedule_node_get_tree(node
);
1838 tree
= isl_schedule_tree_band_shift(tree
, shift
);
1839 return isl_schedule_node_graft_tree(node
, tree
);
1841 isl_multi_union_pw_aff_free(shift
);
1842 isl_schedule_node_free(node
);
1846 /* Tile "node" with tile sizes "sizes".
1848 * The current node is replaced by two nested nodes corresponding
1849 * to the tile dimensions and the point dimensions.
1851 * Return a pointer to the outer (tile) node.
1853 * If any of the descendants of "node" depend on the set of outer band nodes,
1854 * then we refuse to tile the node.
1856 * If the scale tile loops option is set, then the tile loops
1857 * are scaled by the tile sizes. If the shift point loops option is set,
1858 * then the point loops are shifted to start at zero.
1859 * In particular, these options affect the tile and point loop schedules
1862 * scale shift original tile point
1864 * 0 0 i floor(i/s) i
1865 * 1 0 i s * floor(i/s) i
1866 * 0 1 i floor(i/s) i - s * floor(i/s)
1867 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1869 __isl_give isl_schedule_node
*isl_schedule_node_band_tile(
1870 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
1872 isl_schedule_tree
*tree
;
1875 if (!node
|| !sizes
)
1877 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1881 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1882 "cannot tile band node with anchored subtree",
1885 if (check_space_multi_val(node
, sizes
) < 0)
1888 tree
= isl_schedule_node_get_tree(node
);
1889 tree
= isl_schedule_tree_band_tile(tree
, sizes
);
1890 return isl_schedule_node_graft_tree(node
, tree
);
1892 isl_multi_val_free(sizes
);
1893 isl_schedule_node_free(node
);
1897 /* Move the band node "node" down to all the leaves in the subtree
1899 * Return a pointer to the node in the resulting tree that is in the same
1900 * position as the node pointed to by "node" in the original tree.
1902 * If the node only has a leaf child, then nothing needs to be done.
1903 * Otherwise, the child of the node is removed and the result is
1904 * appended to all the leaves in the subtree rooted at the original child.
1905 * Since the node is moved to the leaves, it needs to be expanded
1906 * according to the expansion, if any, defined by that subtree.
1907 * In the end, the original node is replaced by the result of
1908 * attaching copies of the expanded node to the leaves.
1910 * If any of the nodes in the subtree rooted at "node" depend on
1911 * the set of outer band nodes then we refuse to sink the band node.
1913 __isl_give isl_schedule_node
*isl_schedule_node_band_sink(
1914 __isl_take isl_schedule_node
*node
)
1916 enum isl_schedule_node_type type
;
1917 isl_schedule_tree
*tree
, *child
;
1918 isl_union_pw_multi_aff
*contraction
;
1924 type
= isl_schedule_node_get_type(node
);
1925 if (type
!= isl_schedule_node_band
)
1926 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1927 "not a band node", isl_schedule_node_free(node
));
1928 anchored
= isl_schedule_node_is_subtree_anchored(node
);
1930 return isl_schedule_node_free(node
);
1932 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
1933 "cannot sink band node in anchored subtree",
1934 isl_schedule_node_free(node
));
1935 if (isl_schedule_tree_n_children(node
->tree
) == 0)
1938 contraction
= isl_schedule_node_get_subtree_contraction(node
);
1940 tree
= isl_schedule_node_get_tree(node
);
1941 child
= isl_schedule_tree_get_child(tree
, 0);
1942 tree
= isl_schedule_tree_reset_children(tree
);
1943 tree
= isl_schedule_tree_pullback_union_pw_multi_aff(tree
, contraction
);
1944 tree
= isl_schedule_tree_append_to_leaves(child
, tree
);
1946 return isl_schedule_node_graft_tree(node
, tree
);
1949 /* Split "node" into two nested band nodes, one with the first "pos"
1950 * dimensions and one with the remaining dimensions.
1951 * The schedules of the two band nodes live in anonymous spaces.
1953 __isl_give isl_schedule_node
*isl_schedule_node_band_split(
1954 __isl_take isl_schedule_node
*node
, int pos
)
1956 isl_schedule_tree
*tree
;
1958 tree
= isl_schedule_node_get_tree(node
);
1959 tree
= isl_schedule_tree_band_split(tree
, pos
);
1960 return isl_schedule_node_graft_tree(node
, tree
);
1963 /* Return the context of the context node "node".
1965 __isl_give isl_set
*isl_schedule_node_context_get_context(
1966 __isl_keep isl_schedule_node
*node
)
1971 return isl_schedule_tree_context_get_context(node
->tree
);
1974 /* Return the domain of the domain node "node".
1976 __isl_give isl_union_set
*isl_schedule_node_domain_get_domain(
1977 __isl_keep isl_schedule_node
*node
)
1982 return isl_schedule_tree_domain_get_domain(node
->tree
);
1985 /* Return the expansion map of expansion node "node".
1987 __isl_give isl_union_map
*isl_schedule_node_expansion_get_expansion(
1988 __isl_keep isl_schedule_node
*node
)
1993 return isl_schedule_tree_expansion_get_expansion(node
->tree
);
1996 /* Return the contraction of expansion node "node".
1998 __isl_give isl_union_pw_multi_aff
*isl_schedule_node_expansion_get_contraction(
1999 __isl_keep isl_schedule_node
*node
)
2004 return isl_schedule_tree_expansion_get_contraction(node
->tree
);
2007 /* Replace the contraction and the expansion of the expansion node "node"
2008 * by "contraction" and "expansion".
2010 __isl_give isl_schedule_node
*
2011 isl_schedule_node_expansion_set_contraction_and_expansion(
2012 __isl_take isl_schedule_node
*node
,
2013 __isl_take isl_union_pw_multi_aff
*contraction
,
2014 __isl_take isl_union_map
*expansion
)
2016 isl_schedule_tree
*tree
;
2018 if (!node
|| !contraction
|| !expansion
)
2021 tree
= isl_schedule_tree_copy(node
->tree
);
2022 tree
= isl_schedule_tree_expansion_set_contraction_and_expansion(tree
,
2023 contraction
, expansion
);
2024 return isl_schedule_node_graft_tree(node
, tree
);
2026 isl_schedule_node_free(node
);
2027 isl_union_pw_multi_aff_free(contraction
);
2028 isl_union_map_free(expansion
);
2032 /* Return the extension of the extension node "node".
2034 __isl_give isl_union_map
*isl_schedule_node_extension_get_extension(
2035 __isl_keep isl_schedule_node
*node
)
2040 return isl_schedule_tree_extension_get_extension(node
->tree
);
2043 /* Replace the extension of extension node "node" by "extension".
2045 __isl_give isl_schedule_node
*isl_schedule_node_extension_set_extension(
2046 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
2048 isl_schedule_tree
*tree
;
2050 if (!node
|| !extension
)
2053 tree
= isl_schedule_tree_copy(node
->tree
);
2054 tree
= isl_schedule_tree_extension_set_extension(tree
, extension
);
2055 return isl_schedule_node_graft_tree(node
, tree
);
2057 isl_schedule_node_free(node
);
2058 isl_union_map_free(extension
);
2062 /* Return the filter of the filter node "node".
2064 __isl_give isl_union_set
*isl_schedule_node_filter_get_filter(
2065 __isl_keep isl_schedule_node
*node
)
2070 return isl_schedule_tree_filter_get_filter(node
->tree
);
2073 /* Replace the filter of filter node "node" by "filter".
2075 __isl_give isl_schedule_node
*isl_schedule_node_filter_set_filter(
2076 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2078 isl_schedule_tree
*tree
;
2080 if (!node
|| !filter
)
2083 tree
= isl_schedule_tree_copy(node
->tree
);
2084 tree
= isl_schedule_tree_filter_set_filter(tree
, filter
);
2085 return isl_schedule_node_graft_tree(node
, tree
);
2087 isl_schedule_node_free(node
);
2088 isl_union_set_free(filter
);
2092 /* Intersect the filter of filter node "node" with "filter".
2094 * If the filter of the node is already a subset of "filter",
2095 * then leave the node unchanged.
2097 __isl_give isl_schedule_node
*isl_schedule_node_filter_intersect_filter(
2098 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2100 isl_union_set
*node_filter
= NULL
;
2103 if (!node
|| !filter
)
2106 node_filter
= isl_schedule_node_filter_get_filter(node
);
2107 subset
= isl_union_set_is_subset(node_filter
, filter
);
2111 isl_union_set_free(node_filter
);
2112 isl_union_set_free(filter
);
2115 node_filter
= isl_union_set_intersect(node_filter
, filter
);
2116 node
= isl_schedule_node_filter_set_filter(node
, node_filter
);
2119 isl_schedule_node_free(node
);
2120 isl_union_set_free(node_filter
);
2121 isl_union_set_free(filter
);
2125 /* Return the guard of the guard node "node".
2127 __isl_give isl_set
*isl_schedule_node_guard_get_guard(
2128 __isl_keep isl_schedule_node
*node
)
2133 return isl_schedule_tree_guard_get_guard(node
->tree
);
2136 /* Return the mark identifier of the mark node "node".
2138 __isl_give isl_id
*isl_schedule_node_mark_get_id(
2139 __isl_keep isl_schedule_node
*node
)
2144 return isl_schedule_tree_mark_get_id(node
->tree
);
2147 /* Replace the child at position "pos" of the sequence node "node"
2148 * by the children of sequence root node of "tree".
2150 __isl_give isl_schedule_node
*isl_schedule_node_sequence_splice(
2151 __isl_take isl_schedule_node
*node
, int pos
,
2152 __isl_take isl_schedule_tree
*tree
)
2154 isl_schedule_tree
*node_tree
;
2158 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2159 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2160 "not a sequence node", goto error
);
2161 if (isl_schedule_tree_get_type(tree
) != isl_schedule_node_sequence
)
2162 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2163 "not a sequence node", goto error
);
2164 node_tree
= isl_schedule_node_get_tree(node
);
2165 node_tree
= isl_schedule_tree_sequence_splice(node_tree
, pos
, tree
);
2166 node
= isl_schedule_node_graft_tree(node
, node_tree
);
2170 isl_schedule_node_free(node
);
2171 isl_schedule_tree_free(tree
);
2175 /* Given a sequence node "node", with a child at position "pos" that
2176 * is also a sequence node, attach the children of that node directly
2177 * as children of "node" at that position, replacing the original child.
2179 * The filters of these children are intersected with the filter
2180 * of the child at position "pos".
2182 __isl_give isl_schedule_node
*isl_schedule_node_sequence_splice_child(
2183 __isl_take isl_schedule_node
*node
, int pos
)
2186 isl_union_set
*filter
;
2187 isl_schedule_node
*child
;
2188 isl_schedule_tree
*tree
;
2192 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2193 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2194 "not a sequence node", isl_schedule_node_free(node
));
2195 node
= isl_schedule_node_child(node
, pos
);
2196 node
= isl_schedule_node_child(node
, 0);
2197 if (isl_schedule_node_get_type(node
) != isl_schedule_node_sequence
)
2198 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2199 "not a sequence node", isl_schedule_node_free(node
));
2200 child
= isl_schedule_node_copy(node
);
2201 node
= isl_schedule_node_parent(node
);
2202 filter
= isl_schedule_node_filter_get_filter(node
);
2203 n
= isl_schedule_node_n_children(child
);
2204 for (i
= 0; i
< n
; ++i
) {
2205 child
= isl_schedule_node_child(child
, i
);
2206 child
= isl_schedule_node_filter_intersect_filter(child
,
2207 isl_union_set_copy(filter
));
2208 child
= isl_schedule_node_parent(child
);
2210 isl_union_set_free(filter
);
2211 tree
= isl_schedule_node_get_tree(child
);
2212 isl_schedule_node_free(child
);
2213 node
= isl_schedule_node_parent(node
);
2214 node
= isl_schedule_node_sequence_splice(node
, pos
, tree
);
2219 /* Update the ancestors of "node" to point to the tree that "node"
2221 * That is, replace the child in the original parent that corresponds
2222 * to the current tree position by node->tree and continue updating
2223 * the ancestors in the same way until the root is reached.
2225 * If "fn" is not NULL, then it is called on each ancestor as we move up
2226 * the tree so that it can modify the ancestor before it is added
2227 * to the list of ancestors of the modified node.
2228 * The additional "pos" argument records the position
2229 * of the "tree" argument in the original schedule tree.
2231 * If "node" originally points to a leaf of the schedule tree, then make sure
2232 * that in the end it points to a leaf in the updated schedule tree.
2234 static __isl_give isl_schedule_node
*update_ancestors(
2235 __isl_take isl_schedule_node
*node
,
2236 __isl_give isl_schedule_tree
*(*fn
)(__isl_take isl_schedule_tree
*tree
,
2237 __isl_keep isl_schedule_node
*pos
, void *user
), void *user
)
2242 isl_schedule_tree
*tree
;
2243 isl_schedule_node
*pos
= NULL
;
2246 pos
= isl_schedule_node_copy(node
);
2248 node
= isl_schedule_node_cow(node
);
2250 return isl_schedule_node_free(pos
);
2252 ctx
= isl_schedule_node_get_ctx(node
);
2253 n
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
2254 tree
= isl_schedule_tree_copy(node
->tree
);
2256 for (i
= n
- 1; i
>= 0; --i
) {
2257 isl_schedule_tree
*parent
;
2259 parent
= isl_schedule_tree_list_get_schedule_tree(
2260 node
->ancestors
, i
);
2261 parent
= isl_schedule_tree_replace_child(parent
,
2262 node
->child_pos
[i
], tree
);
2264 pos
= isl_schedule_node_parent(pos
);
2265 parent
= fn(parent
, pos
, user
);
2267 node
->ancestors
= isl_schedule_tree_list_set_schedule_tree(
2268 node
->ancestors
, i
, isl_schedule_tree_copy(parent
));
2274 isl_schedule_node_free(pos
);
2276 is_leaf
= isl_schedule_tree_is_leaf(node
->tree
);
2277 node
->schedule
= isl_schedule_set_root(node
->schedule
, tree
);
2279 isl_schedule_tree_free(node
->tree
);
2280 node
->tree
= isl_schedule_node_get_leaf(node
);
2283 if (!node
->schedule
|| !node
->ancestors
)
2284 return isl_schedule_node_free(node
);
2289 /* Replace the subtree that "pos" points to by "tree", updating
2290 * the ancestors to maintain a consistent state.
2292 __isl_give isl_schedule_node
*isl_schedule_node_graft_tree(
2293 __isl_take isl_schedule_node
*pos
, __isl_take isl_schedule_tree
*tree
)
2297 if (pos
->tree
== tree
) {
2298 isl_schedule_tree_free(tree
);
2302 pos
= isl_schedule_node_cow(pos
);
2306 isl_schedule_tree_free(pos
->tree
);
2309 return update_ancestors(pos
, NULL
, NULL
);
2311 isl_schedule_node_free(pos
);
2312 isl_schedule_tree_free(tree
);
2316 /* Make sure we can insert a node between "node" and its parent.
2317 * Return -1 on error, reporting the reason why we cannot insert a node.
2319 static int check_insert(__isl_keep isl_schedule_node
*node
)
2322 enum isl_schedule_node_type type
;
2324 has_parent
= isl_schedule_node_has_parent(node
);
2328 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2329 "cannot insert node outside of root", return -1);
2331 type
= isl_schedule_node_get_parent_type(node
);
2332 if (type
== isl_schedule_node_error
)
2334 if (type
== isl_schedule_node_set
|| type
== isl_schedule_node_sequence
)
2335 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2336 "cannot insert node between set or sequence node "
2337 "and its filter children", return -1);
2342 /* Insert a band node with partial schedule "mupa" between "node" and
2344 * Return a pointer to the new band node.
2346 * If any of the nodes in the subtree rooted at "node" depend on
2347 * the set of outer band nodes then we refuse to insert the band node.
2349 __isl_give isl_schedule_node
*isl_schedule_node_insert_partial_schedule(
2350 __isl_take isl_schedule_node
*node
,
2351 __isl_take isl_multi_union_pw_aff
*mupa
)
2354 isl_schedule_band
*band
;
2355 isl_schedule_tree
*tree
;
2357 if (check_insert(node
) < 0)
2358 node
= isl_schedule_node_free(node
);
2359 anchored
= isl_schedule_node_is_subtree_anchored(node
);
2363 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2364 "cannot insert band node in anchored subtree",
2367 tree
= isl_schedule_node_get_tree(node
);
2368 band
= isl_schedule_band_from_multi_union_pw_aff(mupa
);
2369 tree
= isl_schedule_tree_insert_band(tree
, band
);
2370 node
= isl_schedule_node_graft_tree(node
, tree
);
2374 isl_schedule_node_free(node
);
2375 isl_multi_union_pw_aff_free(mupa
);
2379 /* Insert a context node with context "context" between "node" and its parent.
2380 * Return a pointer to the new context node.
2382 __isl_give isl_schedule_node
*isl_schedule_node_insert_context(
2383 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*context
)
2385 isl_schedule_tree
*tree
;
2387 if (check_insert(node
) < 0)
2388 node
= isl_schedule_node_free(node
);
2390 tree
= isl_schedule_node_get_tree(node
);
2391 tree
= isl_schedule_tree_insert_context(tree
, context
);
2392 node
= isl_schedule_node_graft_tree(node
, tree
);
2397 /* Insert an expansion node with the given "contraction" and "expansion"
2398 * between "node" and its parent.
2399 * Return a pointer to the new expansion node.
2401 * Typically the domain and range spaces of the expansion are different.
2402 * This means that only one of them can refer to the current domain space
2403 * in a consistent tree. It is up to the caller to ensure that the tree
2404 * returns to a consistent state.
2406 __isl_give isl_schedule_node
*isl_schedule_node_insert_expansion(
2407 __isl_take isl_schedule_node
*node
,
2408 __isl_take isl_union_pw_multi_aff
*contraction
,
2409 __isl_take isl_union_map
*expansion
)
2411 isl_schedule_tree
*tree
;
2413 if (check_insert(node
) < 0)
2414 node
= isl_schedule_node_free(node
);
2416 tree
= isl_schedule_node_get_tree(node
);
2417 tree
= isl_schedule_tree_insert_expansion(tree
, contraction
, expansion
);
2418 node
= isl_schedule_node_graft_tree(node
, tree
);
2423 /* Insert an extension node with extension "extension" between "node" and
2425 * Return a pointer to the new extension node.
2427 __isl_give isl_schedule_node
*isl_schedule_node_insert_extension(
2428 __isl_take isl_schedule_node
*node
,
2429 __isl_take isl_union_map
*extension
)
2431 isl_schedule_tree
*tree
;
2433 tree
= isl_schedule_node_get_tree(node
);
2434 tree
= isl_schedule_tree_insert_extension(tree
, extension
);
2435 node
= isl_schedule_node_graft_tree(node
, tree
);
2440 /* Insert a filter node with filter "filter" between "node" and its parent.
2441 * Return a pointer to the new filter node.
2443 __isl_give isl_schedule_node
*isl_schedule_node_insert_filter(
2444 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
2446 isl_schedule_tree
*tree
;
2448 if (check_insert(node
) < 0)
2449 node
= isl_schedule_node_free(node
);
2451 tree
= isl_schedule_node_get_tree(node
);
2452 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
2453 node
= isl_schedule_node_graft_tree(node
, tree
);
2458 /* Insert a guard node with guard "guard" between "node" and its parent.
2459 * Return a pointer to the new guard node.
2461 __isl_give isl_schedule_node
*isl_schedule_node_insert_guard(
2462 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*guard
)
2464 isl_schedule_tree
*tree
;
2466 if (check_insert(node
) < 0)
2467 node
= isl_schedule_node_free(node
);
2469 tree
= isl_schedule_node_get_tree(node
);
2470 tree
= isl_schedule_tree_insert_guard(tree
, guard
);
2471 node
= isl_schedule_node_graft_tree(node
, tree
);
2476 /* Insert a mark node with mark identifier "mark" between "node" and
2478 * Return a pointer to the new mark node.
2480 __isl_give isl_schedule_node
*isl_schedule_node_insert_mark(
2481 __isl_take isl_schedule_node
*node
, __isl_take isl_id
*mark
)
2483 isl_schedule_tree
*tree
;
2485 if (check_insert(node
) < 0)
2486 node
= isl_schedule_node_free(node
);
2488 tree
= isl_schedule_node_get_tree(node
);
2489 tree
= isl_schedule_tree_insert_mark(tree
, mark
);
2490 node
= isl_schedule_node_graft_tree(node
, tree
);
2495 /* Attach the current subtree of "node" to a sequence of filter tree nodes
2496 * with filters described by "filters", attach this sequence
2497 * of filter tree nodes as children to a new tree of type "type" and
2498 * replace the original subtree of "node" by this new tree.
2499 * Each copy of the original subtree is simplified with respect
2500 * to the corresponding filter.
2502 static __isl_give isl_schedule_node
*isl_schedule_node_insert_children(
2503 __isl_take isl_schedule_node
*node
,
2504 enum isl_schedule_node_type type
,
2505 __isl_take isl_union_set_list
*filters
)
2509 isl_schedule_tree
*tree
;
2510 isl_schedule_tree_list
*list
;
2512 if (check_insert(node
) < 0)
2513 node
= isl_schedule_node_free(node
);
2515 if (!node
|| !filters
)
2518 ctx
= isl_schedule_node_get_ctx(node
);
2519 n
= isl_union_set_list_n_union_set(filters
);
2520 list
= isl_schedule_tree_list_alloc(ctx
, n
);
2521 for (i
= 0; i
< n
; ++i
) {
2522 isl_schedule_node
*node_i
;
2523 isl_schedule_tree
*tree
;
2524 isl_union_set
*filter
;
2526 filter
= isl_union_set_list_get_union_set(filters
, i
);
2527 node_i
= isl_schedule_node_copy(node
);
2528 node_i
= isl_schedule_node_gist(node_i
,
2529 isl_union_set_copy(filter
));
2530 tree
= isl_schedule_node_get_tree(node_i
);
2531 isl_schedule_node_free(node_i
);
2532 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
2533 list
= isl_schedule_tree_list_add(list
, tree
);
2535 tree
= isl_schedule_tree_from_children(type
, list
);
2536 node
= isl_schedule_node_graft_tree(node
, tree
);
2538 isl_union_set_list_free(filters
);
2541 isl_union_set_list_free(filters
);
2542 isl_schedule_node_free(node
);
2546 /* Insert a sequence node with child filters "filters" between "node" and
2547 * its parent. That is, the tree that "node" points to is attached
2548 * to each of the child nodes of the filter nodes.
2549 * Return a pointer to the new sequence node.
2551 __isl_give isl_schedule_node
*isl_schedule_node_insert_sequence(
2552 __isl_take isl_schedule_node
*node
,
2553 __isl_take isl_union_set_list
*filters
)
2555 return isl_schedule_node_insert_children(node
,
2556 isl_schedule_node_sequence
, filters
);
2559 /* Insert a set node with child filters "filters" between "node" and
2560 * its parent. That is, the tree that "node" points to is attached
2561 * to each of the child nodes of the filter nodes.
2562 * Return a pointer to the new set node.
2564 __isl_give isl_schedule_node
*isl_schedule_node_insert_set(
2565 __isl_take isl_schedule_node
*node
,
2566 __isl_take isl_union_set_list
*filters
)
2568 return isl_schedule_node_insert_children(node
,
2569 isl_schedule_node_set
, filters
);
2572 /* Remove "node" from its schedule tree and return a pointer
2573 * to the leaf at the same position in the updated schedule tree.
2575 * It is not allowed to remove the root of a schedule tree or
2576 * a child of a set or sequence node.
2578 __isl_give isl_schedule_node
*isl_schedule_node_cut(
2579 __isl_take isl_schedule_node
*node
)
2581 isl_schedule_tree
*leaf
;
2582 enum isl_schedule_node_type parent_type
;
2586 if (!isl_schedule_node_has_parent(node
))
2587 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2588 "cannot cut root", return isl_schedule_node_free(node
));
2590 parent_type
= isl_schedule_node_get_parent_type(node
);
2591 if (parent_type
== isl_schedule_node_set
||
2592 parent_type
== isl_schedule_node_sequence
)
2593 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2594 "cannot cut child of set or sequence",
2595 return isl_schedule_node_free(node
));
2597 leaf
= isl_schedule_node_get_leaf(node
);
2598 return isl_schedule_node_graft_tree(node
, leaf
);
2601 /* Remove a single node from the schedule tree, attaching the child
2602 * of "node" directly to its parent.
2603 * Return a pointer to this former child or to the leaf the position
2604 * of the original node if there was no child.
2605 * It is not allowed to remove the root of a schedule tree,
2606 * a set or sequence node, a child of a set or sequence node or
2607 * a band node with an anchored subtree.
2609 __isl_give isl_schedule_node
*isl_schedule_node_delete(
2610 __isl_take isl_schedule_node
*node
)
2613 isl_schedule_tree
*tree
;
2614 enum isl_schedule_node_type type
;
2619 if (isl_schedule_node_get_tree_depth(node
) == 0)
2620 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2621 "cannot delete root node",
2622 return isl_schedule_node_free(node
));
2623 n
= isl_schedule_node_n_children(node
);
2625 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2626 "can only delete node with a single child",
2627 return isl_schedule_node_free(node
));
2628 type
= isl_schedule_node_get_parent_type(node
);
2629 if (type
== isl_schedule_node_sequence
|| type
== isl_schedule_node_set
)
2630 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
2631 "cannot delete child of set or sequence",
2632 return isl_schedule_node_free(node
));
2633 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
) {
2636 anchored
= isl_schedule_node_is_subtree_anchored(node
);
2638 return isl_schedule_node_free(node
);
2640 isl_die(isl_schedule_node_get_ctx(node
),
2642 "cannot delete band node with anchored subtree",
2643 return isl_schedule_node_free(node
));
2646 tree
= isl_schedule_node_get_tree(node
);
2647 if (!tree
|| isl_schedule_tree_has_children(tree
)) {
2648 tree
= isl_schedule_tree_child(tree
, 0);
2650 isl_schedule_tree_free(tree
);
2651 tree
= isl_schedule_node_get_leaf(node
);
2653 node
= isl_schedule_node_graft_tree(node
, tree
);
2658 /* Internal data structure for the group_ancestor callback.
2660 * If "finished" is set, then we no longer need to modify
2661 * any further ancestors.
2663 * "contraction" and "expansion" represent the expansion
2664 * that reflects the grouping.
2666 * "domain" contains the domain elements that reach the position
2667 * where the grouping is performed. That is, it is the range
2668 * of the resulting expansion.
2669 * "domain_universe" is the universe of "domain".
2670 * "group" is the set of group elements, i.e., the domain
2671 * of the resulting expansion.
2672 * "group_universe" is the universe of "group".
2674 * "sched" is the schedule for the group elements, in pratice
2675 * an identity mapping on "group_universe".
2676 * "dim" is the dimension of "sched".
2678 struct isl_schedule_group_data
{
2681 isl_union_map
*expansion
;
2682 isl_union_pw_multi_aff
*contraction
;
2684 isl_union_set
*domain
;
2685 isl_union_set
*domain_universe
;
2686 isl_union_set
*group
;
2687 isl_union_set
*group_universe
;
2690 isl_multi_aff
*sched
;
2693 /* Is domain covered by data->domain within data->domain_universe?
2695 static int locally_covered_by_domain(__isl_keep isl_union_set
*domain
,
2696 struct isl_schedule_group_data
*data
)
2699 isl_union_set
*test
;
2701 test
= isl_union_set_copy(domain
);
2702 test
= isl_union_set_intersect(test
,
2703 isl_union_set_copy(data
->domain_universe
));
2704 is_subset
= isl_union_set_is_subset(test
, data
->domain
);
2705 isl_union_set_free(test
);
2710 /* Update the band tree root "tree" to refer to the group instances
2711 * in data->group rather than the original domain elements in data->domain.
2712 * "pos" is the position in the original schedule tree where the modified
2713 * "tree" will be attached.
2715 * Add the part of the identity schedule on the group instances data->sched
2716 * that corresponds to this band node to the band schedule.
2717 * If the domain elements that reach the node and that are part
2718 * of data->domain_universe are all elements of data->domain (and therefore
2719 * replaced by the group instances) then this data->domain_universe
2720 * is removed from the domain of the band schedule.
2722 static __isl_give isl_schedule_tree
*group_band(
2723 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2724 struct isl_schedule_group_data
*data
)
2726 isl_union_set
*domain
;
2728 isl_multi_union_pw_aff
*mupa
, *partial
;
2730 int depth
, n
, has_id
;
2732 domain
= isl_schedule_node_get_domain(pos
);
2733 is_covered
= locally_covered_by_domain(domain
, data
);
2734 if (is_covered
>= 0 && is_covered
) {
2735 domain
= isl_union_set_universe(domain
);
2736 domain
= isl_union_set_subtract(domain
,
2737 isl_union_set_copy(data
->domain_universe
));
2738 tree
= isl_schedule_tree_band_intersect_domain(tree
, domain
);
2740 isl_union_set_free(domain
);
2742 return isl_schedule_tree_free(tree
);
2743 depth
= isl_schedule_node_get_schedule_depth(pos
);
2744 n
= isl_schedule_tree_band_n_member(tree
);
2745 ma
= isl_multi_aff_copy(data
->sched
);
2746 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, 0, depth
);
2747 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
, n
, data
->dim
- depth
- n
);
2748 mupa
= isl_multi_union_pw_aff_from_multi_aff(ma
);
2749 partial
= isl_schedule_tree_band_get_partial_schedule(tree
);
2750 has_id
= isl_multi_union_pw_aff_has_tuple_id(partial
, isl_dim_set
);
2752 partial
= isl_multi_union_pw_aff_free(partial
);
2753 } else if (has_id
) {
2755 id
= isl_multi_union_pw_aff_get_tuple_id(partial
, isl_dim_set
);
2756 mupa
= isl_multi_union_pw_aff_set_tuple_id(mupa
,
2759 partial
= isl_multi_union_pw_aff_union_add(partial
, mupa
);
2760 tree
= isl_schedule_tree_band_set_partial_schedule(tree
, partial
);
2765 /* Drop the parameters in "uset" that are not also in "space".
2766 * "n" is the number of parameters in "space".
2768 static __isl_give isl_union_set
*union_set_drop_extra_params(
2769 __isl_take isl_union_set
*uset
, __isl_keep isl_space
*space
, int n
)
2773 uset
= isl_union_set_align_params(uset
, isl_space_copy(space
));
2774 n2
= isl_union_set_dim(uset
, isl_dim_param
);
2775 uset
= isl_union_set_project_out(uset
, isl_dim_param
, n
, n2
- n
);
2780 /* Update the context tree root "tree" to refer to the group instances
2781 * in data->group rather than the original domain elements in data->domain.
2782 * "pos" is the position in the original schedule tree where the modified
2783 * "tree" will be attached.
2785 * We do not actually need to update "tree" since a context node only
2786 * refers to the schedule space. However, we may need to update "data"
2787 * to not refer to any parameters introduced by the context node.
2789 static __isl_give isl_schedule_tree
*group_context(
2790 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2791 struct isl_schedule_group_data
*data
)
2794 isl_union_set
*domain
;
2798 if (isl_schedule_node_get_tree_depth(pos
) == 1)
2801 domain
= isl_schedule_node_get_universe_domain(pos
);
2802 space
= isl_union_set_get_space(domain
);
2803 isl_union_set_free(domain
);
2805 n1
= isl_space_dim(space
, isl_dim_param
);
2806 data
->expansion
= isl_union_map_align_params(data
->expansion
, space
);
2807 n2
= isl_union_map_dim(data
->expansion
, isl_dim_param
);
2809 if (!data
->expansion
)
2810 return isl_schedule_tree_free(tree
);
2814 involves
= isl_union_map_involves_dims(data
->expansion
,
2815 isl_dim_param
, n1
, n2
- n1
);
2817 return isl_schedule_tree_free(tree
);
2819 isl_die(isl_schedule_node_get_ctx(pos
), isl_error_invalid
,
2820 "grouping cannot only refer to global parameters",
2821 return isl_schedule_tree_free(tree
));
2823 data
->expansion
= isl_union_map_project_out(data
->expansion
,
2824 isl_dim_param
, n1
, n2
- n1
);
2825 space
= isl_union_map_get_space(data
->expansion
);
2827 data
->contraction
= isl_union_pw_multi_aff_align_params(
2828 data
->contraction
, isl_space_copy(space
));
2829 n2
= isl_union_pw_multi_aff_dim(data
->contraction
, isl_dim_param
);
2830 data
->contraction
= isl_union_pw_multi_aff_drop_dims(data
->contraction
,
2831 isl_dim_param
, n1
, n2
- n1
);
2833 data
->domain
= union_set_drop_extra_params(data
->domain
, space
, n1
);
2834 data
->domain_universe
=
2835 union_set_drop_extra_params(data
->domain_universe
, space
, n1
);
2836 data
->group
= union_set_drop_extra_params(data
->group
, space
, n1
);
2837 data
->group_universe
=
2838 union_set_drop_extra_params(data
->group_universe
, space
, n1
);
2840 data
->sched
= isl_multi_aff_align_params(data
->sched
,
2841 isl_space_copy(space
));
2842 n2
= isl_multi_aff_dim(data
->sched
, isl_dim_param
);
2843 data
->sched
= isl_multi_aff_drop_dims(data
->sched
,
2844 isl_dim_param
, n1
, n2
- n1
);
2846 isl_space_free(space
);
2851 /* Update the domain tree root "tree" to refer to the group instances
2852 * in data->group rather than the original domain elements in data->domain.
2853 * "pos" is the position in the original schedule tree where the modified
2854 * "tree" will be attached.
2856 * We first double-check that all grouped domain elements are actually
2857 * part of the root domain and then replace those elements by the group
2860 static __isl_give isl_schedule_tree
*group_domain(
2861 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2862 struct isl_schedule_group_data
*data
)
2864 isl_union_set
*domain
;
2867 domain
= isl_schedule_tree_domain_get_domain(tree
);
2868 is_subset
= isl_union_set_is_subset(data
->domain
, domain
);
2869 isl_union_set_free(domain
);
2871 return isl_schedule_tree_free(tree
);
2873 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
2874 "grouped domain should be part of outer domain",
2875 return isl_schedule_tree_free(tree
));
2876 domain
= isl_schedule_tree_domain_get_domain(tree
);
2877 domain
= isl_union_set_subtract(domain
,
2878 isl_union_set_copy(data
->domain
));
2879 domain
= isl_union_set_union(domain
, isl_union_set_copy(data
->group
));
2880 tree
= isl_schedule_tree_domain_set_domain(tree
, domain
);
2885 /* Update the expansion tree root "tree" to refer to the group instances
2886 * in data->group rather than the original domain elements in data->domain.
2887 * "pos" is the position in the original schedule tree where the modified
2888 * "tree" will be attached.
2890 * Let G_1 -> D_1 be the expansion of "tree" and G_2 -> D_2 the newly
2891 * introduced expansion in a descendant of "tree".
2892 * We first double-check that D_2 is a subset of D_1.
2893 * Then we remove D_2 from the range of G_1 -> D_1 and add the mapping
2894 * G_1 -> D_1 . D_2 -> G_2.
2895 * Simmilarly, we restrict the domain of the contraction to the universe
2896 * of the range of the updated expansion and add G_2 -> D_2 . D_1 -> G_1,
2897 * attempting to remove the domain constraints of this additional part.
2899 static __isl_give isl_schedule_tree
*group_expansion(
2900 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2901 struct isl_schedule_group_data
*data
)
2903 isl_union_set
*domain
;
2904 isl_union_map
*expansion
, *umap
;
2905 isl_union_pw_multi_aff
*contraction
, *upma
;
2908 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2909 domain
= isl_union_map_range(expansion
);
2910 is_subset
= isl_union_set_is_subset(data
->domain
, domain
);
2911 isl_union_set_free(domain
);
2913 return isl_schedule_tree_free(tree
);
2915 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_internal
,
2916 "grouped domain should be part "
2917 "of outer expansion domain",
2918 return isl_schedule_tree_free(tree
));
2919 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2920 umap
= isl_union_map_from_union_pw_multi_aff(
2921 isl_union_pw_multi_aff_copy(data
->contraction
));
2922 umap
= isl_union_map_apply_range(expansion
, umap
);
2923 expansion
= isl_schedule_tree_expansion_get_expansion(tree
);
2924 expansion
= isl_union_map_subtract_range(expansion
,
2925 isl_union_set_copy(data
->domain
));
2926 expansion
= isl_union_map_union(expansion
, umap
);
2927 umap
= isl_union_map_universe(isl_union_map_copy(expansion
));
2928 domain
= isl_union_map_range(umap
);
2929 contraction
= isl_schedule_tree_expansion_get_contraction(tree
);
2930 umap
= isl_union_map_from_union_pw_multi_aff(contraction
);
2931 umap
= isl_union_map_apply_range(isl_union_map_copy(data
->expansion
),
2933 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
2934 contraction
= isl_schedule_tree_expansion_get_contraction(tree
);
2935 contraction
= isl_union_pw_multi_aff_intersect_domain(contraction
,
2937 domain
= isl_union_pw_multi_aff_domain(
2938 isl_union_pw_multi_aff_copy(upma
));
2939 upma
= isl_union_pw_multi_aff_gist(upma
, domain
);
2940 contraction
= isl_union_pw_multi_aff_union_add(contraction
, upma
);
2941 tree
= isl_schedule_tree_expansion_set_contraction_and_expansion(tree
,
2942 contraction
, expansion
);
2947 /* Update the tree root "tree" to refer to the group instances
2948 * in data->group rather than the original domain elements in data->domain.
2949 * "pos" is the position in the original schedule tree where the modified
2950 * "tree" will be attached.
2952 * If we have come across a domain or expansion node before (data->finished
2953 * is set), then we no longer need perform any modifications.
2955 * If "tree" is a filter, then we add data->group_universe to the filter.
2956 * We also remove data->domain_universe from the filter if all the domain
2957 * elements in this universe that reach the filter node are part of
2958 * the elements that are being grouped by data->expansion.
2959 * If "tree" is a band, domain or expansion, then it is handled
2960 * in a separate function.
2962 static __isl_give isl_schedule_tree
*group_ancestor(
2963 __isl_take isl_schedule_tree
*tree
, __isl_keep isl_schedule_node
*pos
,
2966 struct isl_schedule_group_data
*data
= user
;
2967 isl_union_set
*domain
;
2971 return isl_schedule_tree_free(tree
);
2976 switch (isl_schedule_tree_get_type(tree
)) {
2977 case isl_schedule_node_error
:
2978 return isl_schedule_tree_free(tree
);
2979 case isl_schedule_node_extension
:
2980 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_unsupported
,
2981 "grouping not allowed in extended tree",
2982 return isl_schedule_tree_free(tree
));
2983 case isl_schedule_node_band
:
2984 tree
= group_band(tree
, pos
, data
);
2986 case isl_schedule_node_context
:
2987 tree
= group_context(tree
, pos
, data
);
2989 case isl_schedule_node_domain
:
2990 tree
= group_domain(tree
, pos
, data
);
2993 case isl_schedule_node_filter
:
2994 domain
= isl_schedule_node_get_domain(pos
);
2995 is_covered
= locally_covered_by_domain(domain
, data
);
2996 isl_union_set_free(domain
);
2998 return isl_schedule_tree_free(tree
);
2999 domain
= isl_schedule_tree_filter_get_filter(tree
);
3001 domain
= isl_union_set_subtract(domain
,
3002 isl_union_set_copy(data
->domain_universe
));
3003 domain
= isl_union_set_union(domain
,
3004 isl_union_set_copy(data
->group_universe
));
3005 tree
= isl_schedule_tree_filter_set_filter(tree
, domain
);
3007 case isl_schedule_node_expansion
:
3008 tree
= group_expansion(tree
, pos
, data
);
3011 case isl_schedule_node_leaf
:
3012 case isl_schedule_node_guard
:
3013 case isl_schedule_node_mark
:
3014 case isl_schedule_node_sequence
:
3015 case isl_schedule_node_set
:
3022 /* Group the domain elements that reach "node" into instances
3023 * of a single statement with identifier "group_id".
3024 * In particular, group the domain elements according to their
3027 * That is, introduce an expansion node with as contraction
3028 * the prefix schedule (with the target space replaced by "group_id")
3029 * and as expansion the inverse of this contraction (with its range
3030 * intersected with the domain elements that reach "node").
3031 * The outer nodes are then modified to refer to the group instances
3032 * instead of the original domain elements.
3034 * No instance of "group_id" is allowed to reach "node" prior
3036 * No ancestor of "node" is allowed to be an extension node.
3038 * Return a pointer to original node in tree, i.e., the child
3039 * of the newly introduced expansion node.
3041 __isl_give isl_schedule_node
*isl_schedule_node_group(
3042 __isl_take isl_schedule_node
*node
, __isl_take isl_id
*group_id
)
3044 struct isl_schedule_group_data data
= { 0 };
3046 isl_union_set
*domain
;
3047 isl_union_pw_multi_aff
*contraction
;
3048 isl_union_map
*expansion
;
3051 if (!node
|| !group_id
)
3053 if (check_insert(node
) < 0)
3056 domain
= isl_schedule_node_get_domain(node
);
3057 data
.domain
= isl_union_set_copy(domain
);
3058 data
.domain_universe
= isl_union_set_copy(domain
);
3059 data
.domain_universe
= isl_union_set_universe(data
.domain_universe
);
3061 data
.dim
= isl_schedule_node_get_schedule_depth(node
);
3062 if (data
.dim
== 0) {
3065 isl_union_set
*group
;
3066 isl_union_map
*univ
;
3068 ctx
= isl_schedule_node_get_ctx(node
);
3069 space
= isl_space_set_alloc(ctx
, 0, 0);
3070 space
= isl_space_set_tuple_id(space
, isl_dim_set
, group_id
);
3071 set
= isl_set_universe(isl_space_copy(space
));
3072 group
= isl_union_set_from_set(set
);
3073 expansion
= isl_union_map_from_domain_and_range(domain
, group
);
3074 univ
= isl_union_map_universe(isl_union_map_copy(expansion
));
3075 contraction
= isl_union_pw_multi_aff_from_union_map(univ
);
3076 expansion
= isl_union_map_reverse(expansion
);
3078 isl_multi_union_pw_aff
*prefix
;
3079 isl_union_set
*univ
;
3082 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
3083 prefix
= isl_multi_union_pw_aff_set_tuple_id(prefix
,
3084 isl_dim_set
, group_id
);
3085 space
= isl_multi_union_pw_aff_get_space(prefix
);
3086 contraction
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
3088 univ
= isl_union_set_universe(isl_union_set_copy(domain
));
3090 isl_union_pw_multi_aff_intersect_domain(contraction
, univ
);
3091 expansion
= isl_union_map_from_union_pw_multi_aff(
3092 isl_union_pw_multi_aff_copy(contraction
));
3093 expansion
= isl_union_map_reverse(expansion
);
3094 expansion
= isl_union_map_intersect_range(expansion
, domain
);
3096 space
= isl_space_map_from_set(space
);
3097 data
.sched
= isl_multi_aff_identity(space
);
3098 data
.group
= isl_union_map_domain(isl_union_map_copy(expansion
));
3099 data
.group
= isl_union_set_coalesce(data
.group
);
3100 data
.group_universe
= isl_union_set_copy(data
.group
);
3101 data
.group_universe
= isl_union_set_universe(data
.group_universe
);
3102 data
.expansion
= isl_union_map_copy(expansion
);
3103 data
.contraction
= isl_union_pw_multi_aff_copy(contraction
);
3104 node
= isl_schedule_node_insert_expansion(node
, contraction
, expansion
);
3106 disjoint
= isl_union_set_is_disjoint(data
.domain_universe
,
3107 data
.group_universe
);
3109 node
= update_ancestors(node
, &group_ancestor
, &data
);
3111 isl_union_set_free(data
.domain
);
3112 isl_union_set_free(data
.domain_universe
);
3113 isl_union_set_free(data
.group
);
3114 isl_union_set_free(data
.group_universe
);
3115 isl_multi_aff_free(data
.sched
);
3116 isl_union_map_free(data
.expansion
);
3117 isl_union_pw_multi_aff_free(data
.contraction
);
3119 node
= isl_schedule_node_child(node
, 0);
3121 if (!node
|| disjoint
< 0)
3122 return isl_schedule_node_free(node
);
3124 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
3125 "group instances already reach node",
3126 isl_schedule_node_free(node
));
3130 isl_schedule_node_free(node
);
3131 isl_id_free(group_id
);
3135 /* Compute the gist of the given band node with respect to "context".
3137 __isl_give isl_schedule_node
*isl_schedule_node_band_gist(
3138 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*context
)
3140 isl_schedule_tree
*tree
;
3142 tree
= isl_schedule_node_get_tree(node
);
3143 tree
= isl_schedule_tree_band_gist(tree
, context
);
3144 return isl_schedule_node_graft_tree(node
, tree
);
3147 /* Internal data structure for isl_schedule_node_gist.
3148 * "n_expansion" is the number of outer expansion nodes
3149 * with respect to the current position
3150 * "filters" contains an element for each outer filter, expansion or
3151 * extension node with respect to the current position, each representing
3152 * the intersection of the previous element and the filter on the filter node
3153 * or the expansion/extension of the previous element.
3154 * The first element in the original context passed to isl_schedule_node_gist.
3156 struct isl_node_gist_data
{
3158 isl_union_set_list
*filters
;
3161 /* Enter the expansion node "node" during a isl_schedule_node_gist traversal.
3163 * In particular, add an extra element to data->filters containing
3164 * the expansion of the previous element and replace the expansion
3165 * and contraction on "node" by the gist with respect to these filters.
3166 * Also keep track of the fact that we have entered another expansion.
3168 static __isl_give isl_schedule_node
*gist_enter_expansion(
3169 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3172 isl_union_set
*inner
;
3173 isl_union_map
*expansion
;
3174 isl_union_pw_multi_aff
*contraction
;
3176 data
->n_expansion
++;
3178 n
= isl_union_set_list_n_union_set(data
->filters
);
3179 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3180 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3181 inner
= isl_union_set_apply(inner
, expansion
);
3183 contraction
= isl_schedule_node_expansion_get_contraction(node
);
3184 contraction
= isl_union_pw_multi_aff_gist(contraction
,
3185 isl_union_set_copy(inner
));
3187 data
->filters
= isl_union_set_list_add(data
->filters
, inner
);
3189 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3190 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3191 expansion
= isl_union_map_gist_domain(expansion
, inner
);
3192 node
= isl_schedule_node_expansion_set_contraction_and_expansion(node
,
3193 contraction
, expansion
);
3198 /* Leave the expansion node "node" during a isl_schedule_node_gist traversal.
3200 * In particular, remove the element in data->filters that was added by
3201 * gist_enter_expansion and decrement the number of outer expansions.
3203 * The expansion has already been simplified in gist_enter_expansion.
3204 * If this simplification results in an identity expansion, then
3205 * it is removed here.
3207 static __isl_give isl_schedule_node
*gist_leave_expansion(
3208 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3212 isl_union_map
*expansion
;
3214 expansion
= isl_schedule_node_expansion_get_expansion(node
);
3215 identity
= isl_union_map_is_identity(expansion
);
3216 isl_union_map_free(expansion
);
3219 node
= isl_schedule_node_free(node
);
3221 node
= isl_schedule_node_delete(node
);
3223 n
= isl_union_set_list_n_union_set(data
->filters
);
3224 data
->filters
= isl_union_set_list_drop(data
->filters
, n
- 1, 1);
3226 data
->n_expansion
--;
3231 /* Enter the extension node "node" during a isl_schedule_node_gist traversal.
3233 * In particular, add an extra element to data->filters containing
3234 * the union of the previous element with the additional domain elements
3235 * introduced by the extension.
3237 static __isl_give isl_schedule_node
*gist_enter_extension(
3238 __isl_take isl_schedule_node
*node
, struct isl_node_gist_data
*data
)
3241 isl_union_set
*inner
, *extra
;
3242 isl_union_map
*extension
;
3244 n
= isl_union_set_list_n_union_set(data
->filters
);
3245 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3246 extension
= isl_schedule_node_extension_get_extension(node
);
3247 extra
= isl_union_map_range(extension
);
3248 inner
= isl_union_set_union(inner
, extra
);
3250 data
->filters
= isl_union_set_list_add(data
->filters
, inner
);
3255 /* Can we finish gisting at this node?
3256 * That is, is the filter on the current filter node a subset of
3257 * the original context passed to isl_schedule_node_gist?
3258 * If we have gone through any expansions, then we cannot perform
3259 * this test since the current domain elements are incomparable
3260 * to the domain elements in the original context.
3262 static int gist_done(__isl_keep isl_schedule_node
*node
,
3263 struct isl_node_gist_data
*data
)
3265 isl_union_set
*filter
, *outer
;
3268 if (data
->n_expansion
!= 0)
3271 filter
= isl_schedule_node_filter_get_filter(node
);
3272 outer
= isl_union_set_list_get_union_set(data
->filters
, 0);
3273 subset
= isl_union_set_is_subset(filter
, outer
);
3274 isl_union_set_free(outer
);
3275 isl_union_set_free(filter
);
3280 /* Callback for "traverse" to enter a node and to move
3281 * to the deepest initial subtree that should be traversed
3282 * by isl_schedule_node_gist.
3284 * The "filters" list is extended by one element each time
3285 * we come across a filter node by the result of intersecting
3286 * the last element in the list with the filter on the filter node.
3288 * If the filter on the current filter node is a subset of
3289 * the original context passed to isl_schedule_node_gist,
3290 * then there is no need to go into its subtree since it cannot
3291 * be further simplified by the context. The "filters" list is
3292 * still extended for consistency, but the actual value of the
3293 * added element is immaterial since it will not be used.
3295 * Otherwise, the filter on the current filter node is replaced by
3296 * the gist of the original filter with respect to the intersection
3297 * of the original context with the intermediate filters.
3299 * If the new element in the "filters" list is empty, then no elements
3300 * can reach the descendants of the current filter node. The subtree
3301 * underneath the filter node is therefore removed.
3303 * Each expansion node we come across is handled by
3304 * gist_enter_expansion.
3306 * Each extension node we come across is handled by
3307 * gist_enter_extension.
3309 static __isl_give isl_schedule_node
*gist_enter(
3310 __isl_take isl_schedule_node
*node
, void *user
)
3312 struct isl_node_gist_data
*data
= user
;
3315 isl_union_set
*filter
, *inner
;
3319 switch (isl_schedule_node_get_type(node
)) {
3320 case isl_schedule_node_error
:
3321 return isl_schedule_node_free(node
);
3322 case isl_schedule_node_expansion
:
3323 node
= gist_enter_expansion(node
, data
);
3325 case isl_schedule_node_extension
:
3326 node
= gist_enter_extension(node
, data
);
3328 case isl_schedule_node_band
:
3329 case isl_schedule_node_context
:
3330 case isl_schedule_node_domain
:
3331 case isl_schedule_node_guard
:
3332 case isl_schedule_node_leaf
:
3333 case isl_schedule_node_mark
:
3334 case isl_schedule_node_sequence
:
3335 case isl_schedule_node_set
:
3337 case isl_schedule_node_filter
:
3340 done
= gist_done(node
, data
);
3341 filter
= isl_schedule_node_filter_get_filter(node
);
3342 if (done
< 0 || done
) {
3343 data
->filters
= isl_union_set_list_add(data
->filters
,
3346 return isl_schedule_node_free(node
);
3349 n
= isl_union_set_list_n_union_set(data
->filters
);
3350 inner
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3351 filter
= isl_union_set_gist(filter
, isl_union_set_copy(inner
));
3352 node
= isl_schedule_node_filter_set_filter(node
,
3353 isl_union_set_copy(filter
));
3354 filter
= isl_union_set_intersect(filter
, inner
);
3355 empty
= isl_union_set_is_empty(filter
);
3356 data
->filters
= isl_union_set_list_add(data
->filters
, filter
);
3358 return isl_schedule_node_free(node
);
3361 node
= isl_schedule_node_child(node
, 0);
3362 node
= isl_schedule_node_cut(node
);
3363 node
= isl_schedule_node_parent(node
);
3365 } while (isl_schedule_node_has_children(node
) &&
3366 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3371 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
3373 * In particular, if the current node is a filter node, then we remove
3374 * the element on the "filters" list that was added when we entered
3375 * the node. There is no need to compute any gist here, since we
3376 * already did that when we entered the node.
3378 * Expansion nodes are handled by gist_leave_expansion.
3380 * If the current node is an extension, then remove the element
3381 * in data->filters that was added by gist_enter_extension.
3383 * If the current node is a band node, then we compute the gist of
3384 * the band node with respect to the intersection of the original context
3385 * and the intermediate filters.
3387 * If the current node is a sequence or set node, then some of
3388 * the filter children may have become empty and so they are removed.
3389 * If only one child is left, then the set or sequence node along with
3390 * the single remaining child filter is removed. The filter can be
3391 * removed because the filters on a sequence or set node are supposed
3392 * to partition the incoming domain instances.
3393 * In principle, it should then be impossible for there to be zero
3394 * remaining children, but should this happen, we replace the entire
3395 * subtree with an empty filter.
3397 static __isl_give isl_schedule_node
*gist_leave(
3398 __isl_take isl_schedule_node
*node
, void *user
)
3400 struct isl_node_gist_data
*data
= user
;
3401 isl_schedule_tree
*tree
;
3403 isl_union_set
*filter
;
3405 switch (isl_schedule_node_get_type(node
)) {
3406 case isl_schedule_node_error
:
3407 return isl_schedule_node_free(node
);
3408 case isl_schedule_node_expansion
:
3409 node
= gist_leave_expansion(node
, data
);
3411 case isl_schedule_node_extension
:
3412 case isl_schedule_node_filter
:
3413 n
= isl_union_set_list_n_union_set(data
->filters
);
3414 data
->filters
= isl_union_set_list_drop(data
->filters
,
3417 case isl_schedule_node_band
:
3418 n
= isl_union_set_list_n_union_set(data
->filters
);
3419 filter
= isl_union_set_list_get_union_set(data
->filters
, n
- 1);
3420 node
= isl_schedule_node_band_gist(node
, filter
);
3422 case isl_schedule_node_set
:
3423 case isl_schedule_node_sequence
:
3424 tree
= isl_schedule_node_get_tree(node
);
3425 n
= isl_schedule_tree_n_children(tree
);
3426 for (i
= n
- 1; i
>= 0; --i
) {
3427 isl_schedule_tree
*child
;
3428 isl_union_set
*filter
;
3431 child
= isl_schedule_tree_get_child(tree
, i
);
3432 filter
= isl_schedule_tree_filter_get_filter(child
);
3433 empty
= isl_union_set_is_empty(filter
);
3434 isl_union_set_free(filter
);
3435 isl_schedule_tree_free(child
);
3437 tree
= isl_schedule_tree_free(tree
);
3439 tree
= isl_schedule_tree_drop_child(tree
, i
);
3441 n
= isl_schedule_tree_n_children(tree
);
3442 node
= isl_schedule_node_graft_tree(node
, tree
);
3444 node
= isl_schedule_node_delete(node
);
3445 node
= isl_schedule_node_delete(node
);
3446 } else if (n
== 0) {
3450 isl_union_set_list_get_union_set(data
->filters
, 0);
3451 space
= isl_union_set_get_space(filter
);
3452 isl_union_set_free(filter
);
3453 filter
= isl_union_set_empty(space
);
3454 node
= isl_schedule_node_cut(node
);
3455 node
= isl_schedule_node_insert_filter(node
, filter
);
3458 case isl_schedule_node_context
:
3459 case isl_schedule_node_domain
:
3460 case isl_schedule_node_guard
:
3461 case isl_schedule_node_leaf
:
3462 case isl_schedule_node_mark
:
3469 /* Compute the gist of the subtree at "node" with respect to
3470 * the reaching domain elements in "context".
3471 * In particular, compute the gist of all band and filter nodes
3472 * in the subtree with respect to "context". Children of set or sequence
3473 * nodes that end up with an empty filter are removed completely.
3475 * We keep track of the intersection of "context" with all outer filters
3476 * of the current node within the subtree in the final element of "filters".
3477 * Initially, this list contains the single element "context" and it is
3478 * extended or shortened each time we enter or leave a filter node.
3480 __isl_give isl_schedule_node
*isl_schedule_node_gist(
3481 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*context
)
3483 struct isl_node_gist_data data
;
3485 data
.n_expansion
= 0;
3486 data
.filters
= isl_union_set_list_from_union_set(context
);
3487 node
= traverse(node
, &gist_enter
, &gist_leave
, &data
);
3488 isl_union_set_list_free(data
.filters
);
3492 /* Intersect the domain of domain node "node" with "domain".
3494 * If the domain of "node" is already a subset of "domain",
3495 * then nothing needs to be changed.
3497 * Otherwise, we replace the domain of the domain node by the intersection
3498 * and simplify the subtree rooted at "node" with respect to this intersection.
3500 __isl_give isl_schedule_node
*isl_schedule_node_domain_intersect_domain(
3501 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
)
3503 isl_schedule_tree
*tree
;
3504 isl_union_set
*uset
;
3507 if (!node
|| !domain
)
3510 uset
= isl_schedule_tree_domain_get_domain(node
->tree
);
3511 is_subset
= isl_union_set_is_subset(uset
, domain
);
3512 isl_union_set_free(uset
);
3516 isl_union_set_free(domain
);
3520 tree
= isl_schedule_tree_copy(node
->tree
);
3521 uset
= isl_schedule_tree_domain_get_domain(tree
);
3522 uset
= isl_union_set_intersect(uset
, domain
);
3523 tree
= isl_schedule_tree_domain_set_domain(tree
,
3524 isl_union_set_copy(uset
));
3525 node
= isl_schedule_node_graft_tree(node
, tree
);
3527 node
= isl_schedule_node_child(node
, 0);
3528 node
= isl_schedule_node_gist(node
, uset
);
3529 node
= isl_schedule_node_parent(node
);
3533 isl_schedule_node_free(node
);
3534 isl_union_set_free(domain
);
3538 /* Replace the domain of domain node "node" with the gist
3539 * of the original domain with respect to the parameter domain "context".
3541 __isl_give isl_schedule_node
*isl_schedule_node_domain_gist_params(
3542 __isl_take isl_schedule_node
*node
, __isl_take isl_set
*context
)
3544 isl_union_set
*domain
;
3545 isl_schedule_tree
*tree
;
3547 if (!node
|| !context
)
3550 tree
= isl_schedule_tree_copy(node
->tree
);
3551 domain
= isl_schedule_tree_domain_get_domain(node
->tree
);
3552 domain
= isl_union_set_gist_params(domain
, context
);
3553 tree
= isl_schedule_tree_domain_set_domain(tree
, domain
);
3554 node
= isl_schedule_node_graft_tree(node
, tree
);
3558 isl_schedule_node_free(node
);
3559 isl_set_free(context
);
3563 /* Internal data structure for isl_schedule_node_get_subtree_expansion.
3564 * "expansions" contains a list of accumulated expansions
3565 * for each outer expansion, set or sequence node. The first element
3566 * in the list is an identity mapping on the reaching domain elements.
3567 * "res" collects the results.
3569 struct isl_subtree_expansion_data
{
3570 isl_union_map_list
*expansions
;
3574 /* Callback for "traverse" to enter a node and to move
3575 * to the deepest initial subtree that should be traversed
3576 * by isl_schedule_node_get_subtree_expansion.
3578 * Whenever we come across an expansion node, the last element
3579 * of data->expansions is combined with the expansion
3580 * on the expansion node.
3582 * Whenever we come across a filter node that is the child
3583 * of a set or sequence node, data->expansions is extended
3584 * with a new element that restricts the previous element
3585 * to the elements selected by the filter.
3586 * The previous element can then be reused while backtracking.
3588 static __isl_give isl_schedule_node
*subtree_expansion_enter(
3589 __isl_take isl_schedule_node
*node
, void *user
)
3591 struct isl_subtree_expansion_data
*data
= user
;
3594 enum isl_schedule_node_type type
;
3595 isl_union_set
*filter
;
3596 isl_union_map
*inner
, *expansion
;
3599 switch (isl_schedule_node_get_type(node
)) {
3600 case isl_schedule_node_error
:
3601 return isl_schedule_node_free(node
);
3602 case isl_schedule_node_filter
:
3603 type
= isl_schedule_node_get_parent_type(node
);
3604 if (type
!= isl_schedule_node_set
&&
3605 type
!= isl_schedule_node_sequence
)
3607 filter
= isl_schedule_node_filter_get_filter(node
);
3608 n
= isl_union_map_list_n_union_map(data
->expansions
);
3610 isl_union_map_list_get_union_map(data
->expansions
,
3612 inner
= isl_union_map_intersect_range(inner
, filter
);
3614 isl_union_map_list_add(data
->expansions
, inner
);
3616 case isl_schedule_node_expansion
:
3617 n
= isl_union_map_list_n_union_map(data
->expansions
);
3619 isl_schedule_node_expansion_get_expansion(node
);
3621 isl_union_map_list_get_union_map(data
->expansions
,
3623 inner
= isl_union_map_apply_range(inner
, expansion
);
3625 isl_union_map_list_set_union_map(data
->expansions
,
3628 case isl_schedule_node_band
:
3629 case isl_schedule_node_context
:
3630 case isl_schedule_node_domain
:
3631 case isl_schedule_node_extension
:
3632 case isl_schedule_node_guard
:
3633 case isl_schedule_node_leaf
:
3634 case isl_schedule_node_mark
:
3635 case isl_schedule_node_sequence
:
3636 case isl_schedule_node_set
:
3639 } while (isl_schedule_node_has_children(node
) &&
3640 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3645 /* Callback for "traverse" to leave a node for
3646 * isl_schedule_node_get_subtree_expansion.
3648 * If we come across a filter node that is the child
3649 * of a set or sequence node, then we remove the element
3650 * of data->expansions that was added in subtree_expansion_enter.
3652 * If we reach a leaf node, then the accumulated expansion is
3653 * added to data->res.
3655 static __isl_give isl_schedule_node
*subtree_expansion_leave(
3656 __isl_take isl_schedule_node
*node
, void *user
)
3658 struct isl_subtree_expansion_data
*data
= user
;
3660 isl_union_map
*inner
;
3661 enum isl_schedule_node_type type
;
3663 switch (isl_schedule_node_get_type(node
)) {
3664 case isl_schedule_node_error
:
3665 return isl_schedule_node_free(node
);
3666 case isl_schedule_node_filter
:
3667 type
= isl_schedule_node_get_parent_type(node
);
3668 if (type
!= isl_schedule_node_set
&&
3669 type
!= isl_schedule_node_sequence
)
3671 n
= isl_union_map_list_n_union_map(data
->expansions
);
3672 data
->expansions
= isl_union_map_list_drop(data
->expansions
,
3675 case isl_schedule_node_leaf
:
3676 n
= isl_union_map_list_n_union_map(data
->expansions
);
3677 inner
= isl_union_map_list_get_union_map(data
->expansions
,
3679 data
->res
= isl_union_map_union(data
->res
, inner
);
3681 case isl_schedule_node_band
:
3682 case isl_schedule_node_context
:
3683 case isl_schedule_node_domain
:
3684 case isl_schedule_node_expansion
:
3685 case isl_schedule_node_extension
:
3686 case isl_schedule_node_guard
:
3687 case isl_schedule_node_mark
:
3688 case isl_schedule_node_sequence
:
3689 case isl_schedule_node_set
:
3696 /* Return a mapping from the domain elements that reach "node"
3697 * to the corresponding domain elements in the leaves of the subtree
3698 * rooted at "node" obtained by composing the intermediate expansions.
3700 * We start out with an identity mapping between the domain elements
3701 * that reach "node" and compose it with all the expansions
3702 * on a path from "node" to a leaf while traversing the subtree.
3703 * Within the children of an a sequence or set node, the
3704 * accumulated expansion is restricted to the elements selected
3705 * by the filter child.
3707 __isl_give isl_union_map
*isl_schedule_node_get_subtree_expansion(
3708 __isl_keep isl_schedule_node
*node
)
3710 struct isl_subtree_expansion_data data
;
3712 isl_union_set
*domain
;
3713 isl_union_map
*expansion
;
3718 domain
= isl_schedule_node_get_universe_domain(node
);
3719 space
= isl_union_set_get_space(domain
);
3720 expansion
= isl_union_set_identity(domain
);
3721 data
.res
= isl_union_map_empty(space
);
3722 data
.expansions
= isl_union_map_list_from_union_map(expansion
);
3724 node
= isl_schedule_node_copy(node
);
3725 node
= traverse(node
, &subtree_expansion_enter
,
3726 &subtree_expansion_leave
, &data
);
3728 data
.res
= isl_union_map_free(data
.res
);
3729 isl_schedule_node_free(node
);
3731 isl_union_map_list_free(data
.expansions
);
3736 /* Internal data structure for isl_schedule_node_get_subtree_contraction.
3737 * "contractions" contains a list of accumulated contractions
3738 * for each outer expansion, set or sequence node. The first element
3739 * in the list is an identity mapping on the reaching domain elements.
3740 * "res" collects the results.
3742 struct isl_subtree_contraction_data
{
3743 isl_union_pw_multi_aff_list
*contractions
;
3744 isl_union_pw_multi_aff
*res
;
3747 /* Callback for "traverse" to enter a node and to move
3748 * to the deepest initial subtree that should be traversed
3749 * by isl_schedule_node_get_subtree_contraction.
3751 * Whenever we come across an expansion node, the last element
3752 * of data->contractions is combined with the contraction
3753 * on the expansion node.
3755 * Whenever we come across a filter node that is the child
3756 * of a set or sequence node, data->contractions is extended
3757 * with a new element that restricts the previous element
3758 * to the elements selected by the filter.
3759 * The previous element can then be reused while backtracking.
3761 static __isl_give isl_schedule_node
*subtree_contraction_enter(
3762 __isl_take isl_schedule_node
*node
, void *user
)
3764 struct isl_subtree_contraction_data
*data
= user
;
3767 enum isl_schedule_node_type type
;
3768 isl_union_set
*filter
;
3769 isl_union_pw_multi_aff
*inner
, *contraction
;
3772 switch (isl_schedule_node_get_type(node
)) {
3773 case isl_schedule_node_error
:
3774 return isl_schedule_node_free(node
);
3775 case isl_schedule_node_filter
:
3776 type
= isl_schedule_node_get_parent_type(node
);
3777 if (type
!= isl_schedule_node_set
&&
3778 type
!= isl_schedule_node_sequence
)
3780 filter
= isl_schedule_node_filter_get_filter(node
);
3781 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3782 data
->contractions
);
3784 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3785 data
->contractions
, n
- 1);
3786 inner
= isl_union_pw_multi_aff_intersect_domain(inner
,
3788 data
->contractions
=
3789 isl_union_pw_multi_aff_list_add(data
->contractions
,
3792 case isl_schedule_node_expansion
:
3793 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3794 data
->contractions
);
3796 isl_schedule_node_expansion_get_contraction(node
);
3798 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3799 data
->contractions
, n
- 1);
3801 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3802 inner
, contraction
);
3803 data
->contractions
=
3804 isl_union_pw_multi_aff_list_set_union_pw_multi_aff(
3805 data
->contractions
, n
- 1, inner
);
3807 case isl_schedule_node_band
:
3808 case isl_schedule_node_context
:
3809 case isl_schedule_node_domain
:
3810 case isl_schedule_node_extension
:
3811 case isl_schedule_node_guard
:
3812 case isl_schedule_node_leaf
:
3813 case isl_schedule_node_mark
:
3814 case isl_schedule_node_sequence
:
3815 case isl_schedule_node_set
:
3818 } while (isl_schedule_node_has_children(node
) &&
3819 (node
= isl_schedule_node_first_child(node
)) != NULL
);
3824 /* Callback for "traverse" to leave a node for
3825 * isl_schedule_node_get_subtree_contraction.
3827 * If we come across a filter node that is the child
3828 * of a set or sequence node, then we remove the element
3829 * of data->contractions that was added in subtree_contraction_enter.
3831 * If we reach a leaf node, then the accumulated contraction is
3832 * added to data->res.
3834 static __isl_give isl_schedule_node
*subtree_contraction_leave(
3835 __isl_take isl_schedule_node
*node
, void *user
)
3837 struct isl_subtree_contraction_data
*data
= user
;
3839 isl_union_pw_multi_aff
*inner
;
3840 enum isl_schedule_node_type type
;
3842 switch (isl_schedule_node_get_type(node
)) {
3843 case isl_schedule_node_error
:
3844 return isl_schedule_node_free(node
);
3845 case isl_schedule_node_filter
:
3846 type
= isl_schedule_node_get_parent_type(node
);
3847 if (type
!= isl_schedule_node_set
&&
3848 type
!= isl_schedule_node_sequence
)
3850 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3851 data
->contractions
);
3852 data
->contractions
=
3853 isl_union_pw_multi_aff_list_drop(data
->contractions
,
3856 case isl_schedule_node_leaf
:
3857 n
= isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3858 data
->contractions
);
3859 inner
= isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3860 data
->contractions
, n
- 1);
3861 data
->res
= isl_union_pw_multi_aff_union_add(data
->res
, inner
);
3863 case isl_schedule_node_band
:
3864 case isl_schedule_node_context
:
3865 case isl_schedule_node_domain
:
3866 case isl_schedule_node_expansion
:
3867 case isl_schedule_node_extension
:
3868 case isl_schedule_node_guard
:
3869 case isl_schedule_node_mark
:
3870 case isl_schedule_node_sequence
:
3871 case isl_schedule_node_set
:
3878 /* Return a mapping from the domain elements in the leaves of the subtree
3879 * rooted at "node" to the corresponding domain elements that reach "node"
3880 * obtained by composing the intermediate contractions.
3882 * We start out with an identity mapping between the domain elements
3883 * that reach "node" and compose it with all the contractions
3884 * on a path from "node" to a leaf while traversing the subtree.
3885 * Within the children of an a sequence or set node, the
3886 * accumulated contraction is restricted to the elements selected
3887 * by the filter child.
3889 __isl_give isl_union_pw_multi_aff
*isl_schedule_node_get_subtree_contraction(
3890 __isl_keep isl_schedule_node
*node
)
3892 struct isl_subtree_contraction_data data
;
3894 isl_union_set
*domain
;
3895 isl_union_pw_multi_aff
*contraction
;
3900 domain
= isl_schedule_node_get_universe_domain(node
);
3901 space
= isl_union_set_get_space(domain
);
3902 contraction
= isl_union_set_identity_union_pw_multi_aff(domain
);
3903 data
.res
= isl_union_pw_multi_aff_empty(space
);
3905 isl_union_pw_multi_aff_list_from_union_pw_multi_aff(contraction
);
3907 node
= isl_schedule_node_copy(node
);
3908 node
= traverse(node
, &subtree_contraction_enter
,
3909 &subtree_contraction_leave
, &data
);
3911 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
3912 isl_schedule_node_free(node
);
3914 isl_union_pw_multi_aff_list_free(data
.contractions
);
3919 /* Do the nearest "n" ancestors of "node" have the types given in "types"
3920 * (starting at the parent of "node")?
3922 static int has_ancestors(__isl_keep isl_schedule_node
*node
,
3923 int n
, enum isl_schedule_node_type
*types
)
3930 n_ancestor
= isl_schedule_tree_list_n_schedule_tree(node
->ancestors
);
3934 for (i
= 0; i
< n
; ++i
) {
3935 isl_schedule_tree
*tree
;
3938 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
,
3939 n_ancestor
- 1 - i
);
3942 correct_type
= isl_schedule_tree_get_type(tree
) == types
[i
];
3943 isl_schedule_tree_free(tree
);
3951 /* Given a node "node" that appears in an extension (i.e., it is the child
3952 * of a filter in a sequence inside an extension node), are the spaces
3953 * of the extension specified by "extension" disjoint from those
3954 * of both the original extension and the domain elements that reach
3955 * that original extension?
3957 static int is_disjoint_extension(__isl_keep isl_schedule_node
*node
,
3958 __isl_keep isl_union_map
*extension
)
3961 isl_union_set
*domain
;
3964 node
= isl_schedule_node_copy(node
);
3965 node
= isl_schedule_node_parent(node
);
3966 node
= isl_schedule_node_parent(node
);
3967 node
= isl_schedule_node_parent(node
);
3968 old
= isl_schedule_node_extension_get_extension(node
);
3969 domain
= isl_schedule_node_get_universe_domain(node
);
3970 isl_schedule_node_free(node
);
3971 old
= isl_union_map_universe(old
);
3972 domain
= isl_union_set_union(domain
, isl_union_map_range(old
));
3973 extension
= isl_union_map_copy(extension
);
3974 extension
= isl_union_map_intersect_range(extension
, domain
);
3975 empty
= isl_union_map_is_empty(extension
);
3976 isl_union_map_free(extension
);
3981 /* Given a node "node" that is governed by an extension node, extend
3982 * that extension node with "extension".
3984 * In particular, "node" is the child of a filter in a sequence that
3985 * is in turn a child of an extension node. Extend that extension node
3988 * Return a pointer to the parent of the original node (i.e., a filter).
3990 static __isl_give isl_schedule_node
*extend_extension(
3991 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
3995 isl_union_map
*node_extension
;
3997 node
= isl_schedule_node_parent(node
);
3998 pos
= isl_schedule_node_get_child_position(node
);
3999 node
= isl_schedule_node_parent(node
);
4000 node
= isl_schedule_node_parent(node
);
4001 node_extension
= isl_schedule_node_extension_get_extension(node
);
4002 disjoint
= isl_union_map_is_disjoint(extension
, node_extension
);
4003 extension
= isl_union_map_union(extension
, node_extension
);
4004 node
= isl_schedule_node_extension_set_extension(node
, extension
);
4005 node
= isl_schedule_node_child(node
, 0);
4006 node
= isl_schedule_node_child(node
, pos
);
4009 return isl_schedule_node_free(node
);
4013 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4014 "extension domain should be disjoint from earlier "
4015 "extensions", return isl_schedule_node_free(node
));
4020 /* Return the universe of "uset" if this universe is disjoint from "ref".
4021 * Otherwise, return "uset".
4023 * Also check if "uset" itself is disjoint from "ref", reporting
4024 * an error if it is not.
4026 static __isl_give isl_union_set
*replace_by_universe_if_disjoint(
4027 __isl_take isl_union_set
*uset
, __isl_keep isl_union_set
*ref
)
4030 isl_union_set
*universe
;
4032 disjoint
= isl_union_set_is_disjoint(uset
, ref
);
4034 return isl_union_set_free(uset
);
4036 isl_die(isl_union_set_get_ctx(uset
), isl_error_invalid
,
4037 "extension domain should be disjoint from "
4038 "current domain", return isl_union_set_free(uset
));
4040 universe
= isl_union_set_universe(isl_union_set_copy(uset
));
4041 disjoint
= isl_union_set_is_disjoint(universe
, ref
);
4042 if (disjoint
>= 0 && disjoint
) {
4043 isl_union_set_free(uset
);
4046 isl_union_set_free(universe
);
4049 return isl_union_set_free(uset
);
4053 /* Insert an extension node on top of "node" with extension "extension".
4054 * In addition, insert a filter that separates node from the extension
4055 * between the extension node and "node".
4056 * Return a pointer to the inserted filter node.
4058 * If "node" already appears in an extension (i.e., if it is the child
4059 * of a filter in a sequence inside an extension node), then extend that
4060 * extension with "extension" instead.
4061 * In this case, a pointer to the original filter node is returned.
4062 * Note that if some of the elements in the new extension live in the
4063 * same space as those of the original extension or the domain elements
4064 * reaching the original extension, then we insert a new extension anyway.
4065 * Otherwise, we would have to adjust the filters in the sequence child
4066 * of the extension to ensure that the elements in the new extension
4069 static __isl_give isl_schedule_node
*insert_extension(
4070 __isl_take isl_schedule_node
*node
, __isl_take isl_union_map
*extension
)
4072 enum isl_schedule_node_type ancestors
[] =
4073 { isl_schedule_node_filter
, isl_schedule_node_sequence
,
4074 isl_schedule_node_extension
};
4075 isl_union_set
*domain
;
4076 isl_union_set
*filter
;
4079 in_ext
= has_ancestors(node
, 3, ancestors
);
4085 disjoint
= is_disjoint_extension(node
, extension
);
4089 return extend_extension(node
, extension
);
4092 filter
= isl_schedule_node_get_domain(node
);
4093 domain
= isl_union_map_range(isl_union_map_copy(extension
));
4094 filter
= replace_by_universe_if_disjoint(filter
, domain
);
4095 isl_union_set_free(domain
);
4097 node
= isl_schedule_node_insert_filter(node
, filter
);
4098 node
= isl_schedule_node_insert_extension(node
, extension
);
4099 node
= isl_schedule_node_child(node
, 0);
4102 isl_schedule_node_free(node
);
4103 isl_union_map_free(extension
);
4107 /* Replace the subtree that "node" points to by "tree" (which has
4108 * a sequence root with two children), except if the parent of "node"
4109 * is a sequence as well, in which case "tree" is spliced at the position
4110 * of "node" in its parent.
4111 * Return a pointer to the child of the "tree_pos" (filter) child of "tree"
4112 * in the updated schedule tree.
4114 static __isl_give isl_schedule_node
*graft_or_splice(
4115 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_tree
*tree
,
4120 if (isl_schedule_node_get_parent_type(node
) ==
4121 isl_schedule_node_sequence
) {
4122 pos
= isl_schedule_node_get_child_position(node
);
4123 node
= isl_schedule_node_parent(node
);
4124 node
= isl_schedule_node_sequence_splice(node
, pos
, tree
);
4127 node
= isl_schedule_node_graft_tree(node
, tree
);
4129 node
= isl_schedule_node_child(node
, pos
+ tree_pos
);
4130 node
= isl_schedule_node_child(node
, 0);
4135 /* Insert a node "graft" into the schedule tree of "node" such that it
4136 * is executed before (if "before" is set) or after (if "before" is not set)
4137 * the node that "node" points to.
4138 * The root of "graft" is an extension node.
4139 * Return a pointer to the node that "node" pointed to.
4141 * We first insert an extension node on top of "node" (or extend
4142 * the extension node if there already is one), with a filter on "node"
4143 * separating it from the extension.
4144 * We then insert a filter in the graft to separate it from the original
4145 * domain elements and combine the original and new tree in a sequence.
4146 * If we have extended an extension node, then the children of this
4147 * sequence are spliced in the sequence of the extended extension
4148 * at the position where "node" appears in the original extension.
4149 * Otherwise, the sequence pair is attached to the new extension node.
4151 static __isl_give isl_schedule_node
*graft_extension(
4152 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
,
4155 isl_union_map
*extension
;
4156 isl_union_set
*graft_domain
;
4157 isl_union_set
*node_domain
;
4158 isl_schedule_tree
*tree
, *tree_graft
;
4160 extension
= isl_schedule_node_extension_get_extension(graft
);
4161 graft_domain
= isl_union_map_range(isl_union_map_copy(extension
));
4162 node_domain
= isl_schedule_node_get_universe_domain(node
);
4163 node
= insert_extension(node
, extension
);
4165 graft_domain
= replace_by_universe_if_disjoint(graft_domain
,
4167 isl_union_set_free(node_domain
);
4169 tree
= isl_schedule_node_get_tree(node
);
4170 if (!isl_schedule_node_has_children(graft
)) {
4171 tree_graft
= isl_schedule_tree_from_filter(graft_domain
);
4173 graft
= isl_schedule_node_child(graft
, 0);
4174 tree_graft
= isl_schedule_node_get_tree(graft
);
4175 tree_graft
= isl_schedule_tree_insert_filter(tree_graft
,
4179 tree
= isl_schedule_tree_sequence_pair(tree_graft
, tree
);
4181 tree
= isl_schedule_tree_sequence_pair(tree
, tree_graft
);
4182 node
= graft_or_splice(node
, tree
, before
);
4184 isl_schedule_node_free(graft
);
4189 /* Replace the root domain node of "node" by an extension node suitable
4190 * for insertion at "pos".
4191 * That is, create an extension node that maps the outer band nodes
4192 * at "pos" to the domain of the root node of "node" and attach
4193 * the child of this root node to the extension node.
4195 static __isl_give isl_schedule_node
*extension_from_domain(
4196 __isl_take isl_schedule_node
*node
, __isl_keep isl_schedule_node
*pos
)
4198 isl_union_set
*universe
;
4199 isl_union_set
*domain
;
4204 isl_schedule_node
*res
;
4205 isl_schedule_tree
*tree
;
4207 anchored
= isl_schedule_node_is_subtree_anchored(node
);
4209 return isl_schedule_node_free(node
);
4211 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
4212 "cannot graft anchored tree with domain root",
4213 return isl_schedule_node_free(node
));
4215 depth
= isl_schedule_node_get_schedule_depth(pos
);
4216 domain
= isl_schedule_node_domain_get_domain(node
);
4217 space
= isl_union_set_get_space(domain
);
4218 space
= isl_space_set_from_params(space
);
4219 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
4220 universe
= isl_union_set_from_set(isl_set_universe(space
));
4221 ext
= isl_union_map_from_domain_and_range(universe
, domain
);
4222 res
= isl_schedule_node_from_extension(ext
);
4223 node
= isl_schedule_node_child(node
, 0);
4225 return isl_schedule_node_free(res
);
4226 if (!isl_schedule_tree_is_leaf(node
->tree
)) {
4227 tree
= isl_schedule_node_get_tree(node
);
4228 res
= isl_schedule_node_child(res
, 0);
4229 res
= isl_schedule_node_graft_tree(res
, tree
);
4230 res
= isl_schedule_node_parent(res
);
4232 isl_schedule_node_free(node
);
4237 /* Insert a node "graft" into the schedule tree of "node" such that it
4238 * is executed before (if "before" is set) or after (if "before" is not set)
4239 * the node that "node" points to.
4240 * The root of "graft" may be either a domain or an extension node.
4241 * In the latter case, the domain of the extension needs to correspond
4242 * to the outer band nodes of "node".
4243 * The elements of the domain or the range of the extension may not
4244 * intersect with the domain elements that reach "node".
4245 * The schedule tree of "graft" may not be anchored.
4247 * The schedule tree of "node" is modified to include an extension node
4248 * corresponding to the root node of "graft" as a child of the original
4249 * parent of "node". The original node that "node" points to and the
4250 * child of the root node of "graft" are attached to this extension node
4251 * through a sequence, with appropriate filters and with the child
4252 * of "graft" appearing before or after the original "node".
4254 * If "node" already appears inside a sequence that is the child of
4255 * an extension node and if the spaces of the new domain elements
4256 * do not overlap with those of the original domain elements,
4257 * then that extension node is extended with the new extension
4258 * rather than introducing a new segment of extension and sequence nodes.
4260 * Return a pointer to the same node in the modified tree that
4261 * "node" pointed to in the original tree.
4263 static __isl_give isl_schedule_node
*isl_schedule_node_graft_before_or_after(
4264 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
,
4267 if (!node
|| !graft
)
4269 if (check_insert(node
) < 0)
4272 if (isl_schedule_node_get_type(graft
) == isl_schedule_node_domain
)
4273 graft
= extension_from_domain(graft
, node
);
4275 if (isl_schedule_node_get_type(graft
) != isl_schedule_node_extension
)
4276 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4277 "expecting domain or extension as root of graft",
4280 return graft_extension(node
, graft
, before
);
4282 isl_schedule_node_free(node
);
4283 isl_schedule_node_free(graft
);
4287 /* Insert a node "graft" into the schedule tree of "node" such that it
4288 * is executed before the node that "node" points to.
4289 * The root of "graft" may be either a domain or an extension node.
4290 * In the latter case, the domain of the extension needs to correspond
4291 * to the outer band nodes of "node".
4292 * The elements of the domain or the range of the extension may not
4293 * intersect with the domain elements that reach "node".
4294 * The schedule tree of "graft" may not be anchored.
4296 * Return a pointer to the same node in the modified tree that
4297 * "node" pointed to in the original tree.
4299 __isl_give isl_schedule_node
*isl_schedule_node_graft_before(
4300 __isl_take isl_schedule_node
*node
, __isl_take isl_schedule_node
*graft
)
4302 return isl_schedule_node_graft_before_or_after(node
, graft
, 1);
4305 /* Insert a node "graft" into the schedule tree of "node" such that it
4306 * is executed after the node that "node" points to.
4307 * The root of "graft" may be either a domain or an extension node.
4308 * In the latter case, the domain of the extension needs to correspond
4309 * to the outer band nodes of "node".
4310 * The elements of the domain or the range of the extension may not
4311 * intersect with the domain elements that reach "node".
4312 * The schedule tree of "graft" may not be anchored.
4314 * Return a pointer to the same node in the modified tree that
4315 * "node" pointed to in the original tree.
4317 __isl_give isl_schedule_node
*isl_schedule_node_graft_after(
4318 __isl_take isl_schedule_node
*node
,
4319 __isl_take isl_schedule_node
*graft
)
4321 return isl_schedule_node_graft_before_or_after(node
, graft
, 0);
4324 /* Split the domain elements that reach "node" into those that satisfy
4325 * "filter" and those that do not. Arrange for the first subset to be
4326 * executed before or after the second subset, depending on the value
4328 * Return a pointer to the tree corresponding to the second subset,
4329 * except when this subset is empty in which case the original pointer
4331 * If both subsets are non-empty, then a sequence node is introduced
4332 * to impose the order. If the grandparent of the original node was
4333 * itself a sequence, then the original child is replaced by two children
4334 * in this sequence instead.
4335 * The children in the sequence are copies of the original subtree,
4336 * simplified with respect to their filters.
4338 static __isl_give isl_schedule_node
*isl_schedule_node_order_before_or_after(
4339 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
,
4342 enum isl_schedule_node_type ancestors
[] =
4343 { isl_schedule_node_filter
, isl_schedule_node_sequence
};
4344 isl_union_set
*node_domain
, *node_filter
= NULL
, *parent_filter
;
4345 isl_schedule_node
*node2
;
4346 isl_schedule_tree
*tree1
, *tree2
;
4350 if (!node
|| !filter
)
4352 if (check_insert(node
) < 0)
4355 in_seq
= has_ancestors(node
, 2, ancestors
);
4358 node_domain
= isl_schedule_node_get_domain(node
);
4359 filter
= isl_union_set_gist(filter
, isl_union_set_copy(node_domain
));
4360 node_filter
= isl_union_set_copy(node_domain
);
4361 node_filter
= isl_union_set_subtract(node_filter
,
4362 isl_union_set_copy(filter
));
4363 node_filter
= isl_union_set_gist(node_filter
, node_domain
);
4364 empty1
= isl_union_set_is_empty(filter
);
4365 empty2
= isl_union_set_is_empty(node_filter
);
4366 if (empty1
< 0 || empty2
< 0)
4368 if (empty1
|| empty2
) {
4369 isl_union_set_free(filter
);
4370 isl_union_set_free(node_filter
);
4375 node
= isl_schedule_node_parent(node
);
4376 parent_filter
= isl_schedule_node_filter_get_filter(node
);
4377 node_filter
= isl_union_set_intersect(node_filter
,
4378 isl_union_set_copy(parent_filter
));
4379 filter
= isl_union_set_intersect(filter
, parent_filter
);
4382 node2
= isl_schedule_node_copy(node
);
4383 node
= isl_schedule_node_gist(node
, isl_union_set_copy(node_filter
));
4384 node2
= isl_schedule_node_gist(node2
, isl_union_set_copy(filter
));
4385 tree1
= isl_schedule_node_get_tree(node
);
4386 tree2
= isl_schedule_node_get_tree(node2
);
4387 tree1
= isl_schedule_tree_insert_filter(tree1
, node_filter
);
4388 tree2
= isl_schedule_tree_insert_filter(tree2
, filter
);
4389 isl_schedule_node_free(node2
);
4392 tree1
= isl_schedule_tree_sequence_pair(tree2
, tree1
);
4393 node
= graft_or_splice(node
, tree1
, 1);
4395 tree1
= isl_schedule_tree_sequence_pair(tree1
, tree2
);
4396 node
= graft_or_splice(node
, tree1
, 0);
4401 isl_schedule_node_free(node
);
4402 isl_union_set_free(filter
);
4403 isl_union_set_free(node_filter
);
4407 /* Split the domain elements that reach "node" into those that satisfy
4408 * "filter" and those that do not. Arrange for the first subset to be
4409 * executed before the second subset.
4410 * Return a pointer to the tree corresponding to the second subset,
4411 * except when this subset is empty in which case the original pointer
4414 __isl_give isl_schedule_node
*isl_schedule_node_order_before(
4415 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
4417 return isl_schedule_node_order_before_or_after(node
, filter
, 1);
4420 /* Split the domain elements that reach "node" into those that satisfy
4421 * "filter" and those that do not. Arrange for the first subset to be
4422 * executed after the second subset.
4423 * Return a pointer to the tree corresponding to the second subset,
4424 * except when this subset is empty in which case the original pointer
4427 __isl_give isl_schedule_node
*isl_schedule_node_order_after(
4428 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*filter
)
4430 return isl_schedule_node_order_before_or_after(node
, filter
, 0);
4433 /* Reset the user pointer on all identifiers of parameters and tuples
4434 * in the schedule node "node".
4436 __isl_give isl_schedule_node
*isl_schedule_node_reset_user(
4437 __isl_take isl_schedule_node
*node
)
4439 isl_schedule_tree
*tree
;
4441 tree
= isl_schedule_node_get_tree(node
);
4442 tree
= isl_schedule_tree_reset_user(tree
);
4443 node
= isl_schedule_node_graft_tree(node
, tree
);
4448 /* Align the parameters of the schedule node "node" to those of "space".
4450 __isl_give isl_schedule_node
*isl_schedule_node_align_params(
4451 __isl_take isl_schedule_node
*node
, __isl_take isl_space
*space
)
4453 isl_schedule_tree
*tree
;
4455 tree
= isl_schedule_node_get_tree(node
);
4456 tree
= isl_schedule_tree_align_params(tree
, space
);
4457 node
= isl_schedule_node_graft_tree(node
, tree
);
4462 /* Compute the pullback of schedule node "node"
4463 * by the function represented by "upma".
4464 * In other words, plug in "upma" in the iteration domains
4465 * of schedule node "node".
4466 * We currently do not handle expansion nodes.
4468 * Note that this is only a helper function for
4469 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
4470 * this function should not be called on a single node without also
4471 * calling it on all the other nodes.
4473 __isl_give isl_schedule_node
*isl_schedule_node_pullback_union_pw_multi_aff(
4474 __isl_take isl_schedule_node
*node
,
4475 __isl_take isl_union_pw_multi_aff
*upma
)
4477 isl_schedule_tree
*tree
;
4479 tree
= isl_schedule_node_get_tree(node
);
4480 tree
= isl_schedule_tree_pullback_union_pw_multi_aff(tree
, upma
);
4481 node
= isl_schedule_node_graft_tree(node
, tree
);
4486 /* Internal data structure for isl_schedule_node_expand.
4487 * "tree" is the tree that needs to be plugged in in all the leaves.
4488 * "domain" is the set of domain elements in the original leaves
4489 * to which the tree applies.
4491 struct isl_schedule_expand_data
{
4492 isl_schedule_tree
*tree
;
4493 isl_union_set
*domain
;
4496 /* If "node" is a leaf, then plug in data->tree, simplifying it
4497 * within its new context.
4499 * If there are any domain elements at the leaf where the tree
4500 * should not be plugged in (i.e., there are elements not in data->domain)
4501 * then first extend the tree to only apply to the elements in data->domain
4502 * by constructing a set node that selects data->tree for elements
4503 * in data->domain and a leaf for the other elements.
4505 static __isl_give isl_schedule_node
*expand(__isl_take isl_schedule_node
*node
,
4508 struct isl_schedule_expand_data
*data
= user
;
4509 isl_schedule_tree
*tree
, *leaf
;
4510 isl_union_set
*domain
, *left
;
4513 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
4516 domain
= isl_schedule_node_get_domain(node
);
4517 tree
= isl_schedule_tree_copy(data
->tree
);
4519 left
= isl_union_set_copy(domain
);
4520 left
= isl_union_set_subtract(left
, isl_union_set_copy(data
->domain
));
4521 empty
= isl_union_set_is_empty(left
);
4522 if (empty
>= 0 && !empty
) {
4523 leaf
= isl_schedule_node_get_leaf(node
);
4524 leaf
= isl_schedule_tree_insert_filter(leaf
, left
);
4525 left
= isl_union_set_copy(data
->domain
);
4526 tree
= isl_schedule_tree_insert_filter(tree
, left
);
4527 tree
= isl_schedule_tree_set_pair(tree
, leaf
);
4530 node
= isl_schedule_node_free(node
);
4531 isl_union_set_free(left
);
4534 node
= isl_schedule_node_graft_tree(node
, tree
);
4535 node
= isl_schedule_node_gist(node
, domain
);
4540 /* Expand the tree rooted at "node" by extending all leaves
4541 * with an expansion node with as child "tree".
4542 * The expansion is determined by "contraction" and "domain".
4543 * That is, the elements of "domain" are contracted according
4544 * to "contraction". The expansion relation is then the inverse
4545 * of "contraction" with its range intersected with "domain".
4547 * Insert the appropriate expansion node on top of "tree" and
4548 * then plug in the result in all leaves of "node".
4550 __isl_give isl_schedule_node
*isl_schedule_node_expand(
4551 __isl_take isl_schedule_node
*node
,
4552 __isl_take isl_union_pw_multi_aff
*contraction
,
4553 __isl_take isl_union_set
*domain
,
4554 __isl_take isl_schedule_tree
*tree
)
4556 struct isl_schedule_expand_data data
;
4557 isl_union_map
*expansion
;
4558 isl_union_pw_multi_aff
*copy
;
4560 if (!node
|| !contraction
|| !tree
)
4561 node
= isl_schedule_node_free(node
);
4563 copy
= isl_union_pw_multi_aff_copy(contraction
);
4564 expansion
= isl_union_map_from_union_pw_multi_aff(copy
);
4565 expansion
= isl_union_map_reverse(expansion
);
4566 expansion
= isl_union_map_intersect_range(expansion
, domain
);
4567 data
.domain
= isl_union_map_domain(isl_union_map_copy(expansion
));
4569 tree
= isl_schedule_tree_insert_expansion(tree
, contraction
, expansion
);
4572 node
= isl_schedule_node_map_descendant_bottom_up(node
, &expand
, &data
);
4573 isl_union_set_free(data
.domain
);
4574 isl_schedule_tree_free(data
.tree
);
4578 /* Return the position of the subtree containing "node" among the children
4579 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
4580 * In particular, both nodes should point to the same schedule tree.
4582 * Return -1 on error.
4584 int isl_schedule_node_get_ancestor_child_position(
4585 __isl_keep isl_schedule_node
*node
,
4586 __isl_keep isl_schedule_node
*ancestor
)
4589 isl_schedule_tree
*tree
;
4591 if (!node
|| !ancestor
)
4594 if (node
->schedule
!= ancestor
->schedule
)
4595 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4596 "not a descendant", return -1);
4598 n1
= isl_schedule_node_get_tree_depth(ancestor
);
4599 n2
= isl_schedule_node_get_tree_depth(node
);
4602 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4603 "not a descendant", return -1);
4604 tree
= isl_schedule_tree_list_get_schedule_tree(node
->ancestors
, n1
);
4605 isl_schedule_tree_free(tree
);
4606 if (tree
!= ancestor
->tree
)
4607 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
4608 "not a descendant", return -1);
4610 return node
->child_pos
[n1
];
4613 /* Given two nodes that point to the same schedule tree, return their
4614 * closest shared ancestor.
4616 * Since the two nodes point to the same schedule, they share at least
4617 * one ancestor, the root of the schedule. We move down from the root
4618 * to the first ancestor where the respective children have a different
4619 * child position. This is the requested ancestor.
4620 * If there is no ancestor where the children have a different position,
4621 * then one node is an ancestor of the other and then this node is
4622 * the requested ancestor.
4624 __isl_give isl_schedule_node
*isl_schedule_node_get_shared_ancestor(
4625 __isl_keep isl_schedule_node
*node1
,
4626 __isl_keep isl_schedule_node
*node2
)
4630 if (!node1
|| !node2
)
4632 if (node1
->schedule
!= node2
->schedule
)
4633 isl_die(isl_schedule_node_get_ctx(node1
), isl_error_invalid
,
4634 "not part of same schedule", return NULL
);
4635 n1
= isl_schedule_node_get_tree_depth(node1
);
4636 n2
= isl_schedule_node_get_tree_depth(node2
);
4638 return isl_schedule_node_get_shared_ancestor(node2
, node1
);
4640 return isl_schedule_node_copy(node1
);
4641 if (isl_schedule_node_is_equal(node1
, node2
))
4642 return isl_schedule_node_copy(node1
);
4644 for (i
= 0; i
< n1
; ++i
)
4645 if (node1
->child_pos
[i
] != node2
->child_pos
[i
])
4648 node1
= isl_schedule_node_copy(node1
);
4649 return isl_schedule_node_ancestor(node1
, n1
- i
);
4652 /* Print "node" to "p".
4654 __isl_give isl_printer
*isl_printer_print_schedule_node(
4655 __isl_take isl_printer
*p
, __isl_keep isl_schedule_node
*node
)
4658 return isl_printer_free(p
);
4659 return isl_printer_print_schedule_tree_mark(p
, node
->schedule
->root
,
4660 isl_schedule_tree_list_n_schedule_tree(node
->ancestors
),
4664 void isl_schedule_node_dump(__isl_keep isl_schedule_node
*node
)
4667 isl_printer
*printer
;
4672 ctx
= isl_schedule_node_get_ctx(node
);
4673 printer
= isl_printer_to_file(ctx
, stderr
);
4674 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
4675 printer
= isl_printer_print_schedule_node(printer
, node
);
4677 isl_printer_free(printer
);
4680 /* Return a string representation of "node".
4681 * Print the schedule node in block format as it would otherwise
4682 * look identical to the entire schedule.
4684 __isl_give
char *isl_schedule_node_to_str(__isl_keep isl_schedule_node
*node
)
4686 isl_printer
*printer
;
4692 printer
= isl_printer_to_str(isl_schedule_node_get_ctx(node
));
4693 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
4694 printer
= isl_printer_print_schedule_node(printer
, node
);
4695 s
= isl_printer_get_str(printer
);
4696 isl_printer_free(printer
);