isl_tab_detect_redundant: check later constraints first
[isl.git] / isl_schedule_node.c
blob95581507e01925887d0869eb2afe271cd443a5df
1 /*
2 * Copyright 2013-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege,
9 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
10 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
11 * B.P. 105 - 78153 Le Chesnay, France
14 #include <isl/set.h>
15 #include <isl_schedule_band.h>
16 #include <isl_schedule_private.h>
17 #include <isl_schedule_node_private.h>
19 /* Create a new schedule node in the given schedule, point at the given
20 * tree with given ancestors and child positions.
21 * "child_pos" may be NULL if there are no ancestors.
23 __isl_give isl_schedule_node *isl_schedule_node_alloc(
24 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree,
25 __isl_take isl_schedule_tree_list *ancestors, int *child_pos)
27 isl_ctx *ctx;
28 isl_schedule_node *node;
29 int i, n;
31 if (!schedule || !tree || !ancestors)
32 goto error;
33 n = isl_schedule_tree_list_n_schedule_tree(ancestors);
34 if (n > 0 && !child_pos)
35 goto error;
36 ctx = isl_schedule_get_ctx(schedule);
37 node = isl_calloc_type(ctx, isl_schedule_node);
38 if (!node)
39 goto error;
40 node->ref = 1;
41 node->schedule = schedule;
42 node->tree = tree;
43 node->ancestors = ancestors;
44 node->child_pos = isl_alloc_array(ctx, int, n);
45 if (n && !node->child_pos)
46 return isl_schedule_node_free(node);
47 for (i = 0; i < n; ++i)
48 node->child_pos[i] = child_pos[i];
50 return node;
51 error:
52 isl_schedule_free(schedule);
53 isl_schedule_tree_free(tree);
54 isl_schedule_tree_list_free(ancestors);
55 return NULL;
58 /* Return a pointer to the root of a schedule tree with as single
59 * node a domain node with the given domain.
61 __isl_give isl_schedule_node *isl_schedule_node_from_domain(
62 __isl_take isl_union_set *domain)
64 isl_schedule *schedule;
65 isl_schedule_node *node;
67 schedule = isl_schedule_from_domain(domain);
68 node = isl_schedule_get_root(schedule);
69 isl_schedule_free(schedule);
71 return node;
74 /* Return a pointer to the root of a schedule tree with as single
75 * node a extension node with the given extension.
77 __isl_give isl_schedule_node *isl_schedule_node_from_extension(
78 __isl_take isl_union_map *extension)
80 isl_ctx *ctx;
81 isl_schedule *schedule;
82 isl_schedule_tree *tree;
83 isl_schedule_node *node;
85 if (!extension)
86 return NULL;
88 ctx = isl_union_map_get_ctx(extension);
89 tree = isl_schedule_tree_from_extension(extension);
90 schedule = isl_schedule_from_schedule_tree(ctx, tree);
91 node = isl_schedule_get_root(schedule);
92 isl_schedule_free(schedule);
94 return node;
97 /* Return the isl_ctx to which "node" belongs.
99 isl_ctx *isl_schedule_node_get_ctx(__isl_keep isl_schedule_node *node)
101 return node ? isl_schedule_get_ctx(node->schedule) : NULL;
104 /* Return a pointer to the leaf of the schedule into which "node" points.
106 __isl_keep isl_schedule_tree *isl_schedule_node_peek_leaf(
107 __isl_keep isl_schedule_node *node)
109 return node ? isl_schedule_peek_leaf(node->schedule) : NULL;
112 /* Return a copy of the leaf of the schedule into which "node" points.
114 __isl_give isl_schedule_tree *isl_schedule_node_get_leaf(
115 __isl_keep isl_schedule_node *node)
117 return isl_schedule_tree_copy(isl_schedule_node_peek_leaf(node));
120 /* Return the type of the node or isl_schedule_node_error on error.
122 enum isl_schedule_node_type isl_schedule_node_get_type(
123 __isl_keep isl_schedule_node *node)
125 return node ? isl_schedule_tree_get_type(node->tree)
126 : isl_schedule_node_error;
129 /* Return the type of the parent of "node" or isl_schedule_node_error on error.
131 enum isl_schedule_node_type isl_schedule_node_get_parent_type(
132 __isl_keep isl_schedule_node *node)
134 int pos;
135 int has_parent;
136 isl_schedule_tree *parent;
137 enum isl_schedule_node_type type;
139 if (!node)
140 return isl_schedule_node_error;
141 has_parent = isl_schedule_node_has_parent(node);
142 if (has_parent < 0)
143 return isl_schedule_node_error;
144 if (!has_parent)
145 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
146 "node has no parent", return isl_schedule_node_error);
148 pos = isl_schedule_tree_list_n_schedule_tree(node->ancestors) - 1;
149 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors, pos);
150 type = isl_schedule_tree_get_type(parent);
151 isl_schedule_tree_free(parent);
153 return type;
156 /* Return a copy of the subtree that this node points to.
158 __isl_give isl_schedule_tree *isl_schedule_node_get_tree(
159 __isl_keep isl_schedule_node *node)
161 if (!node)
162 return NULL;
164 return isl_schedule_tree_copy(node->tree);
167 /* Return a copy of the schedule into which "node" points.
169 __isl_give isl_schedule *isl_schedule_node_get_schedule(
170 __isl_keep isl_schedule_node *node)
172 if (!node)
173 return NULL;
174 return isl_schedule_copy(node->schedule);
177 /* Return a fresh copy of "node".
179 __isl_take isl_schedule_node *isl_schedule_node_dup(
180 __isl_keep isl_schedule_node *node)
182 if (!node)
183 return NULL;
185 return isl_schedule_node_alloc(isl_schedule_copy(node->schedule),
186 isl_schedule_tree_copy(node->tree),
187 isl_schedule_tree_list_copy(node->ancestors),
188 node->child_pos);
191 /* Return an isl_schedule_node that is equal to "node" and that has only
192 * a single reference.
194 __isl_give isl_schedule_node *isl_schedule_node_cow(
195 __isl_take isl_schedule_node *node)
197 if (!node)
198 return NULL;
200 if (node->ref == 1)
201 return node;
202 node->ref--;
203 return isl_schedule_node_dup(node);
206 /* Return a new reference to "node".
208 __isl_give isl_schedule_node *isl_schedule_node_copy(
209 __isl_keep isl_schedule_node *node)
211 if (!node)
212 return NULL;
214 node->ref++;
215 return node;
218 /* Free "node" and return NULL.
220 * Since the node may point to a leaf of its schedule, which
221 * point to a field inside the schedule, we need to make sure
222 * we free the tree before freeing the schedule.
224 __isl_null isl_schedule_node *isl_schedule_node_free(
225 __isl_take isl_schedule_node *node)
227 if (!node)
228 return NULL;
229 if (--node->ref > 0)
230 return NULL;
232 isl_schedule_tree_list_free(node->ancestors);
233 free(node->child_pos);
234 isl_schedule_tree_free(node->tree);
235 isl_schedule_free(node->schedule);
236 free(node);
238 return NULL;
241 /* Do "node1" and "node2" point to the same position in the same
242 * schedule?
244 isl_bool isl_schedule_node_is_equal(__isl_keep isl_schedule_node *node1,
245 __isl_keep isl_schedule_node *node2)
247 int i, n1, n2;
249 if (!node1 || !node2)
250 return isl_bool_error;
251 if (node1 == node2)
252 return isl_bool_true;
253 if (node1->schedule != node2->schedule)
254 return isl_bool_false;
256 n1 = isl_schedule_node_get_tree_depth(node1);
257 n2 = isl_schedule_node_get_tree_depth(node2);
258 if (n1 != n2)
259 return isl_bool_false;
260 for (i = 0; i < n1; ++i)
261 if (node1->child_pos[i] != node2->child_pos[i])
262 return isl_bool_false;
264 return isl_bool_true;
267 /* Return the number of outer schedule dimensions of "node"
268 * in its schedule tree.
270 * Return -1 on error.
272 int isl_schedule_node_get_schedule_depth(__isl_keep isl_schedule_node *node)
274 int i, n;
275 int depth = 0;
277 if (!node)
278 return -1;
280 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
281 for (i = n - 1; i >= 0; --i) {
282 isl_schedule_tree *tree;
284 tree = isl_schedule_tree_list_get_schedule_tree(
285 node->ancestors, i);
286 if (!tree)
287 return -1;
288 if (tree->type == isl_schedule_node_band)
289 depth += isl_schedule_tree_band_n_member(tree);
290 isl_schedule_tree_free(tree);
293 return depth;
296 /* Internal data structure for
297 * isl_schedule_node_get_prefix_schedule_union_pw_multi_aff
299 * "initialized" is set if the filter field has been initialized.
300 * If "universe_domain" is not set, then the collected filter is intersected
301 * with the the domain of the root domain node.
302 * "universe_filter" is set if we are only collecting the universes of filters
303 * "collect_prefix" is set if we are collecting prefixes.
304 * "filter" collects all outer filters and is NULL until "initialized" is set.
305 * "prefix" collects all outer band partial schedules (if "collect_prefix"
306 * is set). If it is used, then it is initialized by the caller
307 * of collect_filter_prefix to a zero-dimensional function.
309 struct isl_schedule_node_get_filter_prefix_data {
310 int initialized;
311 int universe_domain;
312 int universe_filter;
313 int collect_prefix;
314 isl_union_set *filter;
315 isl_multi_union_pw_aff *prefix;
318 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
319 int n, struct isl_schedule_node_get_filter_prefix_data *data);
321 /* Update the filter and prefix information in "data" based on the first "n"
322 * elements in "list" and the expansion tree root "tree".
324 * We first collect the information from the elements in "list",
325 * initializing the filter based on the domain of the expansion.
326 * Then we map the results to the expanded space and combined them
327 * with the results already in "data".
329 static int collect_filter_prefix_expansion(__isl_take isl_schedule_tree *tree,
330 __isl_keep isl_schedule_tree_list *list, int n,
331 struct isl_schedule_node_get_filter_prefix_data *data)
333 struct isl_schedule_node_get_filter_prefix_data contracted;
334 isl_union_pw_multi_aff *c;
335 isl_union_map *exp, *universe;
336 isl_union_set *filter;
338 c = isl_schedule_tree_expansion_get_contraction(tree);
339 exp = isl_schedule_tree_expansion_get_expansion(tree);
341 contracted.initialized = 1;
342 contracted.universe_domain = data->universe_domain;
343 contracted.universe_filter = data->universe_filter;
344 contracted.collect_prefix = data->collect_prefix;
345 universe = isl_union_map_universe(isl_union_map_copy(exp));
346 filter = isl_union_map_domain(universe);
347 if (data->collect_prefix) {
348 isl_space *space = isl_union_set_get_space(filter);
349 space = isl_space_set_from_params(space);
350 contracted.prefix = isl_multi_union_pw_aff_zero(space);
352 contracted.filter = filter;
354 if (collect_filter_prefix(list, n, &contracted) < 0)
355 contracted.filter = isl_union_set_free(contracted.filter);
356 if (data->collect_prefix) {
357 isl_multi_union_pw_aff *prefix;
359 prefix = contracted.prefix;
360 prefix =
361 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix,
362 isl_union_pw_multi_aff_copy(c));
363 data->prefix = isl_multi_union_pw_aff_flat_range_product(
364 prefix, data->prefix);
366 filter = contracted.filter;
367 if (data->universe_domain)
368 filter = isl_union_set_preimage_union_pw_multi_aff(filter,
369 isl_union_pw_multi_aff_copy(c));
370 else
371 filter = isl_union_set_apply(filter, isl_union_map_copy(exp));
372 if (!data->initialized)
373 data->filter = filter;
374 else
375 data->filter = isl_union_set_intersect(filter, data->filter);
376 data->initialized = 1;
378 isl_union_pw_multi_aff_free(c);
379 isl_union_map_free(exp);
380 isl_schedule_tree_free(tree);
382 return 0;
385 /* Update the filter information in "data" based on the first "n"
386 * elements in "list" and the extension tree root "tree", in case
387 * data->universe_domain is set and data->collect_prefix is not.
389 * We collect the universe domain of the elements in "list" and
390 * add it to the universe range of the extension (intersected
391 * with the already collected filter, if any).
393 static int collect_universe_domain_extension(__isl_take isl_schedule_tree *tree,
394 __isl_keep isl_schedule_tree_list *list, int n,
395 struct isl_schedule_node_get_filter_prefix_data *data)
397 struct isl_schedule_node_get_filter_prefix_data data_outer;
398 isl_union_map *extension;
399 isl_union_set *filter;
401 data_outer.initialized = 0;
402 data_outer.universe_domain = 1;
403 data_outer.universe_filter = data->universe_filter;
404 data_outer.collect_prefix = 0;
405 data_outer.filter = NULL;
406 data_outer.prefix = NULL;
408 if (collect_filter_prefix(list, n, &data_outer) < 0)
409 data_outer.filter = isl_union_set_free(data_outer.filter);
411 extension = isl_schedule_tree_extension_get_extension(tree);
412 extension = isl_union_map_universe(extension);
413 filter = isl_union_map_range(extension);
414 if (data_outer.initialized)
415 filter = isl_union_set_union(filter, data_outer.filter);
416 if (data->initialized)
417 filter = isl_union_set_intersect(filter, data->filter);
419 data->filter = filter;
421 isl_schedule_tree_free(tree);
423 return 0;
426 /* Update "data" based on the tree node "tree" in case "data" has
427 * not been initialized yet.
429 * Return 0 on success and -1 on error.
431 * If "tree" is a filter, then we set data->filter to this filter
432 * (or its universe).
433 * If "tree" is a domain, then this means we have reached the root
434 * of the schedule tree without being able to extract any information.
435 * We therefore initialize data->filter to the universe of the domain,
436 * or the domain itself if data->universe_domain is not set.
437 * If "tree" is a band with at least one member, then we set data->filter
438 * to the universe of the schedule domain and replace the zero-dimensional
439 * data->prefix by the band schedule (if data->collect_prefix is set).
441 static int collect_filter_prefix_init(__isl_keep isl_schedule_tree *tree,
442 struct isl_schedule_node_get_filter_prefix_data *data)
444 enum isl_schedule_node_type type;
445 isl_multi_union_pw_aff *mupa;
446 isl_union_set *filter;
448 type = isl_schedule_tree_get_type(tree);
449 switch (type) {
450 case isl_schedule_node_error:
451 return -1;
452 case isl_schedule_node_expansion:
453 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
454 "should be handled by caller", return -1);
455 case isl_schedule_node_extension:
456 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
457 "cannot handle extension nodes", return -1);
458 case isl_schedule_node_context:
459 case isl_schedule_node_leaf:
460 case isl_schedule_node_guard:
461 case isl_schedule_node_mark:
462 case isl_schedule_node_sequence:
463 case isl_schedule_node_set:
464 return 0;
465 case isl_schedule_node_domain:
466 filter = isl_schedule_tree_domain_get_domain(tree);
467 if (data->universe_domain)
468 filter = isl_union_set_universe(filter);
469 data->filter = filter;
470 break;
471 case isl_schedule_node_band:
472 if (isl_schedule_tree_band_n_member(tree) == 0)
473 return 0;
474 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
475 if (data->collect_prefix) {
476 isl_multi_union_pw_aff_free(data->prefix);
477 mupa = isl_multi_union_pw_aff_reset_tuple_id(mupa,
478 isl_dim_set);
479 data->prefix = isl_multi_union_pw_aff_copy(mupa);
481 filter = isl_multi_union_pw_aff_domain(mupa);
482 filter = isl_union_set_universe(filter);
483 data->filter = filter;
484 break;
485 case isl_schedule_node_filter:
486 filter = isl_schedule_tree_filter_get_filter(tree);
487 if (data->universe_filter)
488 filter = isl_union_set_universe(filter);
489 data->filter = filter;
490 break;
493 if ((data->collect_prefix && !data->prefix) || !data->filter)
494 return -1;
496 data->initialized = 1;
498 return 0;
501 /* Update "data" based on the tree node "tree" in case "data" has
502 * already been initialized.
504 * Return 0 on success and -1 on error.
506 * If "tree" is a domain and data->universe_domain is not set, then
507 * intersect data->filter with the domain.
508 * If "tree" is a filter, then we intersect data->filter with this filter
509 * (or its universe).
510 * If "tree" is a band with at least one member and data->collect_prefix
511 * is set, then we extend data->prefix with the band schedule.
512 * If "tree" is an extension, then we make sure that we are not collecting
513 * information on any extended domain elements.
515 static int collect_filter_prefix_update(__isl_keep isl_schedule_tree *tree,
516 struct isl_schedule_node_get_filter_prefix_data *data)
518 enum isl_schedule_node_type type;
519 isl_multi_union_pw_aff *mupa;
520 isl_union_set *filter;
521 isl_union_map *extension;
522 int empty;
524 type = isl_schedule_tree_get_type(tree);
525 switch (type) {
526 case isl_schedule_node_error:
527 return -1;
528 case isl_schedule_node_expansion:
529 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
530 "should be handled by caller", return -1);
531 case isl_schedule_node_extension:
532 extension = isl_schedule_tree_extension_get_extension(tree);
533 extension = isl_union_map_intersect_range(extension,
534 isl_union_set_copy(data->filter));
535 empty = isl_union_map_is_empty(extension);
536 isl_union_map_free(extension);
537 if (empty < 0)
538 return -1;
539 if (empty)
540 break;
541 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
542 "cannot handle extension nodes", return -1);
543 case isl_schedule_node_context:
544 case isl_schedule_node_leaf:
545 case isl_schedule_node_guard:
546 case isl_schedule_node_mark:
547 case isl_schedule_node_sequence:
548 case isl_schedule_node_set:
549 break;
550 case isl_schedule_node_domain:
551 if (data->universe_domain)
552 break;
553 filter = isl_schedule_tree_domain_get_domain(tree);
554 data->filter = isl_union_set_intersect(data->filter, filter);
555 break;
556 case isl_schedule_node_band:
557 if (isl_schedule_tree_band_n_member(tree) == 0)
558 break;
559 if (!data->collect_prefix)
560 break;
561 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
562 data->prefix = isl_multi_union_pw_aff_flat_range_product(mupa,
563 data->prefix);
564 if (!data->prefix)
565 return -1;
566 break;
567 case isl_schedule_node_filter:
568 filter = isl_schedule_tree_filter_get_filter(tree);
569 if (data->universe_filter)
570 filter = isl_union_set_universe(filter);
571 data->filter = isl_union_set_intersect(data->filter, filter);
572 if (!data->filter)
573 return -1;
574 break;
577 return 0;
580 /* Collect filter and/or prefix information from the first "n"
581 * elements in "list" (which represent the ancestors of a node).
582 * Store the results in "data".
584 * Extension nodes are only supported if they do not affect the outcome,
585 * i.e., if we are collecting information on non-extended domain elements,
586 * or if we are collecting the universe domain (without prefix).
588 * Return 0 on success and -1 on error.
590 * We traverse the list from innermost ancestor (last element)
591 * to outermost ancestor (first element), calling collect_filter_prefix_init
592 * on each node as long as we have not been able to extract any information
593 * yet and collect_filter_prefix_update afterwards.
594 * If we come across an expansion node, then we interrupt the traversal
595 * and call collect_filter_prefix_expansion to restart the traversal
596 * over the remaining ancestors and to combine the results with those
597 * that have already been collected.
598 * If we come across an extension node and we are only computing
599 * the universe domain, then we interrupt the traversal and call
600 * collect_universe_domain_extension to restart the traversal
601 * over the remaining ancestors and to combine the results with those
602 * that have already been collected.
603 * On successful return, data->initialized will be set since the outermost
604 * ancestor is a domain node, which always results in an initialization.
606 static int collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
607 int n, struct isl_schedule_node_get_filter_prefix_data *data)
609 int i;
611 if (!list)
612 return -1;
614 for (i = n - 1; i >= 0; --i) {
615 isl_schedule_tree *tree;
616 enum isl_schedule_node_type type;
617 int r;
619 tree = isl_schedule_tree_list_get_schedule_tree(list, i);
620 if (!tree)
621 return -1;
622 type = isl_schedule_tree_get_type(tree);
623 if (type == isl_schedule_node_expansion)
624 return collect_filter_prefix_expansion(tree, list, i,
625 data);
626 if (type == isl_schedule_node_extension &&
627 data->universe_domain && !data->collect_prefix)
628 return collect_universe_domain_extension(tree, list, i,
629 data);
630 if (!data->initialized)
631 r = collect_filter_prefix_init(tree, data);
632 else
633 r = collect_filter_prefix_update(tree, data);
634 isl_schedule_tree_free(tree);
635 if (r < 0)
636 return -1;
639 return 0;
642 /* Return the concatenation of the partial schedules of all outer band
643 * nodes of "node" interesected with all outer filters
644 * as an isl_multi_union_pw_aff.
645 * None of the ancestors of "node" may be an extension node, unless
646 * there is also a filter ancestor that filters out all the extended
647 * domain elements.
649 * If "node" is pointing at the root of the schedule tree, then
650 * there are no domain elements reaching the current node, so
651 * we return an empty result.
653 * We collect all the filters and partial schedules in collect_filter_prefix
654 * and intersect the domain of the combined schedule with the combined filter.
656 __isl_give isl_multi_union_pw_aff *
657 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(
658 __isl_keep isl_schedule_node *node)
660 int n;
661 isl_space *space;
662 struct isl_schedule_node_get_filter_prefix_data data;
664 if (!node)
665 return NULL;
667 space = isl_schedule_get_space(node->schedule);
668 space = isl_space_set_from_params(space);
669 if (node->tree == node->schedule->root)
670 return isl_multi_union_pw_aff_zero(space);
672 data.initialized = 0;
673 data.universe_domain = 1;
674 data.universe_filter = 0;
675 data.collect_prefix = 1;
676 data.filter = NULL;
677 data.prefix = isl_multi_union_pw_aff_zero(space);
679 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
680 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
681 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
683 data.prefix = isl_multi_union_pw_aff_intersect_domain(data.prefix,
684 data.filter);
686 return data.prefix;
689 /* Return the concatenation of the partial schedules of all outer band
690 * nodes of "node" interesected with all outer filters
691 * as an isl_union_pw_multi_aff.
692 * None of the ancestors of "node" may be an extension node, unless
693 * there is also a filter ancestor that filters out all the extended
694 * domain elements.
696 * If "node" is pointing at the root of the schedule tree, then
697 * there are no domain elements reaching the current node, so
698 * we return an empty result.
700 * We collect all the filters and partial schedules in collect_filter_prefix.
701 * The partial schedules are collected as an isl_multi_union_pw_aff.
702 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
703 * contain any domain information, so we construct the isl_union_pw_multi_aff
704 * result as a zero-dimensional function on the collected filter.
705 * Otherwise, we convert the isl_multi_union_pw_aff to
706 * an isl_multi_union_pw_aff and intersect the domain with the filter.
708 __isl_give isl_union_pw_multi_aff *
709 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
710 __isl_keep isl_schedule_node *node)
712 int n;
713 isl_space *space;
714 isl_union_pw_multi_aff *prefix;
715 struct isl_schedule_node_get_filter_prefix_data data;
717 if (!node)
718 return NULL;
720 space = isl_schedule_get_space(node->schedule);
721 if (node->tree == node->schedule->root)
722 return isl_union_pw_multi_aff_empty(space);
724 space = isl_space_set_from_params(space);
725 data.initialized = 0;
726 data.universe_domain = 1;
727 data.universe_filter = 0;
728 data.collect_prefix = 1;
729 data.filter = NULL;
730 data.prefix = isl_multi_union_pw_aff_zero(space);
732 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
733 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
734 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
736 if (data.prefix &&
737 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
738 isl_multi_union_pw_aff_free(data.prefix);
739 prefix = isl_union_pw_multi_aff_from_domain(data.filter);
740 } else {
741 prefix =
742 isl_union_pw_multi_aff_from_multi_union_pw_aff(data.prefix);
743 prefix = isl_union_pw_multi_aff_intersect_domain(prefix,
744 data.filter);
747 return prefix;
750 /* Return the concatenation of the partial schedules of all outer band
751 * nodes of "node" interesected with all outer filters
752 * as an isl_union_map.
754 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_union_map(
755 __isl_keep isl_schedule_node *node)
757 isl_union_pw_multi_aff *upma;
759 upma = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
760 return isl_union_map_from_union_pw_multi_aff(upma);
763 /* Return the concatenation of the partial schedules of all outer band
764 * nodes of "node" intersected with all outer domain constraints.
765 * None of the ancestors of "node" may be an extension node, unless
766 * there is also a filter ancestor that filters out all the extended
767 * domain elements.
769 * Essentially, this function intersects the domain of the output
770 * of isl_schedule_node_get_prefix_schedule_union_map with the output
771 * of isl_schedule_node_get_domain, except that it only traverses
772 * the ancestors of "node" once.
774 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_relation(
775 __isl_keep isl_schedule_node *node)
777 int n;
778 isl_space *space;
779 isl_union_map *prefix;
780 struct isl_schedule_node_get_filter_prefix_data data;
782 if (!node)
783 return NULL;
785 space = isl_schedule_get_space(node->schedule);
786 if (node->tree == node->schedule->root)
787 return isl_union_map_empty(space);
789 space = isl_space_set_from_params(space);
790 data.initialized = 0;
791 data.universe_domain = 0;
792 data.universe_filter = 0;
793 data.collect_prefix = 1;
794 data.filter = NULL;
795 data.prefix = isl_multi_union_pw_aff_zero(space);
797 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
798 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
799 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
801 if (data.prefix &&
802 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
803 isl_multi_union_pw_aff_free(data.prefix);
804 prefix = isl_union_map_from_domain(data.filter);
805 } else {
806 prefix = isl_union_map_from_multi_union_pw_aff(data.prefix);
807 prefix = isl_union_map_intersect_domain(prefix, data.filter);
810 return prefix;
813 /* Return the domain elements that reach "node".
815 * If "node" is pointing at the root of the schedule tree, then
816 * there are no domain elements reaching the current node, so
817 * we return an empty result.
818 * None of the ancestors of "node" may be an extension node, unless
819 * there is also a filter ancestor that filters out all the extended
820 * domain elements.
822 * Otherwise, we collect all filters reaching the node,
823 * intersected with the root domain in collect_filter_prefix.
825 __isl_give isl_union_set *isl_schedule_node_get_domain(
826 __isl_keep isl_schedule_node *node)
828 int n;
829 struct isl_schedule_node_get_filter_prefix_data data;
831 if (!node)
832 return NULL;
834 if (node->tree == node->schedule->root) {
835 isl_space *space;
837 space = isl_schedule_get_space(node->schedule);
838 return isl_union_set_empty(space);
841 data.initialized = 0;
842 data.universe_domain = 0;
843 data.universe_filter = 0;
844 data.collect_prefix = 0;
845 data.filter = NULL;
846 data.prefix = NULL;
848 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
849 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
850 data.filter = isl_union_set_free(data.filter);
852 return data.filter;
855 /* Return the union of universe sets of the domain elements that reach "node".
857 * If "node" is pointing at the root of the schedule tree, then
858 * there are no domain elements reaching the current node, so
859 * we return an empty result.
861 * Otherwise, we collect the universes of all filters reaching the node
862 * in collect_filter_prefix.
864 __isl_give isl_union_set *isl_schedule_node_get_universe_domain(
865 __isl_keep isl_schedule_node *node)
867 int n;
868 struct isl_schedule_node_get_filter_prefix_data data;
870 if (!node)
871 return NULL;
873 if (node->tree == node->schedule->root) {
874 isl_space *space;
876 space = isl_schedule_get_space(node->schedule);
877 return isl_union_set_empty(space);
880 data.initialized = 0;
881 data.universe_domain = 1;
882 data.universe_filter = 1;
883 data.collect_prefix = 0;
884 data.filter = NULL;
885 data.prefix = NULL;
887 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
888 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
889 data.filter = isl_union_set_free(data.filter);
891 return data.filter;
894 /* Return the subtree schedule of "node".
896 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
897 * trees that do not contain any schedule information, we first
898 * move down to the first relevant descendant and handle leaves ourselves.
900 * If the subtree rooted at "node" contains any expansion nodes, then
901 * the returned subtree schedule is formulated in terms of the expanded
902 * domains.
903 * The subtree is not allowed to contain any extension nodes.
905 __isl_give isl_union_map *isl_schedule_node_get_subtree_schedule_union_map(
906 __isl_keep isl_schedule_node *node)
908 isl_schedule_tree *tree, *leaf;
909 isl_union_map *umap;
911 tree = isl_schedule_node_get_tree(node);
912 leaf = isl_schedule_node_peek_leaf(node);
913 tree = isl_schedule_tree_first_schedule_descendant(tree, leaf);
914 if (!tree)
915 return NULL;
916 if (tree == leaf) {
917 isl_union_set *domain;
918 domain = isl_schedule_node_get_universe_domain(node);
919 isl_schedule_tree_free(tree);
920 return isl_union_map_from_domain(domain);
923 umap = isl_schedule_tree_get_subtree_schedule_union_map(tree);
924 isl_schedule_tree_free(tree);
925 return umap;
928 /* Return the number of ancestors of "node" in its schedule tree.
930 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node *node)
932 if (!node)
933 return -1;
934 return isl_schedule_tree_list_n_schedule_tree(node->ancestors);
937 /* Does "node" have a parent?
939 * That is, does it point to any node of the schedule other than the root?
941 isl_bool isl_schedule_node_has_parent(__isl_keep isl_schedule_node *node)
943 if (!node)
944 return isl_bool_error;
945 if (!node->ancestors)
946 return isl_bool_error;
948 return isl_schedule_tree_list_n_schedule_tree(node->ancestors) != 0;
951 /* Return the position of "node" among the children of its parent.
953 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node *node)
955 int n;
956 int has_parent;
958 if (!node)
959 return -1;
960 has_parent = isl_schedule_node_has_parent(node);
961 if (has_parent < 0)
962 return -1;
963 if (!has_parent)
964 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
965 "node has no parent", return -1);
967 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
968 return node->child_pos[n - 1];
971 /* Does the parent (if any) of "node" have any children with a smaller child
972 * position than this one?
974 isl_bool isl_schedule_node_has_previous_sibling(
975 __isl_keep isl_schedule_node *node)
977 int n;
978 isl_bool has_parent;
980 if (!node)
981 return isl_bool_error;
982 has_parent = isl_schedule_node_has_parent(node);
983 if (has_parent < 0 || !has_parent)
984 return has_parent;
986 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
988 return node->child_pos[n - 1] > 0;
991 /* Does the parent (if any) of "node" have any children with a greater child
992 * position than this one?
994 isl_bool isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node *node)
996 int n, n_child;
997 isl_bool has_parent;
998 isl_schedule_tree *tree;
1000 if (!node)
1001 return isl_bool_error;
1002 has_parent = isl_schedule_node_has_parent(node);
1003 if (has_parent < 0 || !has_parent)
1004 return has_parent;
1006 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1007 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n - 1);
1008 if (!tree)
1009 return isl_bool_error;
1010 n_child = isl_schedule_tree_list_n_schedule_tree(tree->children);
1011 isl_schedule_tree_free(tree);
1013 return node->child_pos[n - 1] + 1 < n_child;
1016 /* Does "node" have any children?
1018 * Any node other than the leaf nodes is considered to have at least
1019 * one child, even if the corresponding isl_schedule_tree does not
1020 * have any children.
1022 isl_bool isl_schedule_node_has_children(__isl_keep isl_schedule_node *node)
1024 if (!node)
1025 return isl_bool_error;
1026 return !isl_schedule_tree_is_leaf(node->tree);
1029 /* Return the number of children of "node"?
1031 * Any node other than the leaf nodes is considered to have at least
1032 * one child, even if the corresponding isl_schedule_tree does not
1033 * have any children. That is, the number of children of "node" is
1034 * only zero if its tree is the explicit empty tree. Otherwise,
1035 * if the isl_schedule_tree has any children, then it is equal
1036 * to the number of children of "node". If it has zero children,
1037 * then "node" still has a leaf node as child.
1039 int isl_schedule_node_n_children(__isl_keep isl_schedule_node *node)
1041 int n;
1043 if (!node)
1044 return -1;
1046 if (isl_schedule_tree_is_leaf(node->tree))
1047 return 0;
1049 n = isl_schedule_tree_n_children(node->tree);
1050 if (n == 0)
1051 return 1;
1053 return n;
1056 /* Move the "node" pointer to the ancestor of the given generation
1057 * of the node it currently points to, where generation 0 is the node
1058 * itself and generation 1 is its parent.
1060 __isl_give isl_schedule_node *isl_schedule_node_ancestor(
1061 __isl_take isl_schedule_node *node, int generation)
1063 int n;
1064 isl_schedule_tree *tree;
1066 if (!node)
1067 return NULL;
1068 if (generation == 0)
1069 return node;
1070 n = isl_schedule_node_get_tree_depth(node);
1071 if (n < 0)
1072 return isl_schedule_node_free(node);
1073 if (generation < 0 || generation > n)
1074 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1075 "generation out of bounds",
1076 return isl_schedule_node_free(node));
1077 node = isl_schedule_node_cow(node);
1078 if (!node)
1079 return NULL;
1081 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1082 n - generation);
1083 isl_schedule_tree_free(node->tree);
1084 node->tree = tree;
1085 node->ancestors = isl_schedule_tree_list_drop(node->ancestors,
1086 n - generation, generation);
1087 if (!node->ancestors || !node->tree)
1088 return isl_schedule_node_free(node);
1090 return node;
1093 /* Move the "node" pointer to the parent of the node it currently points to.
1095 __isl_give isl_schedule_node *isl_schedule_node_parent(
1096 __isl_take isl_schedule_node *node)
1098 if (!node)
1099 return NULL;
1100 if (!isl_schedule_node_has_parent(node))
1101 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1102 "node has no parent",
1103 return isl_schedule_node_free(node));
1104 return isl_schedule_node_ancestor(node, 1);
1107 /* Move the "node" pointer to the root of its schedule tree.
1109 __isl_give isl_schedule_node *isl_schedule_node_root(
1110 __isl_take isl_schedule_node *node)
1112 int n;
1114 if (!node)
1115 return NULL;
1116 n = isl_schedule_node_get_tree_depth(node);
1117 if (n < 0)
1118 return isl_schedule_node_free(node);
1119 return isl_schedule_node_ancestor(node, n);
1122 /* Move the "node" pointer to the child at position "pos" of the node
1123 * it currently points to.
1125 __isl_give isl_schedule_node *isl_schedule_node_child(
1126 __isl_take isl_schedule_node *node, int pos)
1128 int n;
1129 isl_ctx *ctx;
1130 isl_schedule_tree *tree;
1131 int *child_pos;
1133 node = isl_schedule_node_cow(node);
1134 if (!node)
1135 return NULL;
1136 if (!isl_schedule_node_has_children(node))
1137 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1138 "node has no children",
1139 return isl_schedule_node_free(node));
1141 ctx = isl_schedule_node_get_ctx(node);
1142 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1143 child_pos = isl_realloc_array(ctx, node->child_pos, int, n + 1);
1144 if (!child_pos)
1145 return isl_schedule_node_free(node);
1146 node->child_pos = child_pos;
1147 node->child_pos[n] = pos;
1149 node->ancestors = isl_schedule_tree_list_add(node->ancestors,
1150 isl_schedule_tree_copy(node->tree));
1151 tree = node->tree;
1152 if (isl_schedule_tree_has_children(tree))
1153 tree = isl_schedule_tree_get_child(tree, pos);
1154 else
1155 tree = isl_schedule_node_get_leaf(node);
1156 isl_schedule_tree_free(node->tree);
1157 node->tree = tree;
1159 if (!node->tree || !node->ancestors)
1160 return isl_schedule_node_free(node);
1162 return node;
1165 /* Move the "node" pointer to the first child of the node
1166 * it currently points to.
1168 __isl_give isl_schedule_node *isl_schedule_node_first_child(
1169 __isl_take isl_schedule_node *node)
1171 return isl_schedule_node_child(node, 0);
1174 /* Move the "node" pointer to the child of this node's parent in
1175 * the previous child position.
1177 __isl_give isl_schedule_node *isl_schedule_node_previous_sibling(
1178 __isl_take isl_schedule_node *node)
1180 int n;
1181 isl_schedule_tree *parent, *tree;
1183 node = isl_schedule_node_cow(node);
1184 if (!node)
1185 return NULL;
1186 if (!isl_schedule_node_has_previous_sibling(node))
1187 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1188 "node has no previous sibling",
1189 return isl_schedule_node_free(node));
1191 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1192 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1193 n - 1);
1194 if (!parent)
1195 return isl_schedule_node_free(node);
1196 node->child_pos[n - 1]--;
1197 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
1198 node->child_pos[n - 1]);
1199 isl_schedule_tree_free(parent);
1200 if (!tree)
1201 return isl_schedule_node_free(node);
1202 isl_schedule_tree_free(node->tree);
1203 node->tree = tree;
1205 return node;
1208 /* Move the "node" pointer to the child of this node's parent in
1209 * the next child position.
1211 __isl_give isl_schedule_node *isl_schedule_node_next_sibling(
1212 __isl_take isl_schedule_node *node)
1214 int n;
1215 isl_schedule_tree *parent, *tree;
1217 node = isl_schedule_node_cow(node);
1218 if (!node)
1219 return NULL;
1220 if (!isl_schedule_node_has_next_sibling(node))
1221 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1222 "node has no next sibling",
1223 return isl_schedule_node_free(node));
1225 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1226 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1227 n - 1);
1228 if (!parent)
1229 return isl_schedule_node_free(node);
1230 node->child_pos[n - 1]++;
1231 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
1232 node->child_pos[n - 1]);
1233 isl_schedule_tree_free(parent);
1234 if (!tree)
1235 return isl_schedule_node_free(node);
1236 isl_schedule_tree_free(node->tree);
1237 node->tree = tree;
1239 return node;
1242 /* Return a copy to the child at position "pos" of "node".
1244 __isl_give isl_schedule_node *isl_schedule_node_get_child(
1245 __isl_keep isl_schedule_node *node, int pos)
1247 return isl_schedule_node_child(isl_schedule_node_copy(node), pos);
1250 /* Traverse the descendant of "node" in depth-first order, including
1251 * "node" itself. Call "enter" whenever a node is entered and "leave"
1252 * whenever a node is left. The callback "enter" is responsible
1253 * for moving to the deepest initial subtree of its argument that
1254 * should be traversed.
1256 static __isl_give isl_schedule_node *traverse(
1257 __isl_take isl_schedule_node *node,
1258 __isl_give isl_schedule_node *(*enter)(
1259 __isl_take isl_schedule_node *node, void *user),
1260 __isl_give isl_schedule_node *(*leave)(
1261 __isl_take isl_schedule_node *node, void *user),
1262 void *user)
1264 int depth;
1266 if (!node)
1267 return NULL;
1269 depth = isl_schedule_node_get_tree_depth(node);
1270 do {
1271 node = enter(node, user);
1272 node = leave(node, user);
1273 while (node && isl_schedule_node_get_tree_depth(node) > depth &&
1274 !isl_schedule_node_has_next_sibling(node)) {
1275 node = isl_schedule_node_parent(node);
1276 node = leave(node, user);
1278 if (node && isl_schedule_node_get_tree_depth(node) > depth)
1279 node = isl_schedule_node_next_sibling(node);
1280 } while (node && isl_schedule_node_get_tree_depth(node) > depth);
1282 return node;
1285 /* Internal data structure for isl_schedule_node_foreach_descendant_top_down.
1287 * "fn" is the user-specified callback function.
1288 * "user" is the user-specified argument for the callback.
1290 struct isl_schedule_node_preorder_data {
1291 isl_bool (*fn)(__isl_keep isl_schedule_node *node, void *user);
1292 void *user;
1295 /* Callback for "traverse" to enter a node and to move
1296 * to the deepest initial subtree that should be traversed
1297 * for use in a preorder visit.
1299 * If the user callback returns a negative value, then we abort
1300 * the traversal. If this callback returns zero, then we skip
1301 * the subtree rooted at the current node. Otherwise, we move
1302 * down to the first child and repeat the process until a leaf
1303 * is reached.
1305 static __isl_give isl_schedule_node *preorder_enter(
1306 __isl_take isl_schedule_node *node, void *user)
1308 struct isl_schedule_node_preorder_data *data = user;
1310 if (!node)
1311 return NULL;
1313 do {
1314 isl_bool r;
1316 r = data->fn(node, data->user);
1317 if (r < 0)
1318 return isl_schedule_node_free(node);
1319 if (r == isl_bool_false)
1320 return node;
1321 } while (isl_schedule_node_has_children(node) &&
1322 (node = isl_schedule_node_first_child(node)) != NULL);
1324 return node;
1327 /* Callback for "traverse" to leave a node
1328 * for use in a preorder visit.
1329 * Since we already visited the node when we entered it,
1330 * we do not need to do anything here.
1332 static __isl_give isl_schedule_node *preorder_leave(
1333 __isl_take isl_schedule_node *node, void *user)
1335 return node;
1338 /* Traverse the descendants of "node" (including the node itself)
1339 * in depth first preorder.
1341 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1342 * If "fn" returns 0 on any of the nodes, then the subtree rooted
1343 * at that node is skipped.
1345 * Return 0 on success and -1 on failure.
1347 isl_stat isl_schedule_node_foreach_descendant_top_down(
1348 __isl_keep isl_schedule_node *node,
1349 isl_bool (*fn)(__isl_keep isl_schedule_node *node, void *user),
1350 void *user)
1352 struct isl_schedule_node_preorder_data data = { fn, user };
1354 node = isl_schedule_node_copy(node);
1355 node = traverse(node, &preorder_enter, &preorder_leave, &data);
1356 isl_schedule_node_free(node);
1358 return node ? isl_stat_ok : isl_stat_error;
1361 /* Internal data structure for isl_schedule_node_map_descendant_bottom_up.
1363 * "fn" is the user-specified callback function.
1364 * "user" is the user-specified argument for the callback.
1366 struct isl_schedule_node_postorder_data {
1367 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1368 void *user);
1369 void *user;
1372 /* Callback for "traverse" to enter a node and to move
1373 * to the deepest initial subtree that should be traversed
1374 * for use in a postorder visit.
1376 * Since we are performing a postorder visit, we only need
1377 * to move to the deepest initial leaf here.
1379 static __isl_give isl_schedule_node *postorder_enter(
1380 __isl_take isl_schedule_node *node, void *user)
1382 while (node && isl_schedule_node_has_children(node))
1383 node = isl_schedule_node_first_child(node);
1385 return node;
1388 /* Callback for "traverse" to leave a node
1389 * for use in a postorder visit.
1391 * Since we are performing a postorder visit, we need
1392 * to call the user callback here.
1394 static __isl_give isl_schedule_node *postorder_leave(
1395 __isl_take isl_schedule_node *node, void *user)
1397 struct isl_schedule_node_postorder_data *data = user;
1399 return data->fn(node, data->user);
1402 /* Traverse the descendants of "node" (including the node itself)
1403 * in depth first postorder, allowing the user to modify the visited node.
1404 * The traversal continues from the node returned by the callback function.
1405 * It is the responsibility of the user to ensure that this does not
1406 * lead to an infinite loop. It is safest to always return a pointer
1407 * to the same position (same ancestors and child positions) as the input node.
1409 __isl_give isl_schedule_node *isl_schedule_node_map_descendant_bottom_up(
1410 __isl_take isl_schedule_node *node,
1411 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1412 void *user), void *user)
1414 struct isl_schedule_node_postorder_data data = { fn, user };
1416 return traverse(node, &postorder_enter, &postorder_leave, &data);
1419 /* Traverse the ancestors of "node" from the root down to and including
1420 * the parent of "node", calling "fn" on each of them.
1422 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1424 * Return 0 on success and -1 on failure.
1426 isl_stat isl_schedule_node_foreach_ancestor_top_down(
1427 __isl_keep isl_schedule_node *node,
1428 isl_stat (*fn)(__isl_keep isl_schedule_node *node, void *user),
1429 void *user)
1431 int i, n;
1433 if (!node)
1434 return isl_stat_error;
1436 n = isl_schedule_node_get_tree_depth(node);
1437 for (i = 0; i < n; ++i) {
1438 isl_schedule_node *ancestor;
1439 isl_stat r;
1441 ancestor = isl_schedule_node_copy(node);
1442 ancestor = isl_schedule_node_ancestor(ancestor, n - i);
1443 r = fn(ancestor, user);
1444 isl_schedule_node_free(ancestor);
1445 if (r < 0)
1446 return isl_stat_error;
1449 return isl_stat_ok;
1452 /* Is any node in the subtree rooted at "node" anchored?
1453 * That is, do any of these nodes reference the outer band nodes?
1455 isl_bool isl_schedule_node_is_subtree_anchored(
1456 __isl_keep isl_schedule_node *node)
1458 if (!node)
1459 return isl_bool_error;
1460 return isl_schedule_tree_is_subtree_anchored(node->tree);
1463 /* Return the number of members in the given band node.
1465 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node *node)
1467 return node ? isl_schedule_tree_band_n_member(node->tree) : 0;
1470 /* Is the band member at position "pos" of the band node "node"
1471 * marked coincident?
1473 isl_bool isl_schedule_node_band_member_get_coincident(
1474 __isl_keep isl_schedule_node *node, int pos)
1476 if (!node)
1477 return isl_bool_error;
1478 return isl_schedule_tree_band_member_get_coincident(node->tree, pos);
1481 /* Mark the band member at position "pos" the band node "node"
1482 * as being coincident or not according to "coincident".
1484 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_coincident(
1485 __isl_take isl_schedule_node *node, int pos, int coincident)
1487 int c;
1488 isl_schedule_tree *tree;
1490 if (!node)
1491 return NULL;
1492 c = isl_schedule_node_band_member_get_coincident(node, pos);
1493 if (c == coincident)
1494 return node;
1496 tree = isl_schedule_tree_copy(node->tree);
1497 tree = isl_schedule_tree_band_member_set_coincident(tree, pos,
1498 coincident);
1499 node = isl_schedule_node_graft_tree(node, tree);
1501 return node;
1504 /* Is the band node "node" marked permutable?
1506 isl_bool isl_schedule_node_band_get_permutable(
1507 __isl_keep isl_schedule_node *node)
1509 if (!node)
1510 return isl_bool_error;
1512 return isl_schedule_tree_band_get_permutable(node->tree);
1515 /* Mark the band node "node" permutable or not according to "permutable"?
1517 __isl_give isl_schedule_node *isl_schedule_node_band_set_permutable(
1518 __isl_take isl_schedule_node *node, int permutable)
1520 isl_schedule_tree *tree;
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 /* Reduce the partial schedule of the band node "node"
1762 * modulo the factors in "mv".
1764 __isl_give isl_schedule_node *isl_schedule_node_band_mod(
1765 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1767 isl_schedule_tree *tree;
1768 isl_bool anchored;
1770 if (!node || !mv)
1771 goto error;
1772 if (check_space_multi_val(node, mv) < 0)
1773 goto error;
1774 anchored = isl_schedule_node_is_subtree_anchored(node);
1775 if (anchored < 0)
1776 goto error;
1777 if (anchored)
1778 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1779 "cannot perform mod on band node with anchored subtree",
1780 goto error);
1782 tree = isl_schedule_node_get_tree(node);
1783 tree = isl_schedule_tree_band_mod(tree, mv);
1784 return isl_schedule_node_graft_tree(node, tree);
1785 error:
1786 isl_multi_val_free(mv);
1787 isl_schedule_node_free(node);
1788 return NULL;
1791 /* Make sure that that spaces of "node" and "mupa" are the same.
1792 * Return isl_stat_error on error, reporting the error to the user.
1794 static isl_stat check_space_multi_union_pw_aff(
1795 __isl_keep isl_schedule_node *node,
1796 __isl_keep isl_multi_union_pw_aff *mupa)
1798 isl_space *node_space, *mupa_space;
1799 isl_bool equal;
1801 node_space = isl_schedule_node_band_get_space(node);
1802 mupa_space = isl_multi_union_pw_aff_get_space(mupa);
1803 equal = isl_space_tuple_is_equal(node_space, isl_dim_set,
1804 mupa_space, isl_dim_set);
1805 isl_space_free(mupa_space);
1806 isl_space_free(node_space);
1807 if (equal < 0)
1808 return isl_stat_error;
1809 if (!equal)
1810 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1811 "spaces don't match", return isl_stat_error);
1813 return isl_stat_ok;
1816 /* Shift the partial schedule of the band node "node" by "shift".
1818 __isl_give isl_schedule_node *isl_schedule_node_band_shift(
1819 __isl_take isl_schedule_node *node,
1820 __isl_take isl_multi_union_pw_aff *shift)
1822 isl_schedule_tree *tree;
1823 int anchored;
1825 if (!node || !shift)
1826 goto error;
1827 if (check_space_multi_union_pw_aff(node, shift) < 0)
1828 goto error;
1829 anchored = isl_schedule_node_is_subtree_anchored(node);
1830 if (anchored < 0)
1831 goto error;
1832 if (anchored)
1833 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1834 "cannot shift band node with anchored subtree",
1835 goto error);
1837 tree = isl_schedule_node_get_tree(node);
1838 tree = isl_schedule_tree_band_shift(tree, shift);
1839 return isl_schedule_node_graft_tree(node, tree);
1840 error:
1841 isl_multi_union_pw_aff_free(shift);
1842 isl_schedule_node_free(node);
1843 return NULL;
1846 /* Tile "node" with tile sizes "sizes".
1848 * The current node is replaced by two nested nodes corresponding
1849 * to the tile dimensions and the point dimensions.
1851 * Return a pointer to the outer (tile) node.
1853 * If any of the descendants of "node" depend on the set of outer band nodes,
1854 * then we refuse to tile the node.
1856 * If the scale tile loops option is set, then the tile loops
1857 * are scaled by the tile sizes. If the shift point loops option is set,
1858 * then the point loops are shifted to start at zero.
1859 * In particular, these options affect the tile and point loop schedules
1860 * as follows
1862 * scale shift original tile point
1864 * 0 0 i floor(i/s) i
1865 * 1 0 i s * floor(i/s) i
1866 * 0 1 i floor(i/s) i - s * floor(i/s)
1867 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1869 __isl_give isl_schedule_node *isl_schedule_node_band_tile(
1870 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
1872 isl_schedule_tree *tree;
1873 int anchored;
1875 if (!node || !sizes)
1876 goto error;
1877 anchored = isl_schedule_node_is_subtree_anchored(node);
1878 if (anchored < 0)
1879 goto error;
1880 if (anchored)
1881 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1882 "cannot tile band node with anchored subtree",
1883 goto error);
1885 if (check_space_multi_val(node, sizes) < 0)
1886 goto error;
1888 tree = isl_schedule_node_get_tree(node);
1889 tree = isl_schedule_tree_band_tile(tree, sizes);
1890 return isl_schedule_node_graft_tree(node, tree);
1891 error:
1892 isl_multi_val_free(sizes);
1893 isl_schedule_node_free(node);
1894 return NULL;
1897 /* Move the band node "node" down to all the leaves in the subtree
1898 * rooted at "node".
1899 * Return a pointer to the node in the resulting tree that is in the same
1900 * position as the node pointed to by "node" in the original tree.
1902 * If the node only has a leaf child, then nothing needs to be done.
1903 * Otherwise, the child of the node is removed and the result is
1904 * appended to all the leaves in the subtree rooted at the original child.
1905 * Since the node is moved to the leaves, it needs to be expanded
1906 * according to the expansion, if any, defined by that subtree.
1907 * In the end, the original node is replaced by the result of
1908 * attaching copies of the expanded node to the leaves.
1910 * If any of the nodes in the subtree rooted at "node" depend on
1911 * the set of outer band nodes then we refuse to sink the band node.
1913 __isl_give isl_schedule_node *isl_schedule_node_band_sink(
1914 __isl_take isl_schedule_node *node)
1916 enum isl_schedule_node_type type;
1917 isl_schedule_tree *tree, *child;
1918 isl_union_pw_multi_aff *contraction;
1919 int anchored;
1921 if (!node)
1922 return NULL;
1924 type = isl_schedule_node_get_type(node);
1925 if (type != isl_schedule_node_band)
1926 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1927 "not a band node", isl_schedule_node_free(node));
1928 anchored = isl_schedule_node_is_subtree_anchored(node);
1929 if (anchored < 0)
1930 return isl_schedule_node_free(node);
1931 if (anchored)
1932 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1933 "cannot sink band node in anchored subtree",
1934 isl_schedule_node_free(node));
1935 if (isl_schedule_tree_n_children(node->tree) == 0)
1936 return node;
1938 contraction = isl_schedule_node_get_subtree_contraction(node);
1940 tree = isl_schedule_node_get_tree(node);
1941 child = isl_schedule_tree_get_child(tree, 0);
1942 tree = isl_schedule_tree_reset_children(tree);
1943 tree = isl_schedule_tree_pullback_union_pw_multi_aff(tree, contraction);
1944 tree = isl_schedule_tree_append_to_leaves(child, tree);
1946 return isl_schedule_node_graft_tree(node, tree);
1949 /* Split "node" into two nested band nodes, one with the first "pos"
1950 * dimensions and one with the remaining dimensions.
1951 * The schedules of the two band nodes live in anonymous spaces.
1953 __isl_give isl_schedule_node *isl_schedule_node_band_split(
1954 __isl_take isl_schedule_node *node, int pos)
1956 isl_schedule_tree *tree;
1958 tree = isl_schedule_node_get_tree(node);
1959 tree = isl_schedule_tree_band_split(tree, pos);
1960 return isl_schedule_node_graft_tree(node, tree);
1963 /* Return the context of the context node "node".
1965 __isl_give isl_set *isl_schedule_node_context_get_context(
1966 __isl_keep isl_schedule_node *node)
1968 if (!node)
1969 return NULL;
1971 return isl_schedule_tree_context_get_context(node->tree);
1974 /* Return the domain of the domain node "node".
1976 __isl_give isl_union_set *isl_schedule_node_domain_get_domain(
1977 __isl_keep isl_schedule_node *node)
1979 if (!node)
1980 return NULL;
1982 return isl_schedule_tree_domain_get_domain(node->tree);
1985 /* Return the expansion map of expansion node "node".
1987 __isl_give isl_union_map *isl_schedule_node_expansion_get_expansion(
1988 __isl_keep isl_schedule_node *node)
1990 if (!node)
1991 return NULL;
1993 return isl_schedule_tree_expansion_get_expansion(node->tree);
1996 /* Return the contraction of expansion node "node".
1998 __isl_give isl_union_pw_multi_aff *isl_schedule_node_expansion_get_contraction(
1999 __isl_keep isl_schedule_node *node)
2001 if (!node)
2002 return NULL;
2004 return isl_schedule_tree_expansion_get_contraction(node->tree);
2007 /* Replace the contraction and the expansion of the expansion node "node"
2008 * by "contraction" and "expansion".
2010 __isl_give isl_schedule_node *
2011 isl_schedule_node_expansion_set_contraction_and_expansion(
2012 __isl_take isl_schedule_node *node,
2013 __isl_take isl_union_pw_multi_aff *contraction,
2014 __isl_take isl_union_map *expansion)
2016 isl_schedule_tree *tree;
2018 if (!node || !contraction || !expansion)
2019 goto error;
2021 tree = isl_schedule_tree_copy(node->tree);
2022 tree = isl_schedule_tree_expansion_set_contraction_and_expansion(tree,
2023 contraction, expansion);
2024 return isl_schedule_node_graft_tree(node, tree);
2025 error:
2026 isl_schedule_node_free(node);
2027 isl_union_pw_multi_aff_free(contraction);
2028 isl_union_map_free(expansion);
2029 return NULL;
2032 /* Return the extension of the extension node "node".
2034 __isl_give isl_union_map *isl_schedule_node_extension_get_extension(
2035 __isl_keep isl_schedule_node *node)
2037 if (!node)
2038 return NULL;
2040 return isl_schedule_tree_extension_get_extension(node->tree);
2043 /* Replace the extension of extension node "node" by "extension".
2045 __isl_give isl_schedule_node *isl_schedule_node_extension_set_extension(
2046 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
2048 isl_schedule_tree *tree;
2050 if (!node || !extension)
2051 goto error;
2053 tree = isl_schedule_tree_copy(node->tree);
2054 tree = isl_schedule_tree_extension_set_extension(tree, extension);
2055 return isl_schedule_node_graft_tree(node, tree);
2056 error:
2057 isl_schedule_node_free(node);
2058 isl_union_map_free(extension);
2059 return NULL;
2062 /* Return the filter of the filter node "node".
2064 __isl_give isl_union_set *isl_schedule_node_filter_get_filter(
2065 __isl_keep isl_schedule_node *node)
2067 if (!node)
2068 return NULL;
2070 return isl_schedule_tree_filter_get_filter(node->tree);
2073 /* Replace the filter of filter node "node" by "filter".
2075 __isl_give isl_schedule_node *isl_schedule_node_filter_set_filter(
2076 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
2078 isl_schedule_tree *tree;
2080 if (!node || !filter)
2081 goto error;
2083 tree = isl_schedule_tree_copy(node->tree);
2084 tree = isl_schedule_tree_filter_set_filter(tree, filter);
2085 return isl_schedule_node_graft_tree(node, tree);
2086 error:
2087 isl_schedule_node_free(node);
2088 isl_union_set_free(filter);
2089 return NULL;
2092 /* Intersect the filter of filter node "node" with "filter".
2094 * If the filter of the node is already a subset of "filter",
2095 * then leave the node unchanged.
2097 __isl_give isl_schedule_node *isl_schedule_node_filter_intersect_filter(
2098 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
2100 isl_union_set *node_filter = NULL;
2101 isl_bool subset;
2103 if (!node || !filter)
2104 goto error;
2106 node_filter = isl_schedule_node_filter_get_filter(node);
2107 subset = isl_union_set_is_subset(node_filter, filter);
2108 if (subset < 0)
2109 goto error;
2110 if (subset) {
2111 isl_union_set_free(node_filter);
2112 isl_union_set_free(filter);
2113 return node;
2115 node_filter = isl_union_set_intersect(node_filter, filter);
2116 node = isl_schedule_node_filter_set_filter(node, node_filter);
2117 return node;
2118 error:
2119 isl_schedule_node_free(node);
2120 isl_union_set_free(node_filter);
2121 isl_union_set_free(filter);
2122 return NULL;
2125 /* Return the guard of the guard node "node".
2127 __isl_give isl_set *isl_schedule_node_guard_get_guard(
2128 __isl_keep isl_schedule_node *node)
2130 if (!node)
2131 return NULL;
2133 return isl_schedule_tree_guard_get_guard(node->tree);
2136 /* Return the mark identifier of the mark node "node".
2138 __isl_give isl_id *isl_schedule_node_mark_get_id(
2139 __isl_keep isl_schedule_node *node)
2141 if (!node)
2142 return NULL;
2144 return isl_schedule_tree_mark_get_id(node->tree);
2147 /* Replace the child at position "pos" of the sequence node "node"
2148 * by the children of sequence root node of "tree".
2150 __isl_give isl_schedule_node *isl_schedule_node_sequence_splice(
2151 __isl_take isl_schedule_node *node, int pos,
2152 __isl_take isl_schedule_tree *tree)
2154 isl_schedule_tree *node_tree;
2156 if (!node || !tree)
2157 goto error;
2158 if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
2159 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2160 "not a sequence node", goto error);
2161 if (isl_schedule_tree_get_type(tree) != isl_schedule_node_sequence)
2162 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2163 "not a sequence node", goto error);
2164 node_tree = isl_schedule_node_get_tree(node);
2165 node_tree = isl_schedule_tree_sequence_splice(node_tree, pos, tree);
2166 node = isl_schedule_node_graft_tree(node, node_tree);
2168 return node;
2169 error:
2170 isl_schedule_node_free(node);
2171 isl_schedule_tree_free(tree);
2172 return NULL;
2175 /* Given a sequence node "node", with a child at position "pos" that
2176 * is also a sequence node, attach the children of that node directly
2177 * as children of "node" at that position, replacing the original child.
2179 * The filters of these children are intersected with the filter
2180 * of the child at position "pos".
2182 __isl_give isl_schedule_node *isl_schedule_node_sequence_splice_child(
2183 __isl_take isl_schedule_node *node, int pos)
2185 int i, n;
2186 isl_union_set *filter;
2187 isl_schedule_node *child;
2188 isl_schedule_tree *tree;
2190 if (!node)
2191 return NULL;
2192 if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
2193 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2194 "not a sequence node", isl_schedule_node_free(node));
2195 node = isl_schedule_node_child(node, pos);
2196 node = isl_schedule_node_child(node, 0);
2197 if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
2198 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2199 "not a sequence node", isl_schedule_node_free(node));
2200 child = isl_schedule_node_copy(node);
2201 node = isl_schedule_node_parent(node);
2202 filter = isl_schedule_node_filter_get_filter(node);
2203 n = isl_schedule_node_n_children(child);
2204 for (i = 0; i < n; ++i) {
2205 child = isl_schedule_node_child(child, i);
2206 child = isl_schedule_node_filter_intersect_filter(child,
2207 isl_union_set_copy(filter));
2208 child = isl_schedule_node_parent(child);
2210 isl_union_set_free(filter);
2211 tree = isl_schedule_node_get_tree(child);
2212 isl_schedule_node_free(child);
2213 node = isl_schedule_node_parent(node);
2214 node = isl_schedule_node_sequence_splice(node, pos, tree);
2216 return node;
2219 /* Update the ancestors of "node" to point to the tree that "node"
2220 * now points to.
2221 * That is, replace the child in the original parent that corresponds
2222 * to the current tree position by node->tree and continue updating
2223 * the ancestors in the same way until the root is reached.
2225 * If "fn" is not NULL, then it is called on each ancestor as we move up
2226 * the tree so that it can modify the ancestor before it is added
2227 * to the list of ancestors of the modified node.
2228 * The additional "pos" argument records the position
2229 * of the "tree" argument in the original schedule tree.
2231 * If "node" originally points to a leaf of the schedule tree, then make sure
2232 * that in the end it points to a leaf in the updated schedule tree.
2234 static __isl_give isl_schedule_node *update_ancestors(
2235 __isl_take isl_schedule_node *node,
2236 __isl_give isl_schedule_tree *(*fn)(__isl_take isl_schedule_tree *tree,
2237 __isl_keep isl_schedule_node *pos, void *user), void *user)
2239 int i, n;
2240 int is_leaf;
2241 isl_ctx *ctx;
2242 isl_schedule_tree *tree;
2243 isl_schedule_node *pos = NULL;
2245 if (fn)
2246 pos = isl_schedule_node_copy(node);
2248 node = isl_schedule_node_cow(node);
2249 if (!node)
2250 return isl_schedule_node_free(pos);
2252 ctx = isl_schedule_node_get_ctx(node);
2253 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
2254 tree = isl_schedule_tree_copy(node->tree);
2256 for (i = n - 1; i >= 0; --i) {
2257 isl_schedule_tree *parent;
2259 parent = isl_schedule_tree_list_get_schedule_tree(
2260 node->ancestors, i);
2261 parent = isl_schedule_tree_replace_child(parent,
2262 node->child_pos[i], tree);
2263 if (fn) {
2264 pos = isl_schedule_node_parent(pos);
2265 parent = fn(parent, pos, user);
2267 node->ancestors = isl_schedule_tree_list_set_schedule_tree(
2268 node->ancestors, i, isl_schedule_tree_copy(parent));
2270 tree = parent;
2273 if (fn)
2274 isl_schedule_node_free(pos);
2276 is_leaf = isl_schedule_tree_is_leaf(node->tree);
2277 node->schedule = isl_schedule_set_root(node->schedule, tree);
2278 if (is_leaf) {
2279 isl_schedule_tree_free(node->tree);
2280 node->tree = isl_schedule_node_get_leaf(node);
2283 if (!node->schedule || !node->ancestors)
2284 return isl_schedule_node_free(node);
2286 return node;
2289 /* Replace the subtree that "pos" points to by "tree", updating
2290 * the ancestors to maintain a consistent state.
2292 __isl_give isl_schedule_node *isl_schedule_node_graft_tree(
2293 __isl_take isl_schedule_node *pos, __isl_take isl_schedule_tree *tree)
2295 if (!tree || !pos)
2296 goto error;
2297 if (pos->tree == tree) {
2298 isl_schedule_tree_free(tree);
2299 return pos;
2302 pos = isl_schedule_node_cow(pos);
2303 if (!pos)
2304 goto error;
2306 isl_schedule_tree_free(pos->tree);
2307 pos->tree = tree;
2309 return update_ancestors(pos, NULL, NULL);
2310 error:
2311 isl_schedule_node_free(pos);
2312 isl_schedule_tree_free(tree);
2313 return NULL;
2316 /* Make sure we can insert a node between "node" and its parent.
2317 * Return -1 on error, reporting the reason why we cannot insert a node.
2319 static int check_insert(__isl_keep isl_schedule_node *node)
2321 int has_parent;
2322 enum isl_schedule_node_type type;
2324 has_parent = isl_schedule_node_has_parent(node);
2325 if (has_parent < 0)
2326 return -1;
2327 if (!has_parent)
2328 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2329 "cannot insert node outside of root", return -1);
2331 type = isl_schedule_node_get_parent_type(node);
2332 if (type == isl_schedule_node_error)
2333 return -1;
2334 if (type == isl_schedule_node_set || type == isl_schedule_node_sequence)
2335 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2336 "cannot insert node between set or sequence node "
2337 "and its filter children", return -1);
2339 return 0;
2342 /* Insert a band node with partial schedule "mupa" between "node" and
2343 * its parent.
2344 * Return a pointer to the new band node.
2346 * If any of the nodes in the subtree rooted at "node" depend on
2347 * the set of outer band nodes then we refuse to insert the band node.
2349 __isl_give isl_schedule_node *isl_schedule_node_insert_partial_schedule(
2350 __isl_take isl_schedule_node *node,
2351 __isl_take isl_multi_union_pw_aff *mupa)
2353 int anchored;
2354 isl_schedule_band *band;
2355 isl_schedule_tree *tree;
2357 if (check_insert(node) < 0)
2358 node = isl_schedule_node_free(node);
2359 anchored = isl_schedule_node_is_subtree_anchored(node);
2360 if (anchored < 0)
2361 goto error;
2362 if (anchored)
2363 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2364 "cannot insert band node in anchored subtree",
2365 goto error);
2367 tree = isl_schedule_node_get_tree(node);
2368 band = isl_schedule_band_from_multi_union_pw_aff(mupa);
2369 tree = isl_schedule_tree_insert_band(tree, band);
2370 node = isl_schedule_node_graft_tree(node, tree);
2372 return node;
2373 error:
2374 isl_schedule_node_free(node);
2375 isl_multi_union_pw_aff_free(mupa);
2376 return NULL;
2379 /* Insert a context node with context "context" between "node" and its parent.
2380 * Return a pointer to the new context node.
2382 __isl_give isl_schedule_node *isl_schedule_node_insert_context(
2383 __isl_take isl_schedule_node *node, __isl_take isl_set *context)
2385 isl_schedule_tree *tree;
2387 if (check_insert(node) < 0)
2388 node = isl_schedule_node_free(node);
2390 tree = isl_schedule_node_get_tree(node);
2391 tree = isl_schedule_tree_insert_context(tree, context);
2392 node = isl_schedule_node_graft_tree(node, tree);
2394 return node;
2397 /* Insert an expansion node with the given "contraction" and "expansion"
2398 * between "node" and its parent.
2399 * Return a pointer to the new expansion node.
2401 * Typically the domain and range spaces of the expansion are different.
2402 * This means that only one of them can refer to the current domain space
2403 * in a consistent tree. It is up to the caller to ensure that the tree
2404 * returns to a consistent state.
2406 __isl_give isl_schedule_node *isl_schedule_node_insert_expansion(
2407 __isl_take isl_schedule_node *node,
2408 __isl_take isl_union_pw_multi_aff *contraction,
2409 __isl_take isl_union_map *expansion)
2411 isl_schedule_tree *tree;
2413 if (check_insert(node) < 0)
2414 node = isl_schedule_node_free(node);
2416 tree = isl_schedule_node_get_tree(node);
2417 tree = isl_schedule_tree_insert_expansion(tree, contraction, expansion);
2418 node = isl_schedule_node_graft_tree(node, tree);
2420 return node;
2423 /* Insert an extension node with extension "extension" between "node" and
2424 * its parent.
2425 * Return a pointer to the new extension node.
2427 __isl_give isl_schedule_node *isl_schedule_node_insert_extension(
2428 __isl_take isl_schedule_node *node,
2429 __isl_take isl_union_map *extension)
2431 isl_schedule_tree *tree;
2433 tree = isl_schedule_node_get_tree(node);
2434 tree = isl_schedule_tree_insert_extension(tree, extension);
2435 node = isl_schedule_node_graft_tree(node, tree);
2437 return node;
2440 /* Insert a filter node with filter "filter" between "node" and its parent.
2441 * Return a pointer to the new filter node.
2443 __isl_give isl_schedule_node *isl_schedule_node_insert_filter(
2444 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
2446 isl_schedule_tree *tree;
2448 if (check_insert(node) < 0)
2449 node = isl_schedule_node_free(node);
2451 tree = isl_schedule_node_get_tree(node);
2452 tree = isl_schedule_tree_insert_filter(tree, filter);
2453 node = isl_schedule_node_graft_tree(node, tree);
2455 return node;
2458 /* Insert a guard node with guard "guard" between "node" and its parent.
2459 * Return a pointer to the new guard node.
2461 __isl_give isl_schedule_node *isl_schedule_node_insert_guard(
2462 __isl_take isl_schedule_node *node, __isl_take isl_set *guard)
2464 isl_schedule_tree *tree;
2466 if (check_insert(node) < 0)
2467 node = isl_schedule_node_free(node);
2469 tree = isl_schedule_node_get_tree(node);
2470 tree = isl_schedule_tree_insert_guard(tree, guard);
2471 node = isl_schedule_node_graft_tree(node, tree);
2473 return node;
2476 /* Insert a mark node with mark identifier "mark" between "node" and
2477 * its parent.
2478 * Return a pointer to the new mark node.
2480 __isl_give isl_schedule_node *isl_schedule_node_insert_mark(
2481 __isl_take isl_schedule_node *node, __isl_take isl_id *mark)
2483 isl_schedule_tree *tree;
2485 if (check_insert(node) < 0)
2486 node = isl_schedule_node_free(node);
2488 tree = isl_schedule_node_get_tree(node);
2489 tree = isl_schedule_tree_insert_mark(tree, mark);
2490 node = isl_schedule_node_graft_tree(node, tree);
2492 return node;
2495 /* Attach the current subtree of "node" to a sequence of filter tree nodes
2496 * with filters described by "filters", attach this sequence
2497 * of filter tree nodes as children to a new tree of type "type" and
2498 * replace the original subtree of "node" by this new tree.
2499 * Each copy of the original subtree is simplified with respect
2500 * to the corresponding filter.
2502 static __isl_give isl_schedule_node *isl_schedule_node_insert_children(
2503 __isl_take isl_schedule_node *node,
2504 enum isl_schedule_node_type type,
2505 __isl_take isl_union_set_list *filters)
2507 int i, n;
2508 isl_ctx *ctx;
2509 isl_schedule_tree *tree;
2510 isl_schedule_tree_list *list;
2512 if (check_insert(node) < 0)
2513 node = isl_schedule_node_free(node);
2515 if (!node || !filters)
2516 goto error;
2518 ctx = isl_schedule_node_get_ctx(node);
2519 n = isl_union_set_list_n_union_set(filters);
2520 list = isl_schedule_tree_list_alloc(ctx, n);
2521 for (i = 0; i < n; ++i) {
2522 isl_schedule_node *node_i;
2523 isl_schedule_tree *tree;
2524 isl_union_set *filter;
2526 filter = isl_union_set_list_get_union_set(filters, i);
2527 node_i = isl_schedule_node_copy(node);
2528 node_i = isl_schedule_node_gist(node_i,
2529 isl_union_set_copy(filter));
2530 tree = isl_schedule_node_get_tree(node_i);
2531 isl_schedule_node_free(node_i);
2532 tree = isl_schedule_tree_insert_filter(tree, filter);
2533 list = isl_schedule_tree_list_add(list, tree);
2535 tree = isl_schedule_tree_from_children(type, list);
2536 node = isl_schedule_node_graft_tree(node, tree);
2538 isl_union_set_list_free(filters);
2539 return node;
2540 error:
2541 isl_union_set_list_free(filters);
2542 isl_schedule_node_free(node);
2543 return NULL;
2546 /* Insert a sequence node with child filters "filters" between "node" and
2547 * its parent. That is, the tree that "node" points to is attached
2548 * to each of the child nodes of the filter nodes.
2549 * Return a pointer to the new sequence node.
2551 __isl_give isl_schedule_node *isl_schedule_node_insert_sequence(
2552 __isl_take isl_schedule_node *node,
2553 __isl_take isl_union_set_list *filters)
2555 return isl_schedule_node_insert_children(node,
2556 isl_schedule_node_sequence, filters);
2559 /* Insert a set node with child filters "filters" between "node" and
2560 * its parent. That is, the tree that "node" points to is attached
2561 * to each of the child nodes of the filter nodes.
2562 * Return a pointer to the new set node.
2564 __isl_give isl_schedule_node *isl_schedule_node_insert_set(
2565 __isl_take isl_schedule_node *node,
2566 __isl_take isl_union_set_list *filters)
2568 return isl_schedule_node_insert_children(node,
2569 isl_schedule_node_set, filters);
2572 /* Remove "node" from its schedule tree and return a pointer
2573 * to the leaf at the same position in the updated schedule tree.
2575 * It is not allowed to remove the root of a schedule tree or
2576 * a child of a set or sequence node.
2578 __isl_give isl_schedule_node *isl_schedule_node_cut(
2579 __isl_take isl_schedule_node *node)
2581 isl_schedule_tree *leaf;
2582 enum isl_schedule_node_type parent_type;
2584 if (!node)
2585 return NULL;
2586 if (!isl_schedule_node_has_parent(node))
2587 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2588 "cannot cut root", return isl_schedule_node_free(node));
2590 parent_type = isl_schedule_node_get_parent_type(node);
2591 if (parent_type == isl_schedule_node_set ||
2592 parent_type == isl_schedule_node_sequence)
2593 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2594 "cannot cut child of set or sequence",
2595 return isl_schedule_node_free(node));
2597 leaf = isl_schedule_node_get_leaf(node);
2598 return isl_schedule_node_graft_tree(node, leaf);
2601 /* Remove a single node from the schedule tree, attaching the child
2602 * of "node" directly to its parent.
2603 * Return a pointer to this former child or to the leaf the position
2604 * of the original node if there was no child.
2605 * It is not allowed to remove the root of a schedule tree,
2606 * a set or sequence node, a child of a set or sequence node or
2607 * a band node with an anchored subtree.
2609 __isl_give isl_schedule_node *isl_schedule_node_delete(
2610 __isl_take isl_schedule_node *node)
2612 int n;
2613 isl_schedule_tree *tree;
2614 enum isl_schedule_node_type type;
2616 if (!node)
2617 return NULL;
2619 if (isl_schedule_node_get_tree_depth(node) == 0)
2620 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2621 "cannot delete root node",
2622 return isl_schedule_node_free(node));
2623 n = isl_schedule_node_n_children(node);
2624 if (n != 1)
2625 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2626 "can only delete node with a single child",
2627 return isl_schedule_node_free(node));
2628 type = isl_schedule_node_get_parent_type(node);
2629 if (type == isl_schedule_node_sequence || type == isl_schedule_node_set)
2630 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2631 "cannot delete child of set or sequence",
2632 return isl_schedule_node_free(node));
2633 if (isl_schedule_node_get_type(node) == isl_schedule_node_band) {
2634 int anchored;
2636 anchored = isl_schedule_node_is_subtree_anchored(node);
2637 if (anchored < 0)
2638 return isl_schedule_node_free(node);
2639 if (anchored)
2640 isl_die(isl_schedule_node_get_ctx(node),
2641 isl_error_invalid,
2642 "cannot delete band node with anchored subtree",
2643 return isl_schedule_node_free(node));
2646 tree = isl_schedule_node_get_tree(node);
2647 if (!tree || isl_schedule_tree_has_children(tree)) {
2648 tree = isl_schedule_tree_child(tree, 0);
2649 } else {
2650 isl_schedule_tree_free(tree);
2651 tree = isl_schedule_node_get_leaf(node);
2653 node = isl_schedule_node_graft_tree(node, tree);
2655 return node;
2658 /* Internal data structure for the group_ancestor callback.
2660 * If "finished" is set, then we no longer need to modify
2661 * any further ancestors.
2663 * "contraction" and "expansion" represent the expansion
2664 * that reflects the grouping.
2666 * "domain" contains the domain elements that reach the position
2667 * where the grouping is performed. That is, it is the range
2668 * of the resulting expansion.
2669 * "domain_universe" is the universe of "domain".
2670 * "group" is the set of group elements, i.e., the domain
2671 * of the resulting expansion.
2672 * "group_universe" is the universe of "group".
2674 * "sched" is the schedule for the group elements, in pratice
2675 * an identity mapping on "group_universe".
2676 * "dim" is the dimension of "sched".
2678 struct isl_schedule_group_data {
2679 int finished;
2681 isl_union_map *expansion;
2682 isl_union_pw_multi_aff *contraction;
2684 isl_union_set *domain;
2685 isl_union_set *domain_universe;
2686 isl_union_set *group;
2687 isl_union_set *group_universe;
2689 int dim;
2690 isl_multi_aff *sched;
2693 /* Is domain covered by data->domain within data->domain_universe?
2695 static int locally_covered_by_domain(__isl_keep isl_union_set *domain,
2696 struct isl_schedule_group_data *data)
2698 int is_subset;
2699 isl_union_set *test;
2701 test = isl_union_set_copy(domain);
2702 test = isl_union_set_intersect(test,
2703 isl_union_set_copy(data->domain_universe));
2704 is_subset = isl_union_set_is_subset(test, data->domain);
2705 isl_union_set_free(test);
2707 return is_subset;
2710 /* Update the band tree root "tree" to refer to the group instances
2711 * in data->group rather than the original domain elements in data->domain.
2712 * "pos" is the position in the original schedule tree where the modified
2713 * "tree" will be attached.
2715 * Add the part of the identity schedule on the group instances data->sched
2716 * that corresponds to this band node to the band schedule.
2717 * If the domain elements that reach the node and that are part
2718 * of data->domain_universe are all elements of data->domain (and therefore
2719 * replaced by the group instances) then this data->domain_universe
2720 * is removed from the domain of the band schedule.
2722 static __isl_give isl_schedule_tree *group_band(
2723 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2724 struct isl_schedule_group_data *data)
2726 isl_union_set *domain;
2727 isl_multi_aff *ma;
2728 isl_multi_union_pw_aff *mupa, *partial;
2729 int is_covered;
2730 int depth, n, has_id;
2732 domain = isl_schedule_node_get_domain(pos);
2733 is_covered = locally_covered_by_domain(domain, data);
2734 if (is_covered >= 0 && is_covered) {
2735 domain = isl_union_set_universe(domain);
2736 domain = isl_union_set_subtract(domain,
2737 isl_union_set_copy(data->domain_universe));
2738 tree = isl_schedule_tree_band_intersect_domain(tree, domain);
2739 } else
2740 isl_union_set_free(domain);
2741 if (is_covered < 0)
2742 return isl_schedule_tree_free(tree);
2743 depth = isl_schedule_node_get_schedule_depth(pos);
2744 n = isl_schedule_tree_band_n_member(tree);
2745 ma = isl_multi_aff_copy(data->sched);
2746 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, depth);
2747 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, n, data->dim - depth - n);
2748 mupa = isl_multi_union_pw_aff_from_multi_aff(ma);
2749 partial = isl_schedule_tree_band_get_partial_schedule(tree);
2750 has_id = isl_multi_union_pw_aff_has_tuple_id(partial, isl_dim_set);
2751 if (has_id < 0) {
2752 partial = isl_multi_union_pw_aff_free(partial);
2753 } else if (has_id) {
2754 isl_id *id;
2755 id = isl_multi_union_pw_aff_get_tuple_id(partial, isl_dim_set);
2756 mupa = isl_multi_union_pw_aff_set_tuple_id(mupa,
2757 isl_dim_set, id);
2759 partial = isl_multi_union_pw_aff_union_add(partial, mupa);
2760 tree = isl_schedule_tree_band_set_partial_schedule(tree, partial);
2762 return tree;
2765 /* Drop the parameters in "uset" that are not also in "space".
2766 * "n" is the number of parameters in "space".
2768 static __isl_give isl_union_set *union_set_drop_extra_params(
2769 __isl_take isl_union_set *uset, __isl_keep isl_space *space, int n)
2771 int n2;
2773 uset = isl_union_set_align_params(uset, isl_space_copy(space));
2774 n2 = isl_union_set_dim(uset, isl_dim_param);
2775 uset = isl_union_set_project_out(uset, isl_dim_param, n, n2 - n);
2777 return uset;
2780 /* Update the context tree root "tree" to refer to the group instances
2781 * in data->group rather than the original domain elements in data->domain.
2782 * "pos" is the position in the original schedule tree where the modified
2783 * "tree" will be attached.
2785 * We do not actually need to update "tree" since a context node only
2786 * refers to the schedule space. However, we may need to update "data"
2787 * to not refer to any parameters introduced by the context node.
2789 static __isl_give isl_schedule_tree *group_context(
2790 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2791 struct isl_schedule_group_data *data)
2793 isl_space *space;
2794 isl_union_set *domain;
2795 int n1, n2;
2796 int involves;
2798 if (isl_schedule_node_get_tree_depth(pos) == 1)
2799 return tree;
2801 domain = isl_schedule_node_get_universe_domain(pos);
2802 space = isl_union_set_get_space(domain);
2803 isl_union_set_free(domain);
2805 n1 = isl_space_dim(space, isl_dim_param);
2806 data->expansion = isl_union_map_align_params(data->expansion, space);
2807 n2 = isl_union_map_dim(data->expansion, isl_dim_param);
2809 if (!data->expansion)
2810 return isl_schedule_tree_free(tree);
2811 if (n1 == n2)
2812 return tree;
2814 involves = isl_union_map_involves_dims(data->expansion,
2815 isl_dim_param, n1, n2 - n1);
2816 if (involves < 0)
2817 return isl_schedule_tree_free(tree);
2818 if (involves)
2819 isl_die(isl_schedule_node_get_ctx(pos), isl_error_invalid,
2820 "grouping cannot only refer to global parameters",
2821 return isl_schedule_tree_free(tree));
2823 data->expansion = isl_union_map_project_out(data->expansion,
2824 isl_dim_param, n1, n2 - n1);
2825 space = isl_union_map_get_space(data->expansion);
2827 data->contraction = isl_union_pw_multi_aff_align_params(
2828 data->contraction, isl_space_copy(space));
2829 n2 = isl_union_pw_multi_aff_dim(data->contraction, isl_dim_param);
2830 data->contraction = isl_union_pw_multi_aff_drop_dims(data->contraction,
2831 isl_dim_param, n1, n2 - n1);
2833 data->domain = union_set_drop_extra_params(data->domain, space, n1);
2834 data->domain_universe =
2835 union_set_drop_extra_params(data->domain_universe, space, n1);
2836 data->group = union_set_drop_extra_params(data->group, space, n1);
2837 data->group_universe =
2838 union_set_drop_extra_params(data->group_universe, space, n1);
2840 data->sched = isl_multi_aff_align_params(data->sched,
2841 isl_space_copy(space));
2842 n2 = isl_multi_aff_dim(data->sched, isl_dim_param);
2843 data->sched = isl_multi_aff_drop_dims(data->sched,
2844 isl_dim_param, n1, n2 - n1);
2846 isl_space_free(space);
2848 return tree;
2851 /* Update the domain tree root "tree" to refer to the group instances
2852 * in data->group rather than the original domain elements in data->domain.
2853 * "pos" is the position in the original schedule tree where the modified
2854 * "tree" will be attached.
2856 * We first double-check that all grouped domain elements are actually
2857 * part of the root domain and then replace those elements by the group
2858 * instances.
2860 static __isl_give isl_schedule_tree *group_domain(
2861 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2862 struct isl_schedule_group_data *data)
2864 isl_union_set *domain;
2865 int is_subset;
2867 domain = isl_schedule_tree_domain_get_domain(tree);
2868 is_subset = isl_union_set_is_subset(data->domain, domain);
2869 isl_union_set_free(domain);
2870 if (is_subset < 0)
2871 return isl_schedule_tree_free(tree);
2872 if (!is_subset)
2873 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
2874 "grouped domain should be part of outer domain",
2875 return isl_schedule_tree_free(tree));
2876 domain = isl_schedule_tree_domain_get_domain(tree);
2877 domain = isl_union_set_subtract(domain,
2878 isl_union_set_copy(data->domain));
2879 domain = isl_union_set_union(domain, isl_union_set_copy(data->group));
2880 tree = isl_schedule_tree_domain_set_domain(tree, domain);
2882 return tree;
2885 /* Update the expansion tree root "tree" to refer to the group instances
2886 * in data->group rather than the original domain elements in data->domain.
2887 * "pos" is the position in the original schedule tree where the modified
2888 * "tree" will be attached.
2890 * Let G_1 -> D_1 be the expansion of "tree" and G_2 -> D_2 the newly
2891 * introduced expansion in a descendant of "tree".
2892 * We first double-check that D_2 is a subset of D_1.
2893 * Then we remove D_2 from the range of G_1 -> D_1 and add the mapping
2894 * G_1 -> D_1 . D_2 -> G_2.
2895 * Simmilarly, we restrict the domain of the contraction to the universe
2896 * of the range of the updated expansion and add G_2 -> D_2 . D_1 -> G_1,
2897 * attempting to remove the domain constraints of this additional part.
2899 static __isl_give isl_schedule_tree *group_expansion(
2900 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2901 struct isl_schedule_group_data *data)
2903 isl_union_set *domain;
2904 isl_union_map *expansion, *umap;
2905 isl_union_pw_multi_aff *contraction, *upma;
2906 int is_subset;
2908 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2909 domain = isl_union_map_range(expansion);
2910 is_subset = isl_union_set_is_subset(data->domain, domain);
2911 isl_union_set_free(domain);
2912 if (is_subset < 0)
2913 return isl_schedule_tree_free(tree);
2914 if (!is_subset)
2915 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
2916 "grouped domain should be part "
2917 "of outer expansion domain",
2918 return isl_schedule_tree_free(tree));
2919 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2920 umap = isl_union_map_from_union_pw_multi_aff(
2921 isl_union_pw_multi_aff_copy(data->contraction));
2922 umap = isl_union_map_apply_range(expansion, umap);
2923 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2924 expansion = isl_union_map_subtract_range(expansion,
2925 isl_union_set_copy(data->domain));
2926 expansion = isl_union_map_union(expansion, umap);
2927 umap = isl_union_map_universe(isl_union_map_copy(expansion));
2928 domain = isl_union_map_range(umap);
2929 contraction = isl_schedule_tree_expansion_get_contraction(tree);
2930 umap = isl_union_map_from_union_pw_multi_aff(contraction);
2931 umap = isl_union_map_apply_range(isl_union_map_copy(data->expansion),
2932 umap);
2933 upma = isl_union_pw_multi_aff_from_union_map(umap);
2934 contraction = isl_schedule_tree_expansion_get_contraction(tree);
2935 contraction = isl_union_pw_multi_aff_intersect_domain(contraction,
2936 domain);
2937 domain = isl_union_pw_multi_aff_domain(
2938 isl_union_pw_multi_aff_copy(upma));
2939 upma = isl_union_pw_multi_aff_gist(upma, domain);
2940 contraction = isl_union_pw_multi_aff_union_add(contraction, upma);
2941 tree = isl_schedule_tree_expansion_set_contraction_and_expansion(tree,
2942 contraction, expansion);
2944 return tree;
2947 /* Update the tree root "tree" to refer to the group instances
2948 * in data->group rather than the original domain elements in data->domain.
2949 * "pos" is the position in the original schedule tree where the modified
2950 * "tree" will be attached.
2952 * If we have come across a domain or expansion node before (data->finished
2953 * is set), then we no longer need perform any modifications.
2955 * If "tree" is a filter, then we add data->group_universe to the filter.
2956 * We also remove data->domain_universe from the filter if all the domain
2957 * elements in this universe that reach the filter node are part of
2958 * the elements that are being grouped by data->expansion.
2959 * If "tree" is a band, domain or expansion, then it is handled
2960 * in a separate function.
2962 static __isl_give isl_schedule_tree *group_ancestor(
2963 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2964 void *user)
2966 struct isl_schedule_group_data *data = user;
2967 isl_union_set *domain;
2968 int is_covered;
2970 if (!tree || !pos)
2971 return isl_schedule_tree_free(tree);
2973 if (data->finished)
2974 return tree;
2976 switch (isl_schedule_tree_get_type(tree)) {
2977 case isl_schedule_node_error:
2978 return isl_schedule_tree_free(tree);
2979 case isl_schedule_node_extension:
2980 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
2981 "grouping not allowed in extended tree",
2982 return isl_schedule_tree_free(tree));
2983 case isl_schedule_node_band:
2984 tree = group_band(tree, pos, data);
2985 break;
2986 case isl_schedule_node_context:
2987 tree = group_context(tree, pos, data);
2988 break;
2989 case isl_schedule_node_domain:
2990 tree = group_domain(tree, pos, data);
2991 data->finished = 1;
2992 break;
2993 case isl_schedule_node_filter:
2994 domain = isl_schedule_node_get_domain(pos);
2995 is_covered = locally_covered_by_domain(domain, data);
2996 isl_union_set_free(domain);
2997 if (is_covered < 0)
2998 return isl_schedule_tree_free(tree);
2999 domain = isl_schedule_tree_filter_get_filter(tree);
3000 if (is_covered)
3001 domain = isl_union_set_subtract(domain,
3002 isl_union_set_copy(data->domain_universe));
3003 domain = isl_union_set_union(domain,
3004 isl_union_set_copy(data->group_universe));
3005 tree = isl_schedule_tree_filter_set_filter(tree, domain);
3006 break;
3007 case isl_schedule_node_expansion:
3008 tree = group_expansion(tree, pos, data);
3009 data->finished = 1;
3010 break;
3011 case isl_schedule_node_leaf:
3012 case isl_schedule_node_guard:
3013 case isl_schedule_node_mark:
3014 case isl_schedule_node_sequence:
3015 case isl_schedule_node_set:
3016 break;
3019 return tree;
3022 /* Group the domain elements that reach "node" into instances
3023 * of a single statement with identifier "group_id".
3024 * In particular, group the domain elements according to their
3025 * prefix schedule.
3027 * That is, introduce an expansion node with as contraction
3028 * the prefix schedule (with the target space replaced by "group_id")
3029 * and as expansion the inverse of this contraction (with its range
3030 * intersected with the domain elements that reach "node").
3031 * The outer nodes are then modified to refer to the group instances
3032 * instead of the original domain elements.
3034 * No instance of "group_id" is allowed to reach "node" prior
3035 * to the grouping.
3036 * No ancestor of "node" is allowed to be an extension node.
3038 * Return a pointer to original node in tree, i.e., the child
3039 * of the newly introduced expansion node.
3041 __isl_give isl_schedule_node *isl_schedule_node_group(
3042 __isl_take isl_schedule_node *node, __isl_take isl_id *group_id)
3044 struct isl_schedule_group_data data = { 0 };
3045 isl_space *space;
3046 isl_union_set *domain;
3047 isl_union_pw_multi_aff *contraction;
3048 isl_union_map *expansion;
3049 int disjoint;
3051 if (!node || !group_id)
3052 goto error;
3053 if (check_insert(node) < 0)
3054 goto error;
3056 domain = isl_schedule_node_get_domain(node);
3057 data.domain = isl_union_set_copy(domain);
3058 data.domain_universe = isl_union_set_copy(domain);
3059 data.domain_universe = isl_union_set_universe(data.domain_universe);
3061 data.dim = isl_schedule_node_get_schedule_depth(node);
3062 if (data.dim == 0) {
3063 isl_ctx *ctx;
3064 isl_set *set;
3065 isl_union_set *group;
3066 isl_union_map *univ;
3068 ctx = isl_schedule_node_get_ctx(node);
3069 space = isl_space_set_alloc(ctx, 0, 0);
3070 space = isl_space_set_tuple_id(space, isl_dim_set, group_id);
3071 set = isl_set_universe(isl_space_copy(space));
3072 group = isl_union_set_from_set(set);
3073 expansion = isl_union_map_from_domain_and_range(domain, group);
3074 univ = isl_union_map_universe(isl_union_map_copy(expansion));
3075 contraction = isl_union_pw_multi_aff_from_union_map(univ);
3076 expansion = isl_union_map_reverse(expansion);
3077 } else {
3078 isl_multi_union_pw_aff *prefix;
3079 isl_union_set *univ;
3081 prefix =
3082 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
3083 prefix = isl_multi_union_pw_aff_set_tuple_id(prefix,
3084 isl_dim_set, group_id);
3085 space = isl_multi_union_pw_aff_get_space(prefix);
3086 contraction = isl_union_pw_multi_aff_from_multi_union_pw_aff(
3087 prefix);
3088 univ = isl_union_set_universe(isl_union_set_copy(domain));
3089 contraction =
3090 isl_union_pw_multi_aff_intersect_domain(contraction, univ);
3091 expansion = isl_union_map_from_union_pw_multi_aff(
3092 isl_union_pw_multi_aff_copy(contraction));
3093 expansion = isl_union_map_reverse(expansion);
3094 expansion = isl_union_map_intersect_range(expansion, domain);
3096 space = isl_space_map_from_set(space);
3097 data.sched = isl_multi_aff_identity(space);
3098 data.group = isl_union_map_domain(isl_union_map_copy(expansion));
3099 data.group = isl_union_set_coalesce(data.group);
3100 data.group_universe = isl_union_set_copy(data.group);
3101 data.group_universe = isl_union_set_universe(data.group_universe);
3102 data.expansion = isl_union_map_copy(expansion);
3103 data.contraction = isl_union_pw_multi_aff_copy(contraction);
3104 node = isl_schedule_node_insert_expansion(node, contraction, expansion);
3106 disjoint = isl_union_set_is_disjoint(data.domain_universe,
3107 data.group_universe);
3109 node = update_ancestors(node, &group_ancestor, &data);
3111 isl_union_set_free(data.domain);
3112 isl_union_set_free(data.domain_universe);
3113 isl_union_set_free(data.group);
3114 isl_union_set_free(data.group_universe);
3115 isl_multi_aff_free(data.sched);
3116 isl_union_map_free(data.expansion);
3117 isl_union_pw_multi_aff_free(data.contraction);
3119 node = isl_schedule_node_child(node, 0);
3121 if (!node || disjoint < 0)
3122 return isl_schedule_node_free(node);
3123 if (!disjoint)
3124 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
3125 "group instances already reach node",
3126 isl_schedule_node_free(node));
3128 return node;
3129 error:
3130 isl_schedule_node_free(node);
3131 isl_id_free(group_id);
3132 return NULL;
3135 /* Compute the gist of the given band node with respect to "context".
3137 __isl_give isl_schedule_node *isl_schedule_node_band_gist(
3138 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
3140 isl_schedule_tree *tree;
3142 tree = isl_schedule_node_get_tree(node);
3143 tree = isl_schedule_tree_band_gist(tree, context);
3144 return isl_schedule_node_graft_tree(node, tree);
3147 /* Internal data structure for isl_schedule_node_gist.
3148 * "n_expansion" is the number of outer expansion nodes
3149 * with respect to the current position
3150 * "filters" contains an element for each outer filter, expansion or
3151 * extension node with respect to the current position, each representing
3152 * the intersection of the previous element and the filter on the filter node
3153 * or the expansion/extension of the previous element.
3154 * The first element in the original context passed to isl_schedule_node_gist.
3156 struct isl_node_gist_data {
3157 int n_expansion;
3158 isl_union_set_list *filters;
3161 /* Enter the expansion node "node" during a isl_schedule_node_gist traversal.
3163 * In particular, add an extra element to data->filters containing
3164 * the expansion of the previous element and replace the expansion
3165 * and contraction on "node" by the gist with respect to these filters.
3166 * Also keep track of the fact that we have entered another expansion.
3168 static __isl_give isl_schedule_node *gist_enter_expansion(
3169 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3171 int n;
3172 isl_union_set *inner;
3173 isl_union_map *expansion;
3174 isl_union_pw_multi_aff *contraction;
3176 data->n_expansion++;
3178 n = isl_union_set_list_n_union_set(data->filters);
3179 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3180 expansion = isl_schedule_node_expansion_get_expansion(node);
3181 inner = isl_union_set_apply(inner, expansion);
3183 contraction = isl_schedule_node_expansion_get_contraction(node);
3184 contraction = isl_union_pw_multi_aff_gist(contraction,
3185 isl_union_set_copy(inner));
3187 data->filters = isl_union_set_list_add(data->filters, inner);
3189 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3190 expansion = isl_schedule_node_expansion_get_expansion(node);
3191 expansion = isl_union_map_gist_domain(expansion, inner);
3192 node = isl_schedule_node_expansion_set_contraction_and_expansion(node,
3193 contraction, expansion);
3195 return node;
3198 /* Leave the expansion node "node" during a isl_schedule_node_gist traversal.
3200 * In particular, remove the element in data->filters that was added by
3201 * gist_enter_expansion and decrement the number of outer expansions.
3203 * The expansion has already been simplified in gist_enter_expansion.
3204 * If this simplification results in an identity expansion, then
3205 * it is removed here.
3207 static __isl_give isl_schedule_node *gist_leave_expansion(
3208 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3210 int n;
3211 isl_bool identity;
3212 isl_union_map *expansion;
3214 expansion = isl_schedule_node_expansion_get_expansion(node);
3215 identity = isl_union_map_is_identity(expansion);
3216 isl_union_map_free(expansion);
3218 if (identity < 0)
3219 node = isl_schedule_node_free(node);
3220 else if (identity)
3221 node = isl_schedule_node_delete(node);
3223 n = isl_union_set_list_n_union_set(data->filters);
3224 data->filters = isl_union_set_list_drop(data->filters, n - 1, 1);
3226 data->n_expansion--;
3228 return node;
3231 /* Enter the extension node "node" during a isl_schedule_node_gist traversal.
3233 * In particular, add an extra element to data->filters containing
3234 * the union of the previous element with the additional domain elements
3235 * introduced by the extension.
3237 static __isl_give isl_schedule_node *gist_enter_extension(
3238 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3240 int n;
3241 isl_union_set *inner, *extra;
3242 isl_union_map *extension;
3244 n = isl_union_set_list_n_union_set(data->filters);
3245 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3246 extension = isl_schedule_node_extension_get_extension(node);
3247 extra = isl_union_map_range(extension);
3248 inner = isl_union_set_union(inner, extra);
3250 data->filters = isl_union_set_list_add(data->filters, inner);
3252 return node;
3255 /* Can we finish gisting at this node?
3256 * That is, is the filter on the current filter node a subset of
3257 * the original context passed to isl_schedule_node_gist?
3258 * If we have gone through any expansions, then we cannot perform
3259 * this test since the current domain elements are incomparable
3260 * to the domain elements in the original context.
3262 static int gist_done(__isl_keep isl_schedule_node *node,
3263 struct isl_node_gist_data *data)
3265 isl_union_set *filter, *outer;
3266 int subset;
3268 if (data->n_expansion != 0)
3269 return 0;
3271 filter = isl_schedule_node_filter_get_filter(node);
3272 outer = isl_union_set_list_get_union_set(data->filters, 0);
3273 subset = isl_union_set_is_subset(filter, outer);
3274 isl_union_set_free(outer);
3275 isl_union_set_free(filter);
3277 return subset;
3280 /* Callback for "traverse" to enter a node and to move
3281 * to the deepest initial subtree that should be traversed
3282 * by isl_schedule_node_gist.
3284 * The "filters" list is extended by one element each time
3285 * we come across a filter node by the result of intersecting
3286 * the last element in the list with the filter on the filter node.
3288 * If the filter on the current filter node is a subset of
3289 * the original context passed to isl_schedule_node_gist,
3290 * then there is no need to go into its subtree since it cannot
3291 * be further simplified by the context. The "filters" list is
3292 * still extended for consistency, but the actual value of the
3293 * added element is immaterial since it will not be used.
3295 * Otherwise, the filter on the current filter node is replaced by
3296 * the gist of the original filter with respect to the intersection
3297 * of the original context with the intermediate filters.
3299 * If the new element in the "filters" list is empty, then no elements
3300 * can reach the descendants of the current filter node. The subtree
3301 * underneath the filter node is therefore removed.
3303 * Each expansion node we come across is handled by
3304 * gist_enter_expansion.
3306 * Each extension node we come across is handled by
3307 * gist_enter_extension.
3309 static __isl_give isl_schedule_node *gist_enter(
3310 __isl_take isl_schedule_node *node, void *user)
3312 struct isl_node_gist_data *data = user;
3314 do {
3315 isl_union_set *filter, *inner;
3316 int done, empty;
3317 int n;
3319 switch (isl_schedule_node_get_type(node)) {
3320 case isl_schedule_node_error:
3321 return isl_schedule_node_free(node);
3322 case isl_schedule_node_expansion:
3323 node = gist_enter_expansion(node, data);
3324 continue;
3325 case isl_schedule_node_extension:
3326 node = gist_enter_extension(node, data);
3327 continue;
3328 case isl_schedule_node_band:
3329 case isl_schedule_node_context:
3330 case isl_schedule_node_domain:
3331 case isl_schedule_node_guard:
3332 case isl_schedule_node_leaf:
3333 case isl_schedule_node_mark:
3334 case isl_schedule_node_sequence:
3335 case isl_schedule_node_set:
3336 continue;
3337 case isl_schedule_node_filter:
3338 break;
3340 done = gist_done(node, data);
3341 filter = isl_schedule_node_filter_get_filter(node);
3342 if (done < 0 || done) {
3343 data->filters = isl_union_set_list_add(data->filters,
3344 filter);
3345 if (done < 0)
3346 return isl_schedule_node_free(node);
3347 return node;
3349 n = isl_union_set_list_n_union_set(data->filters);
3350 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3351 filter = isl_union_set_gist(filter, isl_union_set_copy(inner));
3352 node = isl_schedule_node_filter_set_filter(node,
3353 isl_union_set_copy(filter));
3354 filter = isl_union_set_intersect(filter, inner);
3355 empty = isl_union_set_is_empty(filter);
3356 data->filters = isl_union_set_list_add(data->filters, filter);
3357 if (empty < 0)
3358 return isl_schedule_node_free(node);
3359 if (!empty)
3360 continue;
3361 node = isl_schedule_node_child(node, 0);
3362 node = isl_schedule_node_cut(node);
3363 node = isl_schedule_node_parent(node);
3364 return node;
3365 } while (isl_schedule_node_has_children(node) &&
3366 (node = isl_schedule_node_first_child(node)) != NULL);
3368 return node;
3371 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
3373 * In particular, if the current node is a filter node, then we remove
3374 * the element on the "filters" list that was added when we entered
3375 * the node. There is no need to compute any gist here, since we
3376 * already did that when we entered the node.
3378 * Expansion nodes are handled by gist_leave_expansion.
3380 * If the current node is an extension, then remove the element
3381 * in data->filters that was added by gist_enter_extension.
3383 * If the current node is a band node, then we compute the gist of
3384 * the band node with respect to the intersection of the original context
3385 * and the intermediate filters.
3387 * If the current node is a sequence or set node, then some of
3388 * the filter children may have become empty and so they are removed.
3389 * If only one child is left, then the set or sequence node along with
3390 * the single remaining child filter is removed. The filter can be
3391 * removed because the filters on a sequence or set node are supposed
3392 * to partition the incoming domain instances.
3393 * In principle, it should then be impossible for there to be zero
3394 * remaining children, but should this happen, we replace the entire
3395 * subtree with an empty filter.
3397 static __isl_give isl_schedule_node *gist_leave(
3398 __isl_take isl_schedule_node *node, void *user)
3400 struct isl_node_gist_data *data = user;
3401 isl_schedule_tree *tree;
3402 int i, n;
3403 isl_union_set *filter;
3405 switch (isl_schedule_node_get_type(node)) {
3406 case isl_schedule_node_error:
3407 return isl_schedule_node_free(node);
3408 case isl_schedule_node_expansion:
3409 node = gist_leave_expansion(node, data);
3410 break;
3411 case isl_schedule_node_extension:
3412 case isl_schedule_node_filter:
3413 n = isl_union_set_list_n_union_set(data->filters);
3414 data->filters = isl_union_set_list_drop(data->filters,
3415 n - 1, 1);
3416 break;
3417 case isl_schedule_node_band:
3418 n = isl_union_set_list_n_union_set(data->filters);
3419 filter = isl_union_set_list_get_union_set(data->filters, n - 1);
3420 node = isl_schedule_node_band_gist(node, filter);
3421 break;
3422 case isl_schedule_node_set:
3423 case isl_schedule_node_sequence:
3424 tree = isl_schedule_node_get_tree(node);
3425 n = isl_schedule_tree_n_children(tree);
3426 for (i = n - 1; i >= 0; --i) {
3427 isl_schedule_tree *child;
3428 isl_union_set *filter;
3429 int empty;
3431 child = isl_schedule_tree_get_child(tree, i);
3432 filter = isl_schedule_tree_filter_get_filter(child);
3433 empty = isl_union_set_is_empty(filter);
3434 isl_union_set_free(filter);
3435 isl_schedule_tree_free(child);
3436 if (empty < 0)
3437 tree = isl_schedule_tree_free(tree);
3438 else if (empty)
3439 tree = isl_schedule_tree_drop_child(tree, i);
3441 n = isl_schedule_tree_n_children(tree);
3442 node = isl_schedule_node_graft_tree(node, tree);
3443 if (n == 1) {
3444 node = isl_schedule_node_delete(node);
3445 node = isl_schedule_node_delete(node);
3446 } else if (n == 0) {
3447 isl_space *space;
3449 filter =
3450 isl_union_set_list_get_union_set(data->filters, 0);
3451 space = isl_union_set_get_space(filter);
3452 isl_union_set_free(filter);
3453 filter = isl_union_set_empty(space);
3454 node = isl_schedule_node_cut(node);
3455 node = isl_schedule_node_insert_filter(node, filter);
3457 break;
3458 case isl_schedule_node_context:
3459 case isl_schedule_node_domain:
3460 case isl_schedule_node_guard:
3461 case isl_schedule_node_leaf:
3462 case isl_schedule_node_mark:
3463 break;
3466 return node;
3469 /* Compute the gist of the subtree at "node" with respect to
3470 * the reaching domain elements in "context".
3471 * In particular, compute the gist of all band and filter nodes
3472 * in the subtree with respect to "context". Children of set or sequence
3473 * nodes that end up with an empty filter are removed completely.
3475 * We keep track of the intersection of "context" with all outer filters
3476 * of the current node within the subtree in the final element of "filters".
3477 * Initially, this list contains the single element "context" and it is
3478 * extended or shortened each time we enter or leave a filter node.
3480 __isl_give isl_schedule_node *isl_schedule_node_gist(
3481 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
3483 struct isl_node_gist_data data;
3485 data.n_expansion = 0;
3486 data.filters = isl_union_set_list_from_union_set(context);
3487 node = traverse(node, &gist_enter, &gist_leave, &data);
3488 isl_union_set_list_free(data.filters);
3489 return node;
3492 /* Intersect the domain of domain node "node" with "domain".
3494 * If the domain of "node" is already a subset of "domain",
3495 * then nothing needs to be changed.
3497 * Otherwise, we replace the domain of the domain node by the intersection
3498 * and simplify the subtree rooted at "node" with respect to this intersection.
3500 __isl_give isl_schedule_node *isl_schedule_node_domain_intersect_domain(
3501 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain)
3503 isl_schedule_tree *tree;
3504 isl_union_set *uset;
3505 int is_subset;
3507 if (!node || !domain)
3508 goto error;
3510 uset = isl_schedule_tree_domain_get_domain(node->tree);
3511 is_subset = isl_union_set_is_subset(uset, domain);
3512 isl_union_set_free(uset);
3513 if (is_subset < 0)
3514 goto error;
3515 if (is_subset) {
3516 isl_union_set_free(domain);
3517 return node;
3520 tree = isl_schedule_tree_copy(node->tree);
3521 uset = isl_schedule_tree_domain_get_domain(tree);
3522 uset = isl_union_set_intersect(uset, domain);
3523 tree = isl_schedule_tree_domain_set_domain(tree,
3524 isl_union_set_copy(uset));
3525 node = isl_schedule_node_graft_tree(node, tree);
3527 node = isl_schedule_node_child(node, 0);
3528 node = isl_schedule_node_gist(node, uset);
3529 node = isl_schedule_node_parent(node);
3531 return node;
3532 error:
3533 isl_schedule_node_free(node);
3534 isl_union_set_free(domain);
3535 return NULL;
3538 /* Replace the domain of domain node "node" with the gist
3539 * of the original domain with respect to the parameter domain "context".
3541 __isl_give isl_schedule_node *isl_schedule_node_domain_gist_params(
3542 __isl_take isl_schedule_node *node, __isl_take isl_set *context)
3544 isl_union_set *domain;
3545 isl_schedule_tree *tree;
3547 if (!node || !context)
3548 goto error;
3550 tree = isl_schedule_tree_copy(node->tree);
3551 domain = isl_schedule_tree_domain_get_domain(node->tree);
3552 domain = isl_union_set_gist_params(domain, context);
3553 tree = isl_schedule_tree_domain_set_domain(tree, domain);
3554 node = isl_schedule_node_graft_tree(node, tree);
3556 return node;
3557 error:
3558 isl_schedule_node_free(node);
3559 isl_set_free(context);
3560 return NULL;
3563 /* Internal data structure for isl_schedule_node_get_subtree_expansion.
3564 * "expansions" contains a list of accumulated expansions
3565 * for each outer expansion, set or sequence node. The first element
3566 * in the list is an identity mapping on the reaching domain elements.
3567 * "res" collects the results.
3569 struct isl_subtree_expansion_data {
3570 isl_union_map_list *expansions;
3571 isl_union_map *res;
3574 /* Callback for "traverse" to enter a node and to move
3575 * to the deepest initial subtree that should be traversed
3576 * by isl_schedule_node_get_subtree_expansion.
3578 * Whenever we come across an expansion node, the last element
3579 * of data->expansions is combined with the expansion
3580 * on the expansion node.
3582 * Whenever we come across a filter node that is the child
3583 * of a set or sequence node, data->expansions is extended
3584 * with a new element that restricts the previous element
3585 * to the elements selected by the filter.
3586 * The previous element can then be reused while backtracking.
3588 static __isl_give isl_schedule_node *subtree_expansion_enter(
3589 __isl_take isl_schedule_node *node, void *user)
3591 struct isl_subtree_expansion_data *data = user;
3593 do {
3594 enum isl_schedule_node_type type;
3595 isl_union_set *filter;
3596 isl_union_map *inner, *expansion;
3597 int n;
3599 switch (isl_schedule_node_get_type(node)) {
3600 case isl_schedule_node_error:
3601 return isl_schedule_node_free(node);
3602 case isl_schedule_node_filter:
3603 type = isl_schedule_node_get_parent_type(node);
3604 if (type != isl_schedule_node_set &&
3605 type != isl_schedule_node_sequence)
3606 break;
3607 filter = isl_schedule_node_filter_get_filter(node);
3608 n = isl_union_map_list_n_union_map(data->expansions);
3609 inner =
3610 isl_union_map_list_get_union_map(data->expansions,
3611 n - 1);
3612 inner = isl_union_map_intersect_range(inner, filter);
3613 data->expansions =
3614 isl_union_map_list_add(data->expansions, inner);
3615 break;
3616 case isl_schedule_node_expansion:
3617 n = isl_union_map_list_n_union_map(data->expansions);
3618 expansion =
3619 isl_schedule_node_expansion_get_expansion(node);
3620 inner =
3621 isl_union_map_list_get_union_map(data->expansions,
3622 n - 1);
3623 inner = isl_union_map_apply_range(inner, expansion);
3624 data->expansions =
3625 isl_union_map_list_set_union_map(data->expansions,
3626 n - 1, inner);
3627 break;
3628 case isl_schedule_node_band:
3629 case isl_schedule_node_context:
3630 case isl_schedule_node_domain:
3631 case isl_schedule_node_extension:
3632 case isl_schedule_node_guard:
3633 case isl_schedule_node_leaf:
3634 case isl_schedule_node_mark:
3635 case isl_schedule_node_sequence:
3636 case isl_schedule_node_set:
3637 break;
3639 } while (isl_schedule_node_has_children(node) &&
3640 (node = isl_schedule_node_first_child(node)) != NULL);
3642 return node;
3645 /* Callback for "traverse" to leave a node for
3646 * isl_schedule_node_get_subtree_expansion.
3648 * If we come across a filter node that is the child
3649 * of a set or sequence node, then we remove the element
3650 * of data->expansions that was added in subtree_expansion_enter.
3652 * If we reach a leaf node, then the accumulated expansion is
3653 * added to data->res.
3655 static __isl_give isl_schedule_node *subtree_expansion_leave(
3656 __isl_take isl_schedule_node *node, void *user)
3658 struct isl_subtree_expansion_data *data = user;
3659 int n;
3660 isl_union_map *inner;
3661 enum isl_schedule_node_type type;
3663 switch (isl_schedule_node_get_type(node)) {
3664 case isl_schedule_node_error:
3665 return isl_schedule_node_free(node);
3666 case isl_schedule_node_filter:
3667 type = isl_schedule_node_get_parent_type(node);
3668 if (type != isl_schedule_node_set &&
3669 type != isl_schedule_node_sequence)
3670 break;
3671 n = isl_union_map_list_n_union_map(data->expansions);
3672 data->expansions = isl_union_map_list_drop(data->expansions,
3673 n - 1, 1);
3674 break;
3675 case isl_schedule_node_leaf:
3676 n = isl_union_map_list_n_union_map(data->expansions);
3677 inner = isl_union_map_list_get_union_map(data->expansions,
3678 n - 1);
3679 data->res = isl_union_map_union(data->res, inner);
3680 break;
3681 case isl_schedule_node_band:
3682 case isl_schedule_node_context:
3683 case isl_schedule_node_domain:
3684 case isl_schedule_node_expansion:
3685 case isl_schedule_node_extension:
3686 case isl_schedule_node_guard:
3687 case isl_schedule_node_mark:
3688 case isl_schedule_node_sequence:
3689 case isl_schedule_node_set:
3690 break;
3693 return node;
3696 /* Return a mapping from the domain elements that reach "node"
3697 * to the corresponding domain elements in the leaves of the subtree
3698 * rooted at "node" obtained by composing the intermediate expansions.
3700 * We start out with an identity mapping between the domain elements
3701 * that reach "node" and compose it with all the expansions
3702 * on a path from "node" to a leaf while traversing the subtree.
3703 * Within the children of an a sequence or set node, the
3704 * accumulated expansion is restricted to the elements selected
3705 * by the filter child.
3707 __isl_give isl_union_map *isl_schedule_node_get_subtree_expansion(
3708 __isl_keep isl_schedule_node *node)
3710 struct isl_subtree_expansion_data data;
3711 isl_space *space;
3712 isl_union_set *domain;
3713 isl_union_map *expansion;
3715 if (!node)
3716 return NULL;
3718 domain = isl_schedule_node_get_universe_domain(node);
3719 space = isl_union_set_get_space(domain);
3720 expansion = isl_union_set_identity(domain);
3721 data.res = isl_union_map_empty(space);
3722 data.expansions = isl_union_map_list_from_union_map(expansion);
3724 node = isl_schedule_node_copy(node);
3725 node = traverse(node, &subtree_expansion_enter,
3726 &subtree_expansion_leave, &data);
3727 if (!node)
3728 data.res = isl_union_map_free(data.res);
3729 isl_schedule_node_free(node);
3731 isl_union_map_list_free(data.expansions);
3733 return data.res;
3736 /* Internal data structure for isl_schedule_node_get_subtree_contraction.
3737 * "contractions" contains a list of accumulated contractions
3738 * for each outer expansion, set or sequence node. The first element
3739 * in the list is an identity mapping on the reaching domain elements.
3740 * "res" collects the results.
3742 struct isl_subtree_contraction_data {
3743 isl_union_pw_multi_aff_list *contractions;
3744 isl_union_pw_multi_aff *res;
3747 /* Callback for "traverse" to enter a node and to move
3748 * to the deepest initial subtree that should be traversed
3749 * by isl_schedule_node_get_subtree_contraction.
3751 * Whenever we come across an expansion node, the last element
3752 * of data->contractions is combined with the contraction
3753 * on the expansion node.
3755 * Whenever we come across a filter node that is the child
3756 * of a set or sequence node, data->contractions is extended
3757 * with a new element that restricts the previous element
3758 * to the elements selected by the filter.
3759 * The previous element can then be reused while backtracking.
3761 static __isl_give isl_schedule_node *subtree_contraction_enter(
3762 __isl_take isl_schedule_node *node, void *user)
3764 struct isl_subtree_contraction_data *data = user;
3766 do {
3767 enum isl_schedule_node_type type;
3768 isl_union_set *filter;
3769 isl_union_pw_multi_aff *inner, *contraction;
3770 int n;
3772 switch (isl_schedule_node_get_type(node)) {
3773 case isl_schedule_node_error:
3774 return isl_schedule_node_free(node);
3775 case isl_schedule_node_filter:
3776 type = isl_schedule_node_get_parent_type(node);
3777 if (type != isl_schedule_node_set &&
3778 type != isl_schedule_node_sequence)
3779 break;
3780 filter = isl_schedule_node_filter_get_filter(node);
3781 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3782 data->contractions);
3783 inner =
3784 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3785 data->contractions, n - 1);
3786 inner = isl_union_pw_multi_aff_intersect_domain(inner,
3787 filter);
3788 data->contractions =
3789 isl_union_pw_multi_aff_list_add(data->contractions,
3790 inner);
3791 break;
3792 case isl_schedule_node_expansion:
3793 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3794 data->contractions);
3795 contraction =
3796 isl_schedule_node_expansion_get_contraction(node);
3797 inner =
3798 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3799 data->contractions, n - 1);
3800 inner =
3801 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3802 inner, contraction);
3803 data->contractions =
3804 isl_union_pw_multi_aff_list_set_union_pw_multi_aff(
3805 data->contractions, n - 1, inner);
3806 break;
3807 case isl_schedule_node_band:
3808 case isl_schedule_node_context:
3809 case isl_schedule_node_domain:
3810 case isl_schedule_node_extension:
3811 case isl_schedule_node_guard:
3812 case isl_schedule_node_leaf:
3813 case isl_schedule_node_mark:
3814 case isl_schedule_node_sequence:
3815 case isl_schedule_node_set:
3816 break;
3818 } while (isl_schedule_node_has_children(node) &&
3819 (node = isl_schedule_node_first_child(node)) != NULL);
3821 return node;
3824 /* Callback for "traverse" to leave a node for
3825 * isl_schedule_node_get_subtree_contraction.
3827 * If we come across a filter node that is the child
3828 * of a set or sequence node, then we remove the element
3829 * of data->contractions that was added in subtree_contraction_enter.
3831 * If we reach a leaf node, then the accumulated contraction is
3832 * added to data->res.
3834 static __isl_give isl_schedule_node *subtree_contraction_leave(
3835 __isl_take isl_schedule_node *node, void *user)
3837 struct isl_subtree_contraction_data *data = user;
3838 int n;
3839 isl_union_pw_multi_aff *inner;
3840 enum isl_schedule_node_type type;
3842 switch (isl_schedule_node_get_type(node)) {
3843 case isl_schedule_node_error:
3844 return isl_schedule_node_free(node);
3845 case isl_schedule_node_filter:
3846 type = isl_schedule_node_get_parent_type(node);
3847 if (type != isl_schedule_node_set &&
3848 type != isl_schedule_node_sequence)
3849 break;
3850 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3851 data->contractions);
3852 data->contractions =
3853 isl_union_pw_multi_aff_list_drop(data->contractions,
3854 n - 1, 1);
3855 break;
3856 case isl_schedule_node_leaf:
3857 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3858 data->contractions);
3859 inner = isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3860 data->contractions, n - 1);
3861 data->res = isl_union_pw_multi_aff_union_add(data->res, inner);
3862 break;
3863 case isl_schedule_node_band:
3864 case isl_schedule_node_context:
3865 case isl_schedule_node_domain:
3866 case isl_schedule_node_expansion:
3867 case isl_schedule_node_extension:
3868 case isl_schedule_node_guard:
3869 case isl_schedule_node_mark:
3870 case isl_schedule_node_sequence:
3871 case isl_schedule_node_set:
3872 break;
3875 return node;
3878 /* Return a mapping from the domain elements in the leaves of the subtree
3879 * rooted at "node" to the corresponding domain elements that reach "node"
3880 * obtained by composing the intermediate contractions.
3882 * We start out with an identity mapping between the domain elements
3883 * that reach "node" and compose it with all the contractions
3884 * on a path from "node" to a leaf while traversing the subtree.
3885 * Within the children of an a sequence or set node, the
3886 * accumulated contraction is restricted to the elements selected
3887 * by the filter child.
3889 __isl_give isl_union_pw_multi_aff *isl_schedule_node_get_subtree_contraction(
3890 __isl_keep isl_schedule_node *node)
3892 struct isl_subtree_contraction_data data;
3893 isl_space *space;
3894 isl_union_set *domain;
3895 isl_union_pw_multi_aff *contraction;
3897 if (!node)
3898 return NULL;
3900 domain = isl_schedule_node_get_universe_domain(node);
3901 space = isl_union_set_get_space(domain);
3902 contraction = isl_union_set_identity_union_pw_multi_aff(domain);
3903 data.res = isl_union_pw_multi_aff_empty(space);
3904 data.contractions =
3905 isl_union_pw_multi_aff_list_from_union_pw_multi_aff(contraction);
3907 node = isl_schedule_node_copy(node);
3908 node = traverse(node, &subtree_contraction_enter,
3909 &subtree_contraction_leave, &data);
3910 if (!node)
3911 data.res = isl_union_pw_multi_aff_free(data.res);
3912 isl_schedule_node_free(node);
3914 isl_union_pw_multi_aff_list_free(data.contractions);
3916 return data.res;
3919 /* Do the nearest "n" ancestors of "node" have the types given in "types"
3920 * (starting at the parent of "node")?
3922 static int has_ancestors(__isl_keep isl_schedule_node *node,
3923 int n, enum isl_schedule_node_type *types)
3925 int i, n_ancestor;
3927 if (!node)
3928 return -1;
3930 n_ancestor = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
3931 if (n_ancestor < n)
3932 return 0;
3934 for (i = 0; i < n; ++i) {
3935 isl_schedule_tree *tree;
3936 int correct_type;
3938 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
3939 n_ancestor - 1 - i);
3940 if (!tree)
3941 return -1;
3942 correct_type = isl_schedule_tree_get_type(tree) == types[i];
3943 isl_schedule_tree_free(tree);
3944 if (!correct_type)
3945 return 0;
3948 return 1;
3951 /* Given a node "node" that appears in an extension (i.e., it is the child
3952 * of a filter in a sequence inside an extension node), are the spaces
3953 * of the extension specified by "extension" disjoint from those
3954 * of both the original extension and the domain elements that reach
3955 * that original extension?
3957 static int is_disjoint_extension(__isl_keep isl_schedule_node *node,
3958 __isl_keep isl_union_map *extension)
3960 isl_union_map *old;
3961 isl_union_set *domain;
3962 int empty;
3964 node = isl_schedule_node_copy(node);
3965 node = isl_schedule_node_parent(node);
3966 node = isl_schedule_node_parent(node);
3967 node = isl_schedule_node_parent(node);
3968 old = isl_schedule_node_extension_get_extension(node);
3969 domain = isl_schedule_node_get_universe_domain(node);
3970 isl_schedule_node_free(node);
3971 old = isl_union_map_universe(old);
3972 domain = isl_union_set_union(domain, isl_union_map_range(old));
3973 extension = isl_union_map_copy(extension);
3974 extension = isl_union_map_intersect_range(extension, domain);
3975 empty = isl_union_map_is_empty(extension);
3976 isl_union_map_free(extension);
3978 return empty;
3981 /* Given a node "node" that is governed by an extension node, extend
3982 * that extension node with "extension".
3984 * In particular, "node" is the child of a filter in a sequence that
3985 * is in turn a child of an extension node. Extend that extension node
3986 * with "extension".
3988 * Return a pointer to the parent of the original node (i.e., a filter).
3990 static __isl_give isl_schedule_node *extend_extension(
3991 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
3993 int pos;
3994 int disjoint;
3995 isl_union_map *node_extension;
3997 node = isl_schedule_node_parent(node);
3998 pos = isl_schedule_node_get_child_position(node);
3999 node = isl_schedule_node_parent(node);
4000 node = isl_schedule_node_parent(node);
4001 node_extension = isl_schedule_node_extension_get_extension(node);
4002 disjoint = isl_union_map_is_disjoint(extension, node_extension);
4003 extension = isl_union_map_union(extension, node_extension);
4004 node = isl_schedule_node_extension_set_extension(node, extension);
4005 node = isl_schedule_node_child(node, 0);
4006 node = isl_schedule_node_child(node, pos);
4008 if (disjoint < 0)
4009 return isl_schedule_node_free(node);
4010 if (!node)
4011 return NULL;
4012 if (!disjoint)
4013 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4014 "extension domain should be disjoint from earlier "
4015 "extensions", return isl_schedule_node_free(node));
4017 return node;
4020 /* Return the universe of "uset" if this universe is disjoint from "ref".
4021 * Otherwise, return "uset".
4023 * Also check if "uset" itself is disjoint from "ref", reporting
4024 * an error if it is not.
4026 static __isl_give isl_union_set *replace_by_universe_if_disjoint(
4027 __isl_take isl_union_set *uset, __isl_keep isl_union_set *ref)
4029 int disjoint;
4030 isl_union_set *universe;
4032 disjoint = isl_union_set_is_disjoint(uset, ref);
4033 if (disjoint < 0)
4034 return isl_union_set_free(uset);
4035 if (!disjoint)
4036 isl_die(isl_union_set_get_ctx(uset), isl_error_invalid,
4037 "extension domain should be disjoint from "
4038 "current domain", return isl_union_set_free(uset));
4040 universe = isl_union_set_universe(isl_union_set_copy(uset));
4041 disjoint = isl_union_set_is_disjoint(universe, ref);
4042 if (disjoint >= 0 && disjoint) {
4043 isl_union_set_free(uset);
4044 return universe;
4046 isl_union_set_free(universe);
4048 if (disjoint < 0)
4049 return isl_union_set_free(uset);
4050 return uset;
4053 /* Insert an extension node on top of "node" with extension "extension".
4054 * In addition, insert a filter that separates node from the extension
4055 * between the extension node and "node".
4056 * Return a pointer to the inserted filter node.
4058 * If "node" already appears in an extension (i.e., if it is the child
4059 * of a filter in a sequence inside an extension node), then extend that
4060 * extension with "extension" instead.
4061 * In this case, a pointer to the original filter node is returned.
4062 * Note that if some of the elements in the new extension live in the
4063 * same space as those of the original extension or the domain elements
4064 * reaching the original extension, then we insert a new extension anyway.
4065 * Otherwise, we would have to adjust the filters in the sequence child
4066 * of the extension to ensure that the elements in the new extension
4067 * are filtered out.
4069 static __isl_give isl_schedule_node *insert_extension(
4070 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
4072 enum isl_schedule_node_type ancestors[] =
4073 { isl_schedule_node_filter, isl_schedule_node_sequence,
4074 isl_schedule_node_extension };
4075 isl_union_set *domain;
4076 isl_union_set *filter;
4077 int in_ext;
4079 in_ext = has_ancestors(node, 3, ancestors);
4080 if (in_ext < 0)
4081 goto error;
4082 if (in_ext) {
4083 int disjoint;
4085 disjoint = is_disjoint_extension(node, extension);
4086 if (disjoint < 0)
4087 goto error;
4088 if (disjoint)
4089 return extend_extension(node, extension);
4092 filter = isl_schedule_node_get_domain(node);
4093 domain = isl_union_map_range(isl_union_map_copy(extension));
4094 filter = replace_by_universe_if_disjoint(filter, domain);
4095 isl_union_set_free(domain);
4097 node = isl_schedule_node_insert_filter(node, filter);
4098 node = isl_schedule_node_insert_extension(node, extension);
4099 node = isl_schedule_node_child(node, 0);
4100 return node;
4101 error:
4102 isl_schedule_node_free(node);
4103 isl_union_map_free(extension);
4104 return NULL;
4107 /* Replace the subtree that "node" points to by "tree" (which has
4108 * a sequence root with two children), except if the parent of "node"
4109 * is a sequence as well, in which case "tree" is spliced at the position
4110 * of "node" in its parent.
4111 * Return a pointer to the child of the "tree_pos" (filter) child of "tree"
4112 * in the updated schedule tree.
4114 static __isl_give isl_schedule_node *graft_or_splice(
4115 __isl_take isl_schedule_node *node, __isl_take isl_schedule_tree *tree,
4116 int tree_pos)
4118 int pos;
4120 if (isl_schedule_node_get_parent_type(node) ==
4121 isl_schedule_node_sequence) {
4122 pos = isl_schedule_node_get_child_position(node);
4123 node = isl_schedule_node_parent(node);
4124 node = isl_schedule_node_sequence_splice(node, pos, tree);
4125 } else {
4126 pos = 0;
4127 node = isl_schedule_node_graft_tree(node, tree);
4129 node = isl_schedule_node_child(node, pos + tree_pos);
4130 node = isl_schedule_node_child(node, 0);
4132 return node;
4135 /* Insert a node "graft" into the schedule tree of "node" such that it
4136 * is executed before (if "before" is set) or after (if "before" is not set)
4137 * the node that "node" points to.
4138 * The root of "graft" is an extension node.
4139 * Return a pointer to the node that "node" pointed to.
4141 * We first insert an extension node on top of "node" (or extend
4142 * the extension node if there already is one), with a filter on "node"
4143 * separating it from the extension.
4144 * We then insert a filter in the graft to separate it from the original
4145 * domain elements and combine the original and new tree in a sequence.
4146 * If we have extended an extension node, then the children of this
4147 * sequence are spliced in the sequence of the extended extension
4148 * at the position where "node" appears in the original extension.
4149 * Otherwise, the sequence pair is attached to the new extension node.
4151 static __isl_give isl_schedule_node *graft_extension(
4152 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft,
4153 int before)
4155 isl_union_map *extension;
4156 isl_union_set *graft_domain;
4157 isl_union_set *node_domain;
4158 isl_schedule_tree *tree, *tree_graft;
4160 extension = isl_schedule_node_extension_get_extension(graft);
4161 graft_domain = isl_union_map_range(isl_union_map_copy(extension));
4162 node_domain = isl_schedule_node_get_universe_domain(node);
4163 node = insert_extension(node, extension);
4165 graft_domain = replace_by_universe_if_disjoint(graft_domain,
4166 node_domain);
4167 isl_union_set_free(node_domain);
4169 tree = isl_schedule_node_get_tree(node);
4170 if (!isl_schedule_node_has_children(graft)) {
4171 tree_graft = isl_schedule_tree_from_filter(graft_domain);
4172 } else {
4173 graft = isl_schedule_node_child(graft, 0);
4174 tree_graft = isl_schedule_node_get_tree(graft);
4175 tree_graft = isl_schedule_tree_insert_filter(tree_graft,
4176 graft_domain);
4178 if (before)
4179 tree = isl_schedule_tree_sequence_pair(tree_graft, tree);
4180 else
4181 tree = isl_schedule_tree_sequence_pair(tree, tree_graft);
4182 node = graft_or_splice(node, tree, before);
4184 isl_schedule_node_free(graft);
4186 return node;
4189 /* Replace the root domain node of "node" by an extension node suitable
4190 * for insertion at "pos".
4191 * That is, create an extension node that maps the outer band nodes
4192 * at "pos" to the domain of the root node of "node" and attach
4193 * the child of this root node to the extension node.
4195 static __isl_give isl_schedule_node *extension_from_domain(
4196 __isl_take isl_schedule_node *node, __isl_keep isl_schedule_node *pos)
4198 isl_union_set *universe;
4199 isl_union_set *domain;
4200 isl_union_map *ext;
4201 int depth;
4202 int anchored;
4203 isl_space *space;
4204 isl_schedule_node *res;
4205 isl_schedule_tree *tree;
4207 anchored = isl_schedule_node_is_subtree_anchored(node);
4208 if (anchored < 0)
4209 return isl_schedule_node_free(node);
4210 if (anchored)
4211 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
4212 "cannot graft anchored tree with domain root",
4213 return isl_schedule_node_free(node));
4215 depth = isl_schedule_node_get_schedule_depth(pos);
4216 domain = isl_schedule_node_domain_get_domain(node);
4217 space = isl_union_set_get_space(domain);
4218 space = isl_space_set_from_params(space);
4219 space = isl_space_add_dims(space, isl_dim_set, depth);
4220 universe = isl_union_set_from_set(isl_set_universe(space));
4221 ext = isl_union_map_from_domain_and_range(universe, domain);
4222 res = isl_schedule_node_from_extension(ext);
4223 node = isl_schedule_node_child(node, 0);
4224 if (!node)
4225 return isl_schedule_node_free(res);
4226 if (!isl_schedule_tree_is_leaf(node->tree)) {
4227 tree = isl_schedule_node_get_tree(node);
4228 res = isl_schedule_node_child(res, 0);
4229 res = isl_schedule_node_graft_tree(res, tree);
4230 res = isl_schedule_node_parent(res);
4232 isl_schedule_node_free(node);
4234 return res;
4237 /* Insert a node "graft" into the schedule tree of "node" such that it
4238 * is executed before (if "before" is set) or after (if "before" is not set)
4239 * the node that "node" points to.
4240 * The root of "graft" may be either a domain or an extension node.
4241 * In the latter case, the domain of the extension needs to correspond
4242 * to the outer band nodes of "node".
4243 * The elements of the domain or the range of the extension may not
4244 * intersect with the domain elements that reach "node".
4245 * The schedule tree of "graft" may not be anchored.
4247 * The schedule tree of "node" is modified to include an extension node
4248 * corresponding to the root node of "graft" as a child of the original
4249 * parent of "node". The original node that "node" points to and the
4250 * child of the root node of "graft" are attached to this extension node
4251 * through a sequence, with appropriate filters and with the child
4252 * of "graft" appearing before or after the original "node".
4254 * If "node" already appears inside a sequence that is the child of
4255 * an extension node and if the spaces of the new domain elements
4256 * do not overlap with those of the original domain elements,
4257 * then that extension node is extended with the new extension
4258 * rather than introducing a new segment of extension and sequence nodes.
4260 * Return a pointer to the same node in the modified tree that
4261 * "node" pointed to in the original tree.
4263 static __isl_give isl_schedule_node *isl_schedule_node_graft_before_or_after(
4264 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft,
4265 int before)
4267 if (!node || !graft)
4268 goto error;
4269 if (check_insert(node) < 0)
4270 goto error;
4272 if (isl_schedule_node_get_type(graft) == isl_schedule_node_domain)
4273 graft = extension_from_domain(graft, node);
4275 if (isl_schedule_node_get_type(graft) != isl_schedule_node_extension)
4276 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4277 "expecting domain or extension as root of graft",
4278 goto error);
4280 return graft_extension(node, graft, before);
4281 error:
4282 isl_schedule_node_free(node);
4283 isl_schedule_node_free(graft);
4284 return NULL;
4287 /* Insert a node "graft" into the schedule tree of "node" such that it
4288 * is executed before the node that "node" points to.
4289 * The root of "graft" may be either a domain or an extension node.
4290 * In the latter case, the domain of the extension needs to correspond
4291 * to the outer band nodes of "node".
4292 * The elements of the domain or the range of the extension may not
4293 * intersect with the domain elements that reach "node".
4294 * The schedule tree of "graft" may not be anchored.
4296 * Return a pointer to the same node in the modified tree that
4297 * "node" pointed to in the original tree.
4299 __isl_give isl_schedule_node *isl_schedule_node_graft_before(
4300 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft)
4302 return isl_schedule_node_graft_before_or_after(node, graft, 1);
4305 /* Insert a node "graft" into the schedule tree of "node" such that it
4306 * is executed after the node that "node" points to.
4307 * The root of "graft" may be either a domain or an extension node.
4308 * In the latter case, the domain of the extension needs to correspond
4309 * to the outer band nodes of "node".
4310 * The elements of the domain or the range of the extension may not
4311 * intersect with the domain elements that reach "node".
4312 * The schedule tree of "graft" may not be anchored.
4314 * Return a pointer to the same node in the modified tree that
4315 * "node" pointed to in the original tree.
4317 __isl_give isl_schedule_node *isl_schedule_node_graft_after(
4318 __isl_take isl_schedule_node *node,
4319 __isl_take isl_schedule_node *graft)
4321 return isl_schedule_node_graft_before_or_after(node, graft, 0);
4324 /* Split the domain elements that reach "node" into those that satisfy
4325 * "filter" and those that do not. Arrange for the first subset to be
4326 * executed before or after the second subset, depending on the value
4327 * of "before".
4328 * Return a pointer to the tree corresponding to the second subset,
4329 * except when this subset is empty in which case the original pointer
4330 * is returned.
4331 * If both subsets are non-empty, then a sequence node is introduced
4332 * to impose the order. If the grandparent of the original node was
4333 * itself a sequence, then the original child is replaced by two children
4334 * in this sequence instead.
4335 * The children in the sequence are copies of the original subtree,
4336 * simplified with respect to their filters.
4338 static __isl_give isl_schedule_node *isl_schedule_node_order_before_or_after(
4339 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter,
4340 int before)
4342 enum isl_schedule_node_type ancestors[] =
4343 { isl_schedule_node_filter, isl_schedule_node_sequence };
4344 isl_union_set *node_domain, *node_filter = NULL, *parent_filter;
4345 isl_schedule_node *node2;
4346 isl_schedule_tree *tree1, *tree2;
4347 int empty1, empty2;
4348 int in_seq;
4350 if (!node || !filter)
4351 goto error;
4352 if (check_insert(node) < 0)
4353 goto error;
4355 in_seq = has_ancestors(node, 2, ancestors);
4356 if (in_seq < 0)
4357 goto error;
4358 node_domain = isl_schedule_node_get_domain(node);
4359 filter = isl_union_set_gist(filter, isl_union_set_copy(node_domain));
4360 node_filter = isl_union_set_copy(node_domain);
4361 node_filter = isl_union_set_subtract(node_filter,
4362 isl_union_set_copy(filter));
4363 node_filter = isl_union_set_gist(node_filter, node_domain);
4364 empty1 = isl_union_set_is_empty(filter);
4365 empty2 = isl_union_set_is_empty(node_filter);
4366 if (empty1 < 0 || empty2 < 0)
4367 goto error;
4368 if (empty1 || empty2) {
4369 isl_union_set_free(filter);
4370 isl_union_set_free(node_filter);
4371 return node;
4374 if (in_seq) {
4375 node = isl_schedule_node_parent(node);
4376 parent_filter = isl_schedule_node_filter_get_filter(node);
4377 node_filter = isl_union_set_intersect(node_filter,
4378 isl_union_set_copy(parent_filter));
4379 filter = isl_union_set_intersect(filter, parent_filter);
4382 node2 = isl_schedule_node_copy(node);
4383 node = isl_schedule_node_gist(node, isl_union_set_copy(node_filter));
4384 node2 = isl_schedule_node_gist(node2, isl_union_set_copy(filter));
4385 tree1 = isl_schedule_node_get_tree(node);
4386 tree2 = isl_schedule_node_get_tree(node2);
4387 tree1 = isl_schedule_tree_insert_filter(tree1, node_filter);
4388 tree2 = isl_schedule_tree_insert_filter(tree2, filter);
4389 isl_schedule_node_free(node2);
4391 if (before) {
4392 tree1 = isl_schedule_tree_sequence_pair(tree2, tree1);
4393 node = graft_or_splice(node, tree1, 1);
4394 } else {
4395 tree1 = isl_schedule_tree_sequence_pair(tree1, tree2);
4396 node = graft_or_splice(node, tree1, 0);
4399 return node;
4400 error:
4401 isl_schedule_node_free(node);
4402 isl_union_set_free(filter);
4403 isl_union_set_free(node_filter);
4404 return NULL;
4407 /* Split the domain elements that reach "node" into those that satisfy
4408 * "filter" and those that do not. Arrange for the first subset to be
4409 * executed before the second subset.
4410 * Return a pointer to the tree corresponding to the second subset,
4411 * except when this subset is empty in which case the original pointer
4412 * is returned.
4414 __isl_give isl_schedule_node *isl_schedule_node_order_before(
4415 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
4417 return isl_schedule_node_order_before_or_after(node, filter, 1);
4420 /* Split the domain elements that reach "node" into those that satisfy
4421 * "filter" and those that do not. Arrange for the first subset to be
4422 * executed after the second subset.
4423 * Return a pointer to the tree corresponding to the second subset,
4424 * except when this subset is empty in which case the original pointer
4425 * is returned.
4427 __isl_give isl_schedule_node *isl_schedule_node_order_after(
4428 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
4430 return isl_schedule_node_order_before_or_after(node, filter, 0);
4433 /* Reset the user pointer on all identifiers of parameters and tuples
4434 * in the schedule node "node".
4436 __isl_give isl_schedule_node *isl_schedule_node_reset_user(
4437 __isl_take isl_schedule_node *node)
4439 isl_schedule_tree *tree;
4441 tree = isl_schedule_node_get_tree(node);
4442 tree = isl_schedule_tree_reset_user(tree);
4443 node = isl_schedule_node_graft_tree(node, tree);
4445 return node;
4448 /* Align the parameters of the schedule node "node" to those of "space".
4450 __isl_give isl_schedule_node *isl_schedule_node_align_params(
4451 __isl_take isl_schedule_node *node, __isl_take isl_space *space)
4453 isl_schedule_tree *tree;
4455 tree = isl_schedule_node_get_tree(node);
4456 tree = isl_schedule_tree_align_params(tree, space);
4457 node = isl_schedule_node_graft_tree(node, tree);
4459 return node;
4462 /* Compute the pullback of schedule node "node"
4463 * by the function represented by "upma".
4464 * In other words, plug in "upma" in the iteration domains
4465 * of schedule node "node".
4466 * We currently do not handle expansion nodes.
4468 * Note that this is only a helper function for
4469 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
4470 * this function should not be called on a single node without also
4471 * calling it on all the other nodes.
4473 __isl_give isl_schedule_node *isl_schedule_node_pullback_union_pw_multi_aff(
4474 __isl_take isl_schedule_node *node,
4475 __isl_take isl_union_pw_multi_aff *upma)
4477 isl_schedule_tree *tree;
4479 tree = isl_schedule_node_get_tree(node);
4480 tree = isl_schedule_tree_pullback_union_pw_multi_aff(tree, upma);
4481 node = isl_schedule_node_graft_tree(node, tree);
4483 return node;
4486 /* Internal data structure for isl_schedule_node_expand.
4487 * "tree" is the tree that needs to be plugged in in all the leaves.
4488 * "domain" is the set of domain elements in the original leaves
4489 * to which the tree applies.
4491 struct isl_schedule_expand_data {
4492 isl_schedule_tree *tree;
4493 isl_union_set *domain;
4496 /* If "node" is a leaf, then plug in data->tree, simplifying it
4497 * within its new context.
4499 * If there are any domain elements at the leaf where the tree
4500 * should not be plugged in (i.e., there are elements not in data->domain)
4501 * then first extend the tree to only apply to the elements in data->domain
4502 * by constructing a set node that selects data->tree for elements
4503 * in data->domain and a leaf for the other elements.
4505 static __isl_give isl_schedule_node *expand(__isl_take isl_schedule_node *node,
4506 void *user)
4508 struct isl_schedule_expand_data *data = user;
4509 isl_schedule_tree *tree, *leaf;
4510 isl_union_set *domain, *left;
4511 isl_bool empty;
4513 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
4514 return node;
4516 domain = isl_schedule_node_get_domain(node);
4517 tree = isl_schedule_tree_copy(data->tree);
4519 left = isl_union_set_copy(domain);
4520 left = isl_union_set_subtract(left, isl_union_set_copy(data->domain));
4521 empty = isl_union_set_is_empty(left);
4522 if (empty >= 0 && !empty) {
4523 leaf = isl_schedule_node_get_leaf(node);
4524 leaf = isl_schedule_tree_insert_filter(leaf, left);
4525 left = isl_union_set_copy(data->domain);
4526 tree = isl_schedule_tree_insert_filter(tree, left);
4527 tree = isl_schedule_tree_set_pair(tree, leaf);
4528 } else {
4529 if (empty < 0)
4530 node = isl_schedule_node_free(node);
4531 isl_union_set_free(left);
4534 node = isl_schedule_node_graft_tree(node, tree);
4535 node = isl_schedule_node_gist(node, domain);
4537 return node;
4540 /* Expand the tree rooted at "node" by extending all leaves
4541 * with an expansion node with as child "tree".
4542 * The expansion is determined by "contraction" and "domain".
4543 * That is, the elements of "domain" are contracted according
4544 * to "contraction". The expansion relation is then the inverse
4545 * of "contraction" with its range intersected with "domain".
4547 * Insert the appropriate expansion node on top of "tree" and
4548 * then plug in the result in all leaves of "node".
4550 __isl_give isl_schedule_node *isl_schedule_node_expand(
4551 __isl_take isl_schedule_node *node,
4552 __isl_take isl_union_pw_multi_aff *contraction,
4553 __isl_take isl_union_set *domain,
4554 __isl_take isl_schedule_tree *tree)
4556 struct isl_schedule_expand_data data;
4557 isl_union_map *expansion;
4558 isl_union_pw_multi_aff *copy;
4560 if (!node || !contraction || !tree)
4561 node = isl_schedule_node_free(node);
4563 copy = isl_union_pw_multi_aff_copy(contraction);
4564 expansion = isl_union_map_from_union_pw_multi_aff(copy);
4565 expansion = isl_union_map_reverse(expansion);
4566 expansion = isl_union_map_intersect_range(expansion, domain);
4567 data.domain = isl_union_map_domain(isl_union_map_copy(expansion));
4569 tree = isl_schedule_tree_insert_expansion(tree, contraction, expansion);
4570 data.tree = tree;
4572 node = isl_schedule_node_map_descendant_bottom_up(node, &expand, &data);
4573 isl_union_set_free(data.domain);
4574 isl_schedule_tree_free(data.tree);
4575 return node;
4578 /* Return the position of the subtree containing "node" among the children
4579 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
4580 * In particular, both nodes should point to the same schedule tree.
4582 * Return -1 on error.
4584 int isl_schedule_node_get_ancestor_child_position(
4585 __isl_keep isl_schedule_node *node,
4586 __isl_keep isl_schedule_node *ancestor)
4588 int n1, n2;
4589 isl_schedule_tree *tree;
4591 if (!node || !ancestor)
4592 return -1;
4594 if (node->schedule != ancestor->schedule)
4595 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4596 "not a descendant", return -1);
4598 n1 = isl_schedule_node_get_tree_depth(ancestor);
4599 n2 = isl_schedule_node_get_tree_depth(node);
4601 if (n1 >= n2)
4602 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4603 "not a descendant", return -1);
4604 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n1);
4605 isl_schedule_tree_free(tree);
4606 if (tree != ancestor->tree)
4607 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4608 "not a descendant", return -1);
4610 return node->child_pos[n1];
4613 /* Given two nodes that point to the same schedule tree, return their
4614 * closest shared ancestor.
4616 * Since the two nodes point to the same schedule, they share at least
4617 * one ancestor, the root of the schedule. We move down from the root
4618 * to the first ancestor where the respective children have a different
4619 * child position. This is the requested ancestor.
4620 * If there is no ancestor where the children have a different position,
4621 * then one node is an ancestor of the other and then this node is
4622 * the requested ancestor.
4624 __isl_give isl_schedule_node *isl_schedule_node_get_shared_ancestor(
4625 __isl_keep isl_schedule_node *node1,
4626 __isl_keep isl_schedule_node *node2)
4628 int i, n1, n2;
4630 if (!node1 || !node2)
4631 return NULL;
4632 if (node1->schedule != node2->schedule)
4633 isl_die(isl_schedule_node_get_ctx(node1), isl_error_invalid,
4634 "not part of same schedule", return NULL);
4635 n1 = isl_schedule_node_get_tree_depth(node1);
4636 n2 = isl_schedule_node_get_tree_depth(node2);
4637 if (n2 < n1)
4638 return isl_schedule_node_get_shared_ancestor(node2, node1);
4639 if (n1 == 0)
4640 return isl_schedule_node_copy(node1);
4641 if (isl_schedule_node_is_equal(node1, node2))
4642 return isl_schedule_node_copy(node1);
4644 for (i = 0; i < n1; ++i)
4645 if (node1->child_pos[i] != node2->child_pos[i])
4646 break;
4648 node1 = isl_schedule_node_copy(node1);
4649 return isl_schedule_node_ancestor(node1, n1 - i);
4652 /* Print "node" to "p".
4654 __isl_give isl_printer *isl_printer_print_schedule_node(
4655 __isl_take isl_printer *p, __isl_keep isl_schedule_node *node)
4657 if (!node)
4658 return isl_printer_free(p);
4659 return isl_printer_print_schedule_tree_mark(p, node->schedule->root,
4660 isl_schedule_tree_list_n_schedule_tree(node->ancestors),
4661 node->child_pos);
4664 void isl_schedule_node_dump(__isl_keep isl_schedule_node *node)
4666 isl_ctx *ctx;
4667 isl_printer *printer;
4669 if (!node)
4670 return;
4672 ctx = isl_schedule_node_get_ctx(node);
4673 printer = isl_printer_to_file(ctx, stderr);
4674 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
4675 printer = isl_printer_print_schedule_node(printer, node);
4677 isl_printer_free(printer);
4680 /* Return a string representation of "node".
4681 * Print the schedule node in block format as it would otherwise
4682 * look identical to the entire schedule.
4684 __isl_give char *isl_schedule_node_to_str(__isl_keep isl_schedule_node *node)
4686 isl_printer *printer;
4687 char *s;
4689 if (!node)
4690 return NULL;
4692 printer = isl_printer_to_str(isl_schedule_node_get_ctx(node));
4693 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
4694 printer = isl_printer_print_schedule_node(printer, node);
4695 s = isl_printer_get_str(printer);
4696 isl_printer_free(printer);
4698 return s;