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
)
87 space
= pet_context_get_space(pc
);
89 expr
= pet_expr_plug_in_args(expr
, pc
);
90 expr
= pet_expr_resolve_nested(expr
, space
);
91 expr
= pet_expr_resolve_assume(expr
, pc
);
92 domain
= pet_context_get_domain(pc
);
93 ps
= pet_stmt_from_pet_expr(domain
, loc
, label
, stmt_nr
, expr
);
94 return pet_scop_from_pet_stmt(space
, ps
);
97 /* Construct a pet_scop with a single statement killing the entire
99 * The location of the statement is set to "loc".
101 static struct pet_scop
*kill(__isl_take pet_loc
*loc
, struct pet_array
*array
,
102 __isl_keep pet_context
*pc
, struct pet_state
*state
)
107 isl_multi_pw_aff
*index
;
110 struct pet_scop
*scop
;
114 ctx
= isl_set_get_ctx(array
->extent
);
115 access
= isl_map_from_range(isl_set_copy(array
->extent
));
116 id
= isl_set_get_tuple_id(array
->extent
);
117 space
= isl_space_alloc(ctx
, 0, 0, 0);
118 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
119 index
= isl_multi_pw_aff_zero(space
);
120 expr
= pet_expr_kill_from_access_and_index(access
, index
);
121 return scop_from_expr(expr
, NULL
, state
->n_stmt
++, loc
, pc
);
127 /* Construct and return a pet_array corresponding to the variable
128 * accessed by "access" by calling the extract_array callback.
130 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
131 __isl_keep pet_context
*pc
, struct pet_state
*state
)
133 return state
->extract_array(access
, pc
, state
->user
);
136 /* Construct a pet_scop for a (single) variable declaration
137 * within the context "pc".
139 * The scop contains the variable being declared (as an array)
140 * and a statement killing the array.
142 * If the declaration comes with an initialization, then the scop
143 * also contains an assignment to the variable.
145 static struct pet_scop
*scop_from_decl(__isl_keep pet_tree
*tree
,
146 __isl_keep pet_context
*pc
, struct pet_state
*state
)
150 struct pet_array
*array
;
151 struct pet_scop
*scop_decl
, *scop
;
152 pet_expr
*lhs
, *rhs
, *pe
;
154 array
= extract_array(tree
->u
.d
.var
, pc
, state
);
157 scop_decl
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
158 scop_decl
= pet_scop_add_array(scop_decl
, array
);
160 if (tree
->type
!= pet_tree_decl_init
)
163 lhs
= pet_expr_copy(tree
->u
.d
.var
);
164 rhs
= pet_expr_copy(tree
->u
.d
.init
);
165 type_size
= pet_expr_get_type_size(lhs
);
166 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
167 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
168 pet_tree_get_loc(tree
), pc
);
170 scop_decl
= pet_scop_prefix(scop_decl
, 0);
171 scop
= pet_scop_prefix(scop
, 1);
173 ctx
= pet_tree_get_ctx(tree
);
174 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
179 /* Embed the given iteration domain in an extra outer loop
180 * with induction variable "var".
181 * If this variable appeared as a parameter in the constraints,
182 * it is replaced by the new outermost dimension.
184 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
185 __isl_take isl_id
*var
)
189 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
190 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
192 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
193 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
200 /* Return those elements in the space of "cond" that come after
201 * (based on "sign") an element in "cond" in the final dimension.
203 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
206 isl_map
*previous_to_this
;
209 dim
= isl_set_dim(cond
, isl_dim_set
);
210 space
= isl_space_map_from_set(isl_set_get_space(cond
));
211 previous_to_this
= isl_map_universe(space
);
212 for (i
= 0; i
+ 1 < dim
; ++i
)
213 previous_to_this
= isl_map_equate(previous_to_this
,
214 isl_dim_in
, i
, isl_dim_out
, i
);
216 previous_to_this
= isl_map_order_lt(previous_to_this
,
217 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
219 previous_to_this
= isl_map_order_gt(previous_to_this
,
220 isl_dim_in
, dim
- 1, isl_dim_out
, dim
- 1);
222 cond
= isl_set_apply(cond
, previous_to_this
);
227 /* Remove those iterations of "domain" that have an earlier iteration
228 * (based on "sign") where "skip" is satisfied.
229 * "domain" has an extra outer loop compared to "skip".
230 * The skip condition is first embedded in the same space as "domain".
231 * If "apply_skip_map" is set, then "skip_map" is first applied
232 * to the embedded skip condition before removing it from the domain.
234 static __isl_give isl_set
*apply_affine_break(__isl_take isl_set
*domain
,
235 __isl_take isl_set
*skip
, int sign
,
236 int apply_skip_map
, __isl_keep isl_map
*skip_map
)
238 skip
= embed(skip
, isl_set_get_dim_id(domain
, isl_dim_set
, 0));
240 skip
= isl_set_apply(skip
, isl_map_copy(skip_map
));
241 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
242 return isl_set_subtract(domain
, after(skip
, sign
));
245 /* Create the infinite iteration domain
249 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
)
251 isl_ctx
*ctx
= isl_id_get_ctx(id
);
254 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
255 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
260 /* Create an identity affine expression on the space containing "domain",
261 * which is assumed to be one-dimensional.
263 static __isl_give isl_aff
*identity_aff(__isl_keep isl_set
*domain
)
267 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
268 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
271 /* Create an affine expression that maps elements
272 * of a single-dimensional array "id_test" to the previous element
273 * (according to "inc"), provided this element belongs to "domain".
274 * That is, create the affine expression
276 * { id[x] -> id[x - inc] : x - inc in domain }
278 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
279 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
284 isl_multi_pw_aff
*prev
;
286 space
= isl_set_get_space(domain
);
287 ls
= isl_local_space_from_space(space
);
288 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
289 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
290 prev
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
291 domain
= isl_set_preimage_multi_pw_aff(domain
,
292 isl_multi_pw_aff_copy(prev
));
293 prev
= isl_multi_pw_aff_intersect_domain(prev
, domain
);
294 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
299 /* Add an implication to "scop" expressing that if an element of
300 * virtual array "id_test" has value "satisfied" then all previous elements
301 * of this array also have that value. The set of previous elements
302 * is bounded by "domain". If "sign" is negative then the iterator
303 * is decreasing and we express that all subsequent array elements
304 * (but still defined previously) have the same value.
306 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
307 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
313 domain
= isl_set_set_tuple_id(domain
, id_test
);
314 space
= isl_set_get_space(domain
);
316 map
= isl_map_lex_ge(space
);
318 map
= isl_map_lex_le(space
);
319 map
= isl_map_intersect_range(map
, domain
);
320 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
325 /* Add a filter to "scop" that imposes that it is only executed
326 * when the variable identified by "id_test" has a zero value
327 * for all previous iterations of "domain".
329 * In particular, add a filter that imposes that the array
330 * has a zero value at the previous iteration of domain and
331 * add an implication that implies that it then has that
332 * value for all previous iterations.
334 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
335 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
336 __isl_take isl_val
*inc
)
338 isl_multi_pw_aff
*prev
;
339 int sign
= isl_val_sgn(inc
);
341 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
342 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
343 scop
= pet_scop_filter(scop
, prev
, 0);
348 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
349 __isl_keep pet_context
*pc
, struct pet_state
*state
);
351 /* Construct a pet_scop for an infinite loop around the given body
352 * within the context "pc".
354 * We extract a pet_scop for the body and then embed it in a loop with
363 * If the body contains any break, then it is taken into
364 * account in apply_affine_break (if the skip condition is affine)
365 * or in scop_add_break (if the skip condition is not affine).
367 * Note that in case of an affine skip condition,
368 * since we are dealing with a loop without loop iterator,
369 * the skip condition cannot refer to the current loop iterator and
370 * so effectively, the iteration domain is of the form
372 * { [0]; [t] : t >= 1 and not skip }
374 static struct pet_scop
*scop_from_infinite_loop(__isl_keep pet_tree
*body
,
375 __isl_keep pet_context
*pc
, struct pet_state
*state
)
378 isl_id
*id
, *id_test
;
382 struct pet_scop
*scop
;
383 int has_affine_break
;
386 ctx
= pet_tree_get_ctx(body
);
387 id
= isl_id_alloc(ctx
, "t", NULL
);
388 domain
= infinite_domain(isl_id_copy(id
));
389 ident
= identity_aff(domain
);
391 scop
= scop_from_tree(body
, pc
, state
);
393 has_affine_break
= pet_scop_has_affine_skip(scop
, pet_skip_later
);
394 if (has_affine_break
)
395 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
396 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
398 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
400 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
401 isl_aff_copy(ident
), ident
, id
);
402 if (has_affine_break
) {
403 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
404 scop
= pet_scop_intersect_domain_prefix(scop
,
405 isl_set_copy(domain
));
408 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
410 isl_set_free(domain
);
415 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
420 * within the context "pc".
422 static struct pet_scop
*scop_from_infinite_for(__isl_keep pet_tree
*tree
,
423 __isl_keep pet_context
*pc
, struct pet_state
*state
)
425 struct pet_scop
*scop
;
427 pc
= pet_context_copy(pc
);
428 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
430 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
432 pet_context_free(pc
);
437 /* Construct a pet_scop for a while loop of the form
442 * within the context "pc".
443 * In particular, construct a scop for an infinite loop around body and
444 * intersect the domain with the affine expression.
445 * Note that this intersection may result in an empty loop.
447 static struct pet_scop
*scop_from_affine_while(__isl_keep pet_tree
*tree
,
448 __isl_take isl_pw_aff
*pa
, __isl_take pet_context
*pc
,
449 struct pet_state
*state
)
451 struct pet_scop
*scop
;
455 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
456 dom
= isl_pw_aff_non_zero_set(pa
);
457 scop
= scop_from_infinite_loop(tree
->u
.l
.body
, pc
, state
);
458 scop
= pet_scop_restrict(scop
, isl_set_params(dom
));
459 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
461 pet_context_free(pc
);
465 /* Construct a scop for a while, given the scops for the condition
466 * and the body, the filter identifier and the iteration domain of
469 * In particular, the scop for the condition is filtered to depend
470 * on "id_test" evaluating to true for all previous iterations
471 * of the loop, while the scop for the body is filtered to depend
472 * on "id_test" evaluating to true for all iterations up to the
474 * The actual filter only imposes that this virtual array has
475 * value one on the previous or the current iteration.
476 * The fact that this condition also applies to the previous
477 * iterations is enforced by an implication.
479 * These filtered scops are then combined into a single scop.
481 * "sign" is positive if the iterator increases and negative
484 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
485 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
486 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
488 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
490 isl_multi_pw_aff
*test_index
;
491 isl_multi_pw_aff
*prev
;
492 int sign
= isl_val_sgn(inc
);
493 struct pet_scop
*scop
;
495 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
496 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
498 space
= isl_space_map_from_set(isl_set_get_space(domain
));
499 test_index
= isl_multi_pw_aff_identity(space
);
500 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
501 isl_id_copy(id_test
));
502 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
504 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
505 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
510 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
511 * evaluating "cond" and writing the result to a virtual scalar,
512 * as expressed by "index".
513 * Do so within the context "pc".
514 * The location of the statement is set to "loc".
516 static struct pet_scop
*scop_from_non_affine_condition(
517 __isl_take pet_expr
*cond
, int stmt_nr
,
518 __isl_take isl_multi_pw_aff
*index
,
519 __isl_take pet_loc
*loc
, __isl_keep pet_context
*pc
)
521 pet_expr
*expr
, *write
;
523 write
= pet_expr_from_index(index
);
524 write
= pet_expr_access_set_write(write
, 1);
525 write
= pet_expr_access_set_read(write
, 0);
526 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, cond
);
528 return scop_from_expr(expr
, NULL
, stmt_nr
, loc
, pc
);
531 /* Construct a generic while scop, with iteration domain
532 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
533 * The scop consists of two parts,
534 * one for evaluating the condition "cond" and one for the body.
535 * If "expr_inc" is not NULL, then a scop for evaluating this expression
536 * is added at the end of the body,
537 * after replacing any skip conditions resulting from continue statements
538 * by the skip conditions resulting from break statements (if any).
540 * The schedule is adjusted to reflect that the condition is evaluated
541 * before the body is executed and the body is filtered to depend
542 * on the result of the condition evaluating to true on all iterations
543 * up to the current iteration, while the evaluation of the condition itself
544 * is filtered to depend on the result of the condition evaluating to true
545 * on all previous iterations.
546 * The context of the scop representing the body is dropped
547 * because we don't know how many times the body will be executed,
550 * If the body contains any break, then it is taken into
551 * account in apply_affine_break (if the skip condition is affine)
552 * or in scop_add_break (if the skip condition is not affine).
554 * Note that in case of an affine skip condition,
555 * since we are dealing with a loop without loop iterator,
556 * the skip condition cannot refer to the current loop iterator and
557 * so effectively, the iteration domain is of the form
559 * { [0]; [t] : t >= 1 and not skip }
561 static struct pet_scop
*scop_from_non_affine_while(__isl_take pet_expr
*cond
,
562 __isl_take pet_loc
*loc
, __isl_keep pet_tree
*tree_body
,
563 __isl_take pet_expr
*expr_inc
, __isl_take pet_context
*pc
,
564 struct pet_state
*state
)
567 isl_id
*id
, *id_test
, *id_break_test
;
569 isl_multi_pw_aff
*test_index
;
573 struct pet_scop
*scop
, *scop_body
;
574 int has_affine_break
;
578 space
= pet_context_get_space(pc
);
579 test_index
= pet_create_test_index(space
, state
->n_test
++);
580 scop
= scop_from_non_affine_condition(cond
, state
->n_stmt
++,
581 isl_multi_pw_aff_copy(test_index
),
582 pet_loc_copy(loc
), pc
);
583 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
584 domain
= pet_context_get_domain(pc
);
585 scop
= pet_scop_add_boolean_array(scop
, domain
,
586 test_index
, state
->int_size
);
588 id
= isl_id_alloc(ctx
, "t", NULL
);
589 domain
= infinite_domain(isl_id_copy(id
));
590 ident
= identity_aff(domain
);
592 scop_body
= scop_from_tree(tree_body
, pc
, state
);
594 has_affine_break
= pet_scop_has_affine_skip(scop_body
, pet_skip_later
);
595 if (has_affine_break
)
596 skip
= pet_scop_get_affine_skip_domain(scop_body
,
598 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
600 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
602 scop
= pet_scop_prefix(scop
, 0);
603 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_aff_copy(ident
),
604 isl_aff_copy(ident
), isl_id_copy(id
));
605 scop_body
= pet_scop_reset_context(scop_body
);
606 scop_body
= pet_scop_prefix(scop_body
, 1);
608 struct pet_scop
*scop_inc
;
609 scop_inc
= scop_from_expr(expr_inc
, NULL
, state
->n_stmt
++,
611 scop_inc
= pet_scop_prefix(scop_inc
, 2);
612 if (pet_scop_has_skip(scop_body
, pet_skip_later
)) {
613 isl_multi_pw_aff
*skip
;
614 skip
= pet_scop_get_skip(scop_body
, pet_skip_later
);
615 scop_body
= pet_scop_set_skip(scop_body
,
618 pet_scop_reset_skip(scop_body
, pet_skip_now
);
619 scop_body
= pet_scop_add_seq(ctx
, scop_body
, scop_inc
);
622 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
623 isl_aff_copy(ident
), ident
, id
);
625 if (has_affine_break
) {
626 domain
= apply_affine_break(domain
, skip
, 1, 0, NULL
);
627 scop
= pet_scop_intersect_domain_prefix(scop
,
628 isl_set_copy(domain
));
629 scop_body
= pet_scop_intersect_domain_prefix(scop_body
,
630 isl_set_copy(domain
));
633 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
634 isl_set_copy(domain
), isl_val_one(ctx
));
635 scop_body
= scop_add_break(scop_body
, id_break_test
,
636 isl_set_copy(domain
), isl_val_one(ctx
));
638 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
641 pet_context_free(pc
);
645 /* Check if the while loop is of the form
647 * while (affine expression)
650 * If so, call scop_from_affine_while to construct a scop.
652 * Otherwise, pass control to scop_from_non_affine_while.
654 * "pc" is the context in which the affine expressions in the scop are created.
656 static struct pet_scop
*scop_from_while(__isl_keep pet_tree
*tree
,
657 __isl_keep pet_context
*pc
, struct pet_state
*state
)
665 pc
= pet_context_copy(pc
);
666 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
668 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
669 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
670 pa
= pet_expr_extract_affine_condition(cond_expr
, pc
);
671 pet_expr_free(cond_expr
);
676 if (!isl_pw_aff_involves_nan(pa
))
677 return scop_from_affine_while(tree
, pa
, pc
, state
);
679 return scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
680 pet_tree_get_loc(tree
), tree
->u
.l
.body
, NULL
,
683 pet_context_free(pc
);
687 /* Check whether "cond" expresses a simple loop bound
688 * on the only set dimension.
689 * In particular, if "up" is set then "cond" should contain only
690 * upper bounds on the set dimension.
691 * Otherwise, it should contain only lower bounds.
693 static int is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
695 if (isl_val_is_pos(inc
))
696 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
698 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
701 /* Extend a condition on a given iteration of a loop to one that
702 * imposes the same condition on all previous iterations.
703 * "domain" expresses the lower [upper] bound on the iterations
704 * when inc is positive [negative].
706 * In particular, we construct the condition (when inc is positive)
708 * forall i' : (domain(i') and i' <= i) => cond(i')
710 * which is equivalent to
712 * not exists i' : domain(i') and i' <= i and not cond(i')
714 * We construct this set by negating cond, applying a map
716 * { [i'] -> [i] : domain(i') and i' <= i }
718 * and then negating the result again.
720 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
721 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
723 isl_map
*previous_to_this
;
725 if (isl_val_is_pos(inc
))
726 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
728 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
730 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
732 cond
= isl_set_complement(cond
);
733 cond
= isl_set_apply(cond
, previous_to_this
);
734 cond
= isl_set_complement(cond
);
741 /* Construct a domain of the form
743 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
745 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
746 __isl_take isl_pw_aff
*init
, __isl_take isl_val
*inc
)
752 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
753 dim
= isl_pw_aff_get_domain_space(init
);
754 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
755 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, 0, inc
);
756 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
758 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
759 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
760 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
761 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
763 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
765 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
767 return isl_set_params(set
);
770 /* Assuming "cond" represents a bound on a loop where the loop
771 * iterator "iv" is incremented (or decremented) by one, check if wrapping
774 * Under the given assumptions, wrapping is only possible if "cond" allows
775 * for the last value before wrapping, i.e., 2^width - 1 in case of an
776 * increasing iterator and 0 in case of a decreasing iterator.
778 static int can_wrap(__isl_keep isl_set
*cond
, __isl_keep pet_expr
*iv
,
779 __isl_keep isl_val
*inc
)
786 test
= isl_set_copy(cond
);
788 ctx
= isl_set_get_ctx(test
);
789 if (isl_val_is_neg(inc
))
790 limit
= isl_val_zero(ctx
);
792 limit
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
793 limit
= isl_val_2exp(limit
);
794 limit
= isl_val_sub_ui(limit
, 1);
797 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
798 cw
= !isl_set_is_empty(test
);
804 /* Given a one-dimensional space, construct the following affine expression
807 * { [v] -> [v mod 2^width] }
809 * where width is the number of bits used to represent the values
810 * of the unsigned variable "iv".
812 static __isl_give isl_aff
*compute_wrapping(__isl_take isl_space
*dim
,
813 __isl_keep pet_expr
*iv
)
819 ctx
= isl_space_get_ctx(dim
);
820 mod
= isl_val_int_from_ui(ctx
, pet_expr_get_type_size(iv
));
821 mod
= isl_val_2exp(mod
);
823 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
824 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
825 aff
= isl_aff_mod_val(aff
, mod
);
830 /* Project out the parameter "id" from "set".
832 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
833 __isl_keep isl_id
*id
)
837 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
839 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
844 /* Compute the set of parameters for which "set1" is a subset of "set2".
846 * set1 is a subset of set2 if
848 * forall i in set1 : i in set2
852 * not exists i in set1 and i not in set2
856 * not exists i in set1 \ set2
858 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
859 __isl_take isl_set
*set2
)
861 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
864 /* Compute the set of parameter values for which "cond" holds
865 * on the next iteration for each element of "dom".
867 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
868 * and then compute the set of parameters for which the result is a subset
871 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
872 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
878 space
= isl_set_get_space(dom
);
879 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
880 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
881 aff
= isl_aff_add_constant_val(aff
, inc
);
882 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
884 dom
= isl_set_apply(dom
, next
);
886 return enforce_subset(dom
, cond
);
889 /* Extract the for loop "tree" as a while loop within the context "pc".
891 * That is, the for loop has the form
893 * for (iv = init; cond; iv += inc)
904 * except that the skips resulting from any continue statements
905 * in body do not apply to the increment, but are replaced by the skips
906 * resulting from break statements.
908 * If the loop iterator is declared in the for loop, then it is killed before
909 * and after the loop.
911 static struct pet_scop
*scop_from_non_affine_for(__isl_keep pet_tree
*tree
,
912 __isl_take pet_context
*pc
, struct pet_state
*state
)
916 pet_expr
*expr_iv
, *init
, *inc
;
917 struct pet_scop
*scop_init
, *scop
;
919 struct pet_array
*array
;
920 struct pet_scop
*scop_kill
;
922 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
923 pc
= pet_context_mark_assigned(pc
, iv
);
925 declared
= tree
->u
.l
.declared
;
927 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
928 type_size
= pet_expr_get_type_size(expr_iv
);
929 init
= pet_expr_copy(tree
->u
.l
.init
);
930 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
931 scop_init
= scop_from_expr(init
, NULL
, state
->n_stmt
++,
932 pet_tree_get_loc(tree
), pc
);
933 scop_init
= pet_scop_prefix(scop_init
, declared
);
935 expr_iv
= pet_expr_copy(tree
->u
.l
.iv
);
936 type_size
= pet_expr_get_type_size(expr_iv
);
937 inc
= pet_expr_copy(tree
->u
.l
.inc
);
938 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
940 scop
= scop_from_non_affine_while(pet_expr_copy(tree
->u
.l
.cond
),
941 pet_tree_get_loc(tree
), tree
->u
.l
.body
, inc
,
942 pet_context_copy(pc
), state
);
944 scop
= pet_scop_prefix(scop
, declared
+ 1);
945 scop
= pet_scop_add_seq(state
->ctx
, scop_init
, scop
);
948 pet_context_free(pc
);
952 array
= extract_array(tree
->u
.l
.iv
, pc
, state
);
955 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
956 scop_kill
= pet_scop_prefix(scop_kill
, 0);
957 scop
= pet_scop_add_seq(state
->ctx
, scop_kill
, scop
);
958 scop_kill
= kill(pet_tree_get_loc(tree
), array
, pc
, state
);
959 scop_kill
= pet_scop_add_array(scop_kill
, array
);
960 scop_kill
= pet_scop_prefix(scop_kill
, 3);
961 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_kill
);
963 pet_context_free(pc
);
967 /* Given an access expression "expr", is the variable accessed by
968 * "expr" assigned anywhere inside "tree"?
970 static int is_assigned(__isl_keep pet_expr
*expr
, __isl_keep pet_tree
*tree
)
975 id
= pet_expr_access_get_id(expr
);
976 assigned
= pet_tree_writes(tree
, id
);
982 /* Are all nested access parameters in "pa" allowed given "tree".
983 * In particular, is none of them written by anywhere inside "tree".
985 * If "tree" has any continue nodes in the current loop level,
986 * then no nested access parameters are allowed.
987 * In particular, if there is any nested access in a guard
988 * for a piece of code containing a "continue", then we want to introduce
989 * a separate statement for evaluating this guard so that we can express
990 * that the result is false for all previous iterations.
992 static int is_nested_allowed(__isl_keep isl_pw_aff
*pa
,
993 __isl_keep pet_tree
*tree
)
1000 if (!pet_nested_any_in_pw_aff(pa
))
1003 if (pet_tree_has_continue(tree
))
1006 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
1007 for (i
= 0; i
< nparam
; ++i
) {
1008 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
1012 if (!pet_nested_in_id(id
)) {
1017 expr
= pet_nested_extract_expr(id
);
1018 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
1019 !is_assigned(expr
, tree
);
1021 pet_expr_free(expr
);
1031 /* Construct a pet_scop for a for tree with static affine initialization
1032 * and constant increment within the context "pc".
1034 * The condition is allowed to contain nested accesses, provided
1035 * they are not being written to inside the body of the loop.
1036 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1037 * essentially treated as a while loop, with iteration domain
1038 * { [i] : i >= init }.
1040 * We extract a pet_scop for the body and then embed it in a loop with
1041 * iteration domain and schedule
1043 * { [i] : i >= init and condition' }
1048 * { [i] : i <= init and condition' }
1051 * Where condition' is equal to condition if the latter is
1052 * a simple upper [lower] bound and a condition that is extended
1053 * to apply to all previous iterations otherwise.
1055 * If the condition is non-affine, then we drop the condition from the
1056 * iteration domain and instead create a separate statement
1057 * for evaluating the condition. The body is then filtered to depend
1058 * on the result of the condition evaluating to true on all iterations
1059 * up to the current iteration, while the evaluation the condition itself
1060 * is filtered to depend on the result of the condition evaluating to true
1061 * on all previous iterations.
1062 * The context of the scop representing the body is dropped
1063 * because we don't know how many times the body will be executed,
1066 * If the stride of the loop is not 1, then "i >= init" is replaced by
1068 * (exists a: i = init + stride * a and a >= 0)
1070 * If the loop iterator i is unsigned, then wrapping may occur.
1071 * We therefore use a virtual iterator instead that does not wrap.
1072 * However, the condition in the code applies
1073 * to the wrapped value, so we need to change condition(i)
1074 * into condition([i % 2^width]). Similarly, we replace all accesses
1075 * to the original iterator by the wrapping of the virtual iterator.
1076 * Note that there may be no need to perform this final wrapping
1077 * if the loop condition (after wrapping) satisfies certain conditions.
1078 * However, the is_simple_bound condition is not enough since it doesn't
1079 * check if there even is an upper bound.
1081 * Wrapping on unsigned iterators can be avoided entirely if
1082 * loop condition is simple, the loop iterator is incremented
1083 * [decremented] by one and the last value before wrapping cannot
1084 * possibly satisfy the loop condition.
1086 * Valid parameters for a for loop are those for which the initial
1087 * value itself, the increment on each domain iteration and
1088 * the condition on both the initial value and
1089 * the result of incrementing the iterator for each iteration of the domain
1091 * If the loop condition is non-affine, then we only consider validity
1092 * of the initial value.
1094 * If the body contains any break, then we keep track of it in "skip"
1095 * (if the skip condition is affine) or it is handled in scop_add_break
1096 * (if the skip condition is not affine).
1097 * Note that the affine break condition needs to be considered with
1098 * respect to previous iterations in the virtual domain (if any).
1100 static struct pet_scop
*scop_from_affine_for(__isl_keep pet_tree
*tree
,
1101 __isl_take isl_pw_aff
*init_val
, __isl_take isl_pw_aff
*pa_inc
,
1102 __isl_take isl_val
*inc
, __isl_take pet_context
*pc
,
1103 struct pet_state
*state
)
1105 isl_local_space
*ls
;
1108 isl_set
*cond
= NULL
;
1109 isl_set
*skip
= NULL
;
1110 isl_id
*id
, *id_test
= NULL
, *id_break_test
;
1111 struct pet_scop
*scop
, *scop_cond
= NULL
;
1117 int has_affine_break
;
1119 isl_map
*rev_wrap
= NULL
;
1120 isl_aff
*wrap
= NULL
;
1122 isl_set
*valid_init
;
1123 isl_set
*valid_cond
;
1124 isl_set
*valid_cond_init
;
1125 isl_set
*valid_cond_next
;
1127 pet_expr
*cond_expr
;
1128 pet_context
*pc_nested
;
1130 id
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1132 cond_expr
= pet_expr_copy(tree
->u
.l
.cond
);
1133 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1134 pc_nested
= pet_context_copy(pc
);
1135 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1136 pa
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1137 pet_context_free(pc_nested
);
1138 pet_expr_free(cond_expr
);
1140 valid_inc
= isl_pw_aff_domain(pa_inc
);
1142 is_unsigned
= pet_expr_get_type_size(tree
->u
.l
.iv
) > 0;
1144 is_non_affine
= isl_pw_aff_involves_nan(pa
) ||
1145 !is_nested_allowed(pa
, tree
->u
.l
.body
);
1147 pa
= isl_pw_aff_free(pa
);
1149 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1150 cond
= isl_pw_aff_non_zero_set(pa
);
1152 cond
= isl_set_universe(isl_space_set_alloc(state
->ctx
, 0, 0));
1154 cond
= embed(cond
, isl_id_copy(id
));
1155 valid_cond
= isl_set_coalesce(valid_cond
);
1156 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
1157 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
1158 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
1159 is_virtual
= is_unsigned
&&
1160 (!is_one
|| can_wrap(cond
, tree
->u
.l
.iv
, inc
));
1162 valid_cond_init
= enforce_subset(
1163 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val
))),
1164 isl_set_copy(valid_cond
));
1165 if (is_one
&& !is_virtual
) {
1166 isl_pw_aff_free(init_val
);
1167 pa
= pet_expr_extract_comparison(
1168 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
1169 tree
->u
.l
.iv
, tree
->u
.l
.init
, pc
);
1170 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1171 valid_init
= set_project_out_by_id(valid_init
, id
);
1172 domain
= isl_pw_aff_non_zero_set(pa
);
1174 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
1175 domain
= strided_domain(isl_id_copy(id
), init_val
,
1179 domain
= embed(domain
, isl_id_copy(id
));
1181 wrap
= compute_wrapping(isl_set_get_space(cond
), tree
->u
.l
.iv
);
1182 rev_wrap
= isl_map_from_aff(isl_aff_copy(wrap
));
1183 rev_wrap
= isl_map_reverse(rev_wrap
);
1184 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
1185 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
1186 valid_inc
= isl_set_apply(valid_inc
, isl_map_copy(rev_wrap
));
1188 is_simple
= is_simple_bound(cond
, inc
);
1190 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
1191 is_simple
= is_simple_bound(cond
, inc
);
1194 cond
= valid_for_each_iteration(cond
,
1195 isl_set_copy(domain
), isl_val_copy(inc
));
1196 domain
= isl_set_intersect(domain
, cond
);
1197 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1198 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
1199 sched
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1200 if (isl_val_is_neg(inc
))
1201 sched
= isl_aff_neg(sched
);
1203 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
1205 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
1208 wrap
= identity_aff(domain
);
1210 if (is_non_affine
) {
1212 isl_multi_pw_aff
*test_index
;
1213 space
= pet_context_get_space(pc
);
1214 test_index
= pet_create_test_index(space
, state
->n_test
++);
1215 scop_cond
= scop_from_non_affine_condition(
1216 pet_expr_copy(tree
->u
.l
.cond
), state
->n_stmt
++,
1217 isl_multi_pw_aff_copy(test_index
),
1218 pet_tree_get_loc(tree
), pc
);
1219 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
1221 scop_cond
= pet_scop_add_boolean_array(scop_cond
,
1222 pet_context_get_domain(pc
), test_index
,
1224 scop_cond
= pet_scop_prefix(scop_cond
, 0);
1225 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
1226 isl_aff_copy(sched
), isl_aff_copy(wrap
),
1230 scop
= scop_from_tree(tree
->u
.l
.body
, pc
, state
);
1231 has_affine_break
= scop
&&
1232 pet_scop_has_affine_skip(scop
, pet_skip_later
);
1233 if (has_affine_break
)
1234 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1235 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
1237 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1238 if (is_non_affine
) {
1239 scop
= pet_scop_reset_context(scop
);
1240 scop
= pet_scop_prefix(scop
, 1);
1242 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
, wrap
, id
);
1243 scop
= pet_scop_resolve_nested(scop
);
1244 if (has_affine_break
) {
1245 domain
= apply_affine_break(domain
, skip
, isl_val_sgn(inc
),
1246 is_virtual
, rev_wrap
);
1247 scop
= pet_scop_intersect_domain_prefix(scop
,
1248 isl_set_copy(domain
));
1250 isl_map_free(rev_wrap
);
1252 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
1254 if (is_non_affine
) {
1255 scop
= scop_add_while(scop_cond
, scop
, id_test
, domain
,
1257 isl_set_free(valid_inc
);
1259 scop
= pet_scop_restrict_context(scop
, valid_inc
);
1260 scop
= pet_scop_restrict_context(scop
, valid_cond_next
);
1261 scop
= pet_scop_restrict_context(scop
, valid_cond_init
);
1262 isl_set_free(domain
);
1267 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid_init
));
1269 pet_context_free(pc
);
1273 /* Construct a pet_scop for a for statement within the context of "pc".
1275 * We update the context to reflect the writes to the loop variable and
1276 * the writes inside the body.
1278 * Then we check if the initialization of the for loop
1279 * is a static affine value and the increment is a constant.
1280 * If so, we construct the pet_scop using scop_from_affine_for.
1281 * Otherwise, we treat the for loop as a while loop
1282 * in scop_from_non_affine_for.
1284 static struct pet_scop
*scop_from_for(__isl_keep pet_tree
*tree
,
1285 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1289 isl_pw_aff
*pa_inc
, *init_val
;
1290 pet_context
*pc_init_val
;
1295 iv
= pet_expr_access_get_id(tree
->u
.l
.iv
);
1296 pc
= pet_context_copy(pc
);
1297 pc
= pet_context_clear_value(pc
, iv
);
1298 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.l
.body
);
1300 pc_init_val
= pet_context_copy(pc
);
1301 pc_init_val
= pet_context_mark_unknown(pc_init_val
, isl_id_copy(iv
));
1302 init_val
= pet_expr_extract_affine(tree
->u
.l
.init
, pc_init_val
);
1303 pet_context_free(pc_init_val
);
1304 pa_inc
= pet_expr_extract_affine(tree
->u
.l
.inc
, pc
);
1305 inc
= pet_extract_cst(pa_inc
);
1306 if (!pa_inc
|| !init_val
|| !inc
)
1308 if (!isl_pw_aff_involves_nan(pa_inc
) &&
1309 !isl_pw_aff_involves_nan(init_val
) && !isl_val_is_nan(inc
))
1310 return scop_from_affine_for(tree
, init_val
, pa_inc
, inc
,
1313 isl_pw_aff_free(pa_inc
);
1314 isl_pw_aff_free(init_val
);
1316 return scop_from_non_affine_for(tree
, pc
, state
);
1318 isl_pw_aff_free(pa_inc
);
1319 isl_pw_aff_free(init_val
);
1321 pet_context_free(pc
);
1325 /* Check whether "expr" is an affine constraint within the context "pc".
1327 static int is_affine_condition(__isl_keep pet_expr
*expr
,
1328 __isl_keep pet_context
*pc
)
1333 pa
= pet_expr_extract_affine_condition(expr
, pc
);
1336 is_affine
= !isl_pw_aff_involves_nan(pa
);
1337 isl_pw_aff_free(pa
);
1342 /* Check if the given if statement is a conditional assignement
1343 * with a non-affine condition.
1345 * In particular we check if "stmt" is of the form
1352 * where the condition is non-affine and a is some array or scalar access.
1354 static int is_conditional_assignment(__isl_keep pet_tree
*tree
,
1355 __isl_keep pet_context
*pc
)
1359 pet_expr
*expr1
, *expr2
;
1361 ctx
= pet_tree_get_ctx(tree
);
1362 if (!pet_options_get_detect_conditional_assignment(ctx
))
1364 if (tree
->type
!= pet_tree_if_else
)
1366 if (tree
->u
.i
.then_body
->type
!= pet_tree_expr
)
1368 if (tree
->u
.i
.else_body
->type
!= pet_tree_expr
)
1370 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1371 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1372 if (pet_expr_get_type(expr1
) != pet_expr_op
)
1374 if (pet_expr_get_type(expr2
) != pet_expr_op
)
1376 if (pet_expr_op_get_type(expr1
) != pet_op_assign
)
1378 if (pet_expr_op_get_type(expr2
) != pet_op_assign
)
1380 expr1
= pet_expr_get_arg(expr1
, 0);
1381 expr2
= pet_expr_get_arg(expr2
, 0);
1382 equal
= pet_expr_is_equal(expr1
, expr2
);
1383 pet_expr_free(expr1
);
1384 pet_expr_free(expr2
);
1385 if (equal
< 0 || !equal
)
1387 if (is_affine_condition(tree
->u
.i
.cond
, pc
))
1393 /* Given that "tree" is of the form
1400 * where a is some array or scalar access, construct a pet_scop
1401 * corresponding to this conditional assignment within the context "pc".
1403 * The constructed pet_scop then corresponds to the expression
1405 * a = condition ? f(...) : g(...)
1407 * All access relations in f(...) are intersected with condition
1408 * while all access relation in g(...) are intersected with the complement.
1410 static struct pet_scop
*scop_from_conditional_assignment(
1411 __isl_keep pet_tree
*tree
, __isl_take pet_context
*pc
,
1412 struct pet_state
*state
)
1416 isl_set
*cond
, *comp
;
1417 isl_multi_pw_aff
*index
;
1418 pet_expr
*expr1
, *expr2
;
1419 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
1420 pet_context
*pc_nested
;
1421 struct pet_scop
*scop
;
1423 pe_cond
= pet_expr_copy(tree
->u
.i
.cond
);
1424 pe_cond
= pet_expr_plug_in_args(pe_cond
, pc
);
1425 pc_nested
= pet_context_copy(pc
);
1426 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1427 pa
= pet_expr_extract_affine_condition(pe_cond
, pc_nested
);
1428 pet_context_free(pc_nested
);
1429 pet_expr_free(pe_cond
);
1430 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
1431 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
1432 index
= isl_multi_pw_aff_from_pw_aff(pa
);
1434 expr1
= tree
->u
.i
.then_body
->u
.e
.expr
;
1435 expr2
= tree
->u
.i
.else_body
->u
.e
.expr
;
1437 pe_cond
= pet_expr_from_index(index
);
1439 pe_then
= pet_expr_get_arg(expr1
, 1);
1440 pe_then
= pet_expr_restrict(pe_then
, cond
);
1441 pe_else
= pet_expr_get_arg(expr2
, 1);
1442 pe_else
= pet_expr_restrict(pe_else
, comp
);
1443 pe_write
= pet_expr_get_arg(expr1
, 0);
1445 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
1446 type_size
= pet_expr_get_type_size(pe_write
);
1447 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, pe_write
, pe
);
1449 scop
= scop_from_expr(pe
, NULL
, state
->n_stmt
++,
1450 pet_tree_get_loc(tree
), pc
);
1452 pet_context_free(pc
);
1457 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1459 * We create a separate statement that writes the result
1460 * of the non-affine condition to a virtual scalar.
1461 * A constraint requiring the value of this virtual scalar to be one
1462 * is added to the iteration domains of the then branch.
1463 * Similarly, a constraint requiring the value of this virtual scalar
1464 * to be zero is added to the iteration domains of the else branch, if any.
1465 * We adjust the schedules to ensure that the virtual scalar is written
1466 * before it is read.
1468 * If there are any breaks or continues in the then and/or else
1469 * branches, then we may have to compute a new skip condition.
1470 * This is handled using a pet_skip_info object.
1471 * On initialization, the object checks if skip conditions need
1472 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1473 * adds them in pet_skip_info_if_add.
1475 static struct pet_scop
*scop_from_non_affine_if(__isl_keep pet_tree
*tree
,
1476 __isl_take pet_context
*pc
, struct pet_state
*state
)
1481 isl_multi_pw_aff
*test_index
;
1482 struct pet_skip_info skip
;
1483 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1485 has_else
= tree
->type
== pet_tree_if_else
;
1487 space
= pet_context_get_space(pc
);
1488 test_index
= pet_create_test_index(space
, state
->n_test
++);
1489 scop
= scop_from_non_affine_condition(pet_expr_copy(tree
->u
.i
.cond
),
1490 state
->n_stmt
++, isl_multi_pw_aff_copy(test_index
),
1491 pet_tree_get_loc(tree
), pc
);
1492 domain
= pet_context_get_domain(pc
);
1493 scop
= pet_scop_add_boolean_array(scop
, domain
,
1494 isl_multi_pw_aff_copy(test_index
), state
->int_size
);
1496 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1498 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1500 pet_skip_info_if_init(&skip
, state
->ctx
, scop_then
, scop_else
,
1502 pet_skip_info_if_extract_index(&skip
, test_index
, pc
, state
);
1504 scop
= pet_scop_prefix(scop
, 0);
1505 scop_then
= pet_scop_prefix(scop_then
, 1);
1506 scop_then
= pet_scop_filter(scop_then
,
1507 isl_multi_pw_aff_copy(test_index
), 1);
1509 scop_else
= pet_scop_prefix(scop_else
, 1);
1510 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
1511 scop_then
= pet_scop_add_par(state
->ctx
, scop_then
, scop_else
);
1513 isl_multi_pw_aff_free(test_index
);
1515 scop
= pet_scop_add_seq(state
->ctx
, scop
, scop_then
);
1517 scop
= pet_skip_info_if_add(&skip
, scop
, 2);
1519 pet_context_free(pc
);
1523 /* Construct a pet_scop for an affine if statement within the context "pc".
1525 * The condition is added to the iteration domains of the then branch,
1526 * while the opposite of the condition in added to the iteration domains
1527 * of the else branch, if any.
1529 * If there are any breaks or continues in the then and/or else
1530 * branches, then we may have to compute a new skip condition.
1531 * This is handled using a pet_skip_info_if object.
1532 * On initialization, the object checks if skip conditions need
1533 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1534 * adds them in pet_skip_info_if_add.
1536 static struct pet_scop
*scop_from_affine_if(__isl_keep pet_tree
*tree
,
1537 __isl_take isl_pw_aff
*cond
, __isl_take pet_context
*pc
,
1538 struct pet_state
*state
)
1544 struct pet_skip_info skip
;
1545 struct pet_scop
*scop
, *scop_then
, *scop_else
= NULL
;
1547 ctx
= pet_tree_get_ctx(tree
);
1549 has_else
= tree
->type
== pet_tree_if_else
;
1551 scop_then
= scop_from_tree(tree
->u
.i
.then_body
, pc
, state
);
1553 scop_else
= scop_from_tree(tree
->u
.i
.else_body
, pc
, state
);
1555 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, has_else
, 1);
1556 pet_skip_info_if_extract_cond(&skip
, cond
, pc
, state
);
1558 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1559 set
= isl_pw_aff_non_zero_set(cond
);
1560 scop
= pet_scop_restrict(scop_then
, isl_set_params(isl_set_copy(set
)));
1563 set
= isl_set_subtract(isl_set_copy(valid
), set
);
1564 scop_else
= pet_scop_restrict(scop_else
, isl_set_params(set
));
1565 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
1568 scop
= pet_scop_resolve_nested(scop
);
1569 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
1571 if (pet_skip_info_has_skip(&skip
))
1572 scop
= pet_scop_prefix(scop
, 0);
1573 scop
= pet_skip_info_if_add(&skip
, scop
, 1);
1575 pet_context_free(pc
);
1579 /* Construct a pet_scop for an if statement within the context "pc".
1581 * If the condition fits the pattern of a conditional assignment,
1582 * then it is handled by scop_from_conditional_assignment.
1584 * Otherwise, we check if the condition is affine.
1585 * If so, we construct the scop in scop_from_affine_if.
1586 * Otherwise, we construct the scop in scop_from_non_affine_if.
1588 * We allow the condition to be dynamic, i.e., to refer to
1589 * scalars or array elements that may be written to outside
1590 * of the given if statement. These nested accesses are then represented
1591 * as output dimensions in the wrapping iteration domain.
1592 * If it is also written _inside_ the then or else branch, then
1593 * we treat the condition as non-affine.
1594 * As explained in extract_non_affine_if, this will introduce
1595 * an extra statement.
1596 * For aesthetic reasons, we want this statement to have a statement
1597 * number that is lower than those of the then and else branches.
1598 * In order to evaluate if we will need such a statement, however, we
1599 * first construct scops for the then and else branches.
1600 * We therefore reserve a statement number if we might have to
1601 * introduce such an extra statement.
1603 static struct pet_scop
*scop_from_if(__isl_keep pet_tree
*tree
,
1604 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1608 pet_expr
*cond_expr
;
1609 pet_context
*pc_nested
;
1614 has_else
= tree
->type
== pet_tree_if_else
;
1616 pc
= pet_context_copy(pc
);
1617 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.then_body
);
1619 pc
= pet_context_clear_writes_in_tree(pc
, tree
->u
.i
.else_body
);
1621 if (is_conditional_assignment(tree
, pc
))
1622 return scop_from_conditional_assignment(tree
, pc
, state
);
1624 cond_expr
= pet_expr_copy(tree
->u
.i
.cond
);
1625 cond_expr
= pet_expr_plug_in_args(cond_expr
, pc
);
1626 pc_nested
= pet_context_copy(pc
);
1627 pc_nested
= pet_context_set_allow_nested(pc_nested
, 1);
1628 cond
= pet_expr_extract_affine_condition(cond_expr
, pc_nested
);
1629 pet_context_free(pc_nested
);
1630 pet_expr_free(cond_expr
);
1633 pet_context_free(pc
);
1637 if (isl_pw_aff_involves_nan(cond
)) {
1638 isl_pw_aff_free(cond
);
1639 return scop_from_non_affine_if(tree
, pc
, state
);
1642 if ((!is_nested_allowed(cond
, tree
->u
.i
.then_body
) ||
1643 (has_else
&& !is_nested_allowed(cond
, tree
->u
.i
.else_body
)))) {
1644 isl_pw_aff_free(cond
);
1645 return scop_from_non_affine_if(tree
, pc
, state
);
1648 return scop_from_affine_if(tree
, cond
, pc
, state
);
1651 /* Return a one-dimensional multi piecewise affine expression that is equal
1652 * to the constant 1 and is defined over the given domain.
1654 static __isl_give isl_multi_pw_aff
*one_mpa(__isl_take isl_space
*space
)
1656 isl_local_space
*ls
;
1659 ls
= isl_local_space_from_space(space
);
1660 aff
= isl_aff_zero_on_domain(ls
);
1661 aff
= isl_aff_set_constant_si(aff
, 1);
1663 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1666 /* Construct a pet_scop for a continue statement with the given domain space.
1668 * We simply create an empty scop with a universal pet_skip_now
1669 * skip condition. This skip condition will then be taken into
1670 * account by the enclosing loop construct, possibly after
1671 * being incorporated into outer skip conditions.
1673 static struct pet_scop
*scop_from_continue(__isl_keep pet_tree
*tree
,
1674 __isl_take isl_space
*space
)
1676 struct pet_scop
*scop
;
1678 scop
= pet_scop_empty(isl_space_copy(space
));
1680 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(space
));
1685 /* Construct a pet_scop for a break statement with the given domain space.
1687 * We simply create an empty scop with both a universal pet_skip_now
1688 * skip condition and a universal pet_skip_later skip condition.
1689 * These skip conditions will then be taken into
1690 * account by the enclosing loop construct, possibly after
1691 * being incorporated into outer skip conditions.
1693 static struct pet_scop
*scop_from_break(__isl_keep pet_tree
*tree
,
1694 __isl_take isl_space
*space
)
1696 struct pet_scop
*scop
;
1697 isl_multi_pw_aff
*skip
;
1699 scop
= pet_scop_empty(isl_space_copy(space
));
1701 skip
= one_mpa(space
);
1702 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
1703 isl_multi_pw_aff_copy(skip
));
1704 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
1709 /* Extract a clone of the kill statement in "scop".
1710 * The domain of the clone is given by "domain".
1711 * "scop" is expected to have been created from a DeclStmt
1712 * and should have the kill as its first statement.
1714 static struct pet_scop
*extract_kill(__isl_keep isl_set
*domain
,
1715 struct pet_scop
*scop
, struct pet_state
*state
)
1718 struct pet_stmt
*stmt
;
1719 isl_multi_pw_aff
*index
;
1723 if (!domain
|| !scop
)
1725 if (scop
->n_stmt
< 1)
1726 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
1727 "expecting at least one statement", return NULL
);
1728 stmt
= scop
->stmts
[0];
1729 if (!pet_stmt_is_kill(stmt
))
1730 isl_die(isl_set_get_ctx(domain
), isl_error_internal
,
1731 "expecting kill statement", return NULL
);
1733 arg
= pet_expr_get_arg(stmt
->body
, 0);
1734 index
= pet_expr_access_get_index(arg
);
1735 access
= pet_expr_access_get_access(arg
);
1737 index
= isl_multi_pw_aff_reset_tuple_id(index
, isl_dim_in
);
1738 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
1739 kill
= pet_expr_kill_from_access_and_index(access
, index
);
1740 stmt
= pet_stmt_from_pet_expr(isl_set_copy(domain
),
1741 pet_loc_copy(stmt
->loc
), NULL
, state
->n_stmt
++, kill
);
1742 return pet_scop_from_pet_stmt(isl_set_get_space(domain
), stmt
);
1745 /* Does "tree" represent an assignment to a variable?
1747 * The assignment may be one of
1748 * - a declaration with initialization
1749 * - an expression with a top-level assignment operator
1751 static int is_assignment(__isl_keep pet_tree
*tree
)
1755 if (tree
->type
== pet_tree_decl_init
)
1757 return pet_tree_is_assign(tree
);
1760 /* Update "pc" by taking into account the assignment performed by "tree",
1761 * where "tree" satisfies is_assignment.
1763 * In particular, if the lhs of the assignment is a scalar variable and
1764 * if the rhs is an affine expression, then keep track of this value in "pc"
1765 * so that we can plug it in when we later come across the same variable.
1767 * The variable has already been marked as having been assigned
1768 * an unknown value by scop_handle_writes.
1770 static __isl_give pet_context
*handle_assignment(__isl_take pet_context
*pc
,
1771 __isl_keep pet_tree
*tree
)
1773 pet_expr
*var
, *val
;
1777 if (pet_tree_get_type(tree
) == pet_tree_decl_init
) {
1778 var
= pet_tree_decl_get_var(tree
);
1779 val
= pet_tree_decl_get_init(tree
);
1782 expr
= pet_tree_expr_get_expr(tree
);
1783 var
= pet_expr_get_arg(expr
, 0);
1784 val
= pet_expr_get_arg(expr
, 1);
1785 pet_expr_free(expr
);
1788 if (!pet_expr_is_scalar_access(var
)) {
1794 pa
= pet_expr_extract_affine(val
, pc
);
1796 pc
= pet_context_free(pc
);
1798 if (!isl_pw_aff_involves_nan(pa
)) {
1799 id
= pet_expr_access_get_id(var
);
1800 pc
= pet_context_set_value(pc
, id
, pa
);
1802 isl_pw_aff_free(pa
);
1810 /* Mark all arrays in "scop" as being exposed.
1812 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
1818 for (i
= 0; i
< scop
->n_array
; ++i
)
1819 scop
->arrays
[i
]->exposed
= 1;
1823 /* Try and construct a pet_scop corresponding to (part of)
1824 * a sequence of statements within the context "pc".
1826 * After extracting a statement, we update "pc"
1827 * based on the top-level assignments in the statement
1828 * so that we can exploit them in subsequent statements in the same block.
1830 * If there are any breaks or continues in the individual statements,
1831 * then we may have to compute a new skip condition.
1832 * This is handled using a pet_skip_info object.
1833 * On initialization, the object checks if skip conditions need
1834 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1835 * adds them in pet_skip_info_seq_add.
1837 * If "block" is set, then we need to insert kill statements at
1838 * the end of the block for any array that has been declared by
1839 * one of the statements in the sequence. Each of these declarations
1840 * results in the construction of a kill statement at the place
1841 * of the declaration, so we simply collect duplicates of
1842 * those kill statements and append these duplicates to the constructed scop.
1844 * If "block" is not set, then any array declared by one of the statements
1845 * in the sequence is marked as being exposed.
1847 * If autodetect is set, then we allow the extraction of only a subrange
1848 * of the sequence of statements. However, if there is at least one statement
1849 * for which we could not construct a scop and the final range contains
1850 * either no statements or at least one kill, then we discard the entire
1853 static struct pet_scop
*scop_from_block(__isl_keep pet_tree
*tree
,
1854 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1860 struct pet_scop
*scop
, *kills
;
1862 ctx
= pet_tree_get_ctx(tree
);
1864 space
= pet_context_get_space(pc
);
1865 domain
= pet_context_get_domain(pc
);
1866 pc
= pet_context_copy(pc
);
1867 scop
= pet_scop_empty(isl_space_copy(space
));
1868 kills
= pet_scop_empty(space
);
1869 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
1870 struct pet_scop
*scop_i
;
1872 scop_i
= scop_from_tree(tree
->u
.b
.child
[i
], pc
, state
);
1873 pc
= scop_handle_writes(scop_i
, pc
);
1874 if (is_assignment(tree
->u
.b
.child
[i
]))
1875 pc
= handle_assignment(pc
, tree
->u
.b
.child
[i
]);
1876 struct pet_skip_info skip
;
1877 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
1878 pet_skip_info_seq_extract(&skip
, pc
, state
);
1879 if (pet_skip_info_has_skip(&skip
))
1880 scop_i
= pet_scop_prefix(scop_i
, 0);
1881 if (scop_i
&& pet_tree_is_decl(tree
->u
.b
.child
[i
])) {
1882 if (tree
->u
.b
.block
) {
1883 struct pet_scop
*kill
;
1884 kill
= extract_kill(domain
, scop_i
, state
);
1885 kills
= pet_scop_add_par(ctx
, kills
, kill
);
1887 scop_i
= mark_exposed(scop_i
);
1889 scop_i
= pet_scop_prefix(scop_i
, i
);
1890 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
1892 scop
= pet_skip_info_seq_add(&skip
, scop
, i
);
1897 isl_set_free(domain
);
1899 kills
= pet_scop_prefix(kills
, tree
->u
.b
.n
);
1900 scop
= pet_scop_add_seq(ctx
, scop
, kills
);
1902 pet_context_free(pc
);
1907 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1908 * within the context "pc" by calling the appropriate function
1909 * based on the type of "tree".
1911 static struct pet_scop
*scop_from_tree(__isl_keep pet_tree
*tree
,
1912 __isl_keep pet_context
*pc
, struct pet_state
*state
)
1917 switch (tree
->type
) {
1918 case pet_tree_error
:
1920 case pet_tree_block
:
1921 return scop_from_block(tree
, pc
, state
);
1922 case pet_tree_break
:
1923 return scop_from_break(tree
, pet_context_get_space(pc
));
1924 case pet_tree_continue
:
1925 return scop_from_continue(tree
, pet_context_get_space(pc
));
1927 case pet_tree_decl_init
:
1928 return scop_from_decl(tree
, pc
, state
);
1930 return scop_from_expr(pet_expr_copy(tree
->u
.e
.expr
),
1931 isl_id_copy(tree
->label
),
1933 pet_tree_get_loc(tree
), pc
);
1935 case pet_tree_if_else
:
1936 return scop_from_if(tree
, pc
, state
);
1938 return scop_from_for(tree
, pc
, state
);
1939 case pet_tree_while
:
1940 return scop_from_while(tree
, pc
, state
);
1941 case pet_tree_infinite_loop
:
1942 return scop_from_infinite_for(tree
, pc
, state
);
1945 isl_die(tree
->ctx
, isl_error_internal
, "unhandled type",
1949 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1950 * "int_size" is the number of bytes need to represent an integer.
1951 * "extract_array" is a callback that we can use to create a pet_array
1952 * that corresponds to the variable accessed by an expression.
1954 * Initialize the global state, construct a context and then
1955 * construct the pet_scop by recursively visiting the tree.
1957 struct pet_scop
*pet_scop_from_pet_tree(__isl_take pet_tree
*tree
, int int_size
,
1958 struct pet_array
*(*extract_array
)(__isl_keep pet_expr
*access
,
1959 __isl_keep pet_context
*pc
, void *user
), void *user
,
1960 __isl_keep pet_context
*pc
)
1962 struct pet_scop
*scop
;
1963 struct pet_state state
= { 0 };
1968 state
.ctx
= pet_tree_get_ctx(tree
);
1969 state
.int_size
= int_size
;
1970 state
.extract_array
= extract_array
;
1973 scop
= scop_from_tree(tree
, pc
, &state
);
1974 scop
= pet_scop_set_loc(scop
, pet_tree_get_loc(tree
));
1976 pet_tree_free(tree
);