tree2scop.c: scop_from_non_affine_while: delay construction of body pet_scop
[pet.git] / tree2scop.c
blob6350cb85eef3723dcb997bca323ac8f715380b5a
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 * "test_nr" is the sequence number of the virtual test variable that contains
576 * the result of the condition and "stmt_nr" is the sequence number
577 * of the statement that evaluates the condition.
578 * If "expr_inc" is not NULL, then a scop for evaluating this expression
579 * is added at the end of the body,
580 * after replacing any skip conditions resulting from continue statements
581 * by the skip conditions resulting from break statements (if any).
583 * The schedule is adjusted to reflect that the condition is evaluated
584 * before the body is executed and the body is filtered to depend
585 * on the result of the condition evaluating to true on all iterations
586 * up to the current iteration, while the evaluation of the condition itself
587 * is filtered to depend on the result of the condition evaluating to true
588 * on all previous iterations.
589 * The context of the scop representing the body is dropped
590 * because we don't know how many times the body will be executed,
591 * if at all.
593 * If the body contains any break, then it is taken into
594 * account in apply_affine_break (if the skip condition is affine)
595 * or in scop_add_break (if the skip condition is not affine).
597 * Note that in case of an affine skip condition,
598 * since we are dealing with a loop without loop iterator,
599 * the skip condition cannot refer to the current loop iterator and
600 * so effectively, the iteration domain is of the form
602 * { [0]; [t] : t >= 1 and not skip }
604 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
605 int test_nr, int stmt_nr, __isl_take pet_loc *loc,
606 __isl_keep pet_tree *tree_body, __isl_take pet_expr *expr_inc,
607 __isl_take pet_context *pc, struct pet_state *state)
609 isl_ctx *ctx;
610 isl_id *id, *id_test, *id_break_test;
611 isl_multi_pw_aff *test_index;
612 isl_set *domain;
613 isl_set *skip;
614 isl_aff *ident;
615 struct pet_scop *scop, *scop_body;
616 int has_affine_break;
617 int has_var_break;
619 ctx = state->ctx;
620 test_index = pet_create_test_index(ctx, test_nr);
621 scop = scop_from_non_affine_condition(cond, stmt_nr,
622 isl_multi_pw_aff_copy(test_index),
623 pet_loc_copy(loc), pc);
624 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
625 scop = pet_scop_add_boolean_array(scop, test_index, state->int_size);
627 id = isl_id_alloc(ctx, "t", NULL);
628 domain = infinite_domain(isl_id_copy(id));
629 ident = identity_aff(domain);
631 scop_body = scop_from_tree(tree_body, pc, state);
633 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
634 if (has_affine_break)
635 skip = pet_scop_get_affine_skip_domain(scop_body,
636 pet_skip_later);
637 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
638 if (has_var_break)
639 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
641 scop = pet_scop_prefix(scop, 0);
642 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(ident),
643 isl_aff_copy(ident), isl_id_copy(id));
644 scop_body = pet_scop_reset_context(scop_body);
645 scop_body = pet_scop_prefix(scop_body, 1);
646 if (expr_inc) {
647 struct pet_scop *scop_inc;
648 scop_inc = scop_from_expr(expr_inc, NULL, state->n_stmt++,
649 loc, pc);
650 scop_inc = pet_scop_prefix(scop_inc, 2);
651 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
652 isl_multi_pw_aff *skip;
653 skip = pet_scop_get_skip(scop_body, pet_skip_later);
654 scop_body = pet_scop_set_skip(scop_body,
655 pet_skip_now, skip);
656 } else
657 pet_scop_reset_skip(scop_body, pet_skip_now);
658 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
659 } else
660 pet_loc_free(loc);
661 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain),
662 isl_aff_copy(ident), ident, id);
664 if (has_affine_break) {
665 domain = apply_affine_break(domain, skip, 1, 0, NULL);
666 scop = pet_scop_intersect_domain_prefix(scop,
667 isl_set_copy(domain));
668 scop_body = pet_scop_intersect_domain_prefix(scop_body,
669 isl_set_copy(domain));
671 if (has_var_break) {
672 scop = scop_add_break(scop, isl_id_copy(id_break_test),
673 isl_set_copy(domain), isl_val_one(ctx));
674 scop_body = scop_add_break(scop_body, id_break_test,
675 isl_set_copy(domain), isl_val_one(ctx));
677 scop = scop_add_while(scop, scop_body, id_test, domain,
678 isl_val_one(ctx));
680 pet_context_free(pc);
681 return scop;
684 /* Check if the while loop is of the form
686 * while (affine expression)
687 * body
689 * If so, call scop_from_affine_while to construct a scop.
691 * Otherwise, pass control to scop_from_non_affine_while.
693 * "pc" is the context in which the affine expressions in the scop are created.
695 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
696 __isl_keep pet_context *pc, struct pet_state *state)
698 pet_expr *cond_expr;
699 int test_nr, stmt_nr;
700 isl_pw_aff *pa;
702 if (!tree)
703 return NULL;
705 pc = pet_context_copy(pc);
706 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
708 cond_expr = pet_expr_copy(tree->u.l.cond);
709 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
710 pa = pet_expr_extract_affine_condition(cond_expr, pc);
711 pet_expr_free(cond_expr);
713 if (!pa)
714 goto error;
716 if (!isl_pw_aff_involves_nan(pa))
717 return scop_from_affine_while(tree, pa, pc, state);
718 isl_pw_aff_free(pa);
719 test_nr = state->n_test++;
720 stmt_nr = state->n_stmt++;
721 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
722 test_nr, stmt_nr, pet_tree_get_loc(tree),
723 tree->u.l.body, NULL, pc, state);
724 error:
725 pet_context_free(pc);
726 return NULL;
729 /* Check whether "cond" expresses a simple loop bound
730 * on the only set dimension.
731 * In particular, if "up" is set then "cond" should contain only
732 * upper bounds on the set dimension.
733 * Otherwise, it should contain only lower bounds.
735 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
737 if (isl_val_is_pos(inc))
738 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, 0);
739 else
740 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, 0);
743 /* Extend a condition on a given iteration of a loop to one that
744 * imposes the same condition on all previous iterations.
745 * "domain" expresses the lower [upper] bound on the iterations
746 * when inc is positive [negative].
748 * In particular, we construct the condition (when inc is positive)
750 * forall i' : (domain(i') and i' <= i) => cond(i')
752 * which is equivalent to
754 * not exists i' : domain(i') and i' <= i and not cond(i')
756 * We construct this set by negating cond, applying a map
758 * { [i'] -> [i] : domain(i') and i' <= i }
760 * and then negating the result again.
762 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
763 __isl_take isl_set *domain, __isl_take isl_val *inc)
765 isl_map *previous_to_this;
767 if (isl_val_is_pos(inc))
768 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
769 else
770 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
772 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
774 cond = isl_set_complement(cond);
775 cond = isl_set_apply(cond, previous_to_this);
776 cond = isl_set_complement(cond);
778 isl_val_free(inc);
780 return cond;
783 /* Construct a domain of the form
785 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
787 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
788 __isl_take isl_pw_aff *init, __isl_take isl_val *inc)
790 isl_aff *aff;
791 isl_space *dim;
792 isl_set *set;
794 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
795 dim = isl_pw_aff_get_domain_space(init);
796 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
797 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, 0, inc);
798 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
800 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
801 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
802 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
803 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
805 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
807 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
809 return isl_set_params(set);
812 /* Assuming "cond" represents a bound on a loop where the loop
813 * iterator "iv" is incremented (or decremented) by one, check if wrapping
814 * is possible.
816 * Under the given assumptions, wrapping is only possible if "cond" allows
817 * for the last value before wrapping, i.e., 2^width - 1 in case of an
818 * increasing iterator and 0 in case of a decreasing iterator.
820 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
821 __isl_keep isl_val *inc)
823 int cw;
824 isl_ctx *ctx;
825 isl_val *limit;
826 isl_set *test;
828 test = isl_set_copy(cond);
830 ctx = isl_set_get_ctx(test);
831 if (isl_val_is_neg(inc))
832 limit = isl_val_zero(ctx);
833 else {
834 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
835 limit = isl_val_2exp(limit);
836 limit = isl_val_sub_ui(limit, 1);
839 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
840 cw = !isl_set_is_empty(test);
841 isl_set_free(test);
843 return cw;
846 /* Given a one-dimensional space, construct the following affine expression
847 * on this space
849 * { [v] -> [v mod 2^width] }
851 * where width is the number of bits used to represent the values
852 * of the unsigned variable "iv".
854 static __isl_give isl_aff *compute_wrapping(__isl_take isl_space *dim,
855 __isl_keep pet_expr *iv)
857 isl_ctx *ctx;
858 isl_val *mod;
859 isl_aff *aff;
861 ctx = isl_space_get_ctx(dim);
862 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
863 mod = isl_val_2exp(mod);
865 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
866 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
867 aff = isl_aff_mod_val(aff, mod);
869 return aff;
872 /* Project out the parameter "id" from "set".
874 static __isl_give isl_set *set_project_out_by_id(__isl_take isl_set *set,
875 __isl_keep isl_id *id)
877 int pos;
879 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
880 if (pos >= 0)
881 set = isl_set_project_out(set, isl_dim_param, pos, 1);
883 return set;
886 /* Compute the set of parameters for which "set1" is a subset of "set2".
888 * set1 is a subset of set2 if
890 * forall i in set1 : i in set2
892 * or
894 * not exists i in set1 and i not in set2
896 * i.e.,
898 * not exists i in set1 \ set2
900 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
901 __isl_take isl_set *set2)
903 return isl_set_complement(isl_set_params(isl_set_subtract(set1, set2)));
906 /* Compute the set of parameter values for which "cond" holds
907 * on the next iteration for each element of "dom".
909 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
910 * and then compute the set of parameters for which the result is a subset
911 * of "cond".
913 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
914 __isl_take isl_set *dom, __isl_take isl_val *inc)
916 isl_space *space;
917 isl_aff *aff;
918 isl_map *next;
920 space = isl_set_get_space(dom);
921 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
922 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
923 aff = isl_aff_add_constant_val(aff, inc);
924 next = isl_map_from_basic_map(isl_basic_map_from_aff(aff));
926 dom = isl_set_apply(dom, next);
928 return enforce_subset(dom, cond);
931 /* Extract the for loop "tree" as a while loop within the context "pc".
933 * That is, the for loop has the form
935 * for (iv = init; cond; iv += inc)
936 * body;
938 * and is treated as
940 * iv = init;
941 * while (cond) {
942 * body;
943 * iv += inc;
946 * except that the skips resulting from any continue statements
947 * in body do not apply to the increment, but are replaced by the skips
948 * resulting from break statements.
950 * If the loop iterator is declared in the for loop, then it is killed before
951 * and after the loop.
953 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
954 __isl_take pet_context *pc, struct pet_state *state)
956 int declared;
957 int test_nr, stmt_nr;
958 isl_id *iv;
959 pet_expr *expr_iv, *init, *inc;
960 struct pet_scop *scop_init, *scop;
961 int type_size;
962 struct pet_array *array;
963 struct pet_scop *scop_kill;
965 iv = pet_expr_access_get_id(tree->u.l.iv);
966 pc = pet_context_mark_assigned(pc, iv);
968 declared = tree->u.l.declared;
970 expr_iv = pet_expr_copy(tree->u.l.iv);
971 type_size = pet_expr_get_type_size(expr_iv);
972 init = pet_expr_copy(tree->u.l.init);
973 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
974 scop_init = scop_from_expr(init, NULL, state->n_stmt++,
975 pet_tree_get_loc(tree), pc);
976 scop_init = pet_scop_prefix(scop_init, declared);
978 test_nr = state->n_test++;
979 stmt_nr = state->n_stmt++;
981 expr_iv = pet_expr_copy(tree->u.l.iv);
982 type_size = pet_expr_get_type_size(expr_iv);
983 inc = pet_expr_copy(tree->u.l.inc);
984 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
986 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
987 test_nr, stmt_nr, pet_tree_get_loc(tree),
988 tree->u.l.body, inc, pet_context_copy(pc), state);
990 scop = pet_scop_prefix(scop, declared + 1);
991 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
993 if (!declared) {
994 pet_context_free(pc);
995 return scop;
998 array = extract_array(tree->u.l.iv, pc, state);
999 if (array)
1000 array->declared = 1;
1001 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
1002 scop_kill = pet_scop_prefix(scop_kill, 0);
1003 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1004 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
1005 scop_kill = pet_scop_add_array(scop_kill, array);
1006 scop_kill = pet_scop_prefix(scop_kill, 3);
1007 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1009 pet_context_free(pc);
1010 return scop;
1013 /* Given an access expression "expr", is the variable accessed by
1014 * "expr" assigned anywhere inside "tree"?
1016 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1018 int assigned = 0;
1019 isl_id *id;
1021 id = pet_expr_access_get_id(expr);
1022 assigned = pet_tree_writes(tree, id);
1023 isl_id_free(id);
1025 return assigned;
1028 /* Are all nested access parameters in "pa" allowed given "tree".
1029 * In particular, is none of them written by anywhere inside "tree".
1031 * If "tree" has any continue nodes in the current loop level,
1032 * then no nested access parameters are allowed.
1033 * In particular, if there is any nested access in a guard
1034 * for a piece of code containing a "continue", then we want to introduce
1035 * a separate statement for evaluating this guard so that we can express
1036 * that the result is false for all previous iterations.
1038 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1039 __isl_keep pet_tree *tree)
1041 int i, nparam;
1043 if (!tree)
1044 return -1;
1046 if (!pet_nested_any_in_pw_aff(pa))
1047 return 1;
1049 if (pet_tree_has_continue(tree))
1050 return 0;
1052 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1053 for (i = 0; i < nparam; ++i) {
1054 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1055 pet_expr *expr;
1056 int allowed;
1058 if (!pet_nested_in_id(id)) {
1059 isl_id_free(id);
1060 continue;
1063 expr = pet_nested_extract_expr(id);
1064 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1065 !is_assigned(expr, tree);
1067 pet_expr_free(expr);
1068 isl_id_free(id);
1070 if (!allowed)
1071 return 0;
1074 return 1;
1077 /* Construct a pet_scop for a for tree with static affine initialization
1078 * and constant increment within the context "pc".
1080 * The condition is allowed to contain nested accesses, provided
1081 * they are not being written to inside the body of the loop.
1082 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1083 * essentially treated as a while loop, with iteration domain
1084 * { [i] : i >= init }.
1086 * We extract a pet_scop for the body and then embed it in a loop with
1087 * iteration domain and schedule
1089 * { [i] : i >= init and condition' }
1090 * { [i] -> [i] }
1092 * or
1094 * { [i] : i <= init and condition' }
1095 * { [i] -> [-i] }
1097 * Where condition' is equal to condition if the latter is
1098 * a simple upper [lower] bound and a condition that is extended
1099 * to apply to all previous iterations otherwise.
1101 * If the condition is non-affine, then we drop the condition from the
1102 * iteration domain and instead create a separate statement
1103 * for evaluating the condition. The body is then filtered to depend
1104 * on the result of the condition evaluating to true on all iterations
1105 * up to the current iteration, while the evaluation the condition itself
1106 * is filtered to depend on the result of the condition evaluating to true
1107 * on all previous iterations.
1108 * The context of the scop representing the body is dropped
1109 * because we don't know how many times the body will be executed,
1110 * if at all.
1112 * If the stride of the loop is not 1, then "i >= init" is replaced by
1114 * (exists a: i = init + stride * a and a >= 0)
1116 * If the loop iterator i is unsigned, then wrapping may occur.
1117 * We therefore use a virtual iterator instead that does not wrap.
1118 * However, the condition in the code applies
1119 * to the wrapped value, so we need to change condition(i)
1120 * into condition([i % 2^width]). Similarly, we replace all accesses
1121 * to the original iterator by the wrapping of the virtual iterator.
1122 * Note that there may be no need to perform this final wrapping
1123 * if the loop condition (after wrapping) satisfies certain conditions.
1124 * However, the is_simple_bound condition is not enough since it doesn't
1125 * check if there even is an upper bound.
1127 * Wrapping on unsigned iterators can be avoided entirely if
1128 * loop condition is simple, the loop iterator is incremented
1129 * [decremented] by one and the last value before wrapping cannot
1130 * possibly satisfy the loop condition.
1132 * Valid parameters for a for loop are those for which the initial
1133 * value itself, the increment on each domain iteration and
1134 * the condition on both the initial value and
1135 * the result of incrementing the iterator for each iteration of the domain
1136 * can be evaluated.
1137 * If the loop condition is non-affine, then we only consider validity
1138 * of the initial value.
1140 * If the body contains any break, then we keep track of it in "skip"
1141 * (if the skip condition is affine) or it is handled in scop_add_break
1142 * (if the skip condition is not affine).
1143 * Note that the affine break condition needs to be considered with
1144 * respect to previous iterations in the virtual domain (if any).
1146 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1147 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1148 __isl_take isl_val *inc, __isl_take pet_context *pc,
1149 struct pet_state *state)
1151 isl_local_space *ls;
1152 isl_set *domain;
1153 isl_aff *sched;
1154 isl_set *cond = NULL;
1155 isl_set *skip = NULL;
1156 isl_id *id, *id_test = NULL, *id_break_test;
1157 struct pet_scop *scop, *scop_cond = NULL;
1158 int is_one;
1159 int is_unsigned;
1160 int is_simple;
1161 int is_virtual;
1162 int is_non_affine;
1163 int has_affine_break;
1164 int has_var_break;
1165 isl_map *rev_wrap = NULL;
1166 isl_aff *wrap = NULL;
1167 isl_pw_aff *pa;
1168 isl_set *valid_init;
1169 isl_set *valid_cond;
1170 isl_set *valid_cond_init;
1171 isl_set *valid_cond_next;
1172 isl_set *valid_inc;
1173 pet_expr *cond_expr;
1174 pet_context *pc_nested;
1176 id = pet_expr_access_get_id(tree->u.l.iv);
1178 cond_expr = pet_expr_copy(tree->u.l.cond);
1179 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1180 pc_nested = pet_context_copy(pc);
1181 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1182 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1183 pet_context_free(pc_nested);
1184 pet_expr_free(cond_expr);
1186 valid_inc = isl_pw_aff_domain(pa_inc);
1188 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1190 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1191 !is_nested_allowed(pa, tree->u.l.body);
1192 if (is_non_affine)
1193 pa = isl_pw_aff_free(pa);
1195 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1196 cond = isl_pw_aff_non_zero_set(pa);
1197 if (is_non_affine) {
1198 isl_multi_pw_aff *test_index;
1199 test_index = pet_create_test_index(state->ctx, state->n_test++);
1200 scop_cond = scop_from_non_affine_condition(
1201 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1202 isl_multi_pw_aff_copy(test_index),
1203 pet_tree_get_loc(tree), pc);
1204 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1205 isl_dim_out);
1206 scop_cond = pet_scop_add_boolean_array(scop_cond, test_index,
1207 state->int_size);
1208 scop_cond = pet_scop_prefix(scop_cond, 0);
1209 cond = isl_set_universe(isl_space_set_alloc(state->ctx, 0, 0));
1212 cond = embed(cond, isl_id_copy(id));
1213 valid_cond = isl_set_coalesce(valid_cond);
1214 valid_cond = embed(valid_cond, isl_id_copy(id));
1215 valid_inc = embed(valid_inc, isl_id_copy(id));
1216 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1217 is_virtual = is_unsigned &&
1218 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1220 valid_cond_init = enforce_subset(
1221 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val))),
1222 isl_set_copy(valid_cond));
1223 if (is_one && !is_virtual) {
1224 isl_pw_aff_free(init_val);
1225 pa = pet_expr_extract_comparison(
1226 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1227 tree->u.l.iv, tree->u.l.init, pc);
1228 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1229 valid_init = set_project_out_by_id(valid_init, id);
1230 domain = isl_pw_aff_non_zero_set(pa);
1231 } else {
1232 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1233 domain = strided_domain(isl_id_copy(id), init_val,
1234 isl_val_copy(inc));
1237 domain = embed(domain, isl_id_copy(id));
1238 if (is_virtual) {
1239 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1240 rev_wrap = isl_map_from_aff(isl_aff_copy(wrap));
1241 rev_wrap = isl_map_reverse(rev_wrap);
1242 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1243 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1244 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1246 is_simple = is_simple_bound(cond, inc);
1247 if (!is_simple) {
1248 cond = isl_set_gist(cond, isl_set_copy(domain));
1249 is_simple = is_simple_bound(cond, inc);
1251 if (!is_simple)
1252 cond = valid_for_each_iteration(cond,
1253 isl_set_copy(domain), isl_val_copy(inc));
1254 domain = isl_set_intersect(domain, cond);
1255 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1256 ls = isl_local_space_from_space(isl_set_get_space(domain));
1257 sched = isl_aff_var_on_domain(ls, isl_dim_set, 0);
1258 if (isl_val_is_neg(inc))
1259 sched = isl_aff_neg(sched);
1261 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1262 isl_val_copy(inc));
1263 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1265 if (!is_virtual)
1266 wrap = identity_aff(domain);
1268 scop = scop_from_tree(tree->u.l.body, pc, state);
1270 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1271 isl_aff_copy(sched), isl_aff_copy(wrap), isl_id_copy(id));
1272 has_affine_break = scop &&
1273 pet_scop_has_affine_skip(scop, pet_skip_later);
1274 if (has_affine_break)
1275 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1276 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1277 if (has_var_break)
1278 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1279 if (is_non_affine) {
1280 scop = pet_scop_reset_context(scop);
1281 scop = pet_scop_prefix(scop, 1);
1283 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
1284 scop = pet_scop_resolve_nested(scop);
1285 if (has_affine_break) {
1286 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1287 is_virtual, rev_wrap);
1288 scop = pet_scop_intersect_domain_prefix(scop,
1289 isl_set_copy(domain));
1291 isl_map_free(rev_wrap);
1292 if (has_var_break)
1293 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1294 isl_val_copy(inc));
1295 if (is_non_affine) {
1296 scop = scop_add_while(scop_cond, scop, id_test, domain,
1297 isl_val_copy(inc));
1298 isl_set_free(valid_inc);
1299 } else {
1300 scop = pet_scop_restrict_context(scop, valid_inc);
1301 scop = pet_scop_restrict_context(scop, valid_cond_next);
1302 scop = pet_scop_restrict_context(scop, valid_cond_init);
1303 isl_set_free(domain);
1306 isl_val_free(inc);
1308 scop = pet_scop_restrict_context(scop, isl_set_params(valid_init));
1310 pet_context_free(pc);
1311 return scop;
1314 /* Construct a pet_scop for a for statement within the context of "pc".
1316 * We update the context to reflect the writes to the loop variable and
1317 * the writes inside the body.
1319 * Then we check if the initialization of the for loop
1320 * is a static affine value and the increment is a constant.
1321 * If so, we construct the pet_scop using scop_from_affine_for.
1322 * Otherwise, we treat the for loop as a while loop
1323 * in scop_from_non_affine_for.
1325 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1326 __isl_keep pet_context *pc, struct pet_state *state)
1328 isl_id *iv;
1329 isl_val *inc;
1330 isl_pw_aff *pa_inc, *init_val;
1331 pet_context *pc_init_val;
1333 if (!tree)
1334 return NULL;
1336 iv = pet_expr_access_get_id(tree->u.l.iv);
1337 pc = pet_context_copy(pc);
1338 pc = pet_context_clear_value(pc, iv);
1339 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1341 pc_init_val = pet_context_copy(pc);
1342 pc_init_val = pet_context_mark_unknown(pc_init_val, isl_id_copy(iv));
1343 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1344 pet_context_free(pc_init_val);
1345 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1346 inc = pet_extract_cst(pa_inc);
1347 if (!pa_inc || !init_val || !inc)
1348 goto error;
1349 if (!isl_pw_aff_involves_nan(pa_inc) &&
1350 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1351 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1352 pc, state);
1354 isl_pw_aff_free(pa_inc);
1355 isl_pw_aff_free(init_val);
1356 isl_val_free(inc);
1357 return scop_from_non_affine_for(tree, pc, state);
1358 error:
1359 isl_pw_aff_free(pa_inc);
1360 isl_pw_aff_free(init_val);
1361 isl_val_free(inc);
1362 pet_context_free(pc);
1363 return NULL;
1366 /* Check whether "expr" is an affine constraint within the context "pc".
1368 static int is_affine_condition(__isl_keep pet_expr *expr,
1369 __isl_keep pet_context *pc)
1371 isl_pw_aff *pa;
1372 int is_affine;
1374 pa = pet_expr_extract_affine_condition(expr, pc);
1375 if (!pa)
1376 return -1;
1377 is_affine = !isl_pw_aff_involves_nan(pa);
1378 isl_pw_aff_free(pa);
1380 return is_affine;
1383 /* Check if the given if statement is a conditional assignement
1384 * with a non-affine condition.
1386 * In particular we check if "stmt" is of the form
1388 * if (condition)
1389 * a = f(...);
1390 * else
1391 * a = g(...);
1393 * where the condition is non-affine and a is some array or scalar access.
1395 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1396 __isl_keep pet_context *pc)
1398 int equal;
1399 isl_ctx *ctx;
1400 pet_expr *expr1, *expr2;
1402 ctx = pet_tree_get_ctx(tree);
1403 if (!pet_options_get_detect_conditional_assignment(ctx))
1404 return 0;
1405 if (tree->type != pet_tree_if_else)
1406 return 0;
1407 if (tree->u.i.then_body->type != pet_tree_expr)
1408 return 0;
1409 if (tree->u.i.else_body->type != pet_tree_expr)
1410 return 0;
1411 expr1 = tree->u.i.then_body->u.e.expr;
1412 expr2 = tree->u.i.else_body->u.e.expr;
1413 if (pet_expr_get_type(expr1) != pet_expr_op)
1414 return 0;
1415 if (pet_expr_get_type(expr2) != pet_expr_op)
1416 return 0;
1417 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1418 return 0;
1419 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1420 return 0;
1421 expr1 = pet_expr_get_arg(expr1, 0);
1422 expr2 = pet_expr_get_arg(expr2, 0);
1423 equal = pet_expr_is_equal(expr1, expr2);
1424 pet_expr_free(expr1);
1425 pet_expr_free(expr2);
1426 if (equal < 0 || !equal)
1427 return 0;
1428 if (is_affine_condition(tree->u.i.cond, pc))
1429 return 0;
1431 return 1;
1434 /* Given that "tree" is of the form
1436 * if (condition)
1437 * a = f(...);
1438 * else
1439 * a = g(...);
1441 * where a is some array or scalar access, construct a pet_scop
1442 * corresponding to this conditional assignment within the context "pc".
1444 * The constructed pet_scop then corresponds to the expression
1446 * a = condition ? f(...) : g(...)
1448 * All access relations in f(...) are intersected with condition
1449 * while all access relation in g(...) are intersected with the complement.
1451 static struct pet_scop *scop_from_conditional_assignment(
1452 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1453 struct pet_state *state)
1455 int type_size;
1456 isl_pw_aff *pa;
1457 isl_set *cond, *comp;
1458 isl_multi_pw_aff *index;
1459 pet_expr *expr1, *expr2;
1460 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1461 pet_context *pc_nested;
1462 struct pet_scop *scop;
1464 pe_cond = pet_expr_copy(tree->u.i.cond);
1465 pe_cond = pet_expr_plug_in_args(pe_cond, pc);
1466 pc_nested = pet_context_copy(pc);
1467 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1468 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1469 pet_context_free(pc_nested);
1470 pet_expr_free(pe_cond);
1471 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1472 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1473 index = isl_multi_pw_aff_from_pw_aff(pa);
1475 expr1 = tree->u.i.then_body->u.e.expr;
1476 expr2 = tree->u.i.else_body->u.e.expr;
1478 pe_cond = pet_expr_from_index(index);
1480 pe_then = pet_expr_get_arg(expr1, 1);
1481 pe_then = pet_expr_restrict(pe_then, cond);
1482 pe_else = pet_expr_get_arg(expr2, 1);
1483 pe_else = pet_expr_restrict(pe_else, comp);
1484 pe_write = pet_expr_get_arg(expr1, 0);
1486 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1487 type_size = pet_expr_get_type_size(pe_write);
1488 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1490 scop = scop_from_expr(pe, NULL, state->n_stmt++,
1491 pet_tree_get_loc(tree), pc);
1493 pet_context_free(pc);
1495 return scop;
1498 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1500 * We create a separate statement that writes the result
1501 * of the non-affine condition to a virtual scalar.
1502 * A constraint requiring the value of this virtual scalar to be one
1503 * is added to the iteration domains of the then branch.
1504 * Similarly, a constraint requiring the value of this virtual scalar
1505 * to be zero is added to the iteration domains of the else branch, if any.
1506 * We adjust the schedules to ensure that the virtual scalar is written
1507 * before it is read.
1509 * If there are any breaks or continues in the then and/or else
1510 * branches, then we may have to compute a new skip condition.
1511 * This is handled using a pet_skip_info object.
1512 * On initialization, the object checks if skip conditions need
1513 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1514 * adds them in pet_skip_info_if_add.
1516 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1517 struct pet_scop *scop_then, struct pet_scop *scop_else, int stmt_id,
1518 __isl_take pet_context *pc, struct pet_state *state)
1520 int has_else;
1521 int save_n_stmt = state->n_stmt;
1522 isl_multi_pw_aff *test_index;
1523 struct pet_skip_info skip;
1524 struct pet_scop *scop;
1526 has_else = tree->type == pet_tree_if_else;
1528 test_index = pet_create_test_index(state->ctx, state->n_test++);
1529 state->n_stmt = stmt_id;
1530 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1531 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1532 pet_tree_get_loc(tree), pc);
1533 state->n_stmt = save_n_stmt;
1534 scop = pet_scop_add_boolean_array(scop,
1535 isl_multi_pw_aff_copy(test_index), state->int_size);
1537 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1538 has_else, 0);
1539 pet_skip_info_if_extract_index(&skip, test_index, state);
1541 scop = pet_scop_prefix(scop, 0);
1542 scop_then = pet_scop_prefix(scop_then, 1);
1543 scop_then = pet_scop_filter(scop_then,
1544 isl_multi_pw_aff_copy(test_index), 1);
1545 if (has_else) {
1546 scop_else = pet_scop_prefix(scop_else, 1);
1547 scop_else = pet_scop_filter(scop_else, test_index, 0);
1548 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1549 } else
1550 isl_multi_pw_aff_free(test_index);
1552 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1554 scop = pet_skip_info_if_add(&skip, scop, 2);
1556 pet_context_free(pc);
1557 return scop;
1560 /* Construct a pet_scop for an affine if statement within the context "pc".
1562 * The condition is added to the iteration domains of the then branch,
1563 * while the opposite of the condition in added to the iteration domains
1564 * of the else branch, if any.
1566 * If there are any breaks or continues in the then and/or else
1567 * branches, then we may have to compute a new skip condition.
1568 * This is handled using a pet_skip_info_if object.
1569 * On initialization, the object checks if skip conditions need
1570 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1571 * adds them in pet_skip_info_if_add.
1573 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1574 __isl_take isl_pw_aff *cond,
1575 struct pet_scop *scop_then, struct pet_scop *scop_else,
1576 struct pet_state *state)
1578 int has_else;
1579 isl_ctx *ctx;
1580 isl_set *set;
1581 isl_set *valid;
1582 struct pet_skip_info skip;
1583 struct pet_scop *scop;
1585 ctx = pet_tree_get_ctx(tree);
1587 has_else = tree->type == pet_tree_if_else;
1589 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1590 pet_skip_info_if_extract_cond(&skip, cond, state);
1592 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1593 set = isl_pw_aff_non_zero_set(cond);
1594 scop = pet_scop_restrict(scop_then, isl_set_params(isl_set_copy(set)));
1596 if (has_else) {
1597 set = isl_set_subtract(isl_set_copy(valid), set);
1598 scop_else = pet_scop_restrict(scop_else, isl_set_params(set));
1599 scop = pet_scop_add_par(ctx, scop, scop_else);
1600 } else
1601 isl_set_free(set);
1602 scop = pet_scop_resolve_nested(scop);
1603 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
1605 if (pet_skip_info_has_skip(&skip))
1606 scop = pet_scop_prefix(scop, 0);
1607 scop = pet_skip_info_if_add(&skip, scop, 1);
1609 return scop;
1612 /* Construct a pet_scop for an if statement within the context "pc".
1614 * If the condition fits the pattern of a conditional assignment,
1615 * then it is handled by scop_from_conditional_assignment.
1617 * Otherwise, we check if the condition is affine.
1618 * If so, we construct the scop in scop_from_affine_if.
1619 * Otherwise, we construct the scop in scop_from_non_affine_if.
1621 * We allow the condition to be dynamic, i.e., to refer to
1622 * scalars or array elements that may be written to outside
1623 * of the given if statement. These nested accesses are then represented
1624 * as output dimensions in the wrapping iteration domain.
1625 * If it is also written _inside_ the then or else branch, then
1626 * we treat the condition as non-affine.
1627 * As explained in extract_non_affine_if, this will introduce
1628 * an extra statement.
1629 * For aesthetic reasons, we want this statement to have a statement
1630 * number that is lower than those of the then and else branches.
1631 * In order to evaluate if we will need such a statement, however, we
1632 * first construct scops for the then and else branches.
1633 * We therefore reserve a statement number if we might have to
1634 * introduce such an extra statement.
1636 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1637 __isl_keep pet_context *pc, struct pet_state *state)
1639 int has_else;
1640 int stmt_id;
1641 isl_pw_aff *cond;
1642 pet_expr *cond_expr;
1643 struct pet_scop *scop_then, *scop_else = NULL;
1644 pet_context *pc_nested;
1646 if (!tree)
1647 return NULL;
1649 has_else = tree->type == pet_tree_if_else;
1651 pc = pet_context_copy(pc);
1652 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1653 if (has_else)
1654 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1656 if (is_conditional_assignment(tree, pc))
1657 return scop_from_conditional_assignment(tree, pc, state);
1659 cond_expr = pet_expr_copy(tree->u.i.cond);
1660 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1661 pc_nested = pet_context_copy(pc);
1662 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1663 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1664 pet_context_free(pc_nested);
1665 pet_expr_free(cond_expr);
1667 if (!cond) {
1668 pet_context_free(pc);
1669 return NULL;
1672 if (isl_pw_aff_involves_nan(cond) || pet_nested_any_in_pw_aff(cond))
1673 stmt_id = state->n_stmt++;
1675 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1676 if (has_else)
1677 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1679 if (isl_pw_aff_involves_nan(cond)) {
1680 isl_pw_aff_free(cond);
1681 return scop_from_non_affine_if(tree, scop_then, scop_else,
1682 stmt_id, pc, state);
1685 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1686 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1687 isl_pw_aff_free(cond);
1688 return scop_from_non_affine_if(tree, scop_then, scop_else,
1689 stmt_id, pc, state);
1692 pet_context_free(pc);
1693 return scop_from_affine_if(tree, cond, scop_then, scop_else, state);
1696 /* Return a one-dimensional multi piecewise affine expression that is equal
1697 * to the constant 1 and is defined over a zero-dimensional domain.
1699 static __isl_give isl_multi_pw_aff *one_mpa(isl_ctx *ctx)
1701 isl_space *space;
1702 isl_local_space *ls;
1703 isl_aff *aff;
1705 space = isl_space_set_alloc(ctx, 0, 0);
1706 ls = isl_local_space_from_space(space);
1707 aff = isl_aff_zero_on_domain(ls);
1708 aff = isl_aff_set_constant_si(aff, 1);
1710 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1713 /* Construct a pet_scop for a continue statement.
1715 * We simply create an empty scop with a universal pet_skip_now
1716 * skip condition. This skip condition will then be taken into
1717 * account by the enclosing loop construct, possibly after
1718 * being incorporated into outer skip conditions.
1720 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree)
1722 struct pet_scop *scop;
1723 isl_ctx *ctx;
1725 ctx = pet_tree_get_ctx(tree);
1726 scop = pet_scop_empty(ctx);
1727 if (!scop)
1728 return NULL;
1730 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(ctx));
1732 return scop;
1735 /* Construct a pet_scop for a break statement.
1737 * We simply create an empty scop with both a universal pet_skip_now
1738 * skip condition and a universal pet_skip_later skip condition.
1739 * These skip conditions will then be taken into
1740 * account by the enclosing loop construct, possibly after
1741 * being incorporated into outer skip conditions.
1743 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree)
1745 struct pet_scop *scop;
1746 isl_ctx *ctx;
1747 isl_multi_pw_aff *skip;
1749 ctx = pet_tree_get_ctx(tree);
1750 scop = pet_scop_empty(ctx);
1751 if (!scop)
1752 return NULL;
1754 skip = one_mpa(ctx);
1755 scop = pet_scop_set_skip(scop, pet_skip_now,
1756 isl_multi_pw_aff_copy(skip));
1757 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1759 return scop;
1762 /* Extract a clone of the kill statement in "scop".
1763 * "scop" is expected to have been created from a DeclStmt
1764 * and should have the kill as its first statement.
1766 static struct pet_scop *extract_kill(isl_ctx *ctx, struct pet_scop *scop,
1767 struct pet_state *state)
1769 pet_expr *kill;
1770 struct pet_stmt *stmt;
1771 isl_multi_pw_aff *index;
1772 isl_map *access;
1773 pet_expr *arg;
1775 if (!scop)
1776 return NULL;
1777 if (scop->n_stmt < 1)
1778 isl_die(ctx, isl_error_internal,
1779 "expecting at least one statement", return NULL);
1780 stmt = scop->stmts[0];
1781 if (!pet_stmt_is_kill(stmt))
1782 isl_die(ctx, isl_error_internal,
1783 "expecting kill statement", return NULL);
1785 arg = pet_expr_get_arg(stmt->body, 0);
1786 index = pet_expr_access_get_index(arg);
1787 access = pet_expr_access_get_access(arg);
1788 pet_expr_free(arg);
1789 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
1790 access = isl_map_reset_tuple_id(access, isl_dim_in);
1791 kill = pet_expr_kill_from_access_and_index(access, index);
1792 stmt = pet_stmt_from_pet_expr(pet_loc_copy(stmt->loc),
1793 NULL, state->n_stmt++, kill);
1794 return pet_scop_from_pet_stmt(ctx, stmt);
1797 /* Mark all arrays in "scop" as being exposed.
1799 static struct pet_scop *mark_exposed(struct pet_scop *scop)
1801 int i;
1803 if (!scop)
1804 return NULL;
1805 for (i = 0; i < scop->n_array; ++i)
1806 scop->arrays[i]->exposed = 1;
1807 return scop;
1810 /* Try and construct a pet_scop corresponding to (part of)
1811 * a sequence of statements within the context "pc".
1813 * After extracting a statement, we update "pc"
1814 * based on the top-level assignments in the statement
1815 * so that we can exploit them in subsequent statements in the same block.
1817 * If there are any breaks or continues in the individual statements,
1818 * then we may have to compute a new skip condition.
1819 * This is handled using a pet_skip_info object.
1820 * On initialization, the object checks if skip conditions need
1821 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1822 * adds them in pet_skip_info_seq_add.
1824 * If "block" is set, then we need to insert kill statements at
1825 * the end of the block for any array that has been declared by
1826 * one of the statements in the sequence. Each of these declarations
1827 * results in the construction of a kill statement at the place
1828 * of the declaration, so we simply collect duplicates of
1829 * those kill statements and append these duplicates to the constructed scop.
1831 * If "block" is not set, then any array declared by one of the statements
1832 * in the sequence is marked as being exposed.
1834 * If autodetect is set, then we allow the extraction of only a subrange
1835 * of the sequence of statements. However, if there is at least one statement
1836 * for which we could not construct a scop and the final range contains
1837 * either no statements or at least one kill, then we discard the entire
1838 * range.
1840 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
1841 __isl_keep pet_context *pc, struct pet_state *state)
1843 int i;
1844 isl_ctx *ctx;
1845 struct pet_scop *scop, *kills;
1847 ctx = pet_tree_get_ctx(tree);
1849 pc = pet_context_copy(pc);
1850 scop = pet_scop_empty(ctx);
1851 kills = pet_scop_empty(ctx);
1852 for (i = 0; i < tree->u.b.n; ++i) {
1853 struct pet_scop *scop_i;
1855 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
1856 pc = scop_handle_writes(scop_i, pc);
1857 struct pet_skip_info skip;
1858 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
1859 pet_skip_info_seq_extract(&skip, state);
1860 if (pet_skip_info_has_skip(&skip))
1861 scop_i = pet_scop_prefix(scop_i, 0);
1862 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
1863 if (tree->u.b.block) {
1864 struct pet_scop *kill;
1865 kill = extract_kill(ctx, scop_i, state);
1866 kills = pet_scop_add_par(ctx, kills, kill);
1867 } else
1868 scop_i = mark_exposed(scop_i);
1870 scop_i = pet_scop_prefix(scop_i, i);
1871 scop = pet_scop_add_seq(ctx, scop, scop_i);
1873 scop = pet_skip_info_seq_add(&skip, scop, i);
1875 if (!scop)
1876 break;
1879 kills = pet_scop_prefix(kills, tree->u.b.n);
1880 scop = pet_scop_add_seq(ctx, scop, kills);
1882 pet_context_free(pc);
1884 return scop;
1887 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1888 * within the context "pc" by calling the appropriate function
1889 * based on the type of "tree".
1891 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
1892 __isl_keep pet_context *pc, struct pet_state *state)
1894 if (!tree)
1895 return NULL;
1897 switch (tree->type) {
1898 case pet_tree_error:
1899 return NULL;
1900 case pet_tree_block:
1901 return scop_from_block(tree, pc, state);
1902 case pet_tree_break:
1903 return scop_from_break(tree);
1904 case pet_tree_continue:
1905 return scop_from_continue(tree);
1906 case pet_tree_decl:
1907 case pet_tree_decl_init:
1908 return scop_from_decl(tree, pc, state);
1909 case pet_tree_expr:
1910 return scop_from_expr(pet_expr_copy(tree->u.e.expr),
1911 isl_id_copy(tree->label),
1912 state->n_stmt++,
1913 pet_tree_get_loc(tree), pc);
1914 case pet_tree_if:
1915 case pet_tree_if_else:
1916 return scop_from_if(tree, pc, state);
1917 case pet_tree_for:
1918 return scop_from_for(tree, pc, state);
1919 case pet_tree_while:
1920 return scop_from_while(tree, pc, state);
1921 case pet_tree_infinite_loop:
1922 return scop_from_infinite_for(tree, pc, state);
1925 isl_die(tree->ctx, isl_error_internal, "unhandled type",
1926 return NULL);
1929 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1930 * "int_size" is the number of bytes need to represent an integer.
1931 * "extract_array" is a callback that we can use to create a pet_array
1932 * that corresponds to the variable accessed by an expression.
1934 * Initialize the global state, construct a context and then
1935 * construct the pet_scop by recursively visiting the tree.
1937 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
1938 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
1939 __isl_keep pet_context *pc, void *user), void *user,
1940 __isl_keep pet_context *pc)
1942 struct pet_scop *scop;
1943 struct pet_state state = { 0 };
1945 if (!tree)
1946 return NULL;
1948 state.ctx = pet_tree_get_ctx(tree);
1949 state.int_size = int_size;
1950 state.extract_array = extract_array;
1951 state.user = user;
1953 scop = scop_from_tree(tree, pc, &state);
1954 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
1956 pet_tree_free(tree);
1958 return scop;