add isl_schedule_pullback_union_pw_multi_aff
[isl.git] / isl_schedule.c
blob988458083e1f414d15cd7050253cbe831f4a1761
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 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 isl_schedule *schedule;
41 if (!tree)
42 return NULL;
43 if (isl_schedule_tree_get_type(tree) != isl_schedule_node_domain)
44 isl_die(isl_schedule_tree_get_ctx(tree), isl_error_unsupported,
45 "root of schedule tree should be a domain",
46 goto error);
48 schedule = isl_calloc_type(ctx, isl_schedule);
49 if (!schedule)
50 goto error;
52 schedule->leaf.ctx = ctx;
53 isl_ctx_ref(ctx);
54 schedule->ref = 1;
55 schedule->root = tree;
56 schedule->leaf.ref = -1;
57 schedule->leaf.type = isl_schedule_node_leaf;
59 return schedule;
60 error:
61 isl_schedule_tree_free(tree);
62 return NULL;
65 /* Return a pointer to a schedule with as single node
66 * a domain node with the given domain.
68 __isl_give isl_schedule *isl_schedule_from_domain(
69 __isl_take isl_union_set *domain)
71 isl_ctx *ctx;
72 isl_schedule_tree *tree;
74 ctx = isl_union_set_get_ctx(domain);
75 tree = isl_schedule_tree_from_domain(domain);
76 return isl_schedule_from_schedule_tree(ctx, tree);
79 /* Return a pointer to a schedule with as single node
80 * a domain node with an empty domain.
82 __isl_give isl_schedule *isl_schedule_empty(__isl_take isl_space *space)
84 return isl_schedule_from_domain(isl_union_set_empty(space));
87 /* Return a new reference to "sched".
89 __isl_give isl_schedule *isl_schedule_copy(__isl_keep isl_schedule *sched)
91 if (!sched)
92 return NULL;
94 sched->ref++;
95 return sched;
98 /* Return an isl_schedule that is equal to "schedule" and that has only
99 * a single reference.
101 * We only need and support this function when the schedule is represented
102 * as a schedule tree.
104 __isl_give isl_schedule *isl_schedule_cow(__isl_take isl_schedule *schedule)
106 isl_ctx *ctx;
107 isl_schedule_tree *tree;
109 if (!schedule)
110 return NULL;
111 if (schedule->ref == 1)
112 return schedule;
114 ctx = isl_schedule_get_ctx(schedule);
115 if (!schedule->root)
116 isl_die(ctx, isl_error_internal,
117 "only for schedule tree based schedules",
118 return isl_schedule_free(schedule));
119 schedule->ref--;
120 tree = isl_schedule_tree_copy(schedule->root);
121 return isl_schedule_from_schedule_tree(ctx, tree);
124 __isl_null isl_schedule *isl_schedule_free(__isl_take isl_schedule *sched)
126 if (!sched)
127 return NULL;
129 if (--sched->ref > 0)
130 return NULL;
132 isl_band_list_free(sched->band_forest);
133 isl_schedule_tree_free(sched->root);
134 isl_ctx_deref(sched->leaf.ctx);
135 free(sched);
136 return NULL;
139 /* Replace the root of "schedule" by "tree".
141 __isl_give isl_schedule *isl_schedule_set_root(
142 __isl_take isl_schedule *schedule, __isl_take isl_schedule_tree *tree)
144 if (!schedule || !tree)
145 goto error;
146 if (schedule->root == tree) {
147 isl_schedule_tree_free(tree);
148 return schedule;
151 schedule = isl_schedule_cow(schedule);
152 if (!schedule)
153 goto error;
154 isl_schedule_tree_free(schedule->root);
155 schedule->root = tree;
157 return schedule;
158 error:
159 isl_schedule_free(schedule);
160 isl_schedule_tree_free(tree);
161 return NULL;
164 isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
166 return schedule ? schedule->leaf.ctx : NULL;
169 /* Return a pointer to the leaf of "schedule".
171 * Even though these leaves are not reference counted, we still
172 * indicate that this function does not return a copy.
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 int isl_schedule_plain_is_equal(__isl_keep isl_schedule *schedule1,
183 __isl_keep isl_schedule *schedule2)
185 if (!schedule1 || !schedule2)
186 return -1;
187 if (schedule1 == schedule2)
188 return 1;
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 int 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 0;
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 int 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 -1;
287 return 0;
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 int isl_schedule_foreach_schedule_node(__isl_keep isl_schedule *sched,
338 int (*fn)(__isl_keep isl_schedule_node *node, void *user), void *user)
340 isl_schedule_node *node;
341 int r;
343 if (!sched)
344 return -1;
346 node = isl_schedule_get_root(sched);
347 r = isl_schedule_node_foreach_descendant(node, fn, user);
348 isl_schedule_node_free(node);
350 return r;
353 /* Traverse the node of "sched" in depth first postorder,
354 * allowing the user to modify the visited node.
355 * The traversal continues from the node returned by the callback function.
356 * It is the responsibility of the user to ensure that this does not
357 * lead to an infinite loop. It is safest to always return a pointer
358 * to the same position (same ancestors and child positions) as the input node.
360 __isl_give isl_schedule *isl_schedule_map_schedule_node(
361 __isl_take isl_schedule *schedule,
362 __isl_give isl_schedule_node *(*fn)(
363 __isl_take isl_schedule_node *node, void *user), void *user)
365 isl_schedule_node *node;
367 node = isl_schedule_get_root(schedule);
368 isl_schedule_free(schedule);
370 node = isl_schedule_node_map_descendant(node, fn, user);
371 schedule = isl_schedule_node_get_schedule(node);
372 isl_schedule_node_free(node);
374 return schedule;
377 /* Wrapper around isl_schedule_node_reset_user for use as
378 * an isl_schedule_map_schedule_node callback.
380 static __isl_give isl_schedule_node *reset_user(
381 __isl_take isl_schedule_node *node, void *user)
383 return isl_schedule_node_reset_user(node);
386 /* Reset the user pointer on all identifiers of parameters and tuples
387 * in the schedule "schedule".
389 __isl_give isl_schedule *isl_schedule_reset_user(
390 __isl_take isl_schedule *schedule)
392 return isl_schedule_map_schedule_node(schedule, &reset_user, NULL);
395 /* Wrapper around isl_schedule_node_align_params for use as
396 * an isl_schedule_map_schedule_node callback.
398 static __isl_give isl_schedule_node *align_params(
399 __isl_take isl_schedule_node *node, void *user)
401 isl_space *space = user;
403 return isl_schedule_node_align_params(node, isl_space_copy(space));
406 /* Align the parameters of all nodes in schedule "schedule"
407 * to those of "space".
409 __isl_give isl_schedule *isl_schedule_align_params(
410 __isl_take isl_schedule *schedule, __isl_take isl_space *space)
412 schedule = isl_schedule_map_schedule_node(schedule,
413 &align_params, space);
414 isl_space_free(space);
415 return schedule;
418 /* Wrapper around isl_schedule_node_pullback_union_pw_multi_aff for use as
419 * an isl_schedule_map_schedule_node callback.
421 static __isl_give isl_schedule_node *pullback_upma(
422 __isl_take isl_schedule_node *node, void *user)
424 isl_union_pw_multi_aff *upma = user;
426 return isl_schedule_node_pullback_union_pw_multi_aff(node,
427 isl_union_pw_multi_aff_copy(upma));
430 /* Compute the pullback of "schedule" by the function represented by "upma".
431 * In other words, plug in "upma" in the iteration domains of "schedule".
433 __isl_give isl_schedule *isl_schedule_pullback_union_pw_multi_aff(
434 __isl_take isl_schedule *schedule,
435 __isl_take isl_union_pw_multi_aff *upma)
437 schedule = isl_schedule_map_schedule_node(schedule,
438 &pullback_upma, upma);
439 isl_union_pw_multi_aff_free(upma);
440 return schedule;
443 /* Return an isl_union_map representation of the schedule.
444 * If we still have access to the schedule tree, then we return
445 * an isl_union_map corresponding to the subtree schedule of the child
446 * of the root domain node. That is, we do not intersect the domain
447 * of the returned isl_union_map with the domain constraints.
448 * Otherwise, we must have removed it because we created a band forest.
449 * If so, we extract the isl_union_map from the forest.
450 * This reconstructed schedule map
451 * then needs to be padded with zeros to unify the schedule space
452 * since the result of isl_band_list_get_suffix_schedule may not have
453 * a unified schedule space.
455 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
457 enum isl_schedule_node_type type;
458 isl_schedule_node *node;
459 isl_union_map *umap;
461 if (!sched)
462 return NULL;
464 if (sched->root) {
465 type = isl_schedule_tree_get_type(sched->root);
466 if (type != isl_schedule_node_domain)
467 isl_die(isl_schedule_get_ctx(sched), isl_error_internal,
468 "root node not a domain node", return NULL);
470 node = isl_schedule_get_root(sched);
471 node = isl_schedule_node_child(node, 0);
472 umap = isl_schedule_node_get_subtree_schedule_union_map(node);
473 isl_schedule_node_free(node);
475 return umap;
478 umap = isl_band_list_get_suffix_schedule(sched->band_forest);
479 return pad_schedule_map(umap);
482 static __isl_give isl_band_list *construct_band_list(
483 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
484 __isl_keep isl_band *parent);
486 /* Construct an isl_band structure from the given schedule tree node,
487 * which may be either a band node or a leaf node.
488 * In the latter case, construct a zero-dimensional band.
489 * "domain" is the universe set of the domain elements that reach "node".
490 * "parent" is the parent isl_band of the isl_band constructed
491 * by this function.
493 * In case of a band node, we copy the properties (except tilability,
494 * which is implicit in an isl_band) to the isl_band.
495 * We assume that the band node is not zero-dimensional.
496 * If the child of the band node is not a leaf node,
497 * then we extract the children of the isl_band from this child.
499 static __isl_give isl_band *construct_band(__isl_take isl_schedule_node *node,
500 __isl_take isl_union_set *domain, __isl_keep isl_band *parent)
502 int i;
503 isl_ctx *ctx;
504 isl_band *band = NULL;
505 isl_multi_union_pw_aff *mupa;
507 if (!node || !domain)
508 goto error;
510 ctx = isl_schedule_node_get_ctx(node);
511 band = isl_band_alloc(ctx);
512 if (!band)
513 goto error;
515 band->schedule = node->schedule;
516 band->parent = parent;
518 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
519 band->n = 0;
520 band->pma = isl_union_pw_multi_aff_from_domain(domain);
521 isl_schedule_node_free(node);
522 return band;
525 band->n = isl_schedule_node_band_n_member(node);
526 if (band->n == 0)
527 isl_die(ctx, isl_error_unsupported,
528 "zero-dimensional band nodes not supported",
529 goto error);
530 band->coincident = isl_alloc_array(ctx, int, band->n);
531 if (band->n && !band->coincident)
532 goto error;
533 for (i = 0; i < band->n; ++i)
534 band->coincident[i] =
535 isl_schedule_node_band_member_get_coincident(node, i);
536 mupa = isl_schedule_node_band_get_partial_schedule(node);
537 band->pma = isl_union_pw_multi_aff_from_multi_union_pw_aff(mupa);
538 if (!band->pma)
539 goto error;
541 node = isl_schedule_node_child(node, 0);
542 if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
543 isl_schedule_node_free(node);
544 isl_union_set_free(domain);
545 return band;
548 band->children = construct_band_list(node, domain, band);
549 if (!band->children)
550 return isl_band_free(band);
552 return band;
553 error:
554 isl_union_set_free(domain);
555 isl_schedule_node_free(node);
556 isl_band_free(band);
557 return NULL;
560 /* Construct a list of isl_band structures from the children of "node".
561 * "node" itself is a sequence or set node, so that each of the child nodes
562 * is a filter node and the list returned by node_construct_band_list
563 * consists of a single element.
564 * "domain" is the universe set of the domain elements that reach "node".
565 * "parent" is the parent isl_band of the isl_band structures constructed
566 * by this function.
568 static __isl_give isl_band_list *construct_band_list_from_children(
569 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
570 __isl_keep isl_band *parent)
572 int i, n;
573 isl_ctx *ctx;
574 isl_band_list *list;
576 n = isl_schedule_node_n_children(node);
578 ctx = isl_schedule_node_get_ctx(node);
579 list = isl_band_list_alloc(ctx, 0);
580 for (i = 0; i < n; ++i) {
581 isl_schedule_node *child;
582 isl_band_list *list_i;
584 child = isl_schedule_node_get_child(node, i);
585 list_i = construct_band_list(child, isl_union_set_copy(domain),
586 parent);
587 list = isl_band_list_concat(list, list_i);
590 isl_union_set_free(domain);
591 isl_schedule_node_free(node);
593 return list;
596 /* Construct an isl_band structure from the given sequence node
597 * (or set node that is treated as a sequence node).
598 * A single-dimensional band is created with as schedule for each of
599 * filters of the children, the corresponding child position.
600 * "domain" is the universe set of the domain elements that reach "node".
601 * "parent" is the parent isl_band of the isl_band constructed
602 * by this function.
604 static __isl_give isl_band_list *construct_band_list_sequence(
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 *band = NULL;
611 isl_space *space;
612 isl_union_pw_multi_aff *upma;
614 if (!node || !domain)
615 goto error;
617 ctx = isl_schedule_node_get_ctx(node);
618 band = isl_band_alloc(ctx);
619 if (!band)
620 goto error;
622 band->schedule = node->schedule;
623 band->parent = parent;
624 band->n = 1;
625 band->coincident = isl_calloc_array(ctx, int, band->n);
626 if (!band->coincident)
627 goto error;
629 n = isl_schedule_node_n_children(node);
630 space = isl_union_set_get_space(domain);
631 upma = isl_union_pw_multi_aff_empty(isl_space_copy(space));
633 space = isl_space_set_from_params(space);
634 space = isl_space_add_dims(space, isl_dim_set, 1);
636 for (i = 0; i < n; ++i) {
637 isl_schedule_node *child;
638 isl_union_set *filter;
639 isl_val *v;
640 isl_val_list *vl;
641 isl_multi_val *mv;
642 isl_union_pw_multi_aff *upma_i;
644 child = isl_schedule_node_get_child(node, i);
645 filter = isl_schedule_node_filter_get_filter(child);
646 isl_schedule_node_free(child);
647 filter = isl_union_set_intersect(filter,
648 isl_union_set_copy(domain));
649 v = isl_val_int_from_si(ctx, i);
650 vl = isl_val_list_from_val(v);
651 mv = isl_multi_val_from_val_list(isl_space_copy(space), vl);
652 upma_i = isl_union_pw_multi_aff_multi_val_on_domain(filter, mv);
653 upma = isl_union_pw_multi_aff_union_add(upma, upma_i);
656 isl_space_free(space);
658 band->pma = upma;
659 if (!band->pma)
660 goto error;
662 band->children = construct_band_list_from_children(node, domain, band);
663 if (!band->children)
664 band = isl_band_free(band);
665 return isl_band_list_from_band(band);
666 error:
667 isl_union_set_free(domain);
668 isl_schedule_node_free(node);
669 isl_band_free(band);
670 return NULL;
673 /* Construct a list of isl_band structures from "node" depending
674 * on the type of "node".
675 * "domain" is the universe set of the domain elements that reach "node".
676 * "parent" is the parent isl_band of the isl_band structures constructed
677 * by this function.
679 * If schedule_separate_components is set then set nodes are treated
680 * as sequence nodes. Otherwise, we directly extract an (implicitly
681 * parallel) list of isl_band structures.
683 * If "node" is a filter, then "domain" is updated by the filter.
685 static __isl_give isl_band_list *construct_band_list(
686 __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
687 __isl_keep isl_band *parent)
689 enum isl_schedule_node_type type;
690 isl_ctx *ctx;
691 isl_band *band;
692 isl_band_list *list;
693 isl_union_set *filter;
695 if (!node || !domain)
696 goto error;
698 type = isl_schedule_node_get_type(node);
699 switch (type) {
700 case isl_schedule_node_error:
701 goto error;
702 case isl_schedule_node_domain:
703 isl_die(isl_schedule_node_get_ctx(node), isl_error_invalid,
704 "internal domain nodes not allowed", goto error);
705 case isl_schedule_node_filter:
706 filter = isl_schedule_node_filter_get_filter(node);
707 domain = isl_union_set_intersect(domain, filter);
708 node = isl_schedule_node_child(node, 0);
709 return construct_band_list(node, domain, parent);
710 case isl_schedule_node_set:
711 ctx = isl_schedule_node_get_ctx(node);
712 if (isl_options_get_schedule_separate_components(ctx))
713 return construct_band_list_sequence(node, domain,
714 parent);
715 else
716 return construct_band_list_from_children(node, domain,
717 parent);
718 case isl_schedule_node_sequence:
719 return construct_band_list_sequence(node, domain, parent);
720 case isl_schedule_node_leaf:
721 case isl_schedule_node_band:
722 band = construct_band(node, domain, parent);
723 list = isl_band_list_from_band(band);
724 break;
727 return list;
728 error:
729 isl_union_set_free(domain);
730 isl_schedule_node_free(node);
731 return NULL;
734 /* Return the roots of a band forest representation of the schedule.
735 * The band forest is constructed from the schedule tree,
736 * but once such a band forest is
737 * constructed, we forget about the original schedule tree since
738 * the user may modify the schedule through the band forest.
740 __isl_give isl_band_list *isl_schedule_get_band_forest(
741 __isl_keep isl_schedule *schedule)
743 isl_schedule_node *node;
744 isl_union_set *domain;
746 if (!schedule)
747 return NULL;
748 if (schedule->root) {
749 node = isl_schedule_get_root(schedule);
750 domain = isl_schedule_node_domain_get_domain(node);
751 domain = isl_union_set_universe(domain);
752 node = isl_schedule_node_child(node, 0);
754 schedule->band_forest = construct_band_list(node, domain, NULL);
755 schedule->root = isl_schedule_tree_free(schedule->root);
757 return isl_band_list_dup(schedule->band_forest);
760 /* Call "fn" on each band in the schedule in depth-first post-order.
762 int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
763 int (*fn)(__isl_keep isl_band *band, void *user), void *user)
765 int r;
766 isl_band_list *forest;
768 if (!sched)
769 return -1;
771 forest = isl_schedule_get_band_forest(sched);
772 r = isl_band_list_foreach_band(forest, fn, user);
773 isl_band_list_free(forest);
775 return r;
778 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
779 __isl_keep isl_band_list *list);
781 static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
782 __isl_keep isl_band *band)
784 isl_band_list *children;
786 p = isl_printer_start_line(p);
787 p = isl_printer_print_union_pw_multi_aff(p, band->pma);
788 p = isl_printer_end_line(p);
790 if (!isl_band_has_children(band))
791 return p;
793 children = isl_band_get_children(band);
795 p = isl_printer_indent(p, 4);
796 p = print_band_list(p, children);
797 p = isl_printer_indent(p, -4);
799 isl_band_list_free(children);
801 return p;
804 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
805 __isl_keep isl_band_list *list)
807 int i, n;
809 n = isl_band_list_n_band(list);
810 for (i = 0; i < n; ++i) {
811 isl_band *band;
812 band = isl_band_list_get_band(list, i);
813 p = print_band(p, band);
814 isl_band_free(band);
817 return p;
820 /* Insert a band node with partial schedule "partial" between the domain
821 * root node of "schedule" and its single child.
822 * Return a pointer to the updated schedule.
824 __isl_give isl_schedule *isl_schedule_insert_partial_schedule(
825 __isl_take isl_schedule *schedule,
826 __isl_take isl_multi_union_pw_aff *partial)
828 isl_schedule_node *node;
830 node = isl_schedule_get_root(schedule);
831 isl_schedule_free(schedule);
832 if (!node)
833 goto error;
834 if (isl_schedule_node_get_type(node) != isl_schedule_node_domain)
835 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
836 "root node not a domain node", goto error);
838 node = isl_schedule_node_child(node, 0);
839 node = isl_schedule_node_insert_partial_schedule(node, partial);
841 schedule = isl_schedule_node_get_schedule(node);
842 isl_schedule_node_free(node);
844 return schedule;
845 error:
846 isl_schedule_node_free(node);
847 isl_multi_union_pw_aff_free(partial);
848 return NULL;
851 /* Return a tree with as top-level node a filter corresponding to "filter" and
852 * as child, the (single) child of "tree".
853 * However, if this single child is of type "type", then the filter is inserted
854 * in the children of this single child instead.
856 static __isl_give isl_schedule_tree *insert_filter_in_child_of_type(
857 __isl_take isl_schedule_tree *tree, __isl_take isl_union_set *filter,
858 enum isl_schedule_node_type type)
860 if (!isl_schedule_tree_has_children(tree)) {
861 isl_schedule_tree_free(tree);
862 return isl_schedule_tree_from_filter(filter);
863 } else {
864 tree = isl_schedule_tree_child(tree, 0);
867 if (isl_schedule_tree_get_type(tree) == type)
868 tree = isl_schedule_tree_children_insert_filter(tree, filter);
869 else
870 tree = isl_schedule_tree_insert_filter(tree, filter);
872 return tree;
875 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
876 * with a top-level node (underneath the domain node) of type "type",
877 * either isl_schedule_node_sequence or isl_schedule_node_set.
878 * The domains of the two schedules are assumed to be disjoint.
880 * The new schedule has as domain the union of the domains of the two
881 * schedules. The child of the domain node is a node of type "type"
882 * with two filters corresponding to the domains of the input schedules.
883 * If one (or both) of the top-level nodes of the two schedules is itself
884 * of type "type", then the filter is pushed into the children of that
885 * node and the sequence of set is flattened.
887 __isl_give isl_schedule *isl_schedule_pair(enum isl_schedule_node_type type,
888 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
890 int disjoint;
891 isl_ctx *ctx;
892 enum isl_schedule_node_type root_type;
893 isl_schedule_tree *tree1, *tree2;
894 isl_union_set *filter1, *filter2, *domain;
896 if (!schedule1 || !schedule2)
897 goto error;
899 root_type = isl_schedule_tree_get_type(schedule1->root);
900 if (root_type != isl_schedule_node_domain)
901 isl_die(isl_schedule_get_ctx(schedule1), isl_error_internal,
902 "root node not a domain node", goto error);
903 root_type = isl_schedule_tree_get_type(schedule2->root);
904 if (root_type != isl_schedule_node_domain)
905 isl_die(isl_schedule_get_ctx(schedule1), isl_error_internal,
906 "root node not a domain node", goto error);
908 ctx = isl_schedule_get_ctx(schedule1);
909 tree1 = isl_schedule_tree_copy(schedule1->root);
910 filter1 = isl_schedule_tree_domain_get_domain(tree1);
911 tree2 = isl_schedule_tree_copy(schedule2->root);
912 filter2 = isl_schedule_tree_domain_get_domain(tree2);
914 isl_schedule_free(schedule1);
915 isl_schedule_free(schedule2);
917 disjoint = isl_union_set_is_disjoint(filter1, filter2);
918 if (disjoint < 0)
919 filter1 = isl_union_set_free(filter1);
920 if (!disjoint)
921 isl_die(ctx, isl_error_invalid,
922 "schedule domains not disjoint",
923 filter1 = isl_union_set_free(filter1));
925 domain = isl_union_set_union(isl_union_set_copy(filter1),
926 isl_union_set_copy(filter2));
927 filter1 = isl_union_set_gist(filter1, isl_union_set_copy(domain));
928 filter2 = isl_union_set_gist(filter2, isl_union_set_copy(domain));
930 tree1 = insert_filter_in_child_of_type(tree1, filter1, type);
931 tree2 = insert_filter_in_child_of_type(tree2, filter2, type);
933 tree1 = isl_schedule_tree_from_pair(type, tree1, tree2);
934 tree1 = isl_schedule_tree_insert_domain(tree1, domain);
936 return isl_schedule_from_schedule_tree(ctx, tree1);
937 error:
938 isl_schedule_free(schedule1);
939 isl_schedule_free(schedule2);
940 return NULL;
943 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
944 * through a sequence node.
945 * The domains of the input schedules are assumed to be disjoint.
947 __isl_give isl_schedule *isl_schedule_sequence(
948 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
950 return isl_schedule_pair(isl_schedule_node_sequence,
951 schedule1, schedule2);
954 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
955 * through a set node.
956 * The domains of the input schedules are assumed to be disjoint.
958 __isl_give isl_schedule *isl_schedule_set(
959 __isl_take isl_schedule *schedule1, __isl_take isl_schedule *schedule2)
961 return isl_schedule_pair(isl_schedule_node_set, schedule1, schedule2);
964 /* Print "schedule" to "p".
966 * If "schedule" was created from a schedule tree, then we print
967 * the schedule tree representation. Otherwise, we print
968 * the band forest representation.
970 __isl_give isl_printer *isl_printer_print_schedule(__isl_take isl_printer *p,
971 __isl_keep isl_schedule *schedule)
973 isl_band_list *forest;
975 if (!schedule)
976 return isl_printer_free(p);
978 if (schedule->root)
979 return isl_printer_print_schedule_tree(p, schedule->root);
981 forest = isl_schedule_get_band_forest(schedule);
983 p = print_band_list(p, forest);
985 isl_band_list_free(forest);
987 return p;
990 void isl_schedule_dump(__isl_keep isl_schedule *schedule)
992 isl_printer *printer;
994 if (!schedule)
995 return;
997 printer = isl_printer_to_file(isl_schedule_get_ctx(schedule), stderr);
998 printer = isl_printer_set_yaml_style(printer, ISL_YAML_STYLE_BLOCK);
999 printer = isl_printer_print_schedule(printer, schedule);
1001 isl_printer_free(printer);