rerun configure when llvm-config is updated
[pet.git] / tree2scop.c
blobedc85f3d905d824af94ee28219bba267f4380758
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;
228 if (!array)
229 goto error;
230 ctx = isl_set_get_ctx(array->extent);
231 access = isl_map_from_range(isl_set_copy(array->extent));
232 id = isl_set_get_tuple_id(array->extent);
233 space = isl_space_alloc(ctx, 0, 0, 0);
234 space = isl_space_set_tuple_id(space, isl_dim_out, id);
235 index = isl_multi_pw_aff_zero(space);
236 expr = pet_expr_kill_from_access_and_index(access, index);
237 return scop_from_expr(expr, state->n_stmt++, loc, pc);
238 error:
239 pet_loc_free(loc);
240 return NULL;
243 /* Construct and return a pet_array corresponding to the variable
244 * accessed by "access" by calling the extract_array callback.
246 static struct pet_array *extract_array(__isl_keep pet_expr *access,
247 __isl_keep pet_context *pc, struct pet_state *state)
249 return state->extract_array(access, pc, state->user);
252 /* Construct a pet_scop for a (single) variable declaration
253 * within the context "pc".
255 * The scop contains the variable being declared (as an array)
256 * and a statement killing the array.
258 * If the declaration comes with an initialization, then the scop
259 * also contains an assignment to the variable.
261 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
262 __isl_keep pet_context *pc, struct pet_state *state)
264 int type_size;
265 isl_ctx *ctx;
266 struct pet_array *array;
267 struct pet_scop *scop_decl, *scop;
268 pet_expr *lhs, *rhs, *pe;
270 array = extract_array(tree->u.d.var, pc, state);
271 if (array)
272 array->declared = 1;
273 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
274 scop_decl = pet_scop_add_array(scop_decl, array);
276 if (tree->type != pet_tree_decl_init)
277 return scop_decl;
279 lhs = pet_expr_copy(tree->u.d.var);
280 rhs = pet_expr_copy(tree->u.d.init);
281 type_size = pet_expr_get_type_size(lhs);
282 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
283 scop = scop_from_expr(pe, state->n_stmt++, pet_tree_get_loc(tree), pc);
285 ctx = pet_tree_get_ctx(tree);
286 scop = pet_scop_add_seq(ctx, scop_decl, scop);
288 return scop;
291 /* Does "tree" represent a kill statement?
292 * That is, is it an expression statement that "calls" __pencil_kill?
294 static int is_pencil_kill(__isl_keep pet_tree *tree)
296 pet_expr *expr;
297 const char *name;
299 if (!tree)
300 return -1;
301 if (tree->type != pet_tree_expr)
302 return 0;
303 expr = tree->u.e.expr;
304 if (pet_expr_get_type(expr) != pet_expr_call)
305 return 0;
306 name = pet_expr_call_get_name(expr);
307 if (!name)
308 return -1;
309 return !strcmp(name, "__pencil_kill");
312 /* Add a kill to "scop" that kills what is accessed by
313 * the access expression "expr".
315 * Mark the access as a write prior to evaluation to avoid
316 * the access being replaced by a possible known value
317 * during the evaluation.
319 * If the access expression has any arguments (after evaluation
320 * in the context of "pc"), then we ignore it, since we cannot
321 * tell which elements are definitely killed.
323 * Otherwise, we extend the index expression to the dimension
324 * of the accessed array and intersect with the extent of the array and
325 * add a kill expression that kills these array elements is added to "scop".
327 static struct pet_scop *scop_add_kill(struct pet_scop *scop,
328 __isl_take pet_expr *expr, __isl_take pet_loc *loc,
329 __isl_keep pet_context *pc, struct pet_state *state)
331 int dim1, dim2;
332 isl_id *id;
333 isl_multi_pw_aff *index;
334 isl_map *map;
335 pet_expr *kill;
336 struct pet_array *array;
337 struct pet_scop *scop_i;
339 expr = pet_expr_access_set_write(expr, 1);
340 expr = pet_context_evaluate_expr(pc, expr);
341 if (!expr)
342 goto error;
343 if (expr->n_arg != 0) {
344 pet_loc_free(loc);
345 pet_expr_free(expr);
346 return scop;
348 array = extract_array(expr, pc, state);
349 if (!array)
350 goto error;
351 index = pet_expr_access_get_index(expr);
352 pet_expr_free(expr);
353 map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
354 id = isl_map_get_tuple_id(map, isl_dim_out);
355 dim1 = isl_set_dim(array->extent, isl_dim_set);
356 dim2 = isl_map_dim(map, isl_dim_out);
357 map = isl_map_add_dims(map, isl_dim_out, dim1 - dim2);
358 map = isl_map_set_tuple_id(map, isl_dim_out, id);
359 map = isl_map_intersect_range(map, isl_set_copy(array->extent));
360 pet_array_free(array);
361 kill = pet_expr_kill_from_access_and_index(map, index);
362 scop_i = scop_from_evaluated_expr(kill, state->n_stmt++, loc, pc);
363 scop = pet_scop_add_par(state->ctx, scop, scop_i);
365 return scop;
366 error:
367 pet_expr_free(expr);
368 pet_loc_free(loc);
369 return pet_scop_free(scop);
372 /* For each argument of the __pencil_kill call in "tree" that
373 * represents an access, add a kill statement to "scop" killing the accessed
374 * elements.
376 static struct pet_scop *scop_from_pencil_kill(__isl_keep pet_tree *tree,
377 __isl_keep pet_context *pc, struct pet_state *state)
379 pet_expr *call;
380 struct pet_scop *scop;
381 int i, n;
383 call = tree->u.e.expr;
385 scop = pet_scop_empty(pet_context_get_space(pc));
387 n = pet_expr_get_n_arg(call);
388 for (i = 0; i < n; ++i) {
389 pet_expr *arg;
390 pet_loc *loc;
392 arg = pet_expr_get_arg(call, i);
393 if (!arg)
394 return pet_scop_free(scop);
395 if (pet_expr_get_type(arg) != pet_expr_access) {
396 pet_expr_free(arg);
397 continue;
399 loc = pet_tree_get_loc(tree);
400 scop = scop_add_kill(scop, arg, loc, pc, state);
403 return scop;
406 /* Construct a pet_scop for an expression statement within the context "pc".
408 * If the expression calls __pencil_kill, then it needs to be converted
409 * into zero or more kill statements.
410 * Otherwise, a scop is extracted directly from the tree.
412 static struct pet_scop *scop_from_tree_expr(__isl_keep pet_tree *tree,
413 __isl_keep pet_context *pc, struct pet_state *state)
415 int is_kill;
417 is_kill = is_pencil_kill(tree);
418 if (is_kill < 0)
419 return NULL;
420 if (is_kill)
421 return scop_from_pencil_kill(tree, pc, state);
422 return scop_from_unevaluated_tree(pet_tree_copy(tree),
423 state->n_stmt++, pc);
426 /* Construct a pet_scop for a return statement within the context "pc".
428 static struct pet_scop *scop_from_return(__isl_keep pet_tree *tree,
429 __isl_keep pet_context *pc, struct pet_state *state)
431 return scop_from_unevaluated_tree(pet_tree_copy(tree),
432 state->n_stmt++, pc);
435 /* Return those elements in the space of "cond" that come after
436 * (based on "sign") an element in "cond" in the final dimension.
438 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
440 isl_space *space;
441 isl_map *previous_to_this;
442 int i, dim;
444 dim = isl_set_dim(cond, isl_dim_set);
445 space = isl_space_map_from_set(isl_set_get_space(cond));
446 previous_to_this = isl_map_universe(space);
447 for (i = 0; i + 1 < dim; ++i)
448 previous_to_this = isl_map_equate(previous_to_this,
449 isl_dim_in, i, isl_dim_out, i);
450 if (sign > 0)
451 previous_to_this = isl_map_order_lt(previous_to_this,
452 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
453 else
454 previous_to_this = isl_map_order_gt(previous_to_this,
455 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
457 cond = isl_set_apply(cond, previous_to_this);
459 return cond;
462 /* Remove those iterations of "domain" that have an earlier iteration
463 * (based on "sign") in the final dimension where "skip" is satisfied.
464 * If "apply_skip_map" is set, then "skip_map" is first applied
465 * to the embedded skip condition before removing it from the domain.
467 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
468 __isl_take isl_set *skip, int sign,
469 int apply_skip_map, __isl_keep isl_map *skip_map)
471 if (apply_skip_map)
472 skip = isl_set_apply(skip, isl_map_copy(skip_map));
473 skip = isl_set_intersect(skip , isl_set_copy(domain));
474 return isl_set_subtract(domain, after(skip, sign));
477 /* Create a single-dimensional multi-affine expression on the domain space
478 * of "pc" that is equal to the final dimension of this domain.
479 * "loop_nr" is the sequence number of the corresponding loop.
480 * If "id" is not NULL, then it is used as the output tuple name.
481 * Otherwise, the name is constructed as L_<loop_nr>.
483 static __isl_give isl_multi_aff *map_to_last(__isl_keep pet_context *pc,
484 int loop_nr, __isl_keep isl_id *id)
486 int pos;
487 isl_space *space;
488 isl_local_space *ls;
489 isl_aff *aff;
490 isl_multi_aff *ma;
491 char name[50];
492 isl_id *label;
494 space = pet_context_get_space(pc);
495 pos = isl_space_dim(space, isl_dim_set) - 1;
496 ls = isl_local_space_from_space(space);
497 aff = isl_aff_var_on_domain(ls, isl_dim_set, pos);
498 ma = isl_multi_aff_from_aff(aff);
500 if (id) {
501 label = isl_id_copy(id);
502 } else {
503 snprintf(name, sizeof(name), "L_%d", loop_nr);
504 label = isl_id_alloc(pet_context_get_ctx(pc), name, NULL);
506 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, label);
508 return ma;
511 /* Create an affine expression that maps elements
512 * of an array "id_test" to the previous element in the final dimension
513 * (according to "inc"), provided this element belongs to "domain".
514 * That is, create the affine expression
516 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
518 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
519 __isl_take isl_set *domain, __isl_take isl_val *inc)
521 int pos;
522 isl_space *space;
523 isl_aff *aff;
524 isl_pw_aff *pa;
525 isl_multi_aff *ma;
526 isl_multi_pw_aff *prev;
528 pos = isl_set_dim(domain, isl_dim_set) - 1;
529 space = isl_set_get_space(domain);
530 space = isl_space_map_from_set(space);
531 ma = isl_multi_aff_identity(space);
532 aff = isl_multi_aff_get_aff(ma, pos);
533 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
534 ma = isl_multi_aff_set_aff(ma, pos, aff);
535 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
536 prev = isl_multi_pw_aff_from_multi_aff(ma);
537 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
538 pa = isl_pw_aff_intersect_domain(pa, domain);
539 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
540 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
542 return prev;
545 /* Add an implication to "scop" expressing that if an element of
546 * virtual array "id_test" has value "satisfied" then all previous elements
547 * of this array (in the final dimension) also have that value.
548 * The set of previous elements is bounded by "domain".
549 * If "sign" is negative then the iterator
550 * is decreasing and we express that all subsequent array elements
551 * (but still defined previously) have the same value.
553 static struct pet_scop *add_implication(struct pet_scop *scop,
554 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
555 int satisfied)
557 int i, dim;
558 isl_space *space;
559 isl_map *map;
561 dim = isl_set_dim(domain, isl_dim_set);
562 domain = isl_set_set_tuple_id(domain, id_test);
563 space = isl_space_map_from_set(isl_set_get_space(domain));
564 map = isl_map_universe(space);
565 for (i = 0; i + 1 < dim; ++i)
566 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
567 if (sign > 0)
568 map = isl_map_order_ge(map,
569 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
570 else
571 map = isl_map_order_le(map,
572 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
573 map = isl_map_intersect_range(map, domain);
574 scop = pet_scop_add_implication(scop, map, satisfied);
576 return scop;
579 /* Add a filter to "scop" that imposes that it is only executed
580 * when the variable identified by "id_test" has a zero value
581 * for all previous iterations of "domain".
583 * In particular, add a filter that imposes that the array
584 * has a zero value at the previous iteration of domain and
585 * add an implication that implies that it then has that
586 * value for all previous iterations.
588 static struct pet_scop *scop_add_break(struct pet_scop *scop,
589 __isl_take isl_id *id_test, __isl_take isl_set *domain,
590 __isl_take isl_val *inc)
592 isl_multi_pw_aff *prev;
593 int sign = isl_val_sgn(inc);
595 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
596 scop = add_implication(scop, id_test, domain, sign, 0);
597 scop = pet_scop_filter(scop, prev, 0);
599 return scop;
602 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
603 __isl_keep pet_context *pc, struct pet_state *state);
605 /* Construct a pet_scop for an infinite loop around the given body
606 * within the context "pc".
607 * "loop_id" is the label on the loop or NULL if there is no such label.
609 * The domain of "pc" has already been extended with an infinite loop
611 * { [t] : t >= 0 }
613 * We extract a pet_scop for the body and then embed it in a loop with
614 * schedule
616 * { [outer,t] -> [t] }
618 * If the body contains any break, then it is taken into
619 * account in apply_affine_break (if the skip condition is affine)
620 * or in scop_add_break (if the skip condition is not affine).
622 * Note that in case of an affine skip condition,
623 * since we are dealing with a loop without loop iterator,
624 * the skip condition cannot refer to the current loop iterator and
625 * so effectively, the effect on the iteration domain is of the form
627 * { [outer,0]; [outer,t] : t >= 1 and not skip }
629 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
630 __isl_keep isl_id *loop_id, __isl_keep pet_context *pc,
631 struct pet_state *state)
633 isl_ctx *ctx;
634 isl_id *id_test;
635 isl_set *domain;
636 isl_set *skip;
637 isl_multi_aff *sched;
638 struct pet_scop *scop;
639 int has_affine_break;
640 int has_var_break;
642 ctx = pet_tree_get_ctx(body);
643 domain = pet_context_get_domain(pc);
644 sched = map_to_last(pc, state->n_loop++, loop_id);
646 scop = scop_from_tree(body, pc, state);
648 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
649 if (has_affine_break)
650 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
651 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
652 if (has_var_break)
653 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
655 scop = pet_scop_reset_skips(scop);
656 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
657 if (has_affine_break) {
658 domain = apply_affine_break(domain, skip, 1, 0, NULL);
659 scop = pet_scop_intersect_domain_prefix(scop,
660 isl_set_copy(domain));
662 if (has_var_break)
663 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
664 else
665 isl_set_free(domain);
667 return scop;
670 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
672 * for (;;)
673 * body
675 * within the context "pc".
677 * Extend the domain of "pc" with an extra inner loop
679 * { [t] : t >= 0 }
681 * and construct the scop in scop_from_infinite_loop.
683 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
684 __isl_keep pet_context *pc, struct pet_state *state)
686 struct pet_scop *scop;
688 pc = pet_context_copy(pc);
689 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
691 pc = pet_context_add_infinite_loop(pc);
693 scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
695 pet_context_free(pc);
697 return scop;
700 /* Construct a pet_scop for a while loop of the form
702 * while (pa)
703 * body
705 * within the context "pc".
707 * The domain of "pc" has already been extended with an infinite loop
709 * { [t] : t >= 0 }
711 * Here, we add the constraints on the outer loop iterators
712 * implied by "pa" and construct the scop in scop_from_infinite_loop.
713 * Note that the intersection with these constraints
714 * may result in an empty loop.
716 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
717 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
718 struct pet_state *state)
720 struct pet_scop *scop;
721 isl_set *dom, *local;
722 isl_set *valid;
724 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
725 dom = isl_pw_aff_non_zero_set(pa);
726 local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
727 pc = pet_context_intersect_domain(pc, local);
728 scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
729 scop = pet_scop_restrict(scop, dom);
730 scop = pet_scop_restrict_context(scop, valid);
732 pet_context_free(pc);
733 return scop;
736 /* Construct a scop for a while, given the scops for the condition
737 * and the body, the filter identifier and the iteration domain of
738 * the while loop.
740 * In particular, the scop for the condition is filtered to depend
741 * on "id_test" evaluating to true for all previous iterations
742 * of the loop, while the scop for the body is filtered to depend
743 * on "id_test" evaluating to true for all iterations up to the
744 * current iteration.
745 * The actual filter only imposes that this virtual array has
746 * value one on the previous or the current iteration.
747 * The fact that this condition also applies to the previous
748 * iterations is enforced by an implication.
750 * These filtered scops are then combined into a single scop,
751 * with the condition scop scheduled before the body scop.
753 * "sign" is positive if the iterator increases and negative
754 * if it decreases.
756 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
757 struct pet_scop *scop_body, __isl_take isl_id *id_test,
758 __isl_take isl_set *domain, __isl_take isl_val *inc)
760 isl_ctx *ctx = isl_set_get_ctx(domain);
761 isl_space *space;
762 isl_multi_pw_aff *test_index;
763 isl_multi_pw_aff *prev;
764 int sign = isl_val_sgn(inc);
765 struct pet_scop *scop;
767 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
768 scop_cond = pet_scop_filter(scop_cond, prev, 1);
770 space = isl_space_map_from_set(isl_set_get_space(domain));
771 test_index = isl_multi_pw_aff_identity(space);
772 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
773 isl_id_copy(id_test));
774 scop_body = pet_scop_filter(scop_body, test_index, 1);
776 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
777 scop = add_implication(scop, id_test, domain, sign, 1);
779 return scop;
782 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
783 * evaluating "cond" and writing the result to a virtual scalar,
784 * as expressed by "index".
785 * The expression "cond" has not yet been evaluated in the context of "pc".
786 * Do so within the context "pc".
787 * The location of the statement is set to "loc".
789 static struct pet_scop *scop_from_non_affine_condition(
790 __isl_take pet_expr *cond, int stmt_nr,
791 __isl_take isl_multi_pw_aff *index,
792 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
794 pet_expr *expr, *write;
796 cond = pet_context_evaluate_expr(pc, cond);
798 write = pet_expr_from_index(index);
799 write = pet_expr_access_set_write(write, 1);
800 write = pet_expr_access_set_read(write, 0);
801 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
803 return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
806 /* Given that "scop" has an affine skip condition of type pet_skip_now,
807 * apply this skip condition to the domain of "pc".
808 * That is, remove the elements satisfying the skip condition from
809 * the domain of "pc".
811 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
812 struct pet_scop *scop)
814 isl_set *domain, *skip;
816 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
817 domain = pet_context_get_domain(pc);
818 domain = isl_set_subtract(domain, skip);
819 pc = pet_context_intersect_domain(pc, domain);
821 return pc;
824 /* Add a scop for evaluating the loop increment "inc" at the end
825 * of a loop body "scop" within the context "pc".
827 * The skip conditions resulting from continue statements inside
828 * the body do not apply to "inc", but those resulting from break
829 * statements do need to get applied.
831 static struct pet_scop *scop_add_inc(struct pet_scop *scop,
832 __isl_take pet_expr *inc, __isl_take pet_loc *loc,
833 __isl_keep pet_context *pc, struct pet_state *state)
835 struct pet_scop *scop_inc;
837 pc = pet_context_copy(pc);
839 if (pet_scop_has_skip(scop, pet_skip_later)) {
840 isl_multi_pw_aff *skip;
841 skip = pet_scop_get_skip(scop, pet_skip_later);
842 scop = pet_scop_set_skip(scop, pet_skip_now, skip);
843 if (pet_scop_has_affine_skip(scop, pet_skip_now))
844 pc = apply_affine_continue(pc, scop);
845 } else
846 pet_scop_reset_skip(scop, pet_skip_now);
847 scop_inc = scop_from_expr(inc, state->n_stmt++, loc, pc);
848 scop = pet_scop_add_seq(state->ctx, scop, scop_inc);
850 pet_context_free(pc);
852 return scop;
855 /* Construct a generic while scop, with iteration domain
856 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
857 * "loop_id" is the label on the loop or NULL if there is no such label.
858 * The domain of "pc" has already been extended with this infinite loop
860 * { [t] : t >= 0 }
862 * The scop consists of two parts,
863 * one for evaluating the condition "cond" and one for the body.
864 * If "expr_inc" is not NULL, then a scop for evaluating this expression
865 * is added at the end of the body,
866 * after replacing any skip conditions resulting from continue statements
867 * by the skip conditions resulting from break statements (if any).
869 * The schedules are combined as a sequence to reflect that the condition is
870 * evaluated before the body is executed and the body is filtered to depend
871 * on the result of the condition evaluating to true on all iterations
872 * up to the current iteration, while the evaluation of the condition itself
873 * is filtered to depend on the result of the condition evaluating to true
874 * on all previous iterations.
875 * The context of the scop representing the body is dropped
876 * because we don't know how many times the body will be executed,
877 * if at all.
879 * If the body contains any break, then it is taken into
880 * account in apply_affine_break (if the skip condition is affine)
881 * or in scop_add_break (if the skip condition is not affine).
883 * Note that in case of an affine skip condition,
884 * since we are dealing with a loop without loop iterator,
885 * the skip condition cannot refer to the current loop iterator and
886 * so effectively, the effect on the iteration domain is of the form
888 * { [outer,0]; [outer,t] : t >= 1 and not skip }
890 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
891 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
892 __isl_keep isl_id *loop_id, __isl_take pet_expr *expr_inc,
893 __isl_take pet_context *pc, struct pet_state *state)
895 isl_ctx *ctx;
896 isl_id *id_test, *id_break_test;
897 isl_space *space;
898 isl_multi_pw_aff *test_index;
899 isl_set *domain;
900 isl_set *skip;
901 isl_multi_aff *sched;
902 struct pet_scop *scop, *scop_body;
903 int has_affine_break;
904 int has_var_break;
906 ctx = state->ctx;
907 space = pet_context_get_space(pc);
908 test_index = pet_create_test_index(space, state->n_test++);
909 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
910 isl_multi_pw_aff_copy(test_index),
911 pet_loc_copy(loc), pc);
912 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
913 domain = pet_context_get_domain(pc);
914 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
915 test_index, state->int_size);
917 sched = map_to_last(pc, state->n_loop++, loop_id);
919 scop_body = scop_from_tree(tree_body, pc, state);
921 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
922 if (has_affine_break)
923 skip = pet_scop_get_affine_skip_domain(scop_body,
924 pet_skip_later);
925 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
926 if (has_var_break)
927 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
929 scop_body = pet_scop_reset_context(scop_body);
930 if (expr_inc)
931 scop_body = scop_add_inc(scop_body, expr_inc, loc, pc, state);
932 else
933 pet_loc_free(loc);
934 scop_body = pet_scop_reset_skips(scop_body);
936 if (has_affine_break) {
937 domain = apply_affine_break(domain, skip, 1, 0, NULL);
938 scop = pet_scop_intersect_domain_prefix(scop,
939 isl_set_copy(domain));
940 scop_body = pet_scop_intersect_domain_prefix(scop_body,
941 isl_set_copy(domain));
943 if (has_var_break) {
944 scop = scop_add_break(scop, isl_id_copy(id_break_test),
945 isl_set_copy(domain), isl_val_one(ctx));
946 scop_body = scop_add_break(scop_body, id_break_test,
947 isl_set_copy(domain), isl_val_one(ctx));
949 scop = scop_add_while(scop, scop_body, id_test, isl_set_copy(domain),
950 isl_val_one(ctx));
952 scop = pet_scop_embed(scop, domain, sched);
954 pet_context_free(pc);
955 return scop;
958 /* Check if the while loop is of the form
960 * while (affine expression)
961 * body
963 * If so, call scop_from_affine_while to construct a scop.
965 * Otherwise, pass control to scop_from_non_affine_while.
967 * "pc" is the context in which the affine expressions in the scop are created.
968 * The domain of "pc" is extended with an infinite loop
970 * { [t] : t >= 0 }
972 * before passing control to scop_from_affine_while or
973 * scop_from_non_affine_while.
975 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
976 __isl_keep pet_context *pc, struct pet_state *state)
978 pet_expr *cond_expr;
979 isl_pw_aff *pa;
981 if (!tree)
982 return NULL;
984 pc = pet_context_copy(pc);
985 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
987 cond_expr = pet_expr_copy(tree->u.l.cond);
988 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
989 pa = pet_expr_extract_affine_condition(cond_expr, pc);
990 pet_expr_free(cond_expr);
992 pc = pet_context_add_infinite_loop(pc);
994 if (!pa)
995 goto error;
997 if (!isl_pw_aff_involves_nan(pa))
998 return scop_from_affine_while(tree, pa, pc, state);
999 isl_pw_aff_free(pa);
1000 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1001 pet_tree_get_loc(tree), tree->u.l.body,
1002 tree->label, NULL, pc, state);
1003 error:
1004 pet_context_free(pc);
1005 return NULL;
1008 /* Check whether "cond" expresses a simple loop bound
1009 * on the final set dimension.
1010 * In particular, if "up" is set then "cond" should contain only
1011 * upper bounds on the final set dimension.
1012 * Otherwise, it should contain only lower bounds.
1014 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
1016 int pos;
1018 pos = isl_set_dim(cond, isl_dim_set) - 1;
1019 if (isl_val_is_pos(inc))
1020 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
1021 else
1022 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
1025 /* Extend a condition on a given iteration of a loop to one that
1026 * imposes the same condition on all previous iterations.
1027 * "domain" expresses the lower [upper] bound on the iterations
1028 * when inc is positive [negative] in its final dimension.
1030 * In particular, we construct the condition (when inc is positive)
1032 * forall i' : (domain(i') and i' <= i) => cond(i')
1034 * (where "<=" applies to the final dimension)
1035 * which is equivalent to
1037 * not exists i' : domain(i') and i' <= i and not cond(i')
1039 * We construct this set by subtracting the satisfying cond from domain,
1040 * applying a map
1042 * { [i'] -> [i] : i' <= i }
1044 * and then subtracting the result from domain again.
1046 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
1047 __isl_take isl_set *domain, __isl_take isl_val *inc)
1049 isl_space *space;
1050 isl_map *previous_to_this;
1051 int i, dim;
1053 dim = isl_set_dim(cond, isl_dim_set);
1054 space = isl_space_map_from_set(isl_set_get_space(cond));
1055 previous_to_this = isl_map_universe(space);
1056 for (i = 0; i + 1 < dim; ++i)
1057 previous_to_this = isl_map_equate(previous_to_this,
1058 isl_dim_in, i, isl_dim_out, i);
1059 if (isl_val_is_pos(inc))
1060 previous_to_this = isl_map_order_le(previous_to_this,
1061 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1062 else
1063 previous_to_this = isl_map_order_ge(previous_to_this,
1064 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1066 cond = isl_set_subtract(isl_set_copy(domain), cond);
1067 cond = isl_set_apply(cond, previous_to_this);
1068 cond = isl_set_subtract(domain, cond);
1070 isl_val_free(inc);
1072 return cond;
1075 /* Given an initial value of the form
1077 * { [outer,i] -> init(outer) }
1079 * construct a domain of the form
1081 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1083 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
1084 __isl_take isl_val *inc)
1086 int dim;
1087 isl_aff *aff;
1088 isl_space *space;
1089 isl_local_space *ls;
1090 isl_set *set;
1092 dim = isl_pw_aff_dim(init, isl_dim_in);
1094 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
1095 space = isl_pw_aff_get_domain_space(init);
1096 ls = isl_local_space_from_space(space);
1097 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1098 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
1099 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1101 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
1102 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1104 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
1105 set = isl_set_project_out(set, isl_dim_set, dim, 1);
1107 return set;
1110 /* Assuming "cond" represents a bound on a loop where the loop
1111 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1112 * is possible.
1114 * Under the given assumptions, wrapping is only possible if "cond" allows
1115 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1116 * increasing iterator and 0 in case of a decreasing iterator.
1118 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
1119 __isl_keep isl_val *inc)
1121 int cw;
1122 isl_ctx *ctx;
1123 isl_val *limit;
1124 isl_set *test;
1126 test = isl_set_copy(cond);
1128 ctx = isl_set_get_ctx(test);
1129 if (isl_val_is_neg(inc))
1130 limit = isl_val_zero(ctx);
1131 else {
1132 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1133 limit = isl_val_2exp(limit);
1134 limit = isl_val_sub_ui(limit, 1);
1137 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
1138 cw = !isl_set_is_empty(test);
1139 isl_set_free(test);
1141 return cw;
1144 /* Given a space
1146 * { [outer, v] },
1148 * construct the following affine expression on this space
1150 * { [outer, v] -> [outer, v mod 2^width] }
1152 * where width is the number of bits used to represent the values
1153 * of the unsigned variable "iv".
1155 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
1156 __isl_keep pet_expr *iv)
1158 int dim;
1159 isl_aff *aff;
1160 isl_multi_aff *ma;
1162 dim = isl_space_dim(space, isl_dim_set);
1164 space = isl_space_map_from_set(space);
1165 ma = isl_multi_aff_identity(space);
1167 aff = isl_multi_aff_get_aff(ma, dim - 1);
1168 aff = pet_wrap_aff(aff, pet_expr_get_type_size(iv));
1169 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
1171 return ma;
1174 /* Given two sets in the space
1176 * { [l,i] },
1178 * where l represents the outer loop iterators, compute the set
1179 * of values of l that ensure that "set1" is a subset of "set2".
1181 * set1 is a subset of set2 if
1183 * forall i: set1(l,i) => set2(l,i)
1185 * or
1187 * not exists i: set1(l,i) and not set2(l,i)
1189 * i.e.,
1191 * not exists i: (set1 \ set2)(l,i)
1193 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
1194 __isl_take isl_set *set2)
1196 int pos;
1198 pos = isl_set_dim(set1, isl_dim_set) - 1;
1199 set1 = isl_set_subtract(set1, set2);
1200 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
1201 return isl_set_complement(set1);
1204 /* Compute the set of outer iterator values for which "cond" holds
1205 * on the next iteration of the inner loop for each element of "dom".
1207 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1208 * to the outer loop iterators), plug that into "cond"
1209 * and then compute the set of outer iterators for which "dom" is a subset
1210 * of the result.
1212 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
1213 __isl_take isl_set *dom, __isl_take isl_val *inc)
1215 int pos;
1216 isl_space *space;
1217 isl_aff *aff;
1218 isl_multi_aff *ma;
1220 pos = isl_set_dim(dom, isl_dim_set) - 1;
1221 space = isl_set_get_space(dom);
1222 space = isl_space_map_from_set(space);
1223 ma = isl_multi_aff_identity(space);
1224 aff = isl_multi_aff_get_aff(ma, pos);
1225 aff = isl_aff_add_constant_val(aff, inc);
1226 ma = isl_multi_aff_set_aff(ma, pos, aff);
1227 cond = isl_set_preimage_multi_aff(cond, ma);
1229 return enforce_subset(dom, cond);
1232 /* Construct a pet_scop for the initialization of the iterator
1233 * of the for loop "tree" within the context "pc" (i.e., the context
1234 * of the loop).
1236 static __isl_give pet_scop *scop_from_for_init(__isl_keep pet_tree *tree,
1237 __isl_keep pet_context *pc, struct pet_state *state)
1239 pet_expr *expr_iv, *init;
1240 int type_size;
1242 expr_iv = pet_expr_copy(tree->u.l.iv);
1243 type_size = pet_expr_get_type_size(expr_iv);
1244 init = pet_expr_copy(tree->u.l.init);
1245 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1246 return scop_from_expr(init, state->n_stmt++,
1247 pet_tree_get_loc(tree), pc);
1250 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1251 * In particular, "pc_init" represents the context of the loop,
1252 * whereas "pc" represents the context of the body of the loop and
1253 * has already had its domain extended with an infinite loop
1255 * { [t] : t >= 0 }
1257 * The for loop has the form
1259 * for (iv = init; cond; iv += inc)
1260 * body;
1262 * and is treated as
1264 * iv = init;
1265 * while (cond) {
1266 * body;
1267 * iv += inc;
1270 * except that the skips resulting from any continue statements
1271 * in body do not apply to the increment, but are replaced by the skips
1272 * resulting from break statements.
1274 * If the loop iterator is declared in the for loop, then it is killed before
1275 * and after the loop.
1277 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1278 __isl_keep pet_context *pc_init, __isl_take pet_context *pc,
1279 struct pet_state *state)
1281 int declared;
1282 isl_id *iv;
1283 pet_expr *expr_iv, *inc;
1284 struct pet_scop *scop_init, *scop;
1285 int type_size;
1286 struct pet_array *array;
1287 struct pet_scop *scop_kill;
1289 iv = pet_expr_access_get_id(tree->u.l.iv);
1290 pc = pet_context_clear_value(pc, iv);
1292 declared = tree->u.l.declared;
1294 scop_init = scop_from_for_init(tree, pc_init, state);
1296 expr_iv = pet_expr_copy(tree->u.l.iv);
1297 type_size = pet_expr_get_type_size(expr_iv);
1298 inc = pet_expr_copy(tree->u.l.inc);
1299 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1301 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1302 pet_tree_get_loc(tree), tree->u.l.body, tree->label,
1303 inc, pet_context_copy(pc), state);
1305 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1307 pet_context_free(pc);
1309 if (!declared)
1310 return scop;
1312 array = extract_array(tree->u.l.iv, pc_init, state);
1313 if (array)
1314 array->declared = 1;
1315 scop_kill = kill(pet_tree_get_loc(tree), array, pc_init, state);
1316 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1317 scop_kill = kill(pet_tree_get_loc(tree), array, pc_init, state);
1318 scop_kill = pet_scop_add_array(scop_kill, array);
1319 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1321 return scop;
1324 /* Given an access expression "expr", is the variable accessed by
1325 * "expr" assigned anywhere inside "tree"?
1327 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1329 int assigned = 0;
1330 isl_id *id;
1332 id = pet_expr_access_get_id(expr);
1333 assigned = pet_tree_writes(tree, id);
1334 isl_id_free(id);
1336 return assigned;
1339 /* Are all nested access parameters in "pa" allowed given "tree".
1340 * In particular, is none of them written by anywhere inside "tree".
1342 * If "tree" has any continue or break nodes in the current loop level,
1343 * then no nested access parameters are allowed.
1344 * In particular, if there is any nested access in a guard
1345 * for a piece of code containing a "continue", then we want to introduce
1346 * a separate statement for evaluating this guard so that we can express
1347 * that the result is false for all previous iterations.
1349 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1350 __isl_keep pet_tree *tree)
1352 int i, nparam;
1354 if (!tree)
1355 return -1;
1357 if (!pet_nested_any_in_pw_aff(pa))
1358 return 1;
1360 if (pet_tree_has_continue_or_break(tree))
1361 return 0;
1363 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1364 for (i = 0; i < nparam; ++i) {
1365 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1366 pet_expr *expr;
1367 int allowed;
1369 if (!pet_nested_in_id(id)) {
1370 isl_id_free(id);
1371 continue;
1374 expr = pet_nested_extract_expr(id);
1375 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1376 !is_assigned(expr, tree);
1378 pet_expr_free(expr);
1379 isl_id_free(id);
1381 if (!allowed)
1382 return 0;
1385 return 1;
1388 /* Internal data structure for collect_local.
1389 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1390 * "local" collects the results.
1392 struct pet_tree_collect_local_data {
1393 pet_context *pc;
1394 struct pet_state *state;
1395 isl_union_set *local;
1398 /* Add the variable accessed by "var" to data->local.
1399 * We extract a representation of the variable from
1400 * the pet_array constructed using extract_array
1401 * to ensure consistency with the rest of the scop.
1403 static int add_local(struct pet_tree_collect_local_data *data,
1404 __isl_keep pet_expr *var)
1406 struct pet_array *array;
1407 isl_set *universe;
1409 array = extract_array(var, data->pc, data->state);
1410 if (!array)
1411 return -1;
1413 universe = isl_set_universe(isl_set_get_space(array->extent));
1414 data->local = isl_union_set_add_set(data->local, universe);
1415 pet_array_free(array);
1417 return 0;
1420 /* If the node "tree" declares a variable, then add it to
1421 * data->local.
1423 static int extract_local_var(__isl_keep pet_tree *tree, void *user)
1425 enum pet_tree_type type;
1426 struct pet_tree_collect_local_data *data = user;
1428 type = pet_tree_get_type(tree);
1429 if (type == pet_tree_decl || type == pet_tree_decl_init)
1430 return add_local(data, tree->u.d.var);
1432 return 0;
1435 /* If the node "tree" is a for loop that declares its induction variable,
1436 * then add it this induction variable to data->local.
1438 static int extract_local_iterator(__isl_keep pet_tree *tree, void *user)
1440 struct pet_tree_collect_local_data *data = user;
1442 if (pet_tree_get_type(tree) == pet_tree_for && tree->u.l.declared)
1443 return add_local(data, tree->u.l.iv);
1445 return 0;
1448 /* Collect and return all local variables of the for loop represented
1449 * by "tree", with "scop" the corresponding pet_scop.
1450 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1452 * We collect not only the variables that are declared inside "tree",
1453 * but also the loop iterators that are declared anywhere inside
1454 * any possible macro statements in "scop".
1455 * The latter also appear as declared variable in the scop,
1456 * whereas other declared loop iterators only appear implicitly
1457 * in the iteration domains.
1459 static __isl_give isl_union_set *collect_local(struct pet_scop *scop,
1460 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1461 struct pet_state *state)
1463 int i;
1464 isl_ctx *ctx;
1465 struct pet_tree_collect_local_data data = { pc, state };
1467 ctx = pet_tree_get_ctx(tree);
1468 data.local = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
1470 if (pet_tree_foreach_sub_tree(tree, &extract_local_var, &data) < 0)
1471 return isl_union_set_free(data.local);
1473 for (i = 0; i < scop->n_stmt; ++i) {
1474 pet_tree *body = scop->stmts[i]->body;
1475 if (pet_tree_foreach_sub_tree(body, &extract_local_iterator,
1476 &data) < 0)
1477 return isl_union_set_free(data.local);
1480 return data.local;
1483 /* Add an independence to "scop" if the for node "tree" was marked
1484 * independent.
1485 * "domain" is the set of loop iterators, with the current for loop
1486 * innermost. If "sign" is positive, then the inner iterator increases.
1487 * Otherwise it decreases.
1488 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1490 * If the tree was marked, then collect all local variables and
1491 * add an independence.
1493 static struct pet_scop *set_independence(struct pet_scop *scop,
1494 __isl_keep pet_tree *tree, __isl_keep isl_set *domain, int sign,
1495 __isl_keep pet_context *pc, struct pet_state *state)
1497 isl_union_set *local;
1499 if (!tree->u.l.independent)
1500 return scop;
1502 local = collect_local(scop, tree, pc, state);
1503 scop = pet_scop_set_independent(scop, domain, local, sign);
1505 return scop;
1508 /* Add a scop for assigning to the variable corresponding to the loop
1509 * iterator the result of adding the increment to the loop iterator
1510 * at the end of a loop body "scop" within the context "pc".
1511 * "tree" represents the for loop.
1513 * The increment is of the form
1515 * iv = iv + inc
1517 * Note that "iv" on the right hand side will be evaluated in terms
1518 * of the (possibly virtual) loop iterator, i.e., the inner dimension
1519 * of the domain, while "iv" on the left hand side will not be evaluated
1520 * (because it is a write) and will continue to refer to the original
1521 * variable.
1523 static __isl_give pet_scop *add_iterator_assignment(__isl_take pet_scop *scop,
1524 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1525 struct pet_state *state)
1527 int type_size;
1528 pet_expr *expr, *iv, *inc;
1530 iv = pet_expr_copy(tree->u.l.iv);
1531 type_size = pet_expr_get_type_size(iv);
1532 iv = pet_expr_access_set_write(iv, 0);
1533 iv = pet_expr_access_set_read(iv, 1);
1534 inc = pet_expr_copy(tree->u.l.inc);
1535 expr = pet_expr_new_binary(type_size, pet_op_add, iv, inc);
1536 iv = pet_expr_copy(tree->u.l.iv);
1537 expr = pet_expr_new_binary(type_size, pet_op_assign, iv, expr);
1539 scop = scop_add_inc(scop, expr, pet_tree_get_loc(tree), pc, state);
1541 return scop;
1544 /* Construct a pet_scop for a for tree with static affine initialization
1545 * and constant increment within the context "pc".
1546 * The domain of "pc" has already been extended with an (at this point
1547 * unbounded) inner loop iterator corresponding to the current for loop.
1549 * The condition is allowed to contain nested accesses, provided
1550 * they are not being written to inside the body of the loop.
1551 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1552 * essentially treated as a while loop, with iteration domain
1553 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1555 * We extract a pet_scop for the body after intersecting the domain of "pc"
1557 * { [l,i] : i >= init and condition' }
1559 * or
1561 * { [l,i] : i <= init and condition' }
1563 * Where condition' is equal to condition if the latter is
1564 * a simple upper [lower] bound and a condition that is extended
1565 * to apply to all previous iterations otherwise.
1566 * Afterwards, the schedule of the pet_scop is extended with
1568 * { [l,i] -> [i] }
1570 * or
1572 * { [l,i] -> [-i] }
1574 * If the condition is non-affine, then we drop the condition from the
1575 * iteration domain and instead create a separate statement
1576 * for evaluating the condition. The body is then filtered to depend
1577 * on the result of the condition evaluating to true on all iterations
1578 * up to the current iteration, while the evaluation the condition itself
1579 * is filtered to depend on the result of the condition evaluating to true
1580 * on all previous iterations.
1581 * The context of the scop representing the body is dropped
1582 * because we don't know how many times the body will be executed,
1583 * if at all.
1585 * If the stride of the loop is not 1, then "i >= init" is replaced by
1587 * (exists a: i = init + stride * a and a >= 0)
1589 * If the loop iterator i is unsigned, then wrapping may occur.
1590 * We therefore use a virtual iterator instead that does not wrap.
1591 * However, the condition in the code applies
1592 * to the wrapped value, so we need to change condition(l,i)
1593 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1594 * to the original iterator by the wrapping of the virtual iterator.
1595 * Note that there may be no need to perform this final wrapping
1596 * if the loop condition (after wrapping) satisfies certain conditions.
1597 * However, the is_simple_bound condition is not enough since it doesn't
1598 * check if there even is an upper bound.
1600 * Wrapping on unsigned iterators can be avoided entirely if
1601 * the loop condition is simple, the loop iterator is incremented
1602 * [decremented] by one and the last value before wrapping cannot
1603 * possibly satisfy the loop condition.
1605 * Valid outer iterators for a for loop are those for which the initial
1606 * value itself, the increment on each domain iteration and
1607 * the condition on both the initial value and
1608 * the result of incrementing the iterator for each iteration of the domain
1609 * can be evaluated.
1610 * If the loop condition is non-affine, then we only consider validity
1611 * of the initial value.
1613 * If the loop iterator was not declared inside the loop header,
1614 * then the variable corresponding to this loop iterator is assigned
1615 * the result of adding the increment at the end of the loop body.
1616 * The assignment of the initial value is taken care of by
1617 * scop_from_affine_for_init.
1619 * If the body contains any break, then we keep track of it in "skip"
1620 * (if the skip condition is affine) or it is handled in scop_add_break
1621 * (if the skip condition is not affine).
1622 * Note that the affine break condition needs to be considered with
1623 * respect to previous iterations in the virtual domain (if any).
1625 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1626 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1627 __isl_take isl_val *inc, __isl_take pet_context *pc,
1628 struct pet_state *state)
1630 isl_set *domain;
1631 isl_multi_aff *sched;
1632 isl_set *cond = NULL;
1633 isl_set *skip = NULL;
1634 isl_id *id_test = NULL, *id_break_test;
1635 struct pet_scop *scop, *scop_cond = NULL;
1636 int pos;
1637 int is_one;
1638 int is_unsigned;
1639 int is_simple;
1640 int is_virtual;
1641 int is_non_affine;
1642 int has_affine_break;
1643 int has_var_break;
1644 isl_map *rev_wrap = NULL;
1645 isl_map *init_val_map;
1646 isl_pw_aff *pa;
1647 isl_set *valid_init;
1648 isl_set *valid_cond;
1649 isl_set *valid_cond_init;
1650 isl_set *valid_cond_next;
1651 isl_set *valid_inc;
1652 pet_expr *cond_expr;
1653 pet_context *pc_nested;
1655 pos = pet_context_dim(pc) - 1;
1657 domain = pet_context_get_domain(pc);
1658 cond_expr = pet_expr_copy(tree->u.l.cond);
1659 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1660 pc_nested = pet_context_copy(pc);
1661 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1662 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1663 pet_context_free(pc_nested);
1664 pet_expr_free(cond_expr);
1666 valid_inc = isl_pw_aff_domain(pa_inc);
1668 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1670 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1671 !is_nested_allowed(pa, tree->u.l.body);
1672 if (is_non_affine)
1673 pa = isl_pw_aff_free(pa);
1675 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1676 cond = isl_pw_aff_non_zero_set(pa);
1677 if (is_non_affine)
1678 cond = isl_set_universe(isl_set_get_space(domain));
1680 valid_cond = isl_set_coalesce(valid_cond);
1681 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1682 is_virtual = is_unsigned &&
1683 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1685 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1686 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1687 isl_dim_out, 0);
1688 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1689 isl_set_copy(valid_cond));
1690 if (is_one && !is_virtual) {
1691 isl_set *cond;
1693 isl_pw_aff_free(init_val);
1694 pa = pet_expr_extract_comparison(
1695 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1696 tree->u.l.iv, tree->u.l.init, pc);
1697 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1698 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1699 isl_set_dim(domain, isl_dim_set) - 1, 1);
1700 cond = isl_pw_aff_non_zero_set(pa);
1701 domain = isl_set_intersect(domain, cond);
1702 } else {
1703 isl_set *strided;
1705 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1706 strided = strided_domain(init_val, isl_val_copy(inc));
1707 domain = isl_set_intersect(domain, strided);
1710 if (is_virtual) {
1711 isl_multi_aff *wrap;
1712 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1713 pc = pet_context_preimage_domain(pc, wrap);
1714 rev_wrap = isl_map_from_multi_aff(wrap);
1715 rev_wrap = isl_map_reverse(rev_wrap);
1716 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1717 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1718 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1720 is_simple = is_simple_bound(cond, inc);
1721 if (!is_simple) {
1722 cond = isl_set_gist(cond, isl_set_copy(domain));
1723 is_simple = is_simple_bound(cond, inc);
1725 if (!is_simple)
1726 cond = valid_for_each_iteration(cond,
1727 isl_set_copy(domain), isl_val_copy(inc));
1728 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1729 domain = isl_set_intersect(domain, cond);
1730 sched = map_to_last(pc, state->n_loop++, tree->label);
1731 if (isl_val_is_neg(inc))
1732 sched = isl_multi_aff_neg(sched);
1734 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1735 isl_val_copy(inc));
1736 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1738 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1740 if (is_non_affine) {
1741 isl_space *space;
1742 isl_multi_pw_aff *test_index;
1743 space = isl_set_get_space(domain);
1744 test_index = pet_create_test_index(space, state->n_test++);
1745 scop_cond = scop_from_non_affine_condition(
1746 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1747 isl_multi_pw_aff_copy(test_index),
1748 pet_tree_get_loc(tree), pc);
1749 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1750 isl_dim_out);
1751 scop_cond = pet_scop_add_boolean_array(scop_cond,
1752 isl_set_copy(domain), test_index,
1753 state->int_size);
1756 scop = scop_from_tree(tree->u.l.body, pc, state);
1757 has_affine_break = scop &&
1758 pet_scop_has_affine_skip(scop, pet_skip_later);
1759 if (has_affine_break)
1760 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1761 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1762 if (has_var_break)
1763 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1764 if (is_non_affine) {
1765 scop = pet_scop_reset_context(scop);
1767 if (!tree->u.l.declared)
1768 scop = add_iterator_assignment(scop, tree, pc, state);
1769 scop = pet_scop_reset_skips(scop);
1770 scop = pet_scop_resolve_nested(scop);
1771 if (has_affine_break) {
1772 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1773 is_virtual, rev_wrap);
1774 scop = pet_scop_intersect_domain_prefix(scop,
1775 isl_set_copy(domain));
1777 isl_map_free(rev_wrap);
1778 if (has_var_break)
1779 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1780 isl_val_copy(inc));
1781 if (is_non_affine)
1782 scop = scop_add_while(scop_cond, scop, id_test,
1783 isl_set_copy(domain),
1784 isl_val_copy(inc));
1785 else
1786 scop = set_independence(scop, tree, domain, isl_val_sgn(inc),
1787 pc, state);
1788 scop = pet_scop_embed(scop, domain, sched);
1789 if (is_non_affine) {
1790 isl_set_free(valid_inc);
1791 } else {
1792 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1793 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1794 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1795 scop = pet_scop_restrict_context(scop, valid_inc);
1798 isl_val_free(inc);
1800 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1801 scop = pet_scop_restrict_context(scop, valid_init);
1803 pet_context_free(pc);
1804 return scop;
1807 /* Construct a pet_scop for a for tree with static affine initialization
1808 * and constant increment within the context "pc_init".
1809 * In particular, "pc_init" represents the context of the loop,
1810 * whereas the domain of "pc" has already been extended with an (at this point
1811 * unbounded) inner loop iterator corresponding to the current for loop.
1813 * If the loop iterator was not declared inside the loop header,
1814 * then add an assignment of the initial value to the loop iterator
1815 * before the loop. The construction of a pet_scop for the loop itself,
1816 * including updates to the loop iterator, is handled by scop_from_affine_for.
1818 static __isl_give pet_scop *scop_from_affine_for_init(__isl_keep pet_tree *tree,
1819 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1820 __isl_take isl_val *inc, __isl_keep pet_context *pc_init,
1821 __isl_take pet_context *pc, struct pet_state *state)
1823 pet_scop *scop_init, *scop;
1825 if (!tree->u.l.declared)
1826 scop_init = scop_from_for_init(tree, pc_init, state);
1828 scop = scop_from_affine_for(tree, init_val, pa_inc, inc, pc, state);
1830 if (!tree->u.l.declared)
1831 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1833 return scop;
1836 /* Construct a pet_scop for a for statement within the context of "pc".
1838 * We update the context to reflect the writes to the loop variable and
1839 * the writes inside the body.
1841 * Then we check if the initialization of the for loop
1842 * is a static affine value and the increment is a constant.
1843 * If so, we construct the pet_scop using scop_from_affine_for_init.
1844 * Otherwise, we treat the for loop as a while loop
1845 * in scop_from_non_affine_for.
1847 * Note that the initialization and the increment are extracted
1848 * in a context where the current loop iterator has been added
1849 * to the context. If these turn out not be affine, then we
1850 * have reconstruct the body context without an assignment
1851 * to this loop iterator, as this variable will then not be
1852 * treated as a dimension of the iteration domain, but as any
1853 * other variable.
1855 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1856 __isl_keep pet_context *init_pc, struct pet_state *state)
1858 isl_id *iv;
1859 isl_val *inc;
1860 isl_pw_aff *pa_inc, *init_val;
1861 pet_context *pc, *pc_init_val;
1863 if (!tree)
1864 return NULL;
1866 iv = pet_expr_access_get_id(tree->u.l.iv);
1867 pc = pet_context_copy(init_pc);
1868 pc = pet_context_add_inner_iterator(pc, iv);
1869 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1871 pc_init_val = pet_context_copy(pc);
1872 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1873 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1874 pet_context_free(pc_init_val);
1875 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1876 inc = pet_extract_cst(pa_inc);
1877 if (!pa_inc || !init_val || !inc)
1878 goto error;
1879 if (!isl_pw_aff_involves_nan(pa_inc) &&
1880 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1881 return scop_from_affine_for_init(tree, init_val, pa_inc, inc,
1882 init_pc, pc, state);
1884 isl_pw_aff_free(pa_inc);
1885 isl_pw_aff_free(init_val);
1886 isl_val_free(inc);
1887 pet_context_free(pc);
1889 pc = pet_context_copy(init_pc);
1890 pc = pet_context_add_infinite_loop(pc);
1891 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1892 return scop_from_non_affine_for(tree, init_pc, pc, state);
1893 error:
1894 isl_pw_aff_free(pa_inc);
1895 isl_pw_aff_free(init_val);
1896 isl_val_free(inc);
1897 pet_context_free(pc);
1898 return NULL;
1901 /* Check whether "expr" is an affine constraint within the context "pc".
1903 static int is_affine_condition(__isl_keep pet_expr *expr,
1904 __isl_keep pet_context *pc)
1906 isl_pw_aff *pa;
1907 int is_affine;
1909 pa = pet_expr_extract_affine_condition(expr, pc);
1910 if (!pa)
1911 return -1;
1912 is_affine = !isl_pw_aff_involves_nan(pa);
1913 isl_pw_aff_free(pa);
1915 return is_affine;
1918 /* Check if the given if statement is a conditional assignement
1919 * with a non-affine condition.
1921 * In particular we check if "stmt" is of the form
1923 * if (condition)
1924 * a = f(...);
1925 * else
1926 * a = g(...);
1928 * where the condition is non-affine and a is some array or scalar access.
1930 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1931 __isl_keep pet_context *pc)
1933 int equal;
1934 isl_ctx *ctx;
1935 pet_expr *expr1, *expr2;
1937 ctx = pet_tree_get_ctx(tree);
1938 if (!pet_options_get_detect_conditional_assignment(ctx))
1939 return 0;
1940 if (tree->type != pet_tree_if_else)
1941 return 0;
1942 if (tree->u.i.then_body->type != pet_tree_expr)
1943 return 0;
1944 if (tree->u.i.else_body->type != pet_tree_expr)
1945 return 0;
1946 expr1 = tree->u.i.then_body->u.e.expr;
1947 expr2 = tree->u.i.else_body->u.e.expr;
1948 if (pet_expr_get_type(expr1) != pet_expr_op)
1949 return 0;
1950 if (pet_expr_get_type(expr2) != pet_expr_op)
1951 return 0;
1952 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1953 return 0;
1954 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1955 return 0;
1956 expr1 = pet_expr_get_arg(expr1, 0);
1957 expr2 = pet_expr_get_arg(expr2, 0);
1958 equal = pet_expr_is_equal(expr1, expr2);
1959 pet_expr_free(expr1);
1960 pet_expr_free(expr2);
1961 if (equal < 0 || !equal)
1962 return 0;
1963 if (is_affine_condition(tree->u.i.cond, pc))
1964 return 0;
1966 return 1;
1969 /* Given that "tree" is of the form
1971 * if (condition)
1972 * a = f(...);
1973 * else
1974 * a = g(...);
1976 * where a is some array or scalar access, construct a pet_scop
1977 * corresponding to this conditional assignment within the context "pc".
1978 * "cond_pa" is an affine expression with nested accesses representing
1979 * the condition.
1981 * The constructed pet_scop then corresponds to the expression
1983 * a = condition ? f(...) : g(...)
1985 * All access relations in f(...) are intersected with condition
1986 * while all access relation in g(...) are intersected with the complement.
1988 static struct pet_scop *scop_from_conditional_assignment(
1989 __isl_keep pet_tree *tree, __isl_take isl_pw_aff *cond_pa,
1990 __isl_take pet_context *pc, struct pet_state *state)
1992 int type_size;
1993 isl_set *cond, *comp;
1994 isl_multi_pw_aff *index;
1995 pet_expr *expr1, *expr2;
1996 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1997 struct pet_scop *scop;
1999 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa));
2000 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa));
2001 index = isl_multi_pw_aff_from_pw_aff(cond_pa);
2003 expr1 = tree->u.i.then_body->u.e.expr;
2004 expr2 = tree->u.i.else_body->u.e.expr;
2006 pe_cond = pet_expr_from_index(index);
2008 pe_then = pet_expr_get_arg(expr1, 1);
2009 pe_then = pet_context_evaluate_expr(pc, pe_then);
2010 pe_then = pet_expr_restrict(pe_then, cond);
2011 pe_else = pet_expr_get_arg(expr2, 1);
2012 pe_else = pet_context_evaluate_expr(pc, pe_else);
2013 pe_else = pet_expr_restrict(pe_else, comp);
2014 pe_write = pet_expr_get_arg(expr1, 0);
2015 pe_write = pet_context_evaluate_expr(pc, pe_write);
2017 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
2018 type_size = pet_expr_get_type_size(pe_write);
2019 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
2021 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
2022 pet_tree_get_loc(tree), pc);
2024 pet_context_free(pc);
2026 return scop;
2029 /* Construct a pet_scop for a non-affine if statement within the context "pc".
2031 * We create a separate statement that writes the result
2032 * of the non-affine condition to a virtual scalar.
2033 * A constraint requiring the value of this virtual scalar to be one
2034 * is added to the iteration domains of the then branch.
2035 * Similarly, a constraint requiring the value of this virtual scalar
2036 * to be zero is added to the iteration domains of the else branch, if any.
2037 * We combine the schedules as a sequence to ensure that the virtual scalar
2038 * is written before it is read.
2040 * If there are any breaks or continues in the then and/or else
2041 * branches, then we may have to compute a new skip condition.
2042 * This is handled using a pet_skip_info object.
2043 * On initialization, the object checks if skip conditions need
2044 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
2045 * adds them in pet_skip_info_add.
2047 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
2048 __isl_take pet_context *pc, struct pet_state *state)
2050 int has_else;
2051 isl_space *space;
2052 isl_set *domain;
2053 isl_multi_pw_aff *test_index;
2054 struct pet_skip_info skip;
2055 struct pet_scop *scop, *scop_then, *scop_else = NULL;
2057 has_else = tree->type == pet_tree_if_else;
2059 space = pet_context_get_space(pc);
2060 test_index = pet_create_test_index(space, state->n_test++);
2061 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
2062 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
2063 pet_tree_get_loc(tree), pc);
2064 domain = pet_context_get_domain(pc);
2065 scop = pet_scop_add_boolean_array(scop, domain,
2066 isl_multi_pw_aff_copy(test_index), state->int_size);
2068 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
2069 if (has_else)
2070 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
2072 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
2073 has_else, 0);
2074 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
2076 scop_then = pet_scop_filter(scop_then,
2077 isl_multi_pw_aff_copy(test_index), 1);
2078 if (has_else) {
2079 scop_else = pet_scop_filter(scop_else, test_index, 0);
2080 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
2081 } else
2082 isl_multi_pw_aff_free(test_index);
2084 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
2086 scop = pet_skip_info_add(&skip, scop);
2088 pet_context_free(pc);
2089 return scop;
2092 /* Construct a pet_scop for an affine if statement within the context "pc".
2094 * The condition is added to the iteration domains of the then branch,
2095 * while the opposite of the condition in added to the iteration domains
2096 * of the else branch, if any.
2098 * If there are any breaks or continues in the then and/or else
2099 * branches, then we may have to compute a new skip condition.
2100 * This is handled using a pet_skip_info_if object.
2101 * On initialization, the object checks if skip conditions need
2102 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
2103 * adds them in pet_skip_info_add.
2105 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
2106 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
2107 struct pet_state *state)
2109 int has_else;
2110 isl_ctx *ctx;
2111 isl_set *set, *complement;
2112 isl_set *valid;
2113 struct pet_skip_info skip;
2114 struct pet_scop *scop, *scop_then, *scop_else = NULL;
2115 pet_context *pc_body;
2117 ctx = pet_tree_get_ctx(tree);
2119 has_else = tree->type == pet_tree_if_else;
2121 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
2122 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2124 pc_body = pet_context_copy(pc);
2125 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
2126 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
2127 pet_context_free(pc_body);
2128 if (has_else) {
2129 pc_body = pet_context_copy(pc);
2130 complement = isl_set_copy(valid);
2131 complement = isl_set_subtract(valid, isl_set_copy(set));
2132 pc_body = pet_context_intersect_domain(pc_body,
2133 isl_set_copy(complement));
2134 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
2135 pet_context_free(pc_body);
2138 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
2139 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
2140 isl_pw_aff_free(cond);
2142 scop = pet_scop_restrict(scop_then, set);
2144 if (has_else) {
2145 scop_else = pet_scop_restrict(scop_else, complement);
2146 scop = pet_scop_add_par(ctx, scop, scop_else);
2148 scop = pet_scop_resolve_nested(scop);
2149 scop = pet_scop_restrict_context(scop, valid);
2151 scop = pet_skip_info_add(&skip, scop);
2153 pet_context_free(pc);
2154 return scop;
2157 /* Construct a pet_scop for an if statement within the context "pc".
2159 * If the condition fits the pattern of a conditional assignment,
2160 * then it is handled by scop_from_conditional_assignment.
2161 * Note that the condition is only considered for a conditional assignment
2162 * if it is not static-affine. However, it should still convert
2163 * to an affine expression when nesting is allowed.
2165 * Otherwise, we check if the condition is affine.
2166 * If so, we construct the scop in scop_from_affine_if.
2167 * Otherwise, we construct the scop in scop_from_non_affine_if.
2169 * We allow the condition to be dynamic, i.e., to refer to
2170 * scalars or array elements that may be written to outside
2171 * of the given if statement. These nested accesses are then represented
2172 * as output dimensions in the wrapping iteration domain.
2173 * If it is also written _inside_ the then or else branch, then
2174 * we treat the condition as non-affine.
2175 * As explained in extract_non_affine_if, this will introduce
2176 * an extra statement.
2177 * For aesthetic reasons, we want this statement to have a statement
2178 * number that is lower than those of the then and else branches.
2179 * In order to evaluate if we will need such a statement, however, we
2180 * first construct scops for the then and else branches.
2181 * We therefore reserve a statement number if we might have to
2182 * introduce such an extra statement.
2184 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
2185 __isl_keep pet_context *pc, struct pet_state *state)
2187 int has_else;
2188 isl_pw_aff *cond;
2189 pet_expr *cond_expr;
2190 pet_context *pc_nested;
2192 if (!tree)
2193 return NULL;
2195 has_else = tree->type == pet_tree_if_else;
2197 pc = pet_context_copy(pc);
2198 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
2199 if (has_else)
2200 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
2202 cond_expr = pet_expr_copy(tree->u.i.cond);
2203 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
2204 pc_nested = pet_context_copy(pc);
2205 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
2206 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
2207 pet_context_free(pc_nested);
2208 pet_expr_free(cond_expr);
2210 if (!cond) {
2211 pet_context_free(pc);
2212 return NULL;
2215 if (isl_pw_aff_involves_nan(cond)) {
2216 isl_pw_aff_free(cond);
2217 return scop_from_non_affine_if(tree, pc, state);
2220 if (is_conditional_assignment(tree, pc))
2221 return scop_from_conditional_assignment(tree, cond, pc, state);
2223 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
2224 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
2225 isl_pw_aff_free(cond);
2226 return scop_from_non_affine_if(tree, pc, state);
2229 return scop_from_affine_if(tree, cond, pc, state);
2232 /* Return a one-dimensional multi piecewise affine expression that is equal
2233 * to the constant 1 and is defined over the given domain.
2235 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
2237 isl_local_space *ls;
2238 isl_aff *aff;
2240 ls = isl_local_space_from_space(space);
2241 aff = isl_aff_zero_on_domain(ls);
2242 aff = isl_aff_set_constant_si(aff, 1);
2244 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
2247 /* Construct a pet_scop for a continue statement with the given domain space.
2249 * We simply create an empty scop with a universal pet_skip_now
2250 * skip condition. This skip condition will then be taken into
2251 * account by the enclosing loop construct, possibly after
2252 * being incorporated into outer skip conditions.
2254 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
2255 __isl_take isl_space *space)
2257 struct pet_scop *scop;
2259 scop = pet_scop_empty(isl_space_copy(space));
2261 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
2263 return scop;
2266 /* Construct a pet_scop for a break statement with the given domain space.
2268 * We simply create an empty scop with both a universal pet_skip_now
2269 * skip condition and a universal pet_skip_later skip condition.
2270 * These skip conditions will then be taken into
2271 * account by the enclosing loop construct, possibly after
2272 * being incorporated into outer skip conditions.
2274 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
2275 __isl_take isl_space *space)
2277 struct pet_scop *scop;
2278 isl_multi_pw_aff *skip;
2280 scop = pet_scop_empty(isl_space_copy(space));
2282 skip = one_mpa(space);
2283 scop = pet_scop_set_skip(scop, pet_skip_now,
2284 isl_multi_pw_aff_copy(skip));
2285 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
2287 return scop;
2290 /* Extract a clone of the kill statement "stmt".
2291 * The domain of the clone is given by "domain".
2293 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
2294 struct pet_stmt *stmt, struct pet_state *state)
2296 pet_expr *kill;
2297 isl_space *space;
2298 isl_multi_pw_aff *mpa;
2299 pet_tree *tree;
2301 if (!domain || !stmt)
2302 return NULL;
2304 kill = pet_tree_expr_get_expr(stmt->body);
2305 space = pet_stmt_get_space(stmt);
2306 space = isl_space_map_from_set(space);
2307 mpa = isl_multi_pw_aff_identity(space);
2308 mpa = isl_multi_pw_aff_reset_tuple_id(mpa, isl_dim_in);
2309 kill = pet_expr_update_domain(kill, mpa);
2310 tree = pet_tree_new_expr(kill);
2311 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
2312 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
2313 state->n_stmt++, tree);
2314 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
2317 /* Extract a clone of the kill statements in "scop".
2318 * The domain of each clone is given by "domain".
2319 * "scop" is expected to have been created from a DeclStmt
2320 * and should have (one of) the kill(s) as its first statement.
2321 * If "scop" was created from a declaration group, then there
2322 * may be multiple kill statements inside.
2324 static struct pet_scop *extract_kills(__isl_keep isl_set *domain,
2325 struct pet_scop *scop, struct pet_state *state)
2327 isl_ctx *ctx;
2328 struct pet_stmt *stmt;
2329 struct pet_scop *kill;
2330 int i;
2332 if (!domain || !scop)
2333 return NULL;
2334 ctx = isl_set_get_ctx(domain);
2335 if (scop->n_stmt < 1)
2336 isl_die(ctx, isl_error_internal,
2337 "expecting at least one statement", return NULL);
2338 stmt = scop->stmts[0];
2339 if (!pet_stmt_is_kill(stmt))
2340 isl_die(ctx, isl_error_internal,
2341 "expecting kill statement", return NULL);
2343 kill = extract_kill(domain, stmt, state);
2345 for (i = 1; i < scop->n_stmt; ++i) {
2346 struct pet_scop *kill_i;
2348 stmt = scop->stmts[i];
2349 if (!pet_stmt_is_kill(stmt))
2350 continue;
2352 kill_i = extract_kill(domain, stmt, state);
2353 kill = pet_scop_add_par(ctx, kill, kill_i);
2356 return kill;
2359 /* Has "tree" been created from a DeclStmt?
2360 * That is, is it either a declaration or a group of declarations?
2362 static int tree_is_decl(__isl_keep pet_tree *tree)
2364 int is_decl;
2365 int i;
2367 if (!tree)
2368 return -1;
2369 is_decl = pet_tree_is_decl(tree);
2370 if (is_decl < 0 || is_decl)
2371 return is_decl;
2373 if (tree->type != pet_tree_block)
2374 return 0;
2375 if (pet_tree_block_get_block(tree))
2376 return 0;
2377 if (tree->u.b.n == 0)
2378 return 0;
2380 for (i = 0; i < tree->u.b.n; ++i) {
2381 is_decl = tree_is_decl(tree->u.b.child[i]);
2382 if (is_decl < 0 || !is_decl)
2383 return is_decl;
2386 return 1;
2389 /* Does "tree" represent an assignment to a variable?
2391 * The assignment may be one of
2392 * - a declaration with initialization
2393 * - an expression with a top-level assignment operator
2395 static int is_assignment(__isl_keep pet_tree *tree)
2397 if (!tree)
2398 return 0;
2399 if (tree->type == pet_tree_decl_init)
2400 return 1;
2401 return pet_tree_is_assign(tree);
2404 /* Update "pc" by taking into account the assignment performed by "tree",
2405 * where "tree" satisfies is_assignment.
2407 * In particular, if the lhs of the assignment is a scalar variable and
2408 * if the rhs is an affine expression, then keep track of this value in "pc"
2409 * so that we can plug it in when we later come across the same variable.
2411 * Any previously assigned value to the variable has already been removed
2412 * by scop_handle_writes.
2414 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
2415 __isl_keep pet_tree *tree)
2417 pet_expr *var, *val;
2418 isl_id *id;
2419 isl_pw_aff *pa;
2421 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
2422 var = pet_tree_decl_get_var(tree);
2423 val = pet_tree_decl_get_init(tree);
2424 } else {
2425 pet_expr *expr;
2426 expr = pet_tree_expr_get_expr(tree);
2427 var = pet_expr_get_arg(expr, 0);
2428 val = pet_expr_get_arg(expr, 1);
2429 pet_expr_free(expr);
2432 if (!pet_expr_is_scalar_access(var)) {
2433 pet_expr_free(var);
2434 pet_expr_free(val);
2435 return pc;
2438 pa = pet_expr_extract_affine(val, pc);
2439 if (!pa)
2440 pc = pet_context_free(pc);
2442 if (!isl_pw_aff_involves_nan(pa)) {
2443 id = pet_expr_access_get_id(var);
2444 pc = pet_context_set_value(pc, id, pa);
2445 } else {
2446 isl_pw_aff_free(pa);
2448 pet_expr_free(var);
2449 pet_expr_free(val);
2451 return pc;
2454 /* Mark all arrays in "scop" as being exposed.
2456 static struct pet_scop *mark_exposed(struct pet_scop *scop)
2458 int i;
2460 if (!scop)
2461 return NULL;
2462 for (i = 0; i < scop->n_array; ++i)
2463 scop->arrays[i]->exposed = 1;
2464 return scop;
2467 /* Try and construct a pet_scop corresponding to (part of)
2468 * a sequence of statements within the context "pc".
2470 * After extracting a statement, we update "pc"
2471 * based on the top-level assignments in the statement
2472 * so that we can exploit them in subsequent statements in the same block.
2473 * Top-level affine assumptions are also recorded in the context.
2475 * If there are any breaks or continues in the individual statements,
2476 * then we may have to compute a new skip condition.
2477 * This is handled using a pet_skip_info object.
2478 * On initialization, the object checks if skip conditions need
2479 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2480 * adds them in pet_skip_info_add.
2482 * If "block" is set, then we need to insert kill statements at
2483 * the end of the block for any array that has been declared by
2484 * one of the statements in the sequence. Each of these declarations
2485 * results in the construction of a kill statement at the place
2486 * of the declaration, so we simply collect duplicates of
2487 * those kill statements and append these duplicates to the constructed scop.
2489 * If "block" is not set, then any array declared by one of the statements
2490 * in the sequence is marked as being exposed.
2492 * If autodetect is set, then we allow the extraction of only a subrange
2493 * of the sequence of statements. However, if there is at least one statement
2494 * for which we could not construct a scop and the final range contains
2495 * either no statements or at least one kill, then we discard the entire
2496 * range.
2498 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2499 __isl_keep pet_context *pc, struct pet_state *state)
2501 int i;
2502 isl_ctx *ctx;
2503 isl_space *space;
2504 isl_set *domain;
2505 struct pet_scop *scop, *kills;
2507 ctx = pet_tree_get_ctx(tree);
2509 space = pet_context_get_space(pc);
2510 domain = pet_context_get_domain(pc);
2511 pc = pet_context_copy(pc);
2512 scop = pet_scop_empty(isl_space_copy(space));
2513 kills = pet_scop_empty(space);
2514 for (i = 0; i < tree->u.b.n; ++i) {
2515 struct pet_scop *scop_i;
2517 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2518 pc = apply_affine_continue(pc, scop);
2519 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2520 if (pet_tree_is_assume(tree->u.b.child[i]))
2521 pc = scop_add_affine_assumption(scop_i, pc);
2522 pc = scop_handle_writes(scop_i, pc);
2523 if (is_assignment(tree->u.b.child[i]))
2524 pc = handle_assignment(pc, tree->u.b.child[i]);
2525 struct pet_skip_info skip;
2526 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2527 pet_skip_info_seq_extract(&skip, pc, state);
2528 if (scop_i && tree_is_decl(tree->u.b.child[i])) {
2529 if (tree->u.b.block) {
2530 struct pet_scop *kill;
2531 kill = extract_kills(domain, scop_i, state);
2532 kills = pet_scop_add_par(ctx, kills, kill);
2533 } else
2534 scop_i = mark_exposed(scop_i);
2536 scop = pet_scop_add_seq(ctx, scop, scop_i);
2538 scop = pet_skip_info_add(&skip, scop);
2540 if (!scop)
2541 break;
2543 isl_set_free(domain);
2545 scop = pet_scop_add_seq(ctx, scop, kills);
2547 pet_context_free(pc);
2549 return scop;
2552 /* Internal data structure for extract_declared_arrays.
2554 * "pc" and "state" are used to create pet_array objects and kill statements.
2555 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2556 * found any declared array.
2557 * "scop" has been initialized by the caller and is used to attach
2558 * the created pet_array objects.
2559 * "kill_before" and "kill_after" are created and updated by
2560 * extract_declared_arrays to collect the kills of the arrays.
2562 struct pet_tree_extract_declared_arrays_data {
2563 pet_context *pc;
2564 struct pet_state *state;
2566 isl_ctx *ctx;
2568 int any;
2569 struct pet_scop *scop;
2570 struct pet_scop *kill_before;
2571 struct pet_scop *kill_after;
2574 /* Check if the node "node" declares any array or scalar.
2575 * If so, create the corresponding pet_array and attach it to data->scop.
2576 * Additionally, create two kill statements for the array and add them
2577 * to data->kill_before and data->kill_after.
2579 static int extract_declared_arrays(__isl_keep pet_tree *node, void *user)
2581 enum pet_tree_type type;
2582 struct pet_tree_extract_declared_arrays_data *data = user;
2583 struct pet_array *array;
2584 struct pet_scop *scop_kill;
2585 pet_expr *var;
2587 type = pet_tree_get_type(node);
2588 if (type == pet_tree_decl || type == pet_tree_decl_init)
2589 var = node->u.d.var;
2590 else if (type == pet_tree_for && node->u.l.declared)
2591 var = node->u.l.iv;
2592 else
2593 return 0;
2595 array = extract_array(var, data->pc, data->state);
2596 if (array)
2597 array->declared = 1;
2598 data->scop = pet_scop_add_array(data->scop, array);
2600 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2601 if (!data->any)
2602 data->kill_before = scop_kill;
2603 else
2604 data->kill_before = pet_scop_add_par(data->ctx,
2605 data->kill_before, scop_kill);
2607 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2608 if (!data->any)
2609 data->kill_after = scop_kill;
2610 else
2611 data->kill_after = pet_scop_add_par(data->ctx,
2612 data->kill_after, scop_kill);
2614 data->any = 1;
2616 return 0;
2619 /* Convert a pet_tree that consists of more than a single leaf
2620 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2621 * Do so within the context of "pc", taking into account the writes inside
2622 * "tree". That is, first clear any previously assigned values to variables
2623 * that are written by "tree".
2625 * After constructing the core scop, we also look for any arrays (or scalars)
2626 * that are declared inside "tree". Each of those arrays is marked as
2627 * having been declared and kill statements for these arrays
2628 * are introduced before and after the core scop.
2629 * Note that the input tree is not a leaf so that the declaration
2630 * cannot occur at the outer level.
2632 static struct pet_scop *scop_from_tree_macro(__isl_take pet_tree *tree,
2633 __isl_keep pet_context *pc, struct pet_state *state)
2635 struct pet_tree_extract_declared_arrays_data data = { pc, state };
2637 data.pc = pet_context_copy(data.pc);
2638 data.pc = pet_context_clear_writes_in_tree(data.pc, tree);
2639 data.scop = scop_from_unevaluated_tree(pet_tree_copy(tree),
2640 state->n_stmt++, data.pc);
2642 data.any = 0;
2643 data.ctx = pet_context_get_ctx(data.pc);
2644 if (pet_tree_foreach_sub_tree(tree, &extract_declared_arrays,
2645 &data) < 0)
2646 data.scop = pet_scop_free(data.scop);
2647 pet_tree_free(tree);
2648 pet_context_free(data.pc);
2650 if (!data.any)
2651 return data.scop;
2653 data.scop = pet_scop_add_seq(data.ctx, data.kill_before, data.scop);
2654 data.scop = pet_scop_add_seq(data.ctx, data.scop, data.kill_after);
2656 return data.scop;
2659 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2660 * within the context "pc" by calling the appropriate function
2661 * based on the type of "tree".
2663 * If the initially constructed pet_scop turns out to involve
2664 * dynamic control and if the user has requested an encapsulation
2665 * of all dynamic control, then this pet_scop is discarded and
2666 * a new pet_scop is created with a single statement representing
2667 * the entire "tree".
2668 * However, if the scop contains any active continue or break,
2669 * then we need to include the loop containing the continue or break
2670 * in the encapsulation. We therefore postpone the encapsulation
2671 * until we have constructed a pet_scop for this enclosing loop.
2673 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2674 __isl_keep pet_context *pc, struct pet_state *state)
2676 isl_ctx *ctx;
2677 struct pet_scop *scop = NULL;
2679 if (!tree)
2680 return NULL;
2682 ctx = pet_tree_get_ctx(tree);
2683 switch (tree->type) {
2684 case pet_tree_error:
2685 return NULL;
2686 case pet_tree_block:
2687 return scop_from_block(tree, pc, state);
2688 case pet_tree_break:
2689 return scop_from_break(tree, pet_context_get_space(pc));
2690 case pet_tree_continue:
2691 return scop_from_continue(tree, pet_context_get_space(pc));
2692 case pet_tree_decl:
2693 case pet_tree_decl_init:
2694 return scop_from_decl(tree, pc, state);
2695 case pet_tree_expr:
2696 return scop_from_tree_expr(tree, pc, state);
2697 case pet_tree_return:
2698 return scop_from_return(tree, pc, state);
2699 case pet_tree_if:
2700 case pet_tree_if_else:
2701 scop = scop_from_if(tree, pc, state);
2702 break;
2703 case pet_tree_for:
2704 scop = scop_from_for(tree, pc, state);
2705 break;
2706 case pet_tree_while:
2707 scop = scop_from_while(tree, pc, state);
2708 break;
2709 case pet_tree_infinite_loop:
2710 scop = scop_from_infinite_for(tree, pc, state);
2711 break;
2714 if (!scop)
2715 return NULL;
2717 if (!pet_options_get_encapsulate_dynamic_control(ctx) ||
2718 !pet_scop_has_data_dependent_conditions(scop) ||
2719 pet_scop_has_var_skip(scop, pet_skip_now))
2720 return scop;
2722 pet_scop_free(scop);
2723 return scop_from_tree_macro(pet_tree_copy(tree), pc, state);
2726 /* If "tree" has a label that is of the form S_<nr>, then make
2727 * sure that state->n_stmt is greater than nr to ensure that
2728 * we will not generate S_<nr> ourselves.
2730 static int set_first_stmt(__isl_keep pet_tree *tree, void *user)
2732 struct pet_state *state = user;
2733 const char *name;
2734 int nr;
2736 if (!tree)
2737 return -1;
2738 if (!tree->label)
2739 return 0;
2740 name = isl_id_get_name(tree->label);
2741 if (strncmp(name, "S_", 2) != 0)
2742 return 0;
2743 nr = atoi(name + 2);
2744 if (nr >= state->n_stmt)
2745 state->n_stmt = nr + 1;
2747 return 0;
2750 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2751 * "int_size" is the number of bytes need to represent an integer.
2752 * "extract_array" is a callback that we can use to create a pet_array
2753 * that corresponds to the variable accessed by an expression.
2755 * Initialize the global state, construct a context and then
2756 * construct the pet_scop by recursively visiting the tree.
2758 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2760 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2761 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2762 __isl_keep pet_context *pc, void *user), void *user,
2763 __isl_keep pet_context *pc)
2765 struct pet_scop *scop;
2766 struct pet_state state = { 0 };
2768 if (!tree)
2769 return NULL;
2771 state.ctx = pet_tree_get_ctx(tree);
2772 state.int_size = int_size;
2773 state.extract_array = extract_array;
2774 state.user = user;
2775 if (pet_tree_foreach_sub_tree(tree, &set_first_stmt, &state) < 0)
2776 tree = pet_tree_free(tree);
2778 scop = scop_from_tree(tree, pc, &state);
2779 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2781 pet_tree_free(tree);
2783 if (scop)
2784 scop->context = isl_set_params(scop->context);
2786 return scop;