2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
38 #include <isl/id_to_pw_aff.h>
39 #include <isl/union_set.h>
48 #include "tree2scop.h"
50 /* Update "pc" by taking into account the writes in "stmt".
51 * That is, clear any previously assigned values to variables
52 * that are written by "stmt".
54 static __isl_give pet_context
*handle_writes(struct pet_stmt
*stmt
,
55 __isl_take pet_context
*pc
)
57 return pet_context_clear_writes_in_tree(pc
, stmt
->body
);
60 /* Update "pc" based on the write accesses in "scop".
62 static __isl_give pet_context
*scop_handle_writes(struct pet_scop
*scop
,
63 __isl_take pet_context
*pc
)
68 return pet_context_free(pc
);
69 for (i
= 0; i
< scop
->n_stmt
; ++i
)
70 pc
= handle_writes(scop
->stmts
[i
], pc
);
75 /* Wrapper around pet_expr_resolve_assume
76 * for use as a callback to pet_tree_map_expr.
78 static __isl_give pet_expr
*resolve_assume(__isl_take pet_expr
*expr
,
81 pet_context
*pc
= user
;
83 return pet_expr_resolve_assume(expr
, pc
);
86 /* Check if any expression inside "tree" is an assume expression and
87 * if its single argument can be converted to an affine expression
88 * in the context of "pc".
89 * If so, replace the argument by the affine expression.
91 __isl_give pet_tree
*pet_tree_resolve_assume(__isl_take pet_tree
*tree
,
92 __isl_keep pet_context
*pc
)
94 return pet_tree_map_expr(tree
, &resolve_assume
, pc
);
97 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
98 * "tree" has already been evaluated in the context of "pc".
99 * This mainly involves resolving nested expression parameters
100 * and setting the name of the iteration space.
101 * The name is given by tree->label if it is non-NULL. Otherwise,
102 * it is of the form S_<stmt_nr>.
104 static struct pet_scop
*scop_from_evaluated_tree(__isl_take pet_tree
*tree
,
105 int stmt_nr
, __isl_keep pet_context
*pc
)
111 space
= pet_context_get_space(pc
);
113 tree
= pet_tree_resolve_nested(tree
, space
);
114 tree
= pet_tree_resolve_assume(tree
, pc
);
116 domain
= pet_context_get_domain(pc
);
117 ps
= pet_stmt_from_pet_tree(domain
, stmt_nr
, tree
);
118 return pet_scop_from_pet_stmt(space
, ps
);
121 /* Convert a top-level pet_expr to a pet_scop with one statement
122 * within the context "pc".
123 * "expr" has already been evaluated in the context of "pc".
124 * We construct a pet_tree from "expr" and continue with
125 * scop_from_evaluated_tree.
126 * The name is of the form S_<stmt_nr>.
127 * The location of the statement is set to "loc".
129 static struct pet_scop
*scop_from_evaluated_expr(__isl_take pet_expr
*expr
,
130 int stmt_nr
, __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
134 tree
= pet_tree_new_expr(expr
);
135 tree
= pet_tree_set_loc(tree
, loc
);
136 return scop_from_evaluated_tree(tree
, stmt_nr
, pc
);
139 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
140 * "tree" has not yet been evaluated in the context of "pc".
141 * We evaluate "tree" in the context of "pc" and continue with
142 * scop_from_evaluated_tree.
143 * The statement name is given by tree->label if it is non-NULL. Otherwise,
144 * it is of the form S_<stmt_nr>.
146 static struct pet_scop
*scop_from_unevaluated_tree(__isl_take pet_tree
*tree
,
147 int stmt_nr
, __isl_keep pet_context
*pc
)
149 tree
= pet_context_evaluate_tree(pc
, tree
);
150 return scop_from_evaluated_tree(tree
, stmt_nr
, pc
);
153 /* Convert a top-level pet_expr to a pet_scop with one statement
154 * within the context "pc", where "expr" has not yet been evaluated
155 * in the context of "pc".
156 * We construct a pet_tree from "expr" and continue with
157 * scop_from_unevaluated_tree.
158 * The statement name is of the form S_<stmt_nr>.
159 * The location of the statement is set to "loc".
161 static struct pet_scop
*scop_from_expr(__isl_take pet_expr
*expr
,
162 int stmt_nr
, __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
166 tree
= pet_tree_new_expr(expr
);
167 tree
= pet_tree_set_loc(tree
, loc
);
168 return scop_from_unevaluated_tree(tree
, stmt_nr
, pc
);
171 /* Construct a pet_scop with a single statement killing the entire
173 * The location of the statement is set to "loc".
175 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
176 __isl_keep pet_context
*pc
, struct pet_state
*state
)
181 isl_multi_pw_aff
*index
;
184 struct pet_scop
*scop
;
188 ctx
= isl_set_get_ctx(array
->extent
);
189 access
= isl_map_from_range(isl_set_copy(array
->extent
));
190 id
= isl_set_get_tuple_id(array
->extent
);
191 space
= isl_space_alloc(ctx
, 0, 0, 0);
192 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
193 index
= isl_multi_pw_aff_zero(space
);
194 expr
= pet_expr_kill_from_access_and_index(access
, index
);
195 return scop_from_expr(expr
, state
->n_stmt
++, loc
, pc
);
201 /* Construct and return a pet_array corresponding to the variable
202 * accessed by "access" by calling the extract_array callback.
204 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
205 __isl_keep pet_context
*pc
, struct pet_state
*state
)
207 return state
->extract_array(access
, pc
, state
->user
);
210 /* Construct a pet_scop for a (single) variable declaration
211 * within the context "pc".
213 * The scop contains the variable being declared (as an array)
214 * and a statement killing the array.
216 * If the declaration comes with an initialization, then the scop
217 * also contains an assignment to the variable.
219 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
220 __isl_keep pet_context
*pc
, struct pet_state
*state
)
224 struct pet_array
*array
;
225 struct pet_scop
*scop_decl
, *scop
;
226 pet_expr
*lhs
, *rhs
, *pe
;
228 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
231 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
232 scop_decl
= pet_scop_add_array(scop_decl
, array
);
234 if (tree
->type
!= pet_tree_decl_init
)
237 lhs
= pet_expr_copy(tree
->u
.d
.var
);
238 rhs
= pet_expr_copy(tree
->u
.d
.init
);
239 type_size
= pet_expr_get_type_size(lhs
);
240 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
241 scop
= scop_from_expr(pe
, state
->n_stmt
++, pet_tree_get_loc(tree
), pc
);
243 ctx
= pet_tree_get_ctx(tree
);
244 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
249 /* Does "tree" represent a kill statement?
250 * That is, is it an expression statement that "calls" __pencil_kill?
252 static int is_pencil_kill(__isl_keep pet_tree
*tree
)
259 if (tree
->type
!= pet_tree_expr
)
261 expr
= tree
->u
.e
.expr
;
262 if (pet_expr_get_type(expr
) != pet_expr_call
)
264 name
= pet_expr_call_get_name(expr
);
267 return !strcmp(name
, "__pencil_kill");
270 /* Add a kill to "scop" that kills what is accessed by
271 * the access expression "expr".
273 * Mark the access as a write prior to evaluation to avoid
274 * the access being replaced by a possible known value
275 * during the evaluation.
277 * If the access expression has any arguments (after evaluation
278 * in the context of "pc"), then we ignore it, since we cannot
279 * tell which elements are definitely killed.
281 * Otherwise, we extend the index expression to the dimension
282 * of the accessed array and intersect with the extent of the array and
283 * add a kill expression that kills these array elements is added to "scop".
285 static struct pet_scop
*scop_add_kill(struct pet_scop
*scop
,
286 __isl_take pet_expr
*expr
, __isl_take pet_loc
*loc
,
287 __isl_keep pet_context
*pc
, struct pet_state
*state
)
291 isl_multi_pw_aff
*index
;
294 struct pet_array
*array
;
295 struct pet_scop
*scop_i
;
297 expr
= pet_expr_access_set_write(expr
, 1);
298 expr
= pet_context_evaluate_expr(pc
, expr
);
301 if (expr
->n_arg
!= 0) {
306 array
= extract_array(expr
, pc
, state
);
309 index
= pet_expr_access_get_index(expr
);
311 map
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
312 id
= isl_map_get_tuple_id(map
, isl_dim_out
);
313 dim1
= isl_set_dim(array
->extent
, isl_dim_set
);
314 dim2
= isl_map_dim(map
, isl_dim_out
);
315 map
= isl_map_add_dims(map
, isl_dim_out
, dim1
- dim2
);
316 map
= isl_map_set_tuple_id(map
, isl_dim_out
, id
);
317 map
= isl_map_intersect_range(map
, isl_set_copy(array
->extent
));
318 pet_array_free(array
);
319 kill
= pet_expr_kill_from_access_and_index(map
, index
);
320 scop_i
= scop_from_evaluated_expr(kill
, state
->n_stmt
++, loc
, pc
);
321 scop
= pet_scop_add_par(state
->ctx
, scop
, scop_i
);
327 return pet_scop_free(scop
);
330 /* For each argument of the __pencil_kill call in "tree" that
331 * represents an access, add a kill statement to "scop" killing the accessed
334 static struct pet_scop
*scop_from_pencil_kill(__isl_keep pet_tree
*tree
,
335 __isl_keep pet_context
*pc
, struct pet_state
*state
)
338 struct pet_scop
*scop
;
341 call
= tree
->u
.e
.expr
;
343 scop
= pet_scop_empty(pet_context_get_space(pc
));
345 n
= pet_expr_get_n_arg(call
);
346 for (i
= 0; i
< n
; ++i
) {
350 arg
= pet_expr_get_arg(call
, i
);
352 return pet_scop_free(scop
);
353 if (pet_expr_get_type(arg
) != pet_expr_access
) {
357 loc
= pet_tree_get_loc(tree
);
358 scop
= scop_add_kill(scop
, arg
, loc
, pc
, state
);
364 /* Construct a pet_scop for an expression statement within the context "pc".
366 * If the expression calls __pencil_kill, then it needs to be converted
367 * into zero or more kill statements.
368 * Otherwise, a scop is extracted directly from the tree.
370 static struct pet_scop
*scop_from_tree_expr(__isl_keep pet_tree
*tree
,
371 __isl_keep pet_context
*pc
, struct pet_state
*state
)
375 is_kill
= is_pencil_kill(tree
);
379 return scop_from_pencil_kill(tree
, pc
, state
);
380 return scop_from_unevaluated_tree(pet_tree_copy(tree
),
381 state
->n_stmt
++, pc
);
384 /* Return those elements in the space of "cond" that come after
385 * (based on "sign") an element in "cond" in the final dimension.
387 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
390 isl_map
*previous_to_this
;
393 dim
= isl_set_dim(cond
, isl_dim_set
);
394 space
= isl_space_map_from_set(isl_set_get_space(cond
));
395 previous_to_this
= isl_map_universe(space
);
396 for (i
= 0; i
+ 1 < dim
; ++i
)
397 previous_to_this
= isl_map_equate(previous_to_this
,
398 isl_dim_in
, i
, isl_dim_out
, i
);
400 previous_to_this
= isl_map_order_lt(previous_to_this
,
401 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
403 previous_to_this
= isl_map_order_gt(previous_to_this
,
404 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
406 cond
= isl_set_apply(cond
, previous_to_this
);
411 /* Remove those iterations of "domain" that have an earlier iteration
412 * (based on "sign") in the final dimension where "skip" is satisfied.
413 * If "apply_skip_map" is set, then "skip_map" is first applied
414 * to the embedded skip condition before removing it from the domain.
416 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
417 __isl_take isl_set
*skip
, int sign
,
418 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
421 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
422 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
423 return isl_set_subtract(domain
, after(skip
, sign
));
426 /* Create a single-dimensional multi-affine expression on the domain space
427 * of "pc" that is equal to the final dimension of this domain.
428 * "loop_nr" is the sequence number of the corresponding loop.
429 * If "id" is not NULL, then it is used as the output tuple name.
430 * Otherwise, the name is constructed as L_<loop_nr>.
432 static __isl_give isl_multi_aff
*map_to_last(__isl_keep pet_context
*pc
,
433 int loop_nr
, __isl_keep isl_id
*id
)
443 space
= pet_context_get_space(pc
);
444 pos
= isl_space_dim(space
, isl_dim_set
) - 1;
445 ls
= isl_local_space_from_space(space
);
446 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, pos
);
447 ma
= isl_multi_aff_from_aff(aff
);
450 label
= isl_id_copy(id
);
452 snprintf(name
, sizeof(name
), "L_%d", loop_nr
);
453 label
= isl_id_alloc(pet_context_get_ctx(pc
), name
, NULL
);
455 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, label
);
460 /* Create an affine expression that maps elements
461 * of an array "id_test" to the previous element in the final dimension
462 * (according to "inc"), provided this element belongs to "domain".
463 * That is, create the affine expression
465 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
467 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
468 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
475 isl_multi_pw_aff
*prev
;
477 pos
= isl_set_dim(domain
, isl_dim_set
) - 1;
478 space
= isl_set_get_space(domain
);
479 space
= isl_space_map_from_set(space
);
480 ma
= isl_multi_aff_identity(space
);
481 aff
= isl_multi_aff_get_aff(ma
, pos
);
482 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
483 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
484 domain
= isl_set_preimage_multi_aff(domain
, isl_multi_aff_copy(ma
));
485 prev
= isl_multi_pw_aff_from_multi_aff(ma
);
486 pa
= isl_multi_pw_aff_get_pw_aff(prev
, pos
);
487 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
488 prev
= isl_multi_pw_aff_set_pw_aff(prev
, pos
, pa
);
489 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
494 /* Add an implication to "scop" expressing that if an element of
495 * virtual array "id_test" has value "satisfied" then all previous elements
496 * of this array (in the final dimension) also have that value.
497 * The set of previous elements is bounded by "domain".
498 * If "sign" is negative then the iterator
499 * is decreasing and we express that all subsequent array elements
500 * (but still defined previously) have the same value.
502 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
503 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
510 dim
= isl_set_dim(domain
, isl_dim_set
);
511 domain
= isl_set_set_tuple_id(domain
, id_test
);
512 space
= isl_space_map_from_set(isl_set_get_space(domain
));
513 map
= isl_map_universe(space
);
514 for (i
= 0; i
+ 1 < dim
; ++i
)
515 map
= isl_map_equate(map
, isl_dim_in
, i
, isl_dim_out
, i
);
517 map
= isl_map_order_ge(map
,
518 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
520 map
= isl_map_order_le(map
,
521 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
522 map
= isl_map_intersect_range(map
, domain
);
523 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
528 /* Add a filter to "scop" that imposes that it is only executed
529 * when the variable identified by "id_test" has a zero value
530 * for all previous iterations of "domain".
532 * In particular, add a filter that imposes that the array
533 * has a zero value at the previous iteration of domain and
534 * add an implication that implies that it then has that
535 * value for all previous iterations.
537 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
538 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
539 __isl_take isl_val
*inc
)
541 isl_multi_pw_aff
*prev
;
542 int sign
= isl_val_sgn(inc
);
544 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
545 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
546 scop
= pet_scop_filter(scop
, prev
, 0);
551 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
552 __isl_keep pet_context
*pc
, struct pet_state
*state
);
554 /* Construct a pet_scop for an infinite loop around the given body
555 * within the context "pc".
556 * "loop_id" is the label on the loop or NULL if there is no such label.
558 * The domain of "pc" has already been extended with an infinite loop
562 * We extract a pet_scop for the body and then embed it in a loop with
565 * { [outer,t] -> [t] }
567 * If the body contains any break, then it is taken into
568 * account in apply_affine_break (if the skip condition is affine)
569 * or in scop_add_break (if the skip condition is not affine).
571 * Note that in case of an affine skip condition,
572 * since we are dealing with a loop without loop iterator,
573 * the skip condition cannot refer to the current loop iterator and
574 * so effectively, the effect on the iteration domain is of the form
576 * { [outer,0]; [outer,t] : t >= 1 and not skip }
578 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
579 __isl_keep isl_id
*loop_id
, __isl_keep pet_context
*pc
,
580 struct pet_state
*state
)
586 isl_multi_aff
*sched
;
587 struct pet_scop
*scop
;
588 int has_affine_break
;
591 ctx
= pet_tree_get_ctx(body
);
592 domain
= pet_context_get_domain(pc
);
593 sched
= map_to_last(pc
, state
->n_loop
++, loop_id
);
595 scop
= scop_from_tree(body
, pc
, state
);
597 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
598 if (has_affine_break
)
599 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
600 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
602 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
604 scop
= pet_scop_reset_skips(scop
);
605 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
);
606 if (has_affine_break
) {
607 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
608 scop
= pet_scop_intersect_domain_prefix(scop
,
609 isl_set_copy(domain
));
612 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
614 isl_set_free(domain
);
619 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
624 * within the context "pc".
626 * Extend the domain of "pc" with an extra inner loop
630 * and construct the scop in scop_from_infinite_loop.
632 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
633 __isl_keep pet_context
*pc
, struct pet_state
*state
)
635 struct pet_scop
*scop
;
637 pc
= pet_context_copy(pc
);
638 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
640 pc
= pet_context_add_infinite_loop(pc
);
642 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, tree
->label
, pc
, state
);
644 pet_context_free(pc
);
649 /* Construct a pet_scop for a while loop of the form
654 * within the context "pc".
656 * The domain of "pc" has already been extended with an infinite loop
660 * Here, we add the constraints on the outer loop iterators
661 * implied by "pa" and construct the scop in scop_from_infinite_loop.
662 * Note that the intersection with these constraints
663 * may result in an empty loop.
665 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
666 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
667 struct pet_state
*state
)
669 struct pet_scop
*scop
;
670 isl_set
*dom
, *local
;
673 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
674 dom
= isl_pw_aff_non_zero_set(pa
);
675 local
= isl_set_add_dims(isl_set_copy(dom
), isl_dim_set
, 1);
676 pc
= pet_context_intersect_domain(pc
, local
);
677 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, tree
->label
, pc
, state
);
678 scop
= pet_scop_restrict(scop
, dom
);
679 scop
= pet_scop_restrict_context(scop
, valid
);
681 pet_context_free(pc
);
685 /* Construct a scop for a while, given the scops for the condition
686 * and the body, the filter identifier and the iteration domain of
689 * In particular, the scop for the condition is filtered to depend
690 * on "id_test" evaluating to true for all previous iterations
691 * of the loop, while the scop for the body is filtered to depend
692 * on "id_test" evaluating to true for all iterations up to the
694 * The actual filter only imposes that this virtual array has
695 * value one on the previous or the current iteration.
696 * The fact that this condition also applies to the previous
697 * iterations is enforced by an implication.
699 * These filtered scops are then combined into a single scop,
700 * with the condition scop scheduled before the body scop.
702 * "sign" is positive if the iterator increases and negative
705 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
706 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
707 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
709 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
711 isl_multi_pw_aff
*test_index
;
712 isl_multi_pw_aff
*prev
;
713 int sign
= isl_val_sgn(inc
);
714 struct pet_scop
*scop
;
716 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
717 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
719 space
= isl_space_map_from_set(isl_set_get_space(domain
));
720 test_index
= isl_multi_pw_aff_identity(space
);
721 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
722 isl_id_copy(id_test
));
723 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
725 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
726 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
731 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
732 * evaluating "cond" and writing the result to a virtual scalar,
733 * as expressed by "index".
734 * The expression "cond" has not yet been evaluated in the context of "pc".
735 * Do so within the context "pc".
736 * The location of the statement is set to "loc".
738 static struct pet_scop
*scop_from_non_affine_condition(
739 __isl_take pet_expr
*cond
, int stmt_nr
,
740 __isl_take isl_multi_pw_aff
*index
,
741 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
743 pet_expr
*expr
, *write
;
745 cond
= pet_context_evaluate_expr(pc
, cond
);
747 write
= pet_expr_from_index(index
);
748 write
= pet_expr_access_set_write(write
, 1);
749 write
= pet_expr_access_set_read(write
, 0);
750 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
752 return scop_from_evaluated_expr(expr
, stmt_nr
, loc
, pc
);
755 /* Given that "scop" has an affine skip condition of type pet_skip_now,
756 * apply this skip condition to the domain of "pc".
757 * That is, remove the elements satisfying the skip condition from
758 * the domain of "pc".
760 static __isl_give pet_context
*apply_affine_continue(__isl_take pet_context
*pc
,
761 struct pet_scop
*scop
)
763 isl_set
*domain
, *skip
;
765 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_now
);
766 domain
= pet_context_get_domain(pc
);
767 domain
= isl_set_subtract(domain
, skip
);
768 pc
= pet_context_intersect_domain(pc
, domain
);
773 /* Add a scop for evaluating the loop increment "inc" at the end
774 * of a loop body "scop" within the context "pc".
776 * The skip conditions resulting from continue statements inside
777 * the body do not apply to "inc", but those resulting from break
778 * statements do need to get applied.
780 static struct pet_scop
*scop_add_inc(struct pet_scop
*scop
,
781 __isl_take pet_expr
*inc
, __isl_take pet_loc
*loc
,
782 __isl_keep pet_context
*pc
, struct pet_state
*state
)
784 struct pet_scop
*scop_inc
;
786 pc
= pet_context_copy(pc
);
788 if (pet_scop_has_skip(scop
, pet_skip_later
)) {
789 isl_multi_pw_aff
*skip
;
790 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
791 scop
= pet_scop_set_skip(scop
, pet_skip_now
, skip
);
792 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
793 pc
= apply_affine_continue(pc
, scop
);
795 pet_scop_reset_skip(scop
, pet_skip_now
);
796 scop_inc
= scop_from_expr(inc
, state
->n_stmt
++, loc
, pc
);
797 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_inc
);
799 pet_context_free(pc
);
804 /* Construct a generic while scop, with iteration domain
805 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
806 * "loop_id" is the label on the loop or NULL if there is no such label.
807 * The domain of "pc" has already been extended with this infinite loop
811 * The scop consists of two parts,
812 * one for evaluating the condition "cond" and one for the body.
813 * If "expr_inc" is not NULL, then a scop for evaluating this expression
814 * is added at the end of the body,
815 * after replacing any skip conditions resulting from continue statements
816 * by the skip conditions resulting from break statements (if any).
818 * The schedules are combined as a sequence to reflect that the condition is
819 * evaluated before the body is executed and the body is filtered to depend
820 * on the result of the condition evaluating to true on all iterations
821 * up to the current iteration, while the evaluation of the condition itself
822 * is filtered to depend on the result of the condition evaluating to true
823 * on all previous iterations.
824 * The context of the scop representing the body is dropped
825 * because we don't know how many times the body will be executed,
828 * If the body contains any break, then it is taken into
829 * account in apply_affine_break (if the skip condition is affine)
830 * or in scop_add_break (if the skip condition is not affine).
832 * Note that in case of an affine skip condition,
833 * since we are dealing with a loop without loop iterator,
834 * the skip condition cannot refer to the current loop iterator and
835 * so effectively, the effect on the iteration domain is of the form
837 * { [outer,0]; [outer,t] : t >= 1 and not skip }
839 static struct pet_scop
*scop_from_non_affine_while(__isl_take pet_expr
*cond
,
840 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
841 __isl_keep isl_id
*loop_id
, __isl_take pet_expr
*expr_inc
,
842 __isl_take pet_context
*pc
, struct pet_state
*state
)
845 isl_id
*id_test
, *id_break_test
;
847 isl_multi_pw_aff
*test_index
;
850 isl_multi_aff
*sched
;
851 struct pet_scop
*scop
, *scop_body
;
852 int has_affine_break
;
856 space
= pet_context_get_space(pc
);
857 test_index
= pet_create_test_index(space
, state
->n_test
++);
858 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
859 isl_multi_pw_aff_copy(test_index
),
860 pet_loc_copy(loc
), pc
);
861 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
862 domain
= pet_context_get_domain(pc
);
863 scop
= pet_scop_add_boolean_array(scop
, isl_set_copy(domain
),
864 test_index
, state
->int_size
);
866 sched
= map_to_last(pc
, state
->n_loop
++, loop_id
);
868 scop_body
= scop_from_tree(tree_body
, pc
, state
);
870 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
871 if (has_affine_break
)
872 skip
= pet_scop_get_affine_skip_domain(scop_body
,
874 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
876 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
878 scop_body
= pet_scop_reset_context(scop_body
);
880 scop_body
= scop_add_inc(scop_body
, expr_inc
, loc
, pc
, state
);
883 scop_body
= pet_scop_reset_skips(scop_body
);
885 if (has_affine_break
) {
886 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
887 scop
= pet_scop_intersect_domain_prefix(scop
,
888 isl_set_copy(domain
));
889 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
890 isl_set_copy(domain
));
893 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
894 isl_set_copy(domain
), isl_val_one(ctx
));
895 scop_body
= scop_add_break(scop_body
, id_break_test
,
896 isl_set_copy(domain
), isl_val_one(ctx
));
898 scop
= scop_add_while(scop
, scop_body
, id_test
, isl_set_copy(domain
),
901 scop
= pet_scop_embed(scop
, domain
, sched
);
903 pet_context_free(pc
);
907 /* Check if the while loop is of the form
909 * while (affine expression)
912 * If so, call scop_from_affine_while to construct a scop.
914 * Otherwise, pass control to scop_from_non_affine_while.
916 * "pc" is the context in which the affine expressions in the scop are created.
917 * The domain of "pc" is extended with an infinite loop
921 * before passing control to scop_from_affine_while or
922 * scop_from_non_affine_while.
924 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
925 __isl_keep pet_context
*pc
, struct pet_state
*state
)
933 pc
= pet_context_copy(pc
);
934 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
936 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
937 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
938 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
939 pet_expr_free(cond_expr
);
941 pc
= pet_context_add_infinite_loop(pc
);
946 if (!isl_pw_aff_involves_nan(pa
))
947 return scop_from_affine_while(tree
, pa
, pc
, state
);
949 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
950 pet_tree_get_loc(tree
), tree
->u
.l
.body
,
951 tree
->label
, NULL
, pc
, state
);
953 pet_context_free(pc
);
957 /* Check whether "cond" expresses a simple loop bound
958 * on the final set dimension.
959 * In particular, if "up" is set then "cond" should contain only
960 * upper bounds on the final set dimension.
961 * Otherwise, it should contain only lower bounds.
963 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
967 pos
= isl_set_dim(cond
, isl_dim_set
) - 1;
968 if (isl_val_is_pos(inc
))
969 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, pos
);
971 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, pos
);
974 /* Extend a condition on a given iteration of a loop to one that
975 * imposes the same condition on all previous iterations.
976 * "domain" expresses the lower [upper] bound on the iterations
977 * when inc is positive [negative] in its final dimension.
979 * In particular, we construct the condition (when inc is positive)
981 * forall i' : (domain(i') and i' <= i) => cond(i')
983 * (where "<=" applies to the final dimension)
984 * which is equivalent to
986 * not exists i' : domain(i') and i' <= i and not cond(i')
988 * We construct this set by subtracting the satisfying cond from domain,
991 * { [i'] -> [i] : i' <= i }
993 * and then subtracting the result from domain again.
995 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
996 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
999 isl_map
*previous_to_this
;
1002 dim
= isl_set_dim(cond
, isl_dim_set
);
1003 space
= isl_space_map_from_set(isl_set_get_space(cond
));
1004 previous_to_this
= isl_map_universe(space
);
1005 for (i
= 0; i
+ 1 < dim
; ++i
)
1006 previous_to_this
= isl_map_equate(previous_to_this
,
1007 isl_dim_in
, i
, isl_dim_out
, i
);
1008 if (isl_val_is_pos(inc
))
1009 previous_to_this
= isl_map_order_le(previous_to_this
,
1010 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
1012 previous_to_this
= isl_map_order_ge(previous_to_this
,
1013 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
1015 cond
= isl_set_subtract(isl_set_copy(domain
), cond
);
1016 cond
= isl_set_apply(cond
, previous_to_this
);
1017 cond
= isl_set_subtract(domain
, cond
);
1024 /* Given an initial value of the form
1026 * { [outer,i] -> init(outer) }
1028 * construct a domain of the form
1030 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1032 static __isl_give isl_set
*strided_domain(__isl_take isl_pw_aff
*init
,
1033 __isl_take isl_val
*inc
)
1038 isl_local_space
*ls
;
1041 dim
= isl_pw_aff_dim(init
, isl_dim_in
);
1043 init
= isl_pw_aff_add_dims(init
, isl_dim_in
, 1);
1044 space
= isl_pw_aff_get_domain_space(init
);
1045 ls
= isl_local_space_from_space(space
);
1046 aff
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
1047 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, dim
, inc
);
1048 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
1050 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, dim
- 1);
1051 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
1053 set
= isl_set_lower_bound_si(set
, isl_dim_set
, dim
, 0);
1054 set
= isl_set_project_out(set
, isl_dim_set
, dim
, 1);
1059 /* Assuming "cond" represents a bound on a loop where the loop
1060 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1063 * Under the given assumptions, wrapping is only possible if "cond" allows
1064 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1065 * increasing iterator and 0 in case of a decreasing iterator.
1067 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
1068 __isl_keep isl_val
*inc
)
1075 test
= isl_set_copy(cond
);
1077 ctx
= isl_set_get_ctx(test
);
1078 if (isl_val_is_neg(inc
))
1079 limit
= isl_val_zero(ctx
);
1081 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
1082 limit
= isl_val_2exp(limit
);
1083 limit
= isl_val_sub_ui(limit
, 1);
1086 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
1087 cw
= !isl_set_is_empty(test
);
1097 * construct the following affine expression on this space
1099 * { [outer, v] -> [outer, v mod 2^width] }
1101 * where width is the number of bits used to represent the values
1102 * of the unsigned variable "iv".
1104 static __isl_give isl_multi_aff
*compute_wrapping(__isl_take isl_space
*space
,
1105 __isl_keep pet_expr
*iv
)
1111 dim
= isl_space_dim(space
, isl_dim_set
);
1113 space
= isl_space_map_from_set(space
);
1114 ma
= isl_multi_aff_identity(space
);
1116 aff
= isl_multi_aff_get_aff(ma
, dim
- 1);
1117 aff
= pet_wrap_aff(aff
, pet_expr_get_type_size(iv
));
1118 ma
= isl_multi_aff_set_aff(ma
, dim
- 1, aff
);
1123 /* Given two sets in the space
1127 * where l represents the outer loop iterators, compute the set
1128 * of values of l that ensure that "set1" is a subset of "set2".
1130 * set1 is a subset of set2 if
1132 * forall i: set1(l,i) => set2(l,i)
1136 * not exists i: set1(l,i) and not set2(l,i)
1140 * not exists i: (set1 \ set2)(l,i)
1142 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
1143 __isl_take isl_set
*set2
)
1147 pos
= isl_set_dim(set1
, isl_dim_set
) - 1;
1148 set1
= isl_set_subtract(set1
, set2
);
1149 set1
= isl_set_eliminate(set1
, isl_dim_set
, pos
, 1);
1150 return isl_set_complement(set1
);
1153 /* Compute the set of outer iterator values for which "cond" holds
1154 * on the next iteration of the inner loop for each element of "dom".
1156 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1157 * to the outer loop iterators), plug that into "cond"
1158 * and then compute the set of outer iterators for which "dom" is a subset
1161 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
1162 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
1169 pos
= isl_set_dim(dom
, isl_dim_set
) - 1;
1170 space
= isl_set_get_space(dom
);
1171 space
= isl_space_map_from_set(space
);
1172 ma
= isl_multi_aff_identity(space
);
1173 aff
= isl_multi_aff_get_aff(ma
, pos
);
1174 aff
= isl_aff_add_constant_val(aff
, inc
);
1175 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
1176 cond
= isl_set_preimage_multi_aff(cond
, ma
);
1178 return enforce_subset(dom
, cond
);
1181 /* Construct a pet_scop for the initialization of the iterator
1182 * of the for loop "tree" within the context "pc" (i.e., the context
1185 static __isl_give pet_scop
*scop_from_for_init(__isl_keep pet_tree
*tree
,
1186 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1188 pet_expr
*expr_iv
, *init
;
1191 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
1192 type_size
= pet_expr_get_type_size(expr_iv
);
1193 init
= pet_expr_copy(tree
->u
.l
.init
);
1194 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
1195 return scop_from_expr(init
, state
->n_stmt
++,
1196 pet_tree_get_loc(tree
), pc
);
1199 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1200 * In particular, "pc_init" represents the context of the loop,
1201 * whereas "pc" represents the context of the body of the loop and
1202 * has already had its domain extended with an infinite loop
1206 * The for loop has the form
1208 * for (iv = init; cond; iv += inc)
1219 * except that the skips resulting from any continue statements
1220 * in body do not apply to the increment, but are replaced by the skips
1221 * resulting from break statements.
1223 * If the loop iterator is declared in the for loop, then it is killed before
1224 * and after the loop.
1226 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
1227 __isl_keep pet_context
*pc_init
, __isl_take pet_context
*pc
,
1228 struct pet_state
*state
)
1232 pet_expr
*expr_iv
, *inc
;
1233 struct pet_scop
*scop_init
, *scop
;
1235 struct pet_array
*array
;
1236 struct pet_scop
*scop_kill
;
1238 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1239 pc
= pet_context_clear_value(pc
, iv
);
1241 declared
= tree
->u
.l
.declared
;
1243 scop_init
= scop_from_for_init(tree
, pc_init
, state
);
1245 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
1246 type_size
= pet_expr_get_type_size(expr_iv
);
1247 inc
= pet_expr_copy(tree
->u
.l
.inc
);
1248 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
1250 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
1251 pet_tree_get_loc(tree
), tree
->u
.l
.body
, tree
->label
,
1252 inc
, pet_context_copy(pc
), state
);
1254 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
1256 pet_context_free(pc
);
1261 array
= extract_array(tree
->u
.l
.iv
, pc_init
, state
);
1263 array
->declared
= 1;
1264 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc_init
, state
);
1265 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
1266 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc_init
, state
);
1267 scop_kill
= pet_scop_add_array(scop_kill
, array
);
1268 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
1273 /* Given an access expression "expr", is the variable accessed by
1274 * "expr" assigned anywhere inside "tree"?
1276 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
1281 id
= pet_expr_access_get_id(expr
);
1282 assigned
= pet_tree_writes(tree
, id
);
1288 /* Are all nested access parameters in "pa" allowed given "tree".
1289 * In particular, is none of them written by anywhere inside "tree".
1291 * If "tree" has any continue or break nodes in the current loop level,
1292 * then no nested access parameters are allowed.
1293 * In particular, if there is any nested access in a guard
1294 * for a piece of code containing a "continue", then we want to introduce
1295 * a separate statement for evaluating this guard so that we can express
1296 * that the result is false for all previous iterations.
1298 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
1299 __isl_keep pet_tree
*tree
)
1306 if (!pet_nested_any_in_pw_aff(pa
))
1309 if (pet_tree_has_continue_or_break(tree
))
1312 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1313 for (i
= 0; i
< nparam
; ++i
) {
1314 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1318 if (!pet_nested_in_id(id
)) {
1323 expr
= pet_nested_extract_expr(id
);
1324 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1325 !is_assigned(expr
, tree
);
1327 pet_expr_free(expr
);
1337 /* Internal data structure for collect_local.
1338 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1339 * "local" collects the results.
1341 struct pet_tree_collect_local_data
{
1343 struct pet_state
*state
;
1344 isl_union_set
*local
;
1347 /* Add the variable accessed by "var" to data->local.
1348 * We extract a representation of the variable from
1349 * the pet_array constructed using extract_array
1350 * to ensure consistency with the rest of the scop.
1352 static int add_local(struct pet_tree_collect_local_data
*data
,
1353 __isl_keep pet_expr
*var
)
1355 struct pet_array
*array
;
1358 array
= extract_array(var
, data
->pc
, data
->state
);
1362 universe
= isl_set_universe(isl_set_get_space(array
->extent
));
1363 data
->local
= isl_union_set_add_set(data
->local
, universe
);
1364 pet_array_free(array
);
1369 /* If the node "tree" declares a variable, then add it to
1372 static int extract_local_var(__isl_keep pet_tree
*tree
, void *user
)
1374 enum pet_tree_type type
;
1375 struct pet_tree_collect_local_data
*data
= user
;
1377 type
= pet_tree_get_type(tree
);
1378 if (type
== pet_tree_decl
|| type
== pet_tree_decl_init
)
1379 return add_local(data
, tree
->u
.d
.var
);
1384 /* If the node "tree" is a for loop that declares its induction variable,
1385 * then add it this induction variable to data->local.
1387 static int extract_local_iterator(__isl_keep pet_tree
*tree
, void *user
)
1389 struct pet_tree_collect_local_data
*data
= user
;
1391 if (pet_tree_get_type(tree
) == pet_tree_for
&& tree
->u
.l
.declared
)
1392 return add_local(data
, tree
->u
.l
.iv
);
1397 /* Collect and return all local variables of the for loop represented
1398 * by "tree", with "scop" the corresponding pet_scop.
1399 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1401 * We collect not only the variables that are declared inside "tree",
1402 * but also the loop iterators that are declared anywhere inside
1403 * any possible macro statements in "scop".
1404 * The latter also appear as declared variable in the scop,
1405 * whereas other declared loop iterators only appear implicitly
1406 * in the iteration domains.
1408 static __isl_give isl_union_set
*collect_local(struct pet_scop
*scop
,
1409 __isl_keep pet_tree
*tree
, __isl_keep pet_context
*pc
,
1410 struct pet_state
*state
)
1414 struct pet_tree_collect_local_data data
= { pc
, state
};
1416 ctx
= pet_tree_get_ctx(tree
);
1417 data
.local
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
1419 if (pet_tree_foreach_sub_tree(tree
, &extract_local_var
, &data
) < 0)
1420 return isl_union_set_free(data
.local
);
1422 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1423 pet_tree
*body
= scop
->stmts
[i
]->body
;
1424 if (pet_tree_foreach_sub_tree(body
, &extract_local_iterator
,
1426 return isl_union_set_free(data
.local
);
1432 /* Add an independence to "scop" if the for node "tree" was marked
1434 * "domain" is the set of loop iterators, with the current for loop
1435 * innermost. If "sign" is positive, then the inner iterator increases.
1436 * Otherwise it decreases.
1437 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1439 * If the tree was marked, then collect all local variables and
1440 * add an independence.
1442 static struct pet_scop
*set_independence(struct pet_scop
*scop
,
1443 __isl_keep pet_tree
*tree
, __isl_keep isl_set
*domain
, int sign
,
1444 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1446 isl_union_set
*local
;
1448 if (!tree
->u
.l
.independent
)
1451 local
= collect_local(scop
, tree
, pc
, state
);
1452 scop
= pet_scop_set_independent(scop
, domain
, local
, sign
);
1457 /* Add a scop for assigning to the variable corresponding to the loop
1458 * iterator the result of adding the increment to the loop iterator
1459 * at the end of a loop body "scop" within the context "pc".
1460 * "tree" represents the for loop.
1462 * The increment is of the form
1466 * Note that "iv" on the right hand side will be evaluated in terms
1467 * of the (possibly virtual) loop iterator, i.e., the inner dimension
1468 * of the domain, while "iv" on the left hand side will not be evaluated
1469 * (because it is a write) and will continue to refer to the original
1472 static __isl_give pet_scop
*add_iterator_assignment(__isl_take pet_scop
*scop
,
1473 __isl_keep pet_tree
*tree
, __isl_keep pet_context
*pc
,
1474 struct pet_state
*state
)
1477 pet_expr
*expr
, *iv
, *inc
;
1479 iv
= pet_expr_copy(tree
->u
.l
.iv
);
1480 type_size
= pet_expr_get_type_size(iv
);
1481 iv
= pet_expr_access_set_write(iv
, 0);
1482 iv
= pet_expr_access_set_read(iv
, 1);
1483 inc
= pet_expr_copy(tree
->u
.l
.inc
);
1484 expr
= pet_expr_new_binary(type_size
, pet_op_add
, iv
, inc
);
1485 iv
= pet_expr_copy(tree
->u
.l
.iv
);
1486 expr
= pet_expr_new_binary(type_size
, pet_op_assign
, iv
, expr
);
1488 scop
= scop_add_inc(scop
, expr
, pet_tree_get_loc(tree
), pc
, state
);
1493 /* Construct a pet_scop for a for tree with static affine initialization
1494 * and constant increment within the context "pc".
1495 * The domain of "pc" has already been extended with an (at this point
1496 * unbounded) inner loop iterator corresponding to the current for loop.
1498 * The condition is allowed to contain nested accesses, provided
1499 * they are not being written to inside the body of the loop.
1500 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1501 * essentially treated as a while loop, with iteration domain
1502 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1504 * We extract a pet_scop for the body after intersecting the domain of "pc"
1506 * { [l,i] : i >= init and condition' }
1510 * { [l,i] : i <= init and condition' }
1512 * Where condition' is equal to condition if the latter is
1513 * a simple upper [lower] bound and a condition that is extended
1514 * to apply to all previous iterations otherwise.
1515 * Afterwards, the schedule of the pet_scop is extended with
1523 * If the condition is non-affine, then we drop the condition from the
1524 * iteration domain and instead create a separate statement
1525 * for evaluating the condition. The body is then filtered to depend
1526 * on the result of the condition evaluating to true on all iterations
1527 * up to the current iteration, while the evaluation the condition itself
1528 * is filtered to depend on the result of the condition evaluating to true
1529 * on all previous iterations.
1530 * The context of the scop representing the body is dropped
1531 * because we don't know how many times the body will be executed,
1534 * If the stride of the loop is not 1, then "i >= init" is replaced by
1536 * (exists a: i = init + stride * a and a >= 0)
1538 * If the loop iterator i is unsigned, then wrapping may occur.
1539 * We therefore use a virtual iterator instead that does not wrap.
1540 * However, the condition in the code applies
1541 * to the wrapped value, so we need to change condition(l,i)
1542 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1543 * to the original iterator by the wrapping of the virtual iterator.
1544 * Note that there may be no need to perform this final wrapping
1545 * if the loop condition (after wrapping) satisfies certain conditions.
1546 * However, the is_simple_bound condition is not enough since it doesn't
1547 * check if there even is an upper bound.
1549 * Wrapping on unsigned iterators can be avoided entirely if
1550 * the loop condition is simple, the loop iterator is incremented
1551 * [decremented] by one and the last value before wrapping cannot
1552 * possibly satisfy the loop condition.
1554 * Valid outer iterators for a for loop are those for which the initial
1555 * value itself, the increment on each domain iteration and
1556 * the condition on both the initial value and
1557 * the result of incrementing the iterator for each iteration of the domain
1559 * If the loop condition is non-affine, then we only consider validity
1560 * of the initial value.
1562 * If the loop iterator was not declared inside the loop header,
1563 * then the variable corresponding to this loop iterator is assigned
1564 * the result of adding the increment at the end of the loop body.
1565 * The assignment of the initial value is taken care of by
1566 * scop_from_affine_for_init.
1568 * If the body contains any break, then we keep track of it in "skip"
1569 * (if the skip condition is affine) or it is handled in scop_add_break
1570 * (if the skip condition is not affine).
1571 * Note that the affine break condition needs to be considered with
1572 * respect to previous iterations in the virtual domain (if any).
1574 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1575 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1576 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1577 struct pet_state
*state
)
1580 isl_multi_aff
*sched
;
1581 isl_set
*cond
= NULL
;
1582 isl_set
*skip
= NULL
;
1583 isl_id
*id_test
= NULL
, *id_break_test
;
1584 struct pet_scop
*scop
, *scop_cond
= NULL
;
1591 int has_affine_break
;
1593 isl_map
*rev_wrap
= NULL
;
1594 isl_map
*init_val_map
;
1596 isl_set
*valid_init
;
1597 isl_set
*valid_cond
;
1598 isl_set
*valid_cond_init
;
1599 isl_set
*valid_cond_next
;
1601 pet_expr
*cond_expr
;
1602 pet_context
*pc_nested
;
1604 pos
= pet_context_dim(pc
) - 1;
1606 domain
= pet_context_get_domain(pc
);
1607 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1608 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
1609 pc_nested
= pet_context_copy(pc
);
1610 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1611 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1612 pet_context_free(pc_nested
);
1613 pet_expr_free(cond_expr
);
1615 valid_inc
= isl_pw_aff_domain(pa_inc
);
1617 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1619 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1620 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1622 pa
= isl_pw_aff_free(pa
);
1624 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1625 cond
= isl_pw_aff_non_zero_set(pa
);
1627 cond
= isl_set_universe(isl_set_get_space(domain
));
1629 valid_cond
= isl_set_coalesce(valid_cond
);
1630 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1631 is_virtual
= is_unsigned
&&
1632 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1634 init_val_map
= isl_map_from_pw_aff(isl_pw_aff_copy(init_val
));
1635 init_val_map
= isl_map_equate(init_val_map
, isl_dim_in
, pos
,
1637 valid_cond_init
= enforce_subset(isl_map_domain(init_val_map
),
1638 isl_set_copy(valid_cond
));
1639 if (is_one
&& !is_virtual
) {
1642 isl_pw_aff_free(init_val
);
1643 pa
= pet_expr_extract_comparison(
1644 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1645 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1646 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1647 valid_init
= isl_set_eliminate(valid_init
, isl_dim_set
,
1648 isl_set_dim(domain
, isl_dim_set
) - 1, 1);
1649 cond
= isl_pw_aff_non_zero_set(pa
);
1650 domain
= isl_set_intersect(domain
, cond
);
1654 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1655 strided
= strided_domain(init_val
, isl_val_copy(inc
));
1656 domain
= isl_set_intersect(domain
, strided
);
1660 isl_multi_aff
*wrap
;
1661 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1662 pc
= pet_context_preimage_domain(pc
, wrap
);
1663 rev_wrap
= isl_map_from_multi_aff(wrap
);
1664 rev_wrap
= isl_map_reverse(rev_wrap
);
1665 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1666 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1667 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1669 is_simple
= is_simple_bound(cond
, inc
);
1671 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1672 is_simple
= is_simple_bound(cond
, inc
);
1675 cond
= valid_for_each_iteration(cond
,
1676 isl_set_copy(domain
), isl_val_copy(inc
));
1677 cond
= isl_set_align_params(cond
, isl_set_get_space(domain
));
1678 domain
= isl_set_intersect(domain
, cond
);
1679 sched
= map_to_last(pc
, state
->n_loop
++, tree
->label
);
1680 if (isl_val_is_neg(inc
))
1681 sched
= isl_multi_aff_neg(sched
);
1683 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1685 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1687 pc
= pet_context_intersect_domain(pc
, isl_set_copy(domain
));
1689 if (is_non_affine
) {
1691 isl_multi_pw_aff
*test_index
;
1692 space
= isl_set_get_space(domain
);
1693 test_index
= pet_create_test_index(space
, state
->n_test
++);
1694 scop_cond
= scop_from_non_affine_condition(
1695 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1696 isl_multi_pw_aff_copy(test_index
),
1697 pet_tree_get_loc(tree
), pc
);
1698 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1700 scop_cond
= pet_scop_add_boolean_array(scop_cond
,
1701 isl_set_copy(domain
), test_index
,
1705 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
1706 has_affine_break
= scop
&&
1707 pet_scop_has_affine_skip(scop
, pet_skip_later
);
1708 if (has_affine_break
)
1709 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1710 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
1712 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1713 if (is_non_affine
) {
1714 scop
= pet_scop_reset_context(scop
);
1716 if (!tree
->u
.l
.declared
)
1717 scop
= add_iterator_assignment(scop
, tree
, pc
, state
);
1718 scop
= pet_scop_reset_skips(scop
);
1719 scop
= pet_scop_resolve_nested(scop
);
1720 if (has_affine_break
) {
1721 domain
= apply_affine_break(domain
, skip
, isl_val_sgn(inc
),
1722 is_virtual
, rev_wrap
);
1723 scop
= pet_scop_intersect_domain_prefix(scop
,
1724 isl_set_copy(domain
));
1726 isl_map_free(rev_wrap
);
1728 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
1731 scop
= scop_add_while(scop_cond
, scop
, id_test
,
1732 isl_set_copy(domain
),
1735 scop
= set_independence(scop
, tree
, domain
, isl_val_sgn(inc
),
1737 scop
= pet_scop_embed(scop
, domain
, sched
);
1738 if (is_non_affine
) {
1739 isl_set_free(valid_inc
);
1741 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_next
);
1742 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_init
);
1743 valid_inc
= isl_set_project_out(valid_inc
, isl_dim_set
, pos
, 1);
1744 scop
= pet_scop_restrict_context(scop
, valid_inc
);
1749 valid_init
= isl_set_project_out(valid_init
, isl_dim_set
, pos
, 1);
1750 scop
= pet_scop_restrict_context(scop
, valid_init
);
1752 pet_context_free(pc
);
1756 /* Construct a pet_scop for a for tree with static affine initialization
1757 * and constant increment within the context "pc_init".
1758 * In particular, "pc_init" represents the context of the loop,
1759 * whereas the domain of "pc" has already been extended with an (at this point
1760 * unbounded) inner loop iterator corresponding to the current for loop.
1762 * If the loop iterator was not declared inside the loop header,
1763 * then add an assignment of the initial value to the loop iterator
1764 * before the loop. The construction of a pet_scop for the loop itself,
1765 * including updates to the loop iterator, is handled by scop_from_affine_for.
1767 static __isl_give pet_scop
*scop_from_affine_for_init(__isl_keep pet_tree
*tree
,
1768 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1769 __isl_take isl_val
*inc
, __isl_keep pet_context
*pc_init
,
1770 __isl_take pet_context
*pc
, struct pet_state
*state
)
1772 pet_scop
*scop_init
, *scop
;
1774 if (!tree
->u
.l
.declared
)
1775 scop_init
= scop_from_for_init(tree
, pc_init
, state
);
1777 scop
= scop_from_affine_for(tree
, init_val
, pa_inc
, inc
, pc
, state
);
1779 if (!tree
->u
.l
.declared
)
1780 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
1785 /* Construct a pet_scop for a for statement within the context of "pc".
1787 * We update the context to reflect the writes to the loop variable and
1788 * the writes inside the body.
1790 * Then we check if the initialization of the for loop
1791 * is a static affine value and the increment is a constant.
1792 * If so, we construct the pet_scop using scop_from_affine_for_init.
1793 * Otherwise, we treat the for loop as a while loop
1794 * in scop_from_non_affine_for.
1796 * Note that the initialization and the increment are extracted
1797 * in a context where the current loop iterator has been added
1798 * to the context. If these turn out not be affine, then we
1799 * have reconstruct the body context without an assignment
1800 * to this loop iterator, as this variable will then not be
1801 * treated as a dimension of the iteration domain, but as any
1804 static struct pet_scop
*scop_from_for(__isl_keep pet_tree
*tree
,
1805 __isl_keep pet_context
*init_pc
, struct pet_state
*state
)
1809 isl_pw_aff
*pa_inc
, *init_val
;
1810 pet_context
*pc
, *pc_init_val
;
1815 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1816 pc
= pet_context_copy(init_pc
);
1817 pc
= pet_context_add_inner_iterator(pc
, iv
);
1818 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1820 pc_init_val
= pet_context_copy(pc
);
1821 pc_init_val
= pet_context_clear_value(pc_init_val
, isl_id_copy(iv
));
1822 init_val
= pet_expr_extract_affine(tree
->u
.l
.init
, pc_init_val
);
1823 pet_context_free(pc_init_val
);
1824 pa_inc
= pet_expr_extract_affine(tree
->u
.l
.inc
, pc
);
1825 inc
= pet_extract_cst(pa_inc
);
1826 if (!pa_inc
|| !init_val
|| !inc
)
1828 if (!isl_pw_aff_involves_nan(pa_inc
) &&
1829 !isl_pw_aff_involves_nan(init_val
) && !isl_val_is_nan(inc
))
1830 return scop_from_affine_for_init(tree
, init_val
, pa_inc
, inc
,
1831 init_pc
, pc
, state
);
1833 isl_pw_aff_free(pa_inc
);
1834 isl_pw_aff_free(init_val
);
1836 pet_context_free(pc
);
1838 pc
= pet_context_copy(init_pc
);
1839 pc
= pet_context_add_infinite_loop(pc
);
1840 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1841 return scop_from_non_affine_for(tree
, init_pc
, pc
, state
);
1843 isl_pw_aff_free(pa_inc
);
1844 isl_pw_aff_free(init_val
);
1846 pet_context_free(pc
);
1850 /* Check whether "expr" is an affine constraint within the context "pc".
1852 static int is_affine_condition(__isl_keep pet_expr
*expr
,
1853 __isl_keep pet_context
*pc
)
1858 pa
= pet_expr_extract_affine_condition(expr
, pc
);
1861 is_affine
= !isl_pw_aff_involves_nan(pa
);
1862 isl_pw_aff_free(pa
);
1867 /* Check if the given if statement is a conditional assignement
1868 * with a non-affine condition.
1870 * In particular we check if "stmt" is of the form
1877 * where the condition is non-affine and a is some array or scalar access.
1879 static int is_conditional_assignment(__isl_keep pet_tree
*tree
,
1880 __isl_keep pet_context
*pc
)
1884 pet_expr
*expr1
, *expr2
;
1886 ctx
= pet_tree_get_ctx(tree
);
1887 if (!pet_options_get_detect_conditional_assignment(ctx
))
1889 if (tree
->type
!= pet_tree_if_else
)
1891 if (tree
->u
.i
.then_body
->type
!= pet_tree_expr
)
1893 if (tree
->u
.i
.else_body
->type
!= pet_tree_expr
)
1895 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1896 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1897 if (pet_expr_get_type(expr1
) != pet_expr_op
)
1899 if (pet_expr_get_type(expr2
) != pet_expr_op
)
1901 if (pet_expr_op_get_type(expr1
) != pet_op_assign
)
1903 if (pet_expr_op_get_type(expr2
) != pet_op_assign
)
1905 expr1
= pet_expr_get_arg(expr1
, 0);
1906 expr2
= pet_expr_get_arg(expr2
, 0);
1907 equal
= pet_expr_is_equal(expr1
, expr2
);
1908 pet_expr_free(expr1
);
1909 pet_expr_free(expr2
);
1910 if (equal
< 0 || !equal
)
1912 if (is_affine_condition(tree
->u
.i
.cond
, pc
))
1918 /* Given that "tree" is of the form
1925 * where a is some array or scalar access, construct a pet_scop
1926 * corresponding to this conditional assignment within the context "pc".
1927 * "cond_pa" is an affine expression with nested accesses representing
1930 * The constructed pet_scop then corresponds to the expression
1932 * a = condition ? f(...) : g(...)
1934 * All access relations in f(...) are intersected with condition
1935 * while all access relation in g(...) are intersected with the complement.
1937 static struct pet_scop
*scop_from_conditional_assignment(
1938 __isl_keep pet_tree
*tree
, __isl_take isl_pw_aff
*cond_pa
,
1939 __isl_take pet_context
*pc
, struct pet_state
*state
)
1942 isl_set
*cond
, *comp
;
1943 isl_multi_pw_aff
*index
;
1944 pet_expr
*expr1
, *expr2
;
1945 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
1946 struct pet_scop
*scop
;
1948 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa
));
1949 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa
));
1950 index
= isl_multi_pw_aff_from_pw_aff(cond_pa
);
1952 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1953 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1955 pe_cond
= pet_expr_from_index(index
);
1957 pe_then
= pet_expr_get_arg(expr1
, 1);
1958 pe_then
= pet_context_evaluate_expr(pc
, pe_then
);
1959 pe_then
= pet_expr_restrict(pe_then
, cond
);
1960 pe_else
= pet_expr_get_arg(expr2
, 1);
1961 pe_else
= pet_context_evaluate_expr(pc
, pe_else
);
1962 pe_else
= pet_expr_restrict(pe_else
, comp
);
1963 pe_write
= pet_expr_get_arg(expr1
, 0);
1964 pe_write
= pet_context_evaluate_expr(pc
, pe_write
);
1966 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
1967 type_size
= pet_expr_get_type_size(pe_write
);
1968 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, pe_write
, pe
);
1970 scop
= scop_from_evaluated_expr(pe
, state
->n_stmt
++,
1971 pet_tree_get_loc(tree
), pc
);
1973 pet_context_free(pc
);
1978 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1980 * We create a separate statement that writes the result
1981 * of the non-affine condition to a virtual scalar.
1982 * A constraint requiring the value of this virtual scalar to be one
1983 * is added to the iteration domains of the then branch.
1984 * Similarly, a constraint requiring the value of this virtual scalar
1985 * to be zero is added to the iteration domains of the else branch, if any.
1986 * We combine the schedules as a sequence to ensure that the virtual scalar
1987 * is written before it is read.
1989 * If there are any breaks or continues in the then and/or else
1990 * branches, then we may have to compute a new skip condition.
1991 * This is handled using a pet_skip_info object.
1992 * On initialization, the object checks if skip conditions need
1993 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1994 * adds them in pet_skip_info_add.
1996 static struct pet_scop
*scop_from_non_affine_if(__isl_keep pet_tree
*tree
,
1997 __isl_take pet_context
*pc
, struct pet_state
*state
)
2002 isl_multi_pw_aff
*test_index
;
2003 struct pet_skip_info skip
;
2004 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
2006 has_else
= tree
->type
== pet_tree_if_else
;
2008 space
= pet_context_get_space(pc
);
2009 test_index
= pet_create_test_index(space
, state
->n_test
++);
2010 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
2011 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
2012 pet_tree_get_loc(tree
), pc
);
2013 domain
= pet_context_get_domain(pc
);
2014 scop
= pet_scop_add_boolean_array(scop
, domain
,
2015 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
2017 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
2019 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
2021 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
2023 pet_skip_info_if_extract_index(&skip
, test_index
, pc
, state
);
2025 scop_then
= pet_scop_filter(scop_then
,
2026 isl_multi_pw_aff_copy(test_index
), 1);
2028 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
2029 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
2031 isl_multi_pw_aff_free(test_index
);
2033 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
2035 scop
= pet_skip_info_add(&skip
, scop
);
2037 pet_context_free(pc
);
2041 /* Construct a pet_scop for an affine if statement within the context "pc".
2043 * The condition is added to the iteration domains of the then branch,
2044 * while the opposite of the condition in added to the iteration domains
2045 * of the else branch, if any.
2047 * If there are any breaks or continues in the then and/or else
2048 * branches, then we may have to compute a new skip condition.
2049 * This is handled using a pet_skip_info_if object.
2050 * On initialization, the object checks if skip conditions need
2051 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
2052 * adds them in pet_skip_info_add.
2054 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
2055 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
2056 struct pet_state
*state
)
2060 isl_set
*set
, *complement
;
2062 struct pet_skip_info skip
;
2063 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
2064 pet_context
*pc_body
;
2066 ctx
= pet_tree_get_ctx(tree
);
2068 has_else
= tree
->type
== pet_tree_if_else
;
2070 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
2071 set
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
2073 pc_body
= pet_context_copy(pc
);
2074 pc_body
= pet_context_intersect_domain(pc_body
, isl_set_copy(set
));
2075 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc_body
, state
);
2076 pet_context_free(pc_body
);
2078 pc_body
= pet_context_copy(pc
);
2079 complement
= isl_set_copy(valid
);
2080 complement
= isl_set_subtract(valid
, isl_set_copy(set
));
2081 pc_body
= pet_context_intersect_domain(pc_body
,
2082 isl_set_copy(complement
));
2083 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc_body
, state
);
2084 pet_context_free(pc_body
);
2087 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
2088 pet_skip_info_if_extract_cond(&skip
, cond
, pc
, state
);
2089 isl_pw_aff_free(cond
);
2091 scop
= pet_scop_restrict(scop_then
, set
);
2094 scop_else
= pet_scop_restrict(scop_else
, complement
);
2095 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
2097 scop
= pet_scop_resolve_nested(scop
);
2098 scop
= pet_scop_restrict_context(scop
, valid
);
2100 scop
= pet_skip_info_add(&skip
, scop
);
2102 pet_context_free(pc
);
2106 /* Construct a pet_scop for an if statement within the context "pc".
2108 * If the condition fits the pattern of a conditional assignment,
2109 * then it is handled by scop_from_conditional_assignment.
2110 * Note that the condition is only considered for a conditional assignment
2111 * if it is not static-affine. However, it should still convert
2112 * to an affine expression when nesting is allowed.
2114 * Otherwise, we check if the condition is affine.
2115 * If so, we construct the scop in scop_from_affine_if.
2116 * Otherwise, we construct the scop in scop_from_non_affine_if.
2118 * We allow the condition to be dynamic, i.e., to refer to
2119 * scalars or array elements that may be written to outside
2120 * of the given if statement. These nested accesses are then represented
2121 * as output dimensions in the wrapping iteration domain.
2122 * If it is also written _inside_ the then or else branch, then
2123 * we treat the condition as non-affine.
2124 * As explained in extract_non_affine_if, this will introduce
2125 * an extra statement.
2126 * For aesthetic reasons, we want this statement to have a statement
2127 * number that is lower than those of the then and else branches.
2128 * In order to evaluate if we will need such a statement, however, we
2129 * first construct scops for the then and else branches.
2130 * We therefore reserve a statement number if we might have to
2131 * introduce such an extra statement.
2133 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
2134 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2138 pet_expr
*cond_expr
;
2139 pet_context
*pc_nested
;
2144 has_else
= tree
->type
== pet_tree_if_else
;
2146 pc
= pet_context_copy(pc
);
2147 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
2149 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
2151 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
2152 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
2153 pc_nested
= pet_context_copy(pc
);
2154 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
2155 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
2156 pet_context_free(pc_nested
);
2157 pet_expr_free(cond_expr
);
2160 pet_context_free(pc
);
2164 if (isl_pw_aff_involves_nan(cond
)) {
2165 isl_pw_aff_free(cond
);
2166 return scop_from_non_affine_if(tree
, pc
, state
);
2169 if (is_conditional_assignment(tree
, pc
))
2170 return scop_from_conditional_assignment(tree
, cond
, pc
, state
);
2172 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
2173 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
2174 isl_pw_aff_free(cond
);
2175 return scop_from_non_affine_if(tree
, pc
, state
);
2178 return scop_from_affine_if(tree
, cond
, pc
, state
);
2181 /* Return a one-dimensional multi piecewise affine expression that is equal
2182 * to the constant 1 and is defined over the given domain.
2184 static __isl_give isl_multi_pw_aff
*one_mpa(__isl_take isl_space
*space
)
2186 isl_local_space
*ls
;
2189 ls
= isl_local_space_from_space(space
);
2190 aff
= isl_aff_zero_on_domain(ls
);
2191 aff
= isl_aff_set_constant_si(aff
, 1);
2193 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
2196 /* Construct a pet_scop for a continue statement with the given domain space.
2198 * We simply create an empty scop with a universal pet_skip_now
2199 * skip condition. This skip condition will then be taken into
2200 * account by the enclosing loop construct, possibly after
2201 * being incorporated into outer skip conditions.
2203 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
,
2204 __isl_take isl_space
*space
)
2206 struct pet_scop
*scop
;
2208 scop
= pet_scop_empty(isl_space_copy(space
));
2210 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(space
));
2215 /* Construct a pet_scop for a break statement with the given domain space.
2217 * We simply create an empty scop with both a universal pet_skip_now
2218 * skip condition and a universal pet_skip_later skip condition.
2219 * These skip conditions will then be taken into
2220 * account by the enclosing loop construct, possibly after
2221 * being incorporated into outer skip conditions.
2223 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
,
2224 __isl_take isl_space
*space
)
2226 struct pet_scop
*scop
;
2227 isl_multi_pw_aff
*skip
;
2229 scop
= pet_scop_empty(isl_space_copy(space
));
2231 skip
= one_mpa(space
);
2232 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
2233 isl_multi_pw_aff_copy(skip
));
2234 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
2239 /* Extract a clone of the kill statement "stmt".
2240 * The domain of the clone is given by "domain".
2242 static struct pet_scop
*extract_kill(__isl_keep isl_set
*domain
,
2243 struct pet_stmt
*stmt
, struct pet_state
*state
)
2247 isl_multi_pw_aff
*mpa
;
2250 if (!domain
|| !stmt
)
2253 kill
= pet_tree_expr_get_expr(stmt
->body
);
2254 space
= pet_stmt_get_space(stmt
);
2255 space
= isl_space_map_from_set(space
);
2256 mpa
= isl_multi_pw_aff_identity(space
);
2257 mpa
= isl_multi_pw_aff_reset_tuple_id(mpa
, isl_dim_in
);
2258 kill
= pet_expr_update_domain(kill
, mpa
);
2259 tree
= pet_tree_new_expr(kill
);
2260 tree
= pet_tree_set_loc(tree
, pet_loc_copy(stmt
->loc
));
2261 stmt
= pet_stmt_from_pet_tree(isl_set_copy(domain
),
2262 state
->n_stmt
++, tree
);
2263 return pet_scop_from_pet_stmt(isl_set_get_space(domain
), stmt
);
2266 /* Extract a clone of the kill statements in "scop".
2267 * The domain of each clone is given by "domain".
2268 * "scop" is expected to have been created from a DeclStmt
2269 * and should have (one of) the kill(s) as its first statement.
2270 * If "scop" was created from a declaration group, then there
2271 * may be multiple kill statements inside.
2273 static struct pet_scop
*extract_kills(__isl_keep isl_set
*domain
,
2274 struct pet_scop
*scop
, struct pet_state
*state
)
2277 struct pet_stmt
*stmt
;
2278 struct pet_scop
*kill
;
2281 if (!domain
|| !scop
)
2283 ctx
= isl_set_get_ctx(domain
);
2284 if (scop
->n_stmt
< 1)
2285 isl_die(ctx
, isl_error_internal
,
2286 "expecting at least one statement", return NULL
);
2287 stmt
= scop
->stmts
[0];
2288 if (!pet_stmt_is_kill(stmt
))
2289 isl_die(ctx
, isl_error_internal
,
2290 "expecting kill statement", return NULL
);
2292 kill
= extract_kill(domain
, stmt
, state
);
2294 for (i
= 1; i
< scop
->n_stmt
; ++i
) {
2295 struct pet_scop
*kill_i
;
2297 stmt
= scop
->stmts
[i
];
2298 if (!pet_stmt_is_kill(stmt
))
2301 kill_i
= extract_kill(domain
, stmt
, state
);
2302 kill
= pet_scop_add_par(ctx
, kill
, kill_i
);
2308 /* Has "tree" been created from a DeclStmt?
2309 * That is, is it either a declaration or a group of declarations?
2311 static int tree_is_decl(__isl_keep pet_tree
*tree
)
2318 is_decl
= pet_tree_is_decl(tree
);
2319 if (is_decl
< 0 || is_decl
)
2322 if (tree
->type
!= pet_tree_block
)
2324 if (pet_tree_block_get_block(tree
))
2327 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
2328 is_decl
= tree_is_decl(tree
->u
.b
.child
[i
]);
2329 if (is_decl
< 0 || !is_decl
)
2336 /* Does "tree" represent an assignment to a variable?
2338 * The assignment may be one of
2339 * - a declaration with initialization
2340 * - an expression with a top-level assignment operator
2342 static int is_assignment(__isl_keep pet_tree
*tree
)
2346 if (tree
->type
== pet_tree_decl_init
)
2348 return pet_tree_is_assign(tree
);
2351 /* Update "pc" by taking into account the assignment performed by "tree",
2352 * where "tree" satisfies is_assignment.
2354 * In particular, if the lhs of the assignment is a scalar variable and
2355 * if the rhs is an affine expression, then keep track of this value in "pc"
2356 * so that we can plug it in when we later come across the same variable.
2358 * Any previously assigned value to the variable has already been removed
2359 * by scop_handle_writes.
2361 static __isl_give pet_context
*handle_assignment(__isl_take pet_context
*pc
,
2362 __isl_keep pet_tree
*tree
)
2364 pet_expr
*var
, *val
;
2368 if (pet_tree_get_type(tree
) == pet_tree_decl_init
) {
2369 var
= pet_tree_decl_get_var(tree
);
2370 val
= pet_tree_decl_get_init(tree
);
2373 expr
= pet_tree_expr_get_expr(tree
);
2374 var
= pet_expr_get_arg(expr
, 0);
2375 val
= pet_expr_get_arg(expr
, 1);
2376 pet_expr_free(expr
);
2379 if (!pet_expr_is_scalar_access(var
)) {
2385 pa
= pet_expr_extract_affine(val
, pc
);
2387 pc
= pet_context_free(pc
);
2389 if (!isl_pw_aff_involves_nan(pa
)) {
2390 id
= pet_expr_access_get_id(var
);
2391 pc
= pet_context_set_value(pc
, id
, pa
);
2393 isl_pw_aff_free(pa
);
2401 /* Mark all arrays in "scop" as being exposed.
2403 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
2409 for (i
= 0; i
< scop
->n_array
; ++i
)
2410 scop
->arrays
[i
]->exposed
= 1;
2414 /* Try and construct a pet_scop corresponding to (part of)
2415 * a sequence of statements within the context "pc".
2417 * After extracting a statement, we update "pc"
2418 * based on the top-level assignments in the statement
2419 * so that we can exploit them in subsequent statements in the same block.
2421 * If there are any breaks or continues in the individual statements,
2422 * then we may have to compute a new skip condition.
2423 * This is handled using a pet_skip_info object.
2424 * On initialization, the object checks if skip conditions need
2425 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2426 * adds them in pet_skip_info_add.
2428 * If "block" is set, then we need to insert kill statements at
2429 * the end of the block for any array that has been declared by
2430 * one of the statements in the sequence. Each of these declarations
2431 * results in the construction of a kill statement at the place
2432 * of the declaration, so we simply collect duplicates of
2433 * those kill statements and append these duplicates to the constructed scop.
2435 * If "block" is not set, then any array declared by one of the statements
2436 * in the sequence is marked as being exposed.
2438 * If autodetect is set, then we allow the extraction of only a subrange
2439 * of the sequence of statements. However, if there is at least one statement
2440 * for which we could not construct a scop and the final range contains
2441 * either no statements or at least one kill, then we discard the entire
2444 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
2445 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2451 struct pet_scop
*scop
, *kills
;
2453 ctx
= pet_tree_get_ctx(tree
);
2455 space
= pet_context_get_space(pc
);
2456 domain
= pet_context_get_domain(pc
);
2457 pc
= pet_context_copy(pc
);
2458 scop
= pet_scop_empty(isl_space_copy(space
));
2459 kills
= pet_scop_empty(space
);
2460 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
2461 struct pet_scop
*scop_i
;
2463 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
2464 pc
= apply_affine_continue(pc
, scop
);
2465 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
2466 pc
= scop_handle_writes(scop_i
, pc
);
2467 if (is_assignment(tree
->u
.b
.child
[i
]))
2468 pc
= handle_assignment(pc
, tree
->u
.b
.child
[i
]);
2469 struct pet_skip_info skip
;
2470 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
2471 pet_skip_info_seq_extract(&skip
, pc
, state
);
2472 if (scop_i
&& tree_is_decl(tree
->u
.b
.child
[i
])) {
2473 if (tree
->u
.b
.block
) {
2474 struct pet_scop
*kill
;
2475 kill
= extract_kills(domain
, scop_i
, state
);
2476 kills
= pet_scop_add_par(ctx
, kills
, kill
);
2478 scop_i
= mark_exposed(scop_i
);
2480 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
2482 scop
= pet_skip_info_add(&skip
, scop
);
2487 isl_set_free(domain
);
2489 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
2491 pet_context_free(pc
);
2496 /* Internal data structure for extract_declared_arrays.
2498 * "pc" and "state" are used to create pet_array objects and kill statements.
2499 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2500 * found any declared array.
2501 * "scop" has been initialized by the caller and is used to attach
2502 * the created pet_array objects.
2503 * "kill_before" and "kill_after" are created and updated by
2504 * extract_declared_arrays to collect the kills of the arrays.
2506 struct pet_tree_extract_declared_arrays_data
{
2508 struct pet_state
*state
;
2513 struct pet_scop
*scop
;
2514 struct pet_scop
*kill_before
;
2515 struct pet_scop
*kill_after
;
2518 /* Check if the node "node" declares any array or scalar.
2519 * If so, create the corresponding pet_array and attach it to data->scop.
2520 * Additionally, create two kill statements for the array and add them
2521 * to data->kill_before and data->kill_after.
2523 static int extract_declared_arrays(__isl_keep pet_tree
*node
, void *user
)
2525 enum pet_tree_type type
;
2526 struct pet_tree_extract_declared_arrays_data
*data
= user
;
2527 struct pet_array
*array
;
2528 struct pet_scop
*scop_kill
;
2531 type
= pet_tree_get_type(node
);
2532 if (type
== pet_tree_decl
|| type
== pet_tree_decl_init
)
2533 var
= node
->u
.d
.var
;
2534 else if (type
== pet_tree_for
&& node
->u
.l
.declared
)
2539 array
= extract_array(var
, data
->pc
, data
->state
);
2541 array
->declared
= 1;
2542 data
->scop
= pet_scop_add_array(data
->scop
, array
);
2544 scop_kill
= kill(pet_tree_get_loc(node
), array
, data
->pc
, data
->state
);
2546 data
->kill_before
= scop_kill
;
2548 data
->kill_before
= pet_scop_add_par(data
->ctx
,
2549 data
->kill_before
, scop_kill
);
2551 scop_kill
= kill(pet_tree_get_loc(node
), array
, data
->pc
, data
->state
);
2553 data
->kill_after
= scop_kill
;
2555 data
->kill_after
= pet_scop_add_par(data
->ctx
,
2556 data
->kill_after
, scop_kill
);
2563 /* Convert a pet_tree that consists of more than a single leaf
2564 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2565 * Do so within the context of "pc", taking into account the writes inside
2566 * "tree". That is, first clear any previously assigned values to variables
2567 * that are written by "tree".
2569 * After constructing the core scop, we also look for any arrays (or scalars)
2570 * that are declared inside "tree". Each of those arrays is marked as
2571 * having been declared and kill statements for these arrays
2572 * are introduced before and after the core scop.
2573 * Note that the input tree is not a leaf so that the declaration
2574 * cannot occur at the outer level.
2576 static struct pet_scop
*scop_from_tree_macro(__isl_take pet_tree
*tree
,
2577 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2579 struct pet_tree_extract_declared_arrays_data data
= { pc
, state
};
2581 data
.pc
= pet_context_copy(data
.pc
);
2582 data
.pc
= pet_context_clear_writes_in_tree(data
.pc
, tree
);
2583 data
.scop
= scop_from_unevaluated_tree(pet_tree_copy(tree
),
2584 state
->n_stmt
++, data
.pc
);
2587 data
.ctx
= pet_context_get_ctx(data
.pc
);
2588 if (pet_tree_foreach_sub_tree(tree
, &extract_declared_arrays
,
2590 data
.scop
= pet_scop_free(data
.scop
);
2591 pet_tree_free(tree
);
2592 pet_context_free(data
.pc
);
2597 data
.scop
= pet_scop_add_seq(data
.ctx
, data
.kill_before
, data
.scop
);
2598 data
.scop
= pet_scop_add_seq(data
.ctx
, data
.scop
, data
.kill_after
);
2603 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2604 * within the context "pc" by calling the appropriate function
2605 * based on the type of "tree".
2607 * If the initially constructed pet_scop turns out to involve
2608 * dynamic control and if the user has requested an encapsulation
2609 * of all dynamic control, then this pet_scop is discarded and
2610 * a new pet_scop is created with a single statement representing
2611 * the entire "tree".
2612 * However, if the scop contains any active continue or break,
2613 * then we need to include the loop containing the continue or break
2614 * in the encapsulation. We therefore postpone the encapsulation
2615 * until we have constructed a pet_scop for this enclosing loop.
2617 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
2618 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2621 struct pet_scop
*scop
= NULL
;
2626 ctx
= pet_tree_get_ctx(tree
);
2627 switch (tree
->type
) {
2628 case pet_tree_error
:
2630 case pet_tree_block
:
2631 return scop_from_block(tree
, pc
, state
);
2632 case pet_tree_break
:
2633 return scop_from_break(tree
, pet_context_get_space(pc
));
2634 case pet_tree_continue
:
2635 return scop_from_continue(tree
, pet_context_get_space(pc
));
2637 case pet_tree_decl_init
:
2638 return scop_from_decl(tree
, pc
, state
);
2640 return scop_from_tree_expr(tree
, pc
, state
);
2642 case pet_tree_if_else
:
2643 scop
= scop_from_if(tree
, pc
, state
);
2646 scop
= scop_from_for(tree
, pc
, state
);
2648 case pet_tree_while
:
2649 scop
= scop_from_while(tree
, pc
, state
);
2651 case pet_tree_infinite_loop
:
2652 scop
= scop_from_infinite_for(tree
, pc
, state
);
2659 if (!pet_options_get_encapsulate_dynamic_control(ctx
) ||
2660 !pet_scop_has_data_dependent_conditions(scop
) ||
2661 pet_scop_has_var_skip(scop
, pet_skip_now
))
2664 pet_scop_free(scop
);
2665 return scop_from_tree_macro(pet_tree_copy(tree
), pc
, state
);
2668 /* If "tree" has a label that is of the form S_<nr>, then make
2669 * sure that state->n_stmt is greater than nr to ensure that
2670 * we will not generate S_<nr> ourselves.
2672 static int set_first_stmt(__isl_keep pet_tree
*tree
, void *user
)
2674 struct pet_state
*state
= user
;
2682 name
= isl_id_get_name(tree
->label
);
2683 if (strncmp(name
, "S_", 2) != 0)
2685 nr
= atoi(name
+ 2);
2686 if (nr
>= state
->n_stmt
)
2687 state
->n_stmt
= nr
+ 1;
2692 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2693 * "int_size" is the number of bytes need to represent an integer.
2694 * "extract_array" is a callback that we can use to create a pet_array
2695 * that corresponds to the variable accessed by an expression.
2697 * Initialize the global state, construct a context and then
2698 * construct the pet_scop by recursively visiting the tree.
2700 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2702 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
2703 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
2704 __isl_keep pet_context
*pc
, void *user
), void *user
,
2705 __isl_keep pet_context
*pc
)
2707 struct pet_scop
*scop
;
2708 struct pet_state state
= { 0 };
2713 state
.ctx
= pet_tree_get_ctx(tree
);
2714 state
.int_size
= int_size
;
2715 state
.extract_array
= extract_array
;
2717 if (pet_tree_foreach_sub_tree(tree
, &set_first_stmt
, &state
) < 0)
2718 tree
= pet_tree_free(tree
);
2720 scop
= scop_from_tree(tree
, pc
, &state
);
2721 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
2723 pet_tree_free(tree
);
2726 scop
->context
= isl_set_params(scop
->context
);