isl_tab_basic_set_non_trivial_lexmin: extract out update_outer_levels
[isl.git] / isl_schedule.c
blobed3ae25788ac53b3b9ade5ab23527ddaba313ffc
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl/ctx.h>
15 #include <isl/val.h>
16 #include <isl_aff_private.h>
17 #include <isl/map.h>
18 #include <isl/set.h>
19 #include <isl/schedule.h>
20 #include <isl/schedule_node.h>
21 #include <isl_sort.h>
22 #include <isl/printer.h>
23 #include <isl_schedule_private.h>
24 #include <isl_schedule_tree.h>
25 #include <isl_schedule_node_private.h>
26 #include <isl_band_private.h>
28 /* Return a schedule encapsulating the given schedule tree.
30 * We currently only allow schedule trees with a domain or extension as root.
32 * The leaf field is initialized as a leaf node so that it can be
33 * used to represent leaves in the constructed schedule.
34 * The reference count is set to -1 since the isl_schedule_tree
35 * should never be freed. It is up to the (internal) users of
36 * these leaves to ensure that they are only used while the schedule
37 * is still alive.
39 __isl_give isl_schedule *isl_schedule_from_schedule_tree(isl_ctx *ctx,
40 __isl_take isl_schedule_tree *tree)
42 enum isl_schedule_node_type type;
43 isl_schedule *schedule;
45 if (!tree)
46 return NULL;
47 type = isl_schedule_tree_get_type(tree);
48 if (type != isl_schedule_node_domain &&
49 type != isl_schedule_node_extension)
50 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
51 "root of schedule tree should be a domain or extension",
52 goto error);
54 schedule = isl_calloc_type(ctx, isl_schedule);
55 if (!schedule)
56 goto error;
58 schedule->ref = 1;
59 schedule->root = tree;
60 schedule->leaf = isl_schedule_tree_leaf(ctx);
62 if (!schedule->leaf)
63 return isl_schedule_free(schedule);
64 return schedule;
65 error:
66 isl_schedule_tree_free(tree);
67 return NULL;
70 /* Return a pointer to a schedule with as single node
71 * a domain node with the given domain.
73 __isl_give isl_schedule *isl_schedule_from_domain(
74 __isl_take isl_union_set *domain)
76 isl_ctx *ctx;
77 isl_schedule_tree *tree;
79 ctx = isl_union_set_get_ctx(domain);
80 tree = isl_schedule_tree_from_domain(domain);
81 return isl_schedule_from_schedule_tree(ctx, tree);
84 /* Return a pointer to a schedule with as single node
85 * a domain node with an empty domain.
87 __isl_give isl_schedule *isl_schedule_empty(__isl_take isl_space *space)
89 return isl_schedule_from_domain(isl_union_set_empty(space));
92 /* Return a new reference to "sched".
94 __isl_give isl_schedule *isl_schedule_copy(__isl_keep isl_schedule *sched)
96 if (!sched)
97 return NULL;
99 sched->ref++;
100 return sched;
103 /* Return an isl_schedule that is equal to "schedule" and that has only
104 * a single reference.
106 * We only need and support this function when the schedule is represented
107 * as a schedule tree.
109 __isl_give isl_schedule *isl_schedule_cow(__isl_take isl_schedule *schedule)
111 isl_ctx *ctx;
112 isl_schedule_tree *tree;
114 if (!schedule)
115 return NULL;
116 if (schedule->ref == 1)
117 return schedule;
119 ctx = isl_schedule_get_ctx(schedule);
120 if (!schedule->root)
121 isl_die(ctx, isl_error_internal,
122 "only for schedule tree based schedules",
123 return isl_schedule_free(schedule));
124 schedule->ref--;
125 tree = isl_schedule_tree_copy(schedule->root);
126 return isl_schedule_from_schedule_tree(ctx, tree);
129 __isl_null isl_schedule *isl_schedule_free(__isl_take isl_schedule *sched)
131 if (!sched)
132 return NULL;
134 if (--sched->ref > 0)
135 return NULL;
137 isl_band_list_free(sched->band_forest);
138 isl_schedule_tree_free(sched->root);
139 isl_schedule_tree_free(sched->leaf);
140 free(sched);
141 return NULL;
144 /* Replace the root of "schedule" by "tree".
146 __isl_give isl_schedule *isl_schedule_set_root(
147 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree)
149 if (!schedule || !tree)
150 goto error;
151 if (schedule->root == tree) {
152 isl_schedule_tree_free(tree);
153 return schedule;
156 schedule = isl_schedule_cow(schedule);
157 if (!schedule)
158 goto error;
159 isl_schedule_tree_free(schedule->root);
160 schedule->root = tree;
162 return schedule;
163 error:
164 isl_schedule_free(schedule);
165 isl_schedule_tree_free(tree);
166 return NULL;
169 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
171 return schedule ? isl_schedule_tree_get_ctx(schedule->leaf) : NULL;
174 /* Return a pointer to the leaf of "schedule".
176 __isl_keep isl_schedule_tree *isl_schedule_peek_leaf(
177 __isl_keep isl_schedule *schedule)
179 return schedule ? schedule->leaf : NULL;
182 /* Are "schedule1" and "schedule2" obviously equal to each other?
184 isl_bool isl_schedule_plain_is_equal(__isl_keep isl_schedule *schedule1,
185 __isl_keep isl_schedule *schedule2)
187 if (!schedule1 || !schedule2)
188 return isl_bool_error;
189 if (schedule1 == schedule2)
190 return isl_bool_true;
191 return isl_schedule_tree_plain_is_equal(schedule1->root,
192 schedule2->root);
195 /* Return the (parameter) space of the schedule, i.e., the space
196 * of the root domain.
198 __isl_give isl_space *isl_schedule_get_space(
199 __isl_keep isl_schedule *schedule)
201 enum isl_schedule_node_type type;
202 isl_space *space;
203 isl_union_set *domain;
205 if (!schedule)
206 return NULL;
207 if (!schedule->root)
208 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
209 "schedule tree representation not available",
210 return NULL);
211 type = isl_schedule_tree_get_type(schedule->root);
212 if (type != isl_schedule_node_domain)
213 isl_die(isl_schedule_get_ctx(schedule), isl_error_internal,
214 "root node not a domain node", return NULL);
216 domain = isl_schedule_tree_domain_get_domain(schedule->root);
217 space = isl_union_set_get_space(domain);
218 isl_union_set_free(domain);
220 return space;
223 /* Return a pointer to the root of "schedule".
225 __isl_give isl_schedule_node *isl_schedule_get_root(
226 __isl_keep isl_schedule *schedule)
228 isl_ctx *ctx;
229 isl_schedule_tree *tree;
230 isl_schedule_tree_list *ancestors;
232 if (!schedule)
233 return NULL;
235 if (!schedule->root)
236 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
237 "schedule tree representation not available",
238 return NULL);
240 ctx = isl_schedule_get_ctx(schedule);
241 tree = isl_schedule_tree_copy(schedule->root);
242 schedule = isl_schedule_copy(schedule);
243 ancestors = isl_schedule_tree_list_alloc(ctx, 0);
244 return isl_schedule_node_alloc(schedule, tree, ancestors, NULL);
247 /* Set max_out to the maximal number of output dimensions over
248 * all maps.
250 static isl_stat update_max_out(__isl_take isl_map *map, void *user)
252 int *max_out = user;
253 int n_out = isl_map_dim(map, isl_dim_out);
255 if (n_out > *max_out)
256 *max_out = n_out;
258 isl_map_free(map);
259 return isl_stat_ok;
262 /* Internal data structure for map_pad_range.
264 * "max_out" is the maximal schedule dimension.
265 * "res" collects the results.
267 struct isl_pad_schedule_map_data {
268 int max_out;
269 isl_union_map *res;
272 /* Pad the range of the given map with zeros to data->max_out and
273 * then add the result to data->res.
275 static isl_stat map_pad_range(__isl_take isl_map *map, void *user)
277 struct isl_pad_schedule_map_data *data = user;
278 int i;
279 int n_out = isl_map_dim(map, isl_dim_out);
281 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
282 for (i = n_out; i < data->max_out; ++i)
283 map = isl_map_fix_si(map, isl_dim_out, i, 0);
285 data->res = isl_union_map_add_map(data->res, map);
286 if (!data->res)
287 return isl_stat_error;
289 return isl_stat_ok;
292 /* Pad the ranges of the maps in the union map with zeros such they all have
293 * the same dimension.
295 static __isl_give isl_union_map *pad_schedule_map(
296 __isl_take isl_union_map *umap)
298 struct isl_pad_schedule_map_data data;
300 if (!umap)
301 return NULL;
302 if (isl_union_map_n_map(umap) <= 1)
303 return umap;
305 data.max_out = 0;
306 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
307 return isl_union_map_free(umap);
309 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
310 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
311 data.res = isl_union_map_free(data.res);
313 isl_union_map_free(umap);
314 return data.res;
317 /* Return the domain of the root domain node of "schedule".
319 __isl_give isl_union_set *isl_schedule_get_domain(
320 __isl_keep isl_schedule *schedule)
322 if (!schedule)
323 return NULL;
324 if (!schedule->root)
325 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
326 "schedule tree representation not available",
327 return NULL);
328 return isl_schedule_tree_domain_get_domain(schedule->root);
331 /* Traverse all nodes of "sched" in depth first preorder.
333 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
334 * If "fn" returns 0 on any of the nodes, then the subtree rooted
335 * at that node is skipped.
337 * Return 0 on success and -1 on failure.
339 isl_stat isl_schedule_foreach_schedule_node_top_down(
340 __isl_keep isl_schedule *sched,
341 isl_bool (*fn)(__isl_keep isl_schedule_node *node, void *user),
342 void *user)
344 isl_schedule_node *node;
345 isl_stat r;
347 if (!sched)
348 return isl_stat_error;
350 node = isl_schedule_get_root(sched);
351 r = isl_schedule_node_foreach_descendant_top_down(node, fn, user);
352 isl_schedule_node_free(node);
354 return r;
357 /* Traverse the node of "sched" in depth first postorder,
358 * allowing the user to modify the visited node.
359 * The traversal continues from the node returned by the callback function.
360 * It is the responsibility of the user to ensure that this does not
361 * lead to an infinite loop. It is safest to always return a pointer
362 * to the same position (same ancestors and child positions) as the input node.
364 __isl_give isl_schedule *isl_schedule_map_schedule_node_bottom_up(
365 __isl_take isl_schedule *schedule,
366 __isl_give isl_schedule_node *(*fn)(
367 __isl_take isl_schedule_node *node, void *user), void *user)
369 isl_schedule_node *node;
371 node = isl_schedule_get_root(schedule);
372 isl_schedule_free(schedule);
374 node = isl_schedule_node_map_descendant_bottom_up(node, fn, user);
375 schedule = isl_schedule_node_get_schedule(node);
376 isl_schedule_node_free(node);
378 return schedule;
381 /* Wrapper around isl_schedule_node_reset_user for use as
382 * an isl_schedule_map_schedule_node_bottom_up callback.
384 static __isl_give isl_schedule_node *reset_user(
385 __isl_take isl_schedule_node *node, void *user)
387 return isl_schedule_node_reset_user(node);
390 /* Reset the user pointer on all identifiers of parameters and tuples
391 * in the schedule "schedule".
393 __isl_give isl_schedule *isl_schedule_reset_user(
394 __isl_take isl_schedule *schedule)
396 return isl_schedule_map_schedule_node_bottom_up(schedule, &reset_user,
397 NULL);
400 /* Wrapper around isl_schedule_node_align_params for use as
401 * an isl_schedule_map_schedule_node_bottom_up callback.
403 static __isl_give isl_schedule_node *align_params(
404 __isl_take isl_schedule_node *node, void *user)
406 isl_space *space = user;
408 return isl_schedule_node_align_params(node, isl_space_copy(space));
411 /* Align the parameters of all nodes in schedule "schedule"
412 * to those of "space".
414 __isl_give isl_schedule *isl_schedule_align_params(
415 __isl_take isl_schedule *schedule, __isl_take isl_space *space)
417 schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
418 &align_params, space);
419 isl_space_free(space);
420 return schedule;
423 /* Wrapper around isl_schedule_node_pullback_union_pw_multi_aff for use as
424 * an isl_schedule_map_schedule_node_bottom_up callback.
426 static __isl_give isl_schedule_node *pullback_upma(
427 __isl_take isl_schedule_node *node, void *user)
429 isl_union_pw_multi_aff *upma = user;
431 return isl_schedule_node_pullback_union_pw_multi_aff(node,
432 isl_union_pw_multi_aff_copy(upma));
435 /* Compute the pullback of "schedule" by the function represented by "upma".
436 * In other words, plug in "upma" in the iteration domains of "schedule".
438 * The schedule tree is not allowed to contain any expansion nodes.
440 __isl_give isl_schedule *isl_schedule_pullback_union_pw_multi_aff(
441 __isl_take isl_schedule *schedule,
442 __isl_take isl_union_pw_multi_aff *upma)
444 schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
445 &pullback_upma, upma);
446 isl_union_pw_multi_aff_free(upma);
447 return schedule;
450 /* Expand the schedule "schedule" by extending all leaves
451 * with an expansion node with as subtree the tree of "expansion".
452 * The expansion of the expansion node is determined by "contraction"
453 * and the domain of "expansion". That is, the domain of "expansion"
454 * is contracted according to "contraction".
456 * Call isl_schedule_node_expand after extracting the required
457 * information from "expansion".
459 __isl_give isl_schedule *isl_schedule_expand(__isl_take isl_schedule *schedule,
460 __isl_take isl_union_pw_multi_aff *contraction,
461 __isl_take isl_schedule *expansion)
463 isl_union_set *domain;
464 isl_schedule_node *node;
465 isl_schedule_tree *tree;
467 domain = isl_schedule_get_domain(expansion);
469 node = isl_schedule_get_root(expansion);
470 node = isl_schedule_node_child(node, 0);
471 tree = isl_schedule_node_get_tree(node);
472 isl_schedule_node_free(node);
473 isl_schedule_free(expansion);
475 node = isl_schedule_get_root(schedule);
476 isl_schedule_free(schedule);
477 node = isl_schedule_node_expand(node, contraction, domain, tree);
478 schedule = isl_schedule_node_get_schedule(node);
479 isl_schedule_node_free(node);
481 return schedule;
484 /* Intersect the domain of the schedule "schedule" with "domain".
485 * The root of "schedule" is required to be a domain node.
487 __isl_give isl_schedule *isl_schedule_intersect_domain(
488 __isl_take isl_schedule *schedule, __isl_take isl_union_set *domain)
490 enum isl_schedule_node_type root_type;
491 isl_schedule_node *node;
493 if (!schedule || !domain)
494 goto error;
496 root_type = isl_schedule_tree_get_type(schedule->root);
497 if (root_type != isl_schedule_node_domain)
498 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
499 "root node must be a domain node", goto error);
501 node = isl_schedule_get_root(schedule);
502 isl_schedule_free(schedule);
503 node = isl_schedule_node_domain_intersect_domain(node, domain);
504 schedule = isl_schedule_node_get_schedule(node);
505 isl_schedule_node_free(node);
507 return schedule;
508 error:
509 isl_schedule_free(schedule);
510 isl_union_set_free(domain);
511 return NULL;
514 /* Replace the domain of the schedule "schedule" with the gist
515 * of the original domain with respect to the parameter domain "context".
517 __isl_give isl_schedule *isl_schedule_gist_domain_params(
518 __isl_take isl_schedule *schedule, __isl_take isl_set *context)
520 enum isl_schedule_node_type root_type;
521 isl_schedule_node *node;
523 if (!schedule || !context)
524 goto error;
526 root_type = isl_schedule_tree_get_type(schedule->root);
527 if (root_type != isl_schedule_node_domain)
528 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
529 "root node must be a domain node", goto error);
531 node = isl_schedule_get_root(schedule);
532 isl_schedule_free(schedule);
533 node = isl_schedule_node_domain_gist_params(node, context);
534 schedule = isl_schedule_node_get_schedule(node);
535 isl_schedule_node_free(node);
537 return schedule;
538 error:
539 isl_schedule_free(schedule);
540 isl_set_free(context);
541 return NULL;
544 /* Return an isl_union_map representation of the schedule.
545 * If we still have access to the schedule tree, then we return
546 * an isl_union_map corresponding to the subtree schedule of the child
547 * of the root domain node. That is, we do not intersect the domain
548 * of the returned isl_union_map with the domain constraints.
549 * Otherwise, we must have removed it because we created a band forest.
550 * If so, we extract the isl_union_map from the forest.
551 * This reconstructed schedule map
552 * then needs to be padded with zeros to unify the schedule space
553 * since the result of isl_band_list_get_suffix_schedule may not have
554 * a unified schedule space.
556 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
558 enum isl_schedule_node_type type;
559 isl_schedule_node *node;
560 isl_union_map *umap;
562 if (!sched)
563 return NULL;
565 if (sched->root) {
566 type = isl_schedule_tree_get_type(sched->root);
567 if (type != isl_schedule_node_domain)
568 isl_die(isl_schedule_get_ctx(sched), isl_error_internal,
569 "root node not a domain node", return NULL);
571 node = isl_schedule_get_root(sched);
572 node = isl_schedule_node_child(node, 0);
573 umap = isl_schedule_node_get_subtree_schedule_union_map(node);
574 isl_schedule_node_free(node);
576 return umap;
579 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
580 return pad_schedule_map(umap);
583 static __isl_give isl_band_list *construct_band_list(
584 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
585 __isl_keep isl_band *parent);
587 /* Construct an isl_band structure from the given schedule tree node,
588 * which may be either a band node or a leaf node.
589 * In the latter case, construct a zero-dimensional band.
590 * "domain" is the universe set of the domain elements that reach "node".
591 * "parent" is the parent isl_band of the isl_band constructed
592 * by this function.
594 * In case of a band node, we copy the properties (except tilability,
595 * which is implicit in an isl_band) to the isl_band.
596 * We assume that the band node is not zero-dimensional.
597 * If the child of the band node is not a leaf node,
598 * then we extract the children of the isl_band from this child.
600 static __isl_give isl_band *construct_band(__isl_take isl_schedule_node *node,
601 __isl_take isl_union_set *domain, __isl_keep isl_band *parent)
603 int i;
604 isl_ctx *ctx;
605 isl_band *band = NULL;
606 isl_multi_union_pw_aff *mupa;
608 if (!node || !domain)
609 goto error;
611 ctx = isl_schedule_node_get_ctx(node);
612 band = isl_band_alloc(ctx);
613 if (!band)
614 goto error;
616 band->schedule = node->schedule;
617 band->parent = parent;
619 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
620 band->n = 0;
621 band->pma = isl_union_pw_multi_aff_from_domain(domain);
622 isl_schedule_node_free(node);
623 return band;
626 band->n = isl_schedule_node_band_n_member(node);
627 if (band->n == 0)
628 isl_die(ctx, isl_error_unsupported,
629 "zero-dimensional band nodes not supported",
630 goto error);
631 band->coincident = isl_alloc_array(ctx, int, band->n);
632 if (band->n && !band->coincident)
633 goto error;
634 for (i = 0; i < band->n; ++i)
635 band->coincident[i] =
636 isl_schedule_node_band_member_get_coincident(node, i);
637 mupa = isl_schedule_node_band_get_partial_schedule(node);
638 band->pma = isl_union_pw_multi_aff_from_multi_union_pw_aff(mupa);
639 if (!band->pma)
640 goto error;
642 node = isl_schedule_node_child(node, 0);
643 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
644 isl_schedule_node_free(node);
645 isl_union_set_free(domain);
646 return band;
649 band->children = construct_band_list(node, domain, band);
650 if (!band->children)
651 return isl_band_free(band);
653 return band;
654 error:
655 isl_union_set_free(domain);
656 isl_schedule_node_free(node);
657 isl_band_free(band);
658 return NULL;
661 /* Construct a list of isl_band structures from the children of "node".
662 * "node" itself is a sequence or set node, so that each of the child nodes
663 * is a filter node and the list returned by node_construct_band_list
664 * consists of a single element.
665 * "domain" is the universe set of the domain elements that reach "node".
666 * "parent" is the parent isl_band of the isl_band structures constructed
667 * by this function.
669 static __isl_give isl_band_list *construct_band_list_from_children(
670 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
671 __isl_keep isl_band *parent)
673 int i, n;
674 isl_ctx *ctx;
675 isl_band_list *list;
677 n = isl_schedule_node_n_children(node);
679 ctx = isl_schedule_node_get_ctx(node);
680 list = isl_band_list_alloc(ctx, 0);
681 for (i = 0; i < n; ++i) {
682 isl_schedule_node *child;
683 isl_band_list *list_i;
685 child = isl_schedule_node_get_child(node, i);
686 list_i = construct_band_list(child, isl_union_set_copy(domain),
687 parent);
688 list = isl_band_list_concat(list, list_i);
691 isl_union_set_free(domain);
692 isl_schedule_node_free(node);
694 return list;
697 /* Construct an isl_band structure from the given sequence node
698 * (or set node that is treated as a sequence node).
699 * A single-dimensional band is created with as schedule for each of
700 * filters of the children, the corresponding child position.
701 * "domain" is the universe set of the domain elements that reach "node".
702 * "parent" is the parent isl_band of the isl_band constructed
703 * by this function.
705 static __isl_give isl_band_list *construct_band_list_sequence(
706 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
707 __isl_keep isl_band *parent)
709 int i, n;
710 isl_ctx *ctx;
711 isl_band *band = NULL;
712 isl_space *space;
713 isl_union_pw_multi_aff *upma;
715 if (!node || !domain)
716 goto error;
718 ctx = isl_schedule_node_get_ctx(node);
719 band = isl_band_alloc(ctx);
720 if (!band)
721 goto error;
723 band->schedule = node->schedule;
724 band->parent = parent;
725 band->n = 1;
726 band->coincident = isl_calloc_array(ctx, int, band->n);
727 if (!band->coincident)
728 goto error;
730 n = isl_schedule_node_n_children(node);
731 space = isl_union_set_get_space(domain);
732 upma = isl_union_pw_multi_aff_empty(isl_space_copy(space));
734 space = isl_space_set_from_params(space);
735 space = isl_space_add_dims(space, isl_dim_set, 1);
737 for (i = 0; i < n; ++i) {
738 isl_schedule_node *child;
739 isl_union_set *filter;
740 isl_val *v;
741 isl_val_list *vl;
742 isl_multi_val *mv;
743 isl_union_pw_multi_aff *upma_i;
745 child = isl_schedule_node_get_child(node, i);
746 filter = isl_schedule_node_filter_get_filter(child);
747 isl_schedule_node_free(child);
748 filter = isl_union_set_intersect(filter,
749 isl_union_set_copy(domain));
750 v = isl_val_int_from_si(ctx, i);
751 vl = isl_val_list_from_val(v);
752 mv = isl_multi_val_from_val_list(isl_space_copy(space), vl);
753 upma_i = isl_union_pw_multi_aff_multi_val_on_domain(filter, mv);
754 upma = isl_union_pw_multi_aff_union_add(upma, upma_i);
757 isl_space_free(space);
759 band->pma = upma;
760 if (!band->pma)
761 goto error;
763 band->children = construct_band_list_from_children(node, domain, band);
764 if (!band->children)
765 band = isl_band_free(band);
766 return isl_band_list_from_band(band);
767 error:
768 isl_union_set_free(domain);
769 isl_schedule_node_free(node);
770 isl_band_free(band);
771 return NULL;
774 /* Construct a list of isl_band structures from "node" depending
775 * on the type of "node".
776 * "domain" is the universe set of the domain elements that reach "node".
777 * "parent" is the parent isl_band of the isl_band structures constructed
778 * by this function.
780 * If schedule_separate_components is set then set nodes are treated
781 * as sequence nodes. Otherwise, we directly extract an (implicitly
782 * parallel) list of isl_band structures.
784 * If "node" is a filter, then "domain" is updated by the filter.
786 static __isl_give isl_band_list *construct_band_list(
787 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
788 __isl_keep isl_band *parent)
790 enum isl_schedule_node_type type;
791 isl_ctx *ctx;
792 isl_band *band;
793 isl_band_list *list;
794 isl_union_set *filter;
796 if (!node || !domain)
797 goto error;
799 type = isl_schedule_node_get_type(node);
800 switch (type) {
801 case isl_schedule_node_error:
802 goto error;
803 case isl_schedule_node_context:
804 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
805 "context nodes not supported", goto error);
806 case isl_schedule_node_domain:
807 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
808 "internal domain nodes not allowed", goto error);
809 case isl_schedule_node_expansion:
810 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
811 "expansion nodes not supported", goto error);
812 case isl_schedule_node_extension:
813 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
814 "extension nodes not supported", goto error);
815 case isl_schedule_node_filter:
816 filter = isl_schedule_node_filter_get_filter(node);
817 domain = isl_union_set_intersect(domain, filter);
818 node = isl_schedule_node_child(node, 0);
819 return construct_band_list(node, domain, parent);
820 case isl_schedule_node_guard:
821 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
822 "guard nodes not supported", goto error);
823 case isl_schedule_node_mark:
824 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
825 "mark nodes not supported", goto error);
826 case isl_schedule_node_set:
827 ctx = isl_schedule_node_get_ctx(node);
828 if (isl_options_get_schedule_separate_components(ctx))
829 return construct_band_list_sequence(node, domain,
830 parent);
831 else
832 return construct_band_list_from_children(node, domain,
833 parent);
834 case isl_schedule_node_sequence:
835 return construct_band_list_sequence(node, domain, parent);
836 case isl_schedule_node_leaf:
837 case isl_schedule_node_band:
838 band = construct_band(node, domain, parent);
839 list = isl_band_list_from_band(band);
840 break;
843 return list;
844 error:
845 isl_union_set_free(domain);
846 isl_schedule_node_free(node);
847 return NULL;
850 /* Return the roots of a band forest representation of the schedule.
851 * The band forest is constructed from the schedule tree,
852 * but once such a band forest is
853 * constructed, we forget about the original schedule tree since
854 * the user may modify the schedule through the band forest.
856 __isl_give isl_band_list *isl_schedule_get_band_forest(
857 __isl_keep isl_schedule *schedule)
859 isl_schedule_node *node;
860 isl_union_set *domain;
862 if (!schedule)
863 return NULL;
864 if (schedule->root) {
865 node = isl_schedule_get_root(schedule);
866 domain = isl_schedule_node_domain_get_domain(node);
867 domain = isl_union_set_universe(domain);
868 node = isl_schedule_node_child(node, 0);
870 schedule->band_forest = construct_band_list(node, domain, NULL);
871 schedule->root = isl_schedule_tree_free(schedule->root);
873 return isl_band_list_dup(schedule->band_forest);
876 /* Call "fn" on each band in the schedule in depth-first post-order.
878 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
879 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
881 int r;
882 isl_band_list *forest;
884 if (!sched)
885 return -1;
887 forest = isl_schedule_get_band_forest(sched);
888 r = isl_band_list_foreach_band(forest, fn, user);
889 isl_band_list_free(forest);
891 return r;
894 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
895 __isl_keep isl_band_list *list);
897 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
898 __isl_keep isl_band *band)
900 isl_band_list *children;
902 p = isl_printer_start_line(p);
903 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
904 p = isl_printer_end_line(p);
906 if (!isl_band_has_children(band))
907 return p;
909 children = isl_band_get_children(band);
911 p = isl_printer_indent(p, 4);
912 p = print_band_list(p, children);
913 p = isl_printer_indent(p, -4);
915 isl_band_list_free(children);
917 return p;
920 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
921 __isl_keep isl_band_list *list)
923 int i, n;
925 n = isl_band_list_n_band(list);
926 for (i = 0; i < n; ++i) {
927 isl_band *band;
928 band = isl_band_list_get_band(list, i);
929 p = print_band(p, band);
930 isl_band_free(band);
933 return p;
936 /* Insert a band node with partial schedule "partial" between the domain
937 * root node of "schedule" and its single child.
938 * Return a pointer to the updated schedule.
940 * If any of the nodes in the tree depend on the set of outer band nodes
941 * then we refuse to insert the band node.
943 __isl_give isl_schedule *isl_schedule_insert_partial_schedule(
944 __isl_take isl_schedule *schedule,
945 __isl_take isl_multi_union_pw_aff *partial)
947 isl_schedule_node *node;
948 int anchored;
950 node = isl_schedule_get_root(schedule);
951 isl_schedule_free(schedule);
952 if (!node)
953 goto error;
954 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
955 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
956 "root node not a domain node", goto error);
958 node = isl_schedule_node_child(node, 0);
959 anchored = isl_schedule_node_is_subtree_anchored(node);
960 if (anchored < 0)
961 goto error;
962 if (anchored)
963 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
964 "cannot insert band node in anchored subtree",
965 goto error);
966 node = isl_schedule_node_insert_partial_schedule(node, partial);
968 schedule = isl_schedule_node_get_schedule(node);
969 isl_schedule_node_free(node);
971 return schedule;
972 error:
973 isl_schedule_node_free(node);
974 isl_multi_union_pw_aff_free(partial);
975 return NULL;
978 /* Insert a context node with constraints "context" between the domain
979 * root node of "schedule" and its single child.
980 * Return a pointer to the updated schedule.
982 __isl_give isl_schedule *isl_schedule_insert_context(
983 __isl_take isl_schedule *schedule, __isl_take isl_set *context)
985 isl_schedule_node *node;
987 node = isl_schedule_get_root(schedule);
988 isl_schedule_free(schedule);
989 node = isl_schedule_node_child(node, 0);
990 node = isl_schedule_node_insert_context(node, context);
991 schedule = isl_schedule_node_get_schedule(node);
992 isl_schedule_node_free(node);
994 return schedule;
997 /* Insert a guard node with constraints "guard" between the domain
998 * root node of "schedule" and its single child.
999 * Return a pointer to the updated schedule.
1001 __isl_give isl_schedule *isl_schedule_insert_guard(
1002 __isl_take isl_schedule *schedule, __isl_take isl_set *guard)
1004 isl_schedule_node *node;
1006 node = isl_schedule_get_root(schedule);
1007 isl_schedule_free(schedule);
1008 node = isl_schedule_node_child(node, 0);
1009 node = isl_schedule_node_insert_guard(node, guard);
1010 schedule = isl_schedule_node_get_schedule(node);
1011 isl_schedule_node_free(node);
1013 return schedule;
1016 /* Return a tree with as top-level node a filter corresponding to "filter" and
1017 * as child, the (single) child of "tree".
1018 * However, if this single child is of type "type", then the filter is inserted
1019 * in the children of this single child instead.
1021 static __isl_give isl_schedule_tree *insert_filter_in_child_of_type(
1022 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter,
1023 enum isl_schedule_node_type type)
1025 if (!isl_schedule_tree_has_children(tree)) {
1026 isl_schedule_tree_free(tree);
1027 return isl_schedule_tree_from_filter(filter);
1028 } else {
1029 tree = isl_schedule_tree_child(tree, 0);
1032 if (isl_schedule_tree_get_type(tree) == type)
1033 tree = isl_schedule_tree_children_insert_filter(tree, filter);
1034 else
1035 tree = isl_schedule_tree_insert_filter(tree, filter);
1037 return tree;
1040 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1041 * with a top-level node (underneath the domain node) of type "type",
1042 * either isl_schedule_node_sequence or isl_schedule_node_set.
1043 * The domains of the two schedules are assumed to be disjoint.
1045 * The new schedule has as domain the union of the domains of the two
1046 * schedules. The child of the domain node is a node of type "type"
1047 * with two filters corresponding to the domains of the input schedules.
1048 * If one (or both) of the top-level nodes of the two schedules is itself
1049 * of type "type", then the filter is pushed into the children of that
1050 * node and the sequence of set is flattened.
1052 __isl_give isl_schedule *isl_schedule_pair(enum isl_schedule_node_type type,
1053 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
1055 int disjoint;
1056 isl_ctx *ctx;
1057 enum isl_schedule_node_type root_type;
1058 isl_schedule_tree *tree1, *tree2;
1059 isl_union_set *filter1, *filter2, *domain;
1061 if (!schedule1 || !schedule2)
1062 goto error;
1064 root_type = isl_schedule_tree_get_type(schedule1->root);
1065 if (root_type != isl_schedule_node_domain)
1066 isl_die(isl_schedule_get_ctx(schedule1), isl_error_internal,
1067 "root node not a domain node", goto error);
1068 root_type = isl_schedule_tree_get_type(schedule2->root);
1069 if (root_type != isl_schedule_node_domain)
1070 isl_die(isl_schedule_get_ctx(schedule1), isl_error_internal,
1071 "root node not a domain node", goto error);
1073 ctx = isl_schedule_get_ctx(schedule1);
1074 tree1 = isl_schedule_tree_copy(schedule1->root);
1075 filter1 = isl_schedule_tree_domain_get_domain(tree1);
1076 tree2 = isl_schedule_tree_copy(schedule2->root);
1077 filter2 = isl_schedule_tree_domain_get_domain(tree2);
1079 isl_schedule_free(schedule1);
1080 isl_schedule_free(schedule2);
1082 disjoint = isl_union_set_is_disjoint(filter1, filter2);
1083 if (disjoint < 0)
1084 filter1 = isl_union_set_free(filter1);
1085 if (!disjoint)
1086 isl_die(ctx, isl_error_invalid,
1087 "schedule domains not disjoint",
1088 filter1 = isl_union_set_free(filter1));
1090 domain = isl_union_set_union(isl_union_set_copy(filter1),
1091 isl_union_set_copy(filter2));
1092 filter1 = isl_union_set_gist(filter1, isl_union_set_copy(domain));
1093 filter2 = isl_union_set_gist(filter2, isl_union_set_copy(domain));
1095 tree1 = insert_filter_in_child_of_type(tree1, filter1, type);
1096 tree2 = insert_filter_in_child_of_type(tree2, filter2, type);
1098 tree1 = isl_schedule_tree_from_pair(type, tree1, tree2);
1099 tree1 = isl_schedule_tree_insert_domain(tree1, domain);
1101 return isl_schedule_from_schedule_tree(ctx, tree1);
1102 error:
1103 isl_schedule_free(schedule1);
1104 isl_schedule_free(schedule2);
1105 return NULL;
1108 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1109 * through a sequence node.
1110 * The domains of the input schedules are assumed to be disjoint.
1112 __isl_give isl_schedule *isl_schedule_sequence(
1113 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
1115 return isl_schedule_pair(isl_schedule_node_sequence,
1116 schedule1, schedule2);
1119 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1120 * through a set node.
1121 * The domains of the input schedules are assumed to be disjoint.
1123 __isl_give isl_schedule *isl_schedule_set(
1124 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
1126 return isl_schedule_pair(isl_schedule_node_set, schedule1, schedule2);
1129 /* Print "schedule" to "p".
1131 * If "schedule" was created from a schedule tree, then we print
1132 * the schedule tree representation. Otherwise, we print
1133 * the band forest representation.
1135 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
1136 __isl_keep isl_schedule *schedule)
1138 isl_band_list *forest;
1140 if (!schedule)
1141 return isl_printer_free(p);
1143 if (schedule->root)
1144 return isl_printer_print_schedule_tree(p, schedule->root);
1146 forest = isl_schedule_get_band_forest(schedule);
1148 p = print_band_list(p, forest);
1150 isl_band_list_free(forest);
1152 return p;
1155 #undef BASE
1156 #define BASE schedule
1157 #include <print_templ_yaml.c>