2 * Copyright 2012 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
10 #include <isl_list_private.h>
11 #include <isl_ast_private.h>
12 #include <isl_ast_build_expr.h>
13 #include <isl_ast_build_private.h>
14 #include <isl_ast_graft_private.h>
16 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
17 __isl_keep isl_ast_graft
*graft
);
20 #define BASE ast_graft
22 #include <isl_list_templ.c>
25 #define BASE ast_graft
26 #include <print_templ.c>
28 isl_ctx
*isl_ast_graft_get_ctx(__isl_keep isl_ast_graft
*graft
)
32 return isl_basic_set_get_ctx(graft
->enforced
);
35 __isl_give isl_ast_node
*isl_ast_graft_get_node(
36 __isl_keep isl_ast_graft
*graft
)
38 return graft
? isl_ast_node_copy(graft
->node
) : NULL
;
41 /* Create a graft for "node" with no guards and no enforced conditions.
43 __isl_give isl_ast_graft
*isl_ast_graft_alloc(
44 __isl_take isl_ast_node
*node
, __isl_keep isl_ast_build
*build
)
53 ctx
= isl_ast_node_get_ctx(node
);
54 graft
= isl_calloc_type(ctx
, isl_ast_graft
);
58 space
= isl_ast_build_get_space(build
, 1);
62 graft
->guard
= isl_set_universe(isl_space_copy(space
));
63 graft
->enforced
= isl_basic_set_universe(space
);
65 if (!graft
->guard
|| !graft
->enforced
)
66 return isl_ast_graft_free(graft
);
70 isl_ast_node_free(node
);
74 /* Create a graft with no guards and no enforced conditions
75 * encapsulating a call to the domain element specified by "executed".
76 * "executed" is assumed to be single-valued.
78 __isl_give isl_ast_graft
*isl_ast_graft_alloc_domain(
79 __isl_take isl_map
*executed
, __isl_keep isl_ast_build
*build
)
83 node
= isl_ast_build_call_from_executed(build
, executed
);
85 return isl_ast_graft_alloc(node
, build
);
88 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
89 __isl_keep isl_ast_graft
*graft
)
98 /* Do all the grafts in "list" have the same guard and is this guard
99 * independent of the current depth?
101 static int equal_independent_guards(__isl_keep isl_ast_graft_list
*list
,
102 __isl_keep isl_ast_build
*build
)
106 isl_ast_graft
*graft_0
;
110 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
114 depth
= isl_ast_build_get_depth(build
);
115 skip
= isl_set_involves_dims(graft_0
->guard
, isl_dim_set
, depth
, 1);
116 if (skip
< 0 || skip
) {
117 isl_ast_graft_free(graft_0
);
118 return skip
< 0 ? -1 : 0;
121 n
= isl_ast_graft_list_n_ast_graft(list
);
122 for (i
= 1; i
< n
; ++i
) {
123 isl_ast_graft
*graft
;
124 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
128 equal
= isl_set_is_equal(graft_0
->guard
, graft
->guard
);
129 isl_ast_graft_free(graft
);
130 if (equal
< 0 || !equal
)
134 isl_ast_graft_free(graft_0
);
139 /* Extract a common guard from the grafts in "list" that can be hoisted
140 * out of the current level. If no such guard can be found, then return
143 * If all the grafts in the list have the same guard and if this guard
144 * is independent of the current level, then it can be hoisted out.
145 * Otherwise, we return the unshifted simple hull of the guards.
147 * The special case for equal guards is needed in case those guards
148 * are non-convex. Taking the simple hull would remove information
149 * and would not allow for these guards to be hoisted completely.
151 static __isl_give isl_set
*extract_hoistable_guard(
152 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
156 isl_ast_graft
*graft_0
;
163 n
= isl_ast_graft_list_n_ast_graft(list
);
165 return isl_set_universe(isl_ast_build_get_space(build
, 1));
167 equal
= equal_independent_guards(list
, build
);
171 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
174 guard
= isl_set_copy(graft_0
->guard
);
175 isl_ast_graft_free(graft_0
);
179 depth
= isl_ast_build_get_depth(build
);
180 if (depth
< isl_set_dim(guard
, isl_dim_set
)) {
181 guard
= isl_set_remove_divs_involving_dims(guard
,
182 isl_dim_set
, depth
, 1);
183 guard
= isl_set_eliminate(guard
, isl_dim_set
, depth
, 1);
184 guard
= isl_set_compute_divs(guard
);
187 for (i
= 1; i
< n
; ++i
) {
188 isl_ast_graft
*graft
;
192 is_universe
= isl_set_plain_is_universe(guard
);
194 guard
= isl_set_free(guard
);
198 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
200 guard
= isl_set_free(guard
);
203 guard
= isl_set_union(guard
, isl_set_copy(graft
->guard
));
204 hull
= isl_set_unshifted_simple_hull(guard
);
205 guard
= isl_set_from_basic_set(hull
);
206 isl_ast_graft_free(graft
);
212 /* Insert an if node around graft->node testing the condition encoded
213 * in guard "guard", assuming guard involves any conditions.
215 static __isl_give isl_ast_graft
*insert_if_node(
216 __isl_take isl_ast_graft
*graft
, __isl_take isl_set
*guard
,
217 __isl_keep isl_ast_build
*build
)
226 univ
= isl_set_plain_is_universe(guard
);
234 build
= isl_ast_build_copy(build
);
235 build
= isl_ast_build_set_enforced(build
,
236 isl_ast_graft_get_enforced(graft
));
237 expr
= isl_ast_build_expr_from_set(build
, guard
);
238 isl_ast_build_free(build
);
240 node
= isl_ast_node_alloc_if(expr
);
241 graft
->node
= isl_ast_node_if_set_then(node
, graft
->node
);
244 return isl_ast_graft_free(graft
);
249 return isl_ast_graft_free(graft
);
252 /* Insert an if node around graft->node testing the condition encoded
253 * in graft->guard, assuming graft->guard involves any conditions.
255 static __isl_give isl_ast_graft
*insert_pending_guard_node(
256 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
261 return insert_if_node(graft
, isl_set_copy(graft
->guard
), build
);
264 /* Replace graft->enforced by "enforced".
266 __isl_give isl_ast_graft
*isl_ast_graft_set_enforced(
267 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
269 if (!graft
|| !enforced
)
272 isl_basic_set_free(graft
->enforced
);
273 graft
->enforced
= enforced
;
277 isl_basic_set_free(enforced
);
278 return isl_ast_graft_free(graft
);
281 /* Update "enforced" such that it only involves constraints that are
282 * also enforced by "graft".
284 static __isl_give isl_basic_set
*update_enforced(
285 __isl_take isl_basic_set
*enforced
, __isl_keep isl_ast_graft
*graft
,
288 isl_basic_set
*enforced_g
;
290 enforced_g
= isl_ast_graft_get_enforced(graft
);
291 if (depth
< isl_basic_set_dim(enforced_g
, isl_dim_set
))
292 enforced_g
= isl_basic_set_eliminate(enforced_g
,
293 isl_dim_set
, depth
, 1);
294 enforced_g
= isl_basic_set_remove_unknown_divs(enforced_g
);
295 enforced_g
= isl_basic_set_align_params(enforced_g
,
296 isl_basic_set_get_space(enforced
));
297 enforced
= isl_basic_set_align_params(enforced
,
298 isl_basic_set_get_space(enforced_g
));
299 enforced
= isl_set_simple_hull(isl_basic_set_union(enforced
,
305 /* Extend the node at *body with node.
307 * If body points to the else branch, then *body may still be NULL.
308 * If so, we simply attach node to this else branch.
309 * Otherwise, we attach a list containing the statements already
310 * attached at *body followed by node.
312 static void extend_body(__isl_keep isl_ast_node
**body
,
313 __isl_take isl_ast_node
*node
)
315 isl_ast_node_list
*list
;
322 if ((*body
)->type
== isl_ast_node_block
) {
323 list
= isl_ast_node_block_get_children(*body
);
324 isl_ast_node_free(*body
);
326 list
= isl_ast_node_list_from_ast_node(*body
);
327 list
= isl_ast_node_list_add(list
, node
);
328 *body
= isl_ast_node_alloc_block(list
);
331 /* Merge "graft" into the last graft of "list".
332 * body points to the then or else branch of an if node in that last graft.
334 * We attach graft->node to this branch and update the enforced
335 * set of the last graft of "list" to take into account the enforced
338 static __isl_give isl_ast_graft_list
*graft_extend_body(
339 __isl_take isl_ast_graft_list
*list
,
340 __isl_keep isl_ast_node
**body
, __isl_take isl_ast_graft
*graft
,
341 __isl_keep isl_ast_build
*build
)
347 isl_basic_set
*enforced
;
351 extend_body(body
, isl_ast_node_copy(graft
->node
));
355 n
= isl_ast_graft_list_n_ast_graft(list
);
356 last
= isl_ast_graft_list_get_ast_graft(list
, n
- 1);
358 depth
= isl_ast_build_get_depth(build
);
359 space
= isl_ast_build_get_space(build
, 1);
360 enforced
= isl_basic_set_empty(space
);
361 enforced
= update_enforced(enforced
, last
, depth
);
362 enforced
= update_enforced(enforced
, graft
, depth
);
363 last
= isl_ast_graft_set_enforced(last
, enforced
);
365 list
= isl_ast_graft_list_set_ast_graft(list
, n
- 1, last
);
366 isl_ast_graft_free(graft
);
369 isl_ast_graft_free(graft
);
370 return isl_ast_graft_list_free(list
);
373 /* Merge "graft" into the last graft of "list", attaching graft->node
374 * to the then branch of "last_if".
376 static __isl_give isl_ast_graft_list
*extend_then(
377 __isl_take isl_ast_graft_list
*list
,
378 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
379 __isl_keep isl_ast_build
*build
)
381 return graft_extend_body(list
, &last_if
->u
.i
.then
, graft
, build
);
384 /* Merge "graft" into the last graft of "list", attaching graft->node
385 * to the else branch of "last_if".
387 static __isl_give isl_ast_graft_list
*extend_else(
388 __isl_take isl_ast_graft_list
*list
,
389 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
390 __isl_keep isl_ast_build
*build
)
392 return graft_extend_body(list
, &last_if
->u
.i
.else_node
, graft
, build
);
395 /* This data structure keeps track of an if node.
397 * "node" is the actual if-node
398 * "guard" is the original, non-simplified guard of the node
399 * "complement" is the complement of "guard" in the context of outer if nodes
407 /* Given a list of "n" if nodes, clear those starting at "first"
408 * and return "first" (i.e., the updated size of the array).
410 static int clear_if_nodes(struct isl_if_node
*if_node
, int first
, int n
)
414 for (i
= first
; i
< n
; ++i
) {
415 isl_set_free(if_node
[i
].guard
);
416 isl_set_free(if_node
[i
].complement
);
422 /* For each graft in "list",
423 * insert an if node around graft->node testing the condition encoded
424 * in graft->guard, assuming graft->guard involves any conditions.
426 * We keep track of a list of generated if nodes that can be extended
427 * without changing the order of the elements in "list".
428 * If the guard of a graft is a subset of either the guard or its complement
429 * of one of those if nodes, then the node
430 * of the new graft is inserted into the then or else branch of the last graft
431 * and the current graft is discarded.
432 * The guard of the node is then simplified based on the conditions
433 * enforced at that then or else branch.
434 * Otherwise, the current graft is appended to the list.
436 * We only construct else branches if allowed by the user.
438 static __isl_give isl_ast_graft_list
*insert_pending_guard_nodes(
439 __isl_take isl_ast_graft_list
*list
,
440 __isl_keep isl_ast_build
*build
)
445 isl_ast_graft_list
*res
;
446 struct isl_if_node
*if_node
= NULL
;
449 return isl_ast_graft_list_free(list
);
451 ctx
= isl_ast_build_get_ctx(build
);
452 n
= isl_ast_graft_list_n_ast_graft(list
);
454 allow_else
= isl_options_get_ast_build_allow_else(ctx
);
458 if_node
= isl_alloc_array(ctx
, struct isl_if_node
, n
- 1);
460 return isl_ast_graft_list_free(list
);
463 res
= isl_ast_graft_list_alloc(ctx
, n
);
465 for (i
= 0; i
< n
; ++i
) {
467 isl_ast_graft
*graft
;
468 int subset
, found_then
, found_else
;
471 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
475 found_then
= found_else
= -1;
478 test
= isl_set_copy(graft
->guard
);
479 test
= isl_set_intersect(test
,
480 isl_set_copy(build
->domain
));
481 for (j
= n_if
- 1; j
>= 0; --j
) {
482 subset
= isl_set_is_subset(test
,
484 if (subset
< 0 || subset
) {
490 subset
= isl_set_is_subset(test
,
491 if_node
[j
].complement
);
492 if (subset
< 0 || subset
) {
497 n_if
= clear_if_nodes(if_node
, j
+ 1, n_if
);
501 graft
= isl_ast_graft_free(graft
);
505 guard
= isl_set_copy(graft
->guard
);
507 graft
->guard
= isl_set_gist(graft
->guard
,
508 isl_set_copy(if_node
[found_then
].guard
));
509 else if (found_else
>= 0)
510 graft
->guard
= isl_set_gist(graft
->guard
,
511 isl_set_copy(if_node
[found_else
].complement
));
515 graft
= isl_ast_graft_free(graft
);
516 graft
= insert_pending_guard_node(graft
, build
);
517 if (graft
&& graft
->node
!= node
&& i
!= n
- 1) {
519 if_node
[n_if
].node
= graft
->node
;
520 if_node
[n_if
].guard
= guard
;
522 set
= if_node
[found_then
].guard
;
523 else if (found_else
>= 0)
524 set
= if_node
[found_else
].complement
;
527 set
= isl_set_copy(set
);
528 set
= isl_set_subtract(set
, isl_set_copy(guard
));
529 if_node
[n_if
].complement
= set
;
537 res
= extend_then(res
, if_node
[found_then
].node
,
539 else if (found_else
>= 0)
540 res
= extend_else(res
, if_node
[found_else
].node
,
543 res
= isl_ast_graft_list_add(res
, graft
);
546 res
= isl_ast_graft_list_free(res
);
548 isl_ast_graft_list_free(list
);
549 clear_if_nodes(if_node
, 0, n_if
);
554 /* Collect the nodes contained in the grafts in "list" in a node list.
556 static __isl_give isl_ast_node_list
*extract_node_list(
557 __isl_keep isl_ast_graft_list
*list
)
561 isl_ast_node_list
*node_list
;
565 ctx
= isl_ast_graft_list_get_ctx(list
);
566 n
= isl_ast_graft_list_n_ast_graft(list
);
567 node_list
= isl_ast_node_list_alloc(ctx
, n
);
568 for (i
= 0; i
< n
; ++i
) {
570 isl_ast_graft
*graft
;
572 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
573 node
= isl_ast_graft_get_node(graft
);
574 node_list
= isl_ast_node_list_add(node_list
, node
);
575 isl_ast_graft_free(graft
);
581 /* Look for shared enforced constraints by all the elements in "list"
582 * on outer loops (with respect to the current depth) and return the result.
584 * We assume that the number of children is at least one.
586 static __isl_give isl_basic_set
*extract_shared_enforced(
587 __isl_keep isl_ast_graft_list
*list
,
588 __isl_keep isl_ast_build
*build
)
593 isl_basic_set
*enforced
;
598 n
= isl_ast_graft_list_n_ast_graft(list
);
600 isl_die(isl_ast_graft_list_get_ctx(list
), isl_error_invalid
,
601 "for node should have at least one child",
604 space
= isl_ast_build_get_space(build
, 1);
605 enforced
= isl_basic_set_empty(space
);
607 depth
= isl_ast_build_get_depth(build
);
608 for (i
= 0; i
< n
; ++i
) {
609 isl_ast_graft
*graft
;
611 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
612 enforced
= update_enforced(enforced
, graft
, depth
);
613 isl_ast_graft_free(graft
);
619 /* Record "guard" in "graft" so that it will be enforced somewhere
620 * up the tree. If the graft already has a guard, then it may be partially
621 * redundant in combination with the new guard and in the context
622 * of build->domain. We therefore (re)compute the gist of the intersection.
624 static __isl_give isl_ast_graft
*store_guard(__isl_take isl_ast_graft
*graft
,
625 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
632 is_universe
= isl_set_plain_is_universe(guard
);
640 graft
->guard
= isl_set_intersect(graft
->guard
, guard
);
641 graft
->guard
= isl_ast_build_compute_gist(build
, graft
->guard
);
643 return isl_ast_graft_free(graft
);
648 return isl_ast_graft_free(graft
);
651 /* For each graft in "list", replace its guard with the gist with
652 * respect to "context".
654 static __isl_give isl_ast_graft_list
*gist_guards(
655 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_set
*context
)
662 n
= isl_ast_graft_list_n_ast_graft(list
);
663 for (i
= 0; i
< n
; ++i
) {
664 isl_ast_graft
*graft
;
666 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
669 graft
->guard
= isl_set_gist(graft
->guard
,
670 isl_set_copy(context
));
672 graft
= isl_ast_graft_free(graft
);
673 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
676 return isl_ast_graft_list_free(list
);
681 /* Combine the grafts in the list into a single graft.
683 * If "up" is set then the resulting graft will be used at an outer level.
685 * The guard is initialized to the shared guard of the list elements (if any),
686 * provided it does not depend on the current dimension.
687 * The guards in the elements are then simplified with respect to the
688 * hoisted guard and materialized as if nodes around the contained AST nodes.
690 * The enforced set is initialized to the simple hull of the enforced sets
691 * of the elements, provided the ast_build_exploit_nested_bounds option is set
692 * or the new graft will be used at the same level.
694 * The node is initialized to either a block containing the nodes of "list"
695 * or, if there is only a single element, the node of that element.
697 static __isl_give isl_ast_graft
*ast_graft_list_fuse(
698 __isl_take isl_ast_graft_list
*list
,
699 __isl_keep isl_ast_build
*build
, int up
)
703 isl_ast_graft
*graft
;
704 isl_ast_node_list
*node_list
;
710 ctx
= isl_ast_build_get_ctx(build
);
711 guard
= extract_hoistable_guard(list
, build
);
712 list
= gist_guards(list
, guard
);
713 list
= insert_pending_guard_nodes(list
, build
);
715 node_list
= extract_node_list(list
);
716 node
= isl_ast_node_from_ast_node_list(node_list
);
718 graft
= isl_ast_graft_alloc(node
, build
);
720 if (!up
|| isl_options_get_ast_build_exploit_nested_bounds(ctx
)) {
721 isl_basic_set
*enforced
;
722 enforced
= extract_shared_enforced(list
, build
);
723 graft
= isl_ast_graft_enforce(graft
, enforced
);
726 graft
= store_guard(graft
, guard
, build
);
728 isl_ast_graft_list_free(list
);
732 /* Combine the grafts in the list into a single graft.
733 * Return a list containing this single graft.
734 * If the original list is empty, then return an empty list.
736 __isl_give isl_ast_graft_list
*isl_ast_graft_list_fuse(
737 __isl_take isl_ast_graft_list
*list
,
738 __isl_keep isl_ast_build
*build
)
740 isl_ast_graft
*graft
;
744 if (isl_ast_graft_list_n_ast_graft(list
) <= 1)
746 graft
= ast_graft_list_fuse(list
, build
, 0);
747 return isl_ast_graft_list_from_ast_graft(graft
);
750 /* Combine the two grafts into a single graft.
751 * Return a list containing this single graft.
753 static __isl_give isl_ast_graft
*isl_ast_graft_fuse(
754 __isl_take isl_ast_graft
*graft1
, __isl_take isl_ast_graft
*graft2
,
755 __isl_keep isl_ast_build
*build
)
758 isl_ast_graft_list
*list
;
760 ctx
= isl_ast_build_get_ctx(build
);
762 list
= isl_ast_graft_list_alloc(ctx
, 2);
763 list
= isl_ast_graft_list_add(list
, graft1
);
764 list
= isl_ast_graft_list_add(list
, graft2
);
766 return ast_graft_list_fuse(list
, build
, 0);
769 /* Allocate a graft for the current level based on the list of grafts
770 * of the inner level.
772 * The node is initialized to either a block containing the nodes of "children"
773 * or, if there is only a single child, the node of that child.
774 * If the current level requires a for node, it should be inserted by
775 * a subsequent call to isl_ast_graft_insert_for.
777 __isl_give isl_ast_graft
*isl_ast_graft_alloc_level(
778 __isl_take isl_ast_graft_list
*children
,
779 __isl_keep isl_ast_build
*build
)
781 return ast_graft_list_fuse(children
, build
, 1);
784 /* Insert a for node enclosing the current graft->node.
786 __isl_give isl_ast_graft
*isl_ast_graft_insert_for(
787 __isl_take isl_ast_graft
*graft
, __isl_take isl_ast_node
*node
)
792 graft
->node
= isl_ast_node_for_set_body(node
, graft
->node
);
794 return isl_ast_graft_free(graft
);
798 isl_ast_node_free(node
);
799 isl_ast_graft_free(graft
);
803 /* Represent the graft list as an AST node.
804 * This operation drops the information about guards in the grafts, so
805 * if there are any pending guards, then they are materialized as if nodes.
807 __isl_give isl_ast_node
*isl_ast_node_from_graft_list(
808 __isl_take isl_ast_graft_list
*list
,
809 __isl_keep isl_ast_build
*build
)
811 isl_ast_node_list
*node_list
;
813 list
= insert_pending_guard_nodes(list
, build
);
814 node_list
= extract_node_list(list
);
815 isl_ast_graft_list_free(list
);
817 return isl_ast_node_from_ast_node_list(node_list
);
820 void *isl_ast_graft_free(__isl_take isl_ast_graft
*graft
)
825 if (--graft
->ref
> 0)
828 isl_ast_node_free(graft
->node
);
829 isl_set_free(graft
->guard
);
830 isl_basic_set_free(graft
->enforced
);
836 /* Record that the grafted tree enforces
837 * "enforced" by intersecting graft->enforced with "enforced".
839 __isl_give isl_ast_graft
*isl_ast_graft_enforce(
840 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
842 if (!graft
|| !enforced
)
845 enforced
= isl_basic_set_align_params(enforced
,
846 isl_basic_set_get_space(graft
->enforced
));
847 graft
->enforced
= isl_basic_set_align_params(graft
->enforced
,
848 isl_basic_set_get_space(enforced
));
849 graft
->enforced
= isl_basic_set_intersect(graft
->enforced
, enforced
);
850 if (!graft
->enforced
)
851 return isl_ast_graft_free(graft
);
855 isl_basic_set_free(enforced
);
856 return isl_ast_graft_free(graft
);
859 __isl_give isl_basic_set
*isl_ast_graft_get_enforced(
860 __isl_keep isl_ast_graft
*graft
)
862 return graft
? isl_basic_set_copy(graft
->enforced
) : NULL
;
865 __isl_give isl_set
*isl_ast_graft_get_guard(__isl_keep isl_ast_graft
*graft
)
867 return graft
? isl_set_copy(graft
->guard
) : NULL
;
870 /* Record that "guard" needs to be inserted in "graft".
872 * We first simplify the guard in the context of the enforced set and
873 * then we store the guard in case we may be able
874 * to hoist it to higher levels and/or combine it with those of other grafts.
876 __isl_give isl_ast_graft
*isl_ast_graft_add_guard(
877 __isl_take isl_ast_graft
*graft
,
878 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
880 isl_basic_set
*enforced
;
882 if (!graft
|| !build
)
885 enforced
= isl_basic_set_copy(graft
->enforced
);
886 guard
= isl_set_gist(guard
, isl_set_from_basic_set(enforced
));
888 graft
= store_guard(graft
, guard
, build
);
893 isl_ast_graft_free(graft
);
897 /* Reformulate the "graft", which was generated in the context
898 * of an inner code generation, in terms of the outer code generation
901 * If "product" is set, then the domain of the inner code generation build is
905 * with O the domain of the outer code generation build.
906 * We essentially need to project out S.
908 * If "product" is not set, then we need to project the domains onto
909 * their parameter spaces.
911 __isl_give isl_ast_graft
*isl_ast_graft_unembed(__isl_take isl_ast_graft
*graft
,
914 isl_basic_set
*enforced
;
920 enforced
= graft
->enforced
;
921 enforced
= isl_basic_map_domain(isl_basic_set_unwrap(enforced
));
922 graft
->enforced
= enforced
;
923 graft
->guard
= isl_map_domain(isl_set_unwrap(graft
->guard
));
925 graft
->enforced
= isl_basic_set_params(graft
->enforced
);
926 graft
->guard
= isl_set_params(graft
->guard
);
928 graft
->guard
= isl_set_compute_divs(graft
->guard
);
930 if (!graft
->enforced
|| !graft
->guard
)
931 return isl_ast_graft_free(graft
);
936 /* Reformulate the grafts in "list", which were generated in the context
937 * of an inner code generation, in terms of the outer code generation
940 __isl_give isl_ast_graft_list
*isl_ast_graft_list_unembed(
941 __isl_take isl_ast_graft_list
*list
, int product
)
945 n
= isl_ast_graft_list_n_ast_graft(list
);
946 for (i
= 0; i
< n
; ++i
) {
947 isl_ast_graft
*graft
;
949 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
950 graft
= isl_ast_graft_unembed(graft
, product
);
951 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
957 /* Compute the preimage of "graft" under the function represented by "ma".
958 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
960 __isl_give isl_ast_graft
*isl_ast_graft_preimage_multi_aff(
961 __isl_take isl_ast_graft
*graft
, __isl_take isl_multi_aff
*ma
)
963 isl_basic_set
*enforced
;
968 enforced
= graft
->enforced
;
969 graft
->enforced
= isl_basic_set_preimage_multi_aff(enforced
,
970 isl_multi_aff_copy(ma
));
971 graft
->guard
= isl_set_preimage_multi_aff(graft
->guard
, ma
);
973 if (!graft
->enforced
|| !graft
->guard
)
974 return isl_ast_graft_free(graft
);
979 /* Compute the preimage of all the grafts in "list" under
980 * the function represented by "ma".
982 __isl_give isl_ast_graft_list
*isl_ast_graft_list_preimage_multi_aff(
983 __isl_take isl_ast_graft_list
*list
, __isl_take isl_multi_aff
*ma
)
987 n
= isl_ast_graft_list_n_ast_graft(list
);
988 for (i
= 0; i
< n
; ++i
) {
989 isl_ast_graft
*graft
;
991 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
992 graft
= isl_ast_graft_preimage_multi_aff(graft
,
993 isl_multi_aff_copy(ma
));
994 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
997 isl_multi_aff_free(ma
);
1001 /* Compare two grafts based on their guards.
1003 static int cmp_graft(const void *a
, const void *b
)
1005 isl_ast_graft
* const *g1
= a
;
1006 isl_ast_graft
* const *g2
= b
;
1008 return isl_set_plain_cmp((*g1
)->guard
, (*g2
)->guard
);
1011 /* Order the elements in "list" based on their guards.
1013 __isl_give isl_ast_graft_list
*isl_ast_graft_list_sort(
1014 __isl_take isl_ast_graft_list
*list
)
1021 qsort(list
->p
, list
->n
, sizeof(list
->p
[0]), &cmp_graft
);
1026 /* Merge the given two lists into a single list of grafts,
1027 * merging grafts with the same guard into a single graft.
1029 * "list2" has been sorted using isl_ast_graft_list_sort.
1030 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1031 * and may therefore not be completely sorted.
1033 * The elements in "list2" need to be executed after those in "list1",
1034 * but if the guard of a graft in "list2" is disjoint from the guards
1035 * of some final elements in "list1", then it can be moved up to before
1036 * those final elements.
1038 * In particular, we look at each element g of "list2" in turn
1039 * and move it up beyond elements of "list1" that would be sorted
1040 * after g as long as each of these elements has a guard that is disjoint
1043 * We do not allow the second or any later element of "list2" to be moved
1044 * before a previous elements of "list2" even if the reason that
1045 * that element didn't move up further was that its guard was not disjoint
1046 * from that of the previous element in "list1".
1048 __isl_give isl_ast_graft_list
*isl_ast_graft_list_merge(
1049 __isl_take isl_ast_graft_list
*list1
,
1050 __isl_take isl_ast_graft_list
*list2
,
1051 __isl_keep isl_ast_build
*build
)
1055 if (!list1
|| !list2
|| !build
)
1057 if (list2
->n
== 0) {
1058 isl_ast_graft_list_free(list2
);
1061 if (list1
->n
== 0) {
1062 isl_ast_graft_list_free(list1
);
1067 for (i
= 0; i
< list2
->n
; ++i
) {
1068 isl_ast_graft
*graft
;
1069 graft
= isl_ast_graft_list_get_ast_graft(list2
, i
);
1073 for (j
= list1
->n
; j
>= 0; --j
) {
1075 isl_ast_graft
*graft_j
;
1080 cmp
= isl_set_plain_cmp(list1
->p
[j
- 1]->guard
,
1083 disjoint
= isl_set_is_disjoint(graft
->guard
,
1084 list1
->p
[j
- 1]->guard
);
1086 list1
= isl_ast_graft_list_free(list1
);
1095 list1
= isl_ast_graft_list_insert(list1
, j
,
1102 graft_j
= isl_ast_graft_list_get_ast_graft(list1
, j
);
1103 graft_j
= isl_ast_graft_fuse(graft_j
, graft
, build
);
1104 list1
= isl_ast_graft_list_set_ast_graft(list1
, j
,
1110 isl_die(isl_ast_build_get_ctx(build
),
1112 "element failed to get inserted", break);
1119 list1
= isl_ast_graft_list_free(list1
);
1120 isl_ast_graft_list_free(list2
);
1124 isl_ast_graft_list_free(list1
);
1125 isl_ast_graft_list_free(list2
);
1129 __isl_give isl_printer
*isl_printer_print_ast_graft(__isl_take isl_printer
*p
,
1130 __isl_keep isl_ast_graft
*graft
)
1135 return isl_printer_free(p
);
1137 p
= isl_printer_print_str(p
, "(");
1138 p
= isl_printer_print_str(p
, "guard: ");
1139 p
= isl_printer_print_set(p
, graft
->guard
);
1140 p
= isl_printer_print_str(p
, ", ");
1141 p
= isl_printer_print_str(p
, "enforced: ");
1142 p
= isl_printer_print_basic_set(p
, graft
->enforced
);
1143 p
= isl_printer_print_str(p
, ", ");
1144 p
= isl_printer_print_str(p
, "node: ");
1145 p
= isl_printer_print_ast_node(p
, graft
->node
);
1146 p
= isl_printer_print_str(p
, ")");