2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
38 #include <isl/id_to_pw_aff.h>
39 #include <isl/union_set.h>
48 #include "tree2scop.h"
50 /* Update "pc" by taking into account the writes in "stmt".
51 * That is, clear any previously assigned values to variables
52 * that are written by "stmt".
54 static __isl_give pet_context
*handle_writes(struct pet_stmt
*stmt
,
55 __isl_take pet_context
*pc
)
57 return pet_context_clear_writes_in_tree(pc
, stmt
->body
);
60 /* Update "pc" based on the write accesses in "scop".
62 static __isl_give pet_context
*scop_handle_writes(struct pet_scop
*scop
,
63 __isl_take pet_context
*pc
)
68 return pet_context_free(pc
);
69 for (i
= 0; i
< scop
->n_stmt
; ++i
)
70 pc
= handle_writes(scop
->stmts
[i
], pc
);
75 /* Wrapper around pet_expr_resolve_assume
76 * for use as a callback to pet_tree_map_expr.
78 static __isl_give pet_expr
*resolve_assume(__isl_take pet_expr
*expr
,
81 pet_context
*pc
= user
;
83 return pet_expr_resolve_assume(expr
, pc
);
86 /* Check if any expression inside "tree" is an assume expression and
87 * if its single argument can be converted to an affine expression
88 * in the context of "pc".
89 * If so, replace the argument by the affine expression.
91 __isl_give pet_tree
*pet_tree_resolve_assume(__isl_take pet_tree
*tree
,
92 __isl_keep pet_context
*pc
)
94 return pet_tree_map_expr(tree
, &resolve_assume
, pc
);
97 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
98 * "tree" has already been evaluated in the context of "pc".
99 * This mainly involves resolving nested expression parameters
100 * and setting the name of the iteration space.
101 * The name is given by tree->label if it is non-NULL. Otherwise,
102 * it is of the form S_<stmt_nr>.
104 static struct pet_scop
*scop_from_evaluated_tree(__isl_take pet_tree
*tree
,
105 int stmt_nr
, __isl_keep pet_context
*pc
)
111 space
= pet_context_get_space(pc
);
113 tree
= pet_tree_resolve_nested(tree
, space
);
114 tree
= pet_tree_resolve_assume(tree
, pc
);
116 domain
= pet_context_get_domain(pc
);
117 ps
= pet_stmt_from_pet_tree(domain
, stmt_nr
, tree
);
118 return pet_scop_from_pet_stmt(space
, ps
);
121 /* Convert a top-level pet_expr to a pet_scop with one statement
122 * within the context "pc".
123 * "expr" has already been evaluated in the context of "pc".
124 * We construct a pet_tree from "expr" and continue with
125 * scop_from_evaluated_tree.
126 * The name is of the form S_<stmt_nr>.
127 * The location of the statement is set to "loc".
129 static struct pet_scop
*scop_from_evaluated_expr(__isl_take pet_expr
*expr
,
130 int stmt_nr
, __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
134 tree
= pet_tree_new_expr(expr
);
135 tree
= pet_tree_set_loc(tree
, loc
);
136 return scop_from_evaluated_tree(tree
, stmt_nr
, pc
);
139 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
140 * "tree" has not yet been evaluated in the context of "pc".
141 * We evaluate "tree" in the context of "pc" and continue with
142 * scop_from_evaluated_tree.
143 * The statement name is given by tree->label if it is non-NULL. Otherwise,
144 * it is of the form S_<stmt_nr>.
146 static struct pet_scop
*scop_from_unevaluated_tree(__isl_take pet_tree
*tree
,
147 int stmt_nr
, __isl_keep pet_context
*pc
)
149 tree
= pet_context_evaluate_tree(pc
, tree
);
150 return scop_from_evaluated_tree(tree
, stmt_nr
, pc
);
153 /* Convert a top-level pet_expr to a pet_scop with one statement
154 * within the context "pc", where "expr" has not yet been evaluated
155 * in the context of "pc".
156 * We construct a pet_tree from "expr" and continue with
157 * scop_from_unevaluated_tree.
158 * The statement name is of the form S_<stmt_nr>.
159 * The location of the statement is set to "loc".
161 static struct pet_scop
*scop_from_expr(__isl_take pet_expr
*expr
,
162 int stmt_nr
, __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
166 tree
= pet_tree_new_expr(expr
);
167 tree
= pet_tree_set_loc(tree
, loc
);
168 return scop_from_unevaluated_tree(tree
, stmt_nr
, pc
);
171 /* Construct a pet_scop with a single statement killing the entire
173 * The location of the statement is set to "loc".
175 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
176 __isl_keep pet_context
*pc
, struct pet_state
*state
)
181 isl_multi_pw_aff
*index
;
184 struct pet_scop
*scop
;
188 ctx
= isl_set_get_ctx(array
->extent
);
189 access
= isl_map_from_range(isl_set_copy(array
->extent
));
190 id
= isl_set_get_tuple_id(array
->extent
);
191 space
= isl_space_alloc(ctx
, 0, 0, 0);
192 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
193 index
= isl_multi_pw_aff_zero(space
);
194 expr
= pet_expr_kill_from_access_and_index(access
, index
);
195 return scop_from_expr(expr
, state
->n_stmt
++, loc
, pc
);
201 /* Construct and return a pet_array corresponding to the variable
202 * accessed by "access" by calling the extract_array callback.
204 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
205 __isl_keep pet_context
*pc
, struct pet_state
*state
)
207 return state
->extract_array(access
, pc
, state
->user
);
210 /* Construct a pet_scop for a (single) variable declaration
211 * within the context "pc".
213 * The scop contains the variable being declared (as an array)
214 * and a statement killing the array.
216 * If the declaration comes with an initialization, then the scop
217 * also contains an assignment to the variable.
219 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
220 __isl_keep pet_context
*pc
, struct pet_state
*state
)
224 struct pet_array
*array
;
225 struct pet_scop
*scop_decl
, *scop
;
226 pet_expr
*lhs
, *rhs
, *pe
;
228 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
231 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
232 scop_decl
= pet_scop_add_array(scop_decl
, array
);
234 if (tree
->type
!= pet_tree_decl_init
)
237 lhs
= pet_expr_copy(tree
->u
.d
.var
);
238 rhs
= pet_expr_copy(tree
->u
.d
.init
);
239 type_size
= pet_expr_get_type_size(lhs
);
240 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
241 scop
= scop_from_expr(pe
, state
->n_stmt
++, pet_tree_get_loc(tree
), pc
);
243 ctx
= pet_tree_get_ctx(tree
);
244 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
249 /* Does "tree" represent a kill statement?
250 * That is, is it an expression statement that "calls" __pencil_kill?
252 static int is_pencil_kill(__isl_keep pet_tree
*tree
)
259 if (tree
->type
!= pet_tree_expr
)
261 expr
= tree
->u
.e
.expr
;
262 if (pet_expr_get_type(expr
) != pet_expr_call
)
264 name
= pet_expr_call_get_name(expr
);
267 return !strcmp(name
, "__pencil_kill");
270 /* Add a kill to "scop" that kills what is accessed by
271 * the access expression "expr".
273 * Mark the access as a write prior to evaluation to avoid
274 * the access being replaced by a possible known value
275 * during the evaluation.
277 * If the access expression has any arguments (after evaluation
278 * in the context of "pc"), then we ignore it, since we cannot
279 * tell which elements are definitely killed.
281 * Otherwise, we extend the index expression to the dimension
282 * of the accessed array and intersect with the extent of the array and
283 * add a kill expression that kills these array elements is added to "scop".
285 static struct pet_scop
*scop_add_kill(struct pet_scop
*scop
,
286 __isl_take pet_expr
*expr
, __isl_take pet_loc
*loc
,
287 __isl_keep pet_context
*pc
, struct pet_state
*state
)
291 isl_multi_pw_aff
*index
;
294 struct pet_array
*array
;
295 struct pet_scop
*scop_i
;
297 expr
= pet_expr_access_set_write(expr
, 1);
298 expr
= pet_context_evaluate_expr(pc
, expr
);
301 if (expr
->n_arg
!= 0) {
306 array
= extract_array(expr
, pc
, state
);
309 index
= pet_expr_access_get_index(expr
);
311 map
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
312 id
= isl_map_get_tuple_id(map
, isl_dim_out
);
313 dim1
= isl_set_dim(array
->extent
, isl_dim_set
);
314 dim2
= isl_map_dim(map
, isl_dim_out
);
315 map
= isl_map_add_dims(map
, isl_dim_out
, dim1
- dim2
);
316 map
= isl_map_set_tuple_id(map
, isl_dim_out
, id
);
317 map
= isl_map_intersect_range(map
, isl_set_copy(array
->extent
));
318 pet_array_free(array
);
319 kill
= pet_expr_kill_from_access_and_index(map
, index
);
320 scop_i
= scop_from_evaluated_expr(kill
, state
->n_stmt
++, loc
, pc
);
321 scop
= pet_scop_add_par(state
->ctx
, scop
, scop_i
);
327 return pet_scop_free(scop
);
330 /* For each argument of the __pencil_kill call in "tree" that
331 * represents an access, add a kill statement to "scop" killing the accessed
334 static struct pet_scop
*scop_from_pencil_kill(__isl_keep pet_tree
*tree
,
335 __isl_keep pet_context
*pc
, struct pet_state
*state
)
338 struct pet_scop
*scop
;
341 call
= tree
->u
.e
.expr
;
343 scop
= pet_scop_empty(pet_context_get_space(pc
));
345 n
= pet_expr_get_n_arg(call
);
346 for (i
= 0; i
< n
; ++i
) {
350 arg
= pet_expr_get_arg(call
, i
);
352 return pet_scop_free(scop
);
353 if (pet_expr_get_type(arg
) != pet_expr_access
) {
357 loc
= pet_tree_get_loc(tree
);
358 scop
= scop_add_kill(scop
, arg
, loc
, pc
, state
);
364 /* Construct a pet_scop for an expression statement within the context "pc".
366 * If the expression calls __pencil_kill, then it needs to be converted
367 * into zero or more kill statements.
368 * Otherwise, a scop is extracted directly from the tree.
370 static struct pet_scop
*scop_from_tree_expr(__isl_keep pet_tree
*tree
,
371 __isl_keep pet_context
*pc
, struct pet_state
*state
)
375 is_kill
= is_pencil_kill(tree
);
379 return scop_from_pencil_kill(tree
, pc
, state
);
380 return scop_from_unevaluated_tree(pet_tree_copy(tree
),
381 state
->n_stmt
++, pc
);
384 /* Return those elements in the space of "cond" that come after
385 * (based on "sign") an element in "cond" in the final dimension.
387 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
390 isl_map
*previous_to_this
;
393 dim
= isl_set_dim(cond
, isl_dim_set
);
394 space
= isl_space_map_from_set(isl_set_get_space(cond
));
395 previous_to_this
= isl_map_universe(space
);
396 for (i
= 0; i
+ 1 < dim
; ++i
)
397 previous_to_this
= isl_map_equate(previous_to_this
,
398 isl_dim_in
, i
, isl_dim_out
, i
);
400 previous_to_this
= isl_map_order_lt(previous_to_this
,
401 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
403 previous_to_this
= isl_map_order_gt(previous_to_this
,
404 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
406 cond
= isl_set_apply(cond
, previous_to_this
);
411 /* Remove those iterations of "domain" that have an earlier iteration
412 * (based on "sign") in the final dimension where "skip" is satisfied.
413 * If "apply_skip_map" is set, then "skip_map" is first applied
414 * to the embedded skip condition before removing it from the domain.
416 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
417 __isl_take isl_set
*skip
, int sign
,
418 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
421 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
422 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
423 return isl_set_subtract(domain
, after(skip
, sign
));
426 /* Create a single-dimensional multi-affine expression on the domain space
427 * of "pc" that is equal to the final dimension of this domain.
428 * "loop_nr" is the sequence number of the corresponding loop.
429 * If "id" is not NULL, then it is used as the output tuple name.
430 * Otherwise, the name is constructed as L_<loop_nr>.
432 static __isl_give isl_multi_aff
*map_to_last(__isl_keep pet_context
*pc
,
433 int loop_nr
, __isl_keep isl_id
*id
)
443 space
= pet_context_get_space(pc
);
444 pos
= isl_space_dim(space
, isl_dim_set
) - 1;
445 ls
= isl_local_space_from_space(space
);
446 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, pos
);
447 ma
= isl_multi_aff_from_aff(aff
);
450 label
= isl_id_copy(id
);
452 snprintf(name
, sizeof(name
), "L_%d", loop_nr
);
453 label
= isl_id_alloc(pet_context_get_ctx(pc
), name
, NULL
);
455 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, label
);
460 /* Create an affine expression that maps elements
461 * of an array "id_test" to the previous element in the final dimension
462 * (according to "inc"), provided this element belongs to "domain".
463 * That is, create the affine expression
465 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
467 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
468 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
475 isl_multi_pw_aff
*prev
;
477 pos
= isl_set_dim(domain
, isl_dim_set
) - 1;
478 space
= isl_set_get_space(domain
);
479 space
= isl_space_map_from_set(space
);
480 ma
= isl_multi_aff_identity(space
);
481 aff
= isl_multi_aff_get_aff(ma
, pos
);
482 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
483 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
484 domain
= isl_set_preimage_multi_aff(domain
, isl_multi_aff_copy(ma
));
485 prev
= isl_multi_pw_aff_from_multi_aff(ma
);
486 pa
= isl_multi_pw_aff_get_pw_aff(prev
, pos
);
487 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
488 prev
= isl_multi_pw_aff_set_pw_aff(prev
, pos
, pa
);
489 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
494 /* Add an implication to "scop" expressing that if an element of
495 * virtual array "id_test" has value "satisfied" then all previous elements
496 * of this array (in the final dimension) also have that value.
497 * The set of previous elements is bounded by "domain".
498 * If "sign" is negative then the iterator
499 * is decreasing and we express that all subsequent array elements
500 * (but still defined previously) have the same value.
502 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
503 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
510 dim
= isl_set_dim(domain
, isl_dim_set
);
511 domain
= isl_set_set_tuple_id(domain
, id_test
);
512 space
= isl_space_map_from_set(isl_set_get_space(domain
));
513 map
= isl_map_universe(space
);
514 for (i
= 0; i
+ 1 < dim
; ++i
)
515 map
= isl_map_equate(map
, isl_dim_in
, i
, isl_dim_out
, i
);
517 map
= isl_map_order_ge(map
,
518 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
520 map
= isl_map_order_le(map
,
521 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
522 map
= isl_map_intersect_range(map
, domain
);
523 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
528 /* Add a filter to "scop" that imposes that it is only executed
529 * when the variable identified by "id_test" has a zero value
530 * for all previous iterations of "domain".
532 * In particular, add a filter that imposes that the array
533 * has a zero value at the previous iteration of domain and
534 * add an implication that implies that it then has that
535 * value for all previous iterations.
537 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
538 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
539 __isl_take isl_val
*inc
)
541 isl_multi_pw_aff
*prev
;
542 int sign
= isl_val_sgn(inc
);
544 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
545 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
546 scop
= pet_scop_filter(scop
, prev
, 0);
551 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
552 __isl_keep pet_context
*pc
, struct pet_state
*state
);
554 /* Construct a pet_scop for an infinite loop around the given body
555 * within the context "pc".
556 * "loop_id" is the label on the loop or NULL if there is no such label.
558 * The domain of "pc" has already been extended with an infinite loop
562 * We extract a pet_scop for the body and then embed it in a loop with
565 * { [outer,t] -> [t] }
567 * If the body contains any break, then it is taken into
568 * account in apply_affine_break (if the skip condition is affine)
569 * or in scop_add_break (if the skip condition is not affine).
571 * Note that in case of an affine skip condition,
572 * since we are dealing with a loop without loop iterator,
573 * the skip condition cannot refer to the current loop iterator and
574 * so effectively, the effect on the iteration domain is of the form
576 * { [outer,0]; [outer,t] : t >= 1 and not skip }
578 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
579 __isl_keep isl_id
*loop_id
, __isl_keep pet_context
*pc
,
580 struct pet_state
*state
)
586 isl_multi_aff
*sched
;
587 struct pet_scop
*scop
;
588 int has_affine_break
;
591 ctx
= pet_tree_get_ctx(body
);
592 domain
= pet_context_get_domain(pc
);
593 sched
= map_to_last(pc
, state
->n_loop
++, loop_id
);
595 scop
= scop_from_tree(body
, pc
, state
);
597 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
598 if (has_affine_break
)
599 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
600 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
602 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
604 scop
= pet_scop_reset_skips(scop
);
605 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
);
606 if (has_affine_break
) {
607 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
608 scop
= pet_scop_intersect_domain_prefix(scop
,
609 isl_set_copy(domain
));
612 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
614 isl_set_free(domain
);
619 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
624 * within the context "pc".
626 * Extend the domain of "pc" with an extra inner loop
630 * and construct the scop in scop_from_infinite_loop.
632 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
633 __isl_keep pet_context
*pc
, struct pet_state
*state
)
635 struct pet_scop
*scop
;
637 pc
= pet_context_copy(pc
);
638 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
640 pc
= pet_context_add_infinite_loop(pc
);
642 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, tree
->label
, pc
, state
);
644 pet_context_free(pc
);
649 /* Construct a pet_scop for a while loop of the form
654 * within the context "pc".
656 * The domain of "pc" has already been extended with an infinite loop
660 * Here, we add the constraints on the outer loop iterators
661 * implied by "pa" and construct the scop in scop_from_infinite_loop.
662 * Note that the intersection with these constraints
663 * may result in an empty loop.
665 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
666 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
667 struct pet_state
*state
)
669 struct pet_scop
*scop
;
670 isl_set
*dom
, *local
;
673 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
674 dom
= isl_pw_aff_non_zero_set(pa
);
675 local
= isl_set_add_dims(isl_set_copy(dom
), isl_dim_set
, 1);
676 pc
= pet_context_intersect_domain(pc
, local
);
677 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, tree
->label
, pc
, state
);
678 scop
= pet_scop_restrict(scop
, dom
);
679 scop
= pet_scop_restrict_context(scop
, valid
);
681 pet_context_free(pc
);
685 /* Construct a scop for a while, given the scops for the condition
686 * and the body, the filter identifier and the iteration domain of
689 * In particular, the scop for the condition is filtered to depend
690 * on "id_test" evaluating to true for all previous iterations
691 * of the loop, while the scop for the body is filtered to depend
692 * on "id_test" evaluating to true for all iterations up to the
694 * The actual filter only imposes that this virtual array has
695 * value one on the previous or the current iteration.
696 * The fact that this condition also applies to the previous
697 * iterations is enforced by an implication.
699 * These filtered scops are then combined into a single scop,
700 * with the condition scop scheduled before the body scop.
702 * "sign" is positive if the iterator increases and negative
705 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
706 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
707 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
709 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
711 isl_multi_pw_aff
*test_index
;
712 isl_multi_pw_aff
*prev
;
713 int sign
= isl_val_sgn(inc
);
714 struct pet_scop
*scop
;
716 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
717 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
719 space
= isl_space_map_from_set(isl_set_get_space(domain
));
720 test_index
= isl_multi_pw_aff_identity(space
);
721 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
722 isl_id_copy(id_test
));
723 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
725 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
726 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
731 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
732 * evaluating "cond" and writing the result to a virtual scalar,
733 * as expressed by "index".
734 * The expression "cond" has not yet been evaluated in the context of "pc".
735 * Do so within the context "pc".
736 * The location of the statement is set to "loc".
738 static struct pet_scop
*scop_from_non_affine_condition(
739 __isl_take pet_expr
*cond
, int stmt_nr
,
740 __isl_take isl_multi_pw_aff
*index
,
741 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
743 pet_expr
*expr
, *write
;
745 cond
= pet_context_evaluate_expr(pc
, cond
);
747 write
= pet_expr_from_index(index
);
748 write
= pet_expr_access_set_write(write
, 1);
749 write
= pet_expr_access_set_read(write
, 0);
750 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
752 return scop_from_evaluated_expr(expr
, stmt_nr
, loc
, pc
);
755 /* Given that "scop" has an affine skip condition of type pet_skip_now,
756 * apply this skip condition to the domain of "pc".
757 * That is, remove the elements satisfying the skip condition from
758 * the domain of "pc".
760 static __isl_give pet_context
*apply_affine_continue(__isl_take pet_context
*pc
,
761 struct pet_scop
*scop
)
763 isl_set
*domain
, *skip
;
765 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_now
);
766 domain
= pet_context_get_domain(pc
);
767 domain
= isl_set_subtract(domain
, skip
);
768 pc
= pet_context_intersect_domain(pc
, domain
);
773 /* Add a scop for evaluating the loop increment "inc" add the end
774 * of a loop body "scop" within the context "pc".
776 * The skip conditions resulting from continue statements inside
777 * the body do not apply to "inc", but those resulting from break
778 * statements do need to get applied.
780 static struct pet_scop
*scop_add_inc(struct pet_scop
*scop
,
781 __isl_take pet_expr
*inc
, __isl_take pet_loc
*loc
,
782 __isl_keep pet_context
*pc
, struct pet_state
*state
)
784 struct pet_scop
*scop_inc
;
786 pc
= pet_context_copy(pc
);
788 if (pet_scop_has_skip(scop
, pet_skip_later
)) {
789 isl_multi_pw_aff
*skip
;
790 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
791 scop
= pet_scop_set_skip(scop
, pet_skip_now
, skip
);
792 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
793 pc
= apply_affine_continue(pc
, scop
);
795 pet_scop_reset_skip(scop
, pet_skip_now
);
796 scop_inc
= scop_from_expr(inc
, state
->n_stmt
++, loc
, pc
);
797 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_inc
);
799 pet_context_free(pc
);
804 /* Construct a generic while scop, with iteration domain
805 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
806 * "loop_id" is the label on the loop or NULL if there is no such label.
807 * The domain of "pc" has already been extended with this infinite loop
811 * The scop consists of two parts,
812 * one for evaluating the condition "cond" and one for the body.
813 * If "expr_inc" is not NULL, then a scop for evaluating this expression
814 * is added at the end of the body,
815 * after replacing any skip conditions resulting from continue statements
816 * by the skip conditions resulting from break statements (if any).
818 * The schedules are combined as a sequence to reflect that the condition is
819 * evaluated before the body is executed and the body is filtered to depend
820 * on the result of the condition evaluating to true on all iterations
821 * up to the current iteration, while the evaluation of the condition itself
822 * is filtered to depend on the result of the condition evaluating to true
823 * on all previous iterations.
824 * The context of the scop representing the body is dropped
825 * because we don't know how many times the body will be executed,
828 * If the body contains any break, then it is taken into
829 * account in apply_affine_break (if the skip condition is affine)
830 * or in scop_add_break (if the skip condition is not affine).
832 * Note that in case of an affine skip condition,
833 * since we are dealing with a loop without loop iterator,
834 * the skip condition cannot refer to the current loop iterator and
835 * so effectively, the effect on the iteration domain is of the form
837 * { [outer,0]; [outer,t] : t >= 1 and not skip }
839 static struct pet_scop
*scop_from_non_affine_while(__isl_take pet_expr
*cond
,
840 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
841 __isl_keep isl_id
*loop_id
, __isl_take pet_expr
*expr_inc
,
842 __isl_take pet_context
*pc
, struct pet_state
*state
)
845 isl_id
*id_test
, *id_break_test
;
847 isl_multi_pw_aff
*test_index
;
850 isl_multi_aff
*sched
;
851 struct pet_scop
*scop
, *scop_body
;
852 int has_affine_break
;
856 space
= pet_context_get_space(pc
);
857 test_index
= pet_create_test_index(space
, state
->n_test
++);
858 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
859 isl_multi_pw_aff_copy(test_index
),
860 pet_loc_copy(loc
), pc
);
861 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
862 domain
= pet_context_get_domain(pc
);
863 scop
= pet_scop_add_boolean_array(scop
, isl_set_copy(domain
),
864 test_index
, state
->int_size
);
866 sched
= map_to_last(pc
, state
->n_loop
++, loop_id
);
868 scop_body
= scop_from_tree(tree_body
, pc
, state
);
870 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
871 if (has_affine_break
)
872 skip
= pet_scop_get_affine_skip_domain(scop_body
,
874 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
876 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
878 scop_body
= pet_scop_reset_context(scop_body
);
880 scop_body
= scop_add_inc(scop_body
, expr_inc
, loc
, pc
, state
);
883 scop_body
= pet_scop_reset_skips(scop_body
);
885 if (has_affine_break
) {
886 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
887 scop
= pet_scop_intersect_domain_prefix(scop
,
888 isl_set_copy(domain
));
889 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
890 isl_set_copy(domain
));
893 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
894 isl_set_copy(domain
), isl_val_one(ctx
));
895 scop_body
= scop_add_break(scop_body
, id_break_test
,
896 isl_set_copy(domain
), isl_val_one(ctx
));
898 scop
= scop_add_while(scop
, scop_body
, id_test
, isl_set_copy(domain
),
901 scop
= pet_scop_embed(scop
, domain
, sched
);
903 pet_context_free(pc
);
907 /* Check if the while loop is of the form
909 * while (affine expression)
912 * If so, call scop_from_affine_while to construct a scop.
914 * Otherwise, pass control to scop_from_non_affine_while.
916 * "pc" is the context in which the affine expressions in the scop are created.
917 * The domain of "pc" is extended with an infinite loop
921 * before passing control to scop_from_affine_while or
922 * scop_from_non_affine_while.
924 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
925 __isl_keep pet_context
*pc
, struct pet_state
*state
)
933 pc
= pet_context_copy(pc
);
934 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
936 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
937 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
938 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
939 pet_expr_free(cond_expr
);
941 pc
= pet_context_add_infinite_loop(pc
);
946 if (!isl_pw_aff_involves_nan(pa
))
947 return scop_from_affine_while(tree
, pa
, pc
, state
);
949 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
950 pet_tree_get_loc(tree
), tree
->u
.l
.body
,
951 tree
->label
, NULL
, pc
, state
);
953 pet_context_free(pc
);
957 /* Check whether "cond" expresses a simple loop bound
958 * on the final set dimension.
959 * In particular, if "up" is set then "cond" should contain only
960 * upper bounds on the final set dimension.
961 * Otherwise, it should contain only lower bounds.
963 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
967 pos
= isl_set_dim(cond
, isl_dim_set
) - 1;
968 if (isl_val_is_pos(inc
))
969 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, pos
);
971 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, pos
);
974 /* Extend a condition on a given iteration of a loop to one that
975 * imposes the same condition on all previous iterations.
976 * "domain" expresses the lower [upper] bound on the iterations
977 * when inc is positive [negative] in its final dimension.
979 * In particular, we construct the condition (when inc is positive)
981 * forall i' : (domain(i') and i' <= i) => cond(i')
983 * (where "<=" applies to the final dimension)
984 * which is equivalent to
986 * not exists i' : domain(i') and i' <= i and not cond(i')
988 * We construct this set by subtracting the satisfying cond from domain,
991 * { [i'] -> [i] : i' <= i }
993 * and then subtracting the result from domain again.
995 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
996 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
999 isl_map
*previous_to_this
;
1002 dim
= isl_set_dim(cond
, isl_dim_set
);
1003 space
= isl_space_map_from_set(isl_set_get_space(cond
));
1004 previous_to_this
= isl_map_universe(space
);
1005 for (i
= 0; i
+ 1 < dim
; ++i
)
1006 previous_to_this
= isl_map_equate(previous_to_this
,
1007 isl_dim_in
, i
, isl_dim_out
, i
);
1008 if (isl_val_is_pos(inc
))
1009 previous_to_this
= isl_map_order_le(previous_to_this
,
1010 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
1012 previous_to_this
= isl_map_order_ge(previous_to_this
,
1013 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
1015 cond
= isl_set_subtract(isl_set_copy(domain
), cond
);
1016 cond
= isl_set_apply(cond
, previous_to_this
);
1017 cond
= isl_set_subtract(domain
, cond
);
1024 /* Given an initial value of the form
1026 * { [outer,i] -> init(outer) }
1028 * construct a domain of the form
1030 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1032 static __isl_give isl_set
*strided_domain(__isl_take isl_pw_aff
*init
,
1033 __isl_take isl_val
*inc
)
1038 isl_local_space
*ls
;
1041 dim
= isl_pw_aff_dim(init
, isl_dim_in
);
1043 init
= isl_pw_aff_add_dims(init
, isl_dim_in
, 1);
1044 space
= isl_pw_aff_get_domain_space(init
);
1045 ls
= isl_local_space_from_space(space
);
1046 aff
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
1047 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, dim
, inc
);
1048 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
1050 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, dim
- 1);
1051 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
1053 set
= isl_set_lower_bound_si(set
, isl_dim_set
, dim
, 0);
1054 set
= isl_set_project_out(set
, isl_dim_set
, dim
, 1);
1059 /* Assuming "cond" represents a bound on a loop where the loop
1060 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1063 * Under the given assumptions, wrapping is only possible if "cond" allows
1064 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1065 * increasing iterator and 0 in case of a decreasing iterator.
1067 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
1068 __isl_keep isl_val
*inc
)
1075 test
= isl_set_copy(cond
);
1077 ctx
= isl_set_get_ctx(test
);
1078 if (isl_val_is_neg(inc
))
1079 limit
= isl_val_zero(ctx
);
1081 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
1082 limit
= isl_val_2exp(limit
);
1083 limit
= isl_val_sub_ui(limit
, 1);
1086 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
1087 cw
= !isl_set_is_empty(test
);
1097 * construct the following affine expression on this space
1099 * { [outer, v] -> [outer, v mod 2^width] }
1101 * where width is the number of bits used to represent the values
1102 * of the unsigned variable "iv".
1104 static __isl_give isl_multi_aff
*compute_wrapping(__isl_take isl_space
*space
,
1105 __isl_keep pet_expr
*iv
)
1113 dim
= isl_space_dim(space
, isl_dim_set
);
1115 ctx
= isl_space_get_ctx(space
);
1116 mod
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
1117 mod
= isl_val_2exp(mod
);
1119 space
= isl_space_map_from_set(space
);
1120 ma
= isl_multi_aff_identity(space
);
1122 aff
= isl_multi_aff_get_aff(ma
, dim
- 1);
1123 aff
= isl_aff_mod_val(aff
, mod
);
1124 ma
= isl_multi_aff_set_aff(ma
, dim
- 1, aff
);
1129 /* Given two sets in the space
1133 * where l represents the outer loop iterators, compute the set
1134 * of values of l that ensure that "set1" is a subset of "set2".
1136 * set1 is a subset of set2 if
1138 * forall i: set1(l,i) => set2(l,i)
1142 * not exists i: set1(l,i) and not set2(l,i)
1146 * not exists i: (set1 \ set2)(l,i)
1148 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
1149 __isl_take isl_set
*set2
)
1153 pos
= isl_set_dim(set1
, isl_dim_set
) - 1;
1154 set1
= isl_set_subtract(set1
, set2
);
1155 set1
= isl_set_eliminate(set1
, isl_dim_set
, pos
, 1);
1156 return isl_set_complement(set1
);
1159 /* Compute the set of outer iterator values for which "cond" holds
1160 * on the next iteration of the inner loop for each element of "dom".
1162 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1163 * to the outer loop iterators), plug that into "cond"
1164 * and then compute the set of outer iterators for which "dom" is a subset
1167 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
1168 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
1175 pos
= isl_set_dim(dom
, isl_dim_set
) - 1;
1176 space
= isl_set_get_space(dom
);
1177 space
= isl_space_map_from_set(space
);
1178 ma
= isl_multi_aff_identity(space
);
1179 aff
= isl_multi_aff_get_aff(ma
, pos
);
1180 aff
= isl_aff_add_constant_val(aff
, inc
);
1181 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
1182 cond
= isl_set_preimage_multi_aff(cond
, ma
);
1184 return enforce_subset(dom
, cond
);
1187 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1188 * In particular, "pc_init" represents the context of the loop,
1189 * whereas "pc" represents the context of the body of the loop and
1190 * has already had its domain extended with an infinite loop
1194 * The for loop has the form
1196 * for (iv = init; cond; iv += inc)
1207 * except that the skips resulting from any continue statements
1208 * in body do not apply to the increment, but are replaced by the skips
1209 * resulting from break statements.
1211 * If the loop iterator is declared in the for loop, then it is killed before
1212 * and after the loop.
1214 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
1215 __isl_keep pet_context
*init_pc
, __isl_take pet_context
*pc
,
1216 struct pet_state
*state
)
1220 pet_expr
*expr_iv
, *init
, *inc
;
1221 struct pet_scop
*scop_init
, *scop
;
1223 struct pet_array
*array
;
1224 struct pet_scop
*scop_kill
;
1226 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1227 pc
= pet_context_clear_value(pc
, iv
);
1229 declared
= tree
->u
.l
.declared
;
1231 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
1232 type_size
= pet_expr_get_type_size(expr_iv
);
1233 init
= pet_expr_copy(tree
->u
.l
.init
);
1234 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
1235 scop_init
= scop_from_expr(init
, state
->n_stmt
++,
1236 pet_tree_get_loc(tree
), init_pc
);
1238 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
1239 type_size
= pet_expr_get_type_size(expr_iv
);
1240 inc
= pet_expr_copy(tree
->u
.l
.inc
);
1241 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
1243 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
1244 pet_tree_get_loc(tree
), tree
->u
.l
.body
, tree
->label
,
1245 inc
, pet_context_copy(pc
), state
);
1247 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
1249 pet_context_free(pc
);
1254 array
= extract_array(tree
->u
.l
.iv
, init_pc
, state
);
1256 array
->declared
= 1;
1257 scop_kill
= kill(pet_tree_get_loc(tree
), array
, init_pc
, state
);
1258 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
1259 scop_kill
= kill(pet_tree_get_loc(tree
), array
, init_pc
, state
);
1260 scop_kill
= pet_scop_add_array(scop_kill
, array
);
1261 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
1266 /* Given an access expression "expr", is the variable accessed by
1267 * "expr" assigned anywhere inside "tree"?
1269 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
1274 id
= pet_expr_access_get_id(expr
);
1275 assigned
= pet_tree_writes(tree
, id
);
1281 /* Are all nested access parameters in "pa" allowed given "tree".
1282 * In particular, is none of them written by anywhere inside "tree".
1284 * If "tree" has any continue or break nodes in the current loop level,
1285 * then no nested access parameters are allowed.
1286 * In particular, if there is any nested access in a guard
1287 * for a piece of code containing a "continue", then we want to introduce
1288 * a separate statement for evaluating this guard so that we can express
1289 * that the result is false for all previous iterations.
1291 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
1292 __isl_keep pet_tree
*tree
)
1299 if (!pet_nested_any_in_pw_aff(pa
))
1302 if (pet_tree_has_continue_or_break(tree
))
1305 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1306 for (i
= 0; i
< nparam
; ++i
) {
1307 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1311 if (!pet_nested_in_id(id
)) {
1316 expr
= pet_nested_extract_expr(id
);
1317 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1318 !is_assigned(expr
, tree
);
1320 pet_expr_free(expr
);
1330 /* Internal data structure for collect_local.
1331 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1332 * "local" collects the results.
1334 struct pet_tree_collect_local_data
{
1336 struct pet_state
*state
;
1337 isl_union_set
*local
;
1340 /* Add the variable accessed by "var" to data->local.
1341 * We extract a representation of the variable from
1342 * the pet_array constructed using extract_array
1343 * to ensure consistency with the rest of the scop.
1345 static int add_local(struct pet_tree_collect_local_data
*data
,
1346 __isl_keep pet_expr
*var
)
1348 struct pet_array
*array
;
1351 array
= extract_array(var
, data
->pc
, data
->state
);
1355 universe
= isl_set_universe(isl_set_get_space(array
->extent
));
1356 data
->local
= isl_union_set_add_set(data
->local
, universe
);
1357 pet_array_free(array
);
1362 /* If the node "tree" declares a variable, then add it to
1365 static int extract_local_var(__isl_keep pet_tree
*tree
, void *user
)
1367 enum pet_tree_type type
;
1368 struct pet_tree_collect_local_data
*data
= user
;
1370 type
= pet_tree_get_type(tree
);
1371 if (type
== pet_tree_decl
|| type
== pet_tree_decl_init
)
1372 return add_local(data
, tree
->u
.d
.var
);
1377 /* If the node "tree" is a for loop that declares its induction variable,
1378 * then add it this induction variable to data->local.
1380 static int extract_local_iterator(__isl_keep pet_tree
*tree
, void *user
)
1382 struct pet_tree_collect_local_data
*data
= user
;
1384 if (pet_tree_get_type(tree
) == pet_tree_for
&& tree
->u
.l
.declared
)
1385 return add_local(data
, tree
->u
.l
.iv
);
1390 /* Collect and return all local variables of the for loop represented
1391 * by "tree", with "scop" the corresponding pet_scop.
1392 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1394 * We collect not only the variables that are declared inside "tree",
1395 * but also the loop iterators that are declared anywhere inside
1396 * any possible macro statements in "scop".
1397 * The latter also appear as declared variable in the scop,
1398 * whereas other declared loop iterators only appear implicitly
1399 * in the iteration domains.
1401 static __isl_give isl_union_set
*collect_local(struct pet_scop
*scop
,
1402 __isl_keep pet_tree
*tree
, __isl_keep pet_context
*pc
,
1403 struct pet_state
*state
)
1407 struct pet_tree_collect_local_data data
= { pc
, state
};
1409 ctx
= pet_tree_get_ctx(tree
);
1410 data
.local
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
1412 if (pet_tree_foreach_sub_tree(tree
, &extract_local_var
, &data
) < 0)
1413 return isl_union_set_free(data
.local
);
1415 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1416 pet_tree
*body
= scop
->stmts
[i
]->body
;
1417 if (pet_tree_foreach_sub_tree(body
, &extract_local_iterator
,
1419 return isl_union_set_free(data
.local
);
1425 /* Add an independence to "scop" if the for node "tree" was marked
1427 * "domain" is the set of loop iterators, with the current for loop
1428 * innermost. If "sign" is positive, then the inner iterator increases.
1429 * Otherwise it decreases.
1430 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1432 * If the tree was marked, then collect all local variables and
1433 * add an independence.
1435 static struct pet_scop
*set_independence(struct pet_scop
*scop
,
1436 __isl_keep pet_tree
*tree
, __isl_keep isl_set
*domain
, int sign
,
1437 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1439 isl_union_set
*local
;
1441 if (!tree
->u
.l
.independent
)
1444 local
= collect_local(scop
, tree
, pc
, state
);
1445 scop
= pet_scop_set_independent(scop
, domain
, local
, sign
);
1450 /* Construct a pet_scop for a for tree with static affine initialization
1451 * and constant increment within the context "pc".
1452 * The domain of "pc" has already been extended with an (at this point
1453 * unbounded) inner loop iterator corresponding to the current for loop.
1455 * The condition is allowed to contain nested accesses, provided
1456 * they are not being written to inside the body of the loop.
1457 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1458 * essentially treated as a while loop, with iteration domain
1459 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1461 * We extract a pet_scop for the body after intersecting the domain of "pc"
1463 * { [l,i] : i >= init and condition' }
1467 * { [l,i] : i <= init and condition' }
1469 * Where condition' is equal to condition if the latter is
1470 * a simple upper [lower] bound and a condition that is extended
1471 * to apply to all previous iterations otherwise.
1472 * Afterwards, the schedule of the pet_scop is extended with
1480 * If the condition is non-affine, then we drop the condition from the
1481 * iteration domain and instead create a separate statement
1482 * for evaluating the condition. The body is then filtered to depend
1483 * on the result of the condition evaluating to true on all iterations
1484 * up to the current iteration, while the evaluation the condition itself
1485 * is filtered to depend on the result of the condition evaluating to true
1486 * on all previous iterations.
1487 * The context of the scop representing the body is dropped
1488 * because we don't know how many times the body will be executed,
1491 * If the stride of the loop is not 1, then "i >= init" is replaced by
1493 * (exists a: i = init + stride * a and a >= 0)
1495 * If the loop iterator i is unsigned, then wrapping may occur.
1496 * We therefore use a virtual iterator instead that does not wrap.
1497 * However, the condition in the code applies
1498 * to the wrapped value, so we need to change condition(l,i)
1499 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1500 * to the original iterator by the wrapping of the virtual iterator.
1501 * Note that there may be no need to perform this final wrapping
1502 * if the loop condition (after wrapping) satisfies certain conditions.
1503 * However, the is_simple_bound condition is not enough since it doesn't
1504 * check if there even is an upper bound.
1506 * Wrapping on unsigned iterators can be avoided entirely if
1507 * loop condition is simple, the loop iterator is incremented
1508 * [decremented] by one and the last value before wrapping cannot
1509 * possibly satisfy the loop condition.
1511 * Valid outer iterators for a for loop are those for which the initial
1512 * value itself, the increment on each domain iteration and
1513 * the condition on both the initial value and
1514 * the result of incrementing the iterator for each iteration of the domain
1516 * If the loop condition is non-affine, then we only consider validity
1517 * of the initial value.
1519 * If the body contains any break, then we keep track of it in "skip"
1520 * (if the skip condition is affine) or it is handled in scop_add_break
1521 * (if the skip condition is not affine).
1522 * Note that the affine break condition needs to be considered with
1523 * respect to previous iterations in the virtual domain (if any).
1525 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1526 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1527 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1528 struct pet_state
*state
)
1531 isl_multi_aff
*sched
;
1532 isl_set
*cond
= NULL
;
1533 isl_set
*skip
= NULL
;
1534 isl_id
*id_test
= NULL
, *id_break_test
;
1535 struct pet_scop
*scop
, *scop_cond
= NULL
;
1542 int has_affine_break
;
1544 isl_map
*rev_wrap
= NULL
;
1545 isl_map
*init_val_map
;
1547 isl_set
*valid_init
;
1548 isl_set
*valid_cond
;
1549 isl_set
*valid_cond_init
;
1550 isl_set
*valid_cond_next
;
1552 pet_expr
*cond_expr
;
1553 pet_context
*pc_nested
;
1555 pos
= pet_context_dim(pc
) - 1;
1557 domain
= pet_context_get_domain(pc
);
1558 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1559 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
1560 pc_nested
= pet_context_copy(pc
);
1561 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1562 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1563 pet_context_free(pc_nested
);
1564 pet_expr_free(cond_expr
);
1566 valid_inc
= isl_pw_aff_domain(pa_inc
);
1568 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1570 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1571 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1573 pa
= isl_pw_aff_free(pa
);
1575 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1576 cond
= isl_pw_aff_non_zero_set(pa
);
1578 cond
= isl_set_universe(isl_set_get_space(domain
));
1580 valid_cond
= isl_set_coalesce(valid_cond
);
1581 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1582 is_virtual
= is_unsigned
&&
1583 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1585 init_val_map
= isl_map_from_pw_aff(isl_pw_aff_copy(init_val
));
1586 init_val_map
= isl_map_equate(init_val_map
, isl_dim_in
, pos
,
1588 valid_cond_init
= enforce_subset(isl_map_domain(init_val_map
),
1589 isl_set_copy(valid_cond
));
1590 if (is_one
&& !is_virtual
) {
1593 isl_pw_aff_free(init_val
);
1594 pa
= pet_expr_extract_comparison(
1595 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1596 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1597 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1598 valid_init
= isl_set_eliminate(valid_init
, isl_dim_set
,
1599 isl_set_dim(domain
, isl_dim_set
) - 1, 1);
1600 cond
= isl_pw_aff_non_zero_set(pa
);
1601 domain
= isl_set_intersect(domain
, cond
);
1605 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1606 strided
= strided_domain(init_val
, isl_val_copy(inc
));
1607 domain
= isl_set_intersect(domain
, strided
);
1611 isl_multi_aff
*wrap
;
1612 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1613 pc
= pet_context_preimage_domain(pc
, wrap
);
1614 rev_wrap
= isl_map_from_multi_aff(wrap
);
1615 rev_wrap
= isl_map_reverse(rev_wrap
);
1616 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1617 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1618 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1620 is_simple
= is_simple_bound(cond
, inc
);
1622 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1623 is_simple
= is_simple_bound(cond
, inc
);
1626 cond
= valid_for_each_iteration(cond
,
1627 isl_set_copy(domain
), isl_val_copy(inc
));
1628 cond
= isl_set_align_params(cond
, isl_set_get_space(domain
));
1629 domain
= isl_set_intersect(domain
, cond
);
1630 sched
= map_to_last(pc
, state
->n_loop
++, tree
->label
);
1631 if (isl_val_is_neg(inc
))
1632 sched
= isl_multi_aff_neg(sched
);
1634 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1636 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1638 pc
= pet_context_intersect_domain(pc
, isl_set_copy(domain
));
1640 if (is_non_affine
) {
1642 isl_multi_pw_aff
*test_index
;
1643 space
= isl_set_get_space(domain
);
1644 test_index
= pet_create_test_index(space
, state
->n_test
++);
1645 scop_cond
= scop_from_non_affine_condition(
1646 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1647 isl_multi_pw_aff_copy(test_index
),
1648 pet_tree_get_loc(tree
), pc
);
1649 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1651 scop_cond
= pet_scop_add_boolean_array(scop_cond
,
1652 isl_set_copy(domain
), test_index
,
1656 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
1657 has_affine_break
= scop
&&
1658 pet_scop_has_affine_skip(scop
, pet_skip_later
);
1659 if (has_affine_break
)
1660 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1661 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
1663 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1664 if (is_non_affine
) {
1665 scop
= pet_scop_reset_context(scop
);
1667 scop
= pet_scop_reset_skips(scop
);
1668 scop
= pet_scop_resolve_nested(scop
);
1669 if (has_affine_break
) {
1670 domain
= apply_affine_break(domain
, skip
, isl_val_sgn(inc
),
1671 is_virtual
, rev_wrap
);
1672 scop
= pet_scop_intersect_domain_prefix(scop
,
1673 isl_set_copy(domain
));
1675 isl_map_free(rev_wrap
);
1677 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
1680 scop
= scop_add_while(scop_cond
, scop
, id_test
,
1681 isl_set_copy(domain
),
1684 scop
= set_independence(scop
, tree
, domain
, isl_val_sgn(inc
),
1686 scop
= pet_scop_embed(scop
, domain
, sched
);
1687 if (is_non_affine
) {
1688 isl_set_free(valid_inc
);
1690 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_next
);
1691 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_init
);
1692 valid_inc
= isl_set_project_out(valid_inc
, isl_dim_set
, pos
, 1);
1693 scop
= pet_scop_restrict_context(scop
, valid_inc
);
1698 valid_init
= isl_set_project_out(valid_init
, isl_dim_set
, pos
, 1);
1699 scop
= pet_scop_restrict_context(scop
, valid_init
);
1701 pet_context_free(pc
);
1705 /* Construct a pet_scop for a for statement within the context of "pc".
1707 * We update the context to reflect the writes to the loop variable and
1708 * the writes inside the body.
1710 * Then we check if the initialization of the for loop
1711 * is a static affine value and the increment is a constant.
1712 * If so, we construct the pet_scop using scop_from_affine_for.
1713 * Otherwise, we treat the for loop as a while loop
1714 * in scop_from_non_affine_for.
1716 * Note that the initialization and the increment are extracted
1717 * in a context where the current loop iterator has been added
1718 * to the context. If these turn out not be affine, then we
1719 * have reconstruct the body context without an assignment
1720 * to this loop iterator, as this variable will then not be
1721 * treated as a dimension of the iteration domain, but as any
1724 static struct pet_scop
*scop_from_for(__isl_keep pet_tree
*tree
,
1725 __isl_keep pet_context
*init_pc
, struct pet_state
*state
)
1729 isl_pw_aff
*pa_inc
, *init_val
;
1730 pet_context
*pc
, *pc_init_val
;
1735 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1736 pc
= pet_context_copy(init_pc
);
1737 pc
= pet_context_add_inner_iterator(pc
, iv
);
1738 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1740 pc_init_val
= pet_context_copy(pc
);
1741 pc_init_val
= pet_context_clear_value(pc_init_val
, isl_id_copy(iv
));
1742 init_val
= pet_expr_extract_affine(tree
->u
.l
.init
, pc_init_val
);
1743 pet_context_free(pc_init_val
);
1744 pa_inc
= pet_expr_extract_affine(tree
->u
.l
.inc
, pc
);
1745 inc
= pet_extract_cst(pa_inc
);
1746 if (!pa_inc
|| !init_val
|| !inc
)
1748 if (!isl_pw_aff_involves_nan(pa_inc
) &&
1749 !isl_pw_aff_involves_nan(init_val
) && !isl_val_is_nan(inc
))
1750 return scop_from_affine_for(tree
, init_val
, pa_inc
, inc
,
1753 isl_pw_aff_free(pa_inc
);
1754 isl_pw_aff_free(init_val
);
1756 pet_context_free(pc
);
1758 pc
= pet_context_copy(init_pc
);
1759 pc
= pet_context_add_infinite_loop(pc
);
1760 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1761 return scop_from_non_affine_for(tree
, init_pc
, pc
, state
);
1763 isl_pw_aff_free(pa_inc
);
1764 isl_pw_aff_free(init_val
);
1766 pet_context_free(pc
);
1770 /* Check whether "expr" is an affine constraint within the context "pc".
1772 static int is_affine_condition(__isl_keep pet_expr
*expr
,
1773 __isl_keep pet_context
*pc
)
1778 pa
= pet_expr_extract_affine_condition(expr
, pc
);
1781 is_affine
= !isl_pw_aff_involves_nan(pa
);
1782 isl_pw_aff_free(pa
);
1787 /* Check if the given if statement is a conditional assignement
1788 * with a non-affine condition.
1790 * In particular we check if "stmt" is of the form
1797 * where the condition is non-affine and a is some array or scalar access.
1799 static int is_conditional_assignment(__isl_keep pet_tree
*tree
,
1800 __isl_keep pet_context
*pc
)
1804 pet_expr
*expr1
, *expr2
;
1806 ctx
= pet_tree_get_ctx(tree
);
1807 if (!pet_options_get_detect_conditional_assignment(ctx
))
1809 if (tree
->type
!= pet_tree_if_else
)
1811 if (tree
->u
.i
.then_body
->type
!= pet_tree_expr
)
1813 if (tree
->u
.i
.else_body
->type
!= pet_tree_expr
)
1815 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1816 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1817 if (pet_expr_get_type(expr1
) != pet_expr_op
)
1819 if (pet_expr_get_type(expr2
) != pet_expr_op
)
1821 if (pet_expr_op_get_type(expr1
) != pet_op_assign
)
1823 if (pet_expr_op_get_type(expr2
) != pet_op_assign
)
1825 expr1
= pet_expr_get_arg(expr1
, 0);
1826 expr2
= pet_expr_get_arg(expr2
, 0);
1827 equal
= pet_expr_is_equal(expr1
, expr2
);
1828 pet_expr_free(expr1
);
1829 pet_expr_free(expr2
);
1830 if (equal
< 0 || !equal
)
1832 if (is_affine_condition(tree
->u
.i
.cond
, pc
))
1838 /* Given that "tree" is of the form
1845 * where a is some array or scalar access, construct a pet_scop
1846 * corresponding to this conditional assignment within the context "pc".
1847 * "cond_pa" is an affine expression with nested accesses representing
1850 * The constructed pet_scop then corresponds to the expression
1852 * a = condition ? f(...) : g(...)
1854 * All access relations in f(...) are intersected with condition
1855 * while all access relation in g(...) are intersected with the complement.
1857 static struct pet_scop
*scop_from_conditional_assignment(
1858 __isl_keep pet_tree
*tree
, __isl_take isl_pw_aff
*cond_pa
,
1859 __isl_take pet_context
*pc
, struct pet_state
*state
)
1862 isl_set
*cond
, *comp
;
1863 isl_multi_pw_aff
*index
;
1864 pet_expr
*expr1
, *expr2
;
1865 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
1866 struct pet_scop
*scop
;
1868 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa
));
1869 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa
));
1870 index
= isl_multi_pw_aff_from_pw_aff(cond_pa
);
1872 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1873 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1875 pe_cond
= pet_expr_from_index(index
);
1877 pe_then
= pet_expr_get_arg(expr1
, 1);
1878 pe_then
= pet_context_evaluate_expr(pc
, pe_then
);
1879 pe_then
= pet_expr_restrict(pe_then
, cond
);
1880 pe_else
= pet_expr_get_arg(expr2
, 1);
1881 pe_else
= pet_context_evaluate_expr(pc
, pe_else
);
1882 pe_else
= pet_expr_restrict(pe_else
, comp
);
1883 pe_write
= pet_expr_get_arg(expr1
, 0);
1884 pe_write
= pet_context_evaluate_expr(pc
, pe_write
);
1886 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
1887 type_size
= pet_expr_get_type_size(pe_write
);
1888 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, pe_write
, pe
);
1890 scop
= scop_from_evaluated_expr(pe
, state
->n_stmt
++,
1891 pet_tree_get_loc(tree
), pc
);
1893 pet_context_free(pc
);
1898 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1900 * We create a separate statement that writes the result
1901 * of the non-affine condition to a virtual scalar.
1902 * A constraint requiring the value of this virtual scalar to be one
1903 * is added to the iteration domains of the then branch.
1904 * Similarly, a constraint requiring the value of this virtual scalar
1905 * to be zero is added to the iteration domains of the else branch, if any.
1906 * We combine the schedules as a sequence to ensure that the virtual scalar
1907 * is written before it is read.
1909 * If there are any breaks or continues in the then and/or else
1910 * branches, then we may have to compute a new skip condition.
1911 * This is handled using a pet_skip_info object.
1912 * On initialization, the object checks if skip conditions need
1913 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1914 * adds them in pet_skip_info_add.
1916 static struct pet_scop
*scop_from_non_affine_if(__isl_keep pet_tree
*tree
,
1917 __isl_take pet_context
*pc
, struct pet_state
*state
)
1922 isl_multi_pw_aff
*test_index
;
1923 struct pet_skip_info skip
;
1924 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1926 has_else
= tree
->type
== pet_tree_if_else
;
1928 space
= pet_context_get_space(pc
);
1929 test_index
= pet_create_test_index(space
, state
->n_test
++);
1930 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
1931 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
1932 pet_tree_get_loc(tree
), pc
);
1933 domain
= pet_context_get_domain(pc
);
1934 scop
= pet_scop_add_boolean_array(scop
, domain
,
1935 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1937 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1939 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1941 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1943 pet_skip_info_if_extract_index(&skip
, test_index
, pc
, state
);
1945 scop_then
= pet_scop_filter(scop_then
,
1946 isl_multi_pw_aff_copy(test_index
), 1);
1948 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1949 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1951 isl_multi_pw_aff_free(test_index
);
1953 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1955 scop
= pet_skip_info_add(&skip
, scop
);
1957 pet_context_free(pc
);
1961 /* Construct a pet_scop for an affine if statement within the context "pc".
1963 * The condition is added to the iteration domains of the then branch,
1964 * while the opposite of the condition in added to the iteration domains
1965 * of the else branch, if any.
1967 * If there are any breaks or continues in the then and/or else
1968 * branches, then we may have to compute a new skip condition.
1969 * This is handled using a pet_skip_info_if object.
1970 * On initialization, the object checks if skip conditions need
1971 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1972 * adds them in pet_skip_info_add.
1974 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1975 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
1976 struct pet_state
*state
)
1980 isl_set
*set
, *complement
;
1982 struct pet_skip_info skip
;
1983 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1984 pet_context
*pc_body
;
1986 ctx
= pet_tree_get_ctx(tree
);
1988 has_else
= tree
->type
== pet_tree_if_else
;
1990 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1991 set
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
1993 pc_body
= pet_context_copy(pc
);
1994 pc_body
= pet_context_intersect_domain(pc_body
, isl_set_copy(set
));
1995 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc_body
, state
);
1996 pet_context_free(pc_body
);
1998 pc_body
= pet_context_copy(pc
);
1999 complement
= isl_set_copy(valid
);
2000 complement
= isl_set_subtract(valid
, isl_set_copy(set
));
2001 pc_body
= pet_context_intersect_domain(pc_body
,
2002 isl_set_copy(complement
));
2003 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc_body
, state
);
2004 pet_context_free(pc_body
);
2007 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
2008 pet_skip_info_if_extract_cond(&skip
, cond
, pc
, state
);
2009 isl_pw_aff_free(cond
);
2011 scop
= pet_scop_restrict(scop_then
, set
);
2014 scop_else
= pet_scop_restrict(scop_else
, complement
);
2015 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
2017 scop
= pet_scop_resolve_nested(scop
);
2018 scop
= pet_scop_restrict_context(scop
, valid
);
2020 scop
= pet_skip_info_add(&skip
, scop
);
2022 pet_context_free(pc
);
2026 /* Construct a pet_scop for an if statement within the context "pc".
2028 * If the condition fits the pattern of a conditional assignment,
2029 * then it is handled by scop_from_conditional_assignment.
2030 * Note that the condition is only considered for a conditional assignment
2031 * if it is not static-affine. However, it should still convert
2032 * to an affine expression when nesting is allowed.
2034 * Otherwise, we check if the condition is affine.
2035 * If so, we construct the scop in scop_from_affine_if.
2036 * Otherwise, we construct the scop in scop_from_non_affine_if.
2038 * We allow the condition to be dynamic, i.e., to refer to
2039 * scalars or array elements that may be written to outside
2040 * of the given if statement. These nested accesses are then represented
2041 * as output dimensions in the wrapping iteration domain.
2042 * If it is also written _inside_ the then or else branch, then
2043 * we treat the condition as non-affine.
2044 * As explained in extract_non_affine_if, this will introduce
2045 * an extra statement.
2046 * For aesthetic reasons, we want this statement to have a statement
2047 * number that is lower than those of the then and else branches.
2048 * In order to evaluate if we will need such a statement, however, we
2049 * first construct scops for the then and else branches.
2050 * We therefore reserve a statement number if we might have to
2051 * introduce such an extra statement.
2053 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
2054 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2058 pet_expr
*cond_expr
;
2059 pet_context
*pc_nested
;
2064 has_else
= tree
->type
== pet_tree_if_else
;
2066 pc
= pet_context_copy(pc
);
2067 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
2069 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
2071 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
2072 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
2073 pc_nested
= pet_context_copy(pc
);
2074 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
2075 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
2076 pet_context_free(pc_nested
);
2077 pet_expr_free(cond_expr
);
2080 pet_context_free(pc
);
2084 if (isl_pw_aff_involves_nan(cond
)) {
2085 isl_pw_aff_free(cond
);
2086 return scop_from_non_affine_if(tree
, pc
, state
);
2089 if (is_conditional_assignment(tree
, pc
))
2090 return scop_from_conditional_assignment(tree
, cond
, pc
, state
);
2092 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
2093 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
2094 isl_pw_aff_free(cond
);
2095 return scop_from_non_affine_if(tree
, pc
, state
);
2098 return scop_from_affine_if(tree
, cond
, pc
, state
);
2101 /* Return a one-dimensional multi piecewise affine expression that is equal
2102 * to the constant 1 and is defined over the given domain.
2104 static __isl_give isl_multi_pw_aff
*one_mpa(__isl_take isl_space
*space
)
2106 isl_local_space
*ls
;
2109 ls
= isl_local_space_from_space(space
);
2110 aff
= isl_aff_zero_on_domain(ls
);
2111 aff
= isl_aff_set_constant_si(aff
, 1);
2113 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
2116 /* Construct a pet_scop for a continue statement with the given domain space.
2118 * We simply create an empty scop with a universal pet_skip_now
2119 * skip condition. This skip condition will then be taken into
2120 * account by the enclosing loop construct, possibly after
2121 * being incorporated into outer skip conditions.
2123 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
,
2124 __isl_take isl_space
*space
)
2126 struct pet_scop
*scop
;
2128 scop
= pet_scop_empty(isl_space_copy(space
));
2130 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(space
));
2135 /* Construct a pet_scop for a break statement with the given domain space.
2137 * We simply create an empty scop with both a universal pet_skip_now
2138 * skip condition and a universal pet_skip_later skip condition.
2139 * These skip conditions will then be taken into
2140 * account by the enclosing loop construct, possibly after
2141 * being incorporated into outer skip conditions.
2143 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
,
2144 __isl_take isl_space
*space
)
2146 struct pet_scop
*scop
;
2147 isl_multi_pw_aff
*skip
;
2149 scop
= pet_scop_empty(isl_space_copy(space
));
2151 skip
= one_mpa(space
);
2152 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
2153 isl_multi_pw_aff_copy(skip
));
2154 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
2159 /* Extract a clone of the kill statement "stmt".
2160 * The domain of the clone is given by "domain".
2162 static struct pet_scop
*extract_kill(__isl_keep isl_set
*domain
,
2163 struct pet_stmt
*stmt
, struct pet_state
*state
)
2167 isl_multi_pw_aff
*mpa
;
2170 if (!domain
|| !stmt
)
2173 kill
= pet_tree_expr_get_expr(stmt
->body
);
2174 space
= pet_stmt_get_space(stmt
);
2175 space
= isl_space_map_from_set(space
);
2176 mpa
= isl_multi_pw_aff_identity(space
);
2177 mpa
= isl_multi_pw_aff_reset_tuple_id(mpa
, isl_dim_in
);
2178 kill
= pet_expr_update_domain(kill
, mpa
);
2179 tree
= pet_tree_new_expr(kill
);
2180 tree
= pet_tree_set_loc(tree
, pet_loc_copy(stmt
->loc
));
2181 stmt
= pet_stmt_from_pet_tree(isl_set_copy(domain
),
2182 state
->n_stmt
++, tree
);
2183 return pet_scop_from_pet_stmt(isl_set_get_space(domain
), stmt
);
2186 /* Extract a clone of the kill statements in "scop".
2187 * The domain of each clone is given by "domain".
2188 * "scop" is expected to have been created from a DeclStmt
2189 * and should have (one of) the kill(s) as its first statement.
2190 * If "scop" was created from a declaration group, then there
2191 * may be multiple kill statements inside.
2193 static struct pet_scop
*extract_kills(__isl_keep isl_set
*domain
,
2194 struct pet_scop
*scop
, struct pet_state
*state
)
2197 struct pet_stmt
*stmt
;
2198 struct pet_scop
*kill
;
2201 if (!domain
|| !scop
)
2203 ctx
= isl_set_get_ctx(domain
);
2204 if (scop
->n_stmt
< 1)
2205 isl_die(ctx
, isl_error_internal
,
2206 "expecting at least one statement", return NULL
);
2207 stmt
= scop
->stmts
[0];
2208 if (!pet_stmt_is_kill(stmt
))
2209 isl_die(ctx
, isl_error_internal
,
2210 "expecting kill statement", return NULL
);
2212 kill
= extract_kill(domain
, stmt
, state
);
2214 for (i
= 1; i
< scop
->n_stmt
; ++i
) {
2215 struct pet_scop
*kill_i
;
2217 stmt
= scop
->stmts
[i
];
2218 if (!pet_stmt_is_kill(stmt
))
2221 kill_i
= extract_kill(domain
, stmt
, state
);
2222 kill
= pet_scop_add_par(ctx
, kill
, kill_i
);
2228 /* Has "tree" been created from a DeclStmt?
2229 * That is, is it either a declaration or a group of declarations?
2231 static int tree_is_decl(__isl_keep pet_tree
*tree
)
2238 is_decl
= pet_tree_is_decl(tree
);
2239 if (is_decl
< 0 || is_decl
)
2242 if (tree
->type
!= pet_tree_block
)
2244 if (pet_tree_block_get_block(tree
))
2247 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
2248 is_decl
= tree_is_decl(tree
->u
.b
.child
[i
]);
2249 if (is_decl
< 0 || !is_decl
)
2256 /* Does "tree" represent an assignment to a variable?
2258 * The assignment may be one of
2259 * - a declaration with initialization
2260 * - an expression with a top-level assignment operator
2262 static int is_assignment(__isl_keep pet_tree
*tree
)
2266 if (tree
->type
== pet_tree_decl_init
)
2268 return pet_tree_is_assign(tree
);
2271 /* Update "pc" by taking into account the assignment performed by "tree",
2272 * where "tree" satisfies is_assignment.
2274 * In particular, if the lhs of the assignment is a scalar variable and
2275 * if the rhs is an affine expression, then keep track of this value in "pc"
2276 * so that we can plug it in when we later come across the same variable.
2278 * Any previously assigned value to the variable has already been removed
2279 * by scop_handle_writes.
2281 static __isl_give pet_context
*handle_assignment(__isl_take pet_context
*pc
,
2282 __isl_keep pet_tree
*tree
)
2284 pet_expr
*var
, *val
;
2288 if (pet_tree_get_type(tree
) == pet_tree_decl_init
) {
2289 var
= pet_tree_decl_get_var(tree
);
2290 val
= pet_tree_decl_get_init(tree
);
2293 expr
= pet_tree_expr_get_expr(tree
);
2294 var
= pet_expr_get_arg(expr
, 0);
2295 val
= pet_expr_get_arg(expr
, 1);
2296 pet_expr_free(expr
);
2299 if (!pet_expr_is_scalar_access(var
)) {
2305 pa
= pet_expr_extract_affine(val
, pc
);
2307 pc
= pet_context_free(pc
);
2309 if (!isl_pw_aff_involves_nan(pa
)) {
2310 id
= pet_expr_access_get_id(var
);
2311 pc
= pet_context_set_value(pc
, id
, pa
);
2313 isl_pw_aff_free(pa
);
2321 /* Mark all arrays in "scop" as being exposed.
2323 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
2329 for (i
= 0; i
< scop
->n_array
; ++i
)
2330 scop
->arrays
[i
]->exposed
= 1;
2334 /* Try and construct a pet_scop corresponding to (part of)
2335 * a sequence of statements within the context "pc".
2337 * After extracting a statement, we update "pc"
2338 * based on the top-level assignments in the statement
2339 * so that we can exploit them in subsequent statements in the same block.
2341 * If there are any breaks or continues in the individual statements,
2342 * then we may have to compute a new skip condition.
2343 * This is handled using a pet_skip_info object.
2344 * On initialization, the object checks if skip conditions need
2345 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2346 * adds them in pet_skip_info_add.
2348 * If "block" is set, then we need to insert kill statements at
2349 * the end of the block for any array that has been declared by
2350 * one of the statements in the sequence. Each of these declarations
2351 * results in the construction of a kill statement at the place
2352 * of the declaration, so we simply collect duplicates of
2353 * those kill statements and append these duplicates to the constructed scop.
2355 * If "block" is not set, then any array declared by one of the statements
2356 * in the sequence is marked as being exposed.
2358 * If autodetect is set, then we allow the extraction of only a subrange
2359 * of the sequence of statements. However, if there is at least one statement
2360 * for which we could not construct a scop and the final range contains
2361 * either no statements or at least one kill, then we discard the entire
2364 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
2365 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2371 struct pet_scop
*scop
, *kills
;
2373 ctx
= pet_tree_get_ctx(tree
);
2375 space
= pet_context_get_space(pc
);
2376 domain
= pet_context_get_domain(pc
);
2377 pc
= pet_context_copy(pc
);
2378 scop
= pet_scop_empty(isl_space_copy(space
));
2379 kills
= pet_scop_empty(space
);
2380 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
2381 struct pet_scop
*scop_i
;
2383 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
2384 pc
= apply_affine_continue(pc
, scop
);
2385 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
2386 pc
= scop_handle_writes(scop_i
, pc
);
2387 if (is_assignment(tree
->u
.b
.child
[i
]))
2388 pc
= handle_assignment(pc
, tree
->u
.b
.child
[i
]);
2389 struct pet_skip_info skip
;
2390 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
2391 pet_skip_info_seq_extract(&skip
, pc
, state
);
2392 if (scop_i
&& tree_is_decl(tree
->u
.b
.child
[i
])) {
2393 if (tree
->u
.b
.block
) {
2394 struct pet_scop
*kill
;
2395 kill
= extract_kills(domain
, scop_i
, state
);
2396 kills
= pet_scop_add_par(ctx
, kills
, kill
);
2398 scop_i
= mark_exposed(scop_i
);
2400 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
2402 scop
= pet_skip_info_add(&skip
, scop
);
2407 isl_set_free(domain
);
2409 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
2411 pet_context_free(pc
);
2416 /* Internal data structure for extract_declared_arrays.
2418 * "pc" and "state" are used to create pet_array objects and kill statements.
2419 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2420 * found any declared array.
2421 * "scop" has been initialized by the caller and is used to attach
2422 * the created pet_array objects.
2423 * "kill_before" and "kill_after" are created and updated by
2424 * extract_declared_arrays to collect the kills of the arrays.
2426 struct pet_tree_extract_declared_arrays_data
{
2428 struct pet_state
*state
;
2433 struct pet_scop
*scop
;
2434 struct pet_scop
*kill_before
;
2435 struct pet_scop
*kill_after
;
2438 /* Check if the node "node" declares any array or scalar.
2439 * If so, create the corresponding pet_array and attach it to data->scop.
2440 * Additionally, create two kill statements for the array and add them
2441 * to data->kill_before and data->kill_after.
2443 static int extract_declared_arrays(__isl_keep pet_tree
*node
, void *user
)
2445 enum pet_tree_type type
;
2446 struct pet_tree_extract_declared_arrays_data
*data
= user
;
2447 struct pet_array
*array
;
2448 struct pet_scop
*scop_kill
;
2451 type
= pet_tree_get_type(node
);
2452 if (type
== pet_tree_decl
|| type
== pet_tree_decl_init
)
2453 var
= node
->u
.d
.var
;
2454 else if (type
== pet_tree_for
&& node
->u
.l
.declared
)
2459 array
= extract_array(var
, data
->pc
, data
->state
);
2461 array
->declared
= 1;
2462 data
->scop
= pet_scop_add_array(data
->scop
, array
);
2464 scop_kill
= kill(pet_tree_get_loc(node
), array
, data
->pc
, data
->state
);
2466 data
->kill_before
= scop_kill
;
2468 data
->kill_before
= pet_scop_add_par(data
->ctx
,
2469 data
->kill_before
, scop_kill
);
2471 scop_kill
= kill(pet_tree_get_loc(node
), array
, data
->pc
, data
->state
);
2473 data
->kill_after
= scop_kill
;
2475 data
->kill_after
= pet_scop_add_par(data
->ctx
,
2476 data
->kill_after
, scop_kill
);
2483 /* Convert a pet_tree that consists of more than a single leaf
2484 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2485 * Do so within the context of "pc".
2487 * After constructing the core scop, we also look for any arrays (or scalars)
2488 * that are declared inside "tree". Each of those arrays is marked as
2489 * having been declared and kill statements for these arrays
2490 * are introduced before and after the core scop.
2491 * Note that the input tree is not a leaf so that the declaration
2492 * cannot occur at the outer level.
2494 static struct pet_scop
*scop_from_tree_macro(__isl_take pet_tree
*tree
,
2495 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2497 struct pet_tree_extract_declared_arrays_data data
= { pc
, state
};
2499 data
.scop
= scop_from_unevaluated_tree(pet_tree_copy(tree
),
2500 state
->n_stmt
++, pc
);
2503 data
.ctx
= pet_context_get_ctx(pc
);
2504 if (pet_tree_foreach_sub_tree(tree
, &extract_declared_arrays
,
2506 data
.scop
= pet_scop_free(data
.scop
);
2507 pet_tree_free(tree
);
2512 data
.scop
= pet_scop_add_seq(data
.ctx
, data
.kill_before
, data
.scop
);
2513 data
.scop
= pet_scop_add_seq(data
.ctx
, data
.scop
, data
.kill_after
);
2518 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2519 * within the context "pc" by calling the appropriate function
2520 * based on the type of "tree".
2522 * If the initially constructed pet_scop turns out to involve
2523 * dynamic control and if the user has requested an encapsulation
2524 * of all dynamic control, then this pet_scop is discarded and
2525 * a new pet_scop is created with a single statement representing
2526 * the entire "tree".
2527 * However, if the scop contains any active continue or break,
2528 * then we need to include the loop containing the continue or break
2529 * in the encapsulation. We therefore postpone the encapsulation
2530 * until we have constructed a pet_scop for this enclosing loop.
2532 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
2533 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2536 struct pet_scop
*scop
= NULL
;
2541 ctx
= pet_tree_get_ctx(tree
);
2542 switch (tree
->type
) {
2543 case pet_tree_error
:
2545 case pet_tree_block
:
2546 return scop_from_block(tree
, pc
, state
);
2547 case pet_tree_break
:
2548 return scop_from_break(tree
, pet_context_get_space(pc
));
2549 case pet_tree_continue
:
2550 return scop_from_continue(tree
, pet_context_get_space(pc
));
2552 case pet_tree_decl_init
:
2553 return scop_from_decl(tree
, pc
, state
);
2555 return scop_from_tree_expr(tree
, pc
, state
);
2557 case pet_tree_if_else
:
2558 scop
= scop_from_if(tree
, pc
, state
);
2561 scop
= scop_from_for(tree
, pc
, state
);
2563 case pet_tree_while
:
2564 scop
= scop_from_while(tree
, pc
, state
);
2566 case pet_tree_infinite_loop
:
2567 scop
= scop_from_infinite_for(tree
, pc
, state
);
2574 if (!pet_options_get_encapsulate_dynamic_control(ctx
) ||
2575 !pet_scop_has_data_dependent_conditions(scop
) ||
2576 pet_scop_has_var_skip(scop
, pet_skip_now
))
2579 pet_scop_free(scop
);
2580 return scop_from_tree_macro(pet_tree_copy(tree
), pc
, state
);
2583 /* If "tree" has a label that is of the form S_<nr>, then make
2584 * sure that state->n_stmt is greater than nr to ensure that
2585 * we will not generate S_<nr> ourselves.
2587 static int set_first_stmt(__isl_keep pet_tree
*tree
, void *user
)
2589 struct pet_state
*state
= user
;
2597 name
= isl_id_get_name(tree
->label
);
2598 if (strncmp(name
, "S_", 2) != 0)
2600 nr
= atoi(name
+ 2);
2601 if (nr
>= state
->n_stmt
)
2602 state
->n_stmt
= nr
+ 1;
2607 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2608 * "int_size" is the number of bytes need to represent an integer.
2609 * "extract_array" is a callback that we can use to create a pet_array
2610 * that corresponds to the variable accessed by an expression.
2612 * Initialize the global state, construct a context and then
2613 * construct the pet_scop by recursively visiting the tree.
2615 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2617 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
2618 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
2619 __isl_keep pet_context
*pc
, void *user
), void *user
,
2620 __isl_keep pet_context
*pc
)
2622 struct pet_scop
*scop
;
2623 struct pet_state state
= { 0 };
2628 state
.ctx
= pet_tree_get_ctx(tree
);
2629 state
.int_size
= int_size
;
2630 state
.extract_array
= extract_array
;
2632 if (pet_tree_foreach_sub_tree(tree
, &set_first_stmt
, &state
) < 0)
2633 tree
= pet_tree_free(tree
);
2635 scop
= scop_from_tree(tree
, pc
, &state
);
2636 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
2638 pet_tree_free(tree
);
2641 scop
->context
= isl_set_params(scop
->context
);