pet_scop_embed: extract out pet_scop_reset_skips
[pet.git] / tree2scop.c
blob6b3d0a127eb0c42f38570f952e6db0b08f3c997f
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 <stdlib.h>
36 #include <string.h>
38 #include <isl/id_to_pw_aff.h>
40 #include "aff.h"
41 #include "expr.h"
42 #include "expr_arg.h"
43 #include "nest.h"
44 #include "scop.h"
45 #include "skip.h"
46 #include "state.h"
47 #include "tree2scop.h"
49 /* Update "pc" by taking into account the writes in "stmt".
50 * That is, clear any previously assigned values to variables
51 * that are written by "stmt".
53 static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
54 __isl_take pet_context *pc)
56 return pet_context_clear_writes_in_tree(pc, stmt->body);
59 /* Update "pc" based on the write accesses in "scop".
61 static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
62 __isl_take pet_context *pc)
64 int i;
66 if (!scop)
67 return pet_context_free(pc);
68 for (i = 0; i < scop->n_stmt; ++i)
69 pc = handle_writes(scop->stmts[i], pc);
71 return pc;
74 /* Wrapper around pet_expr_resolve_assume
75 * for use as a callback to pet_tree_map_expr.
77 static __isl_give pet_expr *resolve_assume(__isl_take pet_expr *expr,
78 void *user)
80 pet_context *pc = user;
82 return pet_expr_resolve_assume(expr, pc);
85 /* Check if any expression inside "tree" is an assume expression and
86 * if its single argument can be converted to an affine expression
87 * in the context of "pc".
88 * If so, replace the argument by the affine expression.
90 __isl_give pet_tree *pet_tree_resolve_assume(__isl_take pet_tree *tree,
91 __isl_keep pet_context *pc)
93 return pet_tree_map_expr(tree, &resolve_assume, pc);
96 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
97 * "tree" has already been evaluated in the context of "pc".
98 * This mainly involves resolving nested expression parameters
99 * and setting the name of the iteration space.
100 * The name is given by tree->label if it is non-NULL. Otherwise,
101 * it is of the form S_<stmt_nr>.
103 static struct pet_scop *scop_from_evaluated_tree(__isl_take pet_tree *tree,
104 int stmt_nr, __isl_keep pet_context *pc)
106 isl_space *space;
107 isl_set *domain;
108 struct pet_stmt *ps;
110 space = pet_context_get_space(pc);
112 tree = pet_tree_resolve_nested(tree, space);
113 tree = pet_tree_resolve_assume(tree, pc);
115 domain = pet_context_get_domain(pc);
116 ps = pet_stmt_from_pet_tree(domain, stmt_nr, tree);
117 return pet_scop_from_pet_stmt(space, ps);
120 /* Convert a top-level pet_expr to a pet_scop with one statement
121 * within the context "pc".
122 * "expr" has already been evaluated in the context of "pc".
123 * We construct a pet_tree from "expr" and continue with
124 * scop_from_evaluated_tree.
125 * The name is of the form S_<stmt_nr>.
126 * The location of the statement is set to "loc".
128 static struct pet_scop *scop_from_evaluated_expr(__isl_take pet_expr *expr,
129 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
131 pet_tree *tree;
133 tree = pet_tree_new_expr(expr);
134 tree = pet_tree_set_loc(tree, loc);
135 return scop_from_evaluated_tree(tree, stmt_nr, pc);
138 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
139 * "tree" has not yet been evaluated in the context of "pc".
140 * We evaluate "tree" in the context of "pc" and continue with
141 * scop_from_evaluated_tree.
142 * The statement name is given by tree->label if it is non-NULL. Otherwise,
143 * it is of the form S_<stmt_nr>.
145 static struct pet_scop *scop_from_unevaluated_tree(__isl_take pet_tree *tree,
146 int stmt_nr, __isl_keep pet_context *pc)
148 tree = pet_context_evaluate_tree(pc, tree);
149 return scop_from_evaluated_tree(tree, stmt_nr, pc);
152 /* Convert a top-level pet_expr to a pet_scop with one statement
153 * within the context "pc", where "expr" has not yet been evaluated
154 * in the context of "pc".
155 * We construct a pet_tree from "expr" and continue with
156 * scop_from_unevaluated_tree.
157 * The statement name is of the form S_<stmt_nr>.
158 * The location of the statement is set to "loc".
160 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
161 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
163 pet_tree *tree;
165 tree = pet_tree_new_expr(expr);
166 tree = pet_tree_set_loc(tree, loc);
167 return scop_from_unevaluated_tree(tree, stmt_nr, pc);
170 /* Construct a pet_scop with a single statement killing the entire
171 * array "array".
172 * The location of the statement is set to "loc".
174 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
175 __isl_keep pet_context *pc, struct pet_state *state)
177 isl_ctx *ctx;
178 isl_id *id;
179 isl_space *space;
180 isl_multi_pw_aff *index;
181 isl_map *access;
182 pet_expr *expr;
183 struct pet_scop *scop;
185 if (!array)
186 goto error;
187 ctx = isl_set_get_ctx(array->extent);
188 access = isl_map_from_range(isl_set_copy(array->extent));
189 id = isl_set_get_tuple_id(array->extent);
190 space = isl_space_alloc(ctx, 0, 0, 0);
191 space = isl_space_set_tuple_id(space, isl_dim_out, id);
192 index = isl_multi_pw_aff_zero(space);
193 expr = pet_expr_kill_from_access_and_index(access, index);
194 return scop_from_expr(expr, state->n_stmt++, loc, pc);
195 error:
196 pet_loc_free(loc);
197 return NULL;
200 /* Construct and return a pet_array corresponding to the variable
201 * accessed by "access" by calling the extract_array callback.
203 static struct pet_array *extract_array(__isl_keep pet_expr *access,
204 __isl_keep pet_context *pc, struct pet_state *state)
206 return state->extract_array(access, pc, state->user);
209 /* Construct a pet_scop for a (single) variable declaration
210 * within the context "pc".
212 * The scop contains the variable being declared (as an array)
213 * and a statement killing the array.
215 * If the declaration comes with an initialization, then the scop
216 * also contains an assignment to the variable.
218 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
219 __isl_keep pet_context *pc, struct pet_state *state)
221 int type_size;
222 isl_ctx *ctx;
223 struct pet_array *array;
224 struct pet_scop *scop_decl, *scop;
225 pet_expr *lhs, *rhs, *pe;
227 array = extract_array(tree->u.d.var, pc, state);
228 if (array)
229 array->declared = 1;
230 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
231 scop_decl = pet_scop_add_array(scop_decl, array);
233 if (tree->type != pet_tree_decl_init)
234 return scop_decl;
236 lhs = pet_expr_copy(tree->u.d.var);
237 rhs = pet_expr_copy(tree->u.d.init);
238 type_size = pet_expr_get_type_size(lhs);
239 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
240 scop = scop_from_expr(pe, state->n_stmt++, pet_tree_get_loc(tree), pc);
242 scop_decl = pet_scop_prefix(scop_decl, 0);
243 scop = pet_scop_prefix(scop, 1);
245 ctx = pet_tree_get_ctx(tree);
246 scop = pet_scop_add_seq(ctx, scop_decl, scop);
248 return scop;
251 /* Does "tree" represent a kill statement?
252 * That is, is it an expression statement that "calls" __pencil_kill?
254 static int is_pencil_kill(__isl_keep pet_tree *tree)
256 pet_expr *expr;
257 const char *name;
259 if (!tree)
260 return -1;
261 if (tree->type != pet_tree_expr)
262 return 0;
263 expr = tree->u.e.expr;
264 if (pet_expr_get_type(expr) != pet_expr_call)
265 return 0;
266 name = pet_expr_call_get_name(expr);
267 if (!name)
268 return -1;
269 return !strcmp(name, "__pencil_kill");
272 /* Add a kill to "scop" that kills what is accessed by
273 * the access expression "expr".
275 * If the access expression has any arguments (after evaluation
276 * in the context of "pc"), then we ignore it, since we cannot
277 * tell which elements are definitely killed.
279 * Otherwise, we extend the index expression to the dimension
280 * of the accessed array and intersect with the extent of the array and
281 * add a kill expression that kills these array elements is added to "scop".
283 static struct pet_scop *scop_add_kill(struct pet_scop *scop,
284 __isl_take pet_expr *expr, __isl_take pet_loc *loc,
285 __isl_keep pet_context *pc, struct pet_state *state)
287 int dim1, dim2;
288 isl_id *id;
289 isl_multi_pw_aff *index;
290 isl_map *map;
291 pet_expr *kill;
292 struct pet_array *array;
293 struct pet_scop *scop_i;
295 expr = pet_context_evaluate_expr(pc, expr);
296 if (!expr)
297 goto error;
298 if (expr->n_arg != 0) {
299 pet_expr_free(expr);
300 return scop;
302 array = extract_array(expr, pc, state);
303 if (!array)
304 goto error;
305 index = pet_expr_access_get_index(expr);
306 pet_expr_free(expr);
307 map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
308 id = isl_map_get_tuple_id(map, isl_dim_out);
309 dim1 = isl_set_dim(array->extent, isl_dim_set);
310 dim2 = isl_map_dim(map, isl_dim_out);
311 map = isl_map_add_dims(map, isl_dim_out, dim1 - dim2);
312 map = isl_map_set_tuple_id(map, isl_dim_out, id);
313 map = isl_map_intersect_range(map, isl_set_copy(array->extent));
314 pet_array_free(array);
315 kill = pet_expr_kill_from_access_and_index(map, index);
316 scop_i = scop_from_evaluated_expr(kill, state->n_stmt++, loc, pc);
317 scop = pet_scop_add_par(state->ctx, scop, scop_i);
319 return scop;
320 error:
321 pet_expr_free(expr);
322 return pet_scop_free(scop);
325 /* For each argument of the __pencil_kill call in "tree" that
326 * represents an access, add a kill statement to "scop" killing the accessed
327 * elements.
329 static struct pet_scop *scop_from_pencil_kill(__isl_keep pet_tree *tree,
330 __isl_keep pet_context *pc, struct pet_state *state)
332 pet_expr *call;
333 struct pet_scop *scop;
334 int i, n;
336 call = tree->u.e.expr;
338 scop = pet_scop_empty(pet_context_get_space(pc));
340 n = pet_expr_get_n_arg(call);
341 for (i = 0; i < n; ++i) {
342 pet_expr *arg;
343 pet_loc *loc;
345 arg = pet_expr_get_arg(call, i);
346 if (!arg)
347 return pet_scop_free(scop);
348 if (pet_expr_get_type(arg) != pet_expr_access) {
349 pet_expr_free(arg);
350 continue;
352 loc = pet_tree_get_loc(tree);
353 scop = scop_add_kill(scop, arg, loc, pc, state);
356 return scop;
359 /* Construct a pet_scop for an expression statement within the context "pc".
361 * If the expression calls __pencil_kill, then it needs to be converted
362 * into zero or more kill statements.
363 * Otherwise, a scop is extracted directly from the tree.
365 static struct pet_scop *scop_from_tree_expr(__isl_keep pet_tree *tree,
366 __isl_keep pet_context *pc, struct pet_state *state)
368 int is_kill;
370 is_kill = is_pencil_kill(tree);
371 if (is_kill < 0)
372 return NULL;
373 if (is_kill)
374 return scop_from_pencil_kill(tree, pc, state);
375 return scop_from_unevaluated_tree(pet_tree_copy(tree),
376 state->n_stmt++, pc);
379 /* Return those elements in the space of "cond" that come after
380 * (based on "sign") an element in "cond" in the final dimension.
382 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
384 isl_space *space;
385 isl_map *previous_to_this;
386 int i, dim;
388 dim = isl_set_dim(cond, isl_dim_set);
389 space = isl_space_map_from_set(isl_set_get_space(cond));
390 previous_to_this = isl_map_universe(space);
391 for (i = 0; i + 1 < dim; ++i)
392 previous_to_this = isl_map_equate(previous_to_this,
393 isl_dim_in, i, isl_dim_out, i);
394 if (sign > 0)
395 previous_to_this = isl_map_order_lt(previous_to_this,
396 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
397 else
398 previous_to_this = isl_map_order_gt(previous_to_this,
399 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
401 cond = isl_set_apply(cond, previous_to_this);
403 return cond;
406 /* Remove those iterations of "domain" that have an earlier iteration
407 * (based on "sign") in the final dimension where "skip" is satisfied.
408 * If "apply_skip_map" is set, then "skip_map" is first applied
409 * to the embedded skip condition before removing it from the domain.
411 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
412 __isl_take isl_set *skip, int sign,
413 int apply_skip_map, __isl_keep isl_map *skip_map)
415 if (apply_skip_map)
416 skip = isl_set_apply(skip, isl_map_copy(skip_map));
417 skip = isl_set_intersect(skip , isl_set_copy(domain));
418 return isl_set_subtract(domain, after(skip, sign));
421 /* Create an affine expression on the domain space of "pc" that
422 * is equal to the final dimension of this domain.
424 static __isl_give isl_aff *map_to_last(__isl_keep pet_context *pc)
426 int pos;
427 isl_space *space;
428 isl_local_space *ls;
430 space = pet_context_get_space(pc);
431 pos = isl_space_dim(space, isl_dim_set) - 1;
432 ls = isl_local_space_from_space(space);
433 return isl_aff_var_on_domain(ls, isl_dim_set, pos);
436 /* Create an affine expression that maps elements
437 * of an array "id_test" to the previous element in the final dimension
438 * (according to "inc"), provided this element belongs to "domain".
439 * That is, create the affine expression
441 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
443 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
444 __isl_take isl_set *domain, __isl_take isl_val *inc)
446 int pos;
447 isl_space *space;
448 isl_aff *aff;
449 isl_pw_aff *pa;
450 isl_multi_aff *ma;
451 isl_multi_pw_aff *prev;
453 pos = isl_set_dim(domain, isl_dim_set) - 1;
454 space = isl_set_get_space(domain);
455 space = isl_space_map_from_set(space);
456 ma = isl_multi_aff_identity(space);
457 aff = isl_multi_aff_get_aff(ma, pos);
458 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
459 ma = isl_multi_aff_set_aff(ma, pos, aff);
460 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
461 prev = isl_multi_pw_aff_from_multi_aff(ma);
462 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
463 pa = isl_pw_aff_intersect_domain(pa, domain);
464 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
465 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
467 return prev;
470 /* Add an implication to "scop" expressing that if an element of
471 * virtual array "id_test" has value "satisfied" then all previous elements
472 * of this array (in the final dimension) also have that value.
473 * The set of previous elements is bounded by "domain".
474 * If "sign" is negative then the iterator
475 * is decreasing and we express that all subsequent array elements
476 * (but still defined previously) have the same value.
478 static struct pet_scop *add_implication(struct pet_scop *scop,
479 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
480 int satisfied)
482 int i, dim;
483 isl_space *space;
484 isl_map *map;
486 dim = isl_set_dim(domain, isl_dim_set);
487 domain = isl_set_set_tuple_id(domain, id_test);
488 space = isl_space_map_from_set(isl_set_get_space(domain));
489 map = isl_map_universe(space);
490 for (i = 0; i + 1 < dim; ++i)
491 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
492 if (sign > 0)
493 map = isl_map_order_ge(map,
494 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
495 else
496 map = isl_map_order_le(map,
497 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
498 map = isl_map_intersect_range(map, domain);
499 scop = pet_scop_add_implication(scop, map, satisfied);
501 return scop;
504 /* Add a filter to "scop" that imposes that it is only executed
505 * when the variable identified by "id_test" has a zero value
506 * for all previous iterations of "domain".
508 * In particular, add a filter that imposes that the array
509 * has a zero value at the previous iteration of domain and
510 * add an implication that implies that it then has that
511 * value for all previous iterations.
513 static struct pet_scop *scop_add_break(struct pet_scop *scop,
514 __isl_take isl_id *id_test, __isl_take isl_set *domain,
515 __isl_take isl_val *inc)
517 isl_multi_pw_aff *prev;
518 int sign = isl_val_sgn(inc);
520 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
521 scop = add_implication(scop, id_test, domain, sign, 0);
522 scop = pet_scop_filter(scop, prev, 0);
524 return scop;
527 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
528 __isl_keep pet_context *pc, struct pet_state *state);
530 /* Construct a pet_scop for an infinite loop around the given body
531 * within the context "pc".
533 * The domain of "pc" has already been extended with an infinite loop
535 * { [t] : t >= 0 }
537 * We extract a pet_scop for the body and then embed it in a loop with
538 * schedule
540 * { [outer,t] -> [t] }
542 * If the body contains any break, then it is taken into
543 * account in apply_affine_break (if the skip condition is affine)
544 * or in scop_add_break (if the skip condition is not affine).
546 * Note that in case of an affine skip condition,
547 * since we are dealing with a loop without loop iterator,
548 * the skip condition cannot refer to the current loop iterator and
549 * so effectively, the effect on the iteration domain is of the form
551 * { [outer,0]; [outer,t] : t >= 1 and not skip }
553 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
554 __isl_keep pet_context *pc, struct pet_state *state)
556 isl_ctx *ctx;
557 isl_id *id_test;
558 isl_set *domain;
559 isl_set *skip;
560 isl_aff *sched;
561 struct pet_scop *scop;
562 int has_affine_break;
563 int has_var_break;
565 ctx = pet_tree_get_ctx(body);
566 domain = pet_context_get_domain(pc);
567 sched = map_to_last(pc);
569 scop = scop_from_tree(body, pc, state);
571 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
572 if (has_affine_break)
573 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
574 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
575 if (has_var_break)
576 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
578 scop = pet_scop_reset_skips(scop);
579 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
580 if (has_affine_break) {
581 domain = apply_affine_break(domain, skip, 1, 0, NULL);
582 scop = pet_scop_intersect_domain_prefix(scop,
583 isl_set_copy(domain));
585 if (has_var_break)
586 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
587 else
588 isl_set_free(domain);
590 return scop;
593 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
595 * for (;;)
596 * body
598 * within the context "pc".
600 * Extend the domain of "pc" with an extra inner loop
602 * { [t] : t >= 0 }
604 * and construct the scop in scop_from_infinite_loop.
606 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
607 __isl_keep pet_context *pc, struct pet_state *state)
609 struct pet_scop *scop;
611 pc = pet_context_copy(pc);
612 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
614 pc = pet_context_add_infinite_loop(pc);
616 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
618 pet_context_free(pc);
620 return scop;
623 /* Construct a pet_scop for a while loop of the form
625 * while (pa)
626 * body
628 * within the context "pc".
630 * The domain of "pc" has already been extended with an infinite loop
632 * { [t] : t >= 0 }
634 * Here, we add the constraints on the outer loop iterators
635 * implied by "pa" and construct the scop in scop_from_infinite_loop.
636 * Note that the intersection with these constraints
637 * may result in an empty loop.
639 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
640 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
641 struct pet_state *state)
643 struct pet_scop *scop;
644 isl_set *dom, *local;
645 isl_set *valid;
647 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
648 dom = isl_pw_aff_non_zero_set(pa);
649 local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
650 pc = pet_context_intersect_domain(pc, local);
651 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
652 scop = pet_scop_restrict(scop, dom);
653 scop = pet_scop_restrict_context(scop, valid);
655 pet_context_free(pc);
656 return scop;
659 /* Construct a scop for a while, given the scops for the condition
660 * and the body, the filter identifier and the iteration domain of
661 * the while loop.
663 * In particular, the scop for the condition is filtered to depend
664 * on "id_test" evaluating to true for all previous iterations
665 * of the loop, while the scop for the body is filtered to depend
666 * on "id_test" evaluating to true for all iterations up to the
667 * current iteration.
668 * The actual filter only imposes that this virtual array has
669 * value one on the previous or the current iteration.
670 * The fact that this condition also applies to the previous
671 * iterations is enforced by an implication.
673 * These filtered scops are then combined into a single scop.
675 * "sign" is positive if the iterator increases and negative
676 * if it decreases.
678 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
679 struct pet_scop *scop_body, __isl_take isl_id *id_test,
680 __isl_take isl_set *domain, __isl_take isl_val *inc)
682 isl_ctx *ctx = isl_set_get_ctx(domain);
683 isl_space *space;
684 isl_multi_pw_aff *test_index;
685 isl_multi_pw_aff *prev;
686 int sign = isl_val_sgn(inc);
687 struct pet_scop *scop;
689 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
690 scop_cond = pet_scop_filter(scop_cond, prev, 1);
692 space = isl_space_map_from_set(isl_set_get_space(domain));
693 test_index = isl_multi_pw_aff_identity(space);
694 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
695 isl_id_copy(id_test));
696 scop_body = pet_scop_filter(scop_body, test_index, 1);
698 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
699 scop = add_implication(scop, id_test, domain, sign, 1);
701 return scop;
704 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
705 * evaluating "cond" and writing the result to a virtual scalar,
706 * as expressed by "index".
707 * The expression "cond" has not yet been evaluated in the context of "pc".
708 * Do so within the context "pc".
709 * The location of the statement is set to "loc".
711 static struct pet_scop *scop_from_non_affine_condition(
712 __isl_take pet_expr *cond, int stmt_nr,
713 __isl_take isl_multi_pw_aff *index,
714 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
716 pet_expr *expr, *write;
718 cond = pet_context_evaluate_expr(pc, cond);
720 write = pet_expr_from_index(index);
721 write = pet_expr_access_set_write(write, 1);
722 write = pet_expr_access_set_read(write, 0);
723 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
725 return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
728 /* Given that "scop" has an affine skip condition of type pet_skip_now,
729 * apply this skip condition to the domain of "pc".
730 * That is, remove the elements satisfying the skip condition from
731 * the domain of "pc".
733 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
734 struct pet_scop *scop)
736 isl_set *domain, *skip;
738 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
739 domain = pet_context_get_domain(pc);
740 domain = isl_set_subtract(domain, skip);
741 pc = pet_context_intersect_domain(pc, domain);
743 return pc;
746 /* Add a scop for evaluating the loop increment "inc" add the end
747 * of a loop body "scop" within the context "pc".
749 * The skip conditions resulting from continue statements inside
750 * the body do not apply to "inc", but those resulting from break
751 * statements do need to get applied.
753 static struct pet_scop *scop_add_inc(struct pet_scop *scop,
754 __isl_take pet_expr *inc, __isl_take pet_loc *loc,
755 __isl_keep pet_context *pc, struct pet_state *state)
757 struct pet_scop *scop_inc;
759 pc = pet_context_copy(pc);
761 if (pet_scop_has_skip(scop, pet_skip_later)) {
762 isl_multi_pw_aff *skip;
763 skip = pet_scop_get_skip(scop, pet_skip_later);
764 scop = pet_scop_set_skip(scop, pet_skip_now, skip);
765 if (pet_scop_has_affine_skip(scop, pet_skip_now))
766 pc = apply_affine_continue(pc, scop);
767 } else
768 pet_scop_reset_skip(scop, pet_skip_now);
769 scop_inc = scop_from_expr(inc, state->n_stmt++, loc, pc);
770 scop_inc = pet_scop_prefix(scop_inc, 2);
771 scop = pet_scop_add_seq(state->ctx, scop, scop_inc);
773 pet_context_free(pc);
775 return scop;
778 /* Construct a generic while scop, with iteration domain
779 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
780 * The domain of "pc" has already been extended with this infinite loop
782 * { [t] : t >= 0 }
784 * The scop consists of two parts,
785 * one for evaluating the condition "cond" and one for the body.
786 * If "expr_inc" is not NULL, then a scop for evaluating this expression
787 * is added at the end of the body,
788 * after replacing any skip conditions resulting from continue statements
789 * by the skip conditions resulting from break statements (if any).
791 * The schedule is adjusted to reflect that the condition is evaluated
792 * before the body is executed and the body is filtered to depend
793 * on the result of the condition evaluating to true on all iterations
794 * up to the current iteration, while the evaluation of the condition itself
795 * is filtered to depend on the result of the condition evaluating to true
796 * on all previous iterations.
797 * The context of the scop representing the body is dropped
798 * because we don't know how many times the body will be executed,
799 * if at all.
801 * If the body contains any break, then it is taken into
802 * account in apply_affine_break (if the skip condition is affine)
803 * or in scop_add_break (if the skip condition is not affine).
805 * Note that in case of an affine skip condition,
806 * since we are dealing with a loop without loop iterator,
807 * the skip condition cannot refer to the current loop iterator and
808 * so effectively, the effect on the iteration domain is of the form
810 * { [outer,0]; [outer,t] : t >= 1 and not skip }
812 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
813 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
814 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
815 struct pet_state *state)
817 isl_ctx *ctx;
818 isl_id *id_test, *id_break_test;
819 isl_space *space;
820 isl_multi_pw_aff *test_index;
821 isl_set *domain;
822 isl_set *skip;
823 isl_aff *sched;
824 struct pet_scop *scop, *scop_body;
825 int has_affine_break;
826 int has_var_break;
828 ctx = state->ctx;
829 space = pet_context_get_space(pc);
830 test_index = pet_create_test_index(space, state->n_test++);
831 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
832 isl_multi_pw_aff_copy(test_index),
833 pet_loc_copy(loc), pc);
834 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
835 domain = pet_context_get_domain(pc);
836 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
837 test_index, state->int_size);
839 sched = map_to_last(pc);
841 scop_body = scop_from_tree(tree_body, pc, state);
843 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
844 if (has_affine_break)
845 skip = pet_scop_get_affine_skip_domain(scop_body,
846 pet_skip_later);
847 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
848 if (has_var_break)
849 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
851 scop = pet_scop_prefix(scop, 0);
852 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(sched));
853 scop_body = pet_scop_reset_context(scop_body);
854 scop_body = pet_scop_prefix(scop_body, 1);
855 if (expr_inc) {
856 scop_body = scop_add_inc(scop_body, expr_inc, loc, pc, state);
857 } else
858 pet_loc_free(loc);
859 scop_body = pet_scop_reset_skips(scop_body);
860 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain), sched);
862 if (has_affine_break) {
863 domain = apply_affine_break(domain, skip, 1, 0, NULL);
864 scop = pet_scop_intersect_domain_prefix(scop,
865 isl_set_copy(domain));
866 scop_body = pet_scop_intersect_domain_prefix(scop_body,
867 isl_set_copy(domain));
869 if (has_var_break) {
870 scop = scop_add_break(scop, isl_id_copy(id_break_test),
871 isl_set_copy(domain), isl_val_one(ctx));
872 scop_body = scop_add_break(scop_body, id_break_test,
873 isl_set_copy(domain), isl_val_one(ctx));
875 scop = scop_add_while(scop, scop_body, id_test, domain,
876 isl_val_one(ctx));
878 pet_context_free(pc);
879 return scop;
882 /* Check if the while loop is of the form
884 * while (affine expression)
885 * body
887 * If so, call scop_from_affine_while to construct a scop.
889 * Otherwise, pass control to scop_from_non_affine_while.
891 * "pc" is the context in which the affine expressions in the scop are created.
892 * The domain of "pc" is extended with an infinite loop
894 * { [t] : t >= 0 }
896 * before passing control to scop_from_affine_while or
897 * scop_from_non_affine_while.
899 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
900 __isl_keep pet_context *pc, struct pet_state *state)
902 pet_expr *cond_expr;
903 isl_pw_aff *pa;
905 if (!tree)
906 return NULL;
908 pc = pet_context_copy(pc);
909 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
911 cond_expr = pet_expr_copy(tree->u.l.cond);
912 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
913 pa = pet_expr_extract_affine_condition(cond_expr, pc);
914 pet_expr_free(cond_expr);
916 pc = pet_context_add_infinite_loop(pc);
918 if (!pa)
919 goto error;
921 if (!isl_pw_aff_involves_nan(pa))
922 return scop_from_affine_while(tree, pa, pc, state);
923 isl_pw_aff_free(pa);
924 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
925 pet_tree_get_loc(tree), tree->u.l.body, NULL,
926 pc, state);
927 error:
928 pet_context_free(pc);
929 return NULL;
932 /* Check whether "cond" expresses a simple loop bound
933 * on the final set dimension.
934 * In particular, if "up" is set then "cond" should contain only
935 * upper bounds on the final set dimension.
936 * Otherwise, it should contain only lower bounds.
938 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
940 int pos;
942 pos = isl_set_dim(cond, isl_dim_set) - 1;
943 if (isl_val_is_pos(inc))
944 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
945 else
946 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
949 /* Extend a condition on a given iteration of a loop to one that
950 * imposes the same condition on all previous iterations.
951 * "domain" expresses the lower [upper] bound on the iterations
952 * when inc is positive [negative] in its final dimension.
954 * In particular, we construct the condition (when inc is positive)
956 * forall i' : (domain(i') and i' <= i) => cond(i')
958 * (where "<=" applies to the final dimension)
959 * which is equivalent to
961 * not exists i' : domain(i') and i' <= i and not cond(i')
963 * We construct this set by subtracting the satisfying cond from domain,
964 * applying a map
966 * { [i'] -> [i] : i' <= i }
968 * and then subtracting the result from domain again.
970 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
971 __isl_take isl_set *domain, __isl_take isl_val *inc)
973 isl_space *space;
974 isl_map *previous_to_this;
975 int i, dim;
977 dim = isl_set_dim(cond, isl_dim_set);
978 space = isl_space_map_from_set(isl_set_get_space(cond));
979 previous_to_this = isl_map_universe(space);
980 for (i = 0; i + 1 < dim; ++i)
981 previous_to_this = isl_map_equate(previous_to_this,
982 isl_dim_in, i, isl_dim_out, i);
983 if (isl_val_is_pos(inc))
984 previous_to_this = isl_map_order_le(previous_to_this,
985 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
986 else
987 previous_to_this = isl_map_order_ge(previous_to_this,
988 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
990 cond = isl_set_subtract(isl_set_copy(domain), cond);
991 cond = isl_set_apply(cond, previous_to_this);
992 cond = isl_set_subtract(domain, cond);
994 isl_val_free(inc);
996 return cond;
999 /* Given an initial value of the form
1001 * { [outer,i] -> init(outer) }
1003 * construct a domain of the form
1005 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1007 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
1008 __isl_take isl_val *inc)
1010 int dim;
1011 isl_aff *aff;
1012 isl_space *space;
1013 isl_local_space *ls;
1014 isl_set *set;
1016 dim = isl_pw_aff_dim(init, isl_dim_in);
1018 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
1019 space = isl_pw_aff_get_domain_space(init);
1020 ls = isl_local_space_from_space(space);
1021 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1022 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
1023 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1025 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
1026 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1028 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
1029 set = isl_set_project_out(set, isl_dim_set, dim, 1);
1031 return set;
1034 /* Assuming "cond" represents a bound on a loop where the loop
1035 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1036 * is possible.
1038 * Under the given assumptions, wrapping is only possible if "cond" allows
1039 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1040 * increasing iterator and 0 in case of a decreasing iterator.
1042 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
1043 __isl_keep isl_val *inc)
1045 int cw;
1046 isl_ctx *ctx;
1047 isl_val *limit;
1048 isl_set *test;
1050 test = isl_set_copy(cond);
1052 ctx = isl_set_get_ctx(test);
1053 if (isl_val_is_neg(inc))
1054 limit = isl_val_zero(ctx);
1055 else {
1056 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1057 limit = isl_val_2exp(limit);
1058 limit = isl_val_sub_ui(limit, 1);
1061 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
1062 cw = !isl_set_is_empty(test);
1063 isl_set_free(test);
1065 return cw;
1068 /* Given a space
1070 * { [outer, v] },
1072 * construct the following affine expression on this space
1074 * { [outer, v] -> [outer, v mod 2^width] }
1076 * where width is the number of bits used to represent the values
1077 * of the unsigned variable "iv".
1079 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
1080 __isl_keep pet_expr *iv)
1082 int dim;
1083 isl_ctx *ctx;
1084 isl_val *mod;
1085 isl_aff *aff;
1086 isl_multi_aff *ma;
1088 dim = isl_space_dim(space, isl_dim_set);
1090 ctx = isl_space_get_ctx(space);
1091 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1092 mod = isl_val_2exp(mod);
1094 space = isl_space_map_from_set(space);
1095 ma = isl_multi_aff_identity(space);
1097 aff = isl_multi_aff_get_aff(ma, dim - 1);
1098 aff = isl_aff_mod_val(aff, mod);
1099 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
1101 return ma;
1104 /* Given two sets in the space
1106 * { [l,i] },
1108 * where l represents the outer loop iterators, compute the set
1109 * of values of l that ensure that "set1" is a subset of "set2".
1111 * set1 is a subset of set2 if
1113 * forall i: set1(l,i) => set2(l,i)
1115 * or
1117 * not exists i: set1(l,i) and not set2(l,i)
1119 * i.e.,
1121 * not exists i: (set1 \ set2)(l,i)
1123 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
1124 __isl_take isl_set *set2)
1126 int pos;
1128 pos = isl_set_dim(set1, isl_dim_set) - 1;
1129 set1 = isl_set_subtract(set1, set2);
1130 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
1131 return isl_set_complement(set1);
1134 /* Compute the set of outer iterator values for which "cond" holds
1135 * on the next iteration of the inner loop for each element of "dom".
1137 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1138 * to the outer loop iterators), plug that into "cond"
1139 * and then compute the set of outer iterators for which "dom" is a subset
1140 * of the result.
1142 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
1143 __isl_take isl_set *dom, __isl_take isl_val *inc)
1145 int pos;
1146 isl_space *space;
1147 isl_aff *aff;
1148 isl_multi_aff *ma;
1150 pos = isl_set_dim(dom, isl_dim_set) - 1;
1151 space = isl_set_get_space(dom);
1152 space = isl_space_map_from_set(space);
1153 ma = isl_multi_aff_identity(space);
1154 aff = isl_multi_aff_get_aff(ma, pos);
1155 aff = isl_aff_add_constant_val(aff, inc);
1156 ma = isl_multi_aff_set_aff(ma, pos, aff);
1157 cond = isl_set_preimage_multi_aff(cond, ma);
1159 return enforce_subset(dom, cond);
1162 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1163 * In particular, "pc_init" represents the context of the loop,
1164 * whereas "pc" represents the context of the body of the loop and
1165 * has already had its domain extended with an infinite loop
1167 * { [t] : t >= 0 }
1169 * The for loop has the form
1171 * for (iv = init; cond; iv += inc)
1172 * body;
1174 * and is treated as
1176 * iv = init;
1177 * while (cond) {
1178 * body;
1179 * iv += inc;
1182 * except that the skips resulting from any continue statements
1183 * in body do not apply to the increment, but are replaced by the skips
1184 * resulting from break statements.
1186 * If the loop iterator is declared in the for loop, then it is killed before
1187 * and after the loop.
1189 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1190 __isl_keep pet_context *init_pc, __isl_take pet_context *pc,
1191 struct pet_state *state)
1193 int declared;
1194 isl_id *iv;
1195 pet_expr *expr_iv, *init, *inc;
1196 struct pet_scop *scop_init, *scop;
1197 int type_size;
1198 struct pet_array *array;
1199 struct pet_scop *scop_kill;
1201 iv = pet_expr_access_get_id(tree->u.l.iv);
1202 pc = pet_context_clear_value(pc, iv);
1204 declared = tree->u.l.declared;
1206 expr_iv = pet_expr_copy(tree->u.l.iv);
1207 type_size = pet_expr_get_type_size(expr_iv);
1208 init = pet_expr_copy(tree->u.l.init);
1209 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1210 scop_init = scop_from_expr(init, state->n_stmt++,
1211 pet_tree_get_loc(tree), init_pc);
1212 scop_init = pet_scop_prefix(scop_init, declared);
1214 expr_iv = pet_expr_copy(tree->u.l.iv);
1215 type_size = pet_expr_get_type_size(expr_iv);
1216 inc = pet_expr_copy(tree->u.l.inc);
1217 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1219 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1220 pet_tree_get_loc(tree), tree->u.l.body, inc,
1221 pet_context_copy(pc), state);
1223 scop = pet_scop_prefix(scop, declared + 1);
1224 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1226 pet_context_free(pc);
1228 if (!declared)
1229 return scop;
1231 array = extract_array(tree->u.l.iv, init_pc, state);
1232 if (array)
1233 array->declared = 1;
1234 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1235 scop_kill = pet_scop_prefix(scop_kill, 0);
1236 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1237 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1238 scop_kill = pet_scop_add_array(scop_kill, array);
1239 scop_kill = pet_scop_prefix(scop_kill, 3);
1240 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1242 return scop;
1245 /* Given an access expression "expr", is the variable accessed by
1246 * "expr" assigned anywhere inside "tree"?
1248 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1250 int assigned = 0;
1251 isl_id *id;
1253 id = pet_expr_access_get_id(expr);
1254 assigned = pet_tree_writes(tree, id);
1255 isl_id_free(id);
1257 return assigned;
1260 /* Are all nested access parameters in "pa" allowed given "tree".
1261 * In particular, is none of them written by anywhere inside "tree".
1263 * If "tree" has any continue or break nodes in the current loop level,
1264 * then no nested access parameters are allowed.
1265 * In particular, if there is any nested access in a guard
1266 * for a piece of code containing a "continue", then we want to introduce
1267 * a separate statement for evaluating this guard so that we can express
1268 * that the result is false for all previous iterations.
1270 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1271 __isl_keep pet_tree *tree)
1273 int i, nparam;
1275 if (!tree)
1276 return -1;
1278 if (!pet_nested_any_in_pw_aff(pa))
1279 return 1;
1281 if (pet_tree_has_continue_or_break(tree))
1282 return 0;
1284 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1285 for (i = 0; i < nparam; ++i) {
1286 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1287 pet_expr *expr;
1288 int allowed;
1290 if (!pet_nested_in_id(id)) {
1291 isl_id_free(id);
1292 continue;
1295 expr = pet_nested_extract_expr(id);
1296 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1297 !is_assigned(expr, tree);
1299 pet_expr_free(expr);
1300 isl_id_free(id);
1302 if (!allowed)
1303 return 0;
1306 return 1;
1309 /* Internal data structure for collect_local.
1310 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1311 * "local" collects the results.
1313 struct pet_tree_collect_local_data {
1314 pet_context *pc;
1315 struct pet_state *state;
1316 isl_union_set *local;
1319 /* Add the variable accessed by "var" to data->local.
1320 * We extract a representation of the variable from
1321 * the pet_array constructed using extract_array
1322 * to ensure consistency with the rest of the scop.
1324 static int add_local(struct pet_tree_collect_local_data *data,
1325 __isl_keep pet_expr *var)
1327 struct pet_array *array;
1328 isl_set *universe;
1330 array = extract_array(var, data->pc, data->state);
1331 if (!array)
1332 return -1;
1334 universe = isl_set_universe(isl_set_get_space(array->extent));
1335 data->local = isl_union_set_add_set(data->local, universe);
1336 pet_array_free(array);
1338 return 0;
1341 /* If the node "tree" declares a variable, then add it to
1342 * data->local.
1344 static int extract_local_var(__isl_keep pet_tree *tree, void *user)
1346 enum pet_tree_type type;
1347 struct pet_tree_collect_local_data *data = user;
1349 type = pet_tree_get_type(tree);
1350 if (type == pet_tree_decl || type == pet_tree_decl_init)
1351 return add_local(data, tree->u.d.var);
1353 return 0;
1356 /* If the node "tree" is a for loop that declares its induction variable,
1357 * then add it this induction variable to data->local.
1359 static int extract_local_iterator(__isl_keep pet_tree *tree, void *user)
1361 struct pet_tree_collect_local_data *data = user;
1363 if (pet_tree_get_type(tree) == pet_tree_for && tree->u.l.declared)
1364 return add_local(data, tree->u.l.iv);
1366 return 0;
1369 /* Collect and return all local variables of the for loop represented
1370 * by "tree", with "scop" the corresponding pet_scop.
1371 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1373 * We collect not only the variables that are declared inside "tree",
1374 * but also the loop iterators that are declared anywhere inside
1375 * any possible macro statements in "scop".
1376 * The latter also appear as declared variable in the scop,
1377 * whereas other declared loop iterators only appear implicitly
1378 * in the iteration domains.
1380 static __isl_give isl_union_set *collect_local(struct pet_scop *scop,
1381 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1382 struct pet_state *state)
1384 int i;
1385 isl_ctx *ctx;
1386 struct pet_tree_collect_local_data data = { pc, state };
1388 ctx = pet_tree_get_ctx(tree);
1389 data.local = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
1391 if (pet_tree_foreach_sub_tree(tree, &extract_local_var, &data) < 0)
1392 return isl_union_set_free(data.local);
1394 for (i = 0; i < scop->n_stmt; ++i) {
1395 pet_tree *body = scop->stmts[i]->body;
1396 if (pet_tree_foreach_sub_tree(body, &extract_local_iterator,
1397 &data) < 0)
1398 return isl_union_set_free(data.local);
1401 return data.local;
1404 /* Add an independence to "scop" if the for node "tree" was marked
1405 * independent.
1406 * "domain" is the set of loop iterators, with the current for loop
1407 * innermost. If "sign" is positive, then the inner iterator increases.
1408 * Otherwise it decreases.
1409 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1411 * If the tree was marked, then collect all local variables and
1412 * add an independence.
1414 static struct pet_scop *set_independence(struct pet_scop *scop,
1415 __isl_keep pet_tree *tree, __isl_keep isl_set *domain, int sign,
1416 __isl_keep pet_context *pc, struct pet_state *state)
1418 isl_union_set *local;
1420 if (!tree->u.l.independent)
1421 return scop;
1423 local = collect_local(scop, tree, pc, state);
1424 scop = pet_scop_set_independent(scop, domain, local, sign);
1426 return scop;
1429 /* Construct a pet_scop for a for tree with static affine initialization
1430 * and constant increment within the context "pc".
1431 * The domain of "pc" has already been extended with an (at this point
1432 * unbounded) inner loop iterator corresponding to the current for loop.
1434 * The condition is allowed to contain nested accesses, provided
1435 * they are not being written to inside the body of the loop.
1436 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1437 * essentially treated as a while loop, with iteration domain
1438 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1440 * We extract a pet_scop for the body after intersecting the domain of "pc"
1442 * { [l,i] : i >= init and condition' }
1444 * or
1446 * { [l,i] : i <= init and condition' }
1448 * Where condition' is equal to condition if the latter is
1449 * a simple upper [lower] bound and a condition that is extended
1450 * to apply to all previous iterations otherwise.
1451 * Afterwards, the schedule of the pet_scop is extended with
1453 * { [l,i] -> [i] }
1455 * or
1457 * { [l,i] -> [-i] }
1459 * If the condition is non-affine, then we drop the condition from the
1460 * iteration domain and instead create a separate statement
1461 * for evaluating the condition. The body is then filtered to depend
1462 * on the result of the condition evaluating to true on all iterations
1463 * up to the current iteration, while the evaluation the condition itself
1464 * is filtered to depend on the result of the condition evaluating to true
1465 * on all previous iterations.
1466 * The context of the scop representing the body is dropped
1467 * because we don't know how many times the body will be executed,
1468 * if at all.
1470 * If the stride of the loop is not 1, then "i >= init" is replaced by
1472 * (exists a: i = init + stride * a and a >= 0)
1474 * If the loop iterator i is unsigned, then wrapping may occur.
1475 * We therefore use a virtual iterator instead that does not wrap.
1476 * However, the condition in the code applies
1477 * to the wrapped value, so we need to change condition(l,i)
1478 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1479 * to the original iterator by the wrapping of the virtual iterator.
1480 * Note that there may be no need to perform this final wrapping
1481 * if the loop condition (after wrapping) satisfies certain conditions.
1482 * However, the is_simple_bound condition is not enough since it doesn't
1483 * check if there even is an upper bound.
1485 * Wrapping on unsigned iterators can be avoided entirely if
1486 * loop condition is simple, the loop iterator is incremented
1487 * [decremented] by one and the last value before wrapping cannot
1488 * possibly satisfy the loop condition.
1490 * Valid outer iterators for a for loop are those for which the initial
1491 * value itself, the increment on each domain iteration and
1492 * the condition on both the initial value and
1493 * the result of incrementing the iterator for each iteration of the domain
1494 * can be evaluated.
1495 * If the loop condition is non-affine, then we only consider validity
1496 * of the initial value.
1498 * If the body contains any break, then we keep track of it in "skip"
1499 * (if the skip condition is affine) or it is handled in scop_add_break
1500 * (if the skip condition is not affine).
1501 * Note that the affine break condition needs to be considered with
1502 * respect to previous iterations in the virtual domain (if any).
1504 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1505 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1506 __isl_take isl_val *inc, __isl_take pet_context *pc,
1507 struct pet_state *state)
1509 isl_set *domain;
1510 isl_aff *sched;
1511 isl_set *cond = NULL;
1512 isl_set *skip = NULL;
1513 isl_id *id_test = NULL, *id_break_test;
1514 struct pet_scop *scop, *scop_cond = NULL;
1515 int pos;
1516 int is_one;
1517 int is_unsigned;
1518 int is_simple;
1519 int is_virtual;
1520 int is_non_affine;
1521 int has_affine_break;
1522 int has_var_break;
1523 isl_map *rev_wrap = NULL;
1524 isl_map *init_val_map;
1525 isl_pw_aff *pa;
1526 isl_set *valid_init;
1527 isl_set *valid_cond;
1528 isl_set *valid_cond_init;
1529 isl_set *valid_cond_next;
1530 isl_set *valid_inc;
1531 pet_expr *cond_expr;
1532 pet_context *pc_nested;
1534 pos = pet_context_dim(pc) - 1;
1536 domain = pet_context_get_domain(pc);
1537 cond_expr = pet_expr_copy(tree->u.l.cond);
1538 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1539 pc_nested = pet_context_copy(pc);
1540 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1541 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1542 pet_context_free(pc_nested);
1543 pet_expr_free(cond_expr);
1545 valid_inc = isl_pw_aff_domain(pa_inc);
1547 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1549 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1550 !is_nested_allowed(pa, tree->u.l.body);
1551 if (is_non_affine)
1552 pa = isl_pw_aff_free(pa);
1554 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1555 cond = isl_pw_aff_non_zero_set(pa);
1556 if (is_non_affine)
1557 cond = isl_set_universe(isl_set_get_space(domain));
1559 valid_cond = isl_set_coalesce(valid_cond);
1560 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1561 is_virtual = is_unsigned &&
1562 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1564 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1565 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1566 isl_dim_out, 0);
1567 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1568 isl_set_copy(valid_cond));
1569 if (is_one && !is_virtual) {
1570 isl_set *cond;
1572 isl_pw_aff_free(init_val);
1573 pa = pet_expr_extract_comparison(
1574 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1575 tree->u.l.iv, tree->u.l.init, pc);
1576 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1577 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1578 isl_set_dim(domain, isl_dim_set) - 1, 1);
1579 cond = isl_pw_aff_non_zero_set(pa);
1580 domain = isl_set_intersect(domain, cond);
1581 } else {
1582 isl_set *strided;
1584 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1585 strided = strided_domain(init_val, isl_val_copy(inc));
1586 domain = isl_set_intersect(domain, strided);
1589 if (is_virtual) {
1590 isl_multi_aff *wrap;
1591 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1592 pc = pet_context_preimage_domain(pc, wrap);
1593 rev_wrap = isl_map_from_multi_aff(wrap);
1594 rev_wrap = isl_map_reverse(rev_wrap);
1595 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1596 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1597 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1599 is_simple = is_simple_bound(cond, inc);
1600 if (!is_simple) {
1601 cond = isl_set_gist(cond, isl_set_copy(domain));
1602 is_simple = is_simple_bound(cond, inc);
1604 if (!is_simple)
1605 cond = valid_for_each_iteration(cond,
1606 isl_set_copy(domain), isl_val_copy(inc));
1607 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1608 domain = isl_set_intersect(domain, cond);
1609 sched = map_to_last(pc);
1610 if (isl_val_is_neg(inc))
1611 sched = isl_aff_neg(sched);
1613 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1614 isl_val_copy(inc));
1615 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1617 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1619 if (is_non_affine) {
1620 isl_space *space;
1621 isl_multi_pw_aff *test_index;
1622 space = isl_set_get_space(domain);
1623 test_index = pet_create_test_index(space, state->n_test++);
1624 scop_cond = scop_from_non_affine_condition(
1625 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1626 isl_multi_pw_aff_copy(test_index),
1627 pet_tree_get_loc(tree), pc);
1628 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1629 isl_dim_out);
1630 scop_cond = pet_scop_add_boolean_array(scop_cond,
1631 isl_set_copy(domain), test_index,
1632 state->int_size);
1633 scop_cond = pet_scop_prefix(scop_cond, 0);
1634 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1635 isl_aff_copy(sched));
1638 scop = scop_from_tree(tree->u.l.body, pc, state);
1639 has_affine_break = scop &&
1640 pet_scop_has_affine_skip(scop, pet_skip_later);
1641 if (has_affine_break)
1642 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1643 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1644 if (has_var_break)
1645 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1646 if (is_non_affine) {
1647 scop = pet_scop_reset_context(scop);
1648 scop = pet_scop_prefix(scop, 1);
1650 scop = pet_scop_reset_skips(scop);
1651 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
1652 scop = pet_scop_resolve_nested(scop);
1653 if (has_affine_break) {
1654 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1655 is_virtual, rev_wrap);
1656 scop = pet_scop_intersect_domain_prefix(scop,
1657 isl_set_copy(domain));
1659 isl_map_free(rev_wrap);
1660 if (has_var_break)
1661 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1662 isl_val_copy(inc));
1663 if (is_non_affine) {
1664 scop = scop_add_while(scop_cond, scop, id_test, domain,
1665 isl_val_copy(inc));
1666 isl_set_free(valid_inc);
1667 } else {
1668 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1669 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1670 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1671 scop = pet_scop_restrict_context(scop, valid_inc);
1672 scop = set_independence(scop, tree, domain, isl_val_sgn(inc),
1673 pc, state);
1674 isl_set_free(domain);
1677 isl_val_free(inc);
1679 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1680 scop = pet_scop_restrict_context(scop, valid_init);
1682 pet_context_free(pc);
1683 return scop;
1686 /* Construct a pet_scop for a for statement within the context of "pc".
1688 * We update the context to reflect the writes to the loop variable and
1689 * the writes inside the body.
1691 * Then we check if the initialization of the for loop
1692 * is a static affine value and the increment is a constant.
1693 * If so, we construct the pet_scop using scop_from_affine_for.
1694 * Otherwise, we treat the for loop as a while loop
1695 * in scop_from_non_affine_for.
1697 * Note that the initialization and the increment are extracted
1698 * in a context where the current loop iterator has been added
1699 * to the context. If these turn out not be affine, then we
1700 * have reconstruct the body context without an assignment
1701 * to this loop iterator, as this variable will then not be
1702 * treated as a dimension of the iteration domain, but as any
1703 * other variable.
1705 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1706 __isl_keep pet_context *init_pc, struct pet_state *state)
1708 isl_id *iv;
1709 isl_val *inc;
1710 isl_pw_aff *pa_inc, *init_val;
1711 pet_context *pc, *pc_init_val;
1713 if (!tree)
1714 return NULL;
1716 iv = pet_expr_access_get_id(tree->u.l.iv);
1717 pc = pet_context_copy(init_pc);
1718 pc = pet_context_add_inner_iterator(pc, iv);
1719 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1721 pc_init_val = pet_context_copy(pc);
1722 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1723 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1724 pet_context_free(pc_init_val);
1725 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1726 inc = pet_extract_cst(pa_inc);
1727 if (!pa_inc || !init_val || !inc)
1728 goto error;
1729 if (!isl_pw_aff_involves_nan(pa_inc) &&
1730 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1731 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1732 pc, state);
1734 isl_pw_aff_free(pa_inc);
1735 isl_pw_aff_free(init_val);
1736 isl_val_free(inc);
1737 pet_context_free(pc);
1739 pc = pet_context_copy(init_pc);
1740 pc = pet_context_add_infinite_loop(pc);
1741 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1742 return scop_from_non_affine_for(tree, init_pc, pc, state);
1743 error:
1744 isl_pw_aff_free(pa_inc);
1745 isl_pw_aff_free(init_val);
1746 isl_val_free(inc);
1747 pet_context_free(pc);
1748 return NULL;
1751 /* Check whether "expr" is an affine constraint within the context "pc".
1753 static int is_affine_condition(__isl_keep pet_expr *expr,
1754 __isl_keep pet_context *pc)
1756 isl_pw_aff *pa;
1757 int is_affine;
1759 pa = pet_expr_extract_affine_condition(expr, pc);
1760 if (!pa)
1761 return -1;
1762 is_affine = !isl_pw_aff_involves_nan(pa);
1763 isl_pw_aff_free(pa);
1765 return is_affine;
1768 /* Check if the given if statement is a conditional assignement
1769 * with a non-affine condition.
1771 * In particular we check if "stmt" is of the form
1773 * if (condition)
1774 * a = f(...);
1775 * else
1776 * a = g(...);
1778 * where the condition is non-affine and a is some array or scalar access.
1780 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1781 __isl_keep pet_context *pc)
1783 int equal;
1784 isl_ctx *ctx;
1785 pet_expr *expr1, *expr2;
1787 ctx = pet_tree_get_ctx(tree);
1788 if (!pet_options_get_detect_conditional_assignment(ctx))
1789 return 0;
1790 if (tree->type != pet_tree_if_else)
1791 return 0;
1792 if (tree->u.i.then_body->type != pet_tree_expr)
1793 return 0;
1794 if (tree->u.i.else_body->type != pet_tree_expr)
1795 return 0;
1796 expr1 = tree->u.i.then_body->u.e.expr;
1797 expr2 = tree->u.i.else_body->u.e.expr;
1798 if (pet_expr_get_type(expr1) != pet_expr_op)
1799 return 0;
1800 if (pet_expr_get_type(expr2) != pet_expr_op)
1801 return 0;
1802 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1803 return 0;
1804 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1805 return 0;
1806 expr1 = pet_expr_get_arg(expr1, 0);
1807 expr2 = pet_expr_get_arg(expr2, 0);
1808 equal = pet_expr_is_equal(expr1, expr2);
1809 pet_expr_free(expr1);
1810 pet_expr_free(expr2);
1811 if (equal < 0 || !equal)
1812 return 0;
1813 if (is_affine_condition(tree->u.i.cond, pc))
1814 return 0;
1816 return 1;
1819 /* Given that "tree" is of the form
1821 * if (condition)
1822 * a = f(...);
1823 * else
1824 * a = g(...);
1826 * where a is some array or scalar access, construct a pet_scop
1827 * corresponding to this conditional assignment within the context "pc".
1828 * "cond_pa" is an affine expression with nested accesses representing
1829 * the condition.
1831 * The constructed pet_scop then corresponds to the expression
1833 * a = condition ? f(...) : g(...)
1835 * All access relations in f(...) are intersected with condition
1836 * while all access relation in g(...) are intersected with the complement.
1838 static struct pet_scop *scop_from_conditional_assignment(
1839 __isl_keep pet_tree *tree, __isl_take isl_pw_aff *cond_pa,
1840 __isl_take pet_context *pc, struct pet_state *state)
1842 int type_size;
1843 isl_set *cond, *comp;
1844 isl_multi_pw_aff *index;
1845 pet_expr *expr1, *expr2;
1846 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1847 struct pet_scop *scop;
1849 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa));
1850 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa));
1851 index = isl_multi_pw_aff_from_pw_aff(cond_pa);
1853 expr1 = tree->u.i.then_body->u.e.expr;
1854 expr2 = tree->u.i.else_body->u.e.expr;
1856 pe_cond = pet_expr_from_index(index);
1858 pe_then = pet_expr_get_arg(expr1, 1);
1859 pe_then = pet_context_evaluate_expr(pc, pe_then);
1860 pe_then = pet_expr_restrict(pe_then, cond);
1861 pe_else = pet_expr_get_arg(expr2, 1);
1862 pe_else = pet_context_evaluate_expr(pc, pe_else);
1863 pe_else = pet_expr_restrict(pe_else, comp);
1864 pe_write = pet_expr_get_arg(expr1, 0);
1865 pe_write = pet_context_evaluate_expr(pc, pe_write);
1867 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1868 type_size = pet_expr_get_type_size(pe_write);
1869 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1871 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
1872 pet_tree_get_loc(tree), pc);
1874 pet_context_free(pc);
1876 return scop;
1879 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1881 * We create a separate statement that writes the result
1882 * of the non-affine condition to a virtual scalar.
1883 * A constraint requiring the value of this virtual scalar to be one
1884 * is added to the iteration domains of the then branch.
1885 * Similarly, a constraint requiring the value of this virtual scalar
1886 * to be zero is added to the iteration domains of the else branch, if any.
1887 * We adjust the schedules to ensure that the virtual scalar is written
1888 * before it is read.
1890 * If there are any breaks or continues in the then and/or else
1891 * branches, then we may have to compute a new skip condition.
1892 * This is handled using a pet_skip_info object.
1893 * On initialization, the object checks if skip conditions need
1894 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1895 * adds them in pet_skip_info_if_add.
1897 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1898 __isl_take pet_context *pc, struct pet_state *state)
1900 int has_else;
1901 isl_space *space;
1902 isl_set *domain;
1903 isl_multi_pw_aff *test_index;
1904 struct pet_skip_info skip;
1905 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1907 has_else = tree->type == pet_tree_if_else;
1909 space = pet_context_get_space(pc);
1910 test_index = pet_create_test_index(space, state->n_test++);
1911 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1912 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1913 pet_tree_get_loc(tree), pc);
1914 domain = pet_context_get_domain(pc);
1915 scop = pet_scop_add_boolean_array(scop, domain,
1916 isl_multi_pw_aff_copy(test_index), state->int_size);
1918 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1919 if (has_else)
1920 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1922 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1923 has_else, 0);
1924 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1926 scop = pet_scop_prefix(scop, 0);
1927 scop_then = pet_scop_prefix(scop_then, 1);
1928 scop_then = pet_scop_filter(scop_then,
1929 isl_multi_pw_aff_copy(test_index), 1);
1930 if (has_else) {
1931 scop_else = pet_scop_prefix(scop_else, 1);
1932 scop_else = pet_scop_filter(scop_else, test_index, 0);
1933 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1934 } else
1935 isl_multi_pw_aff_free(test_index);
1937 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1939 scop = pet_skip_info_if_add(&skip, scop, 2);
1941 pet_context_free(pc);
1942 return scop;
1945 /* Construct a pet_scop for an affine if statement within the context "pc".
1947 * The condition is added to the iteration domains of the then branch,
1948 * while the opposite of the condition in added to the iteration domains
1949 * of the else branch, if any.
1951 * If there are any breaks or continues in the then and/or else
1952 * branches, then we may have to compute a new skip condition.
1953 * This is handled using a pet_skip_info_if object.
1954 * On initialization, the object checks if skip conditions need
1955 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1956 * adds them in pet_skip_info_if_add.
1958 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1959 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1960 struct pet_state *state)
1962 int has_else;
1963 isl_ctx *ctx;
1964 isl_set *set, *complement;
1965 isl_set *valid;
1966 struct pet_skip_info skip;
1967 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1968 pet_context *pc_body;
1970 ctx = pet_tree_get_ctx(tree);
1972 has_else = tree->type == pet_tree_if_else;
1974 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1975 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1977 pc_body = pet_context_copy(pc);
1978 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
1979 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
1980 pet_context_free(pc_body);
1981 if (has_else) {
1982 pc_body = pet_context_copy(pc);
1983 complement = isl_set_copy(valid);
1984 complement = isl_set_subtract(valid, isl_set_copy(set));
1985 pc_body = pet_context_intersect_domain(pc_body,
1986 isl_set_copy(complement));
1987 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
1988 pet_context_free(pc_body);
1991 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1992 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
1993 isl_pw_aff_free(cond);
1995 scop = pet_scop_restrict(scop_then, set);
1997 if (has_else) {
1998 scop_else = pet_scop_restrict(scop_else, complement);
1999 scop = pet_scop_add_par(ctx, scop, scop_else);
2001 scop = pet_scop_resolve_nested(scop);
2002 scop = pet_scop_restrict_context(scop, valid);
2004 if (pet_skip_info_has_skip(&skip))
2005 scop = pet_scop_prefix(scop, 0);
2006 scop = pet_skip_info_if_add(&skip, scop, 1);
2008 pet_context_free(pc);
2009 return scop;
2012 /* Construct a pet_scop for an if statement within the context "pc".
2014 * If the condition fits the pattern of a conditional assignment,
2015 * then it is handled by scop_from_conditional_assignment.
2016 * Note that the condition is only considered for a conditional assignment
2017 * if it is not static-affine. However, it should still convert
2018 * to an affine expression when nesting is allowed.
2020 * Otherwise, we check if the condition is affine.
2021 * If so, we construct the scop in scop_from_affine_if.
2022 * Otherwise, we construct the scop in scop_from_non_affine_if.
2024 * We allow the condition to be dynamic, i.e., to refer to
2025 * scalars or array elements that may be written to outside
2026 * of the given if statement. These nested accesses are then represented
2027 * as output dimensions in the wrapping iteration domain.
2028 * If it is also written _inside_ the then or else branch, then
2029 * we treat the condition as non-affine.
2030 * As explained in extract_non_affine_if, this will introduce
2031 * an extra statement.
2032 * For aesthetic reasons, we want this statement to have a statement
2033 * number that is lower than those of the then and else branches.
2034 * In order to evaluate if we will need such a statement, however, we
2035 * first construct scops for the then and else branches.
2036 * We therefore reserve a statement number if we might have to
2037 * introduce such an extra statement.
2039 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
2040 __isl_keep pet_context *pc, struct pet_state *state)
2042 int has_else;
2043 isl_pw_aff *cond;
2044 pet_expr *cond_expr;
2045 pet_context *pc_nested;
2047 if (!tree)
2048 return NULL;
2050 has_else = tree->type == pet_tree_if_else;
2052 pc = pet_context_copy(pc);
2053 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
2054 if (has_else)
2055 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
2057 cond_expr = pet_expr_copy(tree->u.i.cond);
2058 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
2059 pc_nested = pet_context_copy(pc);
2060 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
2061 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
2062 pet_context_free(pc_nested);
2063 pet_expr_free(cond_expr);
2065 if (!cond) {
2066 pet_context_free(pc);
2067 return NULL;
2070 if (isl_pw_aff_involves_nan(cond)) {
2071 isl_pw_aff_free(cond);
2072 return scop_from_non_affine_if(tree, pc, state);
2075 if (is_conditional_assignment(tree, pc))
2076 return scop_from_conditional_assignment(tree, cond, pc, state);
2078 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
2079 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
2080 isl_pw_aff_free(cond);
2081 return scop_from_non_affine_if(tree, pc, state);
2084 return scop_from_affine_if(tree, cond, pc, state);
2087 /* Return a one-dimensional multi piecewise affine expression that is equal
2088 * to the constant 1 and is defined over the given domain.
2090 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
2092 isl_local_space *ls;
2093 isl_aff *aff;
2095 ls = isl_local_space_from_space(space);
2096 aff = isl_aff_zero_on_domain(ls);
2097 aff = isl_aff_set_constant_si(aff, 1);
2099 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
2102 /* Construct a pet_scop for a continue statement with the given domain space.
2104 * We simply create an empty scop with a universal pet_skip_now
2105 * skip condition. This skip condition will then be taken into
2106 * account by the enclosing loop construct, possibly after
2107 * being incorporated into outer skip conditions.
2109 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
2110 __isl_take isl_space *space)
2112 struct pet_scop *scop;
2114 scop = pet_scop_empty(isl_space_copy(space));
2116 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
2118 return scop;
2121 /* Construct a pet_scop for a break statement with the given domain space.
2123 * We simply create an empty scop with both a universal pet_skip_now
2124 * skip condition and a universal pet_skip_later skip condition.
2125 * These skip conditions will then be taken into
2126 * account by the enclosing loop construct, possibly after
2127 * being incorporated into outer skip conditions.
2129 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
2130 __isl_take isl_space *space)
2132 struct pet_scop *scop;
2133 isl_multi_pw_aff *skip;
2135 scop = pet_scop_empty(isl_space_copy(space));
2137 skip = one_mpa(space);
2138 scop = pet_scop_set_skip(scop, pet_skip_now,
2139 isl_multi_pw_aff_copy(skip));
2140 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
2142 return scop;
2145 /* Extract a clone of the kill statement in "scop".
2146 * The domain of the clone is given by "domain".
2147 * "scop" is expected to have been created from a DeclStmt
2148 * and should have the kill as its first statement.
2150 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
2151 struct pet_scop *scop, struct pet_state *state)
2153 pet_expr *kill;
2154 struct pet_stmt *stmt;
2155 isl_space *space;
2156 isl_multi_pw_aff *mpa;
2157 pet_tree *tree;
2159 if (!domain || !scop)
2160 return NULL;
2161 if (scop->n_stmt < 1)
2162 isl_die(isl_set_get_ctx(domain), isl_error_internal,
2163 "expecting at least one statement", return NULL);
2164 stmt = scop->stmts[0];
2165 if (!pet_stmt_is_kill(stmt))
2166 isl_die(isl_set_get_ctx(domain), isl_error_internal,
2167 "expecting kill statement", return NULL);
2169 kill = pet_tree_expr_get_expr(stmt->body);
2170 space = pet_stmt_get_space(stmt);
2171 space = isl_space_map_from_set(space);
2172 mpa = isl_multi_pw_aff_identity(space);
2173 mpa = isl_multi_pw_aff_reset_tuple_id(mpa, isl_dim_in);
2174 kill = pet_expr_update_domain(kill, mpa);
2175 tree = pet_tree_new_expr(kill);
2176 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
2177 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
2178 state->n_stmt++, tree);
2179 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
2182 /* Does "tree" represent an assignment to a variable?
2184 * The assignment may be one of
2185 * - a declaration with initialization
2186 * - an expression with a top-level assignment operator
2188 static int is_assignment(__isl_keep pet_tree *tree)
2190 if (!tree)
2191 return 0;
2192 if (tree->type == pet_tree_decl_init)
2193 return 1;
2194 return pet_tree_is_assign(tree);
2197 /* Update "pc" by taking into account the assignment performed by "tree",
2198 * where "tree" satisfies is_assignment.
2200 * In particular, if the lhs of the assignment is a scalar variable and
2201 * if the rhs is an affine expression, then keep track of this value in "pc"
2202 * so that we can plug it in when we later come across the same variable.
2204 * Any previously assigned value to the variable has already been removed
2205 * by scop_handle_writes.
2207 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
2208 __isl_keep pet_tree *tree)
2210 pet_expr *var, *val;
2211 isl_id *id;
2212 isl_pw_aff *pa;
2214 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
2215 var = pet_tree_decl_get_var(tree);
2216 val = pet_tree_decl_get_init(tree);
2217 } else {
2218 pet_expr *expr;
2219 expr = pet_tree_expr_get_expr(tree);
2220 var = pet_expr_get_arg(expr, 0);
2221 val = pet_expr_get_arg(expr, 1);
2222 pet_expr_free(expr);
2225 if (!pet_expr_is_scalar_access(var)) {
2226 pet_expr_free(var);
2227 pet_expr_free(val);
2228 return pc;
2231 pa = pet_expr_extract_affine(val, pc);
2232 if (!pa)
2233 pc = pet_context_free(pc);
2235 if (!isl_pw_aff_involves_nan(pa)) {
2236 id = pet_expr_access_get_id(var);
2237 pc = pet_context_set_value(pc, id, pa);
2238 } else {
2239 isl_pw_aff_free(pa);
2241 pet_expr_free(var);
2242 pet_expr_free(val);
2244 return pc;
2247 /* Mark all arrays in "scop" as being exposed.
2249 static struct pet_scop *mark_exposed(struct pet_scop *scop)
2251 int i;
2253 if (!scop)
2254 return NULL;
2255 for (i = 0; i < scop->n_array; ++i)
2256 scop->arrays[i]->exposed = 1;
2257 return scop;
2260 /* Try and construct a pet_scop corresponding to (part of)
2261 * a sequence of statements within the context "pc".
2263 * After extracting a statement, we update "pc"
2264 * based on the top-level assignments in the statement
2265 * so that we can exploit them in subsequent statements in the same block.
2267 * If there are any breaks or continues in the individual statements,
2268 * then we may have to compute a new skip condition.
2269 * This is handled using a pet_skip_info object.
2270 * On initialization, the object checks if skip conditions need
2271 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2272 * adds them in pet_skip_info_seq_add.
2274 * If "block" is set, then we need to insert kill statements at
2275 * the end of the block for any array that has been declared by
2276 * one of the statements in the sequence. Each of these declarations
2277 * results in the construction of a kill statement at the place
2278 * of the declaration, so we simply collect duplicates of
2279 * those kill statements and append these duplicates to the constructed scop.
2281 * If "block" is not set, then any array declared by one of the statements
2282 * in the sequence is marked as being exposed.
2284 * If autodetect is set, then we allow the extraction of only a subrange
2285 * of the sequence of statements. However, if there is at least one statement
2286 * for which we could not construct a scop and the final range contains
2287 * either no statements or at least one kill, then we discard the entire
2288 * range.
2290 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2291 __isl_keep pet_context *pc, struct pet_state *state)
2293 int i;
2294 isl_ctx *ctx;
2295 isl_space *space;
2296 isl_set *domain;
2297 struct pet_scop *scop, *kills;
2299 ctx = pet_tree_get_ctx(tree);
2301 space = pet_context_get_space(pc);
2302 domain = pet_context_get_domain(pc);
2303 pc = pet_context_copy(pc);
2304 scop = pet_scop_empty(isl_space_copy(space));
2305 kills = pet_scop_empty(space);
2306 for (i = 0; i < tree->u.b.n; ++i) {
2307 struct pet_scop *scop_i;
2309 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2310 pc = apply_affine_continue(pc, scop);
2311 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2312 pc = scop_handle_writes(scop_i, pc);
2313 if (is_assignment(tree->u.b.child[i]))
2314 pc = handle_assignment(pc, tree->u.b.child[i]);
2315 struct pet_skip_info skip;
2316 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2317 pet_skip_info_seq_extract(&skip, pc, state);
2318 if (pet_skip_info_has_skip(&skip))
2319 scop_i = pet_scop_prefix(scop_i, 0);
2320 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
2321 if (tree->u.b.block) {
2322 struct pet_scop *kill;
2323 kill = extract_kill(domain, scop_i, state);
2324 kills = pet_scop_add_par(ctx, kills, kill);
2325 } else
2326 scop_i = mark_exposed(scop_i);
2328 scop_i = pet_scop_prefix(scop_i, i);
2329 scop = pet_scop_add_seq(ctx, scop, scop_i);
2331 scop = pet_skip_info_seq_add(&skip, scop, i);
2333 if (!scop)
2334 break;
2336 isl_set_free(domain);
2338 kills = pet_scop_prefix(kills, tree->u.b.n);
2339 scop = pet_scop_add_seq(ctx, scop, kills);
2341 pet_context_free(pc);
2343 return scop;
2346 /* Internal data structure for extract_declared_arrays.
2348 * "pc" and "state" are used to create pet_array objects and kill statements.
2349 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2350 * found any declared array.
2351 * "scop" has been initialized by the caller and is used to attach
2352 * the created pet_array objects.
2353 * "kill_before" and "kill_after" are created and updated by
2354 * extract_declared_arrays to collect the kills of the arrays.
2356 struct pet_tree_extract_declared_arrays_data {
2357 pet_context *pc;
2358 struct pet_state *state;
2360 isl_ctx *ctx;
2362 int any;
2363 struct pet_scop *scop;
2364 struct pet_scop *kill_before;
2365 struct pet_scop *kill_after;
2368 /* Check if the node "node" declares any array or scalar.
2369 * If so, create the corresponding pet_array and attach it to data->scop.
2370 * Additionally, create two kill statements for the array and add them
2371 * to data->kill_before and data->kill_after.
2373 static int extract_declared_arrays(__isl_keep pet_tree *node, void *user)
2375 enum pet_tree_type type;
2376 struct pet_tree_extract_declared_arrays_data *data = user;
2377 struct pet_array *array;
2378 struct pet_scop *scop_kill;
2379 pet_expr *var;
2381 type = pet_tree_get_type(node);
2382 if (type == pet_tree_decl || type == pet_tree_decl_init)
2383 var = node->u.d.var;
2384 else if (type == pet_tree_for && node->u.l.declared)
2385 var = node->u.l.iv;
2386 else
2387 return 0;
2389 array = extract_array(var, data->pc, data->state);
2390 if (array)
2391 array->declared = 1;
2392 data->scop = pet_scop_add_array(data->scop, array);
2394 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2395 if (!data->any)
2396 data->kill_before = scop_kill;
2397 else
2398 data->kill_before = pet_scop_add_par(data->ctx,
2399 data->kill_before, scop_kill);
2401 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2402 if (!data->any)
2403 data->kill_after = scop_kill;
2404 else
2405 data->kill_after = pet_scop_add_par(data->ctx,
2406 data->kill_after, scop_kill);
2408 data->any = 1;
2410 return 0;
2413 /* Convert a pet_tree that consists of more than a single leaf
2414 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2415 * Do so within the context of "pc".
2417 * After constructing the core scop, we also look for any arrays (or scalars)
2418 * that are declared inside "tree". Each of those arrays is marked as
2419 * having been declared and kill statements for these arrays
2420 * are introduced before and after the core scop.
2421 * Note that the input tree is not a leaf so that the declaration
2422 * cannot occur at the outer level.
2424 static struct pet_scop *scop_from_tree_macro(__isl_take pet_tree *tree,
2425 __isl_take isl_id *label, __isl_keep pet_context *pc,
2426 struct pet_state *state)
2428 struct pet_tree_extract_declared_arrays_data data = { pc, state };
2430 data.scop = scop_from_unevaluated_tree(pet_tree_copy(tree),
2431 state->n_stmt++, pc);
2433 data.any = 0;
2434 data.ctx = pet_context_get_ctx(pc);
2435 if (pet_tree_foreach_sub_tree(tree, &extract_declared_arrays,
2436 &data) < 0)
2437 data.scop = pet_scop_free(data.scop);
2438 pet_tree_free(tree);
2440 if (!data.any)
2441 return data.scop;
2443 data.kill_before = pet_scop_prefix(data.kill_before, 0);
2444 data.scop = pet_scop_prefix(data.scop, 1);
2445 data.kill_after = pet_scop_prefix(data.kill_after, 2);
2447 data.scop = pet_scop_add_seq(data.ctx, data.kill_before, data.scop);
2448 data.scop = pet_scop_add_seq(data.ctx, data.scop, data.kill_after);
2450 return data.scop;
2453 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2454 * within the context "pc" by calling the appropriate function
2455 * based on the type of "tree".
2457 * If the initially constructed pet_scop turns out to involve
2458 * dynamic control and if the user has requested an encapsulation
2459 * of all dynamic control, then this pet_scop is discarded and
2460 * a new pet_scop is created with a single statement representing
2461 * the entire "tree".
2462 * However, if the scop contains any active continue or break,
2463 * then we need to include the loop containing the continue or break
2464 * in the encapsulation. We therefore postpone the encapsulation
2465 * until we have constructed a pet_scop for this enclosing loop.
2467 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2468 __isl_keep pet_context *pc, struct pet_state *state)
2470 isl_ctx *ctx;
2471 struct pet_scop *scop = NULL;
2473 if (!tree)
2474 return NULL;
2476 ctx = pet_tree_get_ctx(tree);
2477 switch (tree->type) {
2478 case pet_tree_error:
2479 return NULL;
2480 case pet_tree_block:
2481 return scop_from_block(tree, pc, state);
2482 case pet_tree_break:
2483 return scop_from_break(tree, pet_context_get_space(pc));
2484 case pet_tree_continue:
2485 return scop_from_continue(tree, pet_context_get_space(pc));
2486 case pet_tree_decl:
2487 case pet_tree_decl_init:
2488 return scop_from_decl(tree, pc, state);
2489 case pet_tree_expr:
2490 return scop_from_tree_expr(tree, pc, state);
2491 case pet_tree_if:
2492 case pet_tree_if_else:
2493 scop = scop_from_if(tree, pc, state);
2494 break;
2495 case pet_tree_for:
2496 scop = scop_from_for(tree, pc, state);
2497 break;
2498 case pet_tree_while:
2499 scop = scop_from_while(tree, pc, state);
2500 break;
2501 case pet_tree_infinite_loop:
2502 scop = scop_from_infinite_for(tree, pc, state);
2503 break;
2506 if (!scop)
2507 return NULL;
2509 if (!pet_options_get_encapsulate_dynamic_control(ctx) ||
2510 !pet_scop_has_data_dependent_conditions(scop) ||
2511 pet_scop_has_var_skip(scop, pet_skip_now))
2512 return scop;
2514 pet_scop_free(scop);
2515 return scop_from_tree_macro(pet_tree_copy(tree),
2516 isl_id_copy(tree->label), pc, state);
2519 /* If "tree" has a label that is of the form S_<nr>, then make
2520 * sure that state->n_stmt is greater than nr to ensure that
2521 * we will not generate S_<nr> ourselves.
2523 static int set_first_stmt(__isl_keep pet_tree *tree, void *user)
2525 struct pet_state *state = user;
2526 const char *name;
2527 int nr;
2529 if (!tree)
2530 return -1;
2531 if (!tree->label)
2532 return 0;
2533 name = isl_id_get_name(tree->label);
2534 if (strncmp(name, "S_", 2) != 0)
2535 return 0;
2536 nr = atoi(name + 2);
2537 if (nr >= state->n_stmt)
2538 state->n_stmt = nr + 1;
2540 return 0;
2543 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2544 * "int_size" is the number of bytes need to represent an integer.
2545 * "extract_array" is a callback that we can use to create a pet_array
2546 * that corresponds to the variable accessed by an expression.
2548 * Initialize the global state, construct a context and then
2549 * construct the pet_scop by recursively visiting the tree.
2551 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2553 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2554 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2555 __isl_keep pet_context *pc, void *user), void *user,
2556 __isl_keep pet_context *pc)
2558 struct pet_scop *scop;
2559 struct pet_state state = { 0 };
2561 if (!tree)
2562 return NULL;
2564 state.ctx = pet_tree_get_ctx(tree);
2565 state.int_size = int_size;
2566 state.extract_array = extract_array;
2567 state.user = user;
2568 if (pet_tree_foreach_sub_tree(tree, &set_first_stmt, &state) < 0)
2569 tree = pet_tree_free(tree);
2571 scop = scop_from_tree(tree, pc, &state);
2572 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2574 pet_tree_free(tree);
2576 if (scop)
2577 scop->context = isl_set_params(scop->context);
2579 return scop;