pet_scop: keep track of schedule tree
[pet.git] / tree2scop.c
blob4c0e1aa832e5c7d056f718a95b45e114cc9c7590
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 a single-dimensional multi-affine expression on the domain space
422 * of "pc" that is equal to the final dimension of this domain.
423 * "loop_nr" is the sequence number of the corresponding loop.
425 static __isl_give isl_multi_aff *map_to_last(__isl_keep pet_context *pc,
426 int loop_nr)
428 int pos;
429 isl_space *space;
430 isl_local_space *ls;
431 isl_aff *aff;
432 isl_multi_aff *ma;
433 char name[50];
434 isl_id *label;
436 space = pet_context_get_space(pc);
437 pos = isl_space_dim(space, isl_dim_set) - 1;
438 ls = isl_local_space_from_space(space);
439 aff = isl_aff_var_on_domain(ls, isl_dim_set, pos);
440 ma = isl_multi_aff_from_aff(aff);
442 snprintf(name, sizeof(name), "L_%d", loop_nr);
443 label = isl_id_alloc(pet_context_get_ctx(pc), name, NULL);
444 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, label);
446 return ma;
449 /* Create an affine expression that maps elements
450 * of an array "id_test" to the previous element in the final dimension
451 * (according to "inc"), provided this element belongs to "domain".
452 * That is, create the affine expression
454 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
456 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
457 __isl_take isl_set *domain, __isl_take isl_val *inc)
459 int pos;
460 isl_space *space;
461 isl_aff *aff;
462 isl_pw_aff *pa;
463 isl_multi_aff *ma;
464 isl_multi_pw_aff *prev;
466 pos = isl_set_dim(domain, isl_dim_set) - 1;
467 space = isl_set_get_space(domain);
468 space = isl_space_map_from_set(space);
469 ma = isl_multi_aff_identity(space);
470 aff = isl_multi_aff_get_aff(ma, pos);
471 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
472 ma = isl_multi_aff_set_aff(ma, pos, aff);
473 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
474 prev = isl_multi_pw_aff_from_multi_aff(ma);
475 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
476 pa = isl_pw_aff_intersect_domain(pa, domain);
477 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
478 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
480 return prev;
483 /* Add an implication to "scop" expressing that if an element of
484 * virtual array "id_test" has value "satisfied" then all previous elements
485 * of this array (in the final dimension) also have that value.
486 * The set of previous elements is bounded by "domain".
487 * If "sign" is negative then the iterator
488 * is decreasing and we express that all subsequent array elements
489 * (but still defined previously) have the same value.
491 static struct pet_scop *add_implication(struct pet_scop *scop,
492 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
493 int satisfied)
495 int i, dim;
496 isl_space *space;
497 isl_map *map;
499 dim = isl_set_dim(domain, isl_dim_set);
500 domain = isl_set_set_tuple_id(domain, id_test);
501 space = isl_space_map_from_set(isl_set_get_space(domain));
502 map = isl_map_universe(space);
503 for (i = 0; i + 1 < dim; ++i)
504 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
505 if (sign > 0)
506 map = isl_map_order_ge(map,
507 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
508 else
509 map = isl_map_order_le(map,
510 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
511 map = isl_map_intersect_range(map, domain);
512 scop = pet_scop_add_implication(scop, map, satisfied);
514 return scop;
517 /* Add a filter to "scop" that imposes that it is only executed
518 * when the variable identified by "id_test" has a zero value
519 * for all previous iterations of "domain".
521 * In particular, add a filter that imposes that the array
522 * has a zero value at the previous iteration of domain and
523 * add an implication that implies that it then has that
524 * value for all previous iterations.
526 static struct pet_scop *scop_add_break(struct pet_scop *scop,
527 __isl_take isl_id *id_test, __isl_take isl_set *domain,
528 __isl_take isl_val *inc)
530 isl_multi_pw_aff *prev;
531 int sign = isl_val_sgn(inc);
533 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
534 scop = add_implication(scop, id_test, domain, sign, 0);
535 scop = pet_scop_filter(scop, prev, 0);
537 return scop;
540 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
541 __isl_keep pet_context *pc, struct pet_state *state);
543 /* Construct a pet_scop for an infinite loop around the given body
544 * within the context "pc".
546 * The domain of "pc" has already been extended with an infinite loop
548 * { [t] : t >= 0 }
550 * We extract a pet_scop for the body and then embed it in a loop with
551 * schedule
553 * { [outer,t] -> [t] }
555 * If the body contains any break, then it is taken into
556 * account in apply_affine_break (if the skip condition is affine)
557 * or in scop_add_break (if the skip condition is not affine).
559 * Note that in case of an affine skip condition,
560 * since we are dealing with a loop without loop iterator,
561 * the skip condition cannot refer to the current loop iterator and
562 * so effectively, the effect on the iteration domain is of the form
564 * { [outer,0]; [outer,t] : t >= 1 and not skip }
566 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
567 __isl_keep pet_context *pc, struct pet_state *state)
569 isl_ctx *ctx;
570 isl_id *id_test;
571 isl_set *domain;
572 isl_set *skip;
573 isl_multi_aff *sched;
574 struct pet_scop *scop;
575 int has_affine_break;
576 int has_var_break;
578 ctx = pet_tree_get_ctx(body);
579 domain = pet_context_get_domain(pc);
580 sched = map_to_last(pc, state->n_loop++);
582 scop = scop_from_tree(body, pc, state);
584 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
585 if (has_affine_break)
586 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
587 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
588 if (has_var_break)
589 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
591 scop = pet_scop_reset_skips(scop);
592 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
593 if (has_affine_break) {
594 domain = apply_affine_break(domain, skip, 1, 0, NULL);
595 scop = pet_scop_intersect_domain_prefix(scop,
596 isl_set_copy(domain));
598 if (has_var_break)
599 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
600 else
601 isl_set_free(domain);
603 return scop;
606 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
608 * for (;;)
609 * body
611 * within the context "pc".
613 * Extend the domain of "pc" with an extra inner loop
615 * { [t] : t >= 0 }
617 * and construct the scop in scop_from_infinite_loop.
619 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
620 __isl_keep pet_context *pc, struct pet_state *state)
622 struct pet_scop *scop;
624 pc = pet_context_copy(pc);
625 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
627 pc = pet_context_add_infinite_loop(pc);
629 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
631 pet_context_free(pc);
633 return scop;
636 /* Construct a pet_scop for a while loop of the form
638 * while (pa)
639 * body
641 * within the context "pc".
643 * The domain of "pc" has already been extended with an infinite loop
645 * { [t] : t >= 0 }
647 * Here, we add the constraints on the outer loop iterators
648 * implied by "pa" and construct the scop in scop_from_infinite_loop.
649 * Note that the intersection with these constraints
650 * may result in an empty loop.
652 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
653 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
654 struct pet_state *state)
656 struct pet_scop *scop;
657 isl_set *dom, *local;
658 isl_set *valid;
660 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
661 dom = isl_pw_aff_non_zero_set(pa);
662 local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
663 pc = pet_context_intersect_domain(pc, local);
664 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
665 scop = pet_scop_restrict(scop, dom);
666 scop = pet_scop_restrict_context(scop, valid);
668 pet_context_free(pc);
669 return scop;
672 /* Construct a scop for a while, given the scops for the condition
673 * and the body, the filter identifier and the iteration domain of
674 * the while loop.
676 * In particular, the scop for the condition is filtered to depend
677 * on "id_test" evaluating to true for all previous iterations
678 * of the loop, while the scop for the body is filtered to depend
679 * on "id_test" evaluating to true for all iterations up to the
680 * current iteration.
681 * The actual filter only imposes that this virtual array has
682 * value one on the previous or the current iteration.
683 * The fact that this condition also applies to the previous
684 * iterations is enforced by an implication.
686 * These filtered scops are then combined into a single scop,
687 * with the condition scop scheduled before the body scop.
689 * "sign" is positive if the iterator increases and negative
690 * if it decreases.
692 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
693 struct pet_scop *scop_body, __isl_take isl_id *id_test,
694 __isl_take isl_set *domain, __isl_take isl_val *inc)
696 isl_ctx *ctx = isl_set_get_ctx(domain);
697 isl_space *space;
698 isl_multi_pw_aff *test_index;
699 isl_multi_pw_aff *prev;
700 int sign = isl_val_sgn(inc);
701 struct pet_scop *scop;
703 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
704 scop_cond = pet_scop_filter(scop_cond, prev, 1);
706 space = isl_space_map_from_set(isl_set_get_space(domain));
707 test_index = isl_multi_pw_aff_identity(space);
708 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
709 isl_id_copy(id_test));
710 scop_body = pet_scop_filter(scop_body, test_index, 1);
712 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
713 scop = add_implication(scop, id_test, domain, sign, 1);
715 return scop;
718 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
719 * evaluating "cond" and writing the result to a virtual scalar,
720 * as expressed by "index".
721 * The expression "cond" has not yet been evaluated in the context of "pc".
722 * Do so within the context "pc".
723 * The location of the statement is set to "loc".
725 static struct pet_scop *scop_from_non_affine_condition(
726 __isl_take pet_expr *cond, int stmt_nr,
727 __isl_take isl_multi_pw_aff *index,
728 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
730 pet_expr *expr, *write;
732 cond = pet_context_evaluate_expr(pc, cond);
734 write = pet_expr_from_index(index);
735 write = pet_expr_access_set_write(write, 1);
736 write = pet_expr_access_set_read(write, 0);
737 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
739 return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
742 /* Given that "scop" has an affine skip condition of type pet_skip_now,
743 * apply this skip condition to the domain of "pc".
744 * That is, remove the elements satisfying the skip condition from
745 * the domain of "pc".
747 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
748 struct pet_scop *scop)
750 isl_set *domain, *skip;
752 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
753 domain = pet_context_get_domain(pc);
754 domain = isl_set_subtract(domain, skip);
755 pc = pet_context_intersect_domain(pc, domain);
757 return pc;
760 /* Add a scop for evaluating the loop increment "inc" add the end
761 * of a loop body "scop" within the context "pc".
763 * The skip conditions resulting from continue statements inside
764 * the body do not apply to "inc", but those resulting from break
765 * statements do need to get applied.
767 static struct pet_scop *scop_add_inc(struct pet_scop *scop,
768 __isl_take pet_expr *inc, __isl_take pet_loc *loc,
769 __isl_keep pet_context *pc, struct pet_state *state)
771 struct pet_scop *scop_inc;
773 pc = pet_context_copy(pc);
775 if (pet_scop_has_skip(scop, pet_skip_later)) {
776 isl_multi_pw_aff *skip;
777 skip = pet_scop_get_skip(scop, pet_skip_later);
778 scop = pet_scop_set_skip(scop, pet_skip_now, skip);
779 if (pet_scop_has_affine_skip(scop, pet_skip_now))
780 pc = apply_affine_continue(pc, scop);
781 } else
782 pet_scop_reset_skip(scop, pet_skip_now);
783 scop_inc = scop_from_expr(inc, state->n_stmt++, loc, pc);
784 scop_inc = pet_scop_prefix(scop_inc, 2);
785 scop = pet_scop_add_seq(state->ctx, scop, scop_inc);
787 pet_context_free(pc);
789 return scop;
792 /* Construct a generic while scop, with iteration domain
793 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
794 * The domain of "pc" has already been extended with this infinite loop
796 * { [t] : t >= 0 }
798 * The scop consists of two parts,
799 * one for evaluating the condition "cond" and one for the body.
800 * If "expr_inc" is not NULL, then a scop for evaluating this expression
801 * is added at the end of the body,
802 * after replacing any skip conditions resulting from continue statements
803 * by the skip conditions resulting from break statements (if any).
805 * The schedules are combined as a sequence to reflect that the condition is
806 * evaluated before the body is executed and the body is filtered to depend
807 * on the result of the condition evaluating to true on all iterations
808 * up to the current iteration, while the evaluation of the condition itself
809 * is filtered to depend on the result of the condition evaluating to true
810 * on all previous iterations.
811 * The context of the scop representing the body is dropped
812 * because we don't know how many times the body will be executed,
813 * if at all.
815 * If the body contains any break, then it is taken into
816 * account in apply_affine_break (if the skip condition is affine)
817 * or in scop_add_break (if the skip condition is not affine).
819 * Note that in case of an affine skip condition,
820 * since we are dealing with a loop without loop iterator,
821 * the skip condition cannot refer to the current loop iterator and
822 * so effectively, the effect on the iteration domain is of the form
824 * { [outer,0]; [outer,t] : t >= 1 and not skip }
826 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
827 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
828 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
829 struct pet_state *state)
831 isl_ctx *ctx;
832 isl_id *id_test, *id_break_test;
833 isl_space *space;
834 isl_multi_pw_aff *test_index;
835 isl_set *domain;
836 isl_set *skip;
837 isl_multi_aff *sched;
838 struct pet_scop *scop, *scop_body;
839 int has_affine_break;
840 int has_var_break;
842 ctx = state->ctx;
843 space = pet_context_get_space(pc);
844 test_index = pet_create_test_index(space, state->n_test++);
845 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
846 isl_multi_pw_aff_copy(test_index),
847 pet_loc_copy(loc), pc);
848 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
849 domain = pet_context_get_domain(pc);
850 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
851 test_index, state->int_size);
853 sched = map_to_last(pc, state->n_loop++);
855 scop_body = scop_from_tree(tree_body, pc, state);
857 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
858 if (has_affine_break)
859 skip = pet_scop_get_affine_skip_domain(scop_body,
860 pet_skip_later);
861 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
862 if (has_var_break)
863 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
865 scop = pet_scop_prefix(scop, 0);
866 scop_body = pet_scop_reset_context(scop_body);
867 scop_body = pet_scop_prefix(scop_body, 1);
868 if (expr_inc) {
869 scop_body = scop_add_inc(scop_body, expr_inc, loc, pc, state);
870 } else
871 pet_loc_free(loc);
872 scop_body = pet_scop_reset_skips(scop_body);
874 if (has_affine_break) {
875 domain = apply_affine_break(domain, skip, 1, 0, NULL);
876 scop = pet_scop_intersect_domain_prefix(scop,
877 isl_set_copy(domain));
878 scop_body = pet_scop_intersect_domain_prefix(scop_body,
879 isl_set_copy(domain));
881 if (has_var_break) {
882 scop = scop_add_break(scop, isl_id_copy(id_break_test),
883 isl_set_copy(domain), isl_val_one(ctx));
884 scop_body = scop_add_break(scop_body, id_break_test,
885 isl_set_copy(domain), isl_val_one(ctx));
887 scop = scop_add_while(scop, scop_body, id_test, isl_set_copy(domain),
888 isl_val_one(ctx));
890 scop = pet_scop_embed(scop, domain, sched);
892 pet_context_free(pc);
893 return scop;
896 /* Check if the while loop is of the form
898 * while (affine expression)
899 * body
901 * If so, call scop_from_affine_while to construct a scop.
903 * Otherwise, pass control to scop_from_non_affine_while.
905 * "pc" is the context in which the affine expressions in the scop are created.
906 * The domain of "pc" is extended with an infinite loop
908 * { [t] : t >= 0 }
910 * before passing control to scop_from_affine_while or
911 * scop_from_non_affine_while.
913 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
914 __isl_keep pet_context *pc, struct pet_state *state)
916 pet_expr *cond_expr;
917 isl_pw_aff *pa;
919 if (!tree)
920 return NULL;
922 pc = pet_context_copy(pc);
923 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
925 cond_expr = pet_expr_copy(tree->u.l.cond);
926 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
927 pa = pet_expr_extract_affine_condition(cond_expr, pc);
928 pet_expr_free(cond_expr);
930 pc = pet_context_add_infinite_loop(pc);
932 if (!pa)
933 goto error;
935 if (!isl_pw_aff_involves_nan(pa))
936 return scop_from_affine_while(tree, pa, pc, state);
937 isl_pw_aff_free(pa);
938 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
939 pet_tree_get_loc(tree), tree->u.l.body, NULL,
940 pc, state);
941 error:
942 pet_context_free(pc);
943 return NULL;
946 /* Check whether "cond" expresses a simple loop bound
947 * on the final set dimension.
948 * In particular, if "up" is set then "cond" should contain only
949 * upper bounds on the final set dimension.
950 * Otherwise, it should contain only lower bounds.
952 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
954 int pos;
956 pos = isl_set_dim(cond, isl_dim_set) - 1;
957 if (isl_val_is_pos(inc))
958 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
959 else
960 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
963 /* Extend a condition on a given iteration of a loop to one that
964 * imposes the same condition on all previous iterations.
965 * "domain" expresses the lower [upper] bound on the iterations
966 * when inc is positive [negative] in its final dimension.
968 * In particular, we construct the condition (when inc is positive)
970 * forall i' : (domain(i') and i' <= i) => cond(i')
972 * (where "<=" applies to the final dimension)
973 * which is equivalent to
975 * not exists i' : domain(i') and i' <= i and not cond(i')
977 * We construct this set by subtracting the satisfying cond from domain,
978 * applying a map
980 * { [i'] -> [i] : i' <= i }
982 * and then subtracting the result from domain again.
984 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
985 __isl_take isl_set *domain, __isl_take isl_val *inc)
987 isl_space *space;
988 isl_map *previous_to_this;
989 int i, dim;
991 dim = isl_set_dim(cond, isl_dim_set);
992 space = isl_space_map_from_set(isl_set_get_space(cond));
993 previous_to_this = isl_map_universe(space);
994 for (i = 0; i + 1 < dim; ++i)
995 previous_to_this = isl_map_equate(previous_to_this,
996 isl_dim_in, i, isl_dim_out, i);
997 if (isl_val_is_pos(inc))
998 previous_to_this = isl_map_order_le(previous_to_this,
999 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1000 else
1001 previous_to_this = isl_map_order_ge(previous_to_this,
1002 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1004 cond = isl_set_subtract(isl_set_copy(domain), cond);
1005 cond = isl_set_apply(cond, previous_to_this);
1006 cond = isl_set_subtract(domain, cond);
1008 isl_val_free(inc);
1010 return cond;
1013 /* Given an initial value of the form
1015 * { [outer,i] -> init(outer) }
1017 * construct a domain of the form
1019 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1021 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
1022 __isl_take isl_val *inc)
1024 int dim;
1025 isl_aff *aff;
1026 isl_space *space;
1027 isl_local_space *ls;
1028 isl_set *set;
1030 dim = isl_pw_aff_dim(init, isl_dim_in);
1032 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
1033 space = isl_pw_aff_get_domain_space(init);
1034 ls = isl_local_space_from_space(space);
1035 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1036 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
1037 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1039 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
1040 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1042 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
1043 set = isl_set_project_out(set, isl_dim_set, dim, 1);
1045 return set;
1048 /* Assuming "cond" represents a bound on a loop where the loop
1049 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1050 * is possible.
1052 * Under the given assumptions, wrapping is only possible if "cond" allows
1053 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1054 * increasing iterator and 0 in case of a decreasing iterator.
1056 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
1057 __isl_keep isl_val *inc)
1059 int cw;
1060 isl_ctx *ctx;
1061 isl_val *limit;
1062 isl_set *test;
1064 test = isl_set_copy(cond);
1066 ctx = isl_set_get_ctx(test);
1067 if (isl_val_is_neg(inc))
1068 limit = isl_val_zero(ctx);
1069 else {
1070 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1071 limit = isl_val_2exp(limit);
1072 limit = isl_val_sub_ui(limit, 1);
1075 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
1076 cw = !isl_set_is_empty(test);
1077 isl_set_free(test);
1079 return cw;
1082 /* Given a space
1084 * { [outer, v] },
1086 * construct the following affine expression on this space
1088 * { [outer, v] -> [outer, v mod 2^width] }
1090 * where width is the number of bits used to represent the values
1091 * of the unsigned variable "iv".
1093 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
1094 __isl_keep pet_expr *iv)
1096 int dim;
1097 isl_ctx *ctx;
1098 isl_val *mod;
1099 isl_aff *aff;
1100 isl_multi_aff *ma;
1102 dim = isl_space_dim(space, isl_dim_set);
1104 ctx = isl_space_get_ctx(space);
1105 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1106 mod = isl_val_2exp(mod);
1108 space = isl_space_map_from_set(space);
1109 ma = isl_multi_aff_identity(space);
1111 aff = isl_multi_aff_get_aff(ma, dim - 1);
1112 aff = isl_aff_mod_val(aff, mod);
1113 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
1115 return ma;
1118 /* Given two sets in the space
1120 * { [l,i] },
1122 * where l represents the outer loop iterators, compute the set
1123 * of values of l that ensure that "set1" is a subset of "set2".
1125 * set1 is a subset of set2 if
1127 * forall i: set1(l,i) => set2(l,i)
1129 * or
1131 * not exists i: set1(l,i) and not set2(l,i)
1133 * i.e.,
1135 * not exists i: (set1 \ set2)(l,i)
1137 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
1138 __isl_take isl_set *set2)
1140 int pos;
1142 pos = isl_set_dim(set1, isl_dim_set) - 1;
1143 set1 = isl_set_subtract(set1, set2);
1144 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
1145 return isl_set_complement(set1);
1148 /* Compute the set of outer iterator values for which "cond" holds
1149 * on the next iteration of the inner loop for each element of "dom".
1151 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1152 * to the outer loop iterators), plug that into "cond"
1153 * and then compute the set of outer iterators for which "dom" is a subset
1154 * of the result.
1156 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
1157 __isl_take isl_set *dom, __isl_take isl_val *inc)
1159 int pos;
1160 isl_space *space;
1161 isl_aff *aff;
1162 isl_multi_aff *ma;
1164 pos = isl_set_dim(dom, isl_dim_set) - 1;
1165 space = isl_set_get_space(dom);
1166 space = isl_space_map_from_set(space);
1167 ma = isl_multi_aff_identity(space);
1168 aff = isl_multi_aff_get_aff(ma, pos);
1169 aff = isl_aff_add_constant_val(aff, inc);
1170 ma = isl_multi_aff_set_aff(ma, pos, aff);
1171 cond = isl_set_preimage_multi_aff(cond, ma);
1173 return enforce_subset(dom, cond);
1176 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1177 * In particular, "pc_init" represents the context of the loop,
1178 * whereas "pc" represents the context of the body of the loop and
1179 * has already had its domain extended with an infinite loop
1181 * { [t] : t >= 0 }
1183 * The for loop has the form
1185 * for (iv = init; cond; iv += inc)
1186 * body;
1188 * and is treated as
1190 * iv = init;
1191 * while (cond) {
1192 * body;
1193 * iv += inc;
1196 * except that the skips resulting from any continue statements
1197 * in body do not apply to the increment, but are replaced by the skips
1198 * resulting from break statements.
1200 * If the loop iterator is declared in the for loop, then it is killed before
1201 * and after the loop.
1203 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1204 __isl_keep pet_context *init_pc, __isl_take pet_context *pc,
1205 struct pet_state *state)
1207 int declared;
1208 isl_id *iv;
1209 pet_expr *expr_iv, *init, *inc;
1210 struct pet_scop *scop_init, *scop;
1211 int type_size;
1212 struct pet_array *array;
1213 struct pet_scop *scop_kill;
1215 iv = pet_expr_access_get_id(tree->u.l.iv);
1216 pc = pet_context_clear_value(pc, iv);
1218 declared = tree->u.l.declared;
1220 expr_iv = pet_expr_copy(tree->u.l.iv);
1221 type_size = pet_expr_get_type_size(expr_iv);
1222 init = pet_expr_copy(tree->u.l.init);
1223 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1224 scop_init = scop_from_expr(init, state->n_stmt++,
1225 pet_tree_get_loc(tree), init_pc);
1226 scop_init = pet_scop_prefix(scop_init, declared);
1228 expr_iv = pet_expr_copy(tree->u.l.iv);
1229 type_size = pet_expr_get_type_size(expr_iv);
1230 inc = pet_expr_copy(tree->u.l.inc);
1231 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1233 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1234 pet_tree_get_loc(tree), tree->u.l.body, inc,
1235 pet_context_copy(pc), state);
1237 scop = pet_scop_prefix(scop, declared + 1);
1238 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1240 pet_context_free(pc);
1242 if (!declared)
1243 return scop;
1245 array = extract_array(tree->u.l.iv, init_pc, state);
1246 if (array)
1247 array->declared = 1;
1248 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1249 scop_kill = pet_scop_prefix(scop_kill, 0);
1250 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1251 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1252 scop_kill = pet_scop_add_array(scop_kill, array);
1253 scop_kill = pet_scop_prefix(scop_kill, 3);
1254 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1256 return scop;
1259 /* Given an access expression "expr", is the variable accessed by
1260 * "expr" assigned anywhere inside "tree"?
1262 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1264 int assigned = 0;
1265 isl_id *id;
1267 id = pet_expr_access_get_id(expr);
1268 assigned = pet_tree_writes(tree, id);
1269 isl_id_free(id);
1271 return assigned;
1274 /* Are all nested access parameters in "pa" allowed given "tree".
1275 * In particular, is none of them written by anywhere inside "tree".
1277 * If "tree" has any continue or break nodes in the current loop level,
1278 * then no nested access parameters are allowed.
1279 * In particular, if there is any nested access in a guard
1280 * for a piece of code containing a "continue", then we want to introduce
1281 * a separate statement for evaluating this guard so that we can express
1282 * that the result is false for all previous iterations.
1284 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1285 __isl_keep pet_tree *tree)
1287 int i, nparam;
1289 if (!tree)
1290 return -1;
1292 if (!pet_nested_any_in_pw_aff(pa))
1293 return 1;
1295 if (pet_tree_has_continue_or_break(tree))
1296 return 0;
1298 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1299 for (i = 0; i < nparam; ++i) {
1300 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1301 pet_expr *expr;
1302 int allowed;
1304 if (!pet_nested_in_id(id)) {
1305 isl_id_free(id);
1306 continue;
1309 expr = pet_nested_extract_expr(id);
1310 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1311 !is_assigned(expr, tree);
1313 pet_expr_free(expr);
1314 isl_id_free(id);
1316 if (!allowed)
1317 return 0;
1320 return 1;
1323 /* Internal data structure for collect_local.
1324 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1325 * "local" collects the results.
1327 struct pet_tree_collect_local_data {
1328 pet_context *pc;
1329 struct pet_state *state;
1330 isl_union_set *local;
1333 /* Add the variable accessed by "var" to data->local.
1334 * We extract a representation of the variable from
1335 * the pet_array constructed using extract_array
1336 * to ensure consistency with the rest of the scop.
1338 static int add_local(struct pet_tree_collect_local_data *data,
1339 __isl_keep pet_expr *var)
1341 struct pet_array *array;
1342 isl_set *universe;
1344 array = extract_array(var, data->pc, data->state);
1345 if (!array)
1346 return -1;
1348 universe = isl_set_universe(isl_set_get_space(array->extent));
1349 data->local = isl_union_set_add_set(data->local, universe);
1350 pet_array_free(array);
1352 return 0;
1355 /* If the node "tree" declares a variable, then add it to
1356 * data->local.
1358 static int extract_local_var(__isl_keep pet_tree *tree, void *user)
1360 enum pet_tree_type type;
1361 struct pet_tree_collect_local_data *data = user;
1363 type = pet_tree_get_type(tree);
1364 if (type == pet_tree_decl || type == pet_tree_decl_init)
1365 return add_local(data, tree->u.d.var);
1367 return 0;
1370 /* If the node "tree" is a for loop that declares its induction variable,
1371 * then add it this induction variable to data->local.
1373 static int extract_local_iterator(__isl_keep pet_tree *tree, void *user)
1375 struct pet_tree_collect_local_data *data = user;
1377 if (pet_tree_get_type(tree) == pet_tree_for && tree->u.l.declared)
1378 return add_local(data, tree->u.l.iv);
1380 return 0;
1383 /* Collect and return all local variables of the for loop represented
1384 * by "tree", with "scop" the corresponding pet_scop.
1385 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1387 * We collect not only the variables that are declared inside "tree",
1388 * but also the loop iterators that are declared anywhere inside
1389 * any possible macro statements in "scop".
1390 * The latter also appear as declared variable in the scop,
1391 * whereas other declared loop iterators only appear implicitly
1392 * in the iteration domains.
1394 static __isl_give isl_union_set *collect_local(struct pet_scop *scop,
1395 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1396 struct pet_state *state)
1398 int i;
1399 isl_ctx *ctx;
1400 struct pet_tree_collect_local_data data = { pc, state };
1402 ctx = pet_tree_get_ctx(tree);
1403 data.local = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
1405 if (pet_tree_foreach_sub_tree(tree, &extract_local_var, &data) < 0)
1406 return isl_union_set_free(data.local);
1408 for (i = 0; i < scop->n_stmt; ++i) {
1409 pet_tree *body = scop->stmts[i]->body;
1410 if (pet_tree_foreach_sub_tree(body, &extract_local_iterator,
1411 &data) < 0)
1412 return isl_union_set_free(data.local);
1415 return data.local;
1418 /* Add an independence to "scop" if the for node "tree" was marked
1419 * independent.
1420 * "domain" is the set of loop iterators, with the current for loop
1421 * innermost. If "sign" is positive, then the inner iterator increases.
1422 * Otherwise it decreases.
1423 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1425 * If the tree was marked, then collect all local variables and
1426 * add an independence.
1428 static struct pet_scop *set_independence(struct pet_scop *scop,
1429 __isl_keep pet_tree *tree, __isl_keep isl_set *domain, int sign,
1430 __isl_keep pet_context *pc, struct pet_state *state)
1432 isl_union_set *local;
1434 if (!tree->u.l.independent)
1435 return scop;
1437 local = collect_local(scop, tree, pc, state);
1438 scop = pet_scop_set_independent(scop, domain, local, sign);
1440 return scop;
1443 /* Construct a pet_scop for a for tree with static affine initialization
1444 * and constant increment within the context "pc".
1445 * The domain of "pc" has already been extended with an (at this point
1446 * unbounded) inner loop iterator corresponding to the current for loop.
1448 * The condition is allowed to contain nested accesses, provided
1449 * they are not being written to inside the body of the loop.
1450 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1451 * essentially treated as a while loop, with iteration domain
1452 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1454 * We extract a pet_scop for the body after intersecting the domain of "pc"
1456 * { [l,i] : i >= init and condition' }
1458 * or
1460 * { [l,i] : i <= init and condition' }
1462 * Where condition' is equal to condition if the latter is
1463 * a simple upper [lower] bound and a condition that is extended
1464 * to apply to all previous iterations otherwise.
1465 * Afterwards, the schedule of the pet_scop is extended with
1467 * { [l,i] -> [i] }
1469 * or
1471 * { [l,i] -> [-i] }
1473 * If the condition is non-affine, then we drop the condition from the
1474 * iteration domain and instead create a separate statement
1475 * for evaluating the condition. The body is then filtered to depend
1476 * on the result of the condition evaluating to true on all iterations
1477 * up to the current iteration, while the evaluation the condition itself
1478 * is filtered to depend on the result of the condition evaluating to true
1479 * on all previous iterations.
1480 * The context of the scop representing the body is dropped
1481 * because we don't know how many times the body will be executed,
1482 * if at all.
1484 * If the stride of the loop is not 1, then "i >= init" is replaced by
1486 * (exists a: i = init + stride * a and a >= 0)
1488 * If the loop iterator i is unsigned, then wrapping may occur.
1489 * We therefore use a virtual iterator instead that does not wrap.
1490 * However, the condition in the code applies
1491 * to the wrapped value, so we need to change condition(l,i)
1492 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1493 * to the original iterator by the wrapping of the virtual iterator.
1494 * Note that there may be no need to perform this final wrapping
1495 * if the loop condition (after wrapping) satisfies certain conditions.
1496 * However, the is_simple_bound condition is not enough since it doesn't
1497 * check if there even is an upper bound.
1499 * Wrapping on unsigned iterators can be avoided entirely if
1500 * loop condition is simple, the loop iterator is incremented
1501 * [decremented] by one and the last value before wrapping cannot
1502 * possibly satisfy the loop condition.
1504 * Valid outer iterators for a for loop are those for which the initial
1505 * value itself, the increment on each domain iteration and
1506 * the condition on both the initial value and
1507 * the result of incrementing the iterator for each iteration of the domain
1508 * can be evaluated.
1509 * If the loop condition is non-affine, then we only consider validity
1510 * of the initial value.
1512 * If the body contains any break, then we keep track of it in "skip"
1513 * (if the skip condition is affine) or it is handled in scop_add_break
1514 * (if the skip condition is not affine).
1515 * Note that the affine break condition needs to be considered with
1516 * respect to previous iterations in the virtual domain (if any).
1518 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1519 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1520 __isl_take isl_val *inc, __isl_take pet_context *pc,
1521 struct pet_state *state)
1523 isl_set *domain;
1524 isl_multi_aff *sched;
1525 isl_set *cond = NULL;
1526 isl_set *skip = NULL;
1527 isl_id *id_test = NULL, *id_break_test;
1528 struct pet_scop *scop, *scop_cond = NULL;
1529 int pos;
1530 int is_one;
1531 int is_unsigned;
1532 int is_simple;
1533 int is_virtual;
1534 int is_non_affine;
1535 int has_affine_break;
1536 int has_var_break;
1537 isl_map *rev_wrap = NULL;
1538 isl_map *init_val_map;
1539 isl_pw_aff *pa;
1540 isl_set *valid_init;
1541 isl_set *valid_cond;
1542 isl_set *valid_cond_init;
1543 isl_set *valid_cond_next;
1544 isl_set *valid_inc;
1545 pet_expr *cond_expr;
1546 pet_context *pc_nested;
1548 pos = pet_context_dim(pc) - 1;
1550 domain = pet_context_get_domain(pc);
1551 cond_expr = pet_expr_copy(tree->u.l.cond);
1552 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1553 pc_nested = pet_context_copy(pc);
1554 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1555 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1556 pet_context_free(pc_nested);
1557 pet_expr_free(cond_expr);
1559 valid_inc = isl_pw_aff_domain(pa_inc);
1561 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1563 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1564 !is_nested_allowed(pa, tree->u.l.body);
1565 if (is_non_affine)
1566 pa = isl_pw_aff_free(pa);
1568 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1569 cond = isl_pw_aff_non_zero_set(pa);
1570 if (is_non_affine)
1571 cond = isl_set_universe(isl_set_get_space(domain));
1573 valid_cond = isl_set_coalesce(valid_cond);
1574 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1575 is_virtual = is_unsigned &&
1576 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1578 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1579 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1580 isl_dim_out, 0);
1581 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1582 isl_set_copy(valid_cond));
1583 if (is_one && !is_virtual) {
1584 isl_set *cond;
1586 isl_pw_aff_free(init_val);
1587 pa = pet_expr_extract_comparison(
1588 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1589 tree->u.l.iv, tree->u.l.init, pc);
1590 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1591 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1592 isl_set_dim(domain, isl_dim_set) - 1, 1);
1593 cond = isl_pw_aff_non_zero_set(pa);
1594 domain = isl_set_intersect(domain, cond);
1595 } else {
1596 isl_set *strided;
1598 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1599 strided = strided_domain(init_val, isl_val_copy(inc));
1600 domain = isl_set_intersect(domain, strided);
1603 if (is_virtual) {
1604 isl_multi_aff *wrap;
1605 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1606 pc = pet_context_preimage_domain(pc, wrap);
1607 rev_wrap = isl_map_from_multi_aff(wrap);
1608 rev_wrap = isl_map_reverse(rev_wrap);
1609 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1610 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1611 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1613 is_simple = is_simple_bound(cond, inc);
1614 if (!is_simple) {
1615 cond = isl_set_gist(cond, isl_set_copy(domain));
1616 is_simple = is_simple_bound(cond, inc);
1618 if (!is_simple)
1619 cond = valid_for_each_iteration(cond,
1620 isl_set_copy(domain), isl_val_copy(inc));
1621 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1622 domain = isl_set_intersect(domain, cond);
1623 sched = map_to_last(pc, state->n_loop++);
1624 if (isl_val_is_neg(inc))
1625 sched = isl_multi_aff_neg(sched);
1627 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1628 isl_val_copy(inc));
1629 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1631 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1633 if (is_non_affine) {
1634 isl_space *space;
1635 isl_multi_pw_aff *test_index;
1636 space = isl_set_get_space(domain);
1637 test_index = pet_create_test_index(space, state->n_test++);
1638 scop_cond = scop_from_non_affine_condition(
1639 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1640 isl_multi_pw_aff_copy(test_index),
1641 pet_tree_get_loc(tree), pc);
1642 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1643 isl_dim_out);
1644 scop_cond = pet_scop_add_boolean_array(scop_cond,
1645 isl_set_copy(domain), test_index,
1646 state->int_size);
1647 scop_cond = pet_scop_prefix(scop_cond, 0);
1650 scop = scop_from_tree(tree->u.l.body, pc, state);
1651 has_affine_break = scop &&
1652 pet_scop_has_affine_skip(scop, pet_skip_later);
1653 if (has_affine_break)
1654 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1655 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1656 if (has_var_break)
1657 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1658 if (is_non_affine) {
1659 scop = pet_scop_reset_context(scop);
1660 scop = pet_scop_prefix(scop, 1);
1662 scop = pet_scop_reset_skips(scop);
1663 scop = pet_scop_resolve_nested(scop);
1664 if (has_affine_break) {
1665 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1666 is_virtual, rev_wrap);
1667 scop = pet_scop_intersect_domain_prefix(scop,
1668 isl_set_copy(domain));
1670 isl_map_free(rev_wrap);
1671 if (has_var_break)
1672 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1673 isl_val_copy(inc));
1674 if (is_non_affine)
1675 scop = scop_add_while(scop_cond, scop, id_test,
1676 isl_set_copy(domain),
1677 isl_val_copy(inc));
1678 else
1679 scop = set_independence(scop, tree, domain, isl_val_sgn(inc),
1680 pc, state);
1681 scop = pet_scop_embed(scop, domain, sched);
1682 if (is_non_affine) {
1683 isl_set_free(valid_inc);
1684 } else {
1685 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1686 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1687 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1688 scop = pet_scop_restrict_context(scop, valid_inc);
1691 isl_val_free(inc);
1693 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1694 scop = pet_scop_restrict_context(scop, valid_init);
1696 pet_context_free(pc);
1697 return scop;
1700 /* Construct a pet_scop for a for statement within the context of "pc".
1702 * We update the context to reflect the writes to the loop variable and
1703 * the writes inside the body.
1705 * Then we check if the initialization of the for loop
1706 * is a static affine value and the increment is a constant.
1707 * If so, we construct the pet_scop using scop_from_affine_for.
1708 * Otherwise, we treat the for loop as a while loop
1709 * in scop_from_non_affine_for.
1711 * Note that the initialization and the increment are extracted
1712 * in a context where the current loop iterator has been added
1713 * to the context. If these turn out not be affine, then we
1714 * have reconstruct the body context without an assignment
1715 * to this loop iterator, as this variable will then not be
1716 * treated as a dimension of the iteration domain, but as any
1717 * other variable.
1719 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1720 __isl_keep pet_context *init_pc, struct pet_state *state)
1722 isl_id *iv;
1723 isl_val *inc;
1724 isl_pw_aff *pa_inc, *init_val;
1725 pet_context *pc, *pc_init_val;
1727 if (!tree)
1728 return NULL;
1730 iv = pet_expr_access_get_id(tree->u.l.iv);
1731 pc = pet_context_copy(init_pc);
1732 pc = pet_context_add_inner_iterator(pc, iv);
1733 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1735 pc_init_val = pet_context_copy(pc);
1736 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1737 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1738 pet_context_free(pc_init_val);
1739 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1740 inc = pet_extract_cst(pa_inc);
1741 if (!pa_inc || !init_val || !inc)
1742 goto error;
1743 if (!isl_pw_aff_involves_nan(pa_inc) &&
1744 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1745 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1746 pc, state);
1748 isl_pw_aff_free(pa_inc);
1749 isl_pw_aff_free(init_val);
1750 isl_val_free(inc);
1751 pet_context_free(pc);
1753 pc = pet_context_copy(init_pc);
1754 pc = pet_context_add_infinite_loop(pc);
1755 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1756 return scop_from_non_affine_for(tree, init_pc, pc, state);
1757 error:
1758 isl_pw_aff_free(pa_inc);
1759 isl_pw_aff_free(init_val);
1760 isl_val_free(inc);
1761 pet_context_free(pc);
1762 return NULL;
1765 /* Check whether "expr" is an affine constraint within the context "pc".
1767 static int is_affine_condition(__isl_keep pet_expr *expr,
1768 __isl_keep pet_context *pc)
1770 isl_pw_aff *pa;
1771 int is_affine;
1773 pa = pet_expr_extract_affine_condition(expr, pc);
1774 if (!pa)
1775 return -1;
1776 is_affine = !isl_pw_aff_involves_nan(pa);
1777 isl_pw_aff_free(pa);
1779 return is_affine;
1782 /* Check if the given if statement is a conditional assignement
1783 * with a non-affine condition.
1785 * In particular we check if "stmt" is of the form
1787 * if (condition)
1788 * a = f(...);
1789 * else
1790 * a = g(...);
1792 * where the condition is non-affine and a is some array or scalar access.
1794 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1795 __isl_keep pet_context *pc)
1797 int equal;
1798 isl_ctx *ctx;
1799 pet_expr *expr1, *expr2;
1801 ctx = pet_tree_get_ctx(tree);
1802 if (!pet_options_get_detect_conditional_assignment(ctx))
1803 return 0;
1804 if (tree->type != pet_tree_if_else)
1805 return 0;
1806 if (tree->u.i.then_body->type != pet_tree_expr)
1807 return 0;
1808 if (tree->u.i.else_body->type != pet_tree_expr)
1809 return 0;
1810 expr1 = tree->u.i.then_body->u.e.expr;
1811 expr2 = tree->u.i.else_body->u.e.expr;
1812 if (pet_expr_get_type(expr1) != pet_expr_op)
1813 return 0;
1814 if (pet_expr_get_type(expr2) != pet_expr_op)
1815 return 0;
1816 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1817 return 0;
1818 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1819 return 0;
1820 expr1 = pet_expr_get_arg(expr1, 0);
1821 expr2 = pet_expr_get_arg(expr2, 0);
1822 equal = pet_expr_is_equal(expr1, expr2);
1823 pet_expr_free(expr1);
1824 pet_expr_free(expr2);
1825 if (equal < 0 || !equal)
1826 return 0;
1827 if (is_affine_condition(tree->u.i.cond, pc))
1828 return 0;
1830 return 1;
1833 /* Given that "tree" is of the form
1835 * if (condition)
1836 * a = f(...);
1837 * else
1838 * a = g(...);
1840 * where a is some array or scalar access, construct a pet_scop
1841 * corresponding to this conditional assignment within the context "pc".
1842 * "cond_pa" is an affine expression with nested accesses representing
1843 * the condition.
1845 * The constructed pet_scop then corresponds to the expression
1847 * a = condition ? f(...) : g(...)
1849 * All access relations in f(...) are intersected with condition
1850 * while all access relation in g(...) are intersected with the complement.
1852 static struct pet_scop *scop_from_conditional_assignment(
1853 __isl_keep pet_tree *tree, __isl_take isl_pw_aff *cond_pa,
1854 __isl_take pet_context *pc, struct pet_state *state)
1856 int type_size;
1857 isl_set *cond, *comp;
1858 isl_multi_pw_aff *index;
1859 pet_expr *expr1, *expr2;
1860 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1861 struct pet_scop *scop;
1863 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa));
1864 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa));
1865 index = isl_multi_pw_aff_from_pw_aff(cond_pa);
1867 expr1 = tree->u.i.then_body->u.e.expr;
1868 expr2 = tree->u.i.else_body->u.e.expr;
1870 pe_cond = pet_expr_from_index(index);
1872 pe_then = pet_expr_get_arg(expr1, 1);
1873 pe_then = pet_context_evaluate_expr(pc, pe_then);
1874 pe_then = pet_expr_restrict(pe_then, cond);
1875 pe_else = pet_expr_get_arg(expr2, 1);
1876 pe_else = pet_context_evaluate_expr(pc, pe_else);
1877 pe_else = pet_expr_restrict(pe_else, comp);
1878 pe_write = pet_expr_get_arg(expr1, 0);
1879 pe_write = pet_context_evaluate_expr(pc, pe_write);
1881 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1882 type_size = pet_expr_get_type_size(pe_write);
1883 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1885 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
1886 pet_tree_get_loc(tree), pc);
1888 pet_context_free(pc);
1890 return scop;
1893 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1895 * We create a separate statement that writes the result
1896 * of the non-affine condition to a virtual scalar.
1897 * A constraint requiring the value of this virtual scalar to be one
1898 * is added to the iteration domains of the then branch.
1899 * Similarly, a constraint requiring the value of this virtual scalar
1900 * to be zero is added to the iteration domains of the else branch, if any.
1901 * We combine the schedules as a sequence to ensure that the virtual scalar
1902 * is written before it is read.
1904 * If there are any breaks or continues in the then and/or else
1905 * branches, then we may have to compute a new skip condition.
1906 * This is handled using a pet_skip_info object.
1907 * On initialization, the object checks if skip conditions need
1908 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1909 * adds them in pet_skip_info_if_add.
1911 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1912 __isl_take pet_context *pc, struct pet_state *state)
1914 int has_else;
1915 isl_space *space;
1916 isl_set *domain;
1917 isl_multi_pw_aff *test_index;
1918 struct pet_skip_info skip;
1919 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1921 has_else = tree->type == pet_tree_if_else;
1923 space = pet_context_get_space(pc);
1924 test_index = pet_create_test_index(space, state->n_test++);
1925 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1926 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1927 pet_tree_get_loc(tree), pc);
1928 domain = pet_context_get_domain(pc);
1929 scop = pet_scop_add_boolean_array(scop, domain,
1930 isl_multi_pw_aff_copy(test_index), state->int_size);
1932 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1933 if (has_else)
1934 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1936 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1937 has_else, 0);
1938 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1940 scop = pet_scop_prefix(scop, 0);
1941 scop_then = pet_scop_prefix(scop_then, 1);
1942 scop_then = pet_scop_filter(scop_then,
1943 isl_multi_pw_aff_copy(test_index), 1);
1944 if (has_else) {
1945 scop_else = pet_scop_prefix(scop_else, 1);
1946 scop_else = pet_scop_filter(scop_else, test_index, 0);
1947 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1948 } else
1949 isl_multi_pw_aff_free(test_index);
1951 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1953 scop = pet_skip_info_if_add(&skip, scop, 2);
1955 pet_context_free(pc);
1956 return scop;
1959 /* Construct a pet_scop for an affine if statement within the context "pc".
1961 * The condition is added to the iteration domains of the then branch,
1962 * while the opposite of the condition in added to the iteration domains
1963 * of the else branch, if any.
1965 * If there are any breaks or continues in the then and/or else
1966 * branches, then we may have to compute a new skip condition.
1967 * This is handled using a pet_skip_info_if object.
1968 * On initialization, the object checks if skip conditions need
1969 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1970 * adds them in pet_skip_info_if_add.
1972 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1973 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1974 struct pet_state *state)
1976 int has_else;
1977 isl_ctx *ctx;
1978 isl_set *set, *complement;
1979 isl_set *valid;
1980 struct pet_skip_info skip;
1981 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1982 pet_context *pc_body;
1984 ctx = pet_tree_get_ctx(tree);
1986 has_else = tree->type == pet_tree_if_else;
1988 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1989 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1991 pc_body = pet_context_copy(pc);
1992 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
1993 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
1994 pet_context_free(pc_body);
1995 if (has_else) {
1996 pc_body = pet_context_copy(pc);
1997 complement = isl_set_copy(valid);
1998 complement = isl_set_subtract(valid, isl_set_copy(set));
1999 pc_body = pet_context_intersect_domain(pc_body,
2000 isl_set_copy(complement));
2001 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
2002 pet_context_free(pc_body);
2005 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
2006 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
2007 isl_pw_aff_free(cond);
2009 scop = pet_scop_restrict(scop_then, set);
2011 if (has_else) {
2012 scop_else = pet_scop_restrict(scop_else, complement);
2013 scop = pet_scop_add_par(ctx, scop, scop_else);
2015 scop = pet_scop_resolve_nested(scop);
2016 scop = pet_scop_restrict_context(scop, valid);
2018 if (pet_skip_info_has_skip(&skip))
2019 scop = pet_scop_prefix(scop, 0);
2020 scop = pet_skip_info_if_add(&skip, scop, 1);
2022 pet_context_free(pc);
2023 return scop;
2026 /* Construct a pet_scop for an if statement within the context "pc".
2028 * If the condition fits the pattern of a conditional assignment,
2029 * then it is handled by scop_from_conditional_assignment.
2030 * Note that the condition is only considered for a conditional assignment
2031 * if it is not static-affine. However, it should still convert
2032 * to an affine expression when nesting is allowed.
2034 * Otherwise, we check if the condition is affine.
2035 * If so, we construct the scop in scop_from_affine_if.
2036 * Otherwise, we construct the scop in scop_from_non_affine_if.
2038 * We allow the condition to be dynamic, i.e., to refer to
2039 * scalars or array elements that may be written to outside
2040 * of the given if statement. These nested accesses are then represented
2041 * as output dimensions in the wrapping iteration domain.
2042 * If it is also written _inside_ the then or else branch, then
2043 * we treat the condition as non-affine.
2044 * As explained in extract_non_affine_if, this will introduce
2045 * an extra statement.
2046 * For aesthetic reasons, we want this statement to have a statement
2047 * number that is lower than those of the then and else branches.
2048 * In order to evaluate if we will need such a statement, however, we
2049 * first construct scops for the then and else branches.
2050 * We therefore reserve a statement number if we might have to
2051 * introduce such an extra statement.
2053 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
2054 __isl_keep pet_context *pc, struct pet_state *state)
2056 int has_else;
2057 isl_pw_aff *cond;
2058 pet_expr *cond_expr;
2059 pet_context *pc_nested;
2061 if (!tree)
2062 return NULL;
2064 has_else = tree->type == pet_tree_if_else;
2066 pc = pet_context_copy(pc);
2067 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
2068 if (has_else)
2069 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
2071 cond_expr = pet_expr_copy(tree->u.i.cond);
2072 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
2073 pc_nested = pet_context_copy(pc);
2074 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
2075 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
2076 pet_context_free(pc_nested);
2077 pet_expr_free(cond_expr);
2079 if (!cond) {
2080 pet_context_free(pc);
2081 return NULL;
2084 if (isl_pw_aff_involves_nan(cond)) {
2085 isl_pw_aff_free(cond);
2086 return scop_from_non_affine_if(tree, pc, state);
2089 if (is_conditional_assignment(tree, pc))
2090 return scop_from_conditional_assignment(tree, cond, pc, state);
2092 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
2093 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
2094 isl_pw_aff_free(cond);
2095 return scop_from_non_affine_if(tree, pc, state);
2098 return scop_from_affine_if(tree, cond, pc, state);
2101 /* Return a one-dimensional multi piecewise affine expression that is equal
2102 * to the constant 1 and is defined over the given domain.
2104 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
2106 isl_local_space *ls;
2107 isl_aff *aff;
2109 ls = isl_local_space_from_space(space);
2110 aff = isl_aff_zero_on_domain(ls);
2111 aff = isl_aff_set_constant_si(aff, 1);
2113 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
2116 /* Construct a pet_scop for a continue statement with the given domain space.
2118 * We simply create an empty scop with a universal pet_skip_now
2119 * skip condition. This skip condition will then be taken into
2120 * account by the enclosing loop construct, possibly after
2121 * being incorporated into outer skip conditions.
2123 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
2124 __isl_take isl_space *space)
2126 struct pet_scop *scop;
2128 scop = pet_scop_empty(isl_space_copy(space));
2130 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
2132 return scop;
2135 /* Construct a pet_scop for a break statement with the given domain space.
2137 * We simply create an empty scop with both a universal pet_skip_now
2138 * skip condition and a universal pet_skip_later skip condition.
2139 * These skip conditions will then be taken into
2140 * account by the enclosing loop construct, possibly after
2141 * being incorporated into outer skip conditions.
2143 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
2144 __isl_take isl_space *space)
2146 struct pet_scop *scop;
2147 isl_multi_pw_aff *skip;
2149 scop = pet_scop_empty(isl_space_copy(space));
2151 skip = one_mpa(space);
2152 scop = pet_scop_set_skip(scop, pet_skip_now,
2153 isl_multi_pw_aff_copy(skip));
2154 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
2156 return scop;
2159 /* Extract a clone of the kill statement in "scop".
2160 * The domain of the clone is given by "domain".
2161 * "scop" is expected to have been created from a DeclStmt
2162 * and should have the kill as its first statement.
2164 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
2165 struct pet_scop *scop, struct pet_state *state)
2167 pet_expr *kill;
2168 struct pet_stmt *stmt;
2169 isl_space *space;
2170 isl_multi_pw_aff *mpa;
2171 pet_tree *tree;
2173 if (!domain || !scop)
2174 return NULL;
2175 if (scop->n_stmt < 1)
2176 isl_die(isl_set_get_ctx(domain), isl_error_internal,
2177 "expecting at least one statement", return NULL);
2178 stmt = scop->stmts[0];
2179 if (!pet_stmt_is_kill(stmt))
2180 isl_die(isl_set_get_ctx(domain), isl_error_internal,
2181 "expecting kill statement", return NULL);
2183 kill = pet_tree_expr_get_expr(stmt->body);
2184 space = pet_stmt_get_space(stmt);
2185 space = isl_space_map_from_set(space);
2186 mpa = isl_multi_pw_aff_identity(space);
2187 mpa = isl_multi_pw_aff_reset_tuple_id(mpa, isl_dim_in);
2188 kill = pet_expr_update_domain(kill, mpa);
2189 tree = pet_tree_new_expr(kill);
2190 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
2191 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
2192 state->n_stmt++, tree);
2193 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
2196 /* Does "tree" represent an assignment to a variable?
2198 * The assignment may be one of
2199 * - a declaration with initialization
2200 * - an expression with a top-level assignment operator
2202 static int is_assignment(__isl_keep pet_tree *tree)
2204 if (!tree)
2205 return 0;
2206 if (tree->type == pet_tree_decl_init)
2207 return 1;
2208 return pet_tree_is_assign(tree);
2211 /* Update "pc" by taking into account the assignment performed by "tree",
2212 * where "tree" satisfies is_assignment.
2214 * In particular, if the lhs of the assignment is a scalar variable and
2215 * if the rhs is an affine expression, then keep track of this value in "pc"
2216 * so that we can plug it in when we later come across the same variable.
2218 * Any previously assigned value to the variable has already been removed
2219 * by scop_handle_writes.
2221 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
2222 __isl_keep pet_tree *tree)
2224 pet_expr *var, *val;
2225 isl_id *id;
2226 isl_pw_aff *pa;
2228 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
2229 var = pet_tree_decl_get_var(tree);
2230 val = pet_tree_decl_get_init(tree);
2231 } else {
2232 pet_expr *expr;
2233 expr = pet_tree_expr_get_expr(tree);
2234 var = pet_expr_get_arg(expr, 0);
2235 val = pet_expr_get_arg(expr, 1);
2236 pet_expr_free(expr);
2239 if (!pet_expr_is_scalar_access(var)) {
2240 pet_expr_free(var);
2241 pet_expr_free(val);
2242 return pc;
2245 pa = pet_expr_extract_affine(val, pc);
2246 if (!pa)
2247 pc = pet_context_free(pc);
2249 if (!isl_pw_aff_involves_nan(pa)) {
2250 id = pet_expr_access_get_id(var);
2251 pc = pet_context_set_value(pc, id, pa);
2252 } else {
2253 isl_pw_aff_free(pa);
2255 pet_expr_free(var);
2256 pet_expr_free(val);
2258 return pc;
2261 /* Mark all arrays in "scop" as being exposed.
2263 static struct pet_scop *mark_exposed(struct pet_scop *scop)
2265 int i;
2267 if (!scop)
2268 return NULL;
2269 for (i = 0; i < scop->n_array; ++i)
2270 scop->arrays[i]->exposed = 1;
2271 return scop;
2274 /* Try and construct a pet_scop corresponding to (part of)
2275 * a sequence of statements within the context "pc".
2277 * After extracting a statement, we update "pc"
2278 * based on the top-level assignments in the statement
2279 * so that we can exploit them in subsequent statements in the same block.
2281 * If there are any breaks or continues in the individual statements,
2282 * then we may have to compute a new skip condition.
2283 * This is handled using a pet_skip_info object.
2284 * On initialization, the object checks if skip conditions need
2285 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2286 * adds them in pet_skip_info_seq_add.
2288 * If "block" is set, then we need to insert kill statements at
2289 * the end of the block for any array that has been declared by
2290 * one of the statements in the sequence. Each of these declarations
2291 * results in the construction of a kill statement at the place
2292 * of the declaration, so we simply collect duplicates of
2293 * those kill statements and append these duplicates to the constructed scop.
2295 * If "block" is not set, then any array declared by one of the statements
2296 * in the sequence is marked as being exposed.
2298 * If autodetect is set, then we allow the extraction of only a subrange
2299 * of the sequence of statements. However, if there is at least one statement
2300 * for which we could not construct a scop and the final range contains
2301 * either no statements or at least one kill, then we discard the entire
2302 * range.
2304 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2305 __isl_keep pet_context *pc, struct pet_state *state)
2307 int i;
2308 isl_ctx *ctx;
2309 isl_space *space;
2310 isl_set *domain;
2311 struct pet_scop *scop, *kills;
2313 ctx = pet_tree_get_ctx(tree);
2315 space = pet_context_get_space(pc);
2316 domain = pet_context_get_domain(pc);
2317 pc = pet_context_copy(pc);
2318 scop = pet_scop_empty(isl_space_copy(space));
2319 kills = pet_scop_empty(space);
2320 for (i = 0; i < tree->u.b.n; ++i) {
2321 struct pet_scop *scop_i;
2323 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2324 pc = apply_affine_continue(pc, scop);
2325 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2326 pc = scop_handle_writes(scop_i, pc);
2327 if (is_assignment(tree->u.b.child[i]))
2328 pc = handle_assignment(pc, tree->u.b.child[i]);
2329 struct pet_skip_info skip;
2330 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2331 pet_skip_info_seq_extract(&skip, pc, state);
2332 if (pet_skip_info_has_skip(&skip))
2333 scop_i = pet_scop_prefix(scop_i, 0);
2334 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
2335 if (tree->u.b.block) {
2336 struct pet_scop *kill;
2337 kill = extract_kill(domain, scop_i, state);
2338 kills = pet_scop_add_par(ctx, kills, kill);
2339 } else
2340 scop_i = mark_exposed(scop_i);
2342 scop_i = pet_scop_prefix(scop_i, i);
2343 scop = pet_scop_add_seq(ctx, scop, scop_i);
2345 scop = pet_skip_info_seq_add(&skip, scop, i);
2347 if (!scop)
2348 break;
2350 isl_set_free(domain);
2352 kills = pet_scop_prefix(kills, tree->u.b.n);
2353 scop = pet_scop_add_seq(ctx, scop, kills);
2355 pet_context_free(pc);
2357 return scop;
2360 /* Internal data structure for extract_declared_arrays.
2362 * "pc" and "state" are used to create pet_array objects and kill statements.
2363 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2364 * found any declared array.
2365 * "scop" has been initialized by the caller and is used to attach
2366 * the created pet_array objects.
2367 * "kill_before" and "kill_after" are created and updated by
2368 * extract_declared_arrays to collect the kills of the arrays.
2370 struct pet_tree_extract_declared_arrays_data {
2371 pet_context *pc;
2372 struct pet_state *state;
2374 isl_ctx *ctx;
2376 int any;
2377 struct pet_scop *scop;
2378 struct pet_scop *kill_before;
2379 struct pet_scop *kill_after;
2382 /* Check if the node "node" declares any array or scalar.
2383 * If so, create the corresponding pet_array and attach it to data->scop.
2384 * Additionally, create two kill statements for the array and add them
2385 * to data->kill_before and data->kill_after.
2387 static int extract_declared_arrays(__isl_keep pet_tree *node, void *user)
2389 enum pet_tree_type type;
2390 struct pet_tree_extract_declared_arrays_data *data = user;
2391 struct pet_array *array;
2392 struct pet_scop *scop_kill;
2393 pet_expr *var;
2395 type = pet_tree_get_type(node);
2396 if (type == pet_tree_decl || type == pet_tree_decl_init)
2397 var = node->u.d.var;
2398 else if (type == pet_tree_for && node->u.l.declared)
2399 var = node->u.l.iv;
2400 else
2401 return 0;
2403 array = extract_array(var, data->pc, data->state);
2404 if (array)
2405 array->declared = 1;
2406 data->scop = pet_scop_add_array(data->scop, array);
2408 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2409 if (!data->any)
2410 data->kill_before = scop_kill;
2411 else
2412 data->kill_before = pet_scop_add_par(data->ctx,
2413 data->kill_before, scop_kill);
2415 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2416 if (!data->any)
2417 data->kill_after = scop_kill;
2418 else
2419 data->kill_after = pet_scop_add_par(data->ctx,
2420 data->kill_after, scop_kill);
2422 data->any = 1;
2424 return 0;
2427 /* Convert a pet_tree that consists of more than a single leaf
2428 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2429 * Do so within the context of "pc".
2431 * After constructing the core scop, we also look for any arrays (or scalars)
2432 * that are declared inside "tree". Each of those arrays is marked as
2433 * having been declared and kill statements for these arrays
2434 * are introduced before and after the core scop.
2435 * Note that the input tree is not a leaf so that the declaration
2436 * cannot occur at the outer level.
2438 static struct pet_scop *scop_from_tree_macro(__isl_take pet_tree *tree,
2439 __isl_take isl_id *label, __isl_keep pet_context *pc,
2440 struct pet_state *state)
2442 struct pet_tree_extract_declared_arrays_data data = { pc, state };
2444 data.scop = scop_from_unevaluated_tree(pet_tree_copy(tree),
2445 state->n_stmt++, pc);
2447 data.any = 0;
2448 data.ctx = pet_context_get_ctx(pc);
2449 if (pet_tree_foreach_sub_tree(tree, &extract_declared_arrays,
2450 &data) < 0)
2451 data.scop = pet_scop_free(data.scop);
2452 pet_tree_free(tree);
2454 if (!data.any)
2455 return data.scop;
2457 data.kill_before = pet_scop_prefix(data.kill_before, 0);
2458 data.scop = pet_scop_prefix(data.scop, 1);
2459 data.kill_after = pet_scop_prefix(data.kill_after, 2);
2461 data.scop = pet_scop_add_seq(data.ctx, data.kill_before, data.scop);
2462 data.scop = pet_scop_add_seq(data.ctx, data.scop, data.kill_after);
2464 return data.scop;
2467 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2468 * within the context "pc" by calling the appropriate function
2469 * based on the type of "tree".
2471 * If the initially constructed pet_scop turns out to involve
2472 * dynamic control and if the user has requested an encapsulation
2473 * of all dynamic control, then this pet_scop is discarded and
2474 * a new pet_scop is created with a single statement representing
2475 * the entire "tree".
2476 * However, if the scop contains any active continue or break,
2477 * then we need to include the loop containing the continue or break
2478 * in the encapsulation. We therefore postpone the encapsulation
2479 * until we have constructed a pet_scop for this enclosing loop.
2481 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2482 __isl_keep pet_context *pc, struct pet_state *state)
2484 isl_ctx *ctx;
2485 struct pet_scop *scop = NULL;
2487 if (!tree)
2488 return NULL;
2490 ctx = pet_tree_get_ctx(tree);
2491 switch (tree->type) {
2492 case pet_tree_error:
2493 return NULL;
2494 case pet_tree_block:
2495 return scop_from_block(tree, pc, state);
2496 case pet_tree_break:
2497 return scop_from_break(tree, pet_context_get_space(pc));
2498 case pet_tree_continue:
2499 return scop_from_continue(tree, pet_context_get_space(pc));
2500 case pet_tree_decl:
2501 case pet_tree_decl_init:
2502 return scop_from_decl(tree, pc, state);
2503 case pet_tree_expr:
2504 return scop_from_tree_expr(tree, pc, state);
2505 case pet_tree_if:
2506 case pet_tree_if_else:
2507 scop = scop_from_if(tree, pc, state);
2508 break;
2509 case pet_tree_for:
2510 scop = scop_from_for(tree, pc, state);
2511 break;
2512 case pet_tree_while:
2513 scop = scop_from_while(tree, pc, state);
2514 break;
2515 case pet_tree_infinite_loop:
2516 scop = scop_from_infinite_for(tree, pc, state);
2517 break;
2520 if (!scop)
2521 return NULL;
2523 if (!pet_options_get_encapsulate_dynamic_control(ctx) ||
2524 !pet_scop_has_data_dependent_conditions(scop) ||
2525 pet_scop_has_var_skip(scop, pet_skip_now))
2526 return scop;
2528 pet_scop_free(scop);
2529 return scop_from_tree_macro(pet_tree_copy(tree),
2530 isl_id_copy(tree->label), pc, state);
2533 /* If "tree" has a label that is of the form S_<nr>, then make
2534 * sure that state->n_stmt is greater than nr to ensure that
2535 * we will not generate S_<nr> ourselves.
2537 static int set_first_stmt(__isl_keep pet_tree *tree, void *user)
2539 struct pet_state *state = user;
2540 const char *name;
2541 int nr;
2543 if (!tree)
2544 return -1;
2545 if (!tree->label)
2546 return 0;
2547 name = isl_id_get_name(tree->label);
2548 if (strncmp(name, "S_", 2) != 0)
2549 return 0;
2550 nr = atoi(name + 2);
2551 if (nr >= state->n_stmt)
2552 state->n_stmt = nr + 1;
2554 return 0;
2557 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2558 * "int_size" is the number of bytes need to represent an integer.
2559 * "extract_array" is a callback that we can use to create a pet_array
2560 * that corresponds to the variable accessed by an expression.
2562 * Initialize the global state, construct a context and then
2563 * construct the pet_scop by recursively visiting the tree.
2565 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2567 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2568 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2569 __isl_keep pet_context *pc, void *user), void *user,
2570 __isl_keep pet_context *pc)
2572 struct pet_scop *scop;
2573 struct pet_state state = { 0 };
2575 if (!tree)
2576 return NULL;
2578 state.ctx = pet_tree_get_ctx(tree);
2579 state.int_size = int_size;
2580 state.extract_array = extract_array;
2581 state.user = user;
2582 if (pet_tree_foreach_sub_tree(tree, &set_first_stmt, &state) < 0)
2583 tree = pet_tree_free(tree);
2585 scop = scop_from_tree(tree, pc, &state);
2586 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2588 pet_tree_free(tree);
2590 if (scop)
2591 scop->context = isl_set_params(scop->context);
2593 return scop;