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
35 #include <isl/id_to_pw_aff.h>
44 #include "tree2scop.h"
46 /* Update "pc" by taking into account the writes in "stmt".
47 * That is, clear any previously assigned values to variables
48 * that are written by "stmt".
50 static __isl_give pet_context
*handle_writes(struct pet_stmt
*stmt
,
51 __isl_take pet_context
*pc
)
53 return pet_context_clear_writes_in_expr(pc
, stmt
->body
);
56 /* Update "pc" based on the write accesses in "scop".
58 static __isl_give pet_context
*scop_handle_writes(struct pet_scop
*scop
,
59 __isl_take pet_context
*pc
)
64 return pet_context_free(pc
);
65 for (i
= 0; i
< scop
->n_stmt
; ++i
)
66 pc
= handle_writes(scop
->stmts
[i
], pc
);
71 /* Convert a top-level pet_expr to a pet_scop with one statement
72 * within the context "pc".
73 * "expr" has already been evaluated in the context of "pc".
74 * This mainly involves resolving nested expression parameters
75 * and setting the name of the iteration space.
76 * The name is given by "label" if it is non-NULL. Otherwise,
77 * it is of the form S_<stmt_nr>.
78 * The location of the statement is set to "loc".
80 static struct pet_scop
*scop_from_evaluated_expr(__isl_take pet_expr
*expr
,
81 __isl_take isl_id
*label
, int stmt_nr
, __isl_take pet_loc
*loc
,
82 __isl_keep pet_context
*pc
)
88 space
= pet_context_get_space(pc
);
90 expr
= pet_expr_resolve_nested(expr
, space
);
91 expr
= pet_expr_resolve_assume(expr
, pc
);
92 domain
= pet_context_get_domain(pc
);
93 ps
= pet_stmt_from_pet_expr(domain
, loc
, label
, stmt_nr
, expr
);
94 return pet_scop_from_pet_stmt(space
, ps
);
97 /* Convert a top-level pet_expr to a pet_scop with one statement
98 * within the context "pc", where "expr" has not yet been evaluated
99 * in the context of "pc".
100 * We evaluate "expr" in the context of "pc" and continue with
101 * scop_from_evaluated_expr.
102 * The statement name is given by "label" if it is non-NULL. Otherwise,
103 * it is of the form S_<stmt_nr>.
104 * The location of the statement is set to "loc".
106 static struct pet_scop
*scop_from_expr(__isl_take pet_expr
*expr
,
107 __isl_take isl_id
*label
, int stmt_nr
, __isl_take pet_loc
*loc
,
108 __isl_keep pet_context
*pc
)
110 expr
= pet_context_evaluate_expr(pc
, expr
);
111 return scop_from_evaluated_expr(expr
, label
, stmt_nr
, loc
, pc
);
114 /* Construct a pet_scop with a single statement killing the entire
116 * The location of the statement is set to "loc".
118 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
119 __isl_keep pet_context
*pc
, struct pet_state
*state
)
124 isl_multi_pw_aff
*index
;
127 struct pet_scop
*scop
;
131 ctx
= isl_set_get_ctx(array
->extent
);
132 access
= isl_map_from_range(isl_set_copy(array
->extent
));
133 id
= isl_set_get_tuple_id(array
->extent
);
134 space
= isl_space_alloc(ctx
, 0, 0, 0);
135 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
136 index
= isl_multi_pw_aff_zero(space
);
137 expr
= pet_expr_kill_from_access_and_index(access
, index
);
138 return scop_from_expr(expr
, NULL
, state
->n_stmt
++, loc
, pc
);
144 /* Construct and return a pet_array corresponding to the variable
145 * accessed by "access" by calling the extract_array callback.
147 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
148 __isl_keep pet_context
*pc
, struct pet_state
*state
)
150 return state
->extract_array(access
, pc
, state
->user
);
153 /* Construct a pet_scop for a (single) variable declaration
154 * within the context "pc".
156 * The scop contains the variable being declared (as an array)
157 * and a statement killing the array.
159 * If the declaration comes with an initialization, then the scop
160 * also contains an assignment to the variable.
162 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
163 __isl_keep pet_context
*pc
, struct pet_state
*state
)
167 struct pet_array
*array
;
168 struct pet_scop
*scop_decl
, *scop
;
169 pet_expr
*lhs
, *rhs
, *pe
;
171 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
174 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
175 scop_decl
= pet_scop_add_array(scop_decl
, array
);
177 if (tree
->type
!= pet_tree_decl_init
)
180 lhs
= pet_expr_copy(tree
->u
.d
.var
);
181 rhs
= pet_expr_copy(tree
->u
.d
.init
);
182 type_size
= pet_expr_get_type_size(lhs
);
183 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
184 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
185 pet_tree_get_loc(tree
), pc
);
187 scop_decl
= pet_scop_prefix(scop_decl
, 0);
188 scop
= pet_scop_prefix(scop
, 1);
190 ctx
= pet_tree_get_ctx(tree
);
191 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
196 /* Return those elements in the space of "cond" that come after
197 * (based on "sign") an element in "cond" in the final dimension.
199 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
202 isl_map
*previous_to_this
;
205 dim
= isl_set_dim(cond
, isl_dim_set
);
206 space
= isl_space_map_from_set(isl_set_get_space(cond
));
207 previous_to_this
= isl_map_universe(space
);
208 for (i
= 0; i
+ 1 < dim
; ++i
)
209 previous_to_this
= isl_map_equate(previous_to_this
,
210 isl_dim_in
, i
, isl_dim_out
, i
);
212 previous_to_this
= isl_map_order_lt(previous_to_this
,
213 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
215 previous_to_this
= isl_map_order_gt(previous_to_this
,
216 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
218 cond
= isl_set_apply(cond
, previous_to_this
);
223 /* Remove those iterations of "domain" that have an earlier iteration
224 * (based on "sign") in the final dimension where "skip" is satisfied.
225 * If "apply_skip_map" is set, then "skip_map" is first applied
226 * to the embedded skip condition before removing it from the domain.
228 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
229 __isl_take isl_set
*skip
, int sign
,
230 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
233 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
234 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
235 return isl_set_subtract(domain
, after(skip
, sign
));
238 /* Create an affine expression on the domain space of "pc" that
239 * is equal to the final dimension of this domain.
241 static __isl_give isl_aff
*map_to_last(__isl_keep pet_context
*pc
)
247 space
= pet_context_get_space(pc
);
248 pos
= isl_space_dim(space
, isl_dim_set
) - 1;
249 ls
= isl_local_space_from_space(space
);
250 return isl_aff_var_on_domain(ls
, isl_dim_set
, pos
);
253 /* Create an affine expression that maps elements
254 * of an array "id_test" to the previous element in the final dimension
255 * (according to "inc"), provided this element belongs to "domain".
256 * That is, create the affine expression
258 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
260 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
261 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
268 isl_multi_pw_aff
*prev
;
270 pos
= isl_set_dim(domain
, isl_dim_set
) - 1;
271 space
= isl_set_get_space(domain
);
272 space
= isl_space_map_from_set(space
);
273 ma
= isl_multi_aff_identity(space
);
274 aff
= isl_multi_aff_get_aff(ma
, pos
);
275 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
276 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
277 domain
= isl_set_preimage_multi_aff(domain
, isl_multi_aff_copy(ma
));
278 prev
= isl_multi_pw_aff_from_multi_aff(ma
);
279 pa
= isl_multi_pw_aff_get_pw_aff(prev
, pos
);
280 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
281 prev
= isl_multi_pw_aff_set_pw_aff(prev
, pos
, pa
);
282 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
287 /* Add an implication to "scop" expressing that if an element of
288 * virtual array "id_test" has value "satisfied" then all previous elements
289 * of this array (in the final dimension) also have that value.
290 * The set of previous elements is bounded by "domain".
291 * If "sign" is negative then the iterator
292 * is decreasing and we express that all subsequent array elements
293 * (but still defined previously) have the same value.
295 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
296 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
303 dim
= isl_set_dim(domain
, isl_dim_set
);
304 domain
= isl_set_set_tuple_id(domain
, id_test
);
305 space
= isl_space_map_from_set(isl_set_get_space(domain
));
306 map
= isl_map_universe(space
);
307 for (i
= 0; i
+ 1 < dim
; ++i
)
308 map
= isl_map_equate(map
, isl_dim_in
, i
, isl_dim_out
, i
);
310 map
= isl_map_order_ge(map
,
311 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
313 map
= isl_map_order_le(map
,
314 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
315 map
= isl_map_intersect_range(map
, domain
);
316 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
321 /* Add a filter to "scop" that imposes that it is only executed
322 * when the variable identified by "id_test" has a zero value
323 * for all previous iterations of "domain".
325 * In particular, add a filter that imposes that the array
326 * has a zero value at the previous iteration of domain and
327 * add an implication that implies that it then has that
328 * value for all previous iterations.
330 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
331 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
332 __isl_take isl_val
*inc
)
334 isl_multi_pw_aff
*prev
;
335 int sign
= isl_val_sgn(inc
);
337 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
338 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
339 scop
= pet_scop_filter(scop
, prev
, 0);
344 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
345 __isl_keep pet_context
*pc
, struct pet_state
*state
);
347 /* Construct a pet_scop for an infinite loop around the given body
348 * within the context "pc".
350 * The domain of "pc" has already been extended with an infinite loop
354 * We extract a pet_scop for the body and then embed it in a loop with
357 * { [outer,t] -> [t] }
359 * If the body contains any break, then it is taken into
360 * account in apply_affine_break (if the skip condition is affine)
361 * or in scop_add_break (if the skip condition is not affine).
363 * Note that in case of an affine skip condition,
364 * since we are dealing with a loop without loop iterator,
365 * the skip condition cannot refer to the current loop iterator and
366 * so effectively, the effect on the iteration domain is of the form
368 * { [outer,0]; [outer,t] : t >= 1 and not skip }
370 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
371 __isl_keep pet_context
*pc
, struct pet_state
*state
)
378 struct pet_scop
*scop
;
379 int has_affine_break
;
382 ctx
= pet_tree_get_ctx(body
);
383 domain
= pet_context_get_domain(pc
);
384 sched
= map_to_last(pc
);
386 scop
= scop_from_tree(body
, pc
, state
);
388 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
389 if (has_affine_break
)
390 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
391 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
393 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
395 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
);
396 if (has_affine_break
) {
397 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
398 scop
= pet_scop_intersect_domain_prefix(scop
,
399 isl_set_copy(domain
));
402 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
404 isl_set_free(domain
);
409 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
414 * within the context "pc".
416 * Extend the domain of "pc" with an extra inner loop
420 * and construct the scop in scop_from_infinite_loop.
422 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
423 __isl_keep pet_context
*pc
, struct pet_state
*state
)
425 struct pet_scop
*scop
;
427 pc
= pet_context_copy(pc
);
428 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
430 pc
= pet_context_add_infinite_loop(pc
);
432 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
434 pet_context_free(pc
);
439 /* Construct a pet_scop for a while loop of the form
444 * within the context "pc".
446 * The domain of "pc" has already been extended with an infinite loop
450 * Here, we add the constraints on the outer loop iterators
451 * implied by "pa" and construct the scop in scop_from_infinite_loop.
452 * Note that the intersection with these constraints
453 * may result in an empty loop.
455 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
456 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
457 struct pet_state
*state
)
459 struct pet_scop
*scop
;
460 isl_set
*dom
, *local
;
463 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
464 dom
= isl_pw_aff_non_zero_set(pa
);
465 local
= isl_set_add_dims(isl_set_copy(dom
), isl_dim_set
, 1);
466 pc
= pet_context_intersect_domain(pc
, local
);
467 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
468 scop
= pet_scop_restrict(scop
, dom
);
469 scop
= pet_scop_restrict_context(scop
, valid
);
471 pet_context_free(pc
);
475 /* Construct a scop for a while, given the scops for the condition
476 * and the body, the filter identifier and the iteration domain of
479 * In particular, the scop for the condition is filtered to depend
480 * on "id_test" evaluating to true for all previous iterations
481 * of the loop, while the scop for the body is filtered to depend
482 * on "id_test" evaluating to true for all iterations up to the
484 * The actual filter only imposes that this virtual array has
485 * value one on the previous or the current iteration.
486 * The fact that this condition also applies to the previous
487 * iterations is enforced by an implication.
489 * These filtered scops are then combined into a single scop.
491 * "sign" is positive if the iterator increases and negative
494 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
495 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
496 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
498 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
500 isl_multi_pw_aff
*test_index
;
501 isl_multi_pw_aff
*prev
;
502 int sign
= isl_val_sgn(inc
);
503 struct pet_scop
*scop
;
505 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
506 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
508 space
= isl_space_map_from_set(isl_set_get_space(domain
));
509 test_index
= isl_multi_pw_aff_identity(space
);
510 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
511 isl_id_copy(id_test
));
512 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
514 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
515 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
520 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
521 * evaluating "cond" and writing the result to a virtual scalar,
522 * as expressed by "index".
523 * The expression "cond" has not yet been evaluated in the context of "pc".
524 * Do so within the context "pc".
525 * The location of the statement is set to "loc".
527 static struct pet_scop
*scop_from_non_affine_condition(
528 __isl_take pet_expr
*cond
, int stmt_nr
,
529 __isl_take isl_multi_pw_aff
*index
,
530 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
532 pet_expr
*expr
, *write
;
534 cond
= pet_context_evaluate_expr(pc
, cond
);
536 write
= pet_expr_from_index(index
);
537 write
= pet_expr_access_set_write(write
, 1);
538 write
= pet_expr_access_set_read(write
, 0);
539 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
541 return scop_from_evaluated_expr(expr
, NULL
, stmt_nr
, loc
, pc
);
544 /* Construct a generic while scop, with iteration domain
545 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
546 * The domain of "pc" has already been extended with this infinite loop
550 * The scop consists of two parts,
551 * one for evaluating the condition "cond" and one for the body.
552 * If "expr_inc" is not NULL, then a scop for evaluating this expression
553 * is added at the end of the body,
554 * after replacing any skip conditions resulting from continue statements
555 * by the skip conditions resulting from break statements (if any).
557 * The schedule is adjusted to reflect that the condition is evaluated
558 * before the body is executed and the body is filtered to depend
559 * on the result of the condition evaluating to true on all iterations
560 * up to the current iteration, while the evaluation of the condition itself
561 * is filtered to depend on the result of the condition evaluating to true
562 * on all previous iterations.
563 * The context of the scop representing the body is dropped
564 * because we don't know how many times the body will be executed,
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_non_affine_while(__isl_take pet_expr
*cond
,
579 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
580 __isl_take pet_expr
*expr_inc
, __isl_take pet_context
*pc
,
581 struct pet_state
*state
)
584 isl_id
*id_test
, *id_break_test
;
586 isl_multi_pw_aff
*test_index
;
590 struct pet_scop
*scop
, *scop_body
;
591 int has_affine_break
;
595 space
= pet_context_get_space(pc
);
596 test_index
= pet_create_test_index(space
, state
->n_test
++);
597 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
598 isl_multi_pw_aff_copy(test_index
),
599 pet_loc_copy(loc
), pc
);
600 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
601 domain
= pet_context_get_domain(pc
);
602 scop
= pet_scop_add_boolean_array(scop
, isl_set_copy(domain
),
603 test_index
, state
->int_size
);
605 sched
= map_to_last(pc
);
607 scop_body
= scop_from_tree(tree_body
, pc
, state
);
609 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
610 if (has_affine_break
)
611 skip
= pet_scop_get_affine_skip_domain(scop_body
,
613 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
615 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
617 scop
= pet_scop_prefix(scop
, 0);
618 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_aff_copy(sched
));
619 scop_body
= pet_scop_reset_context(scop_body
);
620 scop_body
= pet_scop_prefix(scop_body
, 1);
622 struct pet_scop
*scop_inc
;
623 scop_inc
= scop_from_expr(expr_inc
, NULL
, state
->n_stmt
++,
625 scop_inc
= pet_scop_prefix(scop_inc
, 2);
626 if (pet_scop_has_skip(scop_body
, pet_skip_later
)) {
627 isl_multi_pw_aff
*skip
;
628 skip
= pet_scop_get_skip(scop_body
, pet_skip_later
);
629 scop_body
= pet_scop_set_skip(scop_body
,
632 pet_scop_reset_skip(scop_body
, pet_skip_now
);
633 scop_body
= pet_scop_add_seq(ctx
, scop_body
, scop_inc
);
636 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
), sched
);
638 if (has_affine_break
) {
639 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
640 scop
= pet_scop_intersect_domain_prefix(scop
,
641 isl_set_copy(domain
));
642 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
643 isl_set_copy(domain
));
646 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
647 isl_set_copy(domain
), isl_val_one(ctx
));
648 scop_body
= scop_add_break(scop_body
, id_break_test
,
649 isl_set_copy(domain
), isl_val_one(ctx
));
651 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
654 pet_context_free(pc
);
658 /* Check if the while loop is of the form
660 * while (affine expression)
663 * If so, call scop_from_affine_while to construct a scop.
665 * Otherwise, pass control to scop_from_non_affine_while.
667 * "pc" is the context in which the affine expressions in the scop are created.
668 * The domain of "pc" is extended with an infinite loop
672 * before passing control to scop_from_affine_while or
673 * scop_from_non_affine_while.
675 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
676 __isl_keep pet_context
*pc
, struct pet_state
*state
)
684 pc
= pet_context_copy(pc
);
685 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
687 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
688 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
689 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
690 pet_expr_free(cond_expr
);
692 pc
= pet_context_add_infinite_loop(pc
);
697 if (!isl_pw_aff_involves_nan(pa
))
698 return scop_from_affine_while(tree
, pa
, pc
, state
);
700 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
701 pet_tree_get_loc(tree
), tree
->u
.l
.body
, NULL
,
704 pet_context_free(pc
);
708 /* Check whether "cond" expresses a simple loop bound
709 * on the final set dimension.
710 * In particular, if "up" is set then "cond" should contain only
711 * upper bounds on the final set dimension.
712 * Otherwise, it should contain only lower bounds.
714 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
718 pos
= isl_set_dim(cond
, isl_dim_set
) - 1;
719 if (isl_val_is_pos(inc
))
720 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, pos
);
722 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, pos
);
725 /* Extend a condition on a given iteration of a loop to one that
726 * imposes the same condition on all previous iterations.
727 * "domain" expresses the lower [upper] bound on the iterations
728 * when inc is positive [negative] in its final dimension.
730 * In particular, we construct the condition (when inc is positive)
732 * forall i' : (domain(i') and i' <= i) => cond(i')
734 * (where "<=" applies to the final dimension)
735 * which is equivalent to
737 * not exists i' : domain(i') and i' <= i and not cond(i')
739 * We construct this set by subtracting the satisfying cond from domain,
742 * { [i'] -> [i] : i' <= i }
744 * and then subtracting the result from domain again.
746 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
747 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
750 isl_map
*previous_to_this
;
753 dim
= isl_set_dim(cond
, isl_dim_set
);
754 space
= isl_space_map_from_set(isl_set_get_space(cond
));
755 previous_to_this
= isl_map_universe(space
);
756 for (i
= 0; i
+ 1 < dim
; ++i
)
757 previous_to_this
= isl_map_equate(previous_to_this
,
758 isl_dim_in
, i
, isl_dim_out
, i
);
759 if (isl_val_is_pos(inc
))
760 previous_to_this
= isl_map_order_le(previous_to_this
,
761 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
763 previous_to_this
= isl_map_order_ge(previous_to_this
,
764 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
766 cond
= isl_set_subtract(isl_set_copy(domain
), cond
);
767 cond
= isl_set_apply(cond
, previous_to_this
);
768 cond
= isl_set_subtract(domain
, cond
);
775 /* Given an initial value of the form
777 * { [outer,i] -> init(outer) }
779 * construct a domain of the form
781 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
783 static __isl_give isl_set
*strided_domain(__isl_take isl_pw_aff
*init
,
784 __isl_take isl_val
*inc
)
792 dim
= isl_pw_aff_dim(init
, isl_dim_in
);
794 init
= isl_pw_aff_add_dims(init
, isl_dim_in
, 1);
795 space
= isl_pw_aff_get_domain_space(init
);
796 ls
= isl_local_space_from_space(space
);
797 aff
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
798 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, dim
, inc
);
799 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
801 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, dim
- 1);
802 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
804 set
= isl_set_lower_bound_si(set
, isl_dim_set
, dim
, 0);
805 set
= isl_set_project_out(set
, isl_dim_set
, dim
, 1);
810 /* Assuming "cond" represents a bound on a loop where the loop
811 * iterator "iv" is incremented (or decremented) by one, check if wrapping
814 * Under the given assumptions, wrapping is only possible if "cond" allows
815 * for the last value before wrapping, i.e., 2^width - 1 in case of an
816 * increasing iterator and 0 in case of a decreasing iterator.
818 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
819 __isl_keep isl_val
*inc
)
826 test
= isl_set_copy(cond
);
828 ctx
= isl_set_get_ctx(test
);
829 if (isl_val_is_neg(inc
))
830 limit
= isl_val_zero(ctx
);
832 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
833 limit
= isl_val_2exp(limit
);
834 limit
= isl_val_sub_ui(limit
, 1);
837 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
838 cw
= !isl_set_is_empty(test
);
848 * construct the following affine expression on this space
850 * { [outer, v] -> [outer, v mod 2^width] }
852 * where width is the number of bits used to represent the values
853 * of the unsigned variable "iv".
855 static __isl_give isl_multi_aff
*compute_wrapping(__isl_take isl_space
*space
,
856 __isl_keep pet_expr
*iv
)
864 dim
= isl_space_dim(space
, isl_dim_set
);
866 ctx
= isl_space_get_ctx(space
);
867 mod
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
868 mod
= isl_val_2exp(mod
);
870 space
= isl_space_map_from_set(space
);
871 ma
= isl_multi_aff_identity(space
);
873 aff
= isl_multi_aff_get_aff(ma
, dim
- 1);
874 aff
= isl_aff_mod_val(aff
, mod
);
875 ma
= isl_multi_aff_set_aff(ma
, dim
- 1, aff
);
880 /* Given two sets in the space
884 * where l represents the outer loop iterators, compute the set
885 * of values of l that ensure that "set1" is a subset of "set2".
887 * set1 is a subset of set2 if
889 * forall i: set1(l,i) => set2(l,i)
893 * not exists i: set1(l,i) and not set2(l,i)
897 * not exists i: (set1 \ set2)(l,i)
899 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
900 __isl_take isl_set
*set2
)
904 pos
= isl_set_dim(set1
, isl_dim_set
) - 1;
905 set1
= isl_set_subtract(set1
, set2
);
906 set1
= isl_set_eliminate(set1
, isl_dim_set
, pos
, 1);
907 return isl_set_complement(set1
);
910 /* Compute the set of outer iterator values for which "cond" holds
911 * on the next iteration of the inner loop for each element of "dom".
913 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
914 * to the outer loop iterators), plug that into "cond"
915 * and then compute the set of outer iterators for which "dom" is a subset
918 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
919 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
926 pos
= isl_set_dim(dom
, isl_dim_set
) - 1;
927 space
= isl_set_get_space(dom
);
928 space
= isl_space_map_from_set(space
);
929 ma
= isl_multi_aff_identity(space
);
930 aff
= isl_multi_aff_get_aff(ma
, pos
);
931 aff
= isl_aff_add_constant_val(aff
, inc
);
932 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
933 cond
= isl_set_preimage_multi_aff(cond
, ma
);
935 return enforce_subset(dom
, cond
);
938 /* Extract the for loop "tree" as a while loop within the context "pc_init".
939 * In particular, "pc_init" represents the context of the loop,
940 * whereas "pc" represents the context of the body of the loop and
941 * has already had its domain extended with an infinite loop
945 * The for loop has the form
947 * for (iv = init; cond; iv += inc)
958 * except that the skips resulting from any continue statements
959 * in body do not apply to the increment, but are replaced by the skips
960 * resulting from break statements.
962 * If the loop iterator is declared in the for loop, then it is killed before
963 * and after the loop.
965 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
966 __isl_keep pet_context
*init_pc
, __isl_take pet_context
*pc
,
967 struct pet_state
*state
)
971 pet_expr
*expr_iv
, *init
, *inc
;
972 struct pet_scop
*scop_init
, *scop
;
974 struct pet_array
*array
;
975 struct pet_scop
*scop_kill
;
977 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
978 pc
= pet_context_clear_value(pc
, iv
);
980 declared
= tree
->u
.l
.declared
;
982 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
983 type_size
= pet_expr_get_type_size(expr_iv
);
984 init
= pet_expr_copy(tree
->u
.l
.init
);
985 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
986 scop_init
= scop_from_expr(init
, NULL
, state
->n_stmt
++,
987 pet_tree_get_loc(tree
), init_pc
);
988 scop_init
= pet_scop_prefix(scop_init
, declared
);
990 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
991 type_size
= pet_expr_get_type_size(expr_iv
);
992 inc
= pet_expr_copy(tree
->u
.l
.inc
);
993 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
995 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
996 pet_tree_get_loc(tree
), tree
->u
.l
.body
, inc
,
997 pet_context_copy(pc
), state
);
999 scop
= pet_scop_prefix(scop
, declared
+ 1);
1000 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
1002 pet_context_free(pc
);
1007 array
= extract_array(tree
->u
.l
.iv
, init_pc
, state
);
1009 array
->declared
= 1;
1010 scop_kill
= kill(pet_tree_get_loc(tree
), array
, init_pc
, state
);
1011 scop_kill
= pet_scop_prefix(scop_kill
, 0);
1012 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
1013 scop_kill
= kill(pet_tree_get_loc(tree
), array
, init_pc
, state
);
1014 scop_kill
= pet_scop_add_array(scop_kill
, array
);
1015 scop_kill
= pet_scop_prefix(scop_kill
, 3);
1016 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
1021 /* Given an access expression "expr", is the variable accessed by
1022 * "expr" assigned anywhere inside "tree"?
1024 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
1029 id
= pet_expr_access_get_id(expr
);
1030 assigned
= pet_tree_writes(tree
, id
);
1036 /* Are all nested access parameters in "pa" allowed given "tree".
1037 * In particular, is none of them written by anywhere inside "tree".
1039 * If "tree" has any continue nodes in the current loop level,
1040 * then no nested access parameters are allowed.
1041 * In particular, if there is any nested access in a guard
1042 * for a piece of code containing a "continue", then we want to introduce
1043 * a separate statement for evaluating this guard so that we can express
1044 * that the result is false for all previous iterations.
1046 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
1047 __isl_keep pet_tree
*tree
)
1054 if (!pet_nested_any_in_pw_aff(pa
))
1057 if (pet_tree_has_continue(tree
))
1060 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1061 for (i
= 0; i
< nparam
; ++i
) {
1062 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1066 if (!pet_nested_in_id(id
)) {
1071 expr
= pet_nested_extract_expr(id
);
1072 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1073 !is_assigned(expr
, tree
);
1075 pet_expr_free(expr
);
1085 /* Construct a pet_scop for a for tree with static affine initialization
1086 * and constant increment within the context "pc".
1087 * The domain of "pc" has already been extended with an (at this point
1088 * unbounded) inner loop iterator corresponding to the current for loop.
1090 * The condition is allowed to contain nested accesses, provided
1091 * they are not being written to inside the body of the loop.
1092 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1093 * essentially treated as a while loop, with iteration domain
1094 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1096 * We extract a pet_scop for the body after intersecting the domain of "pc"
1098 * { [l,i] : i >= init and condition' }
1102 * { [l,i] : i <= init and condition' }
1104 * Where condition' is equal to condition if the latter is
1105 * a simple upper [lower] bound and a condition that is extended
1106 * to apply to all previous iterations otherwise.
1107 * Afterwards, the schedule of the pet_scop is extended with
1115 * If the condition is non-affine, then we drop the condition from the
1116 * iteration domain and instead create a separate statement
1117 * for evaluating the condition. The body is then filtered to depend
1118 * on the result of the condition evaluating to true on all iterations
1119 * up to the current iteration, while the evaluation the condition itself
1120 * is filtered to depend on the result of the condition evaluating to true
1121 * on all previous iterations.
1122 * The context of the scop representing the body is dropped
1123 * because we don't know how many times the body will be executed,
1126 * If the stride of the loop is not 1, then "i >= init" is replaced by
1128 * (exists a: i = init + stride * a and a >= 0)
1130 * If the loop iterator i is unsigned, then wrapping may occur.
1131 * We therefore use a virtual iterator instead that does not wrap.
1132 * However, the condition in the code applies
1133 * to the wrapped value, so we need to change condition(l,i)
1134 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1135 * to the original iterator by the wrapping of the virtual iterator.
1136 * Note that there may be no need to perform this final wrapping
1137 * if the loop condition (after wrapping) satisfies certain conditions.
1138 * However, the is_simple_bound condition is not enough since it doesn't
1139 * check if there even is an upper bound.
1141 * Wrapping on unsigned iterators can be avoided entirely if
1142 * loop condition is simple, the loop iterator is incremented
1143 * [decremented] by one and the last value before wrapping cannot
1144 * possibly satisfy the loop condition.
1146 * Valid outer iterators for a for loop are those for which the initial
1147 * value itself, the increment on each domain iteration and
1148 * the condition on both the initial value and
1149 * the result of incrementing the iterator for each iteration of the domain
1151 * If the loop condition is non-affine, then we only consider validity
1152 * of the initial value.
1154 * If the body contains any break, then we keep track of it in "skip"
1155 * (if the skip condition is affine) or it is handled in scop_add_break
1156 * (if the skip condition is not affine).
1157 * Note that the affine break condition needs to be considered with
1158 * respect to previous iterations in the virtual domain (if any).
1160 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1161 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1162 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1163 struct pet_state
*state
)
1167 isl_set
*cond
= NULL
;
1168 isl_set
*skip
= NULL
;
1169 isl_id
*id_test
= NULL
, *id_break_test
;
1170 struct pet_scop
*scop
, *scop_cond
= NULL
;
1177 int has_affine_break
;
1179 isl_map
*rev_wrap
= NULL
;
1180 isl_map
*init_val_map
;
1182 isl_set
*valid_init
;
1183 isl_set
*valid_cond
;
1184 isl_set
*valid_cond_init
;
1185 isl_set
*valid_cond_next
;
1187 pet_expr
*cond_expr
;
1188 pet_context
*pc_nested
;
1190 pos
= pet_context_dim(pc
) - 1;
1192 domain
= pet_context_get_domain(pc
);
1193 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1194 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
1195 pc_nested
= pet_context_copy(pc
);
1196 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1197 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1198 pet_context_free(pc_nested
);
1199 pet_expr_free(cond_expr
);
1201 valid_inc
= isl_pw_aff_domain(pa_inc
);
1203 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1205 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1206 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1208 pa
= isl_pw_aff_free(pa
);
1210 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1211 cond
= isl_pw_aff_non_zero_set(pa
);
1213 cond
= isl_set_universe(isl_set_get_space(domain
));
1215 valid_cond
= isl_set_coalesce(valid_cond
);
1216 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1217 is_virtual
= is_unsigned
&&
1218 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1220 init_val_map
= isl_map_from_pw_aff(isl_pw_aff_copy(init_val
));
1221 init_val_map
= isl_map_equate(init_val_map
, isl_dim_in
, pos
,
1223 valid_cond_init
= enforce_subset(isl_map_domain(init_val_map
),
1224 isl_set_copy(valid_cond
));
1225 if (is_one
&& !is_virtual
) {
1228 isl_pw_aff_free(init_val
);
1229 pa
= pet_expr_extract_comparison(
1230 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1231 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1232 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1233 valid_init
= isl_set_eliminate(valid_init
, isl_dim_set
,
1234 isl_set_dim(domain
, isl_dim_set
) - 1, 1);
1235 cond
= isl_pw_aff_non_zero_set(pa
);
1236 domain
= isl_set_intersect(domain
, cond
);
1240 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1241 strided
= strided_domain(init_val
, isl_val_copy(inc
));
1242 domain
= isl_set_intersect(domain
, strided
);
1246 isl_multi_aff
*wrap
;
1247 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1248 pc
= pet_context_preimage_domain(pc
, wrap
);
1249 rev_wrap
= isl_map_from_multi_aff(wrap
);
1250 rev_wrap
= isl_map_reverse(rev_wrap
);
1251 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1252 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1253 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1255 is_simple
= is_simple_bound(cond
, inc
);
1257 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1258 is_simple
= is_simple_bound(cond
, inc
);
1261 cond
= valid_for_each_iteration(cond
,
1262 isl_set_copy(domain
), isl_val_copy(inc
));
1263 cond
= isl_set_align_params(cond
, isl_set_get_space(domain
));
1264 domain
= isl_set_intersect(domain
, cond
);
1265 sched
= map_to_last(pc
);
1266 if (isl_val_is_neg(inc
))
1267 sched
= isl_aff_neg(sched
);
1269 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1271 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1273 pc
= pet_context_intersect_domain(pc
, isl_set_copy(domain
));
1275 if (is_non_affine
) {
1277 isl_multi_pw_aff
*test_index
;
1278 space
= isl_set_get_space(domain
);
1279 test_index
= pet_create_test_index(space
, state
->n_test
++);
1280 scop_cond
= scop_from_non_affine_condition(
1281 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1282 isl_multi_pw_aff_copy(test_index
),
1283 pet_tree_get_loc(tree
), pc
);
1284 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1286 scop_cond
= pet_scop_add_boolean_array(scop_cond
,
1287 isl_set_copy(domain
), test_index
,
1289 scop_cond
= pet_scop_prefix(scop_cond
, 0);
1290 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
1291 isl_aff_copy(sched
));
1294 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
1295 has_affine_break
= scop
&&
1296 pet_scop_has_affine_skip(scop
, pet_skip_later
);
1297 if (has_affine_break
)
1298 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1299 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
1301 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1302 if (is_non_affine
) {
1303 scop
= pet_scop_reset_context(scop
);
1304 scop
= pet_scop_prefix(scop
, 1);
1306 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
);
1307 scop
= pet_scop_resolve_nested(scop
);
1308 if (has_affine_break
) {
1309 domain
= apply_affine_break(domain
, skip
, isl_val_sgn(inc
),
1310 is_virtual
, rev_wrap
);
1311 scop
= pet_scop_intersect_domain_prefix(scop
,
1312 isl_set_copy(domain
));
1314 isl_map_free(rev_wrap
);
1316 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
1318 if (is_non_affine
) {
1319 scop
= scop_add_while(scop_cond
, scop
, id_test
, domain
,
1321 isl_set_free(valid_inc
);
1323 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_next
);
1324 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_init
);
1325 valid_inc
= isl_set_project_out(valid_inc
, isl_dim_set
, pos
, 1);
1326 scop
= pet_scop_restrict_context(scop
, valid_inc
);
1327 isl_set_free(domain
);
1332 valid_init
= isl_set_project_out(valid_init
, isl_dim_set
, pos
, 1);
1333 scop
= pet_scop_restrict_context(scop
, valid_init
);
1335 pet_context_free(pc
);
1339 /* Construct a pet_scop for a for statement within the context of "pc".
1341 * We update the context to reflect the writes to the loop variable and
1342 * the writes inside the body.
1344 * Then we check if the initialization of the for loop
1345 * is a static affine value and the increment is a constant.
1346 * If so, we construct the pet_scop using scop_from_affine_for.
1347 * Otherwise, we treat the for loop as a while loop
1348 * in scop_from_non_affine_for.
1350 * Note that the initialization and the increment are extracted
1351 * in a context where the current loop iterator has been added
1352 * to the context. If these turn out not be affine, then we
1353 * have reconstruct the body context without an assignment
1354 * to this loop iterator, as this variable will then not be
1355 * treated as a dimension of the iteration domain, but as any
1358 static struct pet_scop
*scop_from_for(__isl_keep pet_tree
*tree
,
1359 __isl_keep pet_context
*init_pc
, struct pet_state
*state
)
1363 isl_pw_aff
*pa_inc
, *init_val
;
1364 pet_context
*pc
, *pc_init_val
;
1369 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1370 pc
= pet_context_copy(init_pc
);
1371 pc
= pet_context_add_inner_iterator(pc
, iv
);
1372 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1374 pc_init_val
= pet_context_copy(pc
);
1375 pc_init_val
= pet_context_clear_value(pc_init_val
, isl_id_copy(iv
));
1376 init_val
= pet_expr_extract_affine(tree
->u
.l
.init
, pc_init_val
);
1377 pet_context_free(pc_init_val
);
1378 pa_inc
= pet_expr_extract_affine(tree
->u
.l
.inc
, pc
);
1379 inc
= pet_extract_cst(pa_inc
);
1380 if (!pa_inc
|| !init_val
|| !inc
)
1382 if (!isl_pw_aff_involves_nan(pa_inc
) &&
1383 !isl_pw_aff_involves_nan(init_val
) && !isl_val_is_nan(inc
))
1384 return scop_from_affine_for(tree
, init_val
, pa_inc
, inc
,
1387 isl_pw_aff_free(pa_inc
);
1388 isl_pw_aff_free(init_val
);
1390 pet_context_free(pc
);
1392 pc
= pet_context_copy(init_pc
);
1393 pc
= pet_context_add_infinite_loop(pc
);
1394 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1395 return scop_from_non_affine_for(tree
, init_pc
, pc
, state
);
1397 isl_pw_aff_free(pa_inc
);
1398 isl_pw_aff_free(init_val
);
1400 pet_context_free(pc
);
1404 /* Check whether "expr" is an affine constraint within the context "pc".
1406 static int is_affine_condition(__isl_keep pet_expr
*expr
,
1407 __isl_keep pet_context
*pc
)
1412 pa
= pet_expr_extract_affine_condition(expr
, pc
);
1415 is_affine
= !isl_pw_aff_involves_nan(pa
);
1416 isl_pw_aff_free(pa
);
1421 /* Check if the given if statement is a conditional assignement
1422 * with a non-affine condition.
1424 * In particular we check if "stmt" is of the form
1431 * where the condition is non-affine and a is some array or scalar access.
1433 static int is_conditional_assignment(__isl_keep pet_tree
*tree
,
1434 __isl_keep pet_context
*pc
)
1438 pet_expr
*expr1
, *expr2
;
1440 ctx
= pet_tree_get_ctx(tree
);
1441 if (!pet_options_get_detect_conditional_assignment(ctx
))
1443 if (tree
->type
!= pet_tree_if_else
)
1445 if (tree
->u
.i
.then_body
->type
!= pet_tree_expr
)
1447 if (tree
->u
.i
.else_body
->type
!= pet_tree_expr
)
1449 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1450 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1451 if (pet_expr_get_type(expr1
) != pet_expr_op
)
1453 if (pet_expr_get_type(expr2
) != pet_expr_op
)
1455 if (pet_expr_op_get_type(expr1
) != pet_op_assign
)
1457 if (pet_expr_op_get_type(expr2
) != pet_op_assign
)
1459 expr1
= pet_expr_get_arg(expr1
, 0);
1460 expr2
= pet_expr_get_arg(expr2
, 0);
1461 equal
= pet_expr_is_equal(expr1
, expr2
);
1462 pet_expr_free(expr1
);
1463 pet_expr_free(expr2
);
1464 if (equal
< 0 || !equal
)
1466 if (is_affine_condition(tree
->u
.i
.cond
, pc
))
1472 /* Given that "tree" is of the form
1479 * where a is some array or scalar access, construct a pet_scop
1480 * corresponding to this conditional assignment within the context "pc".
1482 * The constructed pet_scop then corresponds to the expression
1484 * a = condition ? f(...) : g(...)
1486 * All access relations in f(...) are intersected with condition
1487 * while all access relation in g(...) are intersected with the complement.
1489 static struct pet_scop
*scop_from_conditional_assignment(
1490 __isl_keep pet_tree
*tree
, __isl_take pet_context
*pc
,
1491 struct pet_state
*state
)
1495 isl_set
*cond
, *comp
;
1496 isl_multi_pw_aff
*index
;
1497 pet_expr
*expr1
, *expr2
;
1498 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
1499 pet_context
*pc_nested
;
1500 struct pet_scop
*scop
;
1502 pe_cond
= pet_expr_copy(tree
->u
.i
.cond
);
1503 pe_cond
= pet_context_evaluate_expr(pc
, pe_cond
);
1504 pc_nested
= pet_context_copy(pc
);
1505 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1506 pa
= pet_expr_extract_affine_condition(pe_cond
, pc_nested
);
1507 pet_context_free(pc_nested
);
1508 pet_expr_free(pe_cond
);
1509 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
1510 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
1511 index
= isl_multi_pw_aff_from_pw_aff(pa
);
1513 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1514 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1516 pe_cond
= pet_expr_from_index(index
);
1518 pe_then
= pet_expr_get_arg(expr1
, 1);
1519 pe_then
= pet_context_evaluate_expr(pc
, pe_then
);
1520 pe_then
= pet_expr_restrict(pe_then
, cond
);
1521 pe_else
= pet_expr_get_arg(expr2
, 1);
1522 pe_else
= pet_context_evaluate_expr(pc
, pe_else
);
1523 pe_else
= pet_expr_restrict(pe_else
, comp
);
1524 pe_write
= pet_expr_get_arg(expr1
, 0);
1525 pe_write
= pet_context_evaluate_expr(pc
, pe_write
);
1527 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
1528 type_size
= pet_expr_get_type_size(pe_write
);
1529 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, pe_write
, pe
);
1531 scop
= scop_from_evaluated_expr(pe
, NULL
, state
->n_stmt
++,
1532 pet_tree_get_loc(tree
), pc
);
1534 pet_context_free(pc
);
1539 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1541 * We create a separate statement that writes the result
1542 * of the non-affine condition to a virtual scalar.
1543 * A constraint requiring the value of this virtual scalar to be one
1544 * is added to the iteration domains of the then branch.
1545 * Similarly, a constraint requiring the value of this virtual scalar
1546 * to be zero is added to the iteration domains of the else branch, if any.
1547 * We adjust the schedules to ensure that the virtual scalar is written
1548 * before it is read.
1550 * If there are any breaks or continues in the then and/or else
1551 * branches, then we may have to compute a new skip condition.
1552 * This is handled using a pet_skip_info object.
1553 * On initialization, the object checks if skip conditions need
1554 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1555 * adds them in pet_skip_info_if_add.
1557 static struct pet_scop
*scop_from_non_affine_if(__isl_keep pet_tree
*tree
,
1558 __isl_take pet_context
*pc
, struct pet_state
*state
)
1563 isl_multi_pw_aff
*test_index
;
1564 struct pet_skip_info skip
;
1565 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1567 has_else
= tree
->type
== pet_tree_if_else
;
1569 space
= pet_context_get_space(pc
);
1570 test_index
= pet_create_test_index(space
, state
->n_test
++);
1571 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
1572 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
1573 pet_tree_get_loc(tree
), pc
);
1574 domain
= pet_context_get_domain(pc
);
1575 scop
= pet_scop_add_boolean_array(scop
, domain
,
1576 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1578 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1580 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1582 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1584 pet_skip_info_if_extract_index(&skip
, test_index
, pc
, state
);
1586 scop
= pet_scop_prefix(scop
, 0);
1587 scop_then
= pet_scop_prefix(scop_then
, 1);
1588 scop_then
= pet_scop_filter(scop_then
,
1589 isl_multi_pw_aff_copy(test_index
), 1);
1591 scop_else
= pet_scop_prefix(scop_else
, 1);
1592 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1593 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1595 isl_multi_pw_aff_free(test_index
);
1597 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1599 scop
= pet_skip_info_if_add(&skip
, scop
, 2);
1601 pet_context_free(pc
);
1605 /* Construct a pet_scop for an affine if statement within the context "pc".
1607 * The condition is added to the iteration domains of the then branch,
1608 * while the opposite of the condition in added to the iteration domains
1609 * of the else branch, if any.
1611 * If there are any breaks or continues in the then and/or else
1612 * branches, then we may have to compute a new skip condition.
1613 * This is handled using a pet_skip_info_if object.
1614 * On initialization, the object checks if skip conditions need
1615 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1616 * adds them in pet_skip_info_if_add.
1618 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1619 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
1620 struct pet_state
*state
)
1624 isl_set
*set
, *complement
;
1626 struct pet_skip_info skip
;
1627 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1628 pet_context
*pc_body
;
1630 ctx
= pet_tree_get_ctx(tree
);
1632 has_else
= tree
->type
== pet_tree_if_else
;
1634 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1635 set
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
1637 pc_body
= pet_context_copy(pc
);
1638 pc_body
= pet_context_intersect_domain(pc_body
, isl_set_copy(set
));
1639 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc_body
, state
);
1640 pet_context_free(pc_body
);
1642 pc_body
= pet_context_copy(pc
);
1643 complement
= isl_set_copy(valid
);
1644 complement
= isl_set_subtract(valid
, isl_set_copy(set
));
1645 pc_body
= pet_context_intersect_domain(pc_body
,
1646 isl_set_copy(complement
));
1647 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc_body
, state
);
1648 pet_context_free(pc_body
);
1651 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
1652 pet_skip_info_if_extract_cond(&skip
, cond
, pc
, state
);
1653 isl_pw_aff_free(cond
);
1655 scop
= pet_scop_restrict(scop_then
, set
);
1658 scop_else
= pet_scop_restrict(scop_else
, complement
);
1659 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
1661 scop
= pet_scop_resolve_nested(scop
);
1662 scop
= pet_scop_restrict_context(scop
, valid
);
1664 if (pet_skip_info_has_skip(&skip
))
1665 scop
= pet_scop_prefix(scop
, 0);
1666 scop
= pet_skip_info_if_add(&skip
, scop
, 1);
1668 pet_context_free(pc
);
1672 /* Construct a pet_scop for an if statement within the context "pc".
1674 * If the condition fits the pattern of a conditional assignment,
1675 * then it is handled by scop_from_conditional_assignment.
1677 * Otherwise, we check if the condition is affine.
1678 * If so, we construct the scop in scop_from_affine_if.
1679 * Otherwise, we construct the scop in scop_from_non_affine_if.
1681 * We allow the condition to be dynamic, i.e., to refer to
1682 * scalars or array elements that may be written to outside
1683 * of the given if statement. These nested accesses are then represented
1684 * as output dimensions in the wrapping iteration domain.
1685 * If it is also written _inside_ the then or else branch, then
1686 * we treat the condition as non-affine.
1687 * As explained in extract_non_affine_if, this will introduce
1688 * an extra statement.
1689 * For aesthetic reasons, we want this statement to have a statement
1690 * number that is lower than those of the then and else branches.
1691 * In order to evaluate if we will need such a statement, however, we
1692 * first construct scops for the then and else branches.
1693 * We therefore reserve a statement number if we might have to
1694 * introduce such an extra statement.
1696 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
1697 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1701 pet_expr
*cond_expr
;
1702 pet_context
*pc_nested
;
1707 has_else
= tree
->type
== pet_tree_if_else
;
1709 pc
= pet_context_copy(pc
);
1710 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
1712 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
1714 if (is_conditional_assignment(tree
, pc
))
1715 return scop_from_conditional_assignment(tree
, pc
, state
);
1717 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
1718 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
1719 pc_nested
= pet_context_copy(pc
);
1720 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1721 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1722 pet_context_free(pc_nested
);
1723 pet_expr_free(cond_expr
);
1726 pet_context_free(pc
);
1730 if (isl_pw_aff_involves_nan(cond
)) {
1731 isl_pw_aff_free(cond
);
1732 return scop_from_non_affine_if(tree
, pc
, state
);
1735 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
1736 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
1737 isl_pw_aff_free(cond
);
1738 return scop_from_non_affine_if(tree
, pc
, state
);
1741 return scop_from_affine_if(tree
, cond
, pc
, state
);
1744 /* Return a one-dimensional multi piecewise affine expression that is equal
1745 * to the constant 1 and is defined over the given domain.
1747 static __isl_give isl_multi_pw_aff
*one_mpa(__isl_take isl_space
*space
)
1749 isl_local_space
*ls
;
1752 ls
= isl_local_space_from_space(space
);
1753 aff
= isl_aff_zero_on_domain(ls
);
1754 aff
= isl_aff_set_constant_si(aff
, 1);
1756 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1759 /* Construct a pet_scop for a continue statement with the given domain space.
1761 * We simply create an empty scop with a universal pet_skip_now
1762 * skip condition. This skip condition will then be taken into
1763 * account by the enclosing loop construct, possibly after
1764 * being incorporated into outer skip conditions.
1766 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
,
1767 __isl_take isl_space
*space
)
1769 struct pet_scop
*scop
;
1771 scop
= pet_scop_empty(isl_space_copy(space
));
1773 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(space
));
1778 /* Construct a pet_scop for a break statement with the given domain space.
1780 * We simply create an empty scop with both a universal pet_skip_now
1781 * skip condition and a universal pet_skip_later skip condition.
1782 * These skip conditions will then be taken into
1783 * account by the enclosing loop construct, possibly after
1784 * being incorporated into outer skip conditions.
1786 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
,
1787 __isl_take isl_space
*space
)
1789 struct pet_scop
*scop
;
1790 isl_multi_pw_aff
*skip
;
1792 scop
= pet_scop_empty(isl_space_copy(space
));
1794 skip
= one_mpa(space
);
1795 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
1796 isl_multi_pw_aff_copy(skip
));
1797 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
1802 /* Extract a clone of the kill statement in "scop".
1803 * The domain of the clone is given by "domain".
1804 * "scop" is expected to have been created from a DeclStmt
1805 * and should have the kill as its first statement.
1807 static struct pet_scop
*extract_kill(__isl_keep isl_set
*domain
,
1808 struct pet_scop
*scop
, struct pet_state
*state
)
1811 struct pet_stmt
*stmt
;
1812 isl_multi_pw_aff
*index
;
1816 if (!domain
|| !scop
)
1818 if (scop
->n_stmt
< 1)
1819 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
1820 "expecting at least one statement", return NULL
);
1821 stmt
= scop
->stmts
[0];
1822 if (!pet_stmt_is_kill(stmt
))
1823 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
1824 "expecting kill statement", return NULL
);
1826 arg
= pet_expr_get_arg(stmt
->body
, 0);
1827 index
= pet_expr_access_get_index(arg
);
1828 access
= pet_expr_access_get_access(arg
);
1830 index
= isl_multi_pw_aff_reset_tuple_id(index
, isl_dim_in
);
1831 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
1832 kill
= pet_expr_kill_from_access_and_index(access
, index
);
1833 stmt
= pet_stmt_from_pet_expr(isl_set_copy(domain
),
1834 pet_loc_copy(stmt
->loc
), NULL
, state
->n_stmt
++, kill
);
1835 return pet_scop_from_pet_stmt(isl_set_get_space(domain
), stmt
);
1838 /* Does "tree" represent an assignment to a variable?
1840 * The assignment may be one of
1841 * - a declaration with initialization
1842 * - an expression with a top-level assignment operator
1844 static int is_assignment(__isl_keep pet_tree
*tree
)
1848 if (tree
->type
== pet_tree_decl_init
)
1850 return pet_tree_is_assign(tree
);
1853 /* Update "pc" by taking into account the assignment performed by "tree",
1854 * where "tree" satisfies is_assignment.
1856 * In particular, if the lhs of the assignment is a scalar variable and
1857 * if the rhs is an affine expression, then keep track of this value in "pc"
1858 * so that we can plug it in when we later come across the same variable.
1860 * Any previously assigned value to the variable has already been removed
1861 * by scop_handle_writes.
1863 static __isl_give pet_context
*handle_assignment(__isl_take pet_context
*pc
,
1864 __isl_keep pet_tree
*tree
)
1866 pet_expr
*var
, *val
;
1870 if (pet_tree_get_type(tree
) == pet_tree_decl_init
) {
1871 var
= pet_tree_decl_get_var(tree
);
1872 val
= pet_tree_decl_get_init(tree
);
1875 expr
= pet_tree_expr_get_expr(tree
);
1876 var
= pet_expr_get_arg(expr
, 0);
1877 val
= pet_expr_get_arg(expr
, 1);
1878 pet_expr_free(expr
);
1881 if (!pet_expr_is_scalar_access(var
)) {
1887 pa
= pet_expr_extract_affine(val
, pc
);
1889 pc
= pet_context_free(pc
);
1891 if (!isl_pw_aff_involves_nan(pa
)) {
1892 id
= pet_expr_access_get_id(var
);
1893 pc
= pet_context_set_value(pc
, id
, pa
);
1895 isl_pw_aff_free(pa
);
1903 /* Mark all arrays in "scop" as being exposed.
1905 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
1911 for (i
= 0; i
< scop
->n_array
; ++i
)
1912 scop
->arrays
[i
]->exposed
= 1;
1916 /* Given that "scop" has an affine skip condition of type pet_skip_now,
1917 * apply this skip condition to the domain of "pc".
1918 * That is, remove the elements satisfying the skip condition from
1919 * the domain of "pc".
1921 static __isl_give pet_context
*apply_affine_continue(__isl_take pet_context
*pc
,
1922 struct pet_scop
*scop
)
1924 isl_set
*domain
, *skip
;
1926 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_now
);
1927 domain
= pet_context_get_domain(pc
);
1928 domain
= isl_set_subtract(domain
, skip
);
1929 pc
= pet_context_intersect_domain(pc
, domain
);
1934 /* Try and construct a pet_scop corresponding to (part of)
1935 * a sequence of statements within the context "pc".
1937 * After extracting a statement, we update "pc"
1938 * based on the top-level assignments in the statement
1939 * so that we can exploit them in subsequent statements in the same block.
1941 * If there are any breaks or continues in the individual statements,
1942 * then we may have to compute a new skip condition.
1943 * This is handled using a pet_skip_info object.
1944 * On initialization, the object checks if skip conditions need
1945 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1946 * adds them in pet_skip_info_seq_add.
1948 * If "block" is set, then we need to insert kill statements at
1949 * the end of the block for any array that has been declared by
1950 * one of the statements in the sequence. Each of these declarations
1951 * results in the construction of a kill statement at the place
1952 * of the declaration, so we simply collect duplicates of
1953 * those kill statements and append these duplicates to the constructed scop.
1955 * If "block" is not set, then any array declared by one of the statements
1956 * in the sequence is marked as being exposed.
1958 * If autodetect is set, then we allow the extraction of only a subrange
1959 * of the sequence of statements. However, if there is at least one statement
1960 * for which we could not construct a scop and the final range contains
1961 * either no statements or at least one kill, then we discard the entire
1964 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
1965 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1971 struct pet_scop
*scop
, *kills
;
1973 ctx
= pet_tree_get_ctx(tree
);
1975 space
= pet_context_get_space(pc
);
1976 domain
= pet_context_get_domain(pc
);
1977 pc
= pet_context_copy(pc
);
1978 scop
= pet_scop_empty(isl_space_copy(space
));
1979 kills
= pet_scop_empty(space
);
1980 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
1981 struct pet_scop
*scop_i
;
1983 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
1984 pc
= apply_affine_continue(pc
, scop
);
1985 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
1986 pc
= scop_handle_writes(scop_i
, pc
);
1987 if (is_assignment(tree
->u
.b
.child
[i
]))
1988 pc
= handle_assignment(pc
, tree
->u
.b
.child
[i
]);
1989 struct pet_skip_info skip
;
1990 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
1991 pet_skip_info_seq_extract(&skip
, pc
, state
);
1992 if (pet_skip_info_has_skip(&skip
))
1993 scop_i
= pet_scop_prefix(scop_i
, 0);
1994 if (scop_i
&& pet_tree_is_decl(tree
->u
.b
.child
[i
])) {
1995 if (tree
->u
.b
.block
) {
1996 struct pet_scop
*kill
;
1997 kill
= extract_kill(domain
, scop_i
, state
);
1998 kills
= pet_scop_add_par(ctx
, kills
, kill
);
2000 scop_i
= mark_exposed(scop_i
);
2002 scop_i
= pet_scop_prefix(scop_i
, i
);
2003 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
2005 scop
= pet_skip_info_seq_add(&skip
, scop
, i
);
2010 isl_set_free(domain
);
2012 kills
= pet_scop_prefix(kills
, tree
->u
.b
.n
);
2013 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
2015 pet_context_free(pc
);
2020 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2021 * within the context "pc" by calling the appropriate function
2022 * based on the type of "tree".
2024 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
2025 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2030 switch (tree
->type
) {
2031 case pet_tree_error
:
2033 case pet_tree_block
:
2034 return scop_from_block(tree
, pc
, state
);
2035 case pet_tree_break
:
2036 return scop_from_break(tree
, pet_context_get_space(pc
));
2037 case pet_tree_continue
:
2038 return scop_from_continue(tree
, pet_context_get_space(pc
));
2040 case pet_tree_decl_init
:
2041 return scop_from_decl(tree
, pc
, state
);
2043 return scop_from_expr(pet_expr_copy(tree
->u
.e
.expr
),
2044 isl_id_copy(tree
->label
),
2046 pet_tree_get_loc(tree
), pc
);
2048 case pet_tree_if_else
:
2049 return scop_from_if(tree
, pc
, state
);
2051 return scop_from_for(tree
, pc
, state
);
2052 case pet_tree_while
:
2053 return scop_from_while(tree
, pc
, state
);
2054 case pet_tree_infinite_loop
:
2055 return scop_from_infinite_for(tree
, pc
, state
);
2058 isl_die(tree
->ctx
, isl_error_internal
, "unhandled type",
2062 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2063 * "int_size" is the number of bytes need to represent an integer.
2064 * "extract_array" is a callback that we can use to create a pet_array
2065 * that corresponds to the variable accessed by an expression.
2067 * Initialize the global state, construct a context and then
2068 * construct the pet_scop by recursively visiting the tree.
2070 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
2071 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
2072 __isl_keep pet_context
*pc
, void *user
), void *user
,
2073 __isl_keep pet_context
*pc
)
2075 struct pet_scop
*scop
;
2076 struct pet_state state
= { 0 };
2081 state
.ctx
= pet_tree_get_ctx(tree
);
2082 state
.int_size
= int_size
;
2083 state
.extract_array
= extract_array
;
2086 scop
= scop_from_tree(tree
, pc
, &state
);
2087 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
2089 pet_tree_free(tree
);
2092 scop
->context
= isl_set_params(scop
->context
);