pet_scop_from_pet_tree: create statements directly from expression trees
[pet.git] / tree2scop.c
blob689c1c56eaccf8070117931b2c2bac5e766add7a
1 /*
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
7 * are met:
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
32 * Leiden University.
35 #include <isl/id_to_pw_aff.h>
37 #include "aff.h"
38 #include "expr.h"
39 #include "expr_arg.h"
40 #include "nest.h"
41 #include "scop.h"
42 #include "skip.h"
43 #include "state.h"
44 #include "tree2scop.h"
46 /* Update "pc" by taking into account the writes in "stmt".
47 * That is, clear any previously assigned values to variables
48 * that are written by "stmt".
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_tree(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)
61 int i;
63 if (!scop)
64 return pet_context_free(pc);
65 for (i = 0; i < scop->n_stmt; ++i)
66 pc = handle_writes(scop->stmts[i], pc);
68 return pc;
71 /* Wrapper around pet_expr_resolve_assume
72 * for use as a callback to pet_tree_map_expr.
74 static __isl_give pet_expr *resolve_assume(__isl_take pet_expr *expr,
75 void *user)
77 pet_context *pc = user;
79 return pet_expr_resolve_assume(expr, pc);
82 /* Check if any expression inside "tree" is an assume expression and
83 * if its single argument can be converted to an affine expression
84 * in the context of "pc".
85 * If so, replace the argument by the affine expression.
87 __isl_give pet_tree *pet_tree_resolve_assume(__isl_take pet_tree *tree,
88 __isl_keep pet_context *pc)
90 return pet_tree_map_expr(tree, &resolve_assume, pc);
93 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
94 * "tree" has already been evaluated in the context of "pc".
95 * This mainly involves resolving nested expression parameters
96 * and setting the name of the iteration space.
97 * The name is given by tree->label if it is non-NULL. Otherwise,
98 * it is of the form S_<stmt_nr>.
100 static struct pet_scop *scop_from_evaluated_tree(__isl_take pet_tree *tree,
101 int stmt_nr, __isl_keep pet_context *pc)
103 isl_space *space;
104 isl_set *domain;
105 struct pet_stmt *ps;
107 space = pet_context_get_space(pc);
109 tree = pet_tree_resolve_nested(tree, space);
110 tree = pet_tree_resolve_assume(tree, pc);
112 domain = pet_context_get_domain(pc);
113 ps = pet_stmt_from_pet_tree(domain, stmt_nr, tree);
114 return pet_scop_from_pet_stmt(space, ps);
117 /* Convert a top-level pet_expr to a pet_scop with one statement
118 * within the context "pc".
119 * "expr" has already been evaluated in the context of "pc".
120 * We construct a pet_tree from "expr" and continue with
121 * scop_from_evaluated_tree.
122 * The name is of the form S_<stmt_nr>.
123 * The location of the statement is set to "loc".
125 static struct pet_scop *scop_from_evaluated_expr(__isl_take pet_expr *expr,
126 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
128 pet_tree *tree;
130 tree = pet_tree_new_expr(expr);
131 tree = pet_tree_set_loc(tree, loc);
132 return scop_from_evaluated_tree(tree, stmt_nr, pc);
135 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
136 * "tree" has not yet been evaluated in the context of "pc".
137 * We evaluate "tree" in the context of "pc" and continue with
138 * scop_from_evaluated_tree.
139 * The statement name is given by tree->label if it is non-NULL. Otherwise,
140 * it is of the form S_<stmt_nr>.
142 static struct pet_scop *scop_from_unevaluated_tree(__isl_take pet_tree *tree,
143 int stmt_nr, __isl_keep pet_context *pc)
145 tree = pet_context_evaluate_tree(pc, tree);
146 return scop_from_evaluated_tree(tree, stmt_nr, pc);
149 /* Convert a top-level pet_expr to a pet_scop with one statement
150 * within the context "pc", where "expr" has not yet been evaluated
151 * in the context of "pc".
152 * We construct a pet_tree from "expr" and continue with
153 * scop_from_unevaluated_tree.
154 * The statement name is of the form S_<stmt_nr>.
155 * The location of the statement is set to "loc".
157 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
158 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
160 pet_tree *tree;
162 tree = pet_tree_new_expr(expr);
163 tree = pet_tree_set_loc(tree, loc);
164 return scop_from_unevaluated_tree(tree, stmt_nr, pc);
167 /* Construct a pet_scop with a single statement killing the entire
168 * array "array".
169 * The location of the statement is set to "loc".
171 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
172 __isl_keep pet_context *pc, struct pet_state *state)
174 isl_ctx *ctx;
175 isl_id *id;
176 isl_space *space;
177 isl_multi_pw_aff *index;
178 isl_map *access;
179 pet_expr *expr;
180 struct pet_scop *scop;
182 if (!array)
183 goto error;
184 ctx = isl_set_get_ctx(array->extent);
185 access = isl_map_from_range(isl_set_copy(array->extent));
186 id = isl_set_get_tuple_id(array->extent);
187 space = isl_space_alloc(ctx, 0, 0, 0);
188 space = isl_space_set_tuple_id(space, isl_dim_out, id);
189 index = isl_multi_pw_aff_zero(space);
190 expr = pet_expr_kill_from_access_and_index(access, index);
191 return scop_from_expr(expr, state->n_stmt++, loc, pc);
192 error:
193 pet_loc_free(loc);
194 return NULL;
197 /* Construct and return a pet_array corresponding to the variable
198 * accessed by "access" by calling the extract_array callback.
200 static struct pet_array *extract_array(__isl_keep pet_expr *access,
201 __isl_keep pet_context *pc, struct pet_state *state)
203 return state->extract_array(access, pc, state->user);
206 /* Construct a pet_scop for a (single) variable declaration
207 * within the context "pc".
209 * The scop contains the variable being declared (as an array)
210 * and a statement killing the array.
212 * If the declaration comes with an initialization, then the scop
213 * also contains an assignment to the variable.
215 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
216 __isl_keep pet_context *pc, struct pet_state *state)
218 int type_size;
219 isl_ctx *ctx;
220 struct pet_array *array;
221 struct pet_scop *scop_decl, *scop;
222 pet_expr *lhs, *rhs, *pe;
224 array = extract_array(tree->u.d.var, pc, state);
225 if (array)
226 array->declared = 1;
227 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
228 scop_decl = pet_scop_add_array(scop_decl, array);
230 if (tree->type != pet_tree_decl_init)
231 return scop_decl;
233 lhs = pet_expr_copy(tree->u.d.var);
234 rhs = pet_expr_copy(tree->u.d.init);
235 type_size = pet_expr_get_type_size(lhs);
236 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
237 scop = scop_from_expr(pe, state->n_stmt++, pet_tree_get_loc(tree), pc);
239 scop_decl = pet_scop_prefix(scop_decl, 0);
240 scop = pet_scop_prefix(scop, 1);
242 ctx = pet_tree_get_ctx(tree);
243 scop = pet_scop_add_seq(ctx, scop_decl, scop);
245 return scop;
248 /* Return those elements in the space of "cond" that come after
249 * (based on "sign") an element in "cond" in the final dimension.
251 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
253 isl_space *space;
254 isl_map *previous_to_this;
255 int i, dim;
257 dim = isl_set_dim(cond, isl_dim_set);
258 space = isl_space_map_from_set(isl_set_get_space(cond));
259 previous_to_this = isl_map_universe(space);
260 for (i = 0; i + 1 < dim; ++i)
261 previous_to_this = isl_map_equate(previous_to_this,
262 isl_dim_in, i, isl_dim_out, i);
263 if (sign > 0)
264 previous_to_this = isl_map_order_lt(previous_to_this,
265 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
266 else
267 previous_to_this = isl_map_order_gt(previous_to_this,
268 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
270 cond = isl_set_apply(cond, previous_to_this);
272 return cond;
275 /* Remove those iterations of "domain" that have an earlier iteration
276 * (based on "sign") in the final dimension where "skip" is satisfied.
277 * If "apply_skip_map" is set, then "skip_map" is first applied
278 * to the embedded skip condition before removing it from the domain.
280 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
281 __isl_take isl_set *skip, int sign,
282 int apply_skip_map, __isl_keep isl_map *skip_map)
284 if (apply_skip_map)
285 skip = isl_set_apply(skip, isl_map_copy(skip_map));
286 skip = isl_set_intersect(skip , isl_set_copy(domain));
287 return isl_set_subtract(domain, after(skip, sign));
290 /* Create an affine expression on the domain space of "pc" that
291 * is equal to the final dimension of this domain.
293 static __isl_give isl_aff *map_to_last(__isl_keep pet_context *pc)
295 int pos;
296 isl_space *space;
297 isl_local_space *ls;
299 space = pet_context_get_space(pc);
300 pos = isl_space_dim(space, isl_dim_set) - 1;
301 ls = isl_local_space_from_space(space);
302 return isl_aff_var_on_domain(ls, isl_dim_set, pos);
305 /* Create an affine expression that maps elements
306 * of an array "id_test" to the previous element in the final dimension
307 * (according to "inc"), provided this element belongs to "domain".
308 * That is, create the affine expression
310 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
312 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
313 __isl_take isl_set *domain, __isl_take isl_val *inc)
315 int pos;
316 isl_space *space;
317 isl_aff *aff;
318 isl_pw_aff *pa;
319 isl_multi_aff *ma;
320 isl_multi_pw_aff *prev;
322 pos = isl_set_dim(domain, isl_dim_set) - 1;
323 space = isl_set_get_space(domain);
324 space = isl_space_map_from_set(space);
325 ma = isl_multi_aff_identity(space);
326 aff = isl_multi_aff_get_aff(ma, pos);
327 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
328 ma = isl_multi_aff_set_aff(ma, pos, aff);
329 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
330 prev = isl_multi_pw_aff_from_multi_aff(ma);
331 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
332 pa = isl_pw_aff_intersect_domain(pa, domain);
333 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
334 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
336 return prev;
339 /* Add an implication to "scop" expressing that if an element of
340 * virtual array "id_test" has value "satisfied" then all previous elements
341 * of this array (in the final dimension) also have that value.
342 * The set of previous elements is bounded by "domain".
343 * If "sign" is negative then the iterator
344 * is decreasing and we express that all subsequent array elements
345 * (but still defined previously) have the same value.
347 static struct pet_scop *add_implication(struct pet_scop *scop,
348 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
349 int satisfied)
351 int i, dim;
352 isl_space *space;
353 isl_map *map;
355 dim = isl_set_dim(domain, isl_dim_set);
356 domain = isl_set_set_tuple_id(domain, id_test);
357 space = isl_space_map_from_set(isl_set_get_space(domain));
358 map = isl_map_universe(space);
359 for (i = 0; i + 1 < dim; ++i)
360 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
361 if (sign > 0)
362 map = isl_map_order_ge(map,
363 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
364 else
365 map = isl_map_order_le(map,
366 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
367 map = isl_map_intersect_range(map, domain);
368 scop = pet_scop_add_implication(scop, map, satisfied);
370 return scop;
373 /* Add a filter to "scop" that imposes that it is only executed
374 * when the variable identified by "id_test" has a zero value
375 * for all previous iterations of "domain".
377 * In particular, add a filter that imposes that the array
378 * has a zero value at the previous iteration of domain and
379 * add an implication that implies that it then has that
380 * value for all previous iterations.
382 static struct pet_scop *scop_add_break(struct pet_scop *scop,
383 __isl_take isl_id *id_test, __isl_take isl_set *domain,
384 __isl_take isl_val *inc)
386 isl_multi_pw_aff *prev;
387 int sign = isl_val_sgn(inc);
389 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
390 scop = add_implication(scop, id_test, domain, sign, 0);
391 scop = pet_scop_filter(scop, prev, 0);
393 return scop;
396 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
397 __isl_keep pet_context *pc, struct pet_state *state);
399 /* Construct a pet_scop for an infinite loop around the given body
400 * within the context "pc".
402 * The domain of "pc" has already been extended with an infinite loop
404 * { [t] : t >= 0 }
406 * We extract a pet_scop for the body and then embed it in a loop with
407 * schedule
409 * { [outer,t] -> [t] }
411 * If the body contains any break, then it is taken into
412 * account in apply_affine_break (if the skip condition is affine)
413 * or in scop_add_break (if the skip condition is not affine).
415 * Note that in case of an affine skip condition,
416 * since we are dealing with a loop without loop iterator,
417 * the skip condition cannot refer to the current loop iterator and
418 * so effectively, the effect on the iteration domain is of the form
420 * { [outer,0]; [outer,t] : t >= 1 and not skip }
422 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
423 __isl_keep pet_context *pc, struct pet_state *state)
425 isl_ctx *ctx;
426 isl_id *id_test;
427 isl_set *domain;
428 isl_set *skip;
429 isl_aff *sched;
430 struct pet_scop *scop;
431 int has_affine_break;
432 int has_var_break;
434 ctx = pet_tree_get_ctx(body);
435 domain = pet_context_get_domain(pc);
436 sched = map_to_last(pc);
438 scop = scop_from_tree(body, pc, state);
440 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
441 if (has_affine_break)
442 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
443 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
444 if (has_var_break)
445 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
447 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
448 if (has_affine_break) {
449 domain = apply_affine_break(domain, skip, 1, 0, NULL);
450 scop = pet_scop_intersect_domain_prefix(scop,
451 isl_set_copy(domain));
453 if (has_var_break)
454 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
455 else
456 isl_set_free(domain);
458 return scop;
461 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
463 * for (;;)
464 * body
466 * within the context "pc".
468 * Extend the domain of "pc" with an extra inner loop
470 * { [t] : t >= 0 }
472 * and construct the scop in scop_from_infinite_loop.
474 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
475 __isl_keep pet_context *pc, struct pet_state *state)
477 struct pet_scop *scop;
479 pc = pet_context_copy(pc);
480 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
482 pc = pet_context_add_infinite_loop(pc);
484 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
486 pet_context_free(pc);
488 return scop;
491 /* Construct a pet_scop for a while loop of the form
493 * while (pa)
494 * body
496 * within the context "pc".
498 * The domain of "pc" has already been extended with an infinite loop
500 * { [t] : t >= 0 }
502 * Here, we add the constraints on the outer loop iterators
503 * implied by "pa" and construct the scop in scop_from_infinite_loop.
504 * Note that the intersection with these constraints
505 * may result in an empty loop.
507 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
508 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
509 struct pet_state *state)
511 struct pet_scop *scop;
512 isl_set *dom, *local;
513 isl_set *valid;
515 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
516 dom = isl_pw_aff_non_zero_set(pa);
517 local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
518 pc = pet_context_intersect_domain(pc, local);
519 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
520 scop = pet_scop_restrict(scop, dom);
521 scop = pet_scop_restrict_context(scop, valid);
523 pet_context_free(pc);
524 return scop;
527 /* Construct a scop for a while, given the scops for the condition
528 * and the body, the filter identifier and the iteration domain of
529 * the while loop.
531 * In particular, the scop for the condition is filtered to depend
532 * on "id_test" evaluating to true for all previous iterations
533 * of the loop, while the scop for the body is filtered to depend
534 * on "id_test" evaluating to true for all iterations up to the
535 * current iteration.
536 * The actual filter only imposes that this virtual array has
537 * value one on the previous or the current iteration.
538 * The fact that this condition also applies to the previous
539 * iterations is enforced by an implication.
541 * These filtered scops are then combined into a single scop.
543 * "sign" is positive if the iterator increases and negative
544 * if it decreases.
546 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
547 struct pet_scop *scop_body, __isl_take isl_id *id_test,
548 __isl_take isl_set *domain, __isl_take isl_val *inc)
550 isl_ctx *ctx = isl_set_get_ctx(domain);
551 isl_space *space;
552 isl_multi_pw_aff *test_index;
553 isl_multi_pw_aff *prev;
554 int sign = isl_val_sgn(inc);
555 struct pet_scop *scop;
557 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
558 scop_cond = pet_scop_filter(scop_cond, prev, 1);
560 space = isl_space_map_from_set(isl_set_get_space(domain));
561 test_index = isl_multi_pw_aff_identity(space);
562 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
563 isl_id_copy(id_test));
564 scop_body = pet_scop_filter(scop_body, test_index, 1);
566 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
567 scop = add_implication(scop, id_test, domain, sign, 1);
569 return scop;
572 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
573 * evaluating "cond" and writing the result to a virtual scalar,
574 * as expressed by "index".
575 * The expression "cond" has not yet been evaluated in the context of "pc".
576 * Do so within the context "pc".
577 * The location of the statement is set to "loc".
579 static struct pet_scop *scop_from_non_affine_condition(
580 __isl_take pet_expr *cond, int stmt_nr,
581 __isl_take isl_multi_pw_aff *index,
582 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
584 pet_expr *expr, *write;
586 cond = pet_context_evaluate_expr(pc, cond);
588 write = pet_expr_from_index(index);
589 write = pet_expr_access_set_write(write, 1);
590 write = pet_expr_access_set_read(write, 0);
591 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
593 return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
596 /* Construct a generic while scop, with iteration domain
597 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
598 * The domain of "pc" has already been extended with this infinite loop
600 * { [t] : t >= 0 }
602 * The scop consists of two parts,
603 * one for evaluating the condition "cond" and one for the body.
604 * If "expr_inc" is not NULL, then a scop for evaluating this expression
605 * is added at the end of the body,
606 * after replacing any skip conditions resulting from continue statements
607 * by the skip conditions resulting from break statements (if any).
609 * The schedule is adjusted to reflect that the condition is evaluated
610 * before the body is executed and the body is filtered to depend
611 * on the result of the condition evaluating to true on all iterations
612 * up to the current iteration, while the evaluation of the condition itself
613 * is filtered to depend on the result of the condition evaluating to true
614 * on all previous iterations.
615 * The context of the scop representing the body is dropped
616 * because we don't know how many times the body will be executed,
617 * if at all.
619 * If the body contains any break, then it is taken into
620 * account in apply_affine_break (if the skip condition is affine)
621 * or in scop_add_break (if the skip condition is not affine).
623 * Note that in case of an affine skip condition,
624 * since we are dealing with a loop without loop iterator,
625 * the skip condition cannot refer to the current loop iterator and
626 * so effectively, the effect on the iteration domain is of the form
628 * { [outer,0]; [outer,t] : t >= 1 and not skip }
630 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
631 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
632 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
633 struct pet_state *state)
635 isl_ctx *ctx;
636 isl_id *id_test, *id_break_test;
637 isl_space *space;
638 isl_multi_pw_aff *test_index;
639 isl_set *domain;
640 isl_set *skip;
641 isl_aff *sched;
642 struct pet_scop *scop, *scop_body;
643 int has_affine_break;
644 int has_var_break;
646 ctx = state->ctx;
647 space = pet_context_get_space(pc);
648 test_index = pet_create_test_index(space, state->n_test++);
649 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
650 isl_multi_pw_aff_copy(test_index),
651 pet_loc_copy(loc), pc);
652 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
653 domain = pet_context_get_domain(pc);
654 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
655 test_index, state->int_size);
657 sched = map_to_last(pc);
659 scop_body = scop_from_tree(tree_body, pc, state);
661 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
662 if (has_affine_break)
663 skip = pet_scop_get_affine_skip_domain(scop_body,
664 pet_skip_later);
665 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
666 if (has_var_break)
667 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
669 scop = pet_scop_prefix(scop, 0);
670 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(sched));
671 scop_body = pet_scop_reset_context(scop_body);
672 scop_body = pet_scop_prefix(scop_body, 1);
673 if (expr_inc) {
674 struct pet_scop *scop_inc;
675 scop_inc = scop_from_expr(expr_inc, state->n_stmt++, loc, pc);
676 scop_inc = pet_scop_prefix(scop_inc, 2);
677 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
678 isl_multi_pw_aff *skip;
679 skip = pet_scop_get_skip(scop_body, pet_skip_later);
680 scop_body = pet_scop_set_skip(scop_body,
681 pet_skip_now, skip);
682 } else
683 pet_scop_reset_skip(scop_body, pet_skip_now);
684 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
685 } else
686 pet_loc_free(loc);
687 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain), sched);
689 if (has_affine_break) {
690 domain = apply_affine_break(domain, skip, 1, 0, NULL);
691 scop = pet_scop_intersect_domain_prefix(scop,
692 isl_set_copy(domain));
693 scop_body = pet_scop_intersect_domain_prefix(scop_body,
694 isl_set_copy(domain));
696 if (has_var_break) {
697 scop = scop_add_break(scop, isl_id_copy(id_break_test),
698 isl_set_copy(domain), isl_val_one(ctx));
699 scop_body = scop_add_break(scop_body, id_break_test,
700 isl_set_copy(domain), isl_val_one(ctx));
702 scop = scop_add_while(scop, scop_body, id_test, domain,
703 isl_val_one(ctx));
705 pet_context_free(pc);
706 return scop;
709 /* Check if the while loop is of the form
711 * while (affine expression)
712 * body
714 * If so, call scop_from_affine_while to construct a scop.
716 * Otherwise, pass control to scop_from_non_affine_while.
718 * "pc" is the context in which the affine expressions in the scop are created.
719 * The domain of "pc" is extended with an infinite loop
721 * { [t] : t >= 0 }
723 * before passing control to scop_from_affine_while or
724 * scop_from_non_affine_while.
726 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
727 __isl_keep pet_context *pc, struct pet_state *state)
729 pet_expr *cond_expr;
730 isl_pw_aff *pa;
732 if (!tree)
733 return NULL;
735 pc = pet_context_copy(pc);
736 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
738 cond_expr = pet_expr_copy(tree->u.l.cond);
739 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
740 pa = pet_expr_extract_affine_condition(cond_expr, pc);
741 pet_expr_free(cond_expr);
743 pc = pet_context_add_infinite_loop(pc);
745 if (!pa)
746 goto error;
748 if (!isl_pw_aff_involves_nan(pa))
749 return scop_from_affine_while(tree, pa, pc, state);
750 isl_pw_aff_free(pa);
751 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
752 pet_tree_get_loc(tree), tree->u.l.body, NULL,
753 pc, state);
754 error:
755 pet_context_free(pc);
756 return NULL;
759 /* Check whether "cond" expresses a simple loop bound
760 * on the final set dimension.
761 * In particular, if "up" is set then "cond" should contain only
762 * upper bounds on the final set dimension.
763 * Otherwise, it should contain only lower bounds.
765 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
767 int pos;
769 pos = isl_set_dim(cond, isl_dim_set) - 1;
770 if (isl_val_is_pos(inc))
771 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
772 else
773 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
776 /* Extend a condition on a given iteration of a loop to one that
777 * imposes the same condition on all previous iterations.
778 * "domain" expresses the lower [upper] bound on the iterations
779 * when inc is positive [negative] in its final dimension.
781 * In particular, we construct the condition (when inc is positive)
783 * forall i' : (domain(i') and i' <= i) => cond(i')
785 * (where "<=" applies to the final dimension)
786 * which is equivalent to
788 * not exists i' : domain(i') and i' <= i and not cond(i')
790 * We construct this set by subtracting the satisfying cond from domain,
791 * applying a map
793 * { [i'] -> [i] : i' <= i }
795 * and then subtracting the result from domain again.
797 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
798 __isl_take isl_set *domain, __isl_take isl_val *inc)
800 isl_space *space;
801 isl_map *previous_to_this;
802 int i, dim;
804 dim = isl_set_dim(cond, isl_dim_set);
805 space = isl_space_map_from_set(isl_set_get_space(cond));
806 previous_to_this = isl_map_universe(space);
807 for (i = 0; i + 1 < dim; ++i)
808 previous_to_this = isl_map_equate(previous_to_this,
809 isl_dim_in, i, isl_dim_out, i);
810 if (isl_val_is_pos(inc))
811 previous_to_this = isl_map_order_le(previous_to_this,
812 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
813 else
814 previous_to_this = isl_map_order_ge(previous_to_this,
815 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
817 cond = isl_set_subtract(isl_set_copy(domain), cond);
818 cond = isl_set_apply(cond, previous_to_this);
819 cond = isl_set_subtract(domain, cond);
821 isl_val_free(inc);
823 return cond;
826 /* Given an initial value of the form
828 * { [outer,i] -> init(outer) }
830 * construct a domain of the form
832 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
834 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
835 __isl_take isl_val *inc)
837 int dim;
838 isl_aff *aff;
839 isl_space *space;
840 isl_local_space *ls;
841 isl_set *set;
843 dim = isl_pw_aff_dim(init, isl_dim_in);
845 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
846 space = isl_pw_aff_get_domain_space(init);
847 ls = isl_local_space_from_space(space);
848 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
849 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
850 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
852 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
853 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
855 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
856 set = isl_set_project_out(set, isl_dim_set, dim, 1);
858 return set;
861 /* Assuming "cond" represents a bound on a loop where the loop
862 * iterator "iv" is incremented (or decremented) by one, check if wrapping
863 * is possible.
865 * Under the given assumptions, wrapping is only possible if "cond" allows
866 * for the last value before wrapping, i.e., 2^width - 1 in case of an
867 * increasing iterator and 0 in case of a decreasing iterator.
869 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
870 __isl_keep isl_val *inc)
872 int cw;
873 isl_ctx *ctx;
874 isl_val *limit;
875 isl_set *test;
877 test = isl_set_copy(cond);
879 ctx = isl_set_get_ctx(test);
880 if (isl_val_is_neg(inc))
881 limit = isl_val_zero(ctx);
882 else {
883 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
884 limit = isl_val_2exp(limit);
885 limit = isl_val_sub_ui(limit, 1);
888 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
889 cw = !isl_set_is_empty(test);
890 isl_set_free(test);
892 return cw;
895 /* Given a space
897 * { [outer, v] },
899 * construct the following affine expression on this space
901 * { [outer, v] -> [outer, v mod 2^width] }
903 * where width is the number of bits used to represent the values
904 * of the unsigned variable "iv".
906 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
907 __isl_keep pet_expr *iv)
909 int dim;
910 isl_ctx *ctx;
911 isl_val *mod;
912 isl_aff *aff;
913 isl_multi_aff *ma;
915 dim = isl_space_dim(space, isl_dim_set);
917 ctx = isl_space_get_ctx(space);
918 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
919 mod = isl_val_2exp(mod);
921 space = isl_space_map_from_set(space);
922 ma = isl_multi_aff_identity(space);
924 aff = isl_multi_aff_get_aff(ma, dim - 1);
925 aff = isl_aff_mod_val(aff, mod);
926 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
928 return ma;
931 /* Given two sets in the space
933 * { [l,i] },
935 * where l represents the outer loop iterators, compute the set
936 * of values of l that ensure that "set1" is a subset of "set2".
938 * set1 is a subset of set2 if
940 * forall i: set1(l,i) => set2(l,i)
942 * or
944 * not exists i: set1(l,i) and not set2(l,i)
946 * i.e.,
948 * not exists i: (set1 \ set2)(l,i)
950 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
951 __isl_take isl_set *set2)
953 int pos;
955 pos = isl_set_dim(set1, isl_dim_set) - 1;
956 set1 = isl_set_subtract(set1, set2);
957 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
958 return isl_set_complement(set1);
961 /* Compute the set of outer iterator values for which "cond" holds
962 * on the next iteration of the inner loop for each element of "dom".
964 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
965 * to the outer loop iterators), plug that into "cond"
966 * and then compute the set of outer iterators for which "dom" is a subset
967 * of the result.
969 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
970 __isl_take isl_set *dom, __isl_take isl_val *inc)
972 int pos;
973 isl_space *space;
974 isl_aff *aff;
975 isl_multi_aff *ma;
977 pos = isl_set_dim(dom, isl_dim_set) - 1;
978 space = isl_set_get_space(dom);
979 space = isl_space_map_from_set(space);
980 ma = isl_multi_aff_identity(space);
981 aff = isl_multi_aff_get_aff(ma, pos);
982 aff = isl_aff_add_constant_val(aff, inc);
983 ma = isl_multi_aff_set_aff(ma, pos, aff);
984 cond = isl_set_preimage_multi_aff(cond, ma);
986 return enforce_subset(dom, cond);
989 /* Extract the for loop "tree" as a while loop within the context "pc_init".
990 * In particular, "pc_init" represents the context of the loop,
991 * whereas "pc" represents the context of the body of the loop and
992 * has already had its domain extended with an infinite loop
994 * { [t] : t >= 0 }
996 * The for loop has the form
998 * for (iv = init; cond; iv += inc)
999 * body;
1001 * and is treated as
1003 * iv = init;
1004 * while (cond) {
1005 * body;
1006 * iv += inc;
1009 * except that the skips resulting from any continue statements
1010 * in body do not apply to the increment, but are replaced by the skips
1011 * resulting from break statements.
1013 * If the loop iterator is declared in the for loop, then it is killed before
1014 * and after the loop.
1016 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1017 __isl_keep pet_context *init_pc, __isl_take pet_context *pc,
1018 struct pet_state *state)
1020 int declared;
1021 isl_id *iv;
1022 pet_expr *expr_iv, *init, *inc;
1023 struct pet_scop *scop_init, *scop;
1024 int type_size;
1025 struct pet_array *array;
1026 struct pet_scop *scop_kill;
1028 iv = pet_expr_access_get_id(tree->u.l.iv);
1029 pc = pet_context_clear_value(pc, iv);
1031 declared = tree->u.l.declared;
1033 expr_iv = pet_expr_copy(tree->u.l.iv);
1034 type_size = pet_expr_get_type_size(expr_iv);
1035 init = pet_expr_copy(tree->u.l.init);
1036 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1037 scop_init = scop_from_expr(init, state->n_stmt++,
1038 pet_tree_get_loc(tree), init_pc);
1039 scop_init = pet_scop_prefix(scop_init, declared);
1041 expr_iv = pet_expr_copy(tree->u.l.iv);
1042 type_size = pet_expr_get_type_size(expr_iv);
1043 inc = pet_expr_copy(tree->u.l.inc);
1044 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1046 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1047 pet_tree_get_loc(tree), tree->u.l.body, inc,
1048 pet_context_copy(pc), state);
1050 scop = pet_scop_prefix(scop, declared + 1);
1051 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1053 pet_context_free(pc);
1055 if (!declared)
1056 return scop;
1058 array = extract_array(tree->u.l.iv, init_pc, state);
1059 if (array)
1060 array->declared = 1;
1061 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1062 scop_kill = pet_scop_prefix(scop_kill, 0);
1063 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1064 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1065 scop_kill = pet_scop_add_array(scop_kill, array);
1066 scop_kill = pet_scop_prefix(scop_kill, 3);
1067 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1069 return scop;
1072 /* Given an access expression "expr", is the variable accessed by
1073 * "expr" assigned anywhere inside "tree"?
1075 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1077 int assigned = 0;
1078 isl_id *id;
1080 id = pet_expr_access_get_id(expr);
1081 assigned = pet_tree_writes(tree, id);
1082 isl_id_free(id);
1084 return assigned;
1087 /* Are all nested access parameters in "pa" allowed given "tree".
1088 * In particular, is none of them written by anywhere inside "tree".
1090 * If "tree" has any continue nodes in the current loop level,
1091 * then no nested access parameters are allowed.
1092 * In particular, if there is any nested access in a guard
1093 * for a piece of code containing a "continue", then we want to introduce
1094 * a separate statement for evaluating this guard so that we can express
1095 * that the result is false for all previous iterations.
1097 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1098 __isl_keep pet_tree *tree)
1100 int i, nparam;
1102 if (!tree)
1103 return -1;
1105 if (!pet_nested_any_in_pw_aff(pa))
1106 return 1;
1108 if (pet_tree_has_continue(tree))
1109 return 0;
1111 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1112 for (i = 0; i < nparam; ++i) {
1113 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1114 pet_expr *expr;
1115 int allowed;
1117 if (!pet_nested_in_id(id)) {
1118 isl_id_free(id);
1119 continue;
1122 expr = pet_nested_extract_expr(id);
1123 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1124 !is_assigned(expr, tree);
1126 pet_expr_free(expr);
1127 isl_id_free(id);
1129 if (!allowed)
1130 return 0;
1133 return 1;
1136 /* Construct a pet_scop for a for tree with static affine initialization
1137 * and constant increment within the context "pc".
1138 * The domain of "pc" has already been extended with an (at this point
1139 * unbounded) inner loop iterator corresponding to the current for loop.
1141 * The condition is allowed to contain nested accesses, provided
1142 * they are not being written to inside the body of the loop.
1143 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1144 * essentially treated as a while loop, with iteration domain
1145 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1147 * We extract a pet_scop for the body after intersecting the domain of "pc"
1149 * { [l,i] : i >= init and condition' }
1151 * or
1153 * { [l,i] : i <= init and condition' }
1155 * Where condition' is equal to condition if the latter is
1156 * a simple upper [lower] bound and a condition that is extended
1157 * to apply to all previous iterations otherwise.
1158 * Afterwards, the schedule of the pet_scop is extended with
1160 * { [l,i] -> [i] }
1162 * or
1164 * { [l,i] -> [-i] }
1166 * If the condition is non-affine, then we drop the condition from the
1167 * iteration domain and instead create a separate statement
1168 * for evaluating the condition. The body is then filtered to depend
1169 * on the result of the condition evaluating to true on all iterations
1170 * up to the current iteration, while the evaluation the condition itself
1171 * is filtered to depend on the result of the condition evaluating to true
1172 * on all previous iterations.
1173 * The context of the scop representing the body is dropped
1174 * because we don't know how many times the body will be executed,
1175 * if at all.
1177 * If the stride of the loop is not 1, then "i >= init" is replaced by
1179 * (exists a: i = init + stride * a and a >= 0)
1181 * If the loop iterator i is unsigned, then wrapping may occur.
1182 * We therefore use a virtual iterator instead that does not wrap.
1183 * However, the condition in the code applies
1184 * to the wrapped value, so we need to change condition(l,i)
1185 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1186 * to the original iterator by the wrapping of the virtual iterator.
1187 * Note that there may be no need to perform this final wrapping
1188 * if the loop condition (after wrapping) satisfies certain conditions.
1189 * However, the is_simple_bound condition is not enough since it doesn't
1190 * check if there even is an upper bound.
1192 * Wrapping on unsigned iterators can be avoided entirely if
1193 * loop condition is simple, the loop iterator is incremented
1194 * [decremented] by one and the last value before wrapping cannot
1195 * possibly satisfy the loop condition.
1197 * Valid outer iterators for a for loop are those for which the initial
1198 * value itself, the increment on each domain iteration and
1199 * the condition on both the initial value and
1200 * the result of incrementing the iterator for each iteration of the domain
1201 * can be evaluated.
1202 * If the loop condition is non-affine, then we only consider validity
1203 * of the initial value.
1205 * If the body contains any break, then we keep track of it in "skip"
1206 * (if the skip condition is affine) or it is handled in scop_add_break
1207 * (if the skip condition is not affine).
1208 * Note that the affine break condition needs to be considered with
1209 * respect to previous iterations in the virtual domain (if any).
1211 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1212 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1213 __isl_take isl_val *inc, __isl_take pet_context *pc,
1214 struct pet_state *state)
1216 isl_set *domain;
1217 isl_aff *sched;
1218 isl_set *cond = NULL;
1219 isl_set *skip = NULL;
1220 isl_id *id_test = NULL, *id_break_test;
1221 struct pet_scop *scop, *scop_cond = NULL;
1222 int pos;
1223 int is_one;
1224 int is_unsigned;
1225 int is_simple;
1226 int is_virtual;
1227 int is_non_affine;
1228 int has_affine_break;
1229 int has_var_break;
1230 isl_map *rev_wrap = NULL;
1231 isl_map *init_val_map;
1232 isl_pw_aff *pa;
1233 isl_set *valid_init;
1234 isl_set *valid_cond;
1235 isl_set *valid_cond_init;
1236 isl_set *valid_cond_next;
1237 isl_set *valid_inc;
1238 pet_expr *cond_expr;
1239 pet_context *pc_nested;
1241 pos = pet_context_dim(pc) - 1;
1243 domain = pet_context_get_domain(pc);
1244 cond_expr = pet_expr_copy(tree->u.l.cond);
1245 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1246 pc_nested = pet_context_copy(pc);
1247 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1248 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1249 pet_context_free(pc_nested);
1250 pet_expr_free(cond_expr);
1252 valid_inc = isl_pw_aff_domain(pa_inc);
1254 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1256 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1257 !is_nested_allowed(pa, tree->u.l.body);
1258 if (is_non_affine)
1259 pa = isl_pw_aff_free(pa);
1261 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1262 cond = isl_pw_aff_non_zero_set(pa);
1263 if (is_non_affine)
1264 cond = isl_set_universe(isl_set_get_space(domain));
1266 valid_cond = isl_set_coalesce(valid_cond);
1267 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1268 is_virtual = is_unsigned &&
1269 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1271 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1272 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1273 isl_dim_out, 0);
1274 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1275 isl_set_copy(valid_cond));
1276 if (is_one && !is_virtual) {
1277 isl_set *cond;
1279 isl_pw_aff_free(init_val);
1280 pa = pet_expr_extract_comparison(
1281 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1282 tree->u.l.iv, tree->u.l.init, pc);
1283 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1284 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1285 isl_set_dim(domain, isl_dim_set) - 1, 1);
1286 cond = isl_pw_aff_non_zero_set(pa);
1287 domain = isl_set_intersect(domain, cond);
1288 } else {
1289 isl_set *strided;
1291 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1292 strided = strided_domain(init_val, isl_val_copy(inc));
1293 domain = isl_set_intersect(domain, strided);
1296 if (is_virtual) {
1297 isl_multi_aff *wrap;
1298 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1299 pc = pet_context_preimage_domain(pc, wrap);
1300 rev_wrap = isl_map_from_multi_aff(wrap);
1301 rev_wrap = isl_map_reverse(rev_wrap);
1302 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1303 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1304 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1306 is_simple = is_simple_bound(cond, inc);
1307 if (!is_simple) {
1308 cond = isl_set_gist(cond, isl_set_copy(domain));
1309 is_simple = is_simple_bound(cond, inc);
1311 if (!is_simple)
1312 cond = valid_for_each_iteration(cond,
1313 isl_set_copy(domain), isl_val_copy(inc));
1314 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1315 domain = isl_set_intersect(domain, cond);
1316 sched = map_to_last(pc);
1317 if (isl_val_is_neg(inc))
1318 sched = isl_aff_neg(sched);
1320 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1321 isl_val_copy(inc));
1322 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1324 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1326 if (is_non_affine) {
1327 isl_space *space;
1328 isl_multi_pw_aff *test_index;
1329 space = isl_set_get_space(domain);
1330 test_index = pet_create_test_index(space, state->n_test++);
1331 scop_cond = scop_from_non_affine_condition(
1332 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1333 isl_multi_pw_aff_copy(test_index),
1334 pet_tree_get_loc(tree), pc);
1335 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1336 isl_dim_out);
1337 scop_cond = pet_scop_add_boolean_array(scop_cond,
1338 isl_set_copy(domain), test_index,
1339 state->int_size);
1340 scop_cond = pet_scop_prefix(scop_cond, 0);
1341 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1342 isl_aff_copy(sched));
1345 scop = scop_from_tree(tree->u.l.body, pc, state);
1346 has_affine_break = scop &&
1347 pet_scop_has_affine_skip(scop, pet_skip_later);
1348 if (has_affine_break)
1349 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1350 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1351 if (has_var_break)
1352 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1353 if (is_non_affine) {
1354 scop = pet_scop_reset_context(scop);
1355 scop = pet_scop_prefix(scop, 1);
1357 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
1358 scop = pet_scop_resolve_nested(scop);
1359 if (has_affine_break) {
1360 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1361 is_virtual, rev_wrap);
1362 scop = pet_scop_intersect_domain_prefix(scop,
1363 isl_set_copy(domain));
1365 isl_map_free(rev_wrap);
1366 if (has_var_break)
1367 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1368 isl_val_copy(inc));
1369 if (is_non_affine) {
1370 scop = scop_add_while(scop_cond, scop, id_test, domain,
1371 isl_val_copy(inc));
1372 isl_set_free(valid_inc);
1373 } else {
1374 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1375 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1376 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1377 scop = pet_scop_restrict_context(scop, valid_inc);
1378 isl_set_free(domain);
1381 isl_val_free(inc);
1383 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1384 scop = pet_scop_restrict_context(scop, valid_init);
1386 pet_context_free(pc);
1387 return scop;
1390 /* Construct a pet_scop for a for statement within the context of "pc".
1392 * We update the context to reflect the writes to the loop variable and
1393 * the writes inside the body.
1395 * Then we check if the initialization of the for loop
1396 * is a static affine value and the increment is a constant.
1397 * If so, we construct the pet_scop using scop_from_affine_for.
1398 * Otherwise, we treat the for loop as a while loop
1399 * in scop_from_non_affine_for.
1401 * Note that the initialization and the increment are extracted
1402 * in a context where the current loop iterator has been added
1403 * to the context. If these turn out not be affine, then we
1404 * have reconstruct the body context without an assignment
1405 * to this loop iterator, as this variable will then not be
1406 * treated as a dimension of the iteration domain, but as any
1407 * other variable.
1409 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1410 __isl_keep pet_context *init_pc, struct pet_state *state)
1412 isl_id *iv;
1413 isl_val *inc;
1414 isl_pw_aff *pa_inc, *init_val;
1415 pet_context *pc, *pc_init_val;
1417 if (!tree)
1418 return NULL;
1420 iv = pet_expr_access_get_id(tree->u.l.iv);
1421 pc = pet_context_copy(init_pc);
1422 pc = pet_context_add_inner_iterator(pc, iv);
1423 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1425 pc_init_val = pet_context_copy(pc);
1426 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1427 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1428 pet_context_free(pc_init_val);
1429 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1430 inc = pet_extract_cst(pa_inc);
1431 if (!pa_inc || !init_val || !inc)
1432 goto error;
1433 if (!isl_pw_aff_involves_nan(pa_inc) &&
1434 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1435 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1436 pc, state);
1438 isl_pw_aff_free(pa_inc);
1439 isl_pw_aff_free(init_val);
1440 isl_val_free(inc);
1441 pet_context_free(pc);
1443 pc = pet_context_copy(init_pc);
1444 pc = pet_context_add_infinite_loop(pc);
1445 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1446 return scop_from_non_affine_for(tree, init_pc, pc, state);
1447 error:
1448 isl_pw_aff_free(pa_inc);
1449 isl_pw_aff_free(init_val);
1450 isl_val_free(inc);
1451 pet_context_free(pc);
1452 return NULL;
1455 /* Check whether "expr" is an affine constraint within the context "pc".
1457 static int is_affine_condition(__isl_keep pet_expr *expr,
1458 __isl_keep pet_context *pc)
1460 isl_pw_aff *pa;
1461 int is_affine;
1463 pa = pet_expr_extract_affine_condition(expr, pc);
1464 if (!pa)
1465 return -1;
1466 is_affine = !isl_pw_aff_involves_nan(pa);
1467 isl_pw_aff_free(pa);
1469 return is_affine;
1472 /* Check if the given if statement is a conditional assignement
1473 * with a non-affine condition.
1475 * In particular we check if "stmt" is of the form
1477 * if (condition)
1478 * a = f(...);
1479 * else
1480 * a = g(...);
1482 * where the condition is non-affine and a is some array or scalar access.
1484 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1485 __isl_keep pet_context *pc)
1487 int equal;
1488 isl_ctx *ctx;
1489 pet_expr *expr1, *expr2;
1491 ctx = pet_tree_get_ctx(tree);
1492 if (!pet_options_get_detect_conditional_assignment(ctx))
1493 return 0;
1494 if (tree->type != pet_tree_if_else)
1495 return 0;
1496 if (tree->u.i.then_body->type != pet_tree_expr)
1497 return 0;
1498 if (tree->u.i.else_body->type != pet_tree_expr)
1499 return 0;
1500 expr1 = tree->u.i.then_body->u.e.expr;
1501 expr2 = tree->u.i.else_body->u.e.expr;
1502 if (pet_expr_get_type(expr1) != pet_expr_op)
1503 return 0;
1504 if (pet_expr_get_type(expr2) != pet_expr_op)
1505 return 0;
1506 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1507 return 0;
1508 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1509 return 0;
1510 expr1 = pet_expr_get_arg(expr1, 0);
1511 expr2 = pet_expr_get_arg(expr2, 0);
1512 equal = pet_expr_is_equal(expr1, expr2);
1513 pet_expr_free(expr1);
1514 pet_expr_free(expr2);
1515 if (equal < 0 || !equal)
1516 return 0;
1517 if (is_affine_condition(tree->u.i.cond, pc))
1518 return 0;
1520 return 1;
1523 /* Given that "tree" is of the form
1525 * if (condition)
1526 * a = f(...);
1527 * else
1528 * a = g(...);
1530 * where a is some array or scalar access, construct a pet_scop
1531 * corresponding to this conditional assignment within the context "pc".
1533 * The constructed pet_scop then corresponds to the expression
1535 * a = condition ? f(...) : g(...)
1537 * All access relations in f(...) are intersected with condition
1538 * while all access relation in g(...) are intersected with the complement.
1540 static struct pet_scop *scop_from_conditional_assignment(
1541 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1542 struct pet_state *state)
1544 int type_size;
1545 isl_pw_aff *pa;
1546 isl_set *cond, *comp;
1547 isl_multi_pw_aff *index;
1548 pet_expr *expr1, *expr2;
1549 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1550 pet_context *pc_nested;
1551 struct pet_scop *scop;
1553 pe_cond = pet_expr_copy(tree->u.i.cond);
1554 pe_cond = pet_context_evaluate_expr(pc, pe_cond);
1555 pc_nested = pet_context_copy(pc);
1556 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1557 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1558 pet_context_free(pc_nested);
1559 pet_expr_free(pe_cond);
1560 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1561 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1562 index = isl_multi_pw_aff_from_pw_aff(pa);
1564 expr1 = tree->u.i.then_body->u.e.expr;
1565 expr2 = tree->u.i.else_body->u.e.expr;
1567 pe_cond = pet_expr_from_index(index);
1569 pe_then = pet_expr_get_arg(expr1, 1);
1570 pe_then = pet_context_evaluate_expr(pc, pe_then);
1571 pe_then = pet_expr_restrict(pe_then, cond);
1572 pe_else = pet_expr_get_arg(expr2, 1);
1573 pe_else = pet_context_evaluate_expr(pc, pe_else);
1574 pe_else = pet_expr_restrict(pe_else, comp);
1575 pe_write = pet_expr_get_arg(expr1, 0);
1576 pe_write = pet_context_evaluate_expr(pc, pe_write);
1578 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1579 type_size = pet_expr_get_type_size(pe_write);
1580 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1582 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
1583 pet_tree_get_loc(tree), pc);
1585 pet_context_free(pc);
1587 return scop;
1590 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1592 * We create a separate statement that writes the result
1593 * of the non-affine condition to a virtual scalar.
1594 * A constraint requiring the value of this virtual scalar to be one
1595 * is added to the iteration domains of the then branch.
1596 * Similarly, a constraint requiring the value of this virtual scalar
1597 * to be zero is added to the iteration domains of the else branch, if any.
1598 * We adjust the schedules to ensure that the virtual scalar is written
1599 * before it is read.
1601 * If there are any breaks or continues in the then and/or else
1602 * branches, then we may have to compute a new skip condition.
1603 * This is handled using a pet_skip_info object.
1604 * On initialization, the object checks if skip conditions need
1605 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1606 * adds them in pet_skip_info_if_add.
1608 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1609 __isl_take pet_context *pc, struct pet_state *state)
1611 int has_else;
1612 isl_space *space;
1613 isl_set *domain;
1614 isl_multi_pw_aff *test_index;
1615 struct pet_skip_info skip;
1616 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1618 has_else = tree->type == pet_tree_if_else;
1620 space = pet_context_get_space(pc);
1621 test_index = pet_create_test_index(space, state->n_test++);
1622 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1623 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1624 pet_tree_get_loc(tree), pc);
1625 domain = pet_context_get_domain(pc);
1626 scop = pet_scop_add_boolean_array(scop, domain,
1627 isl_multi_pw_aff_copy(test_index), state->int_size);
1629 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1630 if (has_else)
1631 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1633 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1634 has_else, 0);
1635 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1637 scop = pet_scop_prefix(scop, 0);
1638 scop_then = pet_scop_prefix(scop_then, 1);
1639 scop_then = pet_scop_filter(scop_then,
1640 isl_multi_pw_aff_copy(test_index), 1);
1641 if (has_else) {
1642 scop_else = pet_scop_prefix(scop_else, 1);
1643 scop_else = pet_scop_filter(scop_else, test_index, 0);
1644 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1645 } else
1646 isl_multi_pw_aff_free(test_index);
1648 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1650 scop = pet_skip_info_if_add(&skip, scop, 2);
1652 pet_context_free(pc);
1653 return scop;
1656 /* Construct a pet_scop for an affine if statement within the context "pc".
1658 * The condition is added to the iteration domains of the then branch,
1659 * while the opposite of the condition in added to the iteration domains
1660 * of the else branch, if any.
1662 * If there are any breaks or continues in the then and/or else
1663 * branches, then we may have to compute a new skip condition.
1664 * This is handled using a pet_skip_info_if object.
1665 * On initialization, the object checks if skip conditions need
1666 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1667 * adds them in pet_skip_info_if_add.
1669 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1670 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1671 struct pet_state *state)
1673 int has_else;
1674 isl_ctx *ctx;
1675 isl_set *set, *complement;
1676 isl_set *valid;
1677 struct pet_skip_info skip;
1678 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1679 pet_context *pc_body;
1681 ctx = pet_tree_get_ctx(tree);
1683 has_else = tree->type == pet_tree_if_else;
1685 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1686 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1688 pc_body = pet_context_copy(pc);
1689 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
1690 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
1691 pet_context_free(pc_body);
1692 if (has_else) {
1693 pc_body = pet_context_copy(pc);
1694 complement = isl_set_copy(valid);
1695 complement = isl_set_subtract(valid, isl_set_copy(set));
1696 pc_body = pet_context_intersect_domain(pc_body,
1697 isl_set_copy(complement));
1698 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
1699 pet_context_free(pc_body);
1702 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1703 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
1704 isl_pw_aff_free(cond);
1706 scop = pet_scop_restrict(scop_then, set);
1708 if (has_else) {
1709 scop_else = pet_scop_restrict(scop_else, complement);
1710 scop = pet_scop_add_par(ctx, scop, scop_else);
1712 scop = pet_scop_resolve_nested(scop);
1713 scop = pet_scop_restrict_context(scop, valid);
1715 if (pet_skip_info_has_skip(&skip))
1716 scop = pet_scop_prefix(scop, 0);
1717 scop = pet_skip_info_if_add(&skip, scop, 1);
1719 pet_context_free(pc);
1720 return scop;
1723 /* Construct a pet_scop for an if statement within the context "pc".
1725 * If the condition fits the pattern of a conditional assignment,
1726 * then it is handled by scop_from_conditional_assignment.
1728 * Otherwise, we check if the condition is affine.
1729 * If so, we construct the scop in scop_from_affine_if.
1730 * Otherwise, we construct the scop in scop_from_non_affine_if.
1732 * We allow the condition to be dynamic, i.e., to refer to
1733 * scalars or array elements that may be written to outside
1734 * of the given if statement. These nested accesses are then represented
1735 * as output dimensions in the wrapping iteration domain.
1736 * If it is also written _inside_ the then or else branch, then
1737 * we treat the condition as non-affine.
1738 * As explained in extract_non_affine_if, this will introduce
1739 * an extra statement.
1740 * For aesthetic reasons, we want this statement to have a statement
1741 * number that is lower than those of the then and else branches.
1742 * In order to evaluate if we will need such a statement, however, we
1743 * first construct scops for the then and else branches.
1744 * We therefore reserve a statement number if we might have to
1745 * introduce such an extra statement.
1747 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1748 __isl_keep pet_context *pc, struct pet_state *state)
1750 int has_else;
1751 isl_pw_aff *cond;
1752 pet_expr *cond_expr;
1753 pet_context *pc_nested;
1755 if (!tree)
1756 return NULL;
1758 has_else = tree->type == pet_tree_if_else;
1760 pc = pet_context_copy(pc);
1761 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1762 if (has_else)
1763 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1765 if (is_conditional_assignment(tree, pc))
1766 return scop_from_conditional_assignment(tree, pc, state);
1768 cond_expr = pet_expr_copy(tree->u.i.cond);
1769 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1770 pc_nested = pet_context_copy(pc);
1771 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1772 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1773 pet_context_free(pc_nested);
1774 pet_expr_free(cond_expr);
1776 if (!cond) {
1777 pet_context_free(pc);
1778 return NULL;
1781 if (isl_pw_aff_involves_nan(cond)) {
1782 isl_pw_aff_free(cond);
1783 return scop_from_non_affine_if(tree, pc, state);
1786 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1787 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1788 isl_pw_aff_free(cond);
1789 return scop_from_non_affine_if(tree, pc, state);
1792 return scop_from_affine_if(tree, cond, pc, state);
1795 /* Return a one-dimensional multi piecewise affine expression that is equal
1796 * to the constant 1 and is defined over the given domain.
1798 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
1800 isl_local_space *ls;
1801 isl_aff *aff;
1803 ls = isl_local_space_from_space(space);
1804 aff = isl_aff_zero_on_domain(ls);
1805 aff = isl_aff_set_constant_si(aff, 1);
1807 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1810 /* Construct a pet_scop for a continue statement with the given domain space.
1812 * We simply create an empty scop with a universal pet_skip_now
1813 * skip condition. This skip condition will then be taken into
1814 * account by the enclosing loop construct, possibly after
1815 * being incorporated into outer skip conditions.
1817 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
1818 __isl_take isl_space *space)
1820 struct pet_scop *scop;
1822 scop = pet_scop_empty(isl_space_copy(space));
1824 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
1826 return scop;
1829 /* Construct a pet_scop for a break statement with the given domain space.
1831 * We simply create an empty scop with both a universal pet_skip_now
1832 * skip condition and a universal pet_skip_later skip condition.
1833 * These skip conditions will then be taken into
1834 * account by the enclosing loop construct, possibly after
1835 * being incorporated into outer skip conditions.
1837 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
1838 __isl_take isl_space *space)
1840 struct pet_scop *scop;
1841 isl_multi_pw_aff *skip;
1843 scop = pet_scop_empty(isl_space_copy(space));
1845 skip = one_mpa(space);
1846 scop = pet_scop_set_skip(scop, pet_skip_now,
1847 isl_multi_pw_aff_copy(skip));
1848 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1850 return scop;
1853 /* Extract a clone of the kill statement in "scop".
1854 * The domain of the clone is given by "domain".
1855 * "scop" is expected to have been created from a DeclStmt
1856 * and should have the kill as its first statement.
1858 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
1859 struct pet_scop *scop, struct pet_state *state)
1861 pet_expr *kill;
1862 struct pet_stmt *stmt;
1863 isl_multi_pw_aff *index;
1864 isl_map *access;
1865 pet_expr *expr, *arg;
1866 pet_tree *tree;
1868 if (!domain || !scop)
1869 return NULL;
1870 if (scop->n_stmt < 1)
1871 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1872 "expecting at least one statement", return NULL);
1873 stmt = scop->stmts[0];
1874 if (!pet_stmt_is_kill(stmt))
1875 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1876 "expecting kill statement", return NULL);
1878 expr = pet_tree_expr_get_expr(stmt->body);
1879 arg = pet_expr_get_arg(expr, 0);
1880 pet_expr_free(expr);
1881 index = pet_expr_access_get_index(arg);
1882 access = pet_expr_access_get_access(arg);
1883 pet_expr_free(arg);
1884 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
1885 access = isl_map_reset_tuple_id(access, isl_dim_in);
1886 kill = pet_expr_kill_from_access_and_index(access, index);
1887 tree = pet_tree_new_expr(kill);
1888 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
1889 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
1890 state->n_stmt++, tree);
1891 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
1894 /* Does "tree" represent an assignment to a variable?
1896 * The assignment may be one of
1897 * - a declaration with initialization
1898 * - an expression with a top-level assignment operator
1900 static int is_assignment(__isl_keep pet_tree *tree)
1902 if (!tree)
1903 return 0;
1904 if (tree->type == pet_tree_decl_init)
1905 return 1;
1906 return pet_tree_is_assign(tree);
1909 /* Update "pc" by taking into account the assignment performed by "tree",
1910 * where "tree" satisfies is_assignment.
1912 * In particular, if the lhs of the assignment is a scalar variable and
1913 * if the rhs is an affine expression, then keep track of this value in "pc"
1914 * so that we can plug it in when we later come across the same variable.
1916 * Any previously assigned value to the variable has already been removed
1917 * by scop_handle_writes.
1919 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
1920 __isl_keep pet_tree *tree)
1922 pet_expr *var, *val;
1923 isl_id *id;
1924 isl_pw_aff *pa;
1926 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
1927 var = pet_tree_decl_get_var(tree);
1928 val = pet_tree_decl_get_init(tree);
1929 } else {
1930 pet_expr *expr;
1931 expr = pet_tree_expr_get_expr(tree);
1932 var = pet_expr_get_arg(expr, 0);
1933 val = pet_expr_get_arg(expr, 1);
1934 pet_expr_free(expr);
1937 if (!pet_expr_is_scalar_access(var)) {
1938 pet_expr_free(var);
1939 pet_expr_free(val);
1940 return pc;
1943 pa = pet_expr_extract_affine(val, pc);
1944 if (!pa)
1945 pc = pet_context_free(pc);
1947 if (!isl_pw_aff_involves_nan(pa)) {
1948 id = pet_expr_access_get_id(var);
1949 pc = pet_context_set_value(pc, id, pa);
1950 } else {
1951 isl_pw_aff_free(pa);
1953 pet_expr_free(var);
1954 pet_expr_free(val);
1956 return pc;
1959 /* Mark all arrays in "scop" as being exposed.
1961 static struct pet_scop *mark_exposed(struct pet_scop *scop)
1963 int i;
1965 if (!scop)
1966 return NULL;
1967 for (i = 0; i < scop->n_array; ++i)
1968 scop->arrays[i]->exposed = 1;
1969 return scop;
1972 /* Given that "scop" has an affine skip condition of type pet_skip_now,
1973 * apply this skip condition to the domain of "pc".
1974 * That is, remove the elements satisfying the skip condition from
1975 * the domain of "pc".
1977 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
1978 struct pet_scop *scop)
1980 isl_set *domain, *skip;
1982 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
1983 domain = pet_context_get_domain(pc);
1984 domain = isl_set_subtract(domain, skip);
1985 pc = pet_context_intersect_domain(pc, domain);
1987 return pc;
1990 /* Try and construct a pet_scop corresponding to (part of)
1991 * a sequence of statements within the context "pc".
1993 * After extracting a statement, we update "pc"
1994 * based on the top-level assignments in the statement
1995 * so that we can exploit them in subsequent statements in the same block.
1997 * If there are any breaks or continues in the individual statements,
1998 * then we may have to compute a new skip condition.
1999 * This is handled using a pet_skip_info object.
2000 * On initialization, the object checks if skip conditions need
2001 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2002 * adds them in pet_skip_info_seq_add.
2004 * If "block" is set, then we need to insert kill statements at
2005 * the end of the block for any array that has been declared by
2006 * one of the statements in the sequence. Each of these declarations
2007 * results in the construction of a kill statement at the place
2008 * of the declaration, so we simply collect duplicates of
2009 * those kill statements and append these duplicates to the constructed scop.
2011 * If "block" is not set, then any array declared by one of the statements
2012 * in the sequence is marked as being exposed.
2014 * If autodetect is set, then we allow the extraction of only a subrange
2015 * of the sequence of statements. However, if there is at least one statement
2016 * for which we could not construct a scop and the final range contains
2017 * either no statements or at least one kill, then we discard the entire
2018 * range.
2020 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2021 __isl_keep pet_context *pc, struct pet_state *state)
2023 int i;
2024 isl_ctx *ctx;
2025 isl_space *space;
2026 isl_set *domain;
2027 struct pet_scop *scop, *kills;
2029 ctx = pet_tree_get_ctx(tree);
2031 space = pet_context_get_space(pc);
2032 domain = pet_context_get_domain(pc);
2033 pc = pet_context_copy(pc);
2034 scop = pet_scop_empty(isl_space_copy(space));
2035 kills = pet_scop_empty(space);
2036 for (i = 0; i < tree->u.b.n; ++i) {
2037 struct pet_scop *scop_i;
2039 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2040 pc = apply_affine_continue(pc, scop);
2041 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2042 pc = scop_handle_writes(scop_i, pc);
2043 if (is_assignment(tree->u.b.child[i]))
2044 pc = handle_assignment(pc, tree->u.b.child[i]);
2045 struct pet_skip_info skip;
2046 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2047 pet_skip_info_seq_extract(&skip, pc, state);
2048 if (pet_skip_info_has_skip(&skip))
2049 scop_i = pet_scop_prefix(scop_i, 0);
2050 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
2051 if (tree->u.b.block) {
2052 struct pet_scop *kill;
2053 kill = extract_kill(domain, scop_i, state);
2054 kills = pet_scop_add_par(ctx, kills, kill);
2055 } else
2056 scop_i = mark_exposed(scop_i);
2058 scop_i = pet_scop_prefix(scop_i, i);
2059 scop = pet_scop_add_seq(ctx, scop, scop_i);
2061 scop = pet_skip_info_seq_add(&skip, scop, i);
2063 if (!scop)
2064 break;
2066 isl_set_free(domain);
2068 kills = pet_scop_prefix(kills, tree->u.b.n);
2069 scop = pet_scop_add_seq(ctx, scop, kills);
2071 pet_context_free(pc);
2073 return scop;
2076 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2077 * within the context "pc" by calling the appropriate function
2078 * based on the type of "tree".
2080 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2081 __isl_keep pet_context *pc, struct pet_state *state)
2083 if (!tree)
2084 return NULL;
2086 switch (tree->type) {
2087 case pet_tree_error:
2088 return NULL;
2089 case pet_tree_block:
2090 return scop_from_block(tree, pc, state);
2091 case pet_tree_break:
2092 return scop_from_break(tree, pet_context_get_space(pc));
2093 case pet_tree_continue:
2094 return scop_from_continue(tree, pet_context_get_space(pc));
2095 case pet_tree_decl:
2096 case pet_tree_decl_init:
2097 return scop_from_decl(tree, pc, state);
2098 case pet_tree_expr:
2099 return scop_from_unevaluated_tree(pet_tree_copy(tree),
2100 state->n_stmt++, pc);
2101 case pet_tree_if:
2102 case pet_tree_if_else:
2103 return scop_from_if(tree, pc, state);
2104 case pet_tree_for:
2105 return scop_from_for(tree, pc, state);
2106 case pet_tree_while:
2107 return scop_from_while(tree, pc, state);
2108 case pet_tree_infinite_loop:
2109 return scop_from_infinite_for(tree, pc, state);
2112 isl_die(tree->ctx, isl_error_internal, "unhandled type",
2113 return NULL);
2116 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2117 * "int_size" is the number of bytes need to represent an integer.
2118 * "extract_array" is a callback that we can use to create a pet_array
2119 * that corresponds to the variable accessed by an expression.
2121 * Initialize the global state, construct a context and then
2122 * construct the pet_scop by recursively visiting the tree.
2124 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2125 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2126 __isl_keep pet_context *pc, void *user), void *user,
2127 __isl_keep pet_context *pc)
2129 struct pet_scop *scop;
2130 struct pet_state state = { 0 };
2132 if (!tree)
2133 return NULL;
2135 state.ctx = pet_tree_get_ctx(tree);
2136 state.int_size = int_size;
2137 state.extract_array = extract_array;
2138 state.user = user;
2140 scop = scop_from_tree(tree, pc, &state);
2141 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2143 pet_tree_free(tree);
2145 if (scop)
2146 scop->context = isl_set_params(scop->context);
2148 return scop;