tree2scop.c: add_implication: handle higher dimensional domains
[pet.git] / tree2scop.c
blob3633e9966ed5d1a697d865a57e31f1af0948148d
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
35 #include <isl/id_to_pw_aff.h>
37 #include "aff.h"
38 #include "expr.h"
39 #include "expr_arg.h"
40 #include "nest.h"
41 #include "scop.h"
42 #include "skip.h"
43 #include "state.h"
44 #include "tree2scop.h"
46 /* Update "pc" by taking into account the writes in "stmt".
47 * That is, mark all scalar variables that are written by "stmt"
48 * as having an unknown value.
50 static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
51 __isl_take pet_context *pc)
53 return pet_context_clear_writes_in_expr(pc, stmt->body);
56 /* Update "pc" based on the write accesses in "scop".
58 static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
59 __isl_take pet_context *pc)
61 int i;
63 if (!scop)
64 return pet_context_free(pc);
65 for (i = 0; i < scop->n_stmt; ++i)
66 pc = handle_writes(scop->stmts[i], pc);
68 return pc;
71 /* Convert a top-level pet_expr to a pet_scop with one statement
72 * within the context "pc".
73 * This mainly involves resolving nested expression parameters
74 * and setting the name of the iteration space.
75 * The name is given by "label" if it is non-NULL. Otherwise,
76 * it is of the form S_<stmt_nr>.
77 * The location of the statement is set to "loc".
79 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
80 __isl_take isl_id *label, int stmt_nr, __isl_take pet_loc *loc,
81 __isl_keep pet_context *pc)
83 isl_space *space;
84 isl_set *domain;
85 struct pet_stmt *ps;
87 space = pet_context_get_space(pc);
89 expr = pet_expr_plug_in_args(expr, pc);
90 expr = pet_expr_resolve_nested(expr, space);
91 expr = pet_expr_resolve_assume(expr, pc);
92 domain = pet_context_get_domain(pc);
93 ps = pet_stmt_from_pet_expr(domain, loc, label, stmt_nr, expr);
94 return pet_scop_from_pet_stmt(space, ps);
97 /* Construct a pet_scop with a single statement killing the entire
98 * array "array".
99 * The location of the statement is set to "loc".
101 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
102 __isl_keep pet_context *pc, struct pet_state *state)
104 isl_ctx *ctx;
105 isl_id *id;
106 isl_space *space;
107 isl_multi_pw_aff *index;
108 isl_map *access;
109 pet_expr *expr;
110 struct pet_scop *scop;
112 if (!array)
113 goto error;
114 ctx = isl_set_get_ctx(array->extent);
115 access = isl_map_from_range(isl_set_copy(array->extent));
116 id = isl_set_get_tuple_id(array->extent);
117 space = isl_space_alloc(ctx, 0, 0, 0);
118 space = isl_space_set_tuple_id(space, isl_dim_out, id);
119 index = isl_multi_pw_aff_zero(space);
120 expr = pet_expr_kill_from_access_and_index(access, index);
121 return scop_from_expr(expr, NULL, state->n_stmt++, loc, pc);
122 error:
123 pet_loc_free(loc);
124 return NULL;
127 /* Construct and return a pet_array corresponding to the variable
128 * accessed by "access" by calling the extract_array callback.
130 static struct pet_array *extract_array(__isl_keep pet_expr *access,
131 __isl_keep pet_context *pc, struct pet_state *state)
133 return state->extract_array(access, pc, state->user);
136 /* Construct a pet_scop for a (single) variable declaration
137 * within the context "pc".
139 * The scop contains the variable being declared (as an array)
140 * and a statement killing the array.
142 * If the declaration comes with an initialization, then the scop
143 * also contains an assignment to the variable.
145 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
146 __isl_keep pet_context *pc, struct pet_state *state)
148 int type_size;
149 isl_ctx *ctx;
150 struct pet_array *array;
151 struct pet_scop *scop_decl, *scop;
152 pet_expr *lhs, *rhs, *pe;
154 array = extract_array(tree->u.d.var, pc, state);
155 if (array)
156 array->declared = 1;
157 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
158 scop_decl = pet_scop_add_array(scop_decl, array);
160 if (tree->type != pet_tree_decl_init)
161 return scop_decl;
163 lhs = pet_expr_copy(tree->u.d.var);
164 rhs = pet_expr_copy(tree->u.d.init);
165 type_size = pet_expr_get_type_size(lhs);
166 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
167 scop = scop_from_expr(pe, NULL, state->n_stmt++,
168 pet_tree_get_loc(tree), pc);
170 scop_decl = pet_scop_prefix(scop_decl, 0);
171 scop = pet_scop_prefix(scop, 1);
173 ctx = pet_tree_get_ctx(tree);
174 scop = pet_scop_add_seq(ctx, scop_decl, scop);
176 return scop;
179 /* Embed the given iteration domain in an extra outer loop
180 * with induction variable "var".
181 * If this variable appeared as a parameter in the constraints,
182 * it is replaced by the new outermost dimension.
184 static __isl_give isl_set *embed(__isl_take isl_set *set,
185 __isl_take isl_id *var)
187 int pos;
189 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
190 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
191 if (pos >= 0) {
192 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
193 set = isl_set_project_out(set, isl_dim_param, pos, 1);
196 isl_id_free(var);
197 return set;
200 /* Return those elements in the space of "cond" that come after
201 * (based on "sign") an element in "cond" in the final dimension.
203 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
205 isl_space *space;
206 isl_map *previous_to_this;
207 int i, dim;
209 dim = isl_set_dim(cond, isl_dim_set);
210 space = isl_space_map_from_set(isl_set_get_space(cond));
211 previous_to_this = isl_map_universe(space);
212 for (i = 0; i + 1 < dim; ++i)
213 previous_to_this = isl_map_equate(previous_to_this,
214 isl_dim_in, i, isl_dim_out, i);
215 if (sign > 0)
216 previous_to_this = isl_map_order_lt(previous_to_this,
217 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
218 else
219 previous_to_this = isl_map_order_gt(previous_to_this,
220 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
222 cond = isl_set_apply(cond, previous_to_this);
224 return cond;
227 /* Remove those iterations of "domain" that have an earlier iteration
228 * (based on "sign") where "skip" is satisfied.
229 * "domain" has an extra outer loop compared to "skip".
230 * The skip condition is first embedded in the same space as "domain".
231 * If "apply_skip_map" is set, then "skip_map" is first applied
232 * to the embedded skip condition before removing it from the domain.
234 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
235 __isl_take isl_set *skip, int sign,
236 int apply_skip_map, __isl_keep isl_map *skip_map)
238 skip = embed(skip, isl_set_get_dim_id(domain, isl_dim_set, 0));
239 if (apply_skip_map)
240 skip = isl_set_apply(skip, isl_map_copy(skip_map));
241 skip = isl_set_intersect(skip , isl_set_copy(domain));
242 return isl_set_subtract(domain, after(skip, sign));
245 /* Create the infinite iteration domain
247 * { [id] : id >= 0 }
249 static __isl_give isl_set *infinite_domain(__isl_take isl_id *id)
251 isl_ctx *ctx = isl_id_get_ctx(id);
252 isl_set *domain;
254 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
255 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, id);
257 return domain;
260 /* Create an identity affine expression on the space containing "domain",
261 * which is assumed to be one-dimensional.
263 static __isl_give isl_aff *identity_aff(__isl_keep isl_set *domain)
265 isl_local_space *ls;
267 ls = isl_local_space_from_space(isl_set_get_space(domain));
268 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
271 /* Create an affine expression that maps elements
272 * of an array "id_test" to the previous element in the final dimension
273 * (according to "inc"), provided this element belongs to "domain".
274 * That is, create the affine expression
276 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
278 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
279 __isl_take isl_set *domain, __isl_take isl_val *inc)
281 int pos;
282 isl_space *space;
283 isl_aff *aff;
284 isl_pw_aff *pa;
285 isl_multi_aff *ma;
286 isl_multi_pw_aff *prev;
288 pos = isl_set_dim(domain, isl_dim_set) - 1;
289 space = isl_set_get_space(domain);
290 space = isl_space_map_from_set(space);
291 ma = isl_multi_aff_identity(space);
292 aff = isl_multi_aff_get_aff(ma, pos);
293 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
294 ma = isl_multi_aff_set_aff(ma, pos, aff);
295 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
296 prev = isl_multi_pw_aff_from_multi_aff(ma);
297 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
298 pa = isl_pw_aff_intersect_domain(pa, domain);
299 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
300 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
302 return prev;
305 /* Add an implication to "scop" expressing that if an element of
306 * virtual array "id_test" has value "satisfied" then all previous elements
307 * of this array (in the final dimension) also have that value.
308 * The set of previous elements is bounded by "domain".
309 * If "sign" is negative then the iterator
310 * is decreasing and we express that all subsequent array elements
311 * (but still defined previously) have the same value.
313 static struct pet_scop *add_implication(struct pet_scop *scop,
314 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
315 int satisfied)
317 int i, dim;
318 isl_space *space;
319 isl_map *map;
321 dim = isl_set_dim(domain, isl_dim_set);
322 domain = isl_set_set_tuple_id(domain, id_test);
323 space = isl_space_map_from_set(isl_set_get_space(domain));
324 map = isl_map_universe(space);
325 for (i = 0; i + 1 < dim; ++i)
326 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
327 if (sign > 0)
328 map = isl_map_order_ge(map,
329 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
330 else
331 map = isl_map_order_le(map,
332 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
333 map = isl_map_intersect_range(map, domain);
334 scop = pet_scop_add_implication(scop, map, satisfied);
336 return scop;
339 /* Add a filter to "scop" that imposes that it is only executed
340 * when the variable identified by "id_test" has a zero value
341 * for all previous iterations of "domain".
343 * In particular, add a filter that imposes that the array
344 * has a zero value at the previous iteration of domain and
345 * add an implication that implies that it then has that
346 * value for all previous iterations.
348 static struct pet_scop *scop_add_break(struct pet_scop *scop,
349 __isl_take isl_id *id_test, __isl_take isl_set *domain,
350 __isl_take isl_val *inc)
352 isl_multi_pw_aff *prev;
353 int sign = isl_val_sgn(inc);
355 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
356 scop = add_implication(scop, id_test, domain, sign, 0);
357 scop = pet_scop_filter(scop, prev, 0);
359 return scop;
362 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
363 __isl_keep pet_context *pc, struct pet_state *state);
365 /* Construct a pet_scop for an infinite loop around the given body
366 * within the context "pc".
368 * We extract a pet_scop for the body and then embed it in a loop with
369 * iteration domain
371 * { [t] : t >= 0 }
373 * and schedule
375 * { [t] -> [t] }
377 * If the body contains any break, then it is taken into
378 * account in apply_affine_break (if the skip condition is affine)
379 * or in scop_add_break (if the skip condition is not affine).
381 * Note that in case of an affine skip condition,
382 * since we are dealing with a loop without loop iterator,
383 * the skip condition cannot refer to the current loop iterator and
384 * so effectively, the iteration domain is of the form
386 * { [0]; [t] : t >= 1 and not skip }
388 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
389 __isl_keep pet_context *pc, struct pet_state *state)
391 isl_ctx *ctx;
392 isl_id *id, *id_test;
393 isl_set *domain;
394 isl_set *skip;
395 isl_aff *ident;
396 struct pet_scop *scop;
397 int has_affine_break;
398 int has_var_break;
400 ctx = pet_tree_get_ctx(body);
401 id = isl_id_alloc(ctx, "t", NULL);
402 domain = infinite_domain(isl_id_copy(id));
403 ident = identity_aff(domain);
405 scop = scop_from_tree(body, pc, state);
407 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
408 if (has_affine_break)
409 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
410 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
411 if (has_var_break)
412 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
414 scop = pet_scop_embed(scop, isl_set_copy(domain),
415 isl_aff_copy(ident), ident, id);
416 if (has_affine_break) {
417 domain = apply_affine_break(domain, skip, 1, 0, NULL);
418 scop = pet_scop_intersect_domain_prefix(scop,
419 isl_set_copy(domain));
421 if (has_var_break)
422 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
423 else
424 isl_set_free(domain);
426 return scop;
429 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
431 * for (;;)
432 * body
434 * within the context "pc".
436 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
437 __isl_keep pet_context *pc, struct pet_state *state)
439 struct pet_scop *scop;
441 pc = pet_context_copy(pc);
442 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
444 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
446 pet_context_free(pc);
448 return scop;
451 /* Construct a pet_scop for a while loop of the form
453 * while (pa)
454 * body
456 * within the context "pc".
457 * In particular, construct a scop for an infinite loop around body and
458 * intersect the domain with the affine expression.
459 * Note that this intersection may result in an empty loop.
461 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
462 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
463 struct pet_state *state)
465 struct pet_scop *scop;
466 isl_set *dom;
467 isl_set *valid;
469 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
470 dom = isl_pw_aff_non_zero_set(pa);
471 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
472 scop = pet_scop_restrict(scop, isl_set_params(dom));
473 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
475 pet_context_free(pc);
476 return scop;
479 /* Construct a scop for a while, given the scops for the condition
480 * and the body, the filter identifier and the iteration domain of
481 * the while loop.
483 * In particular, the scop for the condition is filtered to depend
484 * on "id_test" evaluating to true for all previous iterations
485 * of the loop, while the scop for the body is filtered to depend
486 * on "id_test" evaluating to true for all iterations up to the
487 * current iteration.
488 * The actual filter only imposes that this virtual array has
489 * value one on the previous or the current iteration.
490 * The fact that this condition also applies to the previous
491 * iterations is enforced by an implication.
493 * These filtered scops are then combined into a single scop.
495 * "sign" is positive if the iterator increases and negative
496 * if it decreases.
498 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
499 struct pet_scop *scop_body, __isl_take isl_id *id_test,
500 __isl_take isl_set *domain, __isl_take isl_val *inc)
502 isl_ctx *ctx = isl_set_get_ctx(domain);
503 isl_space *space;
504 isl_multi_pw_aff *test_index;
505 isl_multi_pw_aff *prev;
506 int sign = isl_val_sgn(inc);
507 struct pet_scop *scop;
509 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
510 scop_cond = pet_scop_filter(scop_cond, prev, 1);
512 space = isl_space_map_from_set(isl_set_get_space(domain));
513 test_index = isl_multi_pw_aff_identity(space);
514 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
515 isl_id_copy(id_test));
516 scop_body = pet_scop_filter(scop_body, test_index, 1);
518 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
519 scop = add_implication(scop, id_test, domain, sign, 1);
521 return scop;
524 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
525 * evaluating "cond" and writing the result to a virtual scalar,
526 * as expressed by "index".
527 * Do so within the context "pc".
528 * The location of the statement is set to "loc".
530 static struct pet_scop *scop_from_non_affine_condition(
531 __isl_take pet_expr *cond, int stmt_nr,
532 __isl_take isl_multi_pw_aff *index,
533 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
535 pet_expr *expr, *write;
537 write = pet_expr_from_index(index);
538 write = pet_expr_access_set_write(write, 1);
539 write = pet_expr_access_set_read(write, 0);
540 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
542 return scop_from_expr(expr, NULL, stmt_nr, loc, pc);
545 /* Construct a generic while scop, with iteration domain
546 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
547 * The scop consists of two parts,
548 * one for evaluating the condition "cond" and one for the body.
549 * If "expr_inc" is not NULL, then a scop for evaluating this expression
550 * is added at the end of the body,
551 * after replacing any skip conditions resulting from continue statements
552 * by the skip conditions resulting from break statements (if any).
554 * The schedule is adjusted to reflect that the condition is evaluated
555 * before the body is executed and the body is filtered to depend
556 * on the result of the condition evaluating to true on all iterations
557 * up to the current iteration, while the evaluation of the condition itself
558 * is filtered to depend on the result of the condition evaluating to true
559 * on all previous iterations.
560 * The context of the scop representing the body is dropped
561 * because we don't know how many times the body will be executed,
562 * if at all.
564 * If the body contains any break, then it is taken into
565 * account in apply_affine_break (if the skip condition is affine)
566 * or in scop_add_break (if the skip condition is not affine).
568 * Note that in case of an affine skip condition,
569 * since we are dealing with a loop without loop iterator,
570 * the skip condition cannot refer to the current loop iterator and
571 * so effectively, the iteration domain is of the form
573 * { [0]; [t] : t >= 1 and not skip }
575 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
576 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
577 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
578 struct pet_state *state)
580 isl_ctx *ctx;
581 isl_id *id, *id_test, *id_break_test;
582 isl_space *space;
583 isl_multi_pw_aff *test_index;
584 isl_set *domain;
585 isl_set *skip;
586 isl_aff *ident;
587 struct pet_scop *scop, *scop_body;
588 int has_affine_break;
589 int has_var_break;
591 ctx = state->ctx;
592 space = pet_context_get_space(pc);
593 test_index = pet_create_test_index(space, state->n_test++);
594 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
595 isl_multi_pw_aff_copy(test_index),
596 pet_loc_copy(loc), pc);
597 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
598 domain = pet_context_get_domain(pc);
599 scop = pet_scop_add_boolean_array(scop, domain,
600 test_index, state->int_size);
602 id = isl_id_alloc(ctx, "t", NULL);
603 domain = infinite_domain(isl_id_copy(id));
604 ident = identity_aff(domain);
606 scop_body = scop_from_tree(tree_body, pc, state);
608 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
609 if (has_affine_break)
610 skip = pet_scop_get_affine_skip_domain(scop_body,
611 pet_skip_later);
612 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
613 if (has_var_break)
614 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
616 scop = pet_scop_prefix(scop, 0);
617 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(ident),
618 isl_aff_copy(ident), isl_id_copy(id));
619 scop_body = pet_scop_reset_context(scop_body);
620 scop_body = pet_scop_prefix(scop_body, 1);
621 if (expr_inc) {
622 struct pet_scop *scop_inc;
623 scop_inc = scop_from_expr(expr_inc, NULL, state->n_stmt++,
624 loc, pc);
625 scop_inc = pet_scop_prefix(scop_inc, 2);
626 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
627 isl_multi_pw_aff *skip;
628 skip = pet_scop_get_skip(scop_body, pet_skip_later);
629 scop_body = pet_scop_set_skip(scop_body,
630 pet_skip_now, skip);
631 } else
632 pet_scop_reset_skip(scop_body, pet_skip_now);
633 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
634 } else
635 pet_loc_free(loc);
636 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain),
637 isl_aff_copy(ident), ident, id);
639 if (has_affine_break) {
640 domain = apply_affine_break(domain, skip, 1, 0, NULL);
641 scop = pet_scop_intersect_domain_prefix(scop,
642 isl_set_copy(domain));
643 scop_body = pet_scop_intersect_domain_prefix(scop_body,
644 isl_set_copy(domain));
646 if (has_var_break) {
647 scop = scop_add_break(scop, isl_id_copy(id_break_test),
648 isl_set_copy(domain), isl_val_one(ctx));
649 scop_body = scop_add_break(scop_body, id_break_test,
650 isl_set_copy(domain), isl_val_one(ctx));
652 scop = scop_add_while(scop, scop_body, id_test, domain,
653 isl_val_one(ctx));
655 pet_context_free(pc);
656 return scop;
659 /* Check if the while loop is of the form
661 * while (affine expression)
662 * body
664 * If so, call scop_from_affine_while to construct a scop.
666 * Otherwise, pass control to scop_from_non_affine_while.
668 * "pc" is the context in which the affine expressions in the scop are created.
670 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
671 __isl_keep pet_context *pc, struct pet_state *state)
673 pet_expr *cond_expr;
674 isl_pw_aff *pa;
676 if (!tree)
677 return NULL;
679 pc = pet_context_copy(pc);
680 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
682 cond_expr = pet_expr_copy(tree->u.l.cond);
683 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
684 pa = pet_expr_extract_affine_condition(cond_expr, pc);
685 pet_expr_free(cond_expr);
687 if (!pa)
688 goto error;
690 if (!isl_pw_aff_involves_nan(pa))
691 return scop_from_affine_while(tree, pa, pc, state);
692 isl_pw_aff_free(pa);
693 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
694 pet_tree_get_loc(tree), tree->u.l.body, NULL,
695 pc, state);
696 error:
697 pet_context_free(pc);
698 return NULL;
701 /* Check whether "cond" expresses a simple loop bound
702 * on the only set dimension.
703 * In particular, if "up" is set then "cond" should contain only
704 * upper bounds on the set dimension.
705 * Otherwise, it should contain only lower bounds.
707 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
709 if (isl_val_is_pos(inc))
710 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, 0);
711 else
712 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, 0);
715 /* Extend a condition on a given iteration of a loop to one that
716 * imposes the same condition on all previous iterations.
717 * "domain" expresses the lower [upper] bound on the iterations
718 * when inc is positive [negative].
720 * In particular, we construct the condition (when inc is positive)
722 * forall i' : (domain(i') and i' <= i) => cond(i')
724 * which is equivalent to
726 * not exists i' : domain(i') and i' <= i and not cond(i')
728 * We construct this set by negating cond, applying a map
730 * { [i'] -> [i] : domain(i') and i' <= i }
732 * and then negating the result again.
734 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
735 __isl_take isl_set *domain, __isl_take isl_val *inc)
737 isl_map *previous_to_this;
739 if (isl_val_is_pos(inc))
740 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
741 else
742 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
744 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
746 cond = isl_set_complement(cond);
747 cond = isl_set_apply(cond, previous_to_this);
748 cond = isl_set_complement(cond);
750 isl_val_free(inc);
752 return cond;
755 /* Construct a domain of the form
757 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
759 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
760 __isl_take isl_pw_aff *init, __isl_take isl_val *inc)
762 isl_aff *aff;
763 isl_space *dim;
764 isl_set *set;
766 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
767 dim = isl_pw_aff_get_domain_space(init);
768 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
769 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, 0, inc);
770 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
772 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
773 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
774 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
775 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
777 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
779 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
781 return isl_set_params(set);
784 /* Assuming "cond" represents a bound on a loop where the loop
785 * iterator "iv" is incremented (or decremented) by one, check if wrapping
786 * is possible.
788 * Under the given assumptions, wrapping is only possible if "cond" allows
789 * for the last value before wrapping, i.e., 2^width - 1 in case of an
790 * increasing iterator and 0 in case of a decreasing iterator.
792 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
793 __isl_keep isl_val *inc)
795 int cw;
796 isl_ctx *ctx;
797 isl_val *limit;
798 isl_set *test;
800 test = isl_set_copy(cond);
802 ctx = isl_set_get_ctx(test);
803 if (isl_val_is_neg(inc))
804 limit = isl_val_zero(ctx);
805 else {
806 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
807 limit = isl_val_2exp(limit);
808 limit = isl_val_sub_ui(limit, 1);
811 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
812 cw = !isl_set_is_empty(test);
813 isl_set_free(test);
815 return cw;
818 /* Given a one-dimensional space, construct the following affine expression
819 * on this space
821 * { [v] -> [v mod 2^width] }
823 * where width is the number of bits used to represent the values
824 * of the unsigned variable "iv".
826 static __isl_give isl_aff *compute_wrapping(__isl_take isl_space *dim,
827 __isl_keep pet_expr *iv)
829 isl_ctx *ctx;
830 isl_val *mod;
831 isl_aff *aff;
833 ctx = isl_space_get_ctx(dim);
834 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
835 mod = isl_val_2exp(mod);
837 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
838 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
839 aff = isl_aff_mod_val(aff, mod);
841 return aff;
844 /* Project out the parameter "id" from "set".
846 static __isl_give isl_set *set_project_out_by_id(__isl_take isl_set *set,
847 __isl_keep isl_id *id)
849 int pos;
851 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
852 if (pos >= 0)
853 set = isl_set_project_out(set, isl_dim_param, pos, 1);
855 return set;
858 /* Compute the set of parameters for which "set1" is a subset of "set2".
860 * set1 is a subset of set2 if
862 * forall i in set1 : i in set2
864 * or
866 * not exists i in set1 and i not in set2
868 * i.e.,
870 * not exists i in set1 \ set2
872 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
873 __isl_take isl_set *set2)
875 return isl_set_complement(isl_set_params(isl_set_subtract(set1, set2)));
878 /* Compute the set of parameter values for which "cond" holds
879 * on the next iteration for each element of "dom".
881 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
882 * and then compute the set of parameters for which the result is a subset
883 * of "cond".
885 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
886 __isl_take isl_set *dom, __isl_take isl_val *inc)
888 isl_space *space;
889 isl_aff *aff;
890 isl_map *next;
892 space = isl_set_get_space(dom);
893 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
894 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
895 aff = isl_aff_add_constant_val(aff, inc);
896 next = isl_map_from_basic_map(isl_basic_map_from_aff(aff));
898 dom = isl_set_apply(dom, next);
900 return enforce_subset(dom, cond);
903 /* Extract the for loop "tree" as a while loop within the context "pc".
905 * That is, the for loop has the form
907 * for (iv = init; cond; iv += inc)
908 * body;
910 * and is treated as
912 * iv = init;
913 * while (cond) {
914 * body;
915 * iv += inc;
918 * except that the skips resulting from any continue statements
919 * in body do not apply to the increment, but are replaced by the skips
920 * resulting from break statements.
922 * If the loop iterator is declared in the for loop, then it is killed before
923 * and after the loop.
925 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
926 __isl_take pet_context *pc, struct pet_state *state)
928 int declared;
929 isl_id *iv;
930 pet_expr *expr_iv, *init, *inc;
931 struct pet_scop *scop_init, *scop;
932 int type_size;
933 struct pet_array *array;
934 struct pet_scop *scop_kill;
936 iv = pet_expr_access_get_id(tree->u.l.iv);
937 pc = pet_context_mark_assigned(pc, iv);
939 declared = tree->u.l.declared;
941 expr_iv = pet_expr_copy(tree->u.l.iv);
942 type_size = pet_expr_get_type_size(expr_iv);
943 init = pet_expr_copy(tree->u.l.init);
944 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
945 scop_init = scop_from_expr(init, NULL, state->n_stmt++,
946 pet_tree_get_loc(tree), pc);
947 scop_init = pet_scop_prefix(scop_init, declared);
949 expr_iv = pet_expr_copy(tree->u.l.iv);
950 type_size = pet_expr_get_type_size(expr_iv);
951 inc = pet_expr_copy(tree->u.l.inc);
952 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
954 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
955 pet_tree_get_loc(tree), tree->u.l.body, inc,
956 pet_context_copy(pc), state);
958 scop = pet_scop_prefix(scop, declared + 1);
959 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
961 if (!declared) {
962 pet_context_free(pc);
963 return scop;
966 array = extract_array(tree->u.l.iv, pc, state);
967 if (array)
968 array->declared = 1;
969 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
970 scop_kill = pet_scop_prefix(scop_kill, 0);
971 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
972 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
973 scop_kill = pet_scop_add_array(scop_kill, array);
974 scop_kill = pet_scop_prefix(scop_kill, 3);
975 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
977 pet_context_free(pc);
978 return scop;
981 /* Given an access expression "expr", is the variable accessed by
982 * "expr" assigned anywhere inside "tree"?
984 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
986 int assigned = 0;
987 isl_id *id;
989 id = pet_expr_access_get_id(expr);
990 assigned = pet_tree_writes(tree, id);
991 isl_id_free(id);
993 return assigned;
996 /* Are all nested access parameters in "pa" allowed given "tree".
997 * In particular, is none of them written by anywhere inside "tree".
999 * If "tree" has any continue nodes in the current loop level,
1000 * then no nested access parameters are allowed.
1001 * In particular, if there is any nested access in a guard
1002 * for a piece of code containing a "continue", then we want to introduce
1003 * a separate statement for evaluating this guard so that we can express
1004 * that the result is false for all previous iterations.
1006 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1007 __isl_keep pet_tree *tree)
1009 int i, nparam;
1011 if (!tree)
1012 return -1;
1014 if (!pet_nested_any_in_pw_aff(pa))
1015 return 1;
1017 if (pet_tree_has_continue(tree))
1018 return 0;
1020 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1021 for (i = 0; i < nparam; ++i) {
1022 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1023 pet_expr *expr;
1024 int allowed;
1026 if (!pet_nested_in_id(id)) {
1027 isl_id_free(id);
1028 continue;
1031 expr = pet_nested_extract_expr(id);
1032 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1033 !is_assigned(expr, tree);
1035 pet_expr_free(expr);
1036 isl_id_free(id);
1038 if (!allowed)
1039 return 0;
1042 return 1;
1045 /* Construct a pet_scop for a for tree with static affine initialization
1046 * and constant increment within the context "pc".
1048 * The condition is allowed to contain nested accesses, provided
1049 * they are not being written to inside the body of the loop.
1050 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1051 * essentially treated as a while loop, with iteration domain
1052 * { [i] : i >= init }.
1054 * We extract a pet_scop for the body and then embed it in a loop with
1055 * iteration domain and schedule
1057 * { [i] : i >= init and condition' }
1058 * { [i] -> [i] }
1060 * or
1062 * { [i] : i <= init and condition' }
1063 * { [i] -> [-i] }
1065 * Where condition' is equal to condition if the latter is
1066 * a simple upper [lower] bound and a condition that is extended
1067 * to apply to all previous iterations otherwise.
1069 * If the condition is non-affine, then we drop the condition from the
1070 * iteration domain and instead create a separate statement
1071 * for evaluating the condition. The body is then filtered to depend
1072 * on the result of the condition evaluating to true on all iterations
1073 * up to the current iteration, while the evaluation the condition itself
1074 * is filtered to depend on the result of the condition evaluating to true
1075 * on all previous iterations.
1076 * The context of the scop representing the body is dropped
1077 * because we don't know how many times the body will be executed,
1078 * if at all.
1080 * If the stride of the loop is not 1, then "i >= init" is replaced by
1082 * (exists a: i = init + stride * a and a >= 0)
1084 * If the loop iterator i is unsigned, then wrapping may occur.
1085 * We therefore use a virtual iterator instead that does not wrap.
1086 * However, the condition in the code applies
1087 * to the wrapped value, so we need to change condition(i)
1088 * into condition([i % 2^width]). Similarly, we replace all accesses
1089 * to the original iterator by the wrapping of the virtual iterator.
1090 * Note that there may be no need to perform this final wrapping
1091 * if the loop condition (after wrapping) satisfies certain conditions.
1092 * However, the is_simple_bound condition is not enough since it doesn't
1093 * check if there even is an upper bound.
1095 * Wrapping on unsigned iterators can be avoided entirely if
1096 * loop condition is simple, the loop iterator is incremented
1097 * [decremented] by one and the last value before wrapping cannot
1098 * possibly satisfy the loop condition.
1100 * Valid parameters for a for loop are those for which the initial
1101 * value itself, the increment on each domain iteration and
1102 * the condition on both the initial value and
1103 * the result of incrementing the iterator for each iteration of the domain
1104 * can be evaluated.
1105 * If the loop condition is non-affine, then we only consider validity
1106 * of the initial value.
1108 * If the body contains any break, then we keep track of it in "skip"
1109 * (if the skip condition is affine) or it is handled in scop_add_break
1110 * (if the skip condition is not affine).
1111 * Note that the affine break condition needs to be considered with
1112 * respect to previous iterations in the virtual domain (if any).
1114 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1115 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1116 __isl_take isl_val *inc, __isl_take pet_context *pc,
1117 struct pet_state *state)
1119 isl_local_space *ls;
1120 isl_set *domain;
1121 isl_aff *sched;
1122 isl_set *cond = NULL;
1123 isl_set *skip = NULL;
1124 isl_id *id, *id_test = NULL, *id_break_test;
1125 struct pet_scop *scop, *scop_cond = NULL;
1126 int is_one;
1127 int is_unsigned;
1128 int is_simple;
1129 int is_virtual;
1130 int is_non_affine;
1131 int has_affine_break;
1132 int has_var_break;
1133 isl_map *rev_wrap = NULL;
1134 isl_aff *wrap = NULL;
1135 isl_pw_aff *pa;
1136 isl_set *valid_init;
1137 isl_set *valid_cond;
1138 isl_set *valid_cond_init;
1139 isl_set *valid_cond_next;
1140 isl_set *valid_inc;
1141 pet_expr *cond_expr;
1142 pet_context *pc_nested;
1144 id = pet_expr_access_get_id(tree->u.l.iv);
1146 cond_expr = pet_expr_copy(tree->u.l.cond);
1147 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1148 pc_nested = pet_context_copy(pc);
1149 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1150 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1151 pet_context_free(pc_nested);
1152 pet_expr_free(cond_expr);
1154 valid_inc = isl_pw_aff_domain(pa_inc);
1156 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1158 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1159 !is_nested_allowed(pa, tree->u.l.body);
1160 if (is_non_affine)
1161 pa = isl_pw_aff_free(pa);
1163 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1164 cond = isl_pw_aff_non_zero_set(pa);
1165 if (is_non_affine)
1166 cond = isl_set_universe(isl_space_set_alloc(state->ctx, 0, 0));
1168 cond = embed(cond, isl_id_copy(id));
1169 valid_cond = isl_set_coalesce(valid_cond);
1170 valid_cond = embed(valid_cond, isl_id_copy(id));
1171 valid_inc = embed(valid_inc, isl_id_copy(id));
1172 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1173 is_virtual = is_unsigned &&
1174 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1176 valid_cond_init = enforce_subset(
1177 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val))),
1178 isl_set_copy(valid_cond));
1179 if (is_one && !is_virtual) {
1180 isl_pw_aff_free(init_val);
1181 pa = pet_expr_extract_comparison(
1182 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1183 tree->u.l.iv, tree->u.l.init, pc);
1184 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1185 valid_init = set_project_out_by_id(valid_init, id);
1186 domain = isl_pw_aff_non_zero_set(pa);
1187 } else {
1188 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1189 domain = strided_domain(isl_id_copy(id), init_val,
1190 isl_val_copy(inc));
1193 domain = embed(domain, isl_id_copy(id));
1194 if (is_virtual) {
1195 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1196 rev_wrap = isl_map_from_aff(isl_aff_copy(wrap));
1197 rev_wrap = isl_map_reverse(rev_wrap);
1198 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1199 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1200 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1202 is_simple = is_simple_bound(cond, inc);
1203 if (!is_simple) {
1204 cond = isl_set_gist(cond, isl_set_copy(domain));
1205 is_simple = is_simple_bound(cond, inc);
1207 if (!is_simple)
1208 cond = valid_for_each_iteration(cond,
1209 isl_set_copy(domain), isl_val_copy(inc));
1210 domain = isl_set_intersect(domain, cond);
1211 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1212 ls = isl_local_space_from_space(isl_set_get_space(domain));
1213 sched = isl_aff_var_on_domain(ls, isl_dim_set, 0);
1214 if (isl_val_is_neg(inc))
1215 sched = isl_aff_neg(sched);
1217 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1218 isl_val_copy(inc));
1219 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1221 if (!is_virtual)
1222 wrap = identity_aff(domain);
1224 if (is_non_affine) {
1225 isl_space *space;
1226 isl_multi_pw_aff *test_index;
1227 space = pet_context_get_space(pc);
1228 test_index = pet_create_test_index(space, state->n_test++);
1229 scop_cond = scop_from_non_affine_condition(
1230 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1231 isl_multi_pw_aff_copy(test_index),
1232 pet_tree_get_loc(tree), pc);
1233 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1234 isl_dim_out);
1235 scop_cond = pet_scop_add_boolean_array(scop_cond,
1236 pet_context_get_domain(pc), test_index,
1237 state->int_size);
1238 scop_cond = pet_scop_prefix(scop_cond, 0);
1239 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1240 isl_aff_copy(sched), isl_aff_copy(wrap),
1241 isl_id_copy(id));
1244 scop = scop_from_tree(tree->u.l.body, pc, state);
1245 has_affine_break = scop &&
1246 pet_scop_has_affine_skip(scop, pet_skip_later);
1247 if (has_affine_break)
1248 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1249 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1250 if (has_var_break)
1251 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1252 if (is_non_affine) {
1253 scop = pet_scop_reset_context(scop);
1254 scop = pet_scop_prefix(scop, 1);
1256 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
1257 scop = pet_scop_resolve_nested(scop);
1258 if (has_affine_break) {
1259 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1260 is_virtual, rev_wrap);
1261 scop = pet_scop_intersect_domain_prefix(scop,
1262 isl_set_copy(domain));
1264 isl_map_free(rev_wrap);
1265 if (has_var_break)
1266 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1267 isl_val_copy(inc));
1268 if (is_non_affine) {
1269 scop = scop_add_while(scop_cond, scop, id_test, domain,
1270 isl_val_copy(inc));
1271 isl_set_free(valid_inc);
1272 } else {
1273 scop = pet_scop_restrict_context(scop, valid_inc);
1274 scop = pet_scop_restrict_context(scop, valid_cond_next);
1275 scop = pet_scop_restrict_context(scop, valid_cond_init);
1276 isl_set_free(domain);
1279 isl_val_free(inc);
1281 scop = pet_scop_restrict_context(scop, isl_set_params(valid_init));
1283 pet_context_free(pc);
1284 return scop;
1287 /* Construct a pet_scop for a for statement within the context of "pc".
1289 * We update the context to reflect the writes to the loop variable and
1290 * the writes inside the body.
1292 * Then we check if the initialization of the for loop
1293 * is a static affine value and the increment is a constant.
1294 * If so, we construct the pet_scop using scop_from_affine_for.
1295 * Otherwise, we treat the for loop as a while loop
1296 * in scop_from_non_affine_for.
1298 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1299 __isl_keep pet_context *pc, struct pet_state *state)
1301 isl_id *iv;
1302 isl_val *inc;
1303 isl_pw_aff *pa_inc, *init_val;
1304 pet_context *pc_init_val;
1306 if (!tree)
1307 return NULL;
1309 iv = pet_expr_access_get_id(tree->u.l.iv);
1310 pc = pet_context_copy(pc);
1311 pc = pet_context_clear_value(pc, iv);
1312 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1314 pc_init_val = pet_context_copy(pc);
1315 pc_init_val = pet_context_mark_unknown(pc_init_val, isl_id_copy(iv));
1316 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1317 pet_context_free(pc_init_val);
1318 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1319 inc = pet_extract_cst(pa_inc);
1320 if (!pa_inc || !init_val || !inc)
1321 goto error;
1322 if (!isl_pw_aff_involves_nan(pa_inc) &&
1323 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1324 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1325 pc, state);
1327 isl_pw_aff_free(pa_inc);
1328 isl_pw_aff_free(init_val);
1329 isl_val_free(inc);
1330 return scop_from_non_affine_for(tree, pc, state);
1331 error:
1332 isl_pw_aff_free(pa_inc);
1333 isl_pw_aff_free(init_val);
1334 isl_val_free(inc);
1335 pet_context_free(pc);
1336 return NULL;
1339 /* Check whether "expr" is an affine constraint within the context "pc".
1341 static int is_affine_condition(__isl_keep pet_expr *expr,
1342 __isl_keep pet_context *pc)
1344 isl_pw_aff *pa;
1345 int is_affine;
1347 pa = pet_expr_extract_affine_condition(expr, pc);
1348 if (!pa)
1349 return -1;
1350 is_affine = !isl_pw_aff_involves_nan(pa);
1351 isl_pw_aff_free(pa);
1353 return is_affine;
1356 /* Check if the given if statement is a conditional assignement
1357 * with a non-affine condition.
1359 * In particular we check if "stmt" is of the form
1361 * if (condition)
1362 * a = f(...);
1363 * else
1364 * a = g(...);
1366 * where the condition is non-affine and a is some array or scalar access.
1368 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1369 __isl_keep pet_context *pc)
1371 int equal;
1372 isl_ctx *ctx;
1373 pet_expr *expr1, *expr2;
1375 ctx = pet_tree_get_ctx(tree);
1376 if (!pet_options_get_detect_conditional_assignment(ctx))
1377 return 0;
1378 if (tree->type != pet_tree_if_else)
1379 return 0;
1380 if (tree->u.i.then_body->type != pet_tree_expr)
1381 return 0;
1382 if (tree->u.i.else_body->type != pet_tree_expr)
1383 return 0;
1384 expr1 = tree->u.i.then_body->u.e.expr;
1385 expr2 = tree->u.i.else_body->u.e.expr;
1386 if (pet_expr_get_type(expr1) != pet_expr_op)
1387 return 0;
1388 if (pet_expr_get_type(expr2) != pet_expr_op)
1389 return 0;
1390 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1391 return 0;
1392 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1393 return 0;
1394 expr1 = pet_expr_get_arg(expr1, 0);
1395 expr2 = pet_expr_get_arg(expr2, 0);
1396 equal = pet_expr_is_equal(expr1, expr2);
1397 pet_expr_free(expr1);
1398 pet_expr_free(expr2);
1399 if (equal < 0 || !equal)
1400 return 0;
1401 if (is_affine_condition(tree->u.i.cond, pc))
1402 return 0;
1404 return 1;
1407 /* Given that "tree" is of the form
1409 * if (condition)
1410 * a = f(...);
1411 * else
1412 * a = g(...);
1414 * where a is some array or scalar access, construct a pet_scop
1415 * corresponding to this conditional assignment within the context "pc".
1417 * The constructed pet_scop then corresponds to the expression
1419 * a = condition ? f(...) : g(...)
1421 * All access relations in f(...) are intersected with condition
1422 * while all access relation in g(...) are intersected with the complement.
1424 static struct pet_scop *scop_from_conditional_assignment(
1425 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1426 struct pet_state *state)
1428 int type_size;
1429 isl_pw_aff *pa;
1430 isl_set *cond, *comp;
1431 isl_multi_pw_aff *index;
1432 pet_expr *expr1, *expr2;
1433 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1434 pet_context *pc_nested;
1435 struct pet_scop *scop;
1437 pe_cond = pet_expr_copy(tree->u.i.cond);
1438 pe_cond = pet_expr_plug_in_args(pe_cond, pc);
1439 pc_nested = pet_context_copy(pc);
1440 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1441 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1442 pet_context_free(pc_nested);
1443 pet_expr_free(pe_cond);
1444 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1445 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1446 index = isl_multi_pw_aff_from_pw_aff(pa);
1448 expr1 = tree->u.i.then_body->u.e.expr;
1449 expr2 = tree->u.i.else_body->u.e.expr;
1451 pe_cond = pet_expr_from_index(index);
1453 pe_then = pet_expr_get_arg(expr1, 1);
1454 pe_then = pet_expr_restrict(pe_then, cond);
1455 pe_else = pet_expr_get_arg(expr2, 1);
1456 pe_else = pet_expr_restrict(pe_else, comp);
1457 pe_write = pet_expr_get_arg(expr1, 0);
1459 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1460 type_size = pet_expr_get_type_size(pe_write);
1461 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1463 scop = scop_from_expr(pe, NULL, state->n_stmt++,
1464 pet_tree_get_loc(tree), pc);
1466 pet_context_free(pc);
1468 return scop;
1471 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1473 * We create a separate statement that writes the result
1474 * of the non-affine condition to a virtual scalar.
1475 * A constraint requiring the value of this virtual scalar to be one
1476 * is added to the iteration domains of the then branch.
1477 * Similarly, a constraint requiring the value of this virtual scalar
1478 * to be zero is added to the iteration domains of the else branch, if any.
1479 * We adjust the schedules to ensure that the virtual scalar is written
1480 * before it is read.
1482 * If there are any breaks or continues in the then and/or else
1483 * branches, then we may have to compute a new skip condition.
1484 * This is handled using a pet_skip_info object.
1485 * On initialization, the object checks if skip conditions need
1486 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1487 * adds them in pet_skip_info_if_add.
1489 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1490 __isl_take pet_context *pc, struct pet_state *state)
1492 int has_else;
1493 isl_space *space;
1494 isl_set *domain;
1495 isl_multi_pw_aff *test_index;
1496 struct pet_skip_info skip;
1497 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1499 has_else = tree->type == pet_tree_if_else;
1501 space = pet_context_get_space(pc);
1502 test_index = pet_create_test_index(space, state->n_test++);
1503 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1504 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1505 pet_tree_get_loc(tree), pc);
1506 domain = pet_context_get_domain(pc);
1507 scop = pet_scop_add_boolean_array(scop, domain,
1508 isl_multi_pw_aff_copy(test_index), state->int_size);
1510 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1511 if (has_else)
1512 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1514 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1515 has_else, 0);
1516 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1518 scop = pet_scop_prefix(scop, 0);
1519 scop_then = pet_scop_prefix(scop_then, 1);
1520 scop_then = pet_scop_filter(scop_then,
1521 isl_multi_pw_aff_copy(test_index), 1);
1522 if (has_else) {
1523 scop_else = pet_scop_prefix(scop_else, 1);
1524 scop_else = pet_scop_filter(scop_else, test_index, 0);
1525 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1526 } else
1527 isl_multi_pw_aff_free(test_index);
1529 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1531 scop = pet_skip_info_if_add(&skip, scop, 2);
1533 pet_context_free(pc);
1534 return scop;
1537 /* Construct a pet_scop for an affine if statement within the context "pc".
1539 * The condition is added to the iteration domains of the then branch,
1540 * while the opposite of the condition in added to the iteration domains
1541 * of the else branch, if any.
1543 * If there are any breaks or continues in the then and/or else
1544 * branches, then we may have to compute a new skip condition.
1545 * This is handled using a pet_skip_info_if object.
1546 * On initialization, the object checks if skip conditions need
1547 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1548 * adds them in pet_skip_info_if_add.
1550 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1551 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1552 struct pet_state *state)
1554 int has_else;
1555 isl_ctx *ctx;
1556 isl_set *set;
1557 isl_set *valid;
1558 struct pet_skip_info skip;
1559 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1561 ctx = pet_tree_get_ctx(tree);
1563 has_else = tree->type == pet_tree_if_else;
1565 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1566 if (has_else)
1567 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1569 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1570 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
1572 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1573 set = isl_pw_aff_non_zero_set(cond);
1574 scop = pet_scop_restrict(scop_then, isl_set_params(isl_set_copy(set)));
1576 if (has_else) {
1577 set = isl_set_subtract(isl_set_copy(valid), set);
1578 scop_else = pet_scop_restrict(scop_else, isl_set_params(set));
1579 scop = pet_scop_add_par(ctx, scop, scop_else);
1580 } else
1581 isl_set_free(set);
1582 scop = pet_scop_resolve_nested(scop);
1583 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
1585 if (pet_skip_info_has_skip(&skip))
1586 scop = pet_scop_prefix(scop, 0);
1587 scop = pet_skip_info_if_add(&skip, scop, 1);
1589 pet_context_free(pc);
1590 return scop;
1593 /* Construct a pet_scop for an if statement within the context "pc".
1595 * If the condition fits the pattern of a conditional assignment,
1596 * then it is handled by scop_from_conditional_assignment.
1598 * Otherwise, we check if the condition is affine.
1599 * If so, we construct the scop in scop_from_affine_if.
1600 * Otherwise, we construct the scop in scop_from_non_affine_if.
1602 * We allow the condition to be dynamic, i.e., to refer to
1603 * scalars or array elements that may be written to outside
1604 * of the given if statement. These nested accesses are then represented
1605 * as output dimensions in the wrapping iteration domain.
1606 * If it is also written _inside_ the then or else branch, then
1607 * we treat the condition as non-affine.
1608 * As explained in extract_non_affine_if, this will introduce
1609 * an extra statement.
1610 * For aesthetic reasons, we want this statement to have a statement
1611 * number that is lower than those of the then and else branches.
1612 * In order to evaluate if we will need such a statement, however, we
1613 * first construct scops for the then and else branches.
1614 * We therefore reserve a statement number if we might have to
1615 * introduce such an extra statement.
1617 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1618 __isl_keep pet_context *pc, struct pet_state *state)
1620 int has_else;
1621 isl_pw_aff *cond;
1622 pet_expr *cond_expr;
1623 pet_context *pc_nested;
1625 if (!tree)
1626 return NULL;
1628 has_else = tree->type == pet_tree_if_else;
1630 pc = pet_context_copy(pc);
1631 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1632 if (has_else)
1633 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1635 if (is_conditional_assignment(tree, pc))
1636 return scop_from_conditional_assignment(tree, pc, state);
1638 cond_expr = pet_expr_copy(tree->u.i.cond);
1639 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1640 pc_nested = pet_context_copy(pc);
1641 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1642 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1643 pet_context_free(pc_nested);
1644 pet_expr_free(cond_expr);
1646 if (!cond) {
1647 pet_context_free(pc);
1648 return NULL;
1651 if (isl_pw_aff_involves_nan(cond)) {
1652 isl_pw_aff_free(cond);
1653 return scop_from_non_affine_if(tree, pc, state);
1656 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1657 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1658 isl_pw_aff_free(cond);
1659 return scop_from_non_affine_if(tree, pc, state);
1662 return scop_from_affine_if(tree, cond, pc, state);
1665 /* Return a one-dimensional multi piecewise affine expression that is equal
1666 * to the constant 1 and is defined over the given domain.
1668 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
1670 isl_local_space *ls;
1671 isl_aff *aff;
1673 ls = isl_local_space_from_space(space);
1674 aff = isl_aff_zero_on_domain(ls);
1675 aff = isl_aff_set_constant_si(aff, 1);
1677 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1680 /* Construct a pet_scop for a continue statement with the given domain space.
1682 * We simply create an empty scop with a universal pet_skip_now
1683 * skip condition. This skip condition will then be taken into
1684 * account by the enclosing loop construct, possibly after
1685 * being incorporated into outer skip conditions.
1687 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
1688 __isl_take isl_space *space)
1690 struct pet_scop *scop;
1692 scop = pet_scop_empty(isl_space_copy(space));
1694 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
1696 return scop;
1699 /* Construct a pet_scop for a break statement with the given domain space.
1701 * We simply create an empty scop with both a universal pet_skip_now
1702 * skip condition and a universal pet_skip_later skip condition.
1703 * These skip conditions will then be taken into
1704 * account by the enclosing loop construct, possibly after
1705 * being incorporated into outer skip conditions.
1707 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
1708 __isl_take isl_space *space)
1710 struct pet_scop *scop;
1711 isl_multi_pw_aff *skip;
1713 scop = pet_scop_empty(isl_space_copy(space));
1715 skip = one_mpa(space);
1716 scop = pet_scop_set_skip(scop, pet_skip_now,
1717 isl_multi_pw_aff_copy(skip));
1718 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1720 return scop;
1723 /* Extract a clone of the kill statement in "scop".
1724 * The domain of the clone is given by "domain".
1725 * "scop" is expected to have been created from a DeclStmt
1726 * and should have the kill as its first statement.
1728 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
1729 struct pet_scop *scop, struct pet_state *state)
1731 pet_expr *kill;
1732 struct pet_stmt *stmt;
1733 isl_multi_pw_aff *index;
1734 isl_map *access;
1735 pet_expr *arg;
1737 if (!domain || !scop)
1738 return NULL;
1739 if (scop->n_stmt < 1)
1740 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1741 "expecting at least one statement", return NULL);
1742 stmt = scop->stmts[0];
1743 if (!pet_stmt_is_kill(stmt))
1744 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1745 "expecting kill statement", return NULL);
1747 arg = pet_expr_get_arg(stmt->body, 0);
1748 index = pet_expr_access_get_index(arg);
1749 access = pet_expr_access_get_access(arg);
1750 pet_expr_free(arg);
1751 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
1752 access = isl_map_reset_tuple_id(access, isl_dim_in);
1753 kill = pet_expr_kill_from_access_and_index(access, index);
1754 stmt = pet_stmt_from_pet_expr(isl_set_copy(domain),
1755 pet_loc_copy(stmt->loc), NULL, state->n_stmt++, kill);
1756 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
1759 /* Does "tree" represent an assignment to a variable?
1761 * The assignment may be one of
1762 * - a declaration with initialization
1763 * - an expression with a top-level assignment operator
1765 static int is_assignment(__isl_keep pet_tree *tree)
1767 if (!tree)
1768 return 0;
1769 if (tree->type == pet_tree_decl_init)
1770 return 1;
1771 return pet_tree_is_assign(tree);
1774 /* Update "pc" by taking into account the assignment performed by "tree",
1775 * where "tree" satisfies is_assignment.
1777 * In particular, if the lhs of the assignment is a scalar variable and
1778 * if the rhs is an affine expression, then keep track of this value in "pc"
1779 * so that we can plug it in when we later come across the same variable.
1781 * The variable has already been marked as having been assigned
1782 * an unknown value by scop_handle_writes.
1784 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
1785 __isl_keep pet_tree *tree)
1787 pet_expr *var, *val;
1788 isl_id *id;
1789 isl_pw_aff *pa;
1791 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
1792 var = pet_tree_decl_get_var(tree);
1793 val = pet_tree_decl_get_init(tree);
1794 } else {
1795 pet_expr *expr;
1796 expr = pet_tree_expr_get_expr(tree);
1797 var = pet_expr_get_arg(expr, 0);
1798 val = pet_expr_get_arg(expr, 1);
1799 pet_expr_free(expr);
1802 if (!pet_expr_is_scalar_access(var)) {
1803 pet_expr_free(var);
1804 pet_expr_free(val);
1805 return pc;
1808 pa = pet_expr_extract_affine(val, pc);
1809 if (!pa)
1810 pc = pet_context_free(pc);
1812 if (!isl_pw_aff_involves_nan(pa)) {
1813 id = pet_expr_access_get_id(var);
1814 pc = pet_context_set_value(pc, id, pa);
1815 } else {
1816 isl_pw_aff_free(pa);
1818 pet_expr_free(var);
1819 pet_expr_free(val);
1821 return pc;
1824 /* Mark all arrays in "scop" as being exposed.
1826 static struct pet_scop *mark_exposed(struct pet_scop *scop)
1828 int i;
1830 if (!scop)
1831 return NULL;
1832 for (i = 0; i < scop->n_array; ++i)
1833 scop->arrays[i]->exposed = 1;
1834 return scop;
1837 /* Try and construct a pet_scop corresponding to (part of)
1838 * a sequence of statements within the context "pc".
1840 * After extracting a statement, we update "pc"
1841 * based on the top-level assignments in the statement
1842 * so that we can exploit them in subsequent statements in the same block.
1844 * If there are any breaks or continues in the individual statements,
1845 * then we may have to compute a new skip condition.
1846 * This is handled using a pet_skip_info object.
1847 * On initialization, the object checks if skip conditions need
1848 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1849 * adds them in pet_skip_info_seq_add.
1851 * If "block" is set, then we need to insert kill statements at
1852 * the end of the block for any array that has been declared by
1853 * one of the statements in the sequence. Each of these declarations
1854 * results in the construction of a kill statement at the place
1855 * of the declaration, so we simply collect duplicates of
1856 * those kill statements and append these duplicates to the constructed scop.
1858 * If "block" is not set, then any array declared by one of the statements
1859 * in the sequence is marked as being exposed.
1861 * If autodetect is set, then we allow the extraction of only a subrange
1862 * of the sequence of statements. However, if there is at least one statement
1863 * for which we could not construct a scop and the final range contains
1864 * either no statements or at least one kill, then we discard the entire
1865 * range.
1867 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
1868 __isl_keep pet_context *pc, struct pet_state *state)
1870 int i;
1871 isl_ctx *ctx;
1872 isl_space *space;
1873 isl_set *domain;
1874 struct pet_scop *scop, *kills;
1876 ctx = pet_tree_get_ctx(tree);
1878 space = pet_context_get_space(pc);
1879 domain = pet_context_get_domain(pc);
1880 pc = pet_context_copy(pc);
1881 scop = pet_scop_empty(isl_space_copy(space));
1882 kills = pet_scop_empty(space);
1883 for (i = 0; i < tree->u.b.n; ++i) {
1884 struct pet_scop *scop_i;
1886 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
1887 pc = scop_handle_writes(scop_i, pc);
1888 if (is_assignment(tree->u.b.child[i]))
1889 pc = handle_assignment(pc, tree->u.b.child[i]);
1890 struct pet_skip_info skip;
1891 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
1892 pet_skip_info_seq_extract(&skip, pc, state);
1893 if (pet_skip_info_has_skip(&skip))
1894 scop_i = pet_scop_prefix(scop_i, 0);
1895 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
1896 if (tree->u.b.block) {
1897 struct pet_scop *kill;
1898 kill = extract_kill(domain, scop_i, state);
1899 kills = pet_scop_add_par(ctx, kills, kill);
1900 } else
1901 scop_i = mark_exposed(scop_i);
1903 scop_i = pet_scop_prefix(scop_i, i);
1904 scop = pet_scop_add_seq(ctx, scop, scop_i);
1906 scop = pet_skip_info_seq_add(&skip, scop, i);
1908 if (!scop)
1909 break;
1911 isl_set_free(domain);
1913 kills = pet_scop_prefix(kills, tree->u.b.n);
1914 scop = pet_scop_add_seq(ctx, scop, kills);
1916 pet_context_free(pc);
1918 return scop;
1921 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1922 * within the context "pc" by calling the appropriate function
1923 * based on the type of "tree".
1925 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
1926 __isl_keep pet_context *pc, struct pet_state *state)
1928 if (!tree)
1929 return NULL;
1931 switch (tree->type) {
1932 case pet_tree_error:
1933 return NULL;
1934 case pet_tree_block:
1935 return scop_from_block(tree, pc, state);
1936 case pet_tree_break:
1937 return scop_from_break(tree, pet_context_get_space(pc));
1938 case pet_tree_continue:
1939 return scop_from_continue(tree, pet_context_get_space(pc));
1940 case pet_tree_decl:
1941 case pet_tree_decl_init:
1942 return scop_from_decl(tree, pc, state);
1943 case pet_tree_expr:
1944 return scop_from_expr(pet_expr_copy(tree->u.e.expr),
1945 isl_id_copy(tree->label),
1946 state->n_stmt++,
1947 pet_tree_get_loc(tree), pc);
1948 case pet_tree_if:
1949 case pet_tree_if_else:
1950 return scop_from_if(tree, pc, state);
1951 case pet_tree_for:
1952 return scop_from_for(tree, pc, state);
1953 case pet_tree_while:
1954 return scop_from_while(tree, pc, state);
1955 case pet_tree_infinite_loop:
1956 return scop_from_infinite_for(tree, pc, state);
1959 isl_die(tree->ctx, isl_error_internal, "unhandled type",
1960 return NULL);
1963 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1964 * "int_size" is the number of bytes need to represent an integer.
1965 * "extract_array" is a callback that we can use to create a pet_array
1966 * that corresponds to the variable accessed by an expression.
1968 * Initialize the global state, construct a context and then
1969 * construct the pet_scop by recursively visiting the tree.
1971 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
1972 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
1973 __isl_keep pet_context *pc, void *user), void *user,
1974 __isl_keep pet_context *pc)
1976 struct pet_scop *scop;
1977 struct pet_state state = { 0 };
1979 if (!tree)
1980 return NULL;
1982 state.ctx = pet_tree_get_ctx(tree);
1983 state.int_size = int_size;
1984 state.extract_array = extract_array;
1985 state.user = user;
1987 scop = scop_from_tree(tree, pc, &state);
1988 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
1990 pet_tree_free(tree);
1992 return scop;