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>
47 #include "tree2scop.h"
49 /* Update "pc" by taking into account the writes in "stmt".
50 * That is, clear any previously assigned values to variables
51 * that are written by "stmt".
53 static __isl_give pet_context
*handle_writes(struct pet_stmt
*stmt
,
54 __isl_take pet_context
*pc
)
56 return pet_context_clear_writes_in_tree(pc
, stmt
->body
);
59 /* Update "pc" based on the write accesses in "scop".
61 static __isl_give pet_context
*scop_handle_writes(struct pet_scop
*scop
,
62 __isl_take pet_context
*pc
)
67 return pet_context_free(pc
);
68 for (i
= 0; i
< scop
->n_stmt
; ++i
)
69 pc
= handle_writes(scop
->stmts
[i
], pc
);
74 /* Wrapper around pet_expr_resolve_assume
75 * for use as a callback to pet_tree_map_expr.
77 static __isl_give pet_expr
*resolve_assume(__isl_take pet_expr
*expr
,
80 pet_context
*pc
= user
;
82 return pet_expr_resolve_assume(expr
, pc
);
85 /* Check if any expression inside "tree" is an assume expression and
86 * if its single argument can be converted to an affine expression
87 * in the context of "pc".
88 * If so, replace the argument by the affine expression.
90 __isl_give pet_tree
*pet_tree_resolve_assume(__isl_take pet_tree
*tree
,
91 __isl_keep pet_context
*pc
)
93 return pet_tree_map_expr(tree
, &resolve_assume
, pc
);
96 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
97 * "tree" has already been evaluated in the context of "pc".
98 * This mainly involves resolving nested expression parameters
99 * and setting the name of the iteration space.
100 * The name is given by tree->label if it is non-NULL. Otherwise,
101 * it is of the form S_<stmt_nr>.
103 static struct pet_scop
*scop_from_evaluated_tree(__isl_take pet_tree
*tree
,
104 int stmt_nr
, __isl_keep pet_context
*pc
)
110 space
= pet_context_get_space(pc
);
112 tree
= pet_tree_resolve_nested(tree
, space
);
113 tree
= pet_tree_resolve_assume(tree
, pc
);
115 domain
= pet_context_get_domain(pc
);
116 ps
= pet_stmt_from_pet_tree(domain
, stmt_nr
, tree
);
117 return pet_scop_from_pet_stmt(space
, ps
);
120 /* Convert a top-level pet_expr to a pet_scop with one statement
121 * within the context "pc".
122 * "expr" has already been evaluated in the context of "pc".
123 * We construct a pet_tree from "expr" and continue with
124 * scop_from_evaluated_tree.
125 * The name is of the form S_<stmt_nr>.
126 * The location of the statement is set to "loc".
128 static struct pet_scop
*scop_from_evaluated_expr(__isl_take pet_expr
*expr
,
129 int stmt_nr
, __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
133 tree
= pet_tree_new_expr(expr
);
134 tree
= pet_tree_set_loc(tree
, loc
);
135 return scop_from_evaluated_tree(tree
, stmt_nr
, pc
);
138 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
139 * "tree" has not yet been evaluated in the context of "pc".
140 * We evaluate "tree" in the context of "pc" and continue with
141 * scop_from_evaluated_tree.
142 * The statement name is given by tree->label if it is non-NULL. Otherwise,
143 * it is of the form S_<stmt_nr>.
145 static struct pet_scop
*scop_from_unevaluated_tree(__isl_take pet_tree
*tree
,
146 int stmt_nr
, __isl_keep pet_context
*pc
)
148 tree
= pet_context_evaluate_tree(pc
, tree
);
149 return scop_from_evaluated_tree(tree
, stmt_nr
, pc
);
152 /* Convert a top-level pet_expr to a pet_scop with one statement
153 * within the context "pc", where "expr" has not yet been evaluated
154 * in the context of "pc".
155 * We construct a pet_tree from "expr" and continue with
156 * scop_from_unevaluated_tree.
157 * The statement name is of the form S_<stmt_nr>.
158 * The location of the statement is set to "loc".
160 static struct pet_scop
*scop_from_expr(__isl_take pet_expr
*expr
,
161 int stmt_nr
, __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
165 tree
= pet_tree_new_expr(expr
);
166 tree
= pet_tree_set_loc(tree
, loc
);
167 return scop_from_unevaluated_tree(tree
, stmt_nr
, pc
);
170 /* Construct a pet_scop with a single statement killing the entire
172 * The location of the statement is set to "loc".
174 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
175 __isl_keep pet_context
*pc
, struct pet_state
*state
)
180 isl_multi_pw_aff
*index
;
183 struct pet_scop
*scop
;
187 ctx
= isl_set_get_ctx(array
->extent
);
188 access
= isl_map_from_range(isl_set_copy(array
->extent
));
189 id
= isl_set_get_tuple_id(array
->extent
);
190 space
= isl_space_alloc(ctx
, 0, 0, 0);
191 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
192 index
= isl_multi_pw_aff_zero(space
);
193 expr
= pet_expr_kill_from_access_and_index(access
, index
);
194 return scop_from_expr(expr
, state
->n_stmt
++, loc
, pc
);
200 /* Construct and return a pet_array corresponding to the variable
201 * accessed by "access" by calling the extract_array callback.
203 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
204 __isl_keep pet_context
*pc
, struct pet_state
*state
)
206 return state
->extract_array(access
, pc
, state
->user
);
209 /* Construct a pet_scop for a (single) variable declaration
210 * within the context "pc".
212 * The scop contains the variable being declared (as an array)
213 * and a statement killing the array.
215 * If the declaration comes with an initialization, then the scop
216 * also contains an assignment to the variable.
218 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
219 __isl_keep pet_context
*pc
, struct pet_state
*state
)
223 struct pet_array
*array
;
224 struct pet_scop
*scop_decl
, *scop
;
225 pet_expr
*lhs
, *rhs
, *pe
;
227 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
230 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
231 scop_decl
= pet_scop_add_array(scop_decl
, array
);
233 if (tree
->type
!= pet_tree_decl_init
)
236 lhs
= pet_expr_copy(tree
->u
.d
.var
);
237 rhs
= pet_expr_copy(tree
->u
.d
.init
);
238 type_size
= pet_expr_get_type_size(lhs
);
239 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
240 scop
= scop_from_expr(pe
, state
->n_stmt
++, pet_tree_get_loc(tree
), pc
);
242 scop_decl
= pet_scop_prefix(scop_decl
, 0);
243 scop
= pet_scop_prefix(scop
, 1);
245 ctx
= pet_tree_get_ctx(tree
);
246 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
251 /* Does "tree" represent a kill statement?
252 * That is, is it an expression statement that "calls" __pencil_kill?
254 static int is_pencil_kill(__isl_keep pet_tree
*tree
)
261 if (tree
->type
!= pet_tree_expr
)
263 expr
= tree
->u
.e
.expr
;
264 if (pet_expr_get_type(expr
) != pet_expr_call
)
266 name
= pet_expr_call_get_name(expr
);
269 return !strcmp(name
, "__pencil_kill");
272 /* Add a kill to "scop" that kills what is accessed by
273 * the access expression "expr".
275 * If the access expression has any arguments (after evaluation
276 * in the context of "pc"), then we ignore it, since we cannot
277 * tell which elements are definitely killed.
279 * Otherwise, we extend the index expression to the dimension
280 * of the accessed array and intersect with the extent of the array and
281 * add a kill expression that kills these array elements is added to "scop".
283 static struct pet_scop
*scop_add_kill(struct pet_scop
*scop
,
284 __isl_take pet_expr
*expr
, __isl_take pet_loc
*loc
,
285 __isl_keep pet_context
*pc
, struct pet_state
*state
)
289 isl_multi_pw_aff
*index
;
292 struct pet_array
*array
;
293 struct pet_scop
*scop_i
;
295 expr
= pet_context_evaluate_expr(pc
, expr
);
298 if (expr
->n_arg
!= 0) {
302 array
= extract_array(expr
, pc
, state
);
305 index
= pet_expr_access_get_index(expr
);
307 map
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
308 id
= isl_map_get_tuple_id(map
, isl_dim_out
);
309 dim1
= isl_set_dim(array
->extent
, isl_dim_set
);
310 dim2
= isl_map_dim(map
, isl_dim_out
);
311 map
= isl_map_add_dims(map
, isl_dim_out
, dim1
- dim2
);
312 map
= isl_map_set_tuple_id(map
, isl_dim_out
, id
);
313 map
= isl_map_intersect_range(map
, isl_set_copy(array
->extent
));
314 pet_array_free(array
);
315 kill
= pet_expr_kill_from_access_and_index(map
, index
);
316 scop_i
= scop_from_evaluated_expr(kill
, state
->n_stmt
++, loc
, pc
);
317 scop
= pet_scop_add_par(state
->ctx
, scop
, scop_i
);
322 return pet_scop_free(scop
);
325 /* For each argument of the __pencil_kill call in "tree" that
326 * represents an access, add a kill statement to "scop" killing the accessed
329 static struct pet_scop
*scop_from_pencil_kill(__isl_keep pet_tree
*tree
,
330 __isl_keep pet_context
*pc
, struct pet_state
*state
)
333 struct pet_scop
*scop
;
336 call
= tree
->u
.e
.expr
;
338 scop
= pet_scop_empty(pet_context_get_space(pc
));
340 n
= pet_expr_get_n_arg(call
);
341 for (i
= 0; i
< n
; ++i
) {
345 arg
= pet_expr_get_arg(call
, i
);
347 return pet_scop_free(scop
);
348 if (pet_expr_get_type(arg
) != pet_expr_access
) {
352 loc
= pet_tree_get_loc(tree
);
353 scop
= scop_add_kill(scop
, arg
, loc
, pc
, state
);
359 /* Construct a pet_scop for an expression statement within the context "pc".
361 * If the expression calls __pencil_kill, then it needs to be converted
362 * into zero or more kill statements.
363 * Otherwise, a scop is extracted directly from the tree.
365 static struct pet_scop
*scop_from_tree_expr(__isl_keep pet_tree
*tree
,
366 __isl_keep pet_context
*pc
, struct pet_state
*state
)
370 is_kill
= is_pencil_kill(tree
);
374 return scop_from_pencil_kill(tree
, pc
, state
);
375 return scop_from_unevaluated_tree(pet_tree_copy(tree
),
376 state
->n_stmt
++, pc
);
379 /* Return those elements in the space of "cond" that come after
380 * (based on "sign") an element in "cond" in the final dimension.
382 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
385 isl_map
*previous_to_this
;
388 dim
= isl_set_dim(cond
, isl_dim_set
);
389 space
= isl_space_map_from_set(isl_set_get_space(cond
));
390 previous_to_this
= isl_map_universe(space
);
391 for (i
= 0; i
+ 1 < dim
; ++i
)
392 previous_to_this
= isl_map_equate(previous_to_this
,
393 isl_dim_in
, i
, isl_dim_out
, i
);
395 previous_to_this
= isl_map_order_lt(previous_to_this
,
396 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
398 previous_to_this
= isl_map_order_gt(previous_to_this
,
399 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
401 cond
= isl_set_apply(cond
, previous_to_this
);
406 /* Remove those iterations of "domain" that have an earlier iteration
407 * (based on "sign") in the final dimension where "skip" is satisfied.
408 * If "apply_skip_map" is set, then "skip_map" is first applied
409 * to the embedded skip condition before removing it from the domain.
411 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
412 __isl_take isl_set
*skip
, int sign
,
413 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
416 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
417 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
418 return isl_set_subtract(domain
, after(skip
, sign
));
421 /* Create an affine expression on the domain space of "pc" that
422 * is equal to the final dimension of this domain.
424 static __isl_give isl_aff
*map_to_last(__isl_keep pet_context
*pc
)
430 space
= pet_context_get_space(pc
);
431 pos
= isl_space_dim(space
, isl_dim_set
) - 1;
432 ls
= isl_local_space_from_space(space
);
433 return isl_aff_var_on_domain(ls
, isl_dim_set
, pos
);
436 /* Create an affine expression that maps elements
437 * of an array "id_test" to the previous element in the final dimension
438 * (according to "inc"), provided this element belongs to "domain".
439 * That is, create the affine expression
441 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
443 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
444 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
451 isl_multi_pw_aff
*prev
;
453 pos
= isl_set_dim(domain
, isl_dim_set
) - 1;
454 space
= isl_set_get_space(domain
);
455 space
= isl_space_map_from_set(space
);
456 ma
= isl_multi_aff_identity(space
);
457 aff
= isl_multi_aff_get_aff(ma
, pos
);
458 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
459 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
460 domain
= isl_set_preimage_multi_aff(domain
, isl_multi_aff_copy(ma
));
461 prev
= isl_multi_pw_aff_from_multi_aff(ma
);
462 pa
= isl_multi_pw_aff_get_pw_aff(prev
, pos
);
463 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
464 prev
= isl_multi_pw_aff_set_pw_aff(prev
, pos
, pa
);
465 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
470 /* Add an implication to "scop" expressing that if an element of
471 * virtual array "id_test" has value "satisfied" then all previous elements
472 * of this array (in the final dimension) also have that value.
473 * The set of previous elements is bounded by "domain".
474 * If "sign" is negative then the iterator
475 * is decreasing and we express that all subsequent array elements
476 * (but still defined previously) have the same value.
478 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
479 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
486 dim
= isl_set_dim(domain
, isl_dim_set
);
487 domain
= isl_set_set_tuple_id(domain
, id_test
);
488 space
= isl_space_map_from_set(isl_set_get_space(domain
));
489 map
= isl_map_universe(space
);
490 for (i
= 0; i
+ 1 < dim
; ++i
)
491 map
= isl_map_equate(map
, isl_dim_in
, i
, isl_dim_out
, i
);
493 map
= isl_map_order_ge(map
,
494 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
496 map
= isl_map_order_le(map
,
497 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
498 map
= isl_map_intersect_range(map
, domain
);
499 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
504 /* Add a filter to "scop" that imposes that it is only executed
505 * when the variable identified by "id_test" has a zero value
506 * for all previous iterations of "domain".
508 * In particular, add a filter that imposes that the array
509 * has a zero value at the previous iteration of domain and
510 * add an implication that implies that it then has that
511 * value for all previous iterations.
513 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
514 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
515 __isl_take isl_val
*inc
)
517 isl_multi_pw_aff
*prev
;
518 int sign
= isl_val_sgn(inc
);
520 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
521 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
522 scop
= pet_scop_filter(scop
, prev
, 0);
527 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
528 __isl_keep pet_context
*pc
, struct pet_state
*state
);
530 /* Construct a pet_scop for an infinite loop around the given body
531 * within the context "pc".
533 * The domain of "pc" has already been extended with an infinite loop
537 * We extract a pet_scop for the body and then embed it in a loop with
540 * { [outer,t] -> [t] }
542 * If the body contains any break, then it is taken into
543 * account in apply_affine_break (if the skip condition is affine)
544 * or in scop_add_break (if the skip condition is not affine).
546 * Note that in case of an affine skip condition,
547 * since we are dealing with a loop without loop iterator,
548 * the skip condition cannot refer to the current loop iterator and
549 * so effectively, the effect on the iteration domain is of the form
551 * { [outer,0]; [outer,t] : t >= 1 and not skip }
553 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
554 __isl_keep pet_context
*pc
, struct pet_state
*state
)
561 struct pet_scop
*scop
;
562 int has_affine_break
;
565 ctx
= pet_tree_get_ctx(body
);
566 domain
= pet_context_get_domain(pc
);
567 sched
= map_to_last(pc
);
569 scop
= scop_from_tree(body
, pc
, state
);
571 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
572 if (has_affine_break
)
573 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
574 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
576 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
578 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
);
579 if (has_affine_break
) {
580 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
581 scop
= pet_scop_intersect_domain_prefix(scop
,
582 isl_set_copy(domain
));
585 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
587 isl_set_free(domain
);
592 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
597 * within the context "pc".
599 * Extend the domain of "pc" with an extra inner loop
603 * and construct the scop in scop_from_infinite_loop.
605 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
606 __isl_keep pet_context
*pc
, struct pet_state
*state
)
608 struct pet_scop
*scop
;
610 pc
= pet_context_copy(pc
);
611 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
613 pc
= pet_context_add_infinite_loop(pc
);
615 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
617 pet_context_free(pc
);
622 /* Construct a pet_scop for a while loop of the form
627 * within the context "pc".
629 * The domain of "pc" has already been extended with an infinite loop
633 * Here, we add the constraints on the outer loop iterators
634 * implied by "pa" and construct the scop in scop_from_infinite_loop.
635 * Note that the intersection with these constraints
636 * may result in an empty loop.
638 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
639 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
640 struct pet_state
*state
)
642 struct pet_scop
*scop
;
643 isl_set
*dom
, *local
;
646 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
647 dom
= isl_pw_aff_non_zero_set(pa
);
648 local
= isl_set_add_dims(isl_set_copy(dom
), isl_dim_set
, 1);
649 pc
= pet_context_intersect_domain(pc
, local
);
650 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
651 scop
= pet_scop_restrict(scop
, dom
);
652 scop
= pet_scop_restrict_context(scop
, valid
);
654 pet_context_free(pc
);
658 /* Construct a scop for a while, given the scops for the condition
659 * and the body, the filter identifier and the iteration domain of
662 * In particular, the scop for the condition is filtered to depend
663 * on "id_test" evaluating to true for all previous iterations
664 * of the loop, while the scop for the body is filtered to depend
665 * on "id_test" evaluating to true for all iterations up to the
667 * The actual filter only imposes that this virtual array has
668 * value one on the previous or the current iteration.
669 * The fact that this condition also applies to the previous
670 * iterations is enforced by an implication.
672 * These filtered scops are then combined into a single scop.
674 * "sign" is positive if the iterator increases and negative
677 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
678 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
679 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
681 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
683 isl_multi_pw_aff
*test_index
;
684 isl_multi_pw_aff
*prev
;
685 int sign
= isl_val_sgn(inc
);
686 struct pet_scop
*scop
;
688 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
689 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
691 space
= isl_space_map_from_set(isl_set_get_space(domain
));
692 test_index
= isl_multi_pw_aff_identity(space
);
693 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
694 isl_id_copy(id_test
));
695 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
697 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
698 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
703 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
704 * evaluating "cond" and writing the result to a virtual scalar,
705 * as expressed by "index".
706 * The expression "cond" has not yet been evaluated in the context of "pc".
707 * Do so within the context "pc".
708 * The location of the statement is set to "loc".
710 static struct pet_scop
*scop_from_non_affine_condition(
711 __isl_take pet_expr
*cond
, int stmt_nr
,
712 __isl_take isl_multi_pw_aff
*index
,
713 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
715 pet_expr
*expr
, *write
;
717 cond
= pet_context_evaluate_expr(pc
, cond
);
719 write
= pet_expr_from_index(index
);
720 write
= pet_expr_access_set_write(write
, 1);
721 write
= pet_expr_access_set_read(write
, 0);
722 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
724 return scop_from_evaluated_expr(expr
, stmt_nr
, loc
, pc
);
727 /* Given that "scop" has an affine skip condition of type pet_skip_now,
728 * apply this skip condition to the domain of "pc".
729 * That is, remove the elements satisfying the skip condition from
730 * the domain of "pc".
732 static __isl_give pet_context
*apply_affine_continue(__isl_take pet_context
*pc
,
733 struct pet_scop
*scop
)
735 isl_set
*domain
, *skip
;
737 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_now
);
738 domain
= pet_context_get_domain(pc
);
739 domain
= isl_set_subtract(domain
, skip
);
740 pc
= pet_context_intersect_domain(pc
, domain
);
745 /* Add a scop for evaluating the loop increment "inc" add the end
746 * of a loop body "scop" within the context "pc".
748 * The skip conditions resulting from continue statements inside
749 * the body do not apply to "inc", but those resulting from break
750 * statements do need to get applied.
752 static struct pet_scop
*scop_add_inc(struct pet_scop
*scop
,
753 __isl_take pet_expr
*inc
, __isl_take pet_loc
*loc
,
754 __isl_keep pet_context
*pc
, struct pet_state
*state
)
756 struct pet_scop
*scop_inc
;
758 pc
= pet_context_copy(pc
);
760 if (pet_scop_has_skip(scop
, pet_skip_later
)) {
761 isl_multi_pw_aff
*skip
;
762 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
763 scop
= pet_scop_set_skip(scop
, pet_skip_now
, skip
);
764 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
765 pc
= apply_affine_continue(pc
, scop
);
767 pet_scop_reset_skip(scop
, pet_skip_now
);
768 scop_inc
= scop_from_expr(inc
, state
->n_stmt
++, loc
, pc
);
769 scop_inc
= pet_scop_prefix(scop_inc
, 2);
770 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_inc
);
772 pet_context_free(pc
);
777 /* Construct a generic while scop, with iteration domain
778 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
779 * The domain of "pc" has already been extended with this infinite loop
783 * The scop consists of two parts,
784 * one for evaluating the condition "cond" and one for the body.
785 * If "expr_inc" is not NULL, then a scop for evaluating this expression
786 * is added at the end of the body,
787 * after replacing any skip conditions resulting from continue statements
788 * by the skip conditions resulting from break statements (if any).
790 * The schedule is adjusted to reflect that the condition is evaluated
791 * before the body is executed and the body is filtered to depend
792 * on the result of the condition evaluating to true on all iterations
793 * up to the current iteration, while the evaluation of the condition itself
794 * is filtered to depend on the result of the condition evaluating to true
795 * on all previous iterations.
796 * The context of the scop representing the body is dropped
797 * because we don't know how many times the body will be executed,
800 * If the body contains any break, then it is taken into
801 * account in apply_affine_break (if the skip condition is affine)
802 * or in scop_add_break (if the skip condition is not affine).
804 * Note that in case of an affine skip condition,
805 * since we are dealing with a loop without loop iterator,
806 * the skip condition cannot refer to the current loop iterator and
807 * so effectively, the effect on the iteration domain is of the form
809 * { [outer,0]; [outer,t] : t >= 1 and not skip }
811 static struct pet_scop
*scop_from_non_affine_while(__isl_take pet_expr
*cond
,
812 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
813 __isl_take pet_expr
*expr_inc
, __isl_take pet_context
*pc
,
814 struct pet_state
*state
)
817 isl_id
*id_test
, *id_break_test
;
819 isl_multi_pw_aff
*test_index
;
823 struct pet_scop
*scop
, *scop_body
;
824 int has_affine_break
;
828 space
= pet_context_get_space(pc
);
829 test_index
= pet_create_test_index(space
, state
->n_test
++);
830 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
831 isl_multi_pw_aff_copy(test_index
),
832 pet_loc_copy(loc
), pc
);
833 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
834 domain
= pet_context_get_domain(pc
);
835 scop
= pet_scop_add_boolean_array(scop
, isl_set_copy(domain
),
836 test_index
, state
->int_size
);
838 sched
= map_to_last(pc
);
840 scop_body
= scop_from_tree(tree_body
, pc
, state
);
842 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
843 if (has_affine_break
)
844 skip
= pet_scop_get_affine_skip_domain(scop_body
,
846 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
848 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
850 scop
= pet_scop_prefix(scop
, 0);
851 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_aff_copy(sched
));
852 scop_body
= pet_scop_reset_context(scop_body
);
853 scop_body
= pet_scop_prefix(scop_body
, 1);
855 scop_body
= scop_add_inc(scop_body
, expr_inc
, loc
, pc
, state
);
858 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
), sched
);
860 if (has_affine_break
) {
861 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
862 scop
= pet_scop_intersect_domain_prefix(scop
,
863 isl_set_copy(domain
));
864 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
865 isl_set_copy(domain
));
868 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
869 isl_set_copy(domain
), isl_val_one(ctx
));
870 scop_body
= scop_add_break(scop_body
, id_break_test
,
871 isl_set_copy(domain
), isl_val_one(ctx
));
873 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
876 pet_context_free(pc
);
880 /* Check if the while loop is of the form
882 * while (affine expression)
885 * If so, call scop_from_affine_while to construct a scop.
887 * Otherwise, pass control to scop_from_non_affine_while.
889 * "pc" is the context in which the affine expressions in the scop are created.
890 * The domain of "pc" is extended with an infinite loop
894 * before passing control to scop_from_affine_while or
895 * scop_from_non_affine_while.
897 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
898 __isl_keep pet_context
*pc
, struct pet_state
*state
)
906 pc
= pet_context_copy(pc
);
907 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
909 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
910 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
911 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
912 pet_expr_free(cond_expr
);
914 pc
= pet_context_add_infinite_loop(pc
);
919 if (!isl_pw_aff_involves_nan(pa
))
920 return scop_from_affine_while(tree
, pa
, pc
, state
);
922 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
923 pet_tree_get_loc(tree
), tree
->u
.l
.body
, NULL
,
926 pet_context_free(pc
);
930 /* Check whether "cond" expresses a simple loop bound
931 * on the final set dimension.
932 * In particular, if "up" is set then "cond" should contain only
933 * upper bounds on the final set dimension.
934 * Otherwise, it should contain only lower bounds.
936 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
940 pos
= isl_set_dim(cond
, isl_dim_set
) - 1;
941 if (isl_val_is_pos(inc
))
942 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, pos
);
944 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, pos
);
947 /* Extend a condition on a given iteration of a loop to one that
948 * imposes the same condition on all previous iterations.
949 * "domain" expresses the lower [upper] bound on the iterations
950 * when inc is positive [negative] in its final dimension.
952 * In particular, we construct the condition (when inc is positive)
954 * forall i' : (domain(i') and i' <= i) => cond(i')
956 * (where "<=" applies to the final dimension)
957 * which is equivalent to
959 * not exists i' : domain(i') and i' <= i and not cond(i')
961 * We construct this set by subtracting the satisfying cond from domain,
964 * { [i'] -> [i] : i' <= i }
966 * and then subtracting the result from domain again.
968 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
969 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
972 isl_map
*previous_to_this
;
975 dim
= isl_set_dim(cond
, isl_dim_set
);
976 space
= isl_space_map_from_set(isl_set_get_space(cond
));
977 previous_to_this
= isl_map_universe(space
);
978 for (i
= 0; i
+ 1 < dim
; ++i
)
979 previous_to_this
= isl_map_equate(previous_to_this
,
980 isl_dim_in
, i
, isl_dim_out
, i
);
981 if (isl_val_is_pos(inc
))
982 previous_to_this
= isl_map_order_le(previous_to_this
,
983 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
985 previous_to_this
= isl_map_order_ge(previous_to_this
,
986 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
988 cond
= isl_set_subtract(isl_set_copy(domain
), cond
);
989 cond
= isl_set_apply(cond
, previous_to_this
);
990 cond
= isl_set_subtract(domain
, cond
);
997 /* Given an initial value of the form
999 * { [outer,i] -> init(outer) }
1001 * construct a domain of the form
1003 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1005 static __isl_give isl_set
*strided_domain(__isl_take isl_pw_aff
*init
,
1006 __isl_take isl_val
*inc
)
1011 isl_local_space
*ls
;
1014 dim
= isl_pw_aff_dim(init
, isl_dim_in
);
1016 init
= isl_pw_aff_add_dims(init
, isl_dim_in
, 1);
1017 space
= isl_pw_aff_get_domain_space(init
);
1018 ls
= isl_local_space_from_space(space
);
1019 aff
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
1020 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, dim
, inc
);
1021 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
1023 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, dim
- 1);
1024 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
1026 set
= isl_set_lower_bound_si(set
, isl_dim_set
, dim
, 0);
1027 set
= isl_set_project_out(set
, isl_dim_set
, dim
, 1);
1032 /* Assuming "cond" represents a bound on a loop where the loop
1033 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1036 * Under the given assumptions, wrapping is only possible if "cond" allows
1037 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1038 * increasing iterator and 0 in case of a decreasing iterator.
1040 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
1041 __isl_keep isl_val
*inc
)
1048 test
= isl_set_copy(cond
);
1050 ctx
= isl_set_get_ctx(test
);
1051 if (isl_val_is_neg(inc
))
1052 limit
= isl_val_zero(ctx
);
1054 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
1055 limit
= isl_val_2exp(limit
);
1056 limit
= isl_val_sub_ui(limit
, 1);
1059 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
1060 cw
= !isl_set_is_empty(test
);
1070 * construct the following affine expression on this space
1072 * { [outer, v] -> [outer, v mod 2^width] }
1074 * where width is the number of bits used to represent the values
1075 * of the unsigned variable "iv".
1077 static __isl_give isl_multi_aff
*compute_wrapping(__isl_take isl_space
*space
,
1078 __isl_keep pet_expr
*iv
)
1086 dim
= isl_space_dim(space
, isl_dim_set
);
1088 ctx
= isl_space_get_ctx(space
);
1089 mod
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
1090 mod
= isl_val_2exp(mod
);
1092 space
= isl_space_map_from_set(space
);
1093 ma
= isl_multi_aff_identity(space
);
1095 aff
= isl_multi_aff_get_aff(ma
, dim
- 1);
1096 aff
= isl_aff_mod_val(aff
, mod
);
1097 ma
= isl_multi_aff_set_aff(ma
, dim
- 1, aff
);
1102 /* Given two sets in the space
1106 * where l represents the outer loop iterators, compute the set
1107 * of values of l that ensure that "set1" is a subset of "set2".
1109 * set1 is a subset of set2 if
1111 * forall i: set1(l,i) => set2(l,i)
1115 * not exists i: set1(l,i) and not set2(l,i)
1119 * not exists i: (set1 \ set2)(l,i)
1121 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
1122 __isl_take isl_set
*set2
)
1126 pos
= isl_set_dim(set1
, isl_dim_set
) - 1;
1127 set1
= isl_set_subtract(set1
, set2
);
1128 set1
= isl_set_eliminate(set1
, isl_dim_set
, pos
, 1);
1129 return isl_set_complement(set1
);
1132 /* Compute the set of outer iterator values for which "cond" holds
1133 * on the next iteration of the inner loop for each element of "dom".
1135 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1136 * to the outer loop iterators), plug that into "cond"
1137 * and then compute the set of outer iterators for which "dom" is a subset
1140 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
1141 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
1148 pos
= isl_set_dim(dom
, isl_dim_set
) - 1;
1149 space
= isl_set_get_space(dom
);
1150 space
= isl_space_map_from_set(space
);
1151 ma
= isl_multi_aff_identity(space
);
1152 aff
= isl_multi_aff_get_aff(ma
, pos
);
1153 aff
= isl_aff_add_constant_val(aff
, inc
);
1154 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
1155 cond
= isl_set_preimage_multi_aff(cond
, ma
);
1157 return enforce_subset(dom
, cond
);
1160 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1161 * In particular, "pc_init" represents the context of the loop,
1162 * whereas "pc" represents the context of the body of the loop and
1163 * has already had its domain extended with an infinite loop
1167 * The for loop has the form
1169 * for (iv = init; cond; iv += inc)
1180 * except that the skips resulting from any continue statements
1181 * in body do not apply to the increment, but are replaced by the skips
1182 * resulting from break statements.
1184 * If the loop iterator is declared in the for loop, then it is killed before
1185 * and after the loop.
1187 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
1188 __isl_keep pet_context
*init_pc
, __isl_take pet_context
*pc
,
1189 struct pet_state
*state
)
1193 pet_expr
*expr_iv
, *init
, *inc
;
1194 struct pet_scop
*scop_init
, *scop
;
1196 struct pet_array
*array
;
1197 struct pet_scop
*scop_kill
;
1199 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1200 pc
= pet_context_clear_value(pc
, iv
);
1202 declared
= tree
->u
.l
.declared
;
1204 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
1205 type_size
= pet_expr_get_type_size(expr_iv
);
1206 init
= pet_expr_copy(tree
->u
.l
.init
);
1207 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
1208 scop_init
= scop_from_expr(init
, state
->n_stmt
++,
1209 pet_tree_get_loc(tree
), init_pc
);
1210 scop_init
= pet_scop_prefix(scop_init
, declared
);
1212 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
1213 type_size
= pet_expr_get_type_size(expr_iv
);
1214 inc
= pet_expr_copy(tree
->u
.l
.inc
);
1215 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
1217 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
1218 pet_tree_get_loc(tree
), tree
->u
.l
.body
, inc
,
1219 pet_context_copy(pc
), state
);
1221 scop
= pet_scop_prefix(scop
, declared
+ 1);
1222 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
1224 pet_context_free(pc
);
1229 array
= extract_array(tree
->u
.l
.iv
, init_pc
, state
);
1231 array
->declared
= 1;
1232 scop_kill
= kill(pet_tree_get_loc(tree
), array
, init_pc
, state
);
1233 scop_kill
= pet_scop_prefix(scop_kill
, 0);
1234 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
1235 scop_kill
= kill(pet_tree_get_loc(tree
), array
, init_pc
, state
);
1236 scop_kill
= pet_scop_add_array(scop_kill
, array
);
1237 scop_kill
= pet_scop_prefix(scop_kill
, 3);
1238 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
1243 /* Given an access expression "expr", is the variable accessed by
1244 * "expr" assigned anywhere inside "tree"?
1246 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
1251 id
= pet_expr_access_get_id(expr
);
1252 assigned
= pet_tree_writes(tree
, id
);
1258 /* Are all nested access parameters in "pa" allowed given "tree".
1259 * In particular, is none of them written by anywhere inside "tree".
1261 * If "tree" has any continue or break nodes in the current loop level,
1262 * then no nested access parameters are allowed.
1263 * In particular, if there is any nested access in a guard
1264 * for a piece of code containing a "continue", then we want to introduce
1265 * a separate statement for evaluating this guard so that we can express
1266 * that the result is false for all previous iterations.
1268 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
1269 __isl_keep pet_tree
*tree
)
1276 if (!pet_nested_any_in_pw_aff(pa
))
1279 if (pet_tree_has_continue_or_break(tree
))
1282 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1283 for (i
= 0; i
< nparam
; ++i
) {
1284 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1288 if (!pet_nested_in_id(id
)) {
1293 expr
= pet_nested_extract_expr(id
);
1294 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1295 !is_assigned(expr
, tree
);
1297 pet_expr_free(expr
);
1307 /* Internal data structure for collect_local.
1308 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1309 * "local" collects the results.
1311 struct pet_tree_collect_local_data
{
1313 struct pet_state
*state
;
1314 isl_union_set
*local
;
1317 /* Add the variable accessed by "var" to data->local.
1318 * We extract a representation of the variable from
1319 * the pet_array constructed using extract_array
1320 * to ensure consistency with the rest of the scop.
1322 static int add_local(struct pet_tree_collect_local_data
*data
,
1323 __isl_keep pet_expr
*var
)
1325 struct pet_array
*array
;
1328 array
= extract_array(var
, data
->pc
, data
->state
);
1332 universe
= isl_set_universe(isl_set_get_space(array
->extent
));
1333 data
->local
= isl_union_set_add_set(data
->local
, universe
);
1334 pet_array_free(array
);
1339 /* If the node "tree" declares a variable, then add it to
1342 static int extract_local_var(__isl_keep pet_tree
*tree
, void *user
)
1344 enum pet_tree_type type
;
1345 struct pet_tree_collect_local_data
*data
= user
;
1347 type
= pet_tree_get_type(tree
);
1348 if (type
== pet_tree_decl
|| type
== pet_tree_decl_init
)
1349 return add_local(data
, tree
->u
.d
.var
);
1354 /* If the node "tree" is a for loop that declares its induction variable,
1355 * then add it this induction variable to data->local.
1357 static int extract_local_iterator(__isl_keep pet_tree
*tree
, void *user
)
1359 struct pet_tree_collect_local_data
*data
= user
;
1361 if (pet_tree_get_type(tree
) == pet_tree_for
&& tree
->u
.l
.declared
)
1362 return add_local(data
, tree
->u
.l
.iv
);
1367 /* Collect and return all local variables of the for loop represented
1368 * by "tree", with "scop" the corresponding pet_scop.
1369 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1371 * We collect not only the variables that are declared inside "tree",
1372 * but also the loop iterators that are declared anywhere inside
1373 * any possible macro statements in "scop".
1374 * The latter also appear as declared variable in the scop,
1375 * whereas other declared loop iterators only appear implicitly
1376 * in the iteration domains.
1378 static __isl_give isl_union_set
*collect_local(struct pet_scop
*scop
,
1379 __isl_keep pet_tree
*tree
, __isl_keep pet_context
*pc
,
1380 struct pet_state
*state
)
1384 struct pet_tree_collect_local_data data
= { pc
, state
};
1386 ctx
= pet_tree_get_ctx(tree
);
1387 data
.local
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
1389 if (pet_tree_foreach_sub_tree(tree
, &extract_local_var
, &data
) < 0)
1390 return isl_union_set_free(data
.local
);
1392 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1393 pet_tree
*body
= scop
->stmts
[i
]->body
;
1394 if (pet_tree_foreach_sub_tree(body
, &extract_local_iterator
,
1396 return isl_union_set_free(data
.local
);
1402 /* Add an independence to "scop" if the for node "tree" was marked
1404 * "domain" is the set of loop iterators, with the current for loop
1405 * innermost. If "sign" is positive, then the inner iterator increases.
1406 * Otherwise it decreases.
1407 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1409 * If the tree was marked, then collect all local variables and
1410 * add an independence.
1412 static struct pet_scop
*set_independence(struct pet_scop
*scop
,
1413 __isl_keep pet_tree
*tree
, __isl_keep isl_set
*domain
, int sign
,
1414 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1416 isl_union_set
*local
;
1418 if (!tree
->u
.l
.independent
)
1421 local
= collect_local(scop
, tree
, pc
, state
);
1422 scop
= pet_scop_set_independent(scop
, domain
, local
, sign
);
1427 /* Construct a pet_scop for a for tree with static affine initialization
1428 * and constant increment within the context "pc".
1429 * The domain of "pc" has already been extended with an (at this point
1430 * unbounded) inner loop iterator corresponding to the current for loop.
1432 * The condition is allowed to contain nested accesses, provided
1433 * they are not being written to inside the body of the loop.
1434 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1435 * essentially treated as a while loop, with iteration domain
1436 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1438 * We extract a pet_scop for the body after intersecting the domain of "pc"
1440 * { [l,i] : i >= init and condition' }
1444 * { [l,i] : i <= init and condition' }
1446 * Where condition' is equal to condition if the latter is
1447 * a simple upper [lower] bound and a condition that is extended
1448 * to apply to all previous iterations otherwise.
1449 * Afterwards, the schedule of the pet_scop is extended with
1457 * If the condition is non-affine, then we drop the condition from the
1458 * iteration domain and instead create a separate statement
1459 * for evaluating the condition. The body is then filtered to depend
1460 * on the result of the condition evaluating to true on all iterations
1461 * up to the current iteration, while the evaluation the condition itself
1462 * is filtered to depend on the result of the condition evaluating to true
1463 * on all previous iterations.
1464 * The context of the scop representing the body is dropped
1465 * because we don't know how many times the body will be executed,
1468 * If the stride of the loop is not 1, then "i >= init" is replaced by
1470 * (exists a: i = init + stride * a and a >= 0)
1472 * If the loop iterator i is unsigned, then wrapping may occur.
1473 * We therefore use a virtual iterator instead that does not wrap.
1474 * However, the condition in the code applies
1475 * to the wrapped value, so we need to change condition(l,i)
1476 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1477 * to the original iterator by the wrapping of the virtual iterator.
1478 * Note that there may be no need to perform this final wrapping
1479 * if the loop condition (after wrapping) satisfies certain conditions.
1480 * However, the is_simple_bound condition is not enough since it doesn't
1481 * check if there even is an upper bound.
1483 * Wrapping on unsigned iterators can be avoided entirely if
1484 * loop condition is simple, the loop iterator is incremented
1485 * [decremented] by one and the last value before wrapping cannot
1486 * possibly satisfy the loop condition.
1488 * Valid outer iterators for a for loop are those for which the initial
1489 * value itself, the increment on each domain iteration and
1490 * the condition on both the initial value and
1491 * the result of incrementing the iterator for each iteration of the domain
1493 * If the loop condition is non-affine, then we only consider validity
1494 * of the initial value.
1496 * If the body contains any break, then we keep track of it in "skip"
1497 * (if the skip condition is affine) or it is handled in scop_add_break
1498 * (if the skip condition is not affine).
1499 * Note that the affine break condition needs to be considered with
1500 * respect to previous iterations in the virtual domain (if any).
1502 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1503 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1504 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1505 struct pet_state
*state
)
1509 isl_set
*cond
= NULL
;
1510 isl_set
*skip
= NULL
;
1511 isl_id
*id_test
= NULL
, *id_break_test
;
1512 struct pet_scop
*scop
, *scop_cond
= NULL
;
1519 int has_affine_break
;
1521 isl_map
*rev_wrap
= NULL
;
1522 isl_map
*init_val_map
;
1524 isl_set
*valid_init
;
1525 isl_set
*valid_cond
;
1526 isl_set
*valid_cond_init
;
1527 isl_set
*valid_cond_next
;
1529 pet_expr
*cond_expr
;
1530 pet_context
*pc_nested
;
1532 pos
= pet_context_dim(pc
) - 1;
1534 domain
= pet_context_get_domain(pc
);
1535 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1536 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
1537 pc_nested
= pet_context_copy(pc
);
1538 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1539 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1540 pet_context_free(pc_nested
);
1541 pet_expr_free(cond_expr
);
1543 valid_inc
= isl_pw_aff_domain(pa_inc
);
1545 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1547 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1548 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1550 pa
= isl_pw_aff_free(pa
);
1552 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1553 cond
= isl_pw_aff_non_zero_set(pa
);
1555 cond
= isl_set_universe(isl_set_get_space(domain
));
1557 valid_cond
= isl_set_coalesce(valid_cond
);
1558 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1559 is_virtual
= is_unsigned
&&
1560 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1562 init_val_map
= isl_map_from_pw_aff(isl_pw_aff_copy(init_val
));
1563 init_val_map
= isl_map_equate(init_val_map
, isl_dim_in
, pos
,
1565 valid_cond_init
= enforce_subset(isl_map_domain(init_val_map
),
1566 isl_set_copy(valid_cond
));
1567 if (is_one
&& !is_virtual
) {
1570 isl_pw_aff_free(init_val
);
1571 pa
= pet_expr_extract_comparison(
1572 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1573 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1574 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1575 valid_init
= isl_set_eliminate(valid_init
, isl_dim_set
,
1576 isl_set_dim(domain
, isl_dim_set
) - 1, 1);
1577 cond
= isl_pw_aff_non_zero_set(pa
);
1578 domain
= isl_set_intersect(domain
, cond
);
1582 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1583 strided
= strided_domain(init_val
, isl_val_copy(inc
));
1584 domain
= isl_set_intersect(domain
, strided
);
1588 isl_multi_aff
*wrap
;
1589 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1590 pc
= pet_context_preimage_domain(pc
, wrap
);
1591 rev_wrap
= isl_map_from_multi_aff(wrap
);
1592 rev_wrap
= isl_map_reverse(rev_wrap
);
1593 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1594 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1595 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1597 is_simple
= is_simple_bound(cond
, inc
);
1599 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1600 is_simple
= is_simple_bound(cond
, inc
);
1603 cond
= valid_for_each_iteration(cond
,
1604 isl_set_copy(domain
), isl_val_copy(inc
));
1605 cond
= isl_set_align_params(cond
, isl_set_get_space(domain
));
1606 domain
= isl_set_intersect(domain
, cond
);
1607 sched
= map_to_last(pc
);
1608 if (isl_val_is_neg(inc
))
1609 sched
= isl_aff_neg(sched
);
1611 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1613 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1615 pc
= pet_context_intersect_domain(pc
, isl_set_copy(domain
));
1617 if (is_non_affine
) {
1619 isl_multi_pw_aff
*test_index
;
1620 space
= isl_set_get_space(domain
);
1621 test_index
= pet_create_test_index(space
, state
->n_test
++);
1622 scop_cond
= scop_from_non_affine_condition(
1623 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1624 isl_multi_pw_aff_copy(test_index
),
1625 pet_tree_get_loc(tree
), pc
);
1626 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1628 scop_cond
= pet_scop_add_boolean_array(scop_cond
,
1629 isl_set_copy(domain
), test_index
,
1631 scop_cond
= pet_scop_prefix(scop_cond
, 0);
1632 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
1633 isl_aff_copy(sched
));
1636 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
1637 has_affine_break
= scop
&&
1638 pet_scop_has_affine_skip(scop
, pet_skip_later
);
1639 if (has_affine_break
)
1640 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1641 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
1643 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1644 if (is_non_affine
) {
1645 scop
= pet_scop_reset_context(scop
);
1646 scop
= pet_scop_prefix(scop
, 1);
1648 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
);
1649 scop
= pet_scop_resolve_nested(scop
);
1650 if (has_affine_break
) {
1651 domain
= apply_affine_break(domain
, skip
, isl_val_sgn(inc
),
1652 is_virtual
, rev_wrap
);
1653 scop
= pet_scop_intersect_domain_prefix(scop
,
1654 isl_set_copy(domain
));
1656 isl_map_free(rev_wrap
);
1658 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
1660 if (is_non_affine
) {
1661 scop
= scop_add_while(scop_cond
, scop
, id_test
, domain
,
1663 isl_set_free(valid_inc
);
1665 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_next
);
1666 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_init
);
1667 valid_inc
= isl_set_project_out(valid_inc
, isl_dim_set
, pos
, 1);
1668 scop
= pet_scop_restrict_context(scop
, valid_inc
);
1669 scop
= set_independence(scop
, tree
, domain
, isl_val_sgn(inc
),
1671 isl_set_free(domain
);
1676 valid_init
= isl_set_project_out(valid_init
, isl_dim_set
, pos
, 1);
1677 scop
= pet_scop_restrict_context(scop
, valid_init
);
1679 pet_context_free(pc
);
1683 /* Construct a pet_scop for a for statement within the context of "pc".
1685 * We update the context to reflect the writes to the loop variable and
1686 * the writes inside the body.
1688 * Then we check if the initialization of the for loop
1689 * is a static affine value and the increment is a constant.
1690 * If so, we construct the pet_scop using scop_from_affine_for.
1691 * Otherwise, we treat the for loop as a while loop
1692 * in scop_from_non_affine_for.
1694 * Note that the initialization and the increment are extracted
1695 * in a context where the current loop iterator has been added
1696 * to the context. If these turn out not be affine, then we
1697 * have reconstruct the body context without an assignment
1698 * to this loop iterator, as this variable will then not be
1699 * treated as a dimension of the iteration domain, but as any
1702 static struct pet_scop
*scop_from_for(__isl_keep pet_tree
*tree
,
1703 __isl_keep pet_context
*init_pc
, struct pet_state
*state
)
1707 isl_pw_aff
*pa_inc
, *init_val
;
1708 pet_context
*pc
, *pc_init_val
;
1713 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1714 pc
= pet_context_copy(init_pc
);
1715 pc
= pet_context_add_inner_iterator(pc
, iv
);
1716 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1718 pc_init_val
= pet_context_copy(pc
);
1719 pc_init_val
= pet_context_clear_value(pc_init_val
, isl_id_copy(iv
));
1720 init_val
= pet_expr_extract_affine(tree
->u
.l
.init
, pc_init_val
);
1721 pet_context_free(pc_init_val
);
1722 pa_inc
= pet_expr_extract_affine(tree
->u
.l
.inc
, pc
);
1723 inc
= pet_extract_cst(pa_inc
);
1724 if (!pa_inc
|| !init_val
|| !inc
)
1726 if (!isl_pw_aff_involves_nan(pa_inc
) &&
1727 !isl_pw_aff_involves_nan(init_val
) && !isl_val_is_nan(inc
))
1728 return scop_from_affine_for(tree
, init_val
, pa_inc
, inc
,
1731 isl_pw_aff_free(pa_inc
);
1732 isl_pw_aff_free(init_val
);
1734 pet_context_free(pc
);
1736 pc
= pet_context_copy(init_pc
);
1737 pc
= pet_context_add_infinite_loop(pc
);
1738 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1739 return scop_from_non_affine_for(tree
, init_pc
, pc
, state
);
1741 isl_pw_aff_free(pa_inc
);
1742 isl_pw_aff_free(init_val
);
1744 pet_context_free(pc
);
1748 /* Check whether "expr" is an affine constraint within the context "pc".
1750 static int is_affine_condition(__isl_keep pet_expr
*expr
,
1751 __isl_keep pet_context
*pc
)
1756 pa
= pet_expr_extract_affine_condition(expr
, pc
);
1759 is_affine
= !isl_pw_aff_involves_nan(pa
);
1760 isl_pw_aff_free(pa
);
1765 /* Check if the given if statement is a conditional assignement
1766 * with a non-affine condition.
1768 * In particular we check if "stmt" is of the form
1775 * where the condition is non-affine and a is some array or scalar access.
1777 static int is_conditional_assignment(__isl_keep pet_tree
*tree
,
1778 __isl_keep pet_context
*pc
)
1782 pet_expr
*expr1
, *expr2
;
1784 ctx
= pet_tree_get_ctx(tree
);
1785 if (!pet_options_get_detect_conditional_assignment(ctx
))
1787 if (tree
->type
!= pet_tree_if_else
)
1789 if (tree
->u
.i
.then_body
->type
!= pet_tree_expr
)
1791 if (tree
->u
.i
.else_body
->type
!= pet_tree_expr
)
1793 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1794 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1795 if (pet_expr_get_type(expr1
) != pet_expr_op
)
1797 if (pet_expr_get_type(expr2
) != pet_expr_op
)
1799 if (pet_expr_op_get_type(expr1
) != pet_op_assign
)
1801 if (pet_expr_op_get_type(expr2
) != pet_op_assign
)
1803 expr1
= pet_expr_get_arg(expr1
, 0);
1804 expr2
= pet_expr_get_arg(expr2
, 0);
1805 equal
= pet_expr_is_equal(expr1
, expr2
);
1806 pet_expr_free(expr1
);
1807 pet_expr_free(expr2
);
1808 if (equal
< 0 || !equal
)
1810 if (is_affine_condition(tree
->u
.i
.cond
, pc
))
1816 /* Given that "tree" is of the form
1823 * where a is some array or scalar access, construct a pet_scop
1824 * corresponding to this conditional assignment within the context "pc".
1825 * "cond_pa" is an affine expression with nested accesses representing
1828 * The constructed pet_scop then corresponds to the expression
1830 * a = condition ? f(...) : g(...)
1832 * All access relations in f(...) are intersected with condition
1833 * while all access relation in g(...) are intersected with the complement.
1835 static struct pet_scop
*scop_from_conditional_assignment(
1836 __isl_keep pet_tree
*tree
, __isl_take isl_pw_aff
*cond_pa
,
1837 __isl_take pet_context
*pc
, struct pet_state
*state
)
1840 isl_set
*cond
, *comp
;
1841 isl_multi_pw_aff
*index
;
1842 pet_expr
*expr1
, *expr2
;
1843 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
1844 struct pet_scop
*scop
;
1846 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa
));
1847 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa
));
1848 index
= isl_multi_pw_aff_from_pw_aff(cond_pa
);
1850 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1851 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1853 pe_cond
= pet_expr_from_index(index
);
1855 pe_then
= pet_expr_get_arg(expr1
, 1);
1856 pe_then
= pet_context_evaluate_expr(pc
, pe_then
);
1857 pe_then
= pet_expr_restrict(pe_then
, cond
);
1858 pe_else
= pet_expr_get_arg(expr2
, 1);
1859 pe_else
= pet_context_evaluate_expr(pc
, pe_else
);
1860 pe_else
= pet_expr_restrict(pe_else
, comp
);
1861 pe_write
= pet_expr_get_arg(expr1
, 0);
1862 pe_write
= pet_context_evaluate_expr(pc
, pe_write
);
1864 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
1865 type_size
= pet_expr_get_type_size(pe_write
);
1866 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, pe_write
, pe
);
1868 scop
= scop_from_evaluated_expr(pe
, state
->n_stmt
++,
1869 pet_tree_get_loc(tree
), pc
);
1871 pet_context_free(pc
);
1876 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1878 * We create a separate statement that writes the result
1879 * of the non-affine condition to a virtual scalar.
1880 * A constraint requiring the value of this virtual scalar to be one
1881 * is added to the iteration domains of the then branch.
1882 * Similarly, a constraint requiring the value of this virtual scalar
1883 * to be zero is added to the iteration domains of the else branch, if any.
1884 * We adjust the schedules to ensure that the virtual scalar is written
1885 * before it is read.
1887 * If there are any breaks or continues in the then and/or else
1888 * branches, then we may have to compute a new skip condition.
1889 * This is handled using a pet_skip_info object.
1890 * On initialization, the object checks if skip conditions need
1891 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1892 * adds them in pet_skip_info_if_add.
1894 static struct pet_scop
*scop_from_non_affine_if(__isl_keep pet_tree
*tree
,
1895 __isl_take pet_context
*pc
, struct pet_state
*state
)
1900 isl_multi_pw_aff
*test_index
;
1901 struct pet_skip_info skip
;
1902 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1904 has_else
= tree
->type
== pet_tree_if_else
;
1906 space
= pet_context_get_space(pc
);
1907 test_index
= pet_create_test_index(space
, state
->n_test
++);
1908 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
1909 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
1910 pet_tree_get_loc(tree
), pc
);
1911 domain
= pet_context_get_domain(pc
);
1912 scop
= pet_scop_add_boolean_array(scop
, domain
,
1913 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1915 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1917 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1919 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1921 pet_skip_info_if_extract_index(&skip
, test_index
, pc
, state
);
1923 scop
= pet_scop_prefix(scop
, 0);
1924 scop_then
= pet_scop_prefix(scop_then
, 1);
1925 scop_then
= pet_scop_filter(scop_then
,
1926 isl_multi_pw_aff_copy(test_index
), 1);
1928 scop_else
= pet_scop_prefix(scop_else
, 1);
1929 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1930 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1932 isl_multi_pw_aff_free(test_index
);
1934 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1936 scop
= pet_skip_info_if_add(&skip
, scop
, 2);
1938 pet_context_free(pc
);
1942 /* Construct a pet_scop for an affine if statement within the context "pc".
1944 * The condition is added to the iteration domains of the then branch,
1945 * while the opposite of the condition in added to the iteration domains
1946 * of the else branch, if any.
1948 * If there are any breaks or continues in the then and/or else
1949 * branches, then we may have to compute a new skip condition.
1950 * This is handled using a pet_skip_info_if object.
1951 * On initialization, the object checks if skip conditions need
1952 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1953 * adds them in pet_skip_info_if_add.
1955 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1956 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
1957 struct pet_state
*state
)
1961 isl_set
*set
, *complement
;
1963 struct pet_skip_info skip
;
1964 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1965 pet_context
*pc_body
;
1967 ctx
= pet_tree_get_ctx(tree
);
1969 has_else
= tree
->type
== pet_tree_if_else
;
1971 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1972 set
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
1974 pc_body
= pet_context_copy(pc
);
1975 pc_body
= pet_context_intersect_domain(pc_body
, isl_set_copy(set
));
1976 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc_body
, state
);
1977 pet_context_free(pc_body
);
1979 pc_body
= pet_context_copy(pc
);
1980 complement
= isl_set_copy(valid
);
1981 complement
= isl_set_subtract(valid
, isl_set_copy(set
));
1982 pc_body
= pet_context_intersect_domain(pc_body
,
1983 isl_set_copy(complement
));
1984 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc_body
, state
);
1985 pet_context_free(pc_body
);
1988 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
1989 pet_skip_info_if_extract_cond(&skip
, cond
, pc
, state
);
1990 isl_pw_aff_free(cond
);
1992 scop
= pet_scop_restrict(scop_then
, set
);
1995 scop_else
= pet_scop_restrict(scop_else
, complement
);
1996 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
1998 scop
= pet_scop_resolve_nested(scop
);
1999 scop
= pet_scop_restrict_context(scop
, valid
);
2001 if (pet_skip_info_has_skip(&skip
))
2002 scop
= pet_scop_prefix(scop
, 0);
2003 scop
= pet_skip_info_if_add(&skip
, scop
, 1);
2005 pet_context_free(pc
);
2009 /* Construct a pet_scop for an if statement within the context "pc".
2011 * If the condition fits the pattern of a conditional assignment,
2012 * then it is handled by scop_from_conditional_assignment.
2013 * Note that the condition is only considered for a conditional assignment
2014 * if it is not static-affine. However, it should still convert
2015 * to an affine expression when nesting is allowed.
2017 * Otherwise, we check if the condition is affine.
2018 * If so, we construct the scop in scop_from_affine_if.
2019 * Otherwise, we construct the scop in scop_from_non_affine_if.
2021 * We allow the condition to be dynamic, i.e., to refer to
2022 * scalars or array elements that may be written to outside
2023 * of the given if statement. These nested accesses are then represented
2024 * as output dimensions in the wrapping iteration domain.
2025 * If it is also written _inside_ the then or else branch, then
2026 * we treat the condition as non-affine.
2027 * As explained in extract_non_affine_if, this will introduce
2028 * an extra statement.
2029 * For aesthetic reasons, we want this statement to have a statement
2030 * number that is lower than those of the then and else branches.
2031 * In order to evaluate if we will need such a statement, however, we
2032 * first construct scops for the then and else branches.
2033 * We therefore reserve a statement number if we might have to
2034 * introduce such an extra statement.
2036 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
2037 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2041 pet_expr
*cond_expr
;
2042 pet_context
*pc_nested
;
2047 has_else
= tree
->type
== pet_tree_if_else
;
2049 pc
= pet_context_copy(pc
);
2050 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
2052 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
2054 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
2055 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
2056 pc_nested
= pet_context_copy(pc
);
2057 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
2058 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
2059 pet_context_free(pc_nested
);
2060 pet_expr_free(cond_expr
);
2063 pet_context_free(pc
);
2067 if (isl_pw_aff_involves_nan(cond
)) {
2068 isl_pw_aff_free(cond
);
2069 return scop_from_non_affine_if(tree
, pc
, state
);
2072 if (is_conditional_assignment(tree
, pc
))
2073 return scop_from_conditional_assignment(tree
, cond
, pc
, state
);
2075 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
2076 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
2077 isl_pw_aff_free(cond
);
2078 return scop_from_non_affine_if(tree
, pc
, state
);
2081 return scop_from_affine_if(tree
, cond
, pc
, state
);
2084 /* Return a one-dimensional multi piecewise affine expression that is equal
2085 * to the constant 1 and is defined over the given domain.
2087 static __isl_give isl_multi_pw_aff
*one_mpa(__isl_take isl_space
*space
)
2089 isl_local_space
*ls
;
2092 ls
= isl_local_space_from_space(space
);
2093 aff
= isl_aff_zero_on_domain(ls
);
2094 aff
= isl_aff_set_constant_si(aff
, 1);
2096 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
2099 /* Construct a pet_scop for a continue statement with the given domain space.
2101 * We simply create an empty scop with a universal pet_skip_now
2102 * skip condition. This skip condition will then be taken into
2103 * account by the enclosing loop construct, possibly after
2104 * being incorporated into outer skip conditions.
2106 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
,
2107 __isl_take isl_space
*space
)
2109 struct pet_scop
*scop
;
2111 scop
= pet_scop_empty(isl_space_copy(space
));
2113 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(space
));
2118 /* Construct a pet_scop for a break statement with the given domain space.
2120 * We simply create an empty scop with both a universal pet_skip_now
2121 * skip condition and a universal pet_skip_later skip condition.
2122 * These skip conditions will then be taken into
2123 * account by the enclosing loop construct, possibly after
2124 * being incorporated into outer skip conditions.
2126 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
,
2127 __isl_take isl_space
*space
)
2129 struct pet_scop
*scop
;
2130 isl_multi_pw_aff
*skip
;
2132 scop
= pet_scop_empty(isl_space_copy(space
));
2134 skip
= one_mpa(space
);
2135 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
2136 isl_multi_pw_aff_copy(skip
));
2137 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
2142 /* Extract a clone of the kill statement in "scop".
2143 * The domain of the clone is given by "domain".
2144 * "scop" is expected to have been created from a DeclStmt
2145 * and should have the kill as its first statement.
2147 static struct pet_scop
*extract_kill(__isl_keep isl_set
*domain
,
2148 struct pet_scop
*scop
, struct pet_state
*state
)
2151 struct pet_stmt
*stmt
;
2153 isl_multi_pw_aff
*mpa
;
2156 if (!domain
|| !scop
)
2158 if (scop
->n_stmt
< 1)
2159 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
2160 "expecting at least one statement", return NULL
);
2161 stmt
= scop
->stmts
[0];
2162 if (!pet_stmt_is_kill(stmt
))
2163 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
2164 "expecting kill statement", return NULL
);
2166 kill
= pet_tree_expr_get_expr(stmt
->body
);
2167 space
= pet_stmt_get_space(stmt
);
2168 space
= isl_space_map_from_set(space
);
2169 mpa
= isl_multi_pw_aff_identity(space
);
2170 mpa
= isl_multi_pw_aff_reset_tuple_id(mpa
, isl_dim_in
);
2171 kill
= pet_expr_update_domain(kill
, mpa
);
2172 tree
= pet_tree_new_expr(kill
);
2173 tree
= pet_tree_set_loc(tree
, pet_loc_copy(stmt
->loc
));
2174 stmt
= pet_stmt_from_pet_tree(isl_set_copy(domain
),
2175 state
->n_stmt
++, tree
);
2176 return pet_scop_from_pet_stmt(isl_set_get_space(domain
), stmt
);
2179 /* Does "tree" represent an assignment to a variable?
2181 * The assignment may be one of
2182 * - a declaration with initialization
2183 * - an expression with a top-level assignment operator
2185 static int is_assignment(__isl_keep pet_tree
*tree
)
2189 if (tree
->type
== pet_tree_decl_init
)
2191 return pet_tree_is_assign(tree
);
2194 /* Update "pc" by taking into account the assignment performed by "tree",
2195 * where "tree" satisfies is_assignment.
2197 * In particular, if the lhs of the assignment is a scalar variable and
2198 * if the rhs is an affine expression, then keep track of this value in "pc"
2199 * so that we can plug it in when we later come across the same variable.
2201 * Any previously assigned value to the variable has already been removed
2202 * by scop_handle_writes.
2204 static __isl_give pet_context
*handle_assignment(__isl_take pet_context
*pc
,
2205 __isl_keep pet_tree
*tree
)
2207 pet_expr
*var
, *val
;
2211 if (pet_tree_get_type(tree
) == pet_tree_decl_init
) {
2212 var
= pet_tree_decl_get_var(tree
);
2213 val
= pet_tree_decl_get_init(tree
);
2216 expr
= pet_tree_expr_get_expr(tree
);
2217 var
= pet_expr_get_arg(expr
, 0);
2218 val
= pet_expr_get_arg(expr
, 1);
2219 pet_expr_free(expr
);
2222 if (!pet_expr_is_scalar_access(var
)) {
2228 pa
= pet_expr_extract_affine(val
, pc
);
2230 pc
= pet_context_free(pc
);
2232 if (!isl_pw_aff_involves_nan(pa
)) {
2233 id
= pet_expr_access_get_id(var
);
2234 pc
= pet_context_set_value(pc
, id
, pa
);
2236 isl_pw_aff_free(pa
);
2244 /* Mark all arrays in "scop" as being exposed.
2246 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
2252 for (i
= 0; i
< scop
->n_array
; ++i
)
2253 scop
->arrays
[i
]->exposed
= 1;
2257 /* Try and construct a pet_scop corresponding to (part of)
2258 * a sequence of statements within the context "pc".
2260 * After extracting a statement, we update "pc"
2261 * based on the top-level assignments in the statement
2262 * so that we can exploit them in subsequent statements in the same block.
2264 * If there are any breaks or continues in the individual statements,
2265 * then we may have to compute a new skip condition.
2266 * This is handled using a pet_skip_info object.
2267 * On initialization, the object checks if skip conditions need
2268 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2269 * adds them in pet_skip_info_seq_add.
2271 * If "block" is set, then we need to insert kill statements at
2272 * the end of the block for any array that has been declared by
2273 * one of the statements in the sequence. Each of these declarations
2274 * results in the construction of a kill statement at the place
2275 * of the declaration, so we simply collect duplicates of
2276 * those kill statements and append these duplicates to the constructed scop.
2278 * If "block" is not set, then any array declared by one of the statements
2279 * in the sequence is marked as being exposed.
2281 * If autodetect is set, then we allow the extraction of only a subrange
2282 * of the sequence of statements. However, if there is at least one statement
2283 * for which we could not construct a scop and the final range contains
2284 * either no statements or at least one kill, then we discard the entire
2287 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
2288 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2294 struct pet_scop
*scop
, *kills
;
2296 ctx
= pet_tree_get_ctx(tree
);
2298 space
= pet_context_get_space(pc
);
2299 domain
= pet_context_get_domain(pc
);
2300 pc
= pet_context_copy(pc
);
2301 scop
= pet_scop_empty(isl_space_copy(space
));
2302 kills
= pet_scop_empty(space
);
2303 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
2304 struct pet_scop
*scop_i
;
2306 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
2307 pc
= apply_affine_continue(pc
, scop
);
2308 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
2309 pc
= scop_handle_writes(scop_i
, pc
);
2310 if (is_assignment(tree
->u
.b
.child
[i
]))
2311 pc
= handle_assignment(pc
, tree
->u
.b
.child
[i
]);
2312 struct pet_skip_info skip
;
2313 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
2314 pet_skip_info_seq_extract(&skip
, pc
, state
);
2315 if (pet_skip_info_has_skip(&skip
))
2316 scop_i
= pet_scop_prefix(scop_i
, 0);
2317 if (scop_i
&& pet_tree_is_decl(tree
->u
.b
.child
[i
])) {
2318 if (tree
->u
.b
.block
) {
2319 struct pet_scop
*kill
;
2320 kill
= extract_kill(domain
, scop_i
, state
);
2321 kills
= pet_scop_add_par(ctx
, kills
, kill
);
2323 scop_i
= mark_exposed(scop_i
);
2325 scop_i
= pet_scop_prefix(scop_i
, i
);
2326 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
2328 scop
= pet_skip_info_seq_add(&skip
, scop
, i
);
2333 isl_set_free(domain
);
2335 kills
= pet_scop_prefix(kills
, tree
->u
.b
.n
);
2336 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
2338 pet_context_free(pc
);
2343 /* Internal data structure for extract_declared_arrays.
2345 * "pc" and "state" are used to create pet_array objects and kill statements.
2346 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2347 * found any declared array.
2348 * "scop" has been initialized by the caller and is used to attach
2349 * the created pet_array objects.
2350 * "kill_before" and "kill_after" are created and updated by
2351 * extract_declared_arrays to collect the kills of the arrays.
2353 struct pet_tree_extract_declared_arrays_data
{
2355 struct pet_state
*state
;
2360 struct pet_scop
*scop
;
2361 struct pet_scop
*kill_before
;
2362 struct pet_scop
*kill_after
;
2365 /* Check if the node "node" declares any array or scalar.
2366 * If so, create the corresponding pet_array and attach it to data->scop.
2367 * Additionally, create two kill statements for the array and add them
2368 * to data->kill_before and data->kill_after.
2370 static int extract_declared_arrays(__isl_keep pet_tree
*node
, void *user
)
2372 enum pet_tree_type type
;
2373 struct pet_tree_extract_declared_arrays_data
*data
= user
;
2374 struct pet_array
*array
;
2375 struct pet_scop
*scop_kill
;
2378 type
= pet_tree_get_type(node
);
2379 if (type
== pet_tree_decl
|| type
== pet_tree_decl_init
)
2380 var
= node
->u
.d
.var
;
2381 else if (type
== pet_tree_for
&& node
->u
.l
.declared
)
2386 array
= extract_array(var
, data
->pc
, data
->state
);
2388 array
->declared
= 1;
2389 data
->scop
= pet_scop_add_array(data
->scop
, array
);
2391 scop_kill
= kill(pet_tree_get_loc(node
), array
, data
->pc
, data
->state
);
2393 data
->kill_before
= scop_kill
;
2395 data
->kill_before
= pet_scop_add_par(data
->ctx
,
2396 data
->kill_before
, scop_kill
);
2398 scop_kill
= kill(pet_tree_get_loc(node
), array
, data
->pc
, data
->state
);
2400 data
->kill_after
= scop_kill
;
2402 data
->kill_after
= pet_scop_add_par(data
->ctx
,
2403 data
->kill_after
, scop_kill
);
2410 /* Convert a pet_tree that consists of more than a single leaf
2411 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2412 * Do so within the context of "pc".
2414 * After constructing the core scop, we also look for any arrays (or scalars)
2415 * that are declared inside "tree". Each of those arrays is marked as
2416 * having been declared and kill statements for these arrays
2417 * are introduced before and after the core scop.
2418 * Note that the input tree is not a leaf so that the declaration
2419 * cannot occur at the outer level.
2421 static struct pet_scop
*scop_from_tree_macro(__isl_take pet_tree
*tree
,
2422 __isl_take isl_id
*label
, __isl_keep pet_context
*pc
,
2423 struct pet_state
*state
)
2425 struct pet_tree_extract_declared_arrays_data data
= { pc
, state
};
2427 data
.scop
= scop_from_unevaluated_tree(pet_tree_copy(tree
),
2428 state
->n_stmt
++, pc
);
2431 data
.ctx
= pet_context_get_ctx(pc
);
2432 if (pet_tree_foreach_sub_tree(tree
, &extract_declared_arrays
,
2434 data
.scop
= pet_scop_free(data
.scop
);
2435 pet_tree_free(tree
);
2440 data
.kill_before
= pet_scop_prefix(data
.kill_before
, 0);
2441 data
.scop
= pet_scop_prefix(data
.scop
, 1);
2442 data
.kill_after
= pet_scop_prefix(data
.kill_after
, 2);
2444 data
.scop
= pet_scop_add_seq(data
.ctx
, data
.kill_before
, data
.scop
);
2445 data
.scop
= pet_scop_add_seq(data
.ctx
, data
.scop
, data
.kill_after
);
2450 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2451 * within the context "pc" by calling the appropriate function
2452 * based on the type of "tree".
2454 * If the initially constructed pet_scop turns out to involve
2455 * dynamic control and if the user has requested an encapsulation
2456 * of all dynamic control, then this pet_scop is discarded and
2457 * a new pet_scop is created with a single statement representing
2458 * the entire "tree".
2459 * However, if the scop contains any active continue or break,
2460 * then we need to include the loop containing the continue or break
2461 * in the encapsulation. We therefore postpone the encapsulation
2462 * until we have constructed a pet_scop for this enclosing loop.
2464 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
2465 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2468 struct pet_scop
*scop
= NULL
;
2473 ctx
= pet_tree_get_ctx(tree
);
2474 switch (tree
->type
) {
2475 case pet_tree_error
:
2477 case pet_tree_block
:
2478 return scop_from_block(tree
, pc
, state
);
2479 case pet_tree_break
:
2480 return scop_from_break(tree
, pet_context_get_space(pc
));
2481 case pet_tree_continue
:
2482 return scop_from_continue(tree
, pet_context_get_space(pc
));
2484 case pet_tree_decl_init
:
2485 return scop_from_decl(tree
, pc
, state
);
2487 return scop_from_tree_expr(tree
, pc
, state
);
2489 case pet_tree_if_else
:
2490 scop
= scop_from_if(tree
, pc
, state
);
2493 scop
= scop_from_for(tree
, pc
, state
);
2495 case pet_tree_while
:
2496 scop
= scop_from_while(tree
, pc
, state
);
2498 case pet_tree_infinite_loop
:
2499 scop
= scop_from_infinite_for(tree
, pc
, state
);
2506 if (!pet_options_get_encapsulate_dynamic_control(ctx
) ||
2507 !pet_scop_has_data_dependent_conditions(scop
) ||
2508 pet_scop_has_var_skip(scop
, pet_skip_now
))
2511 pet_scop_free(scop
);
2512 return scop_from_tree_macro(pet_tree_copy(tree
),
2513 isl_id_copy(tree
->label
), pc
, state
);
2516 /* If "tree" has a label that is of the form S_<nr>, then make
2517 * sure that state->n_stmt is greater than nr to ensure that
2518 * we will not generate S_<nr> ourselves.
2520 static int set_first_stmt(__isl_keep pet_tree
*tree
, void *user
)
2522 struct pet_state
*state
= user
;
2530 name
= isl_id_get_name(tree
->label
);
2531 if (strncmp(name
, "S_", 2) != 0)
2533 nr
= atoi(name
+ 2);
2534 if (nr
>= state
->n_stmt
)
2535 state
->n_stmt
= nr
+ 1;
2540 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2541 * "int_size" is the number of bytes need to represent an integer.
2542 * "extract_array" is a callback that we can use to create a pet_array
2543 * that corresponds to the variable accessed by an expression.
2545 * Initialize the global state, construct a context and then
2546 * construct the pet_scop by recursively visiting the tree.
2548 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2550 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
2551 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
2552 __isl_keep pet_context
*pc
, void *user
), void *user
,
2553 __isl_keep pet_context
*pc
)
2555 struct pet_scop
*scop
;
2556 struct pet_state state
= { 0 };
2561 state
.ctx
= pet_tree_get_ctx(tree
);
2562 state
.int_size
= int_size
;
2563 state
.extract_array
= extract_array
;
2565 if (pet_tree_foreach_sub_tree(tree
, &set_first_stmt
, &state
) < 0)
2566 tree
= pet_tree_free(tree
);
2568 scop
= scop_from_tree(tree
, pc
, &state
);
2569 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
2571 pet_tree_free(tree
);
2574 scop
->context
= isl_set_params(scop
->context
);