2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
35 #include <isl/id_to_pw_aff.h>
44 #include "tree2scop.h"
46 /* Update "pc" by taking into account the writes in "stmt".
47 * That is, first mark all scalar variables that are written by "stmt"
48 * as having an unknown value. Afterwards,
49 * if "stmt" is a top-level (i.e., unconditional) assignment
50 * to a scalar variable, then update "pc" accordingly.
52 * In particular, if the lhs of the assignment is a scalar variable, then mark
53 * the variable as having been assigned. If, furthermore, the rhs
54 * is an affine expression, then keep track of this value in "pc"
55 * so that we can plug it in when we later come across the same variable.
57 * We skip assignments to virtual arrays (those with NULL user pointer).
59 static __isl_give pet_context
*handle_writes(struct pet_stmt
*stmt
,
60 __isl_take pet_context
*pc
)
62 pet_expr
*body
= stmt
->body
;
67 pc
= pet_context_clear_writes_in_expr(pc
, body
);
71 if (pet_expr_get_type(body
) != pet_expr_op
)
73 if (pet_expr_op_get_type(body
) != pet_op_assign
)
75 if (!isl_set_plain_is_universe(stmt
->domain
))
77 arg
= pet_expr_get_arg(body
, 0);
78 if (!pet_expr_is_scalar_access(arg
)) {
83 id
= pet_expr_access_get_id(arg
);
86 if (!isl_id_get_user(id
)) {
91 arg
= pet_expr_get_arg(body
, 1);
92 pa
= pet_expr_extract_affine(arg
, pc
);
93 pc
= pet_context_mark_assigned(pc
, isl_id_copy(id
));
96 if (pa
&& isl_pw_aff_involves_nan(pa
)) {
102 pc
= pet_context_set_value(pc
, id
, pa
);
107 /* Update "pc" based on the write accesses (and, in particular,
108 * assignments) in "scop".
110 static __isl_give pet_context
*scop_handle_writes(struct pet_scop
*scop
,
111 __isl_take pet_context
*pc
)
116 return pet_context_free(pc
);
117 for (i
= 0; i
< scop
->n_stmt
; ++i
)
118 pc
= handle_writes(scop
->stmts
[i
], pc
);
123 /* Convert a top-level pet_expr to a pet_scop with one statement
124 * within the context "pc".
125 * This mainly involves resolving nested expression parameters
126 * and setting the name of the iteration space.
127 * The name is given by "label" if it is non-NULL. Otherwise,
128 * it is of the form S_<stmt_nr>.
129 * The location of the statement is set to "loc".
131 static struct pet_scop
*scop_from_expr(__isl_take pet_expr
*expr
,
132 __isl_take isl_id
*label
, int stmt_nr
, __isl_take pet_loc
*loc
,
133 __isl_keep pet_context
*pc
)
138 ctx
= pet_expr_get_ctx(expr
);
140 expr
= pet_expr_plug_in_args(expr
, pc
);
141 expr
= pet_expr_resolve_nested(expr
);
142 expr
= pet_expr_resolve_assume(expr
, pc
);
143 ps
= pet_stmt_from_pet_expr(loc
, label
, stmt_nr
, expr
);
144 return pet_scop_from_pet_stmt(ctx
, ps
);
147 /* Construct a pet_scop with a single statement killing the entire
149 * The location of the statement is set to "loc".
151 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
152 __isl_keep pet_context
*pc
, struct pet_state
*state
)
157 isl_multi_pw_aff
*index
;
160 struct pet_scop
*scop
;
164 ctx
= isl_set_get_ctx(array
->extent
);
165 access
= isl_map_from_range(isl_set_copy(array
->extent
));
166 id
= isl_set_get_tuple_id(array
->extent
);
167 space
= isl_space_alloc(ctx
, 0, 0, 0);
168 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
169 index
= isl_multi_pw_aff_zero(space
);
170 expr
= pet_expr_kill_from_access_and_index(access
, index
);
171 return scop_from_expr(expr
, NULL
, state
->n_stmt
++, loc
, pc
);
177 /* Construct and return a pet_array corresponding to the variable
178 * accessed by "access" by calling the extract_array callback.
180 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
181 __isl_keep pet_context
*pc
, struct pet_state
*state
)
183 return state
->extract_array(access
, pc
, state
->user
);
186 /* Construct a pet_scop for a (single) variable declaration
187 * within the context "pc".
189 * The scop contains the variable being declared (as an array)
190 * and a statement killing the array.
192 * If the declaration comes with an initialization, then the scop
193 * also contains an assignment to the variable.
195 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
196 __isl_keep pet_context
*pc
, struct pet_state
*state
)
200 struct pet_array
*array
;
201 struct pet_scop
*scop_decl
, *scop
;
202 pet_expr
*lhs
, *rhs
, *pe
;
204 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
207 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
208 scop_decl
= pet_scop_add_array(scop_decl
, array
);
210 if (tree
->type
!= pet_tree_decl_init
)
213 lhs
= pet_expr_copy(tree
->u
.d
.var
);
214 rhs
= pet_expr_copy(tree
->u
.d
.init
);
215 type_size
= pet_expr_get_type_size(lhs
);
216 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
217 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
218 pet_tree_get_loc(tree
), pc
);
220 scop_decl
= pet_scop_prefix(scop_decl
, 0);
221 scop
= pet_scop_prefix(scop
, 1);
223 ctx
= pet_tree_get_ctx(tree
);
224 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
229 /* Embed the given iteration domain in an extra outer loop
230 * with induction variable "var".
231 * If this variable appeared as a parameter in the constraints,
232 * it is replaced by the new outermost dimension.
234 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
235 __isl_take isl_id
*var
)
239 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
240 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
242 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
243 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
250 /* Return those elements in the space of "cond" that come after
251 * (based on "sign") an element in "cond".
253 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
255 isl_map
*previous_to_this
;
258 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
260 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
262 cond
= isl_set_apply(cond
, previous_to_this
);
267 /* Remove those iterations of "domain" that have an earlier iteration
268 * (based on "sign") where "skip" is satisfied.
269 * "domain" has an extra outer loop compared to "skip".
270 * The skip condition is first embedded in the same space as "domain".
271 * If "apply_skip_map" is set, then "skip_map" is first applied
272 * to the embedded skip condition before removing it from the domain.
274 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
275 __isl_take isl_set
*skip
, int sign
,
276 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
278 skip
= embed(skip
, isl_set_get_dim_id(domain
, isl_dim_set
, 0));
280 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
281 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
282 return isl_set_subtract(domain
, after(skip
, sign
));
285 /* Create the infinite iteration domain
289 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
)
291 isl_ctx
*ctx
= isl_id_get_ctx(id
);
294 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
295 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
300 /* Create an identity affine expression on the space containing "domain",
301 * which is assumed to be one-dimensional.
303 static __isl_give isl_aff
*identity_aff(__isl_keep isl_set
*domain
)
307 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
308 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
311 /* Create an affine expression that maps elements
312 * of a single-dimensional array "id_test" to the previous element
313 * (according to "inc"), provided this element belongs to "domain".
314 * That is, create the affine expression
316 * { id[x] -> id[x - inc] : x - inc in domain }
318 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
319 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
324 isl_multi_pw_aff
*prev
;
326 space
= isl_set_get_space(domain
);
327 ls
= isl_local_space_from_space(space
);
328 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
329 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
330 prev
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
331 domain
= isl_set_preimage_multi_pw_aff(domain
,
332 isl_multi_pw_aff_copy(prev
));
333 prev
= isl_multi_pw_aff_intersect_domain(prev
, domain
);
334 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
339 /* Add an implication to "scop" expressing that if an element of
340 * virtual array "id_test" has value "satisfied" then all previous elements
341 * of this array also have that value. The set of previous elements
342 * is bounded by "domain". If "sign" is negative then the iterator
343 * is decreasing and we express that all subsequent array elements
344 * (but still defined previously) have the same value.
346 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
347 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
353 domain
= isl_set_set_tuple_id(domain
, id_test
);
354 space
= isl_set_get_space(domain
);
356 map
= isl_map_lex_ge(space
);
358 map
= isl_map_lex_le(space
);
359 map
= isl_map_intersect_range(map
, domain
);
360 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
365 /* Add a filter to "scop" that imposes that it is only executed
366 * when the variable identified by "id_test" has a zero value
367 * for all previous iterations of "domain".
369 * In particular, add a filter that imposes that the array
370 * has a zero value at the previous iteration of domain and
371 * add an implication that implies that it then has that
372 * value for all previous iterations.
374 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
375 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
376 __isl_take isl_val
*inc
)
378 isl_multi_pw_aff
*prev
;
379 int sign
= isl_val_sgn(inc
);
381 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
382 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
383 scop
= pet_scop_filter(scop
, prev
, 0);
388 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
389 __isl_keep pet_context
*pc
, struct pet_state
*state
);
391 /* Construct a pet_scop for an infinite loop around the given body
392 * within the context "pc".
394 * We extract a pet_scop for the body and then embed it in a loop with
403 * If the body contains any break, then it is taken into
404 * account in apply_affine_break (if the skip condition is affine)
405 * or in scop_add_break (if the skip condition is not affine).
407 * Note that in case of an affine skip condition,
408 * since we are dealing with a loop without loop iterator,
409 * the skip condition cannot refer to the current loop iterator and
410 * so effectively, the iteration domain is of the form
412 * { [0]; [t] : t >= 1 and not skip }
414 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
415 __isl_keep pet_context
*pc
, struct pet_state
*state
)
418 isl_id
*id
, *id_test
;
422 struct pet_scop
*scop
;
423 int has_affine_break
;
426 ctx
= pet_tree_get_ctx(body
);
427 id
= isl_id_alloc(ctx
, "t", NULL
);
428 domain
= infinite_domain(isl_id_copy(id
));
429 ident
= identity_aff(domain
);
431 scop
= scop_from_tree(body
, pc
, state
);
433 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
434 if (has_affine_break
)
435 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
436 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
438 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
440 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
441 isl_aff_copy(ident
), ident
, id
);
442 if (has_affine_break
) {
443 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
444 scop
= pet_scop_intersect_domain_prefix(scop
,
445 isl_set_copy(domain
));
448 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
450 isl_set_free(domain
);
455 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
460 * within the context "pc".
462 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
463 __isl_keep pet_context
*pc
, struct pet_state
*state
)
465 struct pet_scop
*scop
;
467 pc
= pet_context_copy(pc
);
468 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
470 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
472 pet_context_free(pc
);
477 /* Construct a pet_scop for a while loop of the form
482 * within the context "pc".
483 * In particular, construct a scop for an infinite loop around body and
484 * intersect the domain with the affine expression.
485 * Note that this intersection may result in an empty loop.
487 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
488 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
489 struct pet_state
*state
)
491 struct pet_scop
*scop
;
495 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
496 dom
= isl_pw_aff_non_zero_set(pa
);
497 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
498 scop
= pet_scop_restrict(scop
, isl_set_params(dom
));
499 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
501 pet_context_free(pc
);
505 /* Construct a scop for a while, given the scops for the condition
506 * and the body, the filter identifier and the iteration domain of
509 * In particular, the scop for the condition is filtered to depend
510 * on "id_test" evaluating to true for all previous iterations
511 * of the loop, while the scop for the body is filtered to depend
512 * on "id_test" evaluating to true for all iterations up to the
514 * The actual filter only imposes that this virtual array has
515 * value one on the previous or the current iteration.
516 * The fact that this condition also applies to the previous
517 * iterations is enforced by an implication.
519 * These filtered scops are then combined into a single scop.
521 * "sign" is positive if the iterator increases and negative
524 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
525 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
526 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
528 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
530 isl_multi_pw_aff
*test_index
;
531 isl_multi_pw_aff
*prev
;
532 int sign
= isl_val_sgn(inc
);
533 struct pet_scop
*scop
;
535 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
536 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
538 space
= isl_space_map_from_set(isl_set_get_space(domain
));
539 test_index
= isl_multi_pw_aff_identity(space
);
540 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
541 isl_id_copy(id_test
));
542 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
544 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
545 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
550 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
551 * evaluating "cond" and writing the result to a virtual scalar,
552 * as expressed by "index".
553 * Do so within the context "pc".
554 * The location of the statement is set to "loc".
556 static struct pet_scop
*scop_from_non_affine_condition(
557 __isl_take pet_expr
*cond
, int stmt_nr
,
558 __isl_take isl_multi_pw_aff
*index
,
559 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
561 pet_expr
*expr
, *write
;
563 write
= pet_expr_from_index(index
);
564 write
= pet_expr_access_set_write(write
, 1);
565 write
= pet_expr_access_set_read(write
, 0);
566 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
568 return scop_from_expr(expr
, NULL
, stmt_nr
, loc
, pc
);
571 /* Construct a generic while scop, with iteration domain
572 * { [t] : t >= 0 } around "scop_body" within the context "pc".
573 * The scop consists of two parts,
574 * one for evaluating the condition "cond" and one for the body.
575 * "test_nr" is the sequence number of the virtual test variable that contains
576 * the result of the condition and "stmt_nr" is the sequence number
577 * of the statement that evaluates the condition.
578 * If "scop_inc" is not NULL, then it is added at the end of the body,
579 * after replacing any skip conditions resulting from continue statements
580 * by the skip conditions resulting from break statements (if any).
582 * The schedule is adjusted to reflect that the condition is evaluated
583 * before the body is executed and the body is filtered to depend
584 * on the result of the condition evaluating to true on all iterations
585 * up to the current iteration, while the evaluation of the condition itself
586 * is filtered to depend on the result of the condition evaluating to true
587 * on all previous iterations.
588 * The context of the scop representing the body is dropped
589 * because we don't know how many times the body will be executed,
592 * If the body contains any break, then it is taken into
593 * account in apply_affine_break (if the skip condition is affine)
594 * or in scop_add_break (if the skip condition is not affine).
596 * Note that in case of an affine skip condition,
597 * since we are dealing with a loop without loop iterator,
598 * the skip condition cannot refer to the current loop iterator and
599 * so effectively, the iteration domain is of the form
601 * { [0]; [t] : t >= 1 and not skip }
603 static struct pet_scop
*scop_from_non_affine_while(__isl_take pet_expr
*cond
,
604 int test_nr
, int stmt_nr
, __isl_take pet_loc
*loc
,
605 struct pet_scop
*scop_body
, struct pet_scop
*scop_inc
,
606 __isl_take pet_context
*pc
, struct pet_state
*state
)
609 isl_id
*id
, *id_test
, *id_break_test
;
610 isl_multi_pw_aff
*test_index
;
614 struct pet_scop
*scop
;
615 int has_affine_break
;
619 test_index
= pet_create_test_index(ctx
, test_nr
);
620 scop
= scop_from_non_affine_condition(cond
, stmt_nr
,
621 isl_multi_pw_aff_copy(test_index
), loc
, pc
);
622 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
623 scop
= pet_scop_add_boolean_array(scop
, test_index
, state
->int_size
);
625 id
= isl_id_alloc(ctx
, "t", NULL
);
626 domain
= infinite_domain(isl_id_copy(id
));
627 ident
= identity_aff(domain
);
629 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
630 if (has_affine_break
)
631 skip
= pet_scop_get_affine_skip_domain(scop_body
,
633 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
635 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
637 scop
= pet_scop_prefix(scop
, 0);
638 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_aff_copy(ident
),
639 isl_aff_copy(ident
), isl_id_copy(id
));
640 scop_body
= pet_scop_reset_context(scop_body
);
641 scop_body
= pet_scop_prefix(scop_body
, 1);
643 scop_inc
= pet_scop_prefix(scop_inc
, 2);
644 if (pet_scop_has_skip(scop_body
, pet_skip_later
)) {
645 isl_multi_pw_aff
*skip
;
646 skip
= pet_scop_get_skip(scop_body
, pet_skip_later
);
647 scop_body
= pet_scop_set_skip(scop_body
,
650 pet_scop_reset_skip(scop_body
, pet_skip_now
);
651 scop_body
= pet_scop_add_seq(ctx
, scop_body
, scop_inc
);
653 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
654 isl_aff_copy(ident
), ident
, id
);
656 if (has_affine_break
) {
657 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
658 scop
= pet_scop_intersect_domain_prefix(scop
,
659 isl_set_copy(domain
));
660 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
661 isl_set_copy(domain
));
664 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
665 isl_set_copy(domain
), isl_val_one(ctx
));
666 scop_body
= scop_add_break(scop_body
, id_break_test
,
667 isl_set_copy(domain
), isl_val_one(ctx
));
669 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
672 pet_context_free(pc
);
676 /* Check if the while loop is of the form
678 * while (affine expression)
681 * If so, call scop_from_affine_while to construct a scop.
683 * Otherwise, extract the body and pass control to scop_from_non_affine_while
684 * to extend the iteration domain with an infinite loop.
686 * "pc" is the context in which the affine expressions in the scop are created.
688 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
689 __isl_keep pet_context
*pc
, struct pet_state
*state
)
692 int test_nr
, stmt_nr
;
694 struct pet_scop
*scop_body
;
699 pc
= pet_context_copy(pc
);
700 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
702 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
703 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
704 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
705 pet_expr_free(cond_expr
);
710 if (!isl_pw_aff_involves_nan(pa
))
711 return scop_from_affine_while(tree
, pa
, pc
, state
);
713 test_nr
= state
->n_test
++;
714 stmt_nr
= state
->n_stmt
++;
715 scop_body
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
716 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
717 test_nr
, stmt_nr
, pet_tree_get_loc(tree
),
718 scop_body
, NULL
, pc
, state
);
720 pet_context_free(pc
);
724 /* Check whether "cond" expresses a simple loop bound
725 * on the only set dimension.
726 * In particular, if "up" is set then "cond" should contain only
727 * upper bounds on the set dimension.
728 * Otherwise, it should contain only lower bounds.
730 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
732 if (isl_val_is_pos(inc
))
733 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
735 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
738 /* Extend a condition on a given iteration of a loop to one that
739 * imposes the same condition on all previous iterations.
740 * "domain" expresses the lower [upper] bound on the iterations
741 * when inc is positive [negative].
743 * In particular, we construct the condition (when inc is positive)
745 * forall i' : (domain(i') and i' <= i) => cond(i')
747 * which is equivalent to
749 * not exists i' : domain(i') and i' <= i and not cond(i')
751 * We construct this set by negating cond, applying a map
753 * { [i'] -> [i] : domain(i') and i' <= i }
755 * and then negating the result again.
757 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
758 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
760 isl_map
*previous_to_this
;
762 if (isl_val_is_pos(inc
))
763 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
765 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
767 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
769 cond
= isl_set_complement(cond
);
770 cond
= isl_set_apply(cond
, previous_to_this
);
771 cond
= isl_set_complement(cond
);
778 /* Construct a domain of the form
780 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
782 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
783 __isl_take isl_pw_aff
*init
, __isl_take isl_val
*inc
)
789 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
790 dim
= isl_pw_aff_get_domain_space(init
);
791 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
792 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, 0, inc
);
793 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
795 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
796 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
797 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
798 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
800 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
802 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
804 return isl_set_params(set
);
807 /* Assuming "cond" represents a bound on a loop where the loop
808 * iterator "iv" is incremented (or decremented) by one, check if wrapping
811 * Under the given assumptions, wrapping is only possible if "cond" allows
812 * for the last value before wrapping, i.e., 2^width - 1 in case of an
813 * increasing iterator and 0 in case of a decreasing iterator.
815 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
816 __isl_keep isl_val
*inc
)
823 test
= isl_set_copy(cond
);
825 ctx
= isl_set_get_ctx(test
);
826 if (isl_val_is_neg(inc
))
827 limit
= isl_val_zero(ctx
);
829 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
830 limit
= isl_val_2exp(limit
);
831 limit
= isl_val_sub_ui(limit
, 1);
834 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
835 cw
= !isl_set_is_empty(test
);
841 /* Given a one-dimensional space, construct the following affine expression
844 * { [v] -> [v mod 2^width] }
846 * where width is the number of bits used to represent the values
847 * of the unsigned variable "iv".
849 static __isl_give isl_aff
*compute_wrapping(__isl_take isl_space
*dim
,
850 __isl_keep pet_expr
*iv
)
856 ctx
= isl_space_get_ctx(dim
);
857 mod
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
858 mod
= isl_val_2exp(mod
);
860 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
861 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
862 aff
= isl_aff_mod_val(aff
, mod
);
867 /* Project out the parameter "id" from "set".
869 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
870 __isl_keep isl_id
*id
)
874 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
876 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
881 /* Compute the set of parameters for which "set1" is a subset of "set2".
883 * set1 is a subset of set2 if
885 * forall i in set1 : i in set2
889 * not exists i in set1 and i not in set2
893 * not exists i in set1 \ set2
895 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
896 __isl_take isl_set
*set2
)
898 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
901 /* Compute the set of parameter values for which "cond" holds
902 * on the next iteration for each element of "dom".
904 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
905 * and then compute the set of parameters for which the result is a subset
908 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
909 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
915 space
= isl_set_get_space(dom
);
916 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
917 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
918 aff
= isl_aff_add_constant_val(aff
, inc
);
919 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
921 dom
= isl_set_apply(dom
, next
);
923 return enforce_subset(dom
, cond
);
926 /* Extract the for loop "tree" as a while loop within the context "pc".
928 * That is, the for loop has the form
930 * for (iv = init; cond; iv += inc)
941 * except that the skips resulting from any continue statements
942 * in body do not apply to the increment, but are replaced by the skips
943 * resulting from break statements.
945 * If the loop iterator is declared in the for loop, then it is killed before
946 * and after the loop.
948 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
949 __isl_take pet_context
*pc
, struct pet_state
*state
)
952 int test_nr
, stmt_nr
;
954 pet_expr
*expr_iv
, *init
, *inc
;
955 struct pet_scop
*scop_init
, *scop_inc
, *scop
, *scop_body
;
957 struct pet_array
*array
;
958 struct pet_scop
*scop_kill
;
960 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
961 pc
= pet_context_mark_assigned(pc
, iv
);
963 declared
= tree
->u
.l
.declared
;
965 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
966 type_size
= pet_expr_get_type_size(expr_iv
);
967 init
= pet_expr_copy(tree
->u
.l
.init
);
968 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
969 scop_init
= scop_from_expr(init
, NULL
, state
->n_stmt
++,
970 pet_tree_get_loc(tree
), pc
);
971 scop_init
= pet_scop_prefix(scop_init
, declared
);
973 test_nr
= state
->n_test
++;
974 stmt_nr
= state
->n_stmt
++;
975 scop_body
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
977 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
978 type_size
= pet_expr_get_type_size(expr_iv
);
979 inc
= pet_expr_copy(tree
->u
.l
.inc
);
980 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
981 scop_inc
= scop_from_expr(inc
, NULL
, state
->n_stmt
++,
982 pet_tree_get_loc(tree
), pc
);
984 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
985 test_nr
, stmt_nr
, pet_tree_get_loc(tree
),
986 scop_body
, scop_inc
, pet_context_copy(pc
), state
);
988 scop
= pet_scop_prefix(scop
, declared
+ 1);
989 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
992 pet_context_free(pc
);
996 array
= extract_array(tree
->u
.l
.iv
, pc
, state
);
999 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
1000 scop_kill
= pet_scop_prefix(scop_kill
, 0);
1001 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
1002 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
1003 scop_kill
= pet_scop_add_array(scop_kill
, array
);
1004 scop_kill
= pet_scop_prefix(scop_kill
, 3);
1005 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
1007 pet_context_free(pc
);
1011 /* Given an access expression "expr", is the variable accessed by
1012 * "expr" assigned anywhere inside "tree"?
1014 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
1019 id
= pet_expr_access_get_id(expr
);
1020 assigned
= pet_tree_writes(tree
, id
);
1026 /* Are all nested access parameters in "pa" allowed given "tree".
1027 * In particular, is none of them written by anywhere inside "tree".
1029 * If "tree" has any continue nodes in the current loop level,
1030 * then no nested access parameters are allowed.
1031 * In particular, if there is any nested access in a guard
1032 * for a piece of code containing a "continue", then we want to introduce
1033 * a separate statement for evaluating this guard so that we can express
1034 * that the result is false for all previous iterations.
1036 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
1037 __isl_keep pet_tree
*tree
)
1044 if (!pet_nested_any_in_pw_aff(pa
))
1047 if (pet_tree_has_continue(tree
))
1050 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1051 for (i
= 0; i
< nparam
; ++i
) {
1052 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1056 if (!pet_nested_in_id(id
)) {
1061 expr
= pet_nested_extract_expr(id
);
1062 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1063 !is_assigned(expr
, tree
);
1065 pet_expr_free(expr
);
1075 /* Construct a pet_scop for a for tree with static affine initialization
1076 * and constant increment within the context "pc".
1078 * The condition is allowed to contain nested accesses, provided
1079 * they are not being written to inside the body of the loop.
1080 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1081 * essentially treated as a while loop, with iteration domain
1082 * { [i] : i >= init }.
1084 * We extract a pet_scop for the body and then embed it in a loop with
1085 * iteration domain and schedule
1087 * { [i] : i >= init and condition' }
1092 * { [i] : i <= init and condition' }
1095 * Where condition' is equal to condition if the latter is
1096 * a simple upper [lower] bound and a condition that is extended
1097 * to apply to all previous iterations otherwise.
1099 * If the condition is non-affine, then we drop the condition from the
1100 * iteration domain and instead create a separate statement
1101 * for evaluating the condition. The body is then filtered to depend
1102 * on the result of the condition evaluating to true on all iterations
1103 * up to the current iteration, while the evaluation the condition itself
1104 * is filtered to depend on the result of the condition evaluating to true
1105 * on all previous iterations.
1106 * The context of the scop representing the body is dropped
1107 * because we don't know how many times the body will be executed,
1110 * If the stride of the loop is not 1, then "i >= init" is replaced by
1112 * (exists a: i = init + stride * a and a >= 0)
1114 * If the loop iterator i is unsigned, then wrapping may occur.
1115 * We therefore use a virtual iterator instead that does not wrap.
1116 * However, the condition in the code applies
1117 * to the wrapped value, so we need to change condition(i)
1118 * into condition([i % 2^width]). Similarly, we replace all accesses
1119 * to the original iterator by the wrapping of the virtual iterator.
1120 * Note that there may be no need to perform this final wrapping
1121 * if the loop condition (after wrapping) satisfies certain conditions.
1122 * However, the is_simple_bound condition is not enough since it doesn't
1123 * check if there even is an upper bound.
1125 * Wrapping on unsigned iterators can be avoided entirely if
1126 * loop condition is simple, the loop iterator is incremented
1127 * [decremented] by one and the last value before wrapping cannot
1128 * possibly satisfy the loop condition.
1130 * Valid parameters for a for loop are those for which the initial
1131 * value itself, the increment on each domain iteration and
1132 * the condition on both the initial value and
1133 * the result of incrementing the iterator for each iteration of the domain
1135 * If the loop condition is non-affine, then we only consider validity
1136 * of the initial value.
1138 * If the body contains any break, then we keep track of it in "skip"
1139 * (if the skip condition is affine) or it is handled in scop_add_break
1140 * (if the skip condition is not affine).
1141 * Note that the affine break condition needs to be considered with
1142 * respect to previous iterations in the virtual domain (if any).
1144 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1145 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1146 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1147 struct pet_state
*state
)
1149 isl_local_space
*ls
;
1152 isl_set
*cond
= NULL
;
1153 isl_set
*skip
= NULL
;
1154 isl_id
*id
, *id_test
= NULL
, *id_break_test
;
1155 struct pet_scop
*scop
, *scop_cond
= NULL
;
1161 int has_affine_break
;
1163 isl_map
*rev_wrap
= NULL
;
1164 isl_aff
*wrap
= NULL
;
1166 isl_set
*valid_init
;
1167 isl_set
*valid_cond
;
1168 isl_set
*valid_cond_init
;
1169 isl_set
*valid_cond_next
;
1171 pet_expr
*cond_expr
;
1172 pet_context
*pc_nested
;
1174 id
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1176 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1177 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1178 pc_nested
= pet_context_copy(pc
);
1179 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1180 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1181 pet_context_free(pc_nested
);
1182 pet_expr_free(cond_expr
);
1184 valid_inc
= isl_pw_aff_domain(pa_inc
);
1186 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1188 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1189 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1191 pa
= isl_pw_aff_free(pa
);
1193 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1194 cond
= isl_pw_aff_non_zero_set(pa
);
1195 if (is_non_affine
) {
1196 isl_multi_pw_aff
*test_index
;
1197 test_index
= pet_create_test_index(state
->ctx
, state
->n_test
++);
1198 scop_cond
= scop_from_non_affine_condition(
1199 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1200 isl_multi_pw_aff_copy(test_index
),
1201 pet_tree_get_loc(tree
), pc
);
1202 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1204 scop_cond
= pet_scop_add_boolean_array(scop_cond
, test_index
,
1206 scop_cond
= pet_scop_prefix(scop_cond
, 0);
1207 cond
= isl_set_universe(isl_space_set_alloc(state
->ctx
, 0, 0));
1210 cond
= embed(cond
, isl_id_copy(id
));
1211 valid_cond
= isl_set_coalesce(valid_cond
);
1212 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
1213 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
1214 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1215 is_virtual
= is_unsigned
&&
1216 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1218 valid_cond_init
= enforce_subset(
1219 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val
))),
1220 isl_set_copy(valid_cond
));
1221 if (is_one
&& !is_virtual
) {
1222 isl_pw_aff_free(init_val
);
1223 pa
= pet_expr_extract_comparison(
1224 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1225 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1226 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1227 valid_init
= set_project_out_by_id(valid_init
, id
);
1228 domain
= isl_pw_aff_non_zero_set(pa
);
1230 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1231 domain
= strided_domain(isl_id_copy(id
), init_val
,
1235 domain
= embed(domain
, isl_id_copy(id
));
1237 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1238 rev_wrap
= isl_map_from_aff(isl_aff_copy(wrap
));
1239 rev_wrap
= isl_map_reverse(rev_wrap
);
1240 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1241 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1242 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1244 is_simple
= is_simple_bound(cond
, inc
);
1246 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1247 is_simple
= is_simple_bound(cond
, inc
);
1250 cond
= valid_for_each_iteration(cond
,
1251 isl_set_copy(domain
), isl_val_copy(inc
));
1252 domain
= isl_set_intersect(domain
, cond
);
1253 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1254 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
1255 sched
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1256 if (isl_val_is_neg(inc
))
1257 sched
= isl_aff_neg(sched
);
1259 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1261 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1264 wrap
= identity_aff(domain
);
1266 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
1268 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
1269 isl_aff_copy(sched
), isl_aff_copy(wrap
), isl_id_copy(id
));
1270 has_affine_break
= scop
&&
1271 pet_scop_has_affine_skip(scop
, pet_skip_later
);
1272 if (has_affine_break
)
1273 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1274 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
1276 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1277 if (is_non_affine
) {
1278 scop
= pet_scop_reset_context(scop
);
1279 scop
= pet_scop_prefix(scop
, 1);
1281 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
, wrap
, id
);
1282 scop
= pet_scop_resolve_nested(scop
);
1283 if (has_affine_break
) {
1284 domain
= apply_affine_break(domain
, skip
, isl_val_sgn(inc
),
1285 is_virtual
, rev_wrap
);
1286 scop
= pet_scop_intersect_domain_prefix(scop
,
1287 isl_set_copy(domain
));
1289 isl_map_free(rev_wrap
);
1291 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
1293 if (is_non_affine
) {
1294 scop
= scop_add_while(scop_cond
, scop
, id_test
, domain
,
1296 isl_set_free(valid_inc
);
1298 scop
= pet_scop_restrict_context(scop
, valid_inc
);
1299 scop
= pet_scop_restrict_context(scop
, valid_cond_next
);
1300 scop
= pet_scop_restrict_context(scop
, valid_cond_init
);
1301 isl_set_free(domain
);
1306 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid_init
));
1308 pet_context_free(pc
);
1312 /* Construct a pet_scop for a for statement within the context of "pc".
1314 * We update the context to reflect the writes to the loop variable and
1315 * the writes inside the body.
1317 * Then we check if the initialization of the for loop
1318 * is a static affine value and the increment is a constant.
1319 * If so, we construct the pet_scop using scop_from_affine_for.
1320 * Otherwise, we treat the for loop as a while loop
1321 * in scop_from_non_affine_for.
1323 static struct pet_scop
*scop_from_for(__isl_keep pet_tree
*tree
,
1324 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1328 isl_pw_aff
*pa_inc
, *init_val
;
1329 pet_context
*pc_init_val
;
1334 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1335 pc
= pet_context_copy(pc
);
1336 pc
= pet_context_clear_value(pc
, iv
);
1337 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1339 pc_init_val
= pet_context_copy(pc
);
1340 pc_init_val
= pet_context_mark_unknown(pc_init_val
, isl_id_copy(iv
));
1341 init_val
= pet_expr_extract_affine(tree
->u
.l
.init
, pc_init_val
);
1342 pet_context_free(pc_init_val
);
1343 pa_inc
= pet_expr_extract_affine(tree
->u
.l
.inc
, pc
);
1344 inc
= pet_extract_cst(pa_inc
);
1345 if (!pa_inc
|| !init_val
|| !inc
)
1347 if (!isl_pw_aff_involves_nan(pa_inc
) &&
1348 !isl_pw_aff_involves_nan(init_val
) && !isl_val_is_nan(inc
))
1349 return scop_from_affine_for(tree
, init_val
, pa_inc
, inc
,
1352 isl_pw_aff_free(pa_inc
);
1353 isl_pw_aff_free(init_val
);
1355 return scop_from_non_affine_for(tree
, pc
, state
);
1357 isl_pw_aff_free(pa_inc
);
1358 isl_pw_aff_free(init_val
);
1360 pet_context_free(pc
);
1364 /* Check whether "expr" is an affine constraint within the context "pc".
1366 static int is_affine_condition(__isl_keep pet_expr
*expr
,
1367 __isl_keep pet_context
*pc
)
1372 pa
= pet_expr_extract_affine_condition(expr
, pc
);
1375 is_affine
= !isl_pw_aff_involves_nan(pa
);
1376 isl_pw_aff_free(pa
);
1381 /* Check if the given if statement is a conditional assignement
1382 * with a non-affine condition.
1384 * In particular we check if "stmt" is of the form
1391 * where the condition is non-affine and a is some array or scalar access.
1393 static int is_conditional_assignment(__isl_keep pet_tree
*tree
,
1394 __isl_keep pet_context
*pc
)
1398 pet_expr
*expr1
, *expr2
;
1400 ctx
= pet_tree_get_ctx(tree
);
1401 if (!pet_options_get_detect_conditional_assignment(ctx
))
1403 if (tree
->type
!= pet_tree_if_else
)
1405 if (tree
->u
.i
.then_body
->type
!= pet_tree_expr
)
1407 if (tree
->u
.i
.else_body
->type
!= pet_tree_expr
)
1409 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1410 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1411 if (pet_expr_get_type(expr1
) != pet_expr_op
)
1413 if (pet_expr_get_type(expr2
) != pet_expr_op
)
1415 if (pet_expr_op_get_type(expr1
) != pet_op_assign
)
1417 if (pet_expr_op_get_type(expr2
) != pet_op_assign
)
1419 expr1
= pet_expr_get_arg(expr1
, 0);
1420 expr2
= pet_expr_get_arg(expr2
, 0);
1421 equal
= pet_expr_is_equal(expr1
, expr2
);
1422 pet_expr_free(expr1
);
1423 pet_expr_free(expr2
);
1424 if (equal
< 0 || !equal
)
1426 if (is_affine_condition(tree
->u
.i
.cond
, pc
))
1432 /* Given that "tree" is of the form
1439 * where a is some array or scalar access, construct a pet_scop
1440 * corresponding to this conditional assignment within the context "pc".
1442 * The constructed pet_scop then corresponds to the expression
1444 * a = condition ? f(...) : g(...)
1446 * All access relations in f(...) are intersected with condition
1447 * while all access relation in g(...) are intersected with the complement.
1449 static struct pet_scop
*scop_from_conditional_assignment(
1450 __isl_keep pet_tree
*tree
, __isl_take pet_context
*pc
,
1451 struct pet_state
*state
)
1455 isl_set
*cond
, *comp
;
1456 isl_multi_pw_aff
*index
;
1457 pet_expr
*expr1
, *expr2
;
1458 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
1459 pet_context
*pc_nested
;
1460 struct pet_scop
*scop
;
1462 pe_cond
= pet_expr_copy(tree
->u
.i
.cond
);
1463 pe_cond
= pet_expr_plug_in_args(pe_cond
, pc
);
1464 pc_nested
= pet_context_copy(pc
);
1465 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1466 pa
= pet_expr_extract_affine_condition(pe_cond
, pc_nested
);
1467 pet_context_free(pc_nested
);
1468 pet_expr_free(pe_cond
);
1469 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
1470 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
1471 index
= isl_multi_pw_aff_from_pw_aff(pa
);
1473 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1474 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1476 pe_cond
= pet_expr_from_index(index
);
1478 pe_then
= pet_expr_get_arg(expr1
, 1);
1479 pe_then
= pet_expr_restrict(pe_then
, cond
);
1480 pe_else
= pet_expr_get_arg(expr2
, 1);
1481 pe_else
= pet_expr_restrict(pe_else
, comp
);
1482 pe_write
= pet_expr_get_arg(expr1
, 0);
1484 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
1485 type_size
= pet_expr_get_type_size(pe_write
);
1486 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, pe_write
, pe
);
1488 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
1489 pet_tree_get_loc(tree
), pc
);
1491 pet_context_free(pc
);
1496 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1498 * We create a separate statement that writes the result
1499 * of the non-affine condition to a virtual scalar.
1500 * A constraint requiring the value of this virtual scalar to be one
1501 * is added to the iteration domains of the then branch.
1502 * Similarly, a constraint requiring the value of this virtual scalar
1503 * to be zero is added to the iteration domains of the else branch, if any.
1504 * We adjust the schedules to ensure that the virtual scalar is written
1505 * before it is read.
1507 * If there are any breaks or continues in the then and/or else
1508 * branches, then we may have to compute a new skip condition.
1509 * This is handled using a pet_skip_info object.
1510 * On initialization, the object checks if skip conditions need
1511 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1512 * adds them in pet_skip_info_if_add.
1514 static struct pet_scop
*scop_from_non_affine_if(__isl_keep pet_tree
*tree
,
1515 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
, int stmt_id
,
1516 __isl_take pet_context
*pc
, struct pet_state
*state
)
1519 int save_n_stmt
= state
->n_stmt
;
1520 isl_multi_pw_aff
*test_index
;
1521 struct pet_skip_info skip
;
1522 struct pet_scop
*scop
;
1524 has_else
= tree
->type
== pet_tree_if_else
;
1526 test_index
= pet_create_test_index(state
->ctx
, state
->n_test
++);
1527 state
->n_stmt
= stmt_id
;
1528 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
1529 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
1530 pet_tree_get_loc(tree
), pc
);
1531 state
->n_stmt
= save_n_stmt
;
1532 scop
= pet_scop_add_boolean_array(scop
,
1533 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1535 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1537 pet_skip_info_if_extract_index(&skip
, test_index
, state
);
1539 scop
= pet_scop_prefix(scop
, 0);
1540 scop_then
= pet_scop_prefix(scop_then
, 1);
1541 scop_then
= pet_scop_filter(scop_then
,
1542 isl_multi_pw_aff_copy(test_index
), 1);
1544 scop_else
= pet_scop_prefix(scop_else
, 1);
1545 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1546 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1548 isl_multi_pw_aff_free(test_index
);
1550 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1552 scop
= pet_skip_info_if_add(&skip
, scop
, 2);
1554 pet_context_free(pc
);
1558 /* Construct a pet_scop for an affine if statement within the context "pc".
1560 * The condition is added to the iteration domains of the then branch,
1561 * while the opposite of the condition in added to the iteration domains
1562 * of the else branch, if any.
1564 * If there are any breaks or continues in the then and/or else
1565 * branches, then we may have to compute a new skip condition.
1566 * This is handled using a pet_skip_info_if object.
1567 * On initialization, the object checks if skip conditions need
1568 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1569 * adds them in pet_skip_info_if_add.
1571 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1572 __isl_take isl_pw_aff
*cond
,
1573 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
1574 struct pet_state
*state
)
1580 struct pet_skip_info skip
;
1581 struct pet_scop
*scop
;
1583 ctx
= pet_tree_get_ctx(tree
);
1585 has_else
= tree
->type
== pet_tree_if_else
;
1587 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
1588 pet_skip_info_if_extract_cond(&skip
, cond
, state
);
1590 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1591 set
= isl_pw_aff_non_zero_set(cond
);
1592 scop
= pet_scop_restrict(scop_then
, isl_set_params(isl_set_copy(set
)));
1595 set
= isl_set_subtract(isl_set_copy(valid
), set
);
1596 scop_else
= pet_scop_restrict(scop_else
, isl_set_params(set
));
1597 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
1600 scop
= pet_scop_resolve_nested(scop
);
1601 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
1603 if (pet_skip_info_has_skip(&skip
))
1604 scop
= pet_scop_prefix(scop
, 0);
1605 scop
= pet_skip_info_if_add(&skip
, scop
, 1);
1610 /* Construct a pet_scop for an if statement within the context "pc".
1612 * If the condition fits the pattern of a conditional assignment,
1613 * then it is handled by scop_from_conditional_assignment.
1615 * Otherwise, we check if the condition is affine.
1616 * If so, we construct the scop in scop_from_affine_if.
1617 * Otherwise, we construct the scop in scop_from_non_affine_if.
1619 * We allow the condition to be dynamic, i.e., to refer to
1620 * scalars or array elements that may be written to outside
1621 * of the given if statement. These nested accesses are then represented
1622 * as output dimensions in the wrapping iteration domain.
1623 * If it is also written _inside_ the then or else branch, then
1624 * we treat the condition as non-affine.
1625 * As explained in extract_non_affine_if, this will introduce
1626 * an extra statement.
1627 * For aesthetic reasons, we want this statement to have a statement
1628 * number that is lower than those of the then and else branches.
1629 * In order to evaluate if we will need such a statement, however, we
1630 * first construct scops for the then and else branches.
1631 * We therefore reserve a statement number if we might have to
1632 * introduce such an extra statement.
1634 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
1635 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1640 pet_expr
*cond_expr
;
1641 struct pet_scop
*scop_then
, *scop_else
= NULL
;
1642 pet_context
*pc_nested
;
1647 has_else
= tree
->type
== pet_tree_if_else
;
1649 pc
= pet_context_copy(pc
);
1650 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
1652 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
1654 if (is_conditional_assignment(tree
, pc
))
1655 return scop_from_conditional_assignment(tree
, pc
, state
);
1657 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
1658 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1659 pc_nested
= pet_context_copy(pc
);
1660 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1661 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1662 pet_context_free(pc_nested
);
1663 pet_expr_free(cond_expr
);
1666 pet_context_free(pc
);
1670 if (isl_pw_aff_involves_nan(cond
) || pet_nested_any_in_pw_aff(cond
))
1671 stmt_id
= state
->n_stmt
++;
1673 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1675 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1677 if (isl_pw_aff_involves_nan(cond
)) {
1678 isl_pw_aff_free(cond
);
1679 return scop_from_non_affine_if(tree
, scop_then
, scop_else
,
1680 stmt_id
, pc
, state
);
1683 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
1684 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
1685 isl_pw_aff_free(cond
);
1686 return scop_from_non_affine_if(tree
, scop_then
, scop_else
,
1687 stmt_id
, pc
, state
);
1690 pet_context_free(pc
);
1691 return scop_from_affine_if(tree
, cond
, scop_then
, scop_else
, state
);
1694 /* Return a one-dimensional multi piecewise affine expression that is equal
1695 * to the constant 1 and is defined over a zero-dimensional domain.
1697 static __isl_give isl_multi_pw_aff
*one_mpa(isl_ctx
*ctx
)
1700 isl_local_space
*ls
;
1703 space
= isl_space_set_alloc(ctx
, 0, 0);
1704 ls
= isl_local_space_from_space(space
);
1705 aff
= isl_aff_zero_on_domain(ls
);
1706 aff
= isl_aff_set_constant_si(aff
, 1);
1708 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1711 /* Construct a pet_scop for a continue statement.
1713 * We simply create an empty scop with a universal pet_skip_now
1714 * skip condition. This skip condition will then be taken into
1715 * account by the enclosing loop construct, possibly after
1716 * being incorporated into outer skip conditions.
1718 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
)
1720 struct pet_scop
*scop
;
1723 ctx
= pet_tree_get_ctx(tree
);
1724 scop
= pet_scop_empty(ctx
);
1728 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(ctx
));
1733 /* Construct a pet_scop for a break statement.
1735 * We simply create an empty scop with both a universal pet_skip_now
1736 * skip condition and a universal pet_skip_later skip condition.
1737 * These skip conditions will then be taken into
1738 * account by the enclosing loop construct, possibly after
1739 * being incorporated into outer skip conditions.
1741 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
)
1743 struct pet_scop
*scop
;
1745 isl_multi_pw_aff
*skip
;
1747 ctx
= pet_tree_get_ctx(tree
);
1748 scop
= pet_scop_empty(ctx
);
1752 skip
= one_mpa(ctx
);
1753 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
1754 isl_multi_pw_aff_copy(skip
));
1755 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
1760 /* Extract a clone of the kill statement in "scop".
1761 * "scop" is expected to have been created from a DeclStmt
1762 * and should have the kill as its first statement.
1764 static struct pet_scop
*extract_kill(isl_ctx
*ctx
, struct pet_scop
*scop
,
1765 struct pet_state
*state
)
1768 struct pet_stmt
*stmt
;
1769 isl_multi_pw_aff
*index
;
1775 if (scop
->n_stmt
< 1)
1776 isl_die(ctx
, isl_error_internal
,
1777 "expecting at least one statement", return NULL
);
1778 stmt
= scop
->stmts
[0];
1779 if (!pet_stmt_is_kill(stmt
))
1780 isl_die(ctx
, isl_error_internal
,
1781 "expecting kill statement", return NULL
);
1783 arg
= pet_expr_get_arg(stmt
->body
, 0);
1784 index
= pet_expr_access_get_index(arg
);
1785 access
= pet_expr_access_get_access(arg
);
1787 index
= isl_multi_pw_aff_reset_tuple_id(index
, isl_dim_in
);
1788 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
1789 kill
= pet_expr_kill_from_access_and_index(access
, index
);
1790 stmt
= pet_stmt_from_pet_expr(pet_loc_copy(stmt
->loc
),
1791 NULL
, state
->n_stmt
++, kill
);
1792 return pet_scop_from_pet_stmt(ctx
, stmt
);
1795 /* Mark all arrays in "scop" as being exposed.
1797 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
1803 for (i
= 0; i
< scop
->n_array
; ++i
)
1804 scop
->arrays
[i
]->exposed
= 1;
1808 /* Try and construct a pet_scop corresponding to (part of)
1809 * a sequence of statements within the context "pc".
1811 * After extracting a statement, we update "pc"
1812 * based on the top-level assignments in the statement
1813 * so that we can exploit them in subsequent statements in the same block.
1815 * If there are any breaks or continues in the individual statements,
1816 * then we may have to compute a new skip condition.
1817 * This is handled using a pet_skip_info object.
1818 * On initialization, the object checks if skip conditions need
1819 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1820 * adds them in pet_skip_info_seq_add.
1822 * If "block" is set, then we need to insert kill statements at
1823 * the end of the block for any array that has been declared by
1824 * one of the statements in the sequence. Each of these declarations
1825 * results in the construction of a kill statement at the place
1826 * of the declaration, so we simply collect duplicates of
1827 * those kill statements and append these duplicates to the constructed scop.
1829 * If "block" is not set, then any array declared by one of the statements
1830 * in the sequence is marked as being exposed.
1832 * If autodetect is set, then we allow the extraction of only a subrange
1833 * of the sequence of statements. However, if there is at least one statement
1834 * for which we could not construct a scop and the final range contains
1835 * either no statements or at least one kill, then we discard the entire
1838 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
1839 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1843 struct pet_scop
*scop
, *kills
;
1845 ctx
= pet_tree_get_ctx(tree
);
1847 pc
= pet_context_copy(pc
);
1848 scop
= pet_scop_empty(ctx
);
1849 kills
= pet_scop_empty(ctx
);
1850 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
1851 struct pet_scop
*scop_i
;
1853 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
1854 pc
= scop_handle_writes(scop_i
, pc
);
1855 struct pet_skip_info skip
;
1856 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
1857 pet_skip_info_seq_extract(&skip
, state
);
1858 if (pet_skip_info_has_skip(&skip
))
1859 scop_i
= pet_scop_prefix(scop_i
, 0);
1860 if (scop_i
&& pet_tree_is_decl(tree
->u
.b
.child
[i
])) {
1861 if (tree
->u
.b
.block
) {
1862 struct pet_scop
*kill
;
1863 kill
= extract_kill(ctx
, scop_i
, state
);
1864 kills
= pet_scop_add_par(ctx
, kills
, kill
);
1866 scop_i
= mark_exposed(scop_i
);
1868 scop_i
= pet_scop_prefix(scop_i
, i
);
1869 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
1871 scop
= pet_skip_info_seq_add(&skip
, scop
, i
);
1877 kills
= pet_scop_prefix(kills
, tree
->u
.b
.n
);
1878 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
1880 pet_context_free(pc
);
1885 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1886 * within the context "pc" by calling the appropriate function
1887 * based on the type of "tree".
1889 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
1890 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1895 switch (tree
->type
) {
1896 case pet_tree_error
:
1898 case pet_tree_block
:
1899 return scop_from_block(tree
, pc
, state
);
1900 case pet_tree_break
:
1901 return scop_from_break(tree
);
1902 case pet_tree_continue
:
1903 return scop_from_continue(tree
);
1905 case pet_tree_decl_init
:
1906 return scop_from_decl(tree
, pc
, state
);
1908 return scop_from_expr(pet_expr_copy(tree
->u
.e
.expr
),
1909 isl_id_copy(tree
->label
),
1911 pet_tree_get_loc(tree
), pc
);
1913 case pet_tree_if_else
:
1914 return scop_from_if(tree
, pc
, state
);
1916 return scop_from_for(tree
, pc
, state
);
1917 case pet_tree_while
:
1918 return scop_from_while(tree
, pc
, state
);
1919 case pet_tree_infinite_loop
:
1920 return scop_from_infinite_for(tree
, pc
, state
);
1923 isl_die(tree
->ctx
, isl_error_internal
, "unhandled type",
1927 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1928 * "int_size" is the number of bytes need to represent an integer.
1929 * "extract_array" is a callback that we can use to create a pet_array
1930 * that corresponds to the variable accessed by an expression.
1932 * Initialize the global state, construct a context and then
1933 * construct the pet_scop by recursively visiting the tree.
1935 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
1936 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
1937 __isl_keep pet_context
*pc
, void *user
), void *user
,
1938 __isl_keep pet_context
*pc
)
1940 struct pet_scop
*scop
;
1941 struct pet_state state
= { 0 };
1946 state
.ctx
= pet_tree_get_ctx(tree
);
1947 state
.int_size
= int_size
;
1948 state
.extract_array
= extract_array
;
1951 scop
= scop_from_tree(tree
, pc
, &state
);
1952 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
1954 pet_tree_free(tree
);