pet_expr_resolve_nested: allow specification of domain space
[pet.git] / tree2scop.c
bloba022f98b7b26d445fe89a9a8b4541514a87113ba
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".
203 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
205 isl_map *previous_to_this;
207 if (sign > 0)
208 previous_to_this = isl_map_lex_lt(isl_set_get_space(cond));
209 else
210 previous_to_this = isl_map_lex_gt(isl_set_get_space(cond));
212 cond = isl_set_apply(cond, previous_to_this);
214 return cond;
217 /* Remove those iterations of "domain" that have an earlier iteration
218 * (based on "sign") where "skip" is satisfied.
219 * "domain" has an extra outer loop compared to "skip".
220 * The skip condition is first embedded in the same space as "domain".
221 * If "apply_skip_map" is set, then "skip_map" is first applied
222 * to the embedded skip condition before removing it from the domain.
224 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
225 __isl_take isl_set *skip, int sign,
226 int apply_skip_map, __isl_keep isl_map *skip_map)
228 skip = embed(skip, isl_set_get_dim_id(domain, isl_dim_set, 0));
229 if (apply_skip_map)
230 skip = isl_set_apply(skip, isl_map_copy(skip_map));
231 skip = isl_set_intersect(skip , isl_set_copy(domain));
232 return isl_set_subtract(domain, after(skip, sign));
235 /* Create the infinite iteration domain
237 * { [id] : id >= 0 }
239 static __isl_give isl_set *infinite_domain(__isl_take isl_id *id)
241 isl_ctx *ctx = isl_id_get_ctx(id);
242 isl_set *domain;
244 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
245 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, id);
247 return domain;
250 /* Create an identity affine expression on the space containing "domain",
251 * which is assumed to be one-dimensional.
253 static __isl_give isl_aff *identity_aff(__isl_keep isl_set *domain)
255 isl_local_space *ls;
257 ls = isl_local_space_from_space(isl_set_get_space(domain));
258 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
261 /* Create an affine expression that maps elements
262 * of a single-dimensional array "id_test" to the previous element
263 * (according to "inc"), provided this element belongs to "domain".
264 * That is, create the affine expression
266 * { id[x] -> id[x - inc] : x - inc in domain }
268 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
269 __isl_take isl_set *domain, __isl_take isl_val *inc)
271 isl_space *space;
272 isl_local_space *ls;
273 isl_aff *aff;
274 isl_multi_pw_aff *prev;
276 space = isl_set_get_space(domain);
277 ls = isl_local_space_from_space(space);
278 aff = isl_aff_var_on_domain(ls, isl_dim_set, 0);
279 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
280 prev = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
281 domain = isl_set_preimage_multi_pw_aff(domain,
282 isl_multi_pw_aff_copy(prev));
283 prev = isl_multi_pw_aff_intersect_domain(prev, domain);
284 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
286 return prev;
289 /* Add an implication to "scop" expressing that if an element of
290 * virtual array "id_test" has value "satisfied" then all previous elements
291 * of this array also have that value. The set of previous elements
292 * is bounded by "domain". If "sign" is negative then the iterator
293 * is decreasing and we express that all subsequent array elements
294 * (but still defined previously) have the same value.
296 static struct pet_scop *add_implication(struct pet_scop *scop,
297 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
298 int satisfied)
300 isl_space *space;
301 isl_map *map;
303 domain = isl_set_set_tuple_id(domain, id_test);
304 space = isl_set_get_space(domain);
305 if (sign > 0)
306 map = isl_map_lex_ge(space);
307 else
308 map = isl_map_lex_le(space);
309 map = isl_map_intersect_range(map, domain);
310 scop = pet_scop_add_implication(scop, map, satisfied);
312 return scop;
315 /* Add a filter to "scop" that imposes that it is only executed
316 * when the variable identified by "id_test" has a zero value
317 * for all previous iterations of "domain".
319 * In particular, add a filter that imposes that the array
320 * has a zero value at the previous iteration of domain and
321 * add an implication that implies that it then has that
322 * value for all previous iterations.
324 static struct pet_scop *scop_add_break(struct pet_scop *scop,
325 __isl_take isl_id *id_test, __isl_take isl_set *domain,
326 __isl_take isl_val *inc)
328 isl_multi_pw_aff *prev;
329 int sign = isl_val_sgn(inc);
331 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
332 scop = add_implication(scop, id_test, domain, sign, 0);
333 scop = pet_scop_filter(scop, prev, 0);
335 return scop;
338 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
339 __isl_keep pet_context *pc, struct pet_state *state);
341 /* Construct a pet_scop for an infinite loop around the given body
342 * within the context "pc".
344 * We extract a pet_scop for the body and then embed it in a loop with
345 * iteration domain
347 * { [t] : t >= 0 }
349 * and schedule
351 * { [t] -> [t] }
353 * If the body contains any break, then it is taken into
354 * account in apply_affine_break (if the skip condition is affine)
355 * or in scop_add_break (if the skip condition is not affine).
357 * Note that in case of an affine skip condition,
358 * since we are dealing with a loop without loop iterator,
359 * the skip condition cannot refer to the current loop iterator and
360 * so effectively, the iteration domain is of the form
362 * { [0]; [t] : t >= 1 and not skip }
364 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
365 __isl_keep pet_context *pc, struct pet_state *state)
367 isl_ctx *ctx;
368 isl_id *id, *id_test;
369 isl_set *domain;
370 isl_set *skip;
371 isl_aff *ident;
372 struct pet_scop *scop;
373 int has_affine_break;
374 int has_var_break;
376 ctx = pet_tree_get_ctx(body);
377 id = isl_id_alloc(ctx, "t", NULL);
378 domain = infinite_domain(isl_id_copy(id));
379 ident = identity_aff(domain);
381 scop = scop_from_tree(body, pc, state);
383 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
384 if (has_affine_break)
385 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
386 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
387 if (has_var_break)
388 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
390 scop = pet_scop_embed(scop, isl_set_copy(domain),
391 isl_aff_copy(ident), ident, id);
392 if (has_affine_break) {
393 domain = apply_affine_break(domain, skip, 1, 0, NULL);
394 scop = pet_scop_intersect_domain_prefix(scop,
395 isl_set_copy(domain));
397 if (has_var_break)
398 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
399 else
400 isl_set_free(domain);
402 return scop;
405 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
407 * for (;;)
408 * body
410 * within the context "pc".
412 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
413 __isl_keep pet_context *pc, struct pet_state *state)
415 struct pet_scop *scop;
417 pc = pet_context_copy(pc);
418 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
420 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
422 pet_context_free(pc);
424 return scop;
427 /* Construct a pet_scop for a while loop of the form
429 * while (pa)
430 * body
432 * within the context "pc".
433 * In particular, construct a scop for an infinite loop around body and
434 * intersect the domain with the affine expression.
435 * Note that this intersection may result in an empty loop.
437 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
438 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
439 struct pet_state *state)
441 struct pet_scop *scop;
442 isl_set *dom;
443 isl_set *valid;
445 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
446 dom = isl_pw_aff_non_zero_set(pa);
447 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
448 scop = pet_scop_restrict(scop, isl_set_params(dom));
449 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
451 pet_context_free(pc);
452 return scop;
455 /* Construct a scop for a while, given the scops for the condition
456 * and the body, the filter identifier and the iteration domain of
457 * the while loop.
459 * In particular, the scop for the condition is filtered to depend
460 * on "id_test" evaluating to true for all previous iterations
461 * of the loop, while the scop for the body is filtered to depend
462 * on "id_test" evaluating to true for all iterations up to the
463 * current iteration.
464 * The actual filter only imposes that this virtual array has
465 * value one on the previous or the current iteration.
466 * The fact that this condition also applies to the previous
467 * iterations is enforced by an implication.
469 * These filtered scops are then combined into a single scop.
471 * "sign" is positive if the iterator increases and negative
472 * if it decreases.
474 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
475 struct pet_scop *scop_body, __isl_take isl_id *id_test,
476 __isl_take isl_set *domain, __isl_take isl_val *inc)
478 isl_ctx *ctx = isl_set_get_ctx(domain);
479 isl_space *space;
480 isl_multi_pw_aff *test_index;
481 isl_multi_pw_aff *prev;
482 int sign = isl_val_sgn(inc);
483 struct pet_scop *scop;
485 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
486 scop_cond = pet_scop_filter(scop_cond, prev, 1);
488 space = isl_space_map_from_set(isl_set_get_space(domain));
489 test_index = isl_multi_pw_aff_identity(space);
490 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
491 isl_id_copy(id_test));
492 scop_body = pet_scop_filter(scop_body, test_index, 1);
494 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
495 scop = add_implication(scop, id_test, domain, sign, 1);
497 return scop;
500 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
501 * evaluating "cond" and writing the result to a virtual scalar,
502 * as expressed by "index".
503 * Do so within the context "pc".
504 * The location of the statement is set to "loc".
506 static struct pet_scop *scop_from_non_affine_condition(
507 __isl_take pet_expr *cond, int stmt_nr,
508 __isl_take isl_multi_pw_aff *index,
509 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
511 pet_expr *expr, *write;
513 write = pet_expr_from_index(index);
514 write = pet_expr_access_set_write(write, 1);
515 write = pet_expr_access_set_read(write, 0);
516 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
518 return scop_from_expr(expr, NULL, stmt_nr, loc, pc);
521 /* Construct a generic while scop, with iteration domain
522 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
523 * The scop consists of two parts,
524 * one for evaluating the condition "cond" and one for the body.
525 * If "expr_inc" is not NULL, then a scop for evaluating this expression
526 * is added at the end of the body,
527 * after replacing any skip conditions resulting from continue statements
528 * by the skip conditions resulting from break statements (if any).
530 * The schedule is adjusted to reflect that the condition is evaluated
531 * before the body is executed and the body is filtered to depend
532 * on the result of the condition evaluating to true on all iterations
533 * up to the current iteration, while the evaluation of the condition itself
534 * is filtered to depend on the result of the condition evaluating to true
535 * on all previous iterations.
536 * The context of the scop representing the body is dropped
537 * because we don't know how many times the body will be executed,
538 * if at all.
540 * If the body contains any break, then it is taken into
541 * account in apply_affine_break (if the skip condition is affine)
542 * or in scop_add_break (if the skip condition is not affine).
544 * Note that in case of an affine skip condition,
545 * since we are dealing with a loop without loop iterator,
546 * the skip condition cannot refer to the current loop iterator and
547 * so effectively, the iteration domain is of the form
549 * { [0]; [t] : t >= 1 and not skip }
551 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
552 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
553 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
554 struct pet_state *state)
556 isl_ctx *ctx;
557 isl_id *id, *id_test, *id_break_test;
558 isl_space *space;
559 isl_multi_pw_aff *test_index;
560 isl_set *domain;
561 isl_set *skip;
562 isl_aff *ident;
563 struct pet_scop *scop, *scop_body;
564 int has_affine_break;
565 int has_var_break;
567 ctx = state->ctx;
568 space = pet_context_get_space(pc);
569 test_index = pet_create_test_index(space, state->n_test++);
570 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
571 isl_multi_pw_aff_copy(test_index),
572 pet_loc_copy(loc), pc);
573 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
574 domain = pet_context_get_domain(pc);
575 scop = pet_scop_add_boolean_array(scop, domain,
576 test_index, state->int_size);
578 id = isl_id_alloc(ctx, "t", NULL);
579 domain = infinite_domain(isl_id_copy(id));
580 ident = identity_aff(domain);
582 scop_body = scop_from_tree(tree_body, pc, state);
584 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
585 if (has_affine_break)
586 skip = pet_scop_get_affine_skip_domain(scop_body,
587 pet_skip_later);
588 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
589 if (has_var_break)
590 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
592 scop = pet_scop_prefix(scop, 0);
593 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(ident),
594 isl_aff_copy(ident), isl_id_copy(id));
595 scop_body = pet_scop_reset_context(scop_body);
596 scop_body = pet_scop_prefix(scop_body, 1);
597 if (expr_inc) {
598 struct pet_scop *scop_inc;
599 scop_inc = scop_from_expr(expr_inc, NULL, state->n_stmt++,
600 loc, pc);
601 scop_inc = pet_scop_prefix(scop_inc, 2);
602 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
603 isl_multi_pw_aff *skip;
604 skip = pet_scop_get_skip(scop_body, pet_skip_later);
605 scop_body = pet_scop_set_skip(scop_body,
606 pet_skip_now, skip);
607 } else
608 pet_scop_reset_skip(scop_body, pet_skip_now);
609 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
610 } else
611 pet_loc_free(loc);
612 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain),
613 isl_aff_copy(ident), ident, id);
615 if (has_affine_break) {
616 domain = apply_affine_break(domain, skip, 1, 0, NULL);
617 scop = pet_scop_intersect_domain_prefix(scop,
618 isl_set_copy(domain));
619 scop_body = pet_scop_intersect_domain_prefix(scop_body,
620 isl_set_copy(domain));
622 if (has_var_break) {
623 scop = scop_add_break(scop, isl_id_copy(id_break_test),
624 isl_set_copy(domain), isl_val_one(ctx));
625 scop_body = scop_add_break(scop_body, id_break_test,
626 isl_set_copy(domain), isl_val_one(ctx));
628 scop = scop_add_while(scop, scop_body, id_test, domain,
629 isl_val_one(ctx));
631 pet_context_free(pc);
632 return scop;
635 /* Check if the while loop is of the form
637 * while (affine expression)
638 * body
640 * If so, call scop_from_affine_while to construct a scop.
642 * Otherwise, pass control to scop_from_non_affine_while.
644 * "pc" is the context in which the affine expressions in the scop are created.
646 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
647 __isl_keep pet_context *pc, struct pet_state *state)
649 pet_expr *cond_expr;
650 isl_pw_aff *pa;
652 if (!tree)
653 return NULL;
655 pc = pet_context_copy(pc);
656 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
658 cond_expr = pet_expr_copy(tree->u.l.cond);
659 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
660 pa = pet_expr_extract_affine_condition(cond_expr, pc);
661 pet_expr_free(cond_expr);
663 if (!pa)
664 goto error;
666 if (!isl_pw_aff_involves_nan(pa))
667 return scop_from_affine_while(tree, pa, pc, state);
668 isl_pw_aff_free(pa);
669 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
670 pet_tree_get_loc(tree), tree->u.l.body, NULL,
671 pc, state);
672 error:
673 pet_context_free(pc);
674 return NULL;
677 /* Check whether "cond" expresses a simple loop bound
678 * on the only set dimension.
679 * In particular, if "up" is set then "cond" should contain only
680 * upper bounds on the set dimension.
681 * Otherwise, it should contain only lower bounds.
683 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
685 if (isl_val_is_pos(inc))
686 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, 0);
687 else
688 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, 0);
691 /* Extend a condition on a given iteration of a loop to one that
692 * imposes the same condition on all previous iterations.
693 * "domain" expresses the lower [upper] bound on the iterations
694 * when inc is positive [negative].
696 * In particular, we construct the condition (when inc is positive)
698 * forall i' : (domain(i') and i' <= i) => cond(i')
700 * which is equivalent to
702 * not exists i' : domain(i') and i' <= i and not cond(i')
704 * We construct this set by negating cond, applying a map
706 * { [i'] -> [i] : domain(i') and i' <= i }
708 * and then negating the result again.
710 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
711 __isl_take isl_set *domain, __isl_take isl_val *inc)
713 isl_map *previous_to_this;
715 if (isl_val_is_pos(inc))
716 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
717 else
718 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
720 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
722 cond = isl_set_complement(cond);
723 cond = isl_set_apply(cond, previous_to_this);
724 cond = isl_set_complement(cond);
726 isl_val_free(inc);
728 return cond;
731 /* Construct a domain of the form
733 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
735 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
736 __isl_take isl_pw_aff *init, __isl_take isl_val *inc)
738 isl_aff *aff;
739 isl_space *dim;
740 isl_set *set;
742 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
743 dim = isl_pw_aff_get_domain_space(init);
744 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
745 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, 0, inc);
746 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
748 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
749 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
750 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
751 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
753 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
755 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
757 return isl_set_params(set);
760 /* Assuming "cond" represents a bound on a loop where the loop
761 * iterator "iv" is incremented (or decremented) by one, check if wrapping
762 * is possible.
764 * Under the given assumptions, wrapping is only possible if "cond" allows
765 * for the last value before wrapping, i.e., 2^width - 1 in case of an
766 * increasing iterator and 0 in case of a decreasing iterator.
768 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
769 __isl_keep isl_val *inc)
771 int cw;
772 isl_ctx *ctx;
773 isl_val *limit;
774 isl_set *test;
776 test = isl_set_copy(cond);
778 ctx = isl_set_get_ctx(test);
779 if (isl_val_is_neg(inc))
780 limit = isl_val_zero(ctx);
781 else {
782 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
783 limit = isl_val_2exp(limit);
784 limit = isl_val_sub_ui(limit, 1);
787 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
788 cw = !isl_set_is_empty(test);
789 isl_set_free(test);
791 return cw;
794 /* Given a one-dimensional space, construct the following affine expression
795 * on this space
797 * { [v] -> [v mod 2^width] }
799 * where width is the number of bits used to represent the values
800 * of the unsigned variable "iv".
802 static __isl_give isl_aff *compute_wrapping(__isl_take isl_space *dim,
803 __isl_keep pet_expr *iv)
805 isl_ctx *ctx;
806 isl_val *mod;
807 isl_aff *aff;
809 ctx = isl_space_get_ctx(dim);
810 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
811 mod = isl_val_2exp(mod);
813 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
814 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
815 aff = isl_aff_mod_val(aff, mod);
817 return aff;
820 /* Project out the parameter "id" from "set".
822 static __isl_give isl_set *set_project_out_by_id(__isl_take isl_set *set,
823 __isl_keep isl_id *id)
825 int pos;
827 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
828 if (pos >= 0)
829 set = isl_set_project_out(set, isl_dim_param, pos, 1);
831 return set;
834 /* Compute the set of parameters for which "set1" is a subset of "set2".
836 * set1 is a subset of set2 if
838 * forall i in set1 : i in set2
840 * or
842 * not exists i in set1 and i not in set2
844 * i.e.,
846 * not exists i in set1 \ set2
848 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
849 __isl_take isl_set *set2)
851 return isl_set_complement(isl_set_params(isl_set_subtract(set1, set2)));
854 /* Compute the set of parameter values for which "cond" holds
855 * on the next iteration for each element of "dom".
857 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
858 * and then compute the set of parameters for which the result is a subset
859 * of "cond".
861 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
862 __isl_take isl_set *dom, __isl_take isl_val *inc)
864 isl_space *space;
865 isl_aff *aff;
866 isl_map *next;
868 space = isl_set_get_space(dom);
869 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
870 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
871 aff = isl_aff_add_constant_val(aff, inc);
872 next = isl_map_from_basic_map(isl_basic_map_from_aff(aff));
874 dom = isl_set_apply(dom, next);
876 return enforce_subset(dom, cond);
879 /* Extract the for loop "tree" as a while loop within the context "pc".
881 * That is, the for loop has the form
883 * for (iv = init; cond; iv += inc)
884 * body;
886 * and is treated as
888 * iv = init;
889 * while (cond) {
890 * body;
891 * iv += inc;
894 * except that the skips resulting from any continue statements
895 * in body do not apply to the increment, but are replaced by the skips
896 * resulting from break statements.
898 * If the loop iterator is declared in the for loop, then it is killed before
899 * and after the loop.
901 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
902 __isl_take pet_context *pc, struct pet_state *state)
904 int declared;
905 isl_id *iv;
906 pet_expr *expr_iv, *init, *inc;
907 struct pet_scop *scop_init, *scop;
908 int type_size;
909 struct pet_array *array;
910 struct pet_scop *scop_kill;
912 iv = pet_expr_access_get_id(tree->u.l.iv);
913 pc = pet_context_mark_assigned(pc, iv);
915 declared = tree->u.l.declared;
917 expr_iv = pet_expr_copy(tree->u.l.iv);
918 type_size = pet_expr_get_type_size(expr_iv);
919 init = pet_expr_copy(tree->u.l.init);
920 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
921 scop_init = scop_from_expr(init, NULL, state->n_stmt++,
922 pet_tree_get_loc(tree), pc);
923 scop_init = pet_scop_prefix(scop_init, declared);
925 expr_iv = pet_expr_copy(tree->u.l.iv);
926 type_size = pet_expr_get_type_size(expr_iv);
927 inc = pet_expr_copy(tree->u.l.inc);
928 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
930 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
931 pet_tree_get_loc(tree), tree->u.l.body, inc,
932 pet_context_copy(pc), state);
934 scop = pet_scop_prefix(scop, declared + 1);
935 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
937 if (!declared) {
938 pet_context_free(pc);
939 return scop;
942 array = extract_array(tree->u.l.iv, pc, state);
943 if (array)
944 array->declared = 1;
945 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
946 scop_kill = pet_scop_prefix(scop_kill, 0);
947 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
948 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
949 scop_kill = pet_scop_add_array(scop_kill, array);
950 scop_kill = pet_scop_prefix(scop_kill, 3);
951 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
953 pet_context_free(pc);
954 return scop;
957 /* Given an access expression "expr", is the variable accessed by
958 * "expr" assigned anywhere inside "tree"?
960 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
962 int assigned = 0;
963 isl_id *id;
965 id = pet_expr_access_get_id(expr);
966 assigned = pet_tree_writes(tree, id);
967 isl_id_free(id);
969 return assigned;
972 /* Are all nested access parameters in "pa" allowed given "tree".
973 * In particular, is none of them written by anywhere inside "tree".
975 * If "tree" has any continue nodes in the current loop level,
976 * then no nested access parameters are allowed.
977 * In particular, if there is any nested access in a guard
978 * for a piece of code containing a "continue", then we want to introduce
979 * a separate statement for evaluating this guard so that we can express
980 * that the result is false for all previous iterations.
982 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
983 __isl_keep pet_tree *tree)
985 int i, nparam;
987 if (!tree)
988 return -1;
990 if (!pet_nested_any_in_pw_aff(pa))
991 return 1;
993 if (pet_tree_has_continue(tree))
994 return 0;
996 nparam = isl_pw_aff_dim(pa, isl_dim_param);
997 for (i = 0; i < nparam; ++i) {
998 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
999 pet_expr *expr;
1000 int allowed;
1002 if (!pet_nested_in_id(id)) {
1003 isl_id_free(id);
1004 continue;
1007 expr = pet_nested_extract_expr(id);
1008 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1009 !is_assigned(expr, tree);
1011 pet_expr_free(expr);
1012 isl_id_free(id);
1014 if (!allowed)
1015 return 0;
1018 return 1;
1021 /* Construct a pet_scop for a for tree with static affine initialization
1022 * and constant increment within the context "pc".
1024 * The condition is allowed to contain nested accesses, provided
1025 * they are not being written to inside the body of the loop.
1026 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1027 * essentially treated as a while loop, with iteration domain
1028 * { [i] : i >= init }.
1030 * We extract a pet_scop for the body and then embed it in a loop with
1031 * iteration domain and schedule
1033 * { [i] : i >= init and condition' }
1034 * { [i] -> [i] }
1036 * or
1038 * { [i] : i <= init and condition' }
1039 * { [i] -> [-i] }
1041 * Where condition' is equal to condition if the latter is
1042 * a simple upper [lower] bound and a condition that is extended
1043 * to apply to all previous iterations otherwise.
1045 * If the condition is non-affine, then we drop the condition from the
1046 * iteration domain and instead create a separate statement
1047 * for evaluating the condition. The body is then filtered to depend
1048 * on the result of the condition evaluating to true on all iterations
1049 * up to the current iteration, while the evaluation the condition itself
1050 * is filtered to depend on the result of the condition evaluating to true
1051 * on all previous iterations.
1052 * The context of the scop representing the body is dropped
1053 * because we don't know how many times the body will be executed,
1054 * if at all.
1056 * If the stride of the loop is not 1, then "i >= init" is replaced by
1058 * (exists a: i = init + stride * a and a >= 0)
1060 * If the loop iterator i is unsigned, then wrapping may occur.
1061 * We therefore use a virtual iterator instead that does not wrap.
1062 * However, the condition in the code applies
1063 * to the wrapped value, so we need to change condition(i)
1064 * into condition([i % 2^width]). Similarly, we replace all accesses
1065 * to the original iterator by the wrapping of the virtual iterator.
1066 * Note that there may be no need to perform this final wrapping
1067 * if the loop condition (after wrapping) satisfies certain conditions.
1068 * However, the is_simple_bound condition is not enough since it doesn't
1069 * check if there even is an upper bound.
1071 * Wrapping on unsigned iterators can be avoided entirely if
1072 * loop condition is simple, the loop iterator is incremented
1073 * [decremented] by one and the last value before wrapping cannot
1074 * possibly satisfy the loop condition.
1076 * Valid parameters for a for loop are those for which the initial
1077 * value itself, the increment on each domain iteration and
1078 * the condition on both the initial value and
1079 * the result of incrementing the iterator for each iteration of the domain
1080 * can be evaluated.
1081 * If the loop condition is non-affine, then we only consider validity
1082 * of the initial value.
1084 * If the body contains any break, then we keep track of it in "skip"
1085 * (if the skip condition is affine) or it is handled in scop_add_break
1086 * (if the skip condition is not affine).
1087 * Note that the affine break condition needs to be considered with
1088 * respect to previous iterations in the virtual domain (if any).
1090 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1091 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1092 __isl_take isl_val *inc, __isl_take pet_context *pc,
1093 struct pet_state *state)
1095 isl_local_space *ls;
1096 isl_set *domain;
1097 isl_aff *sched;
1098 isl_set *cond = NULL;
1099 isl_set *skip = NULL;
1100 isl_id *id, *id_test = NULL, *id_break_test;
1101 struct pet_scop *scop, *scop_cond = NULL;
1102 int is_one;
1103 int is_unsigned;
1104 int is_simple;
1105 int is_virtual;
1106 int is_non_affine;
1107 int has_affine_break;
1108 int has_var_break;
1109 isl_map *rev_wrap = NULL;
1110 isl_aff *wrap = NULL;
1111 isl_pw_aff *pa;
1112 isl_set *valid_init;
1113 isl_set *valid_cond;
1114 isl_set *valid_cond_init;
1115 isl_set *valid_cond_next;
1116 isl_set *valid_inc;
1117 pet_expr *cond_expr;
1118 pet_context *pc_nested;
1120 id = pet_expr_access_get_id(tree->u.l.iv);
1122 cond_expr = pet_expr_copy(tree->u.l.cond);
1123 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1124 pc_nested = pet_context_copy(pc);
1125 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1126 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1127 pet_context_free(pc_nested);
1128 pet_expr_free(cond_expr);
1130 valid_inc = isl_pw_aff_domain(pa_inc);
1132 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1134 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1135 !is_nested_allowed(pa, tree->u.l.body);
1136 if (is_non_affine)
1137 pa = isl_pw_aff_free(pa);
1139 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1140 cond = isl_pw_aff_non_zero_set(pa);
1141 if (is_non_affine)
1142 cond = isl_set_universe(isl_space_set_alloc(state->ctx, 0, 0));
1144 cond = embed(cond, isl_id_copy(id));
1145 valid_cond = isl_set_coalesce(valid_cond);
1146 valid_cond = embed(valid_cond, isl_id_copy(id));
1147 valid_inc = embed(valid_inc, isl_id_copy(id));
1148 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1149 is_virtual = is_unsigned &&
1150 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1152 valid_cond_init = enforce_subset(
1153 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val))),
1154 isl_set_copy(valid_cond));
1155 if (is_one && !is_virtual) {
1156 isl_pw_aff_free(init_val);
1157 pa = pet_expr_extract_comparison(
1158 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1159 tree->u.l.iv, tree->u.l.init, pc);
1160 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1161 valid_init = set_project_out_by_id(valid_init, id);
1162 domain = isl_pw_aff_non_zero_set(pa);
1163 } else {
1164 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1165 domain = strided_domain(isl_id_copy(id), init_val,
1166 isl_val_copy(inc));
1169 domain = embed(domain, isl_id_copy(id));
1170 if (is_virtual) {
1171 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1172 rev_wrap = isl_map_from_aff(isl_aff_copy(wrap));
1173 rev_wrap = isl_map_reverse(rev_wrap);
1174 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1175 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1176 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1178 is_simple = is_simple_bound(cond, inc);
1179 if (!is_simple) {
1180 cond = isl_set_gist(cond, isl_set_copy(domain));
1181 is_simple = is_simple_bound(cond, inc);
1183 if (!is_simple)
1184 cond = valid_for_each_iteration(cond,
1185 isl_set_copy(domain), isl_val_copy(inc));
1186 domain = isl_set_intersect(domain, cond);
1187 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1188 ls = isl_local_space_from_space(isl_set_get_space(domain));
1189 sched = isl_aff_var_on_domain(ls, isl_dim_set, 0);
1190 if (isl_val_is_neg(inc))
1191 sched = isl_aff_neg(sched);
1193 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1194 isl_val_copy(inc));
1195 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1197 if (!is_virtual)
1198 wrap = identity_aff(domain);
1200 if (is_non_affine) {
1201 isl_space *space;
1202 isl_multi_pw_aff *test_index;
1203 space = pet_context_get_space(pc);
1204 test_index = pet_create_test_index(space, state->n_test++);
1205 scop_cond = scop_from_non_affine_condition(
1206 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1207 isl_multi_pw_aff_copy(test_index),
1208 pet_tree_get_loc(tree), pc);
1209 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1210 isl_dim_out);
1211 scop_cond = pet_scop_add_boolean_array(scop_cond,
1212 pet_context_get_domain(pc), test_index,
1213 state->int_size);
1214 scop_cond = pet_scop_prefix(scop_cond, 0);
1215 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1216 isl_aff_copy(sched), isl_aff_copy(wrap),
1217 isl_id_copy(id));
1220 scop = scop_from_tree(tree->u.l.body, pc, state);
1221 has_affine_break = scop &&
1222 pet_scop_has_affine_skip(scop, pet_skip_later);
1223 if (has_affine_break)
1224 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1225 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1226 if (has_var_break)
1227 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1228 if (is_non_affine) {
1229 scop = pet_scop_reset_context(scop);
1230 scop = pet_scop_prefix(scop, 1);
1232 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
1233 scop = pet_scop_resolve_nested(scop);
1234 if (has_affine_break) {
1235 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1236 is_virtual, rev_wrap);
1237 scop = pet_scop_intersect_domain_prefix(scop,
1238 isl_set_copy(domain));
1240 isl_map_free(rev_wrap);
1241 if (has_var_break)
1242 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1243 isl_val_copy(inc));
1244 if (is_non_affine) {
1245 scop = scop_add_while(scop_cond, scop, id_test, domain,
1246 isl_val_copy(inc));
1247 isl_set_free(valid_inc);
1248 } else {
1249 scop = pet_scop_restrict_context(scop, valid_inc);
1250 scop = pet_scop_restrict_context(scop, valid_cond_next);
1251 scop = pet_scop_restrict_context(scop, valid_cond_init);
1252 isl_set_free(domain);
1255 isl_val_free(inc);
1257 scop = pet_scop_restrict_context(scop, isl_set_params(valid_init));
1259 pet_context_free(pc);
1260 return scop;
1263 /* Construct a pet_scop for a for statement within the context of "pc".
1265 * We update the context to reflect the writes to the loop variable and
1266 * the writes inside the body.
1268 * Then we check if the initialization of the for loop
1269 * is a static affine value and the increment is a constant.
1270 * If so, we construct the pet_scop using scop_from_affine_for.
1271 * Otherwise, we treat the for loop as a while loop
1272 * in scop_from_non_affine_for.
1274 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1275 __isl_keep pet_context *pc, struct pet_state *state)
1277 isl_id *iv;
1278 isl_val *inc;
1279 isl_pw_aff *pa_inc, *init_val;
1280 pet_context *pc_init_val;
1282 if (!tree)
1283 return NULL;
1285 iv = pet_expr_access_get_id(tree->u.l.iv);
1286 pc = pet_context_copy(pc);
1287 pc = pet_context_clear_value(pc, iv);
1288 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1290 pc_init_val = pet_context_copy(pc);
1291 pc_init_val = pet_context_mark_unknown(pc_init_val, isl_id_copy(iv));
1292 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1293 pet_context_free(pc_init_val);
1294 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1295 inc = pet_extract_cst(pa_inc);
1296 if (!pa_inc || !init_val || !inc)
1297 goto error;
1298 if (!isl_pw_aff_involves_nan(pa_inc) &&
1299 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1300 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1301 pc, state);
1303 isl_pw_aff_free(pa_inc);
1304 isl_pw_aff_free(init_val);
1305 isl_val_free(inc);
1306 return scop_from_non_affine_for(tree, pc, state);
1307 error:
1308 isl_pw_aff_free(pa_inc);
1309 isl_pw_aff_free(init_val);
1310 isl_val_free(inc);
1311 pet_context_free(pc);
1312 return NULL;
1315 /* Check whether "expr" is an affine constraint within the context "pc".
1317 static int is_affine_condition(__isl_keep pet_expr *expr,
1318 __isl_keep pet_context *pc)
1320 isl_pw_aff *pa;
1321 int is_affine;
1323 pa = pet_expr_extract_affine_condition(expr, pc);
1324 if (!pa)
1325 return -1;
1326 is_affine = !isl_pw_aff_involves_nan(pa);
1327 isl_pw_aff_free(pa);
1329 return is_affine;
1332 /* Check if the given if statement is a conditional assignement
1333 * with a non-affine condition.
1335 * In particular we check if "stmt" is of the form
1337 * if (condition)
1338 * a = f(...);
1339 * else
1340 * a = g(...);
1342 * where the condition is non-affine and a is some array or scalar access.
1344 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1345 __isl_keep pet_context *pc)
1347 int equal;
1348 isl_ctx *ctx;
1349 pet_expr *expr1, *expr2;
1351 ctx = pet_tree_get_ctx(tree);
1352 if (!pet_options_get_detect_conditional_assignment(ctx))
1353 return 0;
1354 if (tree->type != pet_tree_if_else)
1355 return 0;
1356 if (tree->u.i.then_body->type != pet_tree_expr)
1357 return 0;
1358 if (tree->u.i.else_body->type != pet_tree_expr)
1359 return 0;
1360 expr1 = tree->u.i.then_body->u.e.expr;
1361 expr2 = tree->u.i.else_body->u.e.expr;
1362 if (pet_expr_get_type(expr1) != pet_expr_op)
1363 return 0;
1364 if (pet_expr_get_type(expr2) != pet_expr_op)
1365 return 0;
1366 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1367 return 0;
1368 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1369 return 0;
1370 expr1 = pet_expr_get_arg(expr1, 0);
1371 expr2 = pet_expr_get_arg(expr2, 0);
1372 equal = pet_expr_is_equal(expr1, expr2);
1373 pet_expr_free(expr1);
1374 pet_expr_free(expr2);
1375 if (equal < 0 || !equal)
1376 return 0;
1377 if (is_affine_condition(tree->u.i.cond, pc))
1378 return 0;
1380 return 1;
1383 /* Given that "tree" is of the form
1385 * if (condition)
1386 * a = f(...);
1387 * else
1388 * a = g(...);
1390 * where a is some array or scalar access, construct a pet_scop
1391 * corresponding to this conditional assignment within the context "pc".
1393 * The constructed pet_scop then corresponds to the expression
1395 * a = condition ? f(...) : g(...)
1397 * All access relations in f(...) are intersected with condition
1398 * while all access relation in g(...) are intersected with the complement.
1400 static struct pet_scop *scop_from_conditional_assignment(
1401 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1402 struct pet_state *state)
1404 int type_size;
1405 isl_pw_aff *pa;
1406 isl_set *cond, *comp;
1407 isl_multi_pw_aff *index;
1408 pet_expr *expr1, *expr2;
1409 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1410 pet_context *pc_nested;
1411 struct pet_scop *scop;
1413 pe_cond = pet_expr_copy(tree->u.i.cond);
1414 pe_cond = pet_expr_plug_in_args(pe_cond, pc);
1415 pc_nested = pet_context_copy(pc);
1416 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1417 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1418 pet_context_free(pc_nested);
1419 pet_expr_free(pe_cond);
1420 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1421 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1422 index = isl_multi_pw_aff_from_pw_aff(pa);
1424 expr1 = tree->u.i.then_body->u.e.expr;
1425 expr2 = tree->u.i.else_body->u.e.expr;
1427 pe_cond = pet_expr_from_index(index);
1429 pe_then = pet_expr_get_arg(expr1, 1);
1430 pe_then = pet_expr_restrict(pe_then, cond);
1431 pe_else = pet_expr_get_arg(expr2, 1);
1432 pe_else = pet_expr_restrict(pe_else, comp);
1433 pe_write = pet_expr_get_arg(expr1, 0);
1435 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1436 type_size = pet_expr_get_type_size(pe_write);
1437 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1439 scop = scop_from_expr(pe, NULL, state->n_stmt++,
1440 pet_tree_get_loc(tree), pc);
1442 pet_context_free(pc);
1444 return scop;
1447 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1449 * We create a separate statement that writes the result
1450 * of the non-affine condition to a virtual scalar.
1451 * A constraint requiring the value of this virtual scalar to be one
1452 * is added to the iteration domains of the then branch.
1453 * Similarly, a constraint requiring the value of this virtual scalar
1454 * to be zero is added to the iteration domains of the else branch, if any.
1455 * We adjust the schedules to ensure that the virtual scalar is written
1456 * before it is read.
1458 * If there are any breaks or continues in the then and/or else
1459 * branches, then we may have to compute a new skip condition.
1460 * This is handled using a pet_skip_info object.
1461 * On initialization, the object checks if skip conditions need
1462 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1463 * adds them in pet_skip_info_if_add.
1465 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1466 __isl_take pet_context *pc, struct pet_state *state)
1468 int has_else;
1469 isl_space *space;
1470 isl_set *domain;
1471 isl_multi_pw_aff *test_index;
1472 struct pet_skip_info skip;
1473 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1475 has_else = tree->type == pet_tree_if_else;
1477 space = pet_context_get_space(pc);
1478 test_index = pet_create_test_index(space, state->n_test++);
1479 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1480 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1481 pet_tree_get_loc(tree), pc);
1482 domain = pet_context_get_domain(pc);
1483 scop = pet_scop_add_boolean_array(scop, domain,
1484 isl_multi_pw_aff_copy(test_index), state->int_size);
1486 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1487 if (has_else)
1488 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1490 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1491 has_else, 0);
1492 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1494 scop = pet_scop_prefix(scop, 0);
1495 scop_then = pet_scop_prefix(scop_then, 1);
1496 scop_then = pet_scop_filter(scop_then,
1497 isl_multi_pw_aff_copy(test_index), 1);
1498 if (has_else) {
1499 scop_else = pet_scop_prefix(scop_else, 1);
1500 scop_else = pet_scop_filter(scop_else, test_index, 0);
1501 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1502 } else
1503 isl_multi_pw_aff_free(test_index);
1505 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1507 scop = pet_skip_info_if_add(&skip, scop, 2);
1509 pet_context_free(pc);
1510 return scop;
1513 /* Construct a pet_scop for an affine if statement within the context "pc".
1515 * The condition is added to the iteration domains of the then branch,
1516 * while the opposite of the condition in added to the iteration domains
1517 * of the else branch, if any.
1519 * If there are any breaks or continues in the then and/or else
1520 * branches, then we may have to compute a new skip condition.
1521 * This is handled using a pet_skip_info_if object.
1522 * On initialization, the object checks if skip conditions need
1523 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1524 * adds them in pet_skip_info_if_add.
1526 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1527 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1528 struct pet_state *state)
1530 int has_else;
1531 isl_ctx *ctx;
1532 isl_set *set;
1533 isl_set *valid;
1534 struct pet_skip_info skip;
1535 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1537 ctx = pet_tree_get_ctx(tree);
1539 has_else = tree->type == pet_tree_if_else;
1541 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1542 if (has_else)
1543 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1545 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1546 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
1548 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1549 set = isl_pw_aff_non_zero_set(cond);
1550 scop = pet_scop_restrict(scop_then, isl_set_params(isl_set_copy(set)));
1552 if (has_else) {
1553 set = isl_set_subtract(isl_set_copy(valid), set);
1554 scop_else = pet_scop_restrict(scop_else, isl_set_params(set));
1555 scop = pet_scop_add_par(ctx, scop, scop_else);
1556 } else
1557 isl_set_free(set);
1558 scop = pet_scop_resolve_nested(scop);
1559 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
1561 if (pet_skip_info_has_skip(&skip))
1562 scop = pet_scop_prefix(scop, 0);
1563 scop = pet_skip_info_if_add(&skip, scop, 1);
1565 pet_context_free(pc);
1566 return scop;
1569 /* Construct a pet_scop for an if statement within the context "pc".
1571 * If the condition fits the pattern of a conditional assignment,
1572 * then it is handled by scop_from_conditional_assignment.
1574 * Otherwise, we check if the condition is affine.
1575 * If so, we construct the scop in scop_from_affine_if.
1576 * Otherwise, we construct the scop in scop_from_non_affine_if.
1578 * We allow the condition to be dynamic, i.e., to refer to
1579 * scalars or array elements that may be written to outside
1580 * of the given if statement. These nested accesses are then represented
1581 * as output dimensions in the wrapping iteration domain.
1582 * If it is also written _inside_ the then or else branch, then
1583 * we treat the condition as non-affine.
1584 * As explained in extract_non_affine_if, this will introduce
1585 * an extra statement.
1586 * For aesthetic reasons, we want this statement to have a statement
1587 * number that is lower than those of the then and else branches.
1588 * In order to evaluate if we will need such a statement, however, we
1589 * first construct scops for the then and else branches.
1590 * We therefore reserve a statement number if we might have to
1591 * introduce such an extra statement.
1593 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1594 __isl_keep pet_context *pc, struct pet_state *state)
1596 int has_else;
1597 isl_pw_aff *cond;
1598 pet_expr *cond_expr;
1599 pet_context *pc_nested;
1601 if (!tree)
1602 return NULL;
1604 has_else = tree->type == pet_tree_if_else;
1606 pc = pet_context_copy(pc);
1607 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1608 if (has_else)
1609 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1611 if (is_conditional_assignment(tree, pc))
1612 return scop_from_conditional_assignment(tree, pc, state);
1614 cond_expr = pet_expr_copy(tree->u.i.cond);
1615 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1616 pc_nested = pet_context_copy(pc);
1617 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1618 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1619 pet_context_free(pc_nested);
1620 pet_expr_free(cond_expr);
1622 if (!cond) {
1623 pet_context_free(pc);
1624 return NULL;
1627 if (isl_pw_aff_involves_nan(cond)) {
1628 isl_pw_aff_free(cond);
1629 return scop_from_non_affine_if(tree, pc, state);
1632 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1633 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1634 isl_pw_aff_free(cond);
1635 return scop_from_non_affine_if(tree, pc, state);
1638 return scop_from_affine_if(tree, cond, pc, state);
1641 /* Return a one-dimensional multi piecewise affine expression that is equal
1642 * to the constant 1 and is defined over the given domain.
1644 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
1646 isl_local_space *ls;
1647 isl_aff *aff;
1649 ls = isl_local_space_from_space(space);
1650 aff = isl_aff_zero_on_domain(ls);
1651 aff = isl_aff_set_constant_si(aff, 1);
1653 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1656 /* Construct a pet_scop for a continue statement with the given domain space.
1658 * We simply create an empty scop with a universal pet_skip_now
1659 * skip condition. This skip condition will then be taken into
1660 * account by the enclosing loop construct, possibly after
1661 * being incorporated into outer skip conditions.
1663 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
1664 __isl_take isl_space *space)
1666 struct pet_scop *scop;
1668 scop = pet_scop_empty(isl_space_copy(space));
1670 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
1672 return scop;
1675 /* Construct a pet_scop for a break statement with the given domain space.
1677 * We simply create an empty scop with both a universal pet_skip_now
1678 * skip condition and a universal pet_skip_later skip condition.
1679 * These skip conditions will then be taken into
1680 * account by the enclosing loop construct, possibly after
1681 * being incorporated into outer skip conditions.
1683 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
1684 __isl_take isl_space *space)
1686 struct pet_scop *scop;
1687 isl_multi_pw_aff *skip;
1689 scop = pet_scop_empty(isl_space_copy(space));
1691 skip = one_mpa(space);
1692 scop = pet_scop_set_skip(scop, pet_skip_now,
1693 isl_multi_pw_aff_copy(skip));
1694 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1696 return scop;
1699 /* Extract a clone of the kill statement in "scop".
1700 * The domain of the clone is given by "domain".
1701 * "scop" is expected to have been created from a DeclStmt
1702 * and should have the kill as its first statement.
1704 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
1705 struct pet_scop *scop, struct pet_state *state)
1707 pet_expr *kill;
1708 struct pet_stmt *stmt;
1709 isl_multi_pw_aff *index;
1710 isl_map *access;
1711 pet_expr *arg;
1713 if (!domain || !scop)
1714 return NULL;
1715 if (scop->n_stmt < 1)
1716 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1717 "expecting at least one statement", return NULL);
1718 stmt = scop->stmts[0];
1719 if (!pet_stmt_is_kill(stmt))
1720 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1721 "expecting kill statement", return NULL);
1723 arg = pet_expr_get_arg(stmt->body, 0);
1724 index = pet_expr_access_get_index(arg);
1725 access = pet_expr_access_get_access(arg);
1726 pet_expr_free(arg);
1727 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
1728 access = isl_map_reset_tuple_id(access, isl_dim_in);
1729 kill = pet_expr_kill_from_access_and_index(access, index);
1730 stmt = pet_stmt_from_pet_expr(isl_set_copy(domain),
1731 pet_loc_copy(stmt->loc), NULL, state->n_stmt++, kill);
1732 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
1735 /* Does "tree" represent an assignment to a variable?
1737 * The assignment may be one of
1738 * - a declaration with initialization
1739 * - an expression with a top-level assignment operator
1741 static int is_assignment(__isl_keep pet_tree *tree)
1743 if (!tree)
1744 return 0;
1745 if (tree->type == pet_tree_decl_init)
1746 return 1;
1747 return pet_tree_is_assign(tree);
1750 /* Update "pc" by taking into account the assignment performed by "tree",
1751 * where "tree" satisfies is_assignment.
1753 * In particular, if the lhs of the assignment is a scalar variable and
1754 * if the rhs is an affine expression, then keep track of this value in "pc"
1755 * so that we can plug it in when we later come across the same variable.
1757 * The variable has already been marked as having been assigned
1758 * an unknown value by scop_handle_writes.
1760 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
1761 __isl_keep pet_tree *tree)
1763 pet_expr *var, *val;
1764 isl_id *id;
1765 isl_pw_aff *pa;
1767 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
1768 var = pet_tree_decl_get_var(tree);
1769 val = pet_tree_decl_get_init(tree);
1770 } else {
1771 pet_expr *expr;
1772 expr = pet_tree_expr_get_expr(tree);
1773 var = pet_expr_get_arg(expr, 0);
1774 val = pet_expr_get_arg(expr, 1);
1775 pet_expr_free(expr);
1778 if (!pet_expr_is_scalar_access(var)) {
1779 pet_expr_free(var);
1780 pet_expr_free(val);
1781 return pc;
1784 pa = pet_expr_extract_affine(val, pc);
1785 if (!pa)
1786 pc = pet_context_free(pc);
1788 if (!isl_pw_aff_involves_nan(pa)) {
1789 id = pet_expr_access_get_id(var);
1790 pc = pet_context_set_value(pc, id, pa);
1791 } else {
1792 isl_pw_aff_free(pa);
1794 pet_expr_free(var);
1795 pet_expr_free(val);
1797 return pc;
1800 /* Mark all arrays in "scop" as being exposed.
1802 static struct pet_scop *mark_exposed(struct pet_scop *scop)
1804 int i;
1806 if (!scop)
1807 return NULL;
1808 for (i = 0; i < scop->n_array; ++i)
1809 scop->arrays[i]->exposed = 1;
1810 return scop;
1813 /* Try and construct a pet_scop corresponding to (part of)
1814 * a sequence of statements within the context "pc".
1816 * After extracting a statement, we update "pc"
1817 * based on the top-level assignments in the statement
1818 * so that we can exploit them in subsequent statements in the same block.
1820 * If there are any breaks or continues in the individual statements,
1821 * then we may have to compute a new skip condition.
1822 * This is handled using a pet_skip_info object.
1823 * On initialization, the object checks if skip conditions need
1824 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1825 * adds them in pet_skip_info_seq_add.
1827 * If "block" is set, then we need to insert kill statements at
1828 * the end of the block for any array that has been declared by
1829 * one of the statements in the sequence. Each of these declarations
1830 * results in the construction of a kill statement at the place
1831 * of the declaration, so we simply collect duplicates of
1832 * those kill statements and append these duplicates to the constructed scop.
1834 * If "block" is not set, then any array declared by one of the statements
1835 * in the sequence is marked as being exposed.
1837 * If autodetect is set, then we allow the extraction of only a subrange
1838 * of the sequence of statements. However, if there is at least one statement
1839 * for which we could not construct a scop and the final range contains
1840 * either no statements or at least one kill, then we discard the entire
1841 * range.
1843 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
1844 __isl_keep pet_context *pc, struct pet_state *state)
1846 int i;
1847 isl_ctx *ctx;
1848 isl_space *space;
1849 isl_set *domain;
1850 struct pet_scop *scop, *kills;
1852 ctx = pet_tree_get_ctx(tree);
1854 space = pet_context_get_space(pc);
1855 domain = pet_context_get_domain(pc);
1856 pc = pet_context_copy(pc);
1857 scop = pet_scop_empty(isl_space_copy(space));
1858 kills = pet_scop_empty(space);
1859 for (i = 0; i < tree->u.b.n; ++i) {
1860 struct pet_scop *scop_i;
1862 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
1863 pc = scop_handle_writes(scop_i, pc);
1864 if (is_assignment(tree->u.b.child[i]))
1865 pc = handle_assignment(pc, tree->u.b.child[i]);
1866 struct pet_skip_info skip;
1867 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
1868 pet_skip_info_seq_extract(&skip, pc, state);
1869 if (pet_skip_info_has_skip(&skip))
1870 scop_i = pet_scop_prefix(scop_i, 0);
1871 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
1872 if (tree->u.b.block) {
1873 struct pet_scop *kill;
1874 kill = extract_kill(domain, scop_i, state);
1875 kills = pet_scop_add_par(ctx, kills, kill);
1876 } else
1877 scop_i = mark_exposed(scop_i);
1879 scop_i = pet_scop_prefix(scop_i, i);
1880 scop = pet_scop_add_seq(ctx, scop, scop_i);
1882 scop = pet_skip_info_seq_add(&skip, scop, i);
1884 if (!scop)
1885 break;
1887 isl_set_free(domain);
1889 kills = pet_scop_prefix(kills, tree->u.b.n);
1890 scop = pet_scop_add_seq(ctx, scop, kills);
1892 pet_context_free(pc);
1894 return scop;
1897 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1898 * within the context "pc" by calling the appropriate function
1899 * based on the type of "tree".
1901 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
1902 __isl_keep pet_context *pc, struct pet_state *state)
1904 if (!tree)
1905 return NULL;
1907 switch (tree->type) {
1908 case pet_tree_error:
1909 return NULL;
1910 case pet_tree_block:
1911 return scop_from_block(tree, pc, state);
1912 case pet_tree_break:
1913 return scop_from_break(tree, pet_context_get_space(pc));
1914 case pet_tree_continue:
1915 return scop_from_continue(tree, pet_context_get_space(pc));
1916 case pet_tree_decl:
1917 case pet_tree_decl_init:
1918 return scop_from_decl(tree, pc, state);
1919 case pet_tree_expr:
1920 return scop_from_expr(pet_expr_copy(tree->u.e.expr),
1921 isl_id_copy(tree->label),
1922 state->n_stmt++,
1923 pet_tree_get_loc(tree), pc);
1924 case pet_tree_if:
1925 case pet_tree_if_else:
1926 return scop_from_if(tree, pc, state);
1927 case pet_tree_for:
1928 return scop_from_for(tree, pc, state);
1929 case pet_tree_while:
1930 return scop_from_while(tree, pc, state);
1931 case pet_tree_infinite_loop:
1932 return scop_from_infinite_for(tree, pc, state);
1935 isl_die(tree->ctx, isl_error_internal, "unhandled type",
1936 return NULL);
1939 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1940 * "int_size" is the number of bytes need to represent an integer.
1941 * "extract_array" is a callback that we can use to create a pet_array
1942 * that corresponds to the variable accessed by an expression.
1944 * Initialize the global state, construct a context and then
1945 * construct the pet_scop by recursively visiting the tree.
1947 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
1948 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
1949 __isl_keep pet_context *pc, void *user), void *user,
1950 __isl_keep pet_context *pc)
1952 struct pet_scop *scop;
1953 struct pet_state state = { 0 };
1955 if (!tree)
1956 return NULL;
1958 state.ctx = pet_tree_get_ctx(tree);
1959 state.int_size = int_size;
1960 state.extract_array = extract_array;
1961 state.user = user;
1963 scop = scop_from_tree(tree, pc, &state);
1964 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
1966 pet_tree_free(tree);
1968 return scop;