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 expr
= pet_expr_plug_in_args(expr
, pc
);
139 expr
= pet_expr_resolve_nested(expr
);
140 expr
= pet_expr_resolve_assume(expr
, pc
);
141 domain
= pet_context_get_domain(pc
);
142 ps
= pet_stmt_from_pet_expr(domain
, loc
, label
, stmt_nr
, expr
);
143 return pet_scop_from_pet_stmt(pet_context_get_space(pc
), ps
);
146 /* Construct a pet_scop with a single statement killing the entire
148 * The location of the statement is set to "loc".
150 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
151 __isl_keep pet_context
*pc
, struct pet_state
*state
)
156 isl_multi_pw_aff
*index
;
159 struct pet_scop
*scop
;
163 ctx
= isl_set_get_ctx(array
->extent
);
164 access
= isl_map_from_range(isl_set_copy(array
->extent
));
165 id
= isl_set_get_tuple_id(array
->extent
);
166 space
= isl_space_alloc(ctx
, 0, 0, 0);
167 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
168 index
= isl_multi_pw_aff_zero(space
);
169 expr
= pet_expr_kill_from_access_and_index(access
, index
);
170 return scop_from_expr(expr
, NULL
, state
->n_stmt
++, loc
, pc
);
176 /* Construct and return a pet_array corresponding to the variable
177 * accessed by "access" by calling the extract_array callback.
179 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
180 __isl_keep pet_context
*pc
, struct pet_state
*state
)
182 return state
->extract_array(access
, pc
, state
->user
);
185 /* Construct a pet_scop for a (single) variable declaration
186 * within the context "pc".
188 * The scop contains the variable being declared (as an array)
189 * and a statement killing the array.
191 * If the declaration comes with an initialization, then the scop
192 * also contains an assignment to the variable.
194 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
195 __isl_keep pet_context
*pc
, struct pet_state
*state
)
199 struct pet_array
*array
;
200 struct pet_scop
*scop_decl
, *scop
;
201 pet_expr
*lhs
, *rhs
, *pe
;
203 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
206 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
207 scop_decl
= pet_scop_add_array(scop_decl
, array
);
209 if (tree
->type
!= pet_tree_decl_init
)
212 lhs
= pet_expr_copy(tree
->u
.d
.var
);
213 rhs
= pet_expr_copy(tree
->u
.d
.init
);
214 type_size
= pet_expr_get_type_size(lhs
);
215 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
216 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
217 pet_tree_get_loc(tree
), pc
);
219 scop_decl
= pet_scop_prefix(scop_decl
, 0);
220 scop
= pet_scop_prefix(scop
, 1);
222 ctx
= pet_tree_get_ctx(tree
);
223 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
228 /* Embed the given iteration domain in an extra outer loop
229 * with induction variable "var".
230 * If this variable appeared as a parameter in the constraints,
231 * it is replaced by the new outermost dimension.
233 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
234 __isl_take isl_id
*var
)
238 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
239 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
241 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
242 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
249 /* Return those elements in the space of "cond" that come after
250 * (based on "sign") an element in "cond".
252 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
254 isl_map
*previous_to_this
;
257 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
259 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
261 cond
= isl_set_apply(cond
, previous_to_this
);
266 /* Remove those iterations of "domain" that have an earlier iteration
267 * (based on "sign") where "skip" is satisfied.
268 * "domain" has an extra outer loop compared to "skip".
269 * The skip condition is first embedded in the same space as "domain".
270 * If "apply_skip_map" is set, then "skip_map" is first applied
271 * to the embedded skip condition before removing it from the domain.
273 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
274 __isl_take isl_set
*skip
, int sign
,
275 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
277 skip
= embed(skip
, isl_set_get_dim_id(domain
, isl_dim_set
, 0));
279 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
280 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
281 return isl_set_subtract(domain
, after(skip
, sign
));
284 /* Create the infinite iteration domain
288 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
)
290 isl_ctx
*ctx
= isl_id_get_ctx(id
);
293 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
294 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
299 /* Create an identity affine expression on the space containing "domain",
300 * which is assumed to be one-dimensional.
302 static __isl_give isl_aff
*identity_aff(__isl_keep isl_set
*domain
)
306 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
307 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
310 /* Create an affine expression that maps elements
311 * of a single-dimensional array "id_test" to the previous element
312 * (according to "inc"), provided this element belongs to "domain".
313 * That is, create the affine expression
315 * { id[x] -> id[x - inc] : x - inc in domain }
317 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
318 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
323 isl_multi_pw_aff
*prev
;
325 space
= isl_set_get_space(domain
);
326 ls
= isl_local_space_from_space(space
);
327 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
328 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
329 prev
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
330 domain
= isl_set_preimage_multi_pw_aff(domain
,
331 isl_multi_pw_aff_copy(prev
));
332 prev
= isl_multi_pw_aff_intersect_domain(prev
, domain
);
333 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
338 /* Add an implication to "scop" expressing that if an element of
339 * virtual array "id_test" has value "satisfied" then all previous elements
340 * of this array also have that value. The set of previous elements
341 * is bounded by "domain". If "sign" is negative then the iterator
342 * is decreasing and we express that all subsequent array elements
343 * (but still defined previously) have the same value.
345 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
346 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
352 domain
= isl_set_set_tuple_id(domain
, id_test
);
353 space
= isl_set_get_space(domain
);
355 map
= isl_map_lex_ge(space
);
357 map
= isl_map_lex_le(space
);
358 map
= isl_map_intersect_range(map
, domain
);
359 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
364 /* Add a filter to "scop" that imposes that it is only executed
365 * when the variable identified by "id_test" has a zero value
366 * for all previous iterations of "domain".
368 * In particular, add a filter that imposes that the array
369 * has a zero value at the previous iteration of domain and
370 * add an implication that implies that it then has that
371 * value for all previous iterations.
373 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
374 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
375 __isl_take isl_val
*inc
)
377 isl_multi_pw_aff
*prev
;
378 int sign
= isl_val_sgn(inc
);
380 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
381 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
382 scop
= pet_scop_filter(scop
, prev
, 0);
387 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
388 __isl_keep pet_context
*pc
, struct pet_state
*state
);
390 /* Construct a pet_scop for an infinite loop around the given body
391 * within the context "pc".
393 * We extract a pet_scop for the body and then embed it in a loop with
402 * If the body contains any break, then it is taken into
403 * account in apply_affine_break (if the skip condition is affine)
404 * or in scop_add_break (if the skip condition is not affine).
406 * Note that in case of an affine skip condition,
407 * since we are dealing with a loop without loop iterator,
408 * the skip condition cannot refer to the current loop iterator and
409 * so effectively, the iteration domain is of the form
411 * { [0]; [t] : t >= 1 and not skip }
413 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
414 __isl_keep pet_context
*pc
, struct pet_state
*state
)
417 isl_id
*id
, *id_test
;
421 struct pet_scop
*scop
;
422 int has_affine_break
;
425 ctx
= pet_tree_get_ctx(body
);
426 id
= isl_id_alloc(ctx
, "t", NULL
);
427 domain
= infinite_domain(isl_id_copy(id
));
428 ident
= identity_aff(domain
);
430 scop
= scop_from_tree(body
, pc
, state
);
432 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
433 if (has_affine_break
)
434 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
435 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
437 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
439 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
440 isl_aff_copy(ident
), ident
, id
);
441 if (has_affine_break
) {
442 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
443 scop
= pet_scop_intersect_domain_prefix(scop
,
444 isl_set_copy(domain
));
447 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
449 isl_set_free(domain
);
454 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
459 * within the context "pc".
461 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
462 __isl_keep pet_context
*pc
, struct pet_state
*state
)
464 struct pet_scop
*scop
;
466 pc
= pet_context_copy(pc
);
467 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
469 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
471 pet_context_free(pc
);
476 /* Construct a pet_scop for a while loop of the form
481 * within the context "pc".
482 * In particular, construct a scop for an infinite loop around body and
483 * intersect the domain with the affine expression.
484 * Note that this intersection may result in an empty loop.
486 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
487 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
488 struct pet_state
*state
)
490 struct pet_scop
*scop
;
494 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
495 dom
= isl_pw_aff_non_zero_set(pa
);
496 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
497 scop
= pet_scop_restrict(scop
, isl_set_params(dom
));
498 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
500 pet_context_free(pc
);
504 /* Construct a scop for a while, given the scops for the condition
505 * and the body, the filter identifier and the iteration domain of
508 * In particular, the scop for the condition is filtered to depend
509 * on "id_test" evaluating to true for all previous iterations
510 * of the loop, while the scop for the body is filtered to depend
511 * on "id_test" evaluating to true for all iterations up to the
513 * The actual filter only imposes that this virtual array has
514 * value one on the previous or the current iteration.
515 * The fact that this condition also applies to the previous
516 * iterations is enforced by an implication.
518 * These filtered scops are then combined into a single scop.
520 * "sign" is positive if the iterator increases and negative
523 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
524 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
525 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
527 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
529 isl_multi_pw_aff
*test_index
;
530 isl_multi_pw_aff
*prev
;
531 int sign
= isl_val_sgn(inc
);
532 struct pet_scop
*scop
;
534 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
535 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
537 space
= isl_space_map_from_set(isl_set_get_space(domain
));
538 test_index
= isl_multi_pw_aff_identity(space
);
539 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
540 isl_id_copy(id_test
));
541 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
543 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
544 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
549 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
550 * evaluating "cond" and writing the result to a virtual scalar,
551 * as expressed by "index".
552 * Do so within the context "pc".
553 * The location of the statement is set to "loc".
555 static struct pet_scop
*scop_from_non_affine_condition(
556 __isl_take pet_expr
*cond
, int stmt_nr
,
557 __isl_take isl_multi_pw_aff
*index
,
558 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
560 pet_expr
*expr
, *write
;
562 write
= pet_expr_from_index(index
);
563 write
= pet_expr_access_set_write(write
, 1);
564 write
= pet_expr_access_set_read(write
, 0);
565 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
567 return scop_from_expr(expr
, NULL
, stmt_nr
, loc
, pc
);
570 /* Construct a generic while scop, with iteration domain
571 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
572 * The scop consists of two parts,
573 * one for evaluating the condition "cond" and one for the body.
574 * If "expr_inc" is not NULL, then a scop for evaluating this expression
575 * is added at the end of the body,
576 * after replacing any skip conditions resulting from continue statements
577 * by the skip conditions resulting from break statements (if any).
579 * The schedule is adjusted to reflect that the condition is evaluated
580 * before the body is executed and the body is filtered to depend
581 * on the result of the condition evaluating to true on all iterations
582 * up to the current iteration, while the evaluation of the condition itself
583 * is filtered to depend on the result of the condition evaluating to true
584 * on all previous iterations.
585 * The context of the scop representing the body is dropped
586 * because we don't know how many times the body will be executed,
589 * If the body contains any break, then it is taken into
590 * account in apply_affine_break (if the skip condition is affine)
591 * or in scop_add_break (if the skip condition is not affine).
593 * Note that in case of an affine skip condition,
594 * since we are dealing with a loop without loop iterator,
595 * the skip condition cannot refer to the current loop iterator and
596 * so effectively, the iteration domain is of the form
598 * { [0]; [t] : t >= 1 and not skip }
600 static struct pet_scop
*scop_from_non_affine_while(__isl_take pet_expr
*cond
,
601 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
602 __isl_take pet_expr
*expr_inc
, __isl_take pet_context
*pc
,
603 struct pet_state
*state
)
606 isl_id
*id
, *id_test
, *id_break_test
;
608 isl_multi_pw_aff
*test_index
;
612 struct pet_scop
*scop
, *scop_body
;
613 int has_affine_break
;
617 space
= pet_context_get_space(pc
);
618 test_index
= pet_create_test_index(space
, state
->n_test
++);
619 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
620 isl_multi_pw_aff_copy(test_index
),
621 pet_loc_copy(loc
), pc
);
622 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
623 domain
= pet_context_get_domain(pc
);
624 scop
= pet_scop_add_boolean_array(scop
, domain
,
625 test_index
, state
->int_size
);
627 id
= isl_id_alloc(ctx
, "t", NULL
);
628 domain
= infinite_domain(isl_id_copy(id
));
629 ident
= identity_aff(domain
);
631 scop_body
= scop_from_tree(tree_body
, pc
, state
);
633 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
634 if (has_affine_break
)
635 skip
= pet_scop_get_affine_skip_domain(scop_body
,
637 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
639 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
641 scop
= pet_scop_prefix(scop
, 0);
642 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_aff_copy(ident
),
643 isl_aff_copy(ident
), isl_id_copy(id
));
644 scop_body
= pet_scop_reset_context(scop_body
);
645 scop_body
= pet_scop_prefix(scop_body
, 1);
647 struct pet_scop
*scop_inc
;
648 scop_inc
= scop_from_expr(expr_inc
, NULL
, state
->n_stmt
++,
650 scop_inc
= pet_scop_prefix(scop_inc
, 2);
651 if (pet_scop_has_skip(scop_body
, pet_skip_later
)) {
652 isl_multi_pw_aff
*skip
;
653 skip
= pet_scop_get_skip(scop_body
, pet_skip_later
);
654 scop_body
= pet_scop_set_skip(scop_body
,
657 pet_scop_reset_skip(scop_body
, pet_skip_now
);
658 scop_body
= pet_scop_add_seq(ctx
, scop_body
, scop_inc
);
661 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
662 isl_aff_copy(ident
), ident
, id
);
664 if (has_affine_break
) {
665 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
666 scop
= pet_scop_intersect_domain_prefix(scop
,
667 isl_set_copy(domain
));
668 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
669 isl_set_copy(domain
));
672 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
673 isl_set_copy(domain
), isl_val_one(ctx
));
674 scop_body
= scop_add_break(scop_body
, id_break_test
,
675 isl_set_copy(domain
), isl_val_one(ctx
));
677 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
680 pet_context_free(pc
);
684 /* Check if the while loop is of the form
686 * while (affine expression)
689 * If so, call scop_from_affine_while to construct a scop.
691 * Otherwise, pass control to scop_from_non_affine_while.
693 * "pc" is the context in which the affine expressions in the scop are created.
695 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
696 __isl_keep pet_context
*pc
, struct pet_state
*state
)
704 pc
= pet_context_copy(pc
);
705 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
707 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
708 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
709 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
710 pet_expr_free(cond_expr
);
715 if (!isl_pw_aff_involves_nan(pa
))
716 return scop_from_affine_while(tree
, pa
, pc
, state
);
718 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
719 pet_tree_get_loc(tree
), tree
->u
.l
.body
, NULL
,
722 pet_context_free(pc
);
726 /* Check whether "cond" expresses a simple loop bound
727 * on the only set dimension.
728 * In particular, if "up" is set then "cond" should contain only
729 * upper bounds on the set dimension.
730 * Otherwise, it should contain only lower bounds.
732 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
734 if (isl_val_is_pos(inc
))
735 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
737 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
740 /* Extend a condition on a given iteration of a loop to one that
741 * imposes the same condition on all previous iterations.
742 * "domain" expresses the lower [upper] bound on the iterations
743 * when inc is positive [negative].
745 * In particular, we construct the condition (when inc is positive)
747 * forall i' : (domain(i') and i' <= i) => cond(i')
749 * which is equivalent to
751 * not exists i' : domain(i') and i' <= i and not cond(i')
753 * We construct this set by negating cond, applying a map
755 * { [i'] -> [i] : domain(i') and i' <= i }
757 * and then negating the result again.
759 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
760 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
762 isl_map
*previous_to_this
;
764 if (isl_val_is_pos(inc
))
765 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
767 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
769 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
771 cond
= isl_set_complement(cond
);
772 cond
= isl_set_apply(cond
, previous_to_this
);
773 cond
= isl_set_complement(cond
);
780 /* Construct a domain of the form
782 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
784 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
785 __isl_take isl_pw_aff
*init
, __isl_take isl_val
*inc
)
791 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
792 dim
= isl_pw_aff_get_domain_space(init
);
793 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
794 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, 0, inc
);
795 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
797 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
798 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
799 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
800 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
802 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
804 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
806 return isl_set_params(set
);
809 /* Assuming "cond" represents a bound on a loop where the loop
810 * iterator "iv" is incremented (or decremented) by one, check if wrapping
813 * Under the given assumptions, wrapping is only possible if "cond" allows
814 * for the last value before wrapping, i.e., 2^width - 1 in case of an
815 * increasing iterator and 0 in case of a decreasing iterator.
817 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
818 __isl_keep isl_val
*inc
)
825 test
= isl_set_copy(cond
);
827 ctx
= isl_set_get_ctx(test
);
828 if (isl_val_is_neg(inc
))
829 limit
= isl_val_zero(ctx
);
831 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
832 limit
= isl_val_2exp(limit
);
833 limit
= isl_val_sub_ui(limit
, 1);
836 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
837 cw
= !isl_set_is_empty(test
);
843 /* Given a one-dimensional space, construct the following affine expression
846 * { [v] -> [v mod 2^width] }
848 * where width is the number of bits used to represent the values
849 * of the unsigned variable "iv".
851 static __isl_give isl_aff
*compute_wrapping(__isl_take isl_space
*dim
,
852 __isl_keep pet_expr
*iv
)
858 ctx
= isl_space_get_ctx(dim
);
859 mod
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
860 mod
= isl_val_2exp(mod
);
862 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
863 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
864 aff
= isl_aff_mod_val(aff
, mod
);
869 /* Project out the parameter "id" from "set".
871 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
872 __isl_keep isl_id
*id
)
876 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
878 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
883 /* Compute the set of parameters for which "set1" is a subset of "set2".
885 * set1 is a subset of set2 if
887 * forall i in set1 : i in set2
891 * not exists i in set1 and i not in set2
895 * not exists i in set1 \ set2
897 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
898 __isl_take isl_set
*set2
)
900 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
903 /* Compute the set of parameter values for which "cond" holds
904 * on the next iteration for each element of "dom".
906 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
907 * and then compute the set of parameters for which the result is a subset
910 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
911 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
917 space
= isl_set_get_space(dom
);
918 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
919 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
920 aff
= isl_aff_add_constant_val(aff
, inc
);
921 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
923 dom
= isl_set_apply(dom
, next
);
925 return enforce_subset(dom
, cond
);
928 /* Extract the for loop "tree" as a while loop within the context "pc".
930 * That is, the for loop has the form
932 * for (iv = init; cond; iv += inc)
943 * except that the skips resulting from any continue statements
944 * in body do not apply to the increment, but are replaced by the skips
945 * resulting from break statements.
947 * If the loop iterator is declared in the for loop, then it is killed before
948 * and after the loop.
950 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
951 __isl_take pet_context
*pc
, struct pet_state
*state
)
955 pet_expr
*expr_iv
, *init
, *inc
;
956 struct pet_scop
*scop_init
, *scop
;
958 struct pet_array
*array
;
959 struct pet_scop
*scop_kill
;
961 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
962 pc
= pet_context_mark_assigned(pc
, iv
);
964 declared
= tree
->u
.l
.declared
;
966 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
967 type_size
= pet_expr_get_type_size(expr_iv
);
968 init
= pet_expr_copy(tree
->u
.l
.init
);
969 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
970 scop_init
= scop_from_expr(init
, NULL
, state
->n_stmt
++,
971 pet_tree_get_loc(tree
), pc
);
972 scop_init
= pet_scop_prefix(scop_init
, declared
);
974 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
975 type_size
= pet_expr_get_type_size(expr_iv
);
976 inc
= pet_expr_copy(tree
->u
.l
.inc
);
977 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
979 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
980 pet_tree_get_loc(tree
), tree
->u
.l
.body
, inc
,
981 pet_context_copy(pc
), state
);
983 scop
= pet_scop_prefix(scop
, declared
+ 1);
984 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
987 pet_context_free(pc
);
991 array
= extract_array(tree
->u
.l
.iv
, pc
, state
);
994 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
995 scop_kill
= pet_scop_prefix(scop_kill
, 0);
996 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
997 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
998 scop_kill
= pet_scop_add_array(scop_kill
, array
);
999 scop_kill
= pet_scop_prefix(scop_kill
, 3);
1000 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
1002 pet_context_free(pc
);
1006 /* Given an access expression "expr", is the variable accessed by
1007 * "expr" assigned anywhere inside "tree"?
1009 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
1014 id
= pet_expr_access_get_id(expr
);
1015 assigned
= pet_tree_writes(tree
, id
);
1021 /* Are all nested access parameters in "pa" allowed given "tree".
1022 * In particular, is none of them written by anywhere inside "tree".
1024 * If "tree" has any continue nodes in the current loop level,
1025 * then no nested access parameters are allowed.
1026 * In particular, if there is any nested access in a guard
1027 * for a piece of code containing a "continue", then we want to introduce
1028 * a separate statement for evaluating this guard so that we can express
1029 * that the result is false for all previous iterations.
1031 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
1032 __isl_keep pet_tree
*tree
)
1039 if (!pet_nested_any_in_pw_aff(pa
))
1042 if (pet_tree_has_continue(tree
))
1045 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1046 for (i
= 0; i
< nparam
; ++i
) {
1047 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1051 if (!pet_nested_in_id(id
)) {
1056 expr
= pet_nested_extract_expr(id
);
1057 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1058 !is_assigned(expr
, tree
);
1060 pet_expr_free(expr
);
1070 /* Construct a pet_scop for a for tree with static affine initialization
1071 * and constant increment within the context "pc".
1073 * The condition is allowed to contain nested accesses, provided
1074 * they are not being written to inside the body of the loop.
1075 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1076 * essentially treated as a while loop, with iteration domain
1077 * { [i] : i >= init }.
1079 * We extract a pet_scop for the body and then embed it in a loop with
1080 * iteration domain and schedule
1082 * { [i] : i >= init and condition' }
1087 * { [i] : i <= init and condition' }
1090 * Where condition' is equal to condition if the latter is
1091 * a simple upper [lower] bound and a condition that is extended
1092 * to apply to all previous iterations otherwise.
1094 * If the condition is non-affine, then we drop the condition from the
1095 * iteration domain and instead create a separate statement
1096 * for evaluating the condition. The body is then filtered to depend
1097 * on the result of the condition evaluating to true on all iterations
1098 * up to the current iteration, while the evaluation the condition itself
1099 * is filtered to depend on the result of the condition evaluating to true
1100 * on all previous iterations.
1101 * The context of the scop representing the body is dropped
1102 * because we don't know how many times the body will be executed,
1105 * If the stride of the loop is not 1, then "i >= init" is replaced by
1107 * (exists a: i = init + stride * a and a >= 0)
1109 * If the loop iterator i is unsigned, then wrapping may occur.
1110 * We therefore use a virtual iterator instead that does not wrap.
1111 * However, the condition in the code applies
1112 * to the wrapped value, so we need to change condition(i)
1113 * into condition([i % 2^width]). Similarly, we replace all accesses
1114 * to the original iterator by the wrapping of the virtual iterator.
1115 * Note that there may be no need to perform this final wrapping
1116 * if the loop condition (after wrapping) satisfies certain conditions.
1117 * However, the is_simple_bound condition is not enough since it doesn't
1118 * check if there even is an upper bound.
1120 * Wrapping on unsigned iterators can be avoided entirely if
1121 * loop condition is simple, the loop iterator is incremented
1122 * [decremented] by one and the last value before wrapping cannot
1123 * possibly satisfy the loop condition.
1125 * Valid parameters for a for loop are those for which the initial
1126 * value itself, the increment on each domain iteration and
1127 * the condition on both the initial value and
1128 * the result of incrementing the iterator for each iteration of the domain
1130 * If the loop condition is non-affine, then we only consider validity
1131 * of the initial value.
1133 * If the body contains any break, then we keep track of it in "skip"
1134 * (if the skip condition is affine) or it is handled in scop_add_break
1135 * (if the skip condition is not affine).
1136 * Note that the affine break condition needs to be considered with
1137 * respect to previous iterations in the virtual domain (if any).
1139 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1140 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1141 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1142 struct pet_state
*state
)
1144 isl_local_space
*ls
;
1147 isl_set
*cond
= NULL
;
1148 isl_set
*skip
= NULL
;
1149 isl_id
*id
, *id_test
= NULL
, *id_break_test
;
1150 struct pet_scop
*scop
, *scop_cond
= NULL
;
1156 int has_affine_break
;
1158 isl_map
*rev_wrap
= NULL
;
1159 isl_aff
*wrap
= NULL
;
1161 isl_set
*valid_init
;
1162 isl_set
*valid_cond
;
1163 isl_set
*valid_cond_init
;
1164 isl_set
*valid_cond_next
;
1166 pet_expr
*cond_expr
;
1167 pet_context
*pc_nested
;
1169 id
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1171 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1172 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1173 pc_nested
= pet_context_copy(pc
);
1174 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1175 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1176 pet_context_free(pc_nested
);
1177 pet_expr_free(cond_expr
);
1179 valid_inc
= isl_pw_aff_domain(pa_inc
);
1181 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1183 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1184 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1186 pa
= isl_pw_aff_free(pa
);
1188 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1189 cond
= isl_pw_aff_non_zero_set(pa
);
1191 cond
= isl_set_universe(isl_space_set_alloc(state
->ctx
, 0, 0));
1193 cond
= embed(cond
, isl_id_copy(id
));
1194 valid_cond
= isl_set_coalesce(valid_cond
);
1195 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
1196 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
1197 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1198 is_virtual
= is_unsigned
&&
1199 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1201 valid_cond_init
= enforce_subset(
1202 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val
))),
1203 isl_set_copy(valid_cond
));
1204 if (is_one
&& !is_virtual
) {
1205 isl_pw_aff_free(init_val
);
1206 pa
= pet_expr_extract_comparison(
1207 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1208 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1209 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1210 valid_init
= set_project_out_by_id(valid_init
, id
);
1211 domain
= isl_pw_aff_non_zero_set(pa
);
1213 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1214 domain
= strided_domain(isl_id_copy(id
), init_val
,
1218 domain
= embed(domain
, isl_id_copy(id
));
1220 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1221 rev_wrap
= isl_map_from_aff(isl_aff_copy(wrap
));
1222 rev_wrap
= isl_map_reverse(rev_wrap
);
1223 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1224 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1225 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1227 is_simple
= is_simple_bound(cond
, inc
);
1229 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1230 is_simple
= is_simple_bound(cond
, inc
);
1233 cond
= valid_for_each_iteration(cond
,
1234 isl_set_copy(domain
), isl_val_copy(inc
));
1235 domain
= isl_set_intersect(domain
, cond
);
1236 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1237 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
1238 sched
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1239 if (isl_val_is_neg(inc
))
1240 sched
= isl_aff_neg(sched
);
1242 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1244 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1247 wrap
= identity_aff(domain
);
1249 if (is_non_affine
) {
1251 isl_multi_pw_aff
*test_index
;
1252 space
= pet_context_get_space(pc
);
1253 test_index
= pet_create_test_index(space
, state
->n_test
++);
1254 scop_cond
= scop_from_non_affine_condition(
1255 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1256 isl_multi_pw_aff_copy(test_index
),
1257 pet_tree_get_loc(tree
), pc
);
1258 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1260 scop_cond
= pet_scop_add_boolean_array(scop_cond
,
1261 pet_context_get_domain(pc
), test_index
,
1263 scop_cond
= pet_scop_prefix(scop_cond
, 0);
1264 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
1265 isl_aff_copy(sched
), isl_aff_copy(wrap
),
1269 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
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 __isl_take pet_context
*pc
, struct pet_state
*state
)
1520 isl_multi_pw_aff
*test_index
;
1521 struct pet_skip_info skip
;
1522 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1524 has_else
= tree
->type
== pet_tree_if_else
;
1526 space
= pet_context_get_space(pc
);
1527 test_index
= pet_create_test_index(space
, state
->n_test
++);
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 domain
= pet_context_get_domain(pc
);
1532 scop
= pet_scop_add_boolean_array(scop
, domain
,
1533 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1535 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1537 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1539 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1541 pet_skip_info_if_extract_index(&skip
, test_index
, pc
, state
);
1543 scop
= pet_scop_prefix(scop
, 0);
1544 scop_then
= pet_scop_prefix(scop_then
, 1);
1545 scop_then
= pet_scop_filter(scop_then
,
1546 isl_multi_pw_aff_copy(test_index
), 1);
1548 scop_else
= pet_scop_prefix(scop_else
, 1);
1549 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1550 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1552 isl_multi_pw_aff_free(test_index
);
1554 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1556 scop
= pet_skip_info_if_add(&skip
, scop
, 2);
1558 pet_context_free(pc
);
1562 /* Construct a pet_scop for an affine if statement within the context "pc".
1564 * The condition is added to the iteration domains of the then branch,
1565 * while the opposite of the condition in added to the iteration domains
1566 * of the else branch, if any.
1568 * If there are any breaks or continues in the then and/or else
1569 * branches, then we may have to compute a new skip condition.
1570 * This is handled using a pet_skip_info_if object.
1571 * On initialization, the object checks if skip conditions need
1572 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1573 * adds them in pet_skip_info_if_add.
1575 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1576 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
1577 struct pet_state
*state
)
1583 struct pet_skip_info skip
;
1584 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1586 ctx
= pet_tree_get_ctx(tree
);
1588 has_else
= tree
->type
== pet_tree_if_else
;
1590 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1592 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1594 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
1595 pet_skip_info_if_extract_cond(&skip
, cond
, pc
, state
);
1597 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1598 set
= isl_pw_aff_non_zero_set(cond
);
1599 scop
= pet_scop_restrict(scop_then
, isl_set_params(isl_set_copy(set
)));
1602 set
= isl_set_subtract(isl_set_copy(valid
), set
);
1603 scop_else
= pet_scop_restrict(scop_else
, isl_set_params(set
));
1604 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
1607 scop
= pet_scop_resolve_nested(scop
);
1608 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
1610 if (pet_skip_info_has_skip(&skip
))
1611 scop
= pet_scop_prefix(scop
, 0);
1612 scop
= pet_skip_info_if_add(&skip
, scop
, 1);
1614 pet_context_free(pc
);
1618 /* Construct a pet_scop for an if statement within the context "pc".
1620 * If the condition fits the pattern of a conditional assignment,
1621 * then it is handled by scop_from_conditional_assignment.
1623 * Otherwise, we check if the condition is affine.
1624 * If so, we construct the scop in scop_from_affine_if.
1625 * Otherwise, we construct the scop in scop_from_non_affine_if.
1627 * We allow the condition to be dynamic, i.e., to refer to
1628 * scalars or array elements that may be written to outside
1629 * of the given if statement. These nested accesses are then represented
1630 * as output dimensions in the wrapping iteration domain.
1631 * If it is also written _inside_ the then or else branch, then
1632 * we treat the condition as non-affine.
1633 * As explained in extract_non_affine_if, this will introduce
1634 * an extra statement.
1635 * For aesthetic reasons, we want this statement to have a statement
1636 * number that is lower than those of the then and else branches.
1637 * In order to evaluate if we will need such a statement, however, we
1638 * first construct scops for the then and else branches.
1639 * We therefore reserve a statement number if we might have to
1640 * introduce such an extra statement.
1642 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
1643 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1647 pet_expr
*cond_expr
;
1648 pet_context
*pc_nested
;
1653 has_else
= tree
->type
== pet_tree_if_else
;
1655 pc
= pet_context_copy(pc
);
1656 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
1658 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
1660 if (is_conditional_assignment(tree
, pc
))
1661 return scop_from_conditional_assignment(tree
, pc
, state
);
1663 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
1664 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1665 pc_nested
= pet_context_copy(pc
);
1666 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1667 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1668 pet_context_free(pc_nested
);
1669 pet_expr_free(cond_expr
);
1672 pet_context_free(pc
);
1676 if (isl_pw_aff_involves_nan(cond
)) {
1677 isl_pw_aff_free(cond
);
1678 return scop_from_non_affine_if(tree
, pc
, state
);
1681 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
1682 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
1683 isl_pw_aff_free(cond
);
1684 return scop_from_non_affine_if(tree
, pc
, state
);
1687 return scop_from_affine_if(tree
, cond
, pc
, state
);
1690 /* Return a one-dimensional multi piecewise affine expression that is equal
1691 * to the constant 1 and is defined over the given domain.
1693 static __isl_give isl_multi_pw_aff
*one_mpa(__isl_take isl_space
*space
)
1695 isl_local_space
*ls
;
1698 ls
= isl_local_space_from_space(space
);
1699 aff
= isl_aff_zero_on_domain(ls
);
1700 aff
= isl_aff_set_constant_si(aff
, 1);
1702 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1705 /* Construct a pet_scop for a continue statement with the given domain space.
1707 * We simply create an empty scop with a universal pet_skip_now
1708 * skip condition. This skip condition will then be taken into
1709 * account by the enclosing loop construct, possibly after
1710 * being incorporated into outer skip conditions.
1712 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
,
1713 __isl_take isl_space
*space
)
1715 struct pet_scop
*scop
;
1717 scop
= pet_scop_empty(isl_space_copy(space
));
1719 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(space
));
1724 /* Construct a pet_scop for a break statement with the given domain space.
1726 * We simply create an empty scop with both a universal pet_skip_now
1727 * skip condition and a universal pet_skip_later skip condition.
1728 * These skip conditions will then be taken into
1729 * account by the enclosing loop construct, possibly after
1730 * being incorporated into outer skip conditions.
1732 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
,
1733 __isl_take isl_space
*space
)
1735 struct pet_scop
*scop
;
1736 isl_multi_pw_aff
*skip
;
1738 scop
= pet_scop_empty(isl_space_copy(space
));
1740 skip
= one_mpa(space
);
1741 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
1742 isl_multi_pw_aff_copy(skip
));
1743 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
1748 /* Extract a clone of the kill statement in "scop".
1749 * The domain of the clone is given by "domain".
1750 * "scop" is expected to have been created from a DeclStmt
1751 * and should have the kill as its first statement.
1753 static struct pet_scop
*extract_kill(__isl_keep isl_set
*domain
,
1754 struct pet_scop
*scop
, struct pet_state
*state
)
1757 struct pet_stmt
*stmt
;
1758 isl_multi_pw_aff
*index
;
1762 if (!domain
|| !scop
)
1764 if (scop
->n_stmt
< 1)
1765 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
1766 "expecting at least one statement", return NULL
);
1767 stmt
= scop
->stmts
[0];
1768 if (!pet_stmt_is_kill(stmt
))
1769 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
1770 "expecting kill statement", return NULL
);
1772 arg
= pet_expr_get_arg(stmt
->body
, 0);
1773 index
= pet_expr_access_get_index(arg
);
1774 access
= pet_expr_access_get_access(arg
);
1776 index
= isl_multi_pw_aff_reset_tuple_id(index
, isl_dim_in
);
1777 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
1778 kill
= pet_expr_kill_from_access_and_index(access
, index
);
1779 stmt
= pet_stmt_from_pet_expr(isl_set_copy(domain
),
1780 pet_loc_copy(stmt
->loc
), NULL
, state
->n_stmt
++, kill
);
1781 return pet_scop_from_pet_stmt(isl_set_get_space(domain
), stmt
);
1784 /* Mark all arrays in "scop" as being exposed.
1786 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
1792 for (i
= 0; i
< scop
->n_array
; ++i
)
1793 scop
->arrays
[i
]->exposed
= 1;
1797 /* Try and construct a pet_scop corresponding to (part of)
1798 * a sequence of statements within the context "pc".
1800 * After extracting a statement, we update "pc"
1801 * based on the top-level assignments in the statement
1802 * so that we can exploit them in subsequent statements in the same block.
1804 * If there are any breaks or continues in the individual statements,
1805 * then we may have to compute a new skip condition.
1806 * This is handled using a pet_skip_info object.
1807 * On initialization, the object checks if skip conditions need
1808 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1809 * adds them in pet_skip_info_seq_add.
1811 * If "block" is set, then we need to insert kill statements at
1812 * the end of the block for any array that has been declared by
1813 * one of the statements in the sequence. Each of these declarations
1814 * results in the construction of a kill statement at the place
1815 * of the declaration, so we simply collect duplicates of
1816 * those kill statements and append these duplicates to the constructed scop.
1818 * If "block" is not set, then any array declared by one of the statements
1819 * in the sequence is marked as being exposed.
1821 * If autodetect is set, then we allow the extraction of only a subrange
1822 * of the sequence of statements. However, if there is at least one statement
1823 * for which we could not construct a scop and the final range contains
1824 * either no statements or at least one kill, then we discard the entire
1827 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
1828 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1834 struct pet_scop
*scop
, *kills
;
1836 ctx
= pet_tree_get_ctx(tree
);
1838 space
= pet_context_get_space(pc
);
1839 domain
= pet_context_get_domain(pc
);
1840 pc
= pet_context_copy(pc
);
1841 scop
= pet_scop_empty(isl_space_copy(space
));
1842 kills
= pet_scop_empty(space
);
1843 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
1844 struct pet_scop
*scop_i
;
1846 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
1847 pc
= scop_handle_writes(scop_i
, pc
);
1848 struct pet_skip_info skip
;
1849 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
1850 pet_skip_info_seq_extract(&skip
, pc
, state
);
1851 if (pet_skip_info_has_skip(&skip
))
1852 scop_i
= pet_scop_prefix(scop_i
, 0);
1853 if (scop_i
&& pet_tree_is_decl(tree
->u
.b
.child
[i
])) {
1854 if (tree
->u
.b
.block
) {
1855 struct pet_scop
*kill
;
1856 kill
= extract_kill(domain
, scop_i
, state
);
1857 kills
= pet_scop_add_par(ctx
, kills
, kill
);
1859 scop_i
= mark_exposed(scop_i
);
1861 scop_i
= pet_scop_prefix(scop_i
, i
);
1862 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
1864 scop
= pet_skip_info_seq_add(&skip
, scop
, i
);
1869 isl_set_free(domain
);
1871 kills
= pet_scop_prefix(kills
, tree
->u
.b
.n
);
1872 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
1874 pet_context_free(pc
);
1879 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1880 * within the context "pc" by calling the appropriate function
1881 * based on the type of "tree".
1883 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
1884 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1889 switch (tree
->type
) {
1890 case pet_tree_error
:
1892 case pet_tree_block
:
1893 return scop_from_block(tree
, pc
, state
);
1894 case pet_tree_break
:
1895 return scop_from_break(tree
, pet_context_get_space(pc
));
1896 case pet_tree_continue
:
1897 return scop_from_continue(tree
, pet_context_get_space(pc
));
1899 case pet_tree_decl_init
:
1900 return scop_from_decl(tree
, pc
, state
);
1902 return scop_from_expr(pet_expr_copy(tree
->u
.e
.expr
),
1903 isl_id_copy(tree
->label
),
1905 pet_tree_get_loc(tree
), pc
);
1907 case pet_tree_if_else
:
1908 return scop_from_if(tree
, pc
, state
);
1910 return scop_from_for(tree
, pc
, state
);
1911 case pet_tree_while
:
1912 return scop_from_while(tree
, pc
, state
);
1913 case pet_tree_infinite_loop
:
1914 return scop_from_infinite_for(tree
, pc
, state
);
1917 isl_die(tree
->ctx
, isl_error_internal
, "unhandled type",
1921 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1922 * "int_size" is the number of bytes need to represent an integer.
1923 * "extract_array" is a callback that we can use to create a pet_array
1924 * that corresponds to the variable accessed by an expression.
1926 * Initialize the global state, construct a context and then
1927 * construct the pet_scop by recursively visiting the tree.
1929 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
1930 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
1931 __isl_keep pet_context
*pc
, void *user
), void *user
,
1932 __isl_keep pet_context
*pc
)
1934 struct pet_scop
*scop
;
1935 struct pet_state state
= { 0 };
1940 state
.ctx
= pet_tree_get_ctx(tree
);
1941 state
.int_size
= int_size
;
1942 state
.extract_array
= extract_array
;
1945 scop
= scop_from_tree(tree
, pc
, &state
);
1946 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
1948 pet_tree_free(tree
);