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
13 #include <isl/space.h>
14 #include <isl_ast_private.h>
15 #include <isl_ast_build_expr.h>
16 #include <isl_ast_build_private.h>
17 #include <isl_ast_graft_private.h>
19 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
20 __isl_keep isl_ast_graft
*graft
);
23 #define BASE ast_graft
25 #include <isl_list_templ.c>
28 #define BASE ast_graft
29 #include <print_templ.c>
31 isl_ctx
*isl_ast_graft_get_ctx(__isl_keep isl_ast_graft
*graft
)
35 return isl_basic_set_get_ctx(graft
->enforced
);
38 __isl_give isl_ast_node
*isl_ast_graft_get_node(
39 __isl_keep isl_ast_graft
*graft
)
41 return graft
? isl_ast_node_copy(graft
->node
) : NULL
;
44 /* Create a graft for "node" with no guards and no enforced conditions.
46 __isl_give isl_ast_graft
*isl_ast_graft_alloc(
47 __isl_take isl_ast_node
*node
, __isl_keep isl_ast_build
*build
)
56 ctx
= isl_ast_node_get_ctx(node
);
57 graft
= isl_calloc_type(ctx
, isl_ast_graft
);
61 space
= isl_ast_build_get_space(build
, 1);
65 graft
->guard
= isl_set_universe(isl_space_copy(space
));
66 graft
->enforced
= isl_basic_set_universe(space
);
68 if (!graft
->guard
|| !graft
->enforced
)
69 return isl_ast_graft_free(graft
);
73 isl_ast_node_free(node
);
77 /* Create a graft with no guards and no enforced conditions
78 * encapsulating a call to the domain element specified by "executed".
79 * "executed" is assumed to be single-valued.
81 __isl_give isl_ast_graft
*isl_ast_graft_alloc_domain(
82 __isl_take isl_map
*executed
, __isl_keep isl_ast_build
*build
)
86 node
= isl_ast_build_call_from_executed(build
, executed
);
88 return isl_ast_graft_alloc(node
, build
);
91 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
92 __isl_keep isl_ast_graft
*graft
)
101 /* Do all the grafts in "list" have the same guard and is this guard
102 * independent of the current depth?
104 static int equal_independent_guards(__isl_keep isl_ast_graft_list
*list
,
105 __isl_keep isl_ast_build
*build
)
109 isl_ast_graft
*graft_0
;
113 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
117 depth
= isl_ast_build_get_depth(build
);
118 if (isl_set_dim(graft_0
->guard
, isl_dim_set
) <= depth
)
121 skip
= isl_set_involves_dims(graft_0
->guard
,
122 isl_dim_set
, depth
, 1);
123 if (skip
< 0 || skip
) {
124 isl_ast_graft_free(graft_0
);
125 return skip
< 0 ? -1 : 0;
128 n
= isl_ast_graft_list_n_ast_graft(list
);
129 for (i
= 1; i
< n
; ++i
) {
130 isl_ast_graft
*graft
;
131 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
135 equal
= isl_set_is_equal(graft_0
->guard
, graft
->guard
);
136 isl_ast_graft_free(graft
);
137 if (equal
< 0 || !equal
)
141 isl_ast_graft_free(graft_0
);
146 /* Hoist "guard" out of the current level (given by "build").
148 * In particular, eliminate the dimension corresponding to the current depth.
150 static __isl_give isl_set
*hoist_guard(__isl_take isl_set
*guard
,
151 __isl_keep isl_ast_build
*build
)
155 depth
= isl_ast_build_get_depth(build
);
156 if (depth
< isl_set_dim(guard
, isl_dim_set
)) {
157 guard
= isl_set_remove_divs_involving_dims(guard
,
158 isl_dim_set
, depth
, 1);
159 guard
= isl_set_eliminate(guard
, isl_dim_set
, depth
, 1);
160 guard
= isl_set_compute_divs(guard
);
166 /* Extract a common guard from the grafts in "list" that can be hoisted
167 * out of the current level. If no such guard can be found, then return
170 * If all the grafts in the list have the same guard and if this guard
171 * is independent of the current level, then it can be hoisted out.
172 * If there is only one graft in the list and if its guard
173 * depends on the current level, then we eliminate this level and
176 * Otherwise, we return the unshifted simple hull of the guards.
177 * In order to be able to hoist as many constraints as possible,
178 * but at the same time avoid hoisting constraints that did not
179 * appear in the guards in the first place, we intersect the guards
180 * with all the information that is available (i.e., the domain
181 * from the build and the enforced constraints of the graft) and
182 * compute the unshifted hull of the result using only constraints
183 * from the original guards.
184 * In particular, intersecting the guards with other known information
185 * allows us to hoist guards that are only explicit is some of
186 * the grafts and implicit in the others.
188 * The special case for equal guards is needed in case those guards
189 * are non-convex. Taking the simple hull would remove information
190 * and would not allow for these guards to be hoisted completely.
192 __isl_give isl_set
*isl_ast_graft_list_extract_hoistable_guard(
193 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
199 isl_set_list
*set_list
;
205 n
= isl_ast_graft_list_n_ast_graft(list
);
207 return isl_set_universe(isl_ast_build_get_space(build
, 1));
209 equal
= equal_independent_guards(list
, build
);
213 if (equal
|| n
== 1) {
214 isl_ast_graft
*graft_0
;
216 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
219 guard
= isl_set_copy(graft_0
->guard
);
221 guard
= hoist_guard(guard
, build
);
222 isl_ast_graft_free(graft_0
);
226 ctx
= isl_ast_build_get_ctx(build
);
227 set_list
= isl_set_list_alloc(ctx
, n
);
228 guard
= isl_set_empty(isl_ast_build_get_space(build
, 1));
229 for (i
= 0; i
< n
; ++i
) {
230 isl_ast_graft
*graft
;
231 isl_basic_set
*enforced
;
234 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
235 enforced
= isl_ast_graft_get_enforced(graft
);
236 guard_i
= isl_set_copy(graft
->guard
);
237 isl_ast_graft_free(graft
);
238 set_list
= isl_set_list_add(set_list
, isl_set_copy(guard_i
));
239 guard_i
= isl_set_intersect(guard_i
,
240 isl_set_from_basic_set(enforced
));
241 guard_i
= isl_set_intersect(guard_i
,
242 isl_ast_build_get_domain(build
));
243 guard
= isl_set_union(guard
, guard_i
);
245 hull
= isl_set_unshifted_simple_hull_from_set_list(guard
, set_list
);
246 guard
= isl_set_from_basic_set(hull
);
247 return hoist_guard(guard
, build
);
250 /* Internal data structure used inside insert_if.
252 * list is the list of guarded nodes created by each call to insert_if.
253 * node is the original node that is guarded by insert_if.
254 * build is the build in which the AST is constructed.
256 struct isl_insert_if_data
{
257 isl_ast_node_list
*list
;
259 isl_ast_build
*build
;
262 static isl_stat
insert_if(__isl_take isl_basic_set
*bset
, void *user
);
264 /* Insert an if node around "node" testing the condition encoded
267 * If the user does not want any disjunctions in the if conditions
268 * and if "guard" does involve a disjunction, then we make the different
269 * disjuncts disjoint and insert an if node corresponding to each disjunct
270 * around a copy of "node". The result is then a block node containing
271 * this sequence of guarded copies of "node".
273 static __isl_give isl_ast_node
*ast_node_insert_if(
274 __isl_take isl_ast_node
*node
, __isl_take isl_set
*guard
,
275 __isl_keep isl_ast_build
*build
)
277 struct isl_insert_if_data data
;
280 ctx
= isl_ast_build_get_ctx(build
);
281 if (isl_options_get_ast_build_allow_or(ctx
) ||
282 isl_set_n_basic_set(guard
) <= 1) {
283 isl_ast_node
*if_node
;
286 expr
= isl_ast_build_expr_from_set_internal(build
, guard
);
288 if_node
= isl_ast_node_alloc_if(expr
);
289 return isl_ast_node_if_set_then(if_node
, node
);
292 guard
= isl_set_make_disjoint(guard
);
294 data
.list
= isl_ast_node_list_alloc(ctx
, 0);
297 if (isl_set_foreach_basic_set(guard
, &insert_if
, &data
) < 0)
298 data
.list
= isl_ast_node_list_free(data
.list
);
301 isl_ast_node_free(data
.node
);
302 return isl_ast_node_alloc_block(data
.list
);
305 /* Insert an if node around a copy of "data->node" testing the condition
306 * encoded in guard "bset" and add the result to data->list.
308 static isl_stat
insert_if(__isl_take isl_basic_set
*bset
, void *user
)
310 struct isl_insert_if_data
*data
= user
;
314 set
= isl_set_from_basic_set(bset
);
315 node
= isl_ast_node_copy(data
->node
);
316 node
= ast_node_insert_if(node
, set
, data
->build
);
317 data
->list
= isl_ast_node_list_add(data
->list
, node
);
322 /* Insert an if node around graft->node testing the condition encoded
323 * in guard "guard", assuming guard involves any conditions.
325 static __isl_give isl_ast_graft
*insert_if_node(
326 __isl_take isl_ast_graft
*graft
, __isl_take isl_set
*guard
,
327 __isl_keep isl_ast_build
*build
)
334 univ
= isl_set_plain_is_universe(guard
);
342 build
= isl_ast_build_copy(build
);
343 graft
->node
= ast_node_insert_if(graft
->node
, guard
, build
);
344 isl_ast_build_free(build
);
347 return isl_ast_graft_free(graft
);
352 return isl_ast_graft_free(graft
);
355 /* Insert an if node around graft->node testing the condition encoded
356 * in graft->guard, assuming graft->guard involves any conditions.
358 static __isl_give isl_ast_graft
*insert_pending_guard_node(
359 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
364 return insert_if_node(graft
, isl_set_copy(graft
->guard
), build
);
367 /* Replace graft->enforced by "enforced".
369 __isl_give isl_ast_graft
*isl_ast_graft_set_enforced(
370 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
372 if (!graft
|| !enforced
)
375 isl_basic_set_free(graft
->enforced
);
376 graft
->enforced
= enforced
;
380 isl_basic_set_free(enforced
);
381 return isl_ast_graft_free(graft
);
384 /* Update "enforced" such that it only involves constraints that are
385 * also enforced by "graft".
387 static __isl_give isl_basic_set
*update_enforced(
388 __isl_take isl_basic_set
*enforced
, __isl_keep isl_ast_graft
*graft
,
391 isl_basic_set
*enforced_g
;
393 enforced_g
= isl_ast_graft_get_enforced(graft
);
394 if (depth
< isl_basic_set_dim(enforced_g
, isl_dim_set
))
395 enforced_g
= isl_basic_set_eliminate(enforced_g
,
396 isl_dim_set
, depth
, 1);
397 enforced_g
= isl_basic_set_remove_unknown_divs(enforced_g
);
398 enforced_g
= isl_basic_set_align_params(enforced_g
,
399 isl_basic_set_get_space(enforced
));
400 enforced
= isl_basic_set_align_params(enforced
,
401 isl_basic_set_get_space(enforced_g
));
402 enforced
= isl_set_simple_hull(isl_basic_set_union(enforced
,
408 /* Extend the node at *body with node.
410 * If body points to the else branch, then *body may still be NULL.
411 * If so, we simply attach node to this else branch.
412 * Otherwise, we attach a list containing the statements already
413 * attached at *body followed by node.
415 static void extend_body(__isl_keep isl_ast_node
**body
,
416 __isl_take isl_ast_node
*node
)
418 isl_ast_node_list
*list
;
425 if ((*body
)->type
== isl_ast_node_block
) {
426 list
= isl_ast_node_block_get_children(*body
);
427 isl_ast_node_free(*body
);
429 list
= isl_ast_node_list_from_ast_node(*body
);
430 list
= isl_ast_node_list_add(list
, node
);
431 *body
= isl_ast_node_alloc_block(list
);
434 /* Merge "graft" into the last graft of "list".
435 * body points to the then or else branch of an if node in that last graft.
437 * We attach graft->node to this branch and update the enforced
438 * set of the last graft of "list" to take into account the enforced
441 static __isl_give isl_ast_graft_list
*graft_extend_body(
442 __isl_take isl_ast_graft_list
*list
,
443 __isl_keep isl_ast_node
**body
, __isl_take isl_ast_graft
*graft
,
444 __isl_keep isl_ast_build
*build
)
450 isl_basic_set
*enforced
;
454 extend_body(body
, isl_ast_node_copy(graft
->node
));
458 n
= isl_ast_graft_list_n_ast_graft(list
);
459 last
= isl_ast_graft_list_get_ast_graft(list
, n
- 1);
461 depth
= isl_ast_build_get_depth(build
);
462 space
= isl_ast_build_get_space(build
, 1);
463 enforced
= isl_basic_set_empty(space
);
464 enforced
= update_enforced(enforced
, last
, depth
);
465 enforced
= update_enforced(enforced
, graft
, depth
);
466 last
= isl_ast_graft_set_enforced(last
, enforced
);
468 list
= isl_ast_graft_list_set_ast_graft(list
, n
- 1, last
);
469 isl_ast_graft_free(graft
);
472 isl_ast_graft_free(graft
);
473 return isl_ast_graft_list_free(list
);
476 /* Merge "graft" into the last graft of "list", attaching graft->node
477 * to the then branch of "last_if".
479 static __isl_give isl_ast_graft_list
*extend_then(
480 __isl_take isl_ast_graft_list
*list
,
481 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
482 __isl_keep isl_ast_build
*build
)
484 return graft_extend_body(list
, &last_if
->u
.i
.then
, graft
, build
);
487 /* Merge "graft" into the last graft of "list", attaching graft->node
488 * to the else branch of "last_if".
490 static __isl_give isl_ast_graft_list
*extend_else(
491 __isl_take isl_ast_graft_list
*list
,
492 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
493 __isl_keep isl_ast_build
*build
)
495 return graft_extend_body(list
, &last_if
->u
.i
.else_node
, graft
, build
);
498 /* This data structure keeps track of an if node.
500 * "node" is the actual if-node
501 * "guard" is the original, non-simplified guard of the node
502 * "complement" is the complement of "guard" in the context of outer if nodes
510 /* Given a list of "n" if nodes, clear those starting at "first"
511 * and return "first" (i.e., the updated size of the array).
513 static int clear_if_nodes(struct isl_if_node
*if_node
, int first
, int n
)
517 for (i
= first
; i
< n
; ++i
) {
518 isl_set_free(if_node
[i
].guard
);
519 isl_set_free(if_node
[i
].complement
);
525 /* For each graft in "list",
526 * insert an if node around graft->node testing the condition encoded
527 * in graft->guard, assuming graft->guard involves any conditions.
529 * We keep track of a list of generated if nodes that can be extended
530 * without changing the order of the elements in "list".
531 * If the guard of a graft is a subset of either the guard or its complement
532 * of one of those if nodes, then the node
533 * of the new graft is inserted into the then or else branch of the last graft
534 * and the current graft is discarded.
535 * The guard of the node is then simplified based on the conditions
536 * enforced at that then or else branch.
537 * Otherwise, the current graft is appended to the list.
539 * We only construct else branches if allowed by the user.
541 static __isl_give isl_ast_graft_list
*insert_pending_guard_nodes(
542 __isl_take isl_ast_graft_list
*list
,
543 __isl_keep isl_ast_build
*build
)
548 isl_ast_graft_list
*res
;
549 struct isl_if_node
*if_node
= NULL
;
552 return isl_ast_graft_list_free(list
);
554 ctx
= isl_ast_build_get_ctx(build
);
555 n
= isl_ast_graft_list_n_ast_graft(list
);
557 allow_else
= isl_options_get_ast_build_allow_else(ctx
);
561 if_node
= isl_alloc_array(ctx
, struct isl_if_node
, n
- 1);
563 return isl_ast_graft_list_free(list
);
566 res
= isl_ast_graft_list_alloc(ctx
, n
);
568 for (i
= 0; i
< n
; ++i
) {
570 isl_ast_graft
*graft
;
571 int subset
, found_then
, found_else
;
574 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
578 found_then
= found_else
= -1;
581 test
= isl_set_copy(graft
->guard
);
582 test
= isl_set_intersect(test
,
583 isl_set_copy(build
->domain
));
584 for (j
= n_if
- 1; j
>= 0; --j
) {
585 subset
= isl_set_is_subset(test
,
587 if (subset
< 0 || subset
) {
593 subset
= isl_set_is_subset(test
,
594 if_node
[j
].complement
);
595 if (subset
< 0 || subset
) {
600 n_if
= clear_if_nodes(if_node
, j
+ 1, n_if
);
604 graft
= isl_ast_graft_free(graft
);
608 guard
= isl_set_copy(graft
->guard
);
610 graft
->guard
= isl_set_gist(graft
->guard
,
611 isl_set_copy(if_node
[found_then
].guard
));
612 else if (found_else
>= 0)
613 graft
->guard
= isl_set_gist(graft
->guard
,
614 isl_set_copy(if_node
[found_else
].complement
));
618 graft
= isl_ast_graft_free(graft
);
619 graft
= insert_pending_guard_node(graft
, build
);
620 if (graft
&& graft
->node
!= node
&& i
!= n
- 1) {
622 if_node
[n_if
].node
= graft
->node
;
623 if_node
[n_if
].guard
= guard
;
625 set
= if_node
[found_then
].guard
;
626 else if (found_else
>= 0)
627 set
= if_node
[found_else
].complement
;
630 set
= isl_set_copy(set
);
631 set
= isl_set_subtract(set
, isl_set_copy(guard
));
632 if_node
[n_if
].complement
= set
;
640 res
= extend_then(res
, if_node
[found_then
].node
,
642 else if (found_else
>= 0)
643 res
= extend_else(res
, if_node
[found_else
].node
,
646 res
= isl_ast_graft_list_add(res
, graft
);
649 res
= isl_ast_graft_list_free(res
);
651 isl_ast_graft_list_free(list
);
652 clear_if_nodes(if_node
, 0, n_if
);
657 /* For each graft in "list",
658 * insert an if node around graft->node testing the condition encoded
659 * in graft->guard, assuming graft->guard involves any conditions.
660 * Subsequently remove the guards from the grafts.
662 __isl_give isl_ast_graft_list
*isl_ast_graft_list_insert_pending_guard_nodes(
663 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
668 list
= insert_pending_guard_nodes(list
, build
);
672 universe
= isl_set_universe(isl_ast_build_get_space(build
, 1));
673 n
= isl_ast_graft_list_n_ast_graft(list
);
674 for (i
= 0; i
< n
; ++i
) {
675 isl_ast_graft
*graft
;
677 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
680 isl_set_free(graft
->guard
);
681 graft
->guard
= isl_set_copy(universe
);
683 graft
= isl_ast_graft_free(graft
);
684 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
686 isl_set_free(universe
);
688 return isl_ast_graft_list_free(list
);
693 /* Collect the nodes contained in the grafts in "list" in a node list.
695 static __isl_give isl_ast_node_list
*extract_node_list(
696 __isl_keep isl_ast_graft_list
*list
)
700 isl_ast_node_list
*node_list
;
704 ctx
= isl_ast_graft_list_get_ctx(list
);
705 n
= isl_ast_graft_list_n_ast_graft(list
);
706 node_list
= isl_ast_node_list_alloc(ctx
, n
);
707 for (i
= 0; i
< n
; ++i
) {
709 isl_ast_graft
*graft
;
711 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
712 node
= isl_ast_graft_get_node(graft
);
713 node_list
= isl_ast_node_list_add(node_list
, node
);
714 isl_ast_graft_free(graft
);
720 /* Look for shared enforced constraints by all the elements in "list"
721 * on outer loops (with respect to the current depth) and return the result.
723 * If there are no elements in "list", then return the empty set.
725 __isl_give isl_basic_set
*isl_ast_graft_list_extract_shared_enforced(
726 __isl_keep isl_ast_graft_list
*list
,
727 __isl_keep isl_ast_build
*build
)
732 isl_basic_set
*enforced
;
737 space
= isl_ast_build_get_space(build
, 1);
738 enforced
= isl_basic_set_empty(space
);
740 depth
= isl_ast_build_get_depth(build
);
741 n
= isl_ast_graft_list_n_ast_graft(list
);
742 for (i
= 0; i
< n
; ++i
) {
743 isl_ast_graft
*graft
;
745 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
746 enforced
= update_enforced(enforced
, graft
, depth
);
747 isl_ast_graft_free(graft
);
753 /* Record "guard" in "graft" so that it will be enforced somewhere
754 * up the tree. If the graft already has a guard, then it may be partially
755 * redundant in combination with the new guard and in the context
756 * the generated constraints of "build". In fact, the new guard
757 * may in itself have some redundant constraints.
758 * We therefore (re)compute the gist of the intersection
759 * and coalesce the result.
761 static __isl_give isl_ast_graft
*store_guard(__isl_take isl_ast_graft
*graft
,
762 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
769 is_universe
= isl_set_plain_is_universe(guard
);
777 graft
->guard
= isl_set_intersect(graft
->guard
, guard
);
778 graft
->guard
= isl_set_gist(graft
->guard
,
779 isl_ast_build_get_generated(build
));
780 graft
->guard
= isl_set_coalesce(graft
->guard
);
782 return isl_ast_graft_free(graft
);
787 return isl_ast_graft_free(graft
);
790 /* For each graft in "list", replace its guard with the gist with
791 * respect to "context".
793 static __isl_give isl_ast_graft_list
*gist_guards(
794 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_set
*context
)
801 n
= isl_ast_graft_list_n_ast_graft(list
);
802 for (i
= 0; i
< n
; ++i
) {
803 isl_ast_graft
*graft
;
805 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
808 graft
->guard
= isl_set_gist(graft
->guard
,
809 isl_set_copy(context
));
811 graft
= isl_ast_graft_free(graft
);
812 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
815 return isl_ast_graft_list_free(list
);
820 /* For each graft in "list", replace its guard with the gist with
821 * respect to "context".
823 __isl_give isl_ast_graft_list
*isl_ast_graft_list_gist_guards(
824 __isl_take isl_ast_graft_list
*list
, __isl_take isl_set
*context
)
826 list
= gist_guards(list
, context
);
827 isl_set_free(context
);
832 /* Allocate a graft in "build" based on the list of grafts in "sub_build".
833 * "guard" and "enforced" are the guard and enforced constraints
834 * of the allocated graft. The guard is used to simplify the guards
835 * of the elements in "list".
837 * The node is initialized to either a block containing the nodes of "children"
838 * or, if there is only a single child, the node of that child.
839 * If the current level requires a for node, it should be inserted by
840 * a subsequent call to isl_ast_graft_insert_for.
842 __isl_give isl_ast_graft
*isl_ast_graft_alloc_from_children(
843 __isl_take isl_ast_graft_list
*list
, __isl_take isl_set
*guard
,
844 __isl_take isl_basic_set
*enforced
, __isl_keep isl_ast_build
*build
,
845 __isl_keep isl_ast_build
*sub_build
)
847 isl_ast_build
*guard_build
;
849 isl_ast_node_list
*node_list
;
850 isl_ast_graft
*graft
;
852 guard_build
= isl_ast_build_copy(sub_build
);
853 guard_build
= isl_ast_build_replace_pending_by_guard(guard_build
,
854 isl_set_copy(guard
));
855 list
= gist_guards(list
, guard
);
856 list
= insert_pending_guard_nodes(list
, guard_build
);
857 isl_ast_build_free(guard_build
);
859 node_list
= extract_node_list(list
);
860 node
= isl_ast_node_from_ast_node_list(node_list
);
861 isl_ast_graft_list_free(list
);
863 graft
= isl_ast_graft_alloc(node
, build
);
864 graft
= store_guard(graft
, guard
, build
);
865 graft
= isl_ast_graft_enforce(graft
, enforced
);
870 /* Combine the grafts in the list into a single graft.
872 * The guard is initialized to the shared guard of the list elements (if any),
873 * provided it does not depend on the current dimension.
874 * The guards in the elements are then simplified with respect to the
875 * hoisted guard and materialized as if nodes around the contained AST nodes
876 * in the context of "sub_build".
878 * The enforced set is initialized to the simple hull of the enforced sets
879 * of the elements, provided the ast_build_exploit_nested_bounds option is set
880 * or the new graft will be used at the same level.
882 * The node is initialized to either a block containing the nodes of "list"
883 * or, if there is only a single element, the node of that element.
885 static __isl_give isl_ast_graft
*ast_graft_list_fuse(
886 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
888 isl_ast_graft
*graft
;
889 isl_basic_set
*enforced
;
895 enforced
= isl_ast_graft_list_extract_shared_enforced(list
, build
);
896 guard
= isl_ast_graft_list_extract_hoistable_guard(list
, build
);
897 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
903 /* Combine the grafts in the list into a single graft.
904 * Return a list containing this single graft.
905 * If the original list is empty, then return an empty list.
907 __isl_give isl_ast_graft_list
*isl_ast_graft_list_fuse(
908 __isl_take isl_ast_graft_list
*list
,
909 __isl_keep isl_ast_build
*build
)
911 isl_ast_graft
*graft
;
915 if (isl_ast_graft_list_n_ast_graft(list
) <= 1)
917 graft
= ast_graft_list_fuse(list
, build
);
918 return isl_ast_graft_list_from_ast_graft(graft
);
921 /* Combine the two grafts into a single graft.
922 * Return a list containing this single graft.
924 static __isl_give isl_ast_graft
*isl_ast_graft_fuse(
925 __isl_take isl_ast_graft
*graft1
, __isl_take isl_ast_graft
*graft2
,
926 __isl_keep isl_ast_build
*build
)
929 isl_ast_graft_list
*list
;
931 ctx
= isl_ast_build_get_ctx(build
);
933 list
= isl_ast_graft_list_alloc(ctx
, 2);
934 list
= isl_ast_graft_list_add(list
, graft1
);
935 list
= isl_ast_graft_list_add(list
, graft2
);
937 return ast_graft_list_fuse(list
, build
);
940 /* Insert a for node enclosing the current graft->node.
942 __isl_give isl_ast_graft
*isl_ast_graft_insert_for(
943 __isl_take isl_ast_graft
*graft
, __isl_take isl_ast_node
*node
)
948 graft
->node
= isl_ast_node_for_set_body(node
, graft
->node
);
950 return isl_ast_graft_free(graft
);
954 isl_ast_node_free(node
);
955 isl_ast_graft_free(graft
);
959 /* Insert a mark governing the current graft->node.
961 __isl_give isl_ast_graft
*isl_ast_graft_insert_mark(
962 __isl_take isl_ast_graft
*graft
, __isl_take isl_id
*mark
)
967 graft
->node
= isl_ast_node_alloc_mark(mark
, graft
->node
);
969 return isl_ast_graft_free(graft
);
974 isl_ast_graft_free(graft
);
978 /* Represent the graft list as an AST node.
979 * This operation drops the information about guards in the grafts, so
980 * if there are any pending guards, then they are materialized as if nodes.
982 __isl_give isl_ast_node
*isl_ast_node_from_graft_list(
983 __isl_take isl_ast_graft_list
*list
,
984 __isl_keep isl_ast_build
*build
)
986 isl_ast_node_list
*node_list
;
988 list
= insert_pending_guard_nodes(list
, build
);
989 node_list
= extract_node_list(list
);
990 isl_ast_graft_list_free(list
);
992 return isl_ast_node_from_ast_node_list(node_list
);
995 void *isl_ast_graft_free(__isl_take isl_ast_graft
*graft
)
1000 if (--graft
->ref
> 0)
1003 isl_ast_node_free(graft
->node
);
1004 isl_set_free(graft
->guard
);
1005 isl_basic_set_free(graft
->enforced
);
1011 /* Record that the grafted tree enforces
1012 * "enforced" by intersecting graft->enforced with "enforced".
1014 __isl_give isl_ast_graft
*isl_ast_graft_enforce(
1015 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
1017 if (!graft
|| !enforced
)
1020 enforced
= isl_basic_set_align_params(enforced
,
1021 isl_basic_set_get_space(graft
->enforced
));
1022 graft
->enforced
= isl_basic_set_align_params(graft
->enforced
,
1023 isl_basic_set_get_space(enforced
));
1024 graft
->enforced
= isl_basic_set_intersect(graft
->enforced
, enforced
);
1025 if (!graft
->enforced
)
1026 return isl_ast_graft_free(graft
);
1030 isl_basic_set_free(enforced
);
1031 return isl_ast_graft_free(graft
);
1034 __isl_give isl_basic_set
*isl_ast_graft_get_enforced(
1035 __isl_keep isl_ast_graft
*graft
)
1037 return graft
? isl_basic_set_copy(graft
->enforced
) : NULL
;
1040 __isl_give isl_set
*isl_ast_graft_get_guard(__isl_keep isl_ast_graft
*graft
)
1042 return graft
? isl_set_copy(graft
->guard
) : NULL
;
1045 /* Record that "guard" needs to be inserted in "graft".
1047 __isl_give isl_ast_graft
*isl_ast_graft_add_guard(
1048 __isl_take isl_ast_graft
*graft
,
1049 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
1051 return store_guard(graft
, guard
, build
);
1054 /* Reformulate the "graft", which was generated in the context
1055 * of an inner code generation, in terms of the outer code generation
1058 * If "product" is set, then the domain of the inner code generation build is
1062 * with O the domain of the outer code generation build.
1063 * We essentially need to project out S.
1065 * If "product" is not set, then we need to project the domains onto
1066 * their parameter spaces.
1068 __isl_give isl_ast_graft
*isl_ast_graft_unembed(__isl_take isl_ast_graft
*graft
,
1071 isl_basic_set
*enforced
;
1077 enforced
= graft
->enforced
;
1078 enforced
= isl_basic_map_domain(isl_basic_set_unwrap(enforced
));
1079 graft
->enforced
= enforced
;
1080 graft
->guard
= isl_map_domain(isl_set_unwrap(graft
->guard
));
1082 graft
->enforced
= isl_basic_set_params(graft
->enforced
);
1083 graft
->guard
= isl_set_params(graft
->guard
);
1085 graft
->guard
= isl_set_compute_divs(graft
->guard
);
1087 if (!graft
->enforced
|| !graft
->guard
)
1088 return isl_ast_graft_free(graft
);
1093 /* Reformulate the grafts in "list", which were generated in the context
1094 * of an inner code generation, in terms of the outer code generation
1097 __isl_give isl_ast_graft_list
*isl_ast_graft_list_unembed(
1098 __isl_take isl_ast_graft_list
*list
, int product
)
1102 n
= isl_ast_graft_list_n_ast_graft(list
);
1103 for (i
= 0; i
< n
; ++i
) {
1104 isl_ast_graft
*graft
;
1106 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
1107 graft
= isl_ast_graft_unembed(graft
, product
);
1108 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
1114 /* Compute the preimage of "graft" under the function represented by "ma".
1115 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
1117 __isl_give isl_ast_graft
*isl_ast_graft_preimage_multi_aff(
1118 __isl_take isl_ast_graft
*graft
, __isl_take isl_multi_aff
*ma
)
1120 isl_basic_set
*enforced
;
1125 enforced
= graft
->enforced
;
1126 graft
->enforced
= isl_basic_set_preimage_multi_aff(enforced
,
1127 isl_multi_aff_copy(ma
));
1128 graft
->guard
= isl_set_preimage_multi_aff(graft
->guard
, ma
);
1130 if (!graft
->enforced
|| !graft
->guard
)
1131 return isl_ast_graft_free(graft
);
1136 /* Compute the preimage of all the grafts in "list" under
1137 * the function represented by "ma".
1139 __isl_give isl_ast_graft_list
*isl_ast_graft_list_preimage_multi_aff(
1140 __isl_take isl_ast_graft_list
*list
, __isl_take isl_multi_aff
*ma
)
1144 n
= isl_ast_graft_list_n_ast_graft(list
);
1145 for (i
= 0; i
< n
; ++i
) {
1146 isl_ast_graft
*graft
;
1148 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
1149 graft
= isl_ast_graft_preimage_multi_aff(graft
,
1150 isl_multi_aff_copy(ma
));
1151 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
1154 isl_multi_aff_free(ma
);
1158 /* Compare two grafts based on their guards.
1160 static int cmp_graft(__isl_keep isl_ast_graft
*a
, __isl_keep isl_ast_graft
*b
,
1163 return isl_set_plain_cmp(a
->guard
, b
->guard
);
1166 /* Order the elements in "list" based on their guards.
1168 __isl_give isl_ast_graft_list
*isl_ast_graft_list_sort_guard(
1169 __isl_take isl_ast_graft_list
*list
)
1171 return isl_ast_graft_list_sort(list
, &cmp_graft
, NULL
);
1174 /* Merge the given two lists into a single list of grafts,
1175 * merging grafts with the same guard into a single graft.
1177 * "list2" has been sorted using isl_ast_graft_list_sort.
1178 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1179 * and may therefore not be completely sorted.
1181 * The elements in "list2" need to be executed after those in "list1",
1182 * but if the guard of a graft in "list2" is disjoint from the guards
1183 * of some final elements in "list1", then it can be moved up to before
1184 * those final elements.
1186 * In particular, we look at each element g of "list2" in turn
1187 * and move it up beyond elements of "list1" that would be sorted
1188 * after g as long as each of these elements has a guard that is disjoint
1191 * We do not allow the second or any later element of "list2" to be moved
1192 * before a previous elements of "list2" even if the reason that
1193 * that element didn't move up further was that its guard was not disjoint
1194 * from that of the previous element in "list1".
1196 __isl_give isl_ast_graft_list
*isl_ast_graft_list_merge(
1197 __isl_take isl_ast_graft_list
*list1
,
1198 __isl_take isl_ast_graft_list
*list2
,
1199 __isl_keep isl_ast_build
*build
)
1203 if (!list1
|| !list2
|| !build
)
1205 if (list2
->n
== 0) {
1206 isl_ast_graft_list_free(list2
);
1209 if (list1
->n
== 0) {
1210 isl_ast_graft_list_free(list1
);
1215 for (i
= 0; i
< list2
->n
; ++i
) {
1216 isl_ast_graft
*graft
;
1217 graft
= isl_ast_graft_list_get_ast_graft(list2
, i
);
1221 for (j
= list1
->n
; j
>= 0; --j
) {
1223 isl_ast_graft
*graft_j
;
1228 cmp
= isl_set_plain_cmp(list1
->p
[j
- 1]->guard
,
1231 disjoint
= isl_set_is_disjoint(graft
->guard
,
1232 list1
->p
[j
- 1]->guard
);
1234 list1
= isl_ast_graft_list_free(list1
);
1243 list1
= isl_ast_graft_list_insert(list1
, j
,
1250 graft_j
= isl_ast_graft_list_get_ast_graft(list1
, j
);
1251 graft_j
= isl_ast_graft_fuse(graft_j
, graft
, build
);
1252 list1
= isl_ast_graft_list_set_ast_graft(list1
, j
,
1258 isl_die(isl_ast_build_get_ctx(build
),
1260 "element failed to get inserted", break);
1267 list1
= isl_ast_graft_list_free(list1
);
1268 isl_ast_graft_list_free(list2
);
1272 isl_ast_graft_list_free(list1
);
1273 isl_ast_graft_list_free(list2
);
1277 __isl_give isl_printer
*isl_printer_print_ast_graft(__isl_take isl_printer
*p
,
1278 __isl_keep isl_ast_graft
*graft
)
1283 return isl_printer_free(p
);
1285 p
= isl_printer_print_str(p
, "(");
1286 p
= isl_printer_print_str(p
, "guard: ");
1287 p
= isl_printer_print_set(p
, graft
->guard
);
1288 p
= isl_printer_print_str(p
, ", ");
1289 p
= isl_printer_print_str(p
, "enforced: ");
1290 p
= isl_printer_print_basic_set(p
, graft
->enforced
);
1291 p
= isl_printer_print_str(p
, ", ");
1292 p
= isl_printer_print_str(p
, "node: ");
1293 p
= isl_printer_print_ast_node(p
, graft
->node
);
1294 p
= isl_printer_print_str(p
, ")");