isl_schedule_read.c: read_band: avoid double free on error path
[isl.git] / isl_schedule.c
blob90d8d5da1cd2304cb67dd00d2e8d60e910a33350
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_aff_private.h>
16 #include <isl/map.h>
17 #include <isl/set.h>
18 #include <isl/schedule.h>
19 #include <isl/schedule_node.h>
20 #include <isl_sort.h>
21 #include <isl_schedule_private.h>
22 #include <isl_schedule_tree.h>
23 #include <isl_schedule_node_private.h>
24 #include <isl_band_private.h>
26 /* Return a schedule encapsulating the given schedule tree.
28 * We currently only allow schedule trees with a domain or extension as root.
30 * The leaf field is initialized as a leaf node so that it can be
31 * used to represent leaves in the constructed schedule.
32 * The reference count is set to -1 since the isl_schedule_tree
33 * should never be freed. It is up to the (internal) users of
34 * these leaves to ensure that they are only used while the schedule
35 * is still alive.
37 __isl_give isl_schedule *isl_schedule_from_schedule_tree(isl_ctx *ctx,
38 __isl_take isl_schedule_tree *tree)
40 enum isl_schedule_node_type type;
41 isl_schedule *schedule;
43 if (!tree)
44 return NULL;
45 type = isl_schedule_tree_get_type(tree);
46 if (type != isl_schedule_node_domain &&
47 type != isl_schedule_node_extension)
48 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
49 "root of schedule tree should be a domain or extension",
50 goto error);
52 schedule = isl_calloc_type(ctx, isl_schedule);
53 if (!schedule)
54 goto error;
56 schedule->ref = 1;
57 schedule->root = tree;
58 schedule->leaf = isl_schedule_tree_leaf(ctx);
60 if (!schedule->leaf)
61 return isl_schedule_free(schedule);
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_schedule_tree_free(sched->leaf);
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 ? isl_schedule_tree_get_ctx(schedule->leaf) : NULL;
172 /* Return a pointer to the leaf of "schedule".
174 __isl_keep isl_schedule_tree *isl_schedule_peek_leaf(
175 __isl_keep isl_schedule *schedule)
177 return schedule ? schedule->leaf : NULL;
180 /* Are "schedule1" and "schedule2" obviously equal to each other?
182 isl_bool isl_schedule_plain_is_equal(__isl_keep isl_schedule *schedule1,
183 __isl_keep isl_schedule *schedule2)
185 if (!schedule1 || !schedule2)
186 return isl_bool_error;
187 if (schedule1 == schedule2)
188 return isl_bool_true;
189 return isl_schedule_tree_plain_is_equal(schedule1->root,
190 schedule2->root);
193 /* Return the (parameter) space of the schedule, i.e., the space
194 * of the root domain.
196 __isl_give isl_space *isl_schedule_get_space(
197 __isl_keep isl_schedule *schedule)
199 enum isl_schedule_node_type type;
200 isl_space *space;
201 isl_union_set *domain;
203 if (!schedule)
204 return NULL;
205 if (!schedule->root)
206 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
207 "schedule tree representation not available",
208 return NULL);
209 type = isl_schedule_tree_get_type(schedule->root);
210 if (type != isl_schedule_node_domain)
211 isl_die(isl_schedule_get_ctx(schedule), isl_error_internal,
212 "root node not a domain node", return NULL);
214 domain = isl_schedule_tree_domain_get_domain(schedule->root);
215 space = isl_union_set_get_space(domain);
216 isl_union_set_free(domain);
218 return space;
221 /* Return a pointer to the root of "schedule".
223 __isl_give isl_schedule_node *isl_schedule_get_root(
224 __isl_keep isl_schedule *schedule)
226 isl_ctx *ctx;
227 isl_schedule_tree *tree;
228 isl_schedule_tree_list *ancestors;
230 if (!schedule)
231 return NULL;
233 if (!schedule->root)
234 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
235 "schedule tree representation not available",
236 return NULL);
238 ctx = isl_schedule_get_ctx(schedule);
239 tree = isl_schedule_tree_copy(schedule->root);
240 schedule = isl_schedule_copy(schedule);
241 ancestors = isl_schedule_tree_list_alloc(ctx, 0);
242 return isl_schedule_node_alloc(schedule, tree, ancestors, NULL);
245 /* Set max_out to the maximal number of output dimensions over
246 * all maps.
248 static isl_stat update_max_out(__isl_take isl_map *map, void *user)
250 int *max_out = user;
251 int n_out = isl_map_dim(map, isl_dim_out);
253 if (n_out > *max_out)
254 *max_out = n_out;
256 isl_map_free(map);
257 return isl_stat_ok;
260 /* Internal data structure for map_pad_range.
262 * "max_out" is the maximal schedule dimension.
263 * "res" collects the results.
265 struct isl_pad_schedule_map_data {
266 int max_out;
267 isl_union_map *res;
270 /* Pad the range of the given map with zeros to data->max_out and
271 * then add the result to data->res.
273 static isl_stat map_pad_range(__isl_take isl_map *map, void *user)
275 struct isl_pad_schedule_map_data *data = user;
276 int i;
277 int n_out = isl_map_dim(map, isl_dim_out);
279 map = isl_map_add_dims(map, isl_dim_out, data->max_out - n_out);
280 for (i = n_out; i < data->max_out; ++i)
281 map = isl_map_fix_si(map, isl_dim_out, i, 0);
283 data->res = isl_union_map_add_map(data->res, map);
284 if (!data->res)
285 return isl_stat_error;
287 return isl_stat_ok;
290 /* Pad the ranges of the maps in the union map with zeros such they all have
291 * the same dimension.
293 static __isl_give isl_union_map *pad_schedule_map(
294 __isl_take isl_union_map *umap)
296 struct isl_pad_schedule_map_data data;
298 if (!umap)
299 return NULL;
300 if (isl_union_map_n_map(umap) <= 1)
301 return umap;
303 data.max_out = 0;
304 if (isl_union_map_foreach_map(umap, &update_max_out, &data.max_out) < 0)
305 return isl_union_map_free(umap);
307 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
308 if (isl_union_map_foreach_map(umap, &map_pad_range, &data) < 0)
309 data.res = isl_union_map_free(data.res);
311 isl_union_map_free(umap);
312 return data.res;
315 /* Return the domain of the root domain node of "schedule".
317 __isl_give isl_union_set *isl_schedule_get_domain(
318 __isl_keep isl_schedule *schedule)
320 if (!schedule)
321 return NULL;
322 if (!schedule->root)
323 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
324 "schedule tree representation not available",
325 return NULL);
326 return isl_schedule_tree_domain_get_domain(schedule->root);
329 /* Traverse all nodes of "sched" in depth first preorder.
331 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
332 * If "fn" returns 0 on any of the nodes, then the subtree rooted
333 * at that node is skipped.
335 * Return 0 on success and -1 on failure.
337 isl_stat isl_schedule_foreach_schedule_node_top_down(
338 __isl_keep isl_schedule *sched,
339 isl_bool (*fn)(__isl_keep isl_schedule_node *node, void *user),
340 void *user)
342 isl_schedule_node *node;
343 isl_stat r;
345 if (!sched)
346 return isl_stat_error;
348 node = isl_schedule_get_root(sched);
349 r = isl_schedule_node_foreach_descendant_top_down(node, fn, user);
350 isl_schedule_node_free(node);
352 return r;
355 /* Traverse the node of "sched" in depth first postorder,
356 * allowing the user to modify the visited node.
357 * The traversal continues from the node returned by the callback function.
358 * It is the responsibility of the user to ensure that this does not
359 * lead to an infinite loop. It is safest to always return a pointer
360 * to the same position (same ancestors and child positions) as the input node.
362 __isl_give isl_schedule *isl_schedule_map_schedule_node_bottom_up(
363 __isl_take isl_schedule *schedule,
364 __isl_give isl_schedule_node *(*fn)(
365 __isl_take isl_schedule_node *node, void *user), void *user)
367 isl_schedule_node *node;
369 node = isl_schedule_get_root(schedule);
370 isl_schedule_free(schedule);
372 node = isl_schedule_node_map_descendant_bottom_up(node, fn, user);
373 schedule = isl_schedule_node_get_schedule(node);
374 isl_schedule_node_free(node);
376 return schedule;
379 /* Wrapper around isl_schedule_node_reset_user for use as
380 * an isl_schedule_map_schedule_node_bottom_up callback.
382 static __isl_give isl_schedule_node *reset_user(
383 __isl_take isl_schedule_node *node, void *user)
385 return isl_schedule_node_reset_user(node);
388 /* Reset the user pointer on all identifiers of parameters and tuples
389 * in the schedule "schedule".
391 __isl_give isl_schedule *isl_schedule_reset_user(
392 __isl_take isl_schedule *schedule)
394 return isl_schedule_map_schedule_node_bottom_up(schedule, &reset_user,
395 NULL);
398 /* Wrapper around isl_schedule_node_align_params for use as
399 * an isl_schedule_map_schedule_node_bottom_up callback.
401 static __isl_give isl_schedule_node *align_params(
402 __isl_take isl_schedule_node *node, void *user)
404 isl_space *space = user;
406 return isl_schedule_node_align_params(node, isl_space_copy(space));
409 /* Align the parameters of all nodes in schedule "schedule"
410 * to those of "space".
412 __isl_give isl_schedule *isl_schedule_align_params(
413 __isl_take isl_schedule *schedule, __isl_take isl_space *space)
415 schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
416 &align_params, space);
417 isl_space_free(space);
418 return schedule;
421 /* Wrapper around isl_schedule_node_pullback_union_pw_multi_aff for use as
422 * an isl_schedule_map_schedule_node_bottom_up callback.
424 static __isl_give isl_schedule_node *pullback_upma(
425 __isl_take isl_schedule_node *node, void *user)
427 isl_union_pw_multi_aff *upma = user;
429 return isl_schedule_node_pullback_union_pw_multi_aff(node,
430 isl_union_pw_multi_aff_copy(upma));
433 /* Compute the pullback of "schedule" by the function represented by "upma".
434 * In other words, plug in "upma" in the iteration domains of "schedule".
436 * The schedule tree is not allowed to contain any expansion nodes.
438 __isl_give isl_schedule *isl_schedule_pullback_union_pw_multi_aff(
439 __isl_take isl_schedule *schedule,
440 __isl_take isl_union_pw_multi_aff *upma)
442 schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
443 &pullback_upma, upma);
444 isl_union_pw_multi_aff_free(upma);
445 return schedule;
448 /* Expand the schedule "schedule" by extending all leaves
449 * with an expansion node with as subtree the tree of "expansion".
450 * The expansion of the expansion node is determined by "contraction"
451 * and the domain of "expansion". That is, the domain of "expansion"
452 * is contracted according to "contraction".
454 * Call isl_schedule_node_expand after extracting the required
455 * information from "expansion".
457 __isl_give isl_schedule *isl_schedule_expand(__isl_take isl_schedule *schedule,
458 __isl_take isl_union_pw_multi_aff *contraction,
459 __isl_take isl_schedule *expansion)
461 isl_union_set *domain;
462 isl_schedule_node *node;
463 isl_schedule_tree *tree;
465 domain = isl_schedule_get_domain(expansion);
467 node = isl_schedule_get_root(expansion);
468 node = isl_schedule_node_child(node, 0);
469 tree = isl_schedule_node_get_tree(node);
470 isl_schedule_node_free(node);
471 isl_schedule_free(expansion);
473 node = isl_schedule_get_root(schedule);
474 isl_schedule_free(schedule);
475 node = isl_schedule_node_expand(node, contraction, domain, tree);
476 schedule = isl_schedule_node_get_schedule(node);
477 isl_schedule_node_free(node);
479 return schedule;
482 /* Intersect the domain of the schedule "schedule" with "domain".
483 * The root of "schedule" is required to be a domain node.
485 __isl_give isl_schedule *isl_schedule_intersect_domain(
486 __isl_take isl_schedule *schedule, __isl_take isl_union_set *domain)
488 enum isl_schedule_node_type root_type;
489 isl_schedule_node *node;
491 if (!schedule || !domain)
492 goto error;
494 root_type = isl_schedule_tree_get_type(schedule->root);
495 if (root_type != isl_schedule_node_domain)
496 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
497 "root node must be a domain node", goto error);
499 node = isl_schedule_get_root(schedule);
500 isl_schedule_free(schedule);
501 node = isl_schedule_node_domain_intersect_domain(node, domain);
502 schedule = isl_schedule_node_get_schedule(node);
503 isl_schedule_node_free(node);
505 return schedule;
506 error:
507 isl_schedule_free(schedule);
508 isl_union_set_free(domain);
509 return NULL;
512 /* Replace the domain of the schedule "schedule" with the gist
513 * of the original domain with respect to the parameter domain "context".
515 __isl_give isl_schedule *isl_schedule_gist_domain_params(
516 __isl_take isl_schedule *schedule, __isl_take isl_set *context)
518 enum isl_schedule_node_type root_type;
519 isl_schedule_node *node;
521 if (!schedule || !context)
522 goto error;
524 root_type = isl_schedule_tree_get_type(schedule->root);
525 if (root_type != isl_schedule_node_domain)
526 isl_die(isl_schedule_get_ctx(schedule), isl_error_invalid,
527 "root node must be a domain node", goto error);
529 node = isl_schedule_get_root(schedule);
530 isl_schedule_free(schedule);
531 node = isl_schedule_node_domain_gist_params(node, context);
532 schedule = isl_schedule_node_get_schedule(node);
533 isl_schedule_node_free(node);
535 return schedule;
536 error:
537 isl_schedule_free(schedule);
538 isl_set_free(context);
539 return NULL;
542 /* Return an isl_union_map representation of the schedule.
543 * If we still have access to the schedule tree, then we return
544 * an isl_union_map corresponding to the subtree schedule of the child
545 * of the root domain node. That is, we do not intersect the domain
546 * of the returned isl_union_map with the domain constraints.
547 * Otherwise, we must have removed it because we created a band forest.
548 * If so, we extract the isl_union_map from the forest.
549 * This reconstructed schedule map
550 * then needs to be padded with zeros to unify the schedule space
551 * since the result of isl_band_list_get_suffix_schedule may not have
552 * a unified schedule space.
554 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
556 enum isl_schedule_node_type type;
557 isl_schedule_node *node;
558 isl_union_map *umap;
560 if (!sched)
561 return NULL;
563 if (sched->root) {
564 type = isl_schedule_tree_get_type(sched->root);
565 if (type != isl_schedule_node_domain)
566 isl_die(isl_schedule_get_ctx(sched), isl_error_internal,
567 "root node not a domain node", return NULL);
569 node = isl_schedule_get_root(sched);
570 node = isl_schedule_node_child(node, 0);
571 umap = isl_schedule_node_get_subtree_schedule_union_map(node);
572 isl_schedule_node_free(node);
574 return umap;
577 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
578 return pad_schedule_map(umap);
581 static __isl_give isl_band_list *construct_band_list(
582 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
583 __isl_keep isl_band *parent);
585 /* Construct an isl_band structure from the given schedule tree node,
586 * which may be either a band node or a leaf node.
587 * In the latter case, construct a zero-dimensional band.
588 * "domain" is the universe set of the domain elements that reach "node".
589 * "parent" is the parent isl_band of the isl_band constructed
590 * by this function.
592 * In case of a band node, we copy the properties (except tilability,
593 * which is implicit in an isl_band) to the isl_band.
594 * We assume that the band node is not zero-dimensional.
595 * If the child of the band node is not a leaf node,
596 * then we extract the children of the isl_band from this child.
598 static __isl_give isl_band *construct_band(__isl_take isl_schedule_node *node,
599 __isl_take isl_union_set *domain, __isl_keep isl_band *parent)
601 int i;
602 isl_ctx *ctx;
603 isl_band *band = NULL;
604 isl_multi_union_pw_aff *mupa;
606 if (!node || !domain)
607 goto error;
609 ctx = isl_schedule_node_get_ctx(node);
610 band = isl_band_alloc(ctx);
611 if (!band)
612 goto error;
614 band->schedule = node->schedule;
615 band->parent = parent;
617 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
618 band->n = 0;
619 band->pma = isl_union_pw_multi_aff_from_domain(domain);
620 isl_schedule_node_free(node);
621 return band;
624 band->n = isl_schedule_node_band_n_member(node);
625 if (band->n == 0)
626 isl_die(ctx, isl_error_unsupported,
627 "zero-dimensional band nodes not supported",
628 goto error);
629 band->coincident = isl_alloc_array(ctx, int, band->n);
630 if (band->n && !band->coincident)
631 goto error;
632 for (i = 0; i < band->n; ++i)
633 band->coincident[i] =
634 isl_schedule_node_band_member_get_coincident(node, i);
635 mupa = isl_schedule_node_band_get_partial_schedule(node);
636 band->pma = isl_union_pw_multi_aff_from_multi_union_pw_aff(mupa);
637 if (!band->pma)
638 goto error;
640 node = isl_schedule_node_child(node, 0);
641 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
642 isl_schedule_node_free(node);
643 isl_union_set_free(domain);
644 return band;
647 band->children = construct_band_list(node, domain, band);
648 if (!band->children)
649 return isl_band_free(band);
651 return band;
652 error:
653 isl_union_set_free(domain);
654 isl_schedule_node_free(node);
655 isl_band_free(band);
656 return NULL;
659 /* Construct a list of isl_band structures from the children of "node".
660 * "node" itself is a sequence or set node, so that each of the child nodes
661 * is a filter node and the list returned by node_construct_band_list
662 * consists of a single element.
663 * "domain" is the universe set of the domain elements that reach "node".
664 * "parent" is the parent isl_band of the isl_band structures constructed
665 * by this function.
667 static __isl_give isl_band_list *construct_band_list_from_children(
668 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
669 __isl_keep isl_band *parent)
671 int i, n;
672 isl_ctx *ctx;
673 isl_band_list *list;
675 n = isl_schedule_node_n_children(node);
677 ctx = isl_schedule_node_get_ctx(node);
678 list = isl_band_list_alloc(ctx, 0);
679 for (i = 0; i < n; ++i) {
680 isl_schedule_node *child;
681 isl_band_list *list_i;
683 child = isl_schedule_node_get_child(node, i);
684 list_i = construct_band_list(child, isl_union_set_copy(domain),
685 parent);
686 list = isl_band_list_concat(list, list_i);
689 isl_union_set_free(domain);
690 isl_schedule_node_free(node);
692 return list;
695 /* Construct an isl_band structure from the given sequence node
696 * (or set node that is treated as a sequence node).
697 * A single-dimensional band is created with as schedule for each of
698 * filters of the children, the corresponding child position.
699 * "domain" is the universe set of the domain elements that reach "node".
700 * "parent" is the parent isl_band of the isl_band constructed
701 * by this function.
703 static __isl_give isl_band_list *construct_band_list_sequence(
704 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
705 __isl_keep isl_band *parent)
707 int i, n;
708 isl_ctx *ctx;
709 isl_band *band = NULL;
710 isl_space *space;
711 isl_union_pw_multi_aff *upma;
713 if (!node || !domain)
714 goto error;
716 ctx = isl_schedule_node_get_ctx(node);
717 band = isl_band_alloc(ctx);
718 if (!band)
719 goto error;
721 band->schedule = node->schedule;
722 band->parent = parent;
723 band->n = 1;
724 band->coincident = isl_calloc_array(ctx, int, band->n);
725 if (!band->coincident)
726 goto error;
728 n = isl_schedule_node_n_children(node);
729 space = isl_union_set_get_space(domain);
730 upma = isl_union_pw_multi_aff_empty(isl_space_copy(space));
732 space = isl_space_set_from_params(space);
733 space = isl_space_add_dims(space, isl_dim_set, 1);
735 for (i = 0; i < n; ++i) {
736 isl_schedule_node *child;
737 isl_union_set *filter;
738 isl_val *v;
739 isl_val_list *vl;
740 isl_multi_val *mv;
741 isl_union_pw_multi_aff *upma_i;
743 child = isl_schedule_node_get_child(node, i);
744 filter = isl_schedule_node_filter_get_filter(child);
745 isl_schedule_node_free(child);
746 filter = isl_union_set_intersect(filter,
747 isl_union_set_copy(domain));
748 v = isl_val_int_from_si(ctx, i);
749 vl = isl_val_list_from_val(v);
750 mv = isl_multi_val_from_val_list(isl_space_copy(space), vl);
751 upma_i = isl_union_pw_multi_aff_multi_val_on_domain(filter, mv);
752 upma = isl_union_pw_multi_aff_union_add(upma, upma_i);
755 isl_space_free(space);
757 band->pma = upma;
758 if (!band->pma)
759 goto error;
761 band->children = construct_band_list_from_children(node, domain, band);
762 if (!band->children)
763 band = isl_band_free(band);
764 return isl_band_list_from_band(band);
765 error:
766 isl_union_set_free(domain);
767 isl_schedule_node_free(node);
768 isl_band_free(band);
769 return NULL;
772 /* Construct a list of isl_band structures from "node" depending
773 * on the type of "node".
774 * "domain" is the universe set of the domain elements that reach "node".
775 * "parent" is the parent isl_band of the isl_band structures constructed
776 * by this function.
778 * If schedule_separate_components is set then set nodes are treated
779 * as sequence nodes. Otherwise, we directly extract an (implicitly
780 * parallel) list of isl_band structures.
782 * If "node" is a filter, then "domain" is updated by the filter.
784 static __isl_give isl_band_list *construct_band_list(
785 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
786 __isl_keep isl_band *parent)
788 enum isl_schedule_node_type type;
789 isl_ctx *ctx;
790 isl_band *band;
791 isl_band_list *list;
792 isl_union_set *filter;
794 if (!node || !domain)
795 goto error;
797 type = isl_schedule_node_get_type(node);
798 switch (type) {
799 case isl_schedule_node_error:
800 goto error;
801 case isl_schedule_node_context:
802 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
803 "context nodes not supported", goto error);
804 case isl_schedule_node_domain:
805 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
806 "internal domain nodes not allowed", goto error);
807 case isl_schedule_node_expansion:
808 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
809 "expansion nodes not supported", goto error);
810 case isl_schedule_node_extension:
811 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
812 "extension nodes not supported", goto error);
813 case isl_schedule_node_filter:
814 filter = isl_schedule_node_filter_get_filter(node);
815 domain = isl_union_set_intersect(domain, filter);
816 node = isl_schedule_node_child(node, 0);
817 return construct_band_list(node, domain, parent);
818 case isl_schedule_node_guard:
819 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
820 "guard nodes not supported", goto error);
821 case isl_schedule_node_mark:
822 isl_die(isl_schedule_node_get_ctx(node), isl_error_unsupported,
823 "mark nodes not supported", goto error);
824 case isl_schedule_node_set:
825 ctx = isl_schedule_node_get_ctx(node);
826 if (isl_options_get_schedule_separate_components(ctx))
827 return construct_band_list_sequence(node, domain,
828 parent);
829 else
830 return construct_band_list_from_children(node, domain,
831 parent);
832 case isl_schedule_node_sequence:
833 return construct_band_list_sequence(node, domain, parent);
834 case isl_schedule_node_leaf:
835 case isl_schedule_node_band:
836 band = construct_band(node, domain, parent);
837 list = isl_band_list_from_band(band);
838 break;
841 return list;
842 error:
843 isl_union_set_free(domain);
844 isl_schedule_node_free(node);
845 return NULL;
848 /* Return the roots of a band forest representation of the schedule.
849 * The band forest is constructed from the schedule tree,
850 * but once such a band forest is
851 * constructed, we forget about the original schedule tree since
852 * the user may modify the schedule through the band forest.
854 __isl_give isl_band_list *isl_schedule_get_band_forest(
855 __isl_keep isl_schedule *schedule)
857 isl_schedule_node *node;
858 isl_union_set *domain;
860 if (!schedule)
861 return NULL;
862 if (schedule->root) {
863 node = isl_schedule_get_root(schedule);
864 domain = isl_schedule_node_domain_get_domain(node);
865 domain = isl_union_set_universe(domain);
866 node = isl_schedule_node_child(node, 0);
868 schedule->band_forest = construct_band_list(node, domain, NULL);
869 schedule->root = isl_schedule_tree_free(schedule->root);
871 return isl_band_list_dup(schedule->band_forest);
874 /* Call "fn" on each band in the schedule in depth-first post-order.
876 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
877 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
879 int r;
880 isl_band_list *forest;
882 if (!sched)
883 return -1;
885 forest = isl_schedule_get_band_forest(sched);
886 r = isl_band_list_foreach_band(forest, fn, user);
887 isl_band_list_free(forest);
889 return r;
892 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
893 __isl_keep isl_band_list *list);
895 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
896 __isl_keep isl_band *band)
898 isl_band_list *children;
900 p = isl_printer_start_line(p);
901 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
902 p = isl_printer_end_line(p);
904 if (!isl_band_has_children(band))
905 return p;
907 children = isl_band_get_children(band);
909 p = isl_printer_indent(p, 4);
910 p = print_band_list(p, children);
911 p = isl_printer_indent(p, -4);
913 isl_band_list_free(children);
915 return p;
918 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
919 __isl_keep isl_band_list *list)
921 int i, n;
923 n = isl_band_list_n_band(list);
924 for (i = 0; i < n; ++i) {
925 isl_band *band;
926 band = isl_band_list_get_band(list, i);
927 p = print_band(p, band);
928 isl_band_free(band);
931 return p;
934 /* Insert a band node with partial schedule "partial" between the domain
935 * root node of "schedule" and its single child.
936 * Return a pointer to the updated schedule.
938 * If any of the nodes in the tree depend on the set of outer band nodes
939 * then we refuse to insert the band node.
941 __isl_give isl_schedule *isl_schedule_insert_partial_schedule(
942 __isl_take isl_schedule *schedule,
943 __isl_take isl_multi_union_pw_aff *partial)
945 isl_schedule_node *node;
946 int anchored;
948 node = isl_schedule_get_root(schedule);
949 isl_schedule_free(schedule);
950 if (!node)
951 goto error;
952 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
953 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
954 "root node not a domain node", goto error);
956 node = isl_schedule_node_child(node, 0);
957 anchored = isl_schedule_node_is_subtree_anchored(node);
958 if (anchored < 0)
959 goto error;
960 if (anchored)
961 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
962 "cannot insert band node in anchored subtree",
963 goto error);
964 node = isl_schedule_node_insert_partial_schedule(node, partial);
966 schedule = isl_schedule_node_get_schedule(node);
967 isl_schedule_node_free(node);
969 return schedule;
970 error:
971 isl_schedule_node_free(node);
972 isl_multi_union_pw_aff_free(partial);
973 return NULL;
976 /* Insert a context node with constraints "context" between the domain
977 * root node of "schedule" and its single child.
978 * Return a pointer to the updated schedule.
980 __isl_give isl_schedule *isl_schedule_insert_context(
981 __isl_take isl_schedule *schedule, __isl_take isl_set *context)
983 isl_schedule_node *node;
985 node = isl_schedule_get_root(schedule);
986 isl_schedule_free(schedule);
987 node = isl_schedule_node_child(node, 0);
988 node = isl_schedule_node_insert_context(node, context);
989 schedule = isl_schedule_node_get_schedule(node);
990 isl_schedule_node_free(node);
992 return schedule;
995 /* Insert a guard node with constraints "guard" between the domain
996 * root node of "schedule" and its single child.
997 * Return a pointer to the updated schedule.
999 __isl_give isl_schedule *isl_schedule_insert_guard(
1000 __isl_take isl_schedule *schedule, __isl_take isl_set *guard)
1002 isl_schedule_node *node;
1004 node = isl_schedule_get_root(schedule);
1005 isl_schedule_free(schedule);
1006 node = isl_schedule_node_child(node, 0);
1007 node = isl_schedule_node_insert_guard(node, guard);
1008 schedule = isl_schedule_node_get_schedule(node);
1009 isl_schedule_node_free(node);
1011 return schedule;
1014 /* Return a tree with as top-level node a filter corresponding to "filter" and
1015 * as child, the (single) child of "tree".
1016 * However, if this single child is of type "type", then the filter is inserted
1017 * in the children of this single child instead.
1019 static __isl_give isl_schedule_tree *insert_filter_in_child_of_type(
1020 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter,
1021 enum isl_schedule_node_type type)
1023 if (!isl_schedule_tree_has_children(tree)) {
1024 isl_schedule_tree_free(tree);
1025 return isl_schedule_tree_from_filter(filter);
1026 } else {
1027 tree = isl_schedule_tree_child(tree, 0);
1030 if (isl_schedule_tree_get_type(tree) == type)
1031 tree = isl_schedule_tree_children_insert_filter(tree, filter);
1032 else
1033 tree = isl_schedule_tree_insert_filter(tree, filter);
1035 return tree;
1038 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1039 * with a top-level node (underneath the domain node) of type "type",
1040 * either isl_schedule_node_sequence or isl_schedule_node_set.
1041 * The domains of the two schedules are assumed to be disjoint.
1043 * The new schedule has as domain the union of the domains of the two
1044 * schedules. The child of the domain node is a node of type "type"
1045 * with two filters corresponding to the domains of the input schedules.
1046 * If one (or both) of the top-level nodes of the two schedules is itself
1047 * of type "type", then the filter is pushed into the children of that
1048 * node and the sequence of set is flattened.
1050 __isl_give isl_schedule *isl_schedule_pair(enum isl_schedule_node_type type,
1051 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
1053 int disjoint;
1054 isl_ctx *ctx;
1055 enum isl_schedule_node_type root_type;
1056 isl_schedule_tree *tree1, *tree2;
1057 isl_union_set *filter1, *filter2, *domain;
1059 if (!schedule1 || !schedule2)
1060 goto error;
1062 root_type = isl_schedule_tree_get_type(schedule1->root);
1063 if (root_type != isl_schedule_node_domain)
1064 isl_die(isl_schedule_get_ctx(schedule1), isl_error_internal,
1065 "root node not a domain node", goto error);
1066 root_type = isl_schedule_tree_get_type(schedule2->root);
1067 if (root_type != isl_schedule_node_domain)
1068 isl_die(isl_schedule_get_ctx(schedule1), isl_error_internal,
1069 "root node not a domain node", goto error);
1071 ctx = isl_schedule_get_ctx(schedule1);
1072 tree1 = isl_schedule_tree_copy(schedule1->root);
1073 filter1 = isl_schedule_tree_domain_get_domain(tree1);
1074 tree2 = isl_schedule_tree_copy(schedule2->root);
1075 filter2 = isl_schedule_tree_domain_get_domain(tree2);
1077 isl_schedule_free(schedule1);
1078 isl_schedule_free(schedule2);
1080 disjoint = isl_union_set_is_disjoint(filter1, filter2);
1081 if (disjoint < 0)
1082 filter1 = isl_union_set_free(filter1);
1083 if (!disjoint)
1084 isl_die(ctx, isl_error_invalid,
1085 "schedule domains not disjoint",
1086 filter1 = isl_union_set_free(filter1));
1088 domain = isl_union_set_union(isl_union_set_copy(filter1),
1089 isl_union_set_copy(filter2));
1090 filter1 = isl_union_set_gist(filter1, isl_union_set_copy(domain));
1091 filter2 = isl_union_set_gist(filter2, isl_union_set_copy(domain));
1093 tree1 = insert_filter_in_child_of_type(tree1, filter1, type);
1094 tree2 = insert_filter_in_child_of_type(tree2, filter2, type);
1096 tree1 = isl_schedule_tree_from_pair(type, tree1, tree2);
1097 tree1 = isl_schedule_tree_insert_domain(tree1, domain);
1099 return isl_schedule_from_schedule_tree(ctx, tree1);
1100 error:
1101 isl_schedule_free(schedule1);
1102 isl_schedule_free(schedule2);
1103 return NULL;
1106 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1107 * through a sequence node.
1108 * The domains of the input schedules are assumed to be disjoint.
1110 __isl_give isl_schedule *isl_schedule_sequence(
1111 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
1113 return isl_schedule_pair(isl_schedule_node_sequence,
1114 schedule1, schedule2);
1117 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1118 * through a set node.
1119 * The domains of the input schedules are assumed to be disjoint.
1121 __isl_give isl_schedule *isl_schedule_set(
1122 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
1124 return isl_schedule_pair(isl_schedule_node_set, schedule1, schedule2);
1127 /* Print "schedule" to "p".
1129 * If "schedule" was created from a schedule tree, then we print
1130 * the schedule tree representation. Otherwise, we print
1131 * the band forest representation.
1133 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
1134 __isl_keep isl_schedule *schedule)
1136 isl_band_list *forest;
1138 if (!schedule)
1139 return isl_printer_free(p);
1141 if (schedule->root)
1142 return isl_printer_print_schedule_tree(p, schedule->root);
1144 forest = isl_schedule_get_band_forest(schedule);
1146 p = print_band_list(p, forest);
1148 isl_band_list_free(forest);
1150 return p;
1153 #undef BASE
1154 #define BASE schedule
1155 #include <print_templ_yaml.c>