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, mark all scalar variables that are written by "stmt"
48 * as having an unknown value.
50 static __isl_give pet_context
*handle_writes(struct pet_stmt
*stmt
,
51 __isl_take pet_context
*pc
)
53 return pet_context_clear_writes_in_expr(pc
, stmt
->body
);
56 /* Update "pc" based on the write accesses in "scop".
58 static __isl_give pet_context
*scop_handle_writes(struct pet_scop
*scop
,
59 __isl_take pet_context
*pc
)
64 return pet_context_free(pc
);
65 for (i
= 0; i
< scop
->n_stmt
; ++i
)
66 pc
= handle_writes(scop
->stmts
[i
], pc
);
71 /* Convert a top-level pet_expr to a pet_scop with one statement
72 * within the context "pc".
73 * This mainly involves resolving nested expression parameters
74 * and setting the name of the iteration space.
75 * The name is given by "label" if it is non-NULL. Otherwise,
76 * it is of the form S_<stmt_nr>.
77 * The location of the statement is set to "loc".
79 static struct pet_scop
*scop_from_expr(__isl_take pet_expr
*expr
,
80 __isl_take isl_id
*label
, int stmt_nr
, __isl_take pet_loc
*loc
,
81 __isl_keep pet_context
*pc
)
86 expr
= pet_expr_plug_in_args(expr
, pc
);
87 expr
= pet_expr_resolve_nested(expr
);
88 expr
= pet_expr_resolve_assume(expr
, pc
);
89 domain
= pet_context_get_domain(pc
);
90 ps
= pet_stmt_from_pet_expr(domain
, loc
, label
, stmt_nr
, expr
);
91 return pet_scop_from_pet_stmt(pet_context_get_space(pc
), ps
);
94 /* Construct a pet_scop with a single statement killing the entire
96 * The location of the statement is set to "loc".
98 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
99 __isl_keep pet_context
*pc
, struct pet_state
*state
)
104 isl_multi_pw_aff
*index
;
107 struct pet_scop
*scop
;
111 ctx
= isl_set_get_ctx(array
->extent
);
112 access
= isl_map_from_range(isl_set_copy(array
->extent
));
113 id
= isl_set_get_tuple_id(array
->extent
);
114 space
= isl_space_alloc(ctx
, 0, 0, 0);
115 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
116 index
= isl_multi_pw_aff_zero(space
);
117 expr
= pet_expr_kill_from_access_and_index(access
, index
);
118 return scop_from_expr(expr
, NULL
, state
->n_stmt
++, loc
, pc
);
124 /* Construct and return a pet_array corresponding to the variable
125 * accessed by "access" by calling the extract_array callback.
127 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
128 __isl_keep pet_context
*pc
, struct pet_state
*state
)
130 return state
->extract_array(access
, pc
, state
->user
);
133 /* Construct a pet_scop for a (single) variable declaration
134 * within the context "pc".
136 * The scop contains the variable being declared (as an array)
137 * and a statement killing the array.
139 * If the declaration comes with an initialization, then the scop
140 * also contains an assignment to the variable.
142 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
143 __isl_keep pet_context
*pc
, struct pet_state
*state
)
147 struct pet_array
*array
;
148 struct pet_scop
*scop_decl
, *scop
;
149 pet_expr
*lhs
, *rhs
, *pe
;
151 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
154 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
155 scop_decl
= pet_scop_add_array(scop_decl
, array
);
157 if (tree
->type
!= pet_tree_decl_init
)
160 lhs
= pet_expr_copy(tree
->u
.d
.var
);
161 rhs
= pet_expr_copy(tree
->u
.d
.init
);
162 type_size
= pet_expr_get_type_size(lhs
);
163 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
164 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
165 pet_tree_get_loc(tree
), pc
);
167 scop_decl
= pet_scop_prefix(scop_decl
, 0);
168 scop
= pet_scop_prefix(scop
, 1);
170 ctx
= pet_tree_get_ctx(tree
);
171 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
176 /* Embed the given iteration domain in an extra outer loop
177 * with induction variable "var".
178 * If this variable appeared as a parameter in the constraints,
179 * it is replaced by the new outermost dimension.
181 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
182 __isl_take isl_id
*var
)
186 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
187 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
189 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
190 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
197 /* Return those elements in the space of "cond" that come after
198 * (based on "sign") an element in "cond".
200 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
202 isl_map
*previous_to_this
;
205 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
207 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
209 cond
= isl_set_apply(cond
, previous_to_this
);
214 /* Remove those iterations of "domain" that have an earlier iteration
215 * (based on "sign") where "skip" is satisfied.
216 * "domain" has an extra outer loop compared to "skip".
217 * The skip condition is first embedded in the same space as "domain".
218 * If "apply_skip_map" is set, then "skip_map" is first applied
219 * to the embedded skip condition before removing it from the domain.
221 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
222 __isl_take isl_set
*skip
, int sign
,
223 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
225 skip
= embed(skip
, isl_set_get_dim_id(domain
, isl_dim_set
, 0));
227 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
228 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
229 return isl_set_subtract(domain
, after(skip
, sign
));
232 /* Create the infinite iteration domain
236 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
)
238 isl_ctx
*ctx
= isl_id_get_ctx(id
);
241 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
242 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
247 /* Create an identity affine expression on the space containing "domain",
248 * which is assumed to be one-dimensional.
250 static __isl_give isl_aff
*identity_aff(__isl_keep isl_set
*domain
)
254 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
255 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
258 /* Create an affine expression that maps elements
259 * of a single-dimensional array "id_test" to the previous element
260 * (according to "inc"), provided this element belongs to "domain".
261 * That is, create the affine expression
263 * { id[x] -> id[x - inc] : x - inc in domain }
265 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
266 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
271 isl_multi_pw_aff
*prev
;
273 space
= isl_set_get_space(domain
);
274 ls
= isl_local_space_from_space(space
);
275 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
276 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
277 prev
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
278 domain
= isl_set_preimage_multi_pw_aff(domain
,
279 isl_multi_pw_aff_copy(prev
));
280 prev
= isl_multi_pw_aff_intersect_domain(prev
, domain
);
281 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
286 /* Add an implication to "scop" expressing that if an element of
287 * virtual array "id_test" has value "satisfied" then all previous elements
288 * of this array also have that value. The set of previous elements
289 * is bounded by "domain". If "sign" is negative then the iterator
290 * is decreasing and we express that all subsequent array elements
291 * (but still defined previously) have the same value.
293 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
294 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
300 domain
= isl_set_set_tuple_id(domain
, id_test
);
301 space
= isl_set_get_space(domain
);
303 map
= isl_map_lex_ge(space
);
305 map
= isl_map_lex_le(space
);
306 map
= isl_map_intersect_range(map
, domain
);
307 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
312 /* Add a filter to "scop" that imposes that it is only executed
313 * when the variable identified by "id_test" has a zero value
314 * for all previous iterations of "domain".
316 * In particular, add a filter that imposes that the array
317 * has a zero value at the previous iteration of domain and
318 * add an implication that implies that it then has that
319 * value for all previous iterations.
321 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
322 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
323 __isl_take isl_val
*inc
)
325 isl_multi_pw_aff
*prev
;
326 int sign
= isl_val_sgn(inc
);
328 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
329 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
330 scop
= pet_scop_filter(scop
, prev
, 0);
335 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
336 __isl_keep pet_context
*pc
, struct pet_state
*state
);
338 /* Construct a pet_scop for an infinite loop around the given body
339 * within the context "pc".
341 * We extract a pet_scop for the body and then embed it in a loop with
350 * If the body contains any break, then it is taken into
351 * account in apply_affine_break (if the skip condition is affine)
352 * or in scop_add_break (if the skip condition is not affine).
354 * Note that in case of an affine skip condition,
355 * since we are dealing with a loop without loop iterator,
356 * the skip condition cannot refer to the current loop iterator and
357 * so effectively, the iteration domain is of the form
359 * { [0]; [t] : t >= 1 and not skip }
361 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
362 __isl_keep pet_context
*pc
, struct pet_state
*state
)
365 isl_id
*id
, *id_test
;
369 struct pet_scop
*scop
;
370 int has_affine_break
;
373 ctx
= pet_tree_get_ctx(body
);
374 id
= isl_id_alloc(ctx
, "t", NULL
);
375 domain
= infinite_domain(isl_id_copy(id
));
376 ident
= identity_aff(domain
);
378 scop
= scop_from_tree(body
, pc
, state
);
380 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
381 if (has_affine_break
)
382 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
383 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
385 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
387 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
388 isl_aff_copy(ident
), ident
, id
);
389 if (has_affine_break
) {
390 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
391 scop
= pet_scop_intersect_domain_prefix(scop
,
392 isl_set_copy(domain
));
395 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
397 isl_set_free(domain
);
402 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
407 * within the context "pc".
409 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
410 __isl_keep pet_context
*pc
, struct pet_state
*state
)
412 struct pet_scop
*scop
;
414 pc
= pet_context_copy(pc
);
415 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
417 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
419 pet_context_free(pc
);
424 /* Construct a pet_scop for a while loop of the form
429 * within the context "pc".
430 * In particular, construct a scop for an infinite loop around body and
431 * intersect the domain with the affine expression.
432 * Note that this intersection may result in an empty loop.
434 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
435 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
436 struct pet_state
*state
)
438 struct pet_scop
*scop
;
442 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
443 dom
= isl_pw_aff_non_zero_set(pa
);
444 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
445 scop
= pet_scop_restrict(scop
, isl_set_params(dom
));
446 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
448 pet_context_free(pc
);
452 /* Construct a scop for a while, given the scops for the condition
453 * and the body, the filter identifier and the iteration domain of
456 * In particular, the scop for the condition is filtered to depend
457 * on "id_test" evaluating to true for all previous iterations
458 * of the loop, while the scop for the body is filtered to depend
459 * on "id_test" evaluating to true for all iterations up to the
461 * The actual filter only imposes that this virtual array has
462 * value one on the previous or the current iteration.
463 * The fact that this condition also applies to the previous
464 * iterations is enforced by an implication.
466 * These filtered scops are then combined into a single scop.
468 * "sign" is positive if the iterator increases and negative
471 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
472 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
473 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
475 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
477 isl_multi_pw_aff
*test_index
;
478 isl_multi_pw_aff
*prev
;
479 int sign
= isl_val_sgn(inc
);
480 struct pet_scop
*scop
;
482 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
483 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
485 space
= isl_space_map_from_set(isl_set_get_space(domain
));
486 test_index
= isl_multi_pw_aff_identity(space
);
487 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
488 isl_id_copy(id_test
));
489 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
491 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
492 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
497 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
498 * evaluating "cond" and writing the result to a virtual scalar,
499 * as expressed by "index".
500 * Do so within the context "pc".
501 * The location of the statement is set to "loc".
503 static struct pet_scop
*scop_from_non_affine_condition(
504 __isl_take pet_expr
*cond
, int stmt_nr
,
505 __isl_take isl_multi_pw_aff
*index
,
506 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
508 pet_expr
*expr
, *write
;
510 write
= pet_expr_from_index(index
);
511 write
= pet_expr_access_set_write(write
, 1);
512 write
= pet_expr_access_set_read(write
, 0);
513 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
515 return scop_from_expr(expr
, NULL
, stmt_nr
, loc
, pc
);
518 /* Construct a generic while scop, with iteration domain
519 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
520 * The scop consists of two parts,
521 * one for evaluating the condition "cond" and one for the body.
522 * If "expr_inc" is not NULL, then a scop for evaluating this expression
523 * is added at the end of the body,
524 * after replacing any skip conditions resulting from continue statements
525 * by the skip conditions resulting from break statements (if any).
527 * The schedule is adjusted to reflect that the condition is evaluated
528 * before the body is executed and the body is filtered to depend
529 * on the result of the condition evaluating to true on all iterations
530 * up to the current iteration, while the evaluation of the condition itself
531 * is filtered to depend on the result of the condition evaluating to true
532 * on all previous iterations.
533 * The context of the scop representing the body is dropped
534 * because we don't know how many times the body will be executed,
537 * If the body contains any break, then it is taken into
538 * account in apply_affine_break (if the skip condition is affine)
539 * or in scop_add_break (if the skip condition is not affine).
541 * Note that in case of an affine skip condition,
542 * since we are dealing with a loop without loop iterator,
543 * the skip condition cannot refer to the current loop iterator and
544 * so effectively, the iteration domain is of the form
546 * { [0]; [t] : t >= 1 and not skip }
548 static struct pet_scop
*scop_from_non_affine_while(__isl_take pet_expr
*cond
,
549 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
550 __isl_take pet_expr
*expr_inc
, __isl_take pet_context
*pc
,
551 struct pet_state
*state
)
554 isl_id
*id
, *id_test
, *id_break_test
;
556 isl_multi_pw_aff
*test_index
;
560 struct pet_scop
*scop
, *scop_body
;
561 int has_affine_break
;
565 space
= pet_context_get_space(pc
);
566 test_index
= pet_create_test_index(space
, state
->n_test
++);
567 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
568 isl_multi_pw_aff_copy(test_index
),
569 pet_loc_copy(loc
), pc
);
570 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
571 domain
= pet_context_get_domain(pc
);
572 scop
= pet_scop_add_boolean_array(scop
, domain
,
573 test_index
, state
->int_size
);
575 id
= isl_id_alloc(ctx
, "t", NULL
);
576 domain
= infinite_domain(isl_id_copy(id
));
577 ident
= identity_aff(domain
);
579 scop_body
= scop_from_tree(tree_body
, pc
, state
);
581 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
582 if (has_affine_break
)
583 skip
= pet_scop_get_affine_skip_domain(scop_body
,
585 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
587 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
589 scop
= pet_scop_prefix(scop
, 0);
590 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_aff_copy(ident
),
591 isl_aff_copy(ident
), isl_id_copy(id
));
592 scop_body
= pet_scop_reset_context(scop_body
);
593 scop_body
= pet_scop_prefix(scop_body
, 1);
595 struct pet_scop
*scop_inc
;
596 scop_inc
= scop_from_expr(expr_inc
, NULL
, state
->n_stmt
++,
598 scop_inc
= pet_scop_prefix(scop_inc
, 2);
599 if (pet_scop_has_skip(scop_body
, pet_skip_later
)) {
600 isl_multi_pw_aff
*skip
;
601 skip
= pet_scop_get_skip(scop_body
, pet_skip_later
);
602 scop_body
= pet_scop_set_skip(scop_body
,
605 pet_scop_reset_skip(scop_body
, pet_skip_now
);
606 scop_body
= pet_scop_add_seq(ctx
, scop_body
, scop_inc
);
609 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
610 isl_aff_copy(ident
), ident
, id
);
612 if (has_affine_break
) {
613 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
614 scop
= pet_scop_intersect_domain_prefix(scop
,
615 isl_set_copy(domain
));
616 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
617 isl_set_copy(domain
));
620 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
621 isl_set_copy(domain
), isl_val_one(ctx
));
622 scop_body
= scop_add_break(scop_body
, id_break_test
,
623 isl_set_copy(domain
), isl_val_one(ctx
));
625 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
628 pet_context_free(pc
);
632 /* Check if the while loop is of the form
634 * while (affine expression)
637 * If so, call scop_from_affine_while to construct a scop.
639 * Otherwise, pass control to scop_from_non_affine_while.
641 * "pc" is the context in which the affine expressions in the scop are created.
643 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
644 __isl_keep pet_context
*pc
, struct pet_state
*state
)
652 pc
= pet_context_copy(pc
);
653 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
655 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
656 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
657 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
658 pet_expr_free(cond_expr
);
663 if (!isl_pw_aff_involves_nan(pa
))
664 return scop_from_affine_while(tree
, pa
, pc
, state
);
666 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
667 pet_tree_get_loc(tree
), tree
->u
.l
.body
, NULL
,
670 pet_context_free(pc
);
674 /* Check whether "cond" expresses a simple loop bound
675 * on the only set dimension.
676 * In particular, if "up" is set then "cond" should contain only
677 * upper bounds on the set dimension.
678 * Otherwise, it should contain only lower bounds.
680 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
682 if (isl_val_is_pos(inc
))
683 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
685 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
688 /* Extend a condition on a given iteration of a loop to one that
689 * imposes the same condition on all previous iterations.
690 * "domain" expresses the lower [upper] bound on the iterations
691 * when inc is positive [negative].
693 * In particular, we construct the condition (when inc is positive)
695 * forall i' : (domain(i') and i' <= i) => cond(i')
697 * which is equivalent to
699 * not exists i' : domain(i') and i' <= i and not cond(i')
701 * We construct this set by negating cond, applying a map
703 * { [i'] -> [i] : domain(i') and i' <= i }
705 * and then negating the result again.
707 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
708 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
710 isl_map
*previous_to_this
;
712 if (isl_val_is_pos(inc
))
713 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
715 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
717 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
719 cond
= isl_set_complement(cond
);
720 cond
= isl_set_apply(cond
, previous_to_this
);
721 cond
= isl_set_complement(cond
);
728 /* Construct a domain of the form
730 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
732 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
733 __isl_take isl_pw_aff
*init
, __isl_take isl_val
*inc
)
739 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
740 dim
= isl_pw_aff_get_domain_space(init
);
741 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
742 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, 0, inc
);
743 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
745 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
746 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
747 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
748 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
750 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
752 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
754 return isl_set_params(set
);
757 /* Assuming "cond" represents a bound on a loop where the loop
758 * iterator "iv" is incremented (or decremented) by one, check if wrapping
761 * Under the given assumptions, wrapping is only possible if "cond" allows
762 * for the last value before wrapping, i.e., 2^width - 1 in case of an
763 * increasing iterator and 0 in case of a decreasing iterator.
765 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
766 __isl_keep isl_val
*inc
)
773 test
= isl_set_copy(cond
);
775 ctx
= isl_set_get_ctx(test
);
776 if (isl_val_is_neg(inc
))
777 limit
= isl_val_zero(ctx
);
779 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
780 limit
= isl_val_2exp(limit
);
781 limit
= isl_val_sub_ui(limit
, 1);
784 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
785 cw
= !isl_set_is_empty(test
);
791 /* Given a one-dimensional space, construct the following affine expression
794 * { [v] -> [v mod 2^width] }
796 * where width is the number of bits used to represent the values
797 * of the unsigned variable "iv".
799 static __isl_give isl_aff
*compute_wrapping(__isl_take isl_space
*dim
,
800 __isl_keep pet_expr
*iv
)
806 ctx
= isl_space_get_ctx(dim
);
807 mod
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
808 mod
= isl_val_2exp(mod
);
810 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
811 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
812 aff
= isl_aff_mod_val(aff
, mod
);
817 /* Project out the parameter "id" from "set".
819 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
820 __isl_keep isl_id
*id
)
824 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
826 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
831 /* Compute the set of parameters for which "set1" is a subset of "set2".
833 * set1 is a subset of set2 if
835 * forall i in set1 : i in set2
839 * not exists i in set1 and i not in set2
843 * not exists i in set1 \ set2
845 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
846 __isl_take isl_set
*set2
)
848 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
851 /* Compute the set of parameter values for which "cond" holds
852 * on the next iteration for each element of "dom".
854 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
855 * and then compute the set of parameters for which the result is a subset
858 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
859 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
865 space
= isl_set_get_space(dom
);
866 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
867 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
868 aff
= isl_aff_add_constant_val(aff
, inc
);
869 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
871 dom
= isl_set_apply(dom
, next
);
873 return enforce_subset(dom
, cond
);
876 /* Extract the for loop "tree" as a while loop within the context "pc".
878 * That is, the for loop has the form
880 * for (iv = init; cond; iv += inc)
891 * except that the skips resulting from any continue statements
892 * in body do not apply to the increment, but are replaced by the skips
893 * resulting from break statements.
895 * If the loop iterator is declared in the for loop, then it is killed before
896 * and after the loop.
898 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
899 __isl_take pet_context
*pc
, struct pet_state
*state
)
903 pet_expr
*expr_iv
, *init
, *inc
;
904 struct pet_scop
*scop_init
, *scop
;
906 struct pet_array
*array
;
907 struct pet_scop
*scop_kill
;
909 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
910 pc
= pet_context_mark_assigned(pc
, iv
);
912 declared
= tree
->u
.l
.declared
;
914 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
915 type_size
= pet_expr_get_type_size(expr_iv
);
916 init
= pet_expr_copy(tree
->u
.l
.init
);
917 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
918 scop_init
= scop_from_expr(init
, NULL
, state
->n_stmt
++,
919 pet_tree_get_loc(tree
), pc
);
920 scop_init
= pet_scop_prefix(scop_init
, declared
);
922 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
923 type_size
= pet_expr_get_type_size(expr_iv
);
924 inc
= pet_expr_copy(tree
->u
.l
.inc
);
925 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
927 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
928 pet_tree_get_loc(tree
), tree
->u
.l
.body
, inc
,
929 pet_context_copy(pc
), state
);
931 scop
= pet_scop_prefix(scop
, declared
+ 1);
932 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
935 pet_context_free(pc
);
939 array
= extract_array(tree
->u
.l
.iv
, pc
, state
);
942 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
943 scop_kill
= pet_scop_prefix(scop_kill
, 0);
944 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
945 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
946 scop_kill
= pet_scop_add_array(scop_kill
, array
);
947 scop_kill
= pet_scop_prefix(scop_kill
, 3);
948 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
950 pet_context_free(pc
);
954 /* Given an access expression "expr", is the variable accessed by
955 * "expr" assigned anywhere inside "tree"?
957 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
962 id
= pet_expr_access_get_id(expr
);
963 assigned
= pet_tree_writes(tree
, id
);
969 /* Are all nested access parameters in "pa" allowed given "tree".
970 * In particular, is none of them written by anywhere inside "tree".
972 * If "tree" has any continue nodes in the current loop level,
973 * then no nested access parameters are allowed.
974 * In particular, if there is any nested access in a guard
975 * for a piece of code containing a "continue", then we want to introduce
976 * a separate statement for evaluating this guard so that we can express
977 * that the result is false for all previous iterations.
979 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
980 __isl_keep pet_tree
*tree
)
987 if (!pet_nested_any_in_pw_aff(pa
))
990 if (pet_tree_has_continue(tree
))
993 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
994 for (i
= 0; i
< nparam
; ++i
) {
995 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
999 if (!pet_nested_in_id(id
)) {
1004 expr
= pet_nested_extract_expr(id
);
1005 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1006 !is_assigned(expr
, tree
);
1008 pet_expr_free(expr
);
1018 /* Construct a pet_scop for a for tree with static affine initialization
1019 * and constant increment within the context "pc".
1021 * The condition is allowed to contain nested accesses, provided
1022 * they are not being written to inside the body of the loop.
1023 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1024 * essentially treated as a while loop, with iteration domain
1025 * { [i] : i >= init }.
1027 * We extract a pet_scop for the body and then embed it in a loop with
1028 * iteration domain and schedule
1030 * { [i] : i >= init and condition' }
1035 * { [i] : i <= init and condition' }
1038 * Where condition' is equal to condition if the latter is
1039 * a simple upper [lower] bound and a condition that is extended
1040 * to apply to all previous iterations otherwise.
1042 * If the condition is non-affine, then we drop the condition from the
1043 * iteration domain and instead create a separate statement
1044 * for evaluating the condition. The body is then filtered to depend
1045 * on the result of the condition evaluating to true on all iterations
1046 * up to the current iteration, while the evaluation the condition itself
1047 * is filtered to depend on the result of the condition evaluating to true
1048 * on all previous iterations.
1049 * The context of the scop representing the body is dropped
1050 * because we don't know how many times the body will be executed,
1053 * If the stride of the loop is not 1, then "i >= init" is replaced by
1055 * (exists a: i = init + stride * a and a >= 0)
1057 * If the loop iterator i is unsigned, then wrapping may occur.
1058 * We therefore use a virtual iterator instead that does not wrap.
1059 * However, the condition in the code applies
1060 * to the wrapped value, so we need to change condition(i)
1061 * into condition([i % 2^width]). Similarly, we replace all accesses
1062 * to the original iterator by the wrapping of the virtual iterator.
1063 * Note that there may be no need to perform this final wrapping
1064 * if the loop condition (after wrapping) satisfies certain conditions.
1065 * However, the is_simple_bound condition is not enough since it doesn't
1066 * check if there even is an upper bound.
1068 * Wrapping on unsigned iterators can be avoided entirely if
1069 * loop condition is simple, the loop iterator is incremented
1070 * [decremented] by one and the last value before wrapping cannot
1071 * possibly satisfy the loop condition.
1073 * Valid parameters for a for loop are those for which the initial
1074 * value itself, the increment on each domain iteration and
1075 * the condition on both the initial value and
1076 * the result of incrementing the iterator for each iteration of the domain
1078 * If the loop condition is non-affine, then we only consider validity
1079 * of the initial value.
1081 * If the body contains any break, then we keep track of it in "skip"
1082 * (if the skip condition is affine) or it is handled in scop_add_break
1083 * (if the skip condition is not affine).
1084 * Note that the affine break condition needs to be considered with
1085 * respect to previous iterations in the virtual domain (if any).
1087 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1088 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1089 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1090 struct pet_state
*state
)
1092 isl_local_space
*ls
;
1095 isl_set
*cond
= NULL
;
1096 isl_set
*skip
= NULL
;
1097 isl_id
*id
, *id_test
= NULL
, *id_break_test
;
1098 struct pet_scop
*scop
, *scop_cond
= NULL
;
1104 int has_affine_break
;
1106 isl_map
*rev_wrap
= NULL
;
1107 isl_aff
*wrap
= NULL
;
1109 isl_set
*valid_init
;
1110 isl_set
*valid_cond
;
1111 isl_set
*valid_cond_init
;
1112 isl_set
*valid_cond_next
;
1114 pet_expr
*cond_expr
;
1115 pet_context
*pc_nested
;
1117 id
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1119 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1120 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1121 pc_nested
= pet_context_copy(pc
);
1122 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1123 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1124 pet_context_free(pc_nested
);
1125 pet_expr_free(cond_expr
);
1127 valid_inc
= isl_pw_aff_domain(pa_inc
);
1129 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1131 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1132 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1134 pa
= isl_pw_aff_free(pa
);
1136 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1137 cond
= isl_pw_aff_non_zero_set(pa
);
1139 cond
= isl_set_universe(isl_space_set_alloc(state
->ctx
, 0, 0));
1141 cond
= embed(cond
, isl_id_copy(id
));
1142 valid_cond
= isl_set_coalesce(valid_cond
);
1143 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
1144 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
1145 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1146 is_virtual
= is_unsigned
&&
1147 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1149 valid_cond_init
= enforce_subset(
1150 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val
))),
1151 isl_set_copy(valid_cond
));
1152 if (is_one
&& !is_virtual
) {
1153 isl_pw_aff_free(init_val
);
1154 pa
= pet_expr_extract_comparison(
1155 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1156 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1157 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1158 valid_init
= set_project_out_by_id(valid_init
, id
);
1159 domain
= isl_pw_aff_non_zero_set(pa
);
1161 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1162 domain
= strided_domain(isl_id_copy(id
), init_val
,
1166 domain
= embed(domain
, isl_id_copy(id
));
1168 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1169 rev_wrap
= isl_map_from_aff(isl_aff_copy(wrap
));
1170 rev_wrap
= isl_map_reverse(rev_wrap
);
1171 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1172 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1173 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1175 is_simple
= is_simple_bound(cond
, inc
);
1177 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1178 is_simple
= is_simple_bound(cond
, inc
);
1181 cond
= valid_for_each_iteration(cond
,
1182 isl_set_copy(domain
), isl_val_copy(inc
));
1183 domain
= isl_set_intersect(domain
, cond
);
1184 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1185 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
1186 sched
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1187 if (isl_val_is_neg(inc
))
1188 sched
= isl_aff_neg(sched
);
1190 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1192 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1195 wrap
= identity_aff(domain
);
1197 if (is_non_affine
) {
1199 isl_multi_pw_aff
*test_index
;
1200 space
= pet_context_get_space(pc
);
1201 test_index
= pet_create_test_index(space
, state
->n_test
++);
1202 scop_cond
= scop_from_non_affine_condition(
1203 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1204 isl_multi_pw_aff_copy(test_index
),
1205 pet_tree_get_loc(tree
), pc
);
1206 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1208 scop_cond
= pet_scop_add_boolean_array(scop_cond
,
1209 pet_context_get_domain(pc
), test_index
,
1211 scop_cond
= pet_scop_prefix(scop_cond
, 0);
1212 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
1213 isl_aff_copy(sched
), isl_aff_copy(wrap
),
1217 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
1218 has_affine_break
= scop
&&
1219 pet_scop_has_affine_skip(scop
, pet_skip_later
);
1220 if (has_affine_break
)
1221 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1222 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
1224 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1225 if (is_non_affine
) {
1226 scop
= pet_scop_reset_context(scop
);
1227 scop
= pet_scop_prefix(scop
, 1);
1229 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
, wrap
, id
);
1230 scop
= pet_scop_resolve_nested(scop
);
1231 if (has_affine_break
) {
1232 domain
= apply_affine_break(domain
, skip
, isl_val_sgn(inc
),
1233 is_virtual
, rev_wrap
);
1234 scop
= pet_scop_intersect_domain_prefix(scop
,
1235 isl_set_copy(domain
));
1237 isl_map_free(rev_wrap
);
1239 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
1241 if (is_non_affine
) {
1242 scop
= scop_add_while(scop_cond
, scop
, id_test
, domain
,
1244 isl_set_free(valid_inc
);
1246 scop
= pet_scop_restrict_context(scop
, valid_inc
);
1247 scop
= pet_scop_restrict_context(scop
, valid_cond_next
);
1248 scop
= pet_scop_restrict_context(scop
, valid_cond_init
);
1249 isl_set_free(domain
);
1254 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid_init
));
1256 pet_context_free(pc
);
1260 /* Construct a pet_scop for a for statement within the context of "pc".
1262 * We update the context to reflect the writes to the loop variable and
1263 * the writes inside the body.
1265 * Then we check if the initialization of the for loop
1266 * is a static affine value and the increment is a constant.
1267 * If so, we construct the pet_scop using scop_from_affine_for.
1268 * Otherwise, we treat the for loop as a while loop
1269 * in scop_from_non_affine_for.
1271 static struct pet_scop
*scop_from_for(__isl_keep pet_tree
*tree
,
1272 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1276 isl_pw_aff
*pa_inc
, *init_val
;
1277 pet_context
*pc_init_val
;
1282 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1283 pc
= pet_context_copy(pc
);
1284 pc
= pet_context_clear_value(pc
, iv
);
1285 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1287 pc_init_val
= pet_context_copy(pc
);
1288 pc_init_val
= pet_context_mark_unknown(pc_init_val
, isl_id_copy(iv
));
1289 init_val
= pet_expr_extract_affine(tree
->u
.l
.init
, pc_init_val
);
1290 pet_context_free(pc_init_val
);
1291 pa_inc
= pet_expr_extract_affine(tree
->u
.l
.inc
, pc
);
1292 inc
= pet_extract_cst(pa_inc
);
1293 if (!pa_inc
|| !init_val
|| !inc
)
1295 if (!isl_pw_aff_involves_nan(pa_inc
) &&
1296 !isl_pw_aff_involves_nan(init_val
) && !isl_val_is_nan(inc
))
1297 return scop_from_affine_for(tree
, init_val
, pa_inc
, inc
,
1300 isl_pw_aff_free(pa_inc
);
1301 isl_pw_aff_free(init_val
);
1303 return scop_from_non_affine_for(tree
, pc
, state
);
1305 isl_pw_aff_free(pa_inc
);
1306 isl_pw_aff_free(init_val
);
1308 pet_context_free(pc
);
1312 /* Check whether "expr" is an affine constraint within the context "pc".
1314 static int is_affine_condition(__isl_keep pet_expr
*expr
,
1315 __isl_keep pet_context
*pc
)
1320 pa
= pet_expr_extract_affine_condition(expr
, pc
);
1323 is_affine
= !isl_pw_aff_involves_nan(pa
);
1324 isl_pw_aff_free(pa
);
1329 /* Check if the given if statement is a conditional assignement
1330 * with a non-affine condition.
1332 * In particular we check if "stmt" is of the form
1339 * where the condition is non-affine and a is some array or scalar access.
1341 static int is_conditional_assignment(__isl_keep pet_tree
*tree
,
1342 __isl_keep pet_context
*pc
)
1346 pet_expr
*expr1
, *expr2
;
1348 ctx
= pet_tree_get_ctx(tree
);
1349 if (!pet_options_get_detect_conditional_assignment(ctx
))
1351 if (tree
->type
!= pet_tree_if_else
)
1353 if (tree
->u
.i
.then_body
->type
!= pet_tree_expr
)
1355 if (tree
->u
.i
.else_body
->type
!= pet_tree_expr
)
1357 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1358 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1359 if (pet_expr_get_type(expr1
) != pet_expr_op
)
1361 if (pet_expr_get_type(expr2
) != pet_expr_op
)
1363 if (pet_expr_op_get_type(expr1
) != pet_op_assign
)
1365 if (pet_expr_op_get_type(expr2
) != pet_op_assign
)
1367 expr1
= pet_expr_get_arg(expr1
, 0);
1368 expr2
= pet_expr_get_arg(expr2
, 0);
1369 equal
= pet_expr_is_equal(expr1
, expr2
);
1370 pet_expr_free(expr1
);
1371 pet_expr_free(expr2
);
1372 if (equal
< 0 || !equal
)
1374 if (is_affine_condition(tree
->u
.i
.cond
, pc
))
1380 /* Given that "tree" is of the form
1387 * where a is some array or scalar access, construct a pet_scop
1388 * corresponding to this conditional assignment within the context "pc".
1390 * The constructed pet_scop then corresponds to the expression
1392 * a = condition ? f(...) : g(...)
1394 * All access relations in f(...) are intersected with condition
1395 * while all access relation in g(...) are intersected with the complement.
1397 static struct pet_scop
*scop_from_conditional_assignment(
1398 __isl_keep pet_tree
*tree
, __isl_take pet_context
*pc
,
1399 struct pet_state
*state
)
1403 isl_set
*cond
, *comp
;
1404 isl_multi_pw_aff
*index
;
1405 pet_expr
*expr1
, *expr2
;
1406 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
1407 pet_context
*pc_nested
;
1408 struct pet_scop
*scop
;
1410 pe_cond
= pet_expr_copy(tree
->u
.i
.cond
);
1411 pe_cond
= pet_expr_plug_in_args(pe_cond
, pc
);
1412 pc_nested
= pet_context_copy(pc
);
1413 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1414 pa
= pet_expr_extract_affine_condition(pe_cond
, pc_nested
);
1415 pet_context_free(pc_nested
);
1416 pet_expr_free(pe_cond
);
1417 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
1418 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
1419 index
= isl_multi_pw_aff_from_pw_aff(pa
);
1421 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1422 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1424 pe_cond
= pet_expr_from_index(index
);
1426 pe_then
= pet_expr_get_arg(expr1
, 1);
1427 pe_then
= pet_expr_restrict(pe_then
, cond
);
1428 pe_else
= pet_expr_get_arg(expr2
, 1);
1429 pe_else
= pet_expr_restrict(pe_else
, comp
);
1430 pe_write
= pet_expr_get_arg(expr1
, 0);
1432 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
1433 type_size
= pet_expr_get_type_size(pe_write
);
1434 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, pe_write
, pe
);
1436 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
1437 pet_tree_get_loc(tree
), pc
);
1439 pet_context_free(pc
);
1444 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1446 * We create a separate statement that writes the result
1447 * of the non-affine condition to a virtual scalar.
1448 * A constraint requiring the value of this virtual scalar to be one
1449 * is added to the iteration domains of the then branch.
1450 * Similarly, a constraint requiring the value of this virtual scalar
1451 * to be zero is added to the iteration domains of the else branch, if any.
1452 * We adjust the schedules to ensure that the virtual scalar is written
1453 * before it is read.
1455 * If there are any breaks or continues in the then and/or else
1456 * branches, then we may have to compute a new skip condition.
1457 * This is handled using a pet_skip_info object.
1458 * On initialization, the object checks if skip conditions need
1459 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1460 * adds them in pet_skip_info_if_add.
1462 static struct pet_scop
*scop_from_non_affine_if(__isl_keep pet_tree
*tree
,
1463 __isl_take pet_context
*pc
, struct pet_state
*state
)
1468 isl_multi_pw_aff
*test_index
;
1469 struct pet_skip_info skip
;
1470 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1472 has_else
= tree
->type
== pet_tree_if_else
;
1474 space
= pet_context_get_space(pc
);
1475 test_index
= pet_create_test_index(space
, state
->n_test
++);
1476 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
1477 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
1478 pet_tree_get_loc(tree
), pc
);
1479 domain
= pet_context_get_domain(pc
);
1480 scop
= pet_scop_add_boolean_array(scop
, domain
,
1481 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1483 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1485 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1487 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1489 pet_skip_info_if_extract_index(&skip
, test_index
, pc
, state
);
1491 scop
= pet_scop_prefix(scop
, 0);
1492 scop_then
= pet_scop_prefix(scop_then
, 1);
1493 scop_then
= pet_scop_filter(scop_then
,
1494 isl_multi_pw_aff_copy(test_index
), 1);
1496 scop_else
= pet_scop_prefix(scop_else
, 1);
1497 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1498 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1500 isl_multi_pw_aff_free(test_index
);
1502 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1504 scop
= pet_skip_info_if_add(&skip
, scop
, 2);
1506 pet_context_free(pc
);
1510 /* Construct a pet_scop for an affine if statement within the context "pc".
1512 * The condition is added to the iteration domains of the then branch,
1513 * while the opposite of the condition in added to the iteration domains
1514 * of the else branch, if any.
1516 * If there are any breaks or continues in the then and/or else
1517 * branches, then we may have to compute a new skip condition.
1518 * This is handled using a pet_skip_info_if object.
1519 * On initialization, the object checks if skip conditions need
1520 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1521 * adds them in pet_skip_info_if_add.
1523 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1524 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
1525 struct pet_state
*state
)
1531 struct pet_skip_info skip
;
1532 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1534 ctx
= pet_tree_get_ctx(tree
);
1536 has_else
= tree
->type
== pet_tree_if_else
;
1538 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1540 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1542 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
1543 pet_skip_info_if_extract_cond(&skip
, cond
, pc
, state
);
1545 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1546 set
= isl_pw_aff_non_zero_set(cond
);
1547 scop
= pet_scop_restrict(scop_then
, isl_set_params(isl_set_copy(set
)));
1550 set
= isl_set_subtract(isl_set_copy(valid
), set
);
1551 scop_else
= pet_scop_restrict(scop_else
, isl_set_params(set
));
1552 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
1555 scop
= pet_scop_resolve_nested(scop
);
1556 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
1558 if (pet_skip_info_has_skip(&skip
))
1559 scop
= pet_scop_prefix(scop
, 0);
1560 scop
= pet_skip_info_if_add(&skip
, scop
, 1);
1562 pet_context_free(pc
);
1566 /* Construct a pet_scop for an if statement within the context "pc".
1568 * If the condition fits the pattern of a conditional assignment,
1569 * then it is handled by scop_from_conditional_assignment.
1571 * Otherwise, we check if the condition is affine.
1572 * If so, we construct the scop in scop_from_affine_if.
1573 * Otherwise, we construct the scop in scop_from_non_affine_if.
1575 * We allow the condition to be dynamic, i.e., to refer to
1576 * scalars or array elements that may be written to outside
1577 * of the given if statement. These nested accesses are then represented
1578 * as output dimensions in the wrapping iteration domain.
1579 * If it is also written _inside_ the then or else branch, then
1580 * we treat the condition as non-affine.
1581 * As explained in extract_non_affine_if, this will introduce
1582 * an extra statement.
1583 * For aesthetic reasons, we want this statement to have a statement
1584 * number that is lower than those of the then and else branches.
1585 * In order to evaluate if we will need such a statement, however, we
1586 * first construct scops for the then and else branches.
1587 * We therefore reserve a statement number if we might have to
1588 * introduce such an extra statement.
1590 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
1591 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1595 pet_expr
*cond_expr
;
1596 pet_context
*pc_nested
;
1601 has_else
= tree
->type
== pet_tree_if_else
;
1603 pc
= pet_context_copy(pc
);
1604 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
1606 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
1608 if (is_conditional_assignment(tree
, pc
))
1609 return scop_from_conditional_assignment(tree
, pc
, state
);
1611 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
1612 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1613 pc_nested
= pet_context_copy(pc
);
1614 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1615 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1616 pet_context_free(pc_nested
);
1617 pet_expr_free(cond_expr
);
1620 pet_context_free(pc
);
1624 if (isl_pw_aff_involves_nan(cond
)) {
1625 isl_pw_aff_free(cond
);
1626 return scop_from_non_affine_if(tree
, pc
, state
);
1629 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
1630 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
1631 isl_pw_aff_free(cond
);
1632 return scop_from_non_affine_if(tree
, pc
, state
);
1635 return scop_from_affine_if(tree
, cond
, pc
, state
);
1638 /* Return a one-dimensional multi piecewise affine expression that is equal
1639 * to the constant 1 and is defined over the given domain.
1641 static __isl_give isl_multi_pw_aff
*one_mpa(__isl_take isl_space
*space
)
1643 isl_local_space
*ls
;
1646 ls
= isl_local_space_from_space(space
);
1647 aff
= isl_aff_zero_on_domain(ls
);
1648 aff
= isl_aff_set_constant_si(aff
, 1);
1650 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1653 /* Construct a pet_scop for a continue statement with the given domain space.
1655 * We simply create an empty scop with a universal pet_skip_now
1656 * skip condition. This skip condition will then be taken into
1657 * account by the enclosing loop construct, possibly after
1658 * being incorporated into outer skip conditions.
1660 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
,
1661 __isl_take isl_space
*space
)
1663 struct pet_scop
*scop
;
1665 scop
= pet_scop_empty(isl_space_copy(space
));
1667 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(space
));
1672 /* Construct a pet_scop for a break statement with the given domain space.
1674 * We simply create an empty scop with both a universal pet_skip_now
1675 * skip condition and a universal pet_skip_later skip condition.
1676 * These skip conditions will then be taken into
1677 * account by the enclosing loop construct, possibly after
1678 * being incorporated into outer skip conditions.
1680 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
,
1681 __isl_take isl_space
*space
)
1683 struct pet_scop
*scop
;
1684 isl_multi_pw_aff
*skip
;
1686 scop
= pet_scop_empty(isl_space_copy(space
));
1688 skip
= one_mpa(space
);
1689 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
1690 isl_multi_pw_aff_copy(skip
));
1691 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
1696 /* Extract a clone of the kill statement in "scop".
1697 * The domain of the clone is given by "domain".
1698 * "scop" is expected to have been created from a DeclStmt
1699 * and should have the kill as its first statement.
1701 static struct pet_scop
*extract_kill(__isl_keep isl_set
*domain
,
1702 struct pet_scop
*scop
, struct pet_state
*state
)
1705 struct pet_stmt
*stmt
;
1706 isl_multi_pw_aff
*index
;
1710 if (!domain
|| !scop
)
1712 if (scop
->n_stmt
< 1)
1713 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
1714 "expecting at least one statement", return NULL
);
1715 stmt
= scop
->stmts
[0];
1716 if (!pet_stmt_is_kill(stmt
))
1717 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
1718 "expecting kill statement", return NULL
);
1720 arg
= pet_expr_get_arg(stmt
->body
, 0);
1721 index
= pet_expr_access_get_index(arg
);
1722 access
= pet_expr_access_get_access(arg
);
1724 index
= isl_multi_pw_aff_reset_tuple_id(index
, isl_dim_in
);
1725 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
1726 kill
= pet_expr_kill_from_access_and_index(access
, index
);
1727 stmt
= pet_stmt_from_pet_expr(isl_set_copy(domain
),
1728 pet_loc_copy(stmt
->loc
), NULL
, state
->n_stmt
++, kill
);
1729 return pet_scop_from_pet_stmt(isl_set_get_space(domain
), stmt
);
1732 /* Does "tree" represent an assignment to a variable?
1734 * The assignment may be one of
1735 * - a declaration with initialization
1736 * - an expression with a top-level assignment operator
1738 static int is_assignment(__isl_keep pet_tree
*tree
)
1742 if (tree
->type
== pet_tree_decl_init
)
1744 return pet_tree_is_assign(tree
);
1747 /* Update "pc" by taking into account the assignment performed by "tree",
1748 * where "tree" satisfies is_assignment.
1750 * In particular, if the lhs of the assignment is a scalar variable and
1751 * if the rhs is an affine expression, then keep track of this value in "pc"
1752 * so that we can plug it in when we later come across the same variable.
1754 * The variable has already been marked as having been assigned
1755 * an unknown value by scop_handle_writes.
1757 static __isl_give pet_context
*handle_assignment(__isl_take pet_context
*pc
,
1758 __isl_keep pet_tree
*tree
)
1760 pet_expr
*var
, *val
;
1764 if (pet_tree_get_type(tree
) == pet_tree_decl_init
) {
1765 var
= pet_tree_decl_get_var(tree
);
1766 val
= pet_tree_decl_get_init(tree
);
1769 expr
= pet_tree_expr_get_expr(tree
);
1770 var
= pet_expr_get_arg(expr
, 0);
1771 val
= pet_expr_get_arg(expr
, 1);
1772 pet_expr_free(expr
);
1775 if (!pet_expr_is_scalar_access(var
)) {
1781 pa
= pet_expr_extract_affine(val
, pc
);
1783 pc
= pet_context_free(pc
);
1785 if (!isl_pw_aff_involves_nan(pa
)) {
1786 id
= pet_expr_access_get_id(var
);
1787 pc
= pet_context_set_value(pc
, id
, pa
);
1789 isl_pw_aff_free(pa
);
1797 /* Mark all arrays in "scop" as being exposed.
1799 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
1805 for (i
= 0; i
< scop
->n_array
; ++i
)
1806 scop
->arrays
[i
]->exposed
= 1;
1810 /* Try and construct a pet_scop corresponding to (part of)
1811 * a sequence of statements within the context "pc".
1813 * After extracting a statement, we update "pc"
1814 * based on the top-level assignments in the statement
1815 * so that we can exploit them in subsequent statements in the same block.
1817 * If there are any breaks or continues in the individual statements,
1818 * then we may have to compute a new skip condition.
1819 * This is handled using a pet_skip_info object.
1820 * On initialization, the object checks if skip conditions need
1821 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1822 * adds them in pet_skip_info_seq_add.
1824 * If "block" is set, then we need to insert kill statements at
1825 * the end of the block for any array that has been declared by
1826 * one of the statements in the sequence. Each of these declarations
1827 * results in the construction of a kill statement at the place
1828 * of the declaration, so we simply collect duplicates of
1829 * those kill statements and append these duplicates to the constructed scop.
1831 * If "block" is not set, then any array declared by one of the statements
1832 * in the sequence is marked as being exposed.
1834 * If autodetect is set, then we allow the extraction of only a subrange
1835 * of the sequence of statements. However, if there is at least one statement
1836 * for which we could not construct a scop and the final range contains
1837 * either no statements or at least one kill, then we discard the entire
1840 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
1841 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1847 struct pet_scop
*scop
, *kills
;
1849 ctx
= pet_tree_get_ctx(tree
);
1851 space
= pet_context_get_space(pc
);
1852 domain
= pet_context_get_domain(pc
);
1853 pc
= pet_context_copy(pc
);
1854 scop
= pet_scop_empty(isl_space_copy(space
));
1855 kills
= pet_scop_empty(space
);
1856 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
1857 struct pet_scop
*scop_i
;
1859 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
1860 pc
= scop_handle_writes(scop_i
, pc
);
1861 if (is_assignment(tree
->u
.b
.child
[i
]))
1862 pc
= handle_assignment(pc
, tree
->u
.b
.child
[i
]);
1863 struct pet_skip_info skip
;
1864 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
1865 pet_skip_info_seq_extract(&skip
, pc
, state
);
1866 if (pet_skip_info_has_skip(&skip
))
1867 scop_i
= pet_scop_prefix(scop_i
, 0);
1868 if (scop_i
&& pet_tree_is_decl(tree
->u
.b
.child
[i
])) {
1869 if (tree
->u
.b
.block
) {
1870 struct pet_scop
*kill
;
1871 kill
= extract_kill(domain
, scop_i
, state
);
1872 kills
= pet_scop_add_par(ctx
, kills
, kill
);
1874 scop_i
= mark_exposed(scop_i
);
1876 scop_i
= pet_scop_prefix(scop_i
, i
);
1877 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
1879 scop
= pet_skip_info_seq_add(&skip
, scop
, i
);
1884 isl_set_free(domain
);
1886 kills
= pet_scop_prefix(kills
, tree
->u
.b
.n
);
1887 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
1889 pet_context_free(pc
);
1894 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1895 * within the context "pc" by calling the appropriate function
1896 * based on the type of "tree".
1898 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
1899 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1904 switch (tree
->type
) {
1905 case pet_tree_error
:
1907 case pet_tree_block
:
1908 return scop_from_block(tree
, pc
, state
);
1909 case pet_tree_break
:
1910 return scop_from_break(tree
, pet_context_get_space(pc
));
1911 case pet_tree_continue
:
1912 return scop_from_continue(tree
, pet_context_get_space(pc
));
1914 case pet_tree_decl_init
:
1915 return scop_from_decl(tree
, pc
, state
);
1917 return scop_from_expr(pet_expr_copy(tree
->u
.e
.expr
),
1918 isl_id_copy(tree
->label
),
1920 pet_tree_get_loc(tree
), pc
);
1922 case pet_tree_if_else
:
1923 return scop_from_if(tree
, pc
, state
);
1925 return scop_from_for(tree
, pc
, state
);
1926 case pet_tree_while
:
1927 return scop_from_while(tree
, pc
, state
);
1928 case pet_tree_infinite_loop
:
1929 return scop_from_infinite_for(tree
, pc
, state
);
1932 isl_die(tree
->ctx
, isl_error_internal
, "unhandled type",
1936 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1937 * "int_size" is the number of bytes need to represent an integer.
1938 * "extract_array" is a callback that we can use to create a pet_array
1939 * that corresponds to the variable accessed by an expression.
1941 * Initialize the global state, construct a context and then
1942 * construct the pet_scop by recursively visiting the tree.
1944 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
1945 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
1946 __isl_keep pet_context
*pc
, void *user
), void *user
,
1947 __isl_keep pet_context
*pc
)
1949 struct pet_scop
*scop
;
1950 struct pet_state state
= { 0 };
1955 state
.ctx
= pet_tree_get_ctx(tree
);
1956 state
.int_size
= int_size
;
1957 state
.extract_array
= extract_array
;
1960 scop
= scop_from_tree(tree
, pc
, &state
);
1961 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
1963 pet_tree_free(tree
);