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,
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl_aff_private.h>
17 #include <isl/schedule.h>
18 #include <isl/schedule_node.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
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
;
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",
51 schedule
= isl_calloc_type(ctx
, isl_schedule
);
55 schedule
->leaf
.ctx
= ctx
;
58 schedule
->root
= tree
;
59 schedule
->leaf
.ref
= -1;
60 schedule
->leaf
.type
= isl_schedule_node_leaf
;
64 isl_schedule_tree_free(tree
);
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
)
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
)
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
)
110 isl_schedule_tree
*tree
;
114 if (schedule
->ref
== 1)
117 ctx
= isl_schedule_get_ctx(schedule
);
119 isl_die(ctx
, isl_error_internal
,
120 "only for schedule tree based schedules",
121 return isl_schedule_free(schedule
));
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
)
132 if (--sched
->ref
> 0)
135 isl_band_list_free(sched
->band_forest
);
136 isl_schedule_tree_free(sched
->root
);
137 isl_ctx_deref(sched
->leaf
.ctx
);
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
)
149 if (schedule
->root
== tree
) {
150 isl_schedule_tree_free(tree
);
154 schedule
= isl_schedule_cow(schedule
);
157 isl_schedule_tree_free(schedule
->root
);
158 schedule
->root
= tree
;
162 isl_schedule_free(schedule
);
163 isl_schedule_tree_free(tree
);
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 int isl_schedule_plain_is_equal(__isl_keep isl_schedule
*schedule1
,
186 __isl_keep isl_schedule
*schedule2
)
188 if (!schedule1
|| !schedule2
)
190 if (schedule1
== schedule2
)
192 return isl_schedule_tree_plain_is_equal(schedule1
->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
;
204 isl_union_set
*domain
;
209 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
210 "schedule tree representation not available",
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
);
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
)
230 isl_schedule_tree
*tree
;
231 isl_schedule_tree_list
*ancestors
;
237 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
238 "schedule tree representation not available",
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
251 static int update_max_out(__isl_take isl_map
*map
, void *user
)
254 int n_out
= isl_map_dim(map
, isl_dim_out
);
256 if (n_out
> *max_out
)
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
{
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 int map_pad_range(__isl_take isl_map
*map
, void *user
)
278 struct isl_pad_schedule_map_data
*data
= user
;
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
);
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
;
303 if (isl_union_map_n_map(umap
) <= 1)
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
);
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
)
326 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
327 "schedule tree representation not available",
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 int isl_schedule_foreach_schedule_node(__isl_keep isl_schedule
*sched
,
341 int (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
), void *user
)
343 isl_schedule_node
*node
;
349 node
= isl_schedule_get_root(sched
);
350 r
= isl_schedule_node_foreach_descendant(node
, fn
, user
);
351 isl_schedule_node_free(node
);
356 /* Traverse the node of "sched" in depth first postorder,
357 * allowing the user to modify the visited node.
358 * The traversal continues from the node returned by the callback function.
359 * It is the responsibility of the user to ensure that this does not
360 * lead to an infinite loop. It is safest to always return a pointer
361 * to the same position (same ancestors and child positions) as the input node.
363 __isl_give isl_schedule
*isl_schedule_map_schedule_node(
364 __isl_take isl_schedule
*schedule
,
365 __isl_give isl_schedule_node
*(*fn
)(
366 __isl_take isl_schedule_node
*node
, void *user
), void *user
)
368 isl_schedule_node
*node
;
370 node
= isl_schedule_get_root(schedule
);
371 isl_schedule_free(schedule
);
373 node
= isl_schedule_node_map_descendant(node
, fn
, user
);
374 schedule
= isl_schedule_node_get_schedule(node
);
375 isl_schedule_node_free(node
);
380 /* Wrapper around isl_schedule_node_reset_user for use as
381 * an isl_schedule_map_schedule_node callback.
383 static __isl_give isl_schedule_node
*reset_user(
384 __isl_take isl_schedule_node
*node
, void *user
)
386 return isl_schedule_node_reset_user(node
);
389 /* Reset the user pointer on all identifiers of parameters and tuples
390 * in the schedule "schedule".
392 __isl_give isl_schedule
*isl_schedule_reset_user(
393 __isl_take isl_schedule
*schedule
)
395 return isl_schedule_map_schedule_node(schedule
, &reset_user
, NULL
);
398 /* Wrapper around isl_schedule_node_align_params for use as
399 * an isl_schedule_map_schedule_node 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(schedule
,
416 &align_params
, space
);
417 isl_space_free(space
);
421 /* Wrapper around isl_schedule_node_pullback_union_pw_multi_aff for use as
422 * an isl_schedule_map_schedule_node 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(schedule
,
443 &pullback_upma
, upma
);
444 isl_union_pw_multi_aff_free(upma
);
448 /* Intersect the domain of the schedule "schedule" with "domain".
449 * The root of "schedule" is required to be a domain node.
451 __isl_give isl_schedule
*isl_schedule_intersect_domain(
452 __isl_take isl_schedule
*schedule
, __isl_take isl_union_set
*domain
)
454 enum isl_schedule_node_type root_type
;
455 isl_schedule_node
*node
;
457 if (!schedule
|| !domain
)
460 root_type
= isl_schedule_tree_get_type(schedule
->root
);
461 if (root_type
!= isl_schedule_node_domain
)
462 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
463 "root node must be a domain node", goto error
);
465 node
= isl_schedule_get_root(schedule
);
466 isl_schedule_free(schedule
);
467 node
= isl_schedule_node_domain_intersect_domain(node
, domain
);
468 schedule
= isl_schedule_node_get_schedule(node
);
469 isl_schedule_node_free(node
);
473 isl_schedule_free(schedule
);
474 isl_union_set_free(domain
);
478 /* Return an isl_union_map representation of the schedule.
479 * If we still have access to the schedule tree, then we return
480 * an isl_union_map corresponding to the subtree schedule of the child
481 * of the root domain node. That is, we do not intersect the domain
482 * of the returned isl_union_map with the domain constraints.
483 * Otherwise, we must have removed it because we created a band forest.
484 * If so, we extract the isl_union_map from the forest.
485 * This reconstructed schedule map
486 * then needs to be padded with zeros to unify the schedule space
487 * since the result of isl_band_list_get_suffix_schedule may not have
488 * a unified schedule space.
490 __isl_give isl_union_map
*isl_schedule_get_map(__isl_keep isl_schedule
*sched
)
492 enum isl_schedule_node_type type
;
493 isl_schedule_node
*node
;
500 type
= isl_schedule_tree_get_type(sched
->root
);
501 if (type
!= isl_schedule_node_domain
)
502 isl_die(isl_schedule_get_ctx(sched
), isl_error_internal
,
503 "root node not a domain node", return NULL
);
505 node
= isl_schedule_get_root(sched
);
506 node
= isl_schedule_node_child(node
, 0);
507 umap
= isl_schedule_node_get_subtree_schedule_union_map(node
);
508 isl_schedule_node_free(node
);
513 umap
= isl_band_list_get_suffix_schedule(sched
->band_forest
);
514 return pad_schedule_map(umap
);
517 static __isl_give isl_band_list
*construct_band_list(
518 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
519 __isl_keep isl_band
*parent
);
521 /* Construct an isl_band structure from the given schedule tree node,
522 * which may be either a band node or a leaf node.
523 * In the latter case, construct a zero-dimensional band.
524 * "domain" is the universe set of the domain elements that reach "node".
525 * "parent" is the parent isl_band of the isl_band constructed
528 * In case of a band node, we copy the properties (except tilability,
529 * which is implicit in an isl_band) to the isl_band.
530 * We assume that the band node is not zero-dimensional.
531 * If the child of the band node is not a leaf node,
532 * then we extract the children of the isl_band from this child.
534 static __isl_give isl_band
*construct_band(__isl_take isl_schedule_node
*node
,
535 __isl_take isl_union_set
*domain
, __isl_keep isl_band
*parent
)
539 isl_band
*band
= NULL
;
540 isl_multi_union_pw_aff
*mupa
;
542 if (!node
|| !domain
)
545 ctx
= isl_schedule_node_get_ctx(node
);
546 band
= isl_band_alloc(ctx
);
550 band
->schedule
= node
->schedule
;
551 band
->parent
= parent
;
553 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
555 band
->pma
= isl_union_pw_multi_aff_from_domain(domain
);
556 isl_schedule_node_free(node
);
560 band
->n
= isl_schedule_node_band_n_member(node
);
562 isl_die(ctx
, isl_error_unsupported
,
563 "zero-dimensional band nodes not supported",
565 band
->coincident
= isl_alloc_array(ctx
, int, band
->n
);
566 if (band
->n
&& !band
->coincident
)
568 for (i
= 0; i
< band
->n
; ++i
)
569 band
->coincident
[i
] =
570 isl_schedule_node_band_member_get_coincident(node
, i
);
571 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
572 band
->pma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(mupa
);
576 node
= isl_schedule_node_child(node
, 0);
577 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
578 isl_schedule_node_free(node
);
579 isl_union_set_free(domain
);
583 band
->children
= construct_band_list(node
, domain
, band
);
585 return isl_band_free(band
);
589 isl_union_set_free(domain
);
590 isl_schedule_node_free(node
);
595 /* Construct a list of isl_band structures from the children of "node".
596 * "node" itself is a sequence or set node, so that each of the child nodes
597 * is a filter node and the list returned by node_construct_band_list
598 * consists of a single element.
599 * "domain" is the universe set of the domain elements that reach "node".
600 * "parent" is the parent isl_band of the isl_band structures constructed
603 static __isl_give isl_band_list
*construct_band_list_from_children(
604 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
605 __isl_keep isl_band
*parent
)
611 n
= isl_schedule_node_n_children(node
);
613 ctx
= isl_schedule_node_get_ctx(node
);
614 list
= isl_band_list_alloc(ctx
, 0);
615 for (i
= 0; i
< n
; ++i
) {
616 isl_schedule_node
*child
;
617 isl_band_list
*list_i
;
619 child
= isl_schedule_node_get_child(node
, i
);
620 list_i
= construct_band_list(child
, isl_union_set_copy(domain
),
622 list
= isl_band_list_concat(list
, list_i
);
625 isl_union_set_free(domain
);
626 isl_schedule_node_free(node
);
631 /* Construct an isl_band structure from the given sequence node
632 * (or set node that is treated as a sequence node).
633 * A single-dimensional band is created with as schedule for each of
634 * filters of the children, the corresponding child position.
635 * "domain" is the universe set of the domain elements that reach "node".
636 * "parent" is the parent isl_band of the isl_band constructed
639 static __isl_give isl_band_list
*construct_band_list_sequence(
640 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
641 __isl_keep isl_band
*parent
)
645 isl_band
*band
= NULL
;
647 isl_union_pw_multi_aff
*upma
;
649 if (!node
|| !domain
)
652 ctx
= isl_schedule_node_get_ctx(node
);
653 band
= isl_band_alloc(ctx
);
657 band
->schedule
= node
->schedule
;
658 band
->parent
= parent
;
660 band
->coincident
= isl_calloc_array(ctx
, int, band
->n
);
661 if (!band
->coincident
)
664 n
= isl_schedule_node_n_children(node
);
665 space
= isl_union_set_get_space(domain
);
666 upma
= isl_union_pw_multi_aff_empty(isl_space_copy(space
));
668 space
= isl_space_set_from_params(space
);
669 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
671 for (i
= 0; i
< n
; ++i
) {
672 isl_schedule_node
*child
;
673 isl_union_set
*filter
;
677 isl_union_pw_multi_aff
*upma_i
;
679 child
= isl_schedule_node_get_child(node
, i
);
680 filter
= isl_schedule_node_filter_get_filter(child
);
681 isl_schedule_node_free(child
);
682 filter
= isl_union_set_intersect(filter
,
683 isl_union_set_copy(domain
));
684 v
= isl_val_int_from_si(ctx
, i
);
685 vl
= isl_val_list_from_val(v
);
686 mv
= isl_multi_val_from_val_list(isl_space_copy(space
), vl
);
687 upma_i
= isl_union_pw_multi_aff_multi_val_on_domain(filter
, mv
);
688 upma
= isl_union_pw_multi_aff_union_add(upma
, upma_i
);
691 isl_space_free(space
);
697 band
->children
= construct_band_list_from_children(node
, domain
, band
);
699 band
= isl_band_free(band
);
700 return isl_band_list_from_band(band
);
702 isl_union_set_free(domain
);
703 isl_schedule_node_free(node
);
708 /* Construct a list of isl_band structures from "node" depending
709 * on the type of "node".
710 * "domain" is the universe set of the domain elements that reach "node".
711 * "parent" is the parent isl_band of the isl_band structures constructed
714 * If schedule_separate_components is set then set nodes are treated
715 * as sequence nodes. Otherwise, we directly extract an (implicitly
716 * parallel) list of isl_band structures.
718 * If "node" is a filter, then "domain" is updated by the filter.
720 static __isl_give isl_band_list
*construct_band_list(
721 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
722 __isl_keep isl_band
*parent
)
724 enum isl_schedule_node_type type
;
728 isl_union_set
*filter
;
730 if (!node
|| !domain
)
733 type
= isl_schedule_node_get_type(node
);
735 case isl_schedule_node_error
:
737 case isl_schedule_node_context
:
738 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
739 "context nodes not supported", goto error
);
740 case isl_schedule_node_domain
:
741 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
742 "internal domain nodes not allowed", goto error
);
743 case isl_schedule_node_expansion
:
744 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
745 "expansion nodes not supported", goto error
);
746 case isl_schedule_node_extension
:
747 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
748 "extension nodes not supported", goto error
);
749 case isl_schedule_node_filter
:
750 filter
= isl_schedule_node_filter_get_filter(node
);
751 domain
= isl_union_set_intersect(domain
, filter
);
752 node
= isl_schedule_node_child(node
, 0);
753 return construct_band_list(node
, domain
, parent
);
754 case isl_schedule_node_guard
:
755 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
756 "guard nodes not supported", goto error
);
757 case isl_schedule_node_mark
:
758 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
759 "mark nodes not supported", goto error
);
760 case isl_schedule_node_set
:
761 ctx
= isl_schedule_node_get_ctx(node
);
762 if (isl_options_get_schedule_separate_components(ctx
))
763 return construct_band_list_sequence(node
, domain
,
766 return construct_band_list_from_children(node
, domain
,
768 case isl_schedule_node_sequence
:
769 return construct_band_list_sequence(node
, domain
, parent
);
770 case isl_schedule_node_leaf
:
771 case isl_schedule_node_band
:
772 band
= construct_band(node
, domain
, parent
);
773 list
= isl_band_list_from_band(band
);
779 isl_union_set_free(domain
);
780 isl_schedule_node_free(node
);
784 /* Return the roots of a band forest representation of the schedule.
785 * The band forest is constructed from the schedule tree,
786 * but once such a band forest is
787 * constructed, we forget about the original schedule tree since
788 * the user may modify the schedule through the band forest.
790 __isl_give isl_band_list
*isl_schedule_get_band_forest(
791 __isl_keep isl_schedule
*schedule
)
793 isl_schedule_node
*node
;
794 isl_union_set
*domain
;
798 if (schedule
->root
) {
799 node
= isl_schedule_get_root(schedule
);
800 domain
= isl_schedule_node_domain_get_domain(node
);
801 domain
= isl_union_set_universe(domain
);
802 node
= isl_schedule_node_child(node
, 0);
804 schedule
->band_forest
= construct_band_list(node
, domain
, NULL
);
805 schedule
->root
= isl_schedule_tree_free(schedule
->root
);
807 return isl_band_list_dup(schedule
->band_forest
);
810 /* Call "fn" on each band in the schedule in depth-first post-order.
812 int isl_schedule_foreach_band(__isl_keep isl_schedule
*sched
,
813 int (*fn
)(__isl_keep isl_band
*band
, void *user
), void *user
)
816 isl_band_list
*forest
;
821 forest
= isl_schedule_get_band_forest(sched
);
822 r
= isl_band_list_foreach_band(forest
, fn
, user
);
823 isl_band_list_free(forest
);
828 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
829 __isl_keep isl_band_list
*list
);
831 static __isl_give isl_printer
*print_band(__isl_take isl_printer
*p
,
832 __isl_keep isl_band
*band
)
834 isl_band_list
*children
;
836 p
= isl_printer_start_line(p
);
837 p
= isl_printer_print_union_pw_multi_aff(p
, band
->pma
);
838 p
= isl_printer_end_line(p
);
840 if (!isl_band_has_children(band
))
843 children
= isl_band_get_children(band
);
845 p
= isl_printer_indent(p
, 4);
846 p
= print_band_list(p
, children
);
847 p
= isl_printer_indent(p
, -4);
849 isl_band_list_free(children
);
854 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
855 __isl_keep isl_band_list
*list
)
859 n
= isl_band_list_n_band(list
);
860 for (i
= 0; i
< n
; ++i
) {
862 band
= isl_band_list_get_band(list
, i
);
863 p
= print_band(p
, band
);
870 /* Insert a band node with partial schedule "partial" between the domain
871 * root node of "schedule" and its single child.
872 * Return a pointer to the updated schedule.
874 * If any of the nodes in the tree depend on the set of outer band nodes
875 * then we refuse to insert the band node.
877 __isl_give isl_schedule
*isl_schedule_insert_partial_schedule(
878 __isl_take isl_schedule
*schedule
,
879 __isl_take isl_multi_union_pw_aff
*partial
)
881 isl_schedule_node
*node
;
884 node
= isl_schedule_get_root(schedule
);
885 isl_schedule_free(schedule
);
888 if (isl_schedule_node_get_type(node
) != isl_schedule_node_domain
)
889 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
890 "root node not a domain node", goto error
);
892 node
= isl_schedule_node_child(node
, 0);
893 anchored
= isl_schedule_node_is_subtree_anchored(node
);
897 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
898 "cannot insert band node in anchored subtree",
900 node
= isl_schedule_node_insert_partial_schedule(node
, partial
);
902 schedule
= isl_schedule_node_get_schedule(node
);
903 isl_schedule_node_free(node
);
907 isl_schedule_node_free(node
);
908 isl_multi_union_pw_aff_free(partial
);
912 /* Insert a context node with constraints "context" between the domain
913 * root node of "schedule" and its single child.
914 * Return a pointer to the updated schedule.
916 __isl_give isl_schedule
*isl_schedule_insert_context(
917 __isl_take isl_schedule
*schedule
, __isl_take isl_set
*context
)
919 isl_schedule_node
*node
;
921 node
= isl_schedule_get_root(schedule
);
922 isl_schedule_free(schedule
);
923 node
= isl_schedule_node_child(node
, 0);
924 node
= isl_schedule_node_insert_context(node
, context
);
925 schedule
= isl_schedule_node_get_schedule(node
);
926 isl_schedule_node_free(node
);
931 /* Insert a guard node with constraints "guard" between the domain
932 * root node of "schedule" and its single child.
933 * Return a pointer to the updated schedule.
935 __isl_give isl_schedule
*isl_schedule_insert_guard(
936 __isl_take isl_schedule
*schedule
, __isl_take isl_set
*guard
)
938 isl_schedule_node
*node
;
940 node
= isl_schedule_get_root(schedule
);
941 isl_schedule_free(schedule
);
942 node
= isl_schedule_node_child(node
, 0);
943 node
= isl_schedule_node_insert_guard(node
, guard
);
944 schedule
= isl_schedule_node_get_schedule(node
);
945 isl_schedule_node_free(node
);
950 /* Return a tree with as top-level node a filter corresponding to "filter" and
951 * as child, the (single) child of "tree".
952 * However, if this single child is of type "type", then the filter is inserted
953 * in the children of this single child instead.
955 static __isl_give isl_schedule_tree
*insert_filter_in_child_of_type(
956 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
,
957 enum isl_schedule_node_type type
)
959 if (!isl_schedule_tree_has_children(tree
)) {
960 isl_schedule_tree_free(tree
);
961 return isl_schedule_tree_from_filter(filter
);
963 tree
= isl_schedule_tree_child(tree
, 0);
966 if (isl_schedule_tree_get_type(tree
) == type
)
967 tree
= isl_schedule_tree_children_insert_filter(tree
, filter
);
969 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
974 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
975 * with a top-level node (underneath the domain node) of type "type",
976 * either isl_schedule_node_sequence or isl_schedule_node_set.
977 * The domains of the two schedules are assumed to be disjoint.
979 * The new schedule has as domain the union of the domains of the two
980 * schedules. The child of the domain node is a node of type "type"
981 * with two filters corresponding to the domains of the input schedules.
982 * If one (or both) of the top-level nodes of the two schedules is itself
983 * of type "type", then the filter is pushed into the children of that
984 * node and the sequence of set is flattened.
986 __isl_give isl_schedule
*isl_schedule_pair(enum isl_schedule_node_type type
,
987 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
991 enum isl_schedule_node_type root_type
;
992 isl_schedule_tree
*tree1
, *tree2
;
993 isl_union_set
*filter1
, *filter2
, *domain
;
995 if (!schedule1
|| !schedule2
)
998 root_type
= isl_schedule_tree_get_type(schedule1
->root
);
999 if (root_type
!= isl_schedule_node_domain
)
1000 isl_die(isl_schedule_get_ctx(schedule1
), isl_error_internal
,
1001 "root node not a domain node", goto error
);
1002 root_type
= isl_schedule_tree_get_type(schedule2
->root
);
1003 if (root_type
!= isl_schedule_node_domain
)
1004 isl_die(isl_schedule_get_ctx(schedule1
), isl_error_internal
,
1005 "root node not a domain node", goto error
);
1007 ctx
= isl_schedule_get_ctx(schedule1
);
1008 tree1
= isl_schedule_tree_copy(schedule1
->root
);
1009 filter1
= isl_schedule_tree_domain_get_domain(tree1
);
1010 tree2
= isl_schedule_tree_copy(schedule2
->root
);
1011 filter2
= isl_schedule_tree_domain_get_domain(tree2
);
1013 isl_schedule_free(schedule1
);
1014 isl_schedule_free(schedule2
);
1016 disjoint
= isl_union_set_is_disjoint(filter1
, filter2
);
1018 filter1
= isl_union_set_free(filter1
);
1020 isl_die(ctx
, isl_error_invalid
,
1021 "schedule domains not disjoint",
1022 filter1
= isl_union_set_free(filter1
));
1024 domain
= isl_union_set_union(isl_union_set_copy(filter1
),
1025 isl_union_set_copy(filter2
));
1026 filter1
= isl_union_set_gist(filter1
, isl_union_set_copy(domain
));
1027 filter2
= isl_union_set_gist(filter2
, isl_union_set_copy(domain
));
1029 tree1
= insert_filter_in_child_of_type(tree1
, filter1
, type
);
1030 tree2
= insert_filter_in_child_of_type(tree2
, filter2
, type
);
1032 tree1
= isl_schedule_tree_from_pair(type
, tree1
, tree2
);
1033 tree1
= isl_schedule_tree_insert_domain(tree1
, domain
);
1035 return isl_schedule_from_schedule_tree(ctx
, tree1
);
1037 isl_schedule_free(schedule1
);
1038 isl_schedule_free(schedule2
);
1042 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1043 * through a sequence node.
1044 * The domains of the input schedules are assumed to be disjoint.
1046 __isl_give isl_schedule
*isl_schedule_sequence(
1047 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
1049 return isl_schedule_pair(isl_schedule_node_sequence
,
1050 schedule1
, schedule2
);
1053 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1054 * through a set node.
1055 * The domains of the input schedules are assumed to be disjoint.
1057 __isl_give isl_schedule
*isl_schedule_set(
1058 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
1060 return isl_schedule_pair(isl_schedule_node_set
, schedule1
, schedule2
);
1063 /* Print "schedule" to "p".
1065 * If "schedule" was created from a schedule tree, then we print
1066 * the schedule tree representation. Otherwise, we print
1067 * the band forest representation.
1069 __isl_give isl_printer
*isl_printer_print_schedule(__isl_take isl_printer
*p
,
1070 __isl_keep isl_schedule
*schedule
)
1072 isl_band_list
*forest
;
1075 return isl_printer_free(p
);
1078 return isl_printer_print_schedule_tree(p
, schedule
->root
);
1080 forest
= isl_schedule_get_band_forest(schedule
);
1082 p
= print_band_list(p
, forest
);
1084 isl_band_list_free(forest
);
1089 void isl_schedule_dump(__isl_keep isl_schedule
*schedule
)
1091 isl_printer
*printer
;
1096 printer
= isl_printer_to_file(isl_schedule_get_ctx(schedule
), stderr
);
1097 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
1098 printer
= isl_printer_print_schedule(printer
, schedule
);
1100 isl_printer_free(printer
);