tree2scop.c: move up apply_affine_continue
[pet.git] / tree2scop.c
blobe1965fcc468cdcb368847f81241db4ce85aac38a
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 /* Given that "scop" has an affine skip condition of type pet_skip_now,
597 * apply this skip condition to the domain of "pc".
598 * That is, remove the elements satisfying the skip condition from
599 * the domain of "pc".
601 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
602 struct pet_scop *scop)
604 isl_set *domain, *skip;
606 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
607 domain = pet_context_get_domain(pc);
608 domain = isl_set_subtract(domain, skip);
609 pc = pet_context_intersect_domain(pc, domain);
611 return pc;
614 /* Add a scop for evaluating the loop increment "inc" add the end
615 * of a loop body "scop" within the context "pc".
617 * The skip conditions resulting from continue statements inside
618 * the body do not apply to "inc", but those resulting from break
619 * statements do need to get applied.
621 static struct pet_scop *scop_add_inc(struct pet_scop *scop,
622 __isl_take pet_expr *inc, __isl_take pet_loc *loc,
623 __isl_keep pet_context *pc, struct pet_state *state)
625 struct pet_scop *scop_inc;
627 scop_inc = scop_from_expr(inc, state->n_stmt++, loc, pc);
628 scop_inc = pet_scop_prefix(scop_inc, 2);
629 if (pet_scop_has_skip(scop, pet_skip_later)) {
630 isl_multi_pw_aff *skip;
631 skip = pet_scop_get_skip(scop, pet_skip_later);
632 scop = pet_scop_set_skip(scop, pet_skip_now, skip);
633 } else
634 pet_scop_reset_skip(scop, pet_skip_now);
635 scop = pet_scop_add_seq(state->ctx, scop, scop_inc);
637 return scop;
640 /* Construct a generic while scop, with iteration domain
641 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
642 * The domain of "pc" has already been extended with this infinite loop
644 * { [t] : t >= 0 }
646 * The scop consists of two parts,
647 * one for evaluating the condition "cond" and one for the body.
648 * If "expr_inc" is not NULL, then a scop for evaluating this expression
649 * is added at the end of the body,
650 * after replacing any skip conditions resulting from continue statements
651 * by the skip conditions resulting from break statements (if any).
653 * The schedule is adjusted to reflect that the condition is evaluated
654 * before the body is executed and the body is filtered to depend
655 * on the result of the condition evaluating to true on all iterations
656 * up to the current iteration, while the evaluation of the condition itself
657 * is filtered to depend on the result of the condition evaluating to true
658 * on all previous iterations.
659 * The context of the scop representing the body is dropped
660 * because we don't know how many times the body will be executed,
661 * if at all.
663 * If the body contains any break, then it is taken into
664 * account in apply_affine_break (if the skip condition is affine)
665 * or in scop_add_break (if the skip condition is not affine).
667 * Note that in case of an affine skip condition,
668 * since we are dealing with a loop without loop iterator,
669 * the skip condition cannot refer to the current loop iterator and
670 * so effectively, the effect on the iteration domain is of the form
672 * { [outer,0]; [outer,t] : t >= 1 and not skip }
674 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
675 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
676 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
677 struct pet_state *state)
679 isl_ctx *ctx;
680 isl_id *id_test, *id_break_test;
681 isl_space *space;
682 isl_multi_pw_aff *test_index;
683 isl_set *domain;
684 isl_set *skip;
685 isl_aff *sched;
686 struct pet_scop *scop, *scop_body;
687 int has_affine_break;
688 int has_var_break;
690 ctx = state->ctx;
691 space = pet_context_get_space(pc);
692 test_index = pet_create_test_index(space, state->n_test++);
693 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
694 isl_multi_pw_aff_copy(test_index),
695 pet_loc_copy(loc), pc);
696 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
697 domain = pet_context_get_domain(pc);
698 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
699 test_index, state->int_size);
701 sched = map_to_last(pc);
703 scop_body = scop_from_tree(tree_body, pc, state);
705 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
706 if (has_affine_break)
707 skip = pet_scop_get_affine_skip_domain(scop_body,
708 pet_skip_later);
709 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
710 if (has_var_break)
711 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
713 scop = pet_scop_prefix(scop, 0);
714 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(sched));
715 scop_body = pet_scop_reset_context(scop_body);
716 scop_body = pet_scop_prefix(scop_body, 1);
717 if (expr_inc) {
718 scop_body = scop_add_inc(scop_body, expr_inc, loc, pc, state);
719 } else
720 pet_loc_free(loc);
721 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain), sched);
723 if (has_affine_break) {
724 domain = apply_affine_break(domain, skip, 1, 0, NULL);
725 scop = pet_scop_intersect_domain_prefix(scop,
726 isl_set_copy(domain));
727 scop_body = pet_scop_intersect_domain_prefix(scop_body,
728 isl_set_copy(domain));
730 if (has_var_break) {
731 scop = scop_add_break(scop, isl_id_copy(id_break_test),
732 isl_set_copy(domain), isl_val_one(ctx));
733 scop_body = scop_add_break(scop_body, id_break_test,
734 isl_set_copy(domain), isl_val_one(ctx));
736 scop = scop_add_while(scop, scop_body, id_test, domain,
737 isl_val_one(ctx));
739 pet_context_free(pc);
740 return scop;
743 /* Check if the while loop is of the form
745 * while (affine expression)
746 * body
748 * If so, call scop_from_affine_while to construct a scop.
750 * Otherwise, pass control to scop_from_non_affine_while.
752 * "pc" is the context in which the affine expressions in the scop are created.
753 * The domain of "pc" is extended with an infinite loop
755 * { [t] : t >= 0 }
757 * before passing control to scop_from_affine_while or
758 * scop_from_non_affine_while.
760 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
761 __isl_keep pet_context *pc, struct pet_state *state)
763 pet_expr *cond_expr;
764 isl_pw_aff *pa;
766 if (!tree)
767 return NULL;
769 pc = pet_context_copy(pc);
770 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
772 cond_expr = pet_expr_copy(tree->u.l.cond);
773 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
774 pa = pet_expr_extract_affine_condition(cond_expr, pc);
775 pet_expr_free(cond_expr);
777 pc = pet_context_add_infinite_loop(pc);
779 if (!pa)
780 goto error;
782 if (!isl_pw_aff_involves_nan(pa))
783 return scop_from_affine_while(tree, pa, pc, state);
784 isl_pw_aff_free(pa);
785 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
786 pet_tree_get_loc(tree), tree->u.l.body, NULL,
787 pc, state);
788 error:
789 pet_context_free(pc);
790 return NULL;
793 /* Check whether "cond" expresses a simple loop bound
794 * on the final set dimension.
795 * In particular, if "up" is set then "cond" should contain only
796 * upper bounds on the final set dimension.
797 * Otherwise, it should contain only lower bounds.
799 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
801 int pos;
803 pos = isl_set_dim(cond, isl_dim_set) - 1;
804 if (isl_val_is_pos(inc))
805 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
806 else
807 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
810 /* Extend a condition on a given iteration of a loop to one that
811 * imposes the same condition on all previous iterations.
812 * "domain" expresses the lower [upper] bound on the iterations
813 * when inc is positive [negative] in its final dimension.
815 * In particular, we construct the condition (when inc is positive)
817 * forall i' : (domain(i') and i' <= i) => cond(i')
819 * (where "<=" applies to the final dimension)
820 * which is equivalent to
822 * not exists i' : domain(i') and i' <= i and not cond(i')
824 * We construct this set by subtracting the satisfying cond from domain,
825 * applying a map
827 * { [i'] -> [i] : i' <= i }
829 * and then subtracting the result from domain again.
831 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
832 __isl_take isl_set *domain, __isl_take isl_val *inc)
834 isl_space *space;
835 isl_map *previous_to_this;
836 int i, dim;
838 dim = isl_set_dim(cond, isl_dim_set);
839 space = isl_space_map_from_set(isl_set_get_space(cond));
840 previous_to_this = isl_map_universe(space);
841 for (i = 0; i + 1 < dim; ++i)
842 previous_to_this = isl_map_equate(previous_to_this,
843 isl_dim_in, i, isl_dim_out, i);
844 if (isl_val_is_pos(inc))
845 previous_to_this = isl_map_order_le(previous_to_this,
846 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
847 else
848 previous_to_this = isl_map_order_ge(previous_to_this,
849 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
851 cond = isl_set_subtract(isl_set_copy(domain), cond);
852 cond = isl_set_apply(cond, previous_to_this);
853 cond = isl_set_subtract(domain, cond);
855 isl_val_free(inc);
857 return cond;
860 /* Given an initial value of the form
862 * { [outer,i] -> init(outer) }
864 * construct a domain of the form
866 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
868 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
869 __isl_take isl_val *inc)
871 int dim;
872 isl_aff *aff;
873 isl_space *space;
874 isl_local_space *ls;
875 isl_set *set;
877 dim = isl_pw_aff_dim(init, isl_dim_in);
879 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
880 space = isl_pw_aff_get_domain_space(init);
881 ls = isl_local_space_from_space(space);
882 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
883 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
884 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
886 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
887 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
889 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
890 set = isl_set_project_out(set, isl_dim_set, dim, 1);
892 return set;
895 /* Assuming "cond" represents a bound on a loop where the loop
896 * iterator "iv" is incremented (or decremented) by one, check if wrapping
897 * is possible.
899 * Under the given assumptions, wrapping is only possible if "cond" allows
900 * for the last value before wrapping, i.e., 2^width - 1 in case of an
901 * increasing iterator and 0 in case of a decreasing iterator.
903 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
904 __isl_keep isl_val *inc)
906 int cw;
907 isl_ctx *ctx;
908 isl_val *limit;
909 isl_set *test;
911 test = isl_set_copy(cond);
913 ctx = isl_set_get_ctx(test);
914 if (isl_val_is_neg(inc))
915 limit = isl_val_zero(ctx);
916 else {
917 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
918 limit = isl_val_2exp(limit);
919 limit = isl_val_sub_ui(limit, 1);
922 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
923 cw = !isl_set_is_empty(test);
924 isl_set_free(test);
926 return cw;
929 /* Given a space
931 * { [outer, v] },
933 * construct the following affine expression on this space
935 * { [outer, v] -> [outer, v mod 2^width] }
937 * where width is the number of bits used to represent the values
938 * of the unsigned variable "iv".
940 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
941 __isl_keep pet_expr *iv)
943 int dim;
944 isl_ctx *ctx;
945 isl_val *mod;
946 isl_aff *aff;
947 isl_multi_aff *ma;
949 dim = isl_space_dim(space, isl_dim_set);
951 ctx = isl_space_get_ctx(space);
952 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
953 mod = isl_val_2exp(mod);
955 space = isl_space_map_from_set(space);
956 ma = isl_multi_aff_identity(space);
958 aff = isl_multi_aff_get_aff(ma, dim - 1);
959 aff = isl_aff_mod_val(aff, mod);
960 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
962 return ma;
965 /* Given two sets in the space
967 * { [l,i] },
969 * where l represents the outer loop iterators, compute the set
970 * of values of l that ensure that "set1" is a subset of "set2".
972 * set1 is a subset of set2 if
974 * forall i: set1(l,i) => set2(l,i)
976 * or
978 * not exists i: set1(l,i) and not set2(l,i)
980 * i.e.,
982 * not exists i: (set1 \ set2)(l,i)
984 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
985 __isl_take isl_set *set2)
987 int pos;
989 pos = isl_set_dim(set1, isl_dim_set) - 1;
990 set1 = isl_set_subtract(set1, set2);
991 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
992 return isl_set_complement(set1);
995 /* Compute the set of outer iterator values for which "cond" holds
996 * on the next iteration of the inner loop for each element of "dom".
998 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
999 * to the outer loop iterators), plug that into "cond"
1000 * and then compute the set of outer iterators for which "dom" is a subset
1001 * of the result.
1003 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
1004 __isl_take isl_set *dom, __isl_take isl_val *inc)
1006 int pos;
1007 isl_space *space;
1008 isl_aff *aff;
1009 isl_multi_aff *ma;
1011 pos = isl_set_dim(dom, isl_dim_set) - 1;
1012 space = isl_set_get_space(dom);
1013 space = isl_space_map_from_set(space);
1014 ma = isl_multi_aff_identity(space);
1015 aff = isl_multi_aff_get_aff(ma, pos);
1016 aff = isl_aff_add_constant_val(aff, inc);
1017 ma = isl_multi_aff_set_aff(ma, pos, aff);
1018 cond = isl_set_preimage_multi_aff(cond, ma);
1020 return enforce_subset(dom, cond);
1023 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1024 * In particular, "pc_init" represents the context of the loop,
1025 * whereas "pc" represents the context of the body of the loop and
1026 * has already had its domain extended with an infinite loop
1028 * { [t] : t >= 0 }
1030 * The for loop has the form
1032 * for (iv = init; cond; iv += inc)
1033 * body;
1035 * and is treated as
1037 * iv = init;
1038 * while (cond) {
1039 * body;
1040 * iv += inc;
1043 * except that the skips resulting from any continue statements
1044 * in body do not apply to the increment, but are replaced by the skips
1045 * resulting from break statements.
1047 * If the loop iterator is declared in the for loop, then it is killed before
1048 * and after the loop.
1050 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1051 __isl_keep pet_context *init_pc, __isl_take pet_context *pc,
1052 struct pet_state *state)
1054 int declared;
1055 isl_id *iv;
1056 pet_expr *expr_iv, *init, *inc;
1057 struct pet_scop *scop_init, *scop;
1058 int type_size;
1059 struct pet_array *array;
1060 struct pet_scop *scop_kill;
1062 iv = pet_expr_access_get_id(tree->u.l.iv);
1063 pc = pet_context_clear_value(pc, iv);
1065 declared = tree->u.l.declared;
1067 expr_iv = pet_expr_copy(tree->u.l.iv);
1068 type_size = pet_expr_get_type_size(expr_iv);
1069 init = pet_expr_copy(tree->u.l.init);
1070 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1071 scop_init = scop_from_expr(init, state->n_stmt++,
1072 pet_tree_get_loc(tree), init_pc);
1073 scop_init = pet_scop_prefix(scop_init, declared);
1075 expr_iv = pet_expr_copy(tree->u.l.iv);
1076 type_size = pet_expr_get_type_size(expr_iv);
1077 inc = pet_expr_copy(tree->u.l.inc);
1078 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1080 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1081 pet_tree_get_loc(tree), tree->u.l.body, inc,
1082 pet_context_copy(pc), state);
1084 scop = pet_scop_prefix(scop, declared + 1);
1085 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1087 pet_context_free(pc);
1089 if (!declared)
1090 return scop;
1092 array = extract_array(tree->u.l.iv, init_pc, state);
1093 if (array)
1094 array->declared = 1;
1095 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1096 scop_kill = pet_scop_prefix(scop_kill, 0);
1097 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1098 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1099 scop_kill = pet_scop_add_array(scop_kill, array);
1100 scop_kill = pet_scop_prefix(scop_kill, 3);
1101 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1103 return scop;
1106 /* Given an access expression "expr", is the variable accessed by
1107 * "expr" assigned anywhere inside "tree"?
1109 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1111 int assigned = 0;
1112 isl_id *id;
1114 id = pet_expr_access_get_id(expr);
1115 assigned = pet_tree_writes(tree, id);
1116 isl_id_free(id);
1118 return assigned;
1121 /* Are all nested access parameters in "pa" allowed given "tree".
1122 * In particular, is none of them written by anywhere inside "tree".
1124 * If "tree" has any continue nodes in the current loop level,
1125 * then no nested access parameters are allowed.
1126 * In particular, if there is any nested access in a guard
1127 * for a piece of code containing a "continue", then we want to introduce
1128 * a separate statement for evaluating this guard so that we can express
1129 * that the result is false for all previous iterations.
1131 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1132 __isl_keep pet_tree *tree)
1134 int i, nparam;
1136 if (!tree)
1137 return -1;
1139 if (!pet_nested_any_in_pw_aff(pa))
1140 return 1;
1142 if (pet_tree_has_continue(tree))
1143 return 0;
1145 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1146 for (i = 0; i < nparam; ++i) {
1147 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1148 pet_expr *expr;
1149 int allowed;
1151 if (!pet_nested_in_id(id)) {
1152 isl_id_free(id);
1153 continue;
1156 expr = pet_nested_extract_expr(id);
1157 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1158 !is_assigned(expr, tree);
1160 pet_expr_free(expr);
1161 isl_id_free(id);
1163 if (!allowed)
1164 return 0;
1167 return 1;
1170 /* Internal data structure for collect_local.
1171 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1172 * "local" collects the results.
1174 struct pet_tree_collect_local_data {
1175 pet_context *pc;
1176 struct pet_state *state;
1177 isl_union_set *local;
1180 /* Add the variable accessed by "var" to data->local.
1181 * We extract a representation of the variable from
1182 * the pet_array constructed using extract_array
1183 * to ensure consistency with the rest of the scop.
1185 static int add_local(struct pet_tree_collect_local_data *data,
1186 __isl_keep pet_expr *var)
1188 struct pet_array *array;
1189 isl_set *universe;
1191 array = extract_array(var, data->pc, data->state);
1192 if (!array)
1193 return -1;
1195 universe = isl_set_universe(isl_set_get_space(array->extent));
1196 data->local = isl_union_set_add_set(data->local, universe);
1197 pet_array_free(array);
1199 return 0;
1202 /* If the node "tree" declares a variable, then add it to
1203 * data->local.
1205 static int extract_local_var(__isl_keep pet_tree *tree, void *user)
1207 enum pet_tree_type type;
1208 struct pet_tree_collect_local_data *data = user;
1210 type = pet_tree_get_type(tree);
1211 if (type == pet_tree_decl || type == pet_tree_decl_init)
1212 return add_local(data, tree->u.d.var);
1214 return 0;
1217 /* If the node "tree" is a for loop that declares its induction variable,
1218 * then add it this induction variable to data->local.
1220 static int extract_local_iterator(__isl_keep pet_tree *tree, void *user)
1222 struct pet_tree_collect_local_data *data = user;
1224 if (pet_tree_get_type(tree) == pet_tree_for && tree->u.l.declared)
1225 return add_local(data, tree->u.l.iv);
1227 return 0;
1230 /* Collect and return all local variables of the for loop represented
1231 * by "tree", with "scop" the corresponding pet_scop.
1232 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1234 * We collect not only the variables that are declared inside "tree",
1235 * but also the loop iterators that are declared anywhere inside
1236 * any possible macro statements in "scop".
1237 * The latter also appear as declared variable in the scop,
1238 * whereas other declared loop iterators only appear implicitly
1239 * in the iteration domains.
1241 static __isl_give isl_union_set *collect_local(struct pet_scop *scop,
1242 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1243 struct pet_state *state)
1245 int i;
1246 isl_ctx *ctx;
1247 struct pet_tree_collect_local_data data = { pc, state };
1249 ctx = pet_tree_get_ctx(tree);
1250 data.local = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
1252 if (pet_tree_foreach_sub_tree(tree, &extract_local_var, &data) < 0)
1253 return isl_union_set_free(data.local);
1255 for (i = 0; i < scop->n_stmt; ++i) {
1256 pet_tree *body = scop->stmts[i]->body;
1257 if (pet_tree_foreach_sub_tree(body, &extract_local_iterator,
1258 &data) < 0)
1259 return isl_union_set_free(data.local);
1262 return data.local;
1265 /* Add an independence to "scop" if the for node "tree" was marked
1266 * independent.
1267 * "domain" is the set of loop iterators, with the current for loop
1268 * innermost. If "sign" is positive, then the inner iterator increases.
1269 * Otherwise it decreases.
1270 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1272 * If the tree was marked, then collect all local variables and
1273 * add an independence.
1275 static struct pet_scop *set_independence(struct pet_scop *scop,
1276 __isl_keep pet_tree *tree, __isl_keep isl_set *domain, int sign,
1277 __isl_keep pet_context *pc, struct pet_state *state)
1279 isl_union_set *local;
1281 if (!tree->u.l.independent)
1282 return scop;
1284 local = collect_local(scop, tree, pc, state);
1285 scop = pet_scop_set_independent(scop, domain, local, sign);
1287 return scop;
1290 /* Construct a pet_scop for a for tree with static affine initialization
1291 * and constant increment within the context "pc".
1292 * The domain of "pc" has already been extended with an (at this point
1293 * unbounded) inner loop iterator corresponding to the current for loop.
1295 * The condition is allowed to contain nested accesses, provided
1296 * they are not being written to inside the body of the loop.
1297 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1298 * essentially treated as a while loop, with iteration domain
1299 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1301 * We extract a pet_scop for the body after intersecting the domain of "pc"
1303 * { [l,i] : i >= init and condition' }
1305 * or
1307 * { [l,i] : i <= init and condition' }
1309 * Where condition' is equal to condition if the latter is
1310 * a simple upper [lower] bound and a condition that is extended
1311 * to apply to all previous iterations otherwise.
1312 * Afterwards, the schedule of the pet_scop is extended with
1314 * { [l,i] -> [i] }
1316 * or
1318 * { [l,i] -> [-i] }
1320 * If the condition is non-affine, then we drop the condition from the
1321 * iteration domain and instead create a separate statement
1322 * for evaluating the condition. The body is then filtered to depend
1323 * on the result of the condition evaluating to true on all iterations
1324 * up to the current iteration, while the evaluation the condition itself
1325 * is filtered to depend on the result of the condition evaluating to true
1326 * on all previous iterations.
1327 * The context of the scop representing the body is dropped
1328 * because we don't know how many times the body will be executed,
1329 * if at all.
1331 * If the stride of the loop is not 1, then "i >= init" is replaced by
1333 * (exists a: i = init + stride * a and a >= 0)
1335 * If the loop iterator i is unsigned, then wrapping may occur.
1336 * We therefore use a virtual iterator instead that does not wrap.
1337 * However, the condition in the code applies
1338 * to the wrapped value, so we need to change condition(l,i)
1339 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1340 * to the original iterator by the wrapping of the virtual iterator.
1341 * Note that there may be no need to perform this final wrapping
1342 * if the loop condition (after wrapping) satisfies certain conditions.
1343 * However, the is_simple_bound condition is not enough since it doesn't
1344 * check if there even is an upper bound.
1346 * Wrapping on unsigned iterators can be avoided entirely if
1347 * loop condition is simple, the loop iterator is incremented
1348 * [decremented] by one and the last value before wrapping cannot
1349 * possibly satisfy the loop condition.
1351 * Valid outer iterators for a for loop are those for which the initial
1352 * value itself, the increment on each domain iteration and
1353 * the condition on both the initial value and
1354 * the result of incrementing the iterator for each iteration of the domain
1355 * can be evaluated.
1356 * If the loop condition is non-affine, then we only consider validity
1357 * of the initial value.
1359 * If the body contains any break, then we keep track of it in "skip"
1360 * (if the skip condition is affine) or it is handled in scop_add_break
1361 * (if the skip condition is not affine).
1362 * Note that the affine break condition needs to be considered with
1363 * respect to previous iterations in the virtual domain (if any).
1365 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1366 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1367 __isl_take isl_val *inc, __isl_take pet_context *pc,
1368 struct pet_state *state)
1370 isl_set *domain;
1371 isl_aff *sched;
1372 isl_set *cond = NULL;
1373 isl_set *skip = NULL;
1374 isl_id *id_test = NULL, *id_break_test;
1375 struct pet_scop *scop, *scop_cond = NULL;
1376 int pos;
1377 int is_one;
1378 int is_unsigned;
1379 int is_simple;
1380 int is_virtual;
1381 int is_non_affine;
1382 int has_affine_break;
1383 int has_var_break;
1384 isl_map *rev_wrap = NULL;
1385 isl_map *init_val_map;
1386 isl_pw_aff *pa;
1387 isl_set *valid_init;
1388 isl_set *valid_cond;
1389 isl_set *valid_cond_init;
1390 isl_set *valid_cond_next;
1391 isl_set *valid_inc;
1392 pet_expr *cond_expr;
1393 pet_context *pc_nested;
1395 pos = pet_context_dim(pc) - 1;
1397 domain = pet_context_get_domain(pc);
1398 cond_expr = pet_expr_copy(tree->u.l.cond);
1399 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1400 pc_nested = pet_context_copy(pc);
1401 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1402 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1403 pet_context_free(pc_nested);
1404 pet_expr_free(cond_expr);
1406 valid_inc = isl_pw_aff_domain(pa_inc);
1408 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1410 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1411 !is_nested_allowed(pa, tree->u.l.body);
1412 if (is_non_affine)
1413 pa = isl_pw_aff_free(pa);
1415 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1416 cond = isl_pw_aff_non_zero_set(pa);
1417 if (is_non_affine)
1418 cond = isl_set_universe(isl_set_get_space(domain));
1420 valid_cond = isl_set_coalesce(valid_cond);
1421 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1422 is_virtual = is_unsigned &&
1423 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1425 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1426 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1427 isl_dim_out, 0);
1428 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1429 isl_set_copy(valid_cond));
1430 if (is_one && !is_virtual) {
1431 isl_set *cond;
1433 isl_pw_aff_free(init_val);
1434 pa = pet_expr_extract_comparison(
1435 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1436 tree->u.l.iv, tree->u.l.init, pc);
1437 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1438 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1439 isl_set_dim(domain, isl_dim_set) - 1, 1);
1440 cond = isl_pw_aff_non_zero_set(pa);
1441 domain = isl_set_intersect(domain, cond);
1442 } else {
1443 isl_set *strided;
1445 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1446 strided = strided_domain(init_val, isl_val_copy(inc));
1447 domain = isl_set_intersect(domain, strided);
1450 if (is_virtual) {
1451 isl_multi_aff *wrap;
1452 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1453 pc = pet_context_preimage_domain(pc, wrap);
1454 rev_wrap = isl_map_from_multi_aff(wrap);
1455 rev_wrap = isl_map_reverse(rev_wrap);
1456 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1457 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1458 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1460 is_simple = is_simple_bound(cond, inc);
1461 if (!is_simple) {
1462 cond = isl_set_gist(cond, isl_set_copy(domain));
1463 is_simple = is_simple_bound(cond, inc);
1465 if (!is_simple)
1466 cond = valid_for_each_iteration(cond,
1467 isl_set_copy(domain), isl_val_copy(inc));
1468 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1469 domain = isl_set_intersect(domain, cond);
1470 sched = map_to_last(pc);
1471 if (isl_val_is_neg(inc))
1472 sched = isl_aff_neg(sched);
1474 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1475 isl_val_copy(inc));
1476 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1478 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1480 if (is_non_affine) {
1481 isl_space *space;
1482 isl_multi_pw_aff *test_index;
1483 space = isl_set_get_space(domain);
1484 test_index = pet_create_test_index(space, state->n_test++);
1485 scop_cond = scop_from_non_affine_condition(
1486 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1487 isl_multi_pw_aff_copy(test_index),
1488 pet_tree_get_loc(tree), pc);
1489 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1490 isl_dim_out);
1491 scop_cond = pet_scop_add_boolean_array(scop_cond,
1492 isl_set_copy(domain), test_index,
1493 state->int_size);
1494 scop_cond = pet_scop_prefix(scop_cond, 0);
1495 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1496 isl_aff_copy(sched));
1499 scop = scop_from_tree(tree->u.l.body, pc, state);
1500 has_affine_break = scop &&
1501 pet_scop_has_affine_skip(scop, pet_skip_later);
1502 if (has_affine_break)
1503 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1504 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1505 if (has_var_break)
1506 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1507 if (is_non_affine) {
1508 scop = pet_scop_reset_context(scop);
1509 scop = pet_scop_prefix(scop, 1);
1511 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
1512 scop = pet_scop_resolve_nested(scop);
1513 if (has_affine_break) {
1514 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1515 is_virtual, rev_wrap);
1516 scop = pet_scop_intersect_domain_prefix(scop,
1517 isl_set_copy(domain));
1519 isl_map_free(rev_wrap);
1520 if (has_var_break)
1521 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1522 isl_val_copy(inc));
1523 if (is_non_affine) {
1524 scop = scop_add_while(scop_cond, scop, id_test, domain,
1525 isl_val_copy(inc));
1526 isl_set_free(valid_inc);
1527 } else {
1528 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1529 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1530 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1531 scop = pet_scop_restrict_context(scop, valid_inc);
1532 scop = set_independence(scop, tree, domain, isl_val_sgn(inc),
1533 pc, state);
1534 isl_set_free(domain);
1537 isl_val_free(inc);
1539 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1540 scop = pet_scop_restrict_context(scop, valid_init);
1542 pet_context_free(pc);
1543 return scop;
1546 /* Construct a pet_scop for a for statement within the context of "pc".
1548 * We update the context to reflect the writes to the loop variable and
1549 * the writes inside the body.
1551 * Then we check if the initialization of the for loop
1552 * is a static affine value and the increment is a constant.
1553 * If so, we construct the pet_scop using scop_from_affine_for.
1554 * Otherwise, we treat the for loop as a while loop
1555 * in scop_from_non_affine_for.
1557 * Note that the initialization and the increment are extracted
1558 * in a context where the current loop iterator has been added
1559 * to the context. If these turn out not be affine, then we
1560 * have reconstruct the body context without an assignment
1561 * to this loop iterator, as this variable will then not be
1562 * treated as a dimension of the iteration domain, but as any
1563 * other variable.
1565 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1566 __isl_keep pet_context *init_pc, struct pet_state *state)
1568 isl_id *iv;
1569 isl_val *inc;
1570 isl_pw_aff *pa_inc, *init_val;
1571 pet_context *pc, *pc_init_val;
1573 if (!tree)
1574 return NULL;
1576 iv = pet_expr_access_get_id(tree->u.l.iv);
1577 pc = pet_context_copy(init_pc);
1578 pc = pet_context_add_inner_iterator(pc, iv);
1579 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1581 pc_init_val = pet_context_copy(pc);
1582 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1583 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1584 pet_context_free(pc_init_val);
1585 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1586 inc = pet_extract_cst(pa_inc);
1587 if (!pa_inc || !init_val || !inc)
1588 goto error;
1589 if (!isl_pw_aff_involves_nan(pa_inc) &&
1590 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1591 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1592 pc, state);
1594 isl_pw_aff_free(pa_inc);
1595 isl_pw_aff_free(init_val);
1596 isl_val_free(inc);
1597 pet_context_free(pc);
1599 pc = pet_context_copy(init_pc);
1600 pc = pet_context_add_infinite_loop(pc);
1601 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1602 return scop_from_non_affine_for(tree, init_pc, pc, state);
1603 error:
1604 isl_pw_aff_free(pa_inc);
1605 isl_pw_aff_free(init_val);
1606 isl_val_free(inc);
1607 pet_context_free(pc);
1608 return NULL;
1611 /* Check whether "expr" is an affine constraint within the context "pc".
1613 static int is_affine_condition(__isl_keep pet_expr *expr,
1614 __isl_keep pet_context *pc)
1616 isl_pw_aff *pa;
1617 int is_affine;
1619 pa = pet_expr_extract_affine_condition(expr, pc);
1620 if (!pa)
1621 return -1;
1622 is_affine = !isl_pw_aff_involves_nan(pa);
1623 isl_pw_aff_free(pa);
1625 return is_affine;
1628 /* Check if the given if statement is a conditional assignement
1629 * with a non-affine condition.
1631 * In particular we check if "stmt" is of the form
1633 * if (condition)
1634 * a = f(...);
1635 * else
1636 * a = g(...);
1638 * where the condition is non-affine and a is some array or scalar access.
1640 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1641 __isl_keep pet_context *pc)
1643 int equal;
1644 isl_ctx *ctx;
1645 pet_expr *expr1, *expr2;
1647 ctx = pet_tree_get_ctx(tree);
1648 if (!pet_options_get_detect_conditional_assignment(ctx))
1649 return 0;
1650 if (tree->type != pet_tree_if_else)
1651 return 0;
1652 if (tree->u.i.then_body->type != pet_tree_expr)
1653 return 0;
1654 if (tree->u.i.else_body->type != pet_tree_expr)
1655 return 0;
1656 expr1 = tree->u.i.then_body->u.e.expr;
1657 expr2 = tree->u.i.else_body->u.e.expr;
1658 if (pet_expr_get_type(expr1) != pet_expr_op)
1659 return 0;
1660 if (pet_expr_get_type(expr2) != pet_expr_op)
1661 return 0;
1662 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1663 return 0;
1664 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1665 return 0;
1666 expr1 = pet_expr_get_arg(expr1, 0);
1667 expr2 = pet_expr_get_arg(expr2, 0);
1668 equal = pet_expr_is_equal(expr1, expr2);
1669 pet_expr_free(expr1);
1670 pet_expr_free(expr2);
1671 if (equal < 0 || !equal)
1672 return 0;
1673 if (is_affine_condition(tree->u.i.cond, pc))
1674 return 0;
1676 return 1;
1679 /* Given that "tree" is of the form
1681 * if (condition)
1682 * a = f(...);
1683 * else
1684 * a = g(...);
1686 * where a is some array or scalar access, construct a pet_scop
1687 * corresponding to this conditional assignment within the context "pc".
1689 * The constructed pet_scop then corresponds to the expression
1691 * a = condition ? f(...) : g(...)
1693 * All access relations in f(...) are intersected with condition
1694 * while all access relation in g(...) are intersected with the complement.
1696 static struct pet_scop *scop_from_conditional_assignment(
1697 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1698 struct pet_state *state)
1700 int type_size;
1701 isl_pw_aff *pa;
1702 isl_set *cond, *comp;
1703 isl_multi_pw_aff *index;
1704 pet_expr *expr1, *expr2;
1705 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1706 pet_context *pc_nested;
1707 struct pet_scop *scop;
1709 pe_cond = pet_expr_copy(tree->u.i.cond);
1710 pe_cond = pet_context_evaluate_expr(pc, pe_cond);
1711 pc_nested = pet_context_copy(pc);
1712 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1713 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1714 pet_context_free(pc_nested);
1715 pet_expr_free(pe_cond);
1716 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1717 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1718 index = isl_multi_pw_aff_from_pw_aff(pa);
1720 expr1 = tree->u.i.then_body->u.e.expr;
1721 expr2 = tree->u.i.else_body->u.e.expr;
1723 pe_cond = pet_expr_from_index(index);
1725 pe_then = pet_expr_get_arg(expr1, 1);
1726 pe_then = pet_context_evaluate_expr(pc, pe_then);
1727 pe_then = pet_expr_restrict(pe_then, cond);
1728 pe_else = pet_expr_get_arg(expr2, 1);
1729 pe_else = pet_context_evaluate_expr(pc, pe_else);
1730 pe_else = pet_expr_restrict(pe_else, comp);
1731 pe_write = pet_expr_get_arg(expr1, 0);
1732 pe_write = pet_context_evaluate_expr(pc, pe_write);
1734 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1735 type_size = pet_expr_get_type_size(pe_write);
1736 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1738 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
1739 pet_tree_get_loc(tree), pc);
1741 pet_context_free(pc);
1743 return scop;
1746 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1748 * We create a separate statement that writes the result
1749 * of the non-affine condition to a virtual scalar.
1750 * A constraint requiring the value of this virtual scalar to be one
1751 * is added to the iteration domains of the then branch.
1752 * Similarly, a constraint requiring the value of this virtual scalar
1753 * to be zero is added to the iteration domains of the else branch, if any.
1754 * We adjust the schedules to ensure that the virtual scalar is written
1755 * before it is read.
1757 * If there are any breaks or continues in the then and/or else
1758 * branches, then we may have to compute a new skip condition.
1759 * This is handled using a pet_skip_info object.
1760 * On initialization, the object checks if skip conditions need
1761 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1762 * adds them in pet_skip_info_if_add.
1764 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1765 __isl_take pet_context *pc, struct pet_state *state)
1767 int has_else;
1768 isl_space *space;
1769 isl_set *domain;
1770 isl_multi_pw_aff *test_index;
1771 struct pet_skip_info skip;
1772 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1774 has_else = tree->type == pet_tree_if_else;
1776 space = pet_context_get_space(pc);
1777 test_index = pet_create_test_index(space, state->n_test++);
1778 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1779 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1780 pet_tree_get_loc(tree), pc);
1781 domain = pet_context_get_domain(pc);
1782 scop = pet_scop_add_boolean_array(scop, domain,
1783 isl_multi_pw_aff_copy(test_index), state->int_size);
1785 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1786 if (has_else)
1787 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1789 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1790 has_else, 0);
1791 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1793 scop = pet_scop_prefix(scop, 0);
1794 scop_then = pet_scop_prefix(scop_then, 1);
1795 scop_then = pet_scop_filter(scop_then,
1796 isl_multi_pw_aff_copy(test_index), 1);
1797 if (has_else) {
1798 scop_else = pet_scop_prefix(scop_else, 1);
1799 scop_else = pet_scop_filter(scop_else, test_index, 0);
1800 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1801 } else
1802 isl_multi_pw_aff_free(test_index);
1804 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1806 scop = pet_skip_info_if_add(&skip, scop, 2);
1808 pet_context_free(pc);
1809 return scop;
1812 /* Construct a pet_scop for an affine if statement within the context "pc".
1814 * The condition is added to the iteration domains of the then branch,
1815 * while the opposite of the condition in added to the iteration domains
1816 * of the else branch, if any.
1818 * If there are any breaks or continues in the then and/or else
1819 * branches, then we may have to compute a new skip condition.
1820 * This is handled using a pet_skip_info_if object.
1821 * On initialization, the object checks if skip conditions need
1822 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1823 * adds them in pet_skip_info_if_add.
1825 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1826 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1827 struct pet_state *state)
1829 int has_else;
1830 isl_ctx *ctx;
1831 isl_set *set, *complement;
1832 isl_set *valid;
1833 struct pet_skip_info skip;
1834 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1835 pet_context *pc_body;
1837 ctx = pet_tree_get_ctx(tree);
1839 has_else = tree->type == pet_tree_if_else;
1841 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1842 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1844 pc_body = pet_context_copy(pc);
1845 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
1846 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
1847 pet_context_free(pc_body);
1848 if (has_else) {
1849 pc_body = pet_context_copy(pc);
1850 complement = isl_set_copy(valid);
1851 complement = isl_set_subtract(valid, isl_set_copy(set));
1852 pc_body = pet_context_intersect_domain(pc_body,
1853 isl_set_copy(complement));
1854 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
1855 pet_context_free(pc_body);
1858 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1859 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
1860 isl_pw_aff_free(cond);
1862 scop = pet_scop_restrict(scop_then, set);
1864 if (has_else) {
1865 scop_else = pet_scop_restrict(scop_else, complement);
1866 scop = pet_scop_add_par(ctx, scop, scop_else);
1868 scop = pet_scop_resolve_nested(scop);
1869 scop = pet_scop_restrict_context(scop, valid);
1871 if (pet_skip_info_has_skip(&skip))
1872 scop = pet_scop_prefix(scop, 0);
1873 scop = pet_skip_info_if_add(&skip, scop, 1);
1875 pet_context_free(pc);
1876 return scop;
1879 /* Construct a pet_scop for an if statement within the context "pc".
1881 * If the condition fits the pattern of a conditional assignment,
1882 * then it is handled by scop_from_conditional_assignment.
1884 * Otherwise, we check if the condition is affine.
1885 * If so, we construct the scop in scop_from_affine_if.
1886 * Otherwise, we construct the scop in scop_from_non_affine_if.
1888 * We allow the condition to be dynamic, i.e., to refer to
1889 * scalars or array elements that may be written to outside
1890 * of the given if statement. These nested accesses are then represented
1891 * as output dimensions in the wrapping iteration domain.
1892 * If it is also written _inside_ the then or else branch, then
1893 * we treat the condition as non-affine.
1894 * As explained in extract_non_affine_if, this will introduce
1895 * an extra statement.
1896 * For aesthetic reasons, we want this statement to have a statement
1897 * number that is lower than those of the then and else branches.
1898 * In order to evaluate if we will need such a statement, however, we
1899 * first construct scops for the then and else branches.
1900 * We therefore reserve a statement number if we might have to
1901 * introduce such an extra statement.
1903 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1904 __isl_keep pet_context *pc, struct pet_state *state)
1906 int has_else;
1907 isl_pw_aff *cond;
1908 pet_expr *cond_expr;
1909 pet_context *pc_nested;
1911 if (!tree)
1912 return NULL;
1914 has_else = tree->type == pet_tree_if_else;
1916 pc = pet_context_copy(pc);
1917 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1918 if (has_else)
1919 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1921 if (is_conditional_assignment(tree, pc))
1922 return scop_from_conditional_assignment(tree, pc, state);
1924 cond_expr = pet_expr_copy(tree->u.i.cond);
1925 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1926 pc_nested = pet_context_copy(pc);
1927 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1928 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1929 pet_context_free(pc_nested);
1930 pet_expr_free(cond_expr);
1932 if (!cond) {
1933 pet_context_free(pc);
1934 return NULL;
1937 if (isl_pw_aff_involves_nan(cond)) {
1938 isl_pw_aff_free(cond);
1939 return scop_from_non_affine_if(tree, pc, state);
1942 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1943 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1944 isl_pw_aff_free(cond);
1945 return scop_from_non_affine_if(tree, pc, state);
1948 return scop_from_affine_if(tree, cond, pc, state);
1951 /* Return a one-dimensional multi piecewise affine expression that is equal
1952 * to the constant 1 and is defined over the given domain.
1954 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
1956 isl_local_space *ls;
1957 isl_aff *aff;
1959 ls = isl_local_space_from_space(space);
1960 aff = isl_aff_zero_on_domain(ls);
1961 aff = isl_aff_set_constant_si(aff, 1);
1963 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1966 /* Construct a pet_scop for a continue statement with the given domain space.
1968 * We simply create an empty scop with a universal pet_skip_now
1969 * skip condition. This skip condition will then be taken into
1970 * account by the enclosing loop construct, possibly after
1971 * being incorporated into outer skip conditions.
1973 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
1974 __isl_take isl_space *space)
1976 struct pet_scop *scop;
1978 scop = pet_scop_empty(isl_space_copy(space));
1980 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
1982 return scop;
1985 /* Construct a pet_scop for a break statement with the given domain space.
1987 * We simply create an empty scop with both a universal pet_skip_now
1988 * skip condition and a universal pet_skip_later skip condition.
1989 * These skip conditions will then be taken into
1990 * account by the enclosing loop construct, possibly after
1991 * being incorporated into outer skip conditions.
1993 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
1994 __isl_take isl_space *space)
1996 struct pet_scop *scop;
1997 isl_multi_pw_aff *skip;
1999 scop = pet_scop_empty(isl_space_copy(space));
2001 skip = one_mpa(space);
2002 scop = pet_scop_set_skip(scop, pet_skip_now,
2003 isl_multi_pw_aff_copy(skip));
2004 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
2006 return scop;
2009 /* Extract a clone of the kill statement in "scop".
2010 * The domain of the clone is given by "domain".
2011 * "scop" is expected to have been created from a DeclStmt
2012 * and should have the kill as its first statement.
2014 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
2015 struct pet_scop *scop, struct pet_state *state)
2017 pet_expr *kill;
2018 struct pet_stmt *stmt;
2019 isl_multi_pw_aff *index;
2020 isl_map *access;
2021 pet_expr *expr, *arg;
2022 pet_tree *tree;
2024 if (!domain || !scop)
2025 return NULL;
2026 if (scop->n_stmt < 1)
2027 isl_die(isl_set_get_ctx(domain), isl_error_internal,
2028 "expecting at least one statement", return NULL);
2029 stmt = scop->stmts[0];
2030 if (!pet_stmt_is_kill(stmt))
2031 isl_die(isl_set_get_ctx(domain), isl_error_internal,
2032 "expecting kill statement", return NULL);
2034 expr = pet_tree_expr_get_expr(stmt->body);
2035 arg = pet_expr_get_arg(expr, 0);
2036 pet_expr_free(expr);
2037 index = pet_expr_access_get_index(arg);
2038 access = pet_expr_access_get_access(arg);
2039 pet_expr_free(arg);
2040 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
2041 access = isl_map_reset_tuple_id(access, isl_dim_in);
2042 kill = pet_expr_kill_from_access_and_index(access, index);
2043 tree = pet_tree_new_expr(kill);
2044 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
2045 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
2046 state->n_stmt++, tree);
2047 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
2050 /* Does "tree" represent an assignment to a variable?
2052 * The assignment may be one of
2053 * - a declaration with initialization
2054 * - an expression with a top-level assignment operator
2056 static int is_assignment(__isl_keep pet_tree *tree)
2058 if (!tree)
2059 return 0;
2060 if (tree->type == pet_tree_decl_init)
2061 return 1;
2062 return pet_tree_is_assign(tree);
2065 /* Update "pc" by taking into account the assignment performed by "tree",
2066 * where "tree" satisfies is_assignment.
2068 * In particular, if the lhs of the assignment is a scalar variable and
2069 * if the rhs is an affine expression, then keep track of this value in "pc"
2070 * so that we can plug it in when we later come across the same variable.
2072 * Any previously assigned value to the variable has already been removed
2073 * by scop_handle_writes.
2075 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
2076 __isl_keep pet_tree *tree)
2078 pet_expr *var, *val;
2079 isl_id *id;
2080 isl_pw_aff *pa;
2082 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
2083 var = pet_tree_decl_get_var(tree);
2084 val = pet_tree_decl_get_init(tree);
2085 } else {
2086 pet_expr *expr;
2087 expr = pet_tree_expr_get_expr(tree);
2088 var = pet_expr_get_arg(expr, 0);
2089 val = pet_expr_get_arg(expr, 1);
2090 pet_expr_free(expr);
2093 if (!pet_expr_is_scalar_access(var)) {
2094 pet_expr_free(var);
2095 pet_expr_free(val);
2096 return pc;
2099 pa = pet_expr_extract_affine(val, pc);
2100 if (!pa)
2101 pc = pet_context_free(pc);
2103 if (!isl_pw_aff_involves_nan(pa)) {
2104 id = pet_expr_access_get_id(var);
2105 pc = pet_context_set_value(pc, id, pa);
2106 } else {
2107 isl_pw_aff_free(pa);
2109 pet_expr_free(var);
2110 pet_expr_free(val);
2112 return pc;
2115 /* Mark all arrays in "scop" as being exposed.
2117 static struct pet_scop *mark_exposed(struct pet_scop *scop)
2119 int i;
2121 if (!scop)
2122 return NULL;
2123 for (i = 0; i < scop->n_array; ++i)
2124 scop->arrays[i]->exposed = 1;
2125 return scop;
2128 /* Try and construct a pet_scop corresponding to (part of)
2129 * a sequence of statements within the context "pc".
2131 * After extracting a statement, we update "pc"
2132 * based on the top-level assignments in the statement
2133 * so that we can exploit them in subsequent statements in the same block.
2135 * If there are any breaks or continues in the individual statements,
2136 * then we may have to compute a new skip condition.
2137 * This is handled using a pet_skip_info object.
2138 * On initialization, the object checks if skip conditions need
2139 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2140 * adds them in pet_skip_info_seq_add.
2142 * If "block" is set, then we need to insert kill statements at
2143 * the end of the block for any array that has been declared by
2144 * one of the statements in the sequence. Each of these declarations
2145 * results in the construction of a kill statement at the place
2146 * of the declaration, so we simply collect duplicates of
2147 * those kill statements and append these duplicates to the constructed scop.
2149 * If "block" is not set, then any array declared by one of the statements
2150 * in the sequence is marked as being exposed.
2152 * If autodetect is set, then we allow the extraction of only a subrange
2153 * of the sequence of statements. However, if there is at least one statement
2154 * for which we could not construct a scop and the final range contains
2155 * either no statements or at least one kill, then we discard the entire
2156 * range.
2158 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2159 __isl_keep pet_context *pc, struct pet_state *state)
2161 int i;
2162 isl_ctx *ctx;
2163 isl_space *space;
2164 isl_set *domain;
2165 struct pet_scop *scop, *kills;
2167 ctx = pet_tree_get_ctx(tree);
2169 space = pet_context_get_space(pc);
2170 domain = pet_context_get_domain(pc);
2171 pc = pet_context_copy(pc);
2172 scop = pet_scop_empty(isl_space_copy(space));
2173 kills = pet_scop_empty(space);
2174 for (i = 0; i < tree->u.b.n; ++i) {
2175 struct pet_scop *scop_i;
2177 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2178 pc = apply_affine_continue(pc, scop);
2179 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2180 pc = scop_handle_writes(scop_i, pc);
2181 if (is_assignment(tree->u.b.child[i]))
2182 pc = handle_assignment(pc, tree->u.b.child[i]);
2183 struct pet_skip_info skip;
2184 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2185 pet_skip_info_seq_extract(&skip, pc, state);
2186 if (pet_skip_info_has_skip(&skip))
2187 scop_i = pet_scop_prefix(scop_i, 0);
2188 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
2189 if (tree->u.b.block) {
2190 struct pet_scop *kill;
2191 kill = extract_kill(domain, scop_i, state);
2192 kills = pet_scop_add_par(ctx, kills, kill);
2193 } else
2194 scop_i = mark_exposed(scop_i);
2196 scop_i = pet_scop_prefix(scop_i, i);
2197 scop = pet_scop_add_seq(ctx, scop, scop_i);
2199 scop = pet_skip_info_seq_add(&skip, scop, i);
2201 if (!scop)
2202 break;
2204 isl_set_free(domain);
2206 kills = pet_scop_prefix(kills, tree->u.b.n);
2207 scop = pet_scop_add_seq(ctx, scop, kills);
2209 pet_context_free(pc);
2211 return scop;
2214 /* Internal data structure for extract_declared_arrays.
2216 * "pc" and "state" are used to create pet_array objects and kill statements.
2217 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2218 * found any declared array.
2219 * "scop" has been initialized by the caller and is used to attach
2220 * the created pet_array objects.
2221 * "kill_before" and "kill_after" are created and updated by
2222 * extract_declared_arrays to collect the kills of the arrays.
2224 struct pet_tree_extract_declared_arrays_data {
2225 pet_context *pc;
2226 struct pet_state *state;
2228 isl_ctx *ctx;
2230 int any;
2231 struct pet_scop *scop;
2232 struct pet_scop *kill_before;
2233 struct pet_scop *kill_after;
2236 /* Check if the node "node" declares any array or scalar.
2237 * If so, create the corresponding pet_array and attach it to data->scop.
2238 * Additionally, create two kill statements for the array and add them
2239 * to data->kill_before and data->kill_after.
2241 static int extract_declared_arrays(__isl_keep pet_tree *node, void *user)
2243 enum pet_tree_type type;
2244 struct pet_tree_extract_declared_arrays_data *data = user;
2245 struct pet_array *array;
2246 struct pet_scop *scop_kill;
2247 pet_expr *var;
2249 type = pet_tree_get_type(node);
2250 if (type == pet_tree_decl || type == pet_tree_decl_init)
2251 var = node->u.d.var;
2252 else if (type == pet_tree_for && node->u.l.declared)
2253 var = node->u.l.iv;
2254 else
2255 return 0;
2257 array = extract_array(var, data->pc, data->state);
2258 if (array)
2259 array->declared = 1;
2260 data->scop = pet_scop_add_array(data->scop, array);
2262 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2263 if (!data->any)
2264 data->kill_before = scop_kill;
2265 else
2266 data->kill_before = pet_scop_add_par(data->ctx,
2267 data->kill_before, scop_kill);
2269 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2270 if (!data->any)
2271 data->kill_after = scop_kill;
2272 else
2273 data->kill_after = pet_scop_add_par(data->ctx,
2274 data->kill_after, scop_kill);
2276 data->any = 1;
2278 return 0;
2281 /* Convert a pet_tree that consists of more than a single leaf
2282 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2283 * Do so within the context of "pc".
2285 * After constructing the core scop, we also look for any arrays (or scalars)
2286 * that are declared inside "tree". Each of those arrays is marked as
2287 * having been declared and kill statements for these arrays
2288 * are introduced before and after the core scop.
2289 * Note that the input tree is not a leaf so that the declaration
2290 * cannot occur at the outer level.
2292 static struct pet_scop *scop_from_tree_macro(__isl_take pet_tree *tree,
2293 __isl_take isl_id *label, __isl_keep pet_context *pc,
2294 struct pet_state *state)
2296 struct pet_tree_extract_declared_arrays_data data = { pc, state };
2298 data.scop = scop_from_unevaluated_tree(pet_tree_copy(tree),
2299 state->n_stmt++, pc);
2301 data.any = 0;
2302 data.ctx = pet_context_get_ctx(pc);
2303 if (pet_tree_foreach_sub_tree(tree, &extract_declared_arrays,
2304 &data) < 0)
2305 data.scop = pet_scop_free(data.scop);
2306 pet_tree_free(tree);
2308 if (!data.any)
2309 return data.scop;
2311 data.kill_before = pet_scop_prefix(data.kill_before, 0);
2312 data.scop = pet_scop_prefix(data.scop, 1);
2313 data.kill_after = pet_scop_prefix(data.kill_after, 2);
2315 data.scop = pet_scop_add_seq(data.ctx, data.kill_before, data.scop);
2316 data.scop = pet_scop_add_seq(data.ctx, data.scop, data.kill_after);
2318 return data.scop;
2321 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2322 * within the context "pc" by calling the appropriate function
2323 * based on the type of "tree".
2325 * If the initially constructed pet_scop turns out to involve
2326 * dynamic control and if the user has requested an encapsulation
2327 * of all dynamic control, then this pet_scop is discarded and
2328 * a new pet_scop is created with a single statement representing
2329 * the entire "tree".
2331 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2332 __isl_keep pet_context *pc, struct pet_state *state)
2334 isl_ctx *ctx;
2335 struct pet_scop *scop = NULL;
2337 if (!tree)
2338 return NULL;
2340 ctx = pet_tree_get_ctx(tree);
2341 switch (tree->type) {
2342 case pet_tree_error:
2343 return NULL;
2344 case pet_tree_block:
2345 return scop_from_block(tree, pc, state);
2346 case pet_tree_break:
2347 return scop_from_break(tree, pet_context_get_space(pc));
2348 case pet_tree_continue:
2349 return scop_from_continue(tree, pet_context_get_space(pc));
2350 case pet_tree_decl:
2351 case pet_tree_decl_init:
2352 return scop_from_decl(tree, pc, state);
2353 case pet_tree_expr:
2354 return scop_from_unevaluated_tree(pet_tree_copy(tree),
2355 state->n_stmt++, pc);
2356 case pet_tree_if:
2357 case pet_tree_if_else:
2358 scop = scop_from_if(tree, pc, state);
2359 break;
2360 case pet_tree_for:
2361 scop = scop_from_for(tree, pc, state);
2362 break;
2363 case pet_tree_while:
2364 scop = scop_from_while(tree, pc, state);
2365 break;
2366 case pet_tree_infinite_loop:
2367 scop = scop_from_infinite_for(tree, pc, state);
2368 break;
2371 if (!scop)
2372 return NULL;
2374 if (!pet_options_get_encapsulate_dynamic_control(ctx) ||
2375 !pet_scop_has_data_dependent_conditions(scop))
2376 return scop;
2378 pet_scop_free(scop);
2379 return scop_from_tree_macro(pet_tree_copy(tree),
2380 isl_id_copy(tree->label), pc, state);
2383 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2384 * "int_size" is the number of bytes need to represent an integer.
2385 * "extract_array" is a callback that we can use to create a pet_array
2386 * that corresponds to the variable accessed by an expression.
2388 * Initialize the global state, construct a context and then
2389 * construct the pet_scop by recursively visiting the tree.
2391 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2392 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2393 __isl_keep pet_context *pc, void *user), void *user,
2394 __isl_keep pet_context *pc)
2396 struct pet_scop *scop;
2397 struct pet_state state = { 0 };
2399 if (!tree)
2400 return NULL;
2402 state.ctx = pet_tree_get_ctx(tree);
2403 state.int_size = int_size;
2404 state.extract_array = extract_array;
2405 state.user = user;
2407 scop = scop_from_tree(tree, pc, &state);
2408 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2410 pet_tree_free(tree);
2412 if (scop)
2413 scop->context = isl_set_params(scop->context);
2415 return scop;