2 * Copyright 2012 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
4 * Copyright 2019 Cerebras Systems
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege,
9 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
10 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
11 * B.P. 105 - 78153 Le Chesnay, France
12 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
16 #include <isl/space.h>
17 #include <isl/stream.h>
18 #include <isl_ast_private.h>
19 #include <isl_ast_build_expr.h>
20 #include <isl_ast_build_private.h>
21 #include <isl_ast_graft_private.h>
22 #include "isl_set_to_ast_graft_list.h"
24 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
25 __isl_keep isl_ast_graft
*graft
);
26 static __isl_give isl_ast_graft
*isl_stream_read_ast_graft(
27 __isl_keep isl_stream
*s
);
30 #define EL_BASE ast_graft
32 #include <isl_list_templ.c>
33 #include <isl_list_read_templ.c>
36 #define BASE ast_graft
37 #include <print_templ.c>
39 isl_ctx
*isl_ast_graft_get_ctx(__isl_keep isl_ast_graft
*graft
)
43 return isl_basic_set_get_ctx(graft
->enforced
);
46 __isl_give isl_ast_node
*isl_ast_graft_get_node(
47 __isl_keep isl_ast_graft
*graft
)
49 return graft
? isl_ast_node_copy(graft
->node
) : NULL
;
52 /* Create a graft for "node" with guards "guard" and
53 * enforced conditions "enforced".
55 static isl_ast_graft
*graft_alloc(__isl_take isl_ast_node
*node
,
56 __isl_take isl_set
*guard
, __isl_take isl_basic_set
*enforced
)
61 if (!node
|| !guard
|| !enforced
)
64 ctx
= isl_ast_node_get_ctx(node
);
65 graft
= isl_calloc_type(ctx
, isl_ast_graft
);
72 graft
->enforced
= enforced
;
76 isl_ast_node_free(node
);
78 isl_basic_set_free(enforced
);
82 /* Create a graft for "node" with no guards and no enforced conditions.
84 __isl_give isl_ast_graft
*isl_ast_graft_alloc(
85 __isl_take isl_ast_node
*node
, __isl_keep isl_ast_build
*build
)
89 isl_basic_set
*enforced
;
94 space
= isl_ast_build_get_space(build
, 1);
96 guard
= isl_set_universe(isl_space_copy(space
));
97 enforced
= isl_basic_set_universe(space
);
99 return graft_alloc(node
, guard
, enforced
);
102 /* Create a graft with no guards and no enforced conditions
103 * encapsulating a call to the domain element specified by "executed".
104 * "executed" is assumed to be single-valued.
106 __isl_give isl_ast_graft
*isl_ast_graft_alloc_domain(
107 __isl_take isl_map
*executed
, __isl_keep isl_ast_build
*build
)
111 node
= isl_ast_build_call_from_executed(build
, executed
);
113 return isl_ast_graft_alloc(node
, build
);
116 static __isl_give isl_ast_graft
*isl_ast_graft_copy(
117 __isl_keep isl_ast_graft
*graft
)
126 /* Do all the grafts in "list" have the same guard and is this guard
127 * independent of the current depth?
129 static isl_bool
equal_independent_guards(__isl_keep isl_ast_graft_list
*list
,
130 __isl_keep isl_ast_build
*build
)
136 isl_ast_graft
*graft_0
;
137 isl_bool equal
= isl_bool_true
;
140 n
= isl_ast_graft_list_n_ast_graft(list
);
141 depth
= isl_ast_build_get_depth(build
);
142 if (n
< 0 || depth
< 0)
143 return isl_bool_error
;
144 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
146 return isl_bool_error
;
148 dim
= isl_set_dim(graft_0
->guard
, isl_dim_set
);
150 skip
= isl_bool_error
;
151 else if (dim
<= depth
)
152 skip
= isl_bool_false
;
154 skip
= isl_set_involves_dims(graft_0
->guard
,
155 isl_dim_set
, depth
, 1);
156 if (skip
< 0 || skip
) {
157 isl_ast_graft_free(graft_0
);
158 return isl_bool_not(skip
);
161 for (i
= 1; i
< n
; ++i
) {
162 isl_ast_graft
*graft
;
163 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
165 equal
= isl_bool_error
;
167 equal
= isl_set_is_equal(graft_0
->guard
, graft
->guard
);
168 isl_ast_graft_free(graft
);
169 if (equal
< 0 || !equal
)
173 isl_ast_graft_free(graft_0
);
178 /* Hoist "guard" out of the current level (given by "build").
180 * In particular, eliminate the dimension corresponding to the current depth.
182 static __isl_give isl_set
*hoist_guard(__isl_take isl_set
*guard
,
183 __isl_keep isl_ast_build
*build
)
188 depth
= isl_ast_build_get_depth(build
);
189 dim
= isl_set_dim(guard
, isl_dim_set
);
190 if (depth
< 0 || dim
< 0)
191 return isl_set_free(guard
);
193 guard
= isl_set_remove_divs_involving_dims(guard
,
194 isl_dim_set
, depth
, 1);
195 guard
= isl_set_eliminate(guard
, isl_dim_set
, depth
, 1);
196 guard
= isl_set_compute_divs(guard
);
202 /* Extract a common guard from the grafts in "list" that can be hoisted
203 * out of the current level. If no such guard can be found, then return
206 * If all the grafts in the list have the same guard and if this guard
207 * is independent of the current level, then it can be hoisted out.
208 * If there is only one graft in the list and if its guard
209 * depends on the current level, then we eliminate this level and
212 * Otherwise, we return the unshifted simple hull of the guards.
213 * In order to be able to hoist as many constraints as possible,
214 * but at the same time avoid hoisting constraints that did not
215 * appear in the guards in the first place, we intersect the guards
216 * with all the information that is available (i.e., the domain
217 * from the build and the enforced constraints of the graft) and
218 * compute the unshifted hull of the result using only constraints
219 * from the original guards.
220 * In particular, intersecting the guards with other known information
221 * allows us to hoist guards that are only explicit is some of
222 * the grafts and implicit in the others.
224 * The special case for equal guards is needed in case those guards
225 * are non-convex. Taking the simple hull would remove information
226 * and would not allow for these guards to be hoisted completely.
228 __isl_give isl_set
*isl_ast_graft_list_extract_hoistable_guard(
229 __isl_keep isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
236 isl_set_list
*set_list
;
242 n
= isl_ast_graft_list_n_ast_graft(list
);
246 return isl_set_universe(isl_ast_build_get_space(build
, 1));
248 equal
= equal_independent_guards(list
, build
);
252 if (equal
|| n
== 1) {
253 isl_ast_graft
*graft_0
;
255 graft_0
= isl_ast_graft_list_get_ast_graft(list
, 0);
258 guard
= isl_set_copy(graft_0
->guard
);
260 guard
= hoist_guard(guard
, build
);
261 isl_ast_graft_free(graft_0
);
265 ctx
= isl_ast_build_get_ctx(build
);
266 set_list
= isl_set_list_alloc(ctx
, n
);
267 guard
= isl_set_empty(isl_ast_build_get_space(build
, 1));
268 for (i
= 0; i
< n
; ++i
) {
269 isl_ast_graft
*graft
;
270 isl_basic_set
*enforced
;
273 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
274 enforced
= isl_ast_graft_get_enforced(graft
);
275 guard_i
= isl_set_copy(graft
->guard
);
276 isl_ast_graft_free(graft
);
277 set_list
= isl_set_list_add(set_list
, isl_set_copy(guard_i
));
278 guard_i
= isl_set_intersect(guard_i
,
279 isl_set_from_basic_set(enforced
));
280 guard_i
= isl_set_intersect(guard_i
,
281 isl_ast_build_get_domain(build
));
282 guard
= isl_set_union(guard
, guard_i
);
284 hull
= isl_set_unshifted_simple_hull_from_set_list(guard
, set_list
);
285 guard
= isl_set_from_basic_set(hull
);
286 return hoist_guard(guard
, build
);
289 /* Internal data structure used inside insert_if.
291 * list is the list of guarded nodes created by each call to insert_if.
292 * node is the original node that is guarded by insert_if.
293 * build is the build in which the AST is constructed.
295 struct isl_insert_if_data
{
296 isl_ast_node_list
*list
;
298 isl_ast_build
*build
;
301 static isl_stat
insert_if(__isl_take isl_basic_set
*bset
, void *user
);
303 /* Insert an if node around "node" testing the condition encoded
306 * If the user does not want any disjunctions in the if conditions
307 * and if "guard" does involve a disjunction, then we make the different
308 * disjuncts disjoint and insert an if node corresponding to each disjunct
309 * around a copy of "node". The result is then a block node containing
310 * this sequence of guarded copies of "node".
312 static __isl_give isl_ast_node
*ast_node_insert_if(
313 __isl_take isl_ast_node
*node
, __isl_take isl_set
*guard
,
314 __isl_keep isl_ast_build
*build
)
316 struct isl_insert_if_data data
;
320 n
= isl_set_n_basic_set(guard
);
323 ctx
= isl_ast_build_get_ctx(build
);
324 if (isl_options_get_ast_build_allow_or(ctx
) || n
<= 1) {
325 isl_ast_node
*if_node
;
328 expr
= isl_ast_build_expr_from_set_internal(build
, guard
);
330 if_node
= isl_ast_node_alloc_if(expr
);
331 return isl_ast_node_if_set_then(if_node
, node
);
334 guard
= isl_set_make_disjoint(guard
);
336 data
.list
= isl_ast_node_list_alloc(ctx
, 0);
339 if (isl_set_foreach_basic_set(guard
, &insert_if
, &data
) < 0)
340 data
.list
= isl_ast_node_list_free(data
.list
);
343 isl_ast_node_free(data
.node
);
344 return isl_ast_node_alloc_block(data
.list
);
347 isl_ast_node_free(node
);
351 /* Insert an if node around a copy of "data->node" testing the condition
352 * encoded in guard "bset" and add the result to data->list.
354 static isl_stat
insert_if(__isl_take isl_basic_set
*bset
, void *user
)
356 struct isl_insert_if_data
*data
= user
;
360 set
= isl_set_from_basic_set(bset
);
361 node
= isl_ast_node_copy(data
->node
);
362 node
= ast_node_insert_if(node
, set
, data
->build
);
363 data
->list
= isl_ast_node_list_add(data
->list
, node
);
368 /* Insert an if node around graft->node testing the condition encoded
369 * in guard "guard", assuming guard involves any conditions.
371 static __isl_give isl_ast_graft
*insert_if_node(
372 __isl_take isl_ast_graft
*graft
, __isl_take isl_set
*guard
,
373 __isl_keep isl_ast_build
*build
)
380 univ
= isl_set_plain_is_universe(guard
);
388 graft
->node
= ast_node_insert_if(graft
->node
, guard
, build
);
391 return isl_ast_graft_free(graft
);
396 return isl_ast_graft_free(graft
);
399 /* Insert an if node around graft->node testing the condition encoded
400 * in graft->guard, assuming graft->guard involves any conditions.
402 static __isl_give isl_ast_graft
*insert_pending_guard_node(
403 __isl_take isl_ast_graft
*graft
, __isl_keep isl_ast_build
*build
)
408 return insert_if_node(graft
, isl_set_copy(graft
->guard
), build
);
411 /* Replace graft->enforced by "enforced".
413 __isl_give isl_ast_graft
*isl_ast_graft_set_enforced(
414 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
416 if (!graft
|| !enforced
)
419 isl_basic_set_free(graft
->enforced
);
420 graft
->enforced
= enforced
;
424 isl_basic_set_free(enforced
);
425 return isl_ast_graft_free(graft
);
428 /* Update "enforced" such that it only involves constraints that are
429 * also enforced by "graft".
431 static __isl_give isl_basic_set
*update_enforced(
432 __isl_take isl_basic_set
*enforced
, __isl_keep isl_ast_graft
*graft
,
436 isl_basic_set
*enforced_g
;
438 enforced_g
= isl_ast_graft_get_enforced(graft
);
439 dim
= isl_basic_set_dim(enforced_g
, isl_dim_set
);
441 enforced_g
= isl_basic_set_free(enforced_g
);
443 enforced_g
= isl_basic_set_eliminate(enforced_g
,
444 isl_dim_set
, depth
, 1);
445 enforced_g
= isl_basic_set_remove_unknown_divs(enforced_g
);
446 enforced_g
= isl_basic_set_align_params(enforced_g
,
447 isl_basic_set_get_space(enforced
));
448 enforced
= isl_basic_set_align_params(enforced
,
449 isl_basic_set_get_space(enforced_g
));
450 enforced
= isl_set_simple_hull(isl_basic_set_union(enforced
,
456 /* Extend the node at *body with node.
458 * If body points to the else branch, then *body may still be NULL.
459 * If so, we simply attach node to this else branch.
460 * Otherwise, we attach a list containing the statements already
461 * attached at *body followed by node.
463 static void extend_body(__isl_keep isl_ast_node
**body
,
464 __isl_take isl_ast_node
*node
)
466 isl_ast_node_list
*list
;
473 if ((*body
)->type
== isl_ast_node_block
) {
474 list
= isl_ast_node_block_get_children(*body
);
475 isl_ast_node_free(*body
);
477 list
= isl_ast_node_list_from_ast_node(*body
);
478 list
= isl_ast_node_list_add(list
, node
);
479 *body
= isl_ast_node_alloc_block(list
);
482 /* Merge "graft" into the last graft of "list".
483 * body points to the then or else branch of an if node in that last graft.
485 * We attach graft->node to this branch and update the enforced
486 * set of the last graft of "list" to take into account the enforced
489 static __isl_give isl_ast_graft_list
*graft_extend_body(
490 __isl_take isl_ast_graft_list
*list
,
491 __isl_keep isl_ast_node
**body
, __isl_take isl_ast_graft
*graft
,
492 __isl_keep isl_ast_build
*build
)
498 isl_basic_set
*enforced
;
500 n
= isl_ast_graft_list_n_ast_graft(list
);
501 depth
= isl_ast_build_get_depth(build
);
502 if (n
< 0 || depth
< 0 || !graft
)
504 extend_body(body
, isl_ast_node_copy(graft
->node
));
508 last
= isl_ast_graft_list_get_ast_graft(list
, n
- 1);
510 space
= isl_ast_build_get_space(build
, 1);
511 enforced
= isl_basic_set_empty(space
);
512 enforced
= update_enforced(enforced
, last
, depth
);
513 enforced
= update_enforced(enforced
, graft
, depth
);
514 last
= isl_ast_graft_set_enforced(last
, enforced
);
516 list
= isl_ast_graft_list_set_ast_graft(list
, n
- 1, last
);
517 isl_ast_graft_free(graft
);
520 isl_ast_graft_free(graft
);
521 return isl_ast_graft_list_free(list
);
524 /* Merge "graft" into the last graft of "list", attaching graft->node
525 * to the then branch of "last_if".
527 static __isl_give isl_ast_graft_list
*extend_then(
528 __isl_take isl_ast_graft_list
*list
,
529 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
530 __isl_keep isl_ast_build
*build
)
532 return graft_extend_body(list
, &last_if
->u
.i
.then
, graft
, build
);
535 /* Merge "graft" into the last graft of "list", attaching graft->node
536 * to the else branch of "last_if".
538 static __isl_give isl_ast_graft_list
*extend_else(
539 __isl_take isl_ast_graft_list
*list
,
540 __isl_keep isl_ast_node
*last_if
, __isl_take isl_ast_graft
*graft
,
541 __isl_keep isl_ast_build
*build
)
543 return graft_extend_body(list
, &last_if
->u
.i
.else_node
, graft
, build
);
546 /* This data structure keeps track of an if node.
548 * "node" is the actual if-node
549 * "guard" is the original, non-simplified guard of the node
550 * "complement" is the complement of "guard" in the context of outer if nodes
558 /* Given a list of "n" if nodes, clear those starting at "first"
559 * and return "first" (i.e., the updated size of the array).
561 static int clear_if_nodes(struct isl_if_node
*if_node
, int first
, int n
)
565 for (i
= first
; i
< n
; ++i
) {
566 isl_set_free(if_node
[i
].guard
);
567 isl_set_free(if_node
[i
].complement
);
573 /* For each graft in "list",
574 * insert an if node around graft->node testing the condition encoded
575 * in graft->guard, assuming graft->guard involves any conditions.
577 * We keep track of a list of generated if nodes that can be extended
578 * without changing the order of the elements in "list".
579 * If the guard of a graft is a subset of either the guard or its complement
580 * of one of those if nodes, then the node
581 * of the new graft is inserted into the then or else branch of the last graft
582 * and the current graft is discarded.
583 * The guard of the node is then simplified based on the conditions
584 * enforced at that then or else branch.
585 * Otherwise, the current graft is appended to the list.
587 * We only construct else branches if allowed by the user.
589 static __isl_give isl_ast_graft_list
*insert_pending_guard_nodes(
590 __isl_take isl_ast_graft_list
*list
,
591 __isl_keep isl_ast_build
*build
)
597 isl_ast_graft_list
*res
;
598 struct isl_if_node
*if_node
= NULL
;
600 n
= isl_ast_graft_list_n_ast_graft(list
);
602 return isl_ast_graft_list_free(list
);
604 ctx
= isl_ast_build_get_ctx(build
);
606 allow_else
= isl_options_get_ast_build_allow_else(ctx
);
610 if_node
= isl_alloc_array(ctx
, struct isl_if_node
, n
- 1);
612 return isl_ast_graft_list_free(list
);
615 res
= isl_ast_graft_list_alloc(ctx
, n
);
617 for (i
= 0; i
< n
; ++i
) {
619 isl_ast_graft
*graft
;
620 int subset
, found_then
, found_else
;
623 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
627 found_then
= found_else
= -1;
630 test
= isl_set_copy(graft
->guard
);
631 test
= isl_set_intersect(test
,
632 isl_set_copy(build
->domain
));
633 for (j
= n_if
- 1; j
>= 0; --j
) {
634 subset
= isl_set_is_subset(test
,
636 if (subset
< 0 || subset
) {
642 subset
= isl_set_is_subset(test
,
643 if_node
[j
].complement
);
644 if (subset
< 0 || subset
) {
649 n_if
= clear_if_nodes(if_node
, j
+ 1, n_if
);
653 graft
= isl_ast_graft_free(graft
);
657 guard
= isl_set_copy(graft
->guard
);
659 graft
->guard
= isl_set_gist(graft
->guard
,
660 isl_set_copy(if_node
[found_then
].guard
));
661 else if (found_else
>= 0)
662 graft
->guard
= isl_set_gist(graft
->guard
,
663 isl_set_copy(if_node
[found_else
].complement
));
667 graft
= isl_ast_graft_free(graft
);
668 graft
= insert_pending_guard_node(graft
, build
);
669 if (graft
&& graft
->node
!= node
&& i
!= n
- 1) {
671 if_node
[n_if
].node
= graft
->node
;
672 if_node
[n_if
].guard
= guard
;
674 set
= if_node
[found_then
].guard
;
675 else if (found_else
>= 0)
676 set
= if_node
[found_else
].complement
;
679 set
= isl_set_copy(set
);
680 set
= isl_set_subtract(set
, isl_set_copy(guard
));
681 if_node
[n_if
].complement
= set
;
689 res
= extend_then(res
, if_node
[found_then
].node
,
691 else if (found_else
>= 0)
692 res
= extend_else(res
, if_node
[found_else
].node
,
695 res
= isl_ast_graft_list_add(res
, graft
);
698 res
= isl_ast_graft_list_free(res
);
700 isl_ast_graft_list_free(list
);
701 clear_if_nodes(if_node
, 0, n_if
);
706 /* For each graft in "list",
707 * insert an if node around graft->node testing the condition encoded
708 * in graft->guard, assuming graft->guard involves any conditions.
709 * Subsequently remove the guards from the grafts.
711 __isl_give isl_ast_graft_list
*isl_ast_graft_list_insert_pending_guard_nodes(
712 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
718 list
= insert_pending_guard_nodes(list
, build
);
719 n
= isl_ast_graft_list_n_ast_graft(list
);
721 return isl_ast_graft_list_free(list
);
723 universe
= isl_set_universe(isl_ast_build_get_space(build
, 1));
724 for (i
= 0; i
< n
; ++i
) {
725 isl_ast_graft
*graft
;
727 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
730 isl_set_free(graft
->guard
);
731 graft
->guard
= isl_set_copy(universe
);
733 graft
= isl_ast_graft_free(graft
);
734 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
736 isl_set_free(universe
);
738 return isl_ast_graft_list_free(list
);
743 /* Collect the nodes contained in the grafts in "list" in a node list.
745 static __isl_give isl_ast_node_list
*extract_node_list(
746 __isl_keep isl_ast_graft_list
*list
)
751 isl_ast_node_list
*node_list
;
753 n
= isl_ast_graft_list_n_ast_graft(list
);
756 ctx
= isl_ast_graft_list_get_ctx(list
);
757 node_list
= isl_ast_node_list_alloc(ctx
, n
);
758 for (i
= 0; i
< n
; ++i
) {
760 isl_ast_graft
*graft
;
762 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
763 node
= isl_ast_graft_get_node(graft
);
764 node_list
= isl_ast_node_list_add(node_list
, node
);
765 isl_ast_graft_free(graft
);
771 /* Look for shared enforced constraints by all the elements in "list"
772 * on outer loops (with respect to the current depth) and return the result.
774 * If there are no elements in "list", then return the empty set.
776 __isl_give isl_basic_set
*isl_ast_graft_list_extract_shared_enforced(
777 __isl_keep isl_ast_graft_list
*list
,
778 __isl_keep isl_ast_build
*build
)
784 isl_basic_set
*enforced
;
786 n
= isl_ast_graft_list_n_ast_graft(list
);
787 depth
= isl_ast_build_get_depth(build
);
788 if (n
< 0 || depth
< 0)
791 space
= isl_ast_build_get_space(build
, 1);
792 enforced
= isl_basic_set_empty(space
);
794 for (i
= 0; i
< n
; ++i
) {
795 isl_ast_graft
*graft
;
797 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
798 enforced
= update_enforced(enforced
, graft
, depth
);
799 isl_ast_graft_free(graft
);
805 /* Record "guard" in "graft" so that it will be enforced somewhere
806 * up the tree. If the graft already has a guard, then it may be partially
807 * redundant in combination with the new guard and in the context
808 * the generated constraints of "build". In fact, the new guard
809 * may in itself have some redundant constraints.
810 * We therefore (re)compute the gist of the intersection
811 * and coalesce the result.
813 static __isl_give isl_ast_graft
*store_guard(__isl_take isl_ast_graft
*graft
,
814 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
821 is_universe
= isl_set_plain_is_universe(guard
);
829 graft
->guard
= isl_set_intersect(graft
->guard
, guard
);
830 graft
->guard
= isl_set_gist(graft
->guard
,
831 isl_ast_build_get_generated(build
));
832 graft
->guard
= isl_set_coalesce(graft
->guard
);
834 return isl_ast_graft_free(graft
);
839 return isl_ast_graft_free(graft
);
842 /* For each graft in "list", replace its guard with the gist with
843 * respect to "context".
845 static __isl_give isl_ast_graft_list
*gist_guards(
846 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_set
*context
)
851 n
= isl_ast_graft_list_n_ast_graft(list
);
853 return isl_ast_graft_list_free(list
);
855 for (i
= 0; i
< n
; ++i
) {
856 isl_ast_graft
*graft
;
858 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
861 graft
->guard
= isl_set_gist(graft
->guard
,
862 isl_set_copy(context
));
864 graft
= isl_ast_graft_free(graft
);
865 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
868 return isl_ast_graft_list_free(list
);
873 /* For each graft in "list", replace its guard with the gist with
874 * respect to "context".
876 __isl_give isl_ast_graft_list
*isl_ast_graft_list_gist_guards(
877 __isl_take isl_ast_graft_list
*list
, __isl_take isl_set
*context
)
879 list
= gist_guards(list
, context
);
880 isl_set_free(context
);
885 /* Allocate a graft in "build" based on the list of grafts in "sub_build".
886 * "guard" and "enforced" are the guard and enforced constraints
887 * of the allocated graft. The guard is used to simplify the guards
888 * of the elements in "list".
890 * The node is initialized to either a block containing the nodes of "children"
891 * or, if there is only a single child, the node of that child.
892 * If the current level requires a for node, it should be inserted by
893 * a subsequent call to isl_ast_graft_insert_for.
895 __isl_give isl_ast_graft
*isl_ast_graft_alloc_from_children(
896 __isl_take isl_ast_graft_list
*list
, __isl_take isl_set
*guard
,
897 __isl_take isl_basic_set
*enforced
, __isl_keep isl_ast_build
*build
,
898 __isl_keep isl_ast_build
*sub_build
)
900 isl_ast_build
*guard_build
;
902 isl_ast_node_list
*node_list
;
903 isl_ast_graft
*graft
;
905 guard_build
= isl_ast_build_copy(sub_build
);
906 guard_build
= isl_ast_build_replace_pending_by_guard(guard_build
,
907 isl_set_copy(guard
));
908 list
= gist_guards(list
, guard
);
909 list
= insert_pending_guard_nodes(list
, guard_build
);
910 isl_ast_build_free(guard_build
);
912 node_list
= extract_node_list(list
);
913 node
= isl_ast_node_from_ast_node_list(node_list
);
914 isl_ast_graft_list_free(list
);
916 graft
= isl_ast_graft_alloc(node
, build
);
917 graft
= store_guard(graft
, guard
, build
);
918 graft
= isl_ast_graft_enforce(graft
, enforced
);
923 /* Combine the grafts in the list into a single graft.
925 * The guard is initialized to the shared guard of the list elements (if any),
926 * provided it does not depend on the current dimension.
927 * The guards in the elements are then simplified with respect to the
928 * hoisted guard and materialized as if nodes around the contained AST nodes
929 * in the context of "sub_build".
931 * The enforced set is initialized to the simple hull of the enforced sets
932 * of the elements, provided the ast_build_exploit_nested_bounds option is set
933 * or the new graft will be used at the same level.
935 * The node is initialized to either a block containing the nodes of "list"
936 * or, if there is only a single element, the node of that element.
938 static __isl_give isl_ast_graft
*ast_graft_list_fuse(
939 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
941 isl_ast_graft
*graft
;
942 isl_basic_set
*enforced
;
948 enforced
= isl_ast_graft_list_extract_shared_enforced(list
, build
);
949 guard
= isl_ast_graft_list_extract_hoistable_guard(list
, build
);
950 graft
= isl_ast_graft_alloc_from_children(list
, guard
, enforced
,
956 /* Combine the grafts in the list into a single graft.
957 * Return a list containing this single graft.
958 * If the original list is empty, then return an empty list.
960 __isl_give isl_ast_graft_list
*isl_ast_graft_list_fuse(
961 __isl_take isl_ast_graft_list
*list
,
962 __isl_keep isl_ast_build
*build
)
965 isl_ast_graft
*graft
;
967 n
= isl_ast_graft_list_n_ast_graft(list
);
969 return isl_ast_graft_list_free(list
);
972 graft
= ast_graft_list_fuse(list
, build
);
973 return isl_ast_graft_list_from_ast_graft(graft
);
976 /* Combine the two grafts into a single graft.
977 * Return a list containing this single graft.
979 static __isl_give isl_ast_graft
*isl_ast_graft_fuse(
980 __isl_take isl_ast_graft
*graft1
, __isl_take isl_ast_graft
*graft2
,
981 __isl_keep isl_ast_build
*build
)
984 isl_ast_graft_list
*list
;
986 ctx
= isl_ast_build_get_ctx(build
);
988 list
= isl_ast_graft_list_alloc(ctx
, 2);
989 list
= isl_ast_graft_list_add(list
, graft1
);
990 list
= isl_ast_graft_list_add(list
, graft2
);
992 return ast_graft_list_fuse(list
, build
);
995 /* Insert a for node enclosing the current graft->node.
997 __isl_give isl_ast_graft
*isl_ast_graft_insert_for(
998 __isl_take isl_ast_graft
*graft
, __isl_take isl_ast_node
*node
)
1003 graft
->node
= isl_ast_node_for_set_body(node
, graft
->node
);
1005 return isl_ast_graft_free(graft
);
1009 isl_ast_node_free(node
);
1010 isl_ast_graft_free(graft
);
1014 /* Insert a mark governing the current graft->node.
1016 __isl_give isl_ast_graft
*isl_ast_graft_insert_mark(
1017 __isl_take isl_ast_graft
*graft
, __isl_take isl_id
*mark
)
1022 graft
->node
= isl_ast_node_alloc_mark(mark
, graft
->node
);
1024 return isl_ast_graft_free(graft
);
1029 isl_ast_graft_free(graft
);
1033 /* Represent the graft list as an AST node.
1034 * This operation drops the information about guards in the grafts, so
1035 * if there are any pending guards, then they are materialized as if nodes.
1037 __isl_give isl_ast_node
*isl_ast_node_from_graft_list(
1038 __isl_take isl_ast_graft_list
*list
,
1039 __isl_keep isl_ast_build
*build
)
1041 isl_ast_node_list
*node_list
;
1043 list
= insert_pending_guard_nodes(list
, build
);
1044 node_list
= extract_node_list(list
);
1045 isl_ast_graft_list_free(list
);
1047 return isl_ast_node_from_ast_node_list(node_list
);
1050 __isl_null isl_ast_graft
*isl_ast_graft_free(__isl_take isl_ast_graft
*graft
)
1055 if (--graft
->ref
> 0)
1058 isl_ast_node_free(graft
->node
);
1059 isl_set_free(graft
->guard
);
1060 isl_basic_set_free(graft
->enforced
);
1066 /* Record that the grafted tree enforces
1067 * "enforced" by intersecting graft->enforced with "enforced".
1069 __isl_give isl_ast_graft
*isl_ast_graft_enforce(
1070 __isl_take isl_ast_graft
*graft
, __isl_take isl_basic_set
*enforced
)
1072 if (!graft
|| !enforced
)
1075 enforced
= isl_basic_set_align_params(enforced
,
1076 isl_basic_set_get_space(graft
->enforced
));
1077 graft
->enforced
= isl_basic_set_align_params(graft
->enforced
,
1078 isl_basic_set_get_space(enforced
));
1079 graft
->enforced
= isl_basic_set_intersect(graft
->enforced
, enforced
);
1080 if (!graft
->enforced
)
1081 return isl_ast_graft_free(graft
);
1085 isl_basic_set_free(enforced
);
1086 return isl_ast_graft_free(graft
);
1089 __isl_give isl_basic_set
*isl_ast_graft_get_enforced(
1090 __isl_keep isl_ast_graft
*graft
)
1092 return graft
? isl_basic_set_copy(graft
->enforced
) : NULL
;
1095 __isl_give isl_set
*isl_ast_graft_get_guard(__isl_keep isl_ast_graft
*graft
)
1097 return graft
? isl_set_copy(graft
->guard
) : NULL
;
1100 /* Record that "guard" needs to be inserted in "graft".
1102 __isl_give isl_ast_graft
*isl_ast_graft_add_guard(
1103 __isl_take isl_ast_graft
*graft
,
1104 __isl_take isl_set
*guard
, __isl_keep isl_ast_build
*build
)
1106 return store_guard(graft
, guard
, build
);
1109 /* Reformulate the "graft", which was generated in the context
1110 * of an inner code generation, in terms of the outer code generation
1113 * If "product" is set, then the domain of the inner code generation build is
1117 * with O the domain of the outer code generation build.
1118 * We essentially need to project out S.
1120 * If "product" is not set, then we need to project the domains onto
1121 * their parameter spaces.
1123 __isl_give isl_ast_graft
*isl_ast_graft_unembed(__isl_take isl_ast_graft
*graft
,
1126 isl_basic_set
*enforced
;
1132 enforced
= graft
->enforced
;
1133 enforced
= isl_basic_map_domain(isl_basic_set_unwrap(enforced
));
1134 graft
->enforced
= enforced
;
1135 graft
->guard
= isl_map_domain(isl_set_unwrap(graft
->guard
));
1137 graft
->enforced
= isl_basic_set_params(graft
->enforced
);
1138 graft
->guard
= isl_set_params(graft
->guard
);
1140 graft
->guard
= isl_set_compute_divs(graft
->guard
);
1142 if (!graft
->enforced
|| !graft
->guard
)
1143 return isl_ast_graft_free(graft
);
1148 /* Reformulate the grafts in "list", which were generated in the context
1149 * of an inner code generation, in terms of the outer code generation
1152 __isl_give isl_ast_graft_list
*isl_ast_graft_list_unembed(
1153 __isl_take isl_ast_graft_list
*list
, int product
)
1158 n
= isl_ast_graft_list_n_ast_graft(list
);
1160 return isl_ast_graft_list_free(list
);
1161 for (i
= 0; i
< n
; ++i
) {
1162 isl_ast_graft
*graft
;
1164 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
1165 graft
= isl_ast_graft_unembed(graft
, product
);
1166 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
1172 /* Compute the preimage of "graft" under the function represented by "ma".
1173 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
1175 __isl_give isl_ast_graft
*isl_ast_graft_preimage_multi_aff(
1176 __isl_take isl_ast_graft
*graft
, __isl_take isl_multi_aff
*ma
)
1178 isl_basic_set
*enforced
;
1183 enforced
= graft
->enforced
;
1184 graft
->enforced
= isl_basic_set_preimage_multi_aff(enforced
,
1185 isl_multi_aff_copy(ma
));
1186 graft
->guard
= isl_set_preimage_multi_aff(graft
->guard
, ma
);
1188 if (!graft
->enforced
|| !graft
->guard
)
1189 return isl_ast_graft_free(graft
);
1194 /* Compute the preimage of all the grafts in "list" under
1195 * the function represented by "ma".
1197 __isl_give isl_ast_graft_list
*isl_ast_graft_list_preimage_multi_aff(
1198 __isl_take isl_ast_graft_list
*list
, __isl_take isl_multi_aff
*ma
)
1203 n
= isl_ast_graft_list_n_ast_graft(list
);
1205 list
= isl_ast_graft_list_free(list
);
1206 for (i
= 0; i
< n
; ++i
) {
1207 isl_ast_graft
*graft
;
1209 graft
= isl_ast_graft_list_get_ast_graft(list
, i
);
1210 graft
= isl_ast_graft_preimage_multi_aff(graft
,
1211 isl_multi_aff_copy(ma
));
1212 list
= isl_ast_graft_list_set_ast_graft(list
, i
, graft
);
1215 isl_multi_aff_free(ma
);
1219 /* Compare two grafts based on their guards.
1221 static int cmp_graft(__isl_keep isl_ast_graft
*a
, __isl_keep isl_ast_graft
*b
,
1224 return isl_set_plain_cmp(a
->guard
, b
->guard
);
1227 /* Order the elements in "list" based on their guards.
1229 __isl_give isl_ast_graft_list
*isl_ast_graft_list_sort_guard(
1230 __isl_take isl_ast_graft_list
*list
)
1232 return isl_ast_graft_list_sort(list
, &cmp_graft
, NULL
);
1235 /* Merge the given two lists into a single list of grafts,
1236 * merging grafts with the same guard into a single graft.
1238 * "list2" has been sorted using isl_ast_graft_list_sort.
1239 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1240 * and may therefore not be completely sorted.
1242 * The elements in "list2" need to be executed after those in "list1",
1243 * but if the guard of a graft in "list2" is disjoint from the guards
1244 * of some final elements in "list1", then it can be moved up to before
1245 * those final elements.
1247 * In particular, we look at each element g of "list2" in turn
1248 * and move it up beyond elements of "list1" that would be sorted
1249 * after g as long as each of these elements has a guard that is disjoint
1252 * We do not allow the second or any later element of "list2" to be moved
1253 * before a previous elements of "list2" even if the reason that
1254 * that element didn't move up further was that its guard was not disjoint
1255 * from that of the previous element in "list1".
1257 __isl_give isl_ast_graft_list
*isl_ast_graft_list_merge(
1258 __isl_take isl_ast_graft_list
*list1
,
1259 __isl_take isl_ast_graft_list
*list2
,
1260 __isl_keep isl_ast_build
*build
)
1264 if (!list1
|| !list2
|| !build
)
1266 if (list2
->n
== 0) {
1267 isl_ast_graft_list_free(list2
);
1270 if (list1
->n
== 0) {
1271 isl_ast_graft_list_free(list1
);
1276 for (i
= 0; i
< list2
->n
; ++i
) {
1277 isl_ast_graft
*graft
;
1278 graft
= isl_ast_graft_list_get_ast_graft(list2
, i
);
1282 for (j
= list1
->n
; j
>= 0; --j
) {
1284 isl_ast_graft
*graft_j
;
1289 cmp
= isl_set_plain_cmp(list1
->p
[j
- 1]->guard
,
1292 disjoint
= isl_set_is_disjoint(graft
->guard
,
1293 list1
->p
[j
- 1]->guard
);
1295 isl_ast_graft_free(graft
);
1296 list1
= isl_ast_graft_list_free(list1
);
1305 list1
= isl_ast_graft_list_insert(list1
, j
,
1312 graft_j
= isl_ast_graft_list_get_ast_graft(list1
, j
);
1313 graft_j
= isl_ast_graft_fuse(graft_j
, graft
, build
);
1314 list1
= isl_ast_graft_list_set_ast_graft(list1
, j
,
1320 isl_ast_graft_free(graft
);
1321 isl_die(isl_ast_build_get_ctx(build
),
1323 "element failed to get inserted", break);
1331 list1
= isl_ast_graft_list_free(list1
);
1332 isl_ast_graft_list_free(list2
);
1336 isl_ast_graft_list_free(list1
);
1337 isl_ast_graft_list_free(list2
);
1341 /* Internal data structure for split_on_guard.
1343 * "guard2list" is the constructed associative array.
1344 * "any_match" gets set if any guard was seen more than once.
1346 struct isl_split_on_guard_data
{
1347 isl_set_to_ast_graft_list
*guard2list
;
1351 /* Add "graft" to the list associated to its guard in data->guard2list.
1352 * If some other graft was already associated to this guard,
1353 * then set data->any_match.
1355 static isl_stat
add_to_guard_list(__isl_take isl_ast_graft
*graft
, void *user
)
1357 struct isl_split_on_guard_data
*data
= user
;
1359 isl_maybe_isl_ast_graft_list m
;
1362 return isl_stat_error
;
1363 m
= isl_set_to_ast_graft_list_try_get(data
->guard2list
, graft
->guard
);
1365 return isl_stat_non_null(isl_ast_graft_free(graft
));
1368 *data
->any_match
= 1;
1369 m
.value
= isl_ast_graft_list_add(m
.value
, graft
);
1371 m
.value
= isl_ast_graft_list_from_ast_graft(graft
);
1373 guard
= isl_set_copy(graft
->guard
);
1375 isl_set_to_ast_graft_list_set(data
->guard2list
, guard
, m
.value
);
1377 return isl_stat_non_null(data
->guard2list
);
1380 /* Construct an associative array that groups the elements
1381 * of "list" based on their guards.
1382 * If any guard appears more than once, then set "any_match".
1384 static __isl_give isl_set_to_ast_graft_list
*split_on_guard(
1385 __isl_keep isl_ast_graft_list
*list
, int *any_match
)
1387 struct isl_split_on_guard_data data
= { NULL
, any_match
};
1391 n
= isl_ast_graft_list_size(list
);
1395 ctx
= isl_ast_graft_list_get_ctx(list
);
1396 data
.guard2list
= isl_set_to_ast_graft_list_alloc(ctx
, n
);
1398 if (isl_ast_graft_list_foreach(list
, &add_to_guard_list
, &data
) < 0)
1399 return isl_set_to_ast_graft_list_free(data
.guard2list
);
1401 return data
.guard2list
;
1404 /* Add the elements of "guard_list" to "list".
1406 static isl_stat
add_same_guard(__isl_take isl_set
*guard
,
1407 __isl_take isl_ast_graft_list
*guard_list
, void *user
)
1409 isl_ast_graft_list
**list
= user
;
1411 isl_set_free(guard
);
1412 *list
= isl_ast_graft_list_concat(*list
, guard_list
);
1414 return isl_stat_non_null(*list
);
1417 /* Given an associative array "guard2list" containing the elements
1418 * of "list" grouped on common guards, reconstruct "list"
1419 * by placing elements with the same guard consecutively.
1421 static __isl_give isl_ast_graft_list
*reconstruct(
1422 __isl_take isl_ast_graft_list
*list
,
1423 __isl_keep isl_set_to_ast_graft_list
*guard2list
,
1424 __isl_keep isl_ast_build
*build
)
1426 list
= isl_ast_graft_list_clear(list
);
1427 if (isl_set_to_ast_graft_list_foreach(guard2list
,
1428 &add_same_guard
, &list
) < 0)
1429 list
= isl_ast_graft_list_free(list
);
1434 /* Group the grafts in "list" based on identical guards.
1436 * Note that there need to be a least three elements in the list
1437 * for the elements not to be grouped already.
1439 * Group the elements in an associative array based on their guards.
1440 * If any guard was seen more than once, then reconstruct the list
1441 * from the associative array. Otherwise, simply return the original list.
1443 __isl_give isl_ast_graft_list
*isl_ast_graft_list_group_on_guard(
1444 __isl_take isl_ast_graft_list
*list
, __isl_keep isl_ast_build
*build
)
1448 isl_set_to_ast_graft_list
*guard2list
;
1450 n
= isl_ast_graft_list_size(list
);
1452 return isl_ast_graft_list_free(list
);
1456 guard2list
= split_on_guard(list
, &any_match
);
1458 list
= reconstruct(list
, guard2list
, build
);
1460 isl_set_to_ast_graft_list_free(guard2list
);
1465 /* An enumeration of the keys that appear in the textual representation
1466 * of an isl_sat_graft object.
1468 enum isl_graft_key
{
1469 isl_graft_key_error
= -1,
1470 isl_graft_key_guard
,
1471 isl_graft_key_enforced
,
1476 /* Textual representations of the keys for an isl_sat_graft object.
1478 static char *key_str
[] = {
1479 [isl_graft_key_guard
] = "guard",
1480 [isl_graft_key_enforced
] = "enforced",
1481 [isl_graft_key_node
] = "node",
1484 __isl_give isl_printer
*isl_printer_print_ast_graft(__isl_take isl_printer
*p
,
1485 __isl_keep isl_ast_graft
*graft
)
1490 return isl_printer_free(p
);
1492 p
= isl_printer_print_str(p
, "(");
1493 p
= isl_printer_print_str(p
, key_str
[isl_graft_key_guard
]);
1494 p
= isl_printer_print_str(p
, ": ");
1495 p
= isl_printer_print_set(p
, graft
->guard
);
1496 p
= isl_printer_print_str(p
, ", ");
1497 p
= isl_printer_print_str(p
, key_str
[isl_graft_key_enforced
]);
1498 p
= isl_printer_print_str(p
, ": ");
1499 p
= isl_printer_print_basic_set(p
, graft
->enforced
);
1500 p
= isl_printer_print_str(p
, ", ");
1501 p
= isl_printer_print_str(p
, key_str
[isl_graft_key_node
]);
1502 p
= isl_printer_print_str(p
, ": ");
1503 p
= isl_printer_print_ast_node(p
, graft
->node
);
1504 p
= isl_printer_print_str(p
, ")");
1510 #define KEY enum isl_graft_key
1512 #define KEY_ERROR isl_graft_key_error
1514 #define KEY_END isl_graft_key_end
1516 #define KEY_STR key_str
1518 #define KEY_EXTRACT extract_key
1520 #define KEY_GET get_key
1521 #include "extract_key.c"
1523 /* Read the key "key" from "s", along with the subsequent colon.
1525 static isl_stat
read_key(__isl_keep isl_stream
*s
, enum isl_graft_key key
)
1527 enum isl_graft_key extracted
;
1529 extracted
= get_key(s
);
1531 return isl_stat_error
;
1532 if (extracted
!= key
)
1533 isl_die(isl_stream_get_ctx(s
), isl_error_invalid
,
1534 "expecting different field", return isl_stat_error
);
1535 if (isl_stream_eat(s
, ':') < 0)
1536 return isl_stat_error
;
1540 /* Read an isl_ast_graft object from "s".
1542 * Read the pieces in the way they are printed in isl_printer_print_ast_graft.
1544 static __isl_give isl_ast_graft
*isl_stream_read_ast_graft(
1545 __isl_keep isl_stream
*s
)
1548 isl_basic_set
*enforced
= NULL
;
1549 isl_ast_node
*node
= NULL
;
1551 if (isl_stream_eat(s
, '(') < 0)
1553 if (read_key(s
, isl_graft_key_guard
) < 0)
1555 guard
= isl_stream_read_set(s
);
1558 if (isl_stream_eat(s
, ',') < 0)
1560 if (read_key(s
, isl_graft_key_enforced
) < 0)
1562 enforced
= isl_stream_read_basic_set(s
);
1565 if (isl_stream_eat(s
, ',') < 0)
1567 if (read_key(s
, isl_graft_key_node
) < 0)
1569 node
= isl_stream_read_ast_node(s
);
1572 if (isl_stream_eat(s
, ')') < 0)
1574 return graft_alloc(node
, guard
, enforced
);
1576 isl_set_free(guard
);
1577 isl_basic_set_free(enforced
);
1578 isl_ast_node_free(node
);