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
)
139 ctx
= pet_expr_get_ctx(expr
);
141 expr
= pet_expr_plug_in_args(expr
, pc
);
142 expr
= pet_expr_resolve_nested(expr
);
143 expr
= pet_expr_resolve_assume(expr
, pc
);
144 domain
= pet_context_get_domain(pc
);
145 ps
= pet_stmt_from_pet_expr(domain
, loc
, label
, stmt_nr
, expr
);
146 return pet_scop_from_pet_stmt(ctx
, ps
);
149 /* Construct a pet_scop with a single statement killing the entire
151 * The location of the statement is set to "loc".
153 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
154 __isl_keep pet_context
*pc
, struct pet_state
*state
)
159 isl_multi_pw_aff
*index
;
162 struct pet_scop
*scop
;
166 ctx
= isl_set_get_ctx(array
->extent
);
167 access
= isl_map_from_range(isl_set_copy(array
->extent
));
168 id
= isl_set_get_tuple_id(array
->extent
);
169 space
= isl_space_alloc(ctx
, 0, 0, 0);
170 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
171 index
= isl_multi_pw_aff_zero(space
);
172 expr
= pet_expr_kill_from_access_and_index(access
, index
);
173 return scop_from_expr(expr
, NULL
, state
->n_stmt
++, loc
, pc
);
179 /* Construct and return a pet_array corresponding to the variable
180 * accessed by "access" by calling the extract_array callback.
182 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
183 __isl_keep pet_context
*pc
, struct pet_state
*state
)
185 return state
->extract_array(access
, pc
, state
->user
);
188 /* Construct a pet_scop for a (single) variable declaration
189 * within the context "pc".
191 * The scop contains the variable being declared (as an array)
192 * and a statement killing the array.
194 * If the declaration comes with an initialization, then the scop
195 * also contains an assignment to the variable.
197 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
198 __isl_keep pet_context
*pc
, struct pet_state
*state
)
202 struct pet_array
*array
;
203 struct pet_scop
*scop_decl
, *scop
;
204 pet_expr
*lhs
, *rhs
, *pe
;
206 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
209 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
210 scop_decl
= pet_scop_add_array(scop_decl
, array
);
212 if (tree
->type
!= pet_tree_decl_init
)
215 lhs
= pet_expr_copy(tree
->u
.d
.var
);
216 rhs
= pet_expr_copy(tree
->u
.d
.init
);
217 type_size
= pet_expr_get_type_size(lhs
);
218 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
219 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
220 pet_tree_get_loc(tree
), pc
);
222 scop_decl
= pet_scop_prefix(scop_decl
, 0);
223 scop
= pet_scop_prefix(scop
, 1);
225 ctx
= pet_tree_get_ctx(tree
);
226 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
231 /* Embed the given iteration domain in an extra outer loop
232 * with induction variable "var".
233 * If this variable appeared as a parameter in the constraints,
234 * it is replaced by the new outermost dimension.
236 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
237 __isl_take isl_id
*var
)
241 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
242 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
244 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
245 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
252 /* Return those elements in the space of "cond" that come after
253 * (based on "sign") an element in "cond".
255 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
257 isl_map
*previous_to_this
;
260 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
262 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
264 cond
= isl_set_apply(cond
, previous_to_this
);
269 /* Remove those iterations of "domain" that have an earlier iteration
270 * (based on "sign") where "skip" is satisfied.
271 * "domain" has an extra outer loop compared to "skip".
272 * The skip condition is first embedded in the same space as "domain".
273 * If "apply_skip_map" is set, then "skip_map" is first applied
274 * to the embedded skip condition before removing it from the domain.
276 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
277 __isl_take isl_set
*skip
, int sign
,
278 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
280 skip
= embed(skip
, isl_set_get_dim_id(domain
, isl_dim_set
, 0));
282 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
283 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
284 return isl_set_subtract(domain
, after(skip
, sign
));
287 /* Create the infinite iteration domain
291 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
)
293 isl_ctx
*ctx
= isl_id_get_ctx(id
);
296 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
297 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
302 /* Create an identity affine expression on the space containing "domain",
303 * which is assumed to be one-dimensional.
305 static __isl_give isl_aff
*identity_aff(__isl_keep isl_set
*domain
)
309 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
310 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
313 /* Create an affine expression that maps elements
314 * of a single-dimensional array "id_test" to the previous element
315 * (according to "inc"), provided this element belongs to "domain".
316 * That is, create the affine expression
318 * { id[x] -> id[x - inc] : x - inc in domain }
320 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
321 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
326 isl_multi_pw_aff
*prev
;
328 space
= isl_set_get_space(domain
);
329 ls
= isl_local_space_from_space(space
);
330 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
331 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
332 prev
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
333 domain
= isl_set_preimage_multi_pw_aff(domain
,
334 isl_multi_pw_aff_copy(prev
));
335 prev
= isl_multi_pw_aff_intersect_domain(prev
, domain
);
336 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
341 /* Add an implication to "scop" expressing that if an element of
342 * virtual array "id_test" has value "satisfied" then all previous elements
343 * of this array also have that value. The set of previous elements
344 * is bounded by "domain". If "sign" is negative then the iterator
345 * is decreasing and we express that all subsequent array elements
346 * (but still defined previously) have the same value.
348 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
349 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
355 domain
= isl_set_set_tuple_id(domain
, id_test
);
356 space
= isl_set_get_space(domain
);
358 map
= isl_map_lex_ge(space
);
360 map
= isl_map_lex_le(space
);
361 map
= isl_map_intersect_range(map
, domain
);
362 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
367 /* Add a filter to "scop" that imposes that it is only executed
368 * when the variable identified by "id_test" has a zero value
369 * for all previous iterations of "domain".
371 * In particular, add a filter that imposes that the array
372 * has a zero value at the previous iteration of domain and
373 * add an implication that implies that it then has that
374 * value for all previous iterations.
376 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
377 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
378 __isl_take isl_val
*inc
)
380 isl_multi_pw_aff
*prev
;
381 int sign
= isl_val_sgn(inc
);
383 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
384 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
385 scop
= pet_scop_filter(scop
, prev
, 0);
390 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
391 __isl_keep pet_context
*pc
, struct pet_state
*state
);
393 /* Construct a pet_scop for an infinite loop around the given body
394 * within the context "pc".
396 * We extract a pet_scop for the body and then embed it in a loop with
405 * If the body contains any break, then it is taken into
406 * account in apply_affine_break (if the skip condition is affine)
407 * or in scop_add_break (if the skip condition is not affine).
409 * Note that in case of an affine skip condition,
410 * since we are dealing with a loop without loop iterator,
411 * the skip condition cannot refer to the current loop iterator and
412 * so effectively, the iteration domain is of the form
414 * { [0]; [t] : t >= 1 and not skip }
416 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
417 __isl_keep pet_context
*pc
, struct pet_state
*state
)
420 isl_id
*id
, *id_test
;
424 struct pet_scop
*scop
;
425 int has_affine_break
;
428 ctx
= pet_tree_get_ctx(body
);
429 id
= isl_id_alloc(ctx
, "t", NULL
);
430 domain
= infinite_domain(isl_id_copy(id
));
431 ident
= identity_aff(domain
);
433 scop
= scop_from_tree(body
, pc
, state
);
435 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
436 if (has_affine_break
)
437 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
438 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
440 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
442 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
443 isl_aff_copy(ident
), ident
, id
);
444 if (has_affine_break
) {
445 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
446 scop
= pet_scop_intersect_domain_prefix(scop
,
447 isl_set_copy(domain
));
450 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
452 isl_set_free(domain
);
457 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
462 * within the context "pc".
464 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
465 __isl_keep pet_context
*pc
, struct pet_state
*state
)
467 struct pet_scop
*scop
;
469 pc
= pet_context_copy(pc
);
470 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
472 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
474 pet_context_free(pc
);
479 /* Construct a pet_scop for a while loop of the form
484 * within the context "pc".
485 * In particular, construct a scop for an infinite loop around body and
486 * intersect the domain with the affine expression.
487 * Note that this intersection may result in an empty loop.
489 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
490 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
491 struct pet_state
*state
)
493 struct pet_scop
*scop
;
497 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
498 dom
= isl_pw_aff_non_zero_set(pa
);
499 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
500 scop
= pet_scop_restrict(scop
, isl_set_params(dom
));
501 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
503 pet_context_free(pc
);
507 /* Construct a scop for a while, given the scops for the condition
508 * and the body, the filter identifier and the iteration domain of
511 * In particular, the scop for the condition is filtered to depend
512 * on "id_test" evaluating to true for all previous iterations
513 * of the loop, while the scop for the body is filtered to depend
514 * on "id_test" evaluating to true for all iterations up to the
516 * The actual filter only imposes that this virtual array has
517 * value one on the previous or the current iteration.
518 * The fact that this condition also applies to the previous
519 * iterations is enforced by an implication.
521 * These filtered scops are then combined into a single scop.
523 * "sign" is positive if the iterator increases and negative
526 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
527 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
528 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
530 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
532 isl_multi_pw_aff
*test_index
;
533 isl_multi_pw_aff
*prev
;
534 int sign
= isl_val_sgn(inc
);
535 struct pet_scop
*scop
;
537 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
538 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
540 space
= isl_space_map_from_set(isl_set_get_space(domain
));
541 test_index
= isl_multi_pw_aff_identity(space
);
542 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
543 isl_id_copy(id_test
));
544 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
546 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
547 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
552 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
553 * evaluating "cond" and writing the result to a virtual scalar,
554 * as expressed by "index".
555 * Do so within the context "pc".
556 * The location of the statement is set to "loc".
558 static struct pet_scop
*scop_from_non_affine_condition(
559 __isl_take pet_expr
*cond
, int stmt_nr
,
560 __isl_take isl_multi_pw_aff
*index
,
561 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
563 pet_expr
*expr
, *write
;
565 write
= pet_expr_from_index(index
);
566 write
= pet_expr_access_set_write(write
, 1);
567 write
= pet_expr_access_set_read(write
, 0);
568 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
570 return scop_from_expr(expr
, NULL
, stmt_nr
, loc
, pc
);
573 /* Construct a generic while scop, with iteration domain
574 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
575 * The scop consists of two parts,
576 * one for evaluating the condition "cond" and one for the body.
577 * If "expr_inc" is not NULL, then a scop for evaluating this expression
578 * 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 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
605 __isl_take pet_expr
*expr_inc
, __isl_take pet_context
*pc
,
606 struct pet_state
*state
)
609 isl_id
*id
, *id_test
, *id_break_test
;
611 isl_multi_pw_aff
*test_index
;
615 struct pet_scop
*scop
, *scop_body
;
616 int has_affine_break
;
620 space
= pet_context_get_space(pc
);
621 test_index
= pet_create_test_index(space
, state
->n_test
++);
622 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
623 isl_multi_pw_aff_copy(test_index
),
624 pet_loc_copy(loc
), pc
);
625 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
626 scop
= pet_scop_add_boolean_array(scop
, test_index
, state
->int_size
);
628 id
= isl_id_alloc(ctx
, "t", NULL
);
629 domain
= infinite_domain(isl_id_copy(id
));
630 ident
= identity_aff(domain
);
632 scop_body
= scop_from_tree(tree_body
, pc
, state
);
634 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
635 if (has_affine_break
)
636 skip
= pet_scop_get_affine_skip_domain(scop_body
,
638 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
640 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
642 scop
= pet_scop_prefix(scop
, 0);
643 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_aff_copy(ident
),
644 isl_aff_copy(ident
), isl_id_copy(id
));
645 scop_body
= pet_scop_reset_context(scop_body
);
646 scop_body
= pet_scop_prefix(scop_body
, 1);
648 struct pet_scop
*scop_inc
;
649 scop_inc
= scop_from_expr(expr_inc
, NULL
, state
->n_stmt
++,
651 scop_inc
= pet_scop_prefix(scop_inc
, 2);
652 if (pet_scop_has_skip(scop_body
, pet_skip_later
)) {
653 isl_multi_pw_aff
*skip
;
654 skip
= pet_scop_get_skip(scop_body
, pet_skip_later
);
655 scop_body
= pet_scop_set_skip(scop_body
,
658 pet_scop_reset_skip(scop_body
, pet_skip_now
);
659 scop_body
= pet_scop_add_seq(ctx
, scop_body
, scop_inc
);
662 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
663 isl_aff_copy(ident
), ident
, id
);
665 if (has_affine_break
) {
666 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
667 scop
= pet_scop_intersect_domain_prefix(scop
,
668 isl_set_copy(domain
));
669 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
670 isl_set_copy(domain
));
673 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
674 isl_set_copy(domain
), isl_val_one(ctx
));
675 scop_body
= scop_add_break(scop_body
, id_break_test
,
676 isl_set_copy(domain
), isl_val_one(ctx
));
678 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
681 pet_context_free(pc
);
685 /* Check if the while loop is of the form
687 * while (affine expression)
690 * If so, call scop_from_affine_while to construct a scop.
692 * Otherwise, pass control to scop_from_non_affine_while.
694 * "pc" is the context in which the affine expressions in the scop are created.
696 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
697 __isl_keep pet_context
*pc
, struct pet_state
*state
)
705 pc
= pet_context_copy(pc
);
706 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
708 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
709 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
710 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
711 pet_expr_free(cond_expr
);
716 if (!isl_pw_aff_involves_nan(pa
))
717 return scop_from_affine_while(tree
, pa
, pc
, state
);
719 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
720 pet_tree_get_loc(tree
), tree
->u
.l
.body
, NULL
,
723 pet_context_free(pc
);
727 /* Check whether "cond" expresses a simple loop bound
728 * on the only set dimension.
729 * In particular, if "up" is set then "cond" should contain only
730 * upper bounds on the set dimension.
731 * Otherwise, it should contain only lower bounds.
733 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
735 if (isl_val_is_pos(inc
))
736 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
738 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
741 /* Extend a condition on a given iteration of a loop to one that
742 * imposes the same condition on all previous iterations.
743 * "domain" expresses the lower [upper] bound on the iterations
744 * when inc is positive [negative].
746 * In particular, we construct the condition (when inc is positive)
748 * forall i' : (domain(i') and i' <= i) => cond(i')
750 * which is equivalent to
752 * not exists i' : domain(i') and i' <= i and not cond(i')
754 * We construct this set by negating cond, applying a map
756 * { [i'] -> [i] : domain(i') and i' <= i }
758 * and then negating the result again.
760 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
761 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
763 isl_map
*previous_to_this
;
765 if (isl_val_is_pos(inc
))
766 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
768 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
770 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
772 cond
= isl_set_complement(cond
);
773 cond
= isl_set_apply(cond
, previous_to_this
);
774 cond
= isl_set_complement(cond
);
781 /* Construct a domain of the form
783 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
785 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
786 __isl_take isl_pw_aff
*init
, __isl_take isl_val
*inc
)
792 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
793 dim
= isl_pw_aff_get_domain_space(init
);
794 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
795 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, 0, inc
);
796 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
798 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
799 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
800 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
801 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
803 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
805 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
807 return isl_set_params(set
);
810 /* Assuming "cond" represents a bound on a loop where the loop
811 * iterator "iv" is incremented (or decremented) by one, check if wrapping
814 * Under the given assumptions, wrapping is only possible if "cond" allows
815 * for the last value before wrapping, i.e., 2^width - 1 in case of an
816 * increasing iterator and 0 in case of a decreasing iterator.
818 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
819 __isl_keep isl_val
*inc
)
826 test
= isl_set_copy(cond
);
828 ctx
= isl_set_get_ctx(test
);
829 if (isl_val_is_neg(inc
))
830 limit
= isl_val_zero(ctx
);
832 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
833 limit
= isl_val_2exp(limit
);
834 limit
= isl_val_sub_ui(limit
, 1);
837 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
838 cw
= !isl_set_is_empty(test
);
844 /* Given a one-dimensional space, construct the following affine expression
847 * { [v] -> [v mod 2^width] }
849 * where width is the number of bits used to represent the values
850 * of the unsigned variable "iv".
852 static __isl_give isl_aff
*compute_wrapping(__isl_take isl_space
*dim
,
853 __isl_keep pet_expr
*iv
)
859 ctx
= isl_space_get_ctx(dim
);
860 mod
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
861 mod
= isl_val_2exp(mod
);
863 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
864 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
865 aff
= isl_aff_mod_val(aff
, mod
);
870 /* Project out the parameter "id" from "set".
872 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
873 __isl_keep isl_id
*id
)
877 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
879 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
884 /* Compute the set of parameters for which "set1" is a subset of "set2".
886 * set1 is a subset of set2 if
888 * forall i in set1 : i in set2
892 * not exists i in set1 and i not in set2
896 * not exists i in set1 \ set2
898 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
899 __isl_take isl_set
*set2
)
901 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
904 /* Compute the set of parameter values for which "cond" holds
905 * on the next iteration for each element of "dom".
907 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
908 * and then compute the set of parameters for which the result is a subset
911 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
912 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
918 space
= isl_set_get_space(dom
);
919 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
920 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
921 aff
= isl_aff_add_constant_val(aff
, inc
);
922 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
924 dom
= isl_set_apply(dom
, next
);
926 return enforce_subset(dom
, cond
);
929 /* Extract the for loop "tree" as a while loop within the context "pc".
931 * That is, the for loop has the form
933 * for (iv = init; cond; iv += inc)
944 * except that the skips resulting from any continue statements
945 * in body do not apply to the increment, but are replaced by the skips
946 * resulting from break statements.
948 * If the loop iterator is declared in the for loop, then it is killed before
949 * and after the loop.
951 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
952 __isl_take pet_context
*pc
, struct pet_state
*state
)
956 pet_expr
*expr_iv
, *init
, *inc
;
957 struct pet_scop
*scop_init
, *scop
;
959 struct pet_array
*array
;
960 struct pet_scop
*scop_kill
;
962 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
963 pc
= pet_context_mark_assigned(pc
, iv
);
965 declared
= tree
->u
.l
.declared
;
967 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
968 type_size
= pet_expr_get_type_size(expr_iv
);
969 init
= pet_expr_copy(tree
->u
.l
.init
);
970 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
971 scop_init
= scop_from_expr(init
, NULL
, state
->n_stmt
++,
972 pet_tree_get_loc(tree
), pc
);
973 scop_init
= pet_scop_prefix(scop_init
, declared
);
975 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
976 type_size
= pet_expr_get_type_size(expr_iv
);
977 inc
= pet_expr_copy(tree
->u
.l
.inc
);
978 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
980 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
981 pet_tree_get_loc(tree
), tree
->u
.l
.body
, inc
,
982 pet_context_copy(pc
), state
);
984 scop
= pet_scop_prefix(scop
, declared
+ 1);
985 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
988 pet_context_free(pc
);
992 array
= extract_array(tree
->u
.l
.iv
, pc
, state
);
995 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
996 scop_kill
= pet_scop_prefix(scop_kill
, 0);
997 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
998 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
999 scop_kill
= pet_scop_add_array(scop_kill
, array
);
1000 scop_kill
= pet_scop_prefix(scop_kill
, 3);
1001 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
1003 pet_context_free(pc
);
1007 /* Given an access expression "expr", is the variable accessed by
1008 * "expr" assigned anywhere inside "tree"?
1010 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
1015 id
= pet_expr_access_get_id(expr
);
1016 assigned
= pet_tree_writes(tree
, id
);
1022 /* Are all nested access parameters in "pa" allowed given "tree".
1023 * In particular, is none of them written by anywhere inside "tree".
1025 * If "tree" has any continue nodes in the current loop level,
1026 * then no nested access parameters are allowed.
1027 * In particular, if there is any nested access in a guard
1028 * for a piece of code containing a "continue", then we want to introduce
1029 * a separate statement for evaluating this guard so that we can express
1030 * that the result is false for all previous iterations.
1032 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
1033 __isl_keep pet_tree
*tree
)
1040 if (!pet_nested_any_in_pw_aff(pa
))
1043 if (pet_tree_has_continue(tree
))
1046 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1047 for (i
= 0; i
< nparam
; ++i
) {
1048 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1052 if (!pet_nested_in_id(id
)) {
1057 expr
= pet_nested_extract_expr(id
);
1058 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1059 !is_assigned(expr
, tree
);
1061 pet_expr_free(expr
);
1071 /* Construct a pet_scop for a for tree with static affine initialization
1072 * and constant increment within the context "pc".
1074 * The condition is allowed to contain nested accesses, provided
1075 * they are not being written to inside the body of the loop.
1076 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1077 * essentially treated as a while loop, with iteration domain
1078 * { [i] : i >= init }.
1080 * We extract a pet_scop for the body and then embed it in a loop with
1081 * iteration domain and schedule
1083 * { [i] : i >= init and condition' }
1088 * { [i] : i <= init and condition' }
1091 * Where condition' is equal to condition if the latter is
1092 * a simple upper [lower] bound and a condition that is extended
1093 * to apply to all previous iterations otherwise.
1095 * If the condition is non-affine, then we drop the condition from the
1096 * iteration domain and instead create a separate statement
1097 * for evaluating the condition. The body is then filtered to depend
1098 * on the result of the condition evaluating to true on all iterations
1099 * up to the current iteration, while the evaluation the condition itself
1100 * is filtered to depend on the result of the condition evaluating to true
1101 * on all previous iterations.
1102 * The context of the scop representing the body is dropped
1103 * because we don't know how many times the body will be executed,
1106 * If the stride of the loop is not 1, then "i >= init" is replaced by
1108 * (exists a: i = init + stride * a and a >= 0)
1110 * If the loop iterator i is unsigned, then wrapping may occur.
1111 * We therefore use a virtual iterator instead that does not wrap.
1112 * However, the condition in the code applies
1113 * to the wrapped value, so we need to change condition(i)
1114 * into condition([i % 2^width]). Similarly, we replace all accesses
1115 * to the original iterator by the wrapping of the virtual iterator.
1116 * Note that there may be no need to perform this final wrapping
1117 * if the loop condition (after wrapping) satisfies certain conditions.
1118 * However, the is_simple_bound condition is not enough since it doesn't
1119 * check if there even is an upper bound.
1121 * Wrapping on unsigned iterators can be avoided entirely if
1122 * loop condition is simple, the loop iterator is incremented
1123 * [decremented] by one and the last value before wrapping cannot
1124 * possibly satisfy the loop condition.
1126 * Valid parameters for a for loop are those for which the initial
1127 * value itself, the increment on each domain iteration and
1128 * the condition on both the initial value and
1129 * the result of incrementing the iterator for each iteration of the domain
1131 * If the loop condition is non-affine, then we only consider validity
1132 * of the initial value.
1134 * If the body contains any break, then we keep track of it in "skip"
1135 * (if the skip condition is affine) or it is handled in scop_add_break
1136 * (if the skip condition is not affine).
1137 * Note that the affine break condition needs to be considered with
1138 * respect to previous iterations in the virtual domain (if any).
1140 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1141 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1142 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1143 struct pet_state
*state
)
1145 isl_local_space
*ls
;
1148 isl_set
*cond
= NULL
;
1149 isl_set
*skip
= NULL
;
1150 isl_id
*id
, *id_test
= NULL
, *id_break_test
;
1151 struct pet_scop
*scop
, *scop_cond
= NULL
;
1157 int has_affine_break
;
1159 isl_map
*rev_wrap
= NULL
;
1160 isl_aff
*wrap
= NULL
;
1162 isl_set
*valid_init
;
1163 isl_set
*valid_cond
;
1164 isl_set
*valid_cond_init
;
1165 isl_set
*valid_cond_next
;
1167 pet_expr
*cond_expr
;
1168 pet_context
*pc_nested
;
1170 id
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1172 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1173 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1174 pc_nested
= pet_context_copy(pc
);
1175 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1176 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1177 pet_context_free(pc_nested
);
1178 pet_expr_free(cond_expr
);
1180 valid_inc
= isl_pw_aff_domain(pa_inc
);
1182 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1184 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1185 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1187 pa
= isl_pw_aff_free(pa
);
1189 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1190 cond
= isl_pw_aff_non_zero_set(pa
);
1192 cond
= isl_set_universe(isl_space_set_alloc(state
->ctx
, 0, 0));
1194 cond
= embed(cond
, isl_id_copy(id
));
1195 valid_cond
= isl_set_coalesce(valid_cond
);
1196 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
1197 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
1198 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1199 is_virtual
= is_unsigned
&&
1200 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1202 valid_cond_init
= enforce_subset(
1203 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val
))),
1204 isl_set_copy(valid_cond
));
1205 if (is_one
&& !is_virtual
) {
1206 isl_pw_aff_free(init_val
);
1207 pa
= pet_expr_extract_comparison(
1208 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1209 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1210 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1211 valid_init
= set_project_out_by_id(valid_init
, id
);
1212 domain
= isl_pw_aff_non_zero_set(pa
);
1214 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1215 domain
= strided_domain(isl_id_copy(id
), init_val
,
1219 domain
= embed(domain
, isl_id_copy(id
));
1221 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1222 rev_wrap
= isl_map_from_aff(isl_aff_copy(wrap
));
1223 rev_wrap
= isl_map_reverse(rev_wrap
);
1224 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1225 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1226 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1228 is_simple
= is_simple_bound(cond
, inc
);
1230 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1231 is_simple
= is_simple_bound(cond
, inc
);
1234 cond
= valid_for_each_iteration(cond
,
1235 isl_set_copy(domain
), isl_val_copy(inc
));
1236 domain
= isl_set_intersect(domain
, cond
);
1237 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1238 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
1239 sched
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1240 if (isl_val_is_neg(inc
))
1241 sched
= isl_aff_neg(sched
);
1243 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1245 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1248 wrap
= identity_aff(domain
);
1250 if (is_non_affine
) {
1252 isl_multi_pw_aff
*test_index
;
1253 space
= pet_context_get_space(pc
);
1254 test_index
= pet_create_test_index(space
, state
->n_test
++);
1255 scop_cond
= scop_from_non_affine_condition(
1256 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1257 isl_multi_pw_aff_copy(test_index
),
1258 pet_tree_get_loc(tree
), pc
);
1259 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1261 scop_cond
= pet_scop_add_boolean_array(scop_cond
, 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
)
1519 isl_multi_pw_aff
*test_index
;
1520 struct pet_skip_info skip
;
1521 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1523 has_else
= tree
->type
== pet_tree_if_else
;
1525 space
= pet_context_get_space(pc
);
1526 test_index
= pet_create_test_index(space
, state
->n_test
++);
1527 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
1528 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
1529 pet_tree_get_loc(tree
), pc
);
1530 scop
= pet_scop_add_boolean_array(scop
,
1531 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1533 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1535 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1537 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1539 pet_skip_info_if_extract_index(&skip
, test_index
, state
);
1541 scop
= pet_scop_prefix(scop
, 0);
1542 scop_then
= pet_scop_prefix(scop_then
, 1);
1543 scop_then
= pet_scop_filter(scop_then
,
1544 isl_multi_pw_aff_copy(test_index
), 1);
1546 scop_else
= pet_scop_prefix(scop_else
, 1);
1547 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1548 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1550 isl_multi_pw_aff_free(test_index
);
1552 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1554 scop
= pet_skip_info_if_add(&skip
, scop
, 2);
1556 pet_context_free(pc
);
1560 /* Construct a pet_scop for an affine if statement within the context "pc".
1562 * The condition is added to the iteration domains of the then branch,
1563 * while the opposite of the condition in added to the iteration domains
1564 * of the else branch, if any.
1566 * If there are any breaks or continues in the then and/or else
1567 * branches, then we may have to compute a new skip condition.
1568 * This is handled using a pet_skip_info_if object.
1569 * On initialization, the object checks if skip conditions need
1570 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1571 * adds them in pet_skip_info_if_add.
1573 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1574 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
1575 struct pet_state
*state
)
1581 struct pet_skip_info skip
;
1582 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1584 ctx
= pet_tree_get_ctx(tree
);
1586 has_else
= tree
->type
== pet_tree_if_else
;
1588 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1590 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1592 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
1593 pet_skip_info_if_extract_cond(&skip
, cond
, state
);
1595 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1596 set
= isl_pw_aff_non_zero_set(cond
);
1597 scop
= pet_scop_restrict(scop_then
, isl_set_params(isl_set_copy(set
)));
1600 set
= isl_set_subtract(isl_set_copy(valid
), set
);
1601 scop_else
= pet_scop_restrict(scop_else
, isl_set_params(set
));
1602 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
1605 scop
= pet_scop_resolve_nested(scop
);
1606 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
1608 if (pet_skip_info_has_skip(&skip
))
1609 scop
= pet_scop_prefix(scop
, 0);
1610 scop
= pet_skip_info_if_add(&skip
, scop
, 1);
1612 pet_context_free(pc
);
1616 /* Construct a pet_scop for an if statement within the context "pc".
1618 * If the condition fits the pattern of a conditional assignment,
1619 * then it is handled by scop_from_conditional_assignment.
1621 * Otherwise, we check if the condition is affine.
1622 * If so, we construct the scop in scop_from_affine_if.
1623 * Otherwise, we construct the scop in scop_from_non_affine_if.
1625 * We allow the condition to be dynamic, i.e., to refer to
1626 * scalars or array elements that may be written to outside
1627 * of the given if statement. These nested accesses are then represented
1628 * as output dimensions in the wrapping iteration domain.
1629 * If it is also written _inside_ the then or else branch, then
1630 * we treat the condition as non-affine.
1631 * As explained in extract_non_affine_if, this will introduce
1632 * an extra statement.
1633 * For aesthetic reasons, we want this statement to have a statement
1634 * number that is lower than those of the then and else branches.
1635 * In order to evaluate if we will need such a statement, however, we
1636 * first construct scops for the then and else branches.
1637 * We therefore reserve a statement number if we might have to
1638 * introduce such an extra statement.
1640 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
1641 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1645 pet_expr
*cond_expr
;
1646 pet_context
*pc_nested
;
1651 has_else
= tree
->type
== pet_tree_if_else
;
1653 pc
= pet_context_copy(pc
);
1654 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
1656 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
1658 if (is_conditional_assignment(tree
, pc
))
1659 return scop_from_conditional_assignment(tree
, pc
, state
);
1661 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
1662 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1663 pc_nested
= pet_context_copy(pc
);
1664 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1665 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1666 pet_context_free(pc_nested
);
1667 pet_expr_free(cond_expr
);
1670 pet_context_free(pc
);
1674 if (isl_pw_aff_involves_nan(cond
)) {
1675 isl_pw_aff_free(cond
);
1676 return scop_from_non_affine_if(tree
, pc
, state
);
1679 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
1680 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
1681 isl_pw_aff_free(cond
);
1682 return scop_from_non_affine_if(tree
, pc
, state
);
1685 return scop_from_affine_if(tree
, cond
, pc
, state
);
1688 /* Return a one-dimensional multi piecewise affine expression that is equal
1689 * to the constant 1 and is defined over a zero-dimensional domain.
1691 static __isl_give isl_multi_pw_aff
*one_mpa(isl_ctx
*ctx
)
1694 isl_local_space
*ls
;
1697 space
= isl_space_set_alloc(ctx
, 0, 0);
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.
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
)
1714 struct pet_scop
*scop
;
1717 ctx
= pet_tree_get_ctx(tree
);
1718 scop
= pet_scop_empty(ctx
);
1722 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(ctx
));
1727 /* Construct a pet_scop for a break statement.
1729 * We simply create an empty scop with both a universal pet_skip_now
1730 * skip condition and a universal pet_skip_later skip condition.
1731 * These skip conditions will then be taken into
1732 * account by the enclosing loop construct, possibly after
1733 * being incorporated into outer skip conditions.
1735 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
)
1737 struct pet_scop
*scop
;
1739 isl_multi_pw_aff
*skip
;
1741 ctx
= pet_tree_get_ctx(tree
);
1742 scop
= pet_scop_empty(ctx
);
1746 skip
= one_mpa(ctx
);
1747 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
1748 isl_multi_pw_aff_copy(skip
));
1749 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
1754 /* Extract a clone of the kill statement in "scop".
1755 * "scop" is expected to have been created from a DeclStmt
1756 * and should have the kill as its first statement.
1758 static struct pet_scop
*extract_kill(isl_ctx
*ctx
, struct pet_scop
*scop
,
1759 struct pet_state
*state
)
1762 struct pet_stmt
*stmt
;
1764 isl_multi_pw_aff
*index
;
1770 if (scop
->n_stmt
< 1)
1771 isl_die(ctx
, isl_error_internal
,
1772 "expecting at least one statement", return NULL
);
1773 stmt
= scop
->stmts
[0];
1774 if (!pet_stmt_is_kill(stmt
))
1775 isl_die(ctx
, isl_error_internal
,
1776 "expecting kill statement", return NULL
);
1778 arg
= pet_expr_get_arg(stmt
->body
, 0);
1779 index
= pet_expr_access_get_index(arg
);
1780 access
= pet_expr_access_get_access(arg
);
1782 index
= isl_multi_pw_aff_reset_tuple_id(index
, isl_dim_in
);
1783 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
1784 kill
= pet_expr_kill_from_access_and_index(access
, index
);
1785 domain
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
1786 stmt
= pet_stmt_from_pet_expr(domain
, pet_loc_copy(stmt
->loc
),
1787 NULL
, state
->n_stmt
++, kill
);
1788 return pet_scop_from_pet_stmt(ctx
, stmt
);
1791 /* Mark all arrays in "scop" as being exposed.
1793 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
1799 for (i
= 0; i
< scop
->n_array
; ++i
)
1800 scop
->arrays
[i
]->exposed
= 1;
1804 /* Try and construct a pet_scop corresponding to (part of)
1805 * a sequence of statements within the context "pc".
1807 * After extracting a statement, we update "pc"
1808 * based on the top-level assignments in the statement
1809 * so that we can exploit them in subsequent statements in the same block.
1811 * If there are any breaks or continues in the individual statements,
1812 * then we may have to compute a new skip condition.
1813 * This is handled using a pet_skip_info object.
1814 * On initialization, the object checks if skip conditions need
1815 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1816 * adds them in pet_skip_info_seq_add.
1818 * If "block" is set, then we need to insert kill statements at
1819 * the end of the block for any array that has been declared by
1820 * one of the statements in the sequence. Each of these declarations
1821 * results in the construction of a kill statement at the place
1822 * of the declaration, so we simply collect duplicates of
1823 * those kill statements and append these duplicates to the constructed scop.
1825 * If "block" is not set, then any array declared by one of the statements
1826 * in the sequence is marked as being exposed.
1828 * If autodetect is set, then we allow the extraction of only a subrange
1829 * of the sequence of statements. However, if there is at least one statement
1830 * for which we could not construct a scop and the final range contains
1831 * either no statements or at least one kill, then we discard the entire
1834 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
1835 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1839 struct pet_scop
*scop
, *kills
;
1841 ctx
= pet_tree_get_ctx(tree
);
1843 pc
= pet_context_copy(pc
);
1844 scop
= pet_scop_empty(ctx
);
1845 kills
= pet_scop_empty(ctx
);
1846 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
1847 struct pet_scop
*scop_i
;
1849 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
1850 pc
= scop_handle_writes(scop_i
, pc
);
1851 struct pet_skip_info skip
;
1852 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
1853 pet_skip_info_seq_extract(&skip
, state
);
1854 if (pet_skip_info_has_skip(&skip
))
1855 scop_i
= pet_scop_prefix(scop_i
, 0);
1856 if (scop_i
&& pet_tree_is_decl(tree
->u
.b
.child
[i
])) {
1857 if (tree
->u
.b
.block
) {
1858 struct pet_scop
*kill
;
1859 kill
= extract_kill(ctx
, scop_i
, state
);
1860 kills
= pet_scop_add_par(ctx
, kills
, kill
);
1862 scop_i
= mark_exposed(scop_i
);
1864 scop_i
= pet_scop_prefix(scop_i
, i
);
1865 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
1867 scop
= pet_skip_info_seq_add(&skip
, scop
, i
);
1873 kills
= pet_scop_prefix(kills
, tree
->u
.b
.n
);
1874 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
1876 pet_context_free(pc
);
1881 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1882 * within the context "pc" by calling the appropriate function
1883 * based on the type of "tree".
1885 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
1886 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1891 switch (tree
->type
) {
1892 case pet_tree_error
:
1894 case pet_tree_block
:
1895 return scop_from_block(tree
, pc
, state
);
1896 case pet_tree_break
:
1897 return scop_from_break(tree
);
1898 case pet_tree_continue
:
1899 return scop_from_continue(tree
);
1901 case pet_tree_decl_init
:
1902 return scop_from_decl(tree
, pc
, state
);
1904 return scop_from_expr(pet_expr_copy(tree
->u
.e
.expr
),
1905 isl_id_copy(tree
->label
),
1907 pet_tree_get_loc(tree
), pc
);
1909 case pet_tree_if_else
:
1910 return scop_from_if(tree
, pc
, state
);
1912 return scop_from_for(tree
, pc
, state
);
1913 case pet_tree_while
:
1914 return scop_from_while(tree
, pc
, state
);
1915 case pet_tree_infinite_loop
:
1916 return scop_from_infinite_for(tree
, pc
, state
);
1919 isl_die(tree
->ctx
, isl_error_internal
, "unhandled type",
1923 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1924 * "int_size" is the number of bytes need to represent an integer.
1925 * "extract_array" is a callback that we can use to create a pet_array
1926 * that corresponds to the variable accessed by an expression.
1928 * Initialize the global state, construct a context and then
1929 * construct the pet_scop by recursively visiting the tree.
1931 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
1932 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
1933 __isl_keep pet_context
*pc
, void *user
), void *user
,
1934 __isl_keep pet_context
*pc
)
1936 struct pet_scop
*scop
;
1937 struct pet_state state
= { 0 };
1942 state
.ctx
= pet_tree_get_ctx(tree
);
1943 state
.int_size
= int_size
;
1944 state
.extract_array
= extract_array
;
1947 scop
= scop_from_tree(tree
, pc
, &state
);
1948 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
1950 pet_tree_free(tree
);