tree2scop.c: scop_from_non_affine_while: drop preallocation of statement number
[pet.git] / tree2scop.c
blob88befb56854af805558a5e2336436359761be9c0
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, first mark all scalar variables that are written by "stmt"
48 * as having an unknown value. Afterwards,
49 * if "stmt" is a top-level (i.e., unconditional) assignment
50 * to a scalar variable, then update "pc" accordingly.
52 * In particular, if the lhs of the assignment is a scalar variable, then mark
53 * the variable as having been assigned. If, furthermore, the rhs
54 * is an affine expression, then keep track of this value in "pc"
55 * so that we can plug it in when we later come across the same variable.
57 * We skip assignments to virtual arrays (those with NULL user pointer).
59 static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
60 __isl_take pet_context *pc)
62 pet_expr *body = stmt->body;
63 pet_expr *arg;
64 isl_id *id;
65 isl_pw_aff *pa;
67 pc = pet_context_clear_writes_in_expr(pc, body);
68 if (!pc)
69 return NULL;
71 if (pet_expr_get_type(body) != pet_expr_op)
72 return pc;
73 if (pet_expr_op_get_type(body) != pet_op_assign)
74 return pc;
75 if (!isl_set_plain_is_universe(stmt->domain))
76 return pc;
77 arg = pet_expr_get_arg(body, 0);
78 if (!pet_expr_is_scalar_access(arg)) {
79 pet_expr_free(arg);
80 return pc;
83 id = pet_expr_access_get_id(arg);
84 pet_expr_free(arg);
86 if (!isl_id_get_user(id)) {
87 isl_id_free(id);
88 return pc;
91 arg = pet_expr_get_arg(body, 1);
92 pa = pet_expr_extract_affine(arg, pc);
93 pc = pet_context_mark_assigned(pc, isl_id_copy(id));
94 pet_expr_free(arg);
96 if (pa && isl_pw_aff_involves_nan(pa)) {
97 isl_id_free(id);
98 isl_pw_aff_free(pa);
99 return pc;
102 pc = pet_context_set_value(pc, id, pa);
104 return pc;
107 /* Update "pc" based on the write accesses (and, in particular,
108 * assignments) in "scop".
110 static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
111 __isl_take pet_context *pc)
113 int i;
115 if (!scop)
116 return pet_context_free(pc);
117 for (i = 0; i < scop->n_stmt; ++i)
118 pc = handle_writes(scop->stmts[i], pc);
120 return pc;
123 /* Convert a top-level pet_expr to a pet_scop with one statement
124 * within the context "pc".
125 * This mainly involves resolving nested expression parameters
126 * and setting the name of the iteration space.
127 * The name is given by "label" if it is non-NULL. Otherwise,
128 * it is of the form S_<stmt_nr>.
129 * The location of the statement is set to "loc".
131 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
132 __isl_take isl_id *label, int stmt_nr, __isl_take pet_loc *loc,
133 __isl_keep pet_context *pc)
135 isl_ctx *ctx;
136 struct pet_stmt *ps;
138 ctx = pet_expr_get_ctx(expr);
140 expr = pet_expr_plug_in_args(expr, pc);
141 expr = pet_expr_resolve_nested(expr);
142 expr = pet_expr_resolve_assume(expr, pc);
143 ps = pet_stmt_from_pet_expr(loc, label, stmt_nr, expr);
144 return pet_scop_from_pet_stmt(ctx, ps);
147 /* Construct a pet_scop with a single statement killing the entire
148 * array "array".
149 * The location of the statement is set to "loc".
151 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
152 __isl_keep pet_context *pc, struct pet_state *state)
154 isl_ctx *ctx;
155 isl_id *id;
156 isl_space *space;
157 isl_multi_pw_aff *index;
158 isl_map *access;
159 pet_expr *expr;
160 struct pet_scop *scop;
162 if (!array)
163 goto error;
164 ctx = isl_set_get_ctx(array->extent);
165 access = isl_map_from_range(isl_set_copy(array->extent));
166 id = isl_set_get_tuple_id(array->extent);
167 space = isl_space_alloc(ctx, 0, 0, 0);
168 space = isl_space_set_tuple_id(space, isl_dim_out, id);
169 index = isl_multi_pw_aff_zero(space);
170 expr = pet_expr_kill_from_access_and_index(access, index);
171 return scop_from_expr(expr, NULL, state->n_stmt++, loc, pc);
172 error:
173 pet_loc_free(loc);
174 return NULL;
177 /* Construct and return a pet_array corresponding to the variable
178 * accessed by "access" by calling the extract_array callback.
180 static struct pet_array *extract_array(__isl_keep pet_expr *access,
181 __isl_keep pet_context *pc, struct pet_state *state)
183 return state->extract_array(access, pc, state->user);
186 /* Construct a pet_scop for a (single) variable declaration
187 * within the context "pc".
189 * The scop contains the variable being declared (as an array)
190 * and a statement killing the array.
192 * If the declaration comes with an initialization, then the scop
193 * also contains an assignment to the variable.
195 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
196 __isl_keep pet_context *pc, struct pet_state *state)
198 int type_size;
199 isl_ctx *ctx;
200 struct pet_array *array;
201 struct pet_scop *scop_decl, *scop;
202 pet_expr *lhs, *rhs, *pe;
204 array = extract_array(tree->u.d.var, pc, state);
205 if (array)
206 array->declared = 1;
207 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
208 scop_decl = pet_scop_add_array(scop_decl, array);
210 if (tree->type != pet_tree_decl_init)
211 return scop_decl;
213 lhs = pet_expr_copy(tree->u.d.var);
214 rhs = pet_expr_copy(tree->u.d.init);
215 type_size = pet_expr_get_type_size(lhs);
216 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
217 scop = scop_from_expr(pe, NULL, state->n_stmt++,
218 pet_tree_get_loc(tree), pc);
220 scop_decl = pet_scop_prefix(scop_decl, 0);
221 scop = pet_scop_prefix(scop, 1);
223 ctx = pet_tree_get_ctx(tree);
224 scop = pet_scop_add_seq(ctx, scop_decl, scop);
226 return scop;
229 /* Embed the given iteration domain in an extra outer loop
230 * with induction variable "var".
231 * If this variable appeared as a parameter in the constraints,
232 * it is replaced by the new outermost dimension.
234 static __isl_give isl_set *embed(__isl_take isl_set *set,
235 __isl_take isl_id *var)
237 int pos;
239 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
240 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
241 if (pos >= 0) {
242 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
243 set = isl_set_project_out(set, isl_dim_param, pos, 1);
246 isl_id_free(var);
247 return set;
250 /* Return those elements in the space of "cond" that come after
251 * (based on "sign") an element in "cond".
253 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
255 isl_map *previous_to_this;
257 if (sign > 0)
258 previous_to_this = isl_map_lex_lt(isl_set_get_space(cond));
259 else
260 previous_to_this = isl_map_lex_gt(isl_set_get_space(cond));
262 cond = isl_set_apply(cond, previous_to_this);
264 return cond;
267 /* Remove those iterations of "domain" that have an earlier iteration
268 * (based on "sign") where "skip" is satisfied.
269 * "domain" has an extra outer loop compared to "skip".
270 * The skip condition is first embedded in the same space as "domain".
271 * If "apply_skip_map" is set, then "skip_map" is first applied
272 * to the embedded skip condition before removing it from the domain.
274 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
275 __isl_take isl_set *skip, int sign,
276 int apply_skip_map, __isl_keep isl_map *skip_map)
278 skip = embed(skip, isl_set_get_dim_id(domain, isl_dim_set, 0));
279 if (apply_skip_map)
280 skip = isl_set_apply(skip, isl_map_copy(skip_map));
281 skip = isl_set_intersect(skip , isl_set_copy(domain));
282 return isl_set_subtract(domain, after(skip, sign));
285 /* Create the infinite iteration domain
287 * { [id] : id >= 0 }
289 static __isl_give isl_set *infinite_domain(__isl_take isl_id *id)
291 isl_ctx *ctx = isl_id_get_ctx(id);
292 isl_set *domain;
294 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
295 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, id);
297 return domain;
300 /* Create an identity affine expression on the space containing "domain",
301 * which is assumed to be one-dimensional.
303 static __isl_give isl_aff *identity_aff(__isl_keep isl_set *domain)
305 isl_local_space *ls;
307 ls = isl_local_space_from_space(isl_set_get_space(domain));
308 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
311 /* Create an affine expression that maps elements
312 * of a single-dimensional array "id_test" to the previous element
313 * (according to "inc"), provided this element belongs to "domain".
314 * That is, create the affine expression
316 * { id[x] -> id[x - inc] : x - inc in domain }
318 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
319 __isl_take isl_set *domain, __isl_take isl_val *inc)
321 isl_space *space;
322 isl_local_space *ls;
323 isl_aff *aff;
324 isl_multi_pw_aff *prev;
326 space = isl_set_get_space(domain);
327 ls = isl_local_space_from_space(space);
328 aff = isl_aff_var_on_domain(ls, isl_dim_set, 0);
329 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
330 prev = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
331 domain = isl_set_preimage_multi_pw_aff(domain,
332 isl_multi_pw_aff_copy(prev));
333 prev = isl_multi_pw_aff_intersect_domain(prev, domain);
334 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
336 return prev;
339 /* Add an implication to "scop" expressing that if an element of
340 * virtual array "id_test" has value "satisfied" then all previous elements
341 * of this array also have that value. The set of previous elements
342 * is bounded by "domain". If "sign" is negative then the iterator
343 * is decreasing and we express that all subsequent array elements
344 * (but still defined previously) have the same value.
346 static struct pet_scop *add_implication(struct pet_scop *scop,
347 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
348 int satisfied)
350 isl_space *space;
351 isl_map *map;
353 domain = isl_set_set_tuple_id(domain, id_test);
354 space = isl_set_get_space(domain);
355 if (sign > 0)
356 map = isl_map_lex_ge(space);
357 else
358 map = isl_map_lex_le(space);
359 map = isl_map_intersect_range(map, domain);
360 scop = pet_scop_add_implication(scop, map, satisfied);
362 return scop;
365 /* Add a filter to "scop" that imposes that it is only executed
366 * when the variable identified by "id_test" has a zero value
367 * for all previous iterations of "domain".
369 * In particular, add a filter that imposes that the array
370 * has a zero value at the previous iteration of domain and
371 * add an implication that implies that it then has that
372 * value for all previous iterations.
374 static struct pet_scop *scop_add_break(struct pet_scop *scop,
375 __isl_take isl_id *id_test, __isl_take isl_set *domain,
376 __isl_take isl_val *inc)
378 isl_multi_pw_aff *prev;
379 int sign = isl_val_sgn(inc);
381 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
382 scop = add_implication(scop, id_test, domain, sign, 0);
383 scop = pet_scop_filter(scop, prev, 0);
385 return scop;
388 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
389 __isl_keep pet_context *pc, struct pet_state *state);
391 /* Construct a pet_scop for an infinite loop around the given body
392 * within the context "pc".
394 * We extract a pet_scop for the body and then embed it in a loop with
395 * iteration domain
397 * { [t] : t >= 0 }
399 * and schedule
401 * { [t] -> [t] }
403 * If the body contains any break, then it is taken into
404 * account in apply_affine_break (if the skip condition is affine)
405 * or in scop_add_break (if the skip condition is not affine).
407 * Note that in case of an affine skip condition,
408 * since we are dealing with a loop without loop iterator,
409 * the skip condition cannot refer to the current loop iterator and
410 * so effectively, the iteration domain is of the form
412 * { [0]; [t] : t >= 1 and not skip }
414 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
415 __isl_keep pet_context *pc, struct pet_state *state)
417 isl_ctx *ctx;
418 isl_id *id, *id_test;
419 isl_set *domain;
420 isl_set *skip;
421 isl_aff *ident;
422 struct pet_scop *scop;
423 int has_affine_break;
424 int has_var_break;
426 ctx = pet_tree_get_ctx(body);
427 id = isl_id_alloc(ctx, "t", NULL);
428 domain = infinite_domain(isl_id_copy(id));
429 ident = identity_aff(domain);
431 scop = scop_from_tree(body, pc, state);
433 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
434 if (has_affine_break)
435 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
436 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
437 if (has_var_break)
438 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
440 scop = pet_scop_embed(scop, isl_set_copy(domain),
441 isl_aff_copy(ident), ident, id);
442 if (has_affine_break) {
443 domain = apply_affine_break(domain, skip, 1, 0, NULL);
444 scop = pet_scop_intersect_domain_prefix(scop,
445 isl_set_copy(domain));
447 if (has_var_break)
448 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
449 else
450 isl_set_free(domain);
452 return scop;
455 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
457 * for (;;)
458 * body
460 * within the context "pc".
462 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
463 __isl_keep pet_context *pc, struct pet_state *state)
465 struct pet_scop *scop;
467 pc = pet_context_copy(pc);
468 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
470 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
472 pet_context_free(pc);
474 return scop;
477 /* Construct a pet_scop for a while loop of the form
479 * while (pa)
480 * body
482 * within the context "pc".
483 * In particular, construct a scop for an infinite loop around body and
484 * intersect the domain with the affine expression.
485 * Note that this intersection may result in an empty loop.
487 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
488 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
489 struct pet_state *state)
491 struct pet_scop *scop;
492 isl_set *dom;
493 isl_set *valid;
495 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
496 dom = isl_pw_aff_non_zero_set(pa);
497 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
498 scop = pet_scop_restrict(scop, isl_set_params(dom));
499 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
501 pet_context_free(pc);
502 return scop;
505 /* Construct a scop for a while, given the scops for the condition
506 * and the body, the filter identifier and the iteration domain of
507 * the while loop.
509 * In particular, the scop for the condition is filtered to depend
510 * on "id_test" evaluating to true for all previous iterations
511 * of the loop, while the scop for the body is filtered to depend
512 * on "id_test" evaluating to true for all iterations up to the
513 * current iteration.
514 * The actual filter only imposes that this virtual array has
515 * value one on the previous or the current iteration.
516 * The fact that this condition also applies to the previous
517 * iterations is enforced by an implication.
519 * These filtered scops are then combined into a single scop.
521 * "sign" is positive if the iterator increases and negative
522 * if it decreases.
524 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
525 struct pet_scop *scop_body, __isl_take isl_id *id_test,
526 __isl_take isl_set *domain, __isl_take isl_val *inc)
528 isl_ctx *ctx = isl_set_get_ctx(domain);
529 isl_space *space;
530 isl_multi_pw_aff *test_index;
531 isl_multi_pw_aff *prev;
532 int sign = isl_val_sgn(inc);
533 struct pet_scop *scop;
535 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
536 scop_cond = pet_scop_filter(scop_cond, prev, 1);
538 space = isl_space_map_from_set(isl_set_get_space(domain));
539 test_index = isl_multi_pw_aff_identity(space);
540 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
541 isl_id_copy(id_test));
542 scop_body = pet_scop_filter(scop_body, test_index, 1);
544 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
545 scop = add_implication(scop, id_test, domain, sign, 1);
547 return scop;
550 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
551 * evaluating "cond" and writing the result to a virtual scalar,
552 * as expressed by "index".
553 * Do so within the context "pc".
554 * The location of the statement is set to "loc".
556 static struct pet_scop *scop_from_non_affine_condition(
557 __isl_take pet_expr *cond, int stmt_nr,
558 __isl_take isl_multi_pw_aff *index,
559 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
561 pet_expr *expr, *write;
563 write = pet_expr_from_index(index);
564 write = pet_expr_access_set_write(write, 1);
565 write = pet_expr_access_set_read(write, 0);
566 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
568 return scop_from_expr(expr, NULL, stmt_nr, loc, pc);
571 /* Construct a generic while scop, with iteration domain
572 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
573 * The scop consists of two parts,
574 * one for evaluating the condition "cond" and one for the body.
575 * If "expr_inc" is not NULL, then a scop for evaluating this expression
576 * is added at the end of the body,
577 * after replacing any skip conditions resulting from continue statements
578 * by the skip conditions resulting from break statements (if any).
580 * The schedule is adjusted to reflect that the condition is evaluated
581 * before the body is executed and the body is filtered to depend
582 * on the result of the condition evaluating to true on all iterations
583 * up to the current iteration, while the evaluation of the condition itself
584 * is filtered to depend on the result of the condition evaluating to true
585 * on all previous iterations.
586 * The context of the scop representing the body is dropped
587 * because we don't know how many times the body will be executed,
588 * if at all.
590 * If the body contains any break, then it is taken into
591 * account in apply_affine_break (if the skip condition is affine)
592 * or in scop_add_break (if the skip condition is not affine).
594 * Note that in case of an affine skip condition,
595 * since we are dealing with a loop without loop iterator,
596 * the skip condition cannot refer to the current loop iterator and
597 * so effectively, the iteration domain is of the form
599 * { [0]; [t] : t >= 1 and not skip }
601 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
602 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
603 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
604 struct pet_state *state)
606 isl_ctx *ctx;
607 isl_id *id, *id_test, *id_break_test;
608 isl_multi_pw_aff *test_index;
609 isl_set *domain;
610 isl_set *skip;
611 isl_aff *ident;
612 struct pet_scop *scop, *scop_body;
613 int has_affine_break;
614 int has_var_break;
616 ctx = state->ctx;
617 test_index = pet_create_test_index(ctx, state->n_test++);
618 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
619 isl_multi_pw_aff_copy(test_index),
620 pet_loc_copy(loc), pc);
621 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
622 scop = pet_scop_add_boolean_array(scop, test_index, state->int_size);
624 id = isl_id_alloc(ctx, "t", NULL);
625 domain = infinite_domain(isl_id_copy(id));
626 ident = identity_aff(domain);
628 scop_body = scop_from_tree(tree_body, pc, state);
630 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
631 if (has_affine_break)
632 skip = pet_scop_get_affine_skip_domain(scop_body,
633 pet_skip_later);
634 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
635 if (has_var_break)
636 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
638 scop = pet_scop_prefix(scop, 0);
639 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(ident),
640 isl_aff_copy(ident), isl_id_copy(id));
641 scop_body = pet_scop_reset_context(scop_body);
642 scop_body = pet_scop_prefix(scop_body, 1);
643 if (expr_inc) {
644 struct pet_scop *scop_inc;
645 scop_inc = scop_from_expr(expr_inc, NULL, state->n_stmt++,
646 loc, pc);
647 scop_inc = pet_scop_prefix(scop_inc, 2);
648 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
649 isl_multi_pw_aff *skip;
650 skip = pet_scop_get_skip(scop_body, pet_skip_later);
651 scop_body = pet_scop_set_skip(scop_body,
652 pet_skip_now, skip);
653 } else
654 pet_scop_reset_skip(scop_body, pet_skip_now);
655 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
656 } else
657 pet_loc_free(loc);
658 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain),
659 isl_aff_copy(ident), ident, id);
661 if (has_affine_break) {
662 domain = apply_affine_break(domain, skip, 1, 0, NULL);
663 scop = pet_scop_intersect_domain_prefix(scop,
664 isl_set_copy(domain));
665 scop_body = pet_scop_intersect_domain_prefix(scop_body,
666 isl_set_copy(domain));
668 if (has_var_break) {
669 scop = scop_add_break(scop, isl_id_copy(id_break_test),
670 isl_set_copy(domain), isl_val_one(ctx));
671 scop_body = scop_add_break(scop_body, id_break_test,
672 isl_set_copy(domain), isl_val_one(ctx));
674 scop = scop_add_while(scop, scop_body, id_test, domain,
675 isl_val_one(ctx));
677 pet_context_free(pc);
678 return scop;
681 /* Check if the while loop is of the form
683 * while (affine expression)
684 * body
686 * If so, call scop_from_affine_while to construct a scop.
688 * Otherwise, pass control to scop_from_non_affine_while.
690 * "pc" is the context in which the affine expressions in the scop are created.
692 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
693 __isl_keep pet_context *pc, struct pet_state *state)
695 pet_expr *cond_expr;
696 isl_pw_aff *pa;
698 if (!tree)
699 return NULL;
701 pc = pet_context_copy(pc);
702 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
704 cond_expr = pet_expr_copy(tree->u.l.cond);
705 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
706 pa = pet_expr_extract_affine_condition(cond_expr, pc);
707 pet_expr_free(cond_expr);
709 if (!pa)
710 goto error;
712 if (!isl_pw_aff_involves_nan(pa))
713 return scop_from_affine_while(tree, pa, pc, state);
714 isl_pw_aff_free(pa);
715 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
716 pet_tree_get_loc(tree), tree->u.l.body, NULL,
717 pc, state);
718 error:
719 pet_context_free(pc);
720 return NULL;
723 /* Check whether "cond" expresses a simple loop bound
724 * on the only set dimension.
725 * In particular, if "up" is set then "cond" should contain only
726 * upper bounds on the set dimension.
727 * Otherwise, it should contain only lower bounds.
729 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
731 if (isl_val_is_pos(inc))
732 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, 0);
733 else
734 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, 0);
737 /* Extend a condition on a given iteration of a loop to one that
738 * imposes the same condition on all previous iterations.
739 * "domain" expresses the lower [upper] bound on the iterations
740 * when inc is positive [negative].
742 * In particular, we construct the condition (when inc is positive)
744 * forall i' : (domain(i') and i' <= i) => cond(i')
746 * which is equivalent to
748 * not exists i' : domain(i') and i' <= i and not cond(i')
750 * We construct this set by negating cond, applying a map
752 * { [i'] -> [i] : domain(i') and i' <= i }
754 * and then negating the result again.
756 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
757 __isl_take isl_set *domain, __isl_take isl_val *inc)
759 isl_map *previous_to_this;
761 if (isl_val_is_pos(inc))
762 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
763 else
764 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
766 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
768 cond = isl_set_complement(cond);
769 cond = isl_set_apply(cond, previous_to_this);
770 cond = isl_set_complement(cond);
772 isl_val_free(inc);
774 return cond;
777 /* Construct a domain of the form
779 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
781 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
782 __isl_take isl_pw_aff *init, __isl_take isl_val *inc)
784 isl_aff *aff;
785 isl_space *dim;
786 isl_set *set;
788 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
789 dim = isl_pw_aff_get_domain_space(init);
790 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
791 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, 0, inc);
792 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
794 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
795 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
796 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
797 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
799 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
801 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
803 return isl_set_params(set);
806 /* Assuming "cond" represents a bound on a loop where the loop
807 * iterator "iv" is incremented (or decremented) by one, check if wrapping
808 * is possible.
810 * Under the given assumptions, wrapping is only possible if "cond" allows
811 * for the last value before wrapping, i.e., 2^width - 1 in case of an
812 * increasing iterator and 0 in case of a decreasing iterator.
814 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
815 __isl_keep isl_val *inc)
817 int cw;
818 isl_ctx *ctx;
819 isl_val *limit;
820 isl_set *test;
822 test = isl_set_copy(cond);
824 ctx = isl_set_get_ctx(test);
825 if (isl_val_is_neg(inc))
826 limit = isl_val_zero(ctx);
827 else {
828 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
829 limit = isl_val_2exp(limit);
830 limit = isl_val_sub_ui(limit, 1);
833 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
834 cw = !isl_set_is_empty(test);
835 isl_set_free(test);
837 return cw;
840 /* Given a one-dimensional space, construct the following affine expression
841 * on this space
843 * { [v] -> [v mod 2^width] }
845 * where width is the number of bits used to represent the values
846 * of the unsigned variable "iv".
848 static __isl_give isl_aff *compute_wrapping(__isl_take isl_space *dim,
849 __isl_keep pet_expr *iv)
851 isl_ctx *ctx;
852 isl_val *mod;
853 isl_aff *aff;
855 ctx = isl_space_get_ctx(dim);
856 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
857 mod = isl_val_2exp(mod);
859 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
860 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
861 aff = isl_aff_mod_val(aff, mod);
863 return aff;
866 /* Project out the parameter "id" from "set".
868 static __isl_give isl_set *set_project_out_by_id(__isl_take isl_set *set,
869 __isl_keep isl_id *id)
871 int pos;
873 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
874 if (pos >= 0)
875 set = isl_set_project_out(set, isl_dim_param, pos, 1);
877 return set;
880 /* Compute the set of parameters for which "set1" is a subset of "set2".
882 * set1 is a subset of set2 if
884 * forall i in set1 : i in set2
886 * or
888 * not exists i in set1 and i not in set2
890 * i.e.,
892 * not exists i in set1 \ set2
894 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
895 __isl_take isl_set *set2)
897 return isl_set_complement(isl_set_params(isl_set_subtract(set1, set2)));
900 /* Compute the set of parameter values for which "cond" holds
901 * on the next iteration for each element of "dom".
903 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
904 * and then compute the set of parameters for which the result is a subset
905 * of "cond".
907 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
908 __isl_take isl_set *dom, __isl_take isl_val *inc)
910 isl_space *space;
911 isl_aff *aff;
912 isl_map *next;
914 space = isl_set_get_space(dom);
915 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
916 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
917 aff = isl_aff_add_constant_val(aff, inc);
918 next = isl_map_from_basic_map(isl_basic_map_from_aff(aff));
920 dom = isl_set_apply(dom, next);
922 return enforce_subset(dom, cond);
925 /* Extract the for loop "tree" as a while loop within the context "pc".
927 * That is, the for loop has the form
929 * for (iv = init; cond; iv += inc)
930 * body;
932 * and is treated as
934 * iv = init;
935 * while (cond) {
936 * body;
937 * iv += inc;
940 * except that the skips resulting from any continue statements
941 * in body do not apply to the increment, but are replaced by the skips
942 * resulting from break statements.
944 * If the loop iterator is declared in the for loop, then it is killed before
945 * and after the loop.
947 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
948 __isl_take pet_context *pc, struct pet_state *state)
950 int declared;
951 isl_id *iv;
952 pet_expr *expr_iv, *init, *inc;
953 struct pet_scop *scop_init, *scop;
954 int type_size;
955 struct pet_array *array;
956 struct pet_scop *scop_kill;
958 iv = pet_expr_access_get_id(tree->u.l.iv);
959 pc = pet_context_mark_assigned(pc, iv);
961 declared = tree->u.l.declared;
963 expr_iv = pet_expr_copy(tree->u.l.iv);
964 type_size = pet_expr_get_type_size(expr_iv);
965 init = pet_expr_copy(tree->u.l.init);
966 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
967 scop_init = scop_from_expr(init, NULL, state->n_stmt++,
968 pet_tree_get_loc(tree), pc);
969 scop_init = pet_scop_prefix(scop_init, declared);
971 expr_iv = pet_expr_copy(tree->u.l.iv);
972 type_size = pet_expr_get_type_size(expr_iv);
973 inc = pet_expr_copy(tree->u.l.inc);
974 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
976 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
977 pet_tree_get_loc(tree), tree->u.l.body, inc,
978 pet_context_copy(pc), state);
980 scop = pet_scop_prefix(scop, declared + 1);
981 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
983 if (!declared) {
984 pet_context_free(pc);
985 return scop;
988 array = extract_array(tree->u.l.iv, pc, state);
989 if (array)
990 array->declared = 1;
991 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
992 scop_kill = pet_scop_prefix(scop_kill, 0);
993 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
994 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
995 scop_kill = pet_scop_add_array(scop_kill, array);
996 scop_kill = pet_scop_prefix(scop_kill, 3);
997 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
999 pet_context_free(pc);
1000 return scop;
1003 /* Given an access expression "expr", is the variable accessed by
1004 * "expr" assigned anywhere inside "tree"?
1006 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1008 int assigned = 0;
1009 isl_id *id;
1011 id = pet_expr_access_get_id(expr);
1012 assigned = pet_tree_writes(tree, id);
1013 isl_id_free(id);
1015 return assigned;
1018 /* Are all nested access parameters in "pa" allowed given "tree".
1019 * In particular, is none of them written by anywhere inside "tree".
1021 * If "tree" has any continue nodes in the current loop level,
1022 * then no nested access parameters are allowed.
1023 * In particular, if there is any nested access in a guard
1024 * for a piece of code containing a "continue", then we want to introduce
1025 * a separate statement for evaluating this guard so that we can express
1026 * that the result is false for all previous iterations.
1028 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1029 __isl_keep pet_tree *tree)
1031 int i, nparam;
1033 if (!tree)
1034 return -1;
1036 if (!pet_nested_any_in_pw_aff(pa))
1037 return 1;
1039 if (pet_tree_has_continue(tree))
1040 return 0;
1042 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1043 for (i = 0; i < nparam; ++i) {
1044 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1045 pet_expr *expr;
1046 int allowed;
1048 if (!pet_nested_in_id(id)) {
1049 isl_id_free(id);
1050 continue;
1053 expr = pet_nested_extract_expr(id);
1054 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1055 !is_assigned(expr, tree);
1057 pet_expr_free(expr);
1058 isl_id_free(id);
1060 if (!allowed)
1061 return 0;
1064 return 1;
1067 /* Construct a pet_scop for a for tree with static affine initialization
1068 * and constant increment within the context "pc".
1070 * The condition is allowed to contain nested accesses, provided
1071 * they are not being written to inside the body of the loop.
1072 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1073 * essentially treated as a while loop, with iteration domain
1074 * { [i] : i >= init }.
1076 * We extract a pet_scop for the body and then embed it in a loop with
1077 * iteration domain and schedule
1079 * { [i] : i >= init and condition' }
1080 * { [i] -> [i] }
1082 * or
1084 * { [i] : i <= init and condition' }
1085 * { [i] -> [-i] }
1087 * Where condition' is equal to condition if the latter is
1088 * a simple upper [lower] bound and a condition that is extended
1089 * to apply to all previous iterations otherwise.
1091 * If the condition is non-affine, then we drop the condition from the
1092 * iteration domain and instead create a separate statement
1093 * for evaluating the condition. The body is then filtered to depend
1094 * on the result of the condition evaluating to true on all iterations
1095 * up to the current iteration, while the evaluation the condition itself
1096 * is filtered to depend on the result of the condition evaluating to true
1097 * on all previous iterations.
1098 * The context of the scop representing the body is dropped
1099 * because we don't know how many times the body will be executed,
1100 * if at all.
1102 * If the stride of the loop is not 1, then "i >= init" is replaced by
1104 * (exists a: i = init + stride * a and a >= 0)
1106 * If the loop iterator i is unsigned, then wrapping may occur.
1107 * We therefore use a virtual iterator instead that does not wrap.
1108 * However, the condition in the code applies
1109 * to the wrapped value, so we need to change condition(i)
1110 * into condition([i % 2^width]). Similarly, we replace all accesses
1111 * to the original iterator by the wrapping of the virtual iterator.
1112 * Note that there may be no need to perform this final wrapping
1113 * if the loop condition (after wrapping) satisfies certain conditions.
1114 * However, the is_simple_bound condition is not enough since it doesn't
1115 * check if there even is an upper bound.
1117 * Wrapping on unsigned iterators can be avoided entirely if
1118 * loop condition is simple, the loop iterator is incremented
1119 * [decremented] by one and the last value before wrapping cannot
1120 * possibly satisfy the loop condition.
1122 * Valid parameters for a for loop are those for which the initial
1123 * value itself, the increment on each domain iteration and
1124 * the condition on both the initial value and
1125 * the result of incrementing the iterator for each iteration of the domain
1126 * can be evaluated.
1127 * If the loop condition is non-affine, then we only consider validity
1128 * of the initial value.
1130 * If the body contains any break, then we keep track of it in "skip"
1131 * (if the skip condition is affine) or it is handled in scop_add_break
1132 * (if the skip condition is not affine).
1133 * Note that the affine break condition needs to be considered with
1134 * respect to previous iterations in the virtual domain (if any).
1136 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1137 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1138 __isl_take isl_val *inc, __isl_take pet_context *pc,
1139 struct pet_state *state)
1141 isl_local_space *ls;
1142 isl_set *domain;
1143 isl_aff *sched;
1144 isl_set *cond = NULL;
1145 isl_set *skip = NULL;
1146 isl_id *id, *id_test = NULL, *id_break_test;
1147 struct pet_scop *scop, *scop_cond = NULL;
1148 int is_one;
1149 int is_unsigned;
1150 int is_simple;
1151 int is_virtual;
1152 int is_non_affine;
1153 int has_affine_break;
1154 int has_var_break;
1155 isl_map *rev_wrap = NULL;
1156 isl_aff *wrap = NULL;
1157 isl_pw_aff *pa;
1158 isl_set *valid_init;
1159 isl_set *valid_cond;
1160 isl_set *valid_cond_init;
1161 isl_set *valid_cond_next;
1162 isl_set *valid_inc;
1163 pet_expr *cond_expr;
1164 pet_context *pc_nested;
1166 id = pet_expr_access_get_id(tree->u.l.iv);
1168 cond_expr = pet_expr_copy(tree->u.l.cond);
1169 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1170 pc_nested = pet_context_copy(pc);
1171 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1172 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1173 pet_context_free(pc_nested);
1174 pet_expr_free(cond_expr);
1176 valid_inc = isl_pw_aff_domain(pa_inc);
1178 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1180 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1181 !is_nested_allowed(pa, tree->u.l.body);
1182 if (is_non_affine)
1183 pa = isl_pw_aff_free(pa);
1185 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1186 cond = isl_pw_aff_non_zero_set(pa);
1187 if (is_non_affine) {
1188 isl_multi_pw_aff *test_index;
1189 test_index = pet_create_test_index(state->ctx, state->n_test++);
1190 scop_cond = scop_from_non_affine_condition(
1191 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1192 isl_multi_pw_aff_copy(test_index),
1193 pet_tree_get_loc(tree), pc);
1194 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1195 isl_dim_out);
1196 scop_cond = pet_scop_add_boolean_array(scop_cond, test_index,
1197 state->int_size);
1198 scop_cond = pet_scop_prefix(scop_cond, 0);
1199 cond = isl_set_universe(isl_space_set_alloc(state->ctx, 0, 0));
1202 cond = embed(cond, isl_id_copy(id));
1203 valid_cond = isl_set_coalesce(valid_cond);
1204 valid_cond = embed(valid_cond, isl_id_copy(id));
1205 valid_inc = embed(valid_inc, isl_id_copy(id));
1206 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1207 is_virtual = is_unsigned &&
1208 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1210 valid_cond_init = enforce_subset(
1211 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val))),
1212 isl_set_copy(valid_cond));
1213 if (is_one && !is_virtual) {
1214 isl_pw_aff_free(init_val);
1215 pa = pet_expr_extract_comparison(
1216 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1217 tree->u.l.iv, tree->u.l.init, pc);
1218 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1219 valid_init = set_project_out_by_id(valid_init, id);
1220 domain = isl_pw_aff_non_zero_set(pa);
1221 } else {
1222 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1223 domain = strided_domain(isl_id_copy(id), init_val,
1224 isl_val_copy(inc));
1227 domain = embed(domain, isl_id_copy(id));
1228 if (is_virtual) {
1229 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1230 rev_wrap = isl_map_from_aff(isl_aff_copy(wrap));
1231 rev_wrap = isl_map_reverse(rev_wrap);
1232 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1233 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1234 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1236 is_simple = is_simple_bound(cond, inc);
1237 if (!is_simple) {
1238 cond = isl_set_gist(cond, isl_set_copy(domain));
1239 is_simple = is_simple_bound(cond, inc);
1241 if (!is_simple)
1242 cond = valid_for_each_iteration(cond,
1243 isl_set_copy(domain), isl_val_copy(inc));
1244 domain = isl_set_intersect(domain, cond);
1245 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1246 ls = isl_local_space_from_space(isl_set_get_space(domain));
1247 sched = isl_aff_var_on_domain(ls, isl_dim_set, 0);
1248 if (isl_val_is_neg(inc))
1249 sched = isl_aff_neg(sched);
1251 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1252 isl_val_copy(inc));
1253 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1255 if (!is_virtual)
1256 wrap = identity_aff(domain);
1258 scop = scop_from_tree(tree->u.l.body, pc, state);
1260 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1261 isl_aff_copy(sched), isl_aff_copy(wrap), isl_id_copy(id));
1262 has_affine_break = scop &&
1263 pet_scop_has_affine_skip(scop, pet_skip_later);
1264 if (has_affine_break)
1265 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1266 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1267 if (has_var_break)
1268 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1269 if (is_non_affine) {
1270 scop = pet_scop_reset_context(scop);
1271 scop = pet_scop_prefix(scop, 1);
1273 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
1274 scop = pet_scop_resolve_nested(scop);
1275 if (has_affine_break) {
1276 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1277 is_virtual, rev_wrap);
1278 scop = pet_scop_intersect_domain_prefix(scop,
1279 isl_set_copy(domain));
1281 isl_map_free(rev_wrap);
1282 if (has_var_break)
1283 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1284 isl_val_copy(inc));
1285 if (is_non_affine) {
1286 scop = scop_add_while(scop_cond, scop, id_test, domain,
1287 isl_val_copy(inc));
1288 isl_set_free(valid_inc);
1289 } else {
1290 scop = pet_scop_restrict_context(scop, valid_inc);
1291 scop = pet_scop_restrict_context(scop, valid_cond_next);
1292 scop = pet_scop_restrict_context(scop, valid_cond_init);
1293 isl_set_free(domain);
1296 isl_val_free(inc);
1298 scop = pet_scop_restrict_context(scop, isl_set_params(valid_init));
1300 pet_context_free(pc);
1301 return scop;
1304 /* Construct a pet_scop for a for statement within the context of "pc".
1306 * We update the context to reflect the writes to the loop variable and
1307 * the writes inside the body.
1309 * Then we check if the initialization of the for loop
1310 * is a static affine value and the increment is a constant.
1311 * If so, we construct the pet_scop using scop_from_affine_for.
1312 * Otherwise, we treat the for loop as a while loop
1313 * in scop_from_non_affine_for.
1315 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1316 __isl_keep pet_context *pc, struct pet_state *state)
1318 isl_id *iv;
1319 isl_val *inc;
1320 isl_pw_aff *pa_inc, *init_val;
1321 pet_context *pc_init_val;
1323 if (!tree)
1324 return NULL;
1326 iv = pet_expr_access_get_id(tree->u.l.iv);
1327 pc = pet_context_copy(pc);
1328 pc = pet_context_clear_value(pc, iv);
1329 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1331 pc_init_val = pet_context_copy(pc);
1332 pc_init_val = pet_context_mark_unknown(pc_init_val, isl_id_copy(iv));
1333 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1334 pet_context_free(pc_init_val);
1335 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1336 inc = pet_extract_cst(pa_inc);
1337 if (!pa_inc || !init_val || !inc)
1338 goto error;
1339 if (!isl_pw_aff_involves_nan(pa_inc) &&
1340 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1341 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1342 pc, state);
1344 isl_pw_aff_free(pa_inc);
1345 isl_pw_aff_free(init_val);
1346 isl_val_free(inc);
1347 return scop_from_non_affine_for(tree, pc, state);
1348 error:
1349 isl_pw_aff_free(pa_inc);
1350 isl_pw_aff_free(init_val);
1351 isl_val_free(inc);
1352 pet_context_free(pc);
1353 return NULL;
1356 /* Check whether "expr" is an affine constraint within the context "pc".
1358 static int is_affine_condition(__isl_keep pet_expr *expr,
1359 __isl_keep pet_context *pc)
1361 isl_pw_aff *pa;
1362 int is_affine;
1364 pa = pet_expr_extract_affine_condition(expr, pc);
1365 if (!pa)
1366 return -1;
1367 is_affine = !isl_pw_aff_involves_nan(pa);
1368 isl_pw_aff_free(pa);
1370 return is_affine;
1373 /* Check if the given if statement is a conditional assignement
1374 * with a non-affine condition.
1376 * In particular we check if "stmt" is of the form
1378 * if (condition)
1379 * a = f(...);
1380 * else
1381 * a = g(...);
1383 * where the condition is non-affine and a is some array or scalar access.
1385 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1386 __isl_keep pet_context *pc)
1388 int equal;
1389 isl_ctx *ctx;
1390 pet_expr *expr1, *expr2;
1392 ctx = pet_tree_get_ctx(tree);
1393 if (!pet_options_get_detect_conditional_assignment(ctx))
1394 return 0;
1395 if (tree->type != pet_tree_if_else)
1396 return 0;
1397 if (tree->u.i.then_body->type != pet_tree_expr)
1398 return 0;
1399 if (tree->u.i.else_body->type != pet_tree_expr)
1400 return 0;
1401 expr1 = tree->u.i.then_body->u.e.expr;
1402 expr2 = tree->u.i.else_body->u.e.expr;
1403 if (pet_expr_get_type(expr1) != pet_expr_op)
1404 return 0;
1405 if (pet_expr_get_type(expr2) != pet_expr_op)
1406 return 0;
1407 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1408 return 0;
1409 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1410 return 0;
1411 expr1 = pet_expr_get_arg(expr1, 0);
1412 expr2 = pet_expr_get_arg(expr2, 0);
1413 equal = pet_expr_is_equal(expr1, expr2);
1414 pet_expr_free(expr1);
1415 pet_expr_free(expr2);
1416 if (equal < 0 || !equal)
1417 return 0;
1418 if (is_affine_condition(tree->u.i.cond, pc))
1419 return 0;
1421 return 1;
1424 /* Given that "tree" is of the form
1426 * if (condition)
1427 * a = f(...);
1428 * else
1429 * a = g(...);
1431 * where a is some array or scalar access, construct a pet_scop
1432 * corresponding to this conditional assignment within the context "pc".
1434 * The constructed pet_scop then corresponds to the expression
1436 * a = condition ? f(...) : g(...)
1438 * All access relations in f(...) are intersected with condition
1439 * while all access relation in g(...) are intersected with the complement.
1441 static struct pet_scop *scop_from_conditional_assignment(
1442 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1443 struct pet_state *state)
1445 int type_size;
1446 isl_pw_aff *pa;
1447 isl_set *cond, *comp;
1448 isl_multi_pw_aff *index;
1449 pet_expr *expr1, *expr2;
1450 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1451 pet_context *pc_nested;
1452 struct pet_scop *scop;
1454 pe_cond = pet_expr_copy(tree->u.i.cond);
1455 pe_cond = pet_expr_plug_in_args(pe_cond, pc);
1456 pc_nested = pet_context_copy(pc);
1457 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1458 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1459 pet_context_free(pc_nested);
1460 pet_expr_free(pe_cond);
1461 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1462 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1463 index = isl_multi_pw_aff_from_pw_aff(pa);
1465 expr1 = tree->u.i.then_body->u.e.expr;
1466 expr2 = tree->u.i.else_body->u.e.expr;
1468 pe_cond = pet_expr_from_index(index);
1470 pe_then = pet_expr_get_arg(expr1, 1);
1471 pe_then = pet_expr_restrict(pe_then, cond);
1472 pe_else = pet_expr_get_arg(expr2, 1);
1473 pe_else = pet_expr_restrict(pe_else, comp);
1474 pe_write = pet_expr_get_arg(expr1, 0);
1476 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1477 type_size = pet_expr_get_type_size(pe_write);
1478 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1480 scop = scop_from_expr(pe, NULL, state->n_stmt++,
1481 pet_tree_get_loc(tree), pc);
1483 pet_context_free(pc);
1485 return scop;
1488 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1490 * We create a separate statement that writes the result
1491 * of the non-affine condition to a virtual scalar.
1492 * A constraint requiring the value of this virtual scalar to be one
1493 * is added to the iteration domains of the then branch.
1494 * Similarly, a constraint requiring the value of this virtual scalar
1495 * to be zero is added to the iteration domains of the else branch, if any.
1496 * We adjust the schedules to ensure that the virtual scalar is written
1497 * before it is read.
1499 * If there are any breaks or continues in the then and/or else
1500 * branches, then we may have to compute a new skip condition.
1501 * This is handled using a pet_skip_info object.
1502 * On initialization, the object checks if skip conditions need
1503 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1504 * adds them in pet_skip_info_if_add.
1506 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1507 struct pet_scop *scop_then, struct pet_scop *scop_else, int stmt_id,
1508 __isl_take pet_context *pc, struct pet_state *state)
1510 int has_else;
1511 int save_n_stmt = state->n_stmt;
1512 isl_multi_pw_aff *test_index;
1513 struct pet_skip_info skip;
1514 struct pet_scop *scop;
1516 has_else = tree->type == pet_tree_if_else;
1518 test_index = pet_create_test_index(state->ctx, state->n_test++);
1519 state->n_stmt = stmt_id;
1520 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1521 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1522 pet_tree_get_loc(tree), pc);
1523 state->n_stmt = save_n_stmt;
1524 scop = pet_scop_add_boolean_array(scop,
1525 isl_multi_pw_aff_copy(test_index), state->int_size);
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, 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,
1565 struct pet_scop *scop_then, struct pet_scop *scop_else,
1566 struct pet_state *state)
1568 int has_else;
1569 isl_ctx *ctx;
1570 isl_set *set;
1571 isl_set *valid;
1572 struct pet_skip_info skip;
1573 struct pet_scop *scop;
1575 ctx = pet_tree_get_ctx(tree);
1577 has_else = tree->type == pet_tree_if_else;
1579 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1580 pet_skip_info_if_extract_cond(&skip, cond, state);
1582 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1583 set = isl_pw_aff_non_zero_set(cond);
1584 scop = pet_scop_restrict(scop_then, isl_set_params(isl_set_copy(set)));
1586 if (has_else) {
1587 set = isl_set_subtract(isl_set_copy(valid), set);
1588 scop_else = pet_scop_restrict(scop_else, isl_set_params(set));
1589 scop = pet_scop_add_par(ctx, scop, scop_else);
1590 } else
1591 isl_set_free(set);
1592 scop = pet_scop_resolve_nested(scop);
1593 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
1595 if (pet_skip_info_has_skip(&skip))
1596 scop = pet_scop_prefix(scop, 0);
1597 scop = pet_skip_info_if_add(&skip, scop, 1);
1599 return scop;
1602 /* Construct a pet_scop for an if statement within the context "pc".
1604 * If the condition fits the pattern of a conditional assignment,
1605 * then it is handled by scop_from_conditional_assignment.
1607 * Otherwise, we check if the condition is affine.
1608 * If so, we construct the scop in scop_from_affine_if.
1609 * Otherwise, we construct the scop in scop_from_non_affine_if.
1611 * We allow the condition to be dynamic, i.e., to refer to
1612 * scalars or array elements that may be written to outside
1613 * of the given if statement. These nested accesses are then represented
1614 * as output dimensions in the wrapping iteration domain.
1615 * If it is also written _inside_ the then or else branch, then
1616 * we treat the condition as non-affine.
1617 * As explained in extract_non_affine_if, this will introduce
1618 * an extra statement.
1619 * For aesthetic reasons, we want this statement to have a statement
1620 * number that is lower than those of the then and else branches.
1621 * In order to evaluate if we will need such a statement, however, we
1622 * first construct scops for the then and else branches.
1623 * We therefore reserve a statement number if we might have to
1624 * introduce such an extra statement.
1626 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1627 __isl_keep pet_context *pc, struct pet_state *state)
1629 int has_else;
1630 int stmt_id;
1631 isl_pw_aff *cond;
1632 pet_expr *cond_expr;
1633 struct pet_scop *scop_then, *scop_else = NULL;
1634 pet_context *pc_nested;
1636 if (!tree)
1637 return NULL;
1639 has_else = tree->type == pet_tree_if_else;
1641 pc = pet_context_copy(pc);
1642 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1643 if (has_else)
1644 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1646 if (is_conditional_assignment(tree, pc))
1647 return scop_from_conditional_assignment(tree, pc, state);
1649 cond_expr = pet_expr_copy(tree->u.i.cond);
1650 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1651 pc_nested = pet_context_copy(pc);
1652 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1653 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1654 pet_context_free(pc_nested);
1655 pet_expr_free(cond_expr);
1657 if (!cond) {
1658 pet_context_free(pc);
1659 return NULL;
1662 if (isl_pw_aff_involves_nan(cond) || pet_nested_any_in_pw_aff(cond))
1663 stmt_id = state->n_stmt++;
1665 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1666 if (has_else)
1667 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1669 if (isl_pw_aff_involves_nan(cond)) {
1670 isl_pw_aff_free(cond);
1671 return scop_from_non_affine_if(tree, scop_then, scop_else,
1672 stmt_id, pc, state);
1675 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1676 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1677 isl_pw_aff_free(cond);
1678 return scop_from_non_affine_if(tree, scop_then, scop_else,
1679 stmt_id, pc, state);
1682 pet_context_free(pc);
1683 return scop_from_affine_if(tree, cond, scop_then, scop_else, state);
1686 /* Return a one-dimensional multi piecewise affine expression that is equal
1687 * to the constant 1 and is defined over a zero-dimensional domain.
1689 static __isl_give isl_multi_pw_aff *one_mpa(isl_ctx *ctx)
1691 isl_space *space;
1692 isl_local_space *ls;
1693 isl_aff *aff;
1695 space = isl_space_set_alloc(ctx, 0, 0);
1696 ls = isl_local_space_from_space(space);
1697 aff = isl_aff_zero_on_domain(ls);
1698 aff = isl_aff_set_constant_si(aff, 1);
1700 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1703 /* Construct a pet_scop for a continue statement.
1705 * We simply create an empty scop with a universal pet_skip_now
1706 * skip condition. This skip condition will then be taken into
1707 * account by the enclosing loop construct, possibly after
1708 * being incorporated into outer skip conditions.
1710 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree)
1712 struct pet_scop *scop;
1713 isl_ctx *ctx;
1715 ctx = pet_tree_get_ctx(tree);
1716 scop = pet_scop_empty(ctx);
1717 if (!scop)
1718 return NULL;
1720 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(ctx));
1722 return scop;
1725 /* Construct a pet_scop for a break statement.
1727 * We simply create an empty scop with both a universal pet_skip_now
1728 * skip condition and a universal pet_skip_later skip condition.
1729 * These skip conditions will then be taken into
1730 * account by the enclosing loop construct, possibly after
1731 * being incorporated into outer skip conditions.
1733 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree)
1735 struct pet_scop *scop;
1736 isl_ctx *ctx;
1737 isl_multi_pw_aff *skip;
1739 ctx = pet_tree_get_ctx(tree);
1740 scop = pet_scop_empty(ctx);
1741 if (!scop)
1742 return NULL;
1744 skip = one_mpa(ctx);
1745 scop = pet_scop_set_skip(scop, pet_skip_now,
1746 isl_multi_pw_aff_copy(skip));
1747 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1749 return scop;
1752 /* Extract a clone of the kill statement in "scop".
1753 * "scop" is expected to have been created from a DeclStmt
1754 * and should have the kill as its first statement.
1756 static struct pet_scop *extract_kill(isl_ctx *ctx, struct pet_scop *scop,
1757 struct pet_state *state)
1759 pet_expr *kill;
1760 struct pet_stmt *stmt;
1761 isl_multi_pw_aff *index;
1762 isl_map *access;
1763 pet_expr *arg;
1765 if (!scop)
1766 return NULL;
1767 if (scop->n_stmt < 1)
1768 isl_die(ctx, isl_error_internal,
1769 "expecting at least one statement", return NULL);
1770 stmt = scop->stmts[0];
1771 if (!pet_stmt_is_kill(stmt))
1772 isl_die(ctx, isl_error_internal,
1773 "expecting kill statement", return NULL);
1775 arg = pet_expr_get_arg(stmt->body, 0);
1776 index = pet_expr_access_get_index(arg);
1777 access = pet_expr_access_get_access(arg);
1778 pet_expr_free(arg);
1779 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
1780 access = isl_map_reset_tuple_id(access, isl_dim_in);
1781 kill = pet_expr_kill_from_access_and_index(access, index);
1782 stmt = pet_stmt_from_pet_expr(pet_loc_copy(stmt->loc),
1783 NULL, state->n_stmt++, kill);
1784 return pet_scop_from_pet_stmt(ctx, stmt);
1787 /* Mark all arrays in "scop" as being exposed.
1789 static struct pet_scop *mark_exposed(struct pet_scop *scop)
1791 int i;
1793 if (!scop)
1794 return NULL;
1795 for (i = 0; i < scop->n_array; ++i)
1796 scop->arrays[i]->exposed = 1;
1797 return scop;
1800 /* Try and construct a pet_scop corresponding to (part of)
1801 * a sequence of statements within the context "pc".
1803 * After extracting a statement, we update "pc"
1804 * based on the top-level assignments in the statement
1805 * so that we can exploit them in subsequent statements in the same block.
1807 * If there are any breaks or continues in the individual statements,
1808 * then we may have to compute a new skip condition.
1809 * This is handled using a pet_skip_info object.
1810 * On initialization, the object checks if skip conditions need
1811 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1812 * adds them in pet_skip_info_seq_add.
1814 * If "block" is set, then we need to insert kill statements at
1815 * the end of the block for any array that has been declared by
1816 * one of the statements in the sequence. Each of these declarations
1817 * results in the construction of a kill statement at the place
1818 * of the declaration, so we simply collect duplicates of
1819 * those kill statements and append these duplicates to the constructed scop.
1821 * If "block" is not set, then any array declared by one of the statements
1822 * in the sequence is marked as being exposed.
1824 * If autodetect is set, then we allow the extraction of only a subrange
1825 * of the sequence of statements. However, if there is at least one statement
1826 * for which we could not construct a scop and the final range contains
1827 * either no statements or at least one kill, then we discard the entire
1828 * range.
1830 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
1831 __isl_keep pet_context *pc, struct pet_state *state)
1833 int i;
1834 isl_ctx *ctx;
1835 struct pet_scop *scop, *kills;
1837 ctx = pet_tree_get_ctx(tree);
1839 pc = pet_context_copy(pc);
1840 scop = pet_scop_empty(ctx);
1841 kills = pet_scop_empty(ctx);
1842 for (i = 0; i < tree->u.b.n; ++i) {
1843 struct pet_scop *scop_i;
1845 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
1846 pc = scop_handle_writes(scop_i, pc);
1847 struct pet_skip_info skip;
1848 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
1849 pet_skip_info_seq_extract(&skip, state);
1850 if (pet_skip_info_has_skip(&skip))
1851 scop_i = pet_scop_prefix(scop_i, 0);
1852 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
1853 if (tree->u.b.block) {
1854 struct pet_scop *kill;
1855 kill = extract_kill(ctx, scop_i, state);
1856 kills = pet_scop_add_par(ctx, kills, kill);
1857 } else
1858 scop_i = mark_exposed(scop_i);
1860 scop_i = pet_scop_prefix(scop_i, i);
1861 scop = pet_scop_add_seq(ctx, scop, scop_i);
1863 scop = pet_skip_info_seq_add(&skip, scop, i);
1865 if (!scop)
1866 break;
1869 kills = pet_scop_prefix(kills, tree->u.b.n);
1870 scop = pet_scop_add_seq(ctx, scop, kills);
1872 pet_context_free(pc);
1874 return scop;
1877 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1878 * within the context "pc" by calling the appropriate function
1879 * based on the type of "tree".
1881 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
1882 __isl_keep pet_context *pc, struct pet_state *state)
1884 if (!tree)
1885 return NULL;
1887 switch (tree->type) {
1888 case pet_tree_error:
1889 return NULL;
1890 case pet_tree_block:
1891 return scop_from_block(tree, pc, state);
1892 case pet_tree_break:
1893 return scop_from_break(tree);
1894 case pet_tree_continue:
1895 return scop_from_continue(tree);
1896 case pet_tree_decl:
1897 case pet_tree_decl_init:
1898 return scop_from_decl(tree, pc, state);
1899 case pet_tree_expr:
1900 return scop_from_expr(pet_expr_copy(tree->u.e.expr),
1901 isl_id_copy(tree->label),
1902 state->n_stmt++,
1903 pet_tree_get_loc(tree), pc);
1904 case pet_tree_if:
1905 case pet_tree_if_else:
1906 return scop_from_if(tree, pc, state);
1907 case pet_tree_for:
1908 return scop_from_for(tree, pc, state);
1909 case pet_tree_while:
1910 return scop_from_while(tree, pc, state);
1911 case pet_tree_infinite_loop:
1912 return scop_from_infinite_for(tree, pc, state);
1915 isl_die(tree->ctx, isl_error_internal, "unhandled type",
1916 return NULL);
1919 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1920 * "int_size" is the number of bytes need to represent an integer.
1921 * "extract_array" is a callback that we can use to create a pet_array
1922 * that corresponds to the variable accessed by an expression.
1924 * Initialize the global state, construct a context and then
1925 * construct the pet_scop by recursively visiting the tree.
1927 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
1928 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
1929 __isl_keep pet_context *pc, void *user), void *user,
1930 __isl_keep pet_context *pc)
1932 struct pet_scop *scop;
1933 struct pet_state state = { 0 };
1935 if (!tree)
1936 return NULL;
1938 state.ctx = pet_tree_get_ctx(tree);
1939 state.int_size = int_size;
1940 state.extract_array = extract_array;
1941 state.user = user;
1943 scop = scop_from_tree(tree, pc, &state);
1944 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
1946 pet_tree_free(tree);
1948 return scop;