isl_bernstein.c: add_fold: return isl_stat
[isl.git] / isl_schedule_node.c
blob4c5c1cfa1208adbdf3f42829d7dc2ec8d0a77034
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/id.h>
15 #include <isl/val.h>
16 #include <isl/space.h>
17 #include <isl/set.h>
18 #include <isl_schedule_band.h>
19 #include <isl_schedule_private.h>
20 #include <isl_schedule_node_private.h>
22 /* Create a new schedule node in the given schedule, point at the given
23 * tree with given ancestors and child positions.
24 * "child_pos" may be NULL if there are no ancestors.
26 __isl_give isl_schedule_node *isl_schedule_node_alloc(
27 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree,
28 __isl_take isl_schedule_tree_list *ancestors, int *child_pos)
30 isl_ctx *ctx;
31 isl_schedule_node *node;
32 int i, n;
34 if (!schedule || !tree || !ancestors)
35 goto error;
36 n = isl_schedule_tree_list_n_schedule_tree(ancestors);
37 if (n > 0 && !child_pos)
38 goto error;
39 ctx = isl_schedule_get_ctx(schedule);
40 node = isl_calloc_type(ctx, isl_schedule_node);
41 if (!node)
42 goto error;
43 node->ref = 1;
44 node->schedule = schedule;
45 node->tree = tree;
46 node->ancestors = ancestors;
47 node->child_pos = isl_alloc_array(ctx, int, n);
48 if (n && !node->child_pos)
49 return isl_schedule_node_free(node);
50 for (i = 0; i < n; ++i)
51 node->child_pos[i] = child_pos[i];
53 return node;
54 error:
55 isl_schedule_free(schedule);
56 isl_schedule_tree_free(tree);
57 isl_schedule_tree_list_free(ancestors);
58 return NULL;
61 /* Return a pointer to the root of a schedule tree with as single
62 * node a domain node with the given domain.
64 __isl_give isl_schedule_node *isl_schedule_node_from_domain(
65 __isl_take isl_union_set *domain)
67 isl_schedule *schedule;
68 isl_schedule_node *node;
70 schedule = isl_schedule_from_domain(domain);
71 node = isl_schedule_get_root(schedule);
72 isl_schedule_free(schedule);
74 return node;
77 /* Return a pointer to the root of a schedule tree with as single
78 * node a extension node with the given extension.
80 __isl_give isl_schedule_node *isl_schedule_node_from_extension(
81 __isl_take isl_union_map *extension)
83 isl_ctx *ctx;
84 isl_schedule *schedule;
85 isl_schedule_tree *tree;
86 isl_schedule_node *node;
88 if (!extension)
89 return NULL;
91 ctx = isl_union_map_get_ctx(extension);
92 tree = isl_schedule_tree_from_extension(extension);
93 schedule = isl_schedule_from_schedule_tree(ctx, tree);
94 node = isl_schedule_get_root(schedule);
95 isl_schedule_free(schedule);
97 return node;
100 /* Return the isl_ctx to which "node" belongs.
102 isl_ctx *isl_schedule_node_get_ctx(__isl_keep isl_schedule_node *node)
104 return node ? isl_schedule_get_ctx(node->schedule) : NULL;
107 /* Return a pointer to the leaf of the schedule into which "node" points.
109 __isl_keep isl_schedule_tree *isl_schedule_node_peek_leaf(
110 __isl_keep isl_schedule_node *node)
112 return node ? isl_schedule_peek_leaf(node->schedule) : NULL;
115 /* Return a copy of the leaf of the schedule into which "node" points.
117 __isl_give isl_schedule_tree *isl_schedule_node_get_leaf(
118 __isl_keep isl_schedule_node *node)
120 return isl_schedule_tree_copy(isl_schedule_node_peek_leaf(node));
123 /* Return the type of the node or isl_schedule_node_error on error.
125 enum isl_schedule_node_type isl_schedule_node_get_type(
126 __isl_keep isl_schedule_node *node)
128 return node ? isl_schedule_tree_get_type(node->tree)
129 : isl_schedule_node_error;
132 /* Return the type of the parent of "node" or isl_schedule_node_error on error.
134 enum isl_schedule_node_type isl_schedule_node_get_parent_type(
135 __isl_keep isl_schedule_node *node)
137 int pos;
138 int has_parent;
139 isl_schedule_tree *parent;
140 enum isl_schedule_node_type type;
142 if (!node)
143 return isl_schedule_node_error;
144 has_parent = isl_schedule_node_has_parent(node);
145 if (has_parent < 0)
146 return isl_schedule_node_error;
147 if (!has_parent)
148 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
149 "node has no parent", return isl_schedule_node_error);
151 pos = isl_schedule_tree_list_n_schedule_tree(node->ancestors) - 1;
152 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors, pos);
153 type = isl_schedule_tree_get_type(parent);
154 isl_schedule_tree_free(parent);
156 return type;
159 /* Return a copy of the subtree that this node points to.
161 __isl_give isl_schedule_tree *isl_schedule_node_get_tree(
162 __isl_keep isl_schedule_node *node)
164 if (!node)
165 return NULL;
167 return isl_schedule_tree_copy(node->tree);
170 /* Return a copy of the schedule into which "node" points.
172 __isl_give isl_schedule *isl_schedule_node_get_schedule(
173 __isl_keep isl_schedule_node *node)
175 if (!node)
176 return NULL;
177 return isl_schedule_copy(node->schedule);
180 /* Return a fresh copy of "node".
182 __isl_take isl_schedule_node *isl_schedule_node_dup(
183 __isl_keep isl_schedule_node *node)
185 if (!node)
186 return NULL;
188 return isl_schedule_node_alloc(isl_schedule_copy(node->schedule),
189 isl_schedule_tree_copy(node->tree),
190 isl_schedule_tree_list_copy(node->ancestors),
191 node->child_pos);
194 /* Return an isl_schedule_node that is equal to "node" and that has only
195 * a single reference.
197 __isl_give isl_schedule_node *isl_schedule_node_cow(
198 __isl_take isl_schedule_node *node)
200 if (!node)
201 return NULL;
203 if (node->ref == 1)
204 return node;
205 node->ref--;
206 return isl_schedule_node_dup(node);
209 /* Return a new reference to "node".
211 __isl_give isl_schedule_node *isl_schedule_node_copy(
212 __isl_keep isl_schedule_node *node)
214 if (!node)
215 return NULL;
217 node->ref++;
218 return node;
221 /* Free "node" and return NULL.
223 __isl_null isl_schedule_node *isl_schedule_node_free(
224 __isl_take isl_schedule_node *node)
226 if (!node)
227 return NULL;
228 if (--node->ref > 0)
229 return NULL;
231 isl_schedule_tree_list_free(node->ancestors);
232 free(node->child_pos);
233 isl_schedule_tree_free(node->tree);
234 isl_schedule_free(node->schedule);
235 free(node);
237 return NULL;
240 /* Do "node1" and "node2" point to the same position in the same
241 * schedule?
243 isl_bool isl_schedule_node_is_equal(__isl_keep isl_schedule_node *node1,
244 __isl_keep isl_schedule_node *node2)
246 int i, n1, n2;
248 if (!node1 || !node2)
249 return isl_bool_error;
250 if (node1 == node2)
251 return isl_bool_true;
252 if (node1->schedule != node2->schedule)
253 return isl_bool_false;
255 n1 = isl_schedule_node_get_tree_depth(node1);
256 n2 = isl_schedule_node_get_tree_depth(node2);
257 if (n1 != n2)
258 return isl_bool_false;
259 for (i = 0; i < n1; ++i)
260 if (node1->child_pos[i] != node2->child_pos[i])
261 return isl_bool_false;
263 return isl_bool_true;
266 /* Return the number of outer schedule dimensions of "node"
267 * in its schedule tree.
269 * Return -1 on error.
271 int isl_schedule_node_get_schedule_depth(__isl_keep isl_schedule_node *node)
273 int i, n;
274 int depth = 0;
276 if (!node)
277 return -1;
279 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
280 for (i = n - 1; i >= 0; --i) {
281 isl_schedule_tree *tree;
283 tree = isl_schedule_tree_list_get_schedule_tree(
284 node->ancestors, i);
285 if (!tree)
286 return -1;
287 if (tree->type == isl_schedule_node_band)
288 depth += isl_schedule_tree_band_n_member(tree);
289 isl_schedule_tree_free(tree);
292 return depth;
295 /* Internal data structure for
296 * isl_schedule_node_get_prefix_schedule_union_pw_multi_aff
298 * "initialized" is set if the filter field has been initialized.
299 * If "universe_domain" is not set, then the collected filter is intersected
300 * with the domain of the root domain node.
301 * "universe_filter" is set if we are only collecting the universes of filters
302 * "collect_prefix" is set if we are collecting prefixes.
303 * "filter" collects all outer filters and is NULL until "initialized" is set.
304 * "prefix" collects all outer band partial schedules (if "collect_prefix"
305 * is set). If it is used, then it is initialized by the caller
306 * of collect_filter_prefix to a zero-dimensional function.
308 struct isl_schedule_node_get_filter_prefix_data {
309 int initialized;
310 int universe_domain;
311 int universe_filter;
312 int collect_prefix;
313 isl_union_set *filter;
314 isl_multi_union_pw_aff *prefix;
317 static isl_stat collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
318 int n, struct isl_schedule_node_get_filter_prefix_data *data);
320 /* Update the filter and prefix information in "data" based on the first "n"
321 * elements in "list" and the expansion tree root "tree".
323 * We first collect the information from the elements in "list",
324 * initializing the filter based on the domain of the expansion.
325 * Then we map the results to the expanded space and combined them
326 * with the results already in "data".
328 static isl_stat collect_filter_prefix_expansion(
329 __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 isl_stat_ok;
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 isl_stat collect_universe_domain_extension(
394 __isl_take isl_schedule_tree *tree,
395 __isl_keep isl_schedule_tree_list *list, int n,
396 struct isl_schedule_node_get_filter_prefix_data *data)
398 struct isl_schedule_node_get_filter_prefix_data data_outer;
399 isl_union_map *extension;
400 isl_union_set *filter;
402 data_outer.initialized = 0;
403 data_outer.universe_domain = 1;
404 data_outer.universe_filter = data->universe_filter;
405 data_outer.collect_prefix = 0;
406 data_outer.filter = NULL;
407 data_outer.prefix = NULL;
409 if (collect_filter_prefix(list, n, &data_outer) < 0)
410 data_outer.filter = isl_union_set_free(data_outer.filter);
412 extension = isl_schedule_tree_extension_get_extension(tree);
413 extension = isl_union_map_universe(extension);
414 filter = isl_union_map_range(extension);
415 if (data_outer.initialized)
416 filter = isl_union_set_union(filter, data_outer.filter);
417 if (data->initialized)
418 filter = isl_union_set_intersect(filter, data->filter);
420 data->filter = filter;
422 isl_schedule_tree_free(tree);
424 return isl_stat_ok;
427 /* Update "data" based on the tree node "tree" in case "data" has
428 * not been initialized yet.
430 * Return 0 on success and -1 on error.
432 * If "tree" is a filter, then we set data->filter to this filter
433 * (or its universe).
434 * If "tree" is a domain, then this means we have reached the root
435 * of the schedule tree without being able to extract any information.
436 * We therefore initialize data->filter to the universe of the domain,
437 * or the domain itself if data->universe_domain is not set.
438 * If "tree" is a band with at least one member, then we set data->filter
439 * to the universe of the schedule domain and replace the zero-dimensional
440 * data->prefix by the band schedule (if data->collect_prefix is set).
442 static isl_stat collect_filter_prefix_init(__isl_keep isl_schedule_tree *tree,
443 struct isl_schedule_node_get_filter_prefix_data *data)
445 enum isl_schedule_node_type type;
446 isl_multi_union_pw_aff *mupa;
447 isl_union_set *filter;
449 type = isl_schedule_tree_get_type(tree);
450 switch (type) {
451 case isl_schedule_node_error:
452 return isl_stat_error;
453 case isl_schedule_node_expansion:
454 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
455 "should be handled by caller", return isl_stat_error);
456 case isl_schedule_node_extension:
457 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
458 "cannot handle extension nodes", return isl_stat_error);
459 case isl_schedule_node_context:
460 case isl_schedule_node_leaf:
461 case isl_schedule_node_guard:
462 case isl_schedule_node_mark:
463 case isl_schedule_node_sequence:
464 case isl_schedule_node_set:
465 return isl_stat_ok;
466 case isl_schedule_node_domain:
467 filter = isl_schedule_tree_domain_get_domain(tree);
468 if (data->universe_domain)
469 filter = isl_union_set_universe(filter);
470 data->filter = filter;
471 break;
472 case isl_schedule_node_band:
473 if (isl_schedule_tree_band_n_member(tree) == 0)
474 return isl_stat_ok;
475 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
476 if (data->collect_prefix) {
477 isl_multi_union_pw_aff_free(data->prefix);
478 mupa = isl_multi_union_pw_aff_reset_tuple_id(mupa,
479 isl_dim_set);
480 data->prefix = isl_multi_union_pw_aff_copy(mupa);
482 filter = isl_multi_union_pw_aff_domain(mupa);
483 filter = isl_union_set_universe(filter);
484 data->filter = filter;
485 break;
486 case isl_schedule_node_filter:
487 filter = isl_schedule_tree_filter_get_filter(tree);
488 if (data->universe_filter)
489 filter = isl_union_set_universe(filter);
490 data->filter = filter;
491 break;
494 if ((data->collect_prefix && !data->prefix) || !data->filter)
495 return isl_stat_error;
497 data->initialized = 1;
499 return isl_stat_ok;
502 /* Update "data" based on the tree node "tree" in case "data" has
503 * already been initialized.
505 * Return 0 on success and -1 on error.
507 * If "tree" is a domain and data->universe_domain is not set, then
508 * intersect data->filter with the domain.
509 * If "tree" is a filter, then we intersect data->filter with this filter
510 * (or its universe).
511 * If "tree" is a band with at least one member and data->collect_prefix
512 * is set, then we extend data->prefix with the band schedule.
513 * If "tree" is an extension, then we make sure that we are not collecting
514 * information on any extended domain elements.
516 static isl_stat collect_filter_prefix_update(__isl_keep isl_schedule_tree *tree,
517 struct isl_schedule_node_get_filter_prefix_data *data)
519 enum isl_schedule_node_type type;
520 isl_multi_union_pw_aff *mupa;
521 isl_union_set *filter;
522 isl_union_map *extension;
523 isl_bool empty;
525 type = isl_schedule_tree_get_type(tree);
526 switch (type) {
527 case isl_schedule_node_error:
528 return isl_stat_error;
529 case isl_schedule_node_expansion:
530 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
531 "should be handled by caller", return isl_stat_error);
532 case isl_schedule_node_extension:
533 extension = isl_schedule_tree_extension_get_extension(tree);
534 extension = isl_union_map_intersect_range(extension,
535 isl_union_set_copy(data->filter));
536 empty = isl_union_map_is_empty(extension);
537 isl_union_map_free(extension);
538 if (empty < 0)
539 return isl_stat_error;
540 if (empty)
541 break;
542 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_invalid,
543 "cannot handle extension nodes", return isl_stat_error);
544 case isl_schedule_node_context:
545 case isl_schedule_node_leaf:
546 case isl_schedule_node_guard:
547 case isl_schedule_node_mark:
548 case isl_schedule_node_sequence:
549 case isl_schedule_node_set:
550 break;
551 case isl_schedule_node_domain:
552 if (data->universe_domain)
553 break;
554 filter = isl_schedule_tree_domain_get_domain(tree);
555 data->filter = isl_union_set_intersect(data->filter, filter);
556 break;
557 case isl_schedule_node_band:
558 if (isl_schedule_tree_band_n_member(tree) == 0)
559 break;
560 if (!data->collect_prefix)
561 break;
562 mupa = isl_schedule_tree_band_get_partial_schedule(tree);
563 data->prefix = isl_multi_union_pw_aff_flat_range_product(mupa,
564 data->prefix);
565 if (!data->prefix)
566 return isl_stat_error;
567 break;
568 case isl_schedule_node_filter:
569 filter = isl_schedule_tree_filter_get_filter(tree);
570 if (data->universe_filter)
571 filter = isl_union_set_universe(filter);
572 data->filter = isl_union_set_intersect(data->filter, filter);
573 if (!data->filter)
574 return isl_stat_error;
575 break;
578 return isl_stat_ok;
581 /* Collect filter and/or prefix information from the first "n"
582 * elements in "list" (which represent the ancestors of a node).
583 * Store the results in "data".
585 * Extension nodes are only supported if they do not affect the outcome,
586 * i.e., if we are collecting information on non-extended domain elements,
587 * or if we are collecting the universe domain (without prefix).
589 * Return 0 on success and -1 on error.
591 * We traverse the list from innermost ancestor (last element)
592 * to outermost ancestor (first element), calling collect_filter_prefix_init
593 * on each node as long as we have not been able to extract any information
594 * yet and collect_filter_prefix_update afterwards.
595 * If we come across an expansion node, then we interrupt the traversal
596 * and call collect_filter_prefix_expansion to restart the traversal
597 * over the remaining ancestors and to combine the results with those
598 * that have already been collected.
599 * If we come across an extension node and we are only computing
600 * the universe domain, then we interrupt the traversal and call
601 * collect_universe_domain_extension to restart the traversal
602 * over the remaining ancestors and to combine the results with those
603 * that have already been collected.
604 * On successful return, data->initialized will be set since the outermost
605 * ancestor is a domain node, which always results in an initialization.
607 static isl_stat collect_filter_prefix(__isl_keep isl_schedule_tree_list *list,
608 int n, struct isl_schedule_node_get_filter_prefix_data *data)
610 int i;
612 if (!list)
613 return isl_stat_error;
615 for (i = n - 1; i >= 0; --i) {
616 isl_schedule_tree *tree;
617 enum isl_schedule_node_type type;
618 isl_stat r;
620 tree = isl_schedule_tree_list_get_schedule_tree(list, i);
621 if (!tree)
622 return isl_stat_error;
623 type = isl_schedule_tree_get_type(tree);
624 if (type == isl_schedule_node_expansion)
625 return collect_filter_prefix_expansion(tree, list, i,
626 data);
627 if (type == isl_schedule_node_extension &&
628 data->universe_domain && !data->collect_prefix)
629 return collect_universe_domain_extension(tree, list, i,
630 data);
631 if (!data->initialized)
632 r = collect_filter_prefix_init(tree, data);
633 else
634 r = collect_filter_prefix_update(tree, data);
635 isl_schedule_tree_free(tree);
636 if (r < 0)
637 return isl_stat_error;
640 return isl_stat_ok;
643 /* Return the concatenation of the partial schedules of all outer band
644 * nodes of "node" interesected with all outer filters
645 * as an isl_multi_union_pw_aff.
646 * None of the ancestors of "node" may be an extension node, unless
647 * there is also a filter ancestor that filters out all the extended
648 * domain elements.
650 * If "node" is pointing at the root of the schedule tree, then
651 * there are no domain elements reaching the current node, so
652 * we return an empty result.
654 * We collect all the filters and partial schedules in collect_filter_prefix
655 * and intersect the domain of the combined schedule with the combined filter.
657 __isl_give isl_multi_union_pw_aff *
658 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(
659 __isl_keep isl_schedule_node *node)
661 int n;
662 isl_space *space;
663 struct isl_schedule_node_get_filter_prefix_data data;
665 if (!node)
666 return NULL;
668 space = isl_schedule_get_space(node->schedule);
669 space = isl_space_set_from_params(space);
670 if (node->tree == node->schedule->root)
671 return isl_multi_union_pw_aff_zero(space);
673 data.initialized = 0;
674 data.universe_domain = 1;
675 data.universe_filter = 0;
676 data.collect_prefix = 1;
677 data.filter = NULL;
678 data.prefix = isl_multi_union_pw_aff_zero(space);
680 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
681 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
682 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
684 data.prefix = isl_multi_union_pw_aff_intersect_domain(data.prefix,
685 data.filter);
687 return data.prefix;
690 /* Return the concatenation of the partial schedules of all outer band
691 * nodes of "node" interesected with all outer filters
692 * as an isl_union_pw_multi_aff.
693 * None of the ancestors of "node" may be an extension node, unless
694 * there is also a filter ancestor that filters out all the extended
695 * domain elements.
697 * If "node" is pointing at the root of the schedule tree, then
698 * there are no domain elements reaching the current node, so
699 * we return an empty result.
701 * We collect all the filters and partial schedules in collect_filter_prefix.
702 * The partial schedules are collected as an isl_multi_union_pw_aff.
703 * If this isl_multi_union_pw_aff is zero-dimensional, then it does not
704 * contain any domain information, so we construct the isl_union_pw_multi_aff
705 * result as a zero-dimensional function on the collected filter.
706 * Otherwise, we convert the isl_multi_union_pw_aff to
707 * an isl_multi_union_pw_aff and intersect the domain with the filter.
709 __isl_give isl_union_pw_multi_aff *
710 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(
711 __isl_keep isl_schedule_node *node)
713 int n;
714 isl_space *space;
715 isl_union_pw_multi_aff *prefix;
716 struct isl_schedule_node_get_filter_prefix_data data;
718 if (!node)
719 return NULL;
721 space = isl_schedule_get_space(node->schedule);
722 if (node->tree == node->schedule->root)
723 return isl_union_pw_multi_aff_empty(space);
725 space = isl_space_set_from_params(space);
726 data.initialized = 0;
727 data.universe_domain = 1;
728 data.universe_filter = 0;
729 data.collect_prefix = 1;
730 data.filter = NULL;
731 data.prefix = isl_multi_union_pw_aff_zero(space);
733 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
734 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
735 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
737 if (data.prefix &&
738 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
739 isl_multi_union_pw_aff_free(data.prefix);
740 prefix = isl_union_pw_multi_aff_from_domain(data.filter);
741 } else {
742 prefix =
743 isl_union_pw_multi_aff_from_multi_union_pw_aff(data.prefix);
744 prefix = isl_union_pw_multi_aff_intersect_domain(prefix,
745 data.filter);
748 return prefix;
751 /* Return the concatenation of the partial schedules of all outer band
752 * nodes of "node" interesected with all outer filters
753 * as an isl_union_map.
755 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_union_map(
756 __isl_keep isl_schedule_node *node)
758 isl_union_pw_multi_aff *upma;
760 upma = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
761 return isl_union_map_from_union_pw_multi_aff(upma);
764 /* Return the concatenation of the partial schedules of all outer band
765 * nodes of "node" intersected with all outer domain constraints.
766 * None of the ancestors of "node" may be an extension node, unless
767 * there is also a filter ancestor that filters out all the extended
768 * domain elements.
770 * Essentially, this function intersects the domain of the output
771 * of isl_schedule_node_get_prefix_schedule_union_map with the output
772 * of isl_schedule_node_get_domain, except that it only traverses
773 * the ancestors of "node" once.
775 __isl_give isl_union_map *isl_schedule_node_get_prefix_schedule_relation(
776 __isl_keep isl_schedule_node *node)
778 int n;
779 isl_space *space;
780 isl_union_map *prefix;
781 struct isl_schedule_node_get_filter_prefix_data data;
783 if (!node)
784 return NULL;
786 space = isl_schedule_get_space(node->schedule);
787 if (node->tree == node->schedule->root)
788 return isl_union_map_empty(space);
790 space = isl_space_set_from_params(space);
791 data.initialized = 0;
792 data.universe_domain = 0;
793 data.universe_filter = 0;
794 data.collect_prefix = 1;
795 data.filter = NULL;
796 data.prefix = isl_multi_union_pw_aff_zero(space);
798 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
799 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
800 data.prefix = isl_multi_union_pw_aff_free(data.prefix);
802 if (data.prefix &&
803 isl_multi_union_pw_aff_dim(data.prefix, isl_dim_set) == 0) {
804 isl_multi_union_pw_aff_free(data.prefix);
805 prefix = isl_union_map_from_domain(data.filter);
806 } else {
807 prefix = isl_union_map_from_multi_union_pw_aff(data.prefix);
808 prefix = isl_union_map_intersect_domain(prefix, data.filter);
811 return prefix;
814 /* Return the domain elements that reach "node".
816 * If "node" is pointing at the root of the schedule tree, then
817 * there are no domain elements reaching the current node, so
818 * we return an empty result.
819 * None of the ancestors of "node" may be an extension node, unless
820 * there is also a filter ancestor that filters out all the extended
821 * domain elements.
823 * Otherwise, we collect all filters reaching the node,
824 * intersected with the root domain in collect_filter_prefix.
826 __isl_give isl_union_set *isl_schedule_node_get_domain(
827 __isl_keep isl_schedule_node *node)
829 int n;
830 struct isl_schedule_node_get_filter_prefix_data data;
832 if (!node)
833 return NULL;
835 if (node->tree == node->schedule->root) {
836 isl_space *space;
838 space = isl_schedule_get_space(node->schedule);
839 return isl_union_set_empty(space);
842 data.initialized = 0;
843 data.universe_domain = 0;
844 data.universe_filter = 0;
845 data.collect_prefix = 0;
846 data.filter = NULL;
847 data.prefix = NULL;
849 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
850 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
851 data.filter = isl_union_set_free(data.filter);
853 return data.filter;
856 /* Return the union of universe sets of the domain elements that reach "node".
858 * If "node" is pointing at the root of the schedule tree, then
859 * there are no domain elements reaching the current node, so
860 * we return an empty result.
862 * Otherwise, we collect the universes of all filters reaching the node
863 * in collect_filter_prefix.
865 __isl_give isl_union_set *isl_schedule_node_get_universe_domain(
866 __isl_keep isl_schedule_node *node)
868 int n;
869 struct isl_schedule_node_get_filter_prefix_data data;
871 if (!node)
872 return NULL;
874 if (node->tree == node->schedule->root) {
875 isl_space *space;
877 space = isl_schedule_get_space(node->schedule);
878 return isl_union_set_empty(space);
881 data.initialized = 0;
882 data.universe_domain = 1;
883 data.universe_filter = 1;
884 data.collect_prefix = 0;
885 data.filter = NULL;
886 data.prefix = NULL;
888 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
889 if (collect_filter_prefix(node->ancestors, n, &data) < 0)
890 data.filter = isl_union_set_free(data.filter);
892 return data.filter;
895 /* Return the subtree schedule of "node".
897 * Since isl_schedule_tree_get_subtree_schedule_union_map does not handle
898 * trees that do not contain any schedule information, we first
899 * move down to the first relevant descendant and handle leaves ourselves.
901 * If the subtree rooted at "node" contains any expansion nodes, then
902 * the returned subtree schedule is formulated in terms of the expanded
903 * domains.
904 * The subtree is not allowed to contain any extension nodes.
906 __isl_give isl_union_map *isl_schedule_node_get_subtree_schedule_union_map(
907 __isl_keep isl_schedule_node *node)
909 isl_schedule_tree *tree, *leaf;
910 isl_union_map *umap;
912 tree = isl_schedule_node_get_tree(node);
913 leaf = isl_schedule_node_peek_leaf(node);
914 tree = isl_schedule_tree_first_schedule_descendant(tree, leaf);
915 if (!tree)
916 return NULL;
917 if (tree == leaf) {
918 isl_union_set *domain;
919 domain = isl_schedule_node_get_universe_domain(node);
920 isl_schedule_tree_free(tree);
921 return isl_union_map_from_domain(domain);
924 umap = isl_schedule_tree_get_subtree_schedule_union_map(tree);
925 isl_schedule_tree_free(tree);
926 return umap;
929 /* Return the number of ancestors of "node" in its schedule tree.
931 int isl_schedule_node_get_tree_depth(__isl_keep isl_schedule_node *node)
933 if (!node)
934 return -1;
935 return isl_schedule_tree_list_n_schedule_tree(node->ancestors);
938 /* Does "node" have a parent?
940 * That is, does it point to any node of the schedule other than the root?
942 isl_bool isl_schedule_node_has_parent(__isl_keep isl_schedule_node *node)
944 int depth;
946 depth = isl_schedule_node_get_tree_depth(node);
947 if (depth < 0)
948 return isl_bool_error;
949 return depth != 0;
952 /* Return the position of "node" among the children of its parent.
954 int isl_schedule_node_get_child_position(__isl_keep isl_schedule_node *node)
956 int n;
957 isl_bool has_parent;
959 if (!node)
960 return -1;
961 has_parent = isl_schedule_node_has_parent(node);
962 if (has_parent < 0)
963 return -1;
964 if (!has_parent)
965 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
966 "node has no parent", return -1);
968 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
969 return node->child_pos[n - 1];
972 /* Does the parent (if any) of "node" have any children with a smaller child
973 * position than this one?
975 isl_bool isl_schedule_node_has_previous_sibling(
976 __isl_keep isl_schedule_node *node)
978 int n;
979 isl_bool has_parent;
981 if (!node)
982 return isl_bool_error;
983 has_parent = isl_schedule_node_has_parent(node);
984 if (has_parent < 0 || !has_parent)
985 return has_parent;
987 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
989 return node->child_pos[n - 1] > 0;
992 /* Does the parent (if any) of "node" have any children with a greater child
993 * position than this one?
995 isl_bool isl_schedule_node_has_next_sibling(__isl_keep isl_schedule_node *node)
997 int n, n_child;
998 isl_bool has_parent;
999 isl_schedule_tree *tree;
1001 if (!node)
1002 return isl_bool_error;
1003 has_parent = isl_schedule_node_has_parent(node);
1004 if (has_parent < 0 || !has_parent)
1005 return has_parent;
1007 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1008 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n - 1);
1009 n_child = isl_schedule_tree_n_children(tree);
1010 isl_schedule_tree_free(tree);
1011 if (n_child < 0)
1012 return isl_bool_error;
1014 return node->child_pos[n - 1] + 1 < n_child;
1017 /* Does "node" have any children?
1019 * Any node other than the leaf nodes is considered to have at least
1020 * one child, even if the corresponding isl_schedule_tree does not
1021 * have any children.
1023 isl_bool isl_schedule_node_has_children(__isl_keep isl_schedule_node *node)
1025 if (!node)
1026 return isl_bool_error;
1027 return !isl_schedule_tree_is_leaf(node->tree);
1030 /* Return the number of children of "node"?
1032 * Any node other than the leaf nodes is considered to have at least
1033 * one child, even if the corresponding isl_schedule_tree does not
1034 * have any children. That is, the number of children of "node" is
1035 * only zero if its tree is the explicit empty tree. Otherwise,
1036 * if the isl_schedule_tree has any children, then it is equal
1037 * to the number of children of "node". If it has zero children,
1038 * then "node" still has a leaf node as child.
1040 int isl_schedule_node_n_children(__isl_keep isl_schedule_node *node)
1042 int n;
1044 if (!node)
1045 return -1;
1047 if (isl_schedule_tree_is_leaf(node->tree))
1048 return 0;
1050 n = isl_schedule_tree_n_children(node->tree);
1051 if (n == 0)
1052 return 1;
1054 return n;
1057 /* Move the "node" pointer to the ancestor of the given generation
1058 * of the node it currently points to, where generation 0 is the node
1059 * itself and generation 1 is its parent.
1061 __isl_give isl_schedule_node *isl_schedule_node_ancestor(
1062 __isl_take isl_schedule_node *node, int generation)
1064 int n;
1065 isl_schedule_tree *tree;
1067 if (!node)
1068 return NULL;
1069 if (generation == 0)
1070 return node;
1071 n = isl_schedule_node_get_tree_depth(node);
1072 if (n < 0)
1073 return isl_schedule_node_free(node);
1074 if (generation < 0 || generation > n)
1075 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1076 "generation out of bounds",
1077 return isl_schedule_node_free(node));
1078 node = isl_schedule_node_cow(node);
1079 if (!node)
1080 return NULL;
1082 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1083 n - generation);
1084 isl_schedule_tree_free(node->tree);
1085 node->tree = tree;
1086 node->ancestors = isl_schedule_tree_list_drop(node->ancestors,
1087 n - generation, generation);
1088 if (!node->ancestors || !node->tree)
1089 return isl_schedule_node_free(node);
1091 return node;
1094 /* Move the "node" pointer to the parent of the node it currently points to.
1096 __isl_give isl_schedule_node *isl_schedule_node_parent(
1097 __isl_take isl_schedule_node *node)
1099 if (!node)
1100 return NULL;
1101 if (!isl_schedule_node_has_parent(node))
1102 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1103 "node has no parent",
1104 return isl_schedule_node_free(node));
1105 return isl_schedule_node_ancestor(node, 1);
1108 /* Move the "node" pointer to the root of its schedule tree.
1110 __isl_give isl_schedule_node *isl_schedule_node_root(
1111 __isl_take isl_schedule_node *node)
1113 int n;
1115 if (!node)
1116 return NULL;
1117 n = isl_schedule_node_get_tree_depth(node);
1118 if (n < 0)
1119 return isl_schedule_node_free(node);
1120 return isl_schedule_node_ancestor(node, n);
1123 /* Move the "node" pointer to the child at position "pos" of the node
1124 * it currently points to.
1126 __isl_give isl_schedule_node *isl_schedule_node_child(
1127 __isl_take isl_schedule_node *node, int pos)
1129 int n;
1130 isl_ctx *ctx;
1131 isl_schedule_tree *tree;
1132 int *child_pos;
1134 node = isl_schedule_node_cow(node);
1135 if (!node)
1136 return NULL;
1137 if (!isl_schedule_node_has_children(node))
1138 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1139 "node has no children",
1140 return isl_schedule_node_free(node));
1142 ctx = isl_schedule_node_get_ctx(node);
1143 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1144 child_pos = isl_realloc_array(ctx, node->child_pos, int, n + 1);
1145 if (!child_pos)
1146 return isl_schedule_node_free(node);
1147 node->child_pos = child_pos;
1148 node->child_pos[n] = pos;
1150 node->ancestors = isl_schedule_tree_list_add(node->ancestors,
1151 isl_schedule_tree_copy(node->tree));
1152 tree = node->tree;
1153 if (isl_schedule_tree_has_children(tree))
1154 tree = isl_schedule_tree_get_child(tree, pos);
1155 else
1156 tree = isl_schedule_node_get_leaf(node);
1157 isl_schedule_tree_free(node->tree);
1158 node->tree = tree;
1160 if (!node->tree || !node->ancestors)
1161 return isl_schedule_node_free(node);
1163 return node;
1166 /* Move the "node" pointer to the first child of the node
1167 * it currently points to.
1169 __isl_give isl_schedule_node *isl_schedule_node_first_child(
1170 __isl_take isl_schedule_node *node)
1172 return isl_schedule_node_child(node, 0);
1175 /* Move the "node" pointer to the child of this node's parent in
1176 * the previous child position.
1178 __isl_give isl_schedule_node *isl_schedule_node_previous_sibling(
1179 __isl_take isl_schedule_node *node)
1181 int n;
1182 isl_schedule_tree *parent, *tree;
1184 node = isl_schedule_node_cow(node);
1185 if (!node)
1186 return NULL;
1187 if (!isl_schedule_node_has_previous_sibling(node))
1188 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1189 "node has no previous sibling",
1190 return isl_schedule_node_free(node));
1192 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1193 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1194 n - 1);
1195 if (!parent)
1196 return isl_schedule_node_free(node);
1197 node->child_pos[n - 1]--;
1198 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
1199 node->child_pos[n - 1]);
1200 isl_schedule_tree_free(parent);
1201 if (!tree)
1202 return isl_schedule_node_free(node);
1203 isl_schedule_tree_free(node->tree);
1204 node->tree = tree;
1206 return node;
1209 /* Move the "node" pointer to the child of this node's parent in
1210 * the next child position.
1212 __isl_give isl_schedule_node *isl_schedule_node_next_sibling(
1213 __isl_take isl_schedule_node *node)
1215 int n;
1216 isl_schedule_tree *parent, *tree;
1218 node = isl_schedule_node_cow(node);
1219 if (!node)
1220 return NULL;
1221 if (!isl_schedule_node_has_next_sibling(node))
1222 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1223 "node has no next sibling",
1224 return isl_schedule_node_free(node));
1226 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
1227 parent = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
1228 n - 1);
1229 if (!parent)
1230 return isl_schedule_node_free(node);
1231 node->child_pos[n - 1]++;
1232 tree = isl_schedule_tree_list_get_schedule_tree(parent->children,
1233 node->child_pos[n - 1]);
1234 isl_schedule_tree_free(parent);
1235 if (!tree)
1236 return isl_schedule_node_free(node);
1237 isl_schedule_tree_free(node->tree);
1238 node->tree = tree;
1240 return node;
1243 /* Return a copy to the child at position "pos" of "node".
1245 __isl_give isl_schedule_node *isl_schedule_node_get_child(
1246 __isl_keep isl_schedule_node *node, int pos)
1248 return isl_schedule_node_child(isl_schedule_node_copy(node), pos);
1251 /* Traverse the descendant of "node" in depth-first order, including
1252 * "node" itself. Call "enter" whenever a node is entered and "leave"
1253 * whenever a node is left. The callback "enter" is responsible
1254 * for moving to the deepest initial subtree of its argument that
1255 * should be traversed.
1257 static __isl_give isl_schedule_node *traverse(
1258 __isl_take isl_schedule_node *node,
1259 __isl_give isl_schedule_node *(*enter)(
1260 __isl_take isl_schedule_node *node, void *user),
1261 __isl_give isl_schedule_node *(*leave)(
1262 __isl_take isl_schedule_node *node, void *user),
1263 void *user)
1265 int depth;
1267 if (!node)
1268 return NULL;
1270 depth = isl_schedule_node_get_tree_depth(node);
1271 do {
1272 node = enter(node, user);
1273 node = leave(node, user);
1274 while (node && isl_schedule_node_get_tree_depth(node) > depth &&
1275 !isl_schedule_node_has_next_sibling(node)) {
1276 node = isl_schedule_node_parent(node);
1277 node = leave(node, user);
1279 if (node && isl_schedule_node_get_tree_depth(node) > depth)
1280 node = isl_schedule_node_next_sibling(node);
1281 } while (node && isl_schedule_node_get_tree_depth(node) > depth);
1283 return node;
1286 /* Internal data structure for isl_schedule_node_foreach_descendant_top_down.
1288 * "fn" is the user-specified callback function.
1289 * "user" is the user-specified argument for the callback.
1291 struct isl_schedule_node_preorder_data {
1292 isl_bool (*fn)(__isl_keep isl_schedule_node *node, void *user);
1293 void *user;
1296 /* Callback for "traverse" to enter a node and to move
1297 * to the deepest initial subtree that should be traversed
1298 * for use in a preorder visit.
1300 * If the user callback returns a negative value, then we abort
1301 * the traversal. If this callback returns zero, then we skip
1302 * the subtree rooted at the current node. Otherwise, we move
1303 * down to the first child and repeat the process until a leaf
1304 * is reached.
1306 static __isl_give isl_schedule_node *preorder_enter(
1307 __isl_take isl_schedule_node *node, void *user)
1309 struct isl_schedule_node_preorder_data *data = user;
1311 if (!node)
1312 return NULL;
1314 do {
1315 isl_bool r;
1317 r = data->fn(node, data->user);
1318 if (r < 0)
1319 return isl_schedule_node_free(node);
1320 if (r == isl_bool_false)
1321 return node;
1322 } while (isl_schedule_node_has_children(node) &&
1323 (node = isl_schedule_node_first_child(node)) != NULL);
1325 return node;
1328 /* Callback for "traverse" to leave a node
1329 * for use in a preorder visit.
1330 * Since we already visited the node when we entered it,
1331 * we do not need to do anything here.
1333 static __isl_give isl_schedule_node *preorder_leave(
1334 __isl_take isl_schedule_node *node, void *user)
1336 return node;
1339 /* Traverse the descendants of "node" (including the node itself)
1340 * in depth first preorder.
1342 * If "fn" returns isl_bool_error on any of the nodes,
1343 * then the traversal is aborted.
1344 * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1345 * at that node is skipped.
1347 * Return isl_stat_ok on success and isl_stat_error on failure.
1349 isl_stat isl_schedule_node_foreach_descendant_top_down(
1350 __isl_keep isl_schedule_node *node,
1351 isl_bool (*fn)(__isl_keep isl_schedule_node *node, void *user),
1352 void *user)
1354 struct isl_schedule_node_preorder_data data = { fn, user };
1356 node = isl_schedule_node_copy(node);
1357 node = traverse(node, &preorder_enter, &preorder_leave, &data);
1358 isl_schedule_node_free(node);
1360 return node ? isl_stat_ok : isl_stat_error;
1363 /* Internal data structure for isl_schedule_node_every_descendant.
1365 * "test" is the user-specified callback function.
1366 * "user" is the user-specified callback function argument.
1368 * "failed" is initialized to 0 and set to 1 if "test" fails
1369 * on any node.
1371 struct isl_union_map_every_data {
1372 isl_bool (*test)(__isl_keep isl_schedule_node *node, void *user);
1373 void *user;
1374 int failed;
1377 /* isl_schedule_node_foreach_descendant_top_down callback
1378 * that sets data->failed if data->test returns false and
1379 * subsequently aborts the traversal.
1381 static isl_bool call_every(__isl_keep isl_schedule_node *node, void *user)
1383 struct isl_union_map_every_data *data = user;
1384 isl_bool r;
1386 r = data->test(node, data->user);
1387 if (r < 0)
1388 return isl_bool_error;
1389 if (r)
1390 return isl_bool_true;
1391 data->failed = 1;
1392 return isl_bool_error;
1395 /* Does "test" succeed on every descendant of "node" (including "node" itself)?
1397 isl_bool isl_schedule_node_every_descendant(__isl_keep isl_schedule_node *node,
1398 isl_bool (*test)(__isl_keep isl_schedule_node *node, void *user),
1399 void *user)
1401 struct isl_union_map_every_data data = { test, user, 0 };
1402 isl_stat r;
1404 r = isl_schedule_node_foreach_descendant_top_down(node, &call_every,
1405 &data);
1406 if (r >= 0)
1407 return isl_bool_true;
1408 if (data.failed)
1409 return isl_bool_false;
1410 return isl_bool_error;
1413 /* Internal data structure for isl_schedule_node_map_descendant_bottom_up.
1415 * "fn" is the user-specified callback function.
1416 * "user" is the user-specified argument for the callback.
1418 struct isl_schedule_node_postorder_data {
1419 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1420 void *user);
1421 void *user;
1424 /* Callback for "traverse" to enter a node and to move
1425 * to the deepest initial subtree that should be traversed
1426 * for use in a postorder visit.
1428 * Since we are performing a postorder visit, we only need
1429 * to move to the deepest initial leaf here.
1431 static __isl_give isl_schedule_node *postorder_enter(
1432 __isl_take isl_schedule_node *node, void *user)
1434 while (node && isl_schedule_node_has_children(node))
1435 node = isl_schedule_node_first_child(node);
1437 return node;
1440 /* Callback for "traverse" to leave a node
1441 * for use in a postorder visit.
1443 * Since we are performing a postorder visit, we need
1444 * to call the user callback here.
1446 static __isl_give isl_schedule_node *postorder_leave(
1447 __isl_take isl_schedule_node *node, void *user)
1449 struct isl_schedule_node_postorder_data *data = user;
1451 return data->fn(node, data->user);
1454 /* Traverse the descendants of "node" (including the node itself)
1455 * in depth first postorder, allowing the user to modify the visited node.
1456 * The traversal continues from the node returned by the callback function.
1457 * It is the responsibility of the user to ensure that this does not
1458 * lead to an infinite loop. It is safest to always return a pointer
1459 * to the same position (same ancestors and child positions) as the input node.
1461 __isl_give isl_schedule_node *isl_schedule_node_map_descendant_bottom_up(
1462 __isl_take isl_schedule_node *node,
1463 __isl_give isl_schedule_node *(*fn)(__isl_take isl_schedule_node *node,
1464 void *user), void *user)
1466 struct isl_schedule_node_postorder_data data = { fn, user };
1468 return traverse(node, &postorder_enter, &postorder_leave, &data);
1471 /* Traverse the ancestors of "node" from the root down to and including
1472 * the parent of "node", calling "fn" on each of them.
1474 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
1476 * Return 0 on success and -1 on failure.
1478 isl_stat isl_schedule_node_foreach_ancestor_top_down(
1479 __isl_keep isl_schedule_node *node,
1480 isl_stat (*fn)(__isl_keep isl_schedule_node *node, void *user),
1481 void *user)
1483 int i, n;
1485 if (!node)
1486 return isl_stat_error;
1488 n = isl_schedule_node_get_tree_depth(node);
1489 for (i = 0; i < n; ++i) {
1490 isl_schedule_node *ancestor;
1491 isl_stat r;
1493 ancestor = isl_schedule_node_copy(node);
1494 ancestor = isl_schedule_node_ancestor(ancestor, n - i);
1495 r = fn(ancestor, user);
1496 isl_schedule_node_free(ancestor);
1497 if (r < 0)
1498 return isl_stat_error;
1501 return isl_stat_ok;
1504 /* Is any node in the subtree rooted at "node" anchored?
1505 * That is, do any of these nodes reference the outer band nodes?
1507 isl_bool isl_schedule_node_is_subtree_anchored(
1508 __isl_keep isl_schedule_node *node)
1510 if (!node)
1511 return isl_bool_error;
1512 return isl_schedule_tree_is_subtree_anchored(node->tree);
1515 /* Return the number of members in the given band node.
1517 unsigned isl_schedule_node_band_n_member(__isl_keep isl_schedule_node *node)
1519 return node ? isl_schedule_tree_band_n_member(node->tree) : 0;
1522 /* Is the band member at position "pos" of the band node "node"
1523 * marked coincident?
1525 isl_bool isl_schedule_node_band_member_get_coincident(
1526 __isl_keep isl_schedule_node *node, int pos)
1528 if (!node)
1529 return isl_bool_error;
1530 return isl_schedule_tree_band_member_get_coincident(node->tree, pos);
1533 /* Mark the band member at position "pos" the band node "node"
1534 * as being coincident or not according to "coincident".
1536 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_coincident(
1537 __isl_take isl_schedule_node *node, int pos, int coincident)
1539 int c;
1540 isl_schedule_tree *tree;
1542 if (!node)
1543 return NULL;
1544 c = isl_schedule_node_band_member_get_coincident(node, pos);
1545 if (c == coincident)
1546 return node;
1548 tree = isl_schedule_tree_copy(node->tree);
1549 tree = isl_schedule_tree_band_member_set_coincident(tree, pos,
1550 coincident);
1551 node = isl_schedule_node_graft_tree(node, tree);
1553 return node;
1556 /* Is the band node "node" marked permutable?
1558 isl_bool isl_schedule_node_band_get_permutable(
1559 __isl_keep isl_schedule_node *node)
1561 if (!node)
1562 return isl_bool_error;
1564 return isl_schedule_tree_band_get_permutable(node->tree);
1567 /* Mark the band node "node" permutable or not according to "permutable"?
1569 __isl_give isl_schedule_node *isl_schedule_node_band_set_permutable(
1570 __isl_take isl_schedule_node *node, int permutable)
1572 isl_schedule_tree *tree;
1574 if (!node)
1575 return NULL;
1576 if (isl_schedule_node_band_get_permutable(node) == permutable)
1577 return node;
1579 tree = isl_schedule_tree_copy(node->tree);
1580 tree = isl_schedule_tree_band_set_permutable(tree, permutable);
1581 node = isl_schedule_node_graft_tree(node, tree);
1583 return node;
1586 /* Return the schedule space of the band node.
1588 __isl_give isl_space *isl_schedule_node_band_get_space(
1589 __isl_keep isl_schedule_node *node)
1591 if (!node)
1592 return NULL;
1594 return isl_schedule_tree_band_get_space(node->tree);
1597 /* Return the schedule of the band node in isolation.
1599 __isl_give isl_multi_union_pw_aff *isl_schedule_node_band_get_partial_schedule(
1600 __isl_keep isl_schedule_node *node)
1602 if (!node)
1603 return NULL;
1605 return isl_schedule_tree_band_get_partial_schedule(node->tree);
1608 /* Return the schedule of the band node in isolation in the form of
1609 * an isl_union_map.
1611 * If the band does not have any members, then we construct a universe map
1612 * with the universe of the domain elements reaching the node as domain.
1613 * Otherwise, we extract an isl_multi_union_pw_aff representation and
1614 * convert that to an isl_union_map.
1616 __isl_give isl_union_map *isl_schedule_node_band_get_partial_schedule_union_map(
1617 __isl_keep isl_schedule_node *node)
1619 isl_multi_union_pw_aff *mupa;
1621 if (!node)
1622 return NULL;
1624 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
1625 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1626 "not a band node", return NULL);
1627 if (isl_schedule_node_band_n_member(node) == 0) {
1628 isl_union_set *domain;
1630 domain = isl_schedule_node_get_universe_domain(node);
1631 return isl_union_map_from_domain(domain);
1634 mupa = isl_schedule_node_band_get_partial_schedule(node);
1635 return isl_union_map_from_multi_union_pw_aff(mupa);
1638 /* Return the loop AST generation type for the band member of band node "node"
1639 * at position "pos".
1641 enum isl_ast_loop_type isl_schedule_node_band_member_get_ast_loop_type(
1642 __isl_keep isl_schedule_node *node, int pos)
1644 if (!node)
1645 return isl_ast_loop_error;
1647 return isl_schedule_tree_band_member_get_ast_loop_type(node->tree, pos);
1650 /* Set the loop AST generation type for the band member of band node "node"
1651 * at position "pos" to "type".
1653 __isl_give isl_schedule_node *isl_schedule_node_band_member_set_ast_loop_type(
1654 __isl_take isl_schedule_node *node, int pos,
1655 enum isl_ast_loop_type type)
1657 isl_schedule_tree *tree;
1659 if (!node)
1660 return NULL;
1662 tree = isl_schedule_tree_copy(node->tree);
1663 tree = isl_schedule_tree_band_member_set_ast_loop_type(tree, pos, type);
1664 return isl_schedule_node_graft_tree(node, tree);
1667 /* Return the loop AST generation type for the band member of band node "node"
1668 * at position "pos" for the isolated part.
1670 enum isl_ast_loop_type isl_schedule_node_band_member_get_isolate_ast_loop_type(
1671 __isl_keep isl_schedule_node *node, int pos)
1673 if (!node)
1674 return isl_ast_loop_error;
1676 return isl_schedule_tree_band_member_get_isolate_ast_loop_type(
1677 node->tree, pos);
1680 /* Set the loop AST generation type for the band member of band node "node"
1681 * at position "pos" for the isolated part to "type".
1683 __isl_give isl_schedule_node *
1684 isl_schedule_node_band_member_set_isolate_ast_loop_type(
1685 __isl_take isl_schedule_node *node, int pos,
1686 enum isl_ast_loop_type type)
1688 isl_schedule_tree *tree;
1690 if (!node)
1691 return NULL;
1693 tree = isl_schedule_tree_copy(node->tree);
1694 tree = isl_schedule_tree_band_member_set_isolate_ast_loop_type(tree,
1695 pos, type);
1696 return isl_schedule_node_graft_tree(node, tree);
1699 /* Return the AST build options associated to band node "node".
1701 __isl_give isl_union_set *isl_schedule_node_band_get_ast_build_options(
1702 __isl_keep isl_schedule_node *node)
1704 if (!node)
1705 return NULL;
1707 return isl_schedule_tree_band_get_ast_build_options(node->tree);
1710 /* Replace the AST build options associated to band node "node" by "options".
1712 __isl_give isl_schedule_node *isl_schedule_node_band_set_ast_build_options(
1713 __isl_take isl_schedule_node *node, __isl_take isl_union_set *options)
1715 isl_schedule_tree *tree;
1717 if (!node || !options)
1718 goto error;
1720 tree = isl_schedule_tree_copy(node->tree);
1721 tree = isl_schedule_tree_band_set_ast_build_options(tree, options);
1722 return isl_schedule_node_graft_tree(node, tree);
1723 error:
1724 isl_schedule_node_free(node);
1725 isl_union_set_free(options);
1726 return NULL;
1729 /* Return the "isolate" option associated to band node "node".
1731 __isl_give isl_set *isl_schedule_node_band_get_ast_isolate_option(
1732 __isl_keep isl_schedule_node *node)
1734 int depth;
1736 if (!node)
1737 return NULL;
1739 depth = isl_schedule_node_get_schedule_depth(node);
1740 return isl_schedule_tree_band_get_ast_isolate_option(node->tree, depth);
1743 /* Make sure that that spaces of "node" and "mv" are the same.
1744 * Return -1 on error, reporting the error to the user.
1746 static int check_space_multi_val(__isl_keep isl_schedule_node *node,
1747 __isl_keep isl_multi_val *mv)
1749 isl_space *node_space, *mv_space;
1750 int equal;
1752 node_space = isl_schedule_node_band_get_space(node);
1753 mv_space = isl_multi_val_get_space(mv);
1754 equal = isl_space_tuple_is_equal(node_space, isl_dim_set,
1755 mv_space, isl_dim_set);
1756 isl_space_free(mv_space);
1757 isl_space_free(node_space);
1758 if (equal < 0)
1759 return -1;
1760 if (!equal)
1761 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1762 "spaces don't match", return -1);
1764 return 0;
1767 /* Multiply the partial schedule of the band node "node"
1768 * with the factors in "mv".
1770 __isl_give isl_schedule_node *isl_schedule_node_band_scale(
1771 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1773 isl_schedule_tree *tree;
1774 int anchored;
1776 if (!node || !mv)
1777 goto error;
1778 if (check_space_multi_val(node, mv) < 0)
1779 goto error;
1780 anchored = isl_schedule_node_is_subtree_anchored(node);
1781 if (anchored < 0)
1782 goto error;
1783 if (anchored)
1784 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1785 "cannot scale band node with anchored subtree",
1786 goto error);
1788 tree = isl_schedule_node_get_tree(node);
1789 tree = isl_schedule_tree_band_scale(tree, mv);
1790 return isl_schedule_node_graft_tree(node, tree);
1791 error:
1792 isl_multi_val_free(mv);
1793 isl_schedule_node_free(node);
1794 return NULL;
1797 /* Divide the partial schedule of the band node "node"
1798 * by the factors in "mv".
1800 __isl_give isl_schedule_node *isl_schedule_node_band_scale_down(
1801 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1803 isl_schedule_tree *tree;
1804 int anchored;
1806 if (!node || !mv)
1807 goto error;
1808 if (check_space_multi_val(node, mv) < 0)
1809 goto error;
1810 anchored = isl_schedule_node_is_subtree_anchored(node);
1811 if (anchored < 0)
1812 goto error;
1813 if (anchored)
1814 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1815 "cannot scale down band node with anchored subtree",
1816 goto error);
1818 tree = isl_schedule_node_get_tree(node);
1819 tree = isl_schedule_tree_band_scale_down(tree, mv);
1820 return isl_schedule_node_graft_tree(node, tree);
1821 error:
1822 isl_multi_val_free(mv);
1823 isl_schedule_node_free(node);
1824 return NULL;
1827 /* Reduce the partial schedule of the band node "node"
1828 * modulo the factors in "mv".
1830 __isl_give isl_schedule_node *isl_schedule_node_band_mod(
1831 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *mv)
1833 isl_schedule_tree *tree;
1834 isl_bool anchored;
1836 if (!node || !mv)
1837 goto error;
1838 if (check_space_multi_val(node, mv) < 0)
1839 goto error;
1840 anchored = isl_schedule_node_is_subtree_anchored(node);
1841 if (anchored < 0)
1842 goto error;
1843 if (anchored)
1844 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1845 "cannot perform mod on band node with anchored subtree",
1846 goto error);
1848 tree = isl_schedule_node_get_tree(node);
1849 tree = isl_schedule_tree_band_mod(tree, mv);
1850 return isl_schedule_node_graft_tree(node, tree);
1851 error:
1852 isl_multi_val_free(mv);
1853 isl_schedule_node_free(node);
1854 return NULL;
1857 /* Make sure that that spaces of "node" and "mupa" are the same.
1858 * Return isl_stat_error on error, reporting the error to the user.
1860 static isl_stat check_space_multi_union_pw_aff(
1861 __isl_keep isl_schedule_node *node,
1862 __isl_keep isl_multi_union_pw_aff *mupa)
1864 isl_space *node_space, *mupa_space;
1865 isl_bool equal;
1867 node_space = isl_schedule_node_band_get_space(node);
1868 mupa_space = isl_multi_union_pw_aff_get_space(mupa);
1869 equal = isl_space_tuple_is_equal(node_space, isl_dim_set,
1870 mupa_space, isl_dim_set);
1871 isl_space_free(mupa_space);
1872 isl_space_free(node_space);
1873 if (equal < 0)
1874 return isl_stat_error;
1875 if (!equal)
1876 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1877 "spaces don't match", return isl_stat_error);
1879 return isl_stat_ok;
1882 /* Shift the partial schedule of the band node "node" by "shift".
1884 __isl_give isl_schedule_node *isl_schedule_node_band_shift(
1885 __isl_take isl_schedule_node *node,
1886 __isl_take isl_multi_union_pw_aff *shift)
1888 isl_schedule_tree *tree;
1889 int anchored;
1891 if (!node || !shift)
1892 goto error;
1893 if (check_space_multi_union_pw_aff(node, shift) < 0)
1894 goto error;
1895 anchored = isl_schedule_node_is_subtree_anchored(node);
1896 if (anchored < 0)
1897 goto error;
1898 if (anchored)
1899 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1900 "cannot shift band node with anchored subtree",
1901 goto error);
1903 tree = isl_schedule_node_get_tree(node);
1904 tree = isl_schedule_tree_band_shift(tree, shift);
1905 return isl_schedule_node_graft_tree(node, tree);
1906 error:
1907 isl_multi_union_pw_aff_free(shift);
1908 isl_schedule_node_free(node);
1909 return NULL;
1912 /* Tile "node" with tile sizes "sizes".
1914 * The current node is replaced by two nested nodes corresponding
1915 * to the tile dimensions and the point dimensions.
1917 * Return a pointer to the outer (tile) node.
1919 * If any of the descendants of "node" depend on the set of outer band nodes,
1920 * then we refuse to tile the node.
1922 * If the scale tile loops option is set, then the tile loops
1923 * are scaled by the tile sizes. If the shift point loops option is set,
1924 * then the point loops are shifted to start at zero.
1925 * In particular, these options affect the tile and point loop schedules
1926 * as follows
1928 * scale shift original tile point
1930 * 0 0 i floor(i/s) i
1931 * 1 0 i s * floor(i/s) i
1932 * 0 1 i floor(i/s) i - s * floor(i/s)
1933 * 1 1 i s * floor(i/s) i - s * floor(i/s)
1935 __isl_give isl_schedule_node *isl_schedule_node_band_tile(
1936 __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
1938 isl_schedule_tree *tree;
1939 int anchored;
1941 if (!node || !sizes)
1942 goto error;
1943 anchored = isl_schedule_node_is_subtree_anchored(node);
1944 if (anchored < 0)
1945 goto error;
1946 if (anchored)
1947 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1948 "cannot tile band node with anchored subtree",
1949 goto error);
1951 if (check_space_multi_val(node, sizes) < 0)
1952 goto error;
1954 tree = isl_schedule_node_get_tree(node);
1955 tree = isl_schedule_tree_band_tile(tree, sizes);
1956 return isl_schedule_node_graft_tree(node, tree);
1957 error:
1958 isl_multi_val_free(sizes);
1959 isl_schedule_node_free(node);
1960 return NULL;
1963 /* Move the band node "node" down to all the leaves in the subtree
1964 * rooted at "node".
1965 * Return a pointer to the node in the resulting tree that is in the same
1966 * position as the node pointed to by "node" in the original tree.
1968 * If the node only has a leaf child, then nothing needs to be done.
1969 * Otherwise, the child of the node is removed and the result is
1970 * appended to all the leaves in the subtree rooted at the original child.
1971 * Since the node is moved to the leaves, it needs to be expanded
1972 * according to the expansion, if any, defined by that subtree.
1973 * In the end, the original node is replaced by the result of
1974 * attaching copies of the expanded node to the leaves.
1976 * If any of the nodes in the subtree rooted at "node" depend on
1977 * the set of outer band nodes then we refuse to sink the band node.
1979 __isl_give isl_schedule_node *isl_schedule_node_band_sink(
1980 __isl_take isl_schedule_node *node)
1982 enum isl_schedule_node_type type;
1983 isl_schedule_tree *tree, *child;
1984 isl_union_pw_multi_aff *contraction;
1985 isl_bool anchored;
1987 if (!node)
1988 return NULL;
1990 type = isl_schedule_node_get_type(node);
1991 if (type != isl_schedule_node_band)
1992 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1993 "not a band node", return isl_schedule_node_free(node));
1994 anchored = isl_schedule_node_is_subtree_anchored(node);
1995 if (anchored < 0)
1996 return isl_schedule_node_free(node);
1997 if (anchored)
1998 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
1999 "cannot sink band node in anchored subtree",
2000 return isl_schedule_node_free(node));
2001 if (isl_schedule_tree_n_children(node->tree) == 0)
2002 return node;
2004 contraction = isl_schedule_node_get_subtree_contraction(node);
2006 tree = isl_schedule_node_get_tree(node);
2007 child = isl_schedule_tree_get_child(tree, 0);
2008 tree = isl_schedule_tree_reset_children(tree);
2009 tree = isl_schedule_tree_pullback_union_pw_multi_aff(tree, contraction);
2010 tree = isl_schedule_tree_append_to_leaves(child, tree);
2012 return isl_schedule_node_graft_tree(node, tree);
2015 /* Split "node" into two nested band nodes, one with the first "pos"
2016 * dimensions and one with the remaining dimensions.
2017 * The schedules of the two band nodes live in anonymous spaces.
2018 * The loop AST generation type options and the isolate option
2019 * are split over the two band nodes.
2021 __isl_give isl_schedule_node *isl_schedule_node_band_split(
2022 __isl_take isl_schedule_node *node, int pos)
2024 int depth;
2025 isl_schedule_tree *tree;
2027 depth = isl_schedule_node_get_schedule_depth(node);
2028 tree = isl_schedule_node_get_tree(node);
2029 tree = isl_schedule_tree_band_split(tree, pos, depth);
2030 return isl_schedule_node_graft_tree(node, tree);
2033 /* Return the context of the context node "node".
2035 __isl_give isl_set *isl_schedule_node_context_get_context(
2036 __isl_keep isl_schedule_node *node)
2038 if (!node)
2039 return NULL;
2041 return isl_schedule_tree_context_get_context(node->tree);
2044 /* Return the domain of the domain node "node".
2046 __isl_give isl_union_set *isl_schedule_node_domain_get_domain(
2047 __isl_keep isl_schedule_node *node)
2049 if (!node)
2050 return NULL;
2052 return isl_schedule_tree_domain_get_domain(node->tree);
2055 /* Return the expansion map of expansion node "node".
2057 __isl_give isl_union_map *isl_schedule_node_expansion_get_expansion(
2058 __isl_keep isl_schedule_node *node)
2060 if (!node)
2061 return NULL;
2063 return isl_schedule_tree_expansion_get_expansion(node->tree);
2066 /* Return the contraction of expansion node "node".
2068 __isl_give isl_union_pw_multi_aff *isl_schedule_node_expansion_get_contraction(
2069 __isl_keep isl_schedule_node *node)
2071 if (!node)
2072 return NULL;
2074 return isl_schedule_tree_expansion_get_contraction(node->tree);
2077 /* Replace the contraction and the expansion of the expansion node "node"
2078 * by "contraction" and "expansion".
2080 __isl_give isl_schedule_node *
2081 isl_schedule_node_expansion_set_contraction_and_expansion(
2082 __isl_take isl_schedule_node *node,
2083 __isl_take isl_union_pw_multi_aff *contraction,
2084 __isl_take isl_union_map *expansion)
2086 isl_schedule_tree *tree;
2088 if (!node || !contraction || !expansion)
2089 goto error;
2091 tree = isl_schedule_tree_copy(node->tree);
2092 tree = isl_schedule_tree_expansion_set_contraction_and_expansion(tree,
2093 contraction, expansion);
2094 return isl_schedule_node_graft_tree(node, tree);
2095 error:
2096 isl_schedule_node_free(node);
2097 isl_union_pw_multi_aff_free(contraction);
2098 isl_union_map_free(expansion);
2099 return NULL;
2102 /* Return the extension of the extension node "node".
2104 __isl_give isl_union_map *isl_schedule_node_extension_get_extension(
2105 __isl_keep isl_schedule_node *node)
2107 if (!node)
2108 return NULL;
2110 return isl_schedule_tree_extension_get_extension(node->tree);
2113 /* Replace the extension of extension node "node" by "extension".
2115 __isl_give isl_schedule_node *isl_schedule_node_extension_set_extension(
2116 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
2118 isl_schedule_tree *tree;
2120 if (!node || !extension)
2121 goto error;
2123 tree = isl_schedule_tree_copy(node->tree);
2124 tree = isl_schedule_tree_extension_set_extension(tree, extension);
2125 return isl_schedule_node_graft_tree(node, tree);
2126 error:
2127 isl_schedule_node_free(node);
2128 isl_union_map_free(extension);
2129 return NULL;
2132 /* Return the filter of the filter node "node".
2134 __isl_give isl_union_set *isl_schedule_node_filter_get_filter(
2135 __isl_keep isl_schedule_node *node)
2137 if (!node)
2138 return NULL;
2140 return isl_schedule_tree_filter_get_filter(node->tree);
2143 /* Replace the filter of filter node "node" by "filter".
2145 __isl_give isl_schedule_node *isl_schedule_node_filter_set_filter(
2146 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
2148 isl_schedule_tree *tree;
2150 if (!node || !filter)
2151 goto error;
2153 tree = isl_schedule_tree_copy(node->tree);
2154 tree = isl_schedule_tree_filter_set_filter(tree, filter);
2155 return isl_schedule_node_graft_tree(node, tree);
2156 error:
2157 isl_schedule_node_free(node);
2158 isl_union_set_free(filter);
2159 return NULL;
2162 /* Intersect the filter of filter node "node" with "filter".
2164 * If the filter of the node is already a subset of "filter",
2165 * then leave the node unchanged.
2167 __isl_give isl_schedule_node *isl_schedule_node_filter_intersect_filter(
2168 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
2170 isl_union_set *node_filter = NULL;
2171 isl_bool subset;
2173 if (!node || !filter)
2174 goto error;
2176 node_filter = isl_schedule_node_filter_get_filter(node);
2177 subset = isl_union_set_is_subset(node_filter, filter);
2178 if (subset < 0)
2179 goto error;
2180 if (subset) {
2181 isl_union_set_free(node_filter);
2182 isl_union_set_free(filter);
2183 return node;
2185 node_filter = isl_union_set_intersect(node_filter, filter);
2186 node = isl_schedule_node_filter_set_filter(node, node_filter);
2187 return node;
2188 error:
2189 isl_schedule_node_free(node);
2190 isl_union_set_free(node_filter);
2191 isl_union_set_free(filter);
2192 return NULL;
2195 /* Return the guard of the guard node "node".
2197 __isl_give isl_set *isl_schedule_node_guard_get_guard(
2198 __isl_keep isl_schedule_node *node)
2200 if (!node)
2201 return NULL;
2203 return isl_schedule_tree_guard_get_guard(node->tree);
2206 /* Return the mark identifier of the mark node "node".
2208 __isl_give isl_id *isl_schedule_node_mark_get_id(
2209 __isl_keep isl_schedule_node *node)
2211 if (!node)
2212 return NULL;
2214 return isl_schedule_tree_mark_get_id(node->tree);
2217 /* Replace the child at position "pos" of the sequence node "node"
2218 * by the children of sequence root node of "tree".
2220 __isl_give isl_schedule_node *isl_schedule_node_sequence_splice(
2221 __isl_take isl_schedule_node *node, int pos,
2222 __isl_take isl_schedule_tree *tree)
2224 isl_schedule_tree *node_tree;
2226 if (!node || !tree)
2227 goto error;
2228 if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
2229 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2230 "not a sequence node", goto error);
2231 if (isl_schedule_tree_get_type(tree) != isl_schedule_node_sequence)
2232 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2233 "not a sequence node", goto error);
2234 node_tree = isl_schedule_node_get_tree(node);
2235 node_tree = isl_schedule_tree_sequence_splice(node_tree, pos, tree);
2236 node = isl_schedule_node_graft_tree(node, node_tree);
2238 return node;
2239 error:
2240 isl_schedule_node_free(node);
2241 isl_schedule_tree_free(tree);
2242 return NULL;
2245 /* Given a sequence node "node", with a child at position "pos" that
2246 * is also a sequence node, attach the children of that node directly
2247 * as children of "node" at that position, replacing the original child.
2249 * The filters of these children are intersected with the filter
2250 * of the child at position "pos".
2252 __isl_give isl_schedule_node *isl_schedule_node_sequence_splice_child(
2253 __isl_take isl_schedule_node *node, int pos)
2255 int i, n;
2256 isl_union_set *filter;
2257 isl_schedule_node *child;
2258 isl_schedule_tree *tree;
2260 if (!node)
2261 return NULL;
2262 if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
2263 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2264 "not a sequence node",
2265 return isl_schedule_node_free(node));
2266 node = isl_schedule_node_child(node, pos);
2267 node = isl_schedule_node_child(node, 0);
2268 if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
2269 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2270 "not a sequence node",
2271 return isl_schedule_node_free(node));
2272 child = isl_schedule_node_copy(node);
2273 node = isl_schedule_node_parent(node);
2274 filter = isl_schedule_node_filter_get_filter(node);
2275 n = isl_schedule_node_n_children(child);
2276 for (i = 0; i < n; ++i) {
2277 child = isl_schedule_node_child(child, i);
2278 child = isl_schedule_node_filter_intersect_filter(child,
2279 isl_union_set_copy(filter));
2280 child = isl_schedule_node_parent(child);
2282 isl_union_set_free(filter);
2283 tree = isl_schedule_node_get_tree(child);
2284 isl_schedule_node_free(child);
2285 node = isl_schedule_node_parent(node);
2286 node = isl_schedule_node_sequence_splice(node, pos, tree);
2288 return node;
2291 /* Update the ancestors of "node" to point to the tree that "node"
2292 * now points to.
2293 * That is, replace the child in the original parent that corresponds
2294 * to the current tree position by node->tree and continue updating
2295 * the ancestors in the same way until the root is reached.
2297 * If "fn" is not NULL, then it is called on each ancestor as we move up
2298 * the tree so that it can modify the ancestor before it is added
2299 * to the list of ancestors of the modified node.
2300 * The additional "pos" argument records the position
2301 * of the "tree" argument in the original schedule tree.
2303 * If "node" originally points to a leaf of the schedule tree, then make sure
2304 * that in the end it points to a leaf in the updated schedule tree.
2306 static __isl_give isl_schedule_node *update_ancestors(
2307 __isl_take isl_schedule_node *node,
2308 __isl_give isl_schedule_tree *(*fn)(__isl_take isl_schedule_tree *tree,
2309 __isl_keep isl_schedule_node *pos, void *user), void *user)
2311 int i, n;
2312 int is_leaf;
2313 isl_schedule_tree *tree;
2314 isl_schedule_node *pos = NULL;
2316 if (fn)
2317 pos = isl_schedule_node_copy(node);
2319 node = isl_schedule_node_cow(node);
2320 if (!node)
2321 return isl_schedule_node_free(pos);
2323 n = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
2324 tree = isl_schedule_tree_copy(node->tree);
2326 for (i = n - 1; i >= 0; --i) {
2327 isl_schedule_tree *parent;
2329 parent = isl_schedule_tree_list_get_schedule_tree(
2330 node->ancestors, i);
2331 parent = isl_schedule_tree_replace_child(parent,
2332 node->child_pos[i], tree);
2333 if (fn) {
2334 pos = isl_schedule_node_parent(pos);
2335 parent = fn(parent, pos, user);
2337 node->ancestors = isl_schedule_tree_list_set_schedule_tree(
2338 node->ancestors, i, isl_schedule_tree_copy(parent));
2340 tree = parent;
2343 if (fn)
2344 isl_schedule_node_free(pos);
2346 is_leaf = isl_schedule_tree_is_leaf(node->tree);
2347 node->schedule = isl_schedule_set_root(node->schedule, tree);
2348 if (is_leaf) {
2349 isl_schedule_tree_free(node->tree);
2350 node->tree = isl_schedule_node_get_leaf(node);
2353 if (!node->schedule || !node->ancestors)
2354 return isl_schedule_node_free(node);
2356 return node;
2359 /* Replace the subtree that "pos" points to by "tree", updating
2360 * the ancestors to maintain a consistent state.
2362 __isl_give isl_schedule_node *isl_schedule_node_graft_tree(
2363 __isl_take isl_schedule_node *pos, __isl_take isl_schedule_tree *tree)
2365 if (!tree || !pos)
2366 goto error;
2367 if (pos->tree == tree) {
2368 isl_schedule_tree_free(tree);
2369 return pos;
2372 pos = isl_schedule_node_cow(pos);
2373 if (!pos)
2374 goto error;
2376 isl_schedule_tree_free(pos->tree);
2377 pos->tree = tree;
2379 return update_ancestors(pos, NULL, NULL);
2380 error:
2381 isl_schedule_node_free(pos);
2382 isl_schedule_tree_free(tree);
2383 return NULL;
2386 /* Make sure we can insert a node between "node" and its parent.
2387 * Return -1 on error, reporting the reason why we cannot insert a node.
2389 static int check_insert(__isl_keep isl_schedule_node *node)
2391 int has_parent;
2392 enum isl_schedule_node_type type;
2394 has_parent = isl_schedule_node_has_parent(node);
2395 if (has_parent < 0)
2396 return -1;
2397 if (!has_parent)
2398 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2399 "cannot insert node outside of root", return -1);
2401 type = isl_schedule_node_get_parent_type(node);
2402 if (type == isl_schedule_node_error)
2403 return -1;
2404 if (type == isl_schedule_node_set || type == isl_schedule_node_sequence)
2405 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2406 "cannot insert node between set or sequence node "
2407 "and its filter children", return -1);
2409 return 0;
2412 /* Insert a band node with partial schedule "mupa" between "node" and
2413 * its parent.
2414 * Return a pointer to the new band node.
2416 * If any of the nodes in the subtree rooted at "node" depend on
2417 * the set of outer band nodes then we refuse to insert the band node.
2419 __isl_give isl_schedule_node *isl_schedule_node_insert_partial_schedule(
2420 __isl_take isl_schedule_node *node,
2421 __isl_take isl_multi_union_pw_aff *mupa)
2423 int anchored;
2424 isl_schedule_band *band;
2425 isl_schedule_tree *tree;
2427 if (check_insert(node) < 0)
2428 node = isl_schedule_node_free(node);
2429 anchored = isl_schedule_node_is_subtree_anchored(node);
2430 if (anchored < 0)
2431 goto error;
2432 if (anchored)
2433 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2434 "cannot insert band node in anchored subtree",
2435 goto error);
2437 tree = isl_schedule_node_get_tree(node);
2438 band = isl_schedule_band_from_multi_union_pw_aff(mupa);
2439 tree = isl_schedule_tree_insert_band(tree, band);
2440 node = isl_schedule_node_graft_tree(node, tree);
2442 return node;
2443 error:
2444 isl_schedule_node_free(node);
2445 isl_multi_union_pw_aff_free(mupa);
2446 return NULL;
2449 /* Insert a context node with context "context" between "node" and its parent.
2450 * Return a pointer to the new context node.
2452 __isl_give isl_schedule_node *isl_schedule_node_insert_context(
2453 __isl_take isl_schedule_node *node, __isl_take isl_set *context)
2455 isl_schedule_tree *tree;
2457 if (check_insert(node) < 0)
2458 node = isl_schedule_node_free(node);
2460 tree = isl_schedule_node_get_tree(node);
2461 tree = isl_schedule_tree_insert_context(tree, context);
2462 node = isl_schedule_node_graft_tree(node, tree);
2464 return node;
2467 /* Insert an expansion node with the given "contraction" and "expansion"
2468 * between "node" and its parent.
2469 * Return a pointer to the new expansion node.
2471 * Typically the domain and range spaces of the expansion are different.
2472 * This means that only one of them can refer to the current domain space
2473 * in a consistent tree. It is up to the caller to ensure that the tree
2474 * returns to a consistent state.
2476 __isl_give isl_schedule_node *isl_schedule_node_insert_expansion(
2477 __isl_take isl_schedule_node *node,
2478 __isl_take isl_union_pw_multi_aff *contraction,
2479 __isl_take isl_union_map *expansion)
2481 isl_schedule_tree *tree;
2483 if (check_insert(node) < 0)
2484 node = isl_schedule_node_free(node);
2486 tree = isl_schedule_node_get_tree(node);
2487 tree = isl_schedule_tree_insert_expansion(tree, contraction, expansion);
2488 node = isl_schedule_node_graft_tree(node, tree);
2490 return node;
2493 /* Insert an extension node with extension "extension" between "node" and
2494 * its parent.
2495 * Return a pointer to the new extension node.
2497 __isl_give isl_schedule_node *isl_schedule_node_insert_extension(
2498 __isl_take isl_schedule_node *node,
2499 __isl_take isl_union_map *extension)
2501 isl_schedule_tree *tree;
2503 tree = isl_schedule_node_get_tree(node);
2504 tree = isl_schedule_tree_insert_extension(tree, extension);
2505 node = isl_schedule_node_graft_tree(node, tree);
2507 return node;
2510 /* Insert a filter node with filter "filter" between "node" and its parent.
2511 * Return a pointer to the new filter node.
2513 __isl_give isl_schedule_node *isl_schedule_node_insert_filter(
2514 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
2516 isl_schedule_tree *tree;
2518 if (check_insert(node) < 0)
2519 node = isl_schedule_node_free(node);
2521 tree = isl_schedule_node_get_tree(node);
2522 tree = isl_schedule_tree_insert_filter(tree, filter);
2523 node = isl_schedule_node_graft_tree(node, tree);
2525 return node;
2528 /* Insert a guard node with guard "guard" between "node" and its parent.
2529 * Return a pointer to the new guard node.
2531 __isl_give isl_schedule_node *isl_schedule_node_insert_guard(
2532 __isl_take isl_schedule_node *node, __isl_take isl_set *guard)
2534 isl_schedule_tree *tree;
2536 if (check_insert(node) < 0)
2537 node = isl_schedule_node_free(node);
2539 tree = isl_schedule_node_get_tree(node);
2540 tree = isl_schedule_tree_insert_guard(tree, guard);
2541 node = isl_schedule_node_graft_tree(node, tree);
2543 return node;
2546 /* Insert a mark node with mark identifier "mark" between "node" and
2547 * its parent.
2548 * Return a pointer to the new mark node.
2550 __isl_give isl_schedule_node *isl_schedule_node_insert_mark(
2551 __isl_take isl_schedule_node *node, __isl_take isl_id *mark)
2553 isl_schedule_tree *tree;
2555 if (check_insert(node) < 0)
2556 node = isl_schedule_node_free(node);
2558 tree = isl_schedule_node_get_tree(node);
2559 tree = isl_schedule_tree_insert_mark(tree, mark);
2560 node = isl_schedule_node_graft_tree(node, tree);
2562 return node;
2565 /* Attach the current subtree of "node" to a sequence of filter tree nodes
2566 * with filters described by "filters", attach this sequence
2567 * of filter tree nodes as children to a new tree of type "type" and
2568 * replace the original subtree of "node" by this new tree.
2569 * Each copy of the original subtree is simplified with respect
2570 * to the corresponding filter.
2572 static __isl_give isl_schedule_node *isl_schedule_node_insert_children(
2573 __isl_take isl_schedule_node *node,
2574 enum isl_schedule_node_type type,
2575 __isl_take isl_union_set_list *filters)
2577 int i, n;
2578 isl_ctx *ctx;
2579 isl_schedule_tree *tree;
2580 isl_schedule_tree_list *list;
2582 if (check_insert(node) < 0)
2583 node = isl_schedule_node_free(node);
2585 if (!node || !filters)
2586 goto error;
2588 ctx = isl_schedule_node_get_ctx(node);
2589 n = isl_union_set_list_n_union_set(filters);
2590 list = isl_schedule_tree_list_alloc(ctx, n);
2591 for (i = 0; i < n; ++i) {
2592 isl_schedule_node *node_i;
2593 isl_schedule_tree *tree;
2594 isl_union_set *filter;
2596 filter = isl_union_set_list_get_union_set(filters, i);
2597 node_i = isl_schedule_node_copy(node);
2598 node_i = isl_schedule_node_gist(node_i,
2599 isl_union_set_copy(filter));
2600 tree = isl_schedule_node_get_tree(node_i);
2601 isl_schedule_node_free(node_i);
2602 tree = isl_schedule_tree_insert_filter(tree, filter);
2603 list = isl_schedule_tree_list_add(list, tree);
2605 tree = isl_schedule_tree_from_children(type, list);
2606 node = isl_schedule_node_graft_tree(node, tree);
2608 isl_union_set_list_free(filters);
2609 return node;
2610 error:
2611 isl_union_set_list_free(filters);
2612 isl_schedule_node_free(node);
2613 return NULL;
2616 /* Insert a sequence node with child filters "filters" between "node" and
2617 * its parent. That is, the tree that "node" points to is attached
2618 * to each of the child nodes of the filter nodes.
2619 * Return a pointer to the new sequence node.
2621 __isl_give isl_schedule_node *isl_schedule_node_insert_sequence(
2622 __isl_take isl_schedule_node *node,
2623 __isl_take isl_union_set_list *filters)
2625 return isl_schedule_node_insert_children(node,
2626 isl_schedule_node_sequence, filters);
2629 /* Insert a set node with child filters "filters" between "node" and
2630 * its parent. That is, the tree that "node" points to is attached
2631 * to each of the child nodes of the filter nodes.
2632 * Return a pointer to the new set node.
2634 __isl_give isl_schedule_node *isl_schedule_node_insert_set(
2635 __isl_take isl_schedule_node *node,
2636 __isl_take isl_union_set_list *filters)
2638 return isl_schedule_node_insert_children(node,
2639 isl_schedule_node_set, filters);
2642 /* Remove "node" from its schedule tree and return a pointer
2643 * to the leaf at the same position in the updated schedule tree.
2645 * It is not allowed to remove the root of a schedule tree or
2646 * a child of a set or sequence node.
2648 __isl_give isl_schedule_node *isl_schedule_node_cut(
2649 __isl_take isl_schedule_node *node)
2651 isl_schedule_tree *leaf;
2652 enum isl_schedule_node_type parent_type;
2654 if (!node)
2655 return NULL;
2656 if (!isl_schedule_node_has_parent(node))
2657 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2658 "cannot cut root", return isl_schedule_node_free(node));
2660 parent_type = isl_schedule_node_get_parent_type(node);
2661 if (parent_type == isl_schedule_node_set ||
2662 parent_type == isl_schedule_node_sequence)
2663 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2664 "cannot cut child of set or sequence",
2665 return isl_schedule_node_free(node));
2667 leaf = isl_schedule_node_get_leaf(node);
2668 return isl_schedule_node_graft_tree(node, leaf);
2671 /* Remove a single node from the schedule tree, attaching the child
2672 * of "node" directly to its parent.
2673 * Return a pointer to this former child or to the leaf the position
2674 * of the original node if there was no child.
2675 * It is not allowed to remove the root of a schedule tree,
2676 * a set or sequence node, a child of a set or sequence node or
2677 * a band node with an anchored subtree.
2679 __isl_give isl_schedule_node *isl_schedule_node_delete(
2680 __isl_take isl_schedule_node *node)
2682 int n;
2683 isl_schedule_tree *tree;
2684 enum isl_schedule_node_type type;
2686 if (!node)
2687 return NULL;
2689 if (isl_schedule_node_get_tree_depth(node) == 0)
2690 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2691 "cannot delete root node",
2692 return isl_schedule_node_free(node));
2693 n = isl_schedule_node_n_children(node);
2694 if (n != 1)
2695 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2696 "can only delete node with a single child",
2697 return isl_schedule_node_free(node));
2698 type = isl_schedule_node_get_parent_type(node);
2699 if (type == isl_schedule_node_sequence || type == isl_schedule_node_set)
2700 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
2701 "cannot delete child of set or sequence",
2702 return isl_schedule_node_free(node));
2703 if (isl_schedule_node_get_type(node) == isl_schedule_node_band) {
2704 int anchored;
2706 anchored = isl_schedule_node_is_subtree_anchored(node);
2707 if (anchored < 0)
2708 return isl_schedule_node_free(node);
2709 if (anchored)
2710 isl_die(isl_schedule_node_get_ctx(node),
2711 isl_error_invalid,
2712 "cannot delete band node with anchored subtree",
2713 return isl_schedule_node_free(node));
2716 tree = isl_schedule_node_get_tree(node);
2717 if (!tree || isl_schedule_tree_has_children(tree)) {
2718 tree = isl_schedule_tree_child(tree, 0);
2719 } else {
2720 isl_schedule_tree_free(tree);
2721 tree = isl_schedule_node_get_leaf(node);
2723 node = isl_schedule_node_graft_tree(node, tree);
2725 return node;
2728 /* Internal data structure for the group_ancestor callback.
2730 * If "finished" is set, then we no longer need to modify
2731 * any further ancestors.
2733 * "contraction" and "expansion" represent the expansion
2734 * that reflects the grouping.
2736 * "domain" contains the domain elements that reach the position
2737 * where the grouping is performed. That is, it is the range
2738 * of the resulting expansion.
2739 * "domain_universe" is the universe of "domain".
2740 * "group" is the set of group elements, i.e., the domain
2741 * of the resulting expansion.
2742 * "group_universe" is the universe of "group".
2744 * "sched" is the schedule for the group elements, in pratice
2745 * an identity mapping on "group_universe".
2746 * "dim" is the dimension of "sched".
2748 struct isl_schedule_group_data {
2749 int finished;
2751 isl_union_map *expansion;
2752 isl_union_pw_multi_aff *contraction;
2754 isl_union_set *domain;
2755 isl_union_set *domain_universe;
2756 isl_union_set *group;
2757 isl_union_set *group_universe;
2759 int dim;
2760 isl_multi_aff *sched;
2763 /* Is domain covered by data->domain within data->domain_universe?
2765 static isl_bool locally_covered_by_domain(__isl_keep isl_union_set *domain,
2766 struct isl_schedule_group_data *data)
2768 isl_bool is_subset;
2769 isl_union_set *test;
2771 test = isl_union_set_copy(domain);
2772 test = isl_union_set_intersect(test,
2773 isl_union_set_copy(data->domain_universe));
2774 is_subset = isl_union_set_is_subset(test, data->domain);
2775 isl_union_set_free(test);
2777 return is_subset;
2780 /* Update the band 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 * Add the part of the identity schedule on the group instances data->sched
2786 * that corresponds to this band node to the band schedule.
2787 * If the domain elements that reach the node and that are part
2788 * of data->domain_universe are all elements of data->domain (and therefore
2789 * replaced by the group instances) then this data->domain_universe
2790 * is removed from the domain of the band schedule.
2792 static __isl_give isl_schedule_tree *group_band(
2793 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2794 struct isl_schedule_group_data *data)
2796 isl_union_set *domain;
2797 isl_multi_aff *ma;
2798 isl_multi_union_pw_aff *mupa, *partial;
2799 isl_bool is_covered;
2800 int depth, n;
2801 isl_bool has_id;
2803 domain = isl_schedule_node_get_domain(pos);
2804 is_covered = locally_covered_by_domain(domain, data);
2805 if (is_covered >= 0 && is_covered) {
2806 domain = isl_union_set_universe(domain);
2807 domain = isl_union_set_subtract(domain,
2808 isl_union_set_copy(data->domain_universe));
2809 tree = isl_schedule_tree_band_intersect_domain(tree, domain);
2810 } else
2811 isl_union_set_free(domain);
2812 if (is_covered < 0)
2813 return isl_schedule_tree_free(tree);
2814 depth = isl_schedule_node_get_schedule_depth(pos);
2815 n = isl_schedule_tree_band_n_member(tree);
2816 ma = isl_multi_aff_copy(data->sched);
2817 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, depth);
2818 ma = isl_multi_aff_drop_dims(ma, isl_dim_out, n, data->dim - depth - n);
2819 mupa = isl_multi_union_pw_aff_from_multi_aff(ma);
2820 partial = isl_schedule_tree_band_get_partial_schedule(tree);
2821 has_id = isl_multi_union_pw_aff_has_tuple_id(partial, isl_dim_set);
2822 if (has_id < 0) {
2823 partial = isl_multi_union_pw_aff_free(partial);
2824 } else if (has_id) {
2825 isl_id *id;
2826 id = isl_multi_union_pw_aff_get_tuple_id(partial, isl_dim_set);
2827 mupa = isl_multi_union_pw_aff_set_tuple_id(mupa,
2828 isl_dim_set, id);
2830 partial = isl_multi_union_pw_aff_union_add(partial, mupa);
2831 tree = isl_schedule_tree_band_set_partial_schedule(tree, partial);
2833 return tree;
2836 /* Drop the parameters in "uset" that are not also in "space".
2837 * "n" is the number of parameters in "space".
2839 static __isl_give isl_union_set *union_set_drop_extra_params(
2840 __isl_take isl_union_set *uset, __isl_keep isl_space *space, int n)
2842 int n2;
2844 uset = isl_union_set_align_params(uset, isl_space_copy(space));
2845 n2 = isl_union_set_dim(uset, isl_dim_param);
2846 uset = isl_union_set_project_out(uset, isl_dim_param, n, n2 - n);
2848 return uset;
2851 /* Update the context 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 do not actually need to update "tree" since a context node only
2857 * refers to the schedule space. However, we may need to update "data"
2858 * to not refer to any parameters introduced by the context node.
2860 static __isl_give isl_schedule_tree *group_context(
2861 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2862 struct isl_schedule_group_data *data)
2864 isl_space *space;
2865 isl_union_set *domain;
2866 int n1, n2;
2867 isl_bool involves;
2869 if (isl_schedule_node_get_tree_depth(pos) == 1)
2870 return tree;
2872 domain = isl_schedule_node_get_universe_domain(pos);
2873 space = isl_union_set_get_space(domain);
2874 isl_union_set_free(domain);
2876 n1 = isl_space_dim(space, isl_dim_param);
2877 data->expansion = isl_union_map_align_params(data->expansion, space);
2878 n2 = isl_union_map_dim(data->expansion, isl_dim_param);
2880 if (!data->expansion)
2881 return isl_schedule_tree_free(tree);
2882 if (n1 == n2)
2883 return tree;
2885 involves = isl_union_map_involves_dims(data->expansion,
2886 isl_dim_param, n1, n2 - n1);
2887 if (involves < 0)
2888 return isl_schedule_tree_free(tree);
2889 if (involves)
2890 isl_die(isl_schedule_node_get_ctx(pos), isl_error_invalid,
2891 "grouping cannot only refer to global parameters",
2892 return isl_schedule_tree_free(tree));
2894 data->expansion = isl_union_map_project_out(data->expansion,
2895 isl_dim_param, n1, n2 - n1);
2896 space = isl_union_map_get_space(data->expansion);
2898 data->contraction = isl_union_pw_multi_aff_align_params(
2899 data->contraction, isl_space_copy(space));
2900 n2 = isl_union_pw_multi_aff_dim(data->contraction, isl_dim_param);
2901 data->contraction = isl_union_pw_multi_aff_drop_dims(data->contraction,
2902 isl_dim_param, n1, n2 - n1);
2904 data->domain = union_set_drop_extra_params(data->domain, space, n1);
2905 data->domain_universe =
2906 union_set_drop_extra_params(data->domain_universe, space, n1);
2907 data->group = union_set_drop_extra_params(data->group, space, n1);
2908 data->group_universe =
2909 union_set_drop_extra_params(data->group_universe, space, n1);
2911 data->sched = isl_multi_aff_align_params(data->sched,
2912 isl_space_copy(space));
2913 n2 = isl_multi_aff_dim(data->sched, isl_dim_param);
2914 data->sched = isl_multi_aff_drop_dims(data->sched,
2915 isl_dim_param, n1, n2 - n1);
2917 isl_space_free(space);
2919 return tree;
2922 /* Update the domain tree root "tree" to refer to the group instances
2923 * in data->group rather than the original domain elements in data->domain.
2924 * "pos" is the position in the original schedule tree where the modified
2925 * "tree" will be attached.
2927 * We first double-check that all grouped domain elements are actually
2928 * part of the root domain and then replace those elements by the group
2929 * instances.
2931 static __isl_give isl_schedule_tree *group_domain(
2932 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2933 struct isl_schedule_group_data *data)
2935 isl_union_set *domain;
2936 isl_bool is_subset;
2938 domain = isl_schedule_tree_domain_get_domain(tree);
2939 is_subset = isl_union_set_is_subset(data->domain, domain);
2940 isl_union_set_free(domain);
2941 if (is_subset < 0)
2942 return isl_schedule_tree_free(tree);
2943 if (!is_subset)
2944 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
2945 "grouped domain should be part of outer domain",
2946 return isl_schedule_tree_free(tree));
2947 domain = isl_schedule_tree_domain_get_domain(tree);
2948 domain = isl_union_set_subtract(domain,
2949 isl_union_set_copy(data->domain));
2950 domain = isl_union_set_union(domain, isl_union_set_copy(data->group));
2951 tree = isl_schedule_tree_domain_set_domain(tree, domain);
2953 return tree;
2956 /* Update the expansion tree root "tree" to refer to the group instances
2957 * in data->group rather than the original domain elements in data->domain.
2958 * "pos" is the position in the original schedule tree where the modified
2959 * "tree" will be attached.
2961 * Let G_1 -> D_1 be the expansion of "tree" and G_2 -> D_2 the newly
2962 * introduced expansion in a descendant of "tree".
2963 * We first double-check that D_2 is a subset of D_1.
2964 * Then we remove D_2 from the range of G_1 -> D_1 and add the mapping
2965 * G_1 -> D_1 . D_2 -> G_2.
2966 * Simmilarly, we restrict the domain of the contraction to the universe
2967 * of the range of the updated expansion and add G_2 -> D_2 . D_1 -> G_1,
2968 * attempting to remove the domain constraints of this additional part.
2970 static __isl_give isl_schedule_tree *group_expansion(
2971 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
2972 struct isl_schedule_group_data *data)
2974 isl_union_set *domain;
2975 isl_union_map *expansion, *umap;
2976 isl_union_pw_multi_aff *contraction, *upma;
2977 int is_subset;
2979 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2980 domain = isl_union_map_range(expansion);
2981 is_subset = isl_union_set_is_subset(data->domain, domain);
2982 isl_union_set_free(domain);
2983 if (is_subset < 0)
2984 return isl_schedule_tree_free(tree);
2985 if (!is_subset)
2986 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_internal,
2987 "grouped domain should be part "
2988 "of outer expansion domain",
2989 return isl_schedule_tree_free(tree));
2990 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2991 umap = isl_union_map_from_union_pw_multi_aff(
2992 isl_union_pw_multi_aff_copy(data->contraction));
2993 umap = isl_union_map_apply_range(expansion, umap);
2994 expansion = isl_schedule_tree_expansion_get_expansion(tree);
2995 expansion = isl_union_map_subtract_range(expansion,
2996 isl_union_set_copy(data->domain));
2997 expansion = isl_union_map_union(expansion, umap);
2998 umap = isl_union_map_universe(isl_union_map_copy(expansion));
2999 domain = isl_union_map_range(umap);
3000 contraction = isl_schedule_tree_expansion_get_contraction(tree);
3001 umap = isl_union_map_from_union_pw_multi_aff(contraction);
3002 umap = isl_union_map_apply_range(isl_union_map_copy(data->expansion),
3003 umap);
3004 upma = isl_union_pw_multi_aff_from_union_map(umap);
3005 contraction = isl_schedule_tree_expansion_get_contraction(tree);
3006 contraction = isl_union_pw_multi_aff_intersect_domain(contraction,
3007 domain);
3008 domain = isl_union_pw_multi_aff_domain(
3009 isl_union_pw_multi_aff_copy(upma));
3010 upma = isl_union_pw_multi_aff_gist(upma, domain);
3011 contraction = isl_union_pw_multi_aff_union_add(contraction, upma);
3012 tree = isl_schedule_tree_expansion_set_contraction_and_expansion(tree,
3013 contraction, expansion);
3015 return tree;
3018 /* Update the tree root "tree" to refer to the group instances
3019 * in data->group rather than the original domain elements in data->domain.
3020 * "pos" is the position in the original schedule tree where the modified
3021 * "tree" will be attached.
3023 * If we have come across a domain or expansion node before (data->finished
3024 * is set), then we no longer need perform any modifications.
3026 * If "tree" is a filter, then we add data->group_universe to the filter.
3027 * We also remove data->domain_universe from the filter if all the domain
3028 * elements in this universe that reach the filter node are part of
3029 * the elements that are being grouped by data->expansion.
3030 * If "tree" is a band, domain or expansion, then it is handled
3031 * in a separate function.
3033 static __isl_give isl_schedule_tree *group_ancestor(
3034 __isl_take isl_schedule_tree *tree, __isl_keep isl_schedule_node *pos,
3035 void *user)
3037 struct isl_schedule_group_data *data = user;
3038 isl_union_set *domain;
3039 isl_bool is_covered;
3041 if (!tree || !pos)
3042 return isl_schedule_tree_free(tree);
3044 if (data->finished)
3045 return tree;
3047 switch (isl_schedule_tree_get_type(tree)) {
3048 case isl_schedule_node_error:
3049 return isl_schedule_tree_free(tree);
3050 case isl_schedule_node_extension:
3051 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
3052 "grouping not allowed in extended tree",
3053 return isl_schedule_tree_free(tree));
3054 case isl_schedule_node_band:
3055 tree = group_band(tree, pos, data);
3056 break;
3057 case isl_schedule_node_context:
3058 tree = group_context(tree, pos, data);
3059 break;
3060 case isl_schedule_node_domain:
3061 tree = group_domain(tree, pos, data);
3062 data->finished = 1;
3063 break;
3064 case isl_schedule_node_filter:
3065 domain = isl_schedule_node_get_domain(pos);
3066 is_covered = locally_covered_by_domain(domain, data);
3067 isl_union_set_free(domain);
3068 if (is_covered < 0)
3069 return isl_schedule_tree_free(tree);
3070 domain = isl_schedule_tree_filter_get_filter(tree);
3071 if (is_covered)
3072 domain = isl_union_set_subtract(domain,
3073 isl_union_set_copy(data->domain_universe));
3074 domain = isl_union_set_union(domain,
3075 isl_union_set_copy(data->group_universe));
3076 tree = isl_schedule_tree_filter_set_filter(tree, domain);
3077 break;
3078 case isl_schedule_node_expansion:
3079 tree = group_expansion(tree, pos, data);
3080 data->finished = 1;
3081 break;
3082 case isl_schedule_node_leaf:
3083 case isl_schedule_node_guard:
3084 case isl_schedule_node_mark:
3085 case isl_schedule_node_sequence:
3086 case isl_schedule_node_set:
3087 break;
3090 return tree;
3093 /* Group the domain elements that reach "node" into instances
3094 * of a single statement with identifier "group_id".
3095 * In particular, group the domain elements according to their
3096 * prefix schedule.
3098 * That is, introduce an expansion node with as contraction
3099 * the prefix schedule (with the target space replaced by "group_id")
3100 * and as expansion the inverse of this contraction (with its range
3101 * intersected with the domain elements that reach "node").
3102 * The outer nodes are then modified to refer to the group instances
3103 * instead of the original domain elements.
3105 * No instance of "group_id" is allowed to reach "node" prior
3106 * to the grouping.
3107 * No ancestor of "node" is allowed to be an extension node.
3109 * Return a pointer to original node in tree, i.e., the child
3110 * of the newly introduced expansion node.
3112 __isl_give isl_schedule_node *isl_schedule_node_group(
3113 __isl_take isl_schedule_node *node, __isl_take isl_id *group_id)
3115 struct isl_schedule_group_data data = { 0 };
3116 isl_space *space;
3117 isl_union_set *domain;
3118 isl_union_pw_multi_aff *contraction;
3119 isl_union_map *expansion;
3120 isl_bool disjoint;
3122 if (!node || !group_id)
3123 goto error;
3124 if (check_insert(node) < 0)
3125 goto error;
3127 domain = isl_schedule_node_get_domain(node);
3128 data.domain = isl_union_set_copy(domain);
3129 data.domain_universe = isl_union_set_copy(domain);
3130 data.domain_universe = isl_union_set_universe(data.domain_universe);
3132 data.dim = isl_schedule_node_get_schedule_depth(node);
3133 if (data.dim == 0) {
3134 isl_ctx *ctx;
3135 isl_set *set;
3136 isl_union_set *group;
3137 isl_union_map *univ;
3139 ctx = isl_schedule_node_get_ctx(node);
3140 space = isl_space_set_alloc(ctx, 0, 0);
3141 space = isl_space_set_tuple_id(space, isl_dim_set, group_id);
3142 set = isl_set_universe(isl_space_copy(space));
3143 group = isl_union_set_from_set(set);
3144 expansion = isl_union_map_from_domain_and_range(domain, group);
3145 univ = isl_union_map_universe(isl_union_map_copy(expansion));
3146 contraction = isl_union_pw_multi_aff_from_union_map(univ);
3147 expansion = isl_union_map_reverse(expansion);
3148 } else {
3149 isl_multi_union_pw_aff *prefix;
3150 isl_union_set *univ;
3152 prefix =
3153 isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
3154 prefix = isl_multi_union_pw_aff_set_tuple_id(prefix,
3155 isl_dim_set, group_id);
3156 space = isl_multi_union_pw_aff_get_space(prefix);
3157 contraction = isl_union_pw_multi_aff_from_multi_union_pw_aff(
3158 prefix);
3159 univ = isl_union_set_universe(isl_union_set_copy(domain));
3160 contraction =
3161 isl_union_pw_multi_aff_intersect_domain(contraction, univ);
3162 expansion = isl_union_map_from_union_pw_multi_aff(
3163 isl_union_pw_multi_aff_copy(contraction));
3164 expansion = isl_union_map_reverse(expansion);
3165 expansion = isl_union_map_intersect_range(expansion, domain);
3167 space = isl_space_map_from_set(space);
3168 data.sched = isl_multi_aff_identity(space);
3169 data.group = isl_union_map_domain(isl_union_map_copy(expansion));
3170 data.group = isl_union_set_coalesce(data.group);
3171 data.group_universe = isl_union_set_copy(data.group);
3172 data.group_universe = isl_union_set_universe(data.group_universe);
3173 data.expansion = isl_union_map_copy(expansion);
3174 data.contraction = isl_union_pw_multi_aff_copy(contraction);
3175 node = isl_schedule_node_insert_expansion(node, contraction, expansion);
3177 disjoint = isl_union_set_is_disjoint(data.domain_universe,
3178 data.group_universe);
3180 node = update_ancestors(node, &group_ancestor, &data);
3182 isl_union_set_free(data.domain);
3183 isl_union_set_free(data.domain_universe);
3184 isl_union_set_free(data.group);
3185 isl_union_set_free(data.group_universe);
3186 isl_multi_aff_free(data.sched);
3187 isl_union_map_free(data.expansion);
3188 isl_union_pw_multi_aff_free(data.contraction);
3190 node = isl_schedule_node_child(node, 0);
3192 if (!node || disjoint < 0)
3193 return isl_schedule_node_free(node);
3194 if (!disjoint)
3195 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
3196 "group instances already reach node",
3197 return isl_schedule_node_free(node));
3199 return node;
3200 error:
3201 isl_schedule_node_free(node);
3202 isl_id_free(group_id);
3203 return NULL;
3206 /* Compute the gist of the given band node with respect to "context".
3208 __isl_give isl_schedule_node *isl_schedule_node_band_gist(
3209 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
3211 isl_schedule_tree *tree;
3213 tree = isl_schedule_node_get_tree(node);
3214 tree = isl_schedule_tree_band_gist(tree, context);
3215 return isl_schedule_node_graft_tree(node, tree);
3218 /* Internal data structure for isl_schedule_node_gist.
3219 * "n_expansion" is the number of outer expansion nodes
3220 * with respect to the current position
3221 * "filters" contains an element for each outer filter, expansion or
3222 * extension node with respect to the current position, each representing
3223 * the intersection of the previous element and the filter on the filter node
3224 * or the expansion/extension of the previous element.
3225 * The first element in the original context passed to isl_schedule_node_gist.
3227 struct isl_node_gist_data {
3228 int n_expansion;
3229 isl_union_set_list *filters;
3232 /* Enter the expansion node "node" during a isl_schedule_node_gist traversal.
3234 * In particular, add an extra element to data->filters containing
3235 * the expansion of the previous element and replace the expansion
3236 * and contraction on "node" by the gist with respect to these filters.
3237 * Also keep track of the fact that we have entered another expansion.
3239 static __isl_give isl_schedule_node *gist_enter_expansion(
3240 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3242 int n;
3243 isl_union_set *inner;
3244 isl_union_map *expansion;
3245 isl_union_pw_multi_aff *contraction;
3247 data->n_expansion++;
3249 n = isl_union_set_list_n_union_set(data->filters);
3250 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3251 expansion = isl_schedule_node_expansion_get_expansion(node);
3252 inner = isl_union_set_apply(inner, expansion);
3254 contraction = isl_schedule_node_expansion_get_contraction(node);
3255 contraction = isl_union_pw_multi_aff_gist(contraction,
3256 isl_union_set_copy(inner));
3258 data->filters = isl_union_set_list_add(data->filters, inner);
3260 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3261 expansion = isl_schedule_node_expansion_get_expansion(node);
3262 expansion = isl_union_map_gist_domain(expansion, inner);
3263 node = isl_schedule_node_expansion_set_contraction_and_expansion(node,
3264 contraction, expansion);
3266 return node;
3269 /* Leave the expansion node "node" during a isl_schedule_node_gist traversal.
3271 * In particular, remove the element in data->filters that was added by
3272 * gist_enter_expansion and decrement the number of outer expansions.
3274 * The expansion has already been simplified in gist_enter_expansion.
3275 * If this simplification results in an identity expansion, then
3276 * it is removed here.
3278 static __isl_give isl_schedule_node *gist_leave_expansion(
3279 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3281 int n;
3282 isl_bool identity;
3283 isl_union_map *expansion;
3285 expansion = isl_schedule_node_expansion_get_expansion(node);
3286 identity = isl_union_map_is_identity(expansion);
3287 isl_union_map_free(expansion);
3289 if (identity < 0)
3290 node = isl_schedule_node_free(node);
3291 else if (identity)
3292 node = isl_schedule_node_delete(node);
3294 n = isl_union_set_list_n_union_set(data->filters);
3295 data->filters = isl_union_set_list_drop(data->filters, n - 1, 1);
3297 data->n_expansion--;
3299 return node;
3302 /* Enter the extension node "node" during a isl_schedule_node_gist traversal.
3304 * In particular, add an extra element to data->filters containing
3305 * the union of the previous element with the additional domain elements
3306 * introduced by the extension.
3308 static __isl_give isl_schedule_node *gist_enter_extension(
3309 __isl_take isl_schedule_node *node, struct isl_node_gist_data *data)
3311 int n;
3312 isl_union_set *inner, *extra;
3313 isl_union_map *extension;
3315 n = isl_union_set_list_n_union_set(data->filters);
3316 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3317 extension = isl_schedule_node_extension_get_extension(node);
3318 extra = isl_union_map_range(extension);
3319 inner = isl_union_set_union(inner, extra);
3321 data->filters = isl_union_set_list_add(data->filters, inner);
3323 return node;
3326 /* Can we finish gisting at this node?
3327 * That is, is the filter on the current filter node a subset of
3328 * the original context passed to isl_schedule_node_gist?
3329 * If we have gone through any expansions, then we cannot perform
3330 * this test since the current domain elements are incomparable
3331 * to the domain elements in the original context.
3333 static isl_bool gist_done(__isl_keep isl_schedule_node *node,
3334 struct isl_node_gist_data *data)
3336 isl_union_set *filter, *outer;
3337 isl_bool subset;
3339 if (data->n_expansion != 0)
3340 return isl_bool_false;
3342 filter = isl_schedule_node_filter_get_filter(node);
3343 outer = isl_union_set_list_get_union_set(data->filters, 0);
3344 subset = isl_union_set_is_subset(filter, outer);
3345 isl_union_set_free(outer);
3346 isl_union_set_free(filter);
3348 return subset;
3351 /* Callback for "traverse" to enter a node and to move
3352 * to the deepest initial subtree that should be traversed
3353 * by isl_schedule_node_gist.
3355 * The "filters" list is extended by one element each time
3356 * we come across a filter node by the result of intersecting
3357 * the last element in the list with the filter on the filter node.
3359 * If the filter on the current filter node is a subset of
3360 * the original context passed to isl_schedule_node_gist,
3361 * then there is no need to go into its subtree since it cannot
3362 * be further simplified by the context. The "filters" list is
3363 * still extended for consistency, but the actual value of the
3364 * added element is immaterial since it will not be used.
3366 * Otherwise, the filter on the current filter node is replaced by
3367 * the gist of the original filter with respect to the intersection
3368 * of the original context with the intermediate filters.
3370 * If the new element in the "filters" list is empty, then no elements
3371 * can reach the descendants of the current filter node. The subtree
3372 * underneath the filter node is therefore removed.
3374 * Each expansion node we come across is handled by
3375 * gist_enter_expansion.
3377 * Each extension node we come across is handled by
3378 * gist_enter_extension.
3380 static __isl_give isl_schedule_node *gist_enter(
3381 __isl_take isl_schedule_node *node, void *user)
3383 struct isl_node_gist_data *data = user;
3385 do {
3386 isl_union_set *filter, *inner;
3387 isl_bool done, empty;
3388 int n;
3390 switch (isl_schedule_node_get_type(node)) {
3391 case isl_schedule_node_error:
3392 return isl_schedule_node_free(node);
3393 case isl_schedule_node_expansion:
3394 node = gist_enter_expansion(node, data);
3395 continue;
3396 case isl_schedule_node_extension:
3397 node = gist_enter_extension(node, data);
3398 continue;
3399 case isl_schedule_node_band:
3400 case isl_schedule_node_context:
3401 case isl_schedule_node_domain:
3402 case isl_schedule_node_guard:
3403 case isl_schedule_node_leaf:
3404 case isl_schedule_node_mark:
3405 case isl_schedule_node_sequence:
3406 case isl_schedule_node_set:
3407 continue;
3408 case isl_schedule_node_filter:
3409 break;
3411 done = gist_done(node, data);
3412 filter = isl_schedule_node_filter_get_filter(node);
3413 if (done < 0 || done) {
3414 data->filters = isl_union_set_list_add(data->filters,
3415 filter);
3416 if (done < 0)
3417 return isl_schedule_node_free(node);
3418 return node;
3420 n = isl_union_set_list_n_union_set(data->filters);
3421 inner = isl_union_set_list_get_union_set(data->filters, n - 1);
3422 filter = isl_union_set_gist(filter, isl_union_set_copy(inner));
3423 node = isl_schedule_node_filter_set_filter(node,
3424 isl_union_set_copy(filter));
3425 filter = isl_union_set_intersect(filter, inner);
3426 empty = isl_union_set_is_empty(filter);
3427 data->filters = isl_union_set_list_add(data->filters, filter);
3428 if (empty < 0)
3429 return isl_schedule_node_free(node);
3430 if (!empty)
3431 continue;
3432 node = isl_schedule_node_child(node, 0);
3433 node = isl_schedule_node_cut(node);
3434 node = isl_schedule_node_parent(node);
3435 return node;
3436 } while (isl_schedule_node_has_children(node) &&
3437 (node = isl_schedule_node_first_child(node)) != NULL);
3439 return node;
3442 /* Callback for "traverse" to leave a node for isl_schedule_node_gist.
3444 * In particular, if the current node is a filter node, then we remove
3445 * the element on the "filters" list that was added when we entered
3446 * the node. There is no need to compute any gist here, since we
3447 * already did that when we entered the node.
3449 * Expansion nodes are handled by gist_leave_expansion.
3451 * If the current node is an extension, then remove the element
3452 * in data->filters that was added by gist_enter_extension.
3454 * If the current node is a band node, then we compute the gist of
3455 * the band node with respect to the intersection of the original context
3456 * and the intermediate filters.
3458 * If the current node is a sequence or set node, then some of
3459 * the filter children may have become empty and so they are removed.
3460 * If only one child is left, then the set or sequence node along with
3461 * the single remaining child filter is removed. The filter can be
3462 * removed because the filters on a sequence or set node are supposed
3463 * to partition the incoming domain instances.
3464 * In principle, it should then be impossible for there to be zero
3465 * remaining children, but should this happen, we replace the entire
3466 * subtree with an empty filter.
3468 static __isl_give isl_schedule_node *gist_leave(
3469 __isl_take isl_schedule_node *node, void *user)
3471 struct isl_node_gist_data *data = user;
3472 isl_schedule_tree *tree;
3473 int i, n;
3474 isl_union_set *filter;
3476 switch (isl_schedule_node_get_type(node)) {
3477 case isl_schedule_node_error:
3478 return isl_schedule_node_free(node);
3479 case isl_schedule_node_expansion:
3480 node = gist_leave_expansion(node, data);
3481 break;
3482 case isl_schedule_node_extension:
3483 case isl_schedule_node_filter:
3484 n = isl_union_set_list_n_union_set(data->filters);
3485 data->filters = isl_union_set_list_drop(data->filters,
3486 n - 1, 1);
3487 break;
3488 case isl_schedule_node_band:
3489 n = isl_union_set_list_n_union_set(data->filters);
3490 filter = isl_union_set_list_get_union_set(data->filters, n - 1);
3491 node = isl_schedule_node_band_gist(node, filter);
3492 break;
3493 case isl_schedule_node_set:
3494 case isl_schedule_node_sequence:
3495 tree = isl_schedule_node_get_tree(node);
3496 n = isl_schedule_tree_n_children(tree);
3497 for (i = n - 1; i >= 0; --i) {
3498 isl_schedule_tree *child;
3499 isl_union_set *filter;
3500 isl_bool empty;
3502 child = isl_schedule_tree_get_child(tree, i);
3503 filter = isl_schedule_tree_filter_get_filter(child);
3504 empty = isl_union_set_is_empty(filter);
3505 isl_union_set_free(filter);
3506 isl_schedule_tree_free(child);
3507 if (empty < 0)
3508 tree = isl_schedule_tree_free(tree);
3509 else if (empty)
3510 tree = isl_schedule_tree_drop_child(tree, i);
3512 n = isl_schedule_tree_n_children(tree);
3513 node = isl_schedule_node_graft_tree(node, tree);
3514 if (n == 1) {
3515 node = isl_schedule_node_delete(node);
3516 node = isl_schedule_node_delete(node);
3517 } else if (n == 0) {
3518 isl_space *space;
3520 filter =
3521 isl_union_set_list_get_union_set(data->filters, 0);
3522 space = isl_union_set_get_space(filter);
3523 isl_union_set_free(filter);
3524 filter = isl_union_set_empty(space);
3525 node = isl_schedule_node_cut(node);
3526 node = isl_schedule_node_insert_filter(node, filter);
3528 break;
3529 case isl_schedule_node_context:
3530 case isl_schedule_node_domain:
3531 case isl_schedule_node_guard:
3532 case isl_schedule_node_leaf:
3533 case isl_schedule_node_mark:
3534 break;
3537 return node;
3540 /* Compute the gist of the subtree at "node" with respect to
3541 * the reaching domain elements in "context".
3542 * In particular, compute the gist of all band and filter nodes
3543 * in the subtree with respect to "context". Children of set or sequence
3544 * nodes that end up with an empty filter are removed completely.
3546 * We keep track of the intersection of "context" with all outer filters
3547 * of the current node within the subtree in the final element of "filters".
3548 * Initially, this list contains the single element "context" and it is
3549 * extended or shortened each time we enter or leave a filter node.
3551 __isl_give isl_schedule_node *isl_schedule_node_gist(
3552 __isl_take isl_schedule_node *node, __isl_take isl_union_set *context)
3554 struct isl_node_gist_data data;
3556 data.n_expansion = 0;
3557 data.filters = isl_union_set_list_from_union_set(context);
3558 node = traverse(node, &gist_enter, &gist_leave, &data);
3559 isl_union_set_list_free(data.filters);
3560 return node;
3563 /* Intersect the domain of domain node "node" with "domain".
3565 * If the domain of "node" is already a subset of "domain",
3566 * then nothing needs to be changed.
3568 * Otherwise, we replace the domain of the domain node by the intersection
3569 * and simplify the subtree rooted at "node" with respect to this intersection.
3571 __isl_give isl_schedule_node *isl_schedule_node_domain_intersect_domain(
3572 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain)
3574 isl_schedule_tree *tree;
3575 isl_union_set *uset;
3576 int is_subset;
3578 if (!node || !domain)
3579 goto error;
3581 uset = isl_schedule_tree_domain_get_domain(node->tree);
3582 is_subset = isl_union_set_is_subset(uset, domain);
3583 isl_union_set_free(uset);
3584 if (is_subset < 0)
3585 goto error;
3586 if (is_subset) {
3587 isl_union_set_free(domain);
3588 return node;
3591 tree = isl_schedule_tree_copy(node->tree);
3592 uset = isl_schedule_tree_domain_get_domain(tree);
3593 uset = isl_union_set_intersect(uset, domain);
3594 tree = isl_schedule_tree_domain_set_domain(tree,
3595 isl_union_set_copy(uset));
3596 node = isl_schedule_node_graft_tree(node, tree);
3598 node = isl_schedule_node_child(node, 0);
3599 node = isl_schedule_node_gist(node, uset);
3600 node = isl_schedule_node_parent(node);
3602 return node;
3603 error:
3604 isl_schedule_node_free(node);
3605 isl_union_set_free(domain);
3606 return NULL;
3609 /* Replace the domain of domain node "node" with the gist
3610 * of the original domain with respect to the parameter domain "context".
3612 __isl_give isl_schedule_node *isl_schedule_node_domain_gist_params(
3613 __isl_take isl_schedule_node *node, __isl_take isl_set *context)
3615 isl_union_set *domain;
3616 isl_schedule_tree *tree;
3618 if (!node || !context)
3619 goto error;
3621 tree = isl_schedule_tree_copy(node->tree);
3622 domain = isl_schedule_tree_domain_get_domain(node->tree);
3623 domain = isl_union_set_gist_params(domain, context);
3624 tree = isl_schedule_tree_domain_set_domain(tree, domain);
3625 node = isl_schedule_node_graft_tree(node, tree);
3627 return node;
3628 error:
3629 isl_schedule_node_free(node);
3630 isl_set_free(context);
3631 return NULL;
3634 /* Internal data structure for isl_schedule_node_get_subtree_expansion.
3635 * "expansions" contains a list of accumulated expansions
3636 * for each outer expansion, set or sequence node. The first element
3637 * in the list is an identity mapping on the reaching domain elements.
3638 * "res" collects the results.
3640 struct isl_subtree_expansion_data {
3641 isl_union_map_list *expansions;
3642 isl_union_map *res;
3645 /* Callback for "traverse" to enter a node and to move
3646 * to the deepest initial subtree that should be traversed
3647 * by isl_schedule_node_get_subtree_expansion.
3649 * Whenever we come across an expansion node, the last element
3650 * of data->expansions is combined with the expansion
3651 * on the expansion node.
3653 * Whenever we come across a filter node that is the child
3654 * of a set or sequence node, data->expansions is extended
3655 * with a new element that restricts the previous element
3656 * to the elements selected by the filter.
3657 * The previous element can then be reused while backtracking.
3659 static __isl_give isl_schedule_node *subtree_expansion_enter(
3660 __isl_take isl_schedule_node *node, void *user)
3662 struct isl_subtree_expansion_data *data = user;
3664 do {
3665 enum isl_schedule_node_type type;
3666 isl_union_set *filter;
3667 isl_union_map *inner, *expansion;
3668 int n;
3670 switch (isl_schedule_node_get_type(node)) {
3671 case isl_schedule_node_error:
3672 return isl_schedule_node_free(node);
3673 case isl_schedule_node_filter:
3674 type = isl_schedule_node_get_parent_type(node);
3675 if (type != isl_schedule_node_set &&
3676 type != isl_schedule_node_sequence)
3677 break;
3678 filter = isl_schedule_node_filter_get_filter(node);
3679 n = isl_union_map_list_n_union_map(data->expansions);
3680 inner =
3681 isl_union_map_list_get_union_map(data->expansions,
3682 n - 1);
3683 inner = isl_union_map_intersect_range(inner, filter);
3684 data->expansions =
3685 isl_union_map_list_add(data->expansions, inner);
3686 break;
3687 case isl_schedule_node_expansion:
3688 n = isl_union_map_list_n_union_map(data->expansions);
3689 expansion =
3690 isl_schedule_node_expansion_get_expansion(node);
3691 inner =
3692 isl_union_map_list_get_union_map(data->expansions,
3693 n - 1);
3694 inner = isl_union_map_apply_range(inner, expansion);
3695 data->expansions =
3696 isl_union_map_list_set_union_map(data->expansions,
3697 n - 1, inner);
3698 break;
3699 case isl_schedule_node_band:
3700 case isl_schedule_node_context:
3701 case isl_schedule_node_domain:
3702 case isl_schedule_node_extension:
3703 case isl_schedule_node_guard:
3704 case isl_schedule_node_leaf:
3705 case isl_schedule_node_mark:
3706 case isl_schedule_node_sequence:
3707 case isl_schedule_node_set:
3708 break;
3710 } while (isl_schedule_node_has_children(node) &&
3711 (node = isl_schedule_node_first_child(node)) != NULL);
3713 return node;
3716 /* Callback for "traverse" to leave a node for
3717 * isl_schedule_node_get_subtree_expansion.
3719 * If we come across a filter node that is the child
3720 * of a set or sequence node, then we remove the element
3721 * of data->expansions that was added in subtree_expansion_enter.
3723 * If we reach a leaf node, then the accumulated expansion is
3724 * added to data->res.
3726 static __isl_give isl_schedule_node *subtree_expansion_leave(
3727 __isl_take isl_schedule_node *node, void *user)
3729 struct isl_subtree_expansion_data *data = user;
3730 int n;
3731 isl_union_map *inner;
3732 enum isl_schedule_node_type type;
3734 switch (isl_schedule_node_get_type(node)) {
3735 case isl_schedule_node_error:
3736 return isl_schedule_node_free(node);
3737 case isl_schedule_node_filter:
3738 type = isl_schedule_node_get_parent_type(node);
3739 if (type != isl_schedule_node_set &&
3740 type != isl_schedule_node_sequence)
3741 break;
3742 n = isl_union_map_list_n_union_map(data->expansions);
3743 data->expansions = isl_union_map_list_drop(data->expansions,
3744 n - 1, 1);
3745 break;
3746 case isl_schedule_node_leaf:
3747 n = isl_union_map_list_n_union_map(data->expansions);
3748 inner = isl_union_map_list_get_union_map(data->expansions,
3749 n - 1);
3750 data->res = isl_union_map_union(data->res, inner);
3751 break;
3752 case isl_schedule_node_band:
3753 case isl_schedule_node_context:
3754 case isl_schedule_node_domain:
3755 case isl_schedule_node_expansion:
3756 case isl_schedule_node_extension:
3757 case isl_schedule_node_guard:
3758 case isl_schedule_node_mark:
3759 case isl_schedule_node_sequence:
3760 case isl_schedule_node_set:
3761 break;
3764 return node;
3767 /* Return a mapping from the domain elements that reach "node"
3768 * to the corresponding domain elements in the leaves of the subtree
3769 * rooted at "node" obtained by composing the intermediate expansions.
3771 * We start out with an identity mapping between the domain elements
3772 * that reach "node" and compose it with all the expansions
3773 * on a path from "node" to a leaf while traversing the subtree.
3774 * Within the children of an a sequence or set node, the
3775 * accumulated expansion is restricted to the elements selected
3776 * by the filter child.
3778 __isl_give isl_union_map *isl_schedule_node_get_subtree_expansion(
3779 __isl_keep isl_schedule_node *node)
3781 struct isl_subtree_expansion_data data;
3782 isl_space *space;
3783 isl_union_set *domain;
3784 isl_union_map *expansion;
3786 if (!node)
3787 return NULL;
3789 domain = isl_schedule_node_get_universe_domain(node);
3790 space = isl_union_set_get_space(domain);
3791 expansion = isl_union_set_identity(domain);
3792 data.res = isl_union_map_empty(space);
3793 data.expansions = isl_union_map_list_from_union_map(expansion);
3795 node = isl_schedule_node_copy(node);
3796 node = traverse(node, &subtree_expansion_enter,
3797 &subtree_expansion_leave, &data);
3798 if (!node)
3799 data.res = isl_union_map_free(data.res);
3800 isl_schedule_node_free(node);
3802 isl_union_map_list_free(data.expansions);
3804 return data.res;
3807 /* Internal data structure for isl_schedule_node_get_subtree_contraction.
3808 * "contractions" contains a list of accumulated contractions
3809 * for each outer expansion, set or sequence node. The first element
3810 * in the list is an identity mapping on the reaching domain elements.
3811 * "res" collects the results.
3813 struct isl_subtree_contraction_data {
3814 isl_union_pw_multi_aff_list *contractions;
3815 isl_union_pw_multi_aff *res;
3818 /* Callback for "traverse" to enter a node and to move
3819 * to the deepest initial subtree that should be traversed
3820 * by isl_schedule_node_get_subtree_contraction.
3822 * Whenever we come across an expansion node, the last element
3823 * of data->contractions is combined with the contraction
3824 * on the expansion node.
3826 * Whenever we come across a filter node that is the child
3827 * of a set or sequence node, data->contractions is extended
3828 * with a new element that restricts the previous element
3829 * to the elements selected by the filter.
3830 * The previous element can then be reused while backtracking.
3832 static __isl_give isl_schedule_node *subtree_contraction_enter(
3833 __isl_take isl_schedule_node *node, void *user)
3835 struct isl_subtree_contraction_data *data = user;
3837 do {
3838 enum isl_schedule_node_type type;
3839 isl_union_set *filter;
3840 isl_union_pw_multi_aff *inner, *contraction;
3841 int n;
3843 switch (isl_schedule_node_get_type(node)) {
3844 case isl_schedule_node_error:
3845 return isl_schedule_node_free(node);
3846 case isl_schedule_node_filter:
3847 type = isl_schedule_node_get_parent_type(node);
3848 if (type != isl_schedule_node_set &&
3849 type != isl_schedule_node_sequence)
3850 break;
3851 filter = isl_schedule_node_filter_get_filter(node);
3852 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3853 data->contractions);
3854 inner =
3855 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3856 data->contractions, n - 1);
3857 inner = isl_union_pw_multi_aff_intersect_domain(inner,
3858 filter);
3859 data->contractions =
3860 isl_union_pw_multi_aff_list_add(data->contractions,
3861 inner);
3862 break;
3863 case isl_schedule_node_expansion:
3864 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3865 data->contractions);
3866 contraction =
3867 isl_schedule_node_expansion_get_contraction(node);
3868 inner =
3869 isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3870 data->contractions, n - 1);
3871 inner =
3872 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3873 inner, contraction);
3874 data->contractions =
3875 isl_union_pw_multi_aff_list_set_union_pw_multi_aff(
3876 data->contractions, n - 1, inner);
3877 break;
3878 case isl_schedule_node_band:
3879 case isl_schedule_node_context:
3880 case isl_schedule_node_domain:
3881 case isl_schedule_node_extension:
3882 case isl_schedule_node_guard:
3883 case isl_schedule_node_leaf:
3884 case isl_schedule_node_mark:
3885 case isl_schedule_node_sequence:
3886 case isl_schedule_node_set:
3887 break;
3889 } while (isl_schedule_node_has_children(node) &&
3890 (node = isl_schedule_node_first_child(node)) != NULL);
3892 return node;
3895 /* Callback for "traverse" to leave a node for
3896 * isl_schedule_node_get_subtree_contraction.
3898 * If we come across a filter node that is the child
3899 * of a set or sequence node, then we remove the element
3900 * of data->contractions that was added in subtree_contraction_enter.
3902 * If we reach a leaf node, then the accumulated contraction is
3903 * added to data->res.
3905 static __isl_give isl_schedule_node *subtree_contraction_leave(
3906 __isl_take isl_schedule_node *node, void *user)
3908 struct isl_subtree_contraction_data *data = user;
3909 int n;
3910 isl_union_pw_multi_aff *inner;
3911 enum isl_schedule_node_type type;
3913 switch (isl_schedule_node_get_type(node)) {
3914 case isl_schedule_node_error:
3915 return isl_schedule_node_free(node);
3916 case isl_schedule_node_filter:
3917 type = isl_schedule_node_get_parent_type(node);
3918 if (type != isl_schedule_node_set &&
3919 type != isl_schedule_node_sequence)
3920 break;
3921 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3922 data->contractions);
3923 data->contractions =
3924 isl_union_pw_multi_aff_list_drop(data->contractions,
3925 n - 1, 1);
3926 break;
3927 case isl_schedule_node_leaf:
3928 n = isl_union_pw_multi_aff_list_n_union_pw_multi_aff(
3929 data->contractions);
3930 inner = isl_union_pw_multi_aff_list_get_union_pw_multi_aff(
3931 data->contractions, n - 1);
3932 data->res = isl_union_pw_multi_aff_union_add(data->res, inner);
3933 break;
3934 case isl_schedule_node_band:
3935 case isl_schedule_node_context:
3936 case isl_schedule_node_domain:
3937 case isl_schedule_node_expansion:
3938 case isl_schedule_node_extension:
3939 case isl_schedule_node_guard:
3940 case isl_schedule_node_mark:
3941 case isl_schedule_node_sequence:
3942 case isl_schedule_node_set:
3943 break;
3946 return node;
3949 /* Return a mapping from the domain elements in the leaves of the subtree
3950 * rooted at "node" to the corresponding domain elements that reach "node"
3951 * obtained by composing the intermediate contractions.
3953 * We start out with an identity mapping between the domain elements
3954 * that reach "node" and compose it with all the contractions
3955 * on a path from "node" to a leaf while traversing the subtree.
3956 * Within the children of an a sequence or set node, the
3957 * accumulated contraction is restricted to the elements selected
3958 * by the filter child.
3960 __isl_give isl_union_pw_multi_aff *isl_schedule_node_get_subtree_contraction(
3961 __isl_keep isl_schedule_node *node)
3963 struct isl_subtree_contraction_data data;
3964 isl_space *space;
3965 isl_union_set *domain;
3966 isl_union_pw_multi_aff *contraction;
3968 if (!node)
3969 return NULL;
3971 domain = isl_schedule_node_get_universe_domain(node);
3972 space = isl_union_set_get_space(domain);
3973 contraction = isl_union_set_identity_union_pw_multi_aff(domain);
3974 data.res = isl_union_pw_multi_aff_empty(space);
3975 data.contractions =
3976 isl_union_pw_multi_aff_list_from_union_pw_multi_aff(contraction);
3978 node = isl_schedule_node_copy(node);
3979 node = traverse(node, &subtree_contraction_enter,
3980 &subtree_contraction_leave, &data);
3981 if (!node)
3982 data.res = isl_union_pw_multi_aff_free(data.res);
3983 isl_schedule_node_free(node);
3985 isl_union_pw_multi_aff_list_free(data.contractions);
3987 return data.res;
3990 /* Do the nearest "n" ancestors of "node" have the types given in "types"
3991 * (starting at the parent of "node")?
3993 static isl_bool has_ancestors(__isl_keep isl_schedule_node *node,
3994 int n, enum isl_schedule_node_type *types)
3996 int i, n_ancestor;
3998 if (!node)
3999 return isl_bool_error;
4001 n_ancestor = isl_schedule_tree_list_n_schedule_tree(node->ancestors);
4002 if (n_ancestor < n)
4003 return isl_bool_false;
4005 for (i = 0; i < n; ++i) {
4006 isl_schedule_tree *tree;
4007 int correct_type;
4009 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors,
4010 n_ancestor - 1 - i);
4011 if (!tree)
4012 return isl_bool_error;
4013 correct_type = isl_schedule_tree_get_type(tree) == types[i];
4014 isl_schedule_tree_free(tree);
4015 if (!correct_type)
4016 return isl_bool_false;
4019 return isl_bool_true;
4022 /* Given a node "node" that appears in an extension (i.e., it is the child
4023 * of a filter in a sequence inside an extension node), are the spaces
4024 * of the extension specified by "extension" disjoint from those
4025 * of both the original extension and the domain elements that reach
4026 * that original extension?
4028 static int is_disjoint_extension(__isl_keep isl_schedule_node *node,
4029 __isl_keep isl_union_map *extension)
4031 isl_union_map *old;
4032 isl_union_set *domain;
4033 int empty;
4035 node = isl_schedule_node_copy(node);
4036 node = isl_schedule_node_parent(node);
4037 node = isl_schedule_node_parent(node);
4038 node = isl_schedule_node_parent(node);
4039 old = isl_schedule_node_extension_get_extension(node);
4040 domain = isl_schedule_node_get_universe_domain(node);
4041 isl_schedule_node_free(node);
4042 old = isl_union_map_universe(old);
4043 domain = isl_union_set_union(domain, isl_union_map_range(old));
4044 extension = isl_union_map_copy(extension);
4045 extension = isl_union_map_intersect_range(extension, domain);
4046 empty = isl_union_map_is_empty(extension);
4047 isl_union_map_free(extension);
4049 return empty;
4052 /* Given a node "node" that is governed by an extension node, extend
4053 * that extension node with "extension".
4055 * In particular, "node" is the child of a filter in a sequence that
4056 * is in turn a child of an extension node. Extend that extension node
4057 * with "extension".
4059 * Return a pointer to the parent of the original node (i.e., a filter).
4061 static __isl_give isl_schedule_node *extend_extension(
4062 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
4064 int pos;
4065 isl_bool disjoint;
4066 isl_union_map *node_extension;
4068 node = isl_schedule_node_parent(node);
4069 pos = isl_schedule_node_get_child_position(node);
4070 node = isl_schedule_node_parent(node);
4071 node = isl_schedule_node_parent(node);
4072 node_extension = isl_schedule_node_extension_get_extension(node);
4073 disjoint = isl_union_map_is_disjoint(extension, node_extension);
4074 extension = isl_union_map_union(extension, node_extension);
4075 node = isl_schedule_node_extension_set_extension(node, extension);
4076 node = isl_schedule_node_child(node, 0);
4077 node = isl_schedule_node_child(node, pos);
4079 if (disjoint < 0)
4080 return isl_schedule_node_free(node);
4081 if (!node)
4082 return NULL;
4083 if (!disjoint)
4084 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4085 "extension domain should be disjoint from earlier "
4086 "extensions", return isl_schedule_node_free(node));
4088 return node;
4091 /* Return the universe of "uset" if this universe is disjoint from "ref".
4092 * Otherwise, return "uset".
4094 * Also check if "uset" itself is disjoint from "ref", reporting
4095 * an error if it is not.
4097 static __isl_give isl_union_set *replace_by_universe_if_disjoint(
4098 __isl_take isl_union_set *uset, __isl_keep isl_union_set *ref)
4100 int disjoint;
4101 isl_union_set *universe;
4103 disjoint = isl_union_set_is_disjoint(uset, ref);
4104 if (disjoint < 0)
4105 return isl_union_set_free(uset);
4106 if (!disjoint)
4107 isl_die(isl_union_set_get_ctx(uset), isl_error_invalid,
4108 "extension domain should be disjoint from "
4109 "current domain", return isl_union_set_free(uset));
4111 universe = isl_union_set_universe(isl_union_set_copy(uset));
4112 disjoint = isl_union_set_is_disjoint(universe, ref);
4113 if (disjoint >= 0 && disjoint) {
4114 isl_union_set_free(uset);
4115 return universe;
4117 isl_union_set_free(universe);
4119 if (disjoint < 0)
4120 return isl_union_set_free(uset);
4121 return uset;
4124 /* Insert an extension node on top of "node" with extension "extension".
4125 * In addition, insert a filter that separates node from the extension
4126 * between the extension node and "node".
4127 * Return a pointer to the inserted filter node.
4129 * If "node" already appears in an extension (i.e., if it is the child
4130 * of a filter in a sequence inside an extension node), then extend that
4131 * extension with "extension" instead.
4132 * In this case, a pointer to the original filter node is returned.
4133 * Note that if some of the elements in the new extension live in the
4134 * same space as those of the original extension or the domain elements
4135 * reaching the original extension, then we insert a new extension anyway.
4136 * Otherwise, we would have to adjust the filters in the sequence child
4137 * of the extension to ensure that the elements in the new extension
4138 * are filtered out.
4140 static __isl_give isl_schedule_node *insert_extension(
4141 __isl_take isl_schedule_node *node, __isl_take isl_union_map *extension)
4143 enum isl_schedule_node_type ancestors[] =
4144 { isl_schedule_node_filter, isl_schedule_node_sequence,
4145 isl_schedule_node_extension };
4146 isl_union_set *domain;
4147 isl_union_set *filter;
4148 isl_bool in_ext;
4150 in_ext = has_ancestors(node, 3, ancestors);
4151 if (in_ext < 0)
4152 goto error;
4153 if (in_ext) {
4154 int disjoint;
4156 disjoint = is_disjoint_extension(node, extension);
4157 if (disjoint < 0)
4158 goto error;
4159 if (disjoint)
4160 return extend_extension(node, extension);
4163 filter = isl_schedule_node_get_domain(node);
4164 domain = isl_union_map_range(isl_union_map_copy(extension));
4165 filter = replace_by_universe_if_disjoint(filter, domain);
4166 isl_union_set_free(domain);
4168 node = isl_schedule_node_insert_filter(node, filter);
4169 node = isl_schedule_node_insert_extension(node, extension);
4170 node = isl_schedule_node_child(node, 0);
4171 return node;
4172 error:
4173 isl_schedule_node_free(node);
4174 isl_union_map_free(extension);
4175 return NULL;
4178 /* Replace the subtree that "node" points to by "tree" (which has
4179 * a sequence root with two children), except if the parent of "node"
4180 * is a sequence as well, in which case "tree" is spliced at the position
4181 * of "node" in its parent.
4182 * Return a pointer to the child of the "tree_pos" (filter) child of "tree"
4183 * in the updated schedule tree.
4185 static __isl_give isl_schedule_node *graft_or_splice(
4186 __isl_take isl_schedule_node *node, __isl_take isl_schedule_tree *tree,
4187 int tree_pos)
4189 int pos;
4191 if (isl_schedule_node_get_parent_type(node) ==
4192 isl_schedule_node_sequence) {
4193 pos = isl_schedule_node_get_child_position(node);
4194 node = isl_schedule_node_parent(node);
4195 node = isl_schedule_node_sequence_splice(node, pos, tree);
4196 } else {
4197 pos = 0;
4198 node = isl_schedule_node_graft_tree(node, tree);
4200 node = isl_schedule_node_child(node, pos + tree_pos);
4201 node = isl_schedule_node_child(node, 0);
4203 return node;
4206 /* Insert a node "graft" into the schedule tree of "node" such that it
4207 * is executed before (if "before" is set) or after (if "before" is not set)
4208 * the node that "node" points to.
4209 * The root of "graft" is an extension node.
4210 * Return a pointer to the node that "node" pointed to.
4212 * We first insert an extension node on top of "node" (or extend
4213 * the extension node if there already is one), with a filter on "node"
4214 * separating it from the extension.
4215 * We then insert a filter in the graft to separate it from the original
4216 * domain elements and combine the original and new tree in a sequence.
4217 * If we have extended an extension node, then the children of this
4218 * sequence are spliced in the sequence of the extended extension
4219 * at the position where "node" appears in the original extension.
4220 * Otherwise, the sequence pair is attached to the new extension node.
4222 static __isl_give isl_schedule_node *graft_extension(
4223 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft,
4224 int before)
4226 isl_union_map *extension;
4227 isl_union_set *graft_domain;
4228 isl_union_set *node_domain;
4229 isl_schedule_tree *tree, *tree_graft;
4231 extension = isl_schedule_node_extension_get_extension(graft);
4232 graft_domain = isl_union_map_range(isl_union_map_copy(extension));
4233 node_domain = isl_schedule_node_get_universe_domain(node);
4234 node = insert_extension(node, extension);
4236 graft_domain = replace_by_universe_if_disjoint(graft_domain,
4237 node_domain);
4238 isl_union_set_free(node_domain);
4240 tree = isl_schedule_node_get_tree(node);
4241 if (!isl_schedule_node_has_children(graft)) {
4242 tree_graft = isl_schedule_tree_from_filter(graft_domain);
4243 } else {
4244 graft = isl_schedule_node_child(graft, 0);
4245 tree_graft = isl_schedule_node_get_tree(graft);
4246 tree_graft = isl_schedule_tree_insert_filter(tree_graft,
4247 graft_domain);
4249 if (before)
4250 tree = isl_schedule_tree_sequence_pair(tree_graft, tree);
4251 else
4252 tree = isl_schedule_tree_sequence_pair(tree, tree_graft);
4253 node = graft_or_splice(node, tree, before);
4255 isl_schedule_node_free(graft);
4257 return node;
4260 /* Replace the root domain node of "node" by an extension node suitable
4261 * for insertion at "pos".
4262 * That is, create an extension node that maps the outer band nodes
4263 * at "pos" to the domain of the root node of "node" and attach
4264 * the child of this root node to the extension node.
4266 static __isl_give isl_schedule_node *extension_from_domain(
4267 __isl_take isl_schedule_node *node, __isl_keep isl_schedule_node *pos)
4269 isl_union_set *universe;
4270 isl_union_set *domain;
4271 isl_union_map *ext;
4272 int depth;
4273 isl_bool anchored;
4274 isl_space *space;
4275 isl_schedule_node *res;
4276 isl_schedule_tree *tree;
4278 anchored = isl_schedule_node_is_subtree_anchored(node);
4279 if (anchored < 0)
4280 return isl_schedule_node_free(node);
4281 if (anchored)
4282 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
4283 "cannot graft anchored tree with domain root",
4284 return isl_schedule_node_free(node));
4286 depth = isl_schedule_node_get_schedule_depth(pos);
4287 domain = isl_schedule_node_domain_get_domain(node);
4288 space = isl_union_set_get_space(domain);
4289 space = isl_space_set_from_params(space);
4290 space = isl_space_add_dims(space, isl_dim_set, depth);
4291 universe = isl_union_set_from_set(isl_set_universe(space));
4292 ext = isl_union_map_from_domain_and_range(universe, domain);
4293 res = isl_schedule_node_from_extension(ext);
4294 node = isl_schedule_node_child(node, 0);
4295 if (!node)
4296 return isl_schedule_node_free(res);
4297 if (!isl_schedule_tree_is_leaf(node->tree)) {
4298 tree = isl_schedule_node_get_tree(node);
4299 res = isl_schedule_node_child(res, 0);
4300 res = isl_schedule_node_graft_tree(res, tree);
4301 res = isl_schedule_node_parent(res);
4303 isl_schedule_node_free(node);
4305 return res;
4308 /* Insert a node "graft" into the schedule tree of "node" such that it
4309 * is executed before (if "before" is set) or after (if "before" is not set)
4310 * the node that "node" points to.
4311 * The root of "graft" may be either a domain or an extension node.
4312 * In the latter case, the domain of the extension needs to correspond
4313 * to the outer band nodes of "node".
4314 * The elements of the domain or the range of the extension may not
4315 * intersect with the domain elements that reach "node".
4316 * The schedule tree of "graft" may not be anchored.
4318 * The schedule tree of "node" is modified to include an extension node
4319 * corresponding to the root node of "graft" as a child of the original
4320 * parent of "node". The original node that "node" points to and the
4321 * child of the root node of "graft" are attached to this extension node
4322 * through a sequence, with appropriate filters and with the child
4323 * of "graft" appearing before or after the original "node".
4325 * If "node" already appears inside a sequence that is the child of
4326 * an extension node and if the spaces of the new domain elements
4327 * do not overlap with those of the original domain elements,
4328 * then that extension node is extended with the new extension
4329 * rather than introducing a new segment of extension and sequence nodes.
4331 * Return a pointer to the same node in the modified tree that
4332 * "node" pointed to in the original tree.
4334 static __isl_give isl_schedule_node *isl_schedule_node_graft_before_or_after(
4335 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft,
4336 int before)
4338 if (!node || !graft)
4339 goto error;
4340 if (check_insert(node) < 0)
4341 goto error;
4343 if (isl_schedule_node_get_type(graft) == isl_schedule_node_domain)
4344 graft = extension_from_domain(graft, node);
4346 if (!graft)
4347 goto error;
4348 if (isl_schedule_node_get_type(graft) != isl_schedule_node_extension)
4349 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4350 "expecting domain or extension as root of graft",
4351 goto error);
4353 return graft_extension(node, graft, before);
4354 error:
4355 isl_schedule_node_free(node);
4356 isl_schedule_node_free(graft);
4357 return NULL;
4360 /* Insert a node "graft" into the schedule tree of "node" such that it
4361 * is executed before the node that "node" points to.
4362 * The root of "graft" may be either a domain or an extension node.
4363 * In the latter case, the domain of the extension needs to correspond
4364 * to the outer band nodes of "node".
4365 * The elements of the domain or the range of the extension may not
4366 * intersect with the domain elements that reach "node".
4367 * The schedule tree of "graft" may not be anchored.
4369 * Return a pointer to the same node in the modified tree that
4370 * "node" pointed to in the original tree.
4372 __isl_give isl_schedule_node *isl_schedule_node_graft_before(
4373 __isl_take isl_schedule_node *node, __isl_take isl_schedule_node *graft)
4375 return isl_schedule_node_graft_before_or_after(node, graft, 1);
4378 /* Insert a node "graft" into the schedule tree of "node" such that it
4379 * is executed after the node that "node" points to.
4380 * The root of "graft" may be either a domain or an extension node.
4381 * In the latter case, the domain of the extension needs to correspond
4382 * to the outer band nodes of "node".
4383 * The elements of the domain or the range of the extension may not
4384 * intersect with the domain elements that reach "node".
4385 * The schedule tree of "graft" may not be anchored.
4387 * Return a pointer to the same node in the modified tree that
4388 * "node" pointed to in the original tree.
4390 __isl_give isl_schedule_node *isl_schedule_node_graft_after(
4391 __isl_take isl_schedule_node *node,
4392 __isl_take isl_schedule_node *graft)
4394 return isl_schedule_node_graft_before_or_after(node, graft, 0);
4397 /* Split the domain elements that reach "node" into those that satisfy
4398 * "filter" and those that do not. Arrange for the first subset to be
4399 * executed before or after the second subset, depending on the value
4400 * of "before".
4401 * Return a pointer to the tree corresponding to the second subset,
4402 * except when this subset is empty in which case the original pointer
4403 * is returned.
4404 * If both subsets are non-empty, then a sequence node is introduced
4405 * to impose the order. If the grandparent of the original node was
4406 * itself a sequence, then the original child is replaced by two children
4407 * in this sequence instead.
4408 * The children in the sequence are copies of the original subtree,
4409 * simplified with respect to their filters.
4411 static __isl_give isl_schedule_node *isl_schedule_node_order_before_or_after(
4412 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter,
4413 int before)
4415 enum isl_schedule_node_type ancestors[] =
4416 { isl_schedule_node_filter, isl_schedule_node_sequence };
4417 isl_union_set *node_domain, *node_filter = NULL, *parent_filter;
4418 isl_schedule_node *node2;
4419 isl_schedule_tree *tree1, *tree2;
4420 isl_bool empty1, empty2;
4421 isl_bool in_seq;
4423 if (!node || !filter)
4424 goto error;
4425 if (check_insert(node) < 0)
4426 goto error;
4428 in_seq = has_ancestors(node, 2, ancestors);
4429 if (in_seq < 0)
4430 goto error;
4431 node_domain = isl_schedule_node_get_domain(node);
4432 filter = isl_union_set_gist(filter, isl_union_set_copy(node_domain));
4433 node_filter = isl_union_set_copy(node_domain);
4434 node_filter = isl_union_set_subtract(node_filter,
4435 isl_union_set_copy(filter));
4436 node_filter = isl_union_set_gist(node_filter, node_domain);
4437 empty1 = isl_union_set_is_empty(filter);
4438 empty2 = isl_union_set_is_empty(node_filter);
4439 if (empty1 < 0 || empty2 < 0)
4440 goto error;
4441 if (empty1 || empty2) {
4442 isl_union_set_free(filter);
4443 isl_union_set_free(node_filter);
4444 return node;
4447 if (in_seq) {
4448 node = isl_schedule_node_parent(node);
4449 parent_filter = isl_schedule_node_filter_get_filter(node);
4450 node_filter = isl_union_set_intersect(node_filter,
4451 isl_union_set_copy(parent_filter));
4452 filter = isl_union_set_intersect(filter, parent_filter);
4455 node2 = isl_schedule_node_copy(node);
4456 node = isl_schedule_node_gist(node, isl_union_set_copy(node_filter));
4457 node2 = isl_schedule_node_gist(node2, isl_union_set_copy(filter));
4458 tree1 = isl_schedule_node_get_tree(node);
4459 tree2 = isl_schedule_node_get_tree(node2);
4460 tree1 = isl_schedule_tree_insert_filter(tree1, node_filter);
4461 tree2 = isl_schedule_tree_insert_filter(tree2, filter);
4462 isl_schedule_node_free(node2);
4464 if (before) {
4465 tree1 = isl_schedule_tree_sequence_pair(tree2, tree1);
4466 node = graft_or_splice(node, tree1, 1);
4467 } else {
4468 tree1 = isl_schedule_tree_sequence_pair(tree1, tree2);
4469 node = graft_or_splice(node, tree1, 0);
4472 return node;
4473 error:
4474 isl_schedule_node_free(node);
4475 isl_union_set_free(filter);
4476 isl_union_set_free(node_filter);
4477 return NULL;
4480 /* Split the domain elements that reach "node" into those that satisfy
4481 * "filter" and those that do not. Arrange for the first subset to be
4482 * executed before the second subset.
4483 * Return a pointer to the tree corresponding to the second subset,
4484 * except when this subset is empty in which case the original pointer
4485 * is returned.
4487 __isl_give isl_schedule_node *isl_schedule_node_order_before(
4488 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
4490 return isl_schedule_node_order_before_or_after(node, filter, 1);
4493 /* Split the domain elements that reach "node" into those that satisfy
4494 * "filter" and those that do not. Arrange for the first subset to be
4495 * executed after the second subset.
4496 * Return a pointer to the tree corresponding to the second subset,
4497 * except when this subset is empty in which case the original pointer
4498 * is returned.
4500 __isl_give isl_schedule_node *isl_schedule_node_order_after(
4501 __isl_take isl_schedule_node *node, __isl_take isl_union_set *filter)
4503 return isl_schedule_node_order_before_or_after(node, filter, 0);
4506 /* Reset the user pointer on all identifiers of parameters and tuples
4507 * in the schedule node "node".
4509 __isl_give isl_schedule_node *isl_schedule_node_reset_user(
4510 __isl_take isl_schedule_node *node)
4512 isl_schedule_tree *tree;
4514 tree = isl_schedule_node_get_tree(node);
4515 tree = isl_schedule_tree_reset_user(tree);
4516 node = isl_schedule_node_graft_tree(node, tree);
4518 return node;
4521 /* Align the parameters of the schedule node "node" to those of "space".
4523 __isl_give isl_schedule_node *isl_schedule_node_align_params(
4524 __isl_take isl_schedule_node *node, __isl_take isl_space *space)
4526 isl_schedule_tree *tree;
4528 tree = isl_schedule_node_get_tree(node);
4529 tree = isl_schedule_tree_align_params(tree, space);
4530 node = isl_schedule_node_graft_tree(node, tree);
4532 return node;
4535 /* Compute the pullback of schedule node "node"
4536 * by the function represented by "upma".
4537 * In other words, plug in "upma" in the iteration domains
4538 * of schedule node "node".
4539 * We currently do not handle expansion nodes.
4541 * Note that this is only a helper function for
4542 * isl_schedule_pullback_union_pw_multi_aff. In order to maintain consistency,
4543 * this function should not be called on a single node without also
4544 * calling it on all the other nodes.
4546 __isl_give isl_schedule_node *isl_schedule_node_pullback_union_pw_multi_aff(
4547 __isl_take isl_schedule_node *node,
4548 __isl_take isl_union_pw_multi_aff *upma)
4550 isl_schedule_tree *tree;
4552 tree = isl_schedule_node_get_tree(node);
4553 tree = isl_schedule_tree_pullback_union_pw_multi_aff(tree, upma);
4554 node = isl_schedule_node_graft_tree(node, tree);
4556 return node;
4559 /* Internal data structure for isl_schedule_node_expand.
4560 * "tree" is the tree that needs to be plugged in in all the leaves.
4561 * "domain" is the set of domain elements in the original leaves
4562 * to which the tree applies.
4564 struct isl_schedule_expand_data {
4565 isl_schedule_tree *tree;
4566 isl_union_set *domain;
4569 /* If "node" is a leaf, then plug in data->tree, simplifying it
4570 * within its new context.
4572 * If there are any domain elements at the leaf where the tree
4573 * should not be plugged in (i.e., there are elements not in data->domain)
4574 * then first extend the tree to only apply to the elements in data->domain
4575 * by constructing a set node that selects data->tree for elements
4576 * in data->domain and a leaf for the other elements.
4578 static __isl_give isl_schedule_node *expand(__isl_take isl_schedule_node *node,
4579 void *user)
4581 struct isl_schedule_expand_data *data = user;
4582 isl_schedule_tree *tree, *leaf;
4583 isl_union_set *domain, *left;
4584 isl_bool empty;
4586 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
4587 return node;
4589 domain = isl_schedule_node_get_domain(node);
4590 tree = isl_schedule_tree_copy(data->tree);
4592 left = isl_union_set_copy(domain);
4593 left = isl_union_set_subtract(left, isl_union_set_copy(data->domain));
4594 empty = isl_union_set_is_empty(left);
4595 if (empty >= 0 && !empty) {
4596 leaf = isl_schedule_node_get_leaf(node);
4597 leaf = isl_schedule_tree_insert_filter(leaf, left);
4598 left = isl_union_set_copy(data->domain);
4599 tree = isl_schedule_tree_insert_filter(tree, left);
4600 tree = isl_schedule_tree_set_pair(tree, leaf);
4601 } else {
4602 if (empty < 0)
4603 node = isl_schedule_node_free(node);
4604 isl_union_set_free(left);
4607 node = isl_schedule_node_graft_tree(node, tree);
4608 node = isl_schedule_node_gist(node, domain);
4610 return node;
4613 /* Expand the tree rooted at "node" by extending all leaves
4614 * with an expansion node with as child "tree".
4615 * The expansion is determined by "contraction" and "domain".
4616 * That is, the elements of "domain" are contracted according
4617 * to "contraction". The expansion relation is then the inverse
4618 * of "contraction" with its range intersected with "domain".
4620 * Insert the appropriate expansion node on top of "tree" and
4621 * then plug in the result in all leaves of "node".
4623 __isl_give isl_schedule_node *isl_schedule_node_expand(
4624 __isl_take isl_schedule_node *node,
4625 __isl_take isl_union_pw_multi_aff *contraction,
4626 __isl_take isl_union_set *domain,
4627 __isl_take isl_schedule_tree *tree)
4629 struct isl_schedule_expand_data data;
4630 isl_union_map *expansion;
4631 isl_union_pw_multi_aff *copy;
4633 if (!node || !contraction || !tree)
4634 node = isl_schedule_node_free(node);
4636 copy = isl_union_pw_multi_aff_copy(contraction);
4637 expansion = isl_union_map_from_union_pw_multi_aff(copy);
4638 expansion = isl_union_map_reverse(expansion);
4639 expansion = isl_union_map_intersect_range(expansion, domain);
4640 data.domain = isl_union_map_domain(isl_union_map_copy(expansion));
4642 tree = isl_schedule_tree_insert_expansion(tree, contraction, expansion);
4643 data.tree = tree;
4645 node = isl_schedule_node_map_descendant_bottom_up(node, &expand, &data);
4646 isl_union_set_free(data.domain);
4647 isl_schedule_tree_free(data.tree);
4648 return node;
4651 /* Return the position of the subtree containing "node" among the children
4652 * of "ancestor". "node" is assumed to be a descendant of "ancestor".
4653 * In particular, both nodes should point to the same schedule tree.
4655 * Return -1 on error.
4657 int isl_schedule_node_get_ancestor_child_position(
4658 __isl_keep isl_schedule_node *node,
4659 __isl_keep isl_schedule_node *ancestor)
4661 int n1, n2;
4662 isl_schedule_tree *tree;
4664 if (!node || !ancestor)
4665 return -1;
4667 if (node->schedule != ancestor->schedule)
4668 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4669 "not a descendant", return -1);
4671 n1 = isl_schedule_node_get_tree_depth(ancestor);
4672 n2 = isl_schedule_node_get_tree_depth(node);
4674 if (n1 >= n2)
4675 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4676 "not a descendant", return -1);
4677 tree = isl_schedule_tree_list_get_schedule_tree(node->ancestors, n1);
4678 isl_schedule_tree_free(tree);
4679 if (tree != ancestor->tree)
4680 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
4681 "not a descendant", return -1);
4683 return node->child_pos[n1];
4686 /* Given two nodes that point to the same schedule tree, return their
4687 * closest shared ancestor.
4689 * Since the two nodes point to the same schedule, they share at least
4690 * one ancestor, the root of the schedule. We move down from the root
4691 * to the first ancestor where the respective children have a different
4692 * child position. This is the requested ancestor.
4693 * If there is no ancestor where the children have a different position,
4694 * then one node is an ancestor of the other and then this node is
4695 * the requested ancestor.
4697 __isl_give isl_schedule_node *isl_schedule_node_get_shared_ancestor(
4698 __isl_keep isl_schedule_node *node1,
4699 __isl_keep isl_schedule_node *node2)
4701 int i, n1, n2;
4703 if (!node1 || !node2)
4704 return NULL;
4705 if (node1->schedule != node2->schedule)
4706 isl_die(isl_schedule_node_get_ctx(node1), isl_error_invalid,
4707 "not part of same schedule", return NULL);
4708 n1 = isl_schedule_node_get_tree_depth(node1);
4709 n2 = isl_schedule_node_get_tree_depth(node2);
4710 if (n2 < n1)
4711 return isl_schedule_node_get_shared_ancestor(node2, node1);
4712 if (n1 == 0)
4713 return isl_schedule_node_copy(node1);
4714 if (isl_schedule_node_is_equal(node1, node2))
4715 return isl_schedule_node_copy(node1);
4717 for (i = 0; i < n1; ++i)
4718 if (node1->child_pos[i] != node2->child_pos[i])
4719 break;
4721 node1 = isl_schedule_node_copy(node1);
4722 return isl_schedule_node_ancestor(node1, n1 - i);
4725 /* Print "node" to "p".
4727 __isl_give isl_printer *isl_printer_print_schedule_node(
4728 __isl_take isl_printer *p, __isl_keep isl_schedule_node *node)
4730 if (!node)
4731 return isl_printer_free(p);
4732 return isl_printer_print_schedule_tree_mark(p, node->schedule->root,
4733 isl_schedule_tree_list_n_schedule_tree(node->ancestors),
4734 node->child_pos);
4737 void isl_schedule_node_dump(__isl_keep isl_schedule_node *node)
4739 isl_ctx *ctx;
4740 isl_printer *printer;
4742 if (!node)
4743 return;
4745 ctx = isl_schedule_node_get_ctx(node);
4746 printer = isl_printer_to_file(ctx, stderr);
4747 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
4748 printer = isl_printer_print_schedule_node(printer, node);
4750 isl_printer_free(printer);
4753 /* Return a string representation of "node".
4754 * Print the schedule node in block format as it would otherwise
4755 * look identical to the entire schedule.
4757 __isl_give char *isl_schedule_node_to_str(__isl_keep isl_schedule_node *node)
4759 isl_printer *printer;
4760 char *s;
4762 if (!node)
4763 return NULL;
4765 printer = isl_printer_to_str(isl_schedule_node_get_ctx(node));
4766 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
4767 printer = isl_printer_print_schedule_node(printer, node);
4768 s = isl_printer_get_str(printer);
4769 isl_printer_free(printer);
4771 return s;