pet_stmt_from_pet_expr: rename "dim" variable to "space"
[pet.git] / tree2scop.c
blob71d898b9ad3dbde43e473d56aff2669ad0fd737b
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 cond = isl_set_universe(isl_space_set_alloc(state->ctx, 0, 0));
1190 cond = embed(cond, isl_id_copy(id));
1191 valid_cond = isl_set_coalesce(valid_cond);
1192 valid_cond = embed(valid_cond, isl_id_copy(id));
1193 valid_inc = embed(valid_inc, isl_id_copy(id));
1194 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1195 is_virtual = is_unsigned &&
1196 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1198 valid_cond_init = enforce_subset(
1199 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val))),
1200 isl_set_copy(valid_cond));
1201 if (is_one && !is_virtual) {
1202 isl_pw_aff_free(init_val);
1203 pa = pet_expr_extract_comparison(
1204 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1205 tree->u.l.iv, tree->u.l.init, pc);
1206 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1207 valid_init = set_project_out_by_id(valid_init, id);
1208 domain = isl_pw_aff_non_zero_set(pa);
1209 } else {
1210 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1211 domain = strided_domain(isl_id_copy(id), init_val,
1212 isl_val_copy(inc));
1215 domain = embed(domain, isl_id_copy(id));
1216 if (is_virtual) {
1217 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1218 rev_wrap = isl_map_from_aff(isl_aff_copy(wrap));
1219 rev_wrap = isl_map_reverse(rev_wrap);
1220 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1221 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1222 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1224 is_simple = is_simple_bound(cond, inc);
1225 if (!is_simple) {
1226 cond = isl_set_gist(cond, isl_set_copy(domain));
1227 is_simple = is_simple_bound(cond, inc);
1229 if (!is_simple)
1230 cond = valid_for_each_iteration(cond,
1231 isl_set_copy(domain), isl_val_copy(inc));
1232 domain = isl_set_intersect(domain, cond);
1233 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1234 ls = isl_local_space_from_space(isl_set_get_space(domain));
1235 sched = isl_aff_var_on_domain(ls, isl_dim_set, 0);
1236 if (isl_val_is_neg(inc))
1237 sched = isl_aff_neg(sched);
1239 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1240 isl_val_copy(inc));
1241 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1243 if (!is_virtual)
1244 wrap = identity_aff(domain);
1246 if (is_non_affine) {
1247 isl_multi_pw_aff *test_index;
1248 test_index = pet_create_test_index(state->ctx, state->n_test++);
1249 scop_cond = scop_from_non_affine_condition(
1250 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1251 isl_multi_pw_aff_copy(test_index),
1252 pet_tree_get_loc(tree), pc);
1253 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1254 isl_dim_out);
1255 scop_cond = pet_scop_add_boolean_array(scop_cond, test_index,
1256 state->int_size);
1257 scop_cond = pet_scop_prefix(scop_cond, 0);
1258 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1259 isl_aff_copy(sched), isl_aff_copy(wrap),
1260 isl_id_copy(id));
1263 scop = scop_from_tree(tree->u.l.body, pc, state);
1264 has_affine_break = scop &&
1265 pet_scop_has_affine_skip(scop, pet_skip_later);
1266 if (has_affine_break)
1267 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1268 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1269 if (has_var_break)
1270 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1271 if (is_non_affine) {
1272 scop = pet_scop_reset_context(scop);
1273 scop = pet_scop_prefix(scop, 1);
1275 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
1276 scop = pet_scop_resolve_nested(scop);
1277 if (has_affine_break) {
1278 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1279 is_virtual, rev_wrap);
1280 scop = pet_scop_intersect_domain_prefix(scop,
1281 isl_set_copy(domain));
1283 isl_map_free(rev_wrap);
1284 if (has_var_break)
1285 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1286 isl_val_copy(inc));
1287 if (is_non_affine) {
1288 scop = scop_add_while(scop_cond, scop, id_test, domain,
1289 isl_val_copy(inc));
1290 isl_set_free(valid_inc);
1291 } else {
1292 scop = pet_scop_restrict_context(scop, valid_inc);
1293 scop = pet_scop_restrict_context(scop, valid_cond_next);
1294 scop = pet_scop_restrict_context(scop, valid_cond_init);
1295 isl_set_free(domain);
1298 isl_val_free(inc);
1300 scop = pet_scop_restrict_context(scop, isl_set_params(valid_init));
1302 pet_context_free(pc);
1303 return scop;
1306 /* Construct a pet_scop for a for statement within the context of "pc".
1308 * We update the context to reflect the writes to the loop variable and
1309 * the writes inside the body.
1311 * Then we check if the initialization of the for loop
1312 * is a static affine value and the increment is a constant.
1313 * If so, we construct the pet_scop using scop_from_affine_for.
1314 * Otherwise, we treat the for loop as a while loop
1315 * in scop_from_non_affine_for.
1317 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1318 __isl_keep pet_context *pc, struct pet_state *state)
1320 isl_id *iv;
1321 isl_val *inc;
1322 isl_pw_aff *pa_inc, *init_val;
1323 pet_context *pc_init_val;
1325 if (!tree)
1326 return NULL;
1328 iv = pet_expr_access_get_id(tree->u.l.iv);
1329 pc = pet_context_copy(pc);
1330 pc = pet_context_clear_value(pc, iv);
1331 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1333 pc_init_val = pet_context_copy(pc);
1334 pc_init_val = pet_context_mark_unknown(pc_init_val, isl_id_copy(iv));
1335 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1336 pet_context_free(pc_init_val);
1337 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1338 inc = pet_extract_cst(pa_inc);
1339 if (!pa_inc || !init_val || !inc)
1340 goto error;
1341 if (!isl_pw_aff_involves_nan(pa_inc) &&
1342 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1343 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1344 pc, state);
1346 isl_pw_aff_free(pa_inc);
1347 isl_pw_aff_free(init_val);
1348 isl_val_free(inc);
1349 return scop_from_non_affine_for(tree, pc, state);
1350 error:
1351 isl_pw_aff_free(pa_inc);
1352 isl_pw_aff_free(init_val);
1353 isl_val_free(inc);
1354 pet_context_free(pc);
1355 return NULL;
1358 /* Check whether "expr" is an affine constraint within the context "pc".
1360 static int is_affine_condition(__isl_keep pet_expr *expr,
1361 __isl_keep pet_context *pc)
1363 isl_pw_aff *pa;
1364 int is_affine;
1366 pa = pet_expr_extract_affine_condition(expr, pc);
1367 if (!pa)
1368 return -1;
1369 is_affine = !isl_pw_aff_involves_nan(pa);
1370 isl_pw_aff_free(pa);
1372 return is_affine;
1375 /* Check if the given if statement is a conditional assignement
1376 * with a non-affine condition.
1378 * In particular we check if "stmt" is of the form
1380 * if (condition)
1381 * a = f(...);
1382 * else
1383 * a = g(...);
1385 * where the condition is non-affine and a is some array or scalar access.
1387 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1388 __isl_keep pet_context *pc)
1390 int equal;
1391 isl_ctx *ctx;
1392 pet_expr *expr1, *expr2;
1394 ctx = pet_tree_get_ctx(tree);
1395 if (!pet_options_get_detect_conditional_assignment(ctx))
1396 return 0;
1397 if (tree->type != pet_tree_if_else)
1398 return 0;
1399 if (tree->u.i.then_body->type != pet_tree_expr)
1400 return 0;
1401 if (tree->u.i.else_body->type != pet_tree_expr)
1402 return 0;
1403 expr1 = tree->u.i.then_body->u.e.expr;
1404 expr2 = tree->u.i.else_body->u.e.expr;
1405 if (pet_expr_get_type(expr1) != pet_expr_op)
1406 return 0;
1407 if (pet_expr_get_type(expr2) != pet_expr_op)
1408 return 0;
1409 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1410 return 0;
1411 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1412 return 0;
1413 expr1 = pet_expr_get_arg(expr1, 0);
1414 expr2 = pet_expr_get_arg(expr2, 0);
1415 equal = pet_expr_is_equal(expr1, expr2);
1416 pet_expr_free(expr1);
1417 pet_expr_free(expr2);
1418 if (equal < 0 || !equal)
1419 return 0;
1420 if (is_affine_condition(tree->u.i.cond, pc))
1421 return 0;
1423 return 1;
1426 /* Given that "tree" is of the form
1428 * if (condition)
1429 * a = f(...);
1430 * else
1431 * a = g(...);
1433 * where a is some array or scalar access, construct a pet_scop
1434 * corresponding to this conditional assignment within the context "pc".
1436 * The constructed pet_scop then corresponds to the expression
1438 * a = condition ? f(...) : g(...)
1440 * All access relations in f(...) are intersected with condition
1441 * while all access relation in g(...) are intersected with the complement.
1443 static struct pet_scop *scop_from_conditional_assignment(
1444 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1445 struct pet_state *state)
1447 int type_size;
1448 isl_pw_aff *pa;
1449 isl_set *cond, *comp;
1450 isl_multi_pw_aff *index;
1451 pet_expr *expr1, *expr2;
1452 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1453 pet_context *pc_nested;
1454 struct pet_scop *scop;
1456 pe_cond = pet_expr_copy(tree->u.i.cond);
1457 pe_cond = pet_expr_plug_in_args(pe_cond, pc);
1458 pc_nested = pet_context_copy(pc);
1459 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1460 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1461 pet_context_free(pc_nested);
1462 pet_expr_free(pe_cond);
1463 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1464 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1465 index = isl_multi_pw_aff_from_pw_aff(pa);
1467 expr1 = tree->u.i.then_body->u.e.expr;
1468 expr2 = tree->u.i.else_body->u.e.expr;
1470 pe_cond = pet_expr_from_index(index);
1472 pe_then = pet_expr_get_arg(expr1, 1);
1473 pe_then = pet_expr_restrict(pe_then, cond);
1474 pe_else = pet_expr_get_arg(expr2, 1);
1475 pe_else = pet_expr_restrict(pe_else, comp);
1476 pe_write = pet_expr_get_arg(expr1, 0);
1478 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1479 type_size = pet_expr_get_type_size(pe_write);
1480 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1482 scop = scop_from_expr(pe, NULL, state->n_stmt++,
1483 pet_tree_get_loc(tree), pc);
1485 pet_context_free(pc);
1487 return scop;
1490 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1492 * We create a separate statement that writes the result
1493 * of the non-affine condition to a virtual scalar.
1494 * A constraint requiring the value of this virtual scalar to be one
1495 * is added to the iteration domains of the then branch.
1496 * Similarly, a constraint requiring the value of this virtual scalar
1497 * to be zero is added to the iteration domains of the else branch, if any.
1498 * We adjust the schedules to ensure that the virtual scalar is written
1499 * before it is read.
1501 * If there are any breaks or continues in the then and/or else
1502 * branches, then we may have to compute a new skip condition.
1503 * This is handled using a pet_skip_info object.
1504 * On initialization, the object checks if skip conditions need
1505 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1506 * adds them in pet_skip_info_if_add.
1508 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1509 __isl_take pet_context *pc, struct pet_state *state)
1511 int has_else;
1512 isl_multi_pw_aff *test_index;
1513 struct pet_skip_info skip;
1514 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1516 has_else = tree->type == pet_tree_if_else;
1518 test_index = pet_create_test_index(state->ctx, state->n_test++);
1519 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1520 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1521 pet_tree_get_loc(tree), pc);
1522 scop = pet_scop_add_boolean_array(scop,
1523 isl_multi_pw_aff_copy(test_index), state->int_size);
1525 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1526 if (has_else)
1527 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1529 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1530 has_else, 0);
1531 pet_skip_info_if_extract_index(&skip, test_index, state);
1533 scop = pet_scop_prefix(scop, 0);
1534 scop_then = pet_scop_prefix(scop_then, 1);
1535 scop_then = pet_scop_filter(scop_then,
1536 isl_multi_pw_aff_copy(test_index), 1);
1537 if (has_else) {
1538 scop_else = pet_scop_prefix(scop_else, 1);
1539 scop_else = pet_scop_filter(scop_else, test_index, 0);
1540 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1541 } else
1542 isl_multi_pw_aff_free(test_index);
1544 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1546 scop = pet_skip_info_if_add(&skip, scop, 2);
1548 pet_context_free(pc);
1549 return scop;
1552 /* Construct a pet_scop for an affine if statement within the context "pc".
1554 * The condition is added to the iteration domains of the then branch,
1555 * while the opposite of the condition in added to the iteration domains
1556 * of the else branch, if any.
1558 * If there are any breaks or continues in the then and/or else
1559 * branches, then we may have to compute a new skip condition.
1560 * This is handled using a pet_skip_info_if object.
1561 * On initialization, the object checks if skip conditions need
1562 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1563 * adds them in pet_skip_info_if_add.
1565 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1566 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1567 struct pet_state *state)
1569 int has_else;
1570 isl_ctx *ctx;
1571 isl_set *set;
1572 isl_set *valid;
1573 struct pet_skip_info skip;
1574 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1576 ctx = pet_tree_get_ctx(tree);
1578 has_else = tree->type == pet_tree_if_else;
1580 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1581 if (has_else)
1582 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1584 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1585 pet_skip_info_if_extract_cond(&skip, cond, state);
1587 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1588 set = isl_pw_aff_non_zero_set(cond);
1589 scop = pet_scop_restrict(scop_then, isl_set_params(isl_set_copy(set)));
1591 if (has_else) {
1592 set = isl_set_subtract(isl_set_copy(valid), set);
1593 scop_else = pet_scop_restrict(scop_else, isl_set_params(set));
1594 scop = pet_scop_add_par(ctx, scop, scop_else);
1595 } else
1596 isl_set_free(set);
1597 scop = pet_scop_resolve_nested(scop);
1598 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
1600 if (pet_skip_info_has_skip(&skip))
1601 scop = pet_scop_prefix(scop, 0);
1602 scop = pet_skip_info_if_add(&skip, scop, 1);
1604 pet_context_free(pc);
1605 return scop;
1608 /* Construct a pet_scop for an if statement within the context "pc".
1610 * If the condition fits the pattern of a conditional assignment,
1611 * then it is handled by scop_from_conditional_assignment.
1613 * Otherwise, we check if the condition is affine.
1614 * If so, we construct the scop in scop_from_affine_if.
1615 * Otherwise, we construct the scop in scop_from_non_affine_if.
1617 * We allow the condition to be dynamic, i.e., to refer to
1618 * scalars or array elements that may be written to outside
1619 * of the given if statement. These nested accesses are then represented
1620 * as output dimensions in the wrapping iteration domain.
1621 * If it is also written _inside_ the then or else branch, then
1622 * we treat the condition as non-affine.
1623 * As explained in extract_non_affine_if, this will introduce
1624 * an extra statement.
1625 * For aesthetic reasons, we want this statement to have a statement
1626 * number that is lower than those of the then and else branches.
1627 * In order to evaluate if we will need such a statement, however, we
1628 * first construct scops for the then and else branches.
1629 * We therefore reserve a statement number if we might have to
1630 * introduce such an extra statement.
1632 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1633 __isl_keep pet_context *pc, struct pet_state *state)
1635 int has_else;
1636 isl_pw_aff *cond;
1637 pet_expr *cond_expr;
1638 pet_context *pc_nested;
1640 if (!tree)
1641 return NULL;
1643 has_else = tree->type == pet_tree_if_else;
1645 pc = pet_context_copy(pc);
1646 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1647 if (has_else)
1648 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1650 if (is_conditional_assignment(tree, pc))
1651 return scop_from_conditional_assignment(tree, pc, state);
1653 cond_expr = pet_expr_copy(tree->u.i.cond);
1654 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1655 pc_nested = pet_context_copy(pc);
1656 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1657 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1658 pet_context_free(pc_nested);
1659 pet_expr_free(cond_expr);
1661 if (!cond) {
1662 pet_context_free(pc);
1663 return NULL;
1666 if (isl_pw_aff_involves_nan(cond)) {
1667 isl_pw_aff_free(cond);
1668 return scop_from_non_affine_if(tree, pc, state);
1671 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1672 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1673 isl_pw_aff_free(cond);
1674 return scop_from_non_affine_if(tree, pc, state);
1677 return scop_from_affine_if(tree, cond, pc, state);
1680 /* Return a one-dimensional multi piecewise affine expression that is equal
1681 * to the constant 1 and is defined over a zero-dimensional domain.
1683 static __isl_give isl_multi_pw_aff *one_mpa(isl_ctx *ctx)
1685 isl_space *space;
1686 isl_local_space *ls;
1687 isl_aff *aff;
1689 space = isl_space_set_alloc(ctx, 0, 0);
1690 ls = isl_local_space_from_space(space);
1691 aff = isl_aff_zero_on_domain(ls);
1692 aff = isl_aff_set_constant_si(aff, 1);
1694 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1697 /* Construct a pet_scop for a continue statement.
1699 * We simply create an empty scop with a universal pet_skip_now
1700 * skip condition. This skip condition will then be taken into
1701 * account by the enclosing loop construct, possibly after
1702 * being incorporated into outer skip conditions.
1704 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree)
1706 struct pet_scop *scop;
1707 isl_ctx *ctx;
1709 ctx = pet_tree_get_ctx(tree);
1710 scop = pet_scop_empty(ctx);
1711 if (!scop)
1712 return NULL;
1714 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(ctx));
1716 return scop;
1719 /* Construct a pet_scop for a break statement.
1721 * We simply create an empty scop with both a universal pet_skip_now
1722 * skip condition and a universal pet_skip_later skip condition.
1723 * These skip conditions will then be taken into
1724 * account by the enclosing loop construct, possibly after
1725 * being incorporated into outer skip conditions.
1727 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree)
1729 struct pet_scop *scop;
1730 isl_ctx *ctx;
1731 isl_multi_pw_aff *skip;
1733 ctx = pet_tree_get_ctx(tree);
1734 scop = pet_scop_empty(ctx);
1735 if (!scop)
1736 return NULL;
1738 skip = one_mpa(ctx);
1739 scop = pet_scop_set_skip(scop, pet_skip_now,
1740 isl_multi_pw_aff_copy(skip));
1741 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1743 return scop;
1746 /* Extract a clone of the kill statement in "scop".
1747 * "scop" is expected to have been created from a DeclStmt
1748 * and should have the kill as its first statement.
1750 static struct pet_scop *extract_kill(isl_ctx *ctx, struct pet_scop *scop,
1751 struct pet_state *state)
1753 pet_expr *kill;
1754 struct pet_stmt *stmt;
1755 isl_multi_pw_aff *index;
1756 isl_map *access;
1757 pet_expr *arg;
1759 if (!scop)
1760 return NULL;
1761 if (scop->n_stmt < 1)
1762 isl_die(ctx, isl_error_internal,
1763 "expecting at least one statement", return NULL);
1764 stmt = scop->stmts[0];
1765 if (!pet_stmt_is_kill(stmt))
1766 isl_die(ctx, isl_error_internal,
1767 "expecting kill statement", return NULL);
1769 arg = pet_expr_get_arg(stmt->body, 0);
1770 index = pet_expr_access_get_index(arg);
1771 access = pet_expr_access_get_access(arg);
1772 pet_expr_free(arg);
1773 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
1774 access = isl_map_reset_tuple_id(access, isl_dim_in);
1775 kill = pet_expr_kill_from_access_and_index(access, index);
1776 stmt = pet_stmt_from_pet_expr(pet_loc_copy(stmt->loc),
1777 NULL, state->n_stmt++, kill);
1778 return pet_scop_from_pet_stmt(ctx, stmt);
1781 /* Mark all arrays in "scop" as being exposed.
1783 static struct pet_scop *mark_exposed(struct pet_scop *scop)
1785 int i;
1787 if (!scop)
1788 return NULL;
1789 for (i = 0; i < scop->n_array; ++i)
1790 scop->arrays[i]->exposed = 1;
1791 return scop;
1794 /* Try and construct a pet_scop corresponding to (part of)
1795 * a sequence of statements within the context "pc".
1797 * After extracting a statement, we update "pc"
1798 * based on the top-level assignments in the statement
1799 * so that we can exploit them in subsequent statements in the same block.
1801 * If there are any breaks or continues in the individual statements,
1802 * then we may have to compute a new skip condition.
1803 * This is handled using a pet_skip_info object.
1804 * On initialization, the object checks if skip conditions need
1805 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1806 * adds them in pet_skip_info_seq_add.
1808 * If "block" is set, then we need to insert kill statements at
1809 * the end of the block for any array that has been declared by
1810 * one of the statements in the sequence. Each of these declarations
1811 * results in the construction of a kill statement at the place
1812 * of the declaration, so we simply collect duplicates of
1813 * those kill statements and append these duplicates to the constructed scop.
1815 * If "block" is not set, then any array declared by one of the statements
1816 * in the sequence is marked as being exposed.
1818 * If autodetect is set, then we allow the extraction of only a subrange
1819 * of the sequence of statements. However, if there is at least one statement
1820 * for which we could not construct a scop and the final range contains
1821 * either no statements or at least one kill, then we discard the entire
1822 * range.
1824 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
1825 __isl_keep pet_context *pc, struct pet_state *state)
1827 int i;
1828 isl_ctx *ctx;
1829 struct pet_scop *scop, *kills;
1831 ctx = pet_tree_get_ctx(tree);
1833 pc = pet_context_copy(pc);
1834 scop = pet_scop_empty(ctx);
1835 kills = pet_scop_empty(ctx);
1836 for (i = 0; i < tree->u.b.n; ++i) {
1837 struct pet_scop *scop_i;
1839 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
1840 pc = scop_handle_writes(scop_i, pc);
1841 struct pet_skip_info skip;
1842 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
1843 pet_skip_info_seq_extract(&skip, state);
1844 if (pet_skip_info_has_skip(&skip))
1845 scop_i = pet_scop_prefix(scop_i, 0);
1846 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
1847 if (tree->u.b.block) {
1848 struct pet_scop *kill;
1849 kill = extract_kill(ctx, scop_i, state);
1850 kills = pet_scop_add_par(ctx, kills, kill);
1851 } else
1852 scop_i = mark_exposed(scop_i);
1854 scop_i = pet_scop_prefix(scop_i, i);
1855 scop = pet_scop_add_seq(ctx, scop, scop_i);
1857 scop = pet_skip_info_seq_add(&skip, scop, i);
1859 if (!scop)
1860 break;
1863 kills = pet_scop_prefix(kills, tree->u.b.n);
1864 scop = pet_scop_add_seq(ctx, scop, kills);
1866 pet_context_free(pc);
1868 return scop;
1871 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1872 * within the context "pc" by calling the appropriate function
1873 * based on the type of "tree".
1875 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
1876 __isl_keep pet_context *pc, struct pet_state *state)
1878 if (!tree)
1879 return NULL;
1881 switch (tree->type) {
1882 case pet_tree_error:
1883 return NULL;
1884 case pet_tree_block:
1885 return scop_from_block(tree, pc, state);
1886 case pet_tree_break:
1887 return scop_from_break(tree);
1888 case pet_tree_continue:
1889 return scop_from_continue(tree);
1890 case pet_tree_decl:
1891 case pet_tree_decl_init:
1892 return scop_from_decl(tree, pc, state);
1893 case pet_tree_expr:
1894 return scop_from_expr(pet_expr_copy(tree->u.e.expr),
1895 isl_id_copy(tree->label),
1896 state->n_stmt++,
1897 pet_tree_get_loc(tree), pc);
1898 case pet_tree_if:
1899 case pet_tree_if_else:
1900 return scop_from_if(tree, pc, state);
1901 case pet_tree_for:
1902 return scop_from_for(tree, pc, state);
1903 case pet_tree_while:
1904 return scop_from_while(tree, pc, state);
1905 case pet_tree_infinite_loop:
1906 return scop_from_infinite_for(tree, pc, state);
1909 isl_die(tree->ctx, isl_error_internal, "unhandled type",
1910 return NULL);
1913 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1914 * "int_size" is the number of bytes need to represent an integer.
1915 * "extract_array" is a callback that we can use to create a pet_array
1916 * that corresponds to the variable accessed by an expression.
1918 * Initialize the global state, construct a context and then
1919 * construct the pet_scop by recursively visiting the tree.
1921 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
1922 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
1923 __isl_keep pet_context *pc, void *user), void *user,
1924 __isl_keep pet_context *pc)
1926 struct pet_scop *scop;
1927 struct pet_state state = { 0 };
1929 if (!tree)
1930 return NULL;
1932 state.ctx = pet_tree_get_ctx(tree);
1933 state.int_size = int_size;
1934 state.extract_array = extract_array;
1935 state.user = user;
1937 scop = scop_from_tree(tree, pc, &state);
1938 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
1940 pet_tree_free(tree);
1942 return scop;