2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
38 #include <isl/id_to_pw_aff.h>
39 #include <isl/union_set.h>
48 #include "tree2scop.h"
50 /* Update "pc" by taking into account the writes in "stmt".
51 * That is, clear any previously assigned values to variables
52 * that are written by "stmt".
54 static __isl_give pet_context
*handle_writes(struct pet_stmt
*stmt
,
55 __isl_take pet_context
*pc
)
57 return pet_context_clear_writes_in_tree(pc
, stmt
->body
);
60 /* Update "pc" based on the write accesses in "scop".
62 static __isl_give pet_context
*scop_handle_writes(struct pet_scop
*scop
,
63 __isl_take pet_context
*pc
)
68 return pet_context_free(pc
);
69 for (i
= 0; i
< scop
->n_stmt
; ++i
)
70 pc
= handle_writes(scop
->stmts
[i
], pc
);
75 /* Wrapper around pet_expr_resolve_assume
76 * for use as a callback to pet_tree_map_expr.
78 static __isl_give pet_expr
*resolve_assume(__isl_take pet_expr
*expr
,
81 pet_context
*pc
= user
;
83 return pet_expr_resolve_assume(expr
, pc
);
86 /* Check if any expression inside "tree" is an assume expression and
87 * if its single argument can be converted to an affine expression
88 * in the context of "pc".
89 * If so, replace the argument by the affine expression.
91 __isl_give pet_tree
*pet_tree_resolve_assume(__isl_take pet_tree
*tree
,
92 __isl_keep pet_context
*pc
)
94 return pet_tree_map_expr(tree
, &resolve_assume
, pc
);
97 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
98 * "tree" has already been evaluated in the context of "pc".
99 * This mainly involves resolving nested expression parameters
100 * and setting the name of the iteration space.
101 * The name is given by tree->label if it is non-NULL. Otherwise,
102 * it is of the form S_<stmt_nr>.
104 static struct pet_scop
*scop_from_evaluated_tree(__isl_take pet_tree
*tree
,
105 int stmt_nr
, __isl_keep pet_context
*pc
)
111 space
= pet_context_get_space(pc
);
113 tree
= pet_tree_resolve_nested(tree
, space
);
114 tree
= pet_tree_resolve_assume(tree
, pc
);
116 domain
= pet_context_get_domain(pc
);
117 ps
= pet_stmt_from_pet_tree(domain
, stmt_nr
, tree
);
118 return pet_scop_from_pet_stmt(space
, ps
);
121 /* Convert a top-level pet_expr to a pet_scop with one statement
122 * within the context "pc".
123 * "expr" has already been evaluated in the context of "pc".
124 * We construct a pet_tree from "expr" and continue with
125 * scop_from_evaluated_tree.
126 * The name is of the form S_<stmt_nr>.
127 * The location of the statement is set to "loc".
129 static struct pet_scop
*scop_from_evaluated_expr(__isl_take pet_expr
*expr
,
130 int stmt_nr
, __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
134 tree
= pet_tree_new_expr(expr
);
135 tree
= pet_tree_set_loc(tree
, loc
);
136 return scop_from_evaluated_tree(tree
, stmt_nr
, pc
);
139 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
140 * "tree" has not yet been evaluated in the context of "pc".
141 * We evaluate "tree" in the context of "pc" and continue with
142 * scop_from_evaluated_tree.
143 * The statement name is given by tree->label if it is non-NULL. Otherwise,
144 * it is of the form S_<stmt_nr>.
146 static struct pet_scop
*scop_from_unevaluated_tree(__isl_take pet_tree
*tree
,
147 int stmt_nr
, __isl_keep pet_context
*pc
)
149 tree
= pet_context_evaluate_tree(pc
, tree
);
150 return scop_from_evaluated_tree(tree
, stmt_nr
, pc
);
153 /* Convert a top-level pet_expr to a pet_scop with one statement
154 * within the context "pc", where "expr" has not yet been evaluated
155 * in the context of "pc".
156 * We construct a pet_tree from "expr" and continue with
157 * scop_from_unevaluated_tree.
158 * The statement name is of the form S_<stmt_nr>.
159 * The location of the statement is set to "loc".
161 static struct pet_scop
*scop_from_expr(__isl_take pet_expr
*expr
,
162 int stmt_nr
, __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
166 tree
= pet_tree_new_expr(expr
);
167 tree
= pet_tree_set_loc(tree
, loc
);
168 return scop_from_unevaluated_tree(tree
, stmt_nr
, pc
);
171 /* Construct a pet_scop with a single statement killing the entire
173 * The location of the statement is set to "loc".
175 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
176 __isl_keep pet_context
*pc
, struct pet_state
*state
)
181 isl_multi_pw_aff
*index
;
184 struct pet_scop
*scop
;
188 ctx
= isl_set_get_ctx(array
->extent
);
189 access
= isl_map_from_range(isl_set_copy(array
->extent
));
190 id
= isl_set_get_tuple_id(array
->extent
);
191 space
= isl_space_alloc(ctx
, 0, 0, 0);
192 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
193 index
= isl_multi_pw_aff_zero(space
);
194 expr
= pet_expr_kill_from_access_and_index(access
, index
);
195 return scop_from_expr(expr
, state
->n_stmt
++, loc
, pc
);
201 /* Construct and return a pet_array corresponding to the variable
202 * accessed by "access" by calling the extract_array callback.
204 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
205 __isl_keep pet_context
*pc
, struct pet_state
*state
)
207 return state
->extract_array(access
, pc
, state
->user
);
210 /* Construct a pet_scop for a (single) variable declaration
211 * within the context "pc".
213 * The scop contains the variable being declared (as an array)
214 * and a statement killing the array.
216 * If the declaration comes with an initialization, then the scop
217 * also contains an assignment to the variable.
219 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
220 __isl_keep pet_context
*pc
, struct pet_state
*state
)
224 struct pet_array
*array
;
225 struct pet_scop
*scop_decl
, *scop
;
226 pet_expr
*lhs
, *rhs
, *pe
;
228 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
231 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
232 scop_decl
= pet_scop_add_array(scop_decl
, array
);
234 if (tree
->type
!= pet_tree_decl_init
)
237 lhs
= pet_expr_copy(tree
->u
.d
.var
);
238 rhs
= pet_expr_copy(tree
->u
.d
.init
);
239 type_size
= pet_expr_get_type_size(lhs
);
240 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
241 scop
= scop_from_expr(pe
, state
->n_stmt
++, pet_tree_get_loc(tree
), pc
);
243 ctx
= pet_tree_get_ctx(tree
);
244 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
249 /* Does "tree" represent a kill statement?
250 * That is, is it an expression statement that "calls" __pencil_kill?
252 static int is_pencil_kill(__isl_keep pet_tree
*tree
)
259 if (tree
->type
!= pet_tree_expr
)
261 expr
= tree
->u
.e
.expr
;
262 if (pet_expr_get_type(expr
) != pet_expr_call
)
264 name
= pet_expr_call_get_name(expr
);
267 return !strcmp(name
, "__pencil_kill");
270 /* Add a kill to "scop" that kills what is accessed by
271 * the access expression "expr".
273 * Mark the access as a write prior to evaluation to avoid
274 * the access being replaced by a possible known value
275 * during the evaluation.
277 * If the access expression has any arguments (after evaluation
278 * in the context of "pc"), then we ignore it, since we cannot
279 * tell which elements are definitely killed.
281 * Otherwise, we extend the index expression to the dimension
282 * of the accessed array and intersect with the extent of the array and
283 * add a kill expression that kills these array elements is added to "scop".
285 static struct pet_scop
*scop_add_kill(struct pet_scop
*scop
,
286 __isl_take pet_expr
*expr
, __isl_take pet_loc
*loc
,
287 __isl_keep pet_context
*pc
, struct pet_state
*state
)
291 isl_multi_pw_aff
*index
;
294 struct pet_array
*array
;
295 struct pet_scop
*scop_i
;
297 expr
= pet_expr_access_set_write(expr
, 1);
298 expr
= pet_context_evaluate_expr(pc
, expr
);
301 if (expr
->n_arg
!= 0) {
306 array
= extract_array(expr
, pc
, state
);
309 index
= pet_expr_access_get_index(expr
);
311 map
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
312 id
= isl_map_get_tuple_id(map
, isl_dim_out
);
313 dim1
= isl_set_dim(array
->extent
, isl_dim_set
);
314 dim2
= isl_map_dim(map
, isl_dim_out
);
315 map
= isl_map_add_dims(map
, isl_dim_out
, dim1
- dim2
);
316 map
= isl_map_set_tuple_id(map
, isl_dim_out
, id
);
317 map
= isl_map_intersect_range(map
, isl_set_copy(array
->extent
));
318 pet_array_free(array
);
319 kill
= pet_expr_kill_from_access_and_index(map
, index
);
320 scop_i
= scop_from_evaluated_expr(kill
, state
->n_stmt
++, loc
, pc
);
321 scop
= pet_scop_add_par(state
->ctx
, scop
, scop_i
);
327 return pet_scop_free(scop
);
330 /* For each argument of the __pencil_kill call in "tree" that
331 * represents an access, add a kill statement to "scop" killing the accessed
334 static struct pet_scop
*scop_from_pencil_kill(__isl_keep pet_tree
*tree
,
335 __isl_keep pet_context
*pc
, struct pet_state
*state
)
338 struct pet_scop
*scop
;
341 call
= tree
->u
.e
.expr
;
343 scop
= pet_scop_empty(pet_context_get_space(pc
));
345 n
= pet_expr_get_n_arg(call
);
346 for (i
= 0; i
< n
; ++i
) {
350 arg
= pet_expr_get_arg(call
, i
);
352 return pet_scop_free(scop
);
353 if (pet_expr_get_type(arg
) != pet_expr_access
) {
357 loc
= pet_tree_get_loc(tree
);
358 scop
= scop_add_kill(scop
, arg
, loc
, pc
, state
);
364 /* Construct a pet_scop for an expression statement within the context "pc".
366 * If the expression calls __pencil_kill, then it needs to be converted
367 * into zero or more kill statements.
368 * Otherwise, a scop is extracted directly from the tree.
370 static struct pet_scop
*scop_from_tree_expr(__isl_keep pet_tree
*tree
,
371 __isl_keep pet_context
*pc
, struct pet_state
*state
)
375 is_kill
= is_pencil_kill(tree
);
379 return scop_from_pencil_kill(tree
, pc
, state
);
380 return scop_from_unevaluated_tree(pet_tree_copy(tree
),
381 state
->n_stmt
++, pc
);
384 /* Return those elements in the space of "cond" that come after
385 * (based on "sign") an element in "cond" in the final dimension.
387 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
390 isl_map
*previous_to_this
;
393 dim
= isl_set_dim(cond
, isl_dim_set
);
394 space
= isl_space_map_from_set(isl_set_get_space(cond
));
395 previous_to_this
= isl_map_universe(space
);
396 for (i
= 0; i
+ 1 < dim
; ++i
)
397 previous_to_this
= isl_map_equate(previous_to_this
,
398 isl_dim_in
, i
, isl_dim_out
, i
);
400 previous_to_this
= isl_map_order_lt(previous_to_this
,
401 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
403 previous_to_this
= isl_map_order_gt(previous_to_this
,
404 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
406 cond
= isl_set_apply(cond
, previous_to_this
);
411 /* Remove those iterations of "domain" that have an earlier iteration
412 * (based on "sign") in the final dimension where "skip" is satisfied.
413 * If "apply_skip_map" is set, then "skip_map" is first applied
414 * to the embedded skip condition before removing it from the domain.
416 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
417 __isl_take isl_set
*skip
, int sign
,
418 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
421 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
422 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
423 return isl_set_subtract(domain
, after(skip
, sign
));
426 /* Create a single-dimensional multi-affine expression on the domain space
427 * of "pc" that is equal to the final dimension of this domain.
428 * "loop_nr" is the sequence number of the corresponding loop.
429 * If "id" is not NULL, then it is used as the output tuple name.
430 * Otherwise, the name is constructed as L_<loop_nr>.
432 static __isl_give isl_multi_aff
*map_to_last(__isl_keep pet_context
*pc
,
433 int loop_nr
, __isl_keep isl_id
*id
)
443 space
= pet_context_get_space(pc
);
444 pos
= isl_space_dim(space
, isl_dim_set
) - 1;
445 ls
= isl_local_space_from_space(space
);
446 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, pos
);
447 ma
= isl_multi_aff_from_aff(aff
);
450 label
= isl_id_copy(id
);
452 snprintf(name
, sizeof(name
), "L_%d", loop_nr
);
453 label
= isl_id_alloc(pet_context_get_ctx(pc
), name
, NULL
);
455 ma
= isl_multi_aff_set_tuple_id(ma
, isl_dim_out
, label
);
460 /* Create an affine expression that maps elements
461 * of an array "id_test" to the previous element in the final dimension
462 * (according to "inc"), provided this element belongs to "domain".
463 * That is, create the affine expression
465 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
467 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
468 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
475 isl_multi_pw_aff
*prev
;
477 pos
= isl_set_dim(domain
, isl_dim_set
) - 1;
478 space
= isl_set_get_space(domain
);
479 space
= isl_space_map_from_set(space
);
480 ma
= isl_multi_aff_identity(space
);
481 aff
= isl_multi_aff_get_aff(ma
, pos
);
482 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
483 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
484 domain
= isl_set_preimage_multi_aff(domain
, isl_multi_aff_copy(ma
));
485 prev
= isl_multi_pw_aff_from_multi_aff(ma
);
486 pa
= isl_multi_pw_aff_get_pw_aff(prev
, pos
);
487 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
488 prev
= isl_multi_pw_aff_set_pw_aff(prev
, pos
, pa
);
489 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
494 /* Add an implication to "scop" expressing that if an element of
495 * virtual array "id_test" has value "satisfied" then all previous elements
496 * of this array (in the final dimension) also have that value.
497 * The set of previous elements is bounded by "domain".
498 * If "sign" is negative then the iterator
499 * is decreasing and we express that all subsequent array elements
500 * (but still defined previously) have the same value.
502 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
503 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
510 dim
= isl_set_dim(domain
, isl_dim_set
);
511 domain
= isl_set_set_tuple_id(domain
, id_test
);
512 space
= isl_space_map_from_set(isl_set_get_space(domain
));
513 map
= isl_map_universe(space
);
514 for (i
= 0; i
+ 1 < dim
; ++i
)
515 map
= isl_map_equate(map
, isl_dim_in
, i
, isl_dim_out
, i
);
517 map
= isl_map_order_ge(map
,
518 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
520 map
= isl_map_order_le(map
,
521 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
522 map
= isl_map_intersect_range(map
, domain
);
523 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
528 /* Add a filter to "scop" that imposes that it is only executed
529 * when the variable identified by "id_test" has a zero value
530 * for all previous iterations of "domain".
532 * In particular, add a filter that imposes that the array
533 * has a zero value at the previous iteration of domain and
534 * add an implication that implies that it then has that
535 * value for all previous iterations.
537 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
538 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
539 __isl_take isl_val
*inc
)
541 isl_multi_pw_aff
*prev
;
542 int sign
= isl_val_sgn(inc
);
544 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
545 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
546 scop
= pet_scop_filter(scop
, prev
, 0);
551 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
552 __isl_keep pet_context
*pc
, struct pet_state
*state
);
554 /* Construct a pet_scop for an infinite loop around the given body
555 * within the context "pc".
556 * "loop_id" is the label on the loop or NULL if there is no such label.
558 * The domain of "pc" has already been extended with an infinite loop
562 * We extract a pet_scop for the body and then embed it in a loop with
565 * { [outer,t] -> [t] }
567 * If the body contains any break, then it is taken into
568 * account in apply_affine_break (if the skip condition is affine)
569 * or in scop_add_break (if the skip condition is not affine).
571 * Note that in case of an affine skip condition,
572 * since we are dealing with a loop without loop iterator,
573 * the skip condition cannot refer to the current loop iterator and
574 * so effectively, the effect on the iteration domain is of the form
576 * { [outer,0]; [outer,t] : t >= 1 and not skip }
578 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
579 __isl_keep isl_id
*loop_id
, __isl_keep pet_context
*pc
,
580 struct pet_state
*state
)
586 isl_multi_aff
*sched
;
587 struct pet_scop
*scop
;
588 int has_affine_break
;
591 ctx
= pet_tree_get_ctx(body
);
592 domain
= pet_context_get_domain(pc
);
593 sched
= map_to_last(pc
, state
->n_loop
++, loop_id
);
595 scop
= scop_from_tree(body
, pc
, state
);
597 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
598 if (has_affine_break
)
599 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
600 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
602 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
604 scop
= pet_scop_reset_skips(scop
);
605 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
);
606 if (has_affine_break
) {
607 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
608 scop
= pet_scop_intersect_domain_prefix(scop
,
609 isl_set_copy(domain
));
612 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
614 isl_set_free(domain
);
619 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
624 * within the context "pc".
626 * Extend the domain of "pc" with an extra inner loop
630 * and construct the scop in scop_from_infinite_loop.
632 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
633 __isl_keep pet_context
*pc
, struct pet_state
*state
)
635 struct pet_scop
*scop
;
637 pc
= pet_context_copy(pc
);
638 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
640 pc
= pet_context_add_infinite_loop(pc
);
642 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, tree
->label
, pc
, state
);
644 pet_context_free(pc
);
649 /* Construct a pet_scop for a while loop of the form
654 * within the context "pc".
656 * The domain of "pc" has already been extended with an infinite loop
660 * Here, we add the constraints on the outer loop iterators
661 * implied by "pa" and construct the scop in scop_from_infinite_loop.
662 * Note that the intersection with these constraints
663 * may result in an empty loop.
665 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
666 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
667 struct pet_state
*state
)
669 struct pet_scop
*scop
;
670 isl_set
*dom
, *local
;
673 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
674 dom
= isl_pw_aff_non_zero_set(pa
);
675 local
= isl_set_add_dims(isl_set_copy(dom
), isl_dim_set
, 1);
676 pc
= pet_context_intersect_domain(pc
, local
);
677 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, tree
->label
, pc
, state
);
678 scop
= pet_scop_restrict(scop
, dom
);
679 scop
= pet_scop_restrict_context(scop
, valid
);
681 pet_context_free(pc
);
685 /* Construct a scop for a while, given the scops for the condition
686 * and the body, the filter identifier and the iteration domain of
689 * In particular, the scop for the condition is filtered to depend
690 * on "id_test" evaluating to true for all previous iterations
691 * of the loop, while the scop for the body is filtered to depend
692 * on "id_test" evaluating to true for all iterations up to the
694 * The actual filter only imposes that this virtual array has
695 * value one on the previous or the current iteration.
696 * The fact that this condition also applies to the previous
697 * iterations is enforced by an implication.
699 * These filtered scops are then combined into a single scop,
700 * with the condition scop scheduled before the body scop.
702 * "sign" is positive if the iterator increases and negative
705 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
706 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
707 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
709 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
711 isl_multi_pw_aff
*test_index
;
712 isl_multi_pw_aff
*prev
;
713 int sign
= isl_val_sgn(inc
);
714 struct pet_scop
*scop
;
716 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
717 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
719 space
= isl_space_map_from_set(isl_set_get_space(domain
));
720 test_index
= isl_multi_pw_aff_identity(space
);
721 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
722 isl_id_copy(id_test
));
723 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
725 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
726 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
731 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
732 * evaluating "cond" and writing the result to a virtual scalar,
733 * as expressed by "index".
734 * The expression "cond" has not yet been evaluated in the context of "pc".
735 * Do so within the context "pc".
736 * The location of the statement is set to "loc".
738 static struct pet_scop
*scop_from_non_affine_condition(
739 __isl_take pet_expr
*cond
, int stmt_nr
,
740 __isl_take isl_multi_pw_aff
*index
,
741 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
743 pet_expr
*expr
, *write
;
745 cond
= pet_context_evaluate_expr(pc
, cond
);
747 write
= pet_expr_from_index(index
);
748 write
= pet_expr_access_set_write(write
, 1);
749 write
= pet_expr_access_set_read(write
, 0);
750 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
752 return scop_from_evaluated_expr(expr
, stmt_nr
, loc
, pc
);
755 /* Given that "scop" has an affine skip condition of type pet_skip_now,
756 * apply this skip condition to the domain of "pc".
757 * That is, remove the elements satisfying the skip condition from
758 * the domain of "pc".
760 static __isl_give pet_context
*apply_affine_continue(__isl_take pet_context
*pc
,
761 struct pet_scop
*scop
)
763 isl_set
*domain
, *skip
;
765 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_now
);
766 domain
= pet_context_get_domain(pc
);
767 domain
= isl_set_subtract(domain
, skip
);
768 pc
= pet_context_intersect_domain(pc
, domain
);
773 /* Add a scop for evaluating the loop increment "inc" at the end
774 * of a loop body "scop" within the context "pc".
776 * The skip conditions resulting from continue statements inside
777 * the body do not apply to "inc", but those resulting from break
778 * statements do need to get applied.
780 static struct pet_scop
*scop_add_inc(struct pet_scop
*scop
,
781 __isl_take pet_expr
*inc
, __isl_take pet_loc
*loc
,
782 __isl_keep pet_context
*pc
, struct pet_state
*state
)
784 struct pet_scop
*scop_inc
;
786 pc
= pet_context_copy(pc
);
788 if (pet_scop_has_skip(scop
, pet_skip_later
)) {
789 isl_multi_pw_aff
*skip
;
790 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
791 scop
= pet_scop_set_skip(scop
, pet_skip_now
, skip
);
792 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
793 pc
= apply_affine_continue(pc
, scop
);
795 pet_scop_reset_skip(scop
, pet_skip_now
);
796 scop_inc
= scop_from_expr(inc
, state
->n_stmt
++, loc
, pc
);
797 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_inc
);
799 pet_context_free(pc
);
804 /* Construct a generic while scop, with iteration domain
805 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
806 * "loop_id" is the label on the loop or NULL if there is no such label.
807 * The domain of "pc" has already been extended with this infinite loop
811 * The scop consists of two parts,
812 * one for evaluating the condition "cond" and one for the body.
813 * If "expr_inc" is not NULL, then a scop for evaluating this expression
814 * is added at the end of the body,
815 * after replacing any skip conditions resulting from continue statements
816 * by the skip conditions resulting from break statements (if any).
818 * The schedules are combined as a sequence to reflect that the condition is
819 * evaluated before the body is executed and the body is filtered to depend
820 * on the result of the condition evaluating to true on all iterations
821 * up to the current iteration, while the evaluation of the condition itself
822 * is filtered to depend on the result of the condition evaluating to true
823 * on all previous iterations.
824 * The context of the scop representing the body is dropped
825 * because we don't know how many times the body will be executed,
828 * If the body contains any break, then it is taken into
829 * account in apply_affine_break (if the skip condition is affine)
830 * or in scop_add_break (if the skip condition is not affine).
832 * Note that in case of an affine skip condition,
833 * since we are dealing with a loop without loop iterator,
834 * the skip condition cannot refer to the current loop iterator and
835 * so effectively, the effect on the iteration domain is of the form
837 * { [outer,0]; [outer,t] : t >= 1 and not skip }
839 static struct pet_scop
*scop_from_non_affine_while(__isl_take pet_expr
*cond
,
840 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
841 __isl_keep isl_id
*loop_id
, __isl_take pet_expr
*expr_inc
,
842 __isl_take pet_context
*pc
, struct pet_state
*state
)
845 isl_id
*id_test
, *id_break_test
;
847 isl_multi_pw_aff
*test_index
;
850 isl_multi_aff
*sched
;
851 struct pet_scop
*scop
, *scop_body
;
852 int has_affine_break
;
856 space
= pet_context_get_space(pc
);
857 test_index
= pet_create_test_index(space
, state
->n_test
++);
858 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
859 isl_multi_pw_aff_copy(test_index
),
860 pet_loc_copy(loc
), pc
);
861 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
862 domain
= pet_context_get_domain(pc
);
863 scop
= pet_scop_add_boolean_array(scop
, isl_set_copy(domain
),
864 test_index
, state
->int_size
);
866 sched
= map_to_last(pc
, state
->n_loop
++, loop_id
);
868 scop_body
= scop_from_tree(tree_body
, pc
, state
);
870 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
871 if (has_affine_break
)
872 skip
= pet_scop_get_affine_skip_domain(scop_body
,
874 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
876 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
878 scop_body
= pet_scop_reset_context(scop_body
);
880 scop_body
= scop_add_inc(scop_body
, expr_inc
, loc
, pc
, state
);
883 scop_body
= pet_scop_reset_skips(scop_body
);
885 if (has_affine_break
) {
886 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
887 scop
= pet_scop_intersect_domain_prefix(scop
,
888 isl_set_copy(domain
));
889 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
890 isl_set_copy(domain
));
893 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
894 isl_set_copy(domain
), isl_val_one(ctx
));
895 scop_body
= scop_add_break(scop_body
, id_break_test
,
896 isl_set_copy(domain
), isl_val_one(ctx
));
898 scop
= scop_add_while(scop
, scop_body
, id_test
, isl_set_copy(domain
),
901 scop
= pet_scop_embed(scop
, domain
, sched
);
903 pet_context_free(pc
);
907 /* Check if the while loop is of the form
909 * while (affine expression)
912 * If so, call scop_from_affine_while to construct a scop.
914 * Otherwise, pass control to scop_from_non_affine_while.
916 * "pc" is the context in which the affine expressions in the scop are created.
917 * The domain of "pc" is extended with an infinite loop
921 * before passing control to scop_from_affine_while or
922 * scop_from_non_affine_while.
924 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
925 __isl_keep pet_context
*pc
, struct pet_state
*state
)
933 pc
= pet_context_copy(pc
);
934 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
936 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
937 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
938 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
939 pet_expr_free(cond_expr
);
941 pc
= pet_context_add_infinite_loop(pc
);
946 if (!isl_pw_aff_involves_nan(pa
))
947 return scop_from_affine_while(tree
, pa
, pc
, state
);
949 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
950 pet_tree_get_loc(tree
), tree
->u
.l
.body
,
951 tree
->label
, NULL
, pc
, state
);
953 pet_context_free(pc
);
957 /* Check whether "cond" expresses a simple loop bound
958 * on the final set dimension.
959 * In particular, if "up" is set then "cond" should contain only
960 * upper bounds on the final set dimension.
961 * Otherwise, it should contain only lower bounds.
963 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
967 pos
= isl_set_dim(cond
, isl_dim_set
) - 1;
968 if (isl_val_is_pos(inc
))
969 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, pos
);
971 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, pos
);
974 /* Extend a condition on a given iteration of a loop to one that
975 * imposes the same condition on all previous iterations.
976 * "domain" expresses the lower [upper] bound on the iterations
977 * when inc is positive [negative] in its final dimension.
979 * In particular, we construct the condition (when inc is positive)
981 * forall i' : (domain(i') and i' <= i) => cond(i')
983 * (where "<=" applies to the final dimension)
984 * which is equivalent to
986 * not exists i' : domain(i') and i' <= i and not cond(i')
988 * We construct this set by subtracting the satisfying cond from domain,
991 * { [i'] -> [i] : i' <= i }
993 * and then subtracting the result from domain again.
995 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
996 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
999 isl_map
*previous_to_this
;
1002 dim
= isl_set_dim(cond
, isl_dim_set
);
1003 space
= isl_space_map_from_set(isl_set_get_space(cond
));
1004 previous_to_this
= isl_map_universe(space
);
1005 for (i
= 0; i
+ 1 < dim
; ++i
)
1006 previous_to_this
= isl_map_equate(previous_to_this
,
1007 isl_dim_in
, i
, isl_dim_out
, i
);
1008 if (isl_val_is_pos(inc
))
1009 previous_to_this
= isl_map_order_le(previous_to_this
,
1010 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
1012 previous_to_this
= isl_map_order_ge(previous_to_this
,
1013 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
1015 cond
= isl_set_subtract(isl_set_copy(domain
), cond
);
1016 cond
= isl_set_apply(cond
, previous_to_this
);
1017 cond
= isl_set_subtract(domain
, cond
);
1024 /* Given an initial value of the form
1026 * { [outer,i] -> init(outer) }
1028 * construct a domain of the form
1030 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1032 static __isl_give isl_set
*strided_domain(__isl_take isl_pw_aff
*init
,
1033 __isl_take isl_val
*inc
)
1038 isl_local_space
*ls
;
1041 dim
= isl_pw_aff_dim(init
, isl_dim_in
);
1043 init
= isl_pw_aff_add_dims(init
, isl_dim_in
, 1);
1044 space
= isl_pw_aff_get_domain_space(init
);
1045 ls
= isl_local_space_from_space(space
);
1046 aff
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
1047 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, dim
, inc
);
1048 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
1050 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, dim
- 1);
1051 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
1053 set
= isl_set_lower_bound_si(set
, isl_dim_set
, dim
, 0);
1054 set
= isl_set_project_out(set
, isl_dim_set
, dim
, 1);
1059 /* Assuming "cond" represents a bound on a loop where the loop
1060 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1063 * Under the given assumptions, wrapping is only possible if "cond" allows
1064 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1065 * increasing iterator and 0 in case of a decreasing iterator.
1067 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
1068 __isl_keep isl_val
*inc
)
1075 test
= isl_set_copy(cond
);
1077 ctx
= isl_set_get_ctx(test
);
1078 if (isl_val_is_neg(inc
))
1079 limit
= isl_val_zero(ctx
);
1081 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
1082 limit
= isl_val_2exp(limit
);
1083 limit
= isl_val_sub_ui(limit
, 1);
1086 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
1087 cw
= !isl_set_is_empty(test
);
1097 * construct the following affine expression on this space
1099 * { [outer, v] -> [outer, v mod 2^width] }
1101 * where width is the number of bits used to represent the values
1102 * of the unsigned variable "iv".
1104 static __isl_give isl_multi_aff
*compute_wrapping(__isl_take isl_space
*space
,
1105 __isl_keep pet_expr
*iv
)
1111 dim
= isl_space_dim(space
, isl_dim_set
);
1113 space
= isl_space_map_from_set(space
);
1114 ma
= isl_multi_aff_identity(space
);
1116 aff
= isl_multi_aff_get_aff(ma
, dim
- 1);
1117 aff
= pet_wrap_aff(aff
, pet_expr_get_type_size(iv
));
1118 ma
= isl_multi_aff_set_aff(ma
, dim
- 1, aff
);
1123 /* Given two sets in the space
1127 * where l represents the outer loop iterators, compute the set
1128 * of values of l that ensure that "set1" is a subset of "set2".
1130 * set1 is a subset of set2 if
1132 * forall i: set1(l,i) => set2(l,i)
1136 * not exists i: set1(l,i) and not set2(l,i)
1140 * not exists i: (set1 \ set2)(l,i)
1142 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
1143 __isl_take isl_set
*set2
)
1147 pos
= isl_set_dim(set1
, isl_dim_set
) - 1;
1148 set1
= isl_set_subtract(set1
, set2
);
1149 set1
= isl_set_eliminate(set1
, isl_dim_set
, pos
, 1);
1150 return isl_set_complement(set1
);
1153 /* Compute the set of outer iterator values for which "cond" holds
1154 * on the next iteration of the inner loop for each element of "dom".
1156 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1157 * to the outer loop iterators), plug that into "cond"
1158 * and then compute the set of outer iterators for which "dom" is a subset
1161 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
1162 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
1169 pos
= isl_set_dim(dom
, isl_dim_set
) - 1;
1170 space
= isl_set_get_space(dom
);
1171 space
= isl_space_map_from_set(space
);
1172 ma
= isl_multi_aff_identity(space
);
1173 aff
= isl_multi_aff_get_aff(ma
, pos
);
1174 aff
= isl_aff_add_constant_val(aff
, inc
);
1175 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
1176 cond
= isl_set_preimage_multi_aff(cond
, ma
);
1178 return enforce_subset(dom
, cond
);
1181 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1182 * In particular, "pc_init" represents the context of the loop,
1183 * whereas "pc" represents the context of the body of the loop and
1184 * has already had its domain extended with an infinite loop
1188 * The for loop has the form
1190 * for (iv = init; cond; iv += inc)
1201 * except that the skips resulting from any continue statements
1202 * in body do not apply to the increment, but are replaced by the skips
1203 * resulting from break statements.
1205 * If the loop iterator is declared in the for loop, then it is killed before
1206 * and after the loop.
1208 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
1209 __isl_keep pet_context
*init_pc
, __isl_take pet_context
*pc
,
1210 struct pet_state
*state
)
1214 pet_expr
*expr_iv
, *init
, *inc
;
1215 struct pet_scop
*scop_init
, *scop
;
1217 struct pet_array
*array
;
1218 struct pet_scop
*scop_kill
;
1220 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1221 pc
= pet_context_clear_value(pc
, iv
);
1223 declared
= tree
->u
.l
.declared
;
1225 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
1226 type_size
= pet_expr_get_type_size(expr_iv
);
1227 init
= pet_expr_copy(tree
->u
.l
.init
);
1228 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
1229 scop_init
= scop_from_expr(init
, state
->n_stmt
++,
1230 pet_tree_get_loc(tree
), init_pc
);
1232 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
1233 type_size
= pet_expr_get_type_size(expr_iv
);
1234 inc
= pet_expr_copy(tree
->u
.l
.inc
);
1235 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
1237 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
1238 pet_tree_get_loc(tree
), tree
->u
.l
.body
, tree
->label
,
1239 inc
, pet_context_copy(pc
), state
);
1241 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
1243 pet_context_free(pc
);
1248 array
= extract_array(tree
->u
.l
.iv
, init_pc
, state
);
1250 array
->declared
= 1;
1251 scop_kill
= kill(pet_tree_get_loc(tree
), array
, init_pc
, state
);
1252 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
1253 scop_kill
= kill(pet_tree_get_loc(tree
), array
, init_pc
, state
);
1254 scop_kill
= pet_scop_add_array(scop_kill
, array
);
1255 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
1260 /* Given an access expression "expr", is the variable accessed by
1261 * "expr" assigned anywhere inside "tree"?
1263 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
1268 id
= pet_expr_access_get_id(expr
);
1269 assigned
= pet_tree_writes(tree
, id
);
1275 /* Are all nested access parameters in "pa" allowed given "tree".
1276 * In particular, is none of them written by anywhere inside "tree".
1278 * If "tree" has any continue or break nodes in the current loop level,
1279 * then no nested access parameters are allowed.
1280 * In particular, if there is any nested access in a guard
1281 * for a piece of code containing a "continue", then we want to introduce
1282 * a separate statement for evaluating this guard so that we can express
1283 * that the result is false for all previous iterations.
1285 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
1286 __isl_keep pet_tree
*tree
)
1293 if (!pet_nested_any_in_pw_aff(pa
))
1296 if (pet_tree_has_continue_or_break(tree
))
1299 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1300 for (i
= 0; i
< nparam
; ++i
) {
1301 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1305 if (!pet_nested_in_id(id
)) {
1310 expr
= pet_nested_extract_expr(id
);
1311 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1312 !is_assigned(expr
, tree
);
1314 pet_expr_free(expr
);
1324 /* Internal data structure for collect_local.
1325 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1326 * "local" collects the results.
1328 struct pet_tree_collect_local_data
{
1330 struct pet_state
*state
;
1331 isl_union_set
*local
;
1334 /* Add the variable accessed by "var" to data->local.
1335 * We extract a representation of the variable from
1336 * the pet_array constructed using extract_array
1337 * to ensure consistency with the rest of the scop.
1339 static int add_local(struct pet_tree_collect_local_data
*data
,
1340 __isl_keep pet_expr
*var
)
1342 struct pet_array
*array
;
1345 array
= extract_array(var
, data
->pc
, data
->state
);
1349 universe
= isl_set_universe(isl_set_get_space(array
->extent
));
1350 data
->local
= isl_union_set_add_set(data
->local
, universe
);
1351 pet_array_free(array
);
1356 /* If the node "tree" declares a variable, then add it to
1359 static int extract_local_var(__isl_keep pet_tree
*tree
, void *user
)
1361 enum pet_tree_type type
;
1362 struct pet_tree_collect_local_data
*data
= user
;
1364 type
= pet_tree_get_type(tree
);
1365 if (type
== pet_tree_decl
|| type
== pet_tree_decl_init
)
1366 return add_local(data
, tree
->u
.d
.var
);
1371 /* If the node "tree" is a for loop that declares its induction variable,
1372 * then add it this induction variable to data->local.
1374 static int extract_local_iterator(__isl_keep pet_tree
*tree
, void *user
)
1376 struct pet_tree_collect_local_data
*data
= user
;
1378 if (pet_tree_get_type(tree
) == pet_tree_for
&& tree
->u
.l
.declared
)
1379 return add_local(data
, tree
->u
.l
.iv
);
1384 /* Collect and return all local variables of the for loop represented
1385 * by "tree", with "scop" the corresponding pet_scop.
1386 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1388 * We collect not only the variables that are declared inside "tree",
1389 * but also the loop iterators that are declared anywhere inside
1390 * any possible macro statements in "scop".
1391 * The latter also appear as declared variable in the scop,
1392 * whereas other declared loop iterators only appear implicitly
1393 * in the iteration domains.
1395 static __isl_give isl_union_set
*collect_local(struct pet_scop
*scop
,
1396 __isl_keep pet_tree
*tree
, __isl_keep pet_context
*pc
,
1397 struct pet_state
*state
)
1401 struct pet_tree_collect_local_data data
= { pc
, state
};
1403 ctx
= pet_tree_get_ctx(tree
);
1404 data
.local
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
1406 if (pet_tree_foreach_sub_tree(tree
, &extract_local_var
, &data
) < 0)
1407 return isl_union_set_free(data
.local
);
1409 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1410 pet_tree
*body
= scop
->stmts
[i
]->body
;
1411 if (pet_tree_foreach_sub_tree(body
, &extract_local_iterator
,
1413 return isl_union_set_free(data
.local
);
1419 /* Add an independence to "scop" if the for node "tree" was marked
1421 * "domain" is the set of loop iterators, with the current for loop
1422 * innermost. If "sign" is positive, then the inner iterator increases.
1423 * Otherwise it decreases.
1424 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1426 * If the tree was marked, then collect all local variables and
1427 * add an independence.
1429 static struct pet_scop
*set_independence(struct pet_scop
*scop
,
1430 __isl_keep pet_tree
*tree
, __isl_keep isl_set
*domain
, int sign
,
1431 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1433 isl_union_set
*local
;
1435 if (!tree
->u
.l
.independent
)
1438 local
= collect_local(scop
, tree
, pc
, state
);
1439 scop
= pet_scop_set_independent(scop
, domain
, local
, sign
);
1444 /* Construct a pet_scop for a for tree with static affine initialization
1445 * and constant increment within the context "pc".
1446 * The domain of "pc" has already been extended with an (at this point
1447 * unbounded) inner loop iterator corresponding to the current for loop.
1449 * The condition is allowed to contain nested accesses, provided
1450 * they are not being written to inside the body of the loop.
1451 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1452 * essentially treated as a while loop, with iteration domain
1453 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1455 * We extract a pet_scop for the body after intersecting the domain of "pc"
1457 * { [l,i] : i >= init and condition' }
1461 * { [l,i] : i <= init and condition' }
1463 * Where condition' is equal to condition if the latter is
1464 * a simple upper [lower] bound and a condition that is extended
1465 * to apply to all previous iterations otherwise.
1466 * Afterwards, the schedule of the pet_scop is extended with
1474 * If the condition is non-affine, then we drop the condition from the
1475 * iteration domain and instead create a separate statement
1476 * for evaluating the condition. The body is then filtered to depend
1477 * on the result of the condition evaluating to true on all iterations
1478 * up to the current iteration, while the evaluation the condition itself
1479 * is filtered to depend on the result of the condition evaluating to true
1480 * on all previous iterations.
1481 * The context of the scop representing the body is dropped
1482 * because we don't know how many times the body will be executed,
1485 * If the stride of the loop is not 1, then "i >= init" is replaced by
1487 * (exists a: i = init + stride * a and a >= 0)
1489 * If the loop iterator i is unsigned, then wrapping may occur.
1490 * We therefore use a virtual iterator instead that does not wrap.
1491 * However, the condition in the code applies
1492 * to the wrapped value, so we need to change condition(l,i)
1493 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1494 * to the original iterator by the wrapping of the virtual iterator.
1495 * Note that there may be no need to perform this final wrapping
1496 * if the loop condition (after wrapping) satisfies certain conditions.
1497 * However, the is_simple_bound condition is not enough since it doesn't
1498 * check if there even is an upper bound.
1500 * Wrapping on unsigned iterators can be avoided entirely if
1501 * the loop condition is simple, the loop iterator is incremented
1502 * [decremented] by one and the last value before wrapping cannot
1503 * possibly satisfy the loop condition.
1505 * Valid outer iterators for a for loop are those for which the initial
1506 * value itself, the increment on each domain iteration and
1507 * the condition on both the initial value and
1508 * the result of incrementing the iterator for each iteration of the domain
1510 * If the loop condition is non-affine, then we only consider validity
1511 * of the initial value.
1513 * If the body contains any break, then we keep track of it in "skip"
1514 * (if the skip condition is affine) or it is handled in scop_add_break
1515 * (if the skip condition is not affine).
1516 * Note that the affine break condition needs to be considered with
1517 * respect to previous iterations in the virtual domain (if any).
1519 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1520 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1521 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1522 struct pet_state
*state
)
1525 isl_multi_aff
*sched
;
1526 isl_set
*cond
= NULL
;
1527 isl_set
*skip
= NULL
;
1528 isl_id
*id_test
= NULL
, *id_break_test
;
1529 struct pet_scop
*scop
, *scop_cond
= NULL
;
1536 int has_affine_break
;
1538 isl_map
*rev_wrap
= NULL
;
1539 isl_map
*init_val_map
;
1541 isl_set
*valid_init
;
1542 isl_set
*valid_cond
;
1543 isl_set
*valid_cond_init
;
1544 isl_set
*valid_cond_next
;
1546 pet_expr
*cond_expr
;
1547 pet_context
*pc_nested
;
1549 pos
= pet_context_dim(pc
) - 1;
1551 domain
= pet_context_get_domain(pc
);
1552 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1553 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
1554 pc_nested
= pet_context_copy(pc
);
1555 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1556 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1557 pet_context_free(pc_nested
);
1558 pet_expr_free(cond_expr
);
1560 valid_inc
= isl_pw_aff_domain(pa_inc
);
1562 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1564 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1565 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1567 pa
= isl_pw_aff_free(pa
);
1569 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1570 cond
= isl_pw_aff_non_zero_set(pa
);
1572 cond
= isl_set_universe(isl_set_get_space(domain
));
1574 valid_cond
= isl_set_coalesce(valid_cond
);
1575 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1576 is_virtual
= is_unsigned
&&
1577 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1579 init_val_map
= isl_map_from_pw_aff(isl_pw_aff_copy(init_val
));
1580 init_val_map
= isl_map_equate(init_val_map
, isl_dim_in
, pos
,
1582 valid_cond_init
= enforce_subset(isl_map_domain(init_val_map
),
1583 isl_set_copy(valid_cond
));
1584 if (is_one
&& !is_virtual
) {
1587 isl_pw_aff_free(init_val
);
1588 pa
= pet_expr_extract_comparison(
1589 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1590 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1591 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1592 valid_init
= isl_set_eliminate(valid_init
, isl_dim_set
,
1593 isl_set_dim(domain
, isl_dim_set
) - 1, 1);
1594 cond
= isl_pw_aff_non_zero_set(pa
);
1595 domain
= isl_set_intersect(domain
, cond
);
1599 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1600 strided
= strided_domain(init_val
, isl_val_copy(inc
));
1601 domain
= isl_set_intersect(domain
, strided
);
1605 isl_multi_aff
*wrap
;
1606 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1607 pc
= pet_context_preimage_domain(pc
, wrap
);
1608 rev_wrap
= isl_map_from_multi_aff(wrap
);
1609 rev_wrap
= isl_map_reverse(rev_wrap
);
1610 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1611 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1612 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1614 is_simple
= is_simple_bound(cond
, inc
);
1616 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1617 is_simple
= is_simple_bound(cond
, inc
);
1620 cond
= valid_for_each_iteration(cond
,
1621 isl_set_copy(domain
), isl_val_copy(inc
));
1622 cond
= isl_set_align_params(cond
, isl_set_get_space(domain
));
1623 domain
= isl_set_intersect(domain
, cond
);
1624 sched
= map_to_last(pc
, state
->n_loop
++, tree
->label
);
1625 if (isl_val_is_neg(inc
))
1626 sched
= isl_multi_aff_neg(sched
);
1628 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1630 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1632 pc
= pet_context_intersect_domain(pc
, isl_set_copy(domain
));
1634 if (is_non_affine
) {
1636 isl_multi_pw_aff
*test_index
;
1637 space
= isl_set_get_space(domain
);
1638 test_index
= pet_create_test_index(space
, state
->n_test
++);
1639 scop_cond
= scop_from_non_affine_condition(
1640 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1641 isl_multi_pw_aff_copy(test_index
),
1642 pet_tree_get_loc(tree
), pc
);
1643 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1645 scop_cond
= pet_scop_add_boolean_array(scop_cond
,
1646 isl_set_copy(domain
), test_index
,
1650 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
1651 has_affine_break
= scop
&&
1652 pet_scop_has_affine_skip(scop
, pet_skip_later
);
1653 if (has_affine_break
)
1654 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1655 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
1657 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1658 if (is_non_affine
) {
1659 scop
= pet_scop_reset_context(scop
);
1661 scop
= pet_scop_reset_skips(scop
);
1662 scop
= pet_scop_resolve_nested(scop
);
1663 if (has_affine_break
) {
1664 domain
= apply_affine_break(domain
, skip
, isl_val_sgn(inc
),
1665 is_virtual
, rev_wrap
);
1666 scop
= pet_scop_intersect_domain_prefix(scop
,
1667 isl_set_copy(domain
));
1669 isl_map_free(rev_wrap
);
1671 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
1674 scop
= scop_add_while(scop_cond
, scop
, id_test
,
1675 isl_set_copy(domain
),
1678 scop
= set_independence(scop
, tree
, domain
, isl_val_sgn(inc
),
1680 scop
= pet_scop_embed(scop
, domain
, sched
);
1681 if (is_non_affine
) {
1682 isl_set_free(valid_inc
);
1684 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_next
);
1685 valid_inc
= isl_set_intersect(valid_inc
, valid_cond_init
);
1686 valid_inc
= isl_set_project_out(valid_inc
, isl_dim_set
, pos
, 1);
1687 scop
= pet_scop_restrict_context(scop
, valid_inc
);
1692 valid_init
= isl_set_project_out(valid_init
, isl_dim_set
, pos
, 1);
1693 scop
= pet_scop_restrict_context(scop
, valid_init
);
1695 pet_context_free(pc
);
1699 /* Construct a pet_scop for a for statement within the context of "pc".
1701 * We update the context to reflect the writes to the loop variable and
1702 * the writes inside the body.
1704 * Then we check if the initialization of the for loop
1705 * is a static affine value and the increment is a constant.
1706 * If so, we construct the pet_scop using scop_from_affine_for.
1707 * Otherwise, we treat the for loop as a while loop
1708 * in scop_from_non_affine_for.
1710 * Note that the initialization and the increment are extracted
1711 * in a context where the current loop iterator has been added
1712 * to the context. If these turn out not be affine, then we
1713 * have reconstruct the body context without an assignment
1714 * to this loop iterator, as this variable will then not be
1715 * treated as a dimension of the iteration domain, but as any
1718 static struct pet_scop
*scop_from_for(__isl_keep pet_tree
*tree
,
1719 __isl_keep pet_context
*init_pc
, struct pet_state
*state
)
1723 isl_pw_aff
*pa_inc
, *init_val
;
1724 pet_context
*pc
, *pc_init_val
;
1729 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1730 pc
= pet_context_copy(init_pc
);
1731 pc
= pet_context_add_inner_iterator(pc
, iv
);
1732 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1734 pc_init_val
= pet_context_copy(pc
);
1735 pc_init_val
= pet_context_clear_value(pc_init_val
, isl_id_copy(iv
));
1736 init_val
= pet_expr_extract_affine(tree
->u
.l
.init
, pc_init_val
);
1737 pet_context_free(pc_init_val
);
1738 pa_inc
= pet_expr_extract_affine(tree
->u
.l
.inc
, pc
);
1739 inc
= pet_extract_cst(pa_inc
);
1740 if (!pa_inc
|| !init_val
|| !inc
)
1742 if (!isl_pw_aff_involves_nan(pa_inc
) &&
1743 !isl_pw_aff_involves_nan(init_val
) && !isl_val_is_nan(inc
))
1744 return scop_from_affine_for(tree
, init_val
, pa_inc
, inc
,
1747 isl_pw_aff_free(pa_inc
);
1748 isl_pw_aff_free(init_val
);
1750 pet_context_free(pc
);
1752 pc
= pet_context_copy(init_pc
);
1753 pc
= pet_context_add_infinite_loop(pc
);
1754 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1755 return scop_from_non_affine_for(tree
, init_pc
, pc
, state
);
1757 isl_pw_aff_free(pa_inc
);
1758 isl_pw_aff_free(init_val
);
1760 pet_context_free(pc
);
1764 /* Check whether "expr" is an affine constraint within the context "pc".
1766 static int is_affine_condition(__isl_keep pet_expr
*expr
,
1767 __isl_keep pet_context
*pc
)
1772 pa
= pet_expr_extract_affine_condition(expr
, pc
);
1775 is_affine
= !isl_pw_aff_involves_nan(pa
);
1776 isl_pw_aff_free(pa
);
1781 /* Check if the given if statement is a conditional assignement
1782 * with a non-affine condition.
1784 * In particular we check if "stmt" is of the form
1791 * where the condition is non-affine and a is some array or scalar access.
1793 static int is_conditional_assignment(__isl_keep pet_tree
*tree
,
1794 __isl_keep pet_context
*pc
)
1798 pet_expr
*expr1
, *expr2
;
1800 ctx
= pet_tree_get_ctx(tree
);
1801 if (!pet_options_get_detect_conditional_assignment(ctx
))
1803 if (tree
->type
!= pet_tree_if_else
)
1805 if (tree
->u
.i
.then_body
->type
!= pet_tree_expr
)
1807 if (tree
->u
.i
.else_body
->type
!= pet_tree_expr
)
1809 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1810 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1811 if (pet_expr_get_type(expr1
) != pet_expr_op
)
1813 if (pet_expr_get_type(expr2
) != pet_expr_op
)
1815 if (pet_expr_op_get_type(expr1
) != pet_op_assign
)
1817 if (pet_expr_op_get_type(expr2
) != pet_op_assign
)
1819 expr1
= pet_expr_get_arg(expr1
, 0);
1820 expr2
= pet_expr_get_arg(expr2
, 0);
1821 equal
= pet_expr_is_equal(expr1
, expr2
);
1822 pet_expr_free(expr1
);
1823 pet_expr_free(expr2
);
1824 if (equal
< 0 || !equal
)
1826 if (is_affine_condition(tree
->u
.i
.cond
, pc
))
1832 /* Given that "tree" is of the form
1839 * where a is some array or scalar access, construct a pet_scop
1840 * corresponding to this conditional assignment within the context "pc".
1841 * "cond_pa" is an affine expression with nested accesses representing
1844 * The constructed pet_scop then corresponds to the expression
1846 * a = condition ? f(...) : g(...)
1848 * All access relations in f(...) are intersected with condition
1849 * while all access relation in g(...) are intersected with the complement.
1851 static struct pet_scop
*scop_from_conditional_assignment(
1852 __isl_keep pet_tree
*tree
, __isl_take isl_pw_aff
*cond_pa
,
1853 __isl_take pet_context
*pc
, struct pet_state
*state
)
1856 isl_set
*cond
, *comp
;
1857 isl_multi_pw_aff
*index
;
1858 pet_expr
*expr1
, *expr2
;
1859 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
1860 struct pet_scop
*scop
;
1862 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa
));
1863 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa
));
1864 index
= isl_multi_pw_aff_from_pw_aff(cond_pa
);
1866 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1867 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1869 pe_cond
= pet_expr_from_index(index
);
1871 pe_then
= pet_expr_get_arg(expr1
, 1);
1872 pe_then
= pet_context_evaluate_expr(pc
, pe_then
);
1873 pe_then
= pet_expr_restrict(pe_then
, cond
);
1874 pe_else
= pet_expr_get_arg(expr2
, 1);
1875 pe_else
= pet_context_evaluate_expr(pc
, pe_else
);
1876 pe_else
= pet_expr_restrict(pe_else
, comp
);
1877 pe_write
= pet_expr_get_arg(expr1
, 0);
1878 pe_write
= pet_context_evaluate_expr(pc
, pe_write
);
1880 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
1881 type_size
= pet_expr_get_type_size(pe_write
);
1882 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, pe_write
, pe
);
1884 scop
= scop_from_evaluated_expr(pe
, state
->n_stmt
++,
1885 pet_tree_get_loc(tree
), pc
);
1887 pet_context_free(pc
);
1892 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1894 * We create a separate statement that writes the result
1895 * of the non-affine condition to a virtual scalar.
1896 * A constraint requiring the value of this virtual scalar to be one
1897 * is added to the iteration domains of the then branch.
1898 * Similarly, a constraint requiring the value of this virtual scalar
1899 * to be zero is added to the iteration domains of the else branch, if any.
1900 * We combine the schedules as a sequence to ensure that the virtual scalar
1901 * is written before it is read.
1903 * If there are any breaks or continues in the then and/or else
1904 * branches, then we may have to compute a new skip condition.
1905 * This is handled using a pet_skip_info object.
1906 * On initialization, the object checks if skip conditions need
1907 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1908 * adds them in pet_skip_info_add.
1910 static struct pet_scop
*scop_from_non_affine_if(__isl_keep pet_tree
*tree
,
1911 __isl_take pet_context
*pc
, struct pet_state
*state
)
1916 isl_multi_pw_aff
*test_index
;
1917 struct pet_skip_info skip
;
1918 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1920 has_else
= tree
->type
== pet_tree_if_else
;
1922 space
= pet_context_get_space(pc
);
1923 test_index
= pet_create_test_index(space
, state
->n_test
++);
1924 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
1925 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
1926 pet_tree_get_loc(tree
), pc
);
1927 domain
= pet_context_get_domain(pc
);
1928 scop
= pet_scop_add_boolean_array(scop
, domain
,
1929 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1931 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1933 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1935 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1937 pet_skip_info_if_extract_index(&skip
, test_index
, pc
, state
);
1939 scop_then
= pet_scop_filter(scop_then
,
1940 isl_multi_pw_aff_copy(test_index
), 1);
1942 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1943 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1945 isl_multi_pw_aff_free(test_index
);
1947 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1949 scop
= pet_skip_info_add(&skip
, scop
);
1951 pet_context_free(pc
);
1955 /* Construct a pet_scop for an affine if statement within the context "pc".
1957 * The condition is added to the iteration domains of the then branch,
1958 * while the opposite of the condition in added to the iteration domains
1959 * of the else branch, if any.
1961 * If there are any breaks or continues in the then and/or else
1962 * branches, then we may have to compute a new skip condition.
1963 * This is handled using a pet_skip_info_if object.
1964 * On initialization, the object checks if skip conditions need
1965 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1966 * adds them in pet_skip_info_add.
1968 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1969 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
1970 struct pet_state
*state
)
1974 isl_set
*set
, *complement
;
1976 struct pet_skip_info skip
;
1977 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1978 pet_context
*pc_body
;
1980 ctx
= pet_tree_get_ctx(tree
);
1982 has_else
= tree
->type
== pet_tree_if_else
;
1984 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1985 set
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
1987 pc_body
= pet_context_copy(pc
);
1988 pc_body
= pet_context_intersect_domain(pc_body
, isl_set_copy(set
));
1989 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc_body
, state
);
1990 pet_context_free(pc_body
);
1992 pc_body
= pet_context_copy(pc
);
1993 complement
= isl_set_copy(valid
);
1994 complement
= isl_set_subtract(valid
, isl_set_copy(set
));
1995 pc_body
= pet_context_intersect_domain(pc_body
,
1996 isl_set_copy(complement
));
1997 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc_body
, state
);
1998 pet_context_free(pc_body
);
2001 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
2002 pet_skip_info_if_extract_cond(&skip
, cond
, pc
, state
);
2003 isl_pw_aff_free(cond
);
2005 scop
= pet_scop_restrict(scop_then
, set
);
2008 scop_else
= pet_scop_restrict(scop_else
, complement
);
2009 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
2011 scop
= pet_scop_resolve_nested(scop
);
2012 scop
= pet_scop_restrict_context(scop
, valid
);
2014 scop
= pet_skip_info_add(&skip
, scop
);
2016 pet_context_free(pc
);
2020 /* Construct a pet_scop for an if statement within the context "pc".
2022 * If the condition fits the pattern of a conditional assignment,
2023 * then it is handled by scop_from_conditional_assignment.
2024 * Note that the condition is only considered for a conditional assignment
2025 * if it is not static-affine. However, it should still convert
2026 * to an affine expression when nesting is allowed.
2028 * Otherwise, we check if the condition is affine.
2029 * If so, we construct the scop in scop_from_affine_if.
2030 * Otherwise, we construct the scop in scop_from_non_affine_if.
2032 * We allow the condition to be dynamic, i.e., to refer to
2033 * scalars or array elements that may be written to outside
2034 * of the given if statement. These nested accesses are then represented
2035 * as output dimensions in the wrapping iteration domain.
2036 * If it is also written _inside_ the then or else branch, then
2037 * we treat the condition as non-affine.
2038 * As explained in extract_non_affine_if, this will introduce
2039 * an extra statement.
2040 * For aesthetic reasons, we want this statement to have a statement
2041 * number that is lower than those of the then and else branches.
2042 * In order to evaluate if we will need such a statement, however, we
2043 * first construct scops for the then and else branches.
2044 * We therefore reserve a statement number if we might have to
2045 * introduce such an extra statement.
2047 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
2048 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2052 pet_expr
*cond_expr
;
2053 pet_context
*pc_nested
;
2058 has_else
= tree
->type
== pet_tree_if_else
;
2060 pc
= pet_context_copy(pc
);
2061 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
2063 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
2065 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
2066 cond_expr
= pet_context_evaluate_expr(pc
, cond_expr
);
2067 pc_nested
= pet_context_copy(pc
);
2068 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
2069 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
2070 pet_context_free(pc_nested
);
2071 pet_expr_free(cond_expr
);
2074 pet_context_free(pc
);
2078 if (isl_pw_aff_involves_nan(cond
)) {
2079 isl_pw_aff_free(cond
);
2080 return scop_from_non_affine_if(tree
, pc
, state
);
2083 if (is_conditional_assignment(tree
, pc
))
2084 return scop_from_conditional_assignment(tree
, cond
, pc
, state
);
2086 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
2087 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
2088 isl_pw_aff_free(cond
);
2089 return scop_from_non_affine_if(tree
, pc
, state
);
2092 return scop_from_affine_if(tree
, cond
, pc
, state
);
2095 /* Return a one-dimensional multi piecewise affine expression that is equal
2096 * to the constant 1 and is defined over the given domain.
2098 static __isl_give isl_multi_pw_aff
*one_mpa(__isl_take isl_space
*space
)
2100 isl_local_space
*ls
;
2103 ls
= isl_local_space_from_space(space
);
2104 aff
= isl_aff_zero_on_domain(ls
);
2105 aff
= isl_aff_set_constant_si(aff
, 1);
2107 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
2110 /* Construct a pet_scop for a continue statement with the given domain space.
2112 * We simply create an empty scop with a universal pet_skip_now
2113 * skip condition. This skip condition will then be taken into
2114 * account by the enclosing loop construct, possibly after
2115 * being incorporated into outer skip conditions.
2117 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
,
2118 __isl_take isl_space
*space
)
2120 struct pet_scop
*scop
;
2122 scop
= pet_scop_empty(isl_space_copy(space
));
2124 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(space
));
2129 /* Construct a pet_scop for a break statement with the given domain space.
2131 * We simply create an empty scop with both a universal pet_skip_now
2132 * skip condition and a universal pet_skip_later skip condition.
2133 * These skip conditions will then be taken into
2134 * account by the enclosing loop construct, possibly after
2135 * being incorporated into outer skip conditions.
2137 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
,
2138 __isl_take isl_space
*space
)
2140 struct pet_scop
*scop
;
2141 isl_multi_pw_aff
*skip
;
2143 scop
= pet_scop_empty(isl_space_copy(space
));
2145 skip
= one_mpa(space
);
2146 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
2147 isl_multi_pw_aff_copy(skip
));
2148 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
2153 /* Extract a clone of the kill statement "stmt".
2154 * The domain of the clone is given by "domain".
2156 static struct pet_scop
*extract_kill(__isl_keep isl_set
*domain
,
2157 struct pet_stmt
*stmt
, struct pet_state
*state
)
2161 isl_multi_pw_aff
*mpa
;
2164 if (!domain
|| !stmt
)
2167 kill
= pet_tree_expr_get_expr(stmt
->body
);
2168 space
= pet_stmt_get_space(stmt
);
2169 space
= isl_space_map_from_set(space
);
2170 mpa
= isl_multi_pw_aff_identity(space
);
2171 mpa
= isl_multi_pw_aff_reset_tuple_id(mpa
, isl_dim_in
);
2172 kill
= pet_expr_update_domain(kill
, mpa
);
2173 tree
= pet_tree_new_expr(kill
);
2174 tree
= pet_tree_set_loc(tree
, pet_loc_copy(stmt
->loc
));
2175 stmt
= pet_stmt_from_pet_tree(isl_set_copy(domain
),
2176 state
->n_stmt
++, tree
);
2177 return pet_scop_from_pet_stmt(isl_set_get_space(domain
), stmt
);
2180 /* Extract a clone of the kill statements in "scop".
2181 * The domain of each clone is given by "domain".
2182 * "scop" is expected to have been created from a DeclStmt
2183 * and should have (one of) the kill(s) as its first statement.
2184 * If "scop" was created from a declaration group, then there
2185 * may be multiple kill statements inside.
2187 static struct pet_scop
*extract_kills(__isl_keep isl_set
*domain
,
2188 struct pet_scop
*scop
, struct pet_state
*state
)
2191 struct pet_stmt
*stmt
;
2192 struct pet_scop
*kill
;
2195 if (!domain
|| !scop
)
2197 ctx
= isl_set_get_ctx(domain
);
2198 if (scop
->n_stmt
< 1)
2199 isl_die(ctx
, isl_error_internal
,
2200 "expecting at least one statement", return NULL
);
2201 stmt
= scop
->stmts
[0];
2202 if (!pet_stmt_is_kill(stmt
))
2203 isl_die(ctx
, isl_error_internal
,
2204 "expecting kill statement", return NULL
);
2206 kill
= extract_kill(domain
, stmt
, state
);
2208 for (i
= 1; i
< scop
->n_stmt
; ++i
) {
2209 struct pet_scop
*kill_i
;
2211 stmt
= scop
->stmts
[i
];
2212 if (!pet_stmt_is_kill(stmt
))
2215 kill_i
= extract_kill(domain
, stmt
, state
);
2216 kill
= pet_scop_add_par(ctx
, kill
, kill_i
);
2222 /* Has "tree" been created from a DeclStmt?
2223 * That is, is it either a declaration or a group of declarations?
2225 static int tree_is_decl(__isl_keep pet_tree
*tree
)
2232 is_decl
= pet_tree_is_decl(tree
);
2233 if (is_decl
< 0 || is_decl
)
2236 if (tree
->type
!= pet_tree_block
)
2238 if (pet_tree_block_get_block(tree
))
2241 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
2242 is_decl
= tree_is_decl(tree
->u
.b
.child
[i
]);
2243 if (is_decl
< 0 || !is_decl
)
2250 /* Does "tree" represent an assignment to a variable?
2252 * The assignment may be one of
2253 * - a declaration with initialization
2254 * - an expression with a top-level assignment operator
2256 static int is_assignment(__isl_keep pet_tree
*tree
)
2260 if (tree
->type
== pet_tree_decl_init
)
2262 return pet_tree_is_assign(tree
);
2265 /* Update "pc" by taking into account the assignment performed by "tree",
2266 * where "tree" satisfies is_assignment.
2268 * In particular, if the lhs of the assignment is a scalar variable and
2269 * if the rhs is an affine expression, then keep track of this value in "pc"
2270 * so that we can plug it in when we later come across the same variable.
2272 * Any previously assigned value to the variable has already been removed
2273 * by scop_handle_writes.
2275 static __isl_give pet_context
*handle_assignment(__isl_take pet_context
*pc
,
2276 __isl_keep pet_tree
*tree
)
2278 pet_expr
*var
, *val
;
2282 if (pet_tree_get_type(tree
) == pet_tree_decl_init
) {
2283 var
= pet_tree_decl_get_var(tree
);
2284 val
= pet_tree_decl_get_init(tree
);
2287 expr
= pet_tree_expr_get_expr(tree
);
2288 var
= pet_expr_get_arg(expr
, 0);
2289 val
= pet_expr_get_arg(expr
, 1);
2290 pet_expr_free(expr
);
2293 if (!pet_expr_is_scalar_access(var
)) {
2299 pa
= pet_expr_extract_affine(val
, pc
);
2301 pc
= pet_context_free(pc
);
2303 if (!isl_pw_aff_involves_nan(pa
)) {
2304 id
= pet_expr_access_get_id(var
);
2305 pc
= pet_context_set_value(pc
, id
, pa
);
2307 isl_pw_aff_free(pa
);
2315 /* Mark all arrays in "scop" as being exposed.
2317 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
2323 for (i
= 0; i
< scop
->n_array
; ++i
)
2324 scop
->arrays
[i
]->exposed
= 1;
2328 /* Try and construct a pet_scop corresponding to (part of)
2329 * a sequence of statements within the context "pc".
2331 * After extracting a statement, we update "pc"
2332 * based on the top-level assignments in the statement
2333 * so that we can exploit them in subsequent statements in the same block.
2335 * If there are any breaks or continues in the individual statements,
2336 * then we may have to compute a new skip condition.
2337 * This is handled using a pet_skip_info object.
2338 * On initialization, the object checks if skip conditions need
2339 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2340 * adds them in pet_skip_info_add.
2342 * If "block" is set, then we need to insert kill statements at
2343 * the end of the block for any array that has been declared by
2344 * one of the statements in the sequence. Each of these declarations
2345 * results in the construction of a kill statement at the place
2346 * of the declaration, so we simply collect duplicates of
2347 * those kill statements and append these duplicates to the constructed scop.
2349 * If "block" is not set, then any array declared by one of the statements
2350 * in the sequence is marked as being exposed.
2352 * If autodetect is set, then we allow the extraction of only a subrange
2353 * of the sequence of statements. However, if there is at least one statement
2354 * for which we could not construct a scop and the final range contains
2355 * either no statements or at least one kill, then we discard the entire
2358 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
2359 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2365 struct pet_scop
*scop
, *kills
;
2367 ctx
= pet_tree_get_ctx(tree
);
2369 space
= pet_context_get_space(pc
);
2370 domain
= pet_context_get_domain(pc
);
2371 pc
= pet_context_copy(pc
);
2372 scop
= pet_scop_empty(isl_space_copy(space
));
2373 kills
= pet_scop_empty(space
);
2374 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
2375 struct pet_scop
*scop_i
;
2377 if (pet_scop_has_affine_skip(scop
, pet_skip_now
))
2378 pc
= apply_affine_continue(pc
, scop
);
2379 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
2380 pc
= scop_handle_writes(scop_i
, pc
);
2381 if (is_assignment(tree
->u
.b
.child
[i
]))
2382 pc
= handle_assignment(pc
, tree
->u
.b
.child
[i
]);
2383 struct pet_skip_info skip
;
2384 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
2385 pet_skip_info_seq_extract(&skip
, pc
, state
);
2386 if (scop_i
&& tree_is_decl(tree
->u
.b
.child
[i
])) {
2387 if (tree
->u
.b
.block
) {
2388 struct pet_scop
*kill
;
2389 kill
= extract_kills(domain
, scop_i
, state
);
2390 kills
= pet_scop_add_par(ctx
, kills
, kill
);
2392 scop_i
= mark_exposed(scop_i
);
2394 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
2396 scop
= pet_skip_info_add(&skip
, scop
);
2401 isl_set_free(domain
);
2403 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
2405 pet_context_free(pc
);
2410 /* Internal data structure for extract_declared_arrays.
2412 * "pc" and "state" are used to create pet_array objects and kill statements.
2413 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2414 * found any declared array.
2415 * "scop" has been initialized by the caller and is used to attach
2416 * the created pet_array objects.
2417 * "kill_before" and "kill_after" are created and updated by
2418 * extract_declared_arrays to collect the kills of the arrays.
2420 struct pet_tree_extract_declared_arrays_data
{
2422 struct pet_state
*state
;
2427 struct pet_scop
*scop
;
2428 struct pet_scop
*kill_before
;
2429 struct pet_scop
*kill_after
;
2432 /* Check if the node "node" declares any array or scalar.
2433 * If so, create the corresponding pet_array and attach it to data->scop.
2434 * Additionally, create two kill statements for the array and add them
2435 * to data->kill_before and data->kill_after.
2437 static int extract_declared_arrays(__isl_keep pet_tree
*node
, void *user
)
2439 enum pet_tree_type type
;
2440 struct pet_tree_extract_declared_arrays_data
*data
= user
;
2441 struct pet_array
*array
;
2442 struct pet_scop
*scop_kill
;
2445 type
= pet_tree_get_type(node
);
2446 if (type
== pet_tree_decl
|| type
== pet_tree_decl_init
)
2447 var
= node
->u
.d
.var
;
2448 else if (type
== pet_tree_for
&& node
->u
.l
.declared
)
2453 array
= extract_array(var
, data
->pc
, data
->state
);
2455 array
->declared
= 1;
2456 data
->scop
= pet_scop_add_array(data
->scop
, array
);
2458 scop_kill
= kill(pet_tree_get_loc(node
), array
, data
->pc
, data
->state
);
2460 data
->kill_before
= scop_kill
;
2462 data
->kill_before
= pet_scop_add_par(data
->ctx
,
2463 data
->kill_before
, scop_kill
);
2465 scop_kill
= kill(pet_tree_get_loc(node
), array
, data
->pc
, data
->state
);
2467 data
->kill_after
= scop_kill
;
2469 data
->kill_after
= pet_scop_add_par(data
->ctx
,
2470 data
->kill_after
, scop_kill
);
2477 /* Convert a pet_tree that consists of more than a single leaf
2478 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2479 * Do so within the context of "pc".
2481 * After constructing the core scop, we also look for any arrays (or scalars)
2482 * that are declared inside "tree". Each of those arrays is marked as
2483 * having been declared and kill statements for these arrays
2484 * are introduced before and after the core scop.
2485 * Note that the input tree is not a leaf so that the declaration
2486 * cannot occur at the outer level.
2488 static struct pet_scop
*scop_from_tree_macro(__isl_take pet_tree
*tree
,
2489 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2491 struct pet_tree_extract_declared_arrays_data data
= { pc
, state
};
2493 data
.scop
= scop_from_unevaluated_tree(pet_tree_copy(tree
),
2494 state
->n_stmt
++, pc
);
2497 data
.ctx
= pet_context_get_ctx(pc
);
2498 if (pet_tree_foreach_sub_tree(tree
, &extract_declared_arrays
,
2500 data
.scop
= pet_scop_free(data
.scop
);
2501 pet_tree_free(tree
);
2506 data
.scop
= pet_scop_add_seq(data
.ctx
, data
.kill_before
, data
.scop
);
2507 data
.scop
= pet_scop_add_seq(data
.ctx
, data
.scop
, data
.kill_after
);
2512 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2513 * within the context "pc" by calling the appropriate function
2514 * based on the type of "tree".
2516 * If the initially constructed pet_scop turns out to involve
2517 * dynamic control and if the user has requested an encapsulation
2518 * of all dynamic control, then this pet_scop is discarded and
2519 * a new pet_scop is created with a single statement representing
2520 * the entire "tree".
2521 * However, if the scop contains any active continue or break,
2522 * then we need to include the loop containing the continue or break
2523 * in the encapsulation. We therefore postpone the encapsulation
2524 * until we have constructed a pet_scop for this enclosing loop.
2526 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
2527 __isl_keep pet_context
*pc
, struct pet_state
*state
)
2530 struct pet_scop
*scop
= NULL
;
2535 ctx
= pet_tree_get_ctx(tree
);
2536 switch (tree
->type
) {
2537 case pet_tree_error
:
2539 case pet_tree_block
:
2540 return scop_from_block(tree
, pc
, state
);
2541 case pet_tree_break
:
2542 return scop_from_break(tree
, pet_context_get_space(pc
));
2543 case pet_tree_continue
:
2544 return scop_from_continue(tree
, pet_context_get_space(pc
));
2546 case pet_tree_decl_init
:
2547 return scop_from_decl(tree
, pc
, state
);
2549 return scop_from_tree_expr(tree
, pc
, state
);
2551 case pet_tree_if_else
:
2552 scop
= scop_from_if(tree
, pc
, state
);
2555 scop
= scop_from_for(tree
, pc
, state
);
2557 case pet_tree_while
:
2558 scop
= scop_from_while(tree
, pc
, state
);
2560 case pet_tree_infinite_loop
:
2561 scop
= scop_from_infinite_for(tree
, pc
, state
);
2568 if (!pet_options_get_encapsulate_dynamic_control(ctx
) ||
2569 !pet_scop_has_data_dependent_conditions(scop
) ||
2570 pet_scop_has_var_skip(scop
, pet_skip_now
))
2573 pet_scop_free(scop
);
2574 return scop_from_tree_macro(pet_tree_copy(tree
), pc
, state
);
2577 /* If "tree" has a label that is of the form S_<nr>, then make
2578 * sure that state->n_stmt is greater than nr to ensure that
2579 * we will not generate S_<nr> ourselves.
2581 static int set_first_stmt(__isl_keep pet_tree
*tree
, void *user
)
2583 struct pet_state
*state
= user
;
2591 name
= isl_id_get_name(tree
->label
);
2592 if (strncmp(name
, "S_", 2) != 0)
2594 nr
= atoi(name
+ 2);
2595 if (nr
>= state
->n_stmt
)
2596 state
->n_stmt
= nr
+ 1;
2601 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2602 * "int_size" is the number of bytes need to represent an integer.
2603 * "extract_array" is a callback that we can use to create a pet_array
2604 * that corresponds to the variable accessed by an expression.
2606 * Initialize the global state, construct a context and then
2607 * construct the pet_scop by recursively visiting the tree.
2609 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2611 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
2612 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
2613 __isl_keep pet_context
*pc
, void *user
), void *user
,
2614 __isl_keep pet_context
*pc
)
2616 struct pet_scop
*scop
;
2617 struct pet_state state
= { 0 };
2622 state
.ctx
= pet_tree_get_ctx(tree
);
2623 state
.int_size
= int_size
;
2624 state
.extract_array
= extract_array
;
2626 if (pet_tree_foreach_sub_tree(tree
, &set_first_stmt
, &state
) < 0)
2627 tree
= pet_tree_free(tree
);
2629 scop
= scop_from_tree(tree
, pc
, &state
);
2630 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
2632 pet_tree_free(tree
);
2635 scop
->context
= isl_set_params(scop
->context
);