pet_expr_filter: avoid introduction of constraints in index expression
[pet.git] / tree2scop.c
blob5a6fbb87b4bf7df724fad3f0ccf5990bc716503c
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 <isl/id_to_pw_aff.h>
37 #include "aff.h"
38 #include "expr.h"
39 #include "expr_arg.h"
40 #include "nest.h"
41 #include "scop.h"
42 #include "skip.h"
43 #include "state.h"
44 #include "tree2scop.h"
46 /* Update "pc" by taking into account the writes in "stmt".
47 * That is, mark all scalar variables that are written by "stmt"
48 * as having an unknown value.
50 static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
51 __isl_take pet_context *pc)
53 return pet_context_clear_writes_in_expr(pc, stmt->body);
56 /* Update "pc" based on the write accesses in "scop".
58 static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
59 __isl_take pet_context *pc)
61 int i;
63 if (!scop)
64 return pet_context_free(pc);
65 for (i = 0; i < scop->n_stmt; ++i)
66 pc = handle_writes(scop->stmts[i], pc);
68 return pc;
71 /* Convert a top-level pet_expr to a pet_scop with one statement
72 * within the context "pc".
73 * This mainly involves resolving nested expression parameters
74 * and setting the name of the iteration space.
75 * The name is given by "label" if it is non-NULL. Otherwise,
76 * it is of the form S_<stmt_nr>.
77 * The location of the statement is set to "loc".
79 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
80 __isl_take isl_id *label, int stmt_nr, __isl_take pet_loc *loc,
81 __isl_keep pet_context *pc)
83 isl_space *space;
84 isl_set *domain;
85 struct pet_stmt *ps;
87 space = pet_context_get_space(pc);
89 expr = pet_expr_plug_in_args(expr, pc);
90 expr = pet_expr_resolve_nested(expr, space);
91 expr = pet_expr_resolve_assume(expr, pc);
92 domain = pet_context_get_domain(pc);
93 ps = pet_stmt_from_pet_expr(domain, loc, label, stmt_nr, expr);
94 return pet_scop_from_pet_stmt(space, ps);
97 /* Construct a pet_scop with a single statement killing the entire
98 * array "array".
99 * The location of the statement is set to "loc".
101 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
102 __isl_keep pet_context *pc, struct pet_state *state)
104 isl_ctx *ctx;
105 isl_id *id;
106 isl_space *space;
107 isl_multi_pw_aff *index;
108 isl_map *access;
109 pet_expr *expr;
110 struct pet_scop *scop;
112 if (!array)
113 goto error;
114 ctx = isl_set_get_ctx(array->extent);
115 access = isl_map_from_range(isl_set_copy(array->extent));
116 id = isl_set_get_tuple_id(array->extent);
117 space = isl_space_alloc(ctx, 0, 0, 0);
118 space = isl_space_set_tuple_id(space, isl_dim_out, id);
119 index = isl_multi_pw_aff_zero(space);
120 expr = pet_expr_kill_from_access_and_index(access, index);
121 return scop_from_expr(expr, NULL, state->n_stmt++, loc, pc);
122 error:
123 pet_loc_free(loc);
124 return NULL;
127 /* Construct and return a pet_array corresponding to the variable
128 * accessed by "access" by calling the extract_array callback.
130 static struct pet_array *extract_array(__isl_keep pet_expr *access,
131 __isl_keep pet_context *pc, struct pet_state *state)
133 return state->extract_array(access, pc, state->user);
136 /* Construct a pet_scop for a (single) variable declaration
137 * within the context "pc".
139 * The scop contains the variable being declared (as an array)
140 * and a statement killing the array.
142 * If the declaration comes with an initialization, then the scop
143 * also contains an assignment to the variable.
145 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
146 __isl_keep pet_context *pc, struct pet_state *state)
148 int type_size;
149 isl_ctx *ctx;
150 struct pet_array *array;
151 struct pet_scop *scop_decl, *scop;
152 pet_expr *lhs, *rhs, *pe;
154 array = extract_array(tree->u.d.var, pc, state);
155 if (array)
156 array->declared = 1;
157 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
158 scop_decl = pet_scop_add_array(scop_decl, array);
160 if (tree->type != pet_tree_decl_init)
161 return scop_decl;
163 lhs = pet_expr_copy(tree->u.d.var);
164 rhs = pet_expr_copy(tree->u.d.init);
165 type_size = pet_expr_get_type_size(lhs);
166 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
167 scop = scop_from_expr(pe, NULL, state->n_stmt++,
168 pet_tree_get_loc(tree), pc);
170 scop_decl = pet_scop_prefix(scop_decl, 0);
171 scop = pet_scop_prefix(scop, 1);
173 ctx = pet_tree_get_ctx(tree);
174 scop = pet_scop_add_seq(ctx, scop_decl, scop);
176 return scop;
179 /* Embed the given iteration domain in an extra outer loop
180 * with induction variable "var".
181 * If this variable appeared as a parameter in the constraints,
182 * it is replaced by the new outermost dimension.
184 static __isl_give isl_set *embed(__isl_take isl_set *set,
185 __isl_take isl_id *var)
187 int pos;
189 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
190 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
191 if (pos >= 0) {
192 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
193 set = isl_set_project_out(set, isl_dim_param, pos, 1);
196 isl_id_free(var);
197 return set;
200 /* Return those elements in the space of "cond" that come after
201 * (based on "sign") an element in "cond" in the final dimension.
203 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
205 isl_space *space;
206 isl_map *previous_to_this;
207 int i, dim;
209 dim = isl_set_dim(cond, isl_dim_set);
210 space = isl_space_map_from_set(isl_set_get_space(cond));
211 previous_to_this = isl_map_universe(space);
212 for (i = 0; i + 1 < dim; ++i)
213 previous_to_this = isl_map_equate(previous_to_this,
214 isl_dim_in, i, isl_dim_out, i);
215 if (sign > 0)
216 previous_to_this = isl_map_order_lt(previous_to_this,
217 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
218 else
219 previous_to_this = isl_map_order_gt(previous_to_this,
220 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
222 cond = isl_set_apply(cond, previous_to_this);
224 return cond;
227 /* Remove those iterations of "domain" that have an earlier iteration
228 * (based on "sign") where "skip" is satisfied.
229 * "domain" has an extra outer loop compared to "skip".
230 * The skip condition is first embedded in the same space as "domain".
231 * If "apply_skip_map" is set, then "skip_map" is first applied
232 * to the embedded skip condition before removing it from the domain.
234 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
235 __isl_take isl_set *skip, int sign,
236 int apply_skip_map, __isl_keep isl_map *skip_map)
238 skip = embed(skip, isl_set_get_dim_id(domain, isl_dim_set, 0));
239 if (apply_skip_map)
240 skip = isl_set_apply(skip, isl_map_copy(skip_map));
241 skip = isl_set_intersect(skip , isl_set_copy(domain));
242 return isl_set_subtract(domain, after(skip, sign));
245 /* Create the infinite iteration domain
247 * { [id] : id >= 0 }
249 static __isl_give isl_set *infinite_domain(__isl_take isl_id *id)
251 isl_ctx *ctx = isl_id_get_ctx(id);
252 isl_set *domain;
254 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
255 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, id);
257 return domain;
260 /* Create an identity affine expression on the space containing "domain",
261 * which is assumed to be one-dimensional.
263 static __isl_give isl_aff *identity_aff(__isl_keep isl_set *domain)
265 isl_local_space *ls;
267 ls = isl_local_space_from_space(isl_set_get_space(domain));
268 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
271 /* Create an affine expression that maps elements
272 * of an array "id_test" to the previous element in the final dimension
273 * (according to "inc"), provided this element belongs to "domain".
274 * That is, create the affine expression
276 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
278 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
279 __isl_take isl_set *domain, __isl_take isl_val *inc)
281 int pos;
282 isl_space *space;
283 isl_aff *aff;
284 isl_pw_aff *pa;
285 isl_multi_aff *ma;
286 isl_multi_pw_aff *prev;
288 pos = isl_set_dim(domain, isl_dim_set) - 1;
289 space = isl_set_get_space(domain);
290 space = isl_space_map_from_set(space);
291 ma = isl_multi_aff_identity(space);
292 aff = isl_multi_aff_get_aff(ma, pos);
293 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
294 ma = isl_multi_aff_set_aff(ma, pos, aff);
295 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
296 prev = isl_multi_pw_aff_from_multi_aff(ma);
297 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
298 pa = isl_pw_aff_intersect_domain(pa, domain);
299 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
300 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
302 return prev;
305 /* Add an implication to "scop" expressing that if an element of
306 * virtual array "id_test" has value "satisfied" then all previous elements
307 * of this array (in the final dimension) also have that value.
308 * The set of previous elements is bounded by "domain".
309 * If "sign" is negative then the iterator
310 * is decreasing and we express that all subsequent array elements
311 * (but still defined previously) have the same value.
313 static struct pet_scop *add_implication(struct pet_scop *scop,
314 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
315 int satisfied)
317 int i, dim;
318 isl_space *space;
319 isl_map *map;
321 dim = isl_set_dim(domain, isl_dim_set);
322 domain = isl_set_set_tuple_id(domain, id_test);
323 space = isl_space_map_from_set(isl_set_get_space(domain));
324 map = isl_map_universe(space);
325 for (i = 0; i + 1 < dim; ++i)
326 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
327 if (sign > 0)
328 map = isl_map_order_ge(map,
329 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
330 else
331 map = isl_map_order_le(map,
332 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
333 map = isl_map_intersect_range(map, domain);
334 scop = pet_scop_add_implication(scop, map, satisfied);
336 return scop;
339 /* Add a filter to "scop" that imposes that it is only executed
340 * when the variable identified by "id_test" has a zero value
341 * for all previous iterations of "domain".
343 * In particular, add a filter that imposes that the array
344 * has a zero value at the previous iteration of domain and
345 * add an implication that implies that it then has that
346 * value for all previous iterations.
348 static struct pet_scop *scop_add_break(struct pet_scop *scop,
349 __isl_take isl_id *id_test, __isl_take isl_set *domain,
350 __isl_take isl_val *inc)
352 isl_multi_pw_aff *prev;
353 int sign = isl_val_sgn(inc);
355 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
356 scop = add_implication(scop, id_test, domain, sign, 0);
357 scop = pet_scop_filter(scop, prev, 0);
359 return scop;
362 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
363 __isl_keep pet_context *pc, struct pet_state *state);
365 /* Construct a pet_scop for an infinite loop around the given body
366 * within the context "pc".
368 * We extract a pet_scop for the body and then embed it in a loop with
369 * iteration domain
371 * { [t] : t >= 0 }
373 * and schedule
375 * { [t] -> [t] }
377 * If the body contains any break, then it is taken into
378 * account in apply_affine_break (if the skip condition is affine)
379 * or in scop_add_break (if the skip condition is not affine).
381 * Note that in case of an affine skip condition,
382 * since we are dealing with a loop without loop iterator,
383 * the skip condition cannot refer to the current loop iterator and
384 * so effectively, the iteration domain is of the form
386 * { [0]; [t] : t >= 1 and not skip }
388 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
389 __isl_keep pet_context *pc, struct pet_state *state)
391 isl_ctx *ctx;
392 isl_id *id, *id_test;
393 isl_set *domain;
394 isl_set *skip;
395 isl_aff *ident;
396 struct pet_scop *scop;
397 int has_affine_break;
398 int has_var_break;
400 ctx = pet_tree_get_ctx(body);
401 id = isl_id_alloc(ctx, "t", NULL);
402 domain = infinite_domain(isl_id_copy(id));
403 ident = identity_aff(domain);
405 scop = scop_from_tree(body, pc, state);
407 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
408 if (has_affine_break)
409 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
410 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
411 if (has_var_break)
412 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
414 scop = pet_scop_embed(scop, isl_set_copy(domain),
415 isl_aff_copy(ident), ident, id);
416 if (has_affine_break) {
417 domain = apply_affine_break(domain, skip, 1, 0, NULL);
418 scop = pet_scop_intersect_domain_prefix(scop,
419 isl_set_copy(domain));
421 if (has_var_break)
422 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
423 else
424 isl_set_free(domain);
426 return scop;
429 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
431 * for (;;)
432 * body
434 * within the context "pc".
436 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
437 __isl_keep pet_context *pc, struct pet_state *state)
439 struct pet_scop *scop;
441 pc = pet_context_copy(pc);
442 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
444 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
446 pet_context_free(pc);
448 return scop;
451 /* Construct a pet_scop for a while loop of the form
453 * while (pa)
454 * body
456 * within the context "pc".
457 * In particular, construct a scop for an infinite loop around body and
458 * intersect the domain with the affine expression.
459 * Note that this intersection may result in an empty loop.
461 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
462 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
463 struct pet_state *state)
465 struct pet_scop *scop;
466 isl_set *dom;
467 isl_set *valid;
469 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
470 dom = isl_pw_aff_non_zero_set(pa);
471 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
472 scop = pet_scop_restrict(scop, isl_set_params(dom));
473 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
475 pet_context_free(pc);
476 return scop;
479 /* Construct a scop for a while, given the scops for the condition
480 * and the body, the filter identifier and the iteration domain of
481 * the while loop.
483 * In particular, the scop for the condition is filtered to depend
484 * on "id_test" evaluating to true for all previous iterations
485 * of the loop, while the scop for the body is filtered to depend
486 * on "id_test" evaluating to true for all iterations up to the
487 * current iteration.
488 * The actual filter only imposes that this virtual array has
489 * value one on the previous or the current iteration.
490 * The fact that this condition also applies to the previous
491 * iterations is enforced by an implication.
493 * These filtered scops are then combined into a single scop.
495 * "sign" is positive if the iterator increases and negative
496 * if it decreases.
498 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
499 struct pet_scop *scop_body, __isl_take isl_id *id_test,
500 __isl_take isl_set *domain, __isl_take isl_val *inc)
502 isl_ctx *ctx = isl_set_get_ctx(domain);
503 isl_space *space;
504 isl_multi_pw_aff *test_index;
505 isl_multi_pw_aff *prev;
506 int sign = isl_val_sgn(inc);
507 struct pet_scop *scop;
509 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
510 scop_cond = pet_scop_filter(scop_cond, prev, 1);
512 space = isl_space_map_from_set(isl_set_get_space(domain));
513 test_index = isl_multi_pw_aff_identity(space);
514 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
515 isl_id_copy(id_test));
516 scop_body = pet_scop_filter(scop_body, test_index, 1);
518 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
519 scop = add_implication(scop, id_test, domain, sign, 1);
521 return scop;
524 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
525 * evaluating "cond" and writing the result to a virtual scalar,
526 * as expressed by "index".
527 * Do so within the context "pc".
528 * The location of the statement is set to "loc".
530 static struct pet_scop *scop_from_non_affine_condition(
531 __isl_take pet_expr *cond, int stmt_nr,
532 __isl_take isl_multi_pw_aff *index,
533 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
535 pet_expr *expr, *write;
537 write = pet_expr_from_index(index);
538 write = pet_expr_access_set_write(write, 1);
539 write = pet_expr_access_set_read(write, 0);
540 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
542 return scop_from_expr(expr, NULL, stmt_nr, loc, pc);
545 /* Construct a generic while scop, with iteration domain
546 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
547 * The scop consists of two parts,
548 * one for evaluating the condition "cond" and one for the body.
549 * If "expr_inc" is not NULL, then a scop for evaluating this expression
550 * is added at the end of the body,
551 * after replacing any skip conditions resulting from continue statements
552 * by the skip conditions resulting from break statements (if any).
554 * The schedule is adjusted to reflect that the condition is evaluated
555 * before the body is executed and the body is filtered to depend
556 * on the result of the condition evaluating to true on all iterations
557 * up to the current iteration, while the evaluation of the condition itself
558 * is filtered to depend on the result of the condition evaluating to true
559 * on all previous iterations.
560 * The context of the scop representing the body is dropped
561 * because we don't know how many times the body will be executed,
562 * if at all.
564 * If the body contains any break, then it is taken into
565 * account in apply_affine_break (if the skip condition is affine)
566 * or in scop_add_break (if the skip condition is not affine).
568 * Note that in case of an affine skip condition,
569 * since we are dealing with a loop without loop iterator,
570 * the skip condition cannot refer to the current loop iterator and
571 * so effectively, the iteration domain is of the form
573 * { [0]; [t] : t >= 1 and not skip }
575 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
576 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
577 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
578 struct pet_state *state)
580 isl_ctx *ctx;
581 isl_id *id, *id_test, *id_break_test;
582 isl_space *space;
583 isl_multi_pw_aff *test_index;
584 isl_set *domain;
585 isl_set *skip;
586 isl_aff *ident;
587 struct pet_scop *scop, *scop_body;
588 int has_affine_break;
589 int has_var_break;
591 ctx = state->ctx;
592 space = pet_context_get_space(pc);
593 test_index = pet_create_test_index(space, state->n_test++);
594 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
595 isl_multi_pw_aff_copy(test_index),
596 pet_loc_copy(loc), pc);
597 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
598 domain = pet_context_get_domain(pc);
599 scop = pet_scop_add_boolean_array(scop, domain,
600 test_index, state->int_size);
602 id = isl_id_alloc(ctx, "t", NULL);
603 domain = infinite_domain(isl_id_copy(id));
604 ident = identity_aff(domain);
606 scop_body = scop_from_tree(tree_body, pc, state);
608 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
609 if (has_affine_break)
610 skip = pet_scop_get_affine_skip_domain(scop_body,
611 pet_skip_later);
612 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
613 if (has_var_break)
614 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
616 scop = pet_scop_prefix(scop, 0);
617 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(ident),
618 isl_aff_copy(ident), isl_id_copy(id));
619 scop_body = pet_scop_reset_context(scop_body);
620 scop_body = pet_scop_prefix(scop_body, 1);
621 if (expr_inc) {
622 struct pet_scop *scop_inc;
623 scop_inc = scop_from_expr(expr_inc, NULL, state->n_stmt++,
624 loc, pc);
625 scop_inc = pet_scop_prefix(scop_inc, 2);
626 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
627 isl_multi_pw_aff *skip;
628 skip = pet_scop_get_skip(scop_body, pet_skip_later);
629 scop_body = pet_scop_set_skip(scop_body,
630 pet_skip_now, skip);
631 } else
632 pet_scop_reset_skip(scop_body, pet_skip_now);
633 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
634 } else
635 pet_loc_free(loc);
636 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain),
637 isl_aff_copy(ident), ident, id);
639 if (has_affine_break) {
640 domain = apply_affine_break(domain, skip, 1, 0, NULL);
641 scop = pet_scop_intersect_domain_prefix(scop,
642 isl_set_copy(domain));
643 scop_body = pet_scop_intersect_domain_prefix(scop_body,
644 isl_set_copy(domain));
646 if (has_var_break) {
647 scop = scop_add_break(scop, isl_id_copy(id_break_test),
648 isl_set_copy(domain), isl_val_one(ctx));
649 scop_body = scop_add_break(scop_body, id_break_test,
650 isl_set_copy(domain), isl_val_one(ctx));
652 scop = scop_add_while(scop, scop_body, id_test, domain,
653 isl_val_one(ctx));
655 pet_context_free(pc);
656 return scop;
659 /* Check if the while loop is of the form
661 * while (affine expression)
662 * body
664 * If so, call scop_from_affine_while to construct a scop.
666 * Otherwise, pass control to scop_from_non_affine_while.
668 * "pc" is the context in which the affine expressions in the scop are created.
670 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
671 __isl_keep pet_context *pc, struct pet_state *state)
673 pet_expr *cond_expr;
674 isl_pw_aff *pa;
676 if (!tree)
677 return NULL;
679 pc = pet_context_copy(pc);
680 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
682 cond_expr = pet_expr_copy(tree->u.l.cond);
683 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
684 pa = pet_expr_extract_affine_condition(cond_expr, pc);
685 pet_expr_free(cond_expr);
687 if (!pa)
688 goto error;
690 if (!isl_pw_aff_involves_nan(pa))
691 return scop_from_affine_while(tree, pa, pc, state);
692 isl_pw_aff_free(pa);
693 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
694 pet_tree_get_loc(tree), tree->u.l.body, NULL,
695 pc, state);
696 error:
697 pet_context_free(pc);
698 return NULL;
701 /* Check whether "cond" expresses a simple loop bound
702 * on the final set dimension.
703 * In particular, if "up" is set then "cond" should contain only
704 * upper bounds on the final set dimension.
705 * Otherwise, it should contain only lower bounds.
707 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
709 int pos;
711 pos = isl_set_dim(cond, isl_dim_set) - 1;
712 if (isl_val_is_pos(inc))
713 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
714 else
715 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
718 /* Extend a condition on a given iteration of a loop to one that
719 * imposes the same condition on all previous iterations.
720 * "domain" expresses the lower [upper] bound on the iterations
721 * when inc is positive [negative] in its final dimension.
723 * In particular, we construct the condition (when inc is positive)
725 * forall i' : (domain(i') and i' <= i) => cond(i')
727 * (where "<=" applies to the final dimension)
728 * which is equivalent to
730 * not exists i' : domain(i') and i' <= i and not cond(i')
732 * We construct this set by subtracting the satisfying cond from domain,
733 * applying a map
735 * { [i'] -> [i] : i' <= i }
737 * and then subtracting the result from domain again.
739 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
740 __isl_take isl_set *domain, __isl_take isl_val *inc)
742 isl_space *space;
743 isl_map *previous_to_this;
744 int i, dim;
746 dim = isl_set_dim(cond, isl_dim_set);
747 space = isl_space_map_from_set(isl_set_get_space(cond));
748 previous_to_this = isl_map_universe(space);
749 for (i = 0; i + 1 < dim; ++i)
750 previous_to_this = isl_map_equate(previous_to_this,
751 isl_dim_in, i, isl_dim_out, i);
752 if (isl_val_is_pos(inc))
753 previous_to_this = isl_map_order_le(previous_to_this,
754 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
755 else
756 previous_to_this = isl_map_order_ge(previous_to_this,
757 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
759 cond = isl_set_subtract(isl_set_copy(domain), cond);
760 cond = isl_set_apply(cond, previous_to_this);
761 cond = isl_set_subtract(domain, cond);
763 isl_val_free(inc);
765 return cond;
768 /* Construct a domain of the form
770 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
772 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
773 __isl_take isl_pw_aff *init, __isl_take isl_val *inc)
775 isl_aff *aff;
776 isl_space *dim;
777 isl_set *set;
779 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
780 dim = isl_pw_aff_get_domain_space(init);
781 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
782 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, 0, inc);
783 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
785 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
786 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
787 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
788 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
790 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
792 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
794 return isl_set_params(set);
797 /* Assuming "cond" represents a bound on a loop where the loop
798 * iterator "iv" is incremented (or decremented) by one, check if wrapping
799 * is possible.
801 * Under the given assumptions, wrapping is only possible if "cond" allows
802 * for the last value before wrapping, i.e., 2^width - 1 in case of an
803 * increasing iterator and 0 in case of a decreasing iterator.
805 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
806 __isl_keep isl_val *inc)
808 int cw;
809 isl_ctx *ctx;
810 isl_val *limit;
811 isl_set *test;
813 test = isl_set_copy(cond);
815 ctx = isl_set_get_ctx(test);
816 if (isl_val_is_neg(inc))
817 limit = isl_val_zero(ctx);
818 else {
819 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
820 limit = isl_val_2exp(limit);
821 limit = isl_val_sub_ui(limit, 1);
824 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
825 cw = !isl_set_is_empty(test);
826 isl_set_free(test);
828 return cw;
831 /* Given a one-dimensional space, construct the following affine expression
832 * on this space
834 * { [v] -> [v mod 2^width] }
836 * where width is the number of bits used to represent the values
837 * of the unsigned variable "iv".
839 static __isl_give isl_aff *compute_wrapping(__isl_take isl_space *dim,
840 __isl_keep pet_expr *iv)
842 isl_ctx *ctx;
843 isl_val *mod;
844 isl_aff *aff;
846 ctx = isl_space_get_ctx(dim);
847 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
848 mod = isl_val_2exp(mod);
850 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
851 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
852 aff = isl_aff_mod_val(aff, mod);
854 return aff;
857 /* Project out the parameter "id" from "set".
859 static __isl_give isl_set *set_project_out_by_id(__isl_take isl_set *set,
860 __isl_keep isl_id *id)
862 int pos;
864 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
865 if (pos >= 0)
866 set = isl_set_project_out(set, isl_dim_param, pos, 1);
868 return set;
871 /* Compute the set of parameters for which "set1" is a subset of "set2".
873 * set1 is a subset of set2 if
875 * forall i in set1 : i in set2
877 * or
879 * not exists i in set1 and i not in set2
881 * i.e.,
883 * not exists i in set1 \ set2
885 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
886 __isl_take isl_set *set2)
888 return isl_set_complement(isl_set_params(isl_set_subtract(set1, set2)));
891 /* Compute the set of parameter values for which "cond" holds
892 * on the next iteration for each element of "dom".
894 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
895 * and then compute the set of parameters for which the result is a subset
896 * of "cond".
898 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
899 __isl_take isl_set *dom, __isl_take isl_val *inc)
901 isl_space *space;
902 isl_aff *aff;
903 isl_map *next;
905 space = isl_set_get_space(dom);
906 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
907 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
908 aff = isl_aff_add_constant_val(aff, inc);
909 next = isl_map_from_basic_map(isl_basic_map_from_aff(aff));
911 dom = isl_set_apply(dom, next);
913 return enforce_subset(dom, cond);
916 /* Extract the for loop "tree" as a while loop within the context "pc".
918 * That is, the for loop has the form
920 * for (iv = init; cond; iv += inc)
921 * body;
923 * and is treated as
925 * iv = init;
926 * while (cond) {
927 * body;
928 * iv += inc;
931 * except that the skips resulting from any continue statements
932 * in body do not apply to the increment, but are replaced by the skips
933 * resulting from break statements.
935 * If the loop iterator is declared in the for loop, then it is killed before
936 * and after the loop.
938 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
939 __isl_take pet_context *pc, struct pet_state *state)
941 int declared;
942 isl_id *iv;
943 pet_expr *expr_iv, *init, *inc;
944 struct pet_scop *scop_init, *scop;
945 int type_size;
946 struct pet_array *array;
947 struct pet_scop *scop_kill;
949 iv = pet_expr_access_get_id(tree->u.l.iv);
950 pc = pet_context_mark_assigned(pc, iv);
952 declared = tree->u.l.declared;
954 expr_iv = pet_expr_copy(tree->u.l.iv);
955 type_size = pet_expr_get_type_size(expr_iv);
956 init = pet_expr_copy(tree->u.l.init);
957 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
958 scop_init = scop_from_expr(init, NULL, state->n_stmt++,
959 pet_tree_get_loc(tree), pc);
960 scop_init = pet_scop_prefix(scop_init, declared);
962 expr_iv = pet_expr_copy(tree->u.l.iv);
963 type_size = pet_expr_get_type_size(expr_iv);
964 inc = pet_expr_copy(tree->u.l.inc);
965 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
967 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
968 pet_tree_get_loc(tree), tree->u.l.body, inc,
969 pet_context_copy(pc), state);
971 scop = pet_scop_prefix(scop, declared + 1);
972 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
974 if (!declared) {
975 pet_context_free(pc);
976 return scop;
979 array = extract_array(tree->u.l.iv, pc, state);
980 if (array)
981 array->declared = 1;
982 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
983 scop_kill = pet_scop_prefix(scop_kill, 0);
984 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
985 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
986 scop_kill = pet_scop_add_array(scop_kill, array);
987 scop_kill = pet_scop_prefix(scop_kill, 3);
988 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
990 pet_context_free(pc);
991 return scop;
994 /* Given an access expression "expr", is the variable accessed by
995 * "expr" assigned anywhere inside "tree"?
997 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
999 int assigned = 0;
1000 isl_id *id;
1002 id = pet_expr_access_get_id(expr);
1003 assigned = pet_tree_writes(tree, id);
1004 isl_id_free(id);
1006 return assigned;
1009 /* Are all nested access parameters in "pa" allowed given "tree".
1010 * In particular, is none of them written by anywhere inside "tree".
1012 * If "tree" has any continue nodes in the current loop level,
1013 * then no nested access parameters are allowed.
1014 * In particular, if there is any nested access in a guard
1015 * for a piece of code containing a "continue", then we want to introduce
1016 * a separate statement for evaluating this guard so that we can express
1017 * that the result is false for all previous iterations.
1019 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1020 __isl_keep pet_tree *tree)
1022 int i, nparam;
1024 if (!tree)
1025 return -1;
1027 if (!pet_nested_any_in_pw_aff(pa))
1028 return 1;
1030 if (pet_tree_has_continue(tree))
1031 return 0;
1033 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1034 for (i = 0; i < nparam; ++i) {
1035 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1036 pet_expr *expr;
1037 int allowed;
1039 if (!pet_nested_in_id(id)) {
1040 isl_id_free(id);
1041 continue;
1044 expr = pet_nested_extract_expr(id);
1045 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1046 !is_assigned(expr, tree);
1048 pet_expr_free(expr);
1049 isl_id_free(id);
1051 if (!allowed)
1052 return 0;
1055 return 1;
1058 /* Construct a pet_scop for a for tree with static affine initialization
1059 * and constant increment within the context "pc".
1061 * The condition is allowed to contain nested accesses, provided
1062 * they are not being written to inside the body of the loop.
1063 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1064 * essentially treated as a while loop, with iteration domain
1065 * { [i] : i >= init }.
1067 * We extract a pet_scop for the body and then embed it in a loop with
1068 * iteration domain and schedule
1070 * { [i] : i >= init and condition' }
1071 * { [i] -> [i] }
1073 * or
1075 * { [i] : i <= init and condition' }
1076 * { [i] -> [-i] }
1078 * Where condition' is equal to condition if the latter is
1079 * a simple upper [lower] bound and a condition that is extended
1080 * to apply to all previous iterations otherwise.
1082 * If the condition is non-affine, then we drop the condition from the
1083 * iteration domain and instead create a separate statement
1084 * for evaluating the condition. The body is then filtered to depend
1085 * on the result of the condition evaluating to true on all iterations
1086 * up to the current iteration, while the evaluation the condition itself
1087 * is filtered to depend on the result of the condition evaluating to true
1088 * on all previous iterations.
1089 * The context of the scop representing the body is dropped
1090 * because we don't know how many times the body will be executed,
1091 * if at all.
1093 * If the stride of the loop is not 1, then "i >= init" is replaced by
1095 * (exists a: i = init + stride * a and a >= 0)
1097 * If the loop iterator i is unsigned, then wrapping may occur.
1098 * We therefore use a virtual iterator instead that does not wrap.
1099 * However, the condition in the code applies
1100 * to the wrapped value, so we need to change condition(i)
1101 * into condition([i % 2^width]). Similarly, we replace all accesses
1102 * to the original iterator by the wrapping of the virtual iterator.
1103 * Note that there may be no need to perform this final wrapping
1104 * if the loop condition (after wrapping) satisfies certain conditions.
1105 * However, the is_simple_bound condition is not enough since it doesn't
1106 * check if there even is an upper bound.
1108 * Wrapping on unsigned iterators can be avoided entirely if
1109 * loop condition is simple, the loop iterator is incremented
1110 * [decremented] by one and the last value before wrapping cannot
1111 * possibly satisfy the loop condition.
1113 * Valid parameters for a for loop are those for which the initial
1114 * value itself, the increment on each domain iteration and
1115 * the condition on both the initial value and
1116 * the result of incrementing the iterator for each iteration of the domain
1117 * can be evaluated.
1118 * If the loop condition is non-affine, then we only consider validity
1119 * of the initial value.
1121 * If the body contains any break, then we keep track of it in "skip"
1122 * (if the skip condition is affine) or it is handled in scop_add_break
1123 * (if the skip condition is not affine).
1124 * Note that the affine break condition needs to be considered with
1125 * respect to previous iterations in the virtual domain (if any).
1127 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1128 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1129 __isl_take isl_val *inc, __isl_take pet_context *pc,
1130 struct pet_state *state)
1132 isl_local_space *ls;
1133 isl_set *domain;
1134 isl_aff *sched;
1135 isl_set *cond = NULL;
1136 isl_set *skip = NULL;
1137 isl_id *id, *id_test = NULL, *id_break_test;
1138 struct pet_scop *scop, *scop_cond = NULL;
1139 int is_one;
1140 int is_unsigned;
1141 int is_simple;
1142 int is_virtual;
1143 int is_non_affine;
1144 int has_affine_break;
1145 int has_var_break;
1146 isl_map *rev_wrap = NULL;
1147 isl_aff *wrap = NULL;
1148 isl_pw_aff *pa;
1149 isl_set *valid_init;
1150 isl_set *valid_cond;
1151 isl_set *valid_cond_init;
1152 isl_set *valid_cond_next;
1153 isl_set *valid_inc;
1154 pet_expr *cond_expr;
1155 pet_context *pc_nested;
1157 id = pet_expr_access_get_id(tree->u.l.iv);
1159 cond_expr = pet_expr_copy(tree->u.l.cond);
1160 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1161 pc_nested = pet_context_copy(pc);
1162 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1163 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1164 pet_context_free(pc_nested);
1165 pet_expr_free(cond_expr);
1167 valid_inc = isl_pw_aff_domain(pa_inc);
1169 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1171 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1172 !is_nested_allowed(pa, tree->u.l.body);
1173 if (is_non_affine)
1174 pa = isl_pw_aff_free(pa);
1176 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1177 cond = isl_pw_aff_non_zero_set(pa);
1178 if (is_non_affine)
1179 cond = isl_set_universe(isl_space_set_alloc(state->ctx, 0, 0));
1181 cond = embed(cond, isl_id_copy(id));
1182 valid_cond = isl_set_coalesce(valid_cond);
1183 valid_cond = embed(valid_cond, isl_id_copy(id));
1184 valid_inc = embed(valid_inc, isl_id_copy(id));
1185 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1186 is_virtual = is_unsigned &&
1187 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1189 valid_cond_init = enforce_subset(
1190 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val))),
1191 isl_set_copy(valid_cond));
1192 if (is_one && !is_virtual) {
1193 isl_pw_aff_free(init_val);
1194 pa = pet_expr_extract_comparison(
1195 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1196 tree->u.l.iv, tree->u.l.init, pc);
1197 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1198 valid_init = set_project_out_by_id(valid_init, id);
1199 domain = isl_pw_aff_non_zero_set(pa);
1200 } else {
1201 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1202 domain = strided_domain(isl_id_copy(id), init_val,
1203 isl_val_copy(inc));
1206 domain = embed(domain, isl_id_copy(id));
1207 if (is_virtual) {
1208 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1209 rev_wrap = isl_map_from_aff(isl_aff_copy(wrap));
1210 rev_wrap = isl_map_reverse(rev_wrap);
1211 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1212 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1213 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1215 is_simple = is_simple_bound(cond, inc);
1216 if (!is_simple) {
1217 cond = isl_set_gist(cond, isl_set_copy(domain));
1218 is_simple = is_simple_bound(cond, inc);
1220 if (!is_simple)
1221 cond = valid_for_each_iteration(cond,
1222 isl_set_copy(domain), isl_val_copy(inc));
1223 domain = isl_set_intersect(domain, cond);
1224 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1225 ls = isl_local_space_from_space(isl_set_get_space(domain));
1226 sched = isl_aff_var_on_domain(ls, isl_dim_set, 0);
1227 if (isl_val_is_neg(inc))
1228 sched = isl_aff_neg(sched);
1230 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1231 isl_val_copy(inc));
1232 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1234 if (!is_virtual)
1235 wrap = identity_aff(domain);
1237 if (is_non_affine) {
1238 isl_space *space;
1239 isl_multi_pw_aff *test_index;
1240 space = pet_context_get_space(pc);
1241 test_index = pet_create_test_index(space, state->n_test++);
1242 scop_cond = scop_from_non_affine_condition(
1243 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1244 isl_multi_pw_aff_copy(test_index),
1245 pet_tree_get_loc(tree), pc);
1246 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1247 isl_dim_out);
1248 scop_cond = pet_scop_add_boolean_array(scop_cond,
1249 pet_context_get_domain(pc), test_index,
1250 state->int_size);
1251 scop_cond = pet_scop_prefix(scop_cond, 0);
1252 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1253 isl_aff_copy(sched), isl_aff_copy(wrap),
1254 isl_id_copy(id));
1257 scop = scop_from_tree(tree->u.l.body, pc, state);
1258 has_affine_break = scop &&
1259 pet_scop_has_affine_skip(scop, pet_skip_later);
1260 if (has_affine_break)
1261 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1262 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1263 if (has_var_break)
1264 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1265 if (is_non_affine) {
1266 scop = pet_scop_reset_context(scop);
1267 scop = pet_scop_prefix(scop, 1);
1269 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
1270 scop = pet_scop_resolve_nested(scop);
1271 if (has_affine_break) {
1272 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1273 is_virtual, rev_wrap);
1274 scop = pet_scop_intersect_domain_prefix(scop,
1275 isl_set_copy(domain));
1277 isl_map_free(rev_wrap);
1278 if (has_var_break)
1279 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1280 isl_val_copy(inc));
1281 if (is_non_affine) {
1282 scop = scop_add_while(scop_cond, scop, id_test, domain,
1283 isl_val_copy(inc));
1284 isl_set_free(valid_inc);
1285 } else {
1286 scop = pet_scop_restrict_context(scop, valid_inc);
1287 scop = pet_scop_restrict_context(scop, valid_cond_next);
1288 scop = pet_scop_restrict_context(scop, valid_cond_init);
1289 isl_set_free(domain);
1292 isl_val_free(inc);
1294 scop = pet_scop_restrict_context(scop, isl_set_params(valid_init));
1296 pet_context_free(pc);
1297 return scop;
1300 /* Construct a pet_scop for a for statement within the context of "pc".
1302 * We update the context to reflect the writes to the loop variable and
1303 * the writes inside the body.
1305 * Then we check if the initialization of the for loop
1306 * is a static affine value and the increment is a constant.
1307 * If so, we construct the pet_scop using scop_from_affine_for.
1308 * Otherwise, we treat the for loop as a while loop
1309 * in scop_from_non_affine_for.
1311 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1312 __isl_keep pet_context *pc, struct pet_state *state)
1314 isl_id *iv;
1315 isl_val *inc;
1316 isl_pw_aff *pa_inc, *init_val;
1317 pet_context *pc_init_val;
1319 if (!tree)
1320 return NULL;
1322 iv = pet_expr_access_get_id(tree->u.l.iv);
1323 pc = pet_context_copy(pc);
1324 pc = pet_context_clear_value(pc, iv);
1325 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1327 pc_init_val = pet_context_copy(pc);
1328 pc_init_val = pet_context_mark_unknown(pc_init_val, isl_id_copy(iv));
1329 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1330 pet_context_free(pc_init_val);
1331 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1332 inc = pet_extract_cst(pa_inc);
1333 if (!pa_inc || !init_val || !inc)
1334 goto error;
1335 if (!isl_pw_aff_involves_nan(pa_inc) &&
1336 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1337 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1338 pc, state);
1340 isl_pw_aff_free(pa_inc);
1341 isl_pw_aff_free(init_val);
1342 isl_val_free(inc);
1343 return scop_from_non_affine_for(tree, pc, state);
1344 error:
1345 isl_pw_aff_free(pa_inc);
1346 isl_pw_aff_free(init_val);
1347 isl_val_free(inc);
1348 pet_context_free(pc);
1349 return NULL;
1352 /* Check whether "expr" is an affine constraint within the context "pc".
1354 static int is_affine_condition(__isl_keep pet_expr *expr,
1355 __isl_keep pet_context *pc)
1357 isl_pw_aff *pa;
1358 int is_affine;
1360 pa = pet_expr_extract_affine_condition(expr, pc);
1361 if (!pa)
1362 return -1;
1363 is_affine = !isl_pw_aff_involves_nan(pa);
1364 isl_pw_aff_free(pa);
1366 return is_affine;
1369 /* Check if the given if statement is a conditional assignement
1370 * with a non-affine condition.
1372 * In particular we check if "stmt" is of the form
1374 * if (condition)
1375 * a = f(...);
1376 * else
1377 * a = g(...);
1379 * where the condition is non-affine and a is some array or scalar access.
1381 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1382 __isl_keep pet_context *pc)
1384 int equal;
1385 isl_ctx *ctx;
1386 pet_expr *expr1, *expr2;
1388 ctx = pet_tree_get_ctx(tree);
1389 if (!pet_options_get_detect_conditional_assignment(ctx))
1390 return 0;
1391 if (tree->type != pet_tree_if_else)
1392 return 0;
1393 if (tree->u.i.then_body->type != pet_tree_expr)
1394 return 0;
1395 if (tree->u.i.else_body->type != pet_tree_expr)
1396 return 0;
1397 expr1 = tree->u.i.then_body->u.e.expr;
1398 expr2 = tree->u.i.else_body->u.e.expr;
1399 if (pet_expr_get_type(expr1) != pet_expr_op)
1400 return 0;
1401 if (pet_expr_get_type(expr2) != pet_expr_op)
1402 return 0;
1403 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1404 return 0;
1405 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1406 return 0;
1407 expr1 = pet_expr_get_arg(expr1, 0);
1408 expr2 = pet_expr_get_arg(expr2, 0);
1409 equal = pet_expr_is_equal(expr1, expr2);
1410 pet_expr_free(expr1);
1411 pet_expr_free(expr2);
1412 if (equal < 0 || !equal)
1413 return 0;
1414 if (is_affine_condition(tree->u.i.cond, pc))
1415 return 0;
1417 return 1;
1420 /* Given that "tree" is of the form
1422 * if (condition)
1423 * a = f(...);
1424 * else
1425 * a = g(...);
1427 * where a is some array or scalar access, construct a pet_scop
1428 * corresponding to this conditional assignment within the context "pc".
1430 * The constructed pet_scop then corresponds to the expression
1432 * a = condition ? f(...) : g(...)
1434 * All access relations in f(...) are intersected with condition
1435 * while all access relation in g(...) are intersected with the complement.
1437 static struct pet_scop *scop_from_conditional_assignment(
1438 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1439 struct pet_state *state)
1441 int type_size;
1442 isl_pw_aff *pa;
1443 isl_set *cond, *comp;
1444 isl_multi_pw_aff *index;
1445 pet_expr *expr1, *expr2;
1446 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1447 pet_context *pc_nested;
1448 struct pet_scop *scop;
1450 pe_cond = pet_expr_copy(tree->u.i.cond);
1451 pe_cond = pet_expr_plug_in_args(pe_cond, pc);
1452 pc_nested = pet_context_copy(pc);
1453 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1454 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1455 pet_context_free(pc_nested);
1456 pet_expr_free(pe_cond);
1457 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1458 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1459 index = isl_multi_pw_aff_from_pw_aff(pa);
1461 expr1 = tree->u.i.then_body->u.e.expr;
1462 expr2 = tree->u.i.else_body->u.e.expr;
1464 pe_cond = pet_expr_from_index(index);
1466 pe_then = pet_expr_get_arg(expr1, 1);
1467 pe_then = pet_expr_restrict(pe_then, cond);
1468 pe_else = pet_expr_get_arg(expr2, 1);
1469 pe_else = pet_expr_restrict(pe_else, comp);
1470 pe_write = pet_expr_get_arg(expr1, 0);
1472 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1473 type_size = pet_expr_get_type_size(pe_write);
1474 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1476 scop = scop_from_expr(pe, NULL, state->n_stmt++,
1477 pet_tree_get_loc(tree), pc);
1479 pet_context_free(pc);
1481 return scop;
1484 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1486 * We create a separate statement that writes the result
1487 * of the non-affine condition to a virtual scalar.
1488 * A constraint requiring the value of this virtual scalar to be one
1489 * is added to the iteration domains of the then branch.
1490 * Similarly, a constraint requiring the value of this virtual scalar
1491 * to be zero is added to the iteration domains of the else branch, if any.
1492 * We adjust the schedules to ensure that the virtual scalar is written
1493 * before it is read.
1495 * If there are any breaks or continues in the then and/or else
1496 * branches, then we may have to compute a new skip condition.
1497 * This is handled using a pet_skip_info object.
1498 * On initialization, the object checks if skip conditions need
1499 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1500 * adds them in pet_skip_info_if_add.
1502 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1503 __isl_take pet_context *pc, struct pet_state *state)
1505 int has_else;
1506 isl_space *space;
1507 isl_set *domain;
1508 isl_multi_pw_aff *test_index;
1509 struct pet_skip_info skip;
1510 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1512 has_else = tree->type == pet_tree_if_else;
1514 space = pet_context_get_space(pc);
1515 test_index = pet_create_test_index(space, state->n_test++);
1516 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1517 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1518 pet_tree_get_loc(tree), pc);
1519 domain = pet_context_get_domain(pc);
1520 scop = pet_scop_add_boolean_array(scop, domain,
1521 isl_multi_pw_aff_copy(test_index), state->int_size);
1523 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1524 if (has_else)
1525 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1527 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1528 has_else, 0);
1529 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1531 scop = pet_scop_prefix(scop, 0);
1532 scop_then = pet_scop_prefix(scop_then, 1);
1533 scop_then = pet_scop_filter(scop_then,
1534 isl_multi_pw_aff_copy(test_index), 1);
1535 if (has_else) {
1536 scop_else = pet_scop_prefix(scop_else, 1);
1537 scop_else = pet_scop_filter(scop_else, test_index, 0);
1538 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1539 } else
1540 isl_multi_pw_aff_free(test_index);
1542 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1544 scop = pet_skip_info_if_add(&skip, scop, 2);
1546 pet_context_free(pc);
1547 return scop;
1550 /* Construct a pet_scop for an affine if statement within the context "pc".
1552 * The condition is added to the iteration domains of the then branch,
1553 * while the opposite of the condition in added to the iteration domains
1554 * of the else branch, if any.
1556 * If there are any breaks or continues in the then and/or else
1557 * branches, then we may have to compute a new skip condition.
1558 * This is handled using a pet_skip_info_if object.
1559 * On initialization, the object checks if skip conditions need
1560 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1561 * adds them in pet_skip_info_if_add.
1563 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1564 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1565 struct pet_state *state)
1567 int has_else;
1568 isl_ctx *ctx;
1569 isl_set *set;
1570 isl_set *valid;
1571 struct pet_skip_info skip;
1572 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1574 ctx = pet_tree_get_ctx(tree);
1576 has_else = tree->type == pet_tree_if_else;
1578 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1579 if (has_else)
1580 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1582 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1583 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
1585 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1586 set = isl_pw_aff_non_zero_set(cond);
1587 scop = pet_scop_restrict(scop_then, isl_set_params(isl_set_copy(set)));
1589 if (has_else) {
1590 set = isl_set_subtract(isl_set_copy(valid), set);
1591 scop_else = pet_scop_restrict(scop_else, isl_set_params(set));
1592 scop = pet_scop_add_par(ctx, scop, scop_else);
1593 } else
1594 isl_set_free(set);
1595 scop = pet_scop_resolve_nested(scop);
1596 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
1598 if (pet_skip_info_has_skip(&skip))
1599 scop = pet_scop_prefix(scop, 0);
1600 scop = pet_skip_info_if_add(&skip, scop, 1);
1602 pet_context_free(pc);
1603 return scop;
1606 /* Construct a pet_scop for an if statement within the context "pc".
1608 * If the condition fits the pattern of a conditional assignment,
1609 * then it is handled by scop_from_conditional_assignment.
1611 * Otherwise, we check if the condition is affine.
1612 * If so, we construct the scop in scop_from_affine_if.
1613 * Otherwise, we construct the scop in scop_from_non_affine_if.
1615 * We allow the condition to be dynamic, i.e., to refer to
1616 * scalars or array elements that may be written to outside
1617 * of the given if statement. These nested accesses are then represented
1618 * as output dimensions in the wrapping iteration domain.
1619 * If it is also written _inside_ the then or else branch, then
1620 * we treat the condition as non-affine.
1621 * As explained in extract_non_affine_if, this will introduce
1622 * an extra statement.
1623 * For aesthetic reasons, we want this statement to have a statement
1624 * number that is lower than those of the then and else branches.
1625 * In order to evaluate if we will need such a statement, however, we
1626 * first construct scops for the then and else branches.
1627 * We therefore reserve a statement number if we might have to
1628 * introduce such an extra statement.
1630 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1631 __isl_keep pet_context *pc, struct pet_state *state)
1633 int has_else;
1634 isl_pw_aff *cond;
1635 pet_expr *cond_expr;
1636 pet_context *pc_nested;
1638 if (!tree)
1639 return NULL;
1641 has_else = tree->type == pet_tree_if_else;
1643 pc = pet_context_copy(pc);
1644 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1645 if (has_else)
1646 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1648 if (is_conditional_assignment(tree, pc))
1649 return scop_from_conditional_assignment(tree, pc, state);
1651 cond_expr = pet_expr_copy(tree->u.i.cond);
1652 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1653 pc_nested = pet_context_copy(pc);
1654 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1655 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1656 pet_context_free(pc_nested);
1657 pet_expr_free(cond_expr);
1659 if (!cond) {
1660 pet_context_free(pc);
1661 return NULL;
1664 if (isl_pw_aff_involves_nan(cond)) {
1665 isl_pw_aff_free(cond);
1666 return scop_from_non_affine_if(tree, pc, state);
1669 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1670 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1671 isl_pw_aff_free(cond);
1672 return scop_from_non_affine_if(tree, pc, state);
1675 return scop_from_affine_if(tree, cond, pc, state);
1678 /* Return a one-dimensional multi piecewise affine expression that is equal
1679 * to the constant 1 and is defined over the given domain.
1681 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
1683 isl_local_space *ls;
1684 isl_aff *aff;
1686 ls = isl_local_space_from_space(space);
1687 aff = isl_aff_zero_on_domain(ls);
1688 aff = isl_aff_set_constant_si(aff, 1);
1690 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1693 /* Construct a pet_scop for a continue statement with the given domain space.
1695 * We simply create an empty scop with a universal pet_skip_now
1696 * skip condition. This skip condition will then be taken into
1697 * account by the enclosing loop construct, possibly after
1698 * being incorporated into outer skip conditions.
1700 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
1701 __isl_take isl_space *space)
1703 struct pet_scop *scop;
1705 scop = pet_scop_empty(isl_space_copy(space));
1707 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
1709 return scop;
1712 /* Construct a pet_scop for a break statement with the given domain space.
1714 * We simply create an empty scop with both a universal pet_skip_now
1715 * skip condition and a universal pet_skip_later skip condition.
1716 * These skip conditions will then be taken into
1717 * account by the enclosing loop construct, possibly after
1718 * being incorporated into outer skip conditions.
1720 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
1721 __isl_take isl_space *space)
1723 struct pet_scop *scop;
1724 isl_multi_pw_aff *skip;
1726 scop = pet_scop_empty(isl_space_copy(space));
1728 skip = one_mpa(space);
1729 scop = pet_scop_set_skip(scop, pet_skip_now,
1730 isl_multi_pw_aff_copy(skip));
1731 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1733 return scop;
1736 /* Extract a clone of the kill statement in "scop".
1737 * The domain of the clone is given by "domain".
1738 * "scop" is expected to have been created from a DeclStmt
1739 * and should have the kill as its first statement.
1741 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
1742 struct pet_scop *scop, struct pet_state *state)
1744 pet_expr *kill;
1745 struct pet_stmt *stmt;
1746 isl_multi_pw_aff *index;
1747 isl_map *access;
1748 pet_expr *arg;
1750 if (!domain || !scop)
1751 return NULL;
1752 if (scop->n_stmt < 1)
1753 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1754 "expecting at least one statement", return NULL);
1755 stmt = scop->stmts[0];
1756 if (!pet_stmt_is_kill(stmt))
1757 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1758 "expecting kill statement", return NULL);
1760 arg = pet_expr_get_arg(stmt->body, 0);
1761 index = pet_expr_access_get_index(arg);
1762 access = pet_expr_access_get_access(arg);
1763 pet_expr_free(arg);
1764 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
1765 access = isl_map_reset_tuple_id(access, isl_dim_in);
1766 kill = pet_expr_kill_from_access_and_index(access, index);
1767 stmt = pet_stmt_from_pet_expr(isl_set_copy(domain),
1768 pet_loc_copy(stmt->loc), NULL, state->n_stmt++, kill);
1769 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
1772 /* Does "tree" represent an assignment to a variable?
1774 * The assignment may be one of
1775 * - a declaration with initialization
1776 * - an expression with a top-level assignment operator
1778 static int is_assignment(__isl_keep pet_tree *tree)
1780 if (!tree)
1781 return 0;
1782 if (tree->type == pet_tree_decl_init)
1783 return 1;
1784 return pet_tree_is_assign(tree);
1787 /* Update "pc" by taking into account the assignment performed by "tree",
1788 * where "tree" satisfies is_assignment.
1790 * In particular, if the lhs of the assignment is a scalar variable and
1791 * if the rhs is an affine expression, then keep track of this value in "pc"
1792 * so that we can plug it in when we later come across the same variable.
1794 * The variable has already been marked as having been assigned
1795 * an unknown value by scop_handle_writes.
1797 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
1798 __isl_keep pet_tree *tree)
1800 pet_expr *var, *val;
1801 isl_id *id;
1802 isl_pw_aff *pa;
1804 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
1805 var = pet_tree_decl_get_var(tree);
1806 val = pet_tree_decl_get_init(tree);
1807 } else {
1808 pet_expr *expr;
1809 expr = pet_tree_expr_get_expr(tree);
1810 var = pet_expr_get_arg(expr, 0);
1811 val = pet_expr_get_arg(expr, 1);
1812 pet_expr_free(expr);
1815 if (!pet_expr_is_scalar_access(var)) {
1816 pet_expr_free(var);
1817 pet_expr_free(val);
1818 return pc;
1821 pa = pet_expr_extract_affine(val, pc);
1822 if (!pa)
1823 pc = pet_context_free(pc);
1825 if (!isl_pw_aff_involves_nan(pa)) {
1826 id = pet_expr_access_get_id(var);
1827 pc = pet_context_set_value(pc, id, pa);
1828 } else {
1829 isl_pw_aff_free(pa);
1831 pet_expr_free(var);
1832 pet_expr_free(val);
1834 return pc;
1837 /* Mark all arrays in "scop" as being exposed.
1839 static struct pet_scop *mark_exposed(struct pet_scop *scop)
1841 int i;
1843 if (!scop)
1844 return NULL;
1845 for (i = 0; i < scop->n_array; ++i)
1846 scop->arrays[i]->exposed = 1;
1847 return scop;
1850 /* Try and construct a pet_scop corresponding to (part of)
1851 * a sequence of statements within the context "pc".
1853 * After extracting a statement, we update "pc"
1854 * based on the top-level assignments in the statement
1855 * so that we can exploit them in subsequent statements in the same block.
1857 * If there are any breaks or continues in the individual statements,
1858 * then we may have to compute a new skip condition.
1859 * This is handled using a pet_skip_info object.
1860 * On initialization, the object checks if skip conditions need
1861 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1862 * adds them in pet_skip_info_seq_add.
1864 * If "block" is set, then we need to insert kill statements at
1865 * the end of the block for any array that has been declared by
1866 * one of the statements in the sequence. Each of these declarations
1867 * results in the construction of a kill statement at the place
1868 * of the declaration, so we simply collect duplicates of
1869 * those kill statements and append these duplicates to the constructed scop.
1871 * If "block" is not set, then any array declared by one of the statements
1872 * in the sequence is marked as being exposed.
1874 * If autodetect is set, then we allow the extraction of only a subrange
1875 * of the sequence of statements. However, if there is at least one statement
1876 * for which we could not construct a scop and the final range contains
1877 * either no statements or at least one kill, then we discard the entire
1878 * range.
1880 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
1881 __isl_keep pet_context *pc, struct pet_state *state)
1883 int i;
1884 isl_ctx *ctx;
1885 isl_space *space;
1886 isl_set *domain;
1887 struct pet_scop *scop, *kills;
1889 ctx = pet_tree_get_ctx(tree);
1891 space = pet_context_get_space(pc);
1892 domain = pet_context_get_domain(pc);
1893 pc = pet_context_copy(pc);
1894 scop = pet_scop_empty(isl_space_copy(space));
1895 kills = pet_scop_empty(space);
1896 for (i = 0; i < tree->u.b.n; ++i) {
1897 struct pet_scop *scop_i;
1899 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
1900 pc = scop_handle_writes(scop_i, pc);
1901 if (is_assignment(tree->u.b.child[i]))
1902 pc = handle_assignment(pc, tree->u.b.child[i]);
1903 struct pet_skip_info skip;
1904 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
1905 pet_skip_info_seq_extract(&skip, pc, state);
1906 if (pet_skip_info_has_skip(&skip))
1907 scop_i = pet_scop_prefix(scop_i, 0);
1908 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
1909 if (tree->u.b.block) {
1910 struct pet_scop *kill;
1911 kill = extract_kill(domain, scop_i, state);
1912 kills = pet_scop_add_par(ctx, kills, kill);
1913 } else
1914 scop_i = mark_exposed(scop_i);
1916 scop_i = pet_scop_prefix(scop_i, i);
1917 scop = pet_scop_add_seq(ctx, scop, scop_i);
1919 scop = pet_skip_info_seq_add(&skip, scop, i);
1921 if (!scop)
1922 break;
1924 isl_set_free(domain);
1926 kills = pet_scop_prefix(kills, tree->u.b.n);
1927 scop = pet_scop_add_seq(ctx, scop, kills);
1929 pet_context_free(pc);
1931 return scop;
1934 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1935 * within the context "pc" by calling the appropriate function
1936 * based on the type of "tree".
1938 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
1939 __isl_keep pet_context *pc, struct pet_state *state)
1941 if (!tree)
1942 return NULL;
1944 switch (tree->type) {
1945 case pet_tree_error:
1946 return NULL;
1947 case pet_tree_block:
1948 return scop_from_block(tree, pc, state);
1949 case pet_tree_break:
1950 return scop_from_break(tree, pet_context_get_space(pc));
1951 case pet_tree_continue:
1952 return scop_from_continue(tree, pet_context_get_space(pc));
1953 case pet_tree_decl:
1954 case pet_tree_decl_init:
1955 return scop_from_decl(tree, pc, state);
1956 case pet_tree_expr:
1957 return scop_from_expr(pet_expr_copy(tree->u.e.expr),
1958 isl_id_copy(tree->label),
1959 state->n_stmt++,
1960 pet_tree_get_loc(tree), pc);
1961 case pet_tree_if:
1962 case pet_tree_if_else:
1963 return scop_from_if(tree, pc, state);
1964 case pet_tree_for:
1965 return scop_from_for(tree, pc, state);
1966 case pet_tree_while:
1967 return scop_from_while(tree, pc, state);
1968 case pet_tree_infinite_loop:
1969 return scop_from_infinite_for(tree, pc, state);
1972 isl_die(tree->ctx, isl_error_internal, "unhandled type",
1973 return NULL);
1976 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1977 * "int_size" is the number of bytes need to represent an integer.
1978 * "extract_array" is a callback that we can use to create a pet_array
1979 * that corresponds to the variable accessed by an expression.
1981 * Initialize the global state, construct a context and then
1982 * construct the pet_scop by recursively visiting the tree.
1984 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
1985 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
1986 __isl_keep pet_context *pc, void *user), void *user,
1987 __isl_keep pet_context *pc)
1989 struct pet_scop *scop;
1990 struct pet_state state = { 0 };
1992 if (!tree)
1993 return NULL;
1995 state.ctx = pet_tree_get_ctx(tree);
1996 state.int_size = int_size;
1997 state.extract_array = extract_array;
1998 state.user = user;
2000 scop = scop_from_tree(tree, pc, &state);
2001 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2003 pet_tree_free(tree);
2005 return scop;