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_ast_private.h>
14 #include <isl_ast_build_expr.h>
15 #include <isl_ast_build_private.h>
16 #include <isl_ast_graft_private.h>
18 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
19 __isl_keep isl_ast_graft
*graft
);
22 #define BASE ast_graft
24 #include <isl_list_templ.c>
27 #define BASE ast_graft
28 #include <print_templ.c>
30 isl_ctx
*isl_ast_graft_get_ctx(__isl_keep isl_ast_graft
*graft
)
34 return isl_basic_set_get_ctx(graft
->enforced
);
37 __isl_give isl_ast_node
*isl_ast_graft_get_node(
38 __isl_keep isl_ast_graft
*graft
)
40 return graft
? isl_ast_node_copy(graft
->node
) : NULL
;
43 /* Create a graft for "node" with no guards and no enforced conditions.
45 __isl_give isl_ast_graft
*isl_ast_graft_alloc(
46 __isl_take isl_ast_node
*node
, __isl_keep isl_ast_build
*build
)
55 ctx
= isl_ast_node_get_ctx(node
);
56 graft
= isl_calloc_type(ctx
, isl_ast_graft
);
60 space
= isl_ast_build_get_space(build
, 1);
64 graft
->guard
= isl_set_universe(isl_space_copy(space
));
65 graft
->enforced
= isl_basic_set_universe(space
);
67 if (!graft
->guard
|| !graft
->enforced
)
68 return isl_ast_graft_free(graft
);
72 isl_ast_node_free(node
);
76 /* Create a graft with no guards and no enforced conditions
77 * encapsulating a call to the domain element specified by "executed".
78 * "executed" is assumed to be single-valued.
80 __isl_give isl_ast_graft
*isl_ast_graft_alloc_domain(
81 __isl_take isl_map
*executed
, __isl_keep isl_ast_build
*build
)
85 node
= isl_ast_build_call_from_executed(build
, executed
);
87 return isl_ast_graft_alloc(node
, build
);
90 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
91 __isl_keep isl_ast_graft
*graft
)
100 /* Do all the grafts in "list" have the same guard and is this guard
101 * independent of the current depth?
103 static int equal_independent_guards(__isl_keep isl_ast_graft_list
*list
,
104 __isl_keep isl_ast_build
*build
)
108 isl_ast_graft
*graft_0
;
112 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
116 depth
= isl_ast_build_get_depth(build
);
117 skip
= isl_set_involves_dims(graft_0
->guard
, isl_dim_set
, depth
, 1);
118 if (skip
< 0 || skip
) {
119 isl_ast_graft_free(graft_0
);
120 return skip
< 0 ? -1 : 0;
123 n
= isl_ast_graft_list_n_ast_graft(list
);
124 for (i
= 1; i
< n
; ++i
) {
125 isl_ast_graft
*graft
;
126 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
130 equal
= isl_set_is_equal(graft_0
->guard
, graft
->guard
);
131 isl_ast_graft_free(graft
);
132 if (equal
< 0 || !equal
)
136 isl_ast_graft_free(graft_0
);
141 /* Hoist "guard" out of the current level (given by "build").
143 * In particular, eliminate the dimension corresponding to the current depth.
145 static __isl_give isl_set
*hoist_guard(__isl_take isl_set
*guard
,
146 __isl_keep isl_ast_build
*build
)
150 depth
= isl_ast_build_get_depth(build
);
151 if (depth
< isl_set_dim(guard
, isl_dim_set
)) {
152 guard
= isl_set_remove_divs_involving_dims(guard
,
153 isl_dim_set
, depth
, 1);
154 guard
= isl_set_eliminate(guard
, isl_dim_set
, depth
, 1);
155 guard
= isl_set_compute_divs(guard
);
161 /* Extract a common guard from the grafts in "list" that can be hoisted
162 * out of the current level. If no such guard can be found, then return
165 * If all the grafts in the list have the same guard and if this guard
166 * is independent of the current level, then it can be hoisted out.
167 * If there is only one graft in the list and if its guard
168 * depends on the current level, then we eliminate this level and
171 * Otherwise, we return the unshifted simple hull of the guards.
172 * In order to be able to hoist as many constraints as possible,
173 * but at the same time avoid hoisting constraints that did not
174 * appear in the guards in the first place, we intersect the guards
175 * with all the information that is available (i.e., the domain
176 * from the build and the enforced constraints of the graft) and
177 * compute the unshifted hull of the result using only constraints
178 * from the original guards.
179 * In particular, intersecting the guards with other known information
180 * allows us to hoist guards that are only explicit is some of
181 * the grafts and implicit in the others.
183 * The special case for equal guards is needed in case those guards
184 * are non-convex. Taking the simple hull would remove information
185 * and would not allow for these guards to be hoisted completely.
187 __isl_give isl_set
*isl_ast_graft_list_extract_hoistable_guard(
188 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
194 isl_set_list
*set_list
;
200 n
= isl_ast_graft_list_n_ast_graft(list
);
202 return isl_set_universe(isl_ast_build_get_space(build
, 1));
204 equal
= equal_independent_guards(list
, build
);
208 if (equal
|| n
== 1) {
209 isl_ast_graft
*graft_0
;
211 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
214 guard
= isl_set_copy(graft_0
->guard
);
216 guard
= hoist_guard(guard
, build
);
217 isl_ast_graft_free(graft_0
);
221 ctx
= isl_ast_build_get_ctx(build
);
222 set_list
= isl_set_list_alloc(ctx
, n
);
223 guard
= isl_set_empty(isl_ast_build_get_space(build
, 1));
224 for (i
= 0; i
< n
; ++i
) {
225 isl_ast_graft
*graft
;
226 isl_basic_set
*enforced
;
229 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
230 enforced
= isl_ast_graft_get_enforced(graft
);
231 guard_i
= isl_set_copy(graft
->guard
);
232 isl_ast_graft_free(graft
);
233 set_list
= isl_set_list_add(set_list
, isl_set_copy(guard_i
));
234 guard_i
= isl_set_intersect(guard_i
,
235 isl_set_from_basic_set(enforced
));
236 guard_i
= isl_set_intersect(guard_i
,
237 isl_ast_build_get_domain(build
));
238 guard
= isl_set_union(guard
, guard_i
);
240 hull
= isl_set_unshifted_simple_hull_from_set_list(guard
, set_list
);
241 guard
= isl_set_from_basic_set(hull
);
242 return hoist_guard(guard
, build
);
245 /* Internal data structure used inside insert_if.
247 * list is the list of guarded nodes created by each call to insert_if.
248 * node is the original node that is guarded by insert_if.
249 * build is the build in which the AST is constructed.
251 struct isl_insert_if_data
{
252 isl_ast_node_list
*list
;
254 isl_ast_build
*build
;
257 static int insert_if(__isl_take isl_basic_set
*bset
, void *user
);
259 /* Insert an if node around "node" testing the condition encoded
262 * If the user does not want any disjunctions in the if conditions
263 * and if "guard" does involve a disjunction, then we make the different
264 * disjuncts disjoint and insert an if node corresponding to each disjunct
265 * around a copy of "node". The result is then a block node containing
266 * this sequence of guarded copies of "node".
268 static __isl_give isl_ast_node
*ast_node_insert_if(
269 __isl_take isl_ast_node
*node
, __isl_take isl_set
*guard
,
270 __isl_keep isl_ast_build
*build
)
272 struct isl_insert_if_data data
;
275 ctx
= isl_ast_build_get_ctx(build
);
276 if (isl_options_get_ast_build_allow_or(ctx
) ||
277 isl_set_n_basic_set(guard
) <= 1) {
278 isl_ast_node
*if_node
;
281 expr
= isl_ast_build_expr_from_set_internal(build
, guard
);
283 if_node
= isl_ast_node_alloc_if(expr
);
284 return isl_ast_node_if_set_then(if_node
, node
);
287 guard
= isl_set_make_disjoint(guard
);
289 data
.list
= isl_ast_node_list_alloc(ctx
, 0);
292 if (isl_set_foreach_basic_set(guard
, &insert_if
, &data
) < 0)
293 data
.list
= isl_ast_node_list_free(data
.list
);
296 isl_ast_node_free(data
.node
);
297 return isl_ast_node_alloc_block(data
.list
);
300 /* Insert an if node around a copy of "data->node" testing the condition
301 * encoded in guard "bset" and add the result to data->list.
303 static int insert_if(__isl_take isl_basic_set
*bset
, void *user
)
305 struct isl_insert_if_data
*data
= user
;
309 set
= isl_set_from_basic_set(bset
);
310 node
= isl_ast_node_copy(data
->node
);
311 node
= ast_node_insert_if(node
, set
, data
->build
);
312 data
->list
= isl_ast_node_list_add(data
->list
, node
);
317 /* Insert an if node around graft->node testing the condition encoded
318 * in guard "guard", assuming guard involves any conditions.
320 static __isl_give isl_ast_graft
*insert_if_node(
321 __isl_take isl_ast_graft
*graft
, __isl_take isl_set
*guard
,
322 __isl_keep isl_ast_build
*build
)
329 univ
= isl_set_plain_is_universe(guard
);
337 build
= isl_ast_build_copy(build
);
338 graft
->node
= ast_node_insert_if(graft
->node
, guard
, build
);
339 isl_ast_build_free(build
);
342 return isl_ast_graft_free(graft
);
347 return isl_ast_graft_free(graft
);
350 /* Insert an if node around graft->node testing the condition encoded
351 * in graft->guard, assuming graft->guard involves any conditions.
353 static __isl_give isl_ast_graft
*insert_pending_guard_node(
354 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
359 return insert_if_node(graft
, isl_set_copy(graft
->guard
), build
);
362 /* Replace graft->enforced by "enforced".
364 __isl_give isl_ast_graft
*isl_ast_graft_set_enforced(
365 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
367 if (!graft
|| !enforced
)
370 isl_basic_set_free(graft
->enforced
);
371 graft
->enforced
= enforced
;
375 isl_basic_set_free(enforced
);
376 return isl_ast_graft_free(graft
);
379 /* Update "enforced" such that it only involves constraints that are
380 * also enforced by "graft".
382 static __isl_give isl_basic_set
*update_enforced(
383 __isl_take isl_basic_set
*enforced
, __isl_keep isl_ast_graft
*graft
,
386 isl_basic_set
*enforced_g
;
388 enforced_g
= isl_ast_graft_get_enforced(graft
);
389 if (depth
< isl_basic_set_dim(enforced_g
, isl_dim_set
))
390 enforced_g
= isl_basic_set_eliminate(enforced_g
,
391 isl_dim_set
, depth
, 1);
392 enforced_g
= isl_basic_set_remove_unknown_divs(enforced_g
);
393 enforced_g
= isl_basic_set_align_params(enforced_g
,
394 isl_basic_set_get_space(enforced
));
395 enforced
= isl_basic_set_align_params(enforced
,
396 isl_basic_set_get_space(enforced_g
));
397 enforced
= isl_set_simple_hull(isl_basic_set_union(enforced
,
403 /* Extend the node at *body with node.
405 * If body points to the else branch, then *body may still be NULL.
406 * If so, we simply attach node to this else branch.
407 * Otherwise, we attach a list containing the statements already
408 * attached at *body followed by node.
410 static void extend_body(__isl_keep isl_ast_node
**body
,
411 __isl_take isl_ast_node
*node
)
413 isl_ast_node_list
*list
;
420 if ((*body
)->type
== isl_ast_node_block
) {
421 list
= isl_ast_node_block_get_children(*body
);
422 isl_ast_node_free(*body
);
424 list
= isl_ast_node_list_from_ast_node(*body
);
425 list
= isl_ast_node_list_add(list
, node
);
426 *body
= isl_ast_node_alloc_block(list
);
429 /* Merge "graft" into the last graft of "list".
430 * body points to the then or else branch of an if node in that last graft.
432 * We attach graft->node to this branch and update the enforced
433 * set of the last graft of "list" to take into account the enforced
436 static __isl_give isl_ast_graft_list
*graft_extend_body(
437 __isl_take isl_ast_graft_list
*list
,
438 __isl_keep isl_ast_node
**body
, __isl_take isl_ast_graft
*graft
,
439 __isl_keep isl_ast_build
*build
)
445 isl_basic_set
*enforced
;
449 extend_body(body
, isl_ast_node_copy(graft
->node
));
453 n
= isl_ast_graft_list_n_ast_graft(list
);
454 last
= isl_ast_graft_list_get_ast_graft(list
, n
- 1);
456 depth
= isl_ast_build_get_depth(build
);
457 space
= isl_ast_build_get_space(build
, 1);
458 enforced
= isl_basic_set_empty(space
);
459 enforced
= update_enforced(enforced
, last
, depth
);
460 enforced
= update_enforced(enforced
, graft
, depth
);
461 last
= isl_ast_graft_set_enforced(last
, enforced
);
463 list
= isl_ast_graft_list_set_ast_graft(list
, n
- 1, last
);
464 isl_ast_graft_free(graft
);
467 isl_ast_graft_free(graft
);
468 return isl_ast_graft_list_free(list
);
471 /* Merge "graft" into the last graft of "list", attaching graft->node
472 * to the then branch of "last_if".
474 static __isl_give isl_ast_graft_list
*extend_then(
475 __isl_take isl_ast_graft_list
*list
,
476 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
477 __isl_keep isl_ast_build
*build
)
479 return graft_extend_body(list
, &last_if
->u
.i
.then
, graft
, build
);
482 /* Merge "graft" into the last graft of "list", attaching graft->node
483 * to the else branch of "last_if".
485 static __isl_give isl_ast_graft_list
*extend_else(
486 __isl_take isl_ast_graft_list
*list
,
487 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
488 __isl_keep isl_ast_build
*build
)
490 return graft_extend_body(list
, &last_if
->u
.i
.else_node
, graft
, build
);
493 /* This data structure keeps track of an if node.
495 * "node" is the actual if-node
496 * "guard" is the original, non-simplified guard of the node
497 * "complement" is the complement of "guard" in the context of outer if nodes
505 /* Given a list of "n" if nodes, clear those starting at "first"
506 * and return "first" (i.e., the updated size of the array).
508 static int clear_if_nodes(struct isl_if_node
*if_node
, int first
, int n
)
512 for (i
= first
; i
< n
; ++i
) {
513 isl_set_free(if_node
[i
].guard
);
514 isl_set_free(if_node
[i
].complement
);
520 /* For each graft in "list",
521 * insert an if node around graft->node testing the condition encoded
522 * in graft->guard, assuming graft->guard involves any conditions.
524 * We keep track of a list of generated if nodes that can be extended
525 * without changing the order of the elements in "list".
526 * If the guard of a graft is a subset of either the guard or its complement
527 * of one of those if nodes, then the node
528 * of the new graft is inserted into the then or else branch of the last graft
529 * and the current graft is discarded.
530 * The guard of the node is then simplified based on the conditions
531 * enforced at that then or else branch.
532 * Otherwise, the current graft is appended to the list.
534 * We only construct else branches if allowed by the user.
536 static __isl_give isl_ast_graft_list
*insert_pending_guard_nodes(
537 __isl_take isl_ast_graft_list
*list
,
538 __isl_keep isl_ast_build
*build
)
543 isl_ast_graft_list
*res
;
544 struct isl_if_node
*if_node
= NULL
;
547 return isl_ast_graft_list_free(list
);
549 ctx
= isl_ast_build_get_ctx(build
);
550 n
= isl_ast_graft_list_n_ast_graft(list
);
552 allow_else
= isl_options_get_ast_build_allow_else(ctx
);
556 if_node
= isl_alloc_array(ctx
, struct isl_if_node
, n
- 1);
558 return isl_ast_graft_list_free(list
);
561 res
= isl_ast_graft_list_alloc(ctx
, n
);
563 for (i
= 0; i
< n
; ++i
) {
565 isl_ast_graft
*graft
;
566 int subset
, found_then
, found_else
;
569 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
573 found_then
= found_else
= -1;
576 test
= isl_set_copy(graft
->guard
);
577 test
= isl_set_intersect(test
,
578 isl_set_copy(build
->domain
));
579 for (j
= n_if
- 1; j
>= 0; --j
) {
580 subset
= isl_set_is_subset(test
,
582 if (subset
< 0 || subset
) {
588 subset
= isl_set_is_subset(test
,
589 if_node
[j
].complement
);
590 if (subset
< 0 || subset
) {
595 n_if
= clear_if_nodes(if_node
, j
+ 1, n_if
);
599 graft
= isl_ast_graft_free(graft
);
603 guard
= isl_set_copy(graft
->guard
);
605 graft
->guard
= isl_set_gist(graft
->guard
,
606 isl_set_copy(if_node
[found_then
].guard
));
607 else if (found_else
>= 0)
608 graft
->guard
= isl_set_gist(graft
->guard
,
609 isl_set_copy(if_node
[found_else
].complement
));
613 graft
= isl_ast_graft_free(graft
);
614 graft
= insert_pending_guard_node(graft
, build
);
615 if (graft
&& graft
->node
!= node
&& i
!= n
- 1) {
617 if_node
[n_if
].node
= graft
->node
;
618 if_node
[n_if
].guard
= guard
;
620 set
= if_node
[found_then
].guard
;
621 else if (found_else
>= 0)
622 set
= if_node
[found_else
].complement
;
625 set
= isl_set_copy(set
);
626 set
= isl_set_subtract(set
, isl_set_copy(guard
));
627 if_node
[n_if
].complement
= set
;
635 res
= extend_then(res
, if_node
[found_then
].node
,
637 else if (found_else
>= 0)
638 res
= extend_else(res
, if_node
[found_else
].node
,
641 res
= isl_ast_graft_list_add(res
, graft
);
644 res
= isl_ast_graft_list_free(res
);
646 isl_ast_graft_list_free(list
);
647 clear_if_nodes(if_node
, 0, n_if
);
652 /* For each graft in "list",
653 * insert an if node around graft->node testing the condition encoded
654 * in graft->guard, assuming graft->guard involves any conditions.
655 * Subsequently remove the guards from the grafts.
657 __isl_give isl_ast_graft_list
*isl_ast_graft_list_insert_pending_guard_nodes(
658 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
663 list
= insert_pending_guard_nodes(list
, build
);
667 universe
= isl_set_universe(isl_ast_build_get_space(build
, 1));
668 n
= isl_ast_graft_list_n_ast_graft(list
);
669 for (i
= 0; i
< n
; ++i
) {
670 isl_ast_graft
*graft
;
672 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
675 isl_set_free(graft
->guard
);
676 graft
->guard
= isl_set_copy(universe
);
678 graft
= isl_ast_graft_free(graft
);
679 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
681 isl_set_free(universe
);
683 return isl_ast_graft_list_free(list
);
688 /* Collect the nodes contained in the grafts in "list" in a node list.
690 static __isl_give isl_ast_node_list
*extract_node_list(
691 __isl_keep isl_ast_graft_list
*list
)
695 isl_ast_node_list
*node_list
;
699 ctx
= isl_ast_graft_list_get_ctx(list
);
700 n
= isl_ast_graft_list_n_ast_graft(list
);
701 node_list
= isl_ast_node_list_alloc(ctx
, n
);
702 for (i
= 0; i
< n
; ++i
) {
704 isl_ast_graft
*graft
;
706 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
707 node
= isl_ast_graft_get_node(graft
);
708 node_list
= isl_ast_node_list_add(node_list
, node
);
709 isl_ast_graft_free(graft
);
715 /* Look for shared enforced constraints by all the elements in "list"
716 * on outer loops (with respect to the current depth) and return the result.
718 * We assume that the number of children is at least one.
720 __isl_give isl_basic_set
*isl_ast_graft_list_extract_shared_enforced(
721 __isl_keep isl_ast_graft_list
*list
,
722 __isl_keep isl_ast_build
*build
)
727 isl_basic_set
*enforced
;
732 n
= isl_ast_graft_list_n_ast_graft(list
);
734 isl_die(isl_ast_graft_list_get_ctx(list
), isl_error_invalid
,
735 "for node should have at least one child",
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 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 /* Represent the graft list as an AST node.
960 * This operation drops the information about guards in the grafts, so
961 * if there are any pending guards, then they are materialized as if nodes.
963 __isl_give isl_ast_node
*isl_ast_node_from_graft_list(
964 __isl_take isl_ast_graft_list
*list
,
965 __isl_keep isl_ast_build
*build
)
967 isl_ast_node_list
*node_list
;
969 list
= insert_pending_guard_nodes(list
, build
);
970 node_list
= extract_node_list(list
);
971 isl_ast_graft_list_free(list
);
973 return isl_ast_node_from_ast_node_list(node_list
);
976 void *isl_ast_graft_free(__isl_take isl_ast_graft
*graft
)
981 if (--graft
->ref
> 0)
984 isl_ast_node_free(graft
->node
);
985 isl_set_free(graft
->guard
);
986 isl_basic_set_free(graft
->enforced
);
992 /* Record that the grafted tree enforces
993 * "enforced" by intersecting graft->enforced with "enforced".
995 __isl_give isl_ast_graft
*isl_ast_graft_enforce(
996 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
998 if (!graft
|| !enforced
)
1001 enforced
= isl_basic_set_align_params(enforced
,
1002 isl_basic_set_get_space(graft
->enforced
));
1003 graft
->enforced
= isl_basic_set_align_params(graft
->enforced
,
1004 isl_basic_set_get_space(enforced
));
1005 graft
->enforced
= isl_basic_set_intersect(graft
->enforced
, enforced
);
1006 if (!graft
->enforced
)
1007 return isl_ast_graft_free(graft
);
1011 isl_basic_set_free(enforced
);
1012 return isl_ast_graft_free(graft
);
1015 __isl_give isl_basic_set
*isl_ast_graft_get_enforced(
1016 __isl_keep isl_ast_graft
*graft
)
1018 return graft
? isl_basic_set_copy(graft
->enforced
) : NULL
;
1021 __isl_give isl_set
*isl_ast_graft_get_guard(__isl_keep isl_ast_graft
*graft
)
1023 return graft
? isl_set_copy(graft
->guard
) : NULL
;
1026 /* Record that "guard" needs to be inserted in "graft".
1028 __isl_give isl_ast_graft
*isl_ast_graft_add_guard(
1029 __isl_take isl_ast_graft
*graft
,
1030 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
1032 return store_guard(graft
, guard
, build
);
1035 /* Reformulate the "graft", which was generated in the context
1036 * of an inner code generation, in terms of the outer code generation
1039 * If "product" is set, then the domain of the inner code generation build is
1043 * with O the domain of the outer code generation build.
1044 * We essentially need to project out S.
1046 * If "product" is not set, then we need to project the domains onto
1047 * their parameter spaces.
1049 __isl_give isl_ast_graft
*isl_ast_graft_unembed(__isl_take isl_ast_graft
*graft
,
1052 isl_basic_set
*enforced
;
1058 enforced
= graft
->enforced
;
1059 enforced
= isl_basic_map_domain(isl_basic_set_unwrap(enforced
));
1060 graft
->enforced
= enforced
;
1061 graft
->guard
= isl_map_domain(isl_set_unwrap(graft
->guard
));
1063 graft
->enforced
= isl_basic_set_params(graft
->enforced
);
1064 graft
->guard
= isl_set_params(graft
->guard
);
1066 graft
->guard
= isl_set_compute_divs(graft
->guard
);
1068 if (!graft
->enforced
|| !graft
->guard
)
1069 return isl_ast_graft_free(graft
);
1074 /* Reformulate the grafts in "list", which were generated in the context
1075 * of an inner code generation, in terms of the outer code generation
1078 __isl_give isl_ast_graft_list
*isl_ast_graft_list_unembed(
1079 __isl_take isl_ast_graft_list
*list
, int product
)
1083 n
= isl_ast_graft_list_n_ast_graft(list
);
1084 for (i
= 0; i
< n
; ++i
) {
1085 isl_ast_graft
*graft
;
1087 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
1088 graft
= isl_ast_graft_unembed(graft
, product
);
1089 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
1095 /* Compute the preimage of "graft" under the function represented by "ma".
1096 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
1098 __isl_give isl_ast_graft
*isl_ast_graft_preimage_multi_aff(
1099 __isl_take isl_ast_graft
*graft
, __isl_take isl_multi_aff
*ma
)
1101 isl_basic_set
*enforced
;
1106 enforced
= graft
->enforced
;
1107 graft
->enforced
= isl_basic_set_preimage_multi_aff(enforced
,
1108 isl_multi_aff_copy(ma
));
1109 graft
->guard
= isl_set_preimage_multi_aff(graft
->guard
, ma
);
1111 if (!graft
->enforced
|| !graft
->guard
)
1112 return isl_ast_graft_free(graft
);
1117 /* Compute the preimage of all the grafts in "list" under
1118 * the function represented by "ma".
1120 __isl_give isl_ast_graft_list
*isl_ast_graft_list_preimage_multi_aff(
1121 __isl_take isl_ast_graft_list
*list
, __isl_take isl_multi_aff
*ma
)
1125 n
= isl_ast_graft_list_n_ast_graft(list
);
1126 for (i
= 0; i
< n
; ++i
) {
1127 isl_ast_graft
*graft
;
1129 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
1130 graft
= isl_ast_graft_preimage_multi_aff(graft
,
1131 isl_multi_aff_copy(ma
));
1132 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
1135 isl_multi_aff_free(ma
);
1139 /* Compare two grafts based on their guards.
1141 static int cmp_graft(__isl_keep isl_ast_graft
*a
, __isl_keep isl_ast_graft
*b
,
1144 return isl_set_plain_cmp(a
->guard
, b
->guard
);
1147 /* Order the elements in "list" based on their guards.
1149 __isl_give isl_ast_graft_list
*isl_ast_graft_list_sort_guard(
1150 __isl_take isl_ast_graft_list
*list
)
1152 return isl_ast_graft_list_sort(list
, &cmp_graft
, NULL
);
1155 /* Merge the given two lists into a single list of grafts,
1156 * merging grafts with the same guard into a single graft.
1158 * "list2" has been sorted using isl_ast_graft_list_sort.
1159 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1160 * and may therefore not be completely sorted.
1162 * The elements in "list2" need to be executed after those in "list1",
1163 * but if the guard of a graft in "list2" is disjoint from the guards
1164 * of some final elements in "list1", then it can be moved up to before
1165 * those final elements.
1167 * In particular, we look at each element g of "list2" in turn
1168 * and move it up beyond elements of "list1" that would be sorted
1169 * after g as long as each of these elements has a guard that is disjoint
1172 * We do not allow the second or any later element of "list2" to be moved
1173 * before a previous elements of "list2" even if the reason that
1174 * that element didn't move up further was that its guard was not disjoint
1175 * from that of the previous element in "list1".
1177 __isl_give isl_ast_graft_list
*isl_ast_graft_list_merge(
1178 __isl_take isl_ast_graft_list
*list1
,
1179 __isl_take isl_ast_graft_list
*list2
,
1180 __isl_keep isl_ast_build
*build
)
1184 if (!list1
|| !list2
|| !build
)
1186 if (list2
->n
== 0) {
1187 isl_ast_graft_list_free(list2
);
1190 if (list1
->n
== 0) {
1191 isl_ast_graft_list_free(list1
);
1196 for (i
= 0; i
< list2
->n
; ++i
) {
1197 isl_ast_graft
*graft
;
1198 graft
= isl_ast_graft_list_get_ast_graft(list2
, i
);
1202 for (j
= list1
->n
; j
>= 0; --j
) {
1204 isl_ast_graft
*graft_j
;
1209 cmp
= isl_set_plain_cmp(list1
->p
[j
- 1]->guard
,
1212 disjoint
= isl_set_is_disjoint(graft
->guard
,
1213 list1
->p
[j
- 1]->guard
);
1215 list1
= isl_ast_graft_list_free(list1
);
1224 list1
= isl_ast_graft_list_insert(list1
, j
,
1231 graft_j
= isl_ast_graft_list_get_ast_graft(list1
, j
);
1232 graft_j
= isl_ast_graft_fuse(graft_j
, graft
, build
);
1233 list1
= isl_ast_graft_list_set_ast_graft(list1
, j
,
1239 isl_die(isl_ast_build_get_ctx(build
),
1241 "element failed to get inserted", break);
1248 list1
= isl_ast_graft_list_free(list1
);
1249 isl_ast_graft_list_free(list2
);
1253 isl_ast_graft_list_free(list1
);
1254 isl_ast_graft_list_free(list2
);
1258 __isl_give isl_printer
*isl_printer_print_ast_graft(__isl_take isl_printer
*p
,
1259 __isl_keep isl_ast_graft
*graft
)
1264 return isl_printer_free(p
);
1266 p
= isl_printer_print_str(p
, "(");
1267 p
= isl_printer_print_str(p
, "guard: ");
1268 p
= isl_printer_print_set(p
, graft
->guard
);
1269 p
= isl_printer_print_str(p
, ", ");
1270 p
= isl_printer_print_str(p
, "enforced: ");
1271 p
= isl_printer_print_basic_set(p
, graft
->enforced
);
1272 p
= isl_printer_print_str(p
, ", ");
1273 p
= isl_printer_print_str(p
, "node: ");
1274 p
= isl_printer_print_ast_node(p
, graft
->node
);
1275 p
= isl_printer_print_str(p
, ")");