2 * Copyright 2012 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
14 #include <isl/space.h>
15 #include <isl_ast_private.h>
16 #include <isl_ast_build_expr.h>
17 #include <isl_ast_build_private.h>
18 #include <isl_ast_graft_private.h>
20 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
21 __isl_keep isl_ast_graft
*graft
);
24 #define BASE ast_graft
26 #include <isl_list_templ.c>
29 #define BASE ast_graft
30 #include <print_templ.c>
32 isl_ctx
*isl_ast_graft_get_ctx(__isl_keep isl_ast_graft
*graft
)
36 return isl_basic_set_get_ctx(graft
->enforced
);
39 __isl_give isl_ast_node
*isl_ast_graft_get_node(
40 __isl_keep isl_ast_graft
*graft
)
42 return graft
? isl_ast_node_copy(graft
->node
) : NULL
;
45 /* Create a graft for "node" with no guards and no enforced conditions.
47 __isl_give isl_ast_graft
*isl_ast_graft_alloc(
48 __isl_take isl_ast_node
*node
, __isl_keep isl_ast_build
*build
)
57 ctx
= isl_ast_node_get_ctx(node
);
58 graft
= isl_calloc_type(ctx
, isl_ast_graft
);
62 space
= isl_ast_build_get_space(build
, 1);
66 graft
->guard
= isl_set_universe(isl_space_copy(space
));
67 graft
->enforced
= isl_basic_set_universe(space
);
69 if (!graft
->guard
|| !graft
->enforced
)
70 return isl_ast_graft_free(graft
);
74 isl_ast_node_free(node
);
78 /* Create a graft with no guards and no enforced conditions
79 * encapsulating a call to the domain element specified by "executed".
80 * "executed" is assumed to be single-valued.
82 __isl_give isl_ast_graft
*isl_ast_graft_alloc_domain(
83 __isl_take isl_map
*executed
, __isl_keep isl_ast_build
*build
)
87 node
= isl_ast_build_call_from_executed(build
, executed
);
89 return isl_ast_graft_alloc(node
, build
);
92 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
93 __isl_keep isl_ast_graft
*graft
)
102 /* Do all the grafts in "list" have the same guard and is this guard
103 * independent of the current depth?
105 static int equal_independent_guards(__isl_keep isl_ast_graft_list
*list
,
106 __isl_keep isl_ast_build
*build
)
110 isl_ast_graft
*graft_0
;
114 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
118 depth
= isl_ast_build_get_depth(build
);
119 if (isl_set_dim(graft_0
->guard
, isl_dim_set
) <= depth
)
122 skip
= isl_set_involves_dims(graft_0
->guard
,
123 isl_dim_set
, depth
, 1);
124 if (skip
< 0 || skip
) {
125 isl_ast_graft_free(graft_0
);
126 return skip
< 0 ? -1 : 0;
129 n
= isl_ast_graft_list_n_ast_graft(list
);
130 for (i
= 1; i
< n
; ++i
) {
131 isl_ast_graft
*graft
;
132 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
136 equal
= isl_set_is_equal(graft_0
->guard
, graft
->guard
);
137 isl_ast_graft_free(graft
);
138 if (equal
< 0 || !equal
)
142 isl_ast_graft_free(graft_0
);
147 /* Hoist "guard" out of the current level (given by "build").
149 * In particular, eliminate the dimension corresponding to the current depth.
151 static __isl_give isl_set
*hoist_guard(__isl_take isl_set
*guard
,
152 __isl_keep isl_ast_build
*build
)
156 depth
= isl_ast_build_get_depth(build
);
157 if (depth
< isl_set_dim(guard
, isl_dim_set
)) {
158 guard
= isl_set_remove_divs_involving_dims(guard
,
159 isl_dim_set
, depth
, 1);
160 guard
= isl_set_eliminate(guard
, isl_dim_set
, depth
, 1);
161 guard
= isl_set_compute_divs(guard
);
167 /* Extract a common guard from the grafts in "list" that can be hoisted
168 * out of the current level. If no such guard can be found, then return
171 * If all the grafts in the list have the same guard and if this guard
172 * is independent of the current level, then it can be hoisted out.
173 * If there is only one graft in the list and if its guard
174 * depends on the current level, then we eliminate this level and
177 * Otherwise, we return the unshifted simple hull of the guards.
178 * In order to be able to hoist as many constraints as possible,
179 * but at the same time avoid hoisting constraints that did not
180 * appear in the guards in the first place, we intersect the guards
181 * with all the information that is available (i.e., the domain
182 * from the build and the enforced constraints of the graft) and
183 * compute the unshifted hull of the result using only constraints
184 * from the original guards.
185 * In particular, intersecting the guards with other known information
186 * allows us to hoist guards that are only explicit is some of
187 * the grafts and implicit in the others.
189 * The special case for equal guards is needed in case those guards
190 * are non-convex. Taking the simple hull would remove information
191 * and would not allow for these guards to be hoisted completely.
193 __isl_give isl_set
*isl_ast_graft_list_extract_hoistable_guard(
194 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
200 isl_set_list
*set_list
;
206 n
= isl_ast_graft_list_n_ast_graft(list
);
208 return isl_set_universe(isl_ast_build_get_space(build
, 1));
210 equal
= equal_independent_guards(list
, build
);
214 if (equal
|| n
== 1) {
215 isl_ast_graft
*graft_0
;
217 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
220 guard
= isl_set_copy(graft_0
->guard
);
222 guard
= hoist_guard(guard
, build
);
223 isl_ast_graft_free(graft_0
);
227 ctx
= isl_ast_build_get_ctx(build
);
228 set_list
= isl_set_list_alloc(ctx
, n
);
229 guard
= isl_set_empty(isl_ast_build_get_space(build
, 1));
230 for (i
= 0; i
< n
; ++i
) {
231 isl_ast_graft
*graft
;
232 isl_basic_set
*enforced
;
235 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
236 enforced
= isl_ast_graft_get_enforced(graft
);
237 guard_i
= isl_set_copy(graft
->guard
);
238 isl_ast_graft_free(graft
);
239 set_list
= isl_set_list_add(set_list
, isl_set_copy(guard_i
));
240 guard_i
= isl_set_intersect(guard_i
,
241 isl_set_from_basic_set(enforced
));
242 guard_i
= isl_set_intersect(guard_i
,
243 isl_ast_build_get_domain(build
));
244 guard
= isl_set_union(guard
, guard_i
);
246 hull
= isl_set_unshifted_simple_hull_from_set_list(guard
, set_list
);
247 guard
= isl_set_from_basic_set(hull
);
248 return hoist_guard(guard
, build
);
251 /* Internal data structure used inside insert_if.
253 * list is the list of guarded nodes created by each call to insert_if.
254 * node is the original node that is guarded by insert_if.
255 * build is the build in which the AST is constructed.
257 struct isl_insert_if_data
{
258 isl_ast_node_list
*list
;
260 isl_ast_build
*build
;
263 static isl_stat
insert_if(__isl_take isl_basic_set
*bset
, void *user
);
265 /* Insert an if node around "node" testing the condition encoded
268 * If the user does not want any disjunctions in the if conditions
269 * and if "guard" does involve a disjunction, then we make the different
270 * disjuncts disjoint and insert an if node corresponding to each disjunct
271 * around a copy of "node". The result is then a block node containing
272 * this sequence of guarded copies of "node".
274 static __isl_give isl_ast_node
*ast_node_insert_if(
275 __isl_take isl_ast_node
*node
, __isl_take isl_set
*guard
,
276 __isl_keep isl_ast_build
*build
)
278 struct isl_insert_if_data data
;
281 ctx
= isl_ast_build_get_ctx(build
);
282 if (isl_options_get_ast_build_allow_or(ctx
) ||
283 isl_set_n_basic_set(guard
) <= 1) {
284 isl_ast_node
*if_node
;
287 expr
= isl_ast_build_expr_from_set_internal(build
, guard
);
289 if_node
= isl_ast_node_alloc_if(expr
);
290 return isl_ast_node_if_set_then(if_node
, node
);
293 guard
= isl_set_make_disjoint(guard
);
295 data
.list
= isl_ast_node_list_alloc(ctx
, 0);
298 if (isl_set_foreach_basic_set(guard
, &insert_if
, &data
) < 0)
299 data
.list
= isl_ast_node_list_free(data
.list
);
302 isl_ast_node_free(data
.node
);
303 return isl_ast_node_alloc_block(data
.list
);
306 /* Insert an if node around a copy of "data->node" testing the condition
307 * encoded in guard "bset" and add the result to data->list.
309 static isl_stat
insert_if(__isl_take isl_basic_set
*bset
, void *user
)
311 struct isl_insert_if_data
*data
= user
;
315 set
= isl_set_from_basic_set(bset
);
316 node
= isl_ast_node_copy(data
->node
);
317 node
= ast_node_insert_if(node
, set
, data
->build
);
318 data
->list
= isl_ast_node_list_add(data
->list
, node
);
323 /* Insert an if node around graft->node testing the condition encoded
324 * in guard "guard", assuming guard involves any conditions.
326 static __isl_give isl_ast_graft
*insert_if_node(
327 __isl_take isl_ast_graft
*graft
, __isl_take isl_set
*guard
,
328 __isl_keep isl_ast_build
*build
)
335 univ
= isl_set_plain_is_universe(guard
);
343 build
= isl_ast_build_copy(build
);
344 graft
->node
= ast_node_insert_if(graft
->node
, guard
, build
);
345 isl_ast_build_free(build
);
348 return isl_ast_graft_free(graft
);
353 return isl_ast_graft_free(graft
);
356 /* Insert an if node around graft->node testing the condition encoded
357 * in graft->guard, assuming graft->guard involves any conditions.
359 static __isl_give isl_ast_graft
*insert_pending_guard_node(
360 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
365 return insert_if_node(graft
, isl_set_copy(graft
->guard
), build
);
368 /* Replace graft->enforced by "enforced".
370 __isl_give isl_ast_graft
*isl_ast_graft_set_enforced(
371 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
373 if (!graft
|| !enforced
)
376 isl_basic_set_free(graft
->enforced
);
377 graft
->enforced
= enforced
;
381 isl_basic_set_free(enforced
);
382 return isl_ast_graft_free(graft
);
385 /* Update "enforced" such that it only involves constraints that are
386 * also enforced by "graft".
388 static __isl_give isl_basic_set
*update_enforced(
389 __isl_take isl_basic_set
*enforced
, __isl_keep isl_ast_graft
*graft
,
392 isl_basic_set
*enforced_g
;
394 enforced_g
= isl_ast_graft_get_enforced(graft
);
395 if (depth
< isl_basic_set_dim(enforced_g
, isl_dim_set
))
396 enforced_g
= isl_basic_set_eliminate(enforced_g
,
397 isl_dim_set
, depth
, 1);
398 enforced_g
= isl_basic_set_remove_unknown_divs(enforced_g
);
399 enforced_g
= isl_basic_set_align_params(enforced_g
,
400 isl_basic_set_get_space(enforced
));
401 enforced
= isl_basic_set_align_params(enforced
,
402 isl_basic_set_get_space(enforced_g
));
403 enforced
= isl_set_simple_hull(isl_basic_set_union(enforced
,
409 /* Extend the node at *body with node.
411 * If body points to the else branch, then *body may still be NULL.
412 * If so, we simply attach node to this else branch.
413 * Otherwise, we attach a list containing the statements already
414 * attached at *body followed by node.
416 static void extend_body(__isl_keep isl_ast_node
**body
,
417 __isl_take isl_ast_node
*node
)
419 isl_ast_node_list
*list
;
426 if ((*body
)->type
== isl_ast_node_block
) {
427 list
= isl_ast_node_block_get_children(*body
);
428 isl_ast_node_free(*body
);
430 list
= isl_ast_node_list_from_ast_node(*body
);
431 list
= isl_ast_node_list_add(list
, node
);
432 *body
= isl_ast_node_alloc_block(list
);
435 /* Merge "graft" into the last graft of "list".
436 * body points to the then or else branch of an if node in that last graft.
438 * We attach graft->node to this branch and update the enforced
439 * set of the last graft of "list" to take into account the enforced
442 static __isl_give isl_ast_graft_list
*graft_extend_body(
443 __isl_take isl_ast_graft_list
*list
,
444 __isl_keep isl_ast_node
**body
, __isl_take isl_ast_graft
*graft
,
445 __isl_keep isl_ast_build
*build
)
451 isl_basic_set
*enforced
;
455 extend_body(body
, isl_ast_node_copy(graft
->node
));
459 n
= isl_ast_graft_list_n_ast_graft(list
);
460 last
= isl_ast_graft_list_get_ast_graft(list
, n
- 1);
462 depth
= isl_ast_build_get_depth(build
);
463 space
= isl_ast_build_get_space(build
, 1);
464 enforced
= isl_basic_set_empty(space
);
465 enforced
= update_enforced(enforced
, last
, depth
);
466 enforced
= update_enforced(enforced
, graft
, depth
);
467 last
= isl_ast_graft_set_enforced(last
, enforced
);
469 list
= isl_ast_graft_list_set_ast_graft(list
, n
- 1, last
);
470 isl_ast_graft_free(graft
);
473 isl_ast_graft_free(graft
);
474 return isl_ast_graft_list_free(list
);
477 /* Merge "graft" into the last graft of "list", attaching graft->node
478 * to the then branch of "last_if".
480 static __isl_give isl_ast_graft_list
*extend_then(
481 __isl_take isl_ast_graft_list
*list
,
482 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
483 __isl_keep isl_ast_build
*build
)
485 return graft_extend_body(list
, &last_if
->u
.i
.then
, graft
, build
);
488 /* Merge "graft" into the last graft of "list", attaching graft->node
489 * to the else branch of "last_if".
491 static __isl_give isl_ast_graft_list
*extend_else(
492 __isl_take isl_ast_graft_list
*list
,
493 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
494 __isl_keep isl_ast_build
*build
)
496 return graft_extend_body(list
, &last_if
->u
.i
.else_node
, graft
, build
);
499 /* This data structure keeps track of an if node.
501 * "node" is the actual if-node
502 * "guard" is the original, non-simplified guard of the node
503 * "complement" is the complement of "guard" in the context of outer if nodes
511 /* Given a list of "n" if nodes, clear those starting at "first"
512 * and return "first" (i.e., the updated size of the array).
514 static int clear_if_nodes(struct isl_if_node
*if_node
, int first
, int n
)
518 for (i
= first
; i
< n
; ++i
) {
519 isl_set_free(if_node
[i
].guard
);
520 isl_set_free(if_node
[i
].complement
);
526 /* For each graft in "list",
527 * insert an if node around graft->node testing the condition encoded
528 * in graft->guard, assuming graft->guard involves any conditions.
530 * We keep track of a list of generated if nodes that can be extended
531 * without changing the order of the elements in "list".
532 * If the guard of a graft is a subset of either the guard or its complement
533 * of one of those if nodes, then the node
534 * of the new graft is inserted into the then or else branch of the last graft
535 * and the current graft is discarded.
536 * The guard of the node is then simplified based on the conditions
537 * enforced at that then or else branch.
538 * Otherwise, the current graft is appended to the list.
540 * We only construct else branches if allowed by the user.
542 static __isl_give isl_ast_graft_list
*insert_pending_guard_nodes(
543 __isl_take isl_ast_graft_list
*list
,
544 __isl_keep isl_ast_build
*build
)
549 isl_ast_graft_list
*res
;
550 struct isl_if_node
*if_node
= NULL
;
553 return isl_ast_graft_list_free(list
);
555 ctx
= isl_ast_build_get_ctx(build
);
556 n
= isl_ast_graft_list_n_ast_graft(list
);
558 allow_else
= isl_options_get_ast_build_allow_else(ctx
);
562 if_node
= isl_alloc_array(ctx
, struct isl_if_node
, n
- 1);
564 return isl_ast_graft_list_free(list
);
567 res
= isl_ast_graft_list_alloc(ctx
, n
);
569 for (i
= 0; i
< n
; ++i
) {
571 isl_ast_graft
*graft
;
572 int subset
, found_then
, found_else
;
575 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
579 found_then
= found_else
= -1;
582 test
= isl_set_copy(graft
->guard
);
583 test
= isl_set_intersect(test
,
584 isl_set_copy(build
->domain
));
585 for (j
= n_if
- 1; j
>= 0; --j
) {
586 subset
= isl_set_is_subset(test
,
588 if (subset
< 0 || subset
) {
594 subset
= isl_set_is_subset(test
,
595 if_node
[j
].complement
);
596 if (subset
< 0 || subset
) {
601 n_if
= clear_if_nodes(if_node
, j
+ 1, n_if
);
605 graft
= isl_ast_graft_free(graft
);
609 guard
= isl_set_copy(graft
->guard
);
611 graft
->guard
= isl_set_gist(graft
->guard
,
612 isl_set_copy(if_node
[found_then
].guard
));
613 else if (found_else
>= 0)
614 graft
->guard
= isl_set_gist(graft
->guard
,
615 isl_set_copy(if_node
[found_else
].complement
));
619 graft
= isl_ast_graft_free(graft
);
620 graft
= insert_pending_guard_node(graft
, build
);
621 if (graft
&& graft
->node
!= node
&& i
!= n
- 1) {
623 if_node
[n_if
].node
= graft
->node
;
624 if_node
[n_if
].guard
= guard
;
626 set
= if_node
[found_then
].guard
;
627 else if (found_else
>= 0)
628 set
= if_node
[found_else
].complement
;
631 set
= isl_set_copy(set
);
632 set
= isl_set_subtract(set
, isl_set_copy(guard
));
633 if_node
[n_if
].complement
= set
;
641 res
= extend_then(res
, if_node
[found_then
].node
,
643 else if (found_else
>= 0)
644 res
= extend_else(res
, if_node
[found_else
].node
,
647 res
= isl_ast_graft_list_add(res
, graft
);
650 res
= isl_ast_graft_list_free(res
);
652 isl_ast_graft_list_free(list
);
653 clear_if_nodes(if_node
, 0, n_if
);
658 /* For each graft in "list",
659 * insert an if node around graft->node testing the condition encoded
660 * in graft->guard, assuming graft->guard involves any conditions.
661 * Subsequently remove the guards from the grafts.
663 __isl_give isl_ast_graft_list
*isl_ast_graft_list_insert_pending_guard_nodes(
664 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
669 list
= insert_pending_guard_nodes(list
, build
);
673 universe
= isl_set_universe(isl_ast_build_get_space(build
, 1));
674 n
= isl_ast_graft_list_n_ast_graft(list
);
675 for (i
= 0; i
< n
; ++i
) {
676 isl_ast_graft
*graft
;
678 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
681 isl_set_free(graft
->guard
);
682 graft
->guard
= isl_set_copy(universe
);
684 graft
= isl_ast_graft_free(graft
);
685 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
687 isl_set_free(universe
);
689 return isl_ast_graft_list_free(list
);
694 /* Collect the nodes contained in the grafts in "list" in a node list.
696 static __isl_give isl_ast_node_list
*extract_node_list(
697 __isl_keep isl_ast_graft_list
*list
)
701 isl_ast_node_list
*node_list
;
705 ctx
= isl_ast_graft_list_get_ctx(list
);
706 n
= isl_ast_graft_list_n_ast_graft(list
);
707 node_list
= isl_ast_node_list_alloc(ctx
, n
);
708 for (i
= 0; i
< n
; ++i
) {
710 isl_ast_graft
*graft
;
712 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
713 node
= isl_ast_graft_get_node(graft
);
714 node_list
= isl_ast_node_list_add(node_list
, node
);
715 isl_ast_graft_free(graft
);
721 /* Look for shared enforced constraints by all the elements in "list"
722 * on outer loops (with respect to the current depth) and return the result.
724 * If there are no elements in "list", then return the empty set.
726 __isl_give isl_basic_set
*isl_ast_graft_list_extract_shared_enforced(
727 __isl_keep isl_ast_graft_list
*list
,
728 __isl_keep isl_ast_build
*build
)
733 isl_basic_set
*enforced
;
738 space
= isl_ast_build_get_space(build
, 1);
739 enforced
= isl_basic_set_empty(space
);
741 depth
= isl_ast_build_get_depth(build
);
742 n
= isl_ast_graft_list_n_ast_graft(list
);
743 for (i
= 0; i
< n
; ++i
) {
744 isl_ast_graft
*graft
;
746 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
747 enforced
= update_enforced(enforced
, graft
, depth
);
748 isl_ast_graft_free(graft
);
754 /* Record "guard" in "graft" so that it will be enforced somewhere
755 * up the tree. If the graft already has a guard, then it may be partially
756 * redundant in combination with the new guard and in the context
757 * the generated constraints of "build". In fact, the new guard
758 * may in itself have some redundant constraints.
759 * We therefore (re)compute the gist of the intersection
760 * and coalesce the result.
762 static __isl_give isl_ast_graft
*store_guard(__isl_take isl_ast_graft
*graft
,
763 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
770 is_universe
= isl_set_plain_is_universe(guard
);
778 graft
->guard
= isl_set_intersect(graft
->guard
, guard
);
779 graft
->guard
= isl_set_gist(graft
->guard
,
780 isl_ast_build_get_generated(build
));
781 graft
->guard
= isl_set_coalesce(graft
->guard
);
783 return isl_ast_graft_free(graft
);
788 return isl_ast_graft_free(graft
);
791 /* For each graft in "list", replace its guard with the gist with
792 * respect to "context".
794 static __isl_give isl_ast_graft_list
*gist_guards(
795 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_set
*context
)
802 n
= isl_ast_graft_list_n_ast_graft(list
);
803 for (i
= 0; i
< n
; ++i
) {
804 isl_ast_graft
*graft
;
806 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
809 graft
->guard
= isl_set_gist(graft
->guard
,
810 isl_set_copy(context
));
812 graft
= isl_ast_graft_free(graft
);
813 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
816 return isl_ast_graft_list_free(list
);
821 /* For each graft in "list", replace its guard with the gist with
822 * respect to "context".
824 __isl_give isl_ast_graft_list
*isl_ast_graft_list_gist_guards(
825 __isl_take isl_ast_graft_list
*list
, __isl_take isl_set
*context
)
827 list
= gist_guards(list
, context
);
828 isl_set_free(context
);
833 /* Allocate a graft in "build" based on the list of grafts in "sub_build".
834 * "guard" and "enforced" are the guard and enforced constraints
835 * of the allocated graft. The guard is used to simplify the guards
836 * of the elements in "list".
838 * The node is initialized to either a block containing the nodes of "children"
839 * or, if there is only a single child, the node of that child.
840 * If the current level requires a for node, it should be inserted by
841 * a subsequent call to isl_ast_graft_insert_for.
843 __isl_give isl_ast_graft
*isl_ast_graft_alloc_from_children(
844 __isl_take isl_ast_graft_list
*list
, __isl_take isl_set
*guard
,
845 __isl_take isl_basic_set
*enforced
, __isl_keep isl_ast_build
*build
,
846 __isl_keep isl_ast_build
*sub_build
)
848 isl_ast_build
*guard_build
;
850 isl_ast_node_list
*node_list
;
851 isl_ast_graft
*graft
;
853 guard_build
= isl_ast_build_copy(sub_build
);
854 guard_build
= isl_ast_build_replace_pending_by_guard(guard_build
,
855 isl_set_copy(guard
));
856 list
= gist_guards(list
, guard
);
857 list
= insert_pending_guard_nodes(list
, guard_build
);
858 isl_ast_build_free(guard_build
);
860 node_list
= extract_node_list(list
);
861 node
= isl_ast_node_from_ast_node_list(node_list
);
862 isl_ast_graft_list_free(list
);
864 graft
= isl_ast_graft_alloc(node
, build
);
865 graft
= store_guard(graft
, guard
, build
);
866 graft
= isl_ast_graft_enforce(graft
, enforced
);
871 /* Combine the grafts in the list into a single graft.
873 * The guard is initialized to the shared guard of the list elements (if any),
874 * provided it does not depend on the current dimension.
875 * The guards in the elements are then simplified with respect to the
876 * hoisted guard and materialized as if nodes around the contained AST nodes
877 * in the context of "sub_build".
879 * The enforced set is initialized to the simple hull of the enforced sets
880 * of the elements, provided the ast_build_exploit_nested_bounds option is set
881 * or the new graft will be used at the same level.
883 * The node is initialized to either a block containing the nodes of "list"
884 * or, if there is only a single element, the node of that element.
886 static __isl_give isl_ast_graft
*ast_graft_list_fuse(
887 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
889 isl_ast_graft
*graft
;
890 isl_basic_set
*enforced
;
896 enforced
= isl_ast_graft_list_extract_shared_enforced(list
, build
);
897 guard
= isl_ast_graft_list_extract_hoistable_guard(list
, build
);
898 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
904 /* Combine the grafts in the list into a single graft.
905 * Return a list containing this single graft.
906 * If the original list is empty, then return an empty list.
908 __isl_give isl_ast_graft_list
*isl_ast_graft_list_fuse(
909 __isl_take isl_ast_graft_list
*list
,
910 __isl_keep isl_ast_build
*build
)
912 isl_ast_graft
*graft
;
916 if (isl_ast_graft_list_n_ast_graft(list
) <= 1)
918 graft
= ast_graft_list_fuse(list
, build
);
919 return isl_ast_graft_list_from_ast_graft(graft
);
922 /* Combine the two grafts into a single graft.
923 * Return a list containing this single graft.
925 static __isl_give isl_ast_graft
*isl_ast_graft_fuse(
926 __isl_take isl_ast_graft
*graft1
, __isl_take isl_ast_graft
*graft2
,
927 __isl_keep isl_ast_build
*build
)
930 isl_ast_graft_list
*list
;
932 ctx
= isl_ast_build_get_ctx(build
);
934 list
= isl_ast_graft_list_alloc(ctx
, 2);
935 list
= isl_ast_graft_list_add(list
, graft1
);
936 list
= isl_ast_graft_list_add(list
, graft2
);
938 return ast_graft_list_fuse(list
, build
);
941 /* Insert a for node enclosing the current graft->node.
943 __isl_give isl_ast_graft
*isl_ast_graft_insert_for(
944 __isl_take isl_ast_graft
*graft
, __isl_take isl_ast_node
*node
)
949 graft
->node
= isl_ast_node_for_set_body(node
, graft
->node
);
951 return isl_ast_graft_free(graft
);
955 isl_ast_node_free(node
);
956 isl_ast_graft_free(graft
);
960 /* Insert a mark governing the current graft->node.
962 __isl_give isl_ast_graft
*isl_ast_graft_insert_mark(
963 __isl_take isl_ast_graft
*graft
, __isl_take isl_id
*mark
)
968 graft
->node
= isl_ast_node_alloc_mark(mark
, graft
->node
);
970 return isl_ast_graft_free(graft
);
975 isl_ast_graft_free(graft
);
979 /* Represent the graft list as an AST node.
980 * This operation drops the information about guards in the grafts, so
981 * if there are any pending guards, then they are materialized as if nodes.
983 __isl_give isl_ast_node
*isl_ast_node_from_graft_list(
984 __isl_take isl_ast_graft_list
*list
,
985 __isl_keep isl_ast_build
*build
)
987 isl_ast_node_list
*node_list
;
989 list
= insert_pending_guard_nodes(list
, build
);
990 node_list
= extract_node_list(list
);
991 isl_ast_graft_list_free(list
);
993 return isl_ast_node_from_ast_node_list(node_list
);
996 __isl_null isl_ast_graft
*isl_ast_graft_free(__isl_take isl_ast_graft
*graft
)
1001 if (--graft
->ref
> 0)
1004 isl_ast_node_free(graft
->node
);
1005 isl_set_free(graft
->guard
);
1006 isl_basic_set_free(graft
->enforced
);
1012 /* Record that the grafted tree enforces
1013 * "enforced" by intersecting graft->enforced with "enforced".
1015 __isl_give isl_ast_graft
*isl_ast_graft_enforce(
1016 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
1018 if (!graft
|| !enforced
)
1021 enforced
= isl_basic_set_align_params(enforced
,
1022 isl_basic_set_get_space(graft
->enforced
));
1023 graft
->enforced
= isl_basic_set_align_params(graft
->enforced
,
1024 isl_basic_set_get_space(enforced
));
1025 graft
->enforced
= isl_basic_set_intersect(graft
->enforced
, enforced
);
1026 if (!graft
->enforced
)
1027 return isl_ast_graft_free(graft
);
1031 isl_basic_set_free(enforced
);
1032 return isl_ast_graft_free(graft
);
1035 __isl_give isl_basic_set
*isl_ast_graft_get_enforced(
1036 __isl_keep isl_ast_graft
*graft
)
1038 return graft
? isl_basic_set_copy(graft
->enforced
) : NULL
;
1041 __isl_give isl_set
*isl_ast_graft_get_guard(__isl_keep isl_ast_graft
*graft
)
1043 return graft
? isl_set_copy(graft
->guard
) : NULL
;
1046 /* Record that "guard" needs to be inserted in "graft".
1048 __isl_give isl_ast_graft
*isl_ast_graft_add_guard(
1049 __isl_take isl_ast_graft
*graft
,
1050 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
1052 return store_guard(graft
, guard
, build
);
1055 /* Reformulate the "graft", which was generated in the context
1056 * of an inner code generation, in terms of the outer code generation
1059 * If "product" is set, then the domain of the inner code generation build is
1063 * with O the domain of the outer code generation build.
1064 * We essentially need to project out S.
1066 * If "product" is not set, then we need to project the domains onto
1067 * their parameter spaces.
1069 __isl_give isl_ast_graft
*isl_ast_graft_unembed(__isl_take isl_ast_graft
*graft
,
1072 isl_basic_set
*enforced
;
1078 enforced
= graft
->enforced
;
1079 enforced
= isl_basic_map_domain(isl_basic_set_unwrap(enforced
));
1080 graft
->enforced
= enforced
;
1081 graft
->guard
= isl_map_domain(isl_set_unwrap(graft
->guard
));
1083 graft
->enforced
= isl_basic_set_params(graft
->enforced
);
1084 graft
->guard
= isl_set_params(graft
->guard
);
1086 graft
->guard
= isl_set_compute_divs(graft
->guard
);
1088 if (!graft
->enforced
|| !graft
->guard
)
1089 return isl_ast_graft_free(graft
);
1094 /* Reformulate the grafts in "list", which were generated in the context
1095 * of an inner code generation, in terms of the outer code generation
1098 __isl_give isl_ast_graft_list
*isl_ast_graft_list_unembed(
1099 __isl_take isl_ast_graft_list
*list
, int product
)
1103 n
= isl_ast_graft_list_n_ast_graft(list
);
1104 for (i
= 0; i
< n
; ++i
) {
1105 isl_ast_graft
*graft
;
1107 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
1108 graft
= isl_ast_graft_unembed(graft
, product
);
1109 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
1115 /* Compute the preimage of "graft" under the function represented by "ma".
1116 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
1118 __isl_give isl_ast_graft
*isl_ast_graft_preimage_multi_aff(
1119 __isl_take isl_ast_graft
*graft
, __isl_take isl_multi_aff
*ma
)
1121 isl_basic_set
*enforced
;
1126 enforced
= graft
->enforced
;
1127 graft
->enforced
= isl_basic_set_preimage_multi_aff(enforced
,
1128 isl_multi_aff_copy(ma
));
1129 graft
->guard
= isl_set_preimage_multi_aff(graft
->guard
, ma
);
1131 if (!graft
->enforced
|| !graft
->guard
)
1132 return isl_ast_graft_free(graft
);
1137 /* Compute the preimage of all the grafts in "list" under
1138 * the function represented by "ma".
1140 __isl_give isl_ast_graft_list
*isl_ast_graft_list_preimage_multi_aff(
1141 __isl_take isl_ast_graft_list
*list
, __isl_take isl_multi_aff
*ma
)
1145 n
= isl_ast_graft_list_n_ast_graft(list
);
1146 for (i
= 0; i
< n
; ++i
) {
1147 isl_ast_graft
*graft
;
1149 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
1150 graft
= isl_ast_graft_preimage_multi_aff(graft
,
1151 isl_multi_aff_copy(ma
));
1152 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
1155 isl_multi_aff_free(ma
);
1159 /* Compare two grafts based on their guards.
1161 static int cmp_graft(__isl_keep isl_ast_graft
*a
, __isl_keep isl_ast_graft
*b
,
1164 return isl_set_plain_cmp(a
->guard
, b
->guard
);
1167 /* Order the elements in "list" based on their guards.
1169 __isl_give isl_ast_graft_list
*isl_ast_graft_list_sort_guard(
1170 __isl_take isl_ast_graft_list
*list
)
1172 return isl_ast_graft_list_sort(list
, &cmp_graft
, NULL
);
1175 /* Merge the given two lists into a single list of grafts,
1176 * merging grafts with the same guard into a single graft.
1178 * "list2" has been sorted using isl_ast_graft_list_sort.
1179 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1180 * and may therefore not be completely sorted.
1182 * The elements in "list2" need to be executed after those in "list1",
1183 * but if the guard of a graft in "list2" is disjoint from the guards
1184 * of some final elements in "list1", then it can be moved up to before
1185 * those final elements.
1187 * In particular, we look at each element g of "list2" in turn
1188 * and move it up beyond elements of "list1" that would be sorted
1189 * after g as long as each of these elements has a guard that is disjoint
1192 * We do not allow the second or any later element of "list2" to be moved
1193 * before a previous elements of "list2" even if the reason that
1194 * that element didn't move up further was that its guard was not disjoint
1195 * from that of the previous element in "list1".
1197 __isl_give isl_ast_graft_list
*isl_ast_graft_list_merge(
1198 __isl_take isl_ast_graft_list
*list1
,
1199 __isl_take isl_ast_graft_list
*list2
,
1200 __isl_keep isl_ast_build
*build
)
1204 if (!list1
|| !list2
|| !build
)
1206 if (list2
->n
== 0) {
1207 isl_ast_graft_list_free(list2
);
1210 if (list1
->n
== 0) {
1211 isl_ast_graft_list_free(list1
);
1216 for (i
= 0; i
< list2
->n
; ++i
) {
1217 isl_ast_graft
*graft
;
1218 graft
= isl_ast_graft_list_get_ast_graft(list2
, i
);
1222 for (j
= list1
->n
; j
>= 0; --j
) {
1224 isl_ast_graft
*graft_j
;
1229 cmp
= isl_set_plain_cmp(list1
->p
[j
- 1]->guard
,
1232 disjoint
= isl_set_is_disjoint(graft
->guard
,
1233 list1
->p
[j
- 1]->guard
);
1235 isl_ast_graft_free(graft
);
1236 list1
= isl_ast_graft_list_free(list1
);
1245 list1
= isl_ast_graft_list_insert(list1
, j
,
1252 graft_j
= isl_ast_graft_list_get_ast_graft(list1
, j
);
1253 graft_j
= isl_ast_graft_fuse(graft_j
, graft
, build
);
1254 list1
= isl_ast_graft_list_set_ast_graft(list1
, j
,
1260 isl_ast_graft_free(graft
);
1261 isl_die(isl_ast_build_get_ctx(build
),
1263 "element failed to get inserted", break);
1271 list1
= isl_ast_graft_list_free(list1
);
1272 isl_ast_graft_list_free(list2
);
1276 isl_ast_graft_list_free(list1
);
1277 isl_ast_graft_list_free(list2
);
1281 __isl_give isl_printer
*isl_printer_print_ast_graft(__isl_take isl_printer
*p
,
1282 __isl_keep isl_ast_graft
*graft
)
1287 return isl_printer_free(p
);
1289 p
= isl_printer_print_str(p
, "(");
1290 p
= isl_printer_print_str(p
, "guard: ");
1291 p
= isl_printer_print_set(p
, graft
->guard
);
1292 p
= isl_printer_print_str(p
, ", ");
1293 p
= isl_printer_print_str(p
, "enforced: ");
1294 p
= isl_printer_print_basic_set(p
, graft
->enforced
);
1295 p
= isl_printer_print_str(p
, ", ");
1296 p
= isl_printer_print_str(p
, "node: ");
1297 p
= isl_printer_print_ast_node(p
, graft
->node
);
1298 p
= isl_printer_print_str(p
, ")");