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 isl_bool
isl_schedule_plain_is_equal(__isl_keep isl_schedule
*schedule1
,
186 __isl_keep isl_schedule
*schedule2
)
188 if (!schedule1
|| !schedule2
)
189 return isl_bool_error
;
190 if (schedule1
== schedule2
)
191 return isl_bool_true
;
192 return isl_schedule_tree_plain_is_equal(schedule1
->root
,
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 isl_stat
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 isl_stat
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
);
288 return isl_stat_error
;
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 isl_stat
isl_schedule_foreach_schedule_node_top_down(
341 __isl_keep isl_schedule
*sched
,
342 isl_bool (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
),
345 isl_schedule_node
*node
;
349 return isl_stat_error
;
351 node
= isl_schedule_get_root(sched
);
352 r
= isl_schedule_node_foreach_descendant_top_down(node
, fn
, user
);
353 isl_schedule_node_free(node
);
358 /* Traverse the node of "sched" in depth first postorder,
359 * allowing the user to modify the visited node.
360 * The traversal continues from the node returned by the callback function.
361 * It is the responsibility of the user to ensure that this does not
362 * lead to an infinite loop. It is safest to always return a pointer
363 * to the same position (same ancestors and child positions) as the input node.
365 __isl_give isl_schedule
*isl_schedule_map_schedule_node_bottom_up(
366 __isl_take isl_schedule
*schedule
,
367 __isl_give isl_schedule_node
*(*fn
)(
368 __isl_take isl_schedule_node
*node
, void *user
), void *user
)
370 isl_schedule_node
*node
;
372 node
= isl_schedule_get_root(schedule
);
373 isl_schedule_free(schedule
);
375 node
= isl_schedule_node_map_descendant_bottom_up(node
, fn
, user
);
376 schedule
= isl_schedule_node_get_schedule(node
);
377 isl_schedule_node_free(node
);
382 /* Wrapper around isl_schedule_node_reset_user for use as
383 * an isl_schedule_map_schedule_node_bottom_up callback.
385 static __isl_give isl_schedule_node
*reset_user(
386 __isl_take isl_schedule_node
*node
, void *user
)
388 return isl_schedule_node_reset_user(node
);
391 /* Reset the user pointer on all identifiers of parameters and tuples
392 * in the schedule "schedule".
394 __isl_give isl_schedule
*isl_schedule_reset_user(
395 __isl_take isl_schedule
*schedule
)
397 return isl_schedule_map_schedule_node_bottom_up(schedule
, &reset_user
,
401 /* Wrapper around isl_schedule_node_align_params for use as
402 * an isl_schedule_map_schedule_node_bottom_up callback.
404 static __isl_give isl_schedule_node
*align_params(
405 __isl_take isl_schedule_node
*node
, void *user
)
407 isl_space
*space
= user
;
409 return isl_schedule_node_align_params(node
, isl_space_copy(space
));
412 /* Align the parameters of all nodes in schedule "schedule"
413 * to those of "space".
415 __isl_give isl_schedule
*isl_schedule_align_params(
416 __isl_take isl_schedule
*schedule
, __isl_take isl_space
*space
)
418 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
419 &align_params
, space
);
420 isl_space_free(space
);
424 /* Wrapper around isl_schedule_node_pullback_union_pw_multi_aff for use as
425 * an isl_schedule_map_schedule_node_bottom_up callback.
427 static __isl_give isl_schedule_node
*pullback_upma(
428 __isl_take isl_schedule_node
*node
, void *user
)
430 isl_union_pw_multi_aff
*upma
= user
;
432 return isl_schedule_node_pullback_union_pw_multi_aff(node
,
433 isl_union_pw_multi_aff_copy(upma
));
436 /* Compute the pullback of "schedule" by the function represented by "upma".
437 * In other words, plug in "upma" in the iteration domains of "schedule".
439 * The schedule tree is not allowed to contain any expansion nodes.
441 __isl_give isl_schedule
*isl_schedule_pullback_union_pw_multi_aff(
442 __isl_take isl_schedule
*schedule
,
443 __isl_take isl_union_pw_multi_aff
*upma
)
445 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
446 &pullback_upma
, upma
);
447 isl_union_pw_multi_aff_free(upma
);
451 /* Intersect the domain of the schedule "schedule" with "domain".
452 * The root of "schedule" is required to be a domain node.
454 __isl_give isl_schedule
*isl_schedule_intersect_domain(
455 __isl_take isl_schedule
*schedule
, __isl_take isl_union_set
*domain
)
457 enum isl_schedule_node_type root_type
;
458 isl_schedule_node
*node
;
460 if (!schedule
|| !domain
)
463 root_type
= isl_schedule_tree_get_type(schedule
->root
);
464 if (root_type
!= isl_schedule_node_domain
)
465 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
466 "root node must be a domain node", goto error
);
468 node
= isl_schedule_get_root(schedule
);
469 isl_schedule_free(schedule
);
470 node
= isl_schedule_node_domain_intersect_domain(node
, domain
);
471 schedule
= isl_schedule_node_get_schedule(node
);
472 isl_schedule_node_free(node
);
476 isl_schedule_free(schedule
);
477 isl_union_set_free(domain
);
481 /* Replace the domain of the schedule "schedule" with the gist
482 * of the original domain with respect to the parameter domain "context".
484 __isl_give isl_schedule
*isl_schedule_gist_domain_params(
485 __isl_take isl_schedule
*schedule
, __isl_take isl_set
*context
)
487 enum isl_schedule_node_type root_type
;
488 isl_schedule_node
*node
;
490 if (!schedule
|| !context
)
493 root_type
= isl_schedule_tree_get_type(schedule
->root
);
494 if (root_type
!= isl_schedule_node_domain
)
495 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
496 "root node must be a domain node", goto error
);
498 node
= isl_schedule_get_root(schedule
);
499 isl_schedule_free(schedule
);
500 node
= isl_schedule_node_domain_gist_params(node
, context
);
501 schedule
= isl_schedule_node_get_schedule(node
);
502 isl_schedule_node_free(node
);
506 isl_schedule_free(schedule
);
507 isl_set_free(context
);
511 /* Return an isl_union_map representation of the schedule.
512 * If we still have access to the schedule tree, then we return
513 * an isl_union_map corresponding to the subtree schedule of the child
514 * of the root domain node. That is, we do not intersect the domain
515 * of the returned isl_union_map with the domain constraints.
516 * Otherwise, we must have removed it because we created a band forest.
517 * If so, we extract the isl_union_map from the forest.
518 * This reconstructed schedule map
519 * then needs to be padded with zeros to unify the schedule space
520 * since the result of isl_band_list_get_suffix_schedule may not have
521 * a unified schedule space.
523 __isl_give isl_union_map
*isl_schedule_get_map(__isl_keep isl_schedule
*sched
)
525 enum isl_schedule_node_type type
;
526 isl_schedule_node
*node
;
533 type
= isl_schedule_tree_get_type(sched
->root
);
534 if (type
!= isl_schedule_node_domain
)
535 isl_die(isl_schedule_get_ctx(sched
), isl_error_internal
,
536 "root node not a domain node", return NULL
);
538 node
= isl_schedule_get_root(sched
);
539 node
= isl_schedule_node_child(node
, 0);
540 umap
= isl_schedule_node_get_subtree_schedule_union_map(node
);
541 isl_schedule_node_free(node
);
546 umap
= isl_band_list_get_suffix_schedule(sched
->band_forest
);
547 return pad_schedule_map(umap
);
550 static __isl_give isl_band_list
*construct_band_list(
551 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
552 __isl_keep isl_band
*parent
);
554 /* Construct an isl_band structure from the given schedule tree node,
555 * which may be either a band node or a leaf node.
556 * In the latter case, construct a zero-dimensional band.
557 * "domain" is the universe set of the domain elements that reach "node".
558 * "parent" is the parent isl_band of the isl_band constructed
561 * In case of a band node, we copy the properties (except tilability,
562 * which is implicit in an isl_band) to the isl_band.
563 * We assume that the band node is not zero-dimensional.
564 * If the child of the band node is not a leaf node,
565 * then we extract the children of the isl_band from this child.
567 static __isl_give isl_band
*construct_band(__isl_take isl_schedule_node
*node
,
568 __isl_take isl_union_set
*domain
, __isl_keep isl_band
*parent
)
572 isl_band
*band
= NULL
;
573 isl_multi_union_pw_aff
*mupa
;
575 if (!node
|| !domain
)
578 ctx
= isl_schedule_node_get_ctx(node
);
579 band
= isl_band_alloc(ctx
);
583 band
->schedule
= node
->schedule
;
584 band
->parent
= parent
;
586 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
588 band
->pma
= isl_union_pw_multi_aff_from_domain(domain
);
589 isl_schedule_node_free(node
);
593 band
->n
= isl_schedule_node_band_n_member(node
);
595 isl_die(ctx
, isl_error_unsupported
,
596 "zero-dimensional band nodes not supported",
598 band
->coincident
= isl_alloc_array(ctx
, int, band
->n
);
599 if (band
->n
&& !band
->coincident
)
601 for (i
= 0; i
< band
->n
; ++i
)
602 band
->coincident
[i
] =
603 isl_schedule_node_band_member_get_coincident(node
, i
);
604 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
605 band
->pma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(mupa
);
609 node
= isl_schedule_node_child(node
, 0);
610 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
611 isl_schedule_node_free(node
);
612 isl_union_set_free(domain
);
616 band
->children
= construct_band_list(node
, domain
, band
);
618 return isl_band_free(band
);
622 isl_union_set_free(domain
);
623 isl_schedule_node_free(node
);
628 /* Construct a list of isl_band structures from the children of "node".
629 * "node" itself is a sequence or set node, so that each of the child nodes
630 * is a filter node and the list returned by node_construct_band_list
631 * consists of a single element.
632 * "domain" is the universe set of the domain elements that reach "node".
633 * "parent" is the parent isl_band of the isl_band structures constructed
636 static __isl_give isl_band_list
*construct_band_list_from_children(
637 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
638 __isl_keep isl_band
*parent
)
644 n
= isl_schedule_node_n_children(node
);
646 ctx
= isl_schedule_node_get_ctx(node
);
647 list
= isl_band_list_alloc(ctx
, 0);
648 for (i
= 0; i
< n
; ++i
) {
649 isl_schedule_node
*child
;
650 isl_band_list
*list_i
;
652 child
= isl_schedule_node_get_child(node
, i
);
653 list_i
= construct_band_list(child
, isl_union_set_copy(domain
),
655 list
= isl_band_list_concat(list
, list_i
);
658 isl_union_set_free(domain
);
659 isl_schedule_node_free(node
);
664 /* Construct an isl_band structure from the given sequence node
665 * (or set node that is treated as a sequence node).
666 * A single-dimensional band is created with as schedule for each of
667 * filters of the children, the corresponding child position.
668 * "domain" is the universe set of the domain elements that reach "node".
669 * "parent" is the parent isl_band of the isl_band constructed
672 static __isl_give isl_band_list
*construct_band_list_sequence(
673 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
674 __isl_keep isl_band
*parent
)
678 isl_band
*band
= NULL
;
680 isl_union_pw_multi_aff
*upma
;
682 if (!node
|| !domain
)
685 ctx
= isl_schedule_node_get_ctx(node
);
686 band
= isl_band_alloc(ctx
);
690 band
->schedule
= node
->schedule
;
691 band
->parent
= parent
;
693 band
->coincident
= isl_calloc_array(ctx
, int, band
->n
);
694 if (!band
->coincident
)
697 n
= isl_schedule_node_n_children(node
);
698 space
= isl_union_set_get_space(domain
);
699 upma
= isl_union_pw_multi_aff_empty(isl_space_copy(space
));
701 space
= isl_space_set_from_params(space
);
702 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
704 for (i
= 0; i
< n
; ++i
) {
705 isl_schedule_node
*child
;
706 isl_union_set
*filter
;
710 isl_union_pw_multi_aff
*upma_i
;
712 child
= isl_schedule_node_get_child(node
, i
);
713 filter
= isl_schedule_node_filter_get_filter(child
);
714 isl_schedule_node_free(child
);
715 filter
= isl_union_set_intersect(filter
,
716 isl_union_set_copy(domain
));
717 v
= isl_val_int_from_si(ctx
, i
);
718 vl
= isl_val_list_from_val(v
);
719 mv
= isl_multi_val_from_val_list(isl_space_copy(space
), vl
);
720 upma_i
= isl_union_pw_multi_aff_multi_val_on_domain(filter
, mv
);
721 upma
= isl_union_pw_multi_aff_union_add(upma
, upma_i
);
724 isl_space_free(space
);
730 band
->children
= construct_band_list_from_children(node
, domain
, band
);
732 band
= isl_band_free(band
);
733 return isl_band_list_from_band(band
);
735 isl_union_set_free(domain
);
736 isl_schedule_node_free(node
);
741 /* Construct a list of isl_band structures from "node" depending
742 * on the type of "node".
743 * "domain" is the universe set of the domain elements that reach "node".
744 * "parent" is the parent isl_band of the isl_band structures constructed
747 * If schedule_separate_components is set then set nodes are treated
748 * as sequence nodes. Otherwise, we directly extract an (implicitly
749 * parallel) list of isl_band structures.
751 * If "node" is a filter, then "domain" is updated by the filter.
753 static __isl_give isl_band_list
*construct_band_list(
754 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
755 __isl_keep isl_band
*parent
)
757 enum isl_schedule_node_type type
;
761 isl_union_set
*filter
;
763 if (!node
|| !domain
)
766 type
= isl_schedule_node_get_type(node
);
768 case isl_schedule_node_error
:
770 case isl_schedule_node_context
:
771 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
772 "context nodes not supported", goto error
);
773 case isl_schedule_node_domain
:
774 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
775 "internal domain nodes not allowed", goto error
);
776 case isl_schedule_node_expansion
:
777 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
778 "expansion nodes not supported", goto error
);
779 case isl_schedule_node_extension
:
780 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
781 "extension nodes not supported", goto error
);
782 case isl_schedule_node_filter
:
783 filter
= isl_schedule_node_filter_get_filter(node
);
784 domain
= isl_union_set_intersect(domain
, filter
);
785 node
= isl_schedule_node_child(node
, 0);
786 return construct_band_list(node
, domain
, parent
);
787 case isl_schedule_node_guard
:
788 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
789 "guard nodes not supported", goto error
);
790 case isl_schedule_node_mark
:
791 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
792 "mark nodes not supported", goto error
);
793 case isl_schedule_node_set
:
794 ctx
= isl_schedule_node_get_ctx(node
);
795 if (isl_options_get_schedule_separate_components(ctx
))
796 return construct_band_list_sequence(node
, domain
,
799 return construct_band_list_from_children(node
, domain
,
801 case isl_schedule_node_sequence
:
802 return construct_band_list_sequence(node
, domain
, parent
);
803 case isl_schedule_node_leaf
:
804 case isl_schedule_node_band
:
805 band
= construct_band(node
, domain
, parent
);
806 list
= isl_band_list_from_band(band
);
812 isl_union_set_free(domain
);
813 isl_schedule_node_free(node
);
817 /* Return the roots of a band forest representation of the schedule.
818 * The band forest is constructed from the schedule tree,
819 * but once such a band forest is
820 * constructed, we forget about the original schedule tree since
821 * the user may modify the schedule through the band forest.
823 __isl_give isl_band_list
*isl_schedule_get_band_forest(
824 __isl_keep isl_schedule
*schedule
)
826 isl_schedule_node
*node
;
827 isl_union_set
*domain
;
831 if (schedule
->root
) {
832 node
= isl_schedule_get_root(schedule
);
833 domain
= isl_schedule_node_domain_get_domain(node
);
834 domain
= isl_union_set_universe(domain
);
835 node
= isl_schedule_node_child(node
, 0);
837 schedule
->band_forest
= construct_band_list(node
, domain
, NULL
);
838 schedule
->root
= isl_schedule_tree_free(schedule
->root
);
840 return isl_band_list_dup(schedule
->band_forest
);
843 /* Call "fn" on each band in the schedule in depth-first post-order.
845 int isl_schedule_foreach_band(__isl_keep isl_schedule
*sched
,
846 int (*fn
)(__isl_keep isl_band
*band
, void *user
), void *user
)
849 isl_band_list
*forest
;
854 forest
= isl_schedule_get_band_forest(sched
);
855 r
= isl_band_list_foreach_band(forest
, fn
, user
);
856 isl_band_list_free(forest
);
861 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
862 __isl_keep isl_band_list
*list
);
864 static __isl_give isl_printer
*print_band(__isl_take isl_printer
*p
,
865 __isl_keep isl_band
*band
)
867 isl_band_list
*children
;
869 p
= isl_printer_start_line(p
);
870 p
= isl_printer_print_union_pw_multi_aff(p
, band
->pma
);
871 p
= isl_printer_end_line(p
);
873 if (!isl_band_has_children(band
))
876 children
= isl_band_get_children(band
);
878 p
= isl_printer_indent(p
, 4);
879 p
= print_band_list(p
, children
);
880 p
= isl_printer_indent(p
, -4);
882 isl_band_list_free(children
);
887 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
888 __isl_keep isl_band_list
*list
)
892 n
= isl_band_list_n_band(list
);
893 for (i
= 0; i
< n
; ++i
) {
895 band
= isl_band_list_get_band(list
, i
);
896 p
= print_band(p
, band
);
903 /* Insert a band node with partial schedule "partial" between the domain
904 * root node of "schedule" and its single child.
905 * Return a pointer to the updated schedule.
907 * If any of the nodes in the tree depend on the set of outer band nodes
908 * then we refuse to insert the band node.
910 __isl_give isl_schedule
*isl_schedule_insert_partial_schedule(
911 __isl_take isl_schedule
*schedule
,
912 __isl_take isl_multi_union_pw_aff
*partial
)
914 isl_schedule_node
*node
;
917 node
= isl_schedule_get_root(schedule
);
918 isl_schedule_free(schedule
);
921 if (isl_schedule_node_get_type(node
) != isl_schedule_node_domain
)
922 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
923 "root node not a domain node", goto error
);
925 node
= isl_schedule_node_child(node
, 0);
926 anchored
= isl_schedule_node_is_subtree_anchored(node
);
930 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
931 "cannot insert band node in anchored subtree",
933 node
= isl_schedule_node_insert_partial_schedule(node
, partial
);
935 schedule
= isl_schedule_node_get_schedule(node
);
936 isl_schedule_node_free(node
);
940 isl_schedule_node_free(node
);
941 isl_multi_union_pw_aff_free(partial
);
945 /* Insert a context node with constraints "context" between the domain
946 * root node of "schedule" and its single child.
947 * Return a pointer to the updated schedule.
949 __isl_give isl_schedule
*isl_schedule_insert_context(
950 __isl_take isl_schedule
*schedule
, __isl_take isl_set
*context
)
952 isl_schedule_node
*node
;
954 node
= isl_schedule_get_root(schedule
);
955 isl_schedule_free(schedule
);
956 node
= isl_schedule_node_child(node
, 0);
957 node
= isl_schedule_node_insert_context(node
, context
);
958 schedule
= isl_schedule_node_get_schedule(node
);
959 isl_schedule_node_free(node
);
964 /* Insert a guard node with constraints "guard" between the domain
965 * root node of "schedule" and its single child.
966 * Return a pointer to the updated schedule.
968 __isl_give isl_schedule
*isl_schedule_insert_guard(
969 __isl_take isl_schedule
*schedule
, __isl_take isl_set
*guard
)
971 isl_schedule_node
*node
;
973 node
= isl_schedule_get_root(schedule
);
974 isl_schedule_free(schedule
);
975 node
= isl_schedule_node_child(node
, 0);
976 node
= isl_schedule_node_insert_guard(node
, guard
);
977 schedule
= isl_schedule_node_get_schedule(node
);
978 isl_schedule_node_free(node
);
983 /* Return a tree with as top-level node a filter corresponding to "filter" and
984 * as child, the (single) child of "tree".
985 * However, if this single child is of type "type", then the filter is inserted
986 * in the children of this single child instead.
988 static __isl_give isl_schedule_tree
*insert_filter_in_child_of_type(
989 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
,
990 enum isl_schedule_node_type type
)
992 if (!isl_schedule_tree_has_children(tree
)) {
993 isl_schedule_tree_free(tree
);
994 return isl_schedule_tree_from_filter(filter
);
996 tree
= isl_schedule_tree_child(tree
, 0);
999 if (isl_schedule_tree_get_type(tree
) == type
)
1000 tree
= isl_schedule_tree_children_insert_filter(tree
, filter
);
1002 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
1007 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1008 * with a top-level node (underneath the domain node) of type "type",
1009 * either isl_schedule_node_sequence or isl_schedule_node_set.
1010 * The domains of the two schedules are assumed to be disjoint.
1012 * The new schedule has as domain the union of the domains of the two
1013 * schedules. The child of the domain node is a node of type "type"
1014 * with two filters corresponding to the domains of the input schedules.
1015 * If one (or both) of the top-level nodes of the two schedules is itself
1016 * of type "type", then the filter is pushed into the children of that
1017 * node and the sequence of set is flattened.
1019 __isl_give isl_schedule
*isl_schedule_pair(enum isl_schedule_node_type type
,
1020 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
1024 enum isl_schedule_node_type root_type
;
1025 isl_schedule_tree
*tree1
, *tree2
;
1026 isl_union_set
*filter1
, *filter2
, *domain
;
1028 if (!schedule1
|| !schedule2
)
1031 root_type
= isl_schedule_tree_get_type(schedule1
->root
);
1032 if (root_type
!= isl_schedule_node_domain
)
1033 isl_die(isl_schedule_get_ctx(schedule1
), isl_error_internal
,
1034 "root node not a domain node", goto error
);
1035 root_type
= isl_schedule_tree_get_type(schedule2
->root
);
1036 if (root_type
!= isl_schedule_node_domain
)
1037 isl_die(isl_schedule_get_ctx(schedule1
), isl_error_internal
,
1038 "root node not a domain node", goto error
);
1040 ctx
= isl_schedule_get_ctx(schedule1
);
1041 tree1
= isl_schedule_tree_copy(schedule1
->root
);
1042 filter1
= isl_schedule_tree_domain_get_domain(tree1
);
1043 tree2
= isl_schedule_tree_copy(schedule2
->root
);
1044 filter2
= isl_schedule_tree_domain_get_domain(tree2
);
1046 isl_schedule_free(schedule1
);
1047 isl_schedule_free(schedule2
);
1049 disjoint
= isl_union_set_is_disjoint(filter1
, filter2
);
1051 filter1
= isl_union_set_free(filter1
);
1053 isl_die(ctx
, isl_error_invalid
,
1054 "schedule domains not disjoint",
1055 filter1
= isl_union_set_free(filter1
));
1057 domain
= isl_union_set_union(isl_union_set_copy(filter1
),
1058 isl_union_set_copy(filter2
));
1059 filter1
= isl_union_set_gist(filter1
, isl_union_set_copy(domain
));
1060 filter2
= isl_union_set_gist(filter2
, isl_union_set_copy(domain
));
1062 tree1
= insert_filter_in_child_of_type(tree1
, filter1
, type
);
1063 tree2
= insert_filter_in_child_of_type(tree2
, filter2
, type
);
1065 tree1
= isl_schedule_tree_from_pair(type
, tree1
, tree2
);
1066 tree1
= isl_schedule_tree_insert_domain(tree1
, domain
);
1068 return isl_schedule_from_schedule_tree(ctx
, tree1
);
1070 isl_schedule_free(schedule1
);
1071 isl_schedule_free(schedule2
);
1075 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1076 * through a sequence node.
1077 * The domains of the input schedules are assumed to be disjoint.
1079 __isl_give isl_schedule
*isl_schedule_sequence(
1080 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
1082 return isl_schedule_pair(isl_schedule_node_sequence
,
1083 schedule1
, schedule2
);
1086 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1087 * through a set node.
1088 * The domains of the input schedules are assumed to be disjoint.
1090 __isl_give isl_schedule
*isl_schedule_set(
1091 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
1093 return isl_schedule_pair(isl_schedule_node_set
, schedule1
, schedule2
);
1096 /* Print "schedule" to "p".
1098 * If "schedule" was created from a schedule tree, then we print
1099 * the schedule tree representation. Otherwise, we print
1100 * the band forest representation.
1102 __isl_give isl_printer
*isl_printer_print_schedule(__isl_take isl_printer
*p
,
1103 __isl_keep isl_schedule
*schedule
)
1105 isl_band_list
*forest
;
1108 return isl_printer_free(p
);
1111 return isl_printer_print_schedule_tree(p
, schedule
->root
);
1113 forest
= isl_schedule_get_band_forest(schedule
);
1115 p
= print_band_list(p
, forest
);
1117 isl_band_list_free(forest
);
1122 void isl_schedule_dump(__isl_keep isl_schedule
*schedule
)
1124 isl_printer
*printer
;
1129 printer
= isl_printer_to_file(isl_schedule_get_ctx(schedule
), stderr
);
1130 printer
= isl_printer_set_yaml_style(printer
, ISL_YAML_STYLE_BLOCK
);
1131 printer
= isl_printer_print_schedule(printer
, schedule
);
1133 isl_printer_free(printer
);