add doc/CodingStyle
[isl.git] / isl_schedule_node.c
blob8f91120f6482ce8ecbbeec2a9c64e68533342130
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 int 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 -1;
257 if (node1 == node2)
258 return 1;
259 if (node1->schedule != node2->schedule)
260 return 0;
262 n1 = isl_schedule_node_get_tree_depth(node1);
263 n2 = isl_schedule_node_get_tree_depth(node2);
264 if (n1 != n2)
265 return 0;
266 for (i = 0; i < n1; ++i)
267 if (node1->child_pos[i] != node2->child_pos[i])
268 return 0;
270 return 1;
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 int isl_schedule_node_has_parent(__isl_keep isl_schedule_node *node)
949 if (!node)
950 return -1;
951 if (!node->ancestors)
952 return -1;
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 int isl_schedule_node_has_previous_sibling(__isl_keep isl_schedule_node *node)
982 int n;
983 int has_parent;
985 if (!node)
986 return -1;
987 has_parent = isl_schedule_node_has_parent(node);
988 if (has_parent < 0 || !has_parent)
989 return has_parent;
991 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
993 return node->child_pos[n - 1] > 0;
996 /* Does the parent (if any) of "node" have any children with a greater child
997 * position than this one?
999 int isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node *node)
1001 int n, n_child;
1002 int has_parent;
1003 isl_schedule_tree *tree;
1005 if (!node)
1006 return -1;
1007 has_parent = isl_schedule_node_has_parent(node);
1008 if (has_parent < 0 || !has_parent)
1009 return has_parent;
1011 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1012 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n - 1);
1013 if (!tree)
1014 return -1;
1015 n_child = isl_schedule_tree_list_n_schedule_tree(tree->children);
1016 isl_schedule_tree_free(tree);
1018 return node->child_pos[n - 1] + 1 < n_child;
1021 /* Does "node" have any children?
1023 * Any node other than the leaf nodes is considered to have at least
1024 * one child, even if the corresponding isl_schedule_tree does not
1025 * have any children.
1027 int isl_schedule_node_has_children(__isl_keep isl_schedule_node *node)
1029 if (!node)
1030 return -1;
1031 return !isl_schedule_tree_is_leaf(node->tree);
1034 /* Return the number of children of "node"?
1036 * Any node other than the leaf nodes is considered to have at least
1037 * one child, even if the corresponding isl_schedule_tree does not
1038 * have any children. That is, the number of children of "node" is
1039 * only zero if its tree is the explicit empty tree. Otherwise,
1040 * if the isl_schedule_tree has any children, then it is equal
1041 * to the number of children of "node". If it has zero children,
1042 * then "node" still has a leaf node as child.
1044 int isl_schedule_node_n_children(__isl_keep isl_schedule_node *node)
1046 int n;
1048 if (!node)
1049 return -1;
1051 if (isl_schedule_tree_is_leaf(node->tree))
1052 return 0;
1054 n = isl_schedule_tree_n_children(node->tree);
1055 if (n == 0)
1056 return 1;
1058 return n;
1061 /* Move the "node" pointer to the ancestor of the given generation
1062 * of the node it currently points to, where generation 0 is the node
1063 * itself and generation 1 is its parent.
1065 __isl_give isl_schedule_node *isl_schedule_node_ancestor(
1066 __isl_take isl_schedule_node *node, int generation)
1068 int n;
1069 isl_schedule_tree *tree;
1071 if (!node)
1072 return NULL;
1073 if (generation == 0)
1074 return node;
1075 n = isl_schedule_node_get_tree_depth(node);
1076 if (n < 0)
1077 return isl_schedule_node_free(node);
1078 if (generation < 0 || generation > n)
1079 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1080 "generation out of bounds",
1081 return isl_schedule_node_free(node));
1082 node = isl_schedule_node_cow(node);
1083 if (!node)
1084 return NULL;
1086 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1087 n - generation);
1088 isl_schedule_tree_free(node->tree);
1089 node->tree = tree;
1090 node->ancestors = isl_schedule_tree_list_drop(node->ancestors,
1091 n - generation, generation);
1092 if (!node->ancestors || !node->tree)
1093 return isl_schedule_node_free(node);
1095 return node;
1098 /* Move the "node" pointer to the parent of the node it currently points to.
1100 __isl_give isl_schedule_node *isl_schedule_node_parent(
1101 __isl_take isl_schedule_node *node)
1103 if (!node)
1104 return NULL;
1105 if (!isl_schedule_node_has_parent(node))
1106 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1107 "node has no parent",
1108 return isl_schedule_node_free(node));
1109 return isl_schedule_node_ancestor(node, 1);
1112 /* Move the "node" pointer to the root of its schedule tree.
1114 __isl_give isl_schedule_node *isl_schedule_node_root(
1115 __isl_take isl_schedule_node *node)
1117 int n;
1119 if (!node)
1120 return NULL;
1121 n = isl_schedule_node_get_tree_depth(node);
1122 if (n < 0)
1123 return isl_schedule_node_free(node);
1124 return isl_schedule_node_ancestor(node, n);
1127 /* Move the "node" pointer to the child at position "pos" of the node
1128 * it currently points to.
1130 __isl_give isl_schedule_node *isl_schedule_node_child(
1131 __isl_take isl_schedule_node *node, int pos)
1133 int n;
1134 isl_ctx *ctx;
1135 isl_schedule_tree *tree;
1136 int *child_pos;
1138 node = isl_schedule_node_cow(node);
1139 if (!node)
1140 return NULL;
1141 if (!isl_schedule_node_has_children(node))
1142 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1143 "node has no children",
1144 return isl_schedule_node_free(node));
1146 ctx = isl_schedule_node_get_ctx(node);
1147 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1148 child_pos = isl_realloc_array(ctx, node->child_pos, int, n + 1);
1149 if (!child_pos)
1150 return isl_schedule_node_free(node);
1151 node->child_pos = child_pos;
1152 node->child_pos[n] = pos;
1154 node->ancestors = isl_schedule_tree_list_add(node->ancestors,
1155 isl_schedule_tree_copy(node->tree));
1156 tree = node->tree;
1157 if (isl_schedule_tree_has_children(tree))
1158 tree = isl_schedule_tree_get_child(tree, pos);
1159 else
1160 tree = isl_schedule_node_get_leaf(node);
1161 isl_schedule_tree_free(node->tree);
1162 node->tree = tree;
1164 if (!node->tree || !node->ancestors)
1165 return isl_schedule_node_free(node);
1167 return node;
1170 /* Move the "node" pointer to the first child of the node
1171 * it currently points to.
1173 __isl_give isl_schedule_node *isl_schedule_node_first_child(
1174 __isl_take isl_schedule_node *node)
1176 return isl_schedule_node_child(node, 0);
1179 /* Move the "node" pointer to the child of this node's parent in
1180 * the previous child position.
1182 __isl_give isl_schedule_node *isl_schedule_node_previous_sibling(
1183 __isl_take isl_schedule_node *node)
1185 int n;
1186 isl_schedule_tree *parent, *tree;
1188 node = isl_schedule_node_cow(node);
1189 if (!node)
1190 return NULL;
1191 if (!isl_schedule_node_has_previous_sibling(node))
1192 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1193 "node has no previous sibling",
1194 return isl_schedule_node_free(node));
1196 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1197 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1198 n - 1);
1199 if (!parent)
1200 return isl_schedule_node_free(node);
1201 node->child_pos[n - 1]--;
1202 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
1203 node->child_pos[n - 1]);
1204 isl_schedule_tree_free(parent);
1205 if (!tree)
1206 return isl_schedule_node_free(node);
1207 isl_schedule_tree_free(node->tree);
1208 node->tree = tree;
1210 return node;
1213 /* Move the "node" pointer to the child of this node's parent in
1214 * the next child position.
1216 __isl_give isl_schedule_node *isl_schedule_node_next_sibling(
1217 __isl_take isl_schedule_node *node)
1219 int n;
1220 isl_schedule_tree *parent, *tree;
1222 node = isl_schedule_node_cow(node);
1223 if (!node)
1224 return NULL;
1225 if (!isl_schedule_node_has_next_sibling(node))
1226 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1227 "node has no next sibling",
1228 return isl_schedule_node_free(node));
1230 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1231 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1232 n - 1);
1233 if (!parent)
1234 return isl_schedule_node_free(node);
1235 node->child_pos[n - 1]++;
1236 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
1237 node->child_pos[n - 1]);
1238 isl_schedule_tree_free(parent);
1239 if (!tree)
1240 return isl_schedule_node_free(node);
1241 isl_schedule_tree_free(node->tree);
1242 node->tree = tree;
1244 return node;
1247 /* Return a copy to the child at position "pos" of "node".
1249 __isl_give isl_schedule_node *isl_schedule_node_get_child(
1250 __isl_keep isl_schedule_node *node, int pos)
1252 return isl_schedule_node_child(isl_schedule_node_copy(node), pos);
1255 /* Traverse the descendant of "node" in depth-first order, including
1256 * "node" itself. Call "enter" whenever a node is entered and "leave"
1257 * whenever a node is left. The callback "enter" is responsible
1258 * for moving to the deepest initial subtree of its argument that
1259 * should be traversed.
1261 static __isl_give isl_schedule_node *traverse(
1262 __isl_take isl_schedule_node *node,
1263 __isl_give isl_schedule_node *(*enter)(
1264 __isl_take isl_schedule_node *node, void *user),
1265 __isl_give isl_schedule_node *(*leave)(
1266 __isl_take isl_schedule_node *node, void *user),
1267 void *user)
1269 int depth;
1271 if (!node)
1272 return NULL;
1274 depth = isl_schedule_node_get_tree_depth(node);
1275 do {
1276 node = enter(node, user);
1277 node = leave(node, user);
1278 while (node && isl_schedule_node_get_tree_depth(node) > depth &&
1279 !isl_schedule_node_has_next_sibling(node)) {
1280 node = isl_schedule_node_parent(node);
1281 node = leave(node, user);
1283 if (node && isl_schedule_node_get_tree_depth(node) > depth)
1284 node = isl_schedule_node_next_sibling(node);
1285 } while (node && isl_schedule_node_get_tree_depth(node) > depth);
1287 return node;
1290 /* Internal data structure for isl_schedule_node_foreach_descendant.
1292 * "fn" is the user-specified callback function.
1293 * "user" is the user-specified argument for the callback.
1295 struct isl_schedule_node_preorder_data {
1296 int (*fn)(__isl_keep isl_schedule_node *node, void *user);
1297 void *user;
1300 /* Callback for "traverse" to enter a node and to move
1301 * to the deepest initial subtree that should be traversed
1302 * for use in a preorder visit.
1304 * If the user callback returns a negative value, then we abort
1305 * the traversal. If this callback returns zero, then we skip
1306 * the subtree rooted at the current node. Otherwise, we move
1307 * down to the first child and repeat the process until a leaf
1308 * is reached.
1310 static __isl_give isl_schedule_node *preorder_enter(
1311 __isl_take isl_schedule_node *node, void *user)
1313 struct isl_schedule_node_preorder_data *data = user;
1315 if (!node)
1316 return NULL;
1318 do {
1319 int r;
1321 r = data->fn(node, data->user);
1322 if (r < 0)
1323 return isl_schedule_node_free(node);
1324 if (r == 0)
1325 return node;
1326 } while (isl_schedule_node_has_children(node) &&
1327 (node = isl_schedule_node_first_child(node)) != NULL);
1329 return node;
1332 /* Callback for "traverse" to leave a node
1333 * for use in a preorder visit.
1334 * Since we already visited the node when we entered it,
1335 * we do not need to do anything here.
1337 static __isl_give isl_schedule_node *preorder_leave(
1338 __isl_take isl_schedule_node *node, void *user)
1340 return node;
1343 /* Traverse the descendants of "node" (including the node itself)
1344 * in depth first preorder.
1346 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1347 * If "fn" returns 0 on any of the nodes, then the subtree rooted
1348 * at that node is skipped.
1350 * Return 0 on success and -1 on failure.
1352 int isl_schedule_node_foreach_descendant(__isl_keep isl_schedule_node *node,
1353 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
1355 struct isl_schedule_node_preorder_data data = { fn, user };
1357 node = isl_schedule_node_copy(node);
1358 node = traverse(node, &preorder_enter, &preorder_leave, &data);
1359 isl_schedule_node_free(node);
1361 return node ? 0 : -1;
1364 /* Internal data structure for isl_schedule_node_map_descendant.
1366 * "fn" is the user-specified callback function.
1367 * "user" is the user-specified argument for the callback.
1369 struct isl_schedule_node_postorder_data {
1370 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1371 void *user);
1372 void *user;
1375 /* Callback for "traverse" to enter a node and to move
1376 * to the deepest initial subtree that should be traversed
1377 * for use in a postorder visit.
1379 * Since we are performing a postorder visit, we only need
1380 * to move to the deepest initial leaf here.
1382 static __isl_give isl_schedule_node *postorder_enter(
1383 __isl_take isl_schedule_node *node, void *user)
1385 while (node && isl_schedule_node_has_children(node))
1386 node = isl_schedule_node_first_child(node);
1388 return node;
1391 /* Callback for "traverse" to leave a node
1392 * for use in a postorder visit.
1394 * Since we are performing a postorder visit, we need
1395 * to call the user callback here.
1397 static __isl_give isl_schedule_node *postorder_leave(
1398 __isl_take isl_schedule_node *node, void *user)
1400 struct isl_schedule_node_postorder_data *data = user;
1402 return data->fn(node, data->user);
1405 /* Traverse the descendants of "node" (including the node itself)
1406 * in depth first postorder, allowing the user to modify the visited node.
1407 * The traversal continues from the node returned by the callback function.
1408 * It is the responsibility of the user to ensure that this does not
1409 * lead to an infinite loop. It is safest to always return a pointer
1410 * to the same position (same ancestors and child positions) as the input node.
1412 __isl_give isl_schedule_node *isl_schedule_node_map_descendant(
1413 __isl_take isl_schedule_node *node,
1414 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1415 void *user), void *user)
1417 struct isl_schedule_node_postorder_data data = { fn, user };
1419 return traverse(node, &postorder_enter, &postorder_leave, &data);
1422 /* Traverse the ancestors of "node" from the root down to and including
1423 * the parent of "node", calling "fn" on each of them.
1425 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1427 * Return 0 on success and -1 on failure.
1429 int isl_schedule_node_foreach_ancestor_top_down(
1430 __isl_keep isl_schedule_node *node,
1431 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
1433 int i, n;
1435 if (!node)
1436 return -1;
1438 n = isl_schedule_node_get_tree_depth(node);
1439 for (i = 0; i < n; ++i) {
1440 isl_schedule_node *ancestor;
1441 int r;
1443 ancestor = isl_schedule_node_copy(node);
1444 ancestor = isl_schedule_node_ancestor(ancestor, n - i);
1445 r = fn(ancestor, user);
1446 isl_schedule_node_free(ancestor);
1447 if (r < 0)
1448 return -1;
1451 return 0;
1454 /* Is any node in the subtree rooted at "node" anchored?
1455 * That is, do any of these nodes reference the outer band nodes?
1457 int isl_schedule_node_is_subtree_anchored(__isl_keep isl_schedule_node *node)
1459 if (!node)
1460 return -1;
1461 return isl_schedule_tree_is_subtree_anchored(node->tree);
1464 /* Return the number of members in the given band node.
1466 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node *node)
1468 return node ? isl_schedule_tree_band_n_member(node->tree) : 0;
1471 /* Is the band member at position "pos" of the band node "node"
1472 * marked coincident?
1474 int isl_schedule_node_band_member_get_coincident(
1475 __isl_keep isl_schedule_node *node, int pos)
1477 if (!node)
1478 return -1;
1479 return isl_schedule_tree_band_member_get_coincident(node->tree, pos);
1482 /* Mark the band member at position "pos" the band node "node"
1483 * as being coincident or not according to "coincident".
1485 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_coincident(
1486 __isl_take isl_schedule_node *node, int pos, int coincident)
1488 int c;
1489 isl_schedule_tree *tree;
1491 if (!node)
1492 return NULL;
1493 c = isl_schedule_node_band_member_get_coincident(node, pos);
1494 if (c == coincident)
1495 return node;
1497 tree = isl_schedule_tree_copy(node->tree);
1498 tree = isl_schedule_tree_band_member_set_coincident(tree, pos,
1499 coincident);
1500 node = isl_schedule_node_graft_tree(node, tree);
1502 return node;
1505 /* Is the band node "node" marked permutable?
1507 int isl_schedule_node_band_get_permutable(__isl_keep isl_schedule_node *node)
1509 if (!node)
1510 return -1;
1512 return isl_schedule_tree_band_get_permutable(node->tree);
1515 /* Mark the band node "node" permutable or not according to "permutable"?
1517 __isl_give isl_schedule_node *isl_schedule_node_band_set_permutable(
1518 __isl_take isl_schedule_node *node, int permutable)
1520 isl_schedule_tree *tree;
1522 if (!node)
1523 return NULL;
1524 if (isl_schedule_node_band_get_permutable(node) == permutable)
1525 return node;
1527 tree = isl_schedule_tree_copy(node->tree);
1528 tree = isl_schedule_tree_band_set_permutable(tree, permutable);
1529 node = isl_schedule_node_graft_tree(node, tree);
1531 return node;
1534 /* Return the schedule space of the band node.
1536 __isl_give isl_space *isl_schedule_node_band_get_space(
1537 __isl_keep isl_schedule_node *node)
1539 if (!node)
1540 return NULL;
1542 return isl_schedule_tree_band_get_space(node->tree);
1545 /* Return the schedule of the band node in isolation.
1547 __isl_give isl_multi_union_pw_aff *isl_schedule_node_band_get_partial_schedule(
1548 __isl_keep isl_schedule_node *node)
1550 if (!node)
1551 return NULL;
1553 return isl_schedule_tree_band_get_partial_schedule(node->tree);
1556 /* Return the schedule of the band node in isolation in the form of
1557 * an isl_union_map.
1559 * If the band does not have any members, then we construct a universe map
1560 * with the universe of the domain elements reaching the node as domain.
1561 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1562 * convert that to an isl_union_map.
1564 __isl_give isl_union_map *isl_schedule_node_band_get_partial_schedule_union_map(
1565 __isl_keep isl_schedule_node *node)
1567 isl_multi_union_pw_aff *mupa;
1569 if (!node)
1570 return NULL;
1572 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
1573 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1574 "not a band node", return NULL);
1575 if (isl_schedule_node_band_n_member(node) == 0) {
1576 isl_union_set *domain;
1578 domain = isl_schedule_node_get_universe_domain(node);
1579 return isl_union_map_from_domain(domain);
1582 mupa = isl_schedule_node_band_get_partial_schedule(node);
1583 return isl_union_map_from_multi_union_pw_aff(mupa);
1586 /* Return the loop AST generation type for the band member of band node "node"
1587 * at position "pos".
1589 enum isl_ast_loop_type isl_schedule_node_band_member_get_ast_loop_type(
1590 __isl_keep isl_schedule_node *node, int pos)
1592 if (!node)
1593 return isl_ast_loop_error;
1595 return isl_schedule_tree_band_member_get_ast_loop_type(node->tree, pos);
1598 /* Set the loop AST generation type for the band member of band node "node"
1599 * at position "pos" to "type".
1601 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_ast_loop_type(
1602 __isl_take isl_schedule_node *node, int pos,
1603 enum isl_ast_loop_type type)
1605 isl_schedule_tree *tree;
1607 if (!node)
1608 return NULL;
1610 tree = isl_schedule_tree_copy(node->tree);
1611 tree = isl_schedule_tree_band_member_set_ast_loop_type(tree, pos, type);
1612 return isl_schedule_node_graft_tree(node, tree);
1615 /* Return the loop AST generation type for the band member of band node "node"
1616 * at position "pos" for the isolated part.
1618 enum isl_ast_loop_type isl_schedule_node_band_member_get_isolate_ast_loop_type(
1619 __isl_keep isl_schedule_node *node, int pos)
1621 if (!node)
1622 return isl_ast_loop_error;
1624 return isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1625 node->tree, pos);
1628 /* Set the loop AST generation type for the band member of band node "node"
1629 * at position "pos" for the isolated part to "type".
1631 __isl_give isl_schedule_node *
1632 isl_schedule_node_band_member_set_isolate_ast_loop_type(
1633 __isl_take isl_schedule_node *node, int pos,
1634 enum isl_ast_loop_type type)
1636 isl_schedule_tree *tree;
1638 if (!node)
1639 return NULL;
1641 tree = isl_schedule_tree_copy(node->tree);
1642 tree = isl_schedule_tree_band_member_set_isolate_ast_loop_type(tree,
1643 pos, type);
1644 return isl_schedule_node_graft_tree(node, tree);
1647 /* Return the AST build options associated to band node "node".
1649 __isl_give isl_union_set *isl_schedule_node_band_get_ast_build_options(
1650 __isl_keep isl_schedule_node *node)
1652 if (!node)
1653 return NULL;
1655 return isl_schedule_tree_band_get_ast_build_options(node->tree);
1658 /* Replace the AST build options associated to band node "node" by "options".
1660 __isl_give isl_schedule_node *isl_schedule_node_band_set_ast_build_options(
1661 __isl_take isl_schedule_node *node, __isl_take isl_union_set *options)
1663 isl_schedule_tree *tree;
1665 if (!node || !options)
1666 goto error;
1668 tree = isl_schedule_tree_copy(node->tree);
1669 tree = isl_schedule_tree_band_set_ast_build_options(tree, options);
1670 return isl_schedule_node_graft_tree(node, tree);
1671 error:
1672 isl_schedule_node_free(node);
1673 isl_union_set_free(options);
1674 return NULL;
1677 /* Make sure that that spaces of "node" and "mv" are the same.
1678 * Return -1 on error, reporting the error to the user.
1680 static int check_space_multi_val(__isl_keep isl_schedule_node *node,
1681 __isl_keep isl_multi_val *mv)
1683 isl_space *node_space, *mv_space;
1684 int equal;
1686 node_space = isl_schedule_node_band_get_space(node);
1687 mv_space = isl_multi_val_get_space(mv);
1688 equal = isl_space_tuple_is_equal(node_space, isl_dim_set,
1689 mv_space, isl_dim_set);
1690 isl_space_free(mv_space);
1691 isl_space_free(node_space);
1692 if (equal < 0)
1693 return -1;
1694 if (!equal)
1695 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1696 "spaces don't match", return -1);
1698 return 0;
1701 /* Multiply the partial schedule of the band node "node"
1702 * with the factors in "mv".
1704 __isl_give isl_schedule_node *isl_schedule_node_band_scale(
1705 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1707 isl_schedule_tree *tree;
1708 int anchored;
1710 if (!node || !mv)
1711 goto error;
1712 if (check_space_multi_val(node, mv) < 0)
1713 goto error;
1714 anchored = isl_schedule_node_is_subtree_anchored(node);
1715 if (anchored < 0)
1716 goto error;
1717 if (anchored)
1718 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1719 "cannot scale band node with anchored subtree",
1720 goto error);
1722 tree = isl_schedule_node_get_tree(node);
1723 tree = isl_schedule_tree_band_scale(tree, mv);
1724 return isl_schedule_node_graft_tree(node, tree);
1725 error:
1726 isl_multi_val_free(mv);
1727 isl_schedule_node_free(node);
1728 return NULL;
1731 /* Divide the partial schedule of the band node "node"
1732 * by the factors in "mv".
1734 __isl_give isl_schedule_node *isl_schedule_node_band_scale_down(
1735 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1737 isl_schedule_tree *tree;
1738 int anchored;
1740 if (!node || !mv)
1741 goto error;
1742 if (check_space_multi_val(node, mv) < 0)
1743 goto error;
1744 anchored = isl_schedule_node_is_subtree_anchored(node);
1745 if (anchored < 0)
1746 goto error;
1747 if (anchored)
1748 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1749 "cannot scale down band node with anchored subtree",
1750 goto error);
1752 tree = isl_schedule_node_get_tree(node);
1753 tree = isl_schedule_tree_band_scale_down(tree, mv);
1754 return isl_schedule_node_graft_tree(node, tree);
1755 error:
1756 isl_multi_val_free(mv);
1757 isl_schedule_node_free(node);
1758 return NULL;
1761 /* Tile "node" with tile sizes "sizes".
1763 * The current node is replaced by two nested nodes corresponding
1764 * to the tile dimensions and the point dimensions.
1766 * Return a pointer to the outer (tile) node.
1768 * If any of the descendants of "node" depend on the set of outer band nodes,
1769 * then we refuse to tile the node.
1771 * If the scale tile loops option is set, then the tile loops
1772 * are scaled by the tile sizes. If the shift point loops option is set,
1773 * then the point loops are shifted to start at zero.
1774 * In particular, these options affect the tile and point loop schedules
1775 * as follows
1777 * scale shift original tile point
1779 * 0 0 i floor(i/s) i
1780 * 1 0 i s * floor(i/s) i
1781 * 0 1 i floor(i/s) i - s * floor(i/s)
1782 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1784 __isl_give isl_schedule_node *isl_schedule_node_band_tile(
1785 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
1787 isl_schedule_tree *tree;
1788 int anchored;
1790 if (!node || !sizes)
1791 goto error;
1792 anchored = isl_schedule_node_is_subtree_anchored(node);
1793 if (anchored < 0)
1794 goto error;
1795 if (anchored)
1796 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1797 "cannot tile band node with anchored subtree",
1798 goto error);
1800 if (check_space_multi_val(node, sizes) < 0)
1801 goto error;
1803 tree = isl_schedule_node_get_tree(node);
1804 tree = isl_schedule_tree_band_tile(tree, sizes);
1805 return isl_schedule_node_graft_tree(node, tree);
1806 error:
1807 isl_multi_val_free(sizes);
1808 isl_schedule_node_free(node);
1809 return NULL;
1812 /* Move the band node "node" down to all the leaves in the subtree
1813 * rooted at "node".
1814 * Return a pointer to the node in the resulting tree that is in the same
1815 * position as the node pointed to by "node" in the original tree.
1817 * If the node only has a leaf child, then nothing needs to be done.
1818 * Otherwise, the child of the node is removed and the result is
1819 * appended to all the leaves in the subtree rooted at the original child.
1820 * The original node is then replaced by the result of this operation.
1822 * If any of the nodes in the subtree rooted at "node" depend on
1823 * the set of outer band nodes then we refuse to sink the band node.
1825 __isl_give isl_schedule_node *isl_schedule_node_band_sink(
1826 __isl_take isl_schedule_node *node)
1828 enum isl_schedule_node_type type;
1829 isl_schedule_tree *tree, *child;
1830 int anchored;
1832 if (!node)
1833 return NULL;
1835 type = isl_schedule_node_get_type(node);
1836 if (type != isl_schedule_node_band)
1837 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1838 "not a band node", isl_schedule_node_free(node));
1839 anchored = isl_schedule_node_is_subtree_anchored(node);
1840 if (anchored < 0)
1841 return isl_schedule_node_free(node);
1842 if (anchored)
1843 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1844 "cannot sink band node in anchored subtree",
1845 isl_schedule_node_free(node));
1846 if (isl_schedule_tree_n_children(node->tree) == 0)
1847 return node;
1849 tree = isl_schedule_node_get_tree(node);
1850 child = isl_schedule_tree_get_child(tree, 0);
1851 tree = isl_schedule_tree_reset_children(tree);
1852 tree = isl_schedule_tree_append_to_leaves(child, tree);
1854 return isl_schedule_node_graft_tree(node, tree);
1857 /* Split "node" into two nested band nodes, one with the first "pos"
1858 * dimensions and one with the remaining dimensions.
1859 * The schedules of the two band nodes live in anonymous spaces.
1861 __isl_give isl_schedule_node *isl_schedule_node_band_split(
1862 __isl_take isl_schedule_node *node, int pos)
1864 isl_schedule_tree *tree;
1866 tree = isl_schedule_node_get_tree(node);
1867 tree = isl_schedule_tree_band_split(tree, pos);
1868 return isl_schedule_node_graft_tree(node, tree);
1871 /* Return the context of the context node "node".
1873 __isl_give isl_set *isl_schedule_node_context_get_context(
1874 __isl_keep isl_schedule_node *node)
1876 if (!node)
1877 return NULL;
1879 return isl_schedule_tree_context_get_context(node->tree);
1882 /* Return the domain of the domain node "node".
1884 __isl_give isl_union_set *isl_schedule_node_domain_get_domain(
1885 __isl_keep isl_schedule_node *node)
1887 if (!node)
1888 return NULL;
1890 return isl_schedule_tree_domain_get_domain(node->tree);
1893 /* Return the expansion map of expansion node "node".
1895 __isl_give isl_union_map *isl_schedule_node_expansion_get_expansion(
1896 __isl_keep isl_schedule_node *node)
1898 if (!node)
1899 return NULL;
1901 return isl_schedule_tree_expansion_get_expansion(node->tree);
1904 /* Return the contraction of expansion node "node".
1906 __isl_give isl_union_pw_multi_aff *isl_schedule_node_expansion_get_contraction(
1907 __isl_keep isl_schedule_node *node)
1909 if (!node)
1910 return NULL;
1912 return isl_schedule_tree_expansion_get_contraction(node->tree);
1915 /* Replace the contraction and the expansion of the expansion node "node"
1916 * by "contraction" and "expansion".
1918 __isl_give isl_schedule_node *
1919 isl_schedule_node_expansion_set_contraction_and_expansion(
1920 __isl_take isl_schedule_node *node,
1921 __isl_take isl_union_pw_multi_aff *contraction,
1922 __isl_take isl_union_map *expansion)
1924 isl_schedule_tree *tree;
1926 if (!node || !contraction || !expansion)
1927 goto error;
1929 tree = isl_schedule_tree_copy(node->tree);
1930 tree = isl_schedule_tree_expansion_set_contraction_and_expansion(tree,
1931 contraction, expansion);
1932 return isl_schedule_node_graft_tree(node, tree);
1933 error:
1934 isl_schedule_node_free(node);
1935 isl_union_pw_multi_aff_free(contraction);
1936 isl_union_map_free(expansion);
1937 return NULL;
1940 /* Return the extension of the extension node "node".
1942 __isl_give isl_union_map *isl_schedule_node_extension_get_extension(
1943 __isl_keep isl_schedule_node *node)
1945 if (!node)
1946 return NULL;
1948 return isl_schedule_tree_extension_get_extension(node->tree);
1951 /* Replace the extension of extension node "node" by "extension".
1953 __isl_give isl_schedule_node *isl_schedule_node_extension_set_extension(
1954 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
1956 isl_schedule_tree *tree;
1958 if (!node || !extension)
1959 goto error;
1961 tree = isl_schedule_tree_copy(node->tree);
1962 tree = isl_schedule_tree_extension_set_extension(tree, extension);
1963 return isl_schedule_node_graft_tree(node, tree);
1964 error:
1965 isl_schedule_node_free(node);
1966 isl_union_map_free(extension);
1967 return NULL;
1970 /* Return the filter of the filter node "node".
1972 __isl_give isl_union_set *isl_schedule_node_filter_get_filter(
1973 __isl_keep isl_schedule_node *node)
1975 if (!node)
1976 return NULL;
1978 return isl_schedule_tree_filter_get_filter(node->tree);
1981 /* Replace the filter of filter node "node" by "filter".
1983 __isl_give isl_schedule_node *isl_schedule_node_filter_set_filter(
1984 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
1986 isl_schedule_tree *tree;
1988 if (!node || !filter)
1989 goto error;
1991 tree = isl_schedule_tree_copy(node->tree);
1992 tree = isl_schedule_tree_filter_set_filter(tree, filter);
1993 return isl_schedule_node_graft_tree(node, tree);
1994 error:
1995 isl_schedule_node_free(node);
1996 isl_union_set_free(filter);
1997 return NULL;
2000 /* Return the guard of the guard node "node".
2002 __isl_give isl_set *isl_schedule_node_guard_get_guard(
2003 __isl_keep isl_schedule_node *node)
2005 if (!node)
2006 return NULL;
2008 return isl_schedule_tree_guard_get_guard(node->tree);
2011 /* Return the mark identifier of the mark node "node".
2013 __isl_give isl_id *isl_schedule_node_mark_get_id(
2014 __isl_keep isl_schedule_node *node)
2016 if (!node)
2017 return NULL;
2019 return isl_schedule_tree_mark_get_id(node->tree);
2022 /* Replace the child at position "pos" of the sequence node "node"
2023 * by the children of sequence root node of "tree".
2025 __isl_give isl_schedule_node *isl_schedule_node_sequence_splice(
2026 __isl_take isl_schedule_node *node, int pos,
2027 __isl_take isl_schedule_tree *tree)
2029 isl_schedule_tree *node_tree;
2031 if (!node || !tree)
2032 goto error;
2033 if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
2034 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2035 "not a sequence node", goto error);
2036 if (isl_schedule_tree_get_type(tree) != isl_schedule_node_sequence)
2037 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2038 "not a sequence node", goto error);
2039 node_tree = isl_schedule_node_get_tree(node);
2040 node_tree = isl_schedule_tree_sequence_splice(node_tree, pos, tree);
2041 node = isl_schedule_node_graft_tree(node, node_tree);
2043 return node;
2044 error:
2045 isl_schedule_node_free(node);
2046 isl_schedule_tree_free(tree);
2047 return NULL;
2050 /* Update the ancestors of "node" to point to the tree that "node"
2051 * now points to.
2052 * That is, replace the child in the original parent that corresponds
2053 * to the current tree position by node->tree and continue updating
2054 * the ancestors in the same way until the root is reached.
2056 * If "fn" is not NULL, then it is called on each ancestor as we move up
2057 * the tree so that it can modify the ancestor before it is added
2058 * to the list of ancestors of the modified node.
2059 * The additional "pos" argument records the position
2060 * of the "tree" argument in the original schedule tree.
2062 * If "node" originally points to a leaf of the schedule tree, then make sure
2063 * that in the end it points to a leaf in the updated schedule tree.
2065 static __isl_give isl_schedule_node *update_ancestors(
2066 __isl_take isl_schedule_node *node,
2067 __isl_give isl_schedule_tree *(*fn)(__isl_take isl_schedule_tree *tree,
2068 __isl_keep isl_schedule_node *pos, void *user), void *user)
2070 int i, n;
2071 int is_leaf;
2072 isl_ctx *ctx;
2073 isl_schedule_tree *tree;
2074 isl_schedule_node *pos = NULL;
2076 if (fn)
2077 pos = isl_schedule_node_copy(node);
2079 node = isl_schedule_node_cow(node);
2080 if (!node)
2081 return isl_schedule_node_free(pos);
2083 ctx = isl_schedule_node_get_ctx(node);
2084 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
2085 tree = isl_schedule_tree_copy(node->tree);
2087 for (i = n - 1; i >= 0; --i) {
2088 isl_schedule_tree *parent;
2090 parent = isl_schedule_tree_list_get_schedule_tree(
2091 node->ancestors, i);
2092 parent = isl_schedule_tree_replace_child(parent,
2093 node->child_pos[i], tree);
2094 if (fn) {
2095 pos = isl_schedule_node_parent(pos);
2096 parent = fn(parent, pos, user);
2098 node->ancestors = isl_schedule_tree_list_set_schedule_tree(
2099 node->ancestors, i, isl_schedule_tree_copy(parent));
2101 tree = parent;
2104 if (fn)
2105 isl_schedule_node_free(pos);
2107 is_leaf = isl_schedule_tree_is_leaf(node->tree);
2108 node->schedule = isl_schedule_set_root(node->schedule, tree);
2109 if (is_leaf) {
2110 isl_schedule_tree_free(node->tree);
2111 node->tree = isl_schedule_node_get_leaf(node);
2114 if (!node->schedule || !node->ancestors)
2115 return isl_schedule_node_free(node);
2117 return node;
2120 /* Replace the subtree that "pos" points to by "tree", updating
2121 * the ancestors to maintain a consistent state.
2123 __isl_give isl_schedule_node *isl_schedule_node_graft_tree(
2124 __isl_take isl_schedule_node *pos, __isl_take isl_schedule_tree *tree)
2126 if (!tree || !pos)
2127 goto error;
2128 if (pos->tree == tree) {
2129 isl_schedule_tree_free(tree);
2130 return pos;
2133 pos = isl_schedule_node_cow(pos);
2134 if (!pos)
2135 goto error;
2137 isl_schedule_tree_free(pos->tree);
2138 pos->tree = tree;
2140 return update_ancestors(pos, NULL, NULL);
2141 error:
2142 isl_schedule_node_free(pos);
2143 isl_schedule_tree_free(tree);
2144 return NULL;
2147 /* Make sure we can insert a node between "node" and its parent.
2148 * Return -1 on error, reporting the reason why we cannot insert a node.
2150 static int check_insert(__isl_keep isl_schedule_node *node)
2152 int has_parent;
2153 enum isl_schedule_node_type type;
2155 has_parent = isl_schedule_node_has_parent(node);
2156 if (has_parent < 0)
2157 return -1;
2158 if (!has_parent)
2159 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2160 "cannot insert node outside of root", return -1);
2162 type = isl_schedule_node_get_parent_type(node);
2163 if (type == isl_schedule_node_error)
2164 return -1;
2165 if (type == isl_schedule_node_set || type == isl_schedule_node_sequence)
2166 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2167 "cannot insert node between set or sequence node "
2168 "and its filter children", return -1);
2170 return 0;
2173 /* Insert a band node with partial schedule "mupa" between "node" and
2174 * its parent.
2175 * Return a pointer to the new band node.
2177 * If any of the nodes in the subtree rooted at "node" depend on
2178 * the set of outer band nodes then we refuse to insert the band node.
2180 __isl_give isl_schedule_node *isl_schedule_node_insert_partial_schedule(
2181 __isl_take isl_schedule_node *node,
2182 __isl_take isl_multi_union_pw_aff *mupa)
2184 int anchored;
2185 isl_schedule_band *band;
2186 isl_schedule_tree *tree;
2188 if (check_insert(node) < 0)
2189 node = isl_schedule_node_free(node);
2190 anchored = isl_schedule_node_is_subtree_anchored(node);
2191 if (anchored < 0)
2192 goto error;
2193 if (anchored)
2194 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2195 "cannot insert band node in anchored subtree",
2196 goto error);
2198 tree = isl_schedule_node_get_tree(node);
2199 band = isl_schedule_band_from_multi_union_pw_aff(mupa);
2200 tree = isl_schedule_tree_insert_band(tree, band);
2201 node = isl_schedule_node_graft_tree(node, tree);
2203 return node;
2204 error:
2205 isl_schedule_node_free(node);
2206 isl_multi_union_pw_aff_free(mupa);
2207 return NULL;
2210 /* Insert a context node with context "context" between "node" and its parent.
2211 * Return a pointer to the new context node.
2213 __isl_give isl_schedule_node *isl_schedule_node_insert_context(
2214 __isl_take isl_schedule_node *node, __isl_take isl_set *context)
2216 isl_schedule_tree *tree;
2218 if (check_insert(node) < 0)
2219 node = isl_schedule_node_free(node);
2221 tree = isl_schedule_node_get_tree(node);
2222 tree = isl_schedule_tree_insert_context(tree, context);
2223 node = isl_schedule_node_graft_tree(node, tree);
2225 return node;
2228 /* Insert an expansion node with the given "contraction" and "expansion"
2229 * between "node" and its parent.
2230 * Return a pointer to the new expansion node.
2232 * Typically the domain and range spaces of the expansion are different.
2233 * This means that only one of them can refer to the current domain space
2234 * in a consistent tree. It is up to the caller to ensure that the tree
2235 * returns to a consistent state.
2237 __isl_give isl_schedule_node *isl_schedule_node_insert_expansion(
2238 __isl_take isl_schedule_node *node,
2239 __isl_take isl_union_pw_multi_aff *contraction,
2240 __isl_take isl_union_map *expansion)
2242 isl_schedule_tree *tree;
2244 if (check_insert(node) < 0)
2245 node = isl_schedule_node_free(node);
2247 tree = isl_schedule_node_get_tree(node);
2248 tree = isl_schedule_tree_insert_expansion(tree, contraction, expansion);
2249 node = isl_schedule_node_graft_tree(node, tree);
2251 return node;
2254 /* Insert an extension node with extension "extension" between "node" and
2255 * its parent.
2256 * Return a pointer to the new extension node.
2258 __isl_give isl_schedule_node *isl_schedule_node_insert_extension(
2259 __isl_take isl_schedule_node *node,
2260 __isl_take isl_union_map *extension)
2262 isl_schedule_tree *tree;
2264 tree = isl_schedule_node_get_tree(node);
2265 tree = isl_schedule_tree_insert_extension(tree, extension);
2266 node = isl_schedule_node_graft_tree(node, tree);
2268 return node;
2271 /* Insert a filter node with filter "filter" between "node" and its parent.
2272 * Return a pointer to the new filter node.
2274 __isl_give isl_schedule_node *isl_schedule_node_insert_filter(
2275 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
2277 isl_schedule_tree *tree;
2279 if (check_insert(node) < 0)
2280 node = isl_schedule_node_free(node);
2282 tree = isl_schedule_node_get_tree(node);
2283 tree = isl_schedule_tree_insert_filter(tree, filter);
2284 node = isl_schedule_node_graft_tree(node, tree);
2286 return node;
2289 /* Insert a guard node with guard "guard" between "node" and its parent.
2290 * Return a pointer to the new guard node.
2292 __isl_give isl_schedule_node *isl_schedule_node_insert_guard(
2293 __isl_take isl_schedule_node *node, __isl_take isl_set *guard)
2295 isl_schedule_tree *tree;
2297 if (check_insert(node) < 0)
2298 node = isl_schedule_node_free(node);
2300 tree = isl_schedule_node_get_tree(node);
2301 tree = isl_schedule_tree_insert_guard(tree, guard);
2302 node = isl_schedule_node_graft_tree(node, tree);
2304 return node;
2307 /* Insert a mark node with mark identifier "mark" between "node" and
2308 * its parent.
2309 * Return a pointer to the new mark node.
2311 __isl_give isl_schedule_node *isl_schedule_node_insert_mark(
2312 __isl_take isl_schedule_node *node, __isl_take isl_id *mark)
2314 isl_schedule_tree *tree;
2316 if (check_insert(node) < 0)
2317 node = isl_schedule_node_free(node);
2319 tree = isl_schedule_node_get_tree(node);
2320 tree = isl_schedule_tree_insert_mark(tree, mark);
2321 node = isl_schedule_node_graft_tree(node, tree);
2323 return node;
2326 /* Attach the current subtree of "node" to a sequence of filter tree nodes
2327 * with filters described by "filters", attach this sequence
2328 * of filter tree nodes as children to a new tree of type "type" and
2329 * replace the original subtree of "node" by this new tree.
2331 static __isl_give isl_schedule_node *isl_schedule_node_insert_children(
2332 __isl_take isl_schedule_node *node,
2333 enum isl_schedule_node_type type,
2334 __isl_take isl_union_set_list *filters)
2336 int i, n;
2337 isl_ctx *ctx;
2338 isl_schedule_tree *tree;
2339 isl_schedule_tree_list *list;
2341 if (check_insert(node) < 0)
2342 node = isl_schedule_node_free(node);
2344 if (!node || !filters)
2345 goto error;
2347 ctx = isl_schedule_node_get_ctx(node);
2348 n = isl_union_set_list_n_union_set(filters);
2349 list = isl_schedule_tree_list_alloc(ctx, n);
2350 for (i = 0; i < n; ++i) {
2351 isl_schedule_tree *tree;
2352 isl_union_set *filter;
2354 tree = isl_schedule_node_get_tree(node);
2355 filter = isl_union_set_list_get_union_set(filters, i);
2356 tree = isl_schedule_tree_insert_filter(tree, filter);
2357 list = isl_schedule_tree_list_add(list, tree);
2359 tree = isl_schedule_tree_from_children(type, list);
2360 node = isl_schedule_node_graft_tree(node, tree);
2362 isl_union_set_list_free(filters);
2363 return node;
2364 error:
2365 isl_union_set_list_free(filters);
2366 isl_schedule_node_free(node);
2367 return NULL;
2370 /* Insert a sequence node with child filters "filters" between "node" and
2371 * its parent. That is, the tree that "node" points to is attached
2372 * to each of the child nodes of the filter nodes.
2373 * Return a pointer to the new sequence node.
2375 __isl_give isl_schedule_node *isl_schedule_node_insert_sequence(
2376 __isl_take isl_schedule_node *node,
2377 __isl_take isl_union_set_list *filters)
2379 return isl_schedule_node_insert_children(node,
2380 isl_schedule_node_sequence, filters);
2383 /* Insert a set node with child filters "filters" between "node" and
2384 * its parent. That is, the tree that "node" points to is attached
2385 * to each of the child nodes of the filter nodes.
2386 * Return a pointer to the new set node.
2388 __isl_give isl_schedule_node *isl_schedule_node_insert_set(
2389 __isl_take isl_schedule_node *node,
2390 __isl_take isl_union_set_list *filters)
2392 return isl_schedule_node_insert_children(node,
2393 isl_schedule_node_set, filters);
2396 /* Remove "node" from its schedule tree and return a pointer
2397 * to the leaf at the same position in the updated schedule tree.
2399 * It is not allowed to remove the root of a schedule tree or
2400 * a child of a set or sequence node.
2402 __isl_give isl_schedule_node *isl_schedule_node_cut(
2403 __isl_take isl_schedule_node *node)
2405 isl_schedule_tree *leaf;
2406 enum isl_schedule_node_type parent_type;
2408 if (!node)
2409 return NULL;
2410 if (!isl_schedule_node_has_parent(node))
2411 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2412 "cannot cut root", return isl_schedule_node_free(node));
2414 parent_type = isl_schedule_node_get_parent_type(node);
2415 if (parent_type == isl_schedule_node_set ||
2416 parent_type == isl_schedule_node_sequence)
2417 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2418 "cannot cut child of set or sequence",
2419 return isl_schedule_node_free(node));
2421 leaf = isl_schedule_node_get_leaf(node);
2422 return isl_schedule_node_graft_tree(node, leaf);
2425 /* Remove a single node from the schedule tree, attaching the child
2426 * of "node" directly to its parent.
2427 * Return a pointer to this former child or to the leaf the position
2428 * of the original node if there was no child.
2429 * It is not allowed to remove the root of a schedule tree,
2430 * a set or sequence node, a child of a set or sequence node or
2431 * a band node with an anchored subtree.
2433 __isl_give isl_schedule_node *isl_schedule_node_delete(
2434 __isl_take isl_schedule_node *node)
2436 int n;
2437 isl_schedule_tree *tree;
2438 enum isl_schedule_node_type type;
2440 if (!node)
2441 return NULL;
2443 if (isl_schedule_node_get_tree_depth(node) == 0)
2444 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2445 "cannot delete root node",
2446 return isl_schedule_node_free(node));
2447 n = isl_schedule_node_n_children(node);
2448 if (n != 1)
2449 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2450 "can only delete node with a single child",
2451 return isl_schedule_node_free(node));
2452 type = isl_schedule_node_get_parent_type(node);
2453 if (type == isl_schedule_node_sequence || type == isl_schedule_node_set)
2454 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2455 "cannot delete child of set or sequence",
2456 return isl_schedule_node_free(node));
2457 if (isl_schedule_node_get_type(node) == isl_schedule_node_band) {
2458 int anchored;
2460 anchored = isl_schedule_node_is_subtree_anchored(node);
2461 if (anchored < 0)
2462 return isl_schedule_node_free(node);
2463 if (anchored)
2464 isl_die(isl_schedule_node_get_ctx(node),
2465 isl_error_invalid,
2466 "cannot delete band node with anchored subtree",
2467 return isl_schedule_node_free(node));
2470 tree = isl_schedule_node_get_tree(node);
2471 if (!tree || isl_schedule_tree_has_children(tree)) {
2472 tree = isl_schedule_tree_child(tree, 0);
2473 } else {
2474 isl_schedule_tree_free(tree);
2475 tree = isl_schedule_node_get_leaf(node);
2477 node = isl_schedule_node_graft_tree(node, tree);
2479 return node;
2482 /* Internal data structure for the group_ancestor callback.
2484 * If "finished" is set, then we no longer need to modify
2485 * any further ancestors.
2487 * "contraction" and "expansion" represent the expansion
2488 * that reflects the grouping.
2490 * "domain" contains the domain elements that reach the position
2491 * where the grouping is performed. That is, it is the range
2492 * of the resulting expansion.
2493 * "domain_universe" is the universe of "domain".
2494 * "group" is the set of group elements, i.e., the domain
2495 * of the resulting expansion.
2496 * "group_universe" is the universe of "group".
2498 * "sched" is the schedule for the group elements, in pratice
2499 * an identity mapping on "group_universe".
2500 * "dim" is the dimension of "sched".
2502 struct isl_schedule_group_data {
2503 int finished;
2505 isl_union_map *expansion;
2506 isl_union_pw_multi_aff *contraction;
2508 isl_union_set *domain;
2509 isl_union_set *domain_universe;
2510 isl_union_set *group;
2511 isl_union_set *group_universe;
2513 int dim;
2514 isl_multi_aff *sched;
2517 /* Is domain covered by data->domain within data->domain_universe?
2519 static int locally_covered_by_domain(__isl_keep isl_union_set *domain,
2520 struct isl_schedule_group_data *data)
2522 int is_subset;
2523 isl_union_set *test;
2525 test = isl_union_set_copy(domain);
2526 test = isl_union_set_intersect(test,
2527 isl_union_set_copy(data->domain_universe));
2528 is_subset = isl_union_set_is_subset(test, data->domain);
2529 isl_union_set_free(test);
2531 return is_subset;
2534 /* Update the band tree root "tree" to refer to the group instances
2535 * in data->group rather than the original domain elements in data->domain.
2536 * "pos" is the position in the original schedule tree where the modified
2537 * "tree" will be attached.
2539 * Add the part of the identity schedule on the group instances data->sched
2540 * that corresponds to this band node to the band schedule.
2541 * If the domain elements that reach the node and that are part
2542 * of data->domain_universe are all elements of data->domain (and therefore
2543 * replaced by the group instances) then this data->domain_universe
2544 * is removed from the domain of the band schedule.
2546 static __isl_give isl_schedule_tree *group_band(
2547 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2548 struct isl_schedule_group_data *data)
2550 isl_union_set *domain;
2551 isl_multi_aff *ma;
2552 isl_multi_union_pw_aff *mupa, *partial;
2553 int is_covered;
2554 int depth, n, has_id;
2556 domain = isl_schedule_node_get_domain(pos);
2557 is_covered = locally_covered_by_domain(domain, data);
2558 if (is_covered >= 0 && is_covered) {
2559 domain = isl_union_set_universe(domain);
2560 domain = isl_union_set_subtract(domain,
2561 isl_union_set_copy(data->domain_universe));
2562 tree = isl_schedule_tree_band_intersect_domain(tree, domain);
2563 } else
2564 isl_union_set_free(domain);
2565 if (is_covered < 0)
2566 return isl_schedule_tree_free(tree);
2567 depth = isl_schedule_node_get_schedule_depth(pos);
2568 n = isl_schedule_tree_band_n_member(tree);
2569 ma = isl_multi_aff_copy(data->sched);
2570 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, depth);
2571 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, n, data->dim - depth - n);
2572 mupa = isl_multi_union_pw_aff_from_multi_aff(ma);
2573 partial = isl_schedule_tree_band_get_partial_schedule(tree);
2574 has_id = isl_multi_union_pw_aff_has_tuple_id(partial, isl_dim_set);
2575 if (has_id < 0) {
2576 partial = isl_multi_union_pw_aff_free(partial);
2577 } else if (has_id) {
2578 isl_id *id;
2579 id = isl_multi_union_pw_aff_get_tuple_id(partial, isl_dim_set);
2580 mupa = isl_multi_union_pw_aff_set_tuple_id(mupa,
2581 isl_dim_set, id);
2583 partial = isl_multi_union_pw_aff_union_add(partial, mupa);
2584 tree = isl_schedule_tree_band_set_partial_schedule(tree, partial);
2586 return tree;
2589 /* Drop the parameters in "uset" that are not also in "space".
2590 * "n" is the number of parameters in "space".
2592 static __isl_give isl_union_set *union_set_drop_extra_params(
2593 __isl_take isl_union_set *uset, __isl_keep isl_space *space, int n)
2595 int n2;
2597 uset = isl_union_set_align_params(uset, isl_space_copy(space));
2598 n2 = isl_union_set_dim(uset, isl_dim_param);
2599 uset = isl_union_set_project_out(uset, isl_dim_param, n, n2 - n);
2601 return uset;
2604 /* Update the context tree root "tree" to refer to the group instances
2605 * in data->group rather than the original domain elements in data->domain.
2606 * "pos" is the position in the original schedule tree where the modified
2607 * "tree" will be attached.
2609 * We do not actually need to update "tree" since a context node only
2610 * refers to the schedule space. However, we may need to update "data"
2611 * to not refer to any parameters introduced by the context node.
2613 static __isl_give isl_schedule_tree *group_context(
2614 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2615 struct isl_schedule_group_data *data)
2617 isl_space *space;
2618 isl_union_set *domain;
2619 int n1, n2;
2620 int involves;
2622 if (isl_schedule_node_get_tree_depth(pos) == 1)
2623 return tree;
2625 domain = isl_schedule_node_get_universe_domain(pos);
2626 space = isl_union_set_get_space(domain);
2627 isl_union_set_free(domain);
2629 n1 = isl_space_dim(space, isl_dim_param);
2630 data->expansion = isl_union_map_align_params(data->expansion, space);
2631 n2 = isl_union_map_dim(data->expansion, isl_dim_param);
2633 if (!data->expansion)
2634 return isl_schedule_tree_free(tree);
2635 if (n1 == n2)
2636 return tree;
2638 involves = isl_union_map_involves_dims(data->expansion,
2639 isl_dim_param, n1, n2 - n1);
2640 if (involves < 0)
2641 return isl_schedule_tree_free(tree);
2642 if (involves)
2643 isl_die(isl_schedule_node_get_ctx(pos), isl_error_invalid,
2644 "grouping cannot only refer to global parameters",
2645 return isl_schedule_tree_free(tree));
2647 data->expansion = isl_union_map_project_out(data->expansion,
2648 isl_dim_param, n1, n2 - n1);
2649 space = isl_union_map_get_space(data->expansion);
2651 data->contraction = isl_union_pw_multi_aff_align_params(
2652 data->contraction, isl_space_copy(space));
2653 n2 = isl_union_pw_multi_aff_dim(data->contraction, isl_dim_param);
2654 data->contraction = isl_union_pw_multi_aff_drop_dims(data->contraction,
2655 isl_dim_param, n1, n2 - n1);
2657 data->domain = union_set_drop_extra_params(data->domain, space, n1);
2658 data->domain_universe =
2659 union_set_drop_extra_params(data->domain_universe, space, n1);
2660 data->group = union_set_drop_extra_params(data->group, space, n1);
2661 data->group_universe =
2662 union_set_drop_extra_params(data->group_universe, space, n1);
2664 data->sched = isl_multi_aff_align_params(data->sched,
2665 isl_space_copy(space));
2666 n2 = isl_multi_aff_dim(data->sched, isl_dim_param);
2667 data->sched = isl_multi_aff_drop_dims(data->sched,
2668 isl_dim_param, n1, n2 - n1);
2670 isl_space_free(space);
2672 return tree;
2675 /* Update the domain tree root "tree" to refer to the group instances
2676 * in data->group rather than the original domain elements in data->domain.
2677 * "pos" is the position in the original schedule tree where the modified
2678 * "tree" will be attached.
2680 * We first double-check that all grouped domain elements are actually
2681 * part of the root domain and then replace those elements by the group
2682 * instances.
2684 static __isl_give isl_schedule_tree *group_domain(
2685 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2686 struct isl_schedule_group_data *data)
2688 isl_union_set *domain;
2689 int is_subset;
2691 domain = isl_schedule_tree_domain_get_domain(tree);
2692 is_subset = isl_union_set_is_subset(data->domain, domain);
2693 isl_union_set_free(domain);
2694 if (is_subset < 0)
2695 return isl_schedule_tree_free(tree);
2696 if (!is_subset)
2697 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
2698 "grouped domain should be part of outer domain",
2699 return isl_schedule_tree_free(tree));
2700 domain = isl_schedule_tree_domain_get_domain(tree);
2701 domain = isl_union_set_subtract(domain,
2702 isl_union_set_copy(data->domain));
2703 domain = isl_union_set_union(domain, isl_union_set_copy(data->group));
2704 tree = isl_schedule_tree_domain_set_domain(tree, domain);
2706 return tree;
2709 /* Update the expansion tree root "tree" to refer to the group instances
2710 * in data->group rather than the original domain elements in data->domain.
2711 * "pos" is the position in the original schedule tree where the modified
2712 * "tree" will be attached.
2714 * Let G_1 -> D_1 be the expansion of "tree" and G_2 -> D_2 the newly
2715 * introduced expansion in a descendant of "tree".
2716 * We first double-check that D_2 is a subset of D_1.
2717 * Then we remove D_2 from the range of G_1 -> D_1 and add the mapping
2718 * G_1 -> D_1 . D_2 -> G_2.
2719 * Simmilarly, we restrict the domain of the contraction to the universe
2720 * of the range of the updated expansion and add G_2 -> D_2 . D_1 -> G_1,
2721 * attempting to remove the domain constraints of this additional part.
2723 static __isl_give isl_schedule_tree *group_expansion(
2724 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2725 struct isl_schedule_group_data *data)
2727 isl_union_set *domain;
2728 isl_union_map *expansion, *umap;
2729 isl_union_pw_multi_aff *contraction, *upma;
2730 int is_subset;
2732 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2733 domain = isl_union_map_range(expansion);
2734 is_subset = isl_union_set_is_subset(data->domain, domain);
2735 isl_union_set_free(domain);
2736 if (is_subset < 0)
2737 return isl_schedule_tree_free(tree);
2738 if (!is_subset)
2739 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
2740 "grouped domain should be part "
2741 "of outer expansion domain",
2742 return isl_schedule_tree_free(tree));
2743 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2744 umap = isl_union_map_from_union_pw_multi_aff(
2745 isl_union_pw_multi_aff_copy(data->contraction));
2746 umap = isl_union_map_apply_range(expansion, umap);
2747 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2748 expansion = isl_union_map_subtract_range(expansion,
2749 isl_union_set_copy(data->domain));
2750 expansion = isl_union_map_union(expansion, umap);
2751 umap = isl_union_map_universe(isl_union_map_copy(expansion));
2752 domain = isl_union_map_range(umap);
2753 contraction = isl_schedule_tree_expansion_get_contraction(tree);
2754 umap = isl_union_map_from_union_pw_multi_aff(contraction);
2755 umap = isl_union_map_apply_range(isl_union_map_copy(data->expansion),
2756 umap);
2757 upma = isl_union_pw_multi_aff_from_union_map(umap);
2758 contraction = isl_schedule_tree_expansion_get_contraction(tree);
2759 contraction = isl_union_pw_multi_aff_intersect_domain(contraction,
2760 domain);
2761 domain = isl_union_pw_multi_aff_domain(
2762 isl_union_pw_multi_aff_copy(upma));
2763 upma = isl_union_pw_multi_aff_gist(upma, domain);
2764 contraction = isl_union_pw_multi_aff_union_add(contraction, upma);
2765 tree = isl_schedule_tree_expansion_set_contraction_and_expansion(tree,
2766 contraction, expansion);
2768 return tree;
2771 /* Update the tree root "tree" to refer to the group instances
2772 * in data->group rather than the original domain elements in data->domain.
2773 * "pos" is the position in the original schedule tree where the modified
2774 * "tree" will be attached.
2776 * If we have come across a domain or expansion node before (data->finished
2777 * is set), then we no longer need perform any modifications.
2779 * If "tree" is a filter, then we add data->group_universe to the filter.
2780 * We also remove data->domain_universe from the filter if all the domain
2781 * elements in this universe that reach the filter node are part of
2782 * the elements that are being grouped by data->expansion.
2783 * If "tree" is a band, domain or expansion, then it is handled
2784 * in a separate function.
2786 static __isl_give isl_schedule_tree *group_ancestor(
2787 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2788 void *user)
2790 struct isl_schedule_group_data *data = user;
2791 isl_union_set *domain;
2792 int is_covered;
2794 if (!tree || !pos)
2795 return isl_schedule_tree_free(tree);
2797 if (data->finished)
2798 return tree;
2800 switch (isl_schedule_tree_get_type(tree)) {
2801 case isl_schedule_node_error:
2802 return isl_schedule_tree_free(tree);
2803 case isl_schedule_node_extension:
2804 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
2805 "grouping not allowed in extended tree",
2806 return isl_schedule_tree_free(tree));
2807 case isl_schedule_node_band:
2808 tree = group_band(tree, pos, data);
2809 break;
2810 case isl_schedule_node_context:
2811 tree = group_context(tree, pos, data);
2812 break;
2813 case isl_schedule_node_domain:
2814 tree = group_domain(tree, pos, data);
2815 data->finished = 1;
2816 break;
2817 case isl_schedule_node_filter:
2818 domain = isl_schedule_node_get_domain(pos);
2819 is_covered = locally_covered_by_domain(domain, data);
2820 isl_union_set_free(domain);
2821 if (is_covered < 0)
2822 return isl_schedule_tree_free(tree);
2823 domain = isl_schedule_tree_filter_get_filter(tree);
2824 if (is_covered)
2825 domain = isl_union_set_subtract(domain,
2826 isl_union_set_copy(data->domain_universe));
2827 domain = isl_union_set_union(domain,
2828 isl_union_set_copy(data->group_universe));
2829 tree = isl_schedule_tree_filter_set_filter(tree, domain);
2830 break;
2831 case isl_schedule_node_expansion:
2832 tree = group_expansion(tree, pos, data);
2833 data->finished = 1;
2834 break;
2835 case isl_schedule_node_leaf:
2836 case isl_schedule_node_guard:
2837 case isl_schedule_node_mark:
2838 case isl_schedule_node_sequence:
2839 case isl_schedule_node_set:
2840 break;
2843 return tree;
2846 /* Group the domain elements that reach "node" into instances
2847 * of a single statement with identifier "group_id".
2848 * In particular, group the domain elements according to their
2849 * prefix schedule.
2851 * That is, introduce an expansion node with as contraction
2852 * the prefix schedule (with the target space replaced by "group_id")
2853 * and as expansion the inverse of this contraction (with its range
2854 * intersected with the domain elements that reach "node").
2855 * The outer nodes are then modified to refer to the group instances
2856 * instead of the original domain elements.
2858 * No instance of "group_id" is allowed to reach "node" prior
2859 * to the grouping.
2860 * No ancestor of "node" is allowed to be an extension node.
2862 * Return a pointer to original node in tree, i.e., the child
2863 * of the newly introduced expansion node.
2865 __isl_give isl_schedule_node *isl_schedule_node_group(
2866 __isl_take isl_schedule_node *node, __isl_take isl_id *group_id)
2868 struct isl_schedule_group_data data = { 0 };
2869 isl_space *space;
2870 isl_union_set *domain;
2871 isl_union_pw_multi_aff *contraction;
2872 isl_union_map *expansion;
2873 int disjoint;
2875 if (!node || !group_id)
2876 goto error;
2877 if (check_insert(node) < 0)
2878 goto error;
2880 domain = isl_schedule_node_get_domain(node);
2881 data.domain = isl_union_set_copy(domain);
2882 data.domain_universe = isl_union_set_copy(domain);
2883 data.domain_universe = isl_union_set_universe(data.domain_universe);
2885 data.dim = isl_schedule_node_get_schedule_depth(node);
2886 if (data.dim == 0) {
2887 isl_ctx *ctx;
2888 isl_set *set;
2889 isl_union_set *group;
2890 isl_union_map *univ;
2892 ctx = isl_schedule_node_get_ctx(node);
2893 space = isl_space_set_alloc(ctx, 0, 0);
2894 space = isl_space_set_tuple_id(space, isl_dim_set, group_id);
2895 set = isl_set_universe(isl_space_copy(space));
2896 group = isl_union_set_from_set(set);
2897 expansion = isl_union_map_from_domain_and_range(domain, group);
2898 univ = isl_union_map_universe(isl_union_map_copy(expansion));
2899 contraction = isl_union_pw_multi_aff_from_union_map(univ);
2900 expansion = isl_union_map_reverse(expansion);
2901 } else {
2902 isl_multi_union_pw_aff *prefix;
2903 isl_union_set *univ;
2905 prefix =
2906 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
2907 prefix = isl_multi_union_pw_aff_set_tuple_id(prefix,
2908 isl_dim_set, group_id);
2909 space = isl_multi_union_pw_aff_get_space(prefix);
2910 contraction = isl_union_pw_multi_aff_from_multi_union_pw_aff(
2911 prefix);
2912 univ = isl_union_set_universe(isl_union_set_copy(domain));
2913 contraction =
2914 isl_union_pw_multi_aff_intersect_domain(contraction, univ);
2915 expansion = isl_union_map_from_union_pw_multi_aff(
2916 isl_union_pw_multi_aff_copy(contraction));
2917 expansion = isl_union_map_reverse(expansion);
2918 expansion = isl_union_map_intersect_range(expansion, domain);
2920 space = isl_space_map_from_set(space);
2921 data.sched = isl_multi_aff_identity(space);
2922 data.group = isl_union_map_domain(isl_union_map_copy(expansion));
2923 data.group = isl_union_set_coalesce(data.group);
2924 data.group_universe = isl_union_set_copy(data.group);
2925 data.group_universe = isl_union_set_universe(data.group_universe);
2926 data.expansion = isl_union_map_copy(expansion);
2927 data.contraction = isl_union_pw_multi_aff_copy(contraction);
2928 node = isl_schedule_node_insert_expansion(node, contraction, expansion);
2930 disjoint = isl_union_set_is_disjoint(data.domain_universe,
2931 data.group_universe);
2933 node = update_ancestors(node, &group_ancestor, &data);
2935 isl_union_set_free(data.domain);
2936 isl_union_set_free(data.domain_universe);
2937 isl_union_set_free(data.group);
2938 isl_union_set_free(data.group_universe);
2939 isl_multi_aff_free(data.sched);
2940 isl_union_map_free(data.expansion);
2941 isl_union_pw_multi_aff_free(data.contraction);
2943 node = isl_schedule_node_child(node, 0);
2945 if (!node || disjoint < 0)
2946 return isl_schedule_node_free(node);
2947 if (!disjoint)
2948 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2949 "group instances already reach node",
2950 isl_schedule_node_free(node));
2952 return node;
2953 error:
2954 isl_schedule_node_free(node);
2955 isl_id_free(group_id);
2956 return NULL;
2959 /* Compute the gist of the given band node with respect to "context".
2961 __isl_give isl_schedule_node *isl_schedule_node_band_gist(
2962 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
2964 isl_schedule_tree *tree;
2966 tree = isl_schedule_node_get_tree(node);
2967 tree = isl_schedule_tree_band_gist(tree, context);
2968 return isl_schedule_node_graft_tree(node, tree);
2971 /* Internal data structure for isl_schedule_node_gist.
2972 * "n_expansion" is the number of outer expansion nodes
2973 * with respect to the current position
2974 * "filters" contains an element for each outer filter, expansion or
2975 * extension node with respect to the current position, each representing
2976 * the intersection of the previous element and the filter on the filter node
2977 * or the expansion/extension of the previous element.
2978 * The first element in the original context passed to isl_schedule_node_gist.
2980 struct isl_node_gist_data {
2981 int n_expansion;
2982 isl_union_set_list *filters;
2985 /* Enter the expansion node "node" during a isl_schedule_node_gist traversal.
2987 * In particular, add an extra element to data->filters containing
2988 * the expansion of the previous element and replace the expansion
2989 * and contraction on "node" by the gist with respect to these filters.
2990 * Also keep track of the fact that we have entered another expansion.
2992 static __isl_give isl_schedule_node *gist_enter_expansion(
2993 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
2995 int n;
2996 isl_union_set *inner;
2997 isl_union_map *expansion;
2998 isl_union_pw_multi_aff *contraction;
3000 data->n_expansion++;
3002 n = isl_union_set_list_n_union_set(data->filters);
3003 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3004 expansion = isl_schedule_node_expansion_get_expansion(node);
3005 inner = isl_union_set_apply(inner, expansion);
3007 contraction = isl_schedule_node_expansion_get_contraction(node);
3008 contraction = isl_union_pw_multi_aff_gist(contraction,
3009 isl_union_set_copy(inner));
3011 data->filters = isl_union_set_list_add(data->filters, inner);
3013 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3014 expansion = isl_schedule_node_expansion_get_expansion(node);
3015 expansion = isl_union_map_gist_domain(expansion, inner);
3016 node = isl_schedule_node_expansion_set_contraction_and_expansion(node,
3017 contraction, expansion);
3019 return node;
3022 /* Enter the extension node "node" during a isl_schedule_node_gist traversal.
3024 * In particular, add an extra element to data->filters containing
3025 * the union of the previous element with the additional domain elements
3026 * introduced by the extension.
3028 static __isl_give isl_schedule_node *gist_enter_extension(
3029 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3031 int n;
3032 isl_union_set *inner, *extra;
3033 isl_union_map *extension;
3035 n = isl_union_set_list_n_union_set(data->filters);
3036 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3037 extension = isl_schedule_node_extension_get_extension(node);
3038 extra = isl_union_map_range(extension);
3039 inner = isl_union_set_union(inner, extra);
3041 data->filters = isl_union_set_list_add(data->filters, inner);
3043 return node;
3046 /* Can we finish gisting at this node?
3047 * That is, is the filter on the current filter node a subset of
3048 * the original context passed to isl_schedule_node_gist?
3049 * If we have gone through any expansions, then we cannot perform
3050 * this test since the current domain elements are incomparable
3051 * to the domain elements in the original context.
3053 static int gist_done(__isl_keep isl_schedule_node *node,
3054 struct isl_node_gist_data *data)
3056 isl_union_set *filter, *outer;
3057 int subset;
3059 if (data->n_expansion != 0)
3060 return 0;
3062 filter = isl_schedule_node_filter_get_filter(node);
3063 outer = isl_union_set_list_get_union_set(data->filters, 0);
3064 subset = isl_union_set_is_subset(filter, outer);
3065 isl_union_set_free(outer);
3066 isl_union_set_free(filter);
3068 return subset;
3071 /* Callback for "traverse" to enter a node and to move
3072 * to the deepest initial subtree that should be traversed
3073 * by isl_schedule_node_gist.
3075 * The "filters" list is extended by one element each time
3076 * we come across a filter node by the result of intersecting
3077 * the last element in the list with the filter on the filter node.
3079 * If the filter on the current filter node is a subset of
3080 * the original context passed to isl_schedule_node_gist,
3081 * then there is no need to go into its subtree since it cannot
3082 * be further simplified by the context. The "filters" list is
3083 * still extended for consistency, but the actual value of the
3084 * added element is immaterial since it will not be used.
3086 * Otherwise, the filter on the current filter node is replaced by
3087 * the gist of the original filter with respect to the intersection
3088 * of the original context with the intermediate filters.
3090 * If the new element in the "filters" list is empty, then no elements
3091 * can reach the descendants of the current filter node. The subtree
3092 * underneath the filter node is therefore removed.
3094 * Each expansion node we come across is handled by
3095 * gist_enter_expansion.
3097 * Each extension node we come across is handled by
3098 * gist_enter_extension.
3100 static __isl_give isl_schedule_node *gist_enter(
3101 __isl_take isl_schedule_node *node, void *user)
3103 struct isl_node_gist_data *data = user;
3105 do {
3106 isl_union_set *filter, *inner;
3107 int done, empty;
3108 int n;
3110 switch (isl_schedule_node_get_type(node)) {
3111 case isl_schedule_node_error:
3112 return isl_schedule_node_free(node);
3113 case isl_schedule_node_expansion:
3114 node = gist_enter_expansion(node, data);
3115 continue;
3116 case isl_schedule_node_extension:
3117 node = gist_enter_extension(node, data);
3118 continue;
3119 case isl_schedule_node_band:
3120 case isl_schedule_node_context:
3121 case isl_schedule_node_domain:
3122 case isl_schedule_node_guard:
3123 case isl_schedule_node_leaf:
3124 case isl_schedule_node_mark:
3125 case isl_schedule_node_sequence:
3126 case isl_schedule_node_set:
3127 continue;
3128 case isl_schedule_node_filter:
3129 break;
3131 done = gist_done(node, data);
3132 filter = isl_schedule_node_filter_get_filter(node);
3133 if (done < 0 || done) {
3134 data->filters = isl_union_set_list_add(data->filters,
3135 filter);
3136 if (done < 0)
3137 return isl_schedule_node_free(node);
3138 return node;
3140 n = isl_union_set_list_n_union_set(data->filters);
3141 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3142 filter = isl_union_set_gist(filter, isl_union_set_copy(inner));
3143 node = isl_schedule_node_filter_set_filter(node,
3144 isl_union_set_copy(filter));
3145 filter = isl_union_set_intersect(filter, inner);
3146 empty = isl_union_set_is_empty(filter);
3147 data->filters = isl_union_set_list_add(data->filters, filter);
3148 if (empty < 0)
3149 return isl_schedule_node_free(node);
3150 if (!empty)
3151 continue;
3152 node = isl_schedule_node_child(node, 0);
3153 node = isl_schedule_node_cut(node);
3154 node = isl_schedule_node_parent(node);
3155 return node;
3156 } while (isl_schedule_node_has_children(node) &&
3157 (node = isl_schedule_node_first_child(node)) != NULL);
3159 return node;
3162 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
3164 * In particular, if the current node is a filter node, then we remove
3165 * the element on the "filters" list that was added when we entered
3166 * the node. There is no need to compute any gist here, since we
3167 * already did that when we entered the node.
3169 * If the current node is an expansion, then we decrement
3170 * the number of outer expansions and remove the element
3171 * in data->filters that was added by gist_enter_expansion.
3173 * If the current node is an extension, then remove the element
3174 * in data->filters that was added by gist_enter_extension.
3176 * If the current node is a band node, then we compute the gist of
3177 * the band node with respect to the intersection of the original context
3178 * and the intermediate filters.
3180 * If the current node is a sequence or set node, then some of
3181 * the filter children may have become empty and so they are removed.
3182 * If only one child is left, then the set or sequence node along with
3183 * the single remaining child filter is removed. The filter can be
3184 * removed because the filters on a sequence or set node are supposed
3185 * to partition the incoming domain instances.
3186 * In principle, it should then be impossible for there to be zero
3187 * remaining children, but should this happen, we replace the entire
3188 * subtree with an empty filter.
3190 static __isl_give isl_schedule_node *gist_leave(
3191 __isl_take isl_schedule_node *node, void *user)
3193 struct isl_node_gist_data *data = user;
3194 isl_schedule_tree *tree;
3195 int i, n;
3196 isl_union_set *filter;
3198 switch (isl_schedule_node_get_type(node)) {
3199 case isl_schedule_node_error:
3200 return isl_schedule_node_free(node);
3201 case isl_schedule_node_expansion:
3202 data->n_expansion--;
3203 case isl_schedule_node_extension:
3204 case isl_schedule_node_filter:
3205 n = isl_union_set_list_n_union_set(data->filters);
3206 data->filters = isl_union_set_list_drop(data->filters,
3207 n - 1, 1);
3208 break;
3209 case isl_schedule_node_band:
3210 n = isl_union_set_list_n_union_set(data->filters);
3211 filter = isl_union_set_list_get_union_set(data->filters, n - 1);
3212 node = isl_schedule_node_band_gist(node, filter);
3213 break;
3214 case isl_schedule_node_set:
3215 case isl_schedule_node_sequence:
3216 tree = isl_schedule_node_get_tree(node);
3217 n = isl_schedule_tree_n_children(tree);
3218 for (i = n - 1; i >= 0; --i) {
3219 isl_schedule_tree *child;
3220 isl_union_set *filter;
3221 int empty;
3223 child = isl_schedule_tree_get_child(tree, i);
3224 filter = isl_schedule_tree_filter_get_filter(child);
3225 empty = isl_union_set_is_empty(filter);
3226 isl_union_set_free(filter);
3227 isl_schedule_tree_free(child);
3228 if (empty < 0)
3229 tree = isl_schedule_tree_free(tree);
3230 else if (empty)
3231 tree = isl_schedule_tree_drop_child(tree, i);
3233 n = isl_schedule_tree_n_children(tree);
3234 node = isl_schedule_node_graft_tree(node, tree);
3235 if (n == 1) {
3236 node = isl_schedule_node_delete(node);
3237 node = isl_schedule_node_delete(node);
3238 } else if (n == 0) {
3239 isl_space *space;
3241 filter =
3242 isl_union_set_list_get_union_set(data->filters, 0);
3243 space = isl_union_set_get_space(filter);
3244 isl_union_set_free(filter);
3245 filter = isl_union_set_empty(space);
3246 node = isl_schedule_node_cut(node);
3247 node = isl_schedule_node_insert_filter(node, filter);
3249 break;
3250 case isl_schedule_node_context:
3251 case isl_schedule_node_domain:
3252 case isl_schedule_node_guard:
3253 case isl_schedule_node_leaf:
3254 case isl_schedule_node_mark:
3255 break;
3258 return node;
3261 /* Compute the gist of the subtree at "node" with respect to
3262 * the reaching domain elements in "context".
3263 * In particular, compute the gist of all band and filter nodes
3264 * in the subtree with respect to "context". Children of set or sequence
3265 * nodes that end up with an empty filter are removed completely.
3267 * We keep track of the intersection of "context" with all outer filters
3268 * of the current node within the subtree in the final element of "filters".
3269 * Initially, this list contains the single element "context" and it is
3270 * extended or shortened each time we enter or leave a filter node.
3272 __isl_give isl_schedule_node *isl_schedule_node_gist(
3273 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
3275 struct isl_node_gist_data data;
3277 data.n_expansion = 0;
3278 data.filters = isl_union_set_list_from_union_set(context);
3279 node = traverse(node, &gist_enter, &gist_leave, &data);
3280 isl_union_set_list_free(data.filters);
3281 return node;
3284 /* Intersect the domain of domain node "node" with "domain".
3286 * If the domain of "node" is already a subset of "domain",
3287 * then nothing needs to be changed.
3289 * Otherwise, we replace the domain of the domain node by the intersection
3290 * and simplify the subtree rooted at "node" with respect to this intersection.
3292 __isl_give isl_schedule_node *isl_schedule_node_domain_intersect_domain(
3293 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain)
3295 isl_schedule_tree *tree;
3296 isl_union_set *uset;
3297 int is_subset;
3299 if (!node || !domain)
3300 goto error;
3302 uset = isl_schedule_tree_domain_get_domain(node->tree);
3303 is_subset = isl_union_set_is_subset(uset, domain);
3304 isl_union_set_free(uset);
3305 if (is_subset < 0)
3306 goto error;
3307 if (is_subset) {
3308 isl_union_set_free(domain);
3309 return node;
3312 tree = isl_schedule_tree_copy(node->tree);
3313 uset = isl_schedule_tree_domain_get_domain(tree);
3314 uset = isl_union_set_intersect(uset, domain);
3315 tree = isl_schedule_tree_domain_set_domain(tree,
3316 isl_union_set_copy(uset));
3317 node = isl_schedule_node_graft_tree(node, tree);
3319 node = isl_schedule_node_child(node, 0);
3320 node = isl_schedule_node_gist(node, uset);
3321 node = isl_schedule_node_parent(node);
3323 return node;
3324 error:
3325 isl_schedule_node_free(node);
3326 isl_union_set_free(domain);
3327 return NULL;
3330 /* Internal data structure for isl_schedule_node_get_subtree_expansion.
3331 * "expansions" contains a list of accumulated expansions
3332 * for each outer expansion, set or sequence node. The first element
3333 * in the list is an identity mapping on the reaching domain elements.
3334 * "res" collects the results.
3336 struct isl_subtree_expansion_data {
3337 isl_union_map_list *expansions;
3338 isl_union_map *res;
3341 /* Callback for "traverse" to enter a node and to move
3342 * to the deepest initial subtree that should be traversed
3343 * by isl_schedule_node_get_subtree_expansion.
3345 * Whenever we come across an expansion node, the last element
3346 * of data->expansions is combined with the expansion
3347 * on the expansion node.
3349 * Whenever we come across a filter node that is the child
3350 * of a set or sequence node, data->expansions is extended
3351 * with a new element that restricts the previous element
3352 * to the elements selected by the filter.
3353 * The previous element can then be reused while backtracking.
3355 static __isl_give isl_schedule_node *subtree_expansion_enter(
3356 __isl_take isl_schedule_node *node, void *user)
3358 struct isl_subtree_expansion_data *data = user;
3360 do {
3361 enum isl_schedule_node_type type;
3362 isl_union_set *filter;
3363 isl_union_map *inner, *expansion;
3364 int n;
3366 switch (isl_schedule_node_get_type(node)) {
3367 case isl_schedule_node_error:
3368 return isl_schedule_node_free(node);
3369 case isl_schedule_node_filter:
3370 type = isl_schedule_node_get_parent_type(node);
3371 if (type != isl_schedule_node_set &&
3372 type != isl_schedule_node_sequence)
3373 break;
3374 filter = isl_schedule_node_filter_get_filter(node);
3375 n = isl_union_map_list_n_union_map(data->expansions);
3376 inner =
3377 isl_union_map_list_get_union_map(data->expansions,
3378 n - 1);
3379 inner = isl_union_map_intersect_range(inner, filter);
3380 data->expansions =
3381 isl_union_map_list_add(data->expansions, inner);
3382 break;
3383 case isl_schedule_node_expansion:
3384 n = isl_union_map_list_n_union_map(data->expansions);
3385 expansion =
3386 isl_schedule_node_expansion_get_expansion(node);
3387 inner =
3388 isl_union_map_list_get_union_map(data->expansions,
3389 n - 1);
3390 inner = isl_union_map_apply_range(inner, expansion);
3391 data->expansions =
3392 isl_union_map_list_set_union_map(data->expansions,
3393 n - 1, inner);
3394 break;
3395 case isl_schedule_node_band:
3396 case isl_schedule_node_context:
3397 case isl_schedule_node_domain:
3398 case isl_schedule_node_extension:
3399 case isl_schedule_node_guard:
3400 case isl_schedule_node_leaf:
3401 case isl_schedule_node_mark:
3402 case isl_schedule_node_sequence:
3403 case isl_schedule_node_set:
3404 break;
3406 } while (isl_schedule_node_has_children(node) &&
3407 (node = isl_schedule_node_first_child(node)) != NULL);
3409 return node;
3412 /* Callback for "traverse" to leave a node for
3413 * isl_schedule_node_get_subtree_expansion.
3415 * If we come across a filter node that is the child
3416 * of a set or sequence node, then we remove the element
3417 * of data->expansions that was added in subtree_expansion_enter.
3419 * If we reach a leaf node, then the accumulated expansion is
3420 * added to data->res.
3422 static __isl_give isl_schedule_node *subtree_expansion_leave(
3423 __isl_take isl_schedule_node *node, void *user)
3425 struct isl_subtree_expansion_data *data = user;
3426 int n;
3427 isl_union_map *inner;
3428 enum isl_schedule_node_type type;
3430 switch (isl_schedule_node_get_type(node)) {
3431 case isl_schedule_node_error:
3432 return isl_schedule_node_free(node);
3433 case isl_schedule_node_filter:
3434 type = isl_schedule_node_get_parent_type(node);
3435 if (type != isl_schedule_node_set &&
3436 type != isl_schedule_node_sequence)
3437 break;
3438 n = isl_union_map_list_n_union_map(data->expansions);
3439 data->expansions = isl_union_map_list_drop(data->expansions,
3440 n - 1, 1);
3441 break;
3442 case isl_schedule_node_leaf:
3443 n = isl_union_map_list_n_union_map(data->expansions);
3444 inner = isl_union_map_list_get_union_map(data->expansions,
3445 n - 1);
3446 data->res = isl_union_map_union(data->res, inner);
3447 break;
3448 case isl_schedule_node_band:
3449 case isl_schedule_node_context:
3450 case isl_schedule_node_domain:
3451 case isl_schedule_node_expansion:
3452 case isl_schedule_node_extension:
3453 case isl_schedule_node_guard:
3454 case isl_schedule_node_mark:
3455 case isl_schedule_node_sequence:
3456 case isl_schedule_node_set:
3457 break;
3460 return node;
3463 /* Return a mapping from the domain elements that reach "node"
3464 * to the corresponding domain elements in the leaves of the subtree
3465 * rooted at "node" obtained by composing the intermediate expansions.
3467 * We start out with an identity mapping between the domain elements
3468 * that reach "node" and compose it with all the expansions
3469 * on a path from "node" to a leaf while traversing the subtree.
3470 * Within the children of an a sequence or set node, the
3471 * accumulated expansion is restricted to the elements selected
3472 * by the filter child.
3474 __isl_give isl_union_map *isl_schedule_node_get_subtree_expansion(
3475 __isl_keep isl_schedule_node *node)
3477 struct isl_subtree_expansion_data data;
3478 isl_space *space;
3479 isl_union_set *domain;
3480 isl_union_map *expansion;
3482 if (!node)
3483 return NULL;
3485 domain = isl_schedule_node_get_universe_domain(node);
3486 space = isl_union_set_get_space(domain);
3487 expansion = isl_union_set_identity(domain);
3488 data.res = isl_union_map_empty(space);
3489 data.expansions = isl_union_map_list_from_union_map(expansion);
3491 node = isl_schedule_node_copy(node);
3492 node = traverse(node, &subtree_expansion_enter,
3493 &subtree_expansion_leave, &data);
3494 if (!node)
3495 data.res = isl_union_map_free(data.res);
3496 isl_schedule_node_free(node);
3498 isl_union_map_list_free(data.expansions);
3500 return data.res;
3503 /* Internal data structure for isl_schedule_node_get_subtree_contraction.
3504 * "contractions" contains a list of accumulated contractions
3505 * for each outer expansion, set or sequence node. The first element
3506 * in the list is an identity mapping on the reaching domain elements.
3507 * "res" collects the results.
3509 struct isl_subtree_contraction_data {
3510 isl_union_pw_multi_aff_list *contractions;
3511 isl_union_pw_multi_aff *res;
3514 /* Callback for "traverse" to enter a node and to move
3515 * to the deepest initial subtree that should be traversed
3516 * by isl_schedule_node_get_subtree_contraction.
3518 * Whenever we come across an expansion node, the last element
3519 * of data->contractions is combined with the contraction
3520 * on the expansion node.
3522 * Whenever we come across a filter node that is the child
3523 * of a set or sequence node, data->contractions is extended
3524 * with a new element that restricts the previous element
3525 * to the elements selected by the filter.
3526 * The previous element can then be reused while backtracking.
3528 static __isl_give isl_schedule_node *subtree_contraction_enter(
3529 __isl_take isl_schedule_node *node, void *user)
3531 struct isl_subtree_contraction_data *data = user;
3533 do {
3534 enum isl_schedule_node_type type;
3535 isl_union_set *filter;
3536 isl_union_pw_multi_aff *inner, *contraction;
3537 int n;
3539 switch (isl_schedule_node_get_type(node)) {
3540 case isl_schedule_node_error:
3541 return isl_schedule_node_free(node);
3542 case isl_schedule_node_filter:
3543 type = isl_schedule_node_get_parent_type(node);
3544 if (type != isl_schedule_node_set &&
3545 type != isl_schedule_node_sequence)
3546 break;
3547 filter = isl_schedule_node_filter_get_filter(node);
3548 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3549 data->contractions);
3550 inner =
3551 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3552 data->contractions, n - 1);
3553 inner = isl_union_pw_multi_aff_intersect_domain(inner,
3554 filter);
3555 data->contractions =
3556 isl_union_pw_multi_aff_list_add(data->contractions,
3557 inner);
3558 break;
3559 case isl_schedule_node_expansion:
3560 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3561 data->contractions);
3562 contraction =
3563 isl_schedule_node_expansion_get_contraction(node);
3564 inner =
3565 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3566 data->contractions, n - 1);
3567 inner =
3568 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3569 inner, contraction);
3570 data->contractions =
3571 isl_union_pw_multi_aff_list_set_union_pw_multi_aff(
3572 data->contractions, n - 1, inner);
3573 break;
3574 case isl_schedule_node_band:
3575 case isl_schedule_node_context:
3576 case isl_schedule_node_domain:
3577 case isl_schedule_node_extension:
3578 case isl_schedule_node_guard:
3579 case isl_schedule_node_leaf:
3580 case isl_schedule_node_mark:
3581 case isl_schedule_node_sequence:
3582 case isl_schedule_node_set:
3583 break;
3585 } while (isl_schedule_node_has_children(node) &&
3586 (node = isl_schedule_node_first_child(node)) != NULL);
3588 return node;
3591 /* Callback for "traverse" to leave a node for
3592 * isl_schedule_node_get_subtree_contraction.
3594 * If we come across a filter node that is the child
3595 * of a set or sequence node, then we remove the element
3596 * of data->contractions that was added in subtree_contraction_enter.
3598 * If we reach a leaf node, then the accumulated contraction is
3599 * added to data->res.
3601 static __isl_give isl_schedule_node *subtree_contraction_leave(
3602 __isl_take isl_schedule_node *node, void *user)
3604 struct isl_subtree_contraction_data *data = user;
3605 int n;
3606 isl_union_pw_multi_aff *inner;
3607 enum isl_schedule_node_type type;
3609 switch (isl_schedule_node_get_type(node)) {
3610 case isl_schedule_node_error:
3611 return isl_schedule_node_free(node);
3612 case isl_schedule_node_filter:
3613 type = isl_schedule_node_get_parent_type(node);
3614 if (type != isl_schedule_node_set &&
3615 type != isl_schedule_node_sequence)
3616 break;
3617 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3618 data->contractions);
3619 data->contractions =
3620 isl_union_pw_multi_aff_list_drop(data->contractions,
3621 n - 1, 1);
3622 break;
3623 case isl_schedule_node_leaf:
3624 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3625 data->contractions);
3626 inner = isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3627 data->contractions, n - 1);
3628 data->res = isl_union_pw_multi_aff_union_add(data->res, inner);
3629 break;
3630 case isl_schedule_node_band:
3631 case isl_schedule_node_context:
3632 case isl_schedule_node_domain:
3633 case isl_schedule_node_expansion:
3634 case isl_schedule_node_extension:
3635 case isl_schedule_node_guard:
3636 case isl_schedule_node_mark:
3637 case isl_schedule_node_sequence:
3638 case isl_schedule_node_set:
3639 break;
3642 return node;
3645 /* Return a mapping from the domain elements in the leaves of the subtree
3646 * rooted at "node" to the corresponding domain elements that reach "node"
3647 * obtained by composing the intermediate contractions.
3649 * We start out with an identity mapping between the domain elements
3650 * that reach "node" and compose it with all the contractions
3651 * on a path from "node" to a leaf while traversing the subtree.
3652 * Within the children of an a sequence or set node, the
3653 * accumulated contraction is restricted to the elements selected
3654 * by the filter child.
3656 __isl_give isl_union_pw_multi_aff *isl_schedule_node_get_subtree_contraction(
3657 __isl_keep isl_schedule_node *node)
3659 struct isl_subtree_contraction_data data;
3660 isl_space *space;
3661 isl_union_set *domain;
3662 isl_union_pw_multi_aff *contraction;
3664 if (!node)
3665 return NULL;
3667 domain = isl_schedule_node_get_universe_domain(node);
3668 space = isl_union_set_get_space(domain);
3669 contraction = isl_union_set_identity_union_pw_multi_aff(domain);
3670 data.res = isl_union_pw_multi_aff_empty(space);
3671 data.contractions =
3672 isl_union_pw_multi_aff_list_from_union_pw_multi_aff(contraction);
3674 node = isl_schedule_node_copy(node);
3675 node = traverse(node, &subtree_contraction_enter,
3676 &subtree_contraction_leave, &data);
3677 if (!node)
3678 data.res = isl_union_pw_multi_aff_free(data.res);
3679 isl_schedule_node_free(node);
3681 isl_union_pw_multi_aff_list_free(data.contractions);
3683 return data.res;
3686 /* Do the nearest "n" ancestors of "node" have the types given in "types"
3687 * (starting at the parent of "node")?
3689 static int has_ancestors(__isl_keep isl_schedule_node *node,
3690 int n, enum isl_schedule_node_type *types)
3692 int i, n_ancestor;
3694 if (!node)
3695 return -1;
3697 n_ancestor = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
3698 if (n_ancestor < n)
3699 return 0;
3701 for (i = 0; i < n; ++i) {
3702 isl_schedule_tree *tree;
3703 int correct_type;
3705 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
3706 n_ancestor - 1 - i);
3707 if (!tree)
3708 return -1;
3709 correct_type = isl_schedule_tree_get_type(tree) == types[i];
3710 isl_schedule_tree_free(tree);
3711 if (!correct_type)
3712 return 0;
3715 return 1;
3718 /* Given a node "node" that appears in an extension (i.e., it is the child
3719 * of a filter in a sequence inside an extension node), are the spaces
3720 * of the extension specified by "extension" disjoint from those
3721 * of both the original extension and the domain elements that reach
3722 * that original extension?
3724 static int is_disjoint_extension(__isl_keep isl_schedule_node *node,
3725 __isl_keep isl_union_map *extension)
3727 isl_union_map *old;
3728 isl_union_set *domain;
3729 int empty;
3731 node = isl_schedule_node_copy(node);
3732 node = isl_schedule_node_parent(node);
3733 node = isl_schedule_node_parent(node);
3734 node = isl_schedule_node_parent(node);
3735 old = isl_schedule_node_extension_get_extension(node);
3736 domain = isl_schedule_node_get_universe_domain(node);
3737 isl_schedule_node_free(node);
3738 old = isl_union_map_universe(old);
3739 domain = isl_union_set_union(domain, isl_union_map_range(old));
3740 extension = isl_union_map_copy(extension);
3741 extension = isl_union_map_intersect_range(extension, domain);
3742 empty = isl_union_map_is_empty(extension);
3743 isl_union_map_free(extension);
3745 return empty;
3748 /* Given a node "node" that is governed by an extension node, extend
3749 * that extension node with "extension".
3751 * In particular, "node" is the child of a filter in a sequence that
3752 * is in turn a child of an extension node. Extend that extension node
3753 * with "extension".
3755 * Return a pointer to the parent of the original node (i.e., a filter).
3757 static __isl_give isl_schedule_node *extend_extension(
3758 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
3760 int pos;
3761 int disjoint;
3762 isl_union_map *node_extension;
3764 node = isl_schedule_node_parent(node);
3765 pos = isl_schedule_node_get_child_position(node);
3766 node = isl_schedule_node_parent(node);
3767 node = isl_schedule_node_parent(node);
3768 node_extension = isl_schedule_node_extension_get_extension(node);
3769 disjoint = isl_union_map_is_disjoint(extension, node_extension);
3770 extension = isl_union_map_union(extension, node_extension);
3771 node = isl_schedule_node_extension_set_extension(node, extension);
3772 node = isl_schedule_node_child(node, 0);
3773 node = isl_schedule_node_child(node, pos);
3775 if (disjoint < 0)
3776 return isl_schedule_node_free(node);
3777 if (!node)
3778 return NULL;
3779 if (!disjoint)
3780 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
3781 "extension domain should be disjoint from earlier "
3782 "extensions", return isl_schedule_node_free(node));
3784 return node;
3787 /* Return the universe of "uset" if this universe is disjoint from "ref".
3788 * Otherwise, return "uset".
3790 * Also check if "uset" itself is disjoint from "ref", reporting
3791 * an error if it is not.
3793 static __isl_give isl_union_set *replace_by_universe_if_disjoint(
3794 __isl_take isl_union_set *uset, __isl_keep isl_union_set *ref)
3796 int disjoint;
3797 isl_union_set *universe;
3799 disjoint = isl_union_set_is_disjoint(uset, ref);
3800 if (disjoint < 0)
3801 return isl_union_set_free(uset);
3802 if (!disjoint)
3803 isl_die(isl_union_set_get_ctx(uset), isl_error_invalid,
3804 "extension domain should be disjoint from "
3805 "current domain", return isl_union_set_free(uset));
3807 universe = isl_union_set_universe(isl_union_set_copy(uset));
3808 disjoint = isl_union_set_is_disjoint(universe, ref);
3809 if (disjoint >= 0 && disjoint) {
3810 isl_union_set_free(uset);
3811 return universe;
3813 isl_union_set_free(universe);
3815 if (disjoint < 0)
3816 return isl_union_set_free(uset);
3817 return uset;
3820 /* Insert an extension node on top of "node" with extension "extension".
3821 * In addition, insert a filter that separates node from the extension
3822 * between the extension node and "node".
3823 * Return a pointer to the inserted filter node.
3825 * If "node" already appears in an extension (i.e., if it is the child
3826 * of a filter in a sequence inside an extension node), then extend that
3827 * extension with "extension" instead.
3828 * In this case, a pointer to the original filter node is returned.
3829 * Note that if some of the elements in the new extension live in the
3830 * same space as those of the original extension or the domain elements
3831 * reaching the original extension, then we insert a new extension anyway.
3832 * Otherwise, we would have to adjust the filters in the sequence child
3833 * of the extension to ensure that the elements in the new extension
3834 * are filtered out.
3836 static __isl_give isl_schedule_node *insert_extension(
3837 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
3839 enum isl_schedule_node_type ancestors[] =
3840 { isl_schedule_node_filter, isl_schedule_node_sequence,
3841 isl_schedule_node_extension };
3842 isl_union_set *domain;
3843 isl_union_set *filter;
3844 int in_ext;
3846 in_ext = has_ancestors(node, 3, ancestors);
3847 if (in_ext < 0)
3848 goto error;
3849 if (in_ext) {
3850 int disjoint;
3852 disjoint = is_disjoint_extension(node, extension);
3853 if (disjoint < 0)
3854 goto error;
3855 if (disjoint)
3856 return extend_extension(node, extension);
3859 filter = isl_schedule_node_get_domain(node);
3860 domain = isl_union_map_range(isl_union_map_copy(extension));
3861 filter = replace_by_universe_if_disjoint(filter, domain);
3862 isl_union_set_free(domain);
3864 node = isl_schedule_node_insert_filter(node, filter);
3865 node = isl_schedule_node_insert_extension(node, extension);
3866 node = isl_schedule_node_child(node, 0);
3867 return node;
3868 error:
3869 isl_schedule_node_free(node);
3870 isl_union_map_free(extension);
3871 return NULL;
3874 /* Replace the subtree that "node" points to by "tree" (which has
3875 * a sequence root with two children), except if the parent of "node"
3876 * is a sequence as well, in which case "tree" is spliced at the position
3877 * of "node" in its parent.
3878 * Return a pointer to the child of the "tree_pos" (filter) child of "tree"
3879 * in the updated schedule tree.
3881 static __isl_give isl_schedule_node *graft_or_splice(
3882 __isl_take isl_schedule_node *node, __isl_take isl_schedule_tree *tree,
3883 int tree_pos)
3885 int pos;
3887 if (isl_schedule_node_get_parent_type(node) ==
3888 isl_schedule_node_sequence) {
3889 pos = isl_schedule_node_get_child_position(node);
3890 node = isl_schedule_node_parent(node);
3891 node = isl_schedule_node_sequence_splice(node, pos, tree);
3892 } else {
3893 pos = 0;
3894 node = isl_schedule_node_graft_tree(node, tree);
3896 node = isl_schedule_node_child(node, pos + tree_pos);
3897 node = isl_schedule_node_child(node, 0);
3899 return node;
3902 /* Insert a node "graft" into the schedule tree of "node" such that it
3903 * is executed before (if "before" is set) or after (if "before" is not set)
3904 * the node that "node" points to.
3905 * The root of "graft" is an extension node.
3906 * Return a pointer to the node that "node" pointed to.
3908 * We first insert an extension node on top of "node" (or extend
3909 * the extension node if there already is one), with a filter on "node"
3910 * separating it from the extension.
3911 * We then insert a filter in the graft to separate it from the original
3912 * domain elements and combine the original and new tree in a sequence.
3913 * If we have extended an extension node, then the children of this
3914 * sequence are spliced in the sequence of the extended extension
3915 * at the position where "node" appears in the original extension.
3916 * Otherwise, the sequence pair is attached to the new extension node.
3918 static __isl_give isl_schedule_node *graft_extension(
3919 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft,
3920 int before)
3922 isl_union_map *extension;
3923 isl_union_set *graft_domain;
3924 isl_union_set *node_domain;
3925 isl_schedule_tree *tree, *tree_graft;
3927 extension = isl_schedule_node_extension_get_extension(graft);
3928 graft_domain = isl_union_map_range(isl_union_map_copy(extension));
3929 node_domain = isl_schedule_node_get_universe_domain(node);
3930 node = insert_extension(node, extension);
3932 graft_domain = replace_by_universe_if_disjoint(graft_domain,
3933 node_domain);
3934 isl_union_set_free(node_domain);
3936 tree = isl_schedule_node_get_tree(node);
3937 if (!isl_schedule_node_has_children(graft)) {
3938 tree_graft = isl_schedule_tree_from_filter(graft_domain);
3939 } else {
3940 graft = isl_schedule_node_child(graft, 0);
3941 tree_graft = isl_schedule_node_get_tree(graft);
3942 tree_graft = isl_schedule_tree_insert_filter(tree_graft,
3943 graft_domain);
3945 if (before)
3946 tree = isl_schedule_tree_sequence_pair(tree_graft, tree);
3947 else
3948 tree = isl_schedule_tree_sequence_pair(tree, tree_graft);
3949 node = graft_or_splice(node, tree, before);
3951 isl_schedule_node_free(graft);
3953 return node;
3956 /* Replace the root domain node of "node" by an extension node suitable
3957 * for insertion at "pos".
3958 * That is, create an extension node that maps the outer band nodes
3959 * at "pos" to the domain of the root node of "node" and attach
3960 * the child of this root node to the extension node.
3962 static __isl_give isl_schedule_node *extension_from_domain(
3963 __isl_take isl_schedule_node *node, __isl_keep isl_schedule_node *pos)
3965 isl_union_set *universe;
3966 isl_union_set *domain;
3967 isl_union_map *ext;
3968 int depth;
3969 int anchored;
3970 isl_space *space;
3971 isl_schedule_node *res;
3972 isl_schedule_tree *tree;
3974 anchored = isl_schedule_node_is_subtree_anchored(node);
3975 if (anchored < 0)
3976 return isl_schedule_node_free(node);
3977 if (anchored)
3978 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
3979 "cannot graft anchored tree with domain root",
3980 return isl_schedule_node_free(node));
3982 depth = isl_schedule_node_get_schedule_depth(pos);
3983 domain = isl_schedule_node_domain_get_domain(node);
3984 space = isl_union_set_get_space(domain);
3985 space = isl_space_set_from_params(space);
3986 space = isl_space_add_dims(space, isl_dim_set, depth);
3987 universe = isl_union_set_from_set(isl_set_universe(space));
3988 ext = isl_union_map_from_domain_and_range(universe, domain);
3989 res = isl_schedule_node_from_extension(ext);
3990 node = isl_schedule_node_child(node, 0);
3991 if (!node)
3992 return isl_schedule_node_free(res);
3993 if (!isl_schedule_tree_is_leaf(node->tree)) {
3994 tree = isl_schedule_node_get_tree(node);
3995 res = isl_schedule_node_child(res, 0);
3996 res = isl_schedule_node_graft_tree(res, tree);
3997 res = isl_schedule_node_parent(res);
3999 isl_schedule_node_free(node);
4001 return res;
4004 /* Insert a node "graft" into the schedule tree of "node" such that it
4005 * is executed before (if "before" is set) or after (if "before" is not set)
4006 * the node that "node" points to.
4007 * The root of "graft" may be either a domain or an extension node.
4008 * In the latter case, the domain of the extension needs to correspond
4009 * to the outer band nodes of "node".
4010 * The elements of the domain or the range of the extension may not
4011 * intersect with the domain elements that reach "node".
4012 * The schedule tree of "graft" may not be anchored.
4014 * The schedule tree of "node" is modified to include an extension node
4015 * corresponding to the root node of "graft" as a child of the original
4016 * parent of "node". The original node that "node" points to and the
4017 * child of the root node of "graft" are attached to this extension node
4018 * through a sequence, with appropriate filters and with the child
4019 * of "graft" appearing before or after the original "node".
4021 * If "node" already appears inside a sequence that is the child of
4022 * an extension node and if the spaces of the new domain elements
4023 * do not overlap with those of the original domain elements,
4024 * then that extension node is extended with the new extension
4025 * rather than introducing a new segment of extension and sequence nodes.
4027 * Return a pointer to the same node in the modified tree that
4028 * "node" pointed to in the original tree.
4030 static __isl_give isl_schedule_node *isl_schedule_node_graft_before_or_after(
4031 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft,
4032 int before)
4034 if (!node || !graft)
4035 goto error;
4036 if (check_insert(node) < 0)
4037 goto error;
4039 if (isl_schedule_node_get_type(graft) == isl_schedule_node_domain)
4040 graft = extension_from_domain(graft, node);
4042 if (isl_schedule_node_get_type(graft) != isl_schedule_node_extension)
4043 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4044 "expecting domain or extension as root of graft",
4045 goto error);
4047 return graft_extension(node, graft, before);
4048 error:
4049 isl_schedule_node_free(node);
4050 isl_schedule_node_free(graft);
4051 return NULL;
4054 /* Insert a node "graft" into the schedule tree of "node" such that it
4055 * is executed before the node that "node" points to.
4056 * The root of "graft" may be either a domain or an extension node.
4057 * In the latter case, the domain of the extension needs to correspond
4058 * to the outer band nodes of "node".
4059 * The elements of the domain or the range of the extension may not
4060 * intersect with the domain elements that reach "node".
4061 * The schedule tree of "graft" may not be anchored.
4063 * Return a pointer to the same node in the modified tree that
4064 * "node" pointed to in the original tree.
4066 __isl_give isl_schedule_node *isl_schedule_node_graft_before(
4067 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft)
4069 return isl_schedule_node_graft_before_or_after(node, graft, 1);
4072 /* Insert a node "graft" into the schedule tree of "node" such that it
4073 * is executed after the node that "node" points to.
4074 * The root of "graft" may be either a domain or an extension node.
4075 * In the latter case, the domain of the extension needs to correspond
4076 * to the outer band nodes of "node".
4077 * The elements of the domain or the range of the extension may not
4078 * intersect with the domain elements that reach "node".
4079 * The schedule tree of "graft" may not be anchored.
4081 * Return a pointer to the same node in the modified tree that
4082 * "node" pointed to in the original tree.
4084 __isl_give isl_schedule_node *isl_schedule_node_graft_after(
4085 __isl_take isl_schedule_node *node,
4086 __isl_take isl_schedule_node *graft)
4088 return isl_schedule_node_graft_before_or_after(node, graft, 0);
4091 /* Split the domain elements that reach "node" into those that satisfy
4092 * "filter" and those that do not. Arrange for the first subset to be
4093 * executed after the second subset.
4094 * Return a pointer to the tree corresponding to the second subset,
4095 * except when this subset is empty in which case the original pointer
4096 * is returned.
4097 * If both subsets are non-empty, then a sequence node is introduced
4098 * to impose the order. If the grandparent of the original node was
4099 * itself a sequence, then the original child is replaced by two children
4100 * in this sequence instead.
4101 * The children in the sequence are copies of the original subtree,
4102 * simplified with respect to their filters.
4104 __isl_give isl_schedule_node *isl_schedule_node_order_after(
4105 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
4107 enum isl_schedule_node_type ancestors[] =
4108 { isl_schedule_node_filter, isl_schedule_node_sequence };
4109 isl_union_set *node_domain, *node_filter = NULL;
4110 isl_schedule_node *node2;
4111 isl_schedule_tree *tree1, *tree2;
4112 int empty1, empty2;
4113 int in_seq;
4115 if (!node || !filter)
4116 goto error;
4117 if (check_insert(node) < 0)
4118 goto error;
4120 in_seq = has_ancestors(node, 2, ancestors);
4121 if (in_seq < 0)
4122 goto error;
4123 if (in_seq)
4124 node = isl_schedule_node_parent(node);
4125 node_domain = isl_schedule_node_get_domain(node);
4126 filter = isl_union_set_gist(filter, isl_union_set_copy(node_domain));
4127 node_filter = isl_union_set_copy(node_domain);
4128 node_filter = isl_union_set_subtract(node_filter,
4129 isl_union_set_copy(filter));
4130 node_filter = isl_union_set_gist(node_filter, node_domain);
4131 empty1 = isl_union_set_is_empty(filter);
4132 empty2 = isl_union_set_is_empty(node_filter);
4133 if (empty1 < 0 || empty2 < 0)
4134 goto error;
4135 if (empty1 || empty2) {
4136 isl_union_set_free(filter);
4137 isl_union_set_free(node_filter);
4138 return node;
4141 node2 = isl_schedule_node_copy(node);
4142 node = isl_schedule_node_gist(node, isl_union_set_copy(node_filter));
4143 node2 = isl_schedule_node_gist(node2, isl_union_set_copy(filter));
4144 tree1 = isl_schedule_node_get_tree(node);
4145 tree2 = isl_schedule_node_get_tree(node2);
4146 isl_schedule_node_free(node2);
4147 tree1 = isl_schedule_tree_insert_filter(tree1, node_filter);
4148 tree2 = isl_schedule_tree_insert_filter(tree2, filter);
4149 tree1 = isl_schedule_tree_sequence_pair(tree1, tree2);
4151 node = graft_or_splice(node, tree1, 0);
4153 return node;
4154 error:
4155 isl_schedule_node_free(node);
4156 isl_union_set_free(filter);
4157 isl_union_set_free(node_filter);
4158 return NULL;
4161 /* Reset the user pointer on all identifiers of parameters and tuples
4162 * in the schedule node "node".
4164 __isl_give isl_schedule_node *isl_schedule_node_reset_user(
4165 __isl_take isl_schedule_node *node)
4167 isl_schedule_tree *tree;
4169 tree = isl_schedule_node_get_tree(node);
4170 tree = isl_schedule_tree_reset_user(tree);
4171 node = isl_schedule_node_graft_tree(node, tree);
4173 return node;
4176 /* Align the parameters of the schedule node "node" to those of "space".
4178 __isl_give isl_schedule_node *isl_schedule_node_align_params(
4179 __isl_take isl_schedule_node *node, __isl_take isl_space *space)
4181 isl_schedule_tree *tree;
4183 tree = isl_schedule_node_get_tree(node);
4184 tree = isl_schedule_tree_align_params(tree, space);
4185 node = isl_schedule_node_graft_tree(node, tree);
4187 return node;
4190 /* Compute the pullback of schedule node "node"
4191 * by the function represented by "upma".
4192 * In other words, plug in "upma" in the iteration domains
4193 * of schedule node "node".
4194 * We currently do not handle expansion nodes.
4196 * Note that this is only a helper function for
4197 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
4198 * this function should not be called on a single node without also
4199 * calling it on all the other nodes.
4201 __isl_give isl_schedule_node *isl_schedule_node_pullback_union_pw_multi_aff(
4202 __isl_take isl_schedule_node *node,
4203 __isl_take isl_union_pw_multi_aff *upma)
4205 isl_schedule_tree *tree;
4207 tree = isl_schedule_node_get_tree(node);
4208 tree = isl_schedule_tree_pullback_union_pw_multi_aff(tree, upma);
4209 node = isl_schedule_node_graft_tree(node, tree);
4211 return node;
4214 /* Return the position of the subtree containing "node" among the children
4215 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
4216 * In particular, both nodes should point to the same schedule tree.
4218 * Return -1 on error.
4220 int isl_schedule_node_get_ancestor_child_position(
4221 __isl_keep isl_schedule_node *node,
4222 __isl_keep isl_schedule_node *ancestor)
4224 int n1, n2;
4225 isl_schedule_tree *tree;
4227 if (!node || !ancestor)
4228 return -1;
4230 if (node->schedule != ancestor->schedule)
4231 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4232 "not a descendant", return -1);
4234 n1 = isl_schedule_node_get_tree_depth(ancestor);
4235 n2 = isl_schedule_node_get_tree_depth(node);
4237 if (n1 >= n2)
4238 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4239 "not a descendant", return -1);
4240 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n1);
4241 isl_schedule_tree_free(tree);
4242 if (tree != ancestor->tree)
4243 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4244 "not a descendant", return -1);
4246 return node->child_pos[n1];
4249 /* Given two nodes that point to the same schedule tree, return their
4250 * closest shared ancestor.
4252 * Since the two nodes point to the same schedule, they share at least
4253 * one ancestor, the root of the schedule. We move down from the root
4254 * to the first ancestor where the respective children have a different
4255 * child position. This is the requested ancestor.
4256 * If there is no ancestor where the children have a different position,
4257 * then one node is an ancestor of the other and then this node is
4258 * the requested ancestor.
4260 __isl_give isl_schedule_node *isl_schedule_node_get_shared_ancestor(
4261 __isl_keep isl_schedule_node *node1,
4262 __isl_keep isl_schedule_node *node2)
4264 int i, n1, n2;
4266 if (!node1 || !node2)
4267 return NULL;
4268 if (node1->schedule != node2->schedule)
4269 isl_die(isl_schedule_node_get_ctx(node1), isl_error_invalid,
4270 "not part of same schedule", return NULL);
4271 n1 = isl_schedule_node_get_tree_depth(node1);
4272 n2 = isl_schedule_node_get_tree_depth(node2);
4273 if (n2 < n1)
4274 return isl_schedule_node_get_shared_ancestor(node2, node1);
4275 if (n1 == 0)
4276 return isl_schedule_node_copy(node1);
4277 if (isl_schedule_node_is_equal(node1, node2))
4278 return isl_schedule_node_copy(node1);
4280 for (i = 0; i < n1; ++i)
4281 if (node1->child_pos[i] != node2->child_pos[i])
4282 break;
4284 node1 = isl_schedule_node_copy(node1);
4285 return isl_schedule_node_ancestor(node1, n1 - i);
4288 /* Print "node" to "p".
4290 __isl_give isl_printer *isl_printer_print_schedule_node(
4291 __isl_take isl_printer *p, __isl_keep isl_schedule_node *node)
4293 if (!node)
4294 return isl_printer_free(p);
4295 return isl_printer_print_schedule_tree_mark(p, node->schedule->root,
4296 isl_schedule_tree_list_n_schedule_tree(node->ancestors),
4297 node->child_pos);
4300 void isl_schedule_node_dump(__isl_keep isl_schedule_node *node)
4302 isl_ctx *ctx;
4303 isl_printer *printer;
4305 if (!node)
4306 return;
4308 ctx = isl_schedule_node_get_ctx(node);
4309 printer = isl_printer_to_file(ctx, stderr);
4310 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
4311 printer = isl_printer_print_schedule_node(printer, node);
4313 isl_printer_free(printer);