2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
16 #include <isl_aff_private.h>
19 #include <isl/schedule.h>
20 #include <isl/schedule_node.h>
22 #include <isl/printer.h>
23 #include <isl_schedule_private.h>
24 #include <isl_schedule_tree.h>
25 #include <isl_schedule_node_private.h>
26 #include <isl_band_private.h>
28 /* Return a schedule encapsulating the given schedule tree.
30 * We currently only allow schedule trees with a domain or extension as root.
32 * The leaf field is initialized as a leaf node so that it can be
33 * used to represent leaves in the constructed schedule.
34 * The reference count is set to -1 since the isl_schedule_tree
35 * should never be freed. It is up to the (internal) users of
36 * these leaves to ensure that they are only used while the schedule
39 __isl_give isl_schedule
*isl_schedule_from_schedule_tree(isl_ctx
*ctx
,
40 __isl_take isl_schedule_tree
*tree
)
42 enum isl_schedule_node_type type
;
43 isl_schedule
*schedule
;
47 type
= isl_schedule_tree_get_type(tree
);
48 if (type
!= isl_schedule_node_domain
&&
49 type
!= isl_schedule_node_extension
)
50 isl_die(isl_schedule_tree_get_ctx(tree
), isl_error_unsupported
,
51 "root of schedule tree should be a domain or extension",
54 schedule
= isl_calloc_type(ctx
, isl_schedule
);
59 schedule
->root
= tree
;
60 schedule
->leaf
= isl_schedule_tree_leaf(ctx
);
63 return isl_schedule_free(schedule
);
66 isl_schedule_tree_free(tree
);
70 /* Return a pointer to a schedule with as single node
71 * a domain node with the given domain.
73 __isl_give isl_schedule
*isl_schedule_from_domain(
74 __isl_take isl_union_set
*domain
)
77 isl_schedule_tree
*tree
;
79 ctx
= isl_union_set_get_ctx(domain
);
80 tree
= isl_schedule_tree_from_domain(domain
);
81 return isl_schedule_from_schedule_tree(ctx
, tree
);
84 /* Return a pointer to a schedule with as single node
85 * a domain node with an empty domain.
87 __isl_give isl_schedule
*isl_schedule_empty(__isl_take isl_space
*space
)
89 return isl_schedule_from_domain(isl_union_set_empty(space
));
92 /* Return a new reference to "sched".
94 __isl_give isl_schedule
*isl_schedule_copy(__isl_keep isl_schedule
*sched
)
103 /* Return an isl_schedule that is equal to "schedule" and that has only
104 * a single reference.
106 * We only need and support this function when the schedule is represented
107 * as a schedule tree.
109 __isl_give isl_schedule
*isl_schedule_cow(__isl_take isl_schedule
*schedule
)
112 isl_schedule_tree
*tree
;
116 if (schedule
->ref
== 1)
119 ctx
= isl_schedule_get_ctx(schedule
);
121 isl_die(ctx
, isl_error_internal
,
122 "only for schedule tree based schedules",
123 return isl_schedule_free(schedule
));
125 tree
= isl_schedule_tree_copy(schedule
->root
);
126 return isl_schedule_from_schedule_tree(ctx
, tree
);
129 __isl_null isl_schedule
*isl_schedule_free(__isl_take isl_schedule
*sched
)
134 if (--sched
->ref
> 0)
137 isl_band_list_free(sched
->band_forest
);
138 isl_schedule_tree_free(sched
->root
);
139 isl_schedule_tree_free(sched
->leaf
);
144 /* Replace the root of "schedule" by "tree".
146 __isl_give isl_schedule
*isl_schedule_set_root(
147 __isl_take isl_schedule
*schedule
, __isl_take isl_schedule_tree
*tree
)
149 if (!schedule
|| !tree
)
151 if (schedule
->root
== tree
) {
152 isl_schedule_tree_free(tree
);
156 schedule
= isl_schedule_cow(schedule
);
159 isl_schedule_tree_free(schedule
->root
);
160 schedule
->root
= tree
;
164 isl_schedule_free(schedule
);
165 isl_schedule_tree_free(tree
);
169 isl_ctx
*isl_schedule_get_ctx(__isl_keep isl_schedule
*schedule
)
171 return schedule
? isl_schedule_tree_get_ctx(schedule
->leaf
) : NULL
;
174 /* Return a pointer to the leaf of "schedule".
176 __isl_keep isl_schedule_tree
*isl_schedule_peek_leaf(
177 __isl_keep isl_schedule
*schedule
)
179 return schedule
? schedule
->leaf
: NULL
;
182 /* Are "schedule1" and "schedule2" obviously equal to each other?
184 isl_bool
isl_schedule_plain_is_equal(__isl_keep isl_schedule
*schedule1
,
185 __isl_keep isl_schedule
*schedule2
)
187 if (!schedule1
|| !schedule2
)
188 return isl_bool_error
;
189 if (schedule1
== schedule2
)
190 return isl_bool_true
;
191 return isl_schedule_tree_plain_is_equal(schedule1
->root
,
195 /* Return the (parameter) space of the schedule, i.e., the space
196 * of the root domain.
198 __isl_give isl_space
*isl_schedule_get_space(
199 __isl_keep isl_schedule
*schedule
)
201 enum isl_schedule_node_type type
;
203 isl_union_set
*domain
;
208 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
209 "schedule tree representation not available",
211 type
= isl_schedule_tree_get_type(schedule
->root
);
212 if (type
!= isl_schedule_node_domain
)
213 isl_die(isl_schedule_get_ctx(schedule
), isl_error_internal
,
214 "root node not a domain node", return NULL
);
216 domain
= isl_schedule_tree_domain_get_domain(schedule
->root
);
217 space
= isl_union_set_get_space(domain
);
218 isl_union_set_free(domain
);
223 /* Return a pointer to the root of "schedule".
225 __isl_give isl_schedule_node
*isl_schedule_get_root(
226 __isl_keep isl_schedule
*schedule
)
229 isl_schedule_tree
*tree
;
230 isl_schedule_tree_list
*ancestors
;
236 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
237 "schedule tree representation not available",
240 ctx
= isl_schedule_get_ctx(schedule
);
241 tree
= isl_schedule_tree_copy(schedule
->root
);
242 schedule
= isl_schedule_copy(schedule
);
243 ancestors
= isl_schedule_tree_list_alloc(ctx
, 0);
244 return isl_schedule_node_alloc(schedule
, tree
, ancestors
, NULL
);
247 /* Set max_out to the maximal number of output dimensions over
250 static isl_stat
update_max_out(__isl_take isl_map
*map
, void *user
)
253 int n_out
= isl_map_dim(map
, isl_dim_out
);
255 if (n_out
> *max_out
)
262 /* Internal data structure for map_pad_range.
264 * "max_out" is the maximal schedule dimension.
265 * "res" collects the results.
267 struct isl_pad_schedule_map_data
{
272 /* Pad the range of the given map with zeros to data->max_out and
273 * then add the result to data->res.
275 static isl_stat
map_pad_range(__isl_take isl_map
*map
, void *user
)
277 struct isl_pad_schedule_map_data
*data
= user
;
279 int n_out
= isl_map_dim(map
, isl_dim_out
);
281 map
= isl_map_add_dims(map
, isl_dim_out
, data
->max_out
- n_out
);
282 for (i
= n_out
; i
< data
->max_out
; ++i
)
283 map
= isl_map_fix_si(map
, isl_dim_out
, i
, 0);
285 data
->res
= isl_union_map_add_map(data
->res
, map
);
287 return isl_stat_error
;
292 /* Pad the ranges of the maps in the union map with zeros such they all have
293 * the same dimension.
295 static __isl_give isl_union_map
*pad_schedule_map(
296 __isl_take isl_union_map
*umap
)
298 struct isl_pad_schedule_map_data data
;
302 if (isl_union_map_n_map(umap
) <= 1)
306 if (isl_union_map_foreach_map(umap
, &update_max_out
, &data
.max_out
) < 0)
307 return isl_union_map_free(umap
);
309 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
310 if (isl_union_map_foreach_map(umap
, &map_pad_range
, &data
) < 0)
311 data
.res
= isl_union_map_free(data
.res
);
313 isl_union_map_free(umap
);
317 /* Return the domain of the root domain node of "schedule".
319 __isl_give isl_union_set
*isl_schedule_get_domain(
320 __isl_keep isl_schedule
*schedule
)
325 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
326 "schedule tree representation not available",
328 return isl_schedule_tree_domain_get_domain(schedule
->root
);
331 /* Traverse all nodes of "sched" in depth first preorder.
333 * If "fn" returns -1 on any of the nodes, then the traversal is aborted.
334 * If "fn" returns 0 on any of the nodes, then the subtree rooted
335 * at that node is skipped.
337 * Return 0 on success and -1 on failure.
339 isl_stat
isl_schedule_foreach_schedule_node_top_down(
340 __isl_keep isl_schedule
*sched
,
341 isl_bool (*fn
)(__isl_keep isl_schedule_node
*node
, void *user
),
344 isl_schedule_node
*node
;
348 return isl_stat_error
;
350 node
= isl_schedule_get_root(sched
);
351 r
= isl_schedule_node_foreach_descendant_top_down(node
, fn
, user
);
352 isl_schedule_node_free(node
);
357 /* Traverse the node of "sched" in depth first postorder,
358 * allowing the user to modify the visited node.
359 * The traversal continues from the node returned by the callback function.
360 * It is the responsibility of the user to ensure that this does not
361 * lead to an infinite loop. It is safest to always return a pointer
362 * to the same position (same ancestors and child positions) as the input node.
364 __isl_give isl_schedule
*isl_schedule_map_schedule_node_bottom_up(
365 __isl_take isl_schedule
*schedule
,
366 __isl_give isl_schedule_node
*(*fn
)(
367 __isl_take isl_schedule_node
*node
, void *user
), void *user
)
369 isl_schedule_node
*node
;
371 node
= isl_schedule_get_root(schedule
);
372 isl_schedule_free(schedule
);
374 node
= isl_schedule_node_map_descendant_bottom_up(node
, fn
, user
);
375 schedule
= isl_schedule_node_get_schedule(node
);
376 isl_schedule_node_free(node
);
381 /* Wrapper around isl_schedule_node_reset_user for use as
382 * an isl_schedule_map_schedule_node_bottom_up callback.
384 static __isl_give isl_schedule_node
*reset_user(
385 __isl_take isl_schedule_node
*node
, void *user
)
387 return isl_schedule_node_reset_user(node
);
390 /* Reset the user pointer on all identifiers of parameters and tuples
391 * in the schedule "schedule".
393 __isl_give isl_schedule
*isl_schedule_reset_user(
394 __isl_take isl_schedule
*schedule
)
396 return isl_schedule_map_schedule_node_bottom_up(schedule
, &reset_user
,
400 /* Wrapper around isl_schedule_node_align_params for use as
401 * an isl_schedule_map_schedule_node_bottom_up callback.
403 static __isl_give isl_schedule_node
*align_params(
404 __isl_take isl_schedule_node
*node
, void *user
)
406 isl_space
*space
= user
;
408 return isl_schedule_node_align_params(node
, isl_space_copy(space
));
411 /* Align the parameters of all nodes in schedule "schedule"
412 * to those of "space".
414 __isl_give isl_schedule
*isl_schedule_align_params(
415 __isl_take isl_schedule
*schedule
, __isl_take isl_space
*space
)
417 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
418 &align_params
, space
);
419 isl_space_free(space
);
423 /* Wrapper around isl_schedule_node_pullback_union_pw_multi_aff for use as
424 * an isl_schedule_map_schedule_node_bottom_up callback.
426 static __isl_give isl_schedule_node
*pullback_upma(
427 __isl_take isl_schedule_node
*node
, void *user
)
429 isl_union_pw_multi_aff
*upma
= user
;
431 return isl_schedule_node_pullback_union_pw_multi_aff(node
,
432 isl_union_pw_multi_aff_copy(upma
));
435 /* Compute the pullback of "schedule" by the function represented by "upma".
436 * In other words, plug in "upma" in the iteration domains of "schedule".
438 * The schedule tree is not allowed to contain any expansion nodes.
440 __isl_give isl_schedule
*isl_schedule_pullback_union_pw_multi_aff(
441 __isl_take isl_schedule
*schedule
,
442 __isl_take isl_union_pw_multi_aff
*upma
)
444 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
445 &pullback_upma
, upma
);
446 isl_union_pw_multi_aff_free(upma
);
450 /* Expand the schedule "schedule" by extending all leaves
451 * with an expansion node with as subtree the tree of "expansion".
452 * The expansion of the expansion node is determined by "contraction"
453 * and the domain of "expansion". That is, the domain of "expansion"
454 * is contracted according to "contraction".
456 * Call isl_schedule_node_expand after extracting the required
457 * information from "expansion".
459 __isl_give isl_schedule
*isl_schedule_expand(__isl_take isl_schedule
*schedule
,
460 __isl_take isl_union_pw_multi_aff
*contraction
,
461 __isl_take isl_schedule
*expansion
)
463 isl_union_set
*domain
;
464 isl_schedule_node
*node
;
465 isl_schedule_tree
*tree
;
467 domain
= isl_schedule_get_domain(expansion
);
469 node
= isl_schedule_get_root(expansion
);
470 node
= isl_schedule_node_child(node
, 0);
471 tree
= isl_schedule_node_get_tree(node
);
472 isl_schedule_node_free(node
);
473 isl_schedule_free(expansion
);
475 node
= isl_schedule_get_root(schedule
);
476 isl_schedule_free(schedule
);
477 node
= isl_schedule_node_expand(node
, contraction
, domain
, tree
);
478 schedule
= isl_schedule_node_get_schedule(node
);
479 isl_schedule_node_free(node
);
484 /* Intersect the domain of the schedule "schedule" with "domain".
485 * The root of "schedule" is required to be a domain node.
487 __isl_give isl_schedule
*isl_schedule_intersect_domain(
488 __isl_take isl_schedule
*schedule
, __isl_take isl_union_set
*domain
)
490 enum isl_schedule_node_type root_type
;
491 isl_schedule_node
*node
;
493 if (!schedule
|| !domain
)
496 root_type
= isl_schedule_tree_get_type(schedule
->root
);
497 if (root_type
!= isl_schedule_node_domain
)
498 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
499 "root node must be a domain node", goto error
);
501 node
= isl_schedule_get_root(schedule
);
502 isl_schedule_free(schedule
);
503 node
= isl_schedule_node_domain_intersect_domain(node
, domain
);
504 schedule
= isl_schedule_node_get_schedule(node
);
505 isl_schedule_node_free(node
);
509 isl_schedule_free(schedule
);
510 isl_union_set_free(domain
);
514 /* Replace the domain of the schedule "schedule" with the gist
515 * of the original domain with respect to the parameter domain "context".
517 __isl_give isl_schedule
*isl_schedule_gist_domain_params(
518 __isl_take isl_schedule
*schedule
, __isl_take isl_set
*context
)
520 enum isl_schedule_node_type root_type
;
521 isl_schedule_node
*node
;
523 if (!schedule
|| !context
)
526 root_type
= isl_schedule_tree_get_type(schedule
->root
);
527 if (root_type
!= isl_schedule_node_domain
)
528 isl_die(isl_schedule_get_ctx(schedule
), isl_error_invalid
,
529 "root node must be a domain node", goto error
);
531 node
= isl_schedule_get_root(schedule
);
532 isl_schedule_free(schedule
);
533 node
= isl_schedule_node_domain_gist_params(node
, context
);
534 schedule
= isl_schedule_node_get_schedule(node
);
535 isl_schedule_node_free(node
);
539 isl_schedule_free(schedule
);
540 isl_set_free(context
);
544 /* Return an isl_union_map representation of the schedule.
545 * If we still have access to the schedule tree, then we return
546 * an isl_union_map corresponding to the subtree schedule of the child
547 * of the root domain node. That is, we do not intersect the domain
548 * of the returned isl_union_map with the domain constraints.
549 * Otherwise, we must have removed it because we created a band forest.
550 * If so, we extract the isl_union_map from the forest.
551 * This reconstructed schedule map
552 * then needs to be padded with zeros to unify the schedule space
553 * since the result of isl_band_list_get_suffix_schedule may not have
554 * a unified schedule space.
556 __isl_give isl_union_map
*isl_schedule_get_map(__isl_keep isl_schedule
*sched
)
558 enum isl_schedule_node_type type
;
559 isl_schedule_node
*node
;
566 type
= isl_schedule_tree_get_type(sched
->root
);
567 if (type
!= isl_schedule_node_domain
)
568 isl_die(isl_schedule_get_ctx(sched
), isl_error_internal
,
569 "root node not a domain node", return NULL
);
571 node
= isl_schedule_get_root(sched
);
572 node
= isl_schedule_node_child(node
, 0);
573 umap
= isl_schedule_node_get_subtree_schedule_union_map(node
);
574 isl_schedule_node_free(node
);
579 umap
= isl_band_list_get_suffix_schedule(sched
->band_forest
);
580 return pad_schedule_map(umap
);
583 static __isl_give isl_band_list
*construct_band_list(
584 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
585 __isl_keep isl_band
*parent
);
587 /* Construct an isl_band structure from the given schedule tree node,
588 * which may be either a band node or a leaf node.
589 * In the latter case, construct a zero-dimensional band.
590 * "domain" is the universe set of the domain elements that reach "node".
591 * "parent" is the parent isl_band of the isl_band constructed
594 * In case of a band node, we copy the properties (except tilability,
595 * which is implicit in an isl_band) to the isl_band.
596 * We assume that the band node is not zero-dimensional.
597 * If the child of the band node is not a leaf node,
598 * then we extract the children of the isl_band from this child.
600 static __isl_give isl_band
*construct_band(__isl_take isl_schedule_node
*node
,
601 __isl_take isl_union_set
*domain
, __isl_keep isl_band
*parent
)
605 isl_band
*band
= NULL
;
606 isl_multi_union_pw_aff
*mupa
;
608 if (!node
|| !domain
)
611 ctx
= isl_schedule_node_get_ctx(node
);
612 band
= isl_band_alloc(ctx
);
616 band
->schedule
= node
->schedule
;
617 band
->parent
= parent
;
619 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
621 band
->pma
= isl_union_pw_multi_aff_from_domain(domain
);
622 isl_schedule_node_free(node
);
626 band
->n
= isl_schedule_node_band_n_member(node
);
628 isl_die(ctx
, isl_error_unsupported
,
629 "zero-dimensional band nodes not supported",
631 band
->coincident
= isl_alloc_array(ctx
, int, band
->n
);
632 if (band
->n
&& !band
->coincident
)
634 for (i
= 0; i
< band
->n
; ++i
)
635 band
->coincident
[i
] =
636 isl_schedule_node_band_member_get_coincident(node
, i
);
637 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
638 band
->pma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(mupa
);
642 node
= isl_schedule_node_child(node
, 0);
643 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
) {
644 isl_schedule_node_free(node
);
645 isl_union_set_free(domain
);
649 band
->children
= construct_band_list(node
, domain
, band
);
651 return isl_band_free(band
);
655 isl_union_set_free(domain
);
656 isl_schedule_node_free(node
);
661 /* Construct a list of isl_band structures from the children of "node".
662 * "node" itself is a sequence or set node, so that each of the child nodes
663 * is a filter node and the list returned by node_construct_band_list
664 * consists of a single element.
665 * "domain" is the universe set of the domain elements that reach "node".
666 * "parent" is the parent isl_band of the isl_band structures constructed
669 static __isl_give isl_band_list
*construct_band_list_from_children(
670 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
671 __isl_keep isl_band
*parent
)
677 n
= isl_schedule_node_n_children(node
);
679 ctx
= isl_schedule_node_get_ctx(node
);
680 list
= isl_band_list_alloc(ctx
, 0);
681 for (i
= 0; i
< n
; ++i
) {
682 isl_schedule_node
*child
;
683 isl_band_list
*list_i
;
685 child
= isl_schedule_node_get_child(node
, i
);
686 list_i
= construct_band_list(child
, isl_union_set_copy(domain
),
688 list
= isl_band_list_concat(list
, list_i
);
691 isl_union_set_free(domain
);
692 isl_schedule_node_free(node
);
697 /* Construct an isl_band structure from the given sequence node
698 * (or set node that is treated as a sequence node).
699 * A single-dimensional band is created with as schedule for each of
700 * filters of the children, the corresponding child position.
701 * "domain" is the universe set of the domain elements that reach "node".
702 * "parent" is the parent isl_band of the isl_band constructed
705 static __isl_give isl_band_list
*construct_band_list_sequence(
706 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
707 __isl_keep isl_band
*parent
)
711 isl_band
*band
= NULL
;
713 isl_union_pw_multi_aff
*upma
;
715 if (!node
|| !domain
)
718 ctx
= isl_schedule_node_get_ctx(node
);
719 band
= isl_band_alloc(ctx
);
723 band
->schedule
= node
->schedule
;
724 band
->parent
= parent
;
726 band
->coincident
= isl_calloc_array(ctx
, int, band
->n
);
727 if (!band
->coincident
)
730 n
= isl_schedule_node_n_children(node
);
731 space
= isl_union_set_get_space(domain
);
732 upma
= isl_union_pw_multi_aff_empty(isl_space_copy(space
));
734 space
= isl_space_set_from_params(space
);
735 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
737 for (i
= 0; i
< n
; ++i
) {
738 isl_schedule_node
*child
;
739 isl_union_set
*filter
;
743 isl_union_pw_multi_aff
*upma_i
;
745 child
= isl_schedule_node_get_child(node
, i
);
746 filter
= isl_schedule_node_filter_get_filter(child
);
747 isl_schedule_node_free(child
);
748 filter
= isl_union_set_intersect(filter
,
749 isl_union_set_copy(domain
));
750 v
= isl_val_int_from_si(ctx
, i
);
751 vl
= isl_val_list_from_val(v
);
752 mv
= isl_multi_val_from_val_list(isl_space_copy(space
), vl
);
753 upma_i
= isl_union_pw_multi_aff_multi_val_on_domain(filter
, mv
);
754 upma
= isl_union_pw_multi_aff_union_add(upma
, upma_i
);
757 isl_space_free(space
);
763 band
->children
= construct_band_list_from_children(node
, domain
, band
);
765 band
= isl_band_free(band
);
766 return isl_band_list_from_band(band
);
768 isl_union_set_free(domain
);
769 isl_schedule_node_free(node
);
774 /* Construct a list of isl_band structures from "node" depending
775 * on the type of "node".
776 * "domain" is the universe set of the domain elements that reach "node".
777 * "parent" is the parent isl_band of the isl_band structures constructed
780 * If schedule_separate_components is set then set nodes are treated
781 * as sequence nodes. Otherwise, we directly extract an (implicitly
782 * parallel) list of isl_band structures.
784 * If "node" is a filter, then "domain" is updated by the filter.
786 static __isl_give isl_band_list
*construct_band_list(
787 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
788 __isl_keep isl_band
*parent
)
790 enum isl_schedule_node_type type
;
794 isl_union_set
*filter
;
796 if (!node
|| !domain
)
799 type
= isl_schedule_node_get_type(node
);
801 case isl_schedule_node_error
:
803 case isl_schedule_node_context
:
804 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
805 "context nodes not supported", goto error
);
806 case isl_schedule_node_domain
:
807 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
808 "internal domain nodes not allowed", goto error
);
809 case isl_schedule_node_expansion
:
810 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
811 "expansion nodes not supported", goto error
);
812 case isl_schedule_node_extension
:
813 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
814 "extension nodes not supported", goto error
);
815 case isl_schedule_node_filter
:
816 filter
= isl_schedule_node_filter_get_filter(node
);
817 domain
= isl_union_set_intersect(domain
, filter
);
818 node
= isl_schedule_node_child(node
, 0);
819 return construct_band_list(node
, domain
, parent
);
820 case isl_schedule_node_guard
:
821 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
822 "guard nodes not supported", goto error
);
823 case isl_schedule_node_mark
:
824 isl_die(isl_schedule_node_get_ctx(node
), isl_error_unsupported
,
825 "mark nodes not supported", goto error
);
826 case isl_schedule_node_set
:
827 ctx
= isl_schedule_node_get_ctx(node
);
828 if (isl_options_get_schedule_separate_components(ctx
))
829 return construct_band_list_sequence(node
, domain
,
832 return construct_band_list_from_children(node
, domain
,
834 case isl_schedule_node_sequence
:
835 return construct_band_list_sequence(node
, domain
, parent
);
836 case isl_schedule_node_leaf
:
837 case isl_schedule_node_band
:
838 band
= construct_band(node
, domain
, parent
);
839 list
= isl_band_list_from_band(band
);
845 isl_union_set_free(domain
);
846 isl_schedule_node_free(node
);
850 /* Return the roots of a band forest representation of the schedule.
851 * The band forest is constructed from the schedule tree,
852 * but once such a band forest is
853 * constructed, we forget about the original schedule tree since
854 * the user may modify the schedule through the band forest.
856 __isl_give isl_band_list
*isl_schedule_get_band_forest(
857 __isl_keep isl_schedule
*schedule
)
859 isl_schedule_node
*node
;
860 isl_union_set
*domain
;
864 if (schedule
->root
) {
865 node
= isl_schedule_get_root(schedule
);
866 domain
= isl_schedule_node_domain_get_domain(node
);
867 domain
= isl_union_set_universe(domain
);
868 node
= isl_schedule_node_child(node
, 0);
870 schedule
->band_forest
= construct_band_list(node
, domain
, NULL
);
871 schedule
->root
= isl_schedule_tree_free(schedule
->root
);
873 return isl_band_list_dup(schedule
->band_forest
);
876 /* Call "fn" on each band in the schedule in depth-first post-order.
878 int isl_schedule_foreach_band(__isl_keep isl_schedule
*sched
,
879 int (*fn
)(__isl_keep isl_band
*band
, void *user
), void *user
)
882 isl_band_list
*forest
;
887 forest
= isl_schedule_get_band_forest(sched
);
888 r
= isl_band_list_foreach_band(forest
, fn
, user
);
889 isl_band_list_free(forest
);
894 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
895 __isl_keep isl_band_list
*list
);
897 static __isl_give isl_printer
*print_band(__isl_take isl_printer
*p
,
898 __isl_keep isl_band
*band
)
900 isl_band_list
*children
;
902 p
= isl_printer_start_line(p
);
903 p
= isl_printer_print_union_pw_multi_aff(p
, band
->pma
);
904 p
= isl_printer_end_line(p
);
906 if (!isl_band_has_children(band
))
909 children
= isl_band_get_children(band
);
911 p
= isl_printer_indent(p
, 4);
912 p
= print_band_list(p
, children
);
913 p
= isl_printer_indent(p
, -4);
915 isl_band_list_free(children
);
920 static __isl_give isl_printer
*print_band_list(__isl_take isl_printer
*p
,
921 __isl_keep isl_band_list
*list
)
925 n
= isl_band_list_n_band(list
);
926 for (i
= 0; i
< n
; ++i
) {
928 band
= isl_band_list_get_band(list
, i
);
929 p
= print_band(p
, band
);
936 /* Insert a band node with partial schedule "partial" between the domain
937 * root node of "schedule" and its single child.
938 * Return a pointer to the updated schedule.
940 * If any of the nodes in the tree depend on the set of outer band nodes
941 * then we refuse to insert the band node.
943 __isl_give isl_schedule
*isl_schedule_insert_partial_schedule(
944 __isl_take isl_schedule
*schedule
,
945 __isl_take isl_multi_union_pw_aff
*partial
)
947 isl_schedule_node
*node
;
950 node
= isl_schedule_get_root(schedule
);
951 isl_schedule_free(schedule
);
954 if (isl_schedule_node_get_type(node
) != isl_schedule_node_domain
)
955 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
956 "root node not a domain node", goto error
);
958 node
= isl_schedule_node_child(node
, 0);
959 anchored
= isl_schedule_node_is_subtree_anchored(node
);
963 isl_die(isl_schedule_node_get_ctx(node
), isl_error_invalid
,
964 "cannot insert band node in anchored subtree",
966 node
= isl_schedule_node_insert_partial_schedule(node
, partial
);
968 schedule
= isl_schedule_node_get_schedule(node
);
969 isl_schedule_node_free(node
);
973 isl_schedule_node_free(node
);
974 isl_multi_union_pw_aff_free(partial
);
978 /* Insert a context node with constraints "context" between the domain
979 * root node of "schedule" and its single child.
980 * Return a pointer to the updated schedule.
982 __isl_give isl_schedule
*isl_schedule_insert_context(
983 __isl_take isl_schedule
*schedule
, __isl_take isl_set
*context
)
985 isl_schedule_node
*node
;
987 node
= isl_schedule_get_root(schedule
);
988 isl_schedule_free(schedule
);
989 node
= isl_schedule_node_child(node
, 0);
990 node
= isl_schedule_node_insert_context(node
, context
);
991 schedule
= isl_schedule_node_get_schedule(node
);
992 isl_schedule_node_free(node
);
997 /* Insert a guard node with constraints "guard" between the domain
998 * root node of "schedule" and its single child.
999 * Return a pointer to the updated schedule.
1001 __isl_give isl_schedule
*isl_schedule_insert_guard(
1002 __isl_take isl_schedule
*schedule
, __isl_take isl_set
*guard
)
1004 isl_schedule_node
*node
;
1006 node
= isl_schedule_get_root(schedule
);
1007 isl_schedule_free(schedule
);
1008 node
= isl_schedule_node_child(node
, 0);
1009 node
= isl_schedule_node_insert_guard(node
, guard
);
1010 schedule
= isl_schedule_node_get_schedule(node
);
1011 isl_schedule_node_free(node
);
1016 /* Return a tree with as top-level node a filter corresponding to "filter" and
1017 * as child, the (single) child of "tree".
1018 * However, if this single child is of type "type", then the filter is inserted
1019 * in the children of this single child instead.
1021 static __isl_give isl_schedule_tree
*insert_filter_in_child_of_type(
1022 __isl_take isl_schedule_tree
*tree
, __isl_take isl_union_set
*filter
,
1023 enum isl_schedule_node_type type
)
1025 if (!isl_schedule_tree_has_children(tree
)) {
1026 isl_schedule_tree_free(tree
);
1027 return isl_schedule_tree_from_filter(filter
);
1029 tree
= isl_schedule_tree_child(tree
, 0);
1032 if (isl_schedule_tree_get_type(tree
) == type
)
1033 tree
= isl_schedule_tree_children_insert_filter(tree
, filter
);
1035 tree
= isl_schedule_tree_insert_filter(tree
, filter
);
1040 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1041 * with a top-level node (underneath the domain node) of type "type",
1042 * either isl_schedule_node_sequence or isl_schedule_node_set.
1043 * The domains of the two schedules are assumed to be disjoint.
1045 * The new schedule has as domain the union of the domains of the two
1046 * schedules. The child of the domain node is a node of type "type"
1047 * with two filters corresponding to the domains of the input schedules.
1048 * If one (or both) of the top-level nodes of the two schedules is itself
1049 * of type "type", then the filter is pushed into the children of that
1050 * node and the sequence of set is flattened.
1052 __isl_give isl_schedule
*isl_schedule_pair(enum isl_schedule_node_type type
,
1053 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
1057 enum isl_schedule_node_type root_type
;
1058 isl_schedule_tree
*tree1
, *tree2
;
1059 isl_union_set
*filter1
, *filter2
, *domain
;
1061 if (!schedule1
|| !schedule2
)
1064 root_type
= isl_schedule_tree_get_type(schedule1
->root
);
1065 if (root_type
!= isl_schedule_node_domain
)
1066 isl_die(isl_schedule_get_ctx(schedule1
), isl_error_internal
,
1067 "root node not a domain node", goto error
);
1068 root_type
= isl_schedule_tree_get_type(schedule2
->root
);
1069 if (root_type
!= isl_schedule_node_domain
)
1070 isl_die(isl_schedule_get_ctx(schedule1
), isl_error_internal
,
1071 "root node not a domain node", goto error
);
1073 ctx
= isl_schedule_get_ctx(schedule1
);
1074 tree1
= isl_schedule_tree_copy(schedule1
->root
);
1075 filter1
= isl_schedule_tree_domain_get_domain(tree1
);
1076 tree2
= isl_schedule_tree_copy(schedule2
->root
);
1077 filter2
= isl_schedule_tree_domain_get_domain(tree2
);
1079 isl_schedule_free(schedule1
);
1080 isl_schedule_free(schedule2
);
1082 disjoint
= isl_union_set_is_disjoint(filter1
, filter2
);
1084 filter1
= isl_union_set_free(filter1
);
1086 isl_die(ctx
, isl_error_invalid
,
1087 "schedule domains not disjoint",
1088 filter1
= isl_union_set_free(filter1
));
1090 domain
= isl_union_set_union(isl_union_set_copy(filter1
),
1091 isl_union_set_copy(filter2
));
1092 filter1
= isl_union_set_gist(filter1
, isl_union_set_copy(domain
));
1093 filter2
= isl_union_set_gist(filter2
, isl_union_set_copy(domain
));
1095 tree1
= insert_filter_in_child_of_type(tree1
, filter1
, type
);
1096 tree2
= insert_filter_in_child_of_type(tree2
, filter2
, type
);
1098 tree1
= isl_schedule_tree_from_pair(type
, tree1
, tree2
);
1099 tree1
= isl_schedule_tree_insert_domain(tree1
, domain
);
1101 return isl_schedule_from_schedule_tree(ctx
, tree1
);
1103 isl_schedule_free(schedule1
);
1104 isl_schedule_free(schedule2
);
1108 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1109 * through a sequence node.
1110 * The domains of the input schedules are assumed to be disjoint.
1112 __isl_give isl_schedule
*isl_schedule_sequence(
1113 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
1115 return isl_schedule_pair(isl_schedule_node_sequence
,
1116 schedule1
, schedule2
);
1119 /* Construct a schedule that combines the schedules "schedule1" and "schedule2"
1120 * through a set node.
1121 * The domains of the input schedules are assumed to be disjoint.
1123 __isl_give isl_schedule
*isl_schedule_set(
1124 __isl_take isl_schedule
*schedule1
, __isl_take isl_schedule
*schedule2
)
1126 return isl_schedule_pair(isl_schedule_node_set
, schedule1
, schedule2
);
1129 /* Print "schedule" to "p".
1131 * If "schedule" was created from a schedule tree, then we print
1132 * the schedule tree representation. Otherwise, we print
1133 * the band forest representation.
1135 __isl_give isl_printer
*isl_printer_print_schedule(__isl_take isl_printer
*p
,
1136 __isl_keep isl_schedule
*schedule
)
1138 isl_band_list
*forest
;
1141 return isl_printer_free(p
);
1144 return isl_printer_print_schedule_tree(p
, schedule
->root
);
1146 forest
= isl_schedule_get_band_forest(schedule
);
1148 p
= print_band_list(p
, forest
);
1150 isl_band_list_free(forest
);
1156 #define BASE schedule
1157 #include <print_templ_yaml.c>