isl_map_simplify.c: eliminate_div: improve error handling
[isl.git] / isl_schedule.c
blob06145f3a0f8809c81936b81b27196e1398e14227
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl/ctx.h>
14 #include <isl_aff_private.h>
15 #include <isl/map.h>
16 #include <isl/set.h>
17 #include <isl/schedule.h>
18 #include <isl/schedule_node.h>
19 #include <isl_sort.h>
20 #include <isl_schedule_private.h>
21 #include <isl_schedule_tree.h>
22 #include <isl_schedule_node_private.h>
23 #include <isl_band_private.h>
25 /* Return a schedule encapsulating the given schedule tree.
27 * We currently only allow schedule trees with a domain or extension as root.
29 * The leaf field is initialized as a leaf node so that it can be
30 * used to represent leaves in the constructed schedule.
31 * The reference count is set to -1 since the isl_schedule_tree
32 * should never be freed. It is up to the (internal) users of
33 * these leaves to ensure that they are only used while the schedule
34 * is still alive.
36 __isl_give isl_schedule *isl_schedule_from_schedule_tree(isl_ctx *ctx,
37 __isl_take isl_schedule_tree *tree)
39 enum isl_schedule_node_type type;
40 isl_schedule *schedule;
42 if (!tree)
43 return NULL;
44 type = isl_schedule_tree_get_type(tree);
45 if (type != isl_schedule_node_domain &&
46 type != isl_schedule_node_extension)
47 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
48 "root of schedule tree should be a domain or extension",
49 goto error);
51 schedule = isl_calloc_type(ctx, isl_schedule);
52 if (!schedule)
53 goto error;
55 schedule->leaf.ctx = ctx;
56 isl_ctx_ref(ctx);
57 schedule->ref = 1;
58 schedule->root = tree;
59 schedule->leaf.ref = -1;
60 schedule->leaf.type = isl_schedule_node_leaf;
62 return schedule;
63 error:
64 isl_schedule_tree_free(tree);
65 return NULL;
68 /* Return a pointer to a schedule with as single node
69 * a domain node with the given domain.
71 __isl_give isl_schedule *isl_schedule_from_domain(
72 __isl_take isl_union_set *domain)
74 isl_ctx *ctx;
75 isl_schedule_tree *tree;
77 ctx = isl_union_set_get_ctx(domain);
78 tree = isl_schedule_tree_from_domain(domain);
79 return isl_schedule_from_schedule_tree(ctx, tree);
82 /* Return a pointer to a schedule with as single node
83 * a domain node with an empty domain.
85 __isl_give isl_schedule *isl_schedule_empty(__isl_take isl_space *space)
87 return isl_schedule_from_domain(isl_union_set_empty(space));
90 /* Return a new reference to "sched".
92 __isl_give isl_schedule *isl_schedule_copy(__isl_keep isl_schedule *sched)
94 if (!sched)
95 return NULL;
97 sched->ref++;
98 return sched;
101 /* Return an isl_schedule that is equal to "schedule" and that has only
102 * a single reference.
104 * We only need and support this function when the schedule is represented
105 * as a schedule tree.
107 __isl_give isl_schedule *isl_schedule_cow(__isl_take isl_schedule *schedule)
109 isl_ctx *ctx;
110 isl_schedule_tree *tree;
112 if (!schedule)
113 return NULL;
114 if (schedule->ref == 1)
115 return schedule;
117 ctx = isl_schedule_get_ctx(schedule);
118 if (!schedule->root)
119 isl_die(ctx, isl_error_internal,
120 "only for schedule tree based schedules",
121 return isl_schedule_free(schedule));
122 schedule->ref--;
123 tree = isl_schedule_tree_copy(schedule->root);
124 return isl_schedule_from_schedule_tree(ctx, tree);
127 __isl_null isl_schedule *isl_schedule_free(__isl_take isl_schedule *sched)
129 if (!sched)
130 return NULL;
132 if (--sched->ref > 0)
133 return NULL;
135 isl_band_list_free(sched->band_forest);
136 isl_schedule_tree_free(sched->root);
137 isl_ctx_deref(sched->leaf.ctx);
138 free(sched);
139 return NULL;
142 /* Replace the root of "schedule" by "tree".
144 __isl_give isl_schedule *isl_schedule_set_root(
145 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree)
147 if (!schedule || !tree)
148 goto error;
149 if (schedule->root == tree) {
150 isl_schedule_tree_free(tree);
151 return schedule;
154 schedule = isl_schedule_cow(schedule);
155 if (!schedule)
156 goto error;
157 isl_schedule_tree_free(schedule->root);
158 schedule->root = tree;
160 return schedule;
161 error:
162 isl_schedule_free(schedule);
163 isl_schedule_tree_free(tree);
164 return NULL;
167 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
169 return schedule ? schedule->leaf.ctx : NULL;
172 /* Return a pointer to the leaf of "schedule".
174 * Even though these leaves are not reference counted, we still
175 * indicate that this function does not return a copy.
177 __isl_keep isl_schedule_tree *isl_schedule_peek_leaf(
178 __isl_keep isl_schedule *schedule)
180 return schedule ? &schedule->leaf : NULL;
183 /* Are "schedule1" and "schedule2" obviously equal to each other?
185 isl_bool isl_schedule_plain_is_equal(__isl_keep isl_schedule *schedule1,
186 __isl_keep isl_schedule *schedule2)
188 if (!schedule1 || !schedule2)
189 return isl_bool_error;
190 if (schedule1 == schedule2)
191 return isl_bool_true;
192 return isl_schedule_tree_plain_is_equal(schedule1->root,
193 schedule2->root);
196 /* Return the (parameter) space of the schedule, i.e., the space
197 * of the root domain.
199 __isl_give isl_space *isl_schedule_get_space(
200 __isl_keep isl_schedule *schedule)
202 enum isl_schedule_node_type type;
203 isl_space *space;
204 isl_union_set *domain;
206 if (!schedule)
207 return NULL;
208 if (!schedule->root)
209 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
210 "schedule tree representation not available",
211 return NULL);
212 type = isl_schedule_tree_get_type(schedule->root);
213 if (type != isl_schedule_node_domain)
214 isl_die(isl_schedule_get_ctx(schedule), isl_error_internal,
215 "root node not a domain node", return NULL);
217 domain = isl_schedule_tree_domain_get_domain(schedule->root);
218 space = isl_union_set_get_space(domain);
219 isl_union_set_free(domain);
221 return space;
224 /* Return a pointer to the root of "schedule".
226 __isl_give isl_schedule_node *isl_schedule_get_root(
227 __isl_keep isl_schedule *schedule)
229 isl_ctx *ctx;
230 isl_schedule_tree *tree;
231 isl_schedule_tree_list *ancestors;
233 if (!schedule)
234 return NULL;
236 if (!schedule->root)
237 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
238 "schedule tree representation not available",
239 return NULL);
241 ctx = isl_schedule_get_ctx(schedule);
242 tree = isl_schedule_tree_copy(schedule->root);
243 schedule = isl_schedule_copy(schedule);
244 ancestors = isl_schedule_tree_list_alloc(ctx, 0);
245 return isl_schedule_node_alloc(schedule, tree, ancestors, NULL);
248 /* Set max_out to the maximal number of output dimensions over
249 * all maps.
251 static isl_stat update_max_out(__isl_take isl_map *map, void *user)
253 int *max_out = user;
254 int n_out = isl_map_dim(map, isl_dim_out);
256 if (n_out > *max_out)
257 *max_out = n_out;
259 isl_map_free(map);
260 return isl_stat_ok;
263 /* Internal data structure for map_pad_range.
265 * "max_out" is the maximal schedule dimension.
266 * "res" collects the results.
268 struct isl_pad_schedule_map_data {
269 int max_out;
270 isl_union_map *res;
273 /* Pad the range of the given map with zeros to data->max_out and
274 * then add the result to data->res.
276 static isl_stat map_pad_range(__isl_take isl_map *map, void *user)
278 struct isl_pad_schedule_map_data *data = user;
279 int i;
280 int n_out = isl_map_dim(map, isl_dim_out);
282 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
283 for (i = n_out; i < data->max_out; ++i)
284 map = isl_map_fix_si(map, isl_dim_out, i, 0);
286 data->res = isl_union_map_add_map(data->res, map);
287 if (!data->res)
288 return isl_stat_error;
290 return isl_stat_ok;
293 /* Pad the ranges of the maps in the union map with zeros such they all have
294 * the same dimension.
296 static __isl_give isl_union_map *pad_schedule_map(
297 __isl_take isl_union_map *umap)
299 struct isl_pad_schedule_map_data data;
301 if (!umap)
302 return NULL;
303 if (isl_union_map_n_map(umap) <= 1)
304 return umap;
306 data.max_out = 0;
307 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
308 return isl_union_map_free(umap);
310 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
311 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
312 data.res = isl_union_map_free(data.res);
314 isl_union_map_free(umap);
315 return data.res;
318 /* Return the domain of the root domain node of "schedule".
320 __isl_give isl_union_set *isl_schedule_get_domain(
321 __isl_keep isl_schedule *schedule)
323 if (!schedule)
324 return NULL;
325 if (!schedule->root)
326 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
327 "schedule tree representation not available",
328 return NULL);
329 return isl_schedule_tree_domain_get_domain(schedule->root);
332 /* Traverse all nodes of "sched" in depth first preorder.
334 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
335 * If "fn" returns 0 on any of the nodes, then the subtree rooted
336 * at that node is skipped.
338 * Return 0 on success and -1 on failure.
340 isl_stat isl_schedule_foreach_schedule_node(__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(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(
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(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 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(schedule, &reset_user, NULL);
399 /* Wrapper around isl_schedule_node_align_params for use as
400 * an isl_schedule_map_schedule_node callback.
402 static __isl_give isl_schedule_node *align_params(
403 __isl_take isl_schedule_node *node, void *user)
405 isl_space *space = user;
407 return isl_schedule_node_align_params(node, isl_space_copy(space));
410 /* Align the parameters of all nodes in schedule "schedule"
411 * to those of "space".
413 __isl_give isl_schedule *isl_schedule_align_params(
414 __isl_take isl_schedule *schedule, __isl_take isl_space *space)
416 schedule = isl_schedule_map_schedule_node(schedule,
417 &align_params, space);
418 isl_space_free(space);
419 return schedule;
422 /* Wrapper around isl_schedule_node_pullback_union_pw_multi_aff for use as
423 * an isl_schedule_map_schedule_node callback.
425 static __isl_give isl_schedule_node *pullback_upma(
426 __isl_take isl_schedule_node *node, void *user)
428 isl_union_pw_multi_aff *upma = user;
430 return isl_schedule_node_pullback_union_pw_multi_aff(node,
431 isl_union_pw_multi_aff_copy(upma));
434 /* Compute the pullback of "schedule" by the function represented by "upma".
435 * In other words, plug in "upma" in the iteration domains of "schedule".
437 * The schedule tree is not allowed to contain any expansion nodes.
439 __isl_give isl_schedule *isl_schedule_pullback_union_pw_multi_aff(
440 __isl_take isl_schedule *schedule,
441 __isl_take isl_union_pw_multi_aff *upma)
443 schedule = isl_schedule_map_schedule_node(schedule,
444 &pullback_upma, upma);
445 isl_union_pw_multi_aff_free(upma);
446 return schedule;
449 /* Intersect the domain of the schedule "schedule" with "domain".
450 * The root of "schedule" is required to be a domain node.
452 __isl_give isl_schedule *isl_schedule_intersect_domain(
453 __isl_take isl_schedule *schedule, __isl_take isl_union_set *domain)
455 enum isl_schedule_node_type root_type;
456 isl_schedule_node *node;
458 if (!schedule || !domain)
459 goto error;
461 root_type = isl_schedule_tree_get_type(schedule->root);
462 if (root_type != isl_schedule_node_domain)
463 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
464 "root node must be a domain node", goto error);
466 node = isl_schedule_get_root(schedule);
467 isl_schedule_free(schedule);
468 node = isl_schedule_node_domain_intersect_domain(node, domain);
469 schedule = isl_schedule_node_get_schedule(node);
470 isl_schedule_node_free(node);
472 return schedule;
473 error:
474 isl_schedule_free(schedule);
475 isl_union_set_free(domain);
476 return NULL;
479 /* Return an isl_union_map representation of the schedule.
480 * If we still have access to the schedule tree, then we return
481 * an isl_union_map corresponding to the subtree schedule of the child
482 * of the root domain node. That is, we do not intersect the domain
483 * of the returned isl_union_map with the domain constraints.
484 * Otherwise, we must have removed it because we created a band forest.
485 * If so, we extract the isl_union_map from the forest.
486 * This reconstructed schedule map
487 * then needs to be padded with zeros to unify the schedule space
488 * since the result of isl_band_list_get_suffix_schedule may not have
489 * a unified schedule space.
491 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
493 enum isl_schedule_node_type type;
494 isl_schedule_node *node;
495 isl_union_map *umap;
497 if (!sched)
498 return NULL;
500 if (sched->root) {
501 type = isl_schedule_tree_get_type(sched->root);
502 if (type != isl_schedule_node_domain)
503 isl_die(isl_schedule_get_ctx(sched), isl_error_internal,
504 "root node not a domain node", return NULL);
506 node = isl_schedule_get_root(sched);
507 node = isl_schedule_node_child(node, 0);
508 umap = isl_schedule_node_get_subtree_schedule_union_map(node);
509 isl_schedule_node_free(node);
511 return umap;
514 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
515 return pad_schedule_map(umap);
518 static __isl_give isl_band_list *construct_band_list(
519 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
520 __isl_keep isl_band *parent);
522 /* Construct an isl_band structure from the given schedule tree node,
523 * which may be either a band node or a leaf node.
524 * In the latter case, construct a zero-dimensional band.
525 * "domain" is the universe set of the domain elements that reach "node".
526 * "parent" is the parent isl_band of the isl_band constructed
527 * by this function.
529 * In case of a band node, we copy the properties (except tilability,
530 * which is implicit in an isl_band) to the isl_band.
531 * We assume that the band node is not zero-dimensional.
532 * If the child of the band node is not a leaf node,
533 * then we extract the children of the isl_band from this child.
535 static __isl_give isl_band *construct_band(__isl_take isl_schedule_node *node,
536 __isl_take isl_union_set *domain, __isl_keep isl_band *parent)
538 int i;
539 isl_ctx *ctx;
540 isl_band *band = NULL;
541 isl_multi_union_pw_aff *mupa;
543 if (!node || !domain)
544 goto error;
546 ctx = isl_schedule_node_get_ctx(node);
547 band = isl_band_alloc(ctx);
548 if (!band)
549 goto error;
551 band->schedule = node->schedule;
552 band->parent = parent;
554 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
555 band->n = 0;
556 band->pma = isl_union_pw_multi_aff_from_domain(domain);
557 isl_schedule_node_free(node);
558 return band;
561 band->n = isl_schedule_node_band_n_member(node);
562 if (band->n == 0)
563 isl_die(ctx, isl_error_unsupported,
564 "zero-dimensional band nodes not supported",
565 goto error);
566 band->coincident = isl_alloc_array(ctx, int, band->n);
567 if (band->n && !band->coincident)
568 goto error;
569 for (i = 0; i < band->n; ++i)
570 band->coincident[i] =
571 isl_schedule_node_band_member_get_coincident(node, i);
572 mupa = isl_schedule_node_band_get_partial_schedule(node);
573 band->pma = isl_union_pw_multi_aff_from_multi_union_pw_aff(mupa);
574 if (!band->pma)
575 goto error;
577 node = isl_schedule_node_child(node, 0);
578 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
579 isl_schedule_node_free(node);
580 isl_union_set_free(domain);
581 return band;
584 band->children = construct_band_list(node, domain, band);
585 if (!band->children)
586 return isl_band_free(band);
588 return band;
589 error:
590 isl_union_set_free(domain);
591 isl_schedule_node_free(node);
592 isl_band_free(band);
593 return NULL;
596 /* Construct a list of isl_band structures from the children of "node".
597 * "node" itself is a sequence or set node, so that each of the child nodes
598 * is a filter node and the list returned by node_construct_band_list
599 * consists of a single element.
600 * "domain" is the universe set of the domain elements that reach "node".
601 * "parent" is the parent isl_band of the isl_band structures constructed
602 * by this function.
604 static __isl_give isl_band_list *construct_band_list_from_children(
605 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
606 __isl_keep isl_band *parent)
608 int i, n;
609 isl_ctx *ctx;
610 isl_band_list *list;
612 n = isl_schedule_node_n_children(node);
614 ctx = isl_schedule_node_get_ctx(node);
615 list = isl_band_list_alloc(ctx, 0);
616 for (i = 0; i < n; ++i) {
617 isl_schedule_node *child;
618 isl_band_list *list_i;
620 child = isl_schedule_node_get_child(node, i);
621 list_i = construct_band_list(child, isl_union_set_copy(domain),
622 parent);
623 list = isl_band_list_concat(list, list_i);
626 isl_union_set_free(domain);
627 isl_schedule_node_free(node);
629 return list;
632 /* Construct an isl_band structure from the given sequence node
633 * (or set node that is treated as a sequence node).
634 * A single-dimensional band is created with as schedule for each of
635 * filters of the children, the corresponding child position.
636 * "domain" is the universe set of the domain elements that reach "node".
637 * "parent" is the parent isl_band of the isl_band constructed
638 * by this function.
640 static __isl_give isl_band_list *construct_band_list_sequence(
641 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
642 __isl_keep isl_band *parent)
644 int i, n;
645 isl_ctx *ctx;
646 isl_band *band = NULL;
647 isl_space *space;
648 isl_union_pw_multi_aff *upma;
650 if (!node || !domain)
651 goto error;
653 ctx = isl_schedule_node_get_ctx(node);
654 band = isl_band_alloc(ctx);
655 if (!band)
656 goto error;
658 band->schedule = node->schedule;
659 band->parent = parent;
660 band->n = 1;
661 band->coincident = isl_calloc_array(ctx, int, band->n);
662 if (!band->coincident)
663 goto error;
665 n = isl_schedule_node_n_children(node);
666 space = isl_union_set_get_space(domain);
667 upma = isl_union_pw_multi_aff_empty(isl_space_copy(space));
669 space = isl_space_set_from_params(space);
670 space = isl_space_add_dims(space, isl_dim_set, 1);
672 for (i = 0; i < n; ++i) {
673 isl_schedule_node *child;
674 isl_union_set *filter;
675 isl_val *v;
676 isl_val_list *vl;
677 isl_multi_val *mv;
678 isl_union_pw_multi_aff *upma_i;
680 child = isl_schedule_node_get_child(node, i);
681 filter = isl_schedule_node_filter_get_filter(child);
682 isl_schedule_node_free(child);
683 filter = isl_union_set_intersect(filter,
684 isl_union_set_copy(domain));
685 v = isl_val_int_from_si(ctx, i);
686 vl = isl_val_list_from_val(v);
687 mv = isl_multi_val_from_val_list(isl_space_copy(space), vl);
688 upma_i = isl_union_pw_multi_aff_multi_val_on_domain(filter, mv);
689 upma = isl_union_pw_multi_aff_union_add(upma, upma_i);
692 isl_space_free(space);
694 band->pma = upma;
695 if (!band->pma)
696 goto error;
698 band->children = construct_band_list_from_children(node, domain, band);
699 if (!band->children)
700 band = isl_band_free(band);
701 return isl_band_list_from_band(band);
702 error:
703 isl_union_set_free(domain);
704 isl_schedule_node_free(node);
705 isl_band_free(band);
706 return NULL;
709 /* Construct a list of isl_band structures from "node" depending
710 * on the type of "node".
711 * "domain" is the universe set of the domain elements that reach "node".
712 * "parent" is the parent isl_band of the isl_band structures constructed
713 * by this function.
715 * If schedule_separate_components is set then set nodes are treated
716 * as sequence nodes. Otherwise, we directly extract an (implicitly
717 * parallel) list of isl_band structures.
719 * If "node" is a filter, then "domain" is updated by the filter.
721 static __isl_give isl_band_list *construct_band_list(
722 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
723 __isl_keep isl_band *parent)
725 enum isl_schedule_node_type type;
726 isl_ctx *ctx;
727 isl_band *band;
728 isl_band_list *list;
729 isl_union_set *filter;
731 if (!node || !domain)
732 goto error;
734 type = isl_schedule_node_get_type(node);
735 switch (type) {
736 case isl_schedule_node_error:
737 goto error;
738 case isl_schedule_node_context:
739 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
740 "context nodes not supported", goto error);
741 case isl_schedule_node_domain:
742 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
743 "internal domain nodes not allowed", goto error);
744 case isl_schedule_node_expansion:
745 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
746 "expansion nodes not supported", goto error);
747 case isl_schedule_node_extension:
748 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
749 "extension nodes not supported", goto error);
750 case isl_schedule_node_filter:
751 filter = isl_schedule_node_filter_get_filter(node);
752 domain = isl_union_set_intersect(domain, filter);
753 node = isl_schedule_node_child(node, 0);
754 return construct_band_list(node, domain, parent);
755 case isl_schedule_node_guard:
756 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
757 "guard nodes not supported", goto error);
758 case isl_schedule_node_mark:
759 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
760 "mark nodes not supported", goto error);
761 case isl_schedule_node_set:
762 ctx = isl_schedule_node_get_ctx(node);
763 if (isl_options_get_schedule_separate_components(ctx))
764 return construct_band_list_sequence(node, domain,
765 parent);
766 else
767 return construct_band_list_from_children(node, domain,
768 parent);
769 case isl_schedule_node_sequence:
770 return construct_band_list_sequence(node, domain, parent);
771 case isl_schedule_node_leaf:
772 case isl_schedule_node_band:
773 band = construct_band(node, domain, parent);
774 list = isl_band_list_from_band(band);
775 break;
778 return list;
779 error:
780 isl_union_set_free(domain);
781 isl_schedule_node_free(node);
782 return NULL;
785 /* Return the roots of a band forest representation of the schedule.
786 * The band forest is constructed from the schedule tree,
787 * but once such a band forest is
788 * constructed, we forget about the original schedule tree since
789 * the user may modify the schedule through the band forest.
791 __isl_give isl_band_list *isl_schedule_get_band_forest(
792 __isl_keep isl_schedule *schedule)
794 isl_schedule_node *node;
795 isl_union_set *domain;
797 if (!schedule)
798 return NULL;
799 if (schedule->root) {
800 node = isl_schedule_get_root(schedule);
801 domain = isl_schedule_node_domain_get_domain(node);
802 domain = isl_union_set_universe(domain);
803 node = isl_schedule_node_child(node, 0);
805 schedule->band_forest = construct_band_list(node, domain, NULL);
806 schedule->root = isl_schedule_tree_free(schedule->root);
808 return isl_band_list_dup(schedule->band_forest);
811 /* Call "fn" on each band in the schedule in depth-first post-order.
813 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
814 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
816 int r;
817 isl_band_list *forest;
819 if (!sched)
820 return -1;
822 forest = isl_schedule_get_band_forest(sched);
823 r = isl_band_list_foreach_band(forest, fn, user);
824 isl_band_list_free(forest);
826 return r;
829 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
830 __isl_keep isl_band_list *list);
832 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
833 __isl_keep isl_band *band)
835 isl_band_list *children;
837 p = isl_printer_start_line(p);
838 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
839 p = isl_printer_end_line(p);
841 if (!isl_band_has_children(band))
842 return p;
844 children = isl_band_get_children(band);
846 p = isl_printer_indent(p, 4);
847 p = print_band_list(p, children);
848 p = isl_printer_indent(p, -4);
850 isl_band_list_free(children);
852 return p;
855 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
856 __isl_keep isl_band_list *list)
858 int i, n;
860 n = isl_band_list_n_band(list);
861 for (i = 0; i < n; ++i) {
862 isl_band *band;
863 band = isl_band_list_get_band(list, i);
864 p = print_band(p, band);
865 isl_band_free(band);
868 return p;
871 /* Insert a band node with partial schedule "partial" between the domain
872 * root node of "schedule" and its single child.
873 * Return a pointer to the updated schedule.
875 * If any of the nodes in the tree depend on the set of outer band nodes
876 * then we refuse to insert the band node.
878 __isl_give isl_schedule *isl_schedule_insert_partial_schedule(
879 __isl_take isl_schedule *schedule,
880 __isl_take isl_multi_union_pw_aff *partial)
882 isl_schedule_node *node;
883 int anchored;
885 node = isl_schedule_get_root(schedule);
886 isl_schedule_free(schedule);
887 if (!node)
888 goto error;
889 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
890 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
891 "root node not a domain node", goto error);
893 node = isl_schedule_node_child(node, 0);
894 anchored = isl_schedule_node_is_subtree_anchored(node);
895 if (anchored < 0)
896 goto error;
897 if (anchored)
898 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
899 "cannot insert band node in anchored subtree",
900 goto error);
901 node = isl_schedule_node_insert_partial_schedule(node, partial);
903 schedule = isl_schedule_node_get_schedule(node);
904 isl_schedule_node_free(node);
906 return schedule;
907 error:
908 isl_schedule_node_free(node);
909 isl_multi_union_pw_aff_free(partial);
910 return NULL;
913 /* Insert a context node with constraints "context" between the domain
914 * root node of "schedule" and its single child.
915 * Return a pointer to the updated schedule.
917 __isl_give isl_schedule *isl_schedule_insert_context(
918 __isl_take isl_schedule *schedule, __isl_take isl_set *context)
920 isl_schedule_node *node;
922 node = isl_schedule_get_root(schedule);
923 isl_schedule_free(schedule);
924 node = isl_schedule_node_child(node, 0);
925 node = isl_schedule_node_insert_context(node, context);
926 schedule = isl_schedule_node_get_schedule(node);
927 isl_schedule_node_free(node);
929 return schedule;
932 /* Insert a guard node with constraints "guard" between the domain
933 * root node of "schedule" and its single child.
934 * Return a pointer to the updated schedule.
936 __isl_give isl_schedule *isl_schedule_insert_guard(
937 __isl_take isl_schedule *schedule, __isl_take isl_set *guard)
939 isl_schedule_node *node;
941 node = isl_schedule_get_root(schedule);
942 isl_schedule_free(schedule);
943 node = isl_schedule_node_child(node, 0);
944 node = isl_schedule_node_insert_guard(node, guard);
945 schedule = isl_schedule_node_get_schedule(node);
946 isl_schedule_node_free(node);
948 return schedule;
951 /* Return a tree with as top-level node a filter corresponding to "filter" and
952 * as child, the (single) child of "tree".
953 * However, if this single child is of type "type", then the filter is inserted
954 * in the children of this single child instead.
956 static __isl_give isl_schedule_tree *insert_filter_in_child_of_type(
957 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter,
958 enum isl_schedule_node_type type)
960 if (!isl_schedule_tree_has_children(tree)) {
961 isl_schedule_tree_free(tree);
962 return isl_schedule_tree_from_filter(filter);
963 } else {
964 tree = isl_schedule_tree_child(tree, 0);
967 if (isl_schedule_tree_get_type(tree) == type)
968 tree = isl_schedule_tree_children_insert_filter(tree, filter);
969 else
970 tree = isl_schedule_tree_insert_filter(tree, filter);
972 return tree;
975 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
976 * with a top-level node (underneath the domain node) of type "type",
977 * either isl_schedule_node_sequence or isl_schedule_node_set.
978 * The domains of the two schedules are assumed to be disjoint.
980 * The new schedule has as domain the union of the domains of the two
981 * schedules. The child of the domain node is a node of type "type"
982 * with two filters corresponding to the domains of the input schedules.
983 * If one (or both) of the top-level nodes of the two schedules is itself
984 * of type "type", then the filter is pushed into the children of that
985 * node and the sequence of set is flattened.
987 __isl_give isl_schedule *isl_schedule_pair(enum isl_schedule_node_type type,
988 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
990 int disjoint;
991 isl_ctx *ctx;
992 enum isl_schedule_node_type root_type;
993 isl_schedule_tree *tree1, *tree2;
994 isl_union_set *filter1, *filter2, *domain;
996 if (!schedule1 || !schedule2)
997 goto error;
999 root_type = isl_schedule_tree_get_type(schedule1->root);
1000 if (root_type != isl_schedule_node_domain)
1001 isl_die(isl_schedule_get_ctx(schedule1), isl_error_internal,
1002 "root node not a domain node", goto error);
1003 root_type = isl_schedule_tree_get_type(schedule2->root);
1004 if (root_type != isl_schedule_node_domain)
1005 isl_die(isl_schedule_get_ctx(schedule1), isl_error_internal,
1006 "root node not a domain node", goto error);
1008 ctx = isl_schedule_get_ctx(schedule1);
1009 tree1 = isl_schedule_tree_copy(schedule1->root);
1010 filter1 = isl_schedule_tree_domain_get_domain(tree1);
1011 tree2 = isl_schedule_tree_copy(schedule2->root);
1012 filter2 = isl_schedule_tree_domain_get_domain(tree2);
1014 isl_schedule_free(schedule1);
1015 isl_schedule_free(schedule2);
1017 disjoint = isl_union_set_is_disjoint(filter1, filter2);
1018 if (disjoint < 0)
1019 filter1 = isl_union_set_free(filter1);
1020 if (!disjoint)
1021 isl_die(ctx, isl_error_invalid,
1022 "schedule domains not disjoint",
1023 filter1 = isl_union_set_free(filter1));
1025 domain = isl_union_set_union(isl_union_set_copy(filter1),
1026 isl_union_set_copy(filter2));
1027 filter1 = isl_union_set_gist(filter1, isl_union_set_copy(domain));
1028 filter2 = isl_union_set_gist(filter2, isl_union_set_copy(domain));
1030 tree1 = insert_filter_in_child_of_type(tree1, filter1, type);
1031 tree2 = insert_filter_in_child_of_type(tree2, filter2, type);
1033 tree1 = isl_schedule_tree_from_pair(type, tree1, tree2);
1034 tree1 = isl_schedule_tree_insert_domain(tree1, domain);
1036 return isl_schedule_from_schedule_tree(ctx, tree1);
1037 error:
1038 isl_schedule_free(schedule1);
1039 isl_schedule_free(schedule2);
1040 return NULL;
1043 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1044 * through a sequence node.
1045 * The domains of the input schedules are assumed to be disjoint.
1047 __isl_give isl_schedule *isl_schedule_sequence(
1048 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
1050 return isl_schedule_pair(isl_schedule_node_sequence,
1051 schedule1, schedule2);
1054 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1055 * through a set node.
1056 * The domains of the input schedules are assumed to be disjoint.
1058 __isl_give isl_schedule *isl_schedule_set(
1059 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
1061 return isl_schedule_pair(isl_schedule_node_set, schedule1, schedule2);
1064 /* Print "schedule" to "p".
1066 * If "schedule" was created from a schedule tree, then we print
1067 * the schedule tree representation. Otherwise, we print
1068 * the band forest representation.
1070 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
1071 __isl_keep isl_schedule *schedule)
1073 isl_band_list *forest;
1075 if (!schedule)
1076 return isl_printer_free(p);
1078 if (schedule->root)
1079 return isl_printer_print_schedule_tree(p, schedule->root);
1081 forest = isl_schedule_get_band_forest(schedule);
1083 p = print_band_list(p, forest);
1085 isl_band_list_free(forest);
1087 return p;
1090 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
1092 isl_printer *printer;
1094 if (!schedule)
1095 return;
1097 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
1098 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
1099 printer = isl_printer_print_schedule(printer, schedule);
1101 isl_printer_free(printer);