scop.c: expr_collect_accesses: drop unused variable
[pet.git] / tree2scop.c
blob93a00a47810a22b1c457695ac71d75b0e03e383b
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.h>
39 #include <isl/val.h>
40 #include <isl/space.h>
41 #include <isl/local_space.h>
42 #include <isl/aff.h>
43 #include <isl/id_to_pw_aff.h>
44 #include <isl/set.h>
45 #include <isl/map.h>
46 #include <isl/union_set.h>
48 #include "aff.h"
49 #include "expr.h"
50 #include "expr_arg.h"
51 #include "nest.h"
52 #include "scop.h"
53 #include "skip.h"
54 #include "state.h"
55 #include "tree2scop.h"
57 /* If "stmt" is an affine assumption, then record the assumption in "pc".
59 static __isl_give pet_context *add_affine_assumption(struct pet_stmt *stmt,
60 __isl_take pet_context *pc)
62 isl_bool affine;
63 isl_set *cond;
65 affine = pet_stmt_is_affine_assume(stmt);
66 if (affine < 0)
67 return pet_context_free(pc);
68 if (!affine)
69 return pc;
70 cond = pet_stmt_assume_get_affine_condition(stmt);
71 cond = isl_set_reset_tuple_id(cond);
72 pc = pet_context_intersect_domain(pc, cond);
73 return pc;
76 /* Given a scop "scop" derived from an assumption statement,
77 * record the assumption in "pc", if it is affine.
78 * Note that "scop" should consist of exactly one statement.
80 static __isl_give pet_context *scop_add_affine_assumption(
81 __isl_keep pet_scop *scop, __isl_take pet_context *pc)
83 int i;
85 if (!scop)
86 return pet_context_free(pc);
87 for (i = 0; i < scop->n_stmt; ++i)
88 pc = add_affine_assumption(scop->stmts[i], pc);
90 return pc;
93 /* Update "pc" by taking into account the writes in "stmt".
94 * That is, clear any previously assigned values to variables
95 * that are written by "stmt".
97 static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
98 __isl_take pet_context *pc)
100 return pet_context_clear_writes_in_tree(pc, stmt->body);
103 /* Update "pc" based on the write accesses in "scop".
105 static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
106 __isl_take pet_context *pc)
108 int i;
110 if (!scop)
111 return pet_context_free(pc);
112 for (i = 0; i < scop->n_stmt; ++i)
113 pc = handle_writes(scop->stmts[i], pc);
115 return pc;
118 /* Wrapper around pet_expr_resolve_assume
119 * for use as a callback to pet_tree_map_expr.
121 static __isl_give pet_expr *resolve_assume(__isl_take pet_expr *expr,
122 void *user)
124 pet_context *pc = user;
126 return pet_expr_resolve_assume(expr, pc);
129 /* Check if any expression inside "tree" is an assume expression and
130 * if its single argument can be converted to an affine expression
131 * in the context of "pc".
132 * If so, replace the argument by the affine expression.
134 __isl_give pet_tree *pet_tree_resolve_assume(__isl_take pet_tree *tree,
135 __isl_keep pet_context *pc)
137 return pet_tree_map_expr(tree, &resolve_assume, pc);
140 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
141 * "tree" has already been evaluated in the context of "pc".
142 * This mainly involves resolving nested expression parameters
143 * and setting the name of the iteration space.
144 * The name is given by tree->label if it is non-NULL. Otherwise,
145 * it is of the form S_<stmt_nr>.
147 static struct pet_scop *scop_from_evaluated_tree(__isl_take pet_tree *tree,
148 int stmt_nr, __isl_keep pet_context *pc)
150 isl_space *space;
151 isl_set *domain;
152 struct pet_stmt *ps;
154 space = pet_context_get_space(pc);
156 tree = pet_tree_resolve_nested(tree, space);
157 tree = pet_tree_resolve_assume(tree, pc);
159 domain = pet_context_get_domain(pc);
160 ps = pet_stmt_from_pet_tree(domain, stmt_nr, tree);
161 return pet_scop_from_pet_stmt(space, ps);
164 /* Convert a top-level pet_expr to a pet_scop with one statement
165 * within the context "pc".
166 * "expr" has already been evaluated in the context of "pc".
167 * We construct a pet_tree from "expr" and continue with
168 * scop_from_evaluated_tree.
169 * The name is of the form S_<stmt_nr>.
170 * The location of the statement is set to "loc".
172 static struct pet_scop *scop_from_evaluated_expr(__isl_take pet_expr *expr,
173 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
175 pet_tree *tree;
177 tree = pet_tree_new_expr(expr);
178 tree = pet_tree_set_loc(tree, loc);
179 return scop_from_evaluated_tree(tree, stmt_nr, pc);
182 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
183 * "tree" has not yet been evaluated in the context of "pc".
184 * We evaluate "tree" in the context of "pc" and continue with
185 * scop_from_evaluated_tree.
186 * The statement name is given by tree->label if it is non-NULL. Otherwise,
187 * it is of the form S_<stmt_nr>.
189 static struct pet_scop *scop_from_unevaluated_tree(__isl_take pet_tree *tree,
190 int stmt_nr, __isl_keep pet_context *pc)
192 tree = pet_context_evaluate_tree(pc, tree);
193 return scop_from_evaluated_tree(tree, stmt_nr, pc);
196 /* Convert a top-level pet_expr to a pet_scop with one statement
197 * within the context "pc", where "expr" has not yet been evaluated
198 * in the context of "pc".
199 * We construct a pet_tree from "expr" and continue with
200 * scop_from_unevaluated_tree.
201 * The statement name is of the form S_<stmt_nr>.
202 * The location of the statement is set to "loc".
204 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
205 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
207 pet_tree *tree;
209 tree = pet_tree_new_expr(expr);
210 tree = pet_tree_set_loc(tree, loc);
211 return scop_from_unevaluated_tree(tree, stmt_nr, pc);
214 /* Construct a pet_scop with a single statement killing the entire
215 * array "array".
216 * The location of the statement is set to "loc".
218 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
219 __isl_keep pet_context *pc, struct pet_state *state)
221 isl_ctx *ctx;
222 isl_id *id;
223 isl_space *space;
224 isl_multi_pw_aff *index;
225 isl_map *access;
226 pet_expr *expr;
227 struct pet_scop *scop;
229 if (!array)
230 goto error;
231 ctx = isl_set_get_ctx(array->extent);
232 access = isl_map_from_range(isl_set_copy(array->extent));
233 id = isl_set_get_tuple_id(array->extent);
234 space = isl_space_alloc(ctx, 0, 0, 0);
235 space = isl_space_set_tuple_id(space, isl_dim_out, id);
236 index = isl_multi_pw_aff_zero(space);
237 expr = pet_expr_kill_from_access_and_index(access, index);
238 return scop_from_expr(expr, state->n_stmt++, loc, pc);
239 error:
240 pet_loc_free(loc);
241 return NULL;
244 /* Construct and return a pet_array corresponding to the variable
245 * accessed by "access" by calling the extract_array callback.
247 static struct pet_array *extract_array(__isl_keep pet_expr *access,
248 __isl_keep pet_context *pc, struct pet_state *state)
250 return state->extract_array(access, pc, state->user);
253 /* Construct a pet_scop for a (single) variable declaration
254 * within the context "pc".
256 * The scop contains the variable being declared (as an array)
257 * and a statement killing the array.
259 * If the declaration comes with an initialization, then the scop
260 * also contains an assignment to the variable.
262 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
263 __isl_keep pet_context *pc, struct pet_state *state)
265 int type_size;
266 isl_ctx *ctx;
267 struct pet_array *array;
268 struct pet_scop *scop_decl, *scop;
269 pet_expr *lhs, *rhs, *pe;
271 array = extract_array(tree->u.d.var, pc, state);
272 if (array)
273 array->declared = 1;
274 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
275 scop_decl = pet_scop_add_array(scop_decl, array);
277 if (tree->type != pet_tree_decl_init)
278 return scop_decl;
280 lhs = pet_expr_copy(tree->u.d.var);
281 rhs = pet_expr_copy(tree->u.d.init);
282 type_size = pet_expr_get_type_size(lhs);
283 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
284 scop = scop_from_expr(pe, state->n_stmt++, pet_tree_get_loc(tree), pc);
286 ctx = pet_tree_get_ctx(tree);
287 scop = pet_scop_add_seq(ctx, scop_decl, scop);
289 return scop;
292 /* Does "tree" represent a kill statement?
293 * That is, is it an expression statement that "calls" __pencil_kill?
295 static int is_pencil_kill(__isl_keep pet_tree *tree)
297 pet_expr *expr;
298 const char *name;
300 if (!tree)
301 return -1;
302 if (tree->type != pet_tree_expr)
303 return 0;
304 expr = tree->u.e.expr;
305 if (pet_expr_get_type(expr) != pet_expr_call)
306 return 0;
307 name = pet_expr_call_get_name(expr);
308 if (!name)
309 return -1;
310 return !strcmp(name, "__pencil_kill");
313 /* Add a kill to "scop" that kills what is accessed by
314 * the access expression "expr".
316 * Mark the access as a write prior to evaluation to avoid
317 * the access being replaced by a possible known value
318 * during the evaluation.
320 * If the access expression has any arguments (after evaluation
321 * in the context of "pc"), then we ignore it, since we cannot
322 * tell which elements are definitely killed.
324 * Otherwise, we extend the index expression to the dimension
325 * of the accessed array and intersect with the extent of the array and
326 * add a kill expression that kills these array elements is added to "scop".
328 static struct pet_scop *scop_add_kill(struct pet_scop *scop,
329 __isl_take pet_expr *expr, __isl_take pet_loc *loc,
330 __isl_keep pet_context *pc, struct pet_state *state)
332 int dim1, dim2;
333 isl_id *id;
334 isl_multi_pw_aff *index;
335 isl_map *map;
336 pet_expr *kill;
337 struct pet_array *array;
338 struct pet_scop *scop_i;
340 expr = pet_expr_access_set_write(expr, 1);
341 expr = pet_context_evaluate_expr(pc, expr);
342 if (!expr)
343 goto error;
344 if (expr->n_arg != 0) {
345 pet_loc_free(loc);
346 pet_expr_free(expr);
347 return scop;
349 array = extract_array(expr, pc, state);
350 if (!array)
351 goto error;
352 index = pet_expr_access_get_index(expr);
353 pet_expr_free(expr);
354 map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
355 id = isl_map_get_tuple_id(map, isl_dim_out);
356 dim1 = isl_set_dim(array->extent, isl_dim_set);
357 dim2 = isl_map_dim(map, isl_dim_out);
358 map = isl_map_add_dims(map, isl_dim_out, dim1 - dim2);
359 map = isl_map_set_tuple_id(map, isl_dim_out, id);
360 map = isl_map_intersect_range(map, isl_set_copy(array->extent));
361 pet_array_free(array);
362 kill = pet_expr_kill_from_access_and_index(map, index);
363 scop_i = scop_from_evaluated_expr(kill, state->n_stmt++, loc, pc);
364 scop = pet_scop_add_par(state->ctx, scop, scop_i);
366 return scop;
367 error:
368 pet_expr_free(expr);
369 pet_loc_free(loc);
370 return pet_scop_free(scop);
373 /* For each argument of the __pencil_kill call in "tree" that
374 * represents an access, add a kill statement to "scop" killing the accessed
375 * elements.
377 static struct pet_scop *scop_from_pencil_kill(__isl_keep pet_tree *tree,
378 __isl_keep pet_context *pc, struct pet_state *state)
380 pet_expr *call;
381 struct pet_scop *scop;
382 int i, n;
384 call = tree->u.e.expr;
386 scop = pet_scop_empty(pet_context_get_space(pc));
388 n = pet_expr_get_n_arg(call);
389 for (i = 0; i < n; ++i) {
390 pet_expr *arg;
391 pet_loc *loc;
393 arg = pet_expr_get_arg(call, i);
394 if (!arg)
395 return pet_scop_free(scop);
396 if (pet_expr_get_type(arg) != pet_expr_access) {
397 pet_expr_free(arg);
398 continue;
400 loc = pet_tree_get_loc(tree);
401 scop = scop_add_kill(scop, arg, loc, pc, state);
404 return scop;
407 /* Construct a pet_scop for an expression statement within the context "pc".
409 * If the expression calls __pencil_kill, then it needs to be converted
410 * into zero or more kill statements.
411 * Otherwise, a scop is extracted directly from the tree.
413 static struct pet_scop *scop_from_tree_expr(__isl_keep pet_tree *tree,
414 __isl_keep pet_context *pc, struct pet_state *state)
416 int is_kill;
418 is_kill = is_pencil_kill(tree);
419 if (is_kill < 0)
420 return NULL;
421 if (is_kill)
422 return scop_from_pencil_kill(tree, pc, state);
423 return scop_from_unevaluated_tree(pet_tree_copy(tree),
424 state->n_stmt++, pc);
427 /* Return those elements in the space of "cond" that come after
428 * (based on "sign") an element in "cond" in the final dimension.
430 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
432 isl_space *space;
433 isl_map *previous_to_this;
434 int i, dim;
436 dim = isl_set_dim(cond, isl_dim_set);
437 space = isl_space_map_from_set(isl_set_get_space(cond));
438 previous_to_this = isl_map_universe(space);
439 for (i = 0; i + 1 < dim; ++i)
440 previous_to_this = isl_map_equate(previous_to_this,
441 isl_dim_in, i, isl_dim_out, i);
442 if (sign > 0)
443 previous_to_this = isl_map_order_lt(previous_to_this,
444 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
445 else
446 previous_to_this = isl_map_order_gt(previous_to_this,
447 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
449 cond = isl_set_apply(cond, previous_to_this);
451 return cond;
454 /* Remove those iterations of "domain" that have an earlier iteration
455 * (based on "sign") in the final dimension where "skip" is satisfied.
456 * If "apply_skip_map" is set, then "skip_map" is first applied
457 * to the embedded skip condition before removing it from the domain.
459 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
460 __isl_take isl_set *skip, int sign,
461 int apply_skip_map, __isl_keep isl_map *skip_map)
463 if (apply_skip_map)
464 skip = isl_set_apply(skip, isl_map_copy(skip_map));
465 skip = isl_set_intersect(skip , isl_set_copy(domain));
466 return isl_set_subtract(domain, after(skip, sign));
469 /* Create a single-dimensional multi-affine expression on the domain space
470 * of "pc" that is equal to the final dimension of this domain.
471 * "loop_nr" is the sequence number of the corresponding loop.
472 * If "id" is not NULL, then it is used as the output tuple name.
473 * Otherwise, the name is constructed as L_<loop_nr>.
475 static __isl_give isl_multi_aff *map_to_last(__isl_keep pet_context *pc,
476 int loop_nr, __isl_keep isl_id *id)
478 int pos;
479 isl_space *space;
480 isl_local_space *ls;
481 isl_aff *aff;
482 isl_multi_aff *ma;
483 char name[50];
484 isl_id *label;
486 space = pet_context_get_space(pc);
487 pos = isl_space_dim(space, isl_dim_set) - 1;
488 ls = isl_local_space_from_space(space);
489 aff = isl_aff_var_on_domain(ls, isl_dim_set, pos);
490 ma = isl_multi_aff_from_aff(aff);
492 if (id) {
493 label = isl_id_copy(id);
494 } else {
495 snprintf(name, sizeof(name), "L_%d", loop_nr);
496 label = isl_id_alloc(pet_context_get_ctx(pc), name, NULL);
498 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, label);
500 return ma;
503 /* Create an affine expression that maps elements
504 * of an array "id_test" to the previous element in the final dimension
505 * (according to "inc"), provided this element belongs to "domain".
506 * That is, create the affine expression
508 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
510 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
511 __isl_take isl_set *domain, __isl_take isl_val *inc)
513 int pos;
514 isl_space *space;
515 isl_aff *aff;
516 isl_pw_aff *pa;
517 isl_multi_aff *ma;
518 isl_multi_pw_aff *prev;
520 pos = isl_set_dim(domain, isl_dim_set) - 1;
521 space = isl_set_get_space(domain);
522 space = isl_space_map_from_set(space);
523 ma = isl_multi_aff_identity(space);
524 aff = isl_multi_aff_get_aff(ma, pos);
525 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
526 ma = isl_multi_aff_set_aff(ma, pos, aff);
527 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
528 prev = isl_multi_pw_aff_from_multi_aff(ma);
529 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
530 pa = isl_pw_aff_intersect_domain(pa, domain);
531 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
532 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
534 return prev;
537 /* Add an implication to "scop" expressing that if an element of
538 * virtual array "id_test" has value "satisfied" then all previous elements
539 * of this array (in the final dimension) also have that value.
540 * The set of previous elements is bounded by "domain".
541 * If "sign" is negative then the iterator
542 * is decreasing and we express that all subsequent array elements
543 * (but still defined previously) have the same value.
545 static struct pet_scop *add_implication(struct pet_scop *scop,
546 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
547 int satisfied)
549 int i, dim;
550 isl_space *space;
551 isl_map *map;
553 dim = isl_set_dim(domain, isl_dim_set);
554 domain = isl_set_set_tuple_id(domain, id_test);
555 space = isl_space_map_from_set(isl_set_get_space(domain));
556 map = isl_map_universe(space);
557 for (i = 0; i + 1 < dim; ++i)
558 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
559 if (sign > 0)
560 map = isl_map_order_ge(map,
561 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
562 else
563 map = isl_map_order_le(map,
564 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
565 map = isl_map_intersect_range(map, domain);
566 scop = pet_scop_add_implication(scop, map, satisfied);
568 return scop;
571 /* Add a filter to "scop" that imposes that it is only executed
572 * when the variable identified by "id_test" has a zero value
573 * for all previous iterations of "domain".
575 * In particular, add a filter that imposes that the array
576 * has a zero value at the previous iteration of domain and
577 * add an implication that implies that it then has that
578 * value for all previous iterations.
580 static struct pet_scop *scop_add_break(struct pet_scop *scop,
581 __isl_take isl_id *id_test, __isl_take isl_set *domain,
582 __isl_take isl_val *inc)
584 isl_multi_pw_aff *prev;
585 int sign = isl_val_sgn(inc);
587 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
588 scop = add_implication(scop, id_test, domain, sign, 0);
589 scop = pet_scop_filter(scop, prev, 0);
591 return scop;
594 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
595 __isl_keep pet_context *pc, struct pet_state *state);
597 /* Construct a pet_scop for an infinite loop around the given body
598 * within the context "pc".
599 * "loop_id" is the label on the loop or NULL if there is no such label.
601 * The domain of "pc" has already been extended with an infinite loop
603 * { [t] : t >= 0 }
605 * We extract a pet_scop for the body and then embed it in a loop with
606 * schedule
608 * { [outer,t] -> [t] }
610 * If the body contains any break, then it is taken into
611 * account in apply_affine_break (if the skip condition is affine)
612 * or in scop_add_break (if the skip condition is not affine).
614 * Note that in case of an affine skip condition,
615 * since we are dealing with a loop without loop iterator,
616 * the skip condition cannot refer to the current loop iterator and
617 * so effectively, the effect on the iteration domain is of the form
619 * { [outer,0]; [outer,t] : t >= 1 and not skip }
621 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
622 __isl_keep isl_id *loop_id, __isl_keep pet_context *pc,
623 struct pet_state *state)
625 isl_ctx *ctx;
626 isl_id *id_test;
627 isl_set *domain;
628 isl_set *skip;
629 isl_multi_aff *sched;
630 struct pet_scop *scop;
631 int has_affine_break;
632 int has_var_break;
634 ctx = pet_tree_get_ctx(body);
635 domain = pet_context_get_domain(pc);
636 sched = map_to_last(pc, state->n_loop++, loop_id);
638 scop = scop_from_tree(body, pc, state);
640 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
641 if (has_affine_break)
642 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
643 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
644 if (has_var_break)
645 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
647 scop = pet_scop_reset_skips(scop);
648 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
649 if (has_affine_break) {
650 domain = apply_affine_break(domain, skip, 1, 0, NULL);
651 scop = pet_scop_intersect_domain_prefix(scop,
652 isl_set_copy(domain));
654 if (has_var_break)
655 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
656 else
657 isl_set_free(domain);
659 return scop;
662 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
664 * for (;;)
665 * body
667 * within the context "pc".
669 * Extend the domain of "pc" with an extra inner loop
671 * { [t] : t >= 0 }
673 * and construct the scop in scop_from_infinite_loop.
675 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
676 __isl_keep pet_context *pc, struct pet_state *state)
678 struct pet_scop *scop;
680 pc = pet_context_copy(pc);
681 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
683 pc = pet_context_add_infinite_loop(pc);
685 scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
687 pet_context_free(pc);
689 return scop;
692 /* Construct a pet_scop for a while loop of the form
694 * while (pa)
695 * body
697 * within the context "pc".
699 * The domain of "pc" has already been extended with an infinite loop
701 * { [t] : t >= 0 }
703 * Here, we add the constraints on the outer loop iterators
704 * implied by "pa" and construct the scop in scop_from_infinite_loop.
705 * Note that the intersection with these constraints
706 * may result in an empty loop.
708 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
709 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
710 struct pet_state *state)
712 struct pet_scop *scop;
713 isl_set *dom, *local;
714 isl_set *valid;
716 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
717 dom = isl_pw_aff_non_zero_set(pa);
718 local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
719 pc = pet_context_intersect_domain(pc, local);
720 scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
721 scop = pet_scop_restrict(scop, dom);
722 scop = pet_scop_restrict_context(scop, valid);
724 pet_context_free(pc);
725 return scop;
728 /* Construct a scop for a while, given the scops for the condition
729 * and the body, the filter identifier and the iteration domain of
730 * the while loop.
732 * In particular, the scop for the condition is filtered to depend
733 * on "id_test" evaluating to true for all previous iterations
734 * of the loop, while the scop for the body is filtered to depend
735 * on "id_test" evaluating to true for all iterations up to the
736 * current iteration.
737 * The actual filter only imposes that this virtual array has
738 * value one on the previous or the current iteration.
739 * The fact that this condition also applies to the previous
740 * iterations is enforced by an implication.
742 * These filtered scops are then combined into a single scop,
743 * with the condition scop scheduled before the body scop.
745 * "sign" is positive if the iterator increases and negative
746 * if it decreases.
748 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
749 struct pet_scop *scop_body, __isl_take isl_id *id_test,
750 __isl_take isl_set *domain, __isl_take isl_val *inc)
752 isl_ctx *ctx = isl_set_get_ctx(domain);
753 isl_space *space;
754 isl_multi_pw_aff *test_index;
755 isl_multi_pw_aff *prev;
756 int sign = isl_val_sgn(inc);
757 struct pet_scop *scop;
759 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
760 scop_cond = pet_scop_filter(scop_cond, prev, 1);
762 space = isl_space_map_from_set(isl_set_get_space(domain));
763 test_index = isl_multi_pw_aff_identity(space);
764 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
765 isl_id_copy(id_test));
766 scop_body = pet_scop_filter(scop_body, test_index, 1);
768 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
769 scop = add_implication(scop, id_test, domain, sign, 1);
771 return scop;
774 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
775 * evaluating "cond" and writing the result to a virtual scalar,
776 * as expressed by "index".
777 * The expression "cond" has not yet been evaluated in the context of "pc".
778 * Do so within the context "pc".
779 * The location of the statement is set to "loc".
781 static struct pet_scop *scop_from_non_affine_condition(
782 __isl_take pet_expr *cond, int stmt_nr,
783 __isl_take isl_multi_pw_aff *index,
784 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
786 pet_expr *expr, *write;
788 cond = pet_context_evaluate_expr(pc, cond);
790 write = pet_expr_from_index(index);
791 write = pet_expr_access_set_write(write, 1);
792 write = pet_expr_access_set_read(write, 0);
793 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
795 return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
798 /* Given that "scop" has an affine skip condition of type pet_skip_now,
799 * apply this skip condition to the domain of "pc".
800 * That is, remove the elements satisfying the skip condition from
801 * the domain of "pc".
803 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
804 struct pet_scop *scop)
806 isl_set *domain, *skip;
808 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
809 domain = pet_context_get_domain(pc);
810 domain = isl_set_subtract(domain, skip);
811 pc = pet_context_intersect_domain(pc, domain);
813 return pc;
816 /* Add a scop for evaluating the loop increment "inc" at the end
817 * of a loop body "scop" within the context "pc".
819 * The skip conditions resulting from continue statements inside
820 * the body do not apply to "inc", but those resulting from break
821 * statements do need to get applied.
823 static struct pet_scop *scop_add_inc(struct pet_scop *scop,
824 __isl_take pet_expr *inc, __isl_take pet_loc *loc,
825 __isl_keep pet_context *pc, struct pet_state *state)
827 struct pet_scop *scop_inc;
829 pc = pet_context_copy(pc);
831 if (pet_scop_has_skip(scop, pet_skip_later)) {
832 isl_multi_pw_aff *skip;
833 skip = pet_scop_get_skip(scop, pet_skip_later);
834 scop = pet_scop_set_skip(scop, pet_skip_now, skip);
835 if (pet_scop_has_affine_skip(scop, pet_skip_now))
836 pc = apply_affine_continue(pc, scop);
837 } else
838 pet_scop_reset_skip(scop, pet_skip_now);
839 scop_inc = scop_from_expr(inc, state->n_stmt++, loc, pc);
840 scop = pet_scop_add_seq(state->ctx, scop, scop_inc);
842 pet_context_free(pc);
844 return scop;
847 /* Construct a generic while scop, with iteration domain
848 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
849 * "loop_id" is the label on the loop or NULL if there is no such label.
850 * The domain of "pc" has already been extended with this infinite loop
852 * { [t] : t >= 0 }
854 * The scop consists of two parts,
855 * one for evaluating the condition "cond" and one for the body.
856 * If "expr_inc" is not NULL, then a scop for evaluating this expression
857 * is added at the end of the body,
858 * after replacing any skip conditions resulting from continue statements
859 * by the skip conditions resulting from break statements (if any).
861 * The schedules are combined as a sequence to reflect that the condition is
862 * evaluated before the body is executed and the body is filtered to depend
863 * on the result of the condition evaluating to true on all iterations
864 * up to the current iteration, while the evaluation of the condition itself
865 * is filtered to depend on the result of the condition evaluating to true
866 * on all previous iterations.
867 * The context of the scop representing the body is dropped
868 * because we don't know how many times the body will be executed,
869 * if at all.
871 * If the body contains any break, then it is taken into
872 * account in apply_affine_break (if the skip condition is affine)
873 * or in scop_add_break (if the skip condition is not affine).
875 * Note that in case of an affine skip condition,
876 * since we are dealing with a loop without loop iterator,
877 * the skip condition cannot refer to the current loop iterator and
878 * so effectively, the effect on the iteration domain is of the form
880 * { [outer,0]; [outer,t] : t >= 1 and not skip }
882 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
883 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
884 __isl_keep isl_id *loop_id, __isl_take pet_expr *expr_inc,
885 __isl_take pet_context *pc, struct pet_state *state)
887 isl_ctx *ctx;
888 isl_id *id_test, *id_break_test;
889 isl_space *space;
890 isl_multi_pw_aff *test_index;
891 isl_set *domain;
892 isl_set *skip;
893 isl_multi_aff *sched;
894 struct pet_scop *scop, *scop_body;
895 int has_affine_break;
896 int has_var_break;
898 ctx = state->ctx;
899 space = pet_context_get_space(pc);
900 test_index = pet_create_test_index(space, state->n_test++);
901 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
902 isl_multi_pw_aff_copy(test_index),
903 pet_loc_copy(loc), pc);
904 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
905 domain = pet_context_get_domain(pc);
906 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
907 test_index, state->int_size);
909 sched = map_to_last(pc, state->n_loop++, loop_id);
911 scop_body = scop_from_tree(tree_body, pc, state);
913 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
914 if (has_affine_break)
915 skip = pet_scop_get_affine_skip_domain(scop_body,
916 pet_skip_later);
917 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
918 if (has_var_break)
919 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
921 scop_body = pet_scop_reset_context(scop_body);
922 if (expr_inc)
923 scop_body = scop_add_inc(scop_body, expr_inc, loc, pc, state);
924 else
925 pet_loc_free(loc);
926 scop_body = pet_scop_reset_skips(scop_body);
928 if (has_affine_break) {
929 domain = apply_affine_break(domain, skip, 1, 0, NULL);
930 scop = pet_scop_intersect_domain_prefix(scop,
931 isl_set_copy(domain));
932 scop_body = pet_scop_intersect_domain_prefix(scop_body,
933 isl_set_copy(domain));
935 if (has_var_break) {
936 scop = scop_add_break(scop, isl_id_copy(id_break_test),
937 isl_set_copy(domain), isl_val_one(ctx));
938 scop_body = scop_add_break(scop_body, id_break_test,
939 isl_set_copy(domain), isl_val_one(ctx));
941 scop = scop_add_while(scop, scop_body, id_test, isl_set_copy(domain),
942 isl_val_one(ctx));
944 scop = pet_scop_embed(scop, domain, sched);
946 pet_context_free(pc);
947 return scop;
950 /* Check if the while loop is of the form
952 * while (affine expression)
953 * body
955 * If so, call scop_from_affine_while to construct a scop.
957 * Otherwise, pass control to scop_from_non_affine_while.
959 * "pc" is the context in which the affine expressions in the scop are created.
960 * The domain of "pc" is extended with an infinite loop
962 * { [t] : t >= 0 }
964 * before passing control to scop_from_affine_while or
965 * scop_from_non_affine_while.
967 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
968 __isl_keep pet_context *pc, struct pet_state *state)
970 pet_expr *cond_expr;
971 isl_pw_aff *pa;
973 if (!tree)
974 return NULL;
976 pc = pet_context_copy(pc);
977 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
979 cond_expr = pet_expr_copy(tree->u.l.cond);
980 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
981 pa = pet_expr_extract_affine_condition(cond_expr, pc);
982 pet_expr_free(cond_expr);
984 pc = pet_context_add_infinite_loop(pc);
986 if (!pa)
987 goto error;
989 if (!isl_pw_aff_involves_nan(pa))
990 return scop_from_affine_while(tree, pa, pc, state);
991 isl_pw_aff_free(pa);
992 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
993 pet_tree_get_loc(tree), tree->u.l.body,
994 tree->label, NULL, pc, state);
995 error:
996 pet_context_free(pc);
997 return NULL;
1000 /* Check whether "cond" expresses a simple loop bound
1001 * on the final set dimension.
1002 * In particular, if "up" is set then "cond" should contain only
1003 * upper bounds on the final set dimension.
1004 * Otherwise, it should contain only lower bounds.
1006 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
1008 int pos;
1010 pos = isl_set_dim(cond, isl_dim_set) - 1;
1011 if (isl_val_is_pos(inc))
1012 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
1013 else
1014 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
1017 /* Extend a condition on a given iteration of a loop to one that
1018 * imposes the same condition on all previous iterations.
1019 * "domain" expresses the lower [upper] bound on the iterations
1020 * when inc is positive [negative] in its final dimension.
1022 * In particular, we construct the condition (when inc is positive)
1024 * forall i' : (domain(i') and i' <= i) => cond(i')
1026 * (where "<=" applies to the final dimension)
1027 * which is equivalent to
1029 * not exists i' : domain(i') and i' <= i and not cond(i')
1031 * We construct this set by subtracting the satisfying cond from domain,
1032 * applying a map
1034 * { [i'] -> [i] : i' <= i }
1036 * and then subtracting the result from domain again.
1038 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
1039 __isl_take isl_set *domain, __isl_take isl_val *inc)
1041 isl_space *space;
1042 isl_map *previous_to_this;
1043 int i, dim;
1045 dim = isl_set_dim(cond, isl_dim_set);
1046 space = isl_space_map_from_set(isl_set_get_space(cond));
1047 previous_to_this = isl_map_universe(space);
1048 for (i = 0; i + 1 < dim; ++i)
1049 previous_to_this = isl_map_equate(previous_to_this,
1050 isl_dim_in, i, isl_dim_out, i);
1051 if (isl_val_is_pos(inc))
1052 previous_to_this = isl_map_order_le(previous_to_this,
1053 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1054 else
1055 previous_to_this = isl_map_order_ge(previous_to_this,
1056 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1058 cond = isl_set_subtract(isl_set_copy(domain), cond);
1059 cond = isl_set_apply(cond, previous_to_this);
1060 cond = isl_set_subtract(domain, cond);
1062 isl_val_free(inc);
1064 return cond;
1067 /* Given an initial value of the form
1069 * { [outer,i] -> init(outer) }
1071 * construct a domain of the form
1073 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1075 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
1076 __isl_take isl_val *inc)
1078 int dim;
1079 isl_aff *aff;
1080 isl_space *space;
1081 isl_local_space *ls;
1082 isl_set *set;
1084 dim = isl_pw_aff_dim(init, isl_dim_in);
1086 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
1087 space = isl_pw_aff_get_domain_space(init);
1088 ls = isl_local_space_from_space(space);
1089 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1090 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
1091 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1093 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
1094 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1096 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
1097 set = isl_set_project_out(set, isl_dim_set, dim, 1);
1099 return set;
1102 /* Assuming "cond" represents a bound on a loop where the loop
1103 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1104 * is possible.
1106 * Under the given assumptions, wrapping is only possible if "cond" allows
1107 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1108 * increasing iterator and 0 in case of a decreasing iterator.
1110 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
1111 __isl_keep isl_val *inc)
1113 int cw;
1114 isl_ctx *ctx;
1115 isl_val *limit;
1116 isl_set *test;
1118 test = isl_set_copy(cond);
1120 ctx = isl_set_get_ctx(test);
1121 if (isl_val_is_neg(inc))
1122 limit = isl_val_zero(ctx);
1123 else {
1124 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1125 limit = isl_val_2exp(limit);
1126 limit = isl_val_sub_ui(limit, 1);
1129 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
1130 cw = !isl_set_is_empty(test);
1131 isl_set_free(test);
1133 return cw;
1136 /* Given a space
1138 * { [outer, v] },
1140 * construct the following affine expression on this space
1142 * { [outer, v] -> [outer, v mod 2^width] }
1144 * where width is the number of bits used to represent the values
1145 * of the unsigned variable "iv".
1147 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
1148 __isl_keep pet_expr *iv)
1150 int dim;
1151 isl_aff *aff;
1152 isl_multi_aff *ma;
1154 dim = isl_space_dim(space, isl_dim_set);
1156 space = isl_space_map_from_set(space);
1157 ma = isl_multi_aff_identity(space);
1159 aff = isl_multi_aff_get_aff(ma, dim - 1);
1160 aff = pet_wrap_aff(aff, pet_expr_get_type_size(iv));
1161 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
1163 return ma;
1166 /* Given two sets in the space
1168 * { [l,i] },
1170 * where l represents the outer loop iterators, compute the set
1171 * of values of l that ensure that "set1" is a subset of "set2".
1173 * set1 is a subset of set2 if
1175 * forall i: set1(l,i) => set2(l,i)
1177 * or
1179 * not exists i: set1(l,i) and not set2(l,i)
1181 * i.e.,
1183 * not exists i: (set1 \ set2)(l,i)
1185 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
1186 __isl_take isl_set *set2)
1188 int pos;
1190 pos = isl_set_dim(set1, isl_dim_set) - 1;
1191 set1 = isl_set_subtract(set1, set2);
1192 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
1193 return isl_set_complement(set1);
1196 /* Compute the set of outer iterator values for which "cond" holds
1197 * on the next iteration of the inner loop for each element of "dom".
1199 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1200 * to the outer loop iterators), plug that into "cond"
1201 * and then compute the set of outer iterators for which "dom" is a subset
1202 * of the result.
1204 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
1205 __isl_take isl_set *dom, __isl_take isl_val *inc)
1207 int pos;
1208 isl_space *space;
1209 isl_aff *aff;
1210 isl_multi_aff *ma;
1212 pos = isl_set_dim(dom, isl_dim_set) - 1;
1213 space = isl_set_get_space(dom);
1214 space = isl_space_map_from_set(space);
1215 ma = isl_multi_aff_identity(space);
1216 aff = isl_multi_aff_get_aff(ma, pos);
1217 aff = isl_aff_add_constant_val(aff, inc);
1218 ma = isl_multi_aff_set_aff(ma, pos, aff);
1219 cond = isl_set_preimage_multi_aff(cond, ma);
1221 return enforce_subset(dom, cond);
1224 /* Construct a pet_scop for the initialization of the iterator
1225 * of the for loop "tree" within the context "pc" (i.e., the context
1226 * of the loop).
1228 static __isl_give pet_scop *scop_from_for_init(__isl_keep pet_tree *tree,
1229 __isl_keep pet_context *pc, struct pet_state *state)
1231 pet_expr *expr_iv, *init;
1232 int type_size;
1234 expr_iv = pet_expr_copy(tree->u.l.iv);
1235 type_size = pet_expr_get_type_size(expr_iv);
1236 init = pet_expr_copy(tree->u.l.init);
1237 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1238 return scop_from_expr(init, state->n_stmt++,
1239 pet_tree_get_loc(tree), pc);
1242 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1243 * In particular, "pc_init" represents the context of the loop,
1244 * whereas "pc" represents the context of the body of the loop and
1245 * has already had its domain extended with an infinite loop
1247 * { [t] : t >= 0 }
1249 * The for loop has the form
1251 * for (iv = init; cond; iv += inc)
1252 * body;
1254 * and is treated as
1256 * iv = init;
1257 * while (cond) {
1258 * body;
1259 * iv += inc;
1262 * except that the skips resulting from any continue statements
1263 * in body do not apply to the increment, but are replaced by the skips
1264 * resulting from break statements.
1266 * If the loop iterator is declared in the for loop, then it is killed before
1267 * and after the loop.
1269 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1270 __isl_keep pet_context *pc_init, __isl_take pet_context *pc,
1271 struct pet_state *state)
1273 int declared;
1274 isl_id *iv;
1275 pet_expr *expr_iv, *inc;
1276 struct pet_scop *scop_init, *scop;
1277 int type_size;
1278 struct pet_array *array;
1279 struct pet_scop *scop_kill;
1281 iv = pet_expr_access_get_id(tree->u.l.iv);
1282 pc = pet_context_clear_value(pc, iv);
1284 declared = tree->u.l.declared;
1286 scop_init = scop_from_for_init(tree, pc_init, state);
1288 expr_iv = pet_expr_copy(tree->u.l.iv);
1289 type_size = pet_expr_get_type_size(expr_iv);
1290 inc = pet_expr_copy(tree->u.l.inc);
1291 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1293 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1294 pet_tree_get_loc(tree), tree->u.l.body, tree->label,
1295 inc, pet_context_copy(pc), state);
1297 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1299 pet_context_free(pc);
1301 if (!declared)
1302 return scop;
1304 array = extract_array(tree->u.l.iv, pc_init, state);
1305 if (array)
1306 array->declared = 1;
1307 scop_kill = kill(pet_tree_get_loc(tree), array, pc_init, state);
1308 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1309 scop_kill = kill(pet_tree_get_loc(tree), array, pc_init, state);
1310 scop_kill = pet_scop_add_array(scop_kill, array);
1311 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1313 return scop;
1316 /* Given an access expression "expr", is the variable accessed by
1317 * "expr" assigned anywhere inside "tree"?
1319 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1321 int assigned = 0;
1322 isl_id *id;
1324 id = pet_expr_access_get_id(expr);
1325 assigned = pet_tree_writes(tree, id);
1326 isl_id_free(id);
1328 return assigned;
1331 /* Are all nested access parameters in "pa" allowed given "tree".
1332 * In particular, is none of them written by anywhere inside "tree".
1334 * If "tree" has any continue or break nodes in the current loop level,
1335 * then no nested access parameters are allowed.
1336 * In particular, if there is any nested access in a guard
1337 * for a piece of code containing a "continue", then we want to introduce
1338 * a separate statement for evaluating this guard so that we can express
1339 * that the result is false for all previous iterations.
1341 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1342 __isl_keep pet_tree *tree)
1344 int i, nparam;
1346 if (!tree)
1347 return -1;
1349 if (!pet_nested_any_in_pw_aff(pa))
1350 return 1;
1352 if (pet_tree_has_continue_or_break(tree))
1353 return 0;
1355 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1356 for (i = 0; i < nparam; ++i) {
1357 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1358 pet_expr *expr;
1359 int allowed;
1361 if (!pet_nested_in_id(id)) {
1362 isl_id_free(id);
1363 continue;
1366 expr = pet_nested_extract_expr(id);
1367 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1368 !is_assigned(expr, tree);
1370 pet_expr_free(expr);
1371 isl_id_free(id);
1373 if (!allowed)
1374 return 0;
1377 return 1;
1380 /* Internal data structure for collect_local.
1381 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1382 * "local" collects the results.
1384 struct pet_tree_collect_local_data {
1385 pet_context *pc;
1386 struct pet_state *state;
1387 isl_union_set *local;
1390 /* Add the variable accessed by "var" to data->local.
1391 * We extract a representation of the variable from
1392 * the pet_array constructed using extract_array
1393 * to ensure consistency with the rest of the scop.
1395 static int add_local(struct pet_tree_collect_local_data *data,
1396 __isl_keep pet_expr *var)
1398 struct pet_array *array;
1399 isl_set *universe;
1401 array = extract_array(var, data->pc, data->state);
1402 if (!array)
1403 return -1;
1405 universe = isl_set_universe(isl_set_get_space(array->extent));
1406 data->local = isl_union_set_add_set(data->local, universe);
1407 pet_array_free(array);
1409 return 0;
1412 /* If the node "tree" declares a variable, then add it to
1413 * data->local.
1415 static int extract_local_var(__isl_keep pet_tree *tree, void *user)
1417 enum pet_tree_type type;
1418 struct pet_tree_collect_local_data *data = user;
1420 type = pet_tree_get_type(tree);
1421 if (type == pet_tree_decl || type == pet_tree_decl_init)
1422 return add_local(data, tree->u.d.var);
1424 return 0;
1427 /* If the node "tree" is a for loop that declares its induction variable,
1428 * then add it this induction variable to data->local.
1430 static int extract_local_iterator(__isl_keep pet_tree *tree, void *user)
1432 struct pet_tree_collect_local_data *data = user;
1434 if (pet_tree_get_type(tree) == pet_tree_for && tree->u.l.declared)
1435 return add_local(data, tree->u.l.iv);
1437 return 0;
1440 /* Collect and return all local variables of the for loop represented
1441 * by "tree", with "scop" the corresponding pet_scop.
1442 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1444 * We collect not only the variables that are declared inside "tree",
1445 * but also the loop iterators that are declared anywhere inside
1446 * any possible macro statements in "scop".
1447 * The latter also appear as declared variable in the scop,
1448 * whereas other declared loop iterators only appear implicitly
1449 * in the iteration domains.
1451 static __isl_give isl_union_set *collect_local(struct pet_scop *scop,
1452 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1453 struct pet_state *state)
1455 int i;
1456 isl_ctx *ctx;
1457 struct pet_tree_collect_local_data data = { pc, state };
1459 ctx = pet_tree_get_ctx(tree);
1460 data.local = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
1462 if (pet_tree_foreach_sub_tree(tree, &extract_local_var, &data) < 0)
1463 return isl_union_set_free(data.local);
1465 for (i = 0; i < scop->n_stmt; ++i) {
1466 pet_tree *body = scop->stmts[i]->body;
1467 if (pet_tree_foreach_sub_tree(body, &extract_local_iterator,
1468 &data) < 0)
1469 return isl_union_set_free(data.local);
1472 return data.local;
1475 /* Add an independence to "scop" if the for node "tree" was marked
1476 * independent.
1477 * "domain" is the set of loop iterators, with the current for loop
1478 * innermost. If "sign" is positive, then the inner iterator increases.
1479 * Otherwise it decreases.
1480 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1482 * If the tree was marked, then collect all local variables and
1483 * add an independence.
1485 static struct pet_scop *set_independence(struct pet_scop *scop,
1486 __isl_keep pet_tree *tree, __isl_keep isl_set *domain, int sign,
1487 __isl_keep pet_context *pc, struct pet_state *state)
1489 isl_union_set *local;
1491 if (!tree->u.l.independent)
1492 return scop;
1494 local = collect_local(scop, tree, pc, state);
1495 scop = pet_scop_set_independent(scop, domain, local, sign);
1497 return scop;
1500 /* Add a scop for assigning to the variable corresponding to the loop
1501 * iterator the result of adding the increment to the loop iterator
1502 * at the end of a loop body "scop" within the context "pc".
1503 * "tree" represents the for loop.
1505 * The increment is of the form
1507 * iv = iv + inc
1509 * Note that "iv" on the right hand side will be evaluated in terms
1510 * of the (possibly virtual) loop iterator, i.e., the inner dimension
1511 * of the domain, while "iv" on the left hand side will not be evaluated
1512 * (because it is a write) and will continue to refer to the original
1513 * variable.
1515 static __isl_give pet_scop *add_iterator_assignment(__isl_take pet_scop *scop,
1516 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1517 struct pet_state *state)
1519 int type_size;
1520 pet_expr *expr, *iv, *inc;
1522 iv = pet_expr_copy(tree->u.l.iv);
1523 type_size = pet_expr_get_type_size(iv);
1524 iv = pet_expr_access_set_write(iv, 0);
1525 iv = pet_expr_access_set_read(iv, 1);
1526 inc = pet_expr_copy(tree->u.l.inc);
1527 expr = pet_expr_new_binary(type_size, pet_op_add, iv, inc);
1528 iv = pet_expr_copy(tree->u.l.iv);
1529 expr = pet_expr_new_binary(type_size, pet_op_assign, iv, expr);
1531 scop = scop_add_inc(scop, expr, pet_tree_get_loc(tree), pc, state);
1533 return scop;
1536 /* Construct a pet_scop for a for tree with static affine initialization
1537 * and constant increment within the context "pc".
1538 * The domain of "pc" has already been extended with an (at this point
1539 * unbounded) inner loop iterator corresponding to the current for loop.
1541 * The condition is allowed to contain nested accesses, provided
1542 * they are not being written to inside the body of the loop.
1543 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1544 * essentially treated as a while loop, with iteration domain
1545 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1547 * We extract a pet_scop for the body after intersecting the domain of "pc"
1549 * { [l,i] : i >= init and condition' }
1551 * or
1553 * { [l,i] : i <= init and condition' }
1555 * Where condition' is equal to condition if the latter is
1556 * a simple upper [lower] bound and a condition that is extended
1557 * to apply to all previous iterations otherwise.
1558 * Afterwards, the schedule of the pet_scop is extended with
1560 * { [l,i] -> [i] }
1562 * or
1564 * { [l,i] -> [-i] }
1566 * If the condition is non-affine, then we drop the condition from the
1567 * iteration domain and instead create a separate statement
1568 * for evaluating the condition. The body is then filtered to depend
1569 * on the result of the condition evaluating to true on all iterations
1570 * up to the current iteration, while the evaluation the condition itself
1571 * is filtered to depend on the result of the condition evaluating to true
1572 * on all previous iterations.
1573 * The context of the scop representing the body is dropped
1574 * because we don't know how many times the body will be executed,
1575 * if at all.
1577 * If the stride of the loop is not 1, then "i >= init" is replaced by
1579 * (exists a: i = init + stride * a and a >= 0)
1581 * If the loop iterator i is unsigned, then wrapping may occur.
1582 * We therefore use a virtual iterator instead that does not wrap.
1583 * However, the condition in the code applies
1584 * to the wrapped value, so we need to change condition(l,i)
1585 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1586 * to the original iterator by the wrapping of the virtual iterator.
1587 * Note that there may be no need to perform this final wrapping
1588 * if the loop condition (after wrapping) satisfies certain conditions.
1589 * However, the is_simple_bound condition is not enough since it doesn't
1590 * check if there even is an upper bound.
1592 * Wrapping on unsigned iterators can be avoided entirely if
1593 * the loop condition is simple, the loop iterator is incremented
1594 * [decremented] by one and the last value before wrapping cannot
1595 * possibly satisfy the loop condition.
1597 * Valid outer iterators for a for loop are those for which the initial
1598 * value itself, the increment on each domain iteration and
1599 * the condition on both the initial value and
1600 * the result of incrementing the iterator for each iteration of the domain
1601 * can be evaluated.
1602 * If the loop condition is non-affine, then we only consider validity
1603 * of the initial value.
1605 * If the loop iterator was not declared inside the loop header,
1606 * then the variable corresponding to this loop iterator is assigned
1607 * the result of adding the increment at the end of the loop body.
1608 * The assignment of the initial value is taken care of by
1609 * scop_from_affine_for_init.
1611 * If the body contains any break, then we keep track of it in "skip"
1612 * (if the skip condition is affine) or it is handled in scop_add_break
1613 * (if the skip condition is not affine).
1614 * Note that the affine break condition needs to be considered with
1615 * respect to previous iterations in the virtual domain (if any).
1617 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1618 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1619 __isl_take isl_val *inc, __isl_take pet_context *pc,
1620 struct pet_state *state)
1622 isl_set *domain;
1623 isl_multi_aff *sched;
1624 isl_set *cond = NULL;
1625 isl_set *skip = NULL;
1626 isl_id *id_test = NULL, *id_break_test;
1627 struct pet_scop *scop, *scop_cond = NULL;
1628 int pos;
1629 int is_one;
1630 int is_unsigned;
1631 int is_simple;
1632 int is_virtual;
1633 int is_non_affine;
1634 int has_affine_break;
1635 int has_var_break;
1636 isl_map *rev_wrap = NULL;
1637 isl_map *init_val_map;
1638 isl_pw_aff *pa;
1639 isl_set *valid_init;
1640 isl_set *valid_cond;
1641 isl_set *valid_cond_init;
1642 isl_set *valid_cond_next;
1643 isl_set *valid_inc;
1644 pet_expr *cond_expr;
1645 pet_context *pc_nested;
1647 pos = pet_context_dim(pc) - 1;
1649 domain = pet_context_get_domain(pc);
1650 cond_expr = pet_expr_copy(tree->u.l.cond);
1651 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1652 pc_nested = pet_context_copy(pc);
1653 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1654 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1655 pet_context_free(pc_nested);
1656 pet_expr_free(cond_expr);
1658 valid_inc = isl_pw_aff_domain(pa_inc);
1660 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1662 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1663 !is_nested_allowed(pa, tree->u.l.body);
1664 if (is_non_affine)
1665 pa = isl_pw_aff_free(pa);
1667 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1668 cond = isl_pw_aff_non_zero_set(pa);
1669 if (is_non_affine)
1670 cond = isl_set_universe(isl_set_get_space(domain));
1672 valid_cond = isl_set_coalesce(valid_cond);
1673 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1674 is_virtual = is_unsigned &&
1675 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1677 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1678 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1679 isl_dim_out, 0);
1680 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1681 isl_set_copy(valid_cond));
1682 if (is_one && !is_virtual) {
1683 isl_set *cond;
1685 isl_pw_aff_free(init_val);
1686 pa = pet_expr_extract_comparison(
1687 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1688 tree->u.l.iv, tree->u.l.init, pc);
1689 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1690 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1691 isl_set_dim(domain, isl_dim_set) - 1, 1);
1692 cond = isl_pw_aff_non_zero_set(pa);
1693 domain = isl_set_intersect(domain, cond);
1694 } else {
1695 isl_set *strided;
1697 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1698 strided = strided_domain(init_val, isl_val_copy(inc));
1699 domain = isl_set_intersect(domain, strided);
1702 if (is_virtual) {
1703 isl_multi_aff *wrap;
1704 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1705 pc = pet_context_preimage_domain(pc, wrap);
1706 rev_wrap = isl_map_from_multi_aff(wrap);
1707 rev_wrap = isl_map_reverse(rev_wrap);
1708 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1709 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1710 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1712 is_simple = is_simple_bound(cond, inc);
1713 if (!is_simple) {
1714 cond = isl_set_gist(cond, isl_set_copy(domain));
1715 is_simple = is_simple_bound(cond, inc);
1717 if (!is_simple)
1718 cond = valid_for_each_iteration(cond,
1719 isl_set_copy(domain), isl_val_copy(inc));
1720 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1721 domain = isl_set_intersect(domain, cond);
1722 sched = map_to_last(pc, state->n_loop++, tree->label);
1723 if (isl_val_is_neg(inc))
1724 sched = isl_multi_aff_neg(sched);
1726 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1727 isl_val_copy(inc));
1728 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1730 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1732 if (is_non_affine) {
1733 isl_space *space;
1734 isl_multi_pw_aff *test_index;
1735 space = isl_set_get_space(domain);
1736 test_index = pet_create_test_index(space, state->n_test++);
1737 scop_cond = scop_from_non_affine_condition(
1738 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1739 isl_multi_pw_aff_copy(test_index),
1740 pet_tree_get_loc(tree), pc);
1741 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1742 isl_dim_out);
1743 scop_cond = pet_scop_add_boolean_array(scop_cond,
1744 isl_set_copy(domain), test_index,
1745 state->int_size);
1748 scop = scop_from_tree(tree->u.l.body, pc, state);
1749 has_affine_break = scop &&
1750 pet_scop_has_affine_skip(scop, pet_skip_later);
1751 if (has_affine_break)
1752 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1753 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1754 if (has_var_break)
1755 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1756 if (is_non_affine) {
1757 scop = pet_scop_reset_context(scop);
1759 if (!tree->u.l.declared)
1760 scop = add_iterator_assignment(scop, tree, pc, state);
1761 scop = pet_scop_reset_skips(scop);
1762 scop = pet_scop_resolve_nested(scop);
1763 if (has_affine_break) {
1764 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1765 is_virtual, rev_wrap);
1766 scop = pet_scop_intersect_domain_prefix(scop,
1767 isl_set_copy(domain));
1769 isl_map_free(rev_wrap);
1770 if (has_var_break)
1771 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1772 isl_val_copy(inc));
1773 if (is_non_affine)
1774 scop = scop_add_while(scop_cond, scop, id_test,
1775 isl_set_copy(domain),
1776 isl_val_copy(inc));
1777 else
1778 scop = set_independence(scop, tree, domain, isl_val_sgn(inc),
1779 pc, state);
1780 scop = pet_scop_embed(scop, domain, sched);
1781 if (is_non_affine) {
1782 isl_set_free(valid_inc);
1783 } else {
1784 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1785 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1786 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1787 scop = pet_scop_restrict_context(scop, valid_inc);
1790 isl_val_free(inc);
1792 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1793 scop = pet_scop_restrict_context(scop, valid_init);
1795 pet_context_free(pc);
1796 return scop;
1799 /* Construct a pet_scop for a for tree with static affine initialization
1800 * and constant increment within the context "pc_init".
1801 * In particular, "pc_init" represents the context of the loop,
1802 * whereas the domain of "pc" has already been extended with an (at this point
1803 * unbounded) inner loop iterator corresponding to the current for loop.
1805 * If the loop iterator was not declared inside the loop header,
1806 * then add an assignment of the initial value to the loop iterator
1807 * before the loop. The construction of a pet_scop for the loop itself,
1808 * including updates to the loop iterator, is handled by scop_from_affine_for.
1810 static __isl_give pet_scop *scop_from_affine_for_init(__isl_keep pet_tree *tree,
1811 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1812 __isl_take isl_val *inc, __isl_keep pet_context *pc_init,
1813 __isl_take pet_context *pc, struct pet_state *state)
1815 pet_scop *scop_init, *scop;
1817 if (!tree->u.l.declared)
1818 scop_init = scop_from_for_init(tree, pc_init, state);
1820 scop = scop_from_affine_for(tree, init_val, pa_inc, inc, pc, state);
1822 if (!tree->u.l.declared)
1823 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1825 return scop;
1828 /* Construct a pet_scop for a for statement within the context of "pc".
1830 * We update the context to reflect the writes to the loop variable and
1831 * the writes inside the body.
1833 * Then we check if the initialization of the for loop
1834 * is a static affine value and the increment is a constant.
1835 * If so, we construct the pet_scop using scop_from_affine_for_init.
1836 * Otherwise, we treat the for loop as a while loop
1837 * in scop_from_non_affine_for.
1839 * Note that the initialization and the increment are extracted
1840 * in a context where the current loop iterator has been added
1841 * to the context. If these turn out not be affine, then we
1842 * have reconstruct the body context without an assignment
1843 * to this loop iterator, as this variable will then not be
1844 * treated as a dimension of the iteration domain, but as any
1845 * other variable.
1847 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1848 __isl_keep pet_context *init_pc, struct pet_state *state)
1850 isl_id *iv;
1851 isl_val *inc;
1852 isl_pw_aff *pa_inc, *init_val;
1853 pet_context *pc, *pc_init_val;
1855 if (!tree)
1856 return NULL;
1858 iv = pet_expr_access_get_id(tree->u.l.iv);
1859 pc = pet_context_copy(init_pc);
1860 pc = pet_context_add_inner_iterator(pc, iv);
1861 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1863 pc_init_val = pet_context_copy(pc);
1864 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1865 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1866 pet_context_free(pc_init_val);
1867 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1868 inc = pet_extract_cst(pa_inc);
1869 if (!pa_inc || !init_val || !inc)
1870 goto error;
1871 if (!isl_pw_aff_involves_nan(pa_inc) &&
1872 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1873 return scop_from_affine_for_init(tree, init_val, pa_inc, inc,
1874 init_pc, pc, state);
1876 isl_pw_aff_free(pa_inc);
1877 isl_pw_aff_free(init_val);
1878 isl_val_free(inc);
1879 pet_context_free(pc);
1881 pc = pet_context_copy(init_pc);
1882 pc = pet_context_add_infinite_loop(pc);
1883 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1884 return scop_from_non_affine_for(tree, init_pc, pc, state);
1885 error:
1886 isl_pw_aff_free(pa_inc);
1887 isl_pw_aff_free(init_val);
1888 isl_val_free(inc);
1889 pet_context_free(pc);
1890 return NULL;
1893 /* Check whether "expr" is an affine constraint within the context "pc".
1895 static int is_affine_condition(__isl_keep pet_expr *expr,
1896 __isl_keep pet_context *pc)
1898 isl_pw_aff *pa;
1899 int is_affine;
1901 pa = pet_expr_extract_affine_condition(expr, pc);
1902 if (!pa)
1903 return -1;
1904 is_affine = !isl_pw_aff_involves_nan(pa);
1905 isl_pw_aff_free(pa);
1907 return is_affine;
1910 /* Check if the given if statement is a conditional assignement
1911 * with a non-affine condition.
1913 * In particular we check if "stmt" is of the form
1915 * if (condition)
1916 * a = f(...);
1917 * else
1918 * a = g(...);
1920 * where the condition is non-affine and a is some array or scalar access.
1922 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1923 __isl_keep pet_context *pc)
1925 int equal;
1926 isl_ctx *ctx;
1927 pet_expr *expr1, *expr2;
1929 ctx = pet_tree_get_ctx(tree);
1930 if (!pet_options_get_detect_conditional_assignment(ctx))
1931 return 0;
1932 if (tree->type != pet_tree_if_else)
1933 return 0;
1934 if (tree->u.i.then_body->type != pet_tree_expr)
1935 return 0;
1936 if (tree->u.i.else_body->type != pet_tree_expr)
1937 return 0;
1938 expr1 = tree->u.i.then_body->u.e.expr;
1939 expr2 = tree->u.i.else_body->u.e.expr;
1940 if (pet_expr_get_type(expr1) != pet_expr_op)
1941 return 0;
1942 if (pet_expr_get_type(expr2) != pet_expr_op)
1943 return 0;
1944 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1945 return 0;
1946 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1947 return 0;
1948 expr1 = pet_expr_get_arg(expr1, 0);
1949 expr2 = pet_expr_get_arg(expr2, 0);
1950 equal = pet_expr_is_equal(expr1, expr2);
1951 pet_expr_free(expr1);
1952 pet_expr_free(expr2);
1953 if (equal < 0 || !equal)
1954 return 0;
1955 if (is_affine_condition(tree->u.i.cond, pc))
1956 return 0;
1958 return 1;
1961 /* Given that "tree" is of the form
1963 * if (condition)
1964 * a = f(...);
1965 * else
1966 * a = g(...);
1968 * where a is some array or scalar access, construct a pet_scop
1969 * corresponding to this conditional assignment within the context "pc".
1970 * "cond_pa" is an affine expression with nested accesses representing
1971 * the condition.
1973 * The constructed pet_scop then corresponds to the expression
1975 * a = condition ? f(...) : g(...)
1977 * All access relations in f(...) are intersected with condition
1978 * while all access relation in g(...) are intersected with the complement.
1980 static struct pet_scop *scop_from_conditional_assignment(
1981 __isl_keep pet_tree *tree, __isl_take isl_pw_aff *cond_pa,
1982 __isl_take pet_context *pc, struct pet_state *state)
1984 int type_size;
1985 isl_set *cond, *comp;
1986 isl_multi_pw_aff *index;
1987 pet_expr *expr1, *expr2;
1988 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1989 struct pet_scop *scop;
1991 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa));
1992 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa));
1993 index = isl_multi_pw_aff_from_pw_aff(cond_pa);
1995 expr1 = tree->u.i.then_body->u.e.expr;
1996 expr2 = tree->u.i.else_body->u.e.expr;
1998 pe_cond = pet_expr_from_index(index);
2000 pe_then = pet_expr_get_arg(expr1, 1);
2001 pe_then = pet_context_evaluate_expr(pc, pe_then);
2002 pe_then = pet_expr_restrict(pe_then, cond);
2003 pe_else = pet_expr_get_arg(expr2, 1);
2004 pe_else = pet_context_evaluate_expr(pc, pe_else);
2005 pe_else = pet_expr_restrict(pe_else, comp);
2006 pe_write = pet_expr_get_arg(expr1, 0);
2007 pe_write = pet_context_evaluate_expr(pc, pe_write);
2009 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
2010 type_size = pet_expr_get_type_size(pe_write);
2011 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
2013 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
2014 pet_tree_get_loc(tree), pc);
2016 pet_context_free(pc);
2018 return scop;
2021 /* Construct a pet_scop for a non-affine if statement within the context "pc".
2023 * We create a separate statement that writes the result
2024 * of the non-affine condition to a virtual scalar.
2025 * A constraint requiring the value of this virtual scalar to be one
2026 * is added to the iteration domains of the then branch.
2027 * Similarly, a constraint requiring the value of this virtual scalar
2028 * to be zero is added to the iteration domains of the else branch, if any.
2029 * We combine the schedules as a sequence to ensure that the virtual scalar
2030 * is written before it is read.
2032 * If there are any breaks or continues in the then and/or else
2033 * branches, then we may have to compute a new skip condition.
2034 * This is handled using a pet_skip_info object.
2035 * On initialization, the object checks if skip conditions need
2036 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
2037 * adds them in pet_skip_info_add.
2039 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
2040 __isl_take pet_context *pc, struct pet_state *state)
2042 int has_else;
2043 isl_space *space;
2044 isl_set *domain;
2045 isl_multi_pw_aff *test_index;
2046 struct pet_skip_info skip;
2047 struct pet_scop *scop, *scop_then, *scop_else = NULL;
2049 has_else = tree->type == pet_tree_if_else;
2051 space = pet_context_get_space(pc);
2052 test_index = pet_create_test_index(space, state->n_test++);
2053 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
2054 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
2055 pet_tree_get_loc(tree), pc);
2056 domain = pet_context_get_domain(pc);
2057 scop = pet_scop_add_boolean_array(scop, domain,
2058 isl_multi_pw_aff_copy(test_index), state->int_size);
2060 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
2061 if (has_else)
2062 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
2064 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
2065 has_else, 0);
2066 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
2068 scop_then = pet_scop_filter(scop_then,
2069 isl_multi_pw_aff_copy(test_index), 1);
2070 if (has_else) {
2071 scop_else = pet_scop_filter(scop_else, test_index, 0);
2072 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
2073 } else
2074 isl_multi_pw_aff_free(test_index);
2076 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
2078 scop = pet_skip_info_add(&skip, scop);
2080 pet_context_free(pc);
2081 return scop;
2084 /* Construct a pet_scop for an affine if statement within the context "pc".
2086 * The condition is added to the iteration domains of the then branch,
2087 * while the opposite of the condition in added to the iteration domains
2088 * of the else branch, if any.
2090 * If there are any breaks or continues in the then and/or else
2091 * branches, then we may have to compute a new skip condition.
2092 * This is handled using a pet_skip_info_if object.
2093 * On initialization, the object checks if skip conditions need
2094 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
2095 * adds them in pet_skip_info_add.
2097 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
2098 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
2099 struct pet_state *state)
2101 int has_else;
2102 isl_ctx *ctx;
2103 isl_set *set, *complement;
2104 isl_set *valid;
2105 struct pet_skip_info skip;
2106 struct pet_scop *scop, *scop_then, *scop_else = NULL;
2107 pet_context *pc_body;
2109 ctx = pet_tree_get_ctx(tree);
2111 has_else = tree->type == pet_tree_if_else;
2113 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
2114 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2116 pc_body = pet_context_copy(pc);
2117 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
2118 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
2119 pet_context_free(pc_body);
2120 if (has_else) {
2121 pc_body = pet_context_copy(pc);
2122 complement = isl_set_copy(valid);
2123 complement = isl_set_subtract(valid, isl_set_copy(set));
2124 pc_body = pet_context_intersect_domain(pc_body,
2125 isl_set_copy(complement));
2126 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
2127 pet_context_free(pc_body);
2130 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
2131 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
2132 isl_pw_aff_free(cond);
2134 scop = pet_scop_restrict(scop_then, set);
2136 if (has_else) {
2137 scop_else = pet_scop_restrict(scop_else, complement);
2138 scop = pet_scop_add_par(ctx, scop, scop_else);
2140 scop = pet_scop_resolve_nested(scop);
2141 scop = pet_scop_restrict_context(scop, valid);
2143 scop = pet_skip_info_add(&skip, scop);
2145 pet_context_free(pc);
2146 return scop;
2149 /* Construct a pet_scop for an if statement within the context "pc".
2151 * If the condition fits the pattern of a conditional assignment,
2152 * then it is handled by scop_from_conditional_assignment.
2153 * Note that the condition is only considered for a conditional assignment
2154 * if it is not static-affine. However, it should still convert
2155 * to an affine expression when nesting is allowed.
2157 * Otherwise, we check if the condition is affine.
2158 * If so, we construct the scop in scop_from_affine_if.
2159 * Otherwise, we construct the scop in scop_from_non_affine_if.
2161 * We allow the condition to be dynamic, i.e., to refer to
2162 * scalars or array elements that may be written to outside
2163 * of the given if statement. These nested accesses are then represented
2164 * as output dimensions in the wrapping iteration domain.
2165 * If it is also written _inside_ the then or else branch, then
2166 * we treat the condition as non-affine.
2167 * As explained in extract_non_affine_if, this will introduce
2168 * an extra statement.
2169 * For aesthetic reasons, we want this statement to have a statement
2170 * number that is lower than those of the then and else branches.
2171 * In order to evaluate if we will need such a statement, however, we
2172 * first construct scops for the then and else branches.
2173 * We therefore reserve a statement number if we might have to
2174 * introduce such an extra statement.
2176 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
2177 __isl_keep pet_context *pc, struct pet_state *state)
2179 int has_else;
2180 isl_pw_aff *cond;
2181 pet_expr *cond_expr;
2182 pet_context *pc_nested;
2184 if (!tree)
2185 return NULL;
2187 has_else = tree->type == pet_tree_if_else;
2189 pc = pet_context_copy(pc);
2190 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
2191 if (has_else)
2192 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
2194 cond_expr = pet_expr_copy(tree->u.i.cond);
2195 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
2196 pc_nested = pet_context_copy(pc);
2197 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
2198 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
2199 pet_context_free(pc_nested);
2200 pet_expr_free(cond_expr);
2202 if (!cond) {
2203 pet_context_free(pc);
2204 return NULL;
2207 if (isl_pw_aff_involves_nan(cond)) {
2208 isl_pw_aff_free(cond);
2209 return scop_from_non_affine_if(tree, pc, state);
2212 if (is_conditional_assignment(tree, pc))
2213 return scop_from_conditional_assignment(tree, cond, pc, state);
2215 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
2216 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
2217 isl_pw_aff_free(cond);
2218 return scop_from_non_affine_if(tree, pc, state);
2221 return scop_from_affine_if(tree, cond, pc, state);
2224 /* Return a one-dimensional multi piecewise affine expression that is equal
2225 * to the constant 1 and is defined over the given domain.
2227 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
2229 isl_local_space *ls;
2230 isl_aff *aff;
2232 ls = isl_local_space_from_space(space);
2233 aff = isl_aff_zero_on_domain(ls);
2234 aff = isl_aff_set_constant_si(aff, 1);
2236 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
2239 /* Construct a pet_scop for a continue statement with the given domain space.
2241 * We simply create an empty scop with a universal pet_skip_now
2242 * skip condition. This skip condition will then be taken into
2243 * account by the enclosing loop construct, possibly after
2244 * being incorporated into outer skip conditions.
2246 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
2247 __isl_take isl_space *space)
2249 struct pet_scop *scop;
2251 scop = pet_scop_empty(isl_space_copy(space));
2253 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
2255 return scop;
2258 /* Construct a pet_scop for a break statement with the given domain space.
2260 * We simply create an empty scop with both a universal pet_skip_now
2261 * skip condition and a universal pet_skip_later skip condition.
2262 * These skip conditions will then be taken into
2263 * account by the enclosing loop construct, possibly after
2264 * being incorporated into outer skip conditions.
2266 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
2267 __isl_take isl_space *space)
2269 struct pet_scop *scop;
2270 isl_multi_pw_aff *skip;
2272 scop = pet_scop_empty(isl_space_copy(space));
2274 skip = one_mpa(space);
2275 scop = pet_scop_set_skip(scop, pet_skip_now,
2276 isl_multi_pw_aff_copy(skip));
2277 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
2279 return scop;
2282 /* Extract a clone of the kill statement "stmt".
2283 * The domain of the clone is given by "domain".
2285 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
2286 struct pet_stmt *stmt, struct pet_state *state)
2288 pet_expr *kill;
2289 isl_space *space;
2290 isl_multi_pw_aff *mpa;
2291 pet_tree *tree;
2293 if (!domain || !stmt)
2294 return NULL;
2296 kill = pet_tree_expr_get_expr(stmt->body);
2297 space = pet_stmt_get_space(stmt);
2298 space = isl_space_map_from_set(space);
2299 mpa = isl_multi_pw_aff_identity(space);
2300 mpa = isl_multi_pw_aff_reset_tuple_id(mpa, isl_dim_in);
2301 kill = pet_expr_update_domain(kill, mpa);
2302 tree = pet_tree_new_expr(kill);
2303 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
2304 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
2305 state->n_stmt++, tree);
2306 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
2309 /* Extract a clone of the kill statements in "scop".
2310 * The domain of each clone is given by "domain".
2311 * "scop" is expected to have been created from a DeclStmt
2312 * and should have (one of) the kill(s) as its first statement.
2313 * If "scop" was created from a declaration group, then there
2314 * may be multiple kill statements inside.
2316 static struct pet_scop *extract_kills(__isl_keep isl_set *domain,
2317 struct pet_scop *scop, struct pet_state *state)
2319 isl_ctx *ctx;
2320 struct pet_stmt *stmt;
2321 struct pet_scop *kill;
2322 int i;
2324 if (!domain || !scop)
2325 return NULL;
2326 ctx = isl_set_get_ctx(domain);
2327 if (scop->n_stmt < 1)
2328 isl_die(ctx, isl_error_internal,
2329 "expecting at least one statement", return NULL);
2330 stmt = scop->stmts[0];
2331 if (!pet_stmt_is_kill(stmt))
2332 isl_die(ctx, isl_error_internal,
2333 "expecting kill statement", return NULL);
2335 kill = extract_kill(domain, stmt, state);
2337 for (i = 1; i < scop->n_stmt; ++i) {
2338 struct pet_scop *kill_i;
2340 stmt = scop->stmts[i];
2341 if (!pet_stmt_is_kill(stmt))
2342 continue;
2344 kill_i = extract_kill(domain, stmt, state);
2345 kill = pet_scop_add_par(ctx, kill, kill_i);
2348 return kill;
2351 /* Has "tree" been created from a DeclStmt?
2352 * That is, is it either a declaration or a group of declarations?
2354 static int tree_is_decl(__isl_keep pet_tree *tree)
2356 int is_decl;
2357 int i;
2359 if (!tree)
2360 return -1;
2361 is_decl = pet_tree_is_decl(tree);
2362 if (is_decl < 0 || is_decl)
2363 return is_decl;
2365 if (tree->type != pet_tree_block)
2366 return 0;
2367 if (pet_tree_block_get_block(tree))
2368 return 0;
2369 if (tree->u.b.n == 0)
2370 return 0;
2372 for (i = 0; i < tree->u.b.n; ++i) {
2373 is_decl = tree_is_decl(tree->u.b.child[i]);
2374 if (is_decl < 0 || !is_decl)
2375 return is_decl;
2378 return 1;
2381 /* Does "tree" represent an assignment to a variable?
2383 * The assignment may be one of
2384 * - a declaration with initialization
2385 * - an expression with a top-level assignment operator
2387 static int is_assignment(__isl_keep pet_tree *tree)
2389 if (!tree)
2390 return 0;
2391 if (tree->type == pet_tree_decl_init)
2392 return 1;
2393 return pet_tree_is_assign(tree);
2396 /* Update "pc" by taking into account the assignment performed by "tree",
2397 * where "tree" satisfies is_assignment.
2399 * In particular, if the lhs of the assignment is a scalar variable and
2400 * if the rhs is an affine expression, then keep track of this value in "pc"
2401 * so that we can plug it in when we later come across the same variable.
2403 * Any previously assigned value to the variable has already been removed
2404 * by scop_handle_writes.
2406 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
2407 __isl_keep pet_tree *tree)
2409 pet_expr *var, *val;
2410 isl_id *id;
2411 isl_pw_aff *pa;
2413 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
2414 var = pet_tree_decl_get_var(tree);
2415 val = pet_tree_decl_get_init(tree);
2416 } else {
2417 pet_expr *expr;
2418 expr = pet_tree_expr_get_expr(tree);
2419 var = pet_expr_get_arg(expr, 0);
2420 val = pet_expr_get_arg(expr, 1);
2421 pet_expr_free(expr);
2424 if (!pet_expr_is_scalar_access(var)) {
2425 pet_expr_free(var);
2426 pet_expr_free(val);
2427 return pc;
2430 pa = pet_expr_extract_affine(val, pc);
2431 if (!pa)
2432 pc = pet_context_free(pc);
2434 if (!isl_pw_aff_involves_nan(pa)) {
2435 id = pet_expr_access_get_id(var);
2436 pc = pet_context_set_value(pc, id, pa);
2437 } else {
2438 isl_pw_aff_free(pa);
2440 pet_expr_free(var);
2441 pet_expr_free(val);
2443 return pc;
2446 /* Mark all arrays in "scop" as being exposed.
2448 static struct pet_scop *mark_exposed(struct pet_scop *scop)
2450 int i;
2452 if (!scop)
2453 return NULL;
2454 for (i = 0; i < scop->n_array; ++i)
2455 scop->arrays[i]->exposed = 1;
2456 return scop;
2459 /* Try and construct a pet_scop corresponding to (part of)
2460 * a sequence of statements within the context "pc".
2462 * After extracting a statement, we update "pc"
2463 * based on the top-level assignments in the statement
2464 * so that we can exploit them in subsequent statements in the same block.
2465 * Top-level affine assumptions are also recorded in the context.
2467 * If there are any breaks or continues in the individual statements,
2468 * then we may have to compute a new skip condition.
2469 * This is handled using a pet_skip_info object.
2470 * On initialization, the object checks if skip conditions need
2471 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2472 * adds them in pet_skip_info_add.
2474 * If "block" is set, then we need to insert kill statements at
2475 * the end of the block for any array that has been declared by
2476 * one of the statements in the sequence. Each of these declarations
2477 * results in the construction of a kill statement at the place
2478 * of the declaration, so we simply collect duplicates of
2479 * those kill statements and append these duplicates to the constructed scop.
2481 * If "block" is not set, then any array declared by one of the statements
2482 * in the sequence is marked as being exposed.
2484 * If autodetect is set, then we allow the extraction of only a subrange
2485 * of the sequence of statements. However, if there is at least one statement
2486 * for which we could not construct a scop and the final range contains
2487 * either no statements or at least one kill, then we discard the entire
2488 * range.
2490 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2491 __isl_keep pet_context *pc, struct pet_state *state)
2493 int i;
2494 isl_ctx *ctx;
2495 isl_space *space;
2496 isl_set *domain;
2497 struct pet_scop *scop, *kills;
2499 ctx = pet_tree_get_ctx(tree);
2501 space = pet_context_get_space(pc);
2502 domain = pet_context_get_domain(pc);
2503 pc = pet_context_copy(pc);
2504 scop = pet_scop_empty(isl_space_copy(space));
2505 kills = pet_scop_empty(space);
2506 for (i = 0; i < tree->u.b.n; ++i) {
2507 struct pet_scop *scop_i;
2509 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2510 pc = apply_affine_continue(pc, scop);
2511 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2512 if (pet_tree_is_assume(tree->u.b.child[i]))
2513 pc = scop_add_affine_assumption(scop_i, pc);
2514 pc = scop_handle_writes(scop_i, pc);
2515 if (is_assignment(tree->u.b.child[i]))
2516 pc = handle_assignment(pc, tree->u.b.child[i]);
2517 struct pet_skip_info skip;
2518 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2519 pet_skip_info_seq_extract(&skip, pc, state);
2520 if (scop_i && tree_is_decl(tree->u.b.child[i])) {
2521 if (tree->u.b.block) {
2522 struct pet_scop *kill;
2523 kill = extract_kills(domain, scop_i, state);
2524 kills = pet_scop_add_par(ctx, kills, kill);
2525 } else
2526 scop_i = mark_exposed(scop_i);
2528 scop = pet_scop_add_seq(ctx, scop, scop_i);
2530 scop = pet_skip_info_add(&skip, scop);
2532 if (!scop)
2533 break;
2535 isl_set_free(domain);
2537 scop = pet_scop_add_seq(ctx, scop, kills);
2539 pet_context_free(pc);
2541 return scop;
2544 /* Internal data structure for extract_declared_arrays.
2546 * "pc" and "state" are used to create pet_array objects and kill statements.
2547 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2548 * found any declared array.
2549 * "scop" has been initialized by the caller and is used to attach
2550 * the created pet_array objects.
2551 * "kill_before" and "kill_after" are created and updated by
2552 * extract_declared_arrays to collect the kills of the arrays.
2554 struct pet_tree_extract_declared_arrays_data {
2555 pet_context *pc;
2556 struct pet_state *state;
2558 isl_ctx *ctx;
2560 int any;
2561 struct pet_scop *scop;
2562 struct pet_scop *kill_before;
2563 struct pet_scop *kill_after;
2566 /* Check if the node "node" declares any array or scalar.
2567 * If so, create the corresponding pet_array and attach it to data->scop.
2568 * Additionally, create two kill statements for the array and add them
2569 * to data->kill_before and data->kill_after.
2571 static int extract_declared_arrays(__isl_keep pet_tree *node, void *user)
2573 enum pet_tree_type type;
2574 struct pet_tree_extract_declared_arrays_data *data = user;
2575 struct pet_array *array;
2576 struct pet_scop *scop_kill;
2577 pet_expr *var;
2579 type = pet_tree_get_type(node);
2580 if (type == pet_tree_decl || type == pet_tree_decl_init)
2581 var = node->u.d.var;
2582 else if (type == pet_tree_for && node->u.l.declared)
2583 var = node->u.l.iv;
2584 else
2585 return 0;
2587 array = extract_array(var, data->pc, data->state);
2588 if (array)
2589 array->declared = 1;
2590 data->scop = pet_scop_add_array(data->scop, array);
2592 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2593 if (!data->any)
2594 data->kill_before = scop_kill;
2595 else
2596 data->kill_before = pet_scop_add_par(data->ctx,
2597 data->kill_before, scop_kill);
2599 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2600 if (!data->any)
2601 data->kill_after = scop_kill;
2602 else
2603 data->kill_after = pet_scop_add_par(data->ctx,
2604 data->kill_after, scop_kill);
2606 data->any = 1;
2608 return 0;
2611 /* Convert a pet_tree that consists of more than a single leaf
2612 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2613 * Do so within the context of "pc", taking into account the writes inside
2614 * "tree". That is, first clear any previously assigned values to variables
2615 * that are written by "tree".
2617 * After constructing the core scop, we also look for any arrays (or scalars)
2618 * that are declared inside "tree". Each of those arrays is marked as
2619 * having been declared and kill statements for these arrays
2620 * are introduced before and after the core scop.
2621 * Note that the input tree is not a leaf so that the declaration
2622 * cannot occur at the outer level.
2624 static struct pet_scop *scop_from_tree_macro(__isl_take pet_tree *tree,
2625 __isl_keep pet_context *pc, struct pet_state *state)
2627 struct pet_tree_extract_declared_arrays_data data = { pc, state };
2629 data.pc = pet_context_copy(data.pc);
2630 data.pc = pet_context_clear_writes_in_tree(data.pc, tree);
2631 data.scop = scop_from_unevaluated_tree(pet_tree_copy(tree),
2632 state->n_stmt++, data.pc);
2634 data.any = 0;
2635 data.ctx = pet_context_get_ctx(data.pc);
2636 if (pet_tree_foreach_sub_tree(tree, &extract_declared_arrays,
2637 &data) < 0)
2638 data.scop = pet_scop_free(data.scop);
2639 pet_tree_free(tree);
2640 pet_context_free(data.pc);
2642 if (!data.any)
2643 return data.scop;
2645 data.scop = pet_scop_add_seq(data.ctx, data.kill_before, data.scop);
2646 data.scop = pet_scop_add_seq(data.ctx, data.scop, data.kill_after);
2648 return data.scop;
2651 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2652 * within the context "pc" by calling the appropriate function
2653 * based on the type of "tree".
2655 * If the initially constructed pet_scop turns out to involve
2656 * dynamic control and if the user has requested an encapsulation
2657 * of all dynamic control, then this pet_scop is discarded and
2658 * a new pet_scop is created with a single statement representing
2659 * the entire "tree".
2660 * However, if the scop contains any active continue or break,
2661 * then we need to include the loop containing the continue or break
2662 * in the encapsulation. We therefore postpone the encapsulation
2663 * until we have constructed a pet_scop for this enclosing loop.
2665 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2666 __isl_keep pet_context *pc, struct pet_state *state)
2668 isl_ctx *ctx;
2669 struct pet_scop *scop = NULL;
2671 if (!tree)
2672 return NULL;
2674 ctx = pet_tree_get_ctx(tree);
2675 switch (tree->type) {
2676 case pet_tree_error:
2677 return NULL;
2678 case pet_tree_block:
2679 return scop_from_block(tree, pc, state);
2680 case pet_tree_break:
2681 return scop_from_break(tree, pet_context_get_space(pc));
2682 case pet_tree_continue:
2683 return scop_from_continue(tree, pet_context_get_space(pc));
2684 case pet_tree_decl:
2685 case pet_tree_decl_init:
2686 return scop_from_decl(tree, pc, state);
2687 case pet_tree_expr:
2688 return scop_from_tree_expr(tree, pc, state);
2689 case pet_tree_if:
2690 case pet_tree_if_else:
2691 scop = scop_from_if(tree, pc, state);
2692 break;
2693 case pet_tree_for:
2694 scop = scop_from_for(tree, pc, state);
2695 break;
2696 case pet_tree_while:
2697 scop = scop_from_while(tree, pc, state);
2698 break;
2699 case pet_tree_infinite_loop:
2700 scop = scop_from_infinite_for(tree, pc, state);
2701 break;
2704 if (!scop)
2705 return NULL;
2707 if (!pet_options_get_encapsulate_dynamic_control(ctx) ||
2708 !pet_scop_has_data_dependent_conditions(scop) ||
2709 pet_scop_has_var_skip(scop, pet_skip_now))
2710 return scop;
2712 pet_scop_free(scop);
2713 return scop_from_tree_macro(pet_tree_copy(tree), pc, state);
2716 /* If "tree" has a label that is of the form S_<nr>, then make
2717 * sure that state->n_stmt is greater than nr to ensure that
2718 * we will not generate S_<nr> ourselves.
2720 static int set_first_stmt(__isl_keep pet_tree *tree, void *user)
2722 struct pet_state *state = user;
2723 const char *name;
2724 int nr;
2726 if (!tree)
2727 return -1;
2728 if (!tree->label)
2729 return 0;
2730 name = isl_id_get_name(tree->label);
2731 if (strncmp(name, "S_", 2) != 0)
2732 return 0;
2733 nr = atoi(name + 2);
2734 if (nr >= state->n_stmt)
2735 state->n_stmt = nr + 1;
2737 return 0;
2740 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2741 * "int_size" is the number of bytes need to represent an integer.
2742 * "extract_array" is a callback that we can use to create a pet_array
2743 * that corresponds to the variable accessed by an expression.
2745 * Initialize the global state, construct a context and then
2746 * construct the pet_scop by recursively visiting the tree.
2748 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2750 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2751 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2752 __isl_keep pet_context *pc, void *user), void *user,
2753 __isl_keep pet_context *pc)
2755 struct pet_scop *scop;
2756 struct pet_state state = { 0 };
2758 if (!tree)
2759 return NULL;
2761 state.ctx = pet_tree_get_ctx(tree);
2762 state.int_size = int_size;
2763 state.extract_array = extract_array;
2764 state.user = user;
2765 if (pet_tree_foreach_sub_tree(tree, &set_first_stmt, &state) < 0)
2766 tree = pet_tree_free(tree);
2768 scop = scop_from_tree(tree, pc, &state);
2769 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2771 pet_tree_free(tree);
2773 if (scop)
2774 scop->context = isl_set_params(scop->context);
2776 return scop;