isl_arg.c: parse_choice_option: handle "phantom" choice options
[isl.git] / isl_schedule_node.c
blob2540d15ed6a994a1d85014b2ff66d0410051c693
1 /*
2 * Copyright 2013-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl/set.h>
14 #include <isl_schedule_band.h>
15 #include <isl_schedule_private.h>
16 #include <isl_schedule_node_private.h>
18 /* Create a new schedule node in the given schedule, point at the given
19 * tree with given ancestors and child positions.
20 * "child_pos" may be NULL if there are no ancestors.
22 __isl_give isl_schedule_node *isl_schedule_node_alloc(
23 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree,
24 __isl_take isl_schedule_tree_list *ancestors, int *child_pos)
26 isl_ctx *ctx;
27 isl_schedule_node *node;
28 int i, n;
30 if (!schedule || !tree || !ancestors)
31 goto error;
32 n = isl_schedule_tree_list_n_schedule_tree(ancestors);
33 if (n > 0 && !child_pos)
34 goto error;
35 ctx = isl_schedule_get_ctx(schedule);
36 node = isl_calloc_type(ctx, isl_schedule_node);
37 if (!node)
38 goto error;
39 node->ref = 1;
40 node->schedule = schedule;
41 node->tree = tree;
42 node->ancestors = ancestors;
43 node->child_pos = isl_alloc_array(ctx, int, n);
44 if (n && !node->child_pos)
45 return isl_schedule_node_free(node);
46 for (i = 0; i < n; ++i)
47 node->child_pos[i] = child_pos[i];
49 return node;
50 error:
51 isl_schedule_free(schedule);
52 isl_schedule_tree_free(tree);
53 isl_schedule_tree_list_free(ancestors);
54 return NULL;
57 /* Return a pointer to the root of a schedule tree with as single
58 * node a domain node with the given domain.
60 __isl_give isl_schedule_node *isl_schedule_node_from_domain(
61 __isl_take isl_union_set *domain)
63 isl_schedule *schedule;
64 isl_schedule_node *node;
66 schedule = isl_schedule_from_domain(domain);
67 node = isl_schedule_get_root(schedule);
68 isl_schedule_free(schedule);
70 return node;
73 /* Return a pointer to the root of a schedule tree with as single
74 * node a extension node with the given extension.
76 __isl_give isl_schedule_node *isl_schedule_node_from_extension(
77 __isl_take isl_union_map *extension)
79 isl_ctx *ctx;
80 isl_schedule *schedule;
81 isl_schedule_tree *tree;
82 isl_schedule_node *node;
84 if (!extension)
85 return NULL;
87 ctx = isl_union_map_get_ctx(extension);
88 tree = isl_schedule_tree_from_extension(extension);
89 schedule = isl_schedule_from_schedule_tree(ctx, tree);
90 node = isl_schedule_get_root(schedule);
91 isl_schedule_free(schedule);
93 return node;
96 /* Return the isl_ctx to which "node" belongs.
98 isl_ctx *isl_schedule_node_get_ctx(__isl_keep isl_schedule_node *node)
100 return node ? isl_schedule_get_ctx(node->schedule) : NULL;
103 /* Return a pointer to the leaf of the schedule into which "node" points.
105 * Even though these leaves are not reference counted, we still
106 * indicate that this function does not return a copy.
108 __isl_keep isl_schedule_tree *isl_schedule_node_peek_leaf(
109 __isl_keep isl_schedule_node *node)
111 return node ? isl_schedule_peek_leaf(node->schedule) : NULL;
114 /* Return a pointer to the leaf of the schedule into which "node" points.
116 * Even though these leaves are not reference counted, we still
117 * return a "copy" of the leaf here such that it can still be "freed"
118 * by the user.
120 __isl_give isl_schedule_tree *isl_schedule_node_get_leaf(
121 __isl_keep isl_schedule_node *node)
123 return isl_schedule_tree_copy(isl_schedule_node_peek_leaf(node));
126 /* Return the type of the node or isl_schedule_node_error on error.
128 enum isl_schedule_node_type isl_schedule_node_get_type(
129 __isl_keep isl_schedule_node *node)
131 return node ? isl_schedule_tree_get_type(node->tree)
132 : isl_schedule_node_error;
135 /* Return the type of the parent of "node" or isl_schedule_node_error on error.
137 enum isl_schedule_node_type isl_schedule_node_get_parent_type(
138 __isl_keep isl_schedule_node *node)
140 int pos;
141 int has_parent;
142 isl_schedule_tree *parent;
143 enum isl_schedule_node_type type;
145 if (!node)
146 return isl_schedule_node_error;
147 has_parent = isl_schedule_node_has_parent(node);
148 if (has_parent < 0)
149 return isl_schedule_node_error;
150 if (!has_parent)
151 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
152 "node has no parent", return isl_schedule_node_error);
154 pos = isl_schedule_tree_list_n_schedule_tree(node->ancestors) - 1;
155 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors, pos);
156 type = isl_schedule_tree_get_type(parent);
157 isl_schedule_tree_free(parent);
159 return type;
162 /* Return a copy of the subtree that this node points to.
164 __isl_give isl_schedule_tree *isl_schedule_node_get_tree(
165 __isl_keep isl_schedule_node *node)
167 if (!node)
168 return NULL;
170 return isl_schedule_tree_copy(node->tree);
173 /* Return a copy of the schedule into which "node" points.
175 __isl_give isl_schedule *isl_schedule_node_get_schedule(
176 __isl_keep isl_schedule_node *node)
178 if (!node)
179 return NULL;
180 return isl_schedule_copy(node->schedule);
183 /* Return a fresh copy of "node".
185 __isl_take isl_schedule_node *isl_schedule_node_dup(
186 __isl_keep isl_schedule_node *node)
188 if (!node)
189 return NULL;
191 return isl_schedule_node_alloc(isl_schedule_copy(node->schedule),
192 isl_schedule_tree_copy(node->tree),
193 isl_schedule_tree_list_copy(node->ancestors),
194 node->child_pos);
197 /* Return an isl_schedule_node that is equal to "node" and that has only
198 * a single reference.
200 __isl_give isl_schedule_node *isl_schedule_node_cow(
201 __isl_take isl_schedule_node *node)
203 if (!node)
204 return NULL;
206 if (node->ref == 1)
207 return node;
208 node->ref--;
209 return isl_schedule_node_dup(node);
212 /* Return a new reference to "node".
214 __isl_give isl_schedule_node *isl_schedule_node_copy(
215 __isl_keep isl_schedule_node *node)
217 if (!node)
218 return NULL;
220 node->ref++;
221 return node;
224 /* Free "node" and return NULL.
226 * Since the node may point to a leaf of its schedule, which
227 * point to a field inside the schedule, we need to make sure
228 * we free the tree before freeing the schedule.
230 __isl_null isl_schedule_node *isl_schedule_node_free(
231 __isl_take isl_schedule_node *node)
233 if (!node)
234 return NULL;
235 if (--node->ref > 0)
236 return NULL;
238 isl_schedule_tree_list_free(node->ancestors);
239 free(node->child_pos);
240 isl_schedule_tree_free(node->tree);
241 isl_schedule_free(node->schedule);
242 free(node);
244 return NULL;
247 /* Do "node1" and "node2" point to the same position in the same
248 * schedule?
250 isl_bool isl_schedule_node_is_equal(__isl_keep isl_schedule_node *node1,
251 __isl_keep isl_schedule_node *node2)
253 int i, n1, n2;
255 if (!node1 || !node2)
256 return isl_bool_error;
257 if (node1 == node2)
258 return isl_bool_true;
259 if (node1->schedule != node2->schedule)
260 return isl_bool_false;
262 n1 = isl_schedule_node_get_tree_depth(node1);
263 n2 = isl_schedule_node_get_tree_depth(node2);
264 if (n1 != n2)
265 return isl_bool_false;
266 for (i = 0; i < n1; ++i)
267 if (node1->child_pos[i] != node2->child_pos[i])
268 return isl_bool_false;
270 return isl_bool_true;
273 /* Return the number of outer schedule dimensions of "node"
274 * in its schedule tree.
276 * Return -1 on error.
278 int isl_schedule_node_get_schedule_depth(__isl_keep isl_schedule_node *node)
280 int i, n;
281 int depth = 0;
283 if (!node)
284 return -1;
286 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
287 for (i = n - 1; i >= 0; --i) {
288 isl_schedule_tree *tree;
290 tree = isl_schedule_tree_list_get_schedule_tree(
291 node->ancestors, i);
292 if (!tree)
293 return -1;
294 if (tree->type == isl_schedule_node_band)
295 depth += isl_schedule_tree_band_n_member(tree);
296 isl_schedule_tree_free(tree);
299 return depth;
302 /* Internal data structure for
303 * isl_schedule_node_get_prefix_schedule_union_pw_multi_aff
305 * "initialized" is set if the filter field has been initialized.
306 * If "universe_domain" is not set, then the collected filter is intersected
307 * with the the domain of the root domain node.
308 * "universe_filter" is set if we are only collecting the universes of filters
309 * "collect_prefix" is set if we are collecting prefixes.
310 * "filter" collects all outer filters and is NULL until "initialized" is set.
311 * "prefix" collects all outer band partial schedules (if "collect_prefix"
312 * is set). If it is used, then it is initialized by the caller
313 * of collect_filter_prefix to a zero-dimensional function.
315 struct isl_schedule_node_get_filter_prefix_data {
316 int initialized;
317 int universe_domain;
318 int universe_filter;
319 int collect_prefix;
320 isl_union_set *filter;
321 isl_multi_union_pw_aff *prefix;
324 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
325 int n, struct isl_schedule_node_get_filter_prefix_data *data);
327 /* Update the filter and prefix information in "data" based on the first "n"
328 * elements in "list" and the expansion tree root "tree".
330 * We first collect the information from the elements in "list",
331 * initializing the filter based on the domain of the expansion.
332 * Then we map the results to the expanded space and combined them
333 * with the results already in "data".
335 static int collect_filter_prefix_expansion(__isl_take isl_schedule_tree *tree,
336 __isl_keep isl_schedule_tree_list *list, int n,
337 struct isl_schedule_node_get_filter_prefix_data *data)
339 struct isl_schedule_node_get_filter_prefix_data contracted;
340 isl_union_pw_multi_aff *c;
341 isl_union_map *exp, *universe;
342 isl_union_set *filter;
344 c = isl_schedule_tree_expansion_get_contraction(tree);
345 exp = isl_schedule_tree_expansion_get_expansion(tree);
347 contracted.initialized = 1;
348 contracted.universe_domain = data->universe_domain;
349 contracted.universe_filter = data->universe_filter;
350 contracted.collect_prefix = data->collect_prefix;
351 universe = isl_union_map_universe(isl_union_map_copy(exp));
352 filter = isl_union_map_domain(universe);
353 if (data->collect_prefix) {
354 isl_space *space = isl_union_set_get_space(filter);
355 space = isl_space_set_from_params(space);
356 contracted.prefix = isl_multi_union_pw_aff_zero(space);
358 contracted.filter = filter;
360 if (collect_filter_prefix(list, n, &contracted) < 0)
361 contracted.filter = isl_union_set_free(contracted.filter);
362 if (data->collect_prefix) {
363 isl_multi_union_pw_aff *prefix;
365 prefix = contracted.prefix;
366 prefix =
367 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix,
368 isl_union_pw_multi_aff_copy(c));
369 data->prefix = isl_multi_union_pw_aff_flat_range_product(
370 prefix, data->prefix);
372 filter = contracted.filter;
373 if (data->universe_domain)
374 filter = isl_union_set_preimage_union_pw_multi_aff(filter,
375 isl_union_pw_multi_aff_copy(c));
376 else
377 filter = isl_union_set_apply(filter, isl_union_map_copy(exp));
378 if (!data->initialized)
379 data->filter = filter;
380 else
381 data->filter = isl_union_set_intersect(filter, data->filter);
382 data->initialized = 1;
384 isl_union_pw_multi_aff_free(c);
385 isl_union_map_free(exp);
386 isl_schedule_tree_free(tree);
388 return 0;
391 /* Update the filter information in "data" based on the first "n"
392 * elements in "list" and the extension tree root "tree", in case
393 * data->universe_domain is set and data->collect_prefix is not.
395 * We collect the universe domain of the elements in "list" and
396 * add it to the universe range of the extension (intersected
397 * with the already collected filter, if any).
399 static int collect_universe_domain_extension(__isl_take isl_schedule_tree *tree,
400 __isl_keep isl_schedule_tree_list *list, int n,
401 struct isl_schedule_node_get_filter_prefix_data *data)
403 struct isl_schedule_node_get_filter_prefix_data data_outer;
404 isl_union_map *extension;
405 isl_union_set *filter;
407 data_outer.initialized = 0;
408 data_outer.universe_domain = 1;
409 data_outer.universe_filter = data->universe_filter;
410 data_outer.collect_prefix = 0;
411 data_outer.filter = NULL;
412 data_outer.prefix = NULL;
414 if (collect_filter_prefix(list, n, &data_outer) < 0)
415 data_outer.filter = isl_union_set_free(data_outer.filter);
417 extension = isl_schedule_tree_extension_get_extension(tree);
418 extension = isl_union_map_universe(extension);
419 filter = isl_union_map_range(extension);
420 if (data_outer.initialized)
421 filter = isl_union_set_union(filter, data_outer.filter);
422 if (data->initialized)
423 filter = isl_union_set_intersect(filter, data->filter);
425 data->filter = filter;
427 isl_schedule_tree_free(tree);
429 return 0;
432 /* Update "data" based on the tree node "tree" in case "data" has
433 * not been initialized yet.
435 * Return 0 on success and -1 on error.
437 * If "tree" is a filter, then we set data->filter to this filter
438 * (or its universe).
439 * If "tree" is a domain, then this means we have reached the root
440 * of the schedule tree without being able to extract any information.
441 * We therefore initialize data->filter to the universe of the domain,
442 * or the domain itself if data->universe_domain is not set.
443 * If "tree" is a band with at least one member, then we set data->filter
444 * to the universe of the schedule domain and replace the zero-dimensional
445 * data->prefix by the band schedule (if data->collect_prefix is set).
447 static int collect_filter_prefix_init(__isl_keep isl_schedule_tree *tree,
448 struct isl_schedule_node_get_filter_prefix_data *data)
450 enum isl_schedule_node_type type;
451 isl_multi_union_pw_aff *mupa;
452 isl_union_set *filter;
454 type = isl_schedule_tree_get_type(tree);
455 switch (type) {
456 case isl_schedule_node_error:
457 return -1;
458 case isl_schedule_node_expansion:
459 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
460 "should be handled by caller", return -1);
461 case isl_schedule_node_extension:
462 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
463 "cannot handle extension nodes", return -1);
464 case isl_schedule_node_context:
465 case isl_schedule_node_leaf:
466 case isl_schedule_node_guard:
467 case isl_schedule_node_mark:
468 case isl_schedule_node_sequence:
469 case isl_schedule_node_set:
470 return 0;
471 case isl_schedule_node_domain:
472 filter = isl_schedule_tree_domain_get_domain(tree);
473 if (data->universe_domain)
474 filter = isl_union_set_universe(filter);
475 data->filter = filter;
476 break;
477 case isl_schedule_node_band:
478 if (isl_schedule_tree_band_n_member(tree) == 0)
479 return 0;
480 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
481 if (data->collect_prefix) {
482 isl_multi_union_pw_aff_free(data->prefix);
483 mupa = isl_multi_union_pw_aff_reset_tuple_id(mupa,
484 isl_dim_set);
485 data->prefix = isl_multi_union_pw_aff_copy(mupa);
487 filter = isl_multi_union_pw_aff_domain(mupa);
488 filter = isl_union_set_universe(filter);
489 data->filter = filter;
490 break;
491 case isl_schedule_node_filter:
492 filter = isl_schedule_tree_filter_get_filter(tree);
493 if (data->universe_filter)
494 filter = isl_union_set_universe(filter);
495 data->filter = filter;
496 break;
499 if ((data->collect_prefix && !data->prefix) || !data->filter)
500 return -1;
502 data->initialized = 1;
504 return 0;
507 /* Update "data" based on the tree node "tree" in case "data" has
508 * already been initialized.
510 * Return 0 on success and -1 on error.
512 * If "tree" is a domain and data->universe_domain is not set, then
513 * intersect data->filter with the domain.
514 * If "tree" is a filter, then we intersect data->filter with this filter
515 * (or its universe).
516 * If "tree" is a band with at least one member and data->collect_prefix
517 * is set, then we extend data->prefix with the band schedule.
518 * If "tree" is an extension, then we make sure that we are not collecting
519 * information on any extended domain elements.
521 static int collect_filter_prefix_update(__isl_keep isl_schedule_tree *tree,
522 struct isl_schedule_node_get_filter_prefix_data *data)
524 enum isl_schedule_node_type type;
525 isl_multi_union_pw_aff *mupa;
526 isl_union_set *filter;
527 isl_union_map *extension;
528 int empty;
530 type = isl_schedule_tree_get_type(tree);
531 switch (type) {
532 case isl_schedule_node_error:
533 return -1;
534 case isl_schedule_node_expansion:
535 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
536 "should be handled by caller", return -1);
537 case isl_schedule_node_extension:
538 extension = isl_schedule_tree_extension_get_extension(tree);
539 extension = isl_union_map_intersect_range(extension,
540 isl_union_set_copy(data->filter));
541 empty = isl_union_map_is_empty(extension);
542 isl_union_map_free(extension);
543 if (empty < 0)
544 return -1;
545 if (empty)
546 break;
547 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
548 "cannot handle extension nodes", return -1);
549 case isl_schedule_node_context:
550 case isl_schedule_node_leaf:
551 case isl_schedule_node_guard:
552 case isl_schedule_node_mark:
553 case isl_schedule_node_sequence:
554 case isl_schedule_node_set:
555 break;
556 case isl_schedule_node_domain:
557 if (data->universe_domain)
558 break;
559 filter = isl_schedule_tree_domain_get_domain(tree);
560 data->filter = isl_union_set_intersect(data->filter, filter);
561 break;
562 case isl_schedule_node_band:
563 if (isl_schedule_tree_band_n_member(tree) == 0)
564 break;
565 if (!data->collect_prefix)
566 break;
567 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
568 data->prefix = isl_multi_union_pw_aff_flat_range_product(mupa,
569 data->prefix);
570 if (!data->prefix)
571 return -1;
572 break;
573 case isl_schedule_node_filter:
574 filter = isl_schedule_tree_filter_get_filter(tree);
575 if (data->universe_filter)
576 filter = isl_union_set_universe(filter);
577 data->filter = isl_union_set_intersect(data->filter, filter);
578 if (!data->filter)
579 return -1;
580 break;
583 return 0;
586 /* Collect filter and/or prefix information from the first "n"
587 * elements in "list" (which represent the ancestors of a node).
588 * Store the results in "data".
590 * Extension nodes are only supported if they do not affect the outcome,
591 * i.e., if we are collecting information on non-extended domain elements,
592 * or if we are collecting the universe domain (without prefix).
594 * Return 0 on success and -1 on error.
596 * We traverse the list from innermost ancestor (last element)
597 * to outermost ancestor (first element), calling collect_filter_prefix_init
598 * on each node as long as we have not been able to extract any information
599 * yet and collect_filter_prefix_update afterwards.
600 * If we come across an expansion node, then we interrupt the traversal
601 * and call collect_filter_prefix_expansion to restart the traversal
602 * over the remaining ancestors and to combine the results with those
603 * that have already been collected.
604 * If we come across an extension node and we are only computing
605 * the universe domain, then we interrupt the traversal and call
606 * collect_universe_domain_extension to restart the traversal
607 * over the remaining ancestors and to combine the results with those
608 * that have already been collected.
609 * On successful return, data->initialized will be set since the outermost
610 * ancestor is a domain node, which always results in an initialization.
612 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
613 int n, struct isl_schedule_node_get_filter_prefix_data *data)
615 int i;
617 if (!list)
618 return -1;
620 for (i = n - 1; i >= 0; --i) {
621 isl_schedule_tree *tree;
622 enum isl_schedule_node_type type;
623 int r;
625 tree = isl_schedule_tree_list_get_schedule_tree(list, i);
626 if (!tree)
627 return -1;
628 type = isl_schedule_tree_get_type(tree);
629 if (type == isl_schedule_node_expansion)
630 return collect_filter_prefix_expansion(tree, list, i,
631 data);
632 if (type == isl_schedule_node_extension &&
633 data->universe_domain && !data->collect_prefix)
634 return collect_universe_domain_extension(tree, list, i,
635 data);
636 if (!data->initialized)
637 r = collect_filter_prefix_init(tree, data);
638 else
639 r = collect_filter_prefix_update(tree, data);
640 isl_schedule_tree_free(tree);
641 if (r < 0)
642 return -1;
645 return 0;
648 /* Return the concatenation of the partial schedules of all outer band
649 * nodes of "node" interesected with all outer filters
650 * as an isl_multi_union_pw_aff.
651 * None of the ancestors of "node" may be an extension node, unless
652 * there is also a filter ancestor that filters out all the extended
653 * domain elements.
655 * If "node" is pointing at the root of the schedule tree, then
656 * there are no domain elements reaching the current node, so
657 * we return an empty result.
659 * We collect all the filters and partial schedules in collect_filter_prefix
660 * and intersect the domain of the combined schedule with the combined filter.
662 __isl_give isl_multi_union_pw_aff *
663 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(
664 __isl_keep isl_schedule_node *node)
666 int n;
667 isl_space *space;
668 struct isl_schedule_node_get_filter_prefix_data data;
670 if (!node)
671 return NULL;
673 space = isl_schedule_get_space(node->schedule);
674 space = isl_space_set_from_params(space);
675 if (node->tree == node->schedule->root)
676 return isl_multi_union_pw_aff_zero(space);
678 data.initialized = 0;
679 data.universe_domain = 1;
680 data.universe_filter = 0;
681 data.collect_prefix = 1;
682 data.filter = NULL;
683 data.prefix = isl_multi_union_pw_aff_zero(space);
685 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
686 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
687 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
689 data.prefix = isl_multi_union_pw_aff_intersect_domain(data.prefix,
690 data.filter);
692 return data.prefix;
695 /* Return the concatenation of the partial schedules of all outer band
696 * nodes of "node" interesected with all outer filters
697 * as an isl_union_pw_multi_aff.
698 * None of the ancestors of "node" may be an extension node, unless
699 * there is also a filter ancestor that filters out all the extended
700 * domain elements.
702 * If "node" is pointing at the root of the schedule tree, then
703 * there are no domain elements reaching the current node, so
704 * we return an empty result.
706 * We collect all the filters and partial schedules in collect_filter_prefix.
707 * The partial schedules are collected as an isl_multi_union_pw_aff.
708 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
709 * contain any domain information, so we construct the isl_union_pw_multi_aff
710 * result as a zero-dimensional function on the collected filter.
711 * Otherwise, we convert the isl_multi_union_pw_aff to
712 * an isl_multi_union_pw_aff and intersect the domain with the filter.
714 __isl_give isl_union_pw_multi_aff *
715 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
716 __isl_keep isl_schedule_node *node)
718 int n;
719 isl_space *space;
720 isl_union_pw_multi_aff *prefix;
721 struct isl_schedule_node_get_filter_prefix_data data;
723 if (!node)
724 return NULL;
726 space = isl_schedule_get_space(node->schedule);
727 if (node->tree == node->schedule->root)
728 return isl_union_pw_multi_aff_empty(space);
730 space = isl_space_set_from_params(space);
731 data.initialized = 0;
732 data.universe_domain = 1;
733 data.universe_filter = 0;
734 data.collect_prefix = 1;
735 data.filter = NULL;
736 data.prefix = isl_multi_union_pw_aff_zero(space);
738 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
739 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
740 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
742 if (data.prefix &&
743 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
744 isl_multi_union_pw_aff_free(data.prefix);
745 prefix = isl_union_pw_multi_aff_from_domain(data.filter);
746 } else {
747 prefix =
748 isl_union_pw_multi_aff_from_multi_union_pw_aff(data.prefix);
749 prefix = isl_union_pw_multi_aff_intersect_domain(prefix,
750 data.filter);
753 return prefix;
756 /* Return the concatenation of the partial schedules of all outer band
757 * nodes of "node" interesected with all outer filters
758 * as an isl_union_map.
760 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_union_map(
761 __isl_keep isl_schedule_node *node)
763 isl_union_pw_multi_aff *upma;
765 upma = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
766 return isl_union_map_from_union_pw_multi_aff(upma);
769 /* Return the concatenation of the partial schedules of all outer band
770 * nodes of "node" intersected with all outer domain constraints.
771 * None of the ancestors of "node" may be an extension node, unless
772 * there is also a filter ancestor that filters out all the extended
773 * domain elements.
775 * Essentially, this functions intersected the domain of the output
776 * of isl_schedule_node_get_prefix_schedule_union_map with the output
777 * of isl_schedule_node_get_domain, except that it only traverses
778 * the ancestors of "node" once.
780 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_relation(
781 __isl_keep isl_schedule_node *node)
783 int n;
784 isl_space *space;
785 isl_union_map *prefix;
786 struct isl_schedule_node_get_filter_prefix_data data;
788 if (!node)
789 return NULL;
791 space = isl_schedule_get_space(node->schedule);
792 if (node->tree == node->schedule->root)
793 return isl_union_map_empty(space);
795 space = isl_space_set_from_params(space);
796 data.initialized = 0;
797 data.universe_domain = 0;
798 data.universe_filter = 0;
799 data.collect_prefix = 1;
800 data.filter = NULL;
801 data.prefix = isl_multi_union_pw_aff_zero(space);
803 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
804 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
805 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
807 if (data.prefix &&
808 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
809 isl_multi_union_pw_aff_free(data.prefix);
810 prefix = isl_union_map_from_domain(data.filter);
811 } else {
812 prefix = isl_union_map_from_multi_union_pw_aff(data.prefix);
813 prefix = isl_union_map_intersect_domain(prefix, data.filter);
816 return prefix;
819 /* Return the domain elements that reach "node".
821 * If "node" is pointing at the root of the schedule tree, then
822 * there are no domain elements reaching the current node, so
823 * we return an empty result.
824 * None of the ancestors of "node" may be an extension node, unless
825 * there is also a filter ancestor that filters out all the extended
826 * domain elements.
828 * Otherwise, we collect all filters reaching the node,
829 * intersected with the root domain in collect_filter_prefix.
831 __isl_give isl_union_set *isl_schedule_node_get_domain(
832 __isl_keep isl_schedule_node *node)
834 int n;
835 struct isl_schedule_node_get_filter_prefix_data data;
837 if (!node)
838 return NULL;
840 if (node->tree == node->schedule->root) {
841 isl_space *space;
843 space = isl_schedule_get_space(node->schedule);
844 return isl_union_set_empty(space);
847 data.initialized = 0;
848 data.universe_domain = 0;
849 data.universe_filter = 0;
850 data.collect_prefix = 0;
851 data.filter = NULL;
852 data.prefix = NULL;
854 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
855 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
856 data.filter = isl_union_set_free(data.filter);
858 return data.filter;
861 /* Return the union of universe sets of the domain elements that reach "node".
863 * If "node" is pointing at the root of the schedule tree, then
864 * there are no domain elements reaching the current node, so
865 * we return an empty result.
867 * Otherwise, we collect the universes of all filters reaching the node
868 * in collect_filter_prefix.
870 __isl_give isl_union_set *isl_schedule_node_get_universe_domain(
871 __isl_keep isl_schedule_node *node)
873 int n;
874 struct isl_schedule_node_get_filter_prefix_data data;
876 if (!node)
877 return NULL;
879 if (node->tree == node->schedule->root) {
880 isl_space *space;
882 space = isl_schedule_get_space(node->schedule);
883 return isl_union_set_empty(space);
886 data.initialized = 0;
887 data.universe_domain = 1;
888 data.universe_filter = 1;
889 data.collect_prefix = 0;
890 data.filter = NULL;
891 data.prefix = NULL;
893 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
894 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
895 data.filter = isl_union_set_free(data.filter);
897 return data.filter;
900 /* Return the subtree schedule of "node".
902 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
903 * trees that do not contain any schedule information, we first
904 * move down to the first relevant descendant and handle leaves ourselves.
906 * If the subtree rooted at "node" contains any expansion nodes, then
907 * the returned subtree schedule is formulated in terms of the expanded
908 * domains.
909 * The subtree is not allowed to contain any extension nodes.
911 __isl_give isl_union_map *isl_schedule_node_get_subtree_schedule_union_map(
912 __isl_keep isl_schedule_node *node)
914 isl_schedule_tree *tree, *leaf;
915 isl_union_map *umap;
917 tree = isl_schedule_node_get_tree(node);
918 leaf = isl_schedule_node_peek_leaf(node);
919 tree = isl_schedule_tree_first_schedule_descendant(tree, leaf);
920 if (!tree)
921 return NULL;
922 if (tree == leaf) {
923 isl_union_set *domain;
924 domain = isl_schedule_node_get_universe_domain(node);
925 isl_schedule_tree_free(tree);
926 return isl_union_map_from_domain(domain);
929 umap = isl_schedule_tree_get_subtree_schedule_union_map(tree);
930 isl_schedule_tree_free(tree);
931 return umap;
934 /* Return the number of ancestors of "node" in its schedule tree.
936 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node *node)
938 if (!node)
939 return -1;
940 return isl_schedule_tree_list_n_schedule_tree(node->ancestors);
943 /* Does "node" have a parent?
945 * That is, does it point to any node of the schedule other than the root?
947 isl_bool isl_schedule_node_has_parent(__isl_keep isl_schedule_node *node)
949 if (!node)
950 return isl_bool_error;
951 if (!node->ancestors)
952 return isl_bool_error;
954 return isl_schedule_tree_list_n_schedule_tree(node->ancestors) != 0;
957 /* Return the position of "node" among the children of its parent.
959 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node *node)
961 int n;
962 int has_parent;
964 if (!node)
965 return -1;
966 has_parent = isl_schedule_node_has_parent(node);
967 if (has_parent < 0)
968 return -1;
969 if (!has_parent)
970 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
971 "node has no parent", return -1);
973 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
974 return node->child_pos[n - 1];
977 /* Does the parent (if any) of "node" have any children with a smaller child
978 * position than this one?
980 isl_bool isl_schedule_node_has_previous_sibling(
981 __isl_keep isl_schedule_node *node)
983 int n;
984 isl_bool has_parent;
986 if (!node)
987 return isl_bool_error;
988 has_parent = isl_schedule_node_has_parent(node);
989 if (has_parent < 0 || !has_parent)
990 return has_parent;
992 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
994 return node->child_pos[n - 1] > 0;
997 /* Does the parent (if any) of "node" have any children with a greater child
998 * position than this one?
1000 isl_bool isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node *node)
1002 int n, n_child;
1003 isl_bool has_parent;
1004 isl_schedule_tree *tree;
1006 if (!node)
1007 return isl_bool_error;
1008 has_parent = isl_schedule_node_has_parent(node);
1009 if (has_parent < 0 || !has_parent)
1010 return has_parent;
1012 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1013 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n - 1);
1014 if (!tree)
1015 return isl_bool_error;
1016 n_child = isl_schedule_tree_list_n_schedule_tree(tree->children);
1017 isl_schedule_tree_free(tree);
1019 return node->child_pos[n - 1] + 1 < n_child;
1022 /* Does "node" have any children?
1024 * Any node other than the leaf nodes is considered to have at least
1025 * one child, even if the corresponding isl_schedule_tree does not
1026 * have any children.
1028 isl_bool isl_schedule_node_has_children(__isl_keep isl_schedule_node *node)
1030 if (!node)
1031 return isl_bool_error;
1032 return !isl_schedule_tree_is_leaf(node->tree);
1035 /* Return the number of children of "node"?
1037 * Any node other than the leaf nodes is considered to have at least
1038 * one child, even if the corresponding isl_schedule_tree does not
1039 * have any children. That is, the number of children of "node" is
1040 * only zero if its tree is the explicit empty tree. Otherwise,
1041 * if the isl_schedule_tree has any children, then it is equal
1042 * to the number of children of "node". If it has zero children,
1043 * then "node" still has a leaf node as child.
1045 int isl_schedule_node_n_children(__isl_keep isl_schedule_node *node)
1047 int n;
1049 if (!node)
1050 return -1;
1052 if (isl_schedule_tree_is_leaf(node->tree))
1053 return 0;
1055 n = isl_schedule_tree_n_children(node->tree);
1056 if (n == 0)
1057 return 1;
1059 return n;
1062 /* Move the "node" pointer to the ancestor of the given generation
1063 * of the node it currently points to, where generation 0 is the node
1064 * itself and generation 1 is its parent.
1066 __isl_give isl_schedule_node *isl_schedule_node_ancestor(
1067 __isl_take isl_schedule_node *node, int generation)
1069 int n;
1070 isl_schedule_tree *tree;
1072 if (!node)
1073 return NULL;
1074 if (generation == 0)
1075 return node;
1076 n = isl_schedule_node_get_tree_depth(node);
1077 if (n < 0)
1078 return isl_schedule_node_free(node);
1079 if (generation < 0 || generation > n)
1080 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1081 "generation out of bounds",
1082 return isl_schedule_node_free(node));
1083 node = isl_schedule_node_cow(node);
1084 if (!node)
1085 return NULL;
1087 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1088 n - generation);
1089 isl_schedule_tree_free(node->tree);
1090 node->tree = tree;
1091 node->ancestors = isl_schedule_tree_list_drop(node->ancestors,
1092 n - generation, generation);
1093 if (!node->ancestors || !node->tree)
1094 return isl_schedule_node_free(node);
1096 return node;
1099 /* Move the "node" pointer to the parent of the node it currently points to.
1101 __isl_give isl_schedule_node *isl_schedule_node_parent(
1102 __isl_take isl_schedule_node *node)
1104 if (!node)
1105 return NULL;
1106 if (!isl_schedule_node_has_parent(node))
1107 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1108 "node has no parent",
1109 return isl_schedule_node_free(node));
1110 return isl_schedule_node_ancestor(node, 1);
1113 /* Move the "node" pointer to the root of its schedule tree.
1115 __isl_give isl_schedule_node *isl_schedule_node_root(
1116 __isl_take isl_schedule_node *node)
1118 int n;
1120 if (!node)
1121 return NULL;
1122 n = isl_schedule_node_get_tree_depth(node);
1123 if (n < 0)
1124 return isl_schedule_node_free(node);
1125 return isl_schedule_node_ancestor(node, n);
1128 /* Move the "node" pointer to the child at position "pos" of the node
1129 * it currently points to.
1131 __isl_give isl_schedule_node *isl_schedule_node_child(
1132 __isl_take isl_schedule_node *node, int pos)
1134 int n;
1135 isl_ctx *ctx;
1136 isl_schedule_tree *tree;
1137 int *child_pos;
1139 node = isl_schedule_node_cow(node);
1140 if (!node)
1141 return NULL;
1142 if (!isl_schedule_node_has_children(node))
1143 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1144 "node has no children",
1145 return isl_schedule_node_free(node));
1147 ctx = isl_schedule_node_get_ctx(node);
1148 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1149 child_pos = isl_realloc_array(ctx, node->child_pos, int, n + 1);
1150 if (!child_pos)
1151 return isl_schedule_node_free(node);
1152 node->child_pos = child_pos;
1153 node->child_pos[n] = pos;
1155 node->ancestors = isl_schedule_tree_list_add(node->ancestors,
1156 isl_schedule_tree_copy(node->tree));
1157 tree = node->tree;
1158 if (isl_schedule_tree_has_children(tree))
1159 tree = isl_schedule_tree_get_child(tree, pos);
1160 else
1161 tree = isl_schedule_node_get_leaf(node);
1162 isl_schedule_tree_free(node->tree);
1163 node->tree = tree;
1165 if (!node->tree || !node->ancestors)
1166 return isl_schedule_node_free(node);
1168 return node;
1171 /* Move the "node" pointer to the first child of the node
1172 * it currently points to.
1174 __isl_give isl_schedule_node *isl_schedule_node_first_child(
1175 __isl_take isl_schedule_node *node)
1177 return isl_schedule_node_child(node, 0);
1180 /* Move the "node" pointer to the child of this node's parent in
1181 * the previous child position.
1183 __isl_give isl_schedule_node *isl_schedule_node_previous_sibling(
1184 __isl_take isl_schedule_node *node)
1186 int n;
1187 isl_schedule_tree *parent, *tree;
1189 node = isl_schedule_node_cow(node);
1190 if (!node)
1191 return NULL;
1192 if (!isl_schedule_node_has_previous_sibling(node))
1193 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1194 "node has no previous sibling",
1195 return isl_schedule_node_free(node));
1197 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1198 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1199 n - 1);
1200 if (!parent)
1201 return isl_schedule_node_free(node);
1202 node->child_pos[n - 1]--;
1203 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
1204 node->child_pos[n - 1]);
1205 isl_schedule_tree_free(parent);
1206 if (!tree)
1207 return isl_schedule_node_free(node);
1208 isl_schedule_tree_free(node->tree);
1209 node->tree = tree;
1211 return node;
1214 /* Move the "node" pointer to the child of this node's parent in
1215 * the next child position.
1217 __isl_give isl_schedule_node *isl_schedule_node_next_sibling(
1218 __isl_take isl_schedule_node *node)
1220 int n;
1221 isl_schedule_tree *parent, *tree;
1223 node = isl_schedule_node_cow(node);
1224 if (!node)
1225 return NULL;
1226 if (!isl_schedule_node_has_next_sibling(node))
1227 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1228 "node has no next sibling",
1229 return isl_schedule_node_free(node));
1231 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1232 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1233 n - 1);
1234 if (!parent)
1235 return isl_schedule_node_free(node);
1236 node->child_pos[n - 1]++;
1237 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
1238 node->child_pos[n - 1]);
1239 isl_schedule_tree_free(parent);
1240 if (!tree)
1241 return isl_schedule_node_free(node);
1242 isl_schedule_tree_free(node->tree);
1243 node->tree = tree;
1245 return node;
1248 /* Return a copy to the child at position "pos" of "node".
1250 __isl_give isl_schedule_node *isl_schedule_node_get_child(
1251 __isl_keep isl_schedule_node *node, int pos)
1253 return isl_schedule_node_child(isl_schedule_node_copy(node), pos);
1256 /* Traverse the descendant of "node" in depth-first order, including
1257 * "node" itself. Call "enter" whenever a node is entered and "leave"
1258 * whenever a node is left. The callback "enter" is responsible
1259 * for moving to the deepest initial subtree of its argument that
1260 * should be traversed.
1262 static __isl_give isl_schedule_node *traverse(
1263 __isl_take isl_schedule_node *node,
1264 __isl_give isl_schedule_node *(*enter)(
1265 __isl_take isl_schedule_node *node, void *user),
1266 __isl_give isl_schedule_node *(*leave)(
1267 __isl_take isl_schedule_node *node, void *user),
1268 void *user)
1270 int depth;
1272 if (!node)
1273 return NULL;
1275 depth = isl_schedule_node_get_tree_depth(node);
1276 do {
1277 node = enter(node, user);
1278 node = leave(node, user);
1279 while (node && isl_schedule_node_get_tree_depth(node) > depth &&
1280 !isl_schedule_node_has_next_sibling(node)) {
1281 node = isl_schedule_node_parent(node);
1282 node = leave(node, user);
1284 if (node && isl_schedule_node_get_tree_depth(node) > depth)
1285 node = isl_schedule_node_next_sibling(node);
1286 } while (node && isl_schedule_node_get_tree_depth(node) > depth);
1288 return node;
1291 /* Internal data structure for isl_schedule_node_foreach_descendant_top_down.
1293 * "fn" is the user-specified callback function.
1294 * "user" is the user-specified argument for the callback.
1296 struct isl_schedule_node_preorder_data {
1297 isl_bool (*fn)(__isl_keep isl_schedule_node *node, void *user);
1298 void *user;
1301 /* Callback for "traverse" to enter a node and to move
1302 * to the deepest initial subtree that should be traversed
1303 * for use in a preorder visit.
1305 * If the user callback returns a negative value, then we abort
1306 * the traversal. If this callback returns zero, then we skip
1307 * the subtree rooted at the current node. Otherwise, we move
1308 * down to the first child and repeat the process until a leaf
1309 * is reached.
1311 static __isl_give isl_schedule_node *preorder_enter(
1312 __isl_take isl_schedule_node *node, void *user)
1314 struct isl_schedule_node_preorder_data *data = user;
1316 if (!node)
1317 return NULL;
1319 do {
1320 isl_bool r;
1322 r = data->fn(node, data->user);
1323 if (r < 0)
1324 return isl_schedule_node_free(node);
1325 if (r == isl_bool_false)
1326 return node;
1327 } while (isl_schedule_node_has_children(node) &&
1328 (node = isl_schedule_node_first_child(node)) != NULL);
1330 return node;
1333 /* Callback for "traverse" to leave a node
1334 * for use in a preorder visit.
1335 * Since we already visited the node when we entered it,
1336 * we do not need to do anything here.
1338 static __isl_give isl_schedule_node *preorder_leave(
1339 __isl_take isl_schedule_node *node, void *user)
1341 return node;
1344 /* Traverse the descendants of "node" (including the node itself)
1345 * in depth first preorder.
1347 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1348 * If "fn" returns 0 on any of the nodes, then the subtree rooted
1349 * at that node is skipped.
1351 * Return 0 on success and -1 on failure.
1353 isl_stat isl_schedule_node_foreach_descendant_top_down(
1354 __isl_keep isl_schedule_node *node,
1355 isl_bool (*fn)(__isl_keep isl_schedule_node *node, void *user),
1356 void *user)
1358 struct isl_schedule_node_preorder_data data = { fn, user };
1360 node = isl_schedule_node_copy(node);
1361 node = traverse(node, &preorder_enter, &preorder_leave, &data);
1362 isl_schedule_node_free(node);
1364 return node ? isl_stat_ok : isl_stat_error;
1367 /* Internal data structure for isl_schedule_node_map_descendant_bottom_up.
1369 * "fn" is the user-specified callback function.
1370 * "user" is the user-specified argument for the callback.
1372 struct isl_schedule_node_postorder_data {
1373 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1374 void *user);
1375 void *user;
1378 /* Callback for "traverse" to enter a node and to move
1379 * to the deepest initial subtree that should be traversed
1380 * for use in a postorder visit.
1382 * Since we are performing a postorder visit, we only need
1383 * to move to the deepest initial leaf here.
1385 static __isl_give isl_schedule_node *postorder_enter(
1386 __isl_take isl_schedule_node *node, void *user)
1388 while (node && isl_schedule_node_has_children(node))
1389 node = isl_schedule_node_first_child(node);
1391 return node;
1394 /* Callback for "traverse" to leave a node
1395 * for use in a postorder visit.
1397 * Since we are performing a postorder visit, we need
1398 * to call the user callback here.
1400 static __isl_give isl_schedule_node *postorder_leave(
1401 __isl_take isl_schedule_node *node, void *user)
1403 struct isl_schedule_node_postorder_data *data = user;
1405 return data->fn(node, data->user);
1408 /* Traverse the descendants of "node" (including the node itself)
1409 * in depth first postorder, allowing the user to modify the visited node.
1410 * The traversal continues from the node returned by the callback function.
1411 * It is the responsibility of the user to ensure that this does not
1412 * lead to an infinite loop. It is safest to always return a pointer
1413 * to the same position (same ancestors and child positions) as the input node.
1415 __isl_give isl_schedule_node *isl_schedule_node_map_descendant_bottom_up(
1416 __isl_take isl_schedule_node *node,
1417 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1418 void *user), void *user)
1420 struct isl_schedule_node_postorder_data data = { fn, user };
1422 return traverse(node, &postorder_enter, &postorder_leave, &data);
1425 /* Traverse the ancestors of "node" from the root down to and including
1426 * the parent of "node", calling "fn" on each of them.
1428 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1430 * Return 0 on success and -1 on failure.
1432 isl_stat isl_schedule_node_foreach_ancestor_top_down(
1433 __isl_keep isl_schedule_node *node,
1434 isl_stat (*fn)(__isl_keep isl_schedule_node *node, void *user),
1435 void *user)
1437 int i, n;
1439 if (!node)
1440 return isl_stat_error;
1442 n = isl_schedule_node_get_tree_depth(node);
1443 for (i = 0; i < n; ++i) {
1444 isl_schedule_node *ancestor;
1445 isl_stat r;
1447 ancestor = isl_schedule_node_copy(node);
1448 ancestor = isl_schedule_node_ancestor(ancestor, n - i);
1449 r = fn(ancestor, user);
1450 isl_schedule_node_free(ancestor);
1451 if (r < 0)
1452 return isl_stat_error;
1455 return isl_stat_ok;
1458 /* Is any node in the subtree rooted at "node" anchored?
1459 * That is, do any of these nodes reference the outer band nodes?
1461 isl_bool isl_schedule_node_is_subtree_anchored(
1462 __isl_keep isl_schedule_node *node)
1464 if (!node)
1465 return isl_bool_error;
1466 return isl_schedule_tree_is_subtree_anchored(node->tree);
1469 /* Return the number of members in the given band node.
1471 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node *node)
1473 return node ? isl_schedule_tree_band_n_member(node->tree) : 0;
1476 /* Is the band member at position "pos" of the band node "node"
1477 * marked coincident?
1479 isl_bool isl_schedule_node_band_member_get_coincident(
1480 __isl_keep isl_schedule_node *node, int pos)
1482 if (!node)
1483 return isl_bool_error;
1484 return isl_schedule_tree_band_member_get_coincident(node->tree, pos);
1487 /* Mark the band member at position "pos" the band node "node"
1488 * as being coincident or not according to "coincident".
1490 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_coincident(
1491 __isl_take isl_schedule_node *node, int pos, int coincident)
1493 int c;
1494 isl_schedule_tree *tree;
1496 if (!node)
1497 return NULL;
1498 c = isl_schedule_node_band_member_get_coincident(node, pos);
1499 if (c == coincident)
1500 return node;
1502 tree = isl_schedule_tree_copy(node->tree);
1503 tree = isl_schedule_tree_band_member_set_coincident(tree, pos,
1504 coincident);
1505 node = isl_schedule_node_graft_tree(node, tree);
1507 return node;
1510 /* Is the band node "node" marked permutable?
1512 isl_bool isl_schedule_node_band_get_permutable(
1513 __isl_keep isl_schedule_node *node)
1515 if (!node)
1516 return isl_bool_error;
1518 return isl_schedule_tree_band_get_permutable(node->tree);
1521 /* Mark the band node "node" permutable or not according to "permutable"?
1523 __isl_give isl_schedule_node *isl_schedule_node_band_set_permutable(
1524 __isl_take isl_schedule_node *node, int permutable)
1526 isl_schedule_tree *tree;
1528 if (!node)
1529 return NULL;
1530 if (isl_schedule_node_band_get_permutable(node) == permutable)
1531 return node;
1533 tree = isl_schedule_tree_copy(node->tree);
1534 tree = isl_schedule_tree_band_set_permutable(tree, permutable);
1535 node = isl_schedule_node_graft_tree(node, tree);
1537 return node;
1540 /* Return the schedule space of the band node.
1542 __isl_give isl_space *isl_schedule_node_band_get_space(
1543 __isl_keep isl_schedule_node *node)
1545 if (!node)
1546 return NULL;
1548 return isl_schedule_tree_band_get_space(node->tree);
1551 /* Return the schedule of the band node in isolation.
1553 __isl_give isl_multi_union_pw_aff *isl_schedule_node_band_get_partial_schedule(
1554 __isl_keep isl_schedule_node *node)
1556 if (!node)
1557 return NULL;
1559 return isl_schedule_tree_band_get_partial_schedule(node->tree);
1562 /* Return the schedule of the band node in isolation in the form of
1563 * an isl_union_map.
1565 * If the band does not have any members, then we construct a universe map
1566 * with the universe of the domain elements reaching the node as domain.
1567 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1568 * convert that to an isl_union_map.
1570 __isl_give isl_union_map *isl_schedule_node_band_get_partial_schedule_union_map(
1571 __isl_keep isl_schedule_node *node)
1573 isl_multi_union_pw_aff *mupa;
1575 if (!node)
1576 return NULL;
1578 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
1579 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1580 "not a band node", return NULL);
1581 if (isl_schedule_node_band_n_member(node) == 0) {
1582 isl_union_set *domain;
1584 domain = isl_schedule_node_get_universe_domain(node);
1585 return isl_union_map_from_domain(domain);
1588 mupa = isl_schedule_node_band_get_partial_schedule(node);
1589 return isl_union_map_from_multi_union_pw_aff(mupa);
1592 /* Return the loop AST generation type for the band member of band node "node"
1593 * at position "pos".
1595 enum isl_ast_loop_type isl_schedule_node_band_member_get_ast_loop_type(
1596 __isl_keep isl_schedule_node *node, int pos)
1598 if (!node)
1599 return isl_ast_loop_error;
1601 return isl_schedule_tree_band_member_get_ast_loop_type(node->tree, pos);
1604 /* Set the loop AST generation type for the band member of band node "node"
1605 * at position "pos" to "type".
1607 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_ast_loop_type(
1608 __isl_take isl_schedule_node *node, int pos,
1609 enum isl_ast_loop_type type)
1611 isl_schedule_tree *tree;
1613 if (!node)
1614 return NULL;
1616 tree = isl_schedule_tree_copy(node->tree);
1617 tree = isl_schedule_tree_band_member_set_ast_loop_type(tree, pos, type);
1618 return isl_schedule_node_graft_tree(node, tree);
1621 /* Return the loop AST generation type for the band member of band node "node"
1622 * at position "pos" for the isolated part.
1624 enum isl_ast_loop_type isl_schedule_node_band_member_get_isolate_ast_loop_type(
1625 __isl_keep isl_schedule_node *node, int pos)
1627 if (!node)
1628 return isl_ast_loop_error;
1630 return isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1631 node->tree, pos);
1634 /* Set the loop AST generation type for the band member of band node "node"
1635 * at position "pos" for the isolated part to "type".
1637 __isl_give isl_schedule_node *
1638 isl_schedule_node_band_member_set_isolate_ast_loop_type(
1639 __isl_take isl_schedule_node *node, int pos,
1640 enum isl_ast_loop_type type)
1642 isl_schedule_tree *tree;
1644 if (!node)
1645 return NULL;
1647 tree = isl_schedule_tree_copy(node->tree);
1648 tree = isl_schedule_tree_band_member_set_isolate_ast_loop_type(tree,
1649 pos, type);
1650 return isl_schedule_node_graft_tree(node, tree);
1653 /* Return the AST build options associated to band node "node".
1655 __isl_give isl_union_set *isl_schedule_node_band_get_ast_build_options(
1656 __isl_keep isl_schedule_node *node)
1658 if (!node)
1659 return NULL;
1661 return isl_schedule_tree_band_get_ast_build_options(node->tree);
1664 /* Replace the AST build options associated to band node "node" by "options".
1666 __isl_give isl_schedule_node *isl_schedule_node_band_set_ast_build_options(
1667 __isl_take isl_schedule_node *node, __isl_take isl_union_set *options)
1669 isl_schedule_tree *tree;
1671 if (!node || !options)
1672 goto error;
1674 tree = isl_schedule_tree_copy(node->tree);
1675 tree = isl_schedule_tree_band_set_ast_build_options(tree, options);
1676 return isl_schedule_node_graft_tree(node, tree);
1677 error:
1678 isl_schedule_node_free(node);
1679 isl_union_set_free(options);
1680 return NULL;
1683 /* Make sure that that spaces of "node" and "mv" are the same.
1684 * Return -1 on error, reporting the error to the user.
1686 static int check_space_multi_val(__isl_keep isl_schedule_node *node,
1687 __isl_keep isl_multi_val *mv)
1689 isl_space *node_space, *mv_space;
1690 int equal;
1692 node_space = isl_schedule_node_band_get_space(node);
1693 mv_space = isl_multi_val_get_space(mv);
1694 equal = isl_space_tuple_is_equal(node_space, isl_dim_set,
1695 mv_space, isl_dim_set);
1696 isl_space_free(mv_space);
1697 isl_space_free(node_space);
1698 if (equal < 0)
1699 return -1;
1700 if (!equal)
1701 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1702 "spaces don't match", return -1);
1704 return 0;
1707 /* Multiply the partial schedule of the band node "node"
1708 * with the factors in "mv".
1710 __isl_give isl_schedule_node *isl_schedule_node_band_scale(
1711 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1713 isl_schedule_tree *tree;
1714 int anchored;
1716 if (!node || !mv)
1717 goto error;
1718 if (check_space_multi_val(node, mv) < 0)
1719 goto error;
1720 anchored = isl_schedule_node_is_subtree_anchored(node);
1721 if (anchored < 0)
1722 goto error;
1723 if (anchored)
1724 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1725 "cannot scale band node with anchored subtree",
1726 goto error);
1728 tree = isl_schedule_node_get_tree(node);
1729 tree = isl_schedule_tree_band_scale(tree, mv);
1730 return isl_schedule_node_graft_tree(node, tree);
1731 error:
1732 isl_multi_val_free(mv);
1733 isl_schedule_node_free(node);
1734 return NULL;
1737 /* Divide the partial schedule of the band node "node"
1738 * by the factors in "mv".
1740 __isl_give isl_schedule_node *isl_schedule_node_band_scale_down(
1741 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1743 isl_schedule_tree *tree;
1744 int anchored;
1746 if (!node || !mv)
1747 goto error;
1748 if (check_space_multi_val(node, mv) < 0)
1749 goto error;
1750 anchored = isl_schedule_node_is_subtree_anchored(node);
1751 if (anchored < 0)
1752 goto error;
1753 if (anchored)
1754 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1755 "cannot scale down band node with anchored subtree",
1756 goto error);
1758 tree = isl_schedule_node_get_tree(node);
1759 tree = isl_schedule_tree_band_scale_down(tree, mv);
1760 return isl_schedule_node_graft_tree(node, tree);
1761 error:
1762 isl_multi_val_free(mv);
1763 isl_schedule_node_free(node);
1764 return NULL;
1767 /* Tile "node" with tile sizes "sizes".
1769 * The current node is replaced by two nested nodes corresponding
1770 * to the tile dimensions and the point dimensions.
1772 * Return a pointer to the outer (tile) node.
1774 * If any of the descendants of "node" depend on the set of outer band nodes,
1775 * then we refuse to tile the node.
1777 * If the scale tile loops option is set, then the tile loops
1778 * are scaled by the tile sizes. If the shift point loops option is set,
1779 * then the point loops are shifted to start at zero.
1780 * In particular, these options affect the tile and point loop schedules
1781 * as follows
1783 * scale shift original tile point
1785 * 0 0 i floor(i/s) i
1786 * 1 0 i s * floor(i/s) i
1787 * 0 1 i floor(i/s) i - s * floor(i/s)
1788 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1790 __isl_give isl_schedule_node *isl_schedule_node_band_tile(
1791 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
1793 isl_schedule_tree *tree;
1794 int anchored;
1796 if (!node || !sizes)
1797 goto error;
1798 anchored = isl_schedule_node_is_subtree_anchored(node);
1799 if (anchored < 0)
1800 goto error;
1801 if (anchored)
1802 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1803 "cannot tile band node with anchored subtree",
1804 goto error);
1806 if (check_space_multi_val(node, sizes) < 0)
1807 goto error;
1809 tree = isl_schedule_node_get_tree(node);
1810 tree = isl_schedule_tree_band_tile(tree, sizes);
1811 return isl_schedule_node_graft_tree(node, tree);
1812 error:
1813 isl_multi_val_free(sizes);
1814 isl_schedule_node_free(node);
1815 return NULL;
1818 /* Move the band node "node" down to all the leaves in the subtree
1819 * rooted at "node".
1820 * Return a pointer to the node in the resulting tree that is in the same
1821 * position as the node pointed to by "node" in the original tree.
1823 * If the node only has a leaf child, then nothing needs to be done.
1824 * Otherwise, the child of the node is removed and the result is
1825 * appended to all the leaves in the subtree rooted at the original child.
1826 * The original node is then replaced by the result of this operation.
1828 * If any of the nodes in the subtree rooted at "node" depend on
1829 * the set of outer band nodes then we refuse to sink the band node.
1831 __isl_give isl_schedule_node *isl_schedule_node_band_sink(
1832 __isl_take isl_schedule_node *node)
1834 enum isl_schedule_node_type type;
1835 isl_schedule_tree *tree, *child;
1836 int anchored;
1838 if (!node)
1839 return NULL;
1841 type = isl_schedule_node_get_type(node);
1842 if (type != isl_schedule_node_band)
1843 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1844 "not a band node", isl_schedule_node_free(node));
1845 anchored = isl_schedule_node_is_subtree_anchored(node);
1846 if (anchored < 0)
1847 return isl_schedule_node_free(node);
1848 if (anchored)
1849 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1850 "cannot sink band node in anchored subtree",
1851 isl_schedule_node_free(node));
1852 if (isl_schedule_tree_n_children(node->tree) == 0)
1853 return node;
1855 tree = isl_schedule_node_get_tree(node);
1856 child = isl_schedule_tree_get_child(tree, 0);
1857 tree = isl_schedule_tree_reset_children(tree);
1858 tree = isl_schedule_tree_append_to_leaves(child, tree);
1860 return isl_schedule_node_graft_tree(node, tree);
1863 /* Split "node" into two nested band nodes, one with the first "pos"
1864 * dimensions and one with the remaining dimensions.
1865 * The schedules of the two band nodes live in anonymous spaces.
1867 __isl_give isl_schedule_node *isl_schedule_node_band_split(
1868 __isl_take isl_schedule_node *node, int pos)
1870 isl_schedule_tree *tree;
1872 tree = isl_schedule_node_get_tree(node);
1873 tree = isl_schedule_tree_band_split(tree, pos);
1874 return isl_schedule_node_graft_tree(node, tree);
1877 /* Return the context of the context node "node".
1879 __isl_give isl_set *isl_schedule_node_context_get_context(
1880 __isl_keep isl_schedule_node *node)
1882 if (!node)
1883 return NULL;
1885 return isl_schedule_tree_context_get_context(node->tree);
1888 /* Return the domain of the domain node "node".
1890 __isl_give isl_union_set *isl_schedule_node_domain_get_domain(
1891 __isl_keep isl_schedule_node *node)
1893 if (!node)
1894 return NULL;
1896 return isl_schedule_tree_domain_get_domain(node->tree);
1899 /* Return the expansion map of expansion node "node".
1901 __isl_give isl_union_map *isl_schedule_node_expansion_get_expansion(
1902 __isl_keep isl_schedule_node *node)
1904 if (!node)
1905 return NULL;
1907 return isl_schedule_tree_expansion_get_expansion(node->tree);
1910 /* Return the contraction of expansion node "node".
1912 __isl_give isl_union_pw_multi_aff *isl_schedule_node_expansion_get_contraction(
1913 __isl_keep isl_schedule_node *node)
1915 if (!node)
1916 return NULL;
1918 return isl_schedule_tree_expansion_get_contraction(node->tree);
1921 /* Replace the contraction and the expansion of the expansion node "node"
1922 * by "contraction" and "expansion".
1924 __isl_give isl_schedule_node *
1925 isl_schedule_node_expansion_set_contraction_and_expansion(
1926 __isl_take isl_schedule_node *node,
1927 __isl_take isl_union_pw_multi_aff *contraction,
1928 __isl_take isl_union_map *expansion)
1930 isl_schedule_tree *tree;
1932 if (!node || !contraction || !expansion)
1933 goto error;
1935 tree = isl_schedule_tree_copy(node->tree);
1936 tree = isl_schedule_tree_expansion_set_contraction_and_expansion(tree,
1937 contraction, expansion);
1938 return isl_schedule_node_graft_tree(node, tree);
1939 error:
1940 isl_schedule_node_free(node);
1941 isl_union_pw_multi_aff_free(contraction);
1942 isl_union_map_free(expansion);
1943 return NULL;
1946 /* Return the extension of the extension node "node".
1948 __isl_give isl_union_map *isl_schedule_node_extension_get_extension(
1949 __isl_keep isl_schedule_node *node)
1951 if (!node)
1952 return NULL;
1954 return isl_schedule_tree_extension_get_extension(node->tree);
1957 /* Replace the extension of extension node "node" by "extension".
1959 __isl_give isl_schedule_node *isl_schedule_node_extension_set_extension(
1960 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
1962 isl_schedule_tree *tree;
1964 if (!node || !extension)
1965 goto error;
1967 tree = isl_schedule_tree_copy(node->tree);
1968 tree = isl_schedule_tree_extension_set_extension(tree, extension);
1969 return isl_schedule_node_graft_tree(node, tree);
1970 error:
1971 isl_schedule_node_free(node);
1972 isl_union_map_free(extension);
1973 return NULL;
1976 /* Return the filter of the filter node "node".
1978 __isl_give isl_union_set *isl_schedule_node_filter_get_filter(
1979 __isl_keep isl_schedule_node *node)
1981 if (!node)
1982 return NULL;
1984 return isl_schedule_tree_filter_get_filter(node->tree);
1987 /* Replace the filter of filter node "node" by "filter".
1989 __isl_give isl_schedule_node *isl_schedule_node_filter_set_filter(
1990 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
1992 isl_schedule_tree *tree;
1994 if (!node || !filter)
1995 goto error;
1997 tree = isl_schedule_tree_copy(node->tree);
1998 tree = isl_schedule_tree_filter_set_filter(tree, filter);
1999 return isl_schedule_node_graft_tree(node, tree);
2000 error:
2001 isl_schedule_node_free(node);
2002 isl_union_set_free(filter);
2003 return NULL;
2006 /* Return the guard of the guard node "node".
2008 __isl_give isl_set *isl_schedule_node_guard_get_guard(
2009 __isl_keep isl_schedule_node *node)
2011 if (!node)
2012 return NULL;
2014 return isl_schedule_tree_guard_get_guard(node->tree);
2017 /* Return the mark identifier of the mark node "node".
2019 __isl_give isl_id *isl_schedule_node_mark_get_id(
2020 __isl_keep isl_schedule_node *node)
2022 if (!node)
2023 return NULL;
2025 return isl_schedule_tree_mark_get_id(node->tree);
2028 /* Replace the child at position "pos" of the sequence node "node"
2029 * by the children of sequence root node of "tree".
2031 __isl_give isl_schedule_node *isl_schedule_node_sequence_splice(
2032 __isl_take isl_schedule_node *node, int pos,
2033 __isl_take isl_schedule_tree *tree)
2035 isl_schedule_tree *node_tree;
2037 if (!node || !tree)
2038 goto error;
2039 if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
2040 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2041 "not a sequence node", goto error);
2042 if (isl_schedule_tree_get_type(tree) != isl_schedule_node_sequence)
2043 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2044 "not a sequence node", goto error);
2045 node_tree = isl_schedule_node_get_tree(node);
2046 node_tree = isl_schedule_tree_sequence_splice(node_tree, pos, tree);
2047 node = isl_schedule_node_graft_tree(node, node_tree);
2049 return node;
2050 error:
2051 isl_schedule_node_free(node);
2052 isl_schedule_tree_free(tree);
2053 return NULL;
2056 /* Update the ancestors of "node" to point to the tree that "node"
2057 * now points to.
2058 * That is, replace the child in the original parent that corresponds
2059 * to the current tree position by node->tree and continue updating
2060 * the ancestors in the same way until the root is reached.
2062 * If "fn" is not NULL, then it is called on each ancestor as we move up
2063 * the tree so that it can modify the ancestor before it is added
2064 * to the list of ancestors of the modified node.
2065 * The additional "pos" argument records the position
2066 * of the "tree" argument in the original schedule tree.
2068 * If "node" originally points to a leaf of the schedule tree, then make sure
2069 * that in the end it points to a leaf in the updated schedule tree.
2071 static __isl_give isl_schedule_node *update_ancestors(
2072 __isl_take isl_schedule_node *node,
2073 __isl_give isl_schedule_tree *(*fn)(__isl_take isl_schedule_tree *tree,
2074 __isl_keep isl_schedule_node *pos, void *user), void *user)
2076 int i, n;
2077 int is_leaf;
2078 isl_ctx *ctx;
2079 isl_schedule_tree *tree;
2080 isl_schedule_node *pos = NULL;
2082 if (fn)
2083 pos = isl_schedule_node_copy(node);
2085 node = isl_schedule_node_cow(node);
2086 if (!node)
2087 return isl_schedule_node_free(pos);
2089 ctx = isl_schedule_node_get_ctx(node);
2090 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
2091 tree = isl_schedule_tree_copy(node->tree);
2093 for (i = n - 1; i >= 0; --i) {
2094 isl_schedule_tree *parent;
2096 parent = isl_schedule_tree_list_get_schedule_tree(
2097 node->ancestors, i);
2098 parent = isl_schedule_tree_replace_child(parent,
2099 node->child_pos[i], tree);
2100 if (fn) {
2101 pos = isl_schedule_node_parent(pos);
2102 parent = fn(parent, pos, user);
2104 node->ancestors = isl_schedule_tree_list_set_schedule_tree(
2105 node->ancestors, i, isl_schedule_tree_copy(parent));
2107 tree = parent;
2110 if (fn)
2111 isl_schedule_node_free(pos);
2113 is_leaf = isl_schedule_tree_is_leaf(node->tree);
2114 node->schedule = isl_schedule_set_root(node->schedule, tree);
2115 if (is_leaf) {
2116 isl_schedule_tree_free(node->tree);
2117 node->tree = isl_schedule_node_get_leaf(node);
2120 if (!node->schedule || !node->ancestors)
2121 return isl_schedule_node_free(node);
2123 return node;
2126 /* Replace the subtree that "pos" points to by "tree", updating
2127 * the ancestors to maintain a consistent state.
2129 __isl_give isl_schedule_node *isl_schedule_node_graft_tree(
2130 __isl_take isl_schedule_node *pos, __isl_take isl_schedule_tree *tree)
2132 if (!tree || !pos)
2133 goto error;
2134 if (pos->tree == tree) {
2135 isl_schedule_tree_free(tree);
2136 return pos;
2139 pos = isl_schedule_node_cow(pos);
2140 if (!pos)
2141 goto error;
2143 isl_schedule_tree_free(pos->tree);
2144 pos->tree = tree;
2146 return update_ancestors(pos, NULL, NULL);
2147 error:
2148 isl_schedule_node_free(pos);
2149 isl_schedule_tree_free(tree);
2150 return NULL;
2153 /* Make sure we can insert a node between "node" and its parent.
2154 * Return -1 on error, reporting the reason why we cannot insert a node.
2156 static int check_insert(__isl_keep isl_schedule_node *node)
2158 int has_parent;
2159 enum isl_schedule_node_type type;
2161 has_parent = isl_schedule_node_has_parent(node);
2162 if (has_parent < 0)
2163 return -1;
2164 if (!has_parent)
2165 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2166 "cannot insert node outside of root", return -1);
2168 type = isl_schedule_node_get_parent_type(node);
2169 if (type == isl_schedule_node_error)
2170 return -1;
2171 if (type == isl_schedule_node_set || type == isl_schedule_node_sequence)
2172 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2173 "cannot insert node between set or sequence node "
2174 "and its filter children", return -1);
2176 return 0;
2179 /* Insert a band node with partial schedule "mupa" between "node" and
2180 * its parent.
2181 * Return a pointer to the new band node.
2183 * If any of the nodes in the subtree rooted at "node" depend on
2184 * the set of outer band nodes then we refuse to insert the band node.
2186 __isl_give isl_schedule_node *isl_schedule_node_insert_partial_schedule(
2187 __isl_take isl_schedule_node *node,
2188 __isl_take isl_multi_union_pw_aff *mupa)
2190 int anchored;
2191 isl_schedule_band *band;
2192 isl_schedule_tree *tree;
2194 if (check_insert(node) < 0)
2195 node = isl_schedule_node_free(node);
2196 anchored = isl_schedule_node_is_subtree_anchored(node);
2197 if (anchored < 0)
2198 goto error;
2199 if (anchored)
2200 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2201 "cannot insert band node in anchored subtree",
2202 goto error);
2204 tree = isl_schedule_node_get_tree(node);
2205 band = isl_schedule_band_from_multi_union_pw_aff(mupa);
2206 tree = isl_schedule_tree_insert_band(tree, band);
2207 node = isl_schedule_node_graft_tree(node, tree);
2209 return node;
2210 error:
2211 isl_schedule_node_free(node);
2212 isl_multi_union_pw_aff_free(mupa);
2213 return NULL;
2216 /* Insert a context node with context "context" between "node" and its parent.
2217 * Return a pointer to the new context node.
2219 __isl_give isl_schedule_node *isl_schedule_node_insert_context(
2220 __isl_take isl_schedule_node *node, __isl_take isl_set *context)
2222 isl_schedule_tree *tree;
2224 if (check_insert(node) < 0)
2225 node = isl_schedule_node_free(node);
2227 tree = isl_schedule_node_get_tree(node);
2228 tree = isl_schedule_tree_insert_context(tree, context);
2229 node = isl_schedule_node_graft_tree(node, tree);
2231 return node;
2234 /* Insert an expansion node with the given "contraction" and "expansion"
2235 * between "node" and its parent.
2236 * Return a pointer to the new expansion node.
2238 * Typically the domain and range spaces of the expansion are different.
2239 * This means that only one of them can refer to the current domain space
2240 * in a consistent tree. It is up to the caller to ensure that the tree
2241 * returns to a consistent state.
2243 __isl_give isl_schedule_node *isl_schedule_node_insert_expansion(
2244 __isl_take isl_schedule_node *node,
2245 __isl_take isl_union_pw_multi_aff *contraction,
2246 __isl_take isl_union_map *expansion)
2248 isl_schedule_tree *tree;
2250 if (check_insert(node) < 0)
2251 node = isl_schedule_node_free(node);
2253 tree = isl_schedule_node_get_tree(node);
2254 tree = isl_schedule_tree_insert_expansion(tree, contraction, expansion);
2255 node = isl_schedule_node_graft_tree(node, tree);
2257 return node;
2260 /* Insert an extension node with extension "extension" between "node" and
2261 * its parent.
2262 * Return a pointer to the new extension node.
2264 __isl_give isl_schedule_node *isl_schedule_node_insert_extension(
2265 __isl_take isl_schedule_node *node,
2266 __isl_take isl_union_map *extension)
2268 isl_schedule_tree *tree;
2270 tree = isl_schedule_node_get_tree(node);
2271 tree = isl_schedule_tree_insert_extension(tree, extension);
2272 node = isl_schedule_node_graft_tree(node, tree);
2274 return node;
2277 /* Insert a filter node with filter "filter" between "node" and its parent.
2278 * Return a pointer to the new filter node.
2280 __isl_give isl_schedule_node *isl_schedule_node_insert_filter(
2281 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
2283 isl_schedule_tree *tree;
2285 if (check_insert(node) < 0)
2286 node = isl_schedule_node_free(node);
2288 tree = isl_schedule_node_get_tree(node);
2289 tree = isl_schedule_tree_insert_filter(tree, filter);
2290 node = isl_schedule_node_graft_tree(node, tree);
2292 return node;
2295 /* Insert a guard node with guard "guard" between "node" and its parent.
2296 * Return a pointer to the new guard node.
2298 __isl_give isl_schedule_node *isl_schedule_node_insert_guard(
2299 __isl_take isl_schedule_node *node, __isl_take isl_set *guard)
2301 isl_schedule_tree *tree;
2303 if (check_insert(node) < 0)
2304 node = isl_schedule_node_free(node);
2306 tree = isl_schedule_node_get_tree(node);
2307 tree = isl_schedule_tree_insert_guard(tree, guard);
2308 node = isl_schedule_node_graft_tree(node, tree);
2310 return node;
2313 /* Insert a mark node with mark identifier "mark" between "node" and
2314 * its parent.
2315 * Return a pointer to the new mark node.
2317 __isl_give isl_schedule_node *isl_schedule_node_insert_mark(
2318 __isl_take isl_schedule_node *node, __isl_take isl_id *mark)
2320 isl_schedule_tree *tree;
2322 if (check_insert(node) < 0)
2323 node = isl_schedule_node_free(node);
2325 tree = isl_schedule_node_get_tree(node);
2326 tree = isl_schedule_tree_insert_mark(tree, mark);
2327 node = isl_schedule_node_graft_tree(node, tree);
2329 return node;
2332 /* Attach the current subtree of "node" to a sequence of filter tree nodes
2333 * with filters described by "filters", attach this sequence
2334 * of filter tree nodes as children to a new tree of type "type" and
2335 * replace the original subtree of "node" by this new tree.
2337 static __isl_give isl_schedule_node *isl_schedule_node_insert_children(
2338 __isl_take isl_schedule_node *node,
2339 enum isl_schedule_node_type type,
2340 __isl_take isl_union_set_list *filters)
2342 int i, n;
2343 isl_ctx *ctx;
2344 isl_schedule_tree *tree;
2345 isl_schedule_tree_list *list;
2347 if (check_insert(node) < 0)
2348 node = isl_schedule_node_free(node);
2350 if (!node || !filters)
2351 goto error;
2353 ctx = isl_schedule_node_get_ctx(node);
2354 n = isl_union_set_list_n_union_set(filters);
2355 list = isl_schedule_tree_list_alloc(ctx, n);
2356 for (i = 0; i < n; ++i) {
2357 isl_schedule_tree *tree;
2358 isl_union_set *filter;
2360 tree = isl_schedule_node_get_tree(node);
2361 filter = isl_union_set_list_get_union_set(filters, i);
2362 tree = isl_schedule_tree_insert_filter(tree, filter);
2363 list = isl_schedule_tree_list_add(list, tree);
2365 tree = isl_schedule_tree_from_children(type, list);
2366 node = isl_schedule_node_graft_tree(node, tree);
2368 isl_union_set_list_free(filters);
2369 return node;
2370 error:
2371 isl_union_set_list_free(filters);
2372 isl_schedule_node_free(node);
2373 return NULL;
2376 /* Insert a sequence node with child filters "filters" between "node" and
2377 * its parent. That is, the tree that "node" points to is attached
2378 * to each of the child nodes of the filter nodes.
2379 * Return a pointer to the new sequence node.
2381 __isl_give isl_schedule_node *isl_schedule_node_insert_sequence(
2382 __isl_take isl_schedule_node *node,
2383 __isl_take isl_union_set_list *filters)
2385 return isl_schedule_node_insert_children(node,
2386 isl_schedule_node_sequence, filters);
2389 /* Insert a set node with child filters "filters" between "node" and
2390 * its parent. That is, the tree that "node" points to is attached
2391 * to each of the child nodes of the filter nodes.
2392 * Return a pointer to the new set node.
2394 __isl_give isl_schedule_node *isl_schedule_node_insert_set(
2395 __isl_take isl_schedule_node *node,
2396 __isl_take isl_union_set_list *filters)
2398 return isl_schedule_node_insert_children(node,
2399 isl_schedule_node_set, filters);
2402 /* Remove "node" from its schedule tree and return a pointer
2403 * to the leaf at the same position in the updated schedule tree.
2405 * It is not allowed to remove the root of a schedule tree or
2406 * a child of a set or sequence node.
2408 __isl_give isl_schedule_node *isl_schedule_node_cut(
2409 __isl_take isl_schedule_node *node)
2411 isl_schedule_tree *leaf;
2412 enum isl_schedule_node_type parent_type;
2414 if (!node)
2415 return NULL;
2416 if (!isl_schedule_node_has_parent(node))
2417 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2418 "cannot cut root", return isl_schedule_node_free(node));
2420 parent_type = isl_schedule_node_get_parent_type(node);
2421 if (parent_type == isl_schedule_node_set ||
2422 parent_type == isl_schedule_node_sequence)
2423 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2424 "cannot cut child of set or sequence",
2425 return isl_schedule_node_free(node));
2427 leaf = isl_schedule_node_get_leaf(node);
2428 return isl_schedule_node_graft_tree(node, leaf);
2431 /* Remove a single node from the schedule tree, attaching the child
2432 * of "node" directly to its parent.
2433 * Return a pointer to this former child or to the leaf the position
2434 * of the original node if there was no child.
2435 * It is not allowed to remove the root of a schedule tree,
2436 * a set or sequence node, a child of a set or sequence node or
2437 * a band node with an anchored subtree.
2439 __isl_give isl_schedule_node *isl_schedule_node_delete(
2440 __isl_take isl_schedule_node *node)
2442 int n;
2443 isl_schedule_tree *tree;
2444 enum isl_schedule_node_type type;
2446 if (!node)
2447 return NULL;
2449 if (isl_schedule_node_get_tree_depth(node) == 0)
2450 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2451 "cannot delete root node",
2452 return isl_schedule_node_free(node));
2453 n = isl_schedule_node_n_children(node);
2454 if (n != 1)
2455 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2456 "can only delete node with a single child",
2457 return isl_schedule_node_free(node));
2458 type = isl_schedule_node_get_parent_type(node);
2459 if (type == isl_schedule_node_sequence || type == isl_schedule_node_set)
2460 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2461 "cannot delete child of set or sequence",
2462 return isl_schedule_node_free(node));
2463 if (isl_schedule_node_get_type(node) == isl_schedule_node_band) {
2464 int anchored;
2466 anchored = isl_schedule_node_is_subtree_anchored(node);
2467 if (anchored < 0)
2468 return isl_schedule_node_free(node);
2469 if (anchored)
2470 isl_die(isl_schedule_node_get_ctx(node),
2471 isl_error_invalid,
2472 "cannot delete band node with anchored subtree",
2473 return isl_schedule_node_free(node));
2476 tree = isl_schedule_node_get_tree(node);
2477 if (!tree || isl_schedule_tree_has_children(tree)) {
2478 tree = isl_schedule_tree_child(tree, 0);
2479 } else {
2480 isl_schedule_tree_free(tree);
2481 tree = isl_schedule_node_get_leaf(node);
2483 node = isl_schedule_node_graft_tree(node, tree);
2485 return node;
2488 /* Internal data structure for the group_ancestor callback.
2490 * If "finished" is set, then we no longer need to modify
2491 * any further ancestors.
2493 * "contraction" and "expansion" represent the expansion
2494 * that reflects the grouping.
2496 * "domain" contains the domain elements that reach the position
2497 * where the grouping is performed. That is, it is the range
2498 * of the resulting expansion.
2499 * "domain_universe" is the universe of "domain".
2500 * "group" is the set of group elements, i.e., the domain
2501 * of the resulting expansion.
2502 * "group_universe" is the universe of "group".
2504 * "sched" is the schedule for the group elements, in pratice
2505 * an identity mapping on "group_universe".
2506 * "dim" is the dimension of "sched".
2508 struct isl_schedule_group_data {
2509 int finished;
2511 isl_union_map *expansion;
2512 isl_union_pw_multi_aff *contraction;
2514 isl_union_set *domain;
2515 isl_union_set *domain_universe;
2516 isl_union_set *group;
2517 isl_union_set *group_universe;
2519 int dim;
2520 isl_multi_aff *sched;
2523 /* Is domain covered by data->domain within data->domain_universe?
2525 static int locally_covered_by_domain(__isl_keep isl_union_set *domain,
2526 struct isl_schedule_group_data *data)
2528 int is_subset;
2529 isl_union_set *test;
2531 test = isl_union_set_copy(domain);
2532 test = isl_union_set_intersect(test,
2533 isl_union_set_copy(data->domain_universe));
2534 is_subset = isl_union_set_is_subset(test, data->domain);
2535 isl_union_set_free(test);
2537 return is_subset;
2540 /* Update the band tree root "tree" to refer to the group instances
2541 * in data->group rather than the original domain elements in data->domain.
2542 * "pos" is the position in the original schedule tree where the modified
2543 * "tree" will be attached.
2545 * Add the part of the identity schedule on the group instances data->sched
2546 * that corresponds to this band node to the band schedule.
2547 * If the domain elements that reach the node and that are part
2548 * of data->domain_universe are all elements of data->domain (and therefore
2549 * replaced by the group instances) then this data->domain_universe
2550 * is removed from the domain of the band schedule.
2552 static __isl_give isl_schedule_tree *group_band(
2553 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2554 struct isl_schedule_group_data *data)
2556 isl_union_set *domain;
2557 isl_multi_aff *ma;
2558 isl_multi_union_pw_aff *mupa, *partial;
2559 int is_covered;
2560 int depth, n, has_id;
2562 domain = isl_schedule_node_get_domain(pos);
2563 is_covered = locally_covered_by_domain(domain, data);
2564 if (is_covered >= 0 && is_covered) {
2565 domain = isl_union_set_universe(domain);
2566 domain = isl_union_set_subtract(domain,
2567 isl_union_set_copy(data->domain_universe));
2568 tree = isl_schedule_tree_band_intersect_domain(tree, domain);
2569 } else
2570 isl_union_set_free(domain);
2571 if (is_covered < 0)
2572 return isl_schedule_tree_free(tree);
2573 depth = isl_schedule_node_get_schedule_depth(pos);
2574 n = isl_schedule_tree_band_n_member(tree);
2575 ma = isl_multi_aff_copy(data->sched);
2576 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, depth);
2577 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, n, data->dim - depth - n);
2578 mupa = isl_multi_union_pw_aff_from_multi_aff(ma);
2579 partial = isl_schedule_tree_band_get_partial_schedule(tree);
2580 has_id = isl_multi_union_pw_aff_has_tuple_id(partial, isl_dim_set);
2581 if (has_id < 0) {
2582 partial = isl_multi_union_pw_aff_free(partial);
2583 } else if (has_id) {
2584 isl_id *id;
2585 id = isl_multi_union_pw_aff_get_tuple_id(partial, isl_dim_set);
2586 mupa = isl_multi_union_pw_aff_set_tuple_id(mupa,
2587 isl_dim_set, id);
2589 partial = isl_multi_union_pw_aff_union_add(partial, mupa);
2590 tree = isl_schedule_tree_band_set_partial_schedule(tree, partial);
2592 return tree;
2595 /* Drop the parameters in "uset" that are not also in "space".
2596 * "n" is the number of parameters in "space".
2598 static __isl_give isl_union_set *union_set_drop_extra_params(
2599 __isl_take isl_union_set *uset, __isl_keep isl_space *space, int n)
2601 int n2;
2603 uset = isl_union_set_align_params(uset, isl_space_copy(space));
2604 n2 = isl_union_set_dim(uset, isl_dim_param);
2605 uset = isl_union_set_project_out(uset, isl_dim_param, n, n2 - n);
2607 return uset;
2610 /* Update the context tree root "tree" to refer to the group instances
2611 * in data->group rather than the original domain elements in data->domain.
2612 * "pos" is the position in the original schedule tree where the modified
2613 * "tree" will be attached.
2615 * We do not actually need to update "tree" since a context node only
2616 * refers to the schedule space. However, we may need to update "data"
2617 * to not refer to any parameters introduced by the context node.
2619 static __isl_give isl_schedule_tree *group_context(
2620 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2621 struct isl_schedule_group_data *data)
2623 isl_space *space;
2624 isl_union_set *domain;
2625 int n1, n2;
2626 int involves;
2628 if (isl_schedule_node_get_tree_depth(pos) == 1)
2629 return tree;
2631 domain = isl_schedule_node_get_universe_domain(pos);
2632 space = isl_union_set_get_space(domain);
2633 isl_union_set_free(domain);
2635 n1 = isl_space_dim(space, isl_dim_param);
2636 data->expansion = isl_union_map_align_params(data->expansion, space);
2637 n2 = isl_union_map_dim(data->expansion, isl_dim_param);
2639 if (!data->expansion)
2640 return isl_schedule_tree_free(tree);
2641 if (n1 == n2)
2642 return tree;
2644 involves = isl_union_map_involves_dims(data->expansion,
2645 isl_dim_param, n1, n2 - n1);
2646 if (involves < 0)
2647 return isl_schedule_tree_free(tree);
2648 if (involves)
2649 isl_die(isl_schedule_node_get_ctx(pos), isl_error_invalid,
2650 "grouping cannot only refer to global parameters",
2651 return isl_schedule_tree_free(tree));
2653 data->expansion = isl_union_map_project_out(data->expansion,
2654 isl_dim_param, n1, n2 - n1);
2655 space = isl_union_map_get_space(data->expansion);
2657 data->contraction = isl_union_pw_multi_aff_align_params(
2658 data->contraction, isl_space_copy(space));
2659 n2 = isl_union_pw_multi_aff_dim(data->contraction, isl_dim_param);
2660 data->contraction = isl_union_pw_multi_aff_drop_dims(data->contraction,
2661 isl_dim_param, n1, n2 - n1);
2663 data->domain = union_set_drop_extra_params(data->domain, space, n1);
2664 data->domain_universe =
2665 union_set_drop_extra_params(data->domain_universe, space, n1);
2666 data->group = union_set_drop_extra_params(data->group, space, n1);
2667 data->group_universe =
2668 union_set_drop_extra_params(data->group_universe, space, n1);
2670 data->sched = isl_multi_aff_align_params(data->sched,
2671 isl_space_copy(space));
2672 n2 = isl_multi_aff_dim(data->sched, isl_dim_param);
2673 data->sched = isl_multi_aff_drop_dims(data->sched,
2674 isl_dim_param, n1, n2 - n1);
2676 isl_space_free(space);
2678 return tree;
2681 /* Update the domain tree root "tree" to refer to the group instances
2682 * in data->group rather than the original domain elements in data->domain.
2683 * "pos" is the position in the original schedule tree where the modified
2684 * "tree" will be attached.
2686 * We first double-check that all grouped domain elements are actually
2687 * part of the root domain and then replace those elements by the group
2688 * instances.
2690 static __isl_give isl_schedule_tree *group_domain(
2691 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2692 struct isl_schedule_group_data *data)
2694 isl_union_set *domain;
2695 int is_subset;
2697 domain = isl_schedule_tree_domain_get_domain(tree);
2698 is_subset = isl_union_set_is_subset(data->domain, domain);
2699 isl_union_set_free(domain);
2700 if (is_subset < 0)
2701 return isl_schedule_tree_free(tree);
2702 if (!is_subset)
2703 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
2704 "grouped domain should be part of outer domain",
2705 return isl_schedule_tree_free(tree));
2706 domain = isl_schedule_tree_domain_get_domain(tree);
2707 domain = isl_union_set_subtract(domain,
2708 isl_union_set_copy(data->domain));
2709 domain = isl_union_set_union(domain, isl_union_set_copy(data->group));
2710 tree = isl_schedule_tree_domain_set_domain(tree, domain);
2712 return tree;
2715 /* Update the expansion tree root "tree" to refer to the group instances
2716 * in data->group rather than the original domain elements in data->domain.
2717 * "pos" is the position in the original schedule tree where the modified
2718 * "tree" will be attached.
2720 * Let G_1 -> D_1 be the expansion of "tree" and G_2 -> D_2 the newly
2721 * introduced expansion in a descendant of "tree".
2722 * We first double-check that D_2 is a subset of D_1.
2723 * Then we remove D_2 from the range of G_1 -> D_1 and add the mapping
2724 * G_1 -> D_1 . D_2 -> G_2.
2725 * Simmilarly, we restrict the domain of the contraction to the universe
2726 * of the range of the updated expansion and add G_2 -> D_2 . D_1 -> G_1,
2727 * attempting to remove the domain constraints of this additional part.
2729 static __isl_give isl_schedule_tree *group_expansion(
2730 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2731 struct isl_schedule_group_data *data)
2733 isl_union_set *domain;
2734 isl_union_map *expansion, *umap;
2735 isl_union_pw_multi_aff *contraction, *upma;
2736 int is_subset;
2738 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2739 domain = isl_union_map_range(expansion);
2740 is_subset = isl_union_set_is_subset(data->domain, domain);
2741 isl_union_set_free(domain);
2742 if (is_subset < 0)
2743 return isl_schedule_tree_free(tree);
2744 if (!is_subset)
2745 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
2746 "grouped domain should be part "
2747 "of outer expansion domain",
2748 return isl_schedule_tree_free(tree));
2749 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2750 umap = isl_union_map_from_union_pw_multi_aff(
2751 isl_union_pw_multi_aff_copy(data->contraction));
2752 umap = isl_union_map_apply_range(expansion, umap);
2753 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2754 expansion = isl_union_map_subtract_range(expansion,
2755 isl_union_set_copy(data->domain));
2756 expansion = isl_union_map_union(expansion, umap);
2757 umap = isl_union_map_universe(isl_union_map_copy(expansion));
2758 domain = isl_union_map_range(umap);
2759 contraction = isl_schedule_tree_expansion_get_contraction(tree);
2760 umap = isl_union_map_from_union_pw_multi_aff(contraction);
2761 umap = isl_union_map_apply_range(isl_union_map_copy(data->expansion),
2762 umap);
2763 upma = isl_union_pw_multi_aff_from_union_map(umap);
2764 contraction = isl_schedule_tree_expansion_get_contraction(tree);
2765 contraction = isl_union_pw_multi_aff_intersect_domain(contraction,
2766 domain);
2767 domain = isl_union_pw_multi_aff_domain(
2768 isl_union_pw_multi_aff_copy(upma));
2769 upma = isl_union_pw_multi_aff_gist(upma, domain);
2770 contraction = isl_union_pw_multi_aff_union_add(contraction, upma);
2771 tree = isl_schedule_tree_expansion_set_contraction_and_expansion(tree,
2772 contraction, expansion);
2774 return tree;
2777 /* Update the tree root "tree" to refer to the group instances
2778 * in data->group rather than the original domain elements in data->domain.
2779 * "pos" is the position in the original schedule tree where the modified
2780 * "tree" will be attached.
2782 * If we have come across a domain or expansion node before (data->finished
2783 * is set), then we no longer need perform any modifications.
2785 * If "tree" is a filter, then we add data->group_universe to the filter.
2786 * We also remove data->domain_universe from the filter if all the domain
2787 * elements in this universe that reach the filter node are part of
2788 * the elements that are being grouped by data->expansion.
2789 * If "tree" is a band, domain or expansion, then it is handled
2790 * in a separate function.
2792 static __isl_give isl_schedule_tree *group_ancestor(
2793 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2794 void *user)
2796 struct isl_schedule_group_data *data = user;
2797 isl_union_set *domain;
2798 int is_covered;
2800 if (!tree || !pos)
2801 return isl_schedule_tree_free(tree);
2803 if (data->finished)
2804 return tree;
2806 switch (isl_schedule_tree_get_type(tree)) {
2807 case isl_schedule_node_error:
2808 return isl_schedule_tree_free(tree);
2809 case isl_schedule_node_extension:
2810 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
2811 "grouping not allowed in extended tree",
2812 return isl_schedule_tree_free(tree));
2813 case isl_schedule_node_band:
2814 tree = group_band(tree, pos, data);
2815 break;
2816 case isl_schedule_node_context:
2817 tree = group_context(tree, pos, data);
2818 break;
2819 case isl_schedule_node_domain:
2820 tree = group_domain(tree, pos, data);
2821 data->finished = 1;
2822 break;
2823 case isl_schedule_node_filter:
2824 domain = isl_schedule_node_get_domain(pos);
2825 is_covered = locally_covered_by_domain(domain, data);
2826 isl_union_set_free(domain);
2827 if (is_covered < 0)
2828 return isl_schedule_tree_free(tree);
2829 domain = isl_schedule_tree_filter_get_filter(tree);
2830 if (is_covered)
2831 domain = isl_union_set_subtract(domain,
2832 isl_union_set_copy(data->domain_universe));
2833 domain = isl_union_set_union(domain,
2834 isl_union_set_copy(data->group_universe));
2835 tree = isl_schedule_tree_filter_set_filter(tree, domain);
2836 break;
2837 case isl_schedule_node_expansion:
2838 tree = group_expansion(tree, pos, data);
2839 data->finished = 1;
2840 break;
2841 case isl_schedule_node_leaf:
2842 case isl_schedule_node_guard:
2843 case isl_schedule_node_mark:
2844 case isl_schedule_node_sequence:
2845 case isl_schedule_node_set:
2846 break;
2849 return tree;
2852 /* Group the domain elements that reach "node" into instances
2853 * of a single statement with identifier "group_id".
2854 * In particular, group the domain elements according to their
2855 * prefix schedule.
2857 * That is, introduce an expansion node with as contraction
2858 * the prefix schedule (with the target space replaced by "group_id")
2859 * and as expansion the inverse of this contraction (with its range
2860 * intersected with the domain elements that reach "node").
2861 * The outer nodes are then modified to refer to the group instances
2862 * instead of the original domain elements.
2864 * No instance of "group_id" is allowed to reach "node" prior
2865 * to the grouping.
2866 * No ancestor of "node" is allowed to be an extension node.
2868 * Return a pointer to original node in tree, i.e., the child
2869 * of the newly introduced expansion node.
2871 __isl_give isl_schedule_node *isl_schedule_node_group(
2872 __isl_take isl_schedule_node *node, __isl_take isl_id *group_id)
2874 struct isl_schedule_group_data data = { 0 };
2875 isl_space *space;
2876 isl_union_set *domain;
2877 isl_union_pw_multi_aff *contraction;
2878 isl_union_map *expansion;
2879 int disjoint;
2881 if (!node || !group_id)
2882 goto error;
2883 if (check_insert(node) < 0)
2884 goto error;
2886 domain = isl_schedule_node_get_domain(node);
2887 data.domain = isl_union_set_copy(domain);
2888 data.domain_universe = isl_union_set_copy(domain);
2889 data.domain_universe = isl_union_set_universe(data.domain_universe);
2891 data.dim = isl_schedule_node_get_schedule_depth(node);
2892 if (data.dim == 0) {
2893 isl_ctx *ctx;
2894 isl_set *set;
2895 isl_union_set *group;
2896 isl_union_map *univ;
2898 ctx = isl_schedule_node_get_ctx(node);
2899 space = isl_space_set_alloc(ctx, 0, 0);
2900 space = isl_space_set_tuple_id(space, isl_dim_set, group_id);
2901 set = isl_set_universe(isl_space_copy(space));
2902 group = isl_union_set_from_set(set);
2903 expansion = isl_union_map_from_domain_and_range(domain, group);
2904 univ = isl_union_map_universe(isl_union_map_copy(expansion));
2905 contraction = isl_union_pw_multi_aff_from_union_map(univ);
2906 expansion = isl_union_map_reverse(expansion);
2907 } else {
2908 isl_multi_union_pw_aff *prefix;
2909 isl_union_set *univ;
2911 prefix =
2912 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
2913 prefix = isl_multi_union_pw_aff_set_tuple_id(prefix,
2914 isl_dim_set, group_id);
2915 space = isl_multi_union_pw_aff_get_space(prefix);
2916 contraction = isl_union_pw_multi_aff_from_multi_union_pw_aff(
2917 prefix);
2918 univ = isl_union_set_universe(isl_union_set_copy(domain));
2919 contraction =
2920 isl_union_pw_multi_aff_intersect_domain(contraction, univ);
2921 expansion = isl_union_map_from_union_pw_multi_aff(
2922 isl_union_pw_multi_aff_copy(contraction));
2923 expansion = isl_union_map_reverse(expansion);
2924 expansion = isl_union_map_intersect_range(expansion, domain);
2926 space = isl_space_map_from_set(space);
2927 data.sched = isl_multi_aff_identity(space);
2928 data.group = isl_union_map_domain(isl_union_map_copy(expansion));
2929 data.group = isl_union_set_coalesce(data.group);
2930 data.group_universe = isl_union_set_copy(data.group);
2931 data.group_universe = isl_union_set_universe(data.group_universe);
2932 data.expansion = isl_union_map_copy(expansion);
2933 data.contraction = isl_union_pw_multi_aff_copy(contraction);
2934 node = isl_schedule_node_insert_expansion(node, contraction, expansion);
2936 disjoint = isl_union_set_is_disjoint(data.domain_universe,
2937 data.group_universe);
2939 node = update_ancestors(node, &group_ancestor, &data);
2941 isl_union_set_free(data.domain);
2942 isl_union_set_free(data.domain_universe);
2943 isl_union_set_free(data.group);
2944 isl_union_set_free(data.group_universe);
2945 isl_multi_aff_free(data.sched);
2946 isl_union_map_free(data.expansion);
2947 isl_union_pw_multi_aff_free(data.contraction);
2949 node = isl_schedule_node_child(node, 0);
2951 if (!node || disjoint < 0)
2952 return isl_schedule_node_free(node);
2953 if (!disjoint)
2954 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2955 "group instances already reach node",
2956 isl_schedule_node_free(node));
2958 return node;
2959 error:
2960 isl_schedule_node_free(node);
2961 isl_id_free(group_id);
2962 return NULL;
2965 /* Compute the gist of the given band node with respect to "context".
2967 __isl_give isl_schedule_node *isl_schedule_node_band_gist(
2968 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
2970 isl_schedule_tree *tree;
2972 tree = isl_schedule_node_get_tree(node);
2973 tree = isl_schedule_tree_band_gist(tree, context);
2974 return isl_schedule_node_graft_tree(node, tree);
2977 /* Internal data structure for isl_schedule_node_gist.
2978 * "n_expansion" is the number of outer expansion nodes
2979 * with respect to the current position
2980 * "filters" contains an element for each outer filter, expansion or
2981 * extension node with respect to the current position, each representing
2982 * the intersection of the previous element and the filter on the filter node
2983 * or the expansion/extension of the previous element.
2984 * The first element in the original context passed to isl_schedule_node_gist.
2986 struct isl_node_gist_data {
2987 int n_expansion;
2988 isl_union_set_list *filters;
2991 /* Enter the expansion node "node" during a isl_schedule_node_gist traversal.
2993 * In particular, add an extra element to data->filters containing
2994 * the expansion of the previous element and replace the expansion
2995 * and contraction on "node" by the gist with respect to these filters.
2996 * Also keep track of the fact that we have entered another expansion.
2998 static __isl_give isl_schedule_node *gist_enter_expansion(
2999 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3001 int n;
3002 isl_union_set *inner;
3003 isl_union_map *expansion;
3004 isl_union_pw_multi_aff *contraction;
3006 data->n_expansion++;
3008 n = isl_union_set_list_n_union_set(data->filters);
3009 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3010 expansion = isl_schedule_node_expansion_get_expansion(node);
3011 inner = isl_union_set_apply(inner, expansion);
3013 contraction = isl_schedule_node_expansion_get_contraction(node);
3014 contraction = isl_union_pw_multi_aff_gist(contraction,
3015 isl_union_set_copy(inner));
3017 data->filters = isl_union_set_list_add(data->filters, inner);
3019 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3020 expansion = isl_schedule_node_expansion_get_expansion(node);
3021 expansion = isl_union_map_gist_domain(expansion, inner);
3022 node = isl_schedule_node_expansion_set_contraction_and_expansion(node,
3023 contraction, expansion);
3025 return node;
3028 /* Enter the extension node "node" during a isl_schedule_node_gist traversal.
3030 * In particular, add an extra element to data->filters containing
3031 * the union of the previous element with the additional domain elements
3032 * introduced by the extension.
3034 static __isl_give isl_schedule_node *gist_enter_extension(
3035 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3037 int n;
3038 isl_union_set *inner, *extra;
3039 isl_union_map *extension;
3041 n = isl_union_set_list_n_union_set(data->filters);
3042 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3043 extension = isl_schedule_node_extension_get_extension(node);
3044 extra = isl_union_map_range(extension);
3045 inner = isl_union_set_union(inner, extra);
3047 data->filters = isl_union_set_list_add(data->filters, inner);
3049 return node;
3052 /* Can we finish gisting at this node?
3053 * That is, is the filter on the current filter node a subset of
3054 * the original context passed to isl_schedule_node_gist?
3055 * If we have gone through any expansions, then we cannot perform
3056 * this test since the current domain elements are incomparable
3057 * to the domain elements in the original context.
3059 static int gist_done(__isl_keep isl_schedule_node *node,
3060 struct isl_node_gist_data *data)
3062 isl_union_set *filter, *outer;
3063 int subset;
3065 if (data->n_expansion != 0)
3066 return 0;
3068 filter = isl_schedule_node_filter_get_filter(node);
3069 outer = isl_union_set_list_get_union_set(data->filters, 0);
3070 subset = isl_union_set_is_subset(filter, outer);
3071 isl_union_set_free(outer);
3072 isl_union_set_free(filter);
3074 return subset;
3077 /* Callback for "traverse" to enter a node and to move
3078 * to the deepest initial subtree that should be traversed
3079 * by isl_schedule_node_gist.
3081 * The "filters" list is extended by one element each time
3082 * we come across a filter node by the result of intersecting
3083 * the last element in the list with the filter on the filter node.
3085 * If the filter on the current filter node is a subset of
3086 * the original context passed to isl_schedule_node_gist,
3087 * then there is no need to go into its subtree since it cannot
3088 * be further simplified by the context. The "filters" list is
3089 * still extended for consistency, but the actual value of the
3090 * added element is immaterial since it will not be used.
3092 * Otherwise, the filter on the current filter node is replaced by
3093 * the gist of the original filter with respect to the intersection
3094 * of the original context with the intermediate filters.
3096 * If the new element in the "filters" list is empty, then no elements
3097 * can reach the descendants of the current filter node. The subtree
3098 * underneath the filter node is therefore removed.
3100 * Each expansion node we come across is handled by
3101 * gist_enter_expansion.
3103 * Each extension node we come across is handled by
3104 * gist_enter_extension.
3106 static __isl_give isl_schedule_node *gist_enter(
3107 __isl_take isl_schedule_node *node, void *user)
3109 struct isl_node_gist_data *data = user;
3111 do {
3112 isl_union_set *filter, *inner;
3113 int done, empty;
3114 int n;
3116 switch (isl_schedule_node_get_type(node)) {
3117 case isl_schedule_node_error:
3118 return isl_schedule_node_free(node);
3119 case isl_schedule_node_expansion:
3120 node = gist_enter_expansion(node, data);
3121 continue;
3122 case isl_schedule_node_extension:
3123 node = gist_enter_extension(node, data);
3124 continue;
3125 case isl_schedule_node_band:
3126 case isl_schedule_node_context:
3127 case isl_schedule_node_domain:
3128 case isl_schedule_node_guard:
3129 case isl_schedule_node_leaf:
3130 case isl_schedule_node_mark:
3131 case isl_schedule_node_sequence:
3132 case isl_schedule_node_set:
3133 continue;
3134 case isl_schedule_node_filter:
3135 break;
3137 done = gist_done(node, data);
3138 filter = isl_schedule_node_filter_get_filter(node);
3139 if (done < 0 || done) {
3140 data->filters = isl_union_set_list_add(data->filters,
3141 filter);
3142 if (done < 0)
3143 return isl_schedule_node_free(node);
3144 return node;
3146 n = isl_union_set_list_n_union_set(data->filters);
3147 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3148 filter = isl_union_set_gist(filter, isl_union_set_copy(inner));
3149 node = isl_schedule_node_filter_set_filter(node,
3150 isl_union_set_copy(filter));
3151 filter = isl_union_set_intersect(filter, inner);
3152 empty = isl_union_set_is_empty(filter);
3153 data->filters = isl_union_set_list_add(data->filters, filter);
3154 if (empty < 0)
3155 return isl_schedule_node_free(node);
3156 if (!empty)
3157 continue;
3158 node = isl_schedule_node_child(node, 0);
3159 node = isl_schedule_node_cut(node);
3160 node = isl_schedule_node_parent(node);
3161 return node;
3162 } while (isl_schedule_node_has_children(node) &&
3163 (node = isl_schedule_node_first_child(node)) != NULL);
3165 return node;
3168 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
3170 * In particular, if the current node is a filter node, then we remove
3171 * the element on the "filters" list that was added when we entered
3172 * the node. There is no need to compute any gist here, since we
3173 * already did that when we entered the node.
3175 * If the current node is an expansion, then we decrement
3176 * the number of outer expansions and remove the element
3177 * in data->filters that was added by gist_enter_expansion.
3179 * If the current node is an extension, then remove the element
3180 * in data->filters that was added by gist_enter_extension.
3182 * If the current node is a band node, then we compute the gist of
3183 * the band node with respect to the intersection of the original context
3184 * and the intermediate filters.
3186 * If the current node is a sequence or set node, then some of
3187 * the filter children may have become empty and so they are removed.
3188 * If only one child is left, then the set or sequence node along with
3189 * the single remaining child filter is removed. The filter can be
3190 * removed because the filters on a sequence or set node are supposed
3191 * to partition the incoming domain instances.
3192 * In principle, it should then be impossible for there to be zero
3193 * remaining children, but should this happen, we replace the entire
3194 * subtree with an empty filter.
3196 static __isl_give isl_schedule_node *gist_leave(
3197 __isl_take isl_schedule_node *node, void *user)
3199 struct isl_node_gist_data *data = user;
3200 isl_schedule_tree *tree;
3201 int i, n;
3202 isl_union_set *filter;
3204 switch (isl_schedule_node_get_type(node)) {
3205 case isl_schedule_node_error:
3206 return isl_schedule_node_free(node);
3207 case isl_schedule_node_expansion:
3208 data->n_expansion--;
3209 case isl_schedule_node_extension:
3210 case isl_schedule_node_filter:
3211 n = isl_union_set_list_n_union_set(data->filters);
3212 data->filters = isl_union_set_list_drop(data->filters,
3213 n - 1, 1);
3214 break;
3215 case isl_schedule_node_band:
3216 n = isl_union_set_list_n_union_set(data->filters);
3217 filter = isl_union_set_list_get_union_set(data->filters, n - 1);
3218 node = isl_schedule_node_band_gist(node, filter);
3219 break;
3220 case isl_schedule_node_set:
3221 case isl_schedule_node_sequence:
3222 tree = isl_schedule_node_get_tree(node);
3223 n = isl_schedule_tree_n_children(tree);
3224 for (i = n - 1; i >= 0; --i) {
3225 isl_schedule_tree *child;
3226 isl_union_set *filter;
3227 int empty;
3229 child = isl_schedule_tree_get_child(tree, i);
3230 filter = isl_schedule_tree_filter_get_filter(child);
3231 empty = isl_union_set_is_empty(filter);
3232 isl_union_set_free(filter);
3233 isl_schedule_tree_free(child);
3234 if (empty < 0)
3235 tree = isl_schedule_tree_free(tree);
3236 else if (empty)
3237 tree = isl_schedule_tree_drop_child(tree, i);
3239 n = isl_schedule_tree_n_children(tree);
3240 node = isl_schedule_node_graft_tree(node, tree);
3241 if (n == 1) {
3242 node = isl_schedule_node_delete(node);
3243 node = isl_schedule_node_delete(node);
3244 } else if (n == 0) {
3245 isl_space *space;
3247 filter =
3248 isl_union_set_list_get_union_set(data->filters, 0);
3249 space = isl_union_set_get_space(filter);
3250 isl_union_set_free(filter);
3251 filter = isl_union_set_empty(space);
3252 node = isl_schedule_node_cut(node);
3253 node = isl_schedule_node_insert_filter(node, filter);
3255 break;
3256 case isl_schedule_node_context:
3257 case isl_schedule_node_domain:
3258 case isl_schedule_node_guard:
3259 case isl_schedule_node_leaf:
3260 case isl_schedule_node_mark:
3261 break;
3264 return node;
3267 /* Compute the gist of the subtree at "node" with respect to
3268 * the reaching domain elements in "context".
3269 * In particular, compute the gist of all band and filter nodes
3270 * in the subtree with respect to "context". Children of set or sequence
3271 * nodes that end up with an empty filter are removed completely.
3273 * We keep track of the intersection of "context" with all outer filters
3274 * of the current node within the subtree in the final element of "filters".
3275 * Initially, this list contains the single element "context" and it is
3276 * extended or shortened each time we enter or leave a filter node.
3278 __isl_give isl_schedule_node *isl_schedule_node_gist(
3279 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
3281 struct isl_node_gist_data data;
3283 data.n_expansion = 0;
3284 data.filters = isl_union_set_list_from_union_set(context);
3285 node = traverse(node, &gist_enter, &gist_leave, &data);
3286 isl_union_set_list_free(data.filters);
3287 return node;
3290 /* Intersect the domain of domain node "node" with "domain".
3292 * If the domain of "node" is already a subset of "domain",
3293 * then nothing needs to be changed.
3295 * Otherwise, we replace the domain of the domain node by the intersection
3296 * and simplify the subtree rooted at "node" with respect to this intersection.
3298 __isl_give isl_schedule_node *isl_schedule_node_domain_intersect_domain(
3299 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain)
3301 isl_schedule_tree *tree;
3302 isl_union_set *uset;
3303 int is_subset;
3305 if (!node || !domain)
3306 goto error;
3308 uset = isl_schedule_tree_domain_get_domain(node->tree);
3309 is_subset = isl_union_set_is_subset(uset, domain);
3310 isl_union_set_free(uset);
3311 if (is_subset < 0)
3312 goto error;
3313 if (is_subset) {
3314 isl_union_set_free(domain);
3315 return node;
3318 tree = isl_schedule_tree_copy(node->tree);
3319 uset = isl_schedule_tree_domain_get_domain(tree);
3320 uset = isl_union_set_intersect(uset, domain);
3321 tree = isl_schedule_tree_domain_set_domain(tree,
3322 isl_union_set_copy(uset));
3323 node = isl_schedule_node_graft_tree(node, tree);
3325 node = isl_schedule_node_child(node, 0);
3326 node = isl_schedule_node_gist(node, uset);
3327 node = isl_schedule_node_parent(node);
3329 return node;
3330 error:
3331 isl_schedule_node_free(node);
3332 isl_union_set_free(domain);
3333 return NULL;
3336 /* Internal data structure for isl_schedule_node_get_subtree_expansion.
3337 * "expansions" contains a list of accumulated expansions
3338 * for each outer expansion, set or sequence node. The first element
3339 * in the list is an identity mapping on the reaching domain elements.
3340 * "res" collects the results.
3342 struct isl_subtree_expansion_data {
3343 isl_union_map_list *expansions;
3344 isl_union_map *res;
3347 /* Callback for "traverse" to enter a node and to move
3348 * to the deepest initial subtree that should be traversed
3349 * by isl_schedule_node_get_subtree_expansion.
3351 * Whenever we come across an expansion node, the last element
3352 * of data->expansions is combined with the expansion
3353 * on the expansion node.
3355 * Whenever we come across a filter node that is the child
3356 * of a set or sequence node, data->expansions is extended
3357 * with a new element that restricts the previous element
3358 * to the elements selected by the filter.
3359 * The previous element can then be reused while backtracking.
3361 static __isl_give isl_schedule_node *subtree_expansion_enter(
3362 __isl_take isl_schedule_node *node, void *user)
3364 struct isl_subtree_expansion_data *data = user;
3366 do {
3367 enum isl_schedule_node_type type;
3368 isl_union_set *filter;
3369 isl_union_map *inner, *expansion;
3370 int n;
3372 switch (isl_schedule_node_get_type(node)) {
3373 case isl_schedule_node_error:
3374 return isl_schedule_node_free(node);
3375 case isl_schedule_node_filter:
3376 type = isl_schedule_node_get_parent_type(node);
3377 if (type != isl_schedule_node_set &&
3378 type != isl_schedule_node_sequence)
3379 break;
3380 filter = isl_schedule_node_filter_get_filter(node);
3381 n = isl_union_map_list_n_union_map(data->expansions);
3382 inner =
3383 isl_union_map_list_get_union_map(data->expansions,
3384 n - 1);
3385 inner = isl_union_map_intersect_range(inner, filter);
3386 data->expansions =
3387 isl_union_map_list_add(data->expansions, inner);
3388 break;
3389 case isl_schedule_node_expansion:
3390 n = isl_union_map_list_n_union_map(data->expansions);
3391 expansion =
3392 isl_schedule_node_expansion_get_expansion(node);
3393 inner =
3394 isl_union_map_list_get_union_map(data->expansions,
3395 n - 1);
3396 inner = isl_union_map_apply_range(inner, expansion);
3397 data->expansions =
3398 isl_union_map_list_set_union_map(data->expansions,
3399 n - 1, inner);
3400 break;
3401 case isl_schedule_node_band:
3402 case isl_schedule_node_context:
3403 case isl_schedule_node_domain:
3404 case isl_schedule_node_extension:
3405 case isl_schedule_node_guard:
3406 case isl_schedule_node_leaf:
3407 case isl_schedule_node_mark:
3408 case isl_schedule_node_sequence:
3409 case isl_schedule_node_set:
3410 break;
3412 } while (isl_schedule_node_has_children(node) &&
3413 (node = isl_schedule_node_first_child(node)) != NULL);
3415 return node;
3418 /* Callback for "traverse" to leave a node for
3419 * isl_schedule_node_get_subtree_expansion.
3421 * If we come across a filter node that is the child
3422 * of a set or sequence node, then we remove the element
3423 * of data->expansions that was added in subtree_expansion_enter.
3425 * If we reach a leaf node, then the accumulated expansion is
3426 * added to data->res.
3428 static __isl_give isl_schedule_node *subtree_expansion_leave(
3429 __isl_take isl_schedule_node *node, void *user)
3431 struct isl_subtree_expansion_data *data = user;
3432 int n;
3433 isl_union_map *inner;
3434 enum isl_schedule_node_type type;
3436 switch (isl_schedule_node_get_type(node)) {
3437 case isl_schedule_node_error:
3438 return isl_schedule_node_free(node);
3439 case isl_schedule_node_filter:
3440 type = isl_schedule_node_get_parent_type(node);
3441 if (type != isl_schedule_node_set &&
3442 type != isl_schedule_node_sequence)
3443 break;
3444 n = isl_union_map_list_n_union_map(data->expansions);
3445 data->expansions = isl_union_map_list_drop(data->expansions,
3446 n - 1, 1);
3447 break;
3448 case isl_schedule_node_leaf:
3449 n = isl_union_map_list_n_union_map(data->expansions);
3450 inner = isl_union_map_list_get_union_map(data->expansions,
3451 n - 1);
3452 data->res = isl_union_map_union(data->res, inner);
3453 break;
3454 case isl_schedule_node_band:
3455 case isl_schedule_node_context:
3456 case isl_schedule_node_domain:
3457 case isl_schedule_node_expansion:
3458 case isl_schedule_node_extension:
3459 case isl_schedule_node_guard:
3460 case isl_schedule_node_mark:
3461 case isl_schedule_node_sequence:
3462 case isl_schedule_node_set:
3463 break;
3466 return node;
3469 /* Return a mapping from the domain elements that reach "node"
3470 * to the corresponding domain elements in the leaves of the subtree
3471 * rooted at "node" obtained by composing the intermediate expansions.
3473 * We start out with an identity mapping between the domain elements
3474 * that reach "node" and compose it with all the expansions
3475 * on a path from "node" to a leaf while traversing the subtree.
3476 * Within the children of an a sequence or set node, the
3477 * accumulated expansion is restricted to the elements selected
3478 * by the filter child.
3480 __isl_give isl_union_map *isl_schedule_node_get_subtree_expansion(
3481 __isl_keep isl_schedule_node *node)
3483 struct isl_subtree_expansion_data data;
3484 isl_space *space;
3485 isl_union_set *domain;
3486 isl_union_map *expansion;
3488 if (!node)
3489 return NULL;
3491 domain = isl_schedule_node_get_universe_domain(node);
3492 space = isl_union_set_get_space(domain);
3493 expansion = isl_union_set_identity(domain);
3494 data.res = isl_union_map_empty(space);
3495 data.expansions = isl_union_map_list_from_union_map(expansion);
3497 node = isl_schedule_node_copy(node);
3498 node = traverse(node, &subtree_expansion_enter,
3499 &subtree_expansion_leave, &data);
3500 if (!node)
3501 data.res = isl_union_map_free(data.res);
3502 isl_schedule_node_free(node);
3504 isl_union_map_list_free(data.expansions);
3506 return data.res;
3509 /* Internal data structure for isl_schedule_node_get_subtree_contraction.
3510 * "contractions" contains a list of accumulated contractions
3511 * for each outer expansion, set or sequence node. The first element
3512 * in the list is an identity mapping on the reaching domain elements.
3513 * "res" collects the results.
3515 struct isl_subtree_contraction_data {
3516 isl_union_pw_multi_aff_list *contractions;
3517 isl_union_pw_multi_aff *res;
3520 /* Callback for "traverse" to enter a node and to move
3521 * to the deepest initial subtree that should be traversed
3522 * by isl_schedule_node_get_subtree_contraction.
3524 * Whenever we come across an expansion node, the last element
3525 * of data->contractions is combined with the contraction
3526 * on the expansion node.
3528 * Whenever we come across a filter node that is the child
3529 * of a set or sequence node, data->contractions is extended
3530 * with a new element that restricts the previous element
3531 * to the elements selected by the filter.
3532 * The previous element can then be reused while backtracking.
3534 static __isl_give isl_schedule_node *subtree_contraction_enter(
3535 __isl_take isl_schedule_node *node, void *user)
3537 struct isl_subtree_contraction_data *data = user;
3539 do {
3540 enum isl_schedule_node_type type;
3541 isl_union_set *filter;
3542 isl_union_pw_multi_aff *inner, *contraction;
3543 int n;
3545 switch (isl_schedule_node_get_type(node)) {
3546 case isl_schedule_node_error:
3547 return isl_schedule_node_free(node);
3548 case isl_schedule_node_filter:
3549 type = isl_schedule_node_get_parent_type(node);
3550 if (type != isl_schedule_node_set &&
3551 type != isl_schedule_node_sequence)
3552 break;
3553 filter = isl_schedule_node_filter_get_filter(node);
3554 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3555 data->contractions);
3556 inner =
3557 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3558 data->contractions, n - 1);
3559 inner = isl_union_pw_multi_aff_intersect_domain(inner,
3560 filter);
3561 data->contractions =
3562 isl_union_pw_multi_aff_list_add(data->contractions,
3563 inner);
3564 break;
3565 case isl_schedule_node_expansion:
3566 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3567 data->contractions);
3568 contraction =
3569 isl_schedule_node_expansion_get_contraction(node);
3570 inner =
3571 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3572 data->contractions, n - 1);
3573 inner =
3574 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3575 inner, contraction);
3576 data->contractions =
3577 isl_union_pw_multi_aff_list_set_union_pw_multi_aff(
3578 data->contractions, n - 1, inner);
3579 break;
3580 case isl_schedule_node_band:
3581 case isl_schedule_node_context:
3582 case isl_schedule_node_domain:
3583 case isl_schedule_node_extension:
3584 case isl_schedule_node_guard:
3585 case isl_schedule_node_leaf:
3586 case isl_schedule_node_mark:
3587 case isl_schedule_node_sequence:
3588 case isl_schedule_node_set:
3589 break;
3591 } while (isl_schedule_node_has_children(node) &&
3592 (node = isl_schedule_node_first_child(node)) != NULL);
3594 return node;
3597 /* Callback for "traverse" to leave a node for
3598 * isl_schedule_node_get_subtree_contraction.
3600 * If we come across a filter node that is the child
3601 * of a set or sequence node, then we remove the element
3602 * of data->contractions that was added in subtree_contraction_enter.
3604 * If we reach a leaf node, then the accumulated contraction is
3605 * added to data->res.
3607 static __isl_give isl_schedule_node *subtree_contraction_leave(
3608 __isl_take isl_schedule_node *node, void *user)
3610 struct isl_subtree_contraction_data *data = user;
3611 int n;
3612 isl_union_pw_multi_aff *inner;
3613 enum isl_schedule_node_type type;
3615 switch (isl_schedule_node_get_type(node)) {
3616 case isl_schedule_node_error:
3617 return isl_schedule_node_free(node);
3618 case isl_schedule_node_filter:
3619 type = isl_schedule_node_get_parent_type(node);
3620 if (type != isl_schedule_node_set &&
3621 type != isl_schedule_node_sequence)
3622 break;
3623 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3624 data->contractions);
3625 data->contractions =
3626 isl_union_pw_multi_aff_list_drop(data->contractions,
3627 n - 1, 1);
3628 break;
3629 case isl_schedule_node_leaf:
3630 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3631 data->contractions);
3632 inner = isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3633 data->contractions, n - 1);
3634 data->res = isl_union_pw_multi_aff_union_add(data->res, inner);
3635 break;
3636 case isl_schedule_node_band:
3637 case isl_schedule_node_context:
3638 case isl_schedule_node_domain:
3639 case isl_schedule_node_expansion:
3640 case isl_schedule_node_extension:
3641 case isl_schedule_node_guard:
3642 case isl_schedule_node_mark:
3643 case isl_schedule_node_sequence:
3644 case isl_schedule_node_set:
3645 break;
3648 return node;
3651 /* Return a mapping from the domain elements in the leaves of the subtree
3652 * rooted at "node" to the corresponding domain elements that reach "node"
3653 * obtained by composing the intermediate contractions.
3655 * We start out with an identity mapping between the domain elements
3656 * that reach "node" and compose it with all the contractions
3657 * on a path from "node" to a leaf while traversing the subtree.
3658 * Within the children of an a sequence or set node, the
3659 * accumulated contraction is restricted to the elements selected
3660 * by the filter child.
3662 __isl_give isl_union_pw_multi_aff *isl_schedule_node_get_subtree_contraction(
3663 __isl_keep isl_schedule_node *node)
3665 struct isl_subtree_contraction_data data;
3666 isl_space *space;
3667 isl_union_set *domain;
3668 isl_union_pw_multi_aff *contraction;
3670 if (!node)
3671 return NULL;
3673 domain = isl_schedule_node_get_universe_domain(node);
3674 space = isl_union_set_get_space(domain);
3675 contraction = isl_union_set_identity_union_pw_multi_aff(domain);
3676 data.res = isl_union_pw_multi_aff_empty(space);
3677 data.contractions =
3678 isl_union_pw_multi_aff_list_from_union_pw_multi_aff(contraction);
3680 node = isl_schedule_node_copy(node);
3681 node = traverse(node, &subtree_contraction_enter,
3682 &subtree_contraction_leave, &data);
3683 if (!node)
3684 data.res = isl_union_pw_multi_aff_free(data.res);
3685 isl_schedule_node_free(node);
3687 isl_union_pw_multi_aff_list_free(data.contractions);
3689 return data.res;
3692 /* Do the nearest "n" ancestors of "node" have the types given in "types"
3693 * (starting at the parent of "node")?
3695 static int has_ancestors(__isl_keep isl_schedule_node *node,
3696 int n, enum isl_schedule_node_type *types)
3698 int i, n_ancestor;
3700 if (!node)
3701 return -1;
3703 n_ancestor = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
3704 if (n_ancestor < n)
3705 return 0;
3707 for (i = 0; i < n; ++i) {
3708 isl_schedule_tree *tree;
3709 int correct_type;
3711 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
3712 n_ancestor - 1 - i);
3713 if (!tree)
3714 return -1;
3715 correct_type = isl_schedule_tree_get_type(tree) == types[i];
3716 isl_schedule_tree_free(tree);
3717 if (!correct_type)
3718 return 0;
3721 return 1;
3724 /* Given a node "node" that appears in an extension (i.e., it is the child
3725 * of a filter in a sequence inside an extension node), are the spaces
3726 * of the extension specified by "extension" disjoint from those
3727 * of both the original extension and the domain elements that reach
3728 * that original extension?
3730 static int is_disjoint_extension(__isl_keep isl_schedule_node *node,
3731 __isl_keep isl_union_map *extension)
3733 isl_union_map *old;
3734 isl_union_set *domain;
3735 int empty;
3737 node = isl_schedule_node_copy(node);
3738 node = isl_schedule_node_parent(node);
3739 node = isl_schedule_node_parent(node);
3740 node = isl_schedule_node_parent(node);
3741 old = isl_schedule_node_extension_get_extension(node);
3742 domain = isl_schedule_node_get_universe_domain(node);
3743 isl_schedule_node_free(node);
3744 old = isl_union_map_universe(old);
3745 domain = isl_union_set_union(domain, isl_union_map_range(old));
3746 extension = isl_union_map_copy(extension);
3747 extension = isl_union_map_intersect_range(extension, domain);
3748 empty = isl_union_map_is_empty(extension);
3749 isl_union_map_free(extension);
3751 return empty;
3754 /* Given a node "node" that is governed by an extension node, extend
3755 * that extension node with "extension".
3757 * In particular, "node" is the child of a filter in a sequence that
3758 * is in turn a child of an extension node. Extend that extension node
3759 * with "extension".
3761 * Return a pointer to the parent of the original node (i.e., a filter).
3763 static __isl_give isl_schedule_node *extend_extension(
3764 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
3766 int pos;
3767 int disjoint;
3768 isl_union_map *node_extension;
3770 node = isl_schedule_node_parent(node);
3771 pos = isl_schedule_node_get_child_position(node);
3772 node = isl_schedule_node_parent(node);
3773 node = isl_schedule_node_parent(node);
3774 node_extension = isl_schedule_node_extension_get_extension(node);
3775 disjoint = isl_union_map_is_disjoint(extension, node_extension);
3776 extension = isl_union_map_union(extension, node_extension);
3777 node = isl_schedule_node_extension_set_extension(node, extension);
3778 node = isl_schedule_node_child(node, 0);
3779 node = isl_schedule_node_child(node, pos);
3781 if (disjoint < 0)
3782 return isl_schedule_node_free(node);
3783 if (!node)
3784 return NULL;
3785 if (!disjoint)
3786 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
3787 "extension domain should be disjoint from earlier "
3788 "extensions", return isl_schedule_node_free(node));
3790 return node;
3793 /* Return the universe of "uset" if this universe is disjoint from "ref".
3794 * Otherwise, return "uset".
3796 * Also check if "uset" itself is disjoint from "ref", reporting
3797 * an error if it is not.
3799 static __isl_give isl_union_set *replace_by_universe_if_disjoint(
3800 __isl_take isl_union_set *uset, __isl_keep isl_union_set *ref)
3802 int disjoint;
3803 isl_union_set *universe;
3805 disjoint = isl_union_set_is_disjoint(uset, ref);
3806 if (disjoint < 0)
3807 return isl_union_set_free(uset);
3808 if (!disjoint)
3809 isl_die(isl_union_set_get_ctx(uset), isl_error_invalid,
3810 "extension domain should be disjoint from "
3811 "current domain", return isl_union_set_free(uset));
3813 universe = isl_union_set_universe(isl_union_set_copy(uset));
3814 disjoint = isl_union_set_is_disjoint(universe, ref);
3815 if (disjoint >= 0 && disjoint) {
3816 isl_union_set_free(uset);
3817 return universe;
3819 isl_union_set_free(universe);
3821 if (disjoint < 0)
3822 return isl_union_set_free(uset);
3823 return uset;
3826 /* Insert an extension node on top of "node" with extension "extension".
3827 * In addition, insert a filter that separates node from the extension
3828 * between the extension node and "node".
3829 * Return a pointer to the inserted filter node.
3831 * If "node" already appears in an extension (i.e., if it is the child
3832 * of a filter in a sequence inside an extension node), then extend that
3833 * extension with "extension" instead.
3834 * In this case, a pointer to the original filter node is returned.
3835 * Note that if some of the elements in the new extension live in the
3836 * same space as those of the original extension or the domain elements
3837 * reaching the original extension, then we insert a new extension anyway.
3838 * Otherwise, we would have to adjust the filters in the sequence child
3839 * of the extension to ensure that the elements in the new extension
3840 * are filtered out.
3842 static __isl_give isl_schedule_node *insert_extension(
3843 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
3845 enum isl_schedule_node_type ancestors[] =
3846 { isl_schedule_node_filter, isl_schedule_node_sequence,
3847 isl_schedule_node_extension };
3848 isl_union_set *domain;
3849 isl_union_set *filter;
3850 int in_ext;
3852 in_ext = has_ancestors(node, 3, ancestors);
3853 if (in_ext < 0)
3854 goto error;
3855 if (in_ext) {
3856 int disjoint;
3858 disjoint = is_disjoint_extension(node, extension);
3859 if (disjoint < 0)
3860 goto error;
3861 if (disjoint)
3862 return extend_extension(node, extension);
3865 filter = isl_schedule_node_get_domain(node);
3866 domain = isl_union_map_range(isl_union_map_copy(extension));
3867 filter = replace_by_universe_if_disjoint(filter, domain);
3868 isl_union_set_free(domain);
3870 node = isl_schedule_node_insert_filter(node, filter);
3871 node = isl_schedule_node_insert_extension(node, extension);
3872 node = isl_schedule_node_child(node, 0);
3873 return node;
3874 error:
3875 isl_schedule_node_free(node);
3876 isl_union_map_free(extension);
3877 return NULL;
3880 /* Replace the subtree that "node" points to by "tree" (which has
3881 * a sequence root with two children), except if the parent of "node"
3882 * is a sequence as well, in which case "tree" is spliced at the position
3883 * of "node" in its parent.
3884 * Return a pointer to the child of the "tree_pos" (filter) child of "tree"
3885 * in the updated schedule tree.
3887 static __isl_give isl_schedule_node *graft_or_splice(
3888 __isl_take isl_schedule_node *node, __isl_take isl_schedule_tree *tree,
3889 int tree_pos)
3891 int pos;
3893 if (isl_schedule_node_get_parent_type(node) ==
3894 isl_schedule_node_sequence) {
3895 pos = isl_schedule_node_get_child_position(node);
3896 node = isl_schedule_node_parent(node);
3897 node = isl_schedule_node_sequence_splice(node, pos, tree);
3898 } else {
3899 pos = 0;
3900 node = isl_schedule_node_graft_tree(node, tree);
3902 node = isl_schedule_node_child(node, pos + tree_pos);
3903 node = isl_schedule_node_child(node, 0);
3905 return node;
3908 /* Insert a node "graft" into the schedule tree of "node" such that it
3909 * is executed before (if "before" is set) or after (if "before" is not set)
3910 * the node that "node" points to.
3911 * The root of "graft" is an extension node.
3912 * Return a pointer to the node that "node" pointed to.
3914 * We first insert an extension node on top of "node" (or extend
3915 * the extension node if there already is one), with a filter on "node"
3916 * separating it from the extension.
3917 * We then insert a filter in the graft to separate it from the original
3918 * domain elements and combine the original and new tree in a sequence.
3919 * If we have extended an extension node, then the children of this
3920 * sequence are spliced in the sequence of the extended extension
3921 * at the position where "node" appears in the original extension.
3922 * Otherwise, the sequence pair is attached to the new extension node.
3924 static __isl_give isl_schedule_node *graft_extension(
3925 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft,
3926 int before)
3928 isl_union_map *extension;
3929 isl_union_set *graft_domain;
3930 isl_union_set *node_domain;
3931 isl_schedule_tree *tree, *tree_graft;
3933 extension = isl_schedule_node_extension_get_extension(graft);
3934 graft_domain = isl_union_map_range(isl_union_map_copy(extension));
3935 node_domain = isl_schedule_node_get_universe_domain(node);
3936 node = insert_extension(node, extension);
3938 graft_domain = replace_by_universe_if_disjoint(graft_domain,
3939 node_domain);
3940 isl_union_set_free(node_domain);
3942 tree = isl_schedule_node_get_tree(node);
3943 if (!isl_schedule_node_has_children(graft)) {
3944 tree_graft = isl_schedule_tree_from_filter(graft_domain);
3945 } else {
3946 graft = isl_schedule_node_child(graft, 0);
3947 tree_graft = isl_schedule_node_get_tree(graft);
3948 tree_graft = isl_schedule_tree_insert_filter(tree_graft,
3949 graft_domain);
3951 if (before)
3952 tree = isl_schedule_tree_sequence_pair(tree_graft, tree);
3953 else
3954 tree = isl_schedule_tree_sequence_pair(tree, tree_graft);
3955 node = graft_or_splice(node, tree, before);
3957 isl_schedule_node_free(graft);
3959 return node;
3962 /* Replace the root domain node of "node" by an extension node suitable
3963 * for insertion at "pos".
3964 * That is, create an extension node that maps the outer band nodes
3965 * at "pos" to the domain of the root node of "node" and attach
3966 * the child of this root node to the extension node.
3968 static __isl_give isl_schedule_node *extension_from_domain(
3969 __isl_take isl_schedule_node *node, __isl_keep isl_schedule_node *pos)
3971 isl_union_set *universe;
3972 isl_union_set *domain;
3973 isl_union_map *ext;
3974 int depth;
3975 int anchored;
3976 isl_space *space;
3977 isl_schedule_node *res;
3978 isl_schedule_tree *tree;
3980 anchored = isl_schedule_node_is_subtree_anchored(node);
3981 if (anchored < 0)
3982 return isl_schedule_node_free(node);
3983 if (anchored)
3984 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
3985 "cannot graft anchored tree with domain root",
3986 return isl_schedule_node_free(node));
3988 depth = isl_schedule_node_get_schedule_depth(pos);
3989 domain = isl_schedule_node_domain_get_domain(node);
3990 space = isl_union_set_get_space(domain);
3991 space = isl_space_set_from_params(space);
3992 space = isl_space_add_dims(space, isl_dim_set, depth);
3993 universe = isl_union_set_from_set(isl_set_universe(space));
3994 ext = isl_union_map_from_domain_and_range(universe, domain);
3995 res = isl_schedule_node_from_extension(ext);
3996 node = isl_schedule_node_child(node, 0);
3997 if (!node)
3998 return isl_schedule_node_free(res);
3999 if (!isl_schedule_tree_is_leaf(node->tree)) {
4000 tree = isl_schedule_node_get_tree(node);
4001 res = isl_schedule_node_child(res, 0);
4002 res = isl_schedule_node_graft_tree(res, tree);
4003 res = isl_schedule_node_parent(res);
4005 isl_schedule_node_free(node);
4007 return res;
4010 /* Insert a node "graft" into the schedule tree of "node" such that it
4011 * is executed before (if "before" is set) or after (if "before" is not set)
4012 * the node that "node" points to.
4013 * The root of "graft" may be either a domain or an extension node.
4014 * In the latter case, the domain of the extension needs to correspond
4015 * to the outer band nodes of "node".
4016 * The elements of the domain or the range of the extension may not
4017 * intersect with the domain elements that reach "node".
4018 * The schedule tree of "graft" may not be anchored.
4020 * The schedule tree of "node" is modified to include an extension node
4021 * corresponding to the root node of "graft" as a child of the original
4022 * parent of "node". The original node that "node" points to and the
4023 * child of the root node of "graft" are attached to this extension node
4024 * through a sequence, with appropriate filters and with the child
4025 * of "graft" appearing before or after the original "node".
4027 * If "node" already appears inside a sequence that is the child of
4028 * an extension node and if the spaces of the new domain elements
4029 * do not overlap with those of the original domain elements,
4030 * then that extension node is extended with the new extension
4031 * rather than introducing a new segment of extension and sequence nodes.
4033 * Return a pointer to the same node in the modified tree that
4034 * "node" pointed to in the original tree.
4036 static __isl_give isl_schedule_node *isl_schedule_node_graft_before_or_after(
4037 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft,
4038 int before)
4040 if (!node || !graft)
4041 goto error;
4042 if (check_insert(node) < 0)
4043 goto error;
4045 if (isl_schedule_node_get_type(graft) == isl_schedule_node_domain)
4046 graft = extension_from_domain(graft, node);
4048 if (isl_schedule_node_get_type(graft) != isl_schedule_node_extension)
4049 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4050 "expecting domain or extension as root of graft",
4051 goto error);
4053 return graft_extension(node, graft, before);
4054 error:
4055 isl_schedule_node_free(node);
4056 isl_schedule_node_free(graft);
4057 return NULL;
4060 /* Insert a node "graft" into the schedule tree of "node" such that it
4061 * is executed before the node that "node" points to.
4062 * The root of "graft" may be either a domain or an extension node.
4063 * In the latter case, the domain of the extension needs to correspond
4064 * to the outer band nodes of "node".
4065 * The elements of the domain or the range of the extension may not
4066 * intersect with the domain elements that reach "node".
4067 * The schedule tree of "graft" may not be anchored.
4069 * Return a pointer to the same node in the modified tree that
4070 * "node" pointed to in the original tree.
4072 __isl_give isl_schedule_node *isl_schedule_node_graft_before(
4073 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft)
4075 return isl_schedule_node_graft_before_or_after(node, graft, 1);
4078 /* Insert a node "graft" into the schedule tree of "node" such that it
4079 * is executed after the node that "node" points to.
4080 * The root of "graft" may be either a domain or an extension node.
4081 * In the latter case, the domain of the extension needs to correspond
4082 * to the outer band nodes of "node".
4083 * The elements of the domain or the range of the extension may not
4084 * intersect with the domain elements that reach "node".
4085 * The schedule tree of "graft" may not be anchored.
4087 * Return a pointer to the same node in the modified tree that
4088 * "node" pointed to in the original tree.
4090 __isl_give isl_schedule_node *isl_schedule_node_graft_after(
4091 __isl_take isl_schedule_node *node,
4092 __isl_take isl_schedule_node *graft)
4094 return isl_schedule_node_graft_before_or_after(node, graft, 0);
4097 /* Split the domain elements that reach "node" into those that satisfy
4098 * "filter" and those that do not. Arrange for the first subset to be
4099 * executed after the second subset.
4100 * Return a pointer to the tree corresponding to the second subset,
4101 * except when this subset is empty in which case the original pointer
4102 * is returned.
4103 * If both subsets are non-empty, then a sequence node is introduced
4104 * to impose the order. If the grandparent of the original node was
4105 * itself a sequence, then the original child is replaced by two children
4106 * in this sequence instead.
4107 * The children in the sequence are copies of the original subtree,
4108 * simplified with respect to their filters.
4110 __isl_give isl_schedule_node *isl_schedule_node_order_after(
4111 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
4113 enum isl_schedule_node_type ancestors[] =
4114 { isl_schedule_node_filter, isl_schedule_node_sequence };
4115 isl_union_set *node_domain, *node_filter = NULL;
4116 isl_schedule_node *node2;
4117 isl_schedule_tree *tree1, *tree2;
4118 int empty1, empty2;
4119 int in_seq;
4121 if (!node || !filter)
4122 goto error;
4123 if (check_insert(node) < 0)
4124 goto error;
4126 in_seq = has_ancestors(node, 2, ancestors);
4127 if (in_seq < 0)
4128 goto error;
4129 if (in_seq)
4130 node = isl_schedule_node_parent(node);
4131 node_domain = isl_schedule_node_get_domain(node);
4132 filter = isl_union_set_gist(filter, isl_union_set_copy(node_domain));
4133 node_filter = isl_union_set_copy(node_domain);
4134 node_filter = isl_union_set_subtract(node_filter,
4135 isl_union_set_copy(filter));
4136 node_filter = isl_union_set_gist(node_filter, node_domain);
4137 empty1 = isl_union_set_is_empty(filter);
4138 empty2 = isl_union_set_is_empty(node_filter);
4139 if (empty1 < 0 || empty2 < 0)
4140 goto error;
4141 if (empty1 || empty2) {
4142 isl_union_set_free(filter);
4143 isl_union_set_free(node_filter);
4144 return node;
4147 node2 = isl_schedule_node_copy(node);
4148 node = isl_schedule_node_gist(node, isl_union_set_copy(node_filter));
4149 node2 = isl_schedule_node_gist(node2, isl_union_set_copy(filter));
4150 tree1 = isl_schedule_node_get_tree(node);
4151 tree2 = isl_schedule_node_get_tree(node2);
4152 isl_schedule_node_free(node2);
4153 tree1 = isl_schedule_tree_insert_filter(tree1, node_filter);
4154 tree2 = isl_schedule_tree_insert_filter(tree2, filter);
4155 tree1 = isl_schedule_tree_sequence_pair(tree1, tree2);
4157 node = graft_or_splice(node, tree1, 0);
4159 return node;
4160 error:
4161 isl_schedule_node_free(node);
4162 isl_union_set_free(filter);
4163 isl_union_set_free(node_filter);
4164 return NULL;
4167 /* Reset the user pointer on all identifiers of parameters and tuples
4168 * in the schedule node "node".
4170 __isl_give isl_schedule_node *isl_schedule_node_reset_user(
4171 __isl_take isl_schedule_node *node)
4173 isl_schedule_tree *tree;
4175 tree = isl_schedule_node_get_tree(node);
4176 tree = isl_schedule_tree_reset_user(tree);
4177 node = isl_schedule_node_graft_tree(node, tree);
4179 return node;
4182 /* Align the parameters of the schedule node "node" to those of "space".
4184 __isl_give isl_schedule_node *isl_schedule_node_align_params(
4185 __isl_take isl_schedule_node *node, __isl_take isl_space *space)
4187 isl_schedule_tree *tree;
4189 tree = isl_schedule_node_get_tree(node);
4190 tree = isl_schedule_tree_align_params(tree, space);
4191 node = isl_schedule_node_graft_tree(node, tree);
4193 return node;
4196 /* Compute the pullback of schedule node "node"
4197 * by the function represented by "upma".
4198 * In other words, plug in "upma" in the iteration domains
4199 * of schedule node "node".
4200 * We currently do not handle expansion nodes.
4202 * Note that this is only a helper function for
4203 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
4204 * this function should not be called on a single node without also
4205 * calling it on all the other nodes.
4207 __isl_give isl_schedule_node *isl_schedule_node_pullback_union_pw_multi_aff(
4208 __isl_take isl_schedule_node *node,
4209 __isl_take isl_union_pw_multi_aff *upma)
4211 isl_schedule_tree *tree;
4213 tree = isl_schedule_node_get_tree(node);
4214 tree = isl_schedule_tree_pullback_union_pw_multi_aff(tree, upma);
4215 node = isl_schedule_node_graft_tree(node, tree);
4217 return node;
4220 /* Return the position of the subtree containing "node" among the children
4221 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
4222 * In particular, both nodes should point to the same schedule tree.
4224 * Return -1 on error.
4226 int isl_schedule_node_get_ancestor_child_position(
4227 __isl_keep isl_schedule_node *node,
4228 __isl_keep isl_schedule_node *ancestor)
4230 int n1, n2;
4231 isl_schedule_tree *tree;
4233 if (!node || !ancestor)
4234 return -1;
4236 if (node->schedule != ancestor->schedule)
4237 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4238 "not a descendant", return -1);
4240 n1 = isl_schedule_node_get_tree_depth(ancestor);
4241 n2 = isl_schedule_node_get_tree_depth(node);
4243 if (n1 >= n2)
4244 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4245 "not a descendant", return -1);
4246 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n1);
4247 isl_schedule_tree_free(tree);
4248 if (tree != ancestor->tree)
4249 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4250 "not a descendant", return -1);
4252 return node->child_pos[n1];
4255 /* Given two nodes that point to the same schedule tree, return their
4256 * closest shared ancestor.
4258 * Since the two nodes point to the same schedule, they share at least
4259 * one ancestor, the root of the schedule. We move down from the root
4260 * to the first ancestor where the respective children have a different
4261 * child position. This is the requested ancestor.
4262 * If there is no ancestor where the children have a different position,
4263 * then one node is an ancestor of the other and then this node is
4264 * the requested ancestor.
4266 __isl_give isl_schedule_node *isl_schedule_node_get_shared_ancestor(
4267 __isl_keep isl_schedule_node *node1,
4268 __isl_keep isl_schedule_node *node2)
4270 int i, n1, n2;
4272 if (!node1 || !node2)
4273 return NULL;
4274 if (node1->schedule != node2->schedule)
4275 isl_die(isl_schedule_node_get_ctx(node1), isl_error_invalid,
4276 "not part of same schedule", return NULL);
4277 n1 = isl_schedule_node_get_tree_depth(node1);
4278 n2 = isl_schedule_node_get_tree_depth(node2);
4279 if (n2 < n1)
4280 return isl_schedule_node_get_shared_ancestor(node2, node1);
4281 if (n1 == 0)
4282 return isl_schedule_node_copy(node1);
4283 if (isl_schedule_node_is_equal(node1, node2))
4284 return isl_schedule_node_copy(node1);
4286 for (i = 0; i < n1; ++i)
4287 if (node1->child_pos[i] != node2->child_pos[i])
4288 break;
4290 node1 = isl_schedule_node_copy(node1);
4291 return isl_schedule_node_ancestor(node1, n1 - i);
4294 /* Print "node" to "p".
4296 __isl_give isl_printer *isl_printer_print_schedule_node(
4297 __isl_take isl_printer *p, __isl_keep isl_schedule_node *node)
4299 if (!node)
4300 return isl_printer_free(p);
4301 return isl_printer_print_schedule_tree_mark(p, node->schedule->root,
4302 isl_schedule_tree_list_n_schedule_tree(node->ancestors),
4303 node->child_pos);
4306 void isl_schedule_node_dump(__isl_keep isl_schedule_node *node)
4308 isl_ctx *ctx;
4309 isl_printer *printer;
4311 if (!node)
4312 return;
4314 ctx = isl_schedule_node_get_ctx(node);
4315 printer = isl_printer_to_file(ctx, stderr);
4316 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
4317 printer = isl_printer_print_schedule_node(printer, node);
4319 isl_printer_free(printer);