nest.c: directly include required headers
[pet.git] / tree2scop.c
blob4d7d99276c05d68280e55009e231e09c4065624b
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
35 #include <stdlib.h>
36 #include <string.h>
38 #include <isl/id_to_pw_aff.h>
39 #include <isl/union_set.h>
41 #include "aff.h"
42 #include "expr.h"
43 #include "expr_arg.h"
44 #include "nest.h"
45 #include "scop.h"
46 #include "skip.h"
47 #include "state.h"
48 #include "tree2scop.h"
50 /* If "stmt" is an affine assumption, then record the assumption in "pc".
52 static __isl_give pet_context *add_affine_assumption(struct pet_stmt *stmt,
53 __isl_take pet_context *pc)
55 isl_bool affine;
56 isl_set *cond;
58 affine = pet_stmt_is_affine_assume(stmt);
59 if (affine < 0)
60 return pet_context_free(pc);
61 if (!affine)
62 return pc;
63 cond = pet_stmt_assume_get_affine_condition(stmt);
64 cond = isl_set_reset_tuple_id(cond);
65 pc = pet_context_intersect_domain(pc, cond);
66 return pc;
69 /* Given a scop "scop" derived from an assumption statement,
70 * record the assumption in "pc", if it is affine.
71 * Note that "scop" should consist of exactly one statement.
73 static __isl_give pet_context *scop_add_affine_assumption(
74 __isl_keep pet_scop *scop, __isl_take pet_context *pc)
76 int i;
78 if (!scop)
79 return pet_context_free(pc);
80 for (i = 0; i < scop->n_stmt; ++i)
81 pc = add_affine_assumption(scop->stmts[i], pc);
83 return pc;
86 /* Update "pc" by taking into account the writes in "stmt".
87 * That is, clear any previously assigned values to variables
88 * that are written by "stmt".
90 static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
91 __isl_take pet_context *pc)
93 return pet_context_clear_writes_in_tree(pc, stmt->body);
96 /* Update "pc" based on the write accesses in "scop".
98 static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
99 __isl_take pet_context *pc)
101 int i;
103 if (!scop)
104 return pet_context_free(pc);
105 for (i = 0; i < scop->n_stmt; ++i)
106 pc = handle_writes(scop->stmts[i], pc);
108 return pc;
111 /* Wrapper around pet_expr_resolve_assume
112 * for use as a callback to pet_tree_map_expr.
114 static __isl_give pet_expr *resolve_assume(__isl_take pet_expr *expr,
115 void *user)
117 pet_context *pc = user;
119 return pet_expr_resolve_assume(expr, pc);
122 /* Check if any expression inside "tree" is an assume expression and
123 * if its single argument can be converted to an affine expression
124 * in the context of "pc".
125 * If so, replace the argument by the affine expression.
127 __isl_give pet_tree *pet_tree_resolve_assume(__isl_take pet_tree *tree,
128 __isl_keep pet_context *pc)
130 return pet_tree_map_expr(tree, &resolve_assume, pc);
133 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
134 * "tree" has already been evaluated in the context of "pc".
135 * This mainly involves resolving nested expression parameters
136 * and setting the name of the iteration space.
137 * The name is given by tree->label if it is non-NULL. Otherwise,
138 * it is of the form S_<stmt_nr>.
140 static struct pet_scop *scop_from_evaluated_tree(__isl_take pet_tree *tree,
141 int stmt_nr, __isl_keep pet_context *pc)
143 isl_space *space;
144 isl_set *domain;
145 struct pet_stmt *ps;
147 space = pet_context_get_space(pc);
149 tree = pet_tree_resolve_nested(tree, space);
150 tree = pet_tree_resolve_assume(tree, pc);
152 domain = pet_context_get_domain(pc);
153 ps = pet_stmt_from_pet_tree(domain, stmt_nr, tree);
154 return pet_scop_from_pet_stmt(space, ps);
157 /* Convert a top-level pet_expr to a pet_scop with one statement
158 * within the context "pc".
159 * "expr" has already been evaluated in the context of "pc".
160 * We construct a pet_tree from "expr" and continue with
161 * scop_from_evaluated_tree.
162 * The name is of the form S_<stmt_nr>.
163 * The location of the statement is set to "loc".
165 static struct pet_scop *scop_from_evaluated_expr(__isl_take pet_expr *expr,
166 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
168 pet_tree *tree;
170 tree = pet_tree_new_expr(expr);
171 tree = pet_tree_set_loc(tree, loc);
172 return scop_from_evaluated_tree(tree, stmt_nr, pc);
175 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
176 * "tree" has not yet been evaluated in the context of "pc".
177 * We evaluate "tree" in the context of "pc" and continue with
178 * scop_from_evaluated_tree.
179 * The statement name is given by tree->label if it is non-NULL. Otherwise,
180 * it is of the form S_<stmt_nr>.
182 static struct pet_scop *scop_from_unevaluated_tree(__isl_take pet_tree *tree,
183 int stmt_nr, __isl_keep pet_context *pc)
185 tree = pet_context_evaluate_tree(pc, tree);
186 return scop_from_evaluated_tree(tree, stmt_nr, pc);
189 /* Convert a top-level pet_expr to a pet_scop with one statement
190 * within the context "pc", where "expr" has not yet been evaluated
191 * in the context of "pc".
192 * We construct a pet_tree from "expr" and continue with
193 * scop_from_unevaluated_tree.
194 * The statement name is of the form S_<stmt_nr>.
195 * The location of the statement is set to "loc".
197 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
198 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
200 pet_tree *tree;
202 tree = pet_tree_new_expr(expr);
203 tree = pet_tree_set_loc(tree, loc);
204 return scop_from_unevaluated_tree(tree, stmt_nr, pc);
207 /* Construct a pet_scop with a single statement killing the entire
208 * array "array".
209 * The location of the statement is set to "loc".
211 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
212 __isl_keep pet_context *pc, struct pet_state *state)
214 isl_ctx *ctx;
215 isl_id *id;
216 isl_space *space;
217 isl_multi_pw_aff *index;
218 isl_map *access;
219 pet_expr *expr;
220 struct pet_scop *scop;
222 if (!array)
223 goto error;
224 ctx = isl_set_get_ctx(array->extent);
225 access = isl_map_from_range(isl_set_copy(array->extent));
226 id = isl_set_get_tuple_id(array->extent);
227 space = isl_space_alloc(ctx, 0, 0, 0);
228 space = isl_space_set_tuple_id(space, isl_dim_out, id);
229 index = isl_multi_pw_aff_zero(space);
230 expr = pet_expr_kill_from_access_and_index(access, index);
231 return scop_from_expr(expr, state->n_stmt++, loc, pc);
232 error:
233 pet_loc_free(loc);
234 return NULL;
237 /* Construct and return a pet_array corresponding to the variable
238 * accessed by "access" by calling the extract_array callback.
240 static struct pet_array *extract_array(__isl_keep pet_expr *access,
241 __isl_keep pet_context *pc, struct pet_state *state)
243 return state->extract_array(access, pc, state->user);
246 /* Construct a pet_scop for a (single) variable declaration
247 * within the context "pc".
249 * The scop contains the variable being declared (as an array)
250 * and a statement killing the array.
252 * If the declaration comes with an initialization, then the scop
253 * also contains an assignment to the variable.
255 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
256 __isl_keep pet_context *pc, struct pet_state *state)
258 int type_size;
259 isl_ctx *ctx;
260 struct pet_array *array;
261 struct pet_scop *scop_decl, *scop;
262 pet_expr *lhs, *rhs, *pe;
264 array = extract_array(tree->u.d.var, pc, state);
265 if (array)
266 array->declared = 1;
267 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
268 scop_decl = pet_scop_add_array(scop_decl, array);
270 if (tree->type != pet_tree_decl_init)
271 return scop_decl;
273 lhs = pet_expr_copy(tree->u.d.var);
274 rhs = pet_expr_copy(tree->u.d.init);
275 type_size = pet_expr_get_type_size(lhs);
276 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
277 scop = scop_from_expr(pe, state->n_stmt++, pet_tree_get_loc(tree), pc);
279 ctx = pet_tree_get_ctx(tree);
280 scop = pet_scop_add_seq(ctx, scop_decl, scop);
282 return scop;
285 /* Does "tree" represent a kill statement?
286 * That is, is it an expression statement that "calls" __pencil_kill?
288 static int is_pencil_kill(__isl_keep pet_tree *tree)
290 pet_expr *expr;
291 const char *name;
293 if (!tree)
294 return -1;
295 if (tree->type != pet_tree_expr)
296 return 0;
297 expr = tree->u.e.expr;
298 if (pet_expr_get_type(expr) != pet_expr_call)
299 return 0;
300 name = pet_expr_call_get_name(expr);
301 if (!name)
302 return -1;
303 return !strcmp(name, "__pencil_kill");
306 /* Add a kill to "scop" that kills what is accessed by
307 * the access expression "expr".
309 * Mark the access as a write prior to evaluation to avoid
310 * the access being replaced by a possible known value
311 * during the evaluation.
313 * If the access expression has any arguments (after evaluation
314 * in the context of "pc"), then we ignore it, since we cannot
315 * tell which elements are definitely killed.
317 * Otherwise, we extend the index expression to the dimension
318 * of the accessed array and intersect with the extent of the array and
319 * add a kill expression that kills these array elements is added to "scop".
321 static struct pet_scop *scop_add_kill(struct pet_scop *scop,
322 __isl_take pet_expr *expr, __isl_take pet_loc *loc,
323 __isl_keep pet_context *pc, struct pet_state *state)
325 int dim1, dim2;
326 isl_id *id;
327 isl_multi_pw_aff *index;
328 isl_map *map;
329 pet_expr *kill;
330 struct pet_array *array;
331 struct pet_scop *scop_i;
333 expr = pet_expr_access_set_write(expr, 1);
334 expr = pet_context_evaluate_expr(pc, expr);
335 if (!expr)
336 goto error;
337 if (expr->n_arg != 0) {
338 pet_loc_free(loc);
339 pet_expr_free(expr);
340 return scop;
342 array = extract_array(expr, pc, state);
343 if (!array)
344 goto error;
345 index = pet_expr_access_get_index(expr);
346 pet_expr_free(expr);
347 map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
348 id = isl_map_get_tuple_id(map, isl_dim_out);
349 dim1 = isl_set_dim(array->extent, isl_dim_set);
350 dim2 = isl_map_dim(map, isl_dim_out);
351 map = isl_map_add_dims(map, isl_dim_out, dim1 - dim2);
352 map = isl_map_set_tuple_id(map, isl_dim_out, id);
353 map = isl_map_intersect_range(map, isl_set_copy(array->extent));
354 pet_array_free(array);
355 kill = pet_expr_kill_from_access_and_index(map, index);
356 scop_i = scop_from_evaluated_expr(kill, state->n_stmt++, loc, pc);
357 scop = pet_scop_add_par(state->ctx, scop, scop_i);
359 return scop;
360 error:
361 pet_expr_free(expr);
362 pet_loc_free(loc);
363 return pet_scop_free(scop);
366 /* For each argument of the __pencil_kill call in "tree" that
367 * represents an access, add a kill statement to "scop" killing the accessed
368 * elements.
370 static struct pet_scop *scop_from_pencil_kill(__isl_keep pet_tree *tree,
371 __isl_keep pet_context *pc, struct pet_state *state)
373 pet_expr *call;
374 struct pet_scop *scop;
375 int i, n;
377 call = tree->u.e.expr;
379 scop = pet_scop_empty(pet_context_get_space(pc));
381 n = pet_expr_get_n_arg(call);
382 for (i = 0; i < n; ++i) {
383 pet_expr *arg;
384 pet_loc *loc;
386 arg = pet_expr_get_arg(call, i);
387 if (!arg)
388 return pet_scop_free(scop);
389 if (pet_expr_get_type(arg) != pet_expr_access) {
390 pet_expr_free(arg);
391 continue;
393 loc = pet_tree_get_loc(tree);
394 scop = scop_add_kill(scop, arg, loc, pc, state);
397 return scop;
400 /* Construct a pet_scop for an expression statement within the context "pc".
402 * If the expression calls __pencil_kill, then it needs to be converted
403 * into zero or more kill statements.
404 * Otherwise, a scop is extracted directly from the tree.
406 static struct pet_scop *scop_from_tree_expr(__isl_keep pet_tree *tree,
407 __isl_keep pet_context *pc, struct pet_state *state)
409 int is_kill;
411 is_kill = is_pencil_kill(tree);
412 if (is_kill < 0)
413 return NULL;
414 if (is_kill)
415 return scop_from_pencil_kill(tree, pc, state);
416 return scop_from_unevaluated_tree(pet_tree_copy(tree),
417 state->n_stmt++, pc);
420 /* Return those elements in the space of "cond" that come after
421 * (based on "sign") an element in "cond" in the final dimension.
423 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
425 isl_space *space;
426 isl_map *previous_to_this;
427 int i, dim;
429 dim = isl_set_dim(cond, isl_dim_set);
430 space = isl_space_map_from_set(isl_set_get_space(cond));
431 previous_to_this = isl_map_universe(space);
432 for (i = 0; i + 1 < dim; ++i)
433 previous_to_this = isl_map_equate(previous_to_this,
434 isl_dim_in, i, isl_dim_out, i);
435 if (sign > 0)
436 previous_to_this = isl_map_order_lt(previous_to_this,
437 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
438 else
439 previous_to_this = isl_map_order_gt(previous_to_this,
440 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
442 cond = isl_set_apply(cond, previous_to_this);
444 return cond;
447 /* Remove those iterations of "domain" that have an earlier iteration
448 * (based on "sign") in the final dimension where "skip" is satisfied.
449 * If "apply_skip_map" is set, then "skip_map" is first applied
450 * to the embedded skip condition before removing it from the domain.
452 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
453 __isl_take isl_set *skip, int sign,
454 int apply_skip_map, __isl_keep isl_map *skip_map)
456 if (apply_skip_map)
457 skip = isl_set_apply(skip, isl_map_copy(skip_map));
458 skip = isl_set_intersect(skip , isl_set_copy(domain));
459 return isl_set_subtract(domain, after(skip, sign));
462 /* Create a single-dimensional multi-affine expression on the domain space
463 * of "pc" that is equal to the final dimension of this domain.
464 * "loop_nr" is the sequence number of the corresponding loop.
465 * If "id" is not NULL, then it is used as the output tuple name.
466 * Otherwise, the name is constructed as L_<loop_nr>.
468 static __isl_give isl_multi_aff *map_to_last(__isl_keep pet_context *pc,
469 int loop_nr, __isl_keep isl_id *id)
471 int pos;
472 isl_space *space;
473 isl_local_space *ls;
474 isl_aff *aff;
475 isl_multi_aff *ma;
476 char name[50];
477 isl_id *label;
479 space = pet_context_get_space(pc);
480 pos = isl_space_dim(space, isl_dim_set) - 1;
481 ls = isl_local_space_from_space(space);
482 aff = isl_aff_var_on_domain(ls, isl_dim_set, pos);
483 ma = isl_multi_aff_from_aff(aff);
485 if (id) {
486 label = isl_id_copy(id);
487 } else {
488 snprintf(name, sizeof(name), "L_%d", loop_nr);
489 label = isl_id_alloc(pet_context_get_ctx(pc), name, NULL);
491 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, label);
493 return ma;
496 /* Create an affine expression that maps elements
497 * of an array "id_test" to the previous element in the final dimension
498 * (according to "inc"), provided this element belongs to "domain".
499 * That is, create the affine expression
501 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
503 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
504 __isl_take isl_set *domain, __isl_take isl_val *inc)
506 int pos;
507 isl_space *space;
508 isl_aff *aff;
509 isl_pw_aff *pa;
510 isl_multi_aff *ma;
511 isl_multi_pw_aff *prev;
513 pos = isl_set_dim(domain, isl_dim_set) - 1;
514 space = isl_set_get_space(domain);
515 space = isl_space_map_from_set(space);
516 ma = isl_multi_aff_identity(space);
517 aff = isl_multi_aff_get_aff(ma, pos);
518 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
519 ma = isl_multi_aff_set_aff(ma, pos, aff);
520 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
521 prev = isl_multi_pw_aff_from_multi_aff(ma);
522 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
523 pa = isl_pw_aff_intersect_domain(pa, domain);
524 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
525 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
527 return prev;
530 /* Add an implication to "scop" expressing that if an element of
531 * virtual array "id_test" has value "satisfied" then all previous elements
532 * of this array (in the final dimension) also have that value.
533 * The set of previous elements is bounded by "domain".
534 * If "sign" is negative then the iterator
535 * is decreasing and we express that all subsequent array elements
536 * (but still defined previously) have the same value.
538 static struct pet_scop *add_implication(struct pet_scop *scop,
539 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
540 int satisfied)
542 int i, dim;
543 isl_space *space;
544 isl_map *map;
546 dim = isl_set_dim(domain, isl_dim_set);
547 domain = isl_set_set_tuple_id(domain, id_test);
548 space = isl_space_map_from_set(isl_set_get_space(domain));
549 map = isl_map_universe(space);
550 for (i = 0; i + 1 < dim; ++i)
551 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
552 if (sign > 0)
553 map = isl_map_order_ge(map,
554 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
555 else
556 map = isl_map_order_le(map,
557 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
558 map = isl_map_intersect_range(map, domain);
559 scop = pet_scop_add_implication(scop, map, satisfied);
561 return scop;
564 /* Add a filter to "scop" that imposes that it is only executed
565 * when the variable identified by "id_test" has a zero value
566 * for all previous iterations of "domain".
568 * In particular, add a filter that imposes that the array
569 * has a zero value at the previous iteration of domain and
570 * add an implication that implies that it then has that
571 * value for all previous iterations.
573 static struct pet_scop *scop_add_break(struct pet_scop *scop,
574 __isl_take isl_id *id_test, __isl_take isl_set *domain,
575 __isl_take isl_val *inc)
577 isl_multi_pw_aff *prev;
578 int sign = isl_val_sgn(inc);
580 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
581 scop = add_implication(scop, id_test, domain, sign, 0);
582 scop = pet_scop_filter(scop, prev, 0);
584 return scop;
587 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
588 __isl_keep pet_context *pc, struct pet_state *state);
590 /* Construct a pet_scop for an infinite loop around the given body
591 * within the context "pc".
592 * "loop_id" is the label on the loop or NULL if there is no such label.
594 * The domain of "pc" has already been extended with an infinite loop
596 * { [t] : t >= 0 }
598 * We extract a pet_scop for the body and then embed it in a loop with
599 * schedule
601 * { [outer,t] -> [t] }
603 * If the body contains any break, then it is taken into
604 * account in apply_affine_break (if the skip condition is affine)
605 * or in scop_add_break (if the skip condition is not affine).
607 * Note that in case of an affine skip condition,
608 * since we are dealing with a loop without loop iterator,
609 * the skip condition cannot refer to the current loop iterator and
610 * so effectively, the effect on the iteration domain is of the form
612 * { [outer,0]; [outer,t] : t >= 1 and not skip }
614 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
615 __isl_keep isl_id *loop_id, __isl_keep pet_context *pc,
616 struct pet_state *state)
618 isl_ctx *ctx;
619 isl_id *id_test;
620 isl_set *domain;
621 isl_set *skip;
622 isl_multi_aff *sched;
623 struct pet_scop *scop;
624 int has_affine_break;
625 int has_var_break;
627 ctx = pet_tree_get_ctx(body);
628 domain = pet_context_get_domain(pc);
629 sched = map_to_last(pc, state->n_loop++, loop_id);
631 scop = scop_from_tree(body, pc, state);
633 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
634 if (has_affine_break)
635 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
636 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
637 if (has_var_break)
638 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
640 scop = pet_scop_reset_skips(scop);
641 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
642 if (has_affine_break) {
643 domain = apply_affine_break(domain, skip, 1, 0, NULL);
644 scop = pet_scop_intersect_domain_prefix(scop,
645 isl_set_copy(domain));
647 if (has_var_break)
648 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
649 else
650 isl_set_free(domain);
652 return scop;
655 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
657 * for (;;)
658 * body
660 * within the context "pc".
662 * Extend the domain of "pc" with an extra inner loop
664 * { [t] : t >= 0 }
666 * and construct the scop in scop_from_infinite_loop.
668 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
669 __isl_keep pet_context *pc, struct pet_state *state)
671 struct pet_scop *scop;
673 pc = pet_context_copy(pc);
674 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
676 pc = pet_context_add_infinite_loop(pc);
678 scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
680 pet_context_free(pc);
682 return scop;
685 /* Construct a pet_scop for a while loop of the form
687 * while (pa)
688 * body
690 * within the context "pc".
692 * The domain of "pc" has already been extended with an infinite loop
694 * { [t] : t >= 0 }
696 * Here, we add the constraints on the outer loop iterators
697 * implied by "pa" and construct the scop in scop_from_infinite_loop.
698 * Note that the intersection with these constraints
699 * may result in an empty loop.
701 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
702 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
703 struct pet_state *state)
705 struct pet_scop *scop;
706 isl_set *dom, *local;
707 isl_set *valid;
709 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
710 dom = isl_pw_aff_non_zero_set(pa);
711 local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
712 pc = pet_context_intersect_domain(pc, local);
713 scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
714 scop = pet_scop_restrict(scop, dom);
715 scop = pet_scop_restrict_context(scop, valid);
717 pet_context_free(pc);
718 return scop;
721 /* Construct a scop for a while, given the scops for the condition
722 * and the body, the filter identifier and the iteration domain of
723 * the while loop.
725 * In particular, the scop for the condition is filtered to depend
726 * on "id_test" evaluating to true for all previous iterations
727 * of the loop, while the scop for the body is filtered to depend
728 * on "id_test" evaluating to true for all iterations up to the
729 * current iteration.
730 * The actual filter only imposes that this virtual array has
731 * value one on the previous or the current iteration.
732 * The fact that this condition also applies to the previous
733 * iterations is enforced by an implication.
735 * These filtered scops are then combined into a single scop,
736 * with the condition scop scheduled before the body scop.
738 * "sign" is positive if the iterator increases and negative
739 * if it decreases.
741 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
742 struct pet_scop *scop_body, __isl_take isl_id *id_test,
743 __isl_take isl_set *domain, __isl_take isl_val *inc)
745 isl_ctx *ctx = isl_set_get_ctx(domain);
746 isl_space *space;
747 isl_multi_pw_aff *test_index;
748 isl_multi_pw_aff *prev;
749 int sign = isl_val_sgn(inc);
750 struct pet_scop *scop;
752 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
753 scop_cond = pet_scop_filter(scop_cond, prev, 1);
755 space = isl_space_map_from_set(isl_set_get_space(domain));
756 test_index = isl_multi_pw_aff_identity(space);
757 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
758 isl_id_copy(id_test));
759 scop_body = pet_scop_filter(scop_body, test_index, 1);
761 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
762 scop = add_implication(scop, id_test, domain, sign, 1);
764 return scop;
767 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
768 * evaluating "cond" and writing the result to a virtual scalar,
769 * as expressed by "index".
770 * The expression "cond" has not yet been evaluated in the context of "pc".
771 * Do so within the context "pc".
772 * The location of the statement is set to "loc".
774 static struct pet_scop *scop_from_non_affine_condition(
775 __isl_take pet_expr *cond, int stmt_nr,
776 __isl_take isl_multi_pw_aff *index,
777 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
779 pet_expr *expr, *write;
781 cond = pet_context_evaluate_expr(pc, cond);
783 write = pet_expr_from_index(index);
784 write = pet_expr_access_set_write(write, 1);
785 write = pet_expr_access_set_read(write, 0);
786 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
788 return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
791 /* Given that "scop" has an affine skip condition of type pet_skip_now,
792 * apply this skip condition to the domain of "pc".
793 * That is, remove the elements satisfying the skip condition from
794 * the domain of "pc".
796 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
797 struct pet_scop *scop)
799 isl_set *domain, *skip;
801 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
802 domain = pet_context_get_domain(pc);
803 domain = isl_set_subtract(domain, skip);
804 pc = pet_context_intersect_domain(pc, domain);
806 return pc;
809 /* Add a scop for evaluating the loop increment "inc" at the end
810 * of a loop body "scop" within the context "pc".
812 * The skip conditions resulting from continue statements inside
813 * the body do not apply to "inc", but those resulting from break
814 * statements do need to get applied.
816 static struct pet_scop *scop_add_inc(struct pet_scop *scop,
817 __isl_take pet_expr *inc, __isl_take pet_loc *loc,
818 __isl_keep pet_context *pc, struct pet_state *state)
820 struct pet_scop *scop_inc;
822 pc = pet_context_copy(pc);
824 if (pet_scop_has_skip(scop, pet_skip_later)) {
825 isl_multi_pw_aff *skip;
826 skip = pet_scop_get_skip(scop, pet_skip_later);
827 scop = pet_scop_set_skip(scop, pet_skip_now, skip);
828 if (pet_scop_has_affine_skip(scop, pet_skip_now))
829 pc = apply_affine_continue(pc, scop);
830 } else
831 pet_scop_reset_skip(scop, pet_skip_now);
832 scop_inc = scop_from_expr(inc, state->n_stmt++, loc, pc);
833 scop = pet_scop_add_seq(state->ctx, scop, scop_inc);
835 pet_context_free(pc);
837 return scop;
840 /* Construct a generic while scop, with iteration domain
841 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
842 * "loop_id" is the label on the loop or NULL if there is no such label.
843 * The domain of "pc" has already been extended with this infinite loop
845 * { [t] : t >= 0 }
847 * The scop consists of two parts,
848 * one for evaluating the condition "cond" and one for the body.
849 * If "expr_inc" is not NULL, then a scop for evaluating this expression
850 * is added at the end of the body,
851 * after replacing any skip conditions resulting from continue statements
852 * by the skip conditions resulting from break statements (if any).
854 * The schedules are combined as a sequence to reflect that the condition is
855 * evaluated before the body is executed and the body is filtered to depend
856 * on the result of the condition evaluating to true on all iterations
857 * up to the current iteration, while the evaluation of the condition itself
858 * is filtered to depend on the result of the condition evaluating to true
859 * on all previous iterations.
860 * The context of the scop representing the body is dropped
861 * because we don't know how many times the body will be executed,
862 * if at all.
864 * If the body contains any break, then it is taken into
865 * account in apply_affine_break (if the skip condition is affine)
866 * or in scop_add_break (if the skip condition is not affine).
868 * Note that in case of an affine skip condition,
869 * since we are dealing with a loop without loop iterator,
870 * the skip condition cannot refer to the current loop iterator and
871 * so effectively, the effect on the iteration domain is of the form
873 * { [outer,0]; [outer,t] : t >= 1 and not skip }
875 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
876 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
877 __isl_keep isl_id *loop_id, __isl_take pet_expr *expr_inc,
878 __isl_take pet_context *pc, struct pet_state *state)
880 isl_ctx *ctx;
881 isl_id *id_test, *id_break_test;
882 isl_space *space;
883 isl_multi_pw_aff *test_index;
884 isl_set *domain;
885 isl_set *skip;
886 isl_multi_aff *sched;
887 struct pet_scop *scop, *scop_body;
888 int has_affine_break;
889 int has_var_break;
891 ctx = state->ctx;
892 space = pet_context_get_space(pc);
893 test_index = pet_create_test_index(space, state->n_test++);
894 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
895 isl_multi_pw_aff_copy(test_index),
896 pet_loc_copy(loc), pc);
897 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
898 domain = pet_context_get_domain(pc);
899 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
900 test_index, state->int_size);
902 sched = map_to_last(pc, state->n_loop++, loop_id);
904 scop_body = scop_from_tree(tree_body, pc, state);
906 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
907 if (has_affine_break)
908 skip = pet_scop_get_affine_skip_domain(scop_body,
909 pet_skip_later);
910 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
911 if (has_var_break)
912 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
914 scop_body = pet_scop_reset_context(scop_body);
915 if (expr_inc)
916 scop_body = scop_add_inc(scop_body, expr_inc, loc, pc, state);
917 else
918 pet_loc_free(loc);
919 scop_body = pet_scop_reset_skips(scop_body);
921 if (has_affine_break) {
922 domain = apply_affine_break(domain, skip, 1, 0, NULL);
923 scop = pet_scop_intersect_domain_prefix(scop,
924 isl_set_copy(domain));
925 scop_body = pet_scop_intersect_domain_prefix(scop_body,
926 isl_set_copy(domain));
928 if (has_var_break) {
929 scop = scop_add_break(scop, isl_id_copy(id_break_test),
930 isl_set_copy(domain), isl_val_one(ctx));
931 scop_body = scop_add_break(scop_body, id_break_test,
932 isl_set_copy(domain), isl_val_one(ctx));
934 scop = scop_add_while(scop, scop_body, id_test, isl_set_copy(domain),
935 isl_val_one(ctx));
937 scop = pet_scop_embed(scop, domain, sched);
939 pet_context_free(pc);
940 return scop;
943 /* Check if the while loop is of the form
945 * while (affine expression)
946 * body
948 * If so, call scop_from_affine_while to construct a scop.
950 * Otherwise, pass control to scop_from_non_affine_while.
952 * "pc" is the context in which the affine expressions in the scop are created.
953 * The domain of "pc" is extended with an infinite loop
955 * { [t] : t >= 0 }
957 * before passing control to scop_from_affine_while or
958 * scop_from_non_affine_while.
960 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
961 __isl_keep pet_context *pc, struct pet_state *state)
963 pet_expr *cond_expr;
964 isl_pw_aff *pa;
966 if (!tree)
967 return NULL;
969 pc = pet_context_copy(pc);
970 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
972 cond_expr = pet_expr_copy(tree->u.l.cond);
973 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
974 pa = pet_expr_extract_affine_condition(cond_expr, pc);
975 pet_expr_free(cond_expr);
977 pc = pet_context_add_infinite_loop(pc);
979 if (!pa)
980 goto error;
982 if (!isl_pw_aff_involves_nan(pa))
983 return scop_from_affine_while(tree, pa, pc, state);
984 isl_pw_aff_free(pa);
985 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
986 pet_tree_get_loc(tree), tree->u.l.body,
987 tree->label, NULL, pc, state);
988 error:
989 pet_context_free(pc);
990 return NULL;
993 /* Check whether "cond" expresses a simple loop bound
994 * on the final set dimension.
995 * In particular, if "up" is set then "cond" should contain only
996 * upper bounds on the final set dimension.
997 * Otherwise, it should contain only lower bounds.
999 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
1001 int pos;
1003 pos = isl_set_dim(cond, isl_dim_set) - 1;
1004 if (isl_val_is_pos(inc))
1005 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
1006 else
1007 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
1010 /* Extend a condition on a given iteration of a loop to one that
1011 * imposes the same condition on all previous iterations.
1012 * "domain" expresses the lower [upper] bound on the iterations
1013 * when inc is positive [negative] in its final dimension.
1015 * In particular, we construct the condition (when inc is positive)
1017 * forall i' : (domain(i') and i' <= i) => cond(i')
1019 * (where "<=" applies to the final dimension)
1020 * which is equivalent to
1022 * not exists i' : domain(i') and i' <= i and not cond(i')
1024 * We construct this set by subtracting the satisfying cond from domain,
1025 * applying a map
1027 * { [i'] -> [i] : i' <= i }
1029 * and then subtracting the result from domain again.
1031 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
1032 __isl_take isl_set *domain, __isl_take isl_val *inc)
1034 isl_space *space;
1035 isl_map *previous_to_this;
1036 int i, dim;
1038 dim = isl_set_dim(cond, isl_dim_set);
1039 space = isl_space_map_from_set(isl_set_get_space(cond));
1040 previous_to_this = isl_map_universe(space);
1041 for (i = 0; i + 1 < dim; ++i)
1042 previous_to_this = isl_map_equate(previous_to_this,
1043 isl_dim_in, i, isl_dim_out, i);
1044 if (isl_val_is_pos(inc))
1045 previous_to_this = isl_map_order_le(previous_to_this,
1046 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1047 else
1048 previous_to_this = isl_map_order_ge(previous_to_this,
1049 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1051 cond = isl_set_subtract(isl_set_copy(domain), cond);
1052 cond = isl_set_apply(cond, previous_to_this);
1053 cond = isl_set_subtract(domain, cond);
1055 isl_val_free(inc);
1057 return cond;
1060 /* Given an initial value of the form
1062 * { [outer,i] -> init(outer) }
1064 * construct a domain of the form
1066 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1068 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
1069 __isl_take isl_val *inc)
1071 int dim;
1072 isl_aff *aff;
1073 isl_space *space;
1074 isl_local_space *ls;
1075 isl_set *set;
1077 dim = isl_pw_aff_dim(init, isl_dim_in);
1079 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
1080 space = isl_pw_aff_get_domain_space(init);
1081 ls = isl_local_space_from_space(space);
1082 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1083 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
1084 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1086 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
1087 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1089 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
1090 set = isl_set_project_out(set, isl_dim_set, dim, 1);
1092 return set;
1095 /* Assuming "cond" represents a bound on a loop where the loop
1096 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1097 * is possible.
1099 * Under the given assumptions, wrapping is only possible if "cond" allows
1100 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1101 * increasing iterator and 0 in case of a decreasing iterator.
1103 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
1104 __isl_keep isl_val *inc)
1106 int cw;
1107 isl_ctx *ctx;
1108 isl_val *limit;
1109 isl_set *test;
1111 test = isl_set_copy(cond);
1113 ctx = isl_set_get_ctx(test);
1114 if (isl_val_is_neg(inc))
1115 limit = isl_val_zero(ctx);
1116 else {
1117 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1118 limit = isl_val_2exp(limit);
1119 limit = isl_val_sub_ui(limit, 1);
1122 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
1123 cw = !isl_set_is_empty(test);
1124 isl_set_free(test);
1126 return cw;
1129 /* Given a space
1131 * { [outer, v] },
1133 * construct the following affine expression on this space
1135 * { [outer, v] -> [outer, v mod 2^width] }
1137 * where width is the number of bits used to represent the values
1138 * of the unsigned variable "iv".
1140 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
1141 __isl_keep pet_expr *iv)
1143 int dim;
1144 isl_aff *aff;
1145 isl_multi_aff *ma;
1147 dim = isl_space_dim(space, isl_dim_set);
1149 space = isl_space_map_from_set(space);
1150 ma = isl_multi_aff_identity(space);
1152 aff = isl_multi_aff_get_aff(ma, dim - 1);
1153 aff = pet_wrap_aff(aff, pet_expr_get_type_size(iv));
1154 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
1156 return ma;
1159 /* Given two sets in the space
1161 * { [l,i] },
1163 * where l represents the outer loop iterators, compute the set
1164 * of values of l that ensure that "set1" is a subset of "set2".
1166 * set1 is a subset of set2 if
1168 * forall i: set1(l,i) => set2(l,i)
1170 * or
1172 * not exists i: set1(l,i) and not set2(l,i)
1174 * i.e.,
1176 * not exists i: (set1 \ set2)(l,i)
1178 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
1179 __isl_take isl_set *set2)
1181 int pos;
1183 pos = isl_set_dim(set1, isl_dim_set) - 1;
1184 set1 = isl_set_subtract(set1, set2);
1185 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
1186 return isl_set_complement(set1);
1189 /* Compute the set of outer iterator values for which "cond" holds
1190 * on the next iteration of the inner loop for each element of "dom".
1192 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1193 * to the outer loop iterators), plug that into "cond"
1194 * and then compute the set of outer iterators for which "dom" is a subset
1195 * of the result.
1197 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
1198 __isl_take isl_set *dom, __isl_take isl_val *inc)
1200 int pos;
1201 isl_space *space;
1202 isl_aff *aff;
1203 isl_multi_aff *ma;
1205 pos = isl_set_dim(dom, isl_dim_set) - 1;
1206 space = isl_set_get_space(dom);
1207 space = isl_space_map_from_set(space);
1208 ma = isl_multi_aff_identity(space);
1209 aff = isl_multi_aff_get_aff(ma, pos);
1210 aff = isl_aff_add_constant_val(aff, inc);
1211 ma = isl_multi_aff_set_aff(ma, pos, aff);
1212 cond = isl_set_preimage_multi_aff(cond, ma);
1214 return enforce_subset(dom, cond);
1217 /* Construct a pet_scop for the initialization of the iterator
1218 * of the for loop "tree" within the context "pc" (i.e., the context
1219 * of the loop).
1221 static __isl_give pet_scop *scop_from_for_init(__isl_keep pet_tree *tree,
1222 __isl_keep pet_context *pc, struct pet_state *state)
1224 pet_expr *expr_iv, *init;
1225 int type_size;
1227 expr_iv = pet_expr_copy(tree->u.l.iv);
1228 type_size = pet_expr_get_type_size(expr_iv);
1229 init = pet_expr_copy(tree->u.l.init);
1230 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1231 return scop_from_expr(init, state->n_stmt++,
1232 pet_tree_get_loc(tree), pc);
1235 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1236 * In particular, "pc_init" represents the context of the loop,
1237 * whereas "pc" represents the context of the body of the loop and
1238 * has already had its domain extended with an infinite loop
1240 * { [t] : t >= 0 }
1242 * The for loop has the form
1244 * for (iv = init; cond; iv += inc)
1245 * body;
1247 * and is treated as
1249 * iv = init;
1250 * while (cond) {
1251 * body;
1252 * iv += inc;
1255 * except that the skips resulting from any continue statements
1256 * in body do not apply to the increment, but are replaced by the skips
1257 * resulting from break statements.
1259 * If the loop iterator is declared in the for loop, then it is killed before
1260 * and after the loop.
1262 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1263 __isl_keep pet_context *pc_init, __isl_take pet_context *pc,
1264 struct pet_state *state)
1266 int declared;
1267 isl_id *iv;
1268 pet_expr *expr_iv, *inc;
1269 struct pet_scop *scop_init, *scop;
1270 int type_size;
1271 struct pet_array *array;
1272 struct pet_scop *scop_kill;
1274 iv = pet_expr_access_get_id(tree->u.l.iv);
1275 pc = pet_context_clear_value(pc, iv);
1277 declared = tree->u.l.declared;
1279 scop_init = scop_from_for_init(tree, pc_init, state);
1281 expr_iv = pet_expr_copy(tree->u.l.iv);
1282 type_size = pet_expr_get_type_size(expr_iv);
1283 inc = pet_expr_copy(tree->u.l.inc);
1284 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1286 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1287 pet_tree_get_loc(tree), tree->u.l.body, tree->label,
1288 inc, pet_context_copy(pc), state);
1290 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1292 pet_context_free(pc);
1294 if (!declared)
1295 return scop;
1297 array = extract_array(tree->u.l.iv, pc_init, state);
1298 if (array)
1299 array->declared = 1;
1300 scop_kill = kill(pet_tree_get_loc(tree), array, pc_init, state);
1301 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1302 scop_kill = kill(pet_tree_get_loc(tree), array, pc_init, state);
1303 scop_kill = pet_scop_add_array(scop_kill, array);
1304 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1306 return scop;
1309 /* Given an access expression "expr", is the variable accessed by
1310 * "expr" assigned anywhere inside "tree"?
1312 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1314 int assigned = 0;
1315 isl_id *id;
1317 id = pet_expr_access_get_id(expr);
1318 assigned = pet_tree_writes(tree, id);
1319 isl_id_free(id);
1321 return assigned;
1324 /* Are all nested access parameters in "pa" allowed given "tree".
1325 * In particular, is none of them written by anywhere inside "tree".
1327 * If "tree" has any continue or break nodes in the current loop level,
1328 * then no nested access parameters are allowed.
1329 * In particular, if there is any nested access in a guard
1330 * for a piece of code containing a "continue", then we want to introduce
1331 * a separate statement for evaluating this guard so that we can express
1332 * that the result is false for all previous iterations.
1334 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1335 __isl_keep pet_tree *tree)
1337 int i, nparam;
1339 if (!tree)
1340 return -1;
1342 if (!pet_nested_any_in_pw_aff(pa))
1343 return 1;
1345 if (pet_tree_has_continue_or_break(tree))
1346 return 0;
1348 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1349 for (i = 0; i < nparam; ++i) {
1350 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1351 pet_expr *expr;
1352 int allowed;
1354 if (!pet_nested_in_id(id)) {
1355 isl_id_free(id);
1356 continue;
1359 expr = pet_nested_extract_expr(id);
1360 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1361 !is_assigned(expr, tree);
1363 pet_expr_free(expr);
1364 isl_id_free(id);
1366 if (!allowed)
1367 return 0;
1370 return 1;
1373 /* Internal data structure for collect_local.
1374 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1375 * "local" collects the results.
1377 struct pet_tree_collect_local_data {
1378 pet_context *pc;
1379 struct pet_state *state;
1380 isl_union_set *local;
1383 /* Add the variable accessed by "var" to data->local.
1384 * We extract a representation of the variable from
1385 * the pet_array constructed using extract_array
1386 * to ensure consistency with the rest of the scop.
1388 static int add_local(struct pet_tree_collect_local_data *data,
1389 __isl_keep pet_expr *var)
1391 struct pet_array *array;
1392 isl_set *universe;
1394 array = extract_array(var, data->pc, data->state);
1395 if (!array)
1396 return -1;
1398 universe = isl_set_universe(isl_set_get_space(array->extent));
1399 data->local = isl_union_set_add_set(data->local, universe);
1400 pet_array_free(array);
1402 return 0;
1405 /* If the node "tree" declares a variable, then add it to
1406 * data->local.
1408 static int extract_local_var(__isl_keep pet_tree *tree, void *user)
1410 enum pet_tree_type type;
1411 struct pet_tree_collect_local_data *data = user;
1413 type = pet_tree_get_type(tree);
1414 if (type == pet_tree_decl || type == pet_tree_decl_init)
1415 return add_local(data, tree->u.d.var);
1417 return 0;
1420 /* If the node "tree" is a for loop that declares its induction variable,
1421 * then add it this induction variable to data->local.
1423 static int extract_local_iterator(__isl_keep pet_tree *tree, void *user)
1425 struct pet_tree_collect_local_data *data = user;
1427 if (pet_tree_get_type(tree) == pet_tree_for && tree->u.l.declared)
1428 return add_local(data, tree->u.l.iv);
1430 return 0;
1433 /* Collect and return all local variables of the for loop represented
1434 * by "tree", with "scop" the corresponding pet_scop.
1435 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1437 * We collect not only the variables that are declared inside "tree",
1438 * but also the loop iterators that are declared anywhere inside
1439 * any possible macro statements in "scop".
1440 * The latter also appear as declared variable in the scop,
1441 * whereas other declared loop iterators only appear implicitly
1442 * in the iteration domains.
1444 static __isl_give isl_union_set *collect_local(struct pet_scop *scop,
1445 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1446 struct pet_state *state)
1448 int i;
1449 isl_ctx *ctx;
1450 struct pet_tree_collect_local_data data = { pc, state };
1452 ctx = pet_tree_get_ctx(tree);
1453 data.local = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
1455 if (pet_tree_foreach_sub_tree(tree, &extract_local_var, &data) < 0)
1456 return isl_union_set_free(data.local);
1458 for (i = 0; i < scop->n_stmt; ++i) {
1459 pet_tree *body = scop->stmts[i]->body;
1460 if (pet_tree_foreach_sub_tree(body, &extract_local_iterator,
1461 &data) < 0)
1462 return isl_union_set_free(data.local);
1465 return data.local;
1468 /* Add an independence to "scop" if the for node "tree" was marked
1469 * independent.
1470 * "domain" is the set of loop iterators, with the current for loop
1471 * innermost. If "sign" is positive, then the inner iterator increases.
1472 * Otherwise it decreases.
1473 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1475 * If the tree was marked, then collect all local variables and
1476 * add an independence.
1478 static struct pet_scop *set_independence(struct pet_scop *scop,
1479 __isl_keep pet_tree *tree, __isl_keep isl_set *domain, int sign,
1480 __isl_keep pet_context *pc, struct pet_state *state)
1482 isl_union_set *local;
1484 if (!tree->u.l.independent)
1485 return scop;
1487 local = collect_local(scop, tree, pc, state);
1488 scop = pet_scop_set_independent(scop, domain, local, sign);
1490 return scop;
1493 /* Add a scop for assigning to the variable corresponding to the loop
1494 * iterator the result of adding the increment to the loop iterator
1495 * at the end of a loop body "scop" within the context "pc".
1496 * "tree" represents the for loop.
1498 * The increment is of the form
1500 * iv = iv + inc
1502 * Note that "iv" on the right hand side will be evaluated in terms
1503 * of the (possibly virtual) loop iterator, i.e., the inner dimension
1504 * of the domain, while "iv" on the left hand side will not be evaluated
1505 * (because it is a write) and will continue to refer to the original
1506 * variable.
1508 static __isl_give pet_scop *add_iterator_assignment(__isl_take pet_scop *scop,
1509 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1510 struct pet_state *state)
1512 int type_size;
1513 pet_expr *expr, *iv, *inc;
1515 iv = pet_expr_copy(tree->u.l.iv);
1516 type_size = pet_expr_get_type_size(iv);
1517 iv = pet_expr_access_set_write(iv, 0);
1518 iv = pet_expr_access_set_read(iv, 1);
1519 inc = pet_expr_copy(tree->u.l.inc);
1520 expr = pet_expr_new_binary(type_size, pet_op_add, iv, inc);
1521 iv = pet_expr_copy(tree->u.l.iv);
1522 expr = pet_expr_new_binary(type_size, pet_op_assign, iv, expr);
1524 scop = scop_add_inc(scop, expr, pet_tree_get_loc(tree), pc, state);
1526 return scop;
1529 /* Construct a pet_scop for a for tree with static affine initialization
1530 * and constant increment within the context "pc".
1531 * The domain of "pc" has already been extended with an (at this point
1532 * unbounded) inner loop iterator corresponding to the current for loop.
1534 * The condition is allowed to contain nested accesses, provided
1535 * they are not being written to inside the body of the loop.
1536 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1537 * essentially treated as a while loop, with iteration domain
1538 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1540 * We extract a pet_scop for the body after intersecting the domain of "pc"
1542 * { [l,i] : i >= init and condition' }
1544 * or
1546 * { [l,i] : i <= init and condition' }
1548 * Where condition' is equal to condition if the latter is
1549 * a simple upper [lower] bound and a condition that is extended
1550 * to apply to all previous iterations otherwise.
1551 * Afterwards, the schedule of the pet_scop is extended with
1553 * { [l,i] -> [i] }
1555 * or
1557 * { [l,i] -> [-i] }
1559 * If the condition is non-affine, then we drop the condition from the
1560 * iteration domain and instead create a separate statement
1561 * for evaluating the condition. The body is then filtered to depend
1562 * on the result of the condition evaluating to true on all iterations
1563 * up to the current iteration, while the evaluation the condition itself
1564 * is filtered to depend on the result of the condition evaluating to true
1565 * on all previous iterations.
1566 * The context of the scop representing the body is dropped
1567 * because we don't know how many times the body will be executed,
1568 * if at all.
1570 * If the stride of the loop is not 1, then "i >= init" is replaced by
1572 * (exists a: i = init + stride * a and a >= 0)
1574 * If the loop iterator i is unsigned, then wrapping may occur.
1575 * We therefore use a virtual iterator instead that does not wrap.
1576 * However, the condition in the code applies
1577 * to the wrapped value, so we need to change condition(l,i)
1578 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1579 * to the original iterator by the wrapping of the virtual iterator.
1580 * Note that there may be no need to perform this final wrapping
1581 * if the loop condition (after wrapping) satisfies certain conditions.
1582 * However, the is_simple_bound condition is not enough since it doesn't
1583 * check if there even is an upper bound.
1585 * Wrapping on unsigned iterators can be avoided entirely if
1586 * the loop condition is simple, the loop iterator is incremented
1587 * [decremented] by one and the last value before wrapping cannot
1588 * possibly satisfy the loop condition.
1590 * Valid outer iterators for a for loop are those for which the initial
1591 * value itself, the increment on each domain iteration and
1592 * the condition on both the initial value and
1593 * the result of incrementing the iterator for each iteration of the domain
1594 * can be evaluated.
1595 * If the loop condition is non-affine, then we only consider validity
1596 * of the initial value.
1598 * If the loop iterator was not declared inside the loop header,
1599 * then the variable corresponding to this loop iterator is assigned
1600 * the result of adding the increment at the end of the loop body.
1601 * The assignment of the initial value is taken care of by
1602 * scop_from_affine_for_init.
1604 * If the body contains any break, then we keep track of it in "skip"
1605 * (if the skip condition is affine) or it is handled in scop_add_break
1606 * (if the skip condition is not affine).
1607 * Note that the affine break condition needs to be considered with
1608 * respect to previous iterations in the virtual domain (if any).
1610 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1611 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1612 __isl_take isl_val *inc, __isl_take pet_context *pc,
1613 struct pet_state *state)
1615 isl_set *domain;
1616 isl_multi_aff *sched;
1617 isl_set *cond = NULL;
1618 isl_set *skip = NULL;
1619 isl_id *id_test = NULL, *id_break_test;
1620 struct pet_scop *scop, *scop_cond = NULL;
1621 int pos;
1622 int is_one;
1623 int is_unsigned;
1624 int is_simple;
1625 int is_virtual;
1626 int is_non_affine;
1627 int has_affine_break;
1628 int has_var_break;
1629 isl_map *rev_wrap = NULL;
1630 isl_map *init_val_map;
1631 isl_pw_aff *pa;
1632 isl_set *valid_init;
1633 isl_set *valid_cond;
1634 isl_set *valid_cond_init;
1635 isl_set *valid_cond_next;
1636 isl_set *valid_inc;
1637 pet_expr *cond_expr;
1638 pet_context *pc_nested;
1640 pos = pet_context_dim(pc) - 1;
1642 domain = pet_context_get_domain(pc);
1643 cond_expr = pet_expr_copy(tree->u.l.cond);
1644 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1645 pc_nested = pet_context_copy(pc);
1646 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1647 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1648 pet_context_free(pc_nested);
1649 pet_expr_free(cond_expr);
1651 valid_inc = isl_pw_aff_domain(pa_inc);
1653 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1655 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1656 !is_nested_allowed(pa, tree->u.l.body);
1657 if (is_non_affine)
1658 pa = isl_pw_aff_free(pa);
1660 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1661 cond = isl_pw_aff_non_zero_set(pa);
1662 if (is_non_affine)
1663 cond = isl_set_universe(isl_set_get_space(domain));
1665 valid_cond = isl_set_coalesce(valid_cond);
1666 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1667 is_virtual = is_unsigned &&
1668 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1670 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1671 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1672 isl_dim_out, 0);
1673 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1674 isl_set_copy(valid_cond));
1675 if (is_one && !is_virtual) {
1676 isl_set *cond;
1678 isl_pw_aff_free(init_val);
1679 pa = pet_expr_extract_comparison(
1680 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1681 tree->u.l.iv, tree->u.l.init, pc);
1682 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1683 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1684 isl_set_dim(domain, isl_dim_set) - 1, 1);
1685 cond = isl_pw_aff_non_zero_set(pa);
1686 domain = isl_set_intersect(domain, cond);
1687 } else {
1688 isl_set *strided;
1690 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1691 strided = strided_domain(init_val, isl_val_copy(inc));
1692 domain = isl_set_intersect(domain, strided);
1695 if (is_virtual) {
1696 isl_multi_aff *wrap;
1697 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1698 pc = pet_context_preimage_domain(pc, wrap);
1699 rev_wrap = isl_map_from_multi_aff(wrap);
1700 rev_wrap = isl_map_reverse(rev_wrap);
1701 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1702 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1703 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1705 is_simple = is_simple_bound(cond, inc);
1706 if (!is_simple) {
1707 cond = isl_set_gist(cond, isl_set_copy(domain));
1708 is_simple = is_simple_bound(cond, inc);
1710 if (!is_simple)
1711 cond = valid_for_each_iteration(cond,
1712 isl_set_copy(domain), isl_val_copy(inc));
1713 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1714 domain = isl_set_intersect(domain, cond);
1715 sched = map_to_last(pc, state->n_loop++, tree->label);
1716 if (isl_val_is_neg(inc))
1717 sched = isl_multi_aff_neg(sched);
1719 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1720 isl_val_copy(inc));
1721 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1723 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1725 if (is_non_affine) {
1726 isl_space *space;
1727 isl_multi_pw_aff *test_index;
1728 space = isl_set_get_space(domain);
1729 test_index = pet_create_test_index(space, state->n_test++);
1730 scop_cond = scop_from_non_affine_condition(
1731 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1732 isl_multi_pw_aff_copy(test_index),
1733 pet_tree_get_loc(tree), pc);
1734 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1735 isl_dim_out);
1736 scop_cond = pet_scop_add_boolean_array(scop_cond,
1737 isl_set_copy(domain), test_index,
1738 state->int_size);
1741 scop = scop_from_tree(tree->u.l.body, pc, state);
1742 has_affine_break = scop &&
1743 pet_scop_has_affine_skip(scop, pet_skip_later);
1744 if (has_affine_break)
1745 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1746 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1747 if (has_var_break)
1748 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1749 if (is_non_affine) {
1750 scop = pet_scop_reset_context(scop);
1752 if (!tree->u.l.declared)
1753 scop = add_iterator_assignment(scop, tree, pc, state);
1754 scop = pet_scop_reset_skips(scop);
1755 scop = pet_scop_resolve_nested(scop);
1756 if (has_affine_break) {
1757 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1758 is_virtual, rev_wrap);
1759 scop = pet_scop_intersect_domain_prefix(scop,
1760 isl_set_copy(domain));
1762 isl_map_free(rev_wrap);
1763 if (has_var_break)
1764 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1765 isl_val_copy(inc));
1766 if (is_non_affine)
1767 scop = scop_add_while(scop_cond, scop, id_test,
1768 isl_set_copy(domain),
1769 isl_val_copy(inc));
1770 else
1771 scop = set_independence(scop, tree, domain, isl_val_sgn(inc),
1772 pc, state);
1773 scop = pet_scop_embed(scop, domain, sched);
1774 if (is_non_affine) {
1775 isl_set_free(valid_inc);
1776 } else {
1777 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1778 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1779 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1780 scop = pet_scop_restrict_context(scop, valid_inc);
1783 isl_val_free(inc);
1785 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1786 scop = pet_scop_restrict_context(scop, valid_init);
1788 pet_context_free(pc);
1789 return scop;
1792 /* Construct a pet_scop for a for tree with static affine initialization
1793 * and constant increment within the context "pc_init".
1794 * In particular, "pc_init" represents the context of the loop,
1795 * whereas the domain of "pc" has already been extended with an (at this point
1796 * unbounded) inner loop iterator corresponding to the current for loop.
1798 * If the loop iterator was not declared inside the loop header,
1799 * then add an assignment of the initial value to the loop iterator
1800 * before the loop. The construction of a pet_scop for the loop itself,
1801 * including updates to the loop iterator, is handled by scop_from_affine_for.
1803 static __isl_give pet_scop *scop_from_affine_for_init(__isl_keep pet_tree *tree,
1804 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1805 __isl_take isl_val *inc, __isl_keep pet_context *pc_init,
1806 __isl_take pet_context *pc, struct pet_state *state)
1808 pet_scop *scop_init, *scop;
1810 if (!tree->u.l.declared)
1811 scop_init = scop_from_for_init(tree, pc_init, state);
1813 scop = scop_from_affine_for(tree, init_val, pa_inc, inc, pc, state);
1815 if (!tree->u.l.declared)
1816 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1818 return scop;
1821 /* Construct a pet_scop for a for statement within the context of "pc".
1823 * We update the context to reflect the writes to the loop variable and
1824 * the writes inside the body.
1826 * Then we check if the initialization of the for loop
1827 * is a static affine value and the increment is a constant.
1828 * If so, we construct the pet_scop using scop_from_affine_for_init.
1829 * Otherwise, we treat the for loop as a while loop
1830 * in scop_from_non_affine_for.
1832 * Note that the initialization and the increment are extracted
1833 * in a context where the current loop iterator has been added
1834 * to the context. If these turn out not be affine, then we
1835 * have reconstruct the body context without an assignment
1836 * to this loop iterator, as this variable will then not be
1837 * treated as a dimension of the iteration domain, but as any
1838 * other variable.
1840 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1841 __isl_keep pet_context *init_pc, struct pet_state *state)
1843 isl_id *iv;
1844 isl_val *inc;
1845 isl_pw_aff *pa_inc, *init_val;
1846 pet_context *pc, *pc_init_val;
1848 if (!tree)
1849 return NULL;
1851 iv = pet_expr_access_get_id(tree->u.l.iv);
1852 pc = pet_context_copy(init_pc);
1853 pc = pet_context_add_inner_iterator(pc, iv);
1854 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1856 pc_init_val = pet_context_copy(pc);
1857 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1858 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1859 pet_context_free(pc_init_val);
1860 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1861 inc = pet_extract_cst(pa_inc);
1862 if (!pa_inc || !init_val || !inc)
1863 goto error;
1864 if (!isl_pw_aff_involves_nan(pa_inc) &&
1865 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1866 return scop_from_affine_for_init(tree, init_val, pa_inc, inc,
1867 init_pc, pc, state);
1869 isl_pw_aff_free(pa_inc);
1870 isl_pw_aff_free(init_val);
1871 isl_val_free(inc);
1872 pet_context_free(pc);
1874 pc = pet_context_copy(init_pc);
1875 pc = pet_context_add_infinite_loop(pc);
1876 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1877 return scop_from_non_affine_for(tree, init_pc, pc, state);
1878 error:
1879 isl_pw_aff_free(pa_inc);
1880 isl_pw_aff_free(init_val);
1881 isl_val_free(inc);
1882 pet_context_free(pc);
1883 return NULL;
1886 /* Check whether "expr" is an affine constraint within the context "pc".
1888 static int is_affine_condition(__isl_keep pet_expr *expr,
1889 __isl_keep pet_context *pc)
1891 isl_pw_aff *pa;
1892 int is_affine;
1894 pa = pet_expr_extract_affine_condition(expr, pc);
1895 if (!pa)
1896 return -1;
1897 is_affine = !isl_pw_aff_involves_nan(pa);
1898 isl_pw_aff_free(pa);
1900 return is_affine;
1903 /* Check if the given if statement is a conditional assignement
1904 * with a non-affine condition.
1906 * In particular we check if "stmt" is of the form
1908 * if (condition)
1909 * a = f(...);
1910 * else
1911 * a = g(...);
1913 * where the condition is non-affine and a is some array or scalar access.
1915 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1916 __isl_keep pet_context *pc)
1918 int equal;
1919 isl_ctx *ctx;
1920 pet_expr *expr1, *expr2;
1922 ctx = pet_tree_get_ctx(tree);
1923 if (!pet_options_get_detect_conditional_assignment(ctx))
1924 return 0;
1925 if (tree->type != pet_tree_if_else)
1926 return 0;
1927 if (tree->u.i.then_body->type != pet_tree_expr)
1928 return 0;
1929 if (tree->u.i.else_body->type != pet_tree_expr)
1930 return 0;
1931 expr1 = tree->u.i.then_body->u.e.expr;
1932 expr2 = tree->u.i.else_body->u.e.expr;
1933 if (pet_expr_get_type(expr1) != pet_expr_op)
1934 return 0;
1935 if (pet_expr_get_type(expr2) != pet_expr_op)
1936 return 0;
1937 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1938 return 0;
1939 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1940 return 0;
1941 expr1 = pet_expr_get_arg(expr1, 0);
1942 expr2 = pet_expr_get_arg(expr2, 0);
1943 equal = pet_expr_is_equal(expr1, expr2);
1944 pet_expr_free(expr1);
1945 pet_expr_free(expr2);
1946 if (equal < 0 || !equal)
1947 return 0;
1948 if (is_affine_condition(tree->u.i.cond, pc))
1949 return 0;
1951 return 1;
1954 /* Given that "tree" is of the form
1956 * if (condition)
1957 * a = f(...);
1958 * else
1959 * a = g(...);
1961 * where a is some array or scalar access, construct a pet_scop
1962 * corresponding to this conditional assignment within the context "pc".
1963 * "cond_pa" is an affine expression with nested accesses representing
1964 * the condition.
1966 * The constructed pet_scop then corresponds to the expression
1968 * a = condition ? f(...) : g(...)
1970 * All access relations in f(...) are intersected with condition
1971 * while all access relation in g(...) are intersected with the complement.
1973 static struct pet_scop *scop_from_conditional_assignment(
1974 __isl_keep pet_tree *tree, __isl_take isl_pw_aff *cond_pa,
1975 __isl_take pet_context *pc, struct pet_state *state)
1977 int type_size;
1978 isl_set *cond, *comp;
1979 isl_multi_pw_aff *index;
1980 pet_expr *expr1, *expr2;
1981 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1982 struct pet_scop *scop;
1984 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa));
1985 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa));
1986 index = isl_multi_pw_aff_from_pw_aff(cond_pa);
1988 expr1 = tree->u.i.then_body->u.e.expr;
1989 expr2 = tree->u.i.else_body->u.e.expr;
1991 pe_cond = pet_expr_from_index(index);
1993 pe_then = pet_expr_get_arg(expr1, 1);
1994 pe_then = pet_context_evaluate_expr(pc, pe_then);
1995 pe_then = pet_expr_restrict(pe_then, cond);
1996 pe_else = pet_expr_get_arg(expr2, 1);
1997 pe_else = pet_context_evaluate_expr(pc, pe_else);
1998 pe_else = pet_expr_restrict(pe_else, comp);
1999 pe_write = pet_expr_get_arg(expr1, 0);
2000 pe_write = pet_context_evaluate_expr(pc, pe_write);
2002 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
2003 type_size = pet_expr_get_type_size(pe_write);
2004 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
2006 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
2007 pet_tree_get_loc(tree), pc);
2009 pet_context_free(pc);
2011 return scop;
2014 /* Construct a pet_scop for a non-affine if statement within the context "pc".
2016 * We create a separate statement that writes the result
2017 * of the non-affine condition to a virtual scalar.
2018 * A constraint requiring the value of this virtual scalar to be one
2019 * is added to the iteration domains of the then branch.
2020 * Similarly, a constraint requiring the value of this virtual scalar
2021 * to be zero is added to the iteration domains of the else branch, if any.
2022 * We combine the schedules as a sequence to ensure that the virtual scalar
2023 * is written before it is read.
2025 * If there are any breaks or continues in the then and/or else
2026 * branches, then we may have to compute a new skip condition.
2027 * This is handled using a pet_skip_info object.
2028 * On initialization, the object checks if skip conditions need
2029 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
2030 * adds them in pet_skip_info_add.
2032 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
2033 __isl_take pet_context *pc, struct pet_state *state)
2035 int has_else;
2036 isl_space *space;
2037 isl_set *domain;
2038 isl_multi_pw_aff *test_index;
2039 struct pet_skip_info skip;
2040 struct pet_scop *scop, *scop_then, *scop_else = NULL;
2042 has_else = tree->type == pet_tree_if_else;
2044 space = pet_context_get_space(pc);
2045 test_index = pet_create_test_index(space, state->n_test++);
2046 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
2047 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
2048 pet_tree_get_loc(tree), pc);
2049 domain = pet_context_get_domain(pc);
2050 scop = pet_scop_add_boolean_array(scop, domain,
2051 isl_multi_pw_aff_copy(test_index), state->int_size);
2053 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
2054 if (has_else)
2055 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
2057 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
2058 has_else, 0);
2059 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
2061 scop_then = pet_scop_filter(scop_then,
2062 isl_multi_pw_aff_copy(test_index), 1);
2063 if (has_else) {
2064 scop_else = pet_scop_filter(scop_else, test_index, 0);
2065 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
2066 } else
2067 isl_multi_pw_aff_free(test_index);
2069 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
2071 scop = pet_skip_info_add(&skip, scop);
2073 pet_context_free(pc);
2074 return scop;
2077 /* Construct a pet_scop for an affine if statement within the context "pc".
2079 * The condition is added to the iteration domains of the then branch,
2080 * while the opposite of the condition in added to the iteration domains
2081 * of the else branch, if any.
2083 * If there are any breaks or continues in the then and/or else
2084 * branches, then we may have to compute a new skip condition.
2085 * This is handled using a pet_skip_info_if object.
2086 * On initialization, the object checks if skip conditions need
2087 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
2088 * adds them in pet_skip_info_add.
2090 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
2091 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
2092 struct pet_state *state)
2094 int has_else;
2095 isl_ctx *ctx;
2096 isl_set *set, *complement;
2097 isl_set *valid;
2098 struct pet_skip_info skip;
2099 struct pet_scop *scop, *scop_then, *scop_else = NULL;
2100 pet_context *pc_body;
2102 ctx = pet_tree_get_ctx(tree);
2104 has_else = tree->type == pet_tree_if_else;
2106 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
2107 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2109 pc_body = pet_context_copy(pc);
2110 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
2111 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
2112 pet_context_free(pc_body);
2113 if (has_else) {
2114 pc_body = pet_context_copy(pc);
2115 complement = isl_set_copy(valid);
2116 complement = isl_set_subtract(valid, isl_set_copy(set));
2117 pc_body = pet_context_intersect_domain(pc_body,
2118 isl_set_copy(complement));
2119 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
2120 pet_context_free(pc_body);
2123 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
2124 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
2125 isl_pw_aff_free(cond);
2127 scop = pet_scop_restrict(scop_then, set);
2129 if (has_else) {
2130 scop_else = pet_scop_restrict(scop_else, complement);
2131 scop = pet_scop_add_par(ctx, scop, scop_else);
2133 scop = pet_scop_resolve_nested(scop);
2134 scop = pet_scop_restrict_context(scop, valid);
2136 scop = pet_skip_info_add(&skip, scop);
2138 pet_context_free(pc);
2139 return scop;
2142 /* Construct a pet_scop for an if statement within the context "pc".
2144 * If the condition fits the pattern of a conditional assignment,
2145 * then it is handled by scop_from_conditional_assignment.
2146 * Note that the condition is only considered for a conditional assignment
2147 * if it is not static-affine. However, it should still convert
2148 * to an affine expression when nesting is allowed.
2150 * Otherwise, we check if the condition is affine.
2151 * If so, we construct the scop in scop_from_affine_if.
2152 * Otherwise, we construct the scop in scop_from_non_affine_if.
2154 * We allow the condition to be dynamic, i.e., to refer to
2155 * scalars or array elements that may be written to outside
2156 * of the given if statement. These nested accesses are then represented
2157 * as output dimensions in the wrapping iteration domain.
2158 * If it is also written _inside_ the then or else branch, then
2159 * we treat the condition as non-affine.
2160 * As explained in extract_non_affine_if, this will introduce
2161 * an extra statement.
2162 * For aesthetic reasons, we want this statement to have a statement
2163 * number that is lower than those of the then and else branches.
2164 * In order to evaluate if we will need such a statement, however, we
2165 * first construct scops for the then and else branches.
2166 * We therefore reserve a statement number if we might have to
2167 * introduce such an extra statement.
2169 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
2170 __isl_keep pet_context *pc, struct pet_state *state)
2172 int has_else;
2173 isl_pw_aff *cond;
2174 pet_expr *cond_expr;
2175 pet_context *pc_nested;
2177 if (!tree)
2178 return NULL;
2180 has_else = tree->type == pet_tree_if_else;
2182 pc = pet_context_copy(pc);
2183 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
2184 if (has_else)
2185 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
2187 cond_expr = pet_expr_copy(tree->u.i.cond);
2188 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
2189 pc_nested = pet_context_copy(pc);
2190 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
2191 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
2192 pet_context_free(pc_nested);
2193 pet_expr_free(cond_expr);
2195 if (!cond) {
2196 pet_context_free(pc);
2197 return NULL;
2200 if (isl_pw_aff_involves_nan(cond)) {
2201 isl_pw_aff_free(cond);
2202 return scop_from_non_affine_if(tree, pc, state);
2205 if (is_conditional_assignment(tree, pc))
2206 return scop_from_conditional_assignment(tree, cond, pc, state);
2208 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
2209 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
2210 isl_pw_aff_free(cond);
2211 return scop_from_non_affine_if(tree, pc, state);
2214 return scop_from_affine_if(tree, cond, pc, state);
2217 /* Return a one-dimensional multi piecewise affine expression that is equal
2218 * to the constant 1 and is defined over the given domain.
2220 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
2222 isl_local_space *ls;
2223 isl_aff *aff;
2225 ls = isl_local_space_from_space(space);
2226 aff = isl_aff_zero_on_domain(ls);
2227 aff = isl_aff_set_constant_si(aff, 1);
2229 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
2232 /* Construct a pet_scop for a continue statement with the given domain space.
2234 * We simply create an empty scop with a universal pet_skip_now
2235 * skip condition. This skip condition will then be taken into
2236 * account by the enclosing loop construct, possibly after
2237 * being incorporated into outer skip conditions.
2239 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
2240 __isl_take isl_space *space)
2242 struct pet_scop *scop;
2244 scop = pet_scop_empty(isl_space_copy(space));
2246 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
2248 return scop;
2251 /* Construct a pet_scop for a break statement with the given domain space.
2253 * We simply create an empty scop with both a universal pet_skip_now
2254 * skip condition and a universal pet_skip_later skip condition.
2255 * These skip conditions will then be taken into
2256 * account by the enclosing loop construct, possibly after
2257 * being incorporated into outer skip conditions.
2259 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
2260 __isl_take isl_space *space)
2262 struct pet_scop *scop;
2263 isl_multi_pw_aff *skip;
2265 scop = pet_scop_empty(isl_space_copy(space));
2267 skip = one_mpa(space);
2268 scop = pet_scop_set_skip(scop, pet_skip_now,
2269 isl_multi_pw_aff_copy(skip));
2270 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
2272 return scop;
2275 /* Extract a clone of the kill statement "stmt".
2276 * The domain of the clone is given by "domain".
2278 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
2279 struct pet_stmt *stmt, struct pet_state *state)
2281 pet_expr *kill;
2282 isl_space *space;
2283 isl_multi_pw_aff *mpa;
2284 pet_tree *tree;
2286 if (!domain || !stmt)
2287 return NULL;
2289 kill = pet_tree_expr_get_expr(stmt->body);
2290 space = pet_stmt_get_space(stmt);
2291 space = isl_space_map_from_set(space);
2292 mpa = isl_multi_pw_aff_identity(space);
2293 mpa = isl_multi_pw_aff_reset_tuple_id(mpa, isl_dim_in);
2294 kill = pet_expr_update_domain(kill, mpa);
2295 tree = pet_tree_new_expr(kill);
2296 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
2297 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
2298 state->n_stmt++, tree);
2299 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
2302 /* Extract a clone of the kill statements in "scop".
2303 * The domain of each clone is given by "domain".
2304 * "scop" is expected to have been created from a DeclStmt
2305 * and should have (one of) the kill(s) as its first statement.
2306 * If "scop" was created from a declaration group, then there
2307 * may be multiple kill statements inside.
2309 static struct pet_scop *extract_kills(__isl_keep isl_set *domain,
2310 struct pet_scop *scop, struct pet_state *state)
2312 isl_ctx *ctx;
2313 struct pet_stmt *stmt;
2314 struct pet_scop *kill;
2315 int i;
2317 if (!domain || !scop)
2318 return NULL;
2319 ctx = isl_set_get_ctx(domain);
2320 if (scop->n_stmt < 1)
2321 isl_die(ctx, isl_error_internal,
2322 "expecting at least one statement", return NULL);
2323 stmt = scop->stmts[0];
2324 if (!pet_stmt_is_kill(stmt))
2325 isl_die(ctx, isl_error_internal,
2326 "expecting kill statement", return NULL);
2328 kill = extract_kill(domain, stmt, state);
2330 for (i = 1; i < scop->n_stmt; ++i) {
2331 struct pet_scop *kill_i;
2333 stmt = scop->stmts[i];
2334 if (!pet_stmt_is_kill(stmt))
2335 continue;
2337 kill_i = extract_kill(domain, stmt, state);
2338 kill = pet_scop_add_par(ctx, kill, kill_i);
2341 return kill;
2344 /* Has "tree" been created from a DeclStmt?
2345 * That is, is it either a declaration or a group of declarations?
2347 static int tree_is_decl(__isl_keep pet_tree *tree)
2349 int is_decl;
2350 int i;
2352 if (!tree)
2353 return -1;
2354 is_decl = pet_tree_is_decl(tree);
2355 if (is_decl < 0 || is_decl)
2356 return is_decl;
2358 if (tree->type != pet_tree_block)
2359 return 0;
2360 if (pet_tree_block_get_block(tree))
2361 return 0;
2362 if (tree->u.b.n == 0)
2363 return 0;
2365 for (i = 0; i < tree->u.b.n; ++i) {
2366 is_decl = tree_is_decl(tree->u.b.child[i]);
2367 if (is_decl < 0 || !is_decl)
2368 return is_decl;
2371 return 1;
2374 /* Does "tree" represent an assignment to a variable?
2376 * The assignment may be one of
2377 * - a declaration with initialization
2378 * - an expression with a top-level assignment operator
2380 static int is_assignment(__isl_keep pet_tree *tree)
2382 if (!tree)
2383 return 0;
2384 if (tree->type == pet_tree_decl_init)
2385 return 1;
2386 return pet_tree_is_assign(tree);
2389 /* Update "pc" by taking into account the assignment performed by "tree",
2390 * where "tree" satisfies is_assignment.
2392 * In particular, if the lhs of the assignment is a scalar variable and
2393 * if the rhs is an affine expression, then keep track of this value in "pc"
2394 * so that we can plug it in when we later come across the same variable.
2396 * Any previously assigned value to the variable has already been removed
2397 * by scop_handle_writes.
2399 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
2400 __isl_keep pet_tree *tree)
2402 pet_expr *var, *val;
2403 isl_id *id;
2404 isl_pw_aff *pa;
2406 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
2407 var = pet_tree_decl_get_var(tree);
2408 val = pet_tree_decl_get_init(tree);
2409 } else {
2410 pet_expr *expr;
2411 expr = pet_tree_expr_get_expr(tree);
2412 var = pet_expr_get_arg(expr, 0);
2413 val = pet_expr_get_arg(expr, 1);
2414 pet_expr_free(expr);
2417 if (!pet_expr_is_scalar_access(var)) {
2418 pet_expr_free(var);
2419 pet_expr_free(val);
2420 return pc;
2423 pa = pet_expr_extract_affine(val, pc);
2424 if (!pa)
2425 pc = pet_context_free(pc);
2427 if (!isl_pw_aff_involves_nan(pa)) {
2428 id = pet_expr_access_get_id(var);
2429 pc = pet_context_set_value(pc, id, pa);
2430 } else {
2431 isl_pw_aff_free(pa);
2433 pet_expr_free(var);
2434 pet_expr_free(val);
2436 return pc;
2439 /* Mark all arrays in "scop" as being exposed.
2441 static struct pet_scop *mark_exposed(struct pet_scop *scop)
2443 int i;
2445 if (!scop)
2446 return NULL;
2447 for (i = 0; i < scop->n_array; ++i)
2448 scop->arrays[i]->exposed = 1;
2449 return scop;
2452 /* Try and construct a pet_scop corresponding to (part of)
2453 * a sequence of statements within the context "pc".
2455 * After extracting a statement, we update "pc"
2456 * based on the top-level assignments in the statement
2457 * so that we can exploit them in subsequent statements in the same block.
2458 * Top-level affine assumptions are also recorded in the context.
2460 * If there are any breaks or continues in the individual statements,
2461 * then we may have to compute a new skip condition.
2462 * This is handled using a pet_skip_info object.
2463 * On initialization, the object checks if skip conditions need
2464 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2465 * adds them in pet_skip_info_add.
2467 * If "block" is set, then we need to insert kill statements at
2468 * the end of the block for any array that has been declared by
2469 * one of the statements in the sequence. Each of these declarations
2470 * results in the construction of a kill statement at the place
2471 * of the declaration, so we simply collect duplicates of
2472 * those kill statements and append these duplicates to the constructed scop.
2474 * If "block" is not set, then any array declared by one of the statements
2475 * in the sequence is marked as being exposed.
2477 * If autodetect is set, then we allow the extraction of only a subrange
2478 * of the sequence of statements. However, if there is at least one statement
2479 * for which we could not construct a scop and the final range contains
2480 * either no statements or at least one kill, then we discard the entire
2481 * range.
2483 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2484 __isl_keep pet_context *pc, struct pet_state *state)
2486 int i;
2487 isl_ctx *ctx;
2488 isl_space *space;
2489 isl_set *domain;
2490 struct pet_scop *scop, *kills;
2492 ctx = pet_tree_get_ctx(tree);
2494 space = pet_context_get_space(pc);
2495 domain = pet_context_get_domain(pc);
2496 pc = pet_context_copy(pc);
2497 scop = pet_scop_empty(isl_space_copy(space));
2498 kills = pet_scop_empty(space);
2499 for (i = 0; i < tree->u.b.n; ++i) {
2500 struct pet_scop *scop_i;
2502 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2503 pc = apply_affine_continue(pc, scop);
2504 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2505 if (pet_tree_is_assume(tree->u.b.child[i]))
2506 pc = scop_add_affine_assumption(scop_i, pc);
2507 pc = scop_handle_writes(scop_i, pc);
2508 if (is_assignment(tree->u.b.child[i]))
2509 pc = handle_assignment(pc, tree->u.b.child[i]);
2510 struct pet_skip_info skip;
2511 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2512 pet_skip_info_seq_extract(&skip, pc, state);
2513 if (scop_i && tree_is_decl(tree->u.b.child[i])) {
2514 if (tree->u.b.block) {
2515 struct pet_scop *kill;
2516 kill = extract_kills(domain, scop_i, state);
2517 kills = pet_scop_add_par(ctx, kills, kill);
2518 } else
2519 scop_i = mark_exposed(scop_i);
2521 scop = pet_scop_add_seq(ctx, scop, scop_i);
2523 scop = pet_skip_info_add(&skip, scop);
2525 if (!scop)
2526 break;
2528 isl_set_free(domain);
2530 scop = pet_scop_add_seq(ctx, scop, kills);
2532 pet_context_free(pc);
2534 return scop;
2537 /* Internal data structure for extract_declared_arrays.
2539 * "pc" and "state" are used to create pet_array objects and kill statements.
2540 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2541 * found any declared array.
2542 * "scop" has been initialized by the caller and is used to attach
2543 * the created pet_array objects.
2544 * "kill_before" and "kill_after" are created and updated by
2545 * extract_declared_arrays to collect the kills of the arrays.
2547 struct pet_tree_extract_declared_arrays_data {
2548 pet_context *pc;
2549 struct pet_state *state;
2551 isl_ctx *ctx;
2553 int any;
2554 struct pet_scop *scop;
2555 struct pet_scop *kill_before;
2556 struct pet_scop *kill_after;
2559 /* Check if the node "node" declares any array or scalar.
2560 * If so, create the corresponding pet_array and attach it to data->scop.
2561 * Additionally, create two kill statements for the array and add them
2562 * to data->kill_before and data->kill_after.
2564 static int extract_declared_arrays(__isl_keep pet_tree *node, void *user)
2566 enum pet_tree_type type;
2567 struct pet_tree_extract_declared_arrays_data *data = user;
2568 struct pet_array *array;
2569 struct pet_scop *scop_kill;
2570 pet_expr *var;
2572 type = pet_tree_get_type(node);
2573 if (type == pet_tree_decl || type == pet_tree_decl_init)
2574 var = node->u.d.var;
2575 else if (type == pet_tree_for && node->u.l.declared)
2576 var = node->u.l.iv;
2577 else
2578 return 0;
2580 array = extract_array(var, data->pc, data->state);
2581 if (array)
2582 array->declared = 1;
2583 data->scop = pet_scop_add_array(data->scop, array);
2585 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2586 if (!data->any)
2587 data->kill_before = scop_kill;
2588 else
2589 data->kill_before = pet_scop_add_par(data->ctx,
2590 data->kill_before, scop_kill);
2592 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2593 if (!data->any)
2594 data->kill_after = scop_kill;
2595 else
2596 data->kill_after = pet_scop_add_par(data->ctx,
2597 data->kill_after, scop_kill);
2599 data->any = 1;
2601 return 0;
2604 /* Convert a pet_tree that consists of more than a single leaf
2605 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2606 * Do so within the context of "pc", taking into account the writes inside
2607 * "tree". That is, first clear any previously assigned values to variables
2608 * that are written by "tree".
2610 * After constructing the core scop, we also look for any arrays (or scalars)
2611 * that are declared inside "tree". Each of those arrays is marked as
2612 * having been declared and kill statements for these arrays
2613 * are introduced before and after the core scop.
2614 * Note that the input tree is not a leaf so that the declaration
2615 * cannot occur at the outer level.
2617 static struct pet_scop *scop_from_tree_macro(__isl_take pet_tree *tree,
2618 __isl_keep pet_context *pc, struct pet_state *state)
2620 struct pet_tree_extract_declared_arrays_data data = { pc, state };
2622 data.pc = pet_context_copy(data.pc);
2623 data.pc = pet_context_clear_writes_in_tree(data.pc, tree);
2624 data.scop = scop_from_unevaluated_tree(pet_tree_copy(tree),
2625 state->n_stmt++, data.pc);
2627 data.any = 0;
2628 data.ctx = pet_context_get_ctx(data.pc);
2629 if (pet_tree_foreach_sub_tree(tree, &extract_declared_arrays,
2630 &data) < 0)
2631 data.scop = pet_scop_free(data.scop);
2632 pet_tree_free(tree);
2633 pet_context_free(data.pc);
2635 if (!data.any)
2636 return data.scop;
2638 data.scop = pet_scop_add_seq(data.ctx, data.kill_before, data.scop);
2639 data.scop = pet_scop_add_seq(data.ctx, data.scop, data.kill_after);
2641 return data.scop;
2644 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2645 * within the context "pc" by calling the appropriate function
2646 * based on the type of "tree".
2648 * If the initially constructed pet_scop turns out to involve
2649 * dynamic control and if the user has requested an encapsulation
2650 * of all dynamic control, then this pet_scop is discarded and
2651 * a new pet_scop is created with a single statement representing
2652 * the entire "tree".
2653 * However, if the scop contains any active continue or break,
2654 * then we need to include the loop containing the continue or break
2655 * in the encapsulation. We therefore postpone the encapsulation
2656 * until we have constructed a pet_scop for this enclosing loop.
2658 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2659 __isl_keep pet_context *pc, struct pet_state *state)
2661 isl_ctx *ctx;
2662 struct pet_scop *scop = NULL;
2664 if (!tree)
2665 return NULL;
2667 ctx = pet_tree_get_ctx(tree);
2668 switch (tree->type) {
2669 case pet_tree_error:
2670 return NULL;
2671 case pet_tree_block:
2672 return scop_from_block(tree, pc, state);
2673 case pet_tree_break:
2674 return scop_from_break(tree, pet_context_get_space(pc));
2675 case pet_tree_continue:
2676 return scop_from_continue(tree, pet_context_get_space(pc));
2677 case pet_tree_decl:
2678 case pet_tree_decl_init:
2679 return scop_from_decl(tree, pc, state);
2680 case pet_tree_expr:
2681 return scop_from_tree_expr(tree, pc, state);
2682 case pet_tree_if:
2683 case pet_tree_if_else:
2684 scop = scop_from_if(tree, pc, state);
2685 break;
2686 case pet_tree_for:
2687 scop = scop_from_for(tree, pc, state);
2688 break;
2689 case pet_tree_while:
2690 scop = scop_from_while(tree, pc, state);
2691 break;
2692 case pet_tree_infinite_loop:
2693 scop = scop_from_infinite_for(tree, pc, state);
2694 break;
2697 if (!scop)
2698 return NULL;
2700 if (!pet_options_get_encapsulate_dynamic_control(ctx) ||
2701 !pet_scop_has_data_dependent_conditions(scop) ||
2702 pet_scop_has_var_skip(scop, pet_skip_now))
2703 return scop;
2705 pet_scop_free(scop);
2706 return scop_from_tree_macro(pet_tree_copy(tree), pc, state);
2709 /* If "tree" has a label that is of the form S_<nr>, then make
2710 * sure that state->n_stmt is greater than nr to ensure that
2711 * we will not generate S_<nr> ourselves.
2713 static int set_first_stmt(__isl_keep pet_tree *tree, void *user)
2715 struct pet_state *state = user;
2716 const char *name;
2717 int nr;
2719 if (!tree)
2720 return -1;
2721 if (!tree->label)
2722 return 0;
2723 name = isl_id_get_name(tree->label);
2724 if (strncmp(name, "S_", 2) != 0)
2725 return 0;
2726 nr = atoi(name + 2);
2727 if (nr >= state->n_stmt)
2728 state->n_stmt = nr + 1;
2730 return 0;
2733 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2734 * "int_size" is the number of bytes need to represent an integer.
2735 * "extract_array" is a callback that we can use to create a pet_array
2736 * that corresponds to the variable accessed by an expression.
2738 * Initialize the global state, construct a context and then
2739 * construct the pet_scop by recursively visiting the tree.
2741 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2743 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2744 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2745 __isl_keep pet_context *pc, void *user), void *user,
2746 __isl_keep pet_context *pc)
2748 struct pet_scop *scop;
2749 struct pet_state state = { 0 };
2751 if (!tree)
2752 return NULL;
2754 state.ctx = pet_tree_get_ctx(tree);
2755 state.int_size = int_size;
2756 state.extract_array = extract_array;
2757 state.user = user;
2758 if (pet_tree_foreach_sub_tree(tree, &set_first_stmt, &state) < 0)
2759 tree = pet_tree_free(tree);
2761 scop = scop_from_tree(tree, pc, &state);
2762 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2764 pet_tree_free(tree);
2766 if (scop)
2767 scop->context = isl_set_params(scop->context);
2769 return scop;