pet 0.05
[pet.git] / tree2scop.c
blobfad56620135aa42a96a4102438145a0982dabf47
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, clear any previously assigned values to variables
48 * that are written by "stmt".
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_tree(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 /* Wrapper around pet_expr_resolve_assume
72 * for use as a callback to pet_tree_map_expr.
74 static __isl_give pet_expr *resolve_assume(__isl_take pet_expr *expr,
75 void *user)
77 pet_context *pc = user;
79 return pet_expr_resolve_assume(expr, pc);
82 /* Check if any expression inside "tree" is an assume expression and
83 * if its single argument can be converted to an affine expression
84 * in the context of "pc".
85 * If so, replace the argument by the affine expression.
87 __isl_give pet_tree *pet_tree_resolve_assume(__isl_take pet_tree *tree,
88 __isl_keep pet_context *pc)
90 return pet_tree_map_expr(tree, &resolve_assume, pc);
93 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
94 * "tree" has already been evaluated in the context of "pc".
95 * This mainly involves resolving nested expression parameters
96 * and setting the name of the iteration space.
97 * The name is given by tree->label if it is non-NULL. Otherwise,
98 * it is of the form S_<stmt_nr>.
100 static struct pet_scop *scop_from_evaluated_tree(__isl_take pet_tree *tree,
101 int stmt_nr, __isl_keep pet_context *pc)
103 isl_space *space;
104 isl_set *domain;
105 struct pet_stmt *ps;
107 space = pet_context_get_space(pc);
109 tree = pet_tree_resolve_nested(tree, space);
110 tree = pet_tree_resolve_assume(tree, pc);
112 domain = pet_context_get_domain(pc);
113 ps = pet_stmt_from_pet_tree(domain, stmt_nr, tree);
114 return pet_scop_from_pet_stmt(space, ps);
117 /* Convert a top-level pet_expr to a pet_scop with one statement
118 * within the context "pc".
119 * "expr" has already been evaluated in the context of "pc".
120 * We construct a pet_tree from "expr" and continue with
121 * scop_from_evaluated_tree.
122 * The name is of the form S_<stmt_nr>.
123 * The location of the statement is set to "loc".
125 static struct pet_scop *scop_from_evaluated_expr(__isl_take pet_expr *expr,
126 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
128 pet_tree *tree;
130 tree = pet_tree_new_expr(expr);
131 tree = pet_tree_set_loc(tree, loc);
132 return scop_from_evaluated_tree(tree, stmt_nr, pc);
135 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
136 * "tree" has not yet been evaluated in the context of "pc".
137 * We evaluate "tree" in the context of "pc" and continue with
138 * scop_from_evaluated_tree.
139 * The statement name is given by tree->label if it is non-NULL. Otherwise,
140 * it is of the form S_<stmt_nr>.
142 static struct pet_scop *scop_from_unevaluated_tree(__isl_take pet_tree *tree,
143 int stmt_nr, __isl_keep pet_context *pc)
145 tree = pet_context_evaluate_tree(pc, tree);
146 return scop_from_evaluated_tree(tree, stmt_nr, pc);
149 /* Convert a top-level pet_expr to a pet_scop with one statement
150 * within the context "pc", where "expr" has not yet been evaluated
151 * in the context of "pc".
152 * We construct a pet_tree from "expr" and continue with
153 * scop_from_unevaluated_tree.
154 * The statement name is of the form S_<stmt_nr>.
155 * The location of the statement is set to "loc".
157 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
158 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
160 pet_tree *tree;
162 tree = pet_tree_new_expr(expr);
163 tree = pet_tree_set_loc(tree, loc);
164 return scop_from_unevaluated_tree(tree, stmt_nr, pc);
167 /* Construct a pet_scop with a single statement killing the entire
168 * array "array".
169 * The location of the statement is set to "loc".
171 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
172 __isl_keep pet_context *pc, struct pet_state *state)
174 isl_ctx *ctx;
175 isl_id *id;
176 isl_space *space;
177 isl_multi_pw_aff *index;
178 isl_map *access;
179 pet_expr *expr;
180 struct pet_scop *scop;
182 if (!array)
183 goto error;
184 ctx = isl_set_get_ctx(array->extent);
185 access = isl_map_from_range(isl_set_copy(array->extent));
186 id = isl_set_get_tuple_id(array->extent);
187 space = isl_space_alloc(ctx, 0, 0, 0);
188 space = isl_space_set_tuple_id(space, isl_dim_out, id);
189 index = isl_multi_pw_aff_zero(space);
190 expr = pet_expr_kill_from_access_and_index(access, index);
191 return scop_from_expr(expr, state->n_stmt++, loc, pc);
192 error:
193 pet_loc_free(loc);
194 return NULL;
197 /* Construct and return a pet_array corresponding to the variable
198 * accessed by "access" by calling the extract_array callback.
200 static struct pet_array *extract_array(__isl_keep pet_expr *access,
201 __isl_keep pet_context *pc, struct pet_state *state)
203 return state->extract_array(access, pc, state->user);
206 /* Construct a pet_scop for a (single) variable declaration
207 * within the context "pc".
209 * The scop contains the variable being declared (as an array)
210 * and a statement killing the array.
212 * If the declaration comes with an initialization, then the scop
213 * also contains an assignment to the variable.
215 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
216 __isl_keep pet_context *pc, struct pet_state *state)
218 int type_size;
219 isl_ctx *ctx;
220 struct pet_array *array;
221 struct pet_scop *scop_decl, *scop;
222 pet_expr *lhs, *rhs, *pe;
224 array = extract_array(tree->u.d.var, pc, state);
225 if (array)
226 array->declared = 1;
227 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
228 scop_decl = pet_scop_add_array(scop_decl, array);
230 if (tree->type != pet_tree_decl_init)
231 return scop_decl;
233 lhs = pet_expr_copy(tree->u.d.var);
234 rhs = pet_expr_copy(tree->u.d.init);
235 type_size = pet_expr_get_type_size(lhs);
236 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
237 scop = scop_from_expr(pe, state->n_stmt++, pet_tree_get_loc(tree), pc);
239 scop_decl = pet_scop_prefix(scop_decl, 0);
240 scop = pet_scop_prefix(scop, 1);
242 ctx = pet_tree_get_ctx(tree);
243 scop = pet_scop_add_seq(ctx, scop_decl, scop);
245 return scop;
248 /* Return those elements in the space of "cond" that come after
249 * (based on "sign") an element in "cond" in the final dimension.
251 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
253 isl_space *space;
254 isl_map *previous_to_this;
255 int i, dim;
257 dim = isl_set_dim(cond, isl_dim_set);
258 space = isl_space_map_from_set(isl_set_get_space(cond));
259 previous_to_this = isl_map_universe(space);
260 for (i = 0; i + 1 < dim; ++i)
261 previous_to_this = isl_map_equate(previous_to_this,
262 isl_dim_in, i, isl_dim_out, i);
263 if (sign > 0)
264 previous_to_this = isl_map_order_lt(previous_to_this,
265 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
266 else
267 previous_to_this = isl_map_order_gt(previous_to_this,
268 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
270 cond = isl_set_apply(cond, previous_to_this);
272 return cond;
275 /* Remove those iterations of "domain" that have an earlier iteration
276 * (based on "sign") in the final dimension where "skip" is satisfied.
277 * If "apply_skip_map" is set, then "skip_map" is first applied
278 * to the embedded skip condition before removing it from the domain.
280 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
281 __isl_take isl_set *skip, int sign,
282 int apply_skip_map, __isl_keep isl_map *skip_map)
284 if (apply_skip_map)
285 skip = isl_set_apply(skip, isl_map_copy(skip_map));
286 skip = isl_set_intersect(skip , isl_set_copy(domain));
287 return isl_set_subtract(domain, after(skip, sign));
290 /* Create an affine expression on the domain space of "pc" that
291 * is equal to the final dimension of this domain.
293 static __isl_give isl_aff *map_to_last(__isl_keep pet_context *pc)
295 int pos;
296 isl_space *space;
297 isl_local_space *ls;
299 space = pet_context_get_space(pc);
300 pos = isl_space_dim(space, isl_dim_set) - 1;
301 ls = isl_local_space_from_space(space);
302 return isl_aff_var_on_domain(ls, isl_dim_set, pos);
305 /* Create an affine expression that maps elements
306 * of an array "id_test" to the previous element in the final dimension
307 * (according to "inc"), provided this element belongs to "domain".
308 * That is, create the affine expression
310 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
312 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
313 __isl_take isl_set *domain, __isl_take isl_val *inc)
315 int pos;
316 isl_space *space;
317 isl_aff *aff;
318 isl_pw_aff *pa;
319 isl_multi_aff *ma;
320 isl_multi_pw_aff *prev;
322 pos = isl_set_dim(domain, isl_dim_set) - 1;
323 space = isl_set_get_space(domain);
324 space = isl_space_map_from_set(space);
325 ma = isl_multi_aff_identity(space);
326 aff = isl_multi_aff_get_aff(ma, pos);
327 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
328 ma = isl_multi_aff_set_aff(ma, pos, aff);
329 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
330 prev = isl_multi_pw_aff_from_multi_aff(ma);
331 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
332 pa = isl_pw_aff_intersect_domain(pa, domain);
333 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
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 (in the final dimension) also have that value.
342 * The set of previous elements is bounded by "domain".
343 * If "sign" is negative then the iterator
344 * is decreasing and we express that all subsequent array elements
345 * (but still defined previously) have the same value.
347 static struct pet_scop *add_implication(struct pet_scop *scop,
348 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
349 int satisfied)
351 int i, dim;
352 isl_space *space;
353 isl_map *map;
355 dim = isl_set_dim(domain, isl_dim_set);
356 domain = isl_set_set_tuple_id(domain, id_test);
357 space = isl_space_map_from_set(isl_set_get_space(domain));
358 map = isl_map_universe(space);
359 for (i = 0; i + 1 < dim; ++i)
360 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
361 if (sign > 0)
362 map = isl_map_order_ge(map,
363 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
364 else
365 map = isl_map_order_le(map,
366 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
367 map = isl_map_intersect_range(map, domain);
368 scop = pet_scop_add_implication(scop, map, satisfied);
370 return scop;
373 /* Add a filter to "scop" that imposes that it is only executed
374 * when the variable identified by "id_test" has a zero value
375 * for all previous iterations of "domain".
377 * In particular, add a filter that imposes that the array
378 * has a zero value at the previous iteration of domain and
379 * add an implication that implies that it then has that
380 * value for all previous iterations.
382 static struct pet_scop *scop_add_break(struct pet_scop *scop,
383 __isl_take isl_id *id_test, __isl_take isl_set *domain,
384 __isl_take isl_val *inc)
386 isl_multi_pw_aff *prev;
387 int sign = isl_val_sgn(inc);
389 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
390 scop = add_implication(scop, id_test, domain, sign, 0);
391 scop = pet_scop_filter(scop, prev, 0);
393 return scop;
396 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
397 __isl_keep pet_context *pc, struct pet_state *state);
399 /* Construct a pet_scop for an infinite loop around the given body
400 * within the context "pc".
402 * The domain of "pc" has already been extended with an infinite loop
404 * { [t] : t >= 0 }
406 * We extract a pet_scop for the body and then embed it in a loop with
407 * schedule
409 * { [outer,t] -> [t] }
411 * If the body contains any break, then it is taken into
412 * account in apply_affine_break (if the skip condition is affine)
413 * or in scop_add_break (if the skip condition is not affine).
415 * Note that in case of an affine skip condition,
416 * since we are dealing with a loop without loop iterator,
417 * the skip condition cannot refer to the current loop iterator and
418 * so effectively, the effect on the iteration domain is of the form
420 * { [outer,0]; [outer,t] : t >= 1 and not skip }
422 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
423 __isl_keep pet_context *pc, struct pet_state *state)
425 isl_ctx *ctx;
426 isl_id *id_test;
427 isl_set *domain;
428 isl_set *skip;
429 isl_aff *sched;
430 struct pet_scop *scop;
431 int has_affine_break;
432 int has_var_break;
434 ctx = pet_tree_get_ctx(body);
435 domain = pet_context_get_domain(pc);
436 sched = map_to_last(pc);
438 scop = scop_from_tree(body, pc, state);
440 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
441 if (has_affine_break)
442 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
443 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
444 if (has_var_break)
445 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
447 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
448 if (has_affine_break) {
449 domain = apply_affine_break(domain, skip, 1, 0, NULL);
450 scop = pet_scop_intersect_domain_prefix(scop,
451 isl_set_copy(domain));
453 if (has_var_break)
454 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
455 else
456 isl_set_free(domain);
458 return scop;
461 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
463 * for (;;)
464 * body
466 * within the context "pc".
468 * Extend the domain of "pc" with an extra inner loop
470 * { [t] : t >= 0 }
472 * and construct the scop in scop_from_infinite_loop.
474 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
475 __isl_keep pet_context *pc, struct pet_state *state)
477 struct pet_scop *scop;
479 pc = pet_context_copy(pc);
480 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
482 pc = pet_context_add_infinite_loop(pc);
484 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
486 pet_context_free(pc);
488 return scop;
491 /* Construct a pet_scop for a while loop of the form
493 * while (pa)
494 * body
496 * within the context "pc".
498 * The domain of "pc" has already been extended with an infinite loop
500 * { [t] : t >= 0 }
502 * Here, we add the constraints on the outer loop iterators
503 * implied by "pa" and construct the scop in scop_from_infinite_loop.
504 * Note that the intersection with these constraints
505 * may result in an empty loop.
507 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
508 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
509 struct pet_state *state)
511 struct pet_scop *scop;
512 isl_set *dom, *local;
513 isl_set *valid;
515 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
516 dom = isl_pw_aff_non_zero_set(pa);
517 local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
518 pc = pet_context_intersect_domain(pc, local);
519 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
520 scop = pet_scop_restrict(scop, dom);
521 scop = pet_scop_restrict_context(scop, valid);
523 pet_context_free(pc);
524 return scop;
527 /* Construct a scop for a while, given the scops for the condition
528 * and the body, the filter identifier and the iteration domain of
529 * the while loop.
531 * In particular, the scop for the condition is filtered to depend
532 * on "id_test" evaluating to true for all previous iterations
533 * of the loop, while the scop for the body is filtered to depend
534 * on "id_test" evaluating to true for all iterations up to the
535 * current iteration.
536 * The actual filter only imposes that this virtual array has
537 * value one on the previous or the current iteration.
538 * The fact that this condition also applies to the previous
539 * iterations is enforced by an implication.
541 * These filtered scops are then combined into a single scop.
543 * "sign" is positive if the iterator increases and negative
544 * if it decreases.
546 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
547 struct pet_scop *scop_body, __isl_take isl_id *id_test,
548 __isl_take isl_set *domain, __isl_take isl_val *inc)
550 isl_ctx *ctx = isl_set_get_ctx(domain);
551 isl_space *space;
552 isl_multi_pw_aff *test_index;
553 isl_multi_pw_aff *prev;
554 int sign = isl_val_sgn(inc);
555 struct pet_scop *scop;
557 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
558 scop_cond = pet_scop_filter(scop_cond, prev, 1);
560 space = isl_space_map_from_set(isl_set_get_space(domain));
561 test_index = isl_multi_pw_aff_identity(space);
562 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
563 isl_id_copy(id_test));
564 scop_body = pet_scop_filter(scop_body, test_index, 1);
566 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
567 scop = add_implication(scop, id_test, domain, sign, 1);
569 return scop;
572 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
573 * evaluating "cond" and writing the result to a virtual scalar,
574 * as expressed by "index".
575 * The expression "cond" has not yet been evaluated in the context of "pc".
576 * Do so within the context "pc".
577 * The location of the statement is set to "loc".
579 static struct pet_scop *scop_from_non_affine_condition(
580 __isl_take pet_expr *cond, int stmt_nr,
581 __isl_take isl_multi_pw_aff *index,
582 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
584 pet_expr *expr, *write;
586 cond = pet_context_evaluate_expr(pc, cond);
588 write = pet_expr_from_index(index);
589 write = pet_expr_access_set_write(write, 1);
590 write = pet_expr_access_set_read(write, 0);
591 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
593 return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
596 /* Construct a generic while scop, with iteration domain
597 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
598 * The domain of "pc" has already been extended with this infinite loop
600 * { [t] : t >= 0 }
602 * The scop consists of two parts,
603 * one for evaluating the condition "cond" and one for the body.
604 * If "expr_inc" is not NULL, then a scop for evaluating this expression
605 * is added at the end of the body,
606 * after replacing any skip conditions resulting from continue statements
607 * by the skip conditions resulting from break statements (if any).
609 * The schedule is adjusted to reflect that the condition is evaluated
610 * before the body is executed and the body is filtered to depend
611 * on the result of the condition evaluating to true on all iterations
612 * up to the current iteration, while the evaluation of the condition itself
613 * is filtered to depend on the result of the condition evaluating to true
614 * on all previous iterations.
615 * The context of the scop representing the body is dropped
616 * because we don't know how many times the body will be executed,
617 * if at all.
619 * If the body contains any break, then it is taken into
620 * account in apply_affine_break (if the skip condition is affine)
621 * or in scop_add_break (if the skip condition is not affine).
623 * Note that in case of an affine skip condition,
624 * since we are dealing with a loop without loop iterator,
625 * the skip condition cannot refer to the current loop iterator and
626 * so effectively, the effect on the iteration domain is of the form
628 * { [outer,0]; [outer,t] : t >= 1 and not skip }
630 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
631 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
632 __isl_take pet_expr *expr_inc, __isl_take pet_context *pc,
633 struct pet_state *state)
635 isl_ctx *ctx;
636 isl_id *id_test, *id_break_test;
637 isl_space *space;
638 isl_multi_pw_aff *test_index;
639 isl_set *domain;
640 isl_set *skip;
641 isl_aff *sched;
642 struct pet_scop *scop, *scop_body;
643 int has_affine_break;
644 int has_var_break;
646 ctx = state->ctx;
647 space = pet_context_get_space(pc);
648 test_index = pet_create_test_index(space, state->n_test++);
649 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
650 isl_multi_pw_aff_copy(test_index),
651 pet_loc_copy(loc), pc);
652 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
653 domain = pet_context_get_domain(pc);
654 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
655 test_index, state->int_size);
657 sched = map_to_last(pc);
659 scop_body = scop_from_tree(tree_body, pc, state);
661 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
662 if (has_affine_break)
663 skip = pet_scop_get_affine_skip_domain(scop_body,
664 pet_skip_later);
665 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
666 if (has_var_break)
667 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
669 scop = pet_scop_prefix(scop, 0);
670 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(sched));
671 scop_body = pet_scop_reset_context(scop_body);
672 scop_body = pet_scop_prefix(scop_body, 1);
673 if (expr_inc) {
674 struct pet_scop *scop_inc;
675 scop_inc = scop_from_expr(expr_inc, state->n_stmt++, loc, pc);
676 scop_inc = pet_scop_prefix(scop_inc, 2);
677 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
678 isl_multi_pw_aff *skip;
679 skip = pet_scop_get_skip(scop_body, pet_skip_later);
680 scop_body = pet_scop_set_skip(scop_body,
681 pet_skip_now, skip);
682 } else
683 pet_scop_reset_skip(scop_body, pet_skip_now);
684 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
685 } else
686 pet_loc_free(loc);
687 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain), sched);
689 if (has_affine_break) {
690 domain = apply_affine_break(domain, skip, 1, 0, NULL);
691 scop = pet_scop_intersect_domain_prefix(scop,
692 isl_set_copy(domain));
693 scop_body = pet_scop_intersect_domain_prefix(scop_body,
694 isl_set_copy(domain));
696 if (has_var_break) {
697 scop = scop_add_break(scop, isl_id_copy(id_break_test),
698 isl_set_copy(domain), isl_val_one(ctx));
699 scop_body = scop_add_break(scop_body, id_break_test,
700 isl_set_copy(domain), isl_val_one(ctx));
702 scop = scop_add_while(scop, scop_body, id_test, domain,
703 isl_val_one(ctx));
705 pet_context_free(pc);
706 return scop;
709 /* Check if the while loop is of the form
711 * while (affine expression)
712 * body
714 * If so, call scop_from_affine_while to construct a scop.
716 * Otherwise, pass control to scop_from_non_affine_while.
718 * "pc" is the context in which the affine expressions in the scop are created.
719 * The domain of "pc" is extended with an infinite loop
721 * { [t] : t >= 0 }
723 * before passing control to scop_from_affine_while or
724 * scop_from_non_affine_while.
726 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
727 __isl_keep pet_context *pc, struct pet_state *state)
729 pet_expr *cond_expr;
730 isl_pw_aff *pa;
732 if (!tree)
733 return NULL;
735 pc = pet_context_copy(pc);
736 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
738 cond_expr = pet_expr_copy(tree->u.l.cond);
739 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
740 pa = pet_expr_extract_affine_condition(cond_expr, pc);
741 pet_expr_free(cond_expr);
743 pc = pet_context_add_infinite_loop(pc);
745 if (!pa)
746 goto error;
748 if (!isl_pw_aff_involves_nan(pa))
749 return scop_from_affine_while(tree, pa, pc, state);
750 isl_pw_aff_free(pa);
751 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
752 pet_tree_get_loc(tree), tree->u.l.body, NULL,
753 pc, state);
754 error:
755 pet_context_free(pc);
756 return NULL;
759 /* Check whether "cond" expresses a simple loop bound
760 * on the final set dimension.
761 * In particular, if "up" is set then "cond" should contain only
762 * upper bounds on the final set dimension.
763 * Otherwise, it should contain only lower bounds.
765 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
767 int pos;
769 pos = isl_set_dim(cond, isl_dim_set) - 1;
770 if (isl_val_is_pos(inc))
771 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
772 else
773 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
776 /* Extend a condition on a given iteration of a loop to one that
777 * imposes the same condition on all previous iterations.
778 * "domain" expresses the lower [upper] bound on the iterations
779 * when inc is positive [negative] in its final dimension.
781 * In particular, we construct the condition (when inc is positive)
783 * forall i' : (domain(i') and i' <= i) => cond(i')
785 * (where "<=" applies to the final dimension)
786 * which is equivalent to
788 * not exists i' : domain(i') and i' <= i and not cond(i')
790 * We construct this set by subtracting the satisfying cond from domain,
791 * applying a map
793 * { [i'] -> [i] : i' <= i }
795 * and then subtracting the result from domain again.
797 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
798 __isl_take isl_set *domain, __isl_take isl_val *inc)
800 isl_space *space;
801 isl_map *previous_to_this;
802 int i, dim;
804 dim = isl_set_dim(cond, isl_dim_set);
805 space = isl_space_map_from_set(isl_set_get_space(cond));
806 previous_to_this = isl_map_universe(space);
807 for (i = 0; i + 1 < dim; ++i)
808 previous_to_this = isl_map_equate(previous_to_this,
809 isl_dim_in, i, isl_dim_out, i);
810 if (isl_val_is_pos(inc))
811 previous_to_this = isl_map_order_le(previous_to_this,
812 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
813 else
814 previous_to_this = isl_map_order_ge(previous_to_this,
815 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
817 cond = isl_set_subtract(isl_set_copy(domain), cond);
818 cond = isl_set_apply(cond, previous_to_this);
819 cond = isl_set_subtract(domain, cond);
821 isl_val_free(inc);
823 return cond;
826 /* Given an initial value of the form
828 * { [outer,i] -> init(outer) }
830 * construct a domain of the form
832 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
834 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
835 __isl_take isl_val *inc)
837 int dim;
838 isl_aff *aff;
839 isl_space *space;
840 isl_local_space *ls;
841 isl_set *set;
843 dim = isl_pw_aff_dim(init, isl_dim_in);
845 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
846 space = isl_pw_aff_get_domain_space(init);
847 ls = isl_local_space_from_space(space);
848 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
849 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
850 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
852 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
853 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
855 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
856 set = isl_set_project_out(set, isl_dim_set, dim, 1);
858 return set;
861 /* Assuming "cond" represents a bound on a loop where the loop
862 * iterator "iv" is incremented (or decremented) by one, check if wrapping
863 * is possible.
865 * Under the given assumptions, wrapping is only possible if "cond" allows
866 * for the last value before wrapping, i.e., 2^width - 1 in case of an
867 * increasing iterator and 0 in case of a decreasing iterator.
869 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
870 __isl_keep isl_val *inc)
872 int cw;
873 isl_ctx *ctx;
874 isl_val *limit;
875 isl_set *test;
877 test = isl_set_copy(cond);
879 ctx = isl_set_get_ctx(test);
880 if (isl_val_is_neg(inc))
881 limit = isl_val_zero(ctx);
882 else {
883 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
884 limit = isl_val_2exp(limit);
885 limit = isl_val_sub_ui(limit, 1);
888 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
889 cw = !isl_set_is_empty(test);
890 isl_set_free(test);
892 return cw;
895 /* Given a space
897 * { [outer, v] },
899 * construct the following affine expression on this space
901 * { [outer, v] -> [outer, v mod 2^width] }
903 * where width is the number of bits used to represent the values
904 * of the unsigned variable "iv".
906 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
907 __isl_keep pet_expr *iv)
909 int dim;
910 isl_ctx *ctx;
911 isl_val *mod;
912 isl_aff *aff;
913 isl_multi_aff *ma;
915 dim = isl_space_dim(space, isl_dim_set);
917 ctx = isl_space_get_ctx(space);
918 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
919 mod = isl_val_2exp(mod);
921 space = isl_space_map_from_set(space);
922 ma = isl_multi_aff_identity(space);
924 aff = isl_multi_aff_get_aff(ma, dim - 1);
925 aff = isl_aff_mod_val(aff, mod);
926 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
928 return ma;
931 /* Given two sets in the space
933 * { [l,i] },
935 * where l represents the outer loop iterators, compute the set
936 * of values of l that ensure that "set1" is a subset of "set2".
938 * set1 is a subset of set2 if
940 * forall i: set1(l,i) => set2(l,i)
942 * or
944 * not exists i: set1(l,i) and not set2(l,i)
946 * i.e.,
948 * not exists i: (set1 \ set2)(l,i)
950 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
951 __isl_take isl_set *set2)
953 int pos;
955 pos = isl_set_dim(set1, isl_dim_set) - 1;
956 set1 = isl_set_subtract(set1, set2);
957 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
958 return isl_set_complement(set1);
961 /* Compute the set of outer iterator values for which "cond" holds
962 * on the next iteration of the inner loop for each element of "dom".
964 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
965 * to the outer loop iterators), plug that into "cond"
966 * and then compute the set of outer iterators for which "dom" is a subset
967 * of the result.
969 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
970 __isl_take isl_set *dom, __isl_take isl_val *inc)
972 int pos;
973 isl_space *space;
974 isl_aff *aff;
975 isl_multi_aff *ma;
977 pos = isl_set_dim(dom, isl_dim_set) - 1;
978 space = isl_set_get_space(dom);
979 space = isl_space_map_from_set(space);
980 ma = isl_multi_aff_identity(space);
981 aff = isl_multi_aff_get_aff(ma, pos);
982 aff = isl_aff_add_constant_val(aff, inc);
983 ma = isl_multi_aff_set_aff(ma, pos, aff);
984 cond = isl_set_preimage_multi_aff(cond, ma);
986 return enforce_subset(dom, cond);
989 /* Extract the for loop "tree" as a while loop within the context "pc_init".
990 * In particular, "pc_init" represents the context of the loop,
991 * whereas "pc" represents the context of the body of the loop and
992 * has already had its domain extended with an infinite loop
994 * { [t] : t >= 0 }
996 * The for loop has the form
998 * for (iv = init; cond; iv += inc)
999 * body;
1001 * and is treated as
1003 * iv = init;
1004 * while (cond) {
1005 * body;
1006 * iv += inc;
1009 * except that the skips resulting from any continue statements
1010 * in body do not apply to the increment, but are replaced by the skips
1011 * resulting from break statements.
1013 * If the loop iterator is declared in the for loop, then it is killed before
1014 * and after the loop.
1016 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1017 __isl_keep pet_context *init_pc, __isl_take pet_context *pc,
1018 struct pet_state *state)
1020 int declared;
1021 isl_id *iv;
1022 pet_expr *expr_iv, *init, *inc;
1023 struct pet_scop *scop_init, *scop;
1024 int type_size;
1025 struct pet_array *array;
1026 struct pet_scop *scop_kill;
1028 iv = pet_expr_access_get_id(tree->u.l.iv);
1029 pc = pet_context_clear_value(pc, iv);
1031 declared = tree->u.l.declared;
1033 expr_iv = pet_expr_copy(tree->u.l.iv);
1034 type_size = pet_expr_get_type_size(expr_iv);
1035 init = pet_expr_copy(tree->u.l.init);
1036 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1037 scop_init = scop_from_expr(init, state->n_stmt++,
1038 pet_tree_get_loc(tree), init_pc);
1039 scop_init = pet_scop_prefix(scop_init, declared);
1041 expr_iv = pet_expr_copy(tree->u.l.iv);
1042 type_size = pet_expr_get_type_size(expr_iv);
1043 inc = pet_expr_copy(tree->u.l.inc);
1044 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1046 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1047 pet_tree_get_loc(tree), tree->u.l.body, inc,
1048 pet_context_copy(pc), state);
1050 scop = pet_scop_prefix(scop, declared + 1);
1051 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1053 pet_context_free(pc);
1055 if (!declared)
1056 return scop;
1058 array = extract_array(tree->u.l.iv, init_pc, state);
1059 if (array)
1060 array->declared = 1;
1061 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1062 scop_kill = pet_scop_prefix(scop_kill, 0);
1063 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1064 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1065 scop_kill = pet_scop_add_array(scop_kill, array);
1066 scop_kill = pet_scop_prefix(scop_kill, 3);
1067 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1069 return scop;
1072 /* Given an access expression "expr", is the variable accessed by
1073 * "expr" assigned anywhere inside "tree"?
1075 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1077 int assigned = 0;
1078 isl_id *id;
1080 id = pet_expr_access_get_id(expr);
1081 assigned = pet_tree_writes(tree, id);
1082 isl_id_free(id);
1084 return assigned;
1087 /* Are all nested access parameters in "pa" allowed given "tree".
1088 * In particular, is none of them written by anywhere inside "tree".
1090 * If "tree" has any continue nodes in the current loop level,
1091 * then no nested access parameters are allowed.
1092 * In particular, if there is any nested access in a guard
1093 * for a piece of code containing a "continue", then we want to introduce
1094 * a separate statement for evaluating this guard so that we can express
1095 * that the result is false for all previous iterations.
1097 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1098 __isl_keep pet_tree *tree)
1100 int i, nparam;
1102 if (!tree)
1103 return -1;
1105 if (!pet_nested_any_in_pw_aff(pa))
1106 return 1;
1108 if (pet_tree_has_continue(tree))
1109 return 0;
1111 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1112 for (i = 0; i < nparam; ++i) {
1113 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1114 pet_expr *expr;
1115 int allowed;
1117 if (!pet_nested_in_id(id)) {
1118 isl_id_free(id);
1119 continue;
1122 expr = pet_nested_extract_expr(id);
1123 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1124 !is_assigned(expr, tree);
1126 pet_expr_free(expr);
1127 isl_id_free(id);
1129 if (!allowed)
1130 return 0;
1133 return 1;
1136 /* Internal data structure for collect_local.
1137 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1138 * "local" collects the results.
1140 struct pet_tree_collect_local_data {
1141 pet_context *pc;
1142 struct pet_state *state;
1143 isl_union_set *local;
1146 /* Add the variable accessed by "var" to data->local.
1147 * We extract a representation of the variable from
1148 * the pet_array constructed using extract_array
1149 * to ensure consistency with the rest of the scop.
1151 static int add_local(struct pet_tree_collect_local_data *data,
1152 __isl_keep pet_expr *var)
1154 struct pet_array *array;
1155 isl_set *universe;
1157 array = extract_array(var, data->pc, data->state);
1158 if (!array)
1159 return -1;
1161 universe = isl_set_universe(isl_set_get_space(array->extent));
1162 data->local = isl_union_set_add_set(data->local, universe);
1163 pet_array_free(array);
1165 return 0;
1168 /* If the node "tree" declares a variable, then add it to
1169 * data->local.
1171 static int extract_local_var(__isl_keep pet_tree *tree, void *user)
1173 enum pet_tree_type type;
1174 struct pet_tree_collect_local_data *data = user;
1176 type = pet_tree_get_type(tree);
1177 if (type == pet_tree_decl || type == pet_tree_decl_init)
1178 return add_local(data, tree->u.d.var);
1180 return 0;
1183 /* If the node "tree" is a for loop that declares its induction variable,
1184 * then add it this induction variable to data->local.
1186 static int extract_local_iterator(__isl_keep pet_tree *tree, void *user)
1188 struct pet_tree_collect_local_data *data = user;
1190 if (pet_tree_get_type(tree) == pet_tree_for && tree->u.l.declared)
1191 return add_local(data, tree->u.l.iv);
1193 return 0;
1196 /* Collect and return all local variables of the for loop represented
1197 * by "tree", with "scop" the corresponding pet_scop.
1198 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1200 * We collect not only the variables that are declared inside "tree",
1201 * but also the loop iterators that are declared anywhere inside
1202 * any possible macro statements in "scop".
1203 * The latter also appear as declared variable in the scop,
1204 * whereas other declared loop iterators only appear implicitly
1205 * in the iteration domains.
1207 static __isl_give isl_union_set *collect_local(struct pet_scop *scop,
1208 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1209 struct pet_state *state)
1211 int i;
1212 isl_ctx *ctx;
1213 struct pet_tree_collect_local_data data = { pc, state };
1215 ctx = pet_tree_get_ctx(tree);
1216 data.local = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
1218 if (pet_tree_foreach_sub_tree(tree, &extract_local_var, &data) < 0)
1219 return isl_union_set_free(data.local);
1221 for (i = 0; i < scop->n_stmt; ++i) {
1222 pet_tree *body = scop->stmts[i]->body;
1223 if (pet_tree_foreach_sub_tree(body, &extract_local_iterator,
1224 &data) < 0)
1225 return isl_union_set_free(data.local);
1228 return data.local;
1231 /* Add an independence to "scop" if the for node "tree" was marked
1232 * independent.
1233 * "domain" is the set of loop iterators, with the current for loop
1234 * innermost. If "sign" is positive, then the inner iterator increases.
1235 * Otherwise it decreases.
1236 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1238 * If the tree was marked, then collect all local variables and
1239 * add an independence.
1241 static struct pet_scop *set_independence(struct pet_scop *scop,
1242 __isl_keep pet_tree *tree, __isl_keep isl_set *domain, int sign,
1243 __isl_keep pet_context *pc, struct pet_state *state)
1245 isl_union_set *local;
1247 if (!tree->u.l.independent)
1248 return scop;
1250 local = collect_local(scop, tree, pc, state);
1251 scop = pet_scop_set_independent(scop, domain, local, sign);
1253 return scop;
1256 /* Construct a pet_scop for a for tree with static affine initialization
1257 * and constant increment within the context "pc".
1258 * The domain of "pc" has already been extended with an (at this point
1259 * unbounded) inner loop iterator corresponding to the current for loop.
1261 * The condition is allowed to contain nested accesses, provided
1262 * they are not being written to inside the body of the loop.
1263 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1264 * essentially treated as a while loop, with iteration domain
1265 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1267 * We extract a pet_scop for the body after intersecting the domain of "pc"
1269 * { [l,i] : i >= init and condition' }
1271 * or
1273 * { [l,i] : i <= init and condition' }
1275 * Where condition' is equal to condition if the latter is
1276 * a simple upper [lower] bound and a condition that is extended
1277 * to apply to all previous iterations otherwise.
1278 * Afterwards, the schedule of the pet_scop is extended with
1280 * { [l,i] -> [i] }
1282 * or
1284 * { [l,i] -> [-i] }
1286 * If the condition is non-affine, then we drop the condition from the
1287 * iteration domain and instead create a separate statement
1288 * for evaluating the condition. The body is then filtered to depend
1289 * on the result of the condition evaluating to true on all iterations
1290 * up to the current iteration, while the evaluation the condition itself
1291 * is filtered to depend on the result of the condition evaluating to true
1292 * on all previous iterations.
1293 * The context of the scop representing the body is dropped
1294 * because we don't know how many times the body will be executed,
1295 * if at all.
1297 * If the stride of the loop is not 1, then "i >= init" is replaced by
1299 * (exists a: i = init + stride * a and a >= 0)
1301 * If the loop iterator i is unsigned, then wrapping may occur.
1302 * We therefore use a virtual iterator instead that does not wrap.
1303 * However, the condition in the code applies
1304 * to the wrapped value, so we need to change condition(l,i)
1305 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1306 * to the original iterator by the wrapping of the virtual iterator.
1307 * Note that there may be no need to perform this final wrapping
1308 * if the loop condition (after wrapping) satisfies certain conditions.
1309 * However, the is_simple_bound condition is not enough since it doesn't
1310 * check if there even is an upper bound.
1312 * Wrapping on unsigned iterators can be avoided entirely if
1313 * loop condition is simple, the loop iterator is incremented
1314 * [decremented] by one and the last value before wrapping cannot
1315 * possibly satisfy the loop condition.
1317 * Valid outer iterators for a for loop are those for which the initial
1318 * value itself, the increment on each domain iteration and
1319 * the condition on both the initial value and
1320 * the result of incrementing the iterator for each iteration of the domain
1321 * can be evaluated.
1322 * If the loop condition is non-affine, then we only consider validity
1323 * of the initial value.
1325 * If the body contains any break, then we keep track of it in "skip"
1326 * (if the skip condition is affine) or it is handled in scop_add_break
1327 * (if the skip condition is not affine).
1328 * Note that the affine break condition needs to be considered with
1329 * respect to previous iterations in the virtual domain (if any).
1331 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1332 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1333 __isl_take isl_val *inc, __isl_take pet_context *pc,
1334 struct pet_state *state)
1336 isl_set *domain;
1337 isl_aff *sched;
1338 isl_set *cond = NULL;
1339 isl_set *skip = NULL;
1340 isl_id *id_test = NULL, *id_break_test;
1341 struct pet_scop *scop, *scop_cond = NULL;
1342 int pos;
1343 int is_one;
1344 int is_unsigned;
1345 int is_simple;
1346 int is_virtual;
1347 int is_non_affine;
1348 int has_affine_break;
1349 int has_var_break;
1350 isl_map *rev_wrap = NULL;
1351 isl_map *init_val_map;
1352 isl_pw_aff *pa;
1353 isl_set *valid_init;
1354 isl_set *valid_cond;
1355 isl_set *valid_cond_init;
1356 isl_set *valid_cond_next;
1357 isl_set *valid_inc;
1358 pet_expr *cond_expr;
1359 pet_context *pc_nested;
1361 pos = pet_context_dim(pc) - 1;
1363 domain = pet_context_get_domain(pc);
1364 cond_expr = pet_expr_copy(tree->u.l.cond);
1365 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1366 pc_nested = pet_context_copy(pc);
1367 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1368 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1369 pet_context_free(pc_nested);
1370 pet_expr_free(cond_expr);
1372 valid_inc = isl_pw_aff_domain(pa_inc);
1374 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1376 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1377 !is_nested_allowed(pa, tree->u.l.body);
1378 if (is_non_affine)
1379 pa = isl_pw_aff_free(pa);
1381 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1382 cond = isl_pw_aff_non_zero_set(pa);
1383 if (is_non_affine)
1384 cond = isl_set_universe(isl_set_get_space(domain));
1386 valid_cond = isl_set_coalesce(valid_cond);
1387 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1388 is_virtual = is_unsigned &&
1389 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1391 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1392 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1393 isl_dim_out, 0);
1394 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1395 isl_set_copy(valid_cond));
1396 if (is_one && !is_virtual) {
1397 isl_set *cond;
1399 isl_pw_aff_free(init_val);
1400 pa = pet_expr_extract_comparison(
1401 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1402 tree->u.l.iv, tree->u.l.init, pc);
1403 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1404 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1405 isl_set_dim(domain, isl_dim_set) - 1, 1);
1406 cond = isl_pw_aff_non_zero_set(pa);
1407 domain = isl_set_intersect(domain, cond);
1408 } else {
1409 isl_set *strided;
1411 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1412 strided = strided_domain(init_val, isl_val_copy(inc));
1413 domain = isl_set_intersect(domain, strided);
1416 if (is_virtual) {
1417 isl_multi_aff *wrap;
1418 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1419 pc = pet_context_preimage_domain(pc, wrap);
1420 rev_wrap = isl_map_from_multi_aff(wrap);
1421 rev_wrap = isl_map_reverse(rev_wrap);
1422 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1423 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1424 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1426 is_simple = is_simple_bound(cond, inc);
1427 if (!is_simple) {
1428 cond = isl_set_gist(cond, isl_set_copy(domain));
1429 is_simple = is_simple_bound(cond, inc);
1431 if (!is_simple)
1432 cond = valid_for_each_iteration(cond,
1433 isl_set_copy(domain), isl_val_copy(inc));
1434 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1435 domain = isl_set_intersect(domain, cond);
1436 sched = map_to_last(pc);
1437 if (isl_val_is_neg(inc))
1438 sched = isl_aff_neg(sched);
1440 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1441 isl_val_copy(inc));
1442 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1444 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1446 if (is_non_affine) {
1447 isl_space *space;
1448 isl_multi_pw_aff *test_index;
1449 space = isl_set_get_space(domain);
1450 test_index = pet_create_test_index(space, state->n_test++);
1451 scop_cond = scop_from_non_affine_condition(
1452 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1453 isl_multi_pw_aff_copy(test_index),
1454 pet_tree_get_loc(tree), pc);
1455 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1456 isl_dim_out);
1457 scop_cond = pet_scop_add_boolean_array(scop_cond,
1458 isl_set_copy(domain), test_index,
1459 state->int_size);
1460 scop_cond = pet_scop_prefix(scop_cond, 0);
1461 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1462 isl_aff_copy(sched));
1465 scop = scop_from_tree(tree->u.l.body, pc, state);
1466 has_affine_break = scop &&
1467 pet_scop_has_affine_skip(scop, pet_skip_later);
1468 if (has_affine_break)
1469 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1470 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1471 if (has_var_break)
1472 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1473 if (is_non_affine) {
1474 scop = pet_scop_reset_context(scop);
1475 scop = pet_scop_prefix(scop, 1);
1477 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
1478 scop = pet_scop_resolve_nested(scop);
1479 if (has_affine_break) {
1480 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1481 is_virtual, rev_wrap);
1482 scop = pet_scop_intersect_domain_prefix(scop,
1483 isl_set_copy(domain));
1485 isl_map_free(rev_wrap);
1486 if (has_var_break)
1487 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1488 isl_val_copy(inc));
1489 if (is_non_affine) {
1490 scop = scop_add_while(scop_cond, scop, id_test, domain,
1491 isl_val_copy(inc));
1492 isl_set_free(valid_inc);
1493 } else {
1494 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1495 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1496 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1497 scop = pet_scop_restrict_context(scop, valid_inc);
1498 scop = set_independence(scop, tree, domain, isl_val_sgn(inc),
1499 pc, state);
1500 isl_set_free(domain);
1503 isl_val_free(inc);
1505 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1506 scop = pet_scop_restrict_context(scop, valid_init);
1508 pet_context_free(pc);
1509 return scop;
1512 /* Construct a pet_scop for a for statement within the context of "pc".
1514 * We update the context to reflect the writes to the loop variable and
1515 * the writes inside the body.
1517 * Then we check if the initialization of the for loop
1518 * is a static affine value and the increment is a constant.
1519 * If so, we construct the pet_scop using scop_from_affine_for.
1520 * Otherwise, we treat the for loop as a while loop
1521 * in scop_from_non_affine_for.
1523 * Note that the initialization and the increment are extracted
1524 * in a context where the current loop iterator has been added
1525 * to the context. If these turn out not be affine, then we
1526 * have reconstruct the body context without an assignment
1527 * to this loop iterator, as this variable will then not be
1528 * treated as a dimension of the iteration domain, but as any
1529 * other variable.
1531 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1532 __isl_keep pet_context *init_pc, struct pet_state *state)
1534 isl_id *iv;
1535 isl_val *inc;
1536 isl_pw_aff *pa_inc, *init_val;
1537 pet_context *pc, *pc_init_val;
1539 if (!tree)
1540 return NULL;
1542 iv = pet_expr_access_get_id(tree->u.l.iv);
1543 pc = pet_context_copy(init_pc);
1544 pc = pet_context_add_inner_iterator(pc, iv);
1545 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1547 pc_init_val = pet_context_copy(pc);
1548 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1549 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1550 pet_context_free(pc_init_val);
1551 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1552 inc = pet_extract_cst(pa_inc);
1553 if (!pa_inc || !init_val || !inc)
1554 goto error;
1555 if (!isl_pw_aff_involves_nan(pa_inc) &&
1556 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1557 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1558 pc, state);
1560 isl_pw_aff_free(pa_inc);
1561 isl_pw_aff_free(init_val);
1562 isl_val_free(inc);
1563 pet_context_free(pc);
1565 pc = pet_context_copy(init_pc);
1566 pc = pet_context_add_infinite_loop(pc);
1567 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1568 return scop_from_non_affine_for(tree, init_pc, pc, state);
1569 error:
1570 isl_pw_aff_free(pa_inc);
1571 isl_pw_aff_free(init_val);
1572 isl_val_free(inc);
1573 pet_context_free(pc);
1574 return NULL;
1577 /* Check whether "expr" is an affine constraint within the context "pc".
1579 static int is_affine_condition(__isl_keep pet_expr *expr,
1580 __isl_keep pet_context *pc)
1582 isl_pw_aff *pa;
1583 int is_affine;
1585 pa = pet_expr_extract_affine_condition(expr, pc);
1586 if (!pa)
1587 return -1;
1588 is_affine = !isl_pw_aff_involves_nan(pa);
1589 isl_pw_aff_free(pa);
1591 return is_affine;
1594 /* Check if the given if statement is a conditional assignement
1595 * with a non-affine condition.
1597 * In particular we check if "stmt" is of the form
1599 * if (condition)
1600 * a = f(...);
1601 * else
1602 * a = g(...);
1604 * where the condition is non-affine and a is some array or scalar access.
1606 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1607 __isl_keep pet_context *pc)
1609 int equal;
1610 isl_ctx *ctx;
1611 pet_expr *expr1, *expr2;
1613 ctx = pet_tree_get_ctx(tree);
1614 if (!pet_options_get_detect_conditional_assignment(ctx))
1615 return 0;
1616 if (tree->type != pet_tree_if_else)
1617 return 0;
1618 if (tree->u.i.then_body->type != pet_tree_expr)
1619 return 0;
1620 if (tree->u.i.else_body->type != pet_tree_expr)
1621 return 0;
1622 expr1 = tree->u.i.then_body->u.e.expr;
1623 expr2 = tree->u.i.else_body->u.e.expr;
1624 if (pet_expr_get_type(expr1) != pet_expr_op)
1625 return 0;
1626 if (pet_expr_get_type(expr2) != pet_expr_op)
1627 return 0;
1628 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1629 return 0;
1630 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1631 return 0;
1632 expr1 = pet_expr_get_arg(expr1, 0);
1633 expr2 = pet_expr_get_arg(expr2, 0);
1634 equal = pet_expr_is_equal(expr1, expr2);
1635 pet_expr_free(expr1);
1636 pet_expr_free(expr2);
1637 if (equal < 0 || !equal)
1638 return 0;
1639 if (is_affine_condition(tree->u.i.cond, pc))
1640 return 0;
1642 return 1;
1645 /* Given that "tree" is of the form
1647 * if (condition)
1648 * a = f(...);
1649 * else
1650 * a = g(...);
1652 * where a is some array or scalar access, construct a pet_scop
1653 * corresponding to this conditional assignment within the context "pc".
1655 * The constructed pet_scop then corresponds to the expression
1657 * a = condition ? f(...) : g(...)
1659 * All access relations in f(...) are intersected with condition
1660 * while all access relation in g(...) are intersected with the complement.
1662 static struct pet_scop *scop_from_conditional_assignment(
1663 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1664 struct pet_state *state)
1666 int type_size;
1667 isl_pw_aff *pa;
1668 isl_set *cond, *comp;
1669 isl_multi_pw_aff *index;
1670 pet_expr *expr1, *expr2;
1671 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1672 pet_context *pc_nested;
1673 struct pet_scop *scop;
1675 pe_cond = pet_expr_copy(tree->u.i.cond);
1676 pe_cond = pet_context_evaluate_expr(pc, pe_cond);
1677 pc_nested = pet_context_copy(pc);
1678 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1679 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1680 pet_context_free(pc_nested);
1681 pet_expr_free(pe_cond);
1682 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1683 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1684 index = isl_multi_pw_aff_from_pw_aff(pa);
1686 expr1 = tree->u.i.then_body->u.e.expr;
1687 expr2 = tree->u.i.else_body->u.e.expr;
1689 pe_cond = pet_expr_from_index(index);
1691 pe_then = pet_expr_get_arg(expr1, 1);
1692 pe_then = pet_context_evaluate_expr(pc, pe_then);
1693 pe_then = pet_expr_restrict(pe_then, cond);
1694 pe_else = pet_expr_get_arg(expr2, 1);
1695 pe_else = pet_context_evaluate_expr(pc, pe_else);
1696 pe_else = pet_expr_restrict(pe_else, comp);
1697 pe_write = pet_expr_get_arg(expr1, 0);
1698 pe_write = pet_context_evaluate_expr(pc, pe_write);
1700 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1701 type_size = pet_expr_get_type_size(pe_write);
1702 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1704 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
1705 pet_tree_get_loc(tree), pc);
1707 pet_context_free(pc);
1709 return scop;
1712 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1714 * We create a separate statement that writes the result
1715 * of the non-affine condition to a virtual scalar.
1716 * A constraint requiring the value of this virtual scalar to be one
1717 * is added to the iteration domains of the then branch.
1718 * Similarly, a constraint requiring the value of this virtual scalar
1719 * to be zero is added to the iteration domains of the else branch, if any.
1720 * We adjust the schedules to ensure that the virtual scalar is written
1721 * before it is read.
1723 * If there are any breaks or continues in the then and/or else
1724 * branches, then we may have to compute a new skip condition.
1725 * This is handled using a pet_skip_info object.
1726 * On initialization, the object checks if skip conditions need
1727 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1728 * adds them in pet_skip_info_if_add.
1730 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1731 __isl_take pet_context *pc, struct pet_state *state)
1733 int has_else;
1734 isl_space *space;
1735 isl_set *domain;
1736 isl_multi_pw_aff *test_index;
1737 struct pet_skip_info skip;
1738 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1740 has_else = tree->type == pet_tree_if_else;
1742 space = pet_context_get_space(pc);
1743 test_index = pet_create_test_index(space, state->n_test++);
1744 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1745 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1746 pet_tree_get_loc(tree), pc);
1747 domain = pet_context_get_domain(pc);
1748 scop = pet_scop_add_boolean_array(scop, domain,
1749 isl_multi_pw_aff_copy(test_index), state->int_size);
1751 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1752 if (has_else)
1753 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1755 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1756 has_else, 0);
1757 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1759 scop = pet_scop_prefix(scop, 0);
1760 scop_then = pet_scop_prefix(scop_then, 1);
1761 scop_then = pet_scop_filter(scop_then,
1762 isl_multi_pw_aff_copy(test_index), 1);
1763 if (has_else) {
1764 scop_else = pet_scop_prefix(scop_else, 1);
1765 scop_else = pet_scop_filter(scop_else, test_index, 0);
1766 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1767 } else
1768 isl_multi_pw_aff_free(test_index);
1770 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1772 scop = pet_skip_info_if_add(&skip, scop, 2);
1774 pet_context_free(pc);
1775 return scop;
1778 /* Construct a pet_scop for an affine if statement within the context "pc".
1780 * The condition is added to the iteration domains of the then branch,
1781 * while the opposite of the condition in added to the iteration domains
1782 * of the else branch, if any.
1784 * If there are any breaks or continues in the then and/or else
1785 * branches, then we may have to compute a new skip condition.
1786 * This is handled using a pet_skip_info_if object.
1787 * On initialization, the object checks if skip conditions need
1788 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1789 * adds them in pet_skip_info_if_add.
1791 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1792 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1793 struct pet_state *state)
1795 int has_else;
1796 isl_ctx *ctx;
1797 isl_set *set, *complement;
1798 isl_set *valid;
1799 struct pet_skip_info skip;
1800 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1801 pet_context *pc_body;
1803 ctx = pet_tree_get_ctx(tree);
1805 has_else = tree->type == pet_tree_if_else;
1807 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1808 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1810 pc_body = pet_context_copy(pc);
1811 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
1812 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
1813 pet_context_free(pc_body);
1814 if (has_else) {
1815 pc_body = pet_context_copy(pc);
1816 complement = isl_set_copy(valid);
1817 complement = isl_set_subtract(valid, isl_set_copy(set));
1818 pc_body = pet_context_intersect_domain(pc_body,
1819 isl_set_copy(complement));
1820 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
1821 pet_context_free(pc_body);
1824 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1825 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
1826 isl_pw_aff_free(cond);
1828 scop = pet_scop_restrict(scop_then, set);
1830 if (has_else) {
1831 scop_else = pet_scop_restrict(scop_else, complement);
1832 scop = pet_scop_add_par(ctx, scop, scop_else);
1834 scop = pet_scop_resolve_nested(scop);
1835 scop = pet_scop_restrict_context(scop, valid);
1837 if (pet_skip_info_has_skip(&skip))
1838 scop = pet_scop_prefix(scop, 0);
1839 scop = pet_skip_info_if_add(&skip, scop, 1);
1841 pet_context_free(pc);
1842 return scop;
1845 /* Construct a pet_scop for an if statement within the context "pc".
1847 * If the condition fits the pattern of a conditional assignment,
1848 * then it is handled by scop_from_conditional_assignment.
1850 * Otherwise, we check if the condition is affine.
1851 * If so, we construct the scop in scop_from_affine_if.
1852 * Otherwise, we construct the scop in scop_from_non_affine_if.
1854 * We allow the condition to be dynamic, i.e., to refer to
1855 * scalars or array elements that may be written to outside
1856 * of the given if statement. These nested accesses are then represented
1857 * as output dimensions in the wrapping iteration domain.
1858 * If it is also written _inside_ the then or else branch, then
1859 * we treat the condition as non-affine.
1860 * As explained in extract_non_affine_if, this will introduce
1861 * an extra statement.
1862 * For aesthetic reasons, we want this statement to have a statement
1863 * number that is lower than those of the then and else branches.
1864 * In order to evaluate if we will need such a statement, however, we
1865 * first construct scops for the then and else branches.
1866 * We therefore reserve a statement number if we might have to
1867 * introduce such an extra statement.
1869 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1870 __isl_keep pet_context *pc, struct pet_state *state)
1872 int has_else;
1873 isl_pw_aff *cond;
1874 pet_expr *cond_expr;
1875 pet_context *pc_nested;
1877 if (!tree)
1878 return NULL;
1880 has_else = tree->type == pet_tree_if_else;
1882 pc = pet_context_copy(pc);
1883 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1884 if (has_else)
1885 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1887 if (is_conditional_assignment(tree, pc))
1888 return scop_from_conditional_assignment(tree, pc, state);
1890 cond_expr = pet_expr_copy(tree->u.i.cond);
1891 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1892 pc_nested = pet_context_copy(pc);
1893 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1894 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1895 pet_context_free(pc_nested);
1896 pet_expr_free(cond_expr);
1898 if (!cond) {
1899 pet_context_free(pc);
1900 return NULL;
1903 if (isl_pw_aff_involves_nan(cond)) {
1904 isl_pw_aff_free(cond);
1905 return scop_from_non_affine_if(tree, pc, state);
1908 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1909 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1910 isl_pw_aff_free(cond);
1911 return scop_from_non_affine_if(tree, pc, state);
1914 return scop_from_affine_if(tree, cond, pc, state);
1917 /* Return a one-dimensional multi piecewise affine expression that is equal
1918 * to the constant 1 and is defined over the given domain.
1920 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
1922 isl_local_space *ls;
1923 isl_aff *aff;
1925 ls = isl_local_space_from_space(space);
1926 aff = isl_aff_zero_on_domain(ls);
1927 aff = isl_aff_set_constant_si(aff, 1);
1929 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1932 /* Construct a pet_scop for a continue statement with the given domain space.
1934 * We simply create an empty scop with a universal pet_skip_now
1935 * skip condition. This skip condition will then be taken into
1936 * account by the enclosing loop construct, possibly after
1937 * being incorporated into outer skip conditions.
1939 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
1940 __isl_take isl_space *space)
1942 struct pet_scop *scop;
1944 scop = pet_scop_empty(isl_space_copy(space));
1946 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
1948 return scop;
1951 /* Construct a pet_scop for a break statement with the given domain space.
1953 * We simply create an empty scop with both a universal pet_skip_now
1954 * skip condition and a universal pet_skip_later skip condition.
1955 * These skip conditions will then be taken into
1956 * account by the enclosing loop construct, possibly after
1957 * being incorporated into outer skip conditions.
1959 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
1960 __isl_take isl_space *space)
1962 struct pet_scop *scop;
1963 isl_multi_pw_aff *skip;
1965 scop = pet_scop_empty(isl_space_copy(space));
1967 skip = one_mpa(space);
1968 scop = pet_scop_set_skip(scop, pet_skip_now,
1969 isl_multi_pw_aff_copy(skip));
1970 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1972 return scop;
1975 /* Extract a clone of the kill statement in "scop".
1976 * The domain of the clone is given by "domain".
1977 * "scop" is expected to have been created from a DeclStmt
1978 * and should have the kill as its first statement.
1980 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
1981 struct pet_scop *scop, struct pet_state *state)
1983 pet_expr *kill;
1984 struct pet_stmt *stmt;
1985 isl_multi_pw_aff *index;
1986 isl_map *access;
1987 pet_expr *expr, *arg;
1988 pet_tree *tree;
1990 if (!domain || !scop)
1991 return NULL;
1992 if (scop->n_stmt < 1)
1993 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1994 "expecting at least one statement", return NULL);
1995 stmt = scop->stmts[0];
1996 if (!pet_stmt_is_kill(stmt))
1997 isl_die(isl_set_get_ctx(domain), isl_error_internal,
1998 "expecting kill statement", return NULL);
2000 expr = pet_tree_expr_get_expr(stmt->body);
2001 arg = pet_expr_get_arg(expr, 0);
2002 pet_expr_free(expr);
2003 index = pet_expr_access_get_index(arg);
2004 access = pet_expr_access_get_access(arg);
2005 pet_expr_free(arg);
2006 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
2007 access = isl_map_reset_tuple_id(access, isl_dim_in);
2008 kill = pet_expr_kill_from_access_and_index(access, index);
2009 tree = pet_tree_new_expr(kill);
2010 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
2011 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
2012 state->n_stmt++, tree);
2013 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
2016 /* Does "tree" represent an assignment to a variable?
2018 * The assignment may be one of
2019 * - a declaration with initialization
2020 * - an expression with a top-level assignment operator
2022 static int is_assignment(__isl_keep pet_tree *tree)
2024 if (!tree)
2025 return 0;
2026 if (tree->type == pet_tree_decl_init)
2027 return 1;
2028 return pet_tree_is_assign(tree);
2031 /* Update "pc" by taking into account the assignment performed by "tree",
2032 * where "tree" satisfies is_assignment.
2034 * In particular, if the lhs of the assignment is a scalar variable and
2035 * if the rhs is an affine expression, then keep track of this value in "pc"
2036 * so that we can plug it in when we later come across the same variable.
2038 * Any previously assigned value to the variable has already been removed
2039 * by scop_handle_writes.
2041 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
2042 __isl_keep pet_tree *tree)
2044 pet_expr *var, *val;
2045 isl_id *id;
2046 isl_pw_aff *pa;
2048 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
2049 var = pet_tree_decl_get_var(tree);
2050 val = pet_tree_decl_get_init(tree);
2051 } else {
2052 pet_expr *expr;
2053 expr = pet_tree_expr_get_expr(tree);
2054 var = pet_expr_get_arg(expr, 0);
2055 val = pet_expr_get_arg(expr, 1);
2056 pet_expr_free(expr);
2059 if (!pet_expr_is_scalar_access(var)) {
2060 pet_expr_free(var);
2061 pet_expr_free(val);
2062 return pc;
2065 pa = pet_expr_extract_affine(val, pc);
2066 if (!pa)
2067 pc = pet_context_free(pc);
2069 if (!isl_pw_aff_involves_nan(pa)) {
2070 id = pet_expr_access_get_id(var);
2071 pc = pet_context_set_value(pc, id, pa);
2072 } else {
2073 isl_pw_aff_free(pa);
2075 pet_expr_free(var);
2076 pet_expr_free(val);
2078 return pc;
2081 /* Mark all arrays in "scop" as being exposed.
2083 static struct pet_scop *mark_exposed(struct pet_scop *scop)
2085 int i;
2087 if (!scop)
2088 return NULL;
2089 for (i = 0; i < scop->n_array; ++i)
2090 scop->arrays[i]->exposed = 1;
2091 return scop;
2094 /* Given that "scop" has an affine skip condition of type pet_skip_now,
2095 * apply this skip condition to the domain of "pc".
2096 * That is, remove the elements satisfying the skip condition from
2097 * the domain of "pc".
2099 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
2100 struct pet_scop *scop)
2102 isl_set *domain, *skip;
2104 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
2105 domain = pet_context_get_domain(pc);
2106 domain = isl_set_subtract(domain, skip);
2107 pc = pet_context_intersect_domain(pc, domain);
2109 return pc;
2112 /* Try and construct a pet_scop corresponding to (part of)
2113 * a sequence of statements within the context "pc".
2115 * After extracting a statement, we update "pc"
2116 * based on the top-level assignments in the statement
2117 * so that we can exploit them in subsequent statements in the same block.
2119 * If there are any breaks or continues in the individual statements,
2120 * then we may have to compute a new skip condition.
2121 * This is handled using a pet_skip_info object.
2122 * On initialization, the object checks if skip conditions need
2123 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2124 * adds them in pet_skip_info_seq_add.
2126 * If "block" is set, then we need to insert kill statements at
2127 * the end of the block for any array that has been declared by
2128 * one of the statements in the sequence. Each of these declarations
2129 * results in the construction of a kill statement at the place
2130 * of the declaration, so we simply collect duplicates of
2131 * those kill statements and append these duplicates to the constructed scop.
2133 * If "block" is not set, then any array declared by one of the statements
2134 * in the sequence is marked as being exposed.
2136 * If autodetect is set, then we allow the extraction of only a subrange
2137 * of the sequence of statements. However, if there is at least one statement
2138 * for which we could not construct a scop and the final range contains
2139 * either no statements or at least one kill, then we discard the entire
2140 * range.
2142 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2143 __isl_keep pet_context *pc, struct pet_state *state)
2145 int i;
2146 isl_ctx *ctx;
2147 isl_space *space;
2148 isl_set *domain;
2149 struct pet_scop *scop, *kills;
2151 ctx = pet_tree_get_ctx(tree);
2153 space = pet_context_get_space(pc);
2154 domain = pet_context_get_domain(pc);
2155 pc = pet_context_copy(pc);
2156 scop = pet_scop_empty(isl_space_copy(space));
2157 kills = pet_scop_empty(space);
2158 for (i = 0; i < tree->u.b.n; ++i) {
2159 struct pet_scop *scop_i;
2161 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2162 pc = apply_affine_continue(pc, scop);
2163 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2164 pc = scop_handle_writes(scop_i, pc);
2165 if (is_assignment(tree->u.b.child[i]))
2166 pc = handle_assignment(pc, tree->u.b.child[i]);
2167 struct pet_skip_info skip;
2168 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2169 pet_skip_info_seq_extract(&skip, pc, state);
2170 if (pet_skip_info_has_skip(&skip))
2171 scop_i = pet_scop_prefix(scop_i, 0);
2172 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
2173 if (tree->u.b.block) {
2174 struct pet_scop *kill;
2175 kill = extract_kill(domain, scop_i, state);
2176 kills = pet_scop_add_par(ctx, kills, kill);
2177 } else
2178 scop_i = mark_exposed(scop_i);
2180 scop_i = pet_scop_prefix(scop_i, i);
2181 scop = pet_scop_add_seq(ctx, scop, scop_i);
2183 scop = pet_skip_info_seq_add(&skip, scop, i);
2185 if (!scop)
2186 break;
2188 isl_set_free(domain);
2190 kills = pet_scop_prefix(kills, tree->u.b.n);
2191 scop = pet_scop_add_seq(ctx, scop, kills);
2193 pet_context_free(pc);
2195 return scop;
2198 /* Internal data structure for extract_declared_arrays.
2200 * "pc" and "state" are used to create pet_array objects and kill statements.
2201 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2202 * found any declared array.
2203 * "scop" has been initialized by the caller and is used to attach
2204 * the created pet_array objects.
2205 * "kill_before" and "kill_after" are created and updated by
2206 * extract_declared_arrays to collect the kills of the arrays.
2208 struct pet_tree_extract_declared_arrays_data {
2209 pet_context *pc;
2210 struct pet_state *state;
2212 isl_ctx *ctx;
2214 int any;
2215 struct pet_scop *scop;
2216 struct pet_scop *kill_before;
2217 struct pet_scop *kill_after;
2220 /* Check if the node "node" declares any array or scalar.
2221 * If so, create the corresponding pet_array and attach it to data->scop.
2222 * Additionally, create two kill statements for the array and add them
2223 * to data->kill_before and data->kill_after.
2225 static int extract_declared_arrays(__isl_keep pet_tree *node, void *user)
2227 enum pet_tree_type type;
2228 struct pet_tree_extract_declared_arrays_data *data = user;
2229 struct pet_array *array;
2230 struct pet_scop *scop_kill;
2231 pet_expr *var;
2233 type = pet_tree_get_type(node);
2234 if (type == pet_tree_decl || type == pet_tree_decl_init)
2235 var = node->u.d.var;
2236 else if (type == pet_tree_for && node->u.l.declared)
2237 var = node->u.l.iv;
2238 else
2239 return 0;
2241 array = extract_array(var, data->pc, data->state);
2242 if (array)
2243 array->declared = 1;
2244 data->scop = pet_scop_add_array(data->scop, array);
2246 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2247 if (!data->any)
2248 data->kill_before = scop_kill;
2249 else
2250 data->kill_before = pet_scop_add_par(data->ctx,
2251 data->kill_before, scop_kill);
2253 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2254 if (!data->any)
2255 data->kill_after = scop_kill;
2256 else
2257 data->kill_after = pet_scop_add_par(data->ctx,
2258 data->kill_after, scop_kill);
2260 data->any = 1;
2262 return 0;
2265 /* Convert a pet_tree that consists of more than a single leaf
2266 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2267 * Do so within the context of "pc".
2269 * After constructing the core scop, we also look for any arrays (or scalars)
2270 * that are declared inside "tree". Each of those arrays is marked as
2271 * having been declared and kill statements for these arrays
2272 * are introduced before and after the core scop.
2273 * Note that the input tree is not a leaf so that the declaration
2274 * cannot occur at the outer level.
2276 static struct pet_scop *scop_from_tree_macro(__isl_take pet_tree *tree,
2277 __isl_take isl_id *label, __isl_keep pet_context *pc,
2278 struct pet_state *state)
2280 struct pet_tree_extract_declared_arrays_data data = { pc, state };
2282 data.scop = scop_from_unevaluated_tree(pet_tree_copy(tree),
2283 state->n_stmt++, pc);
2285 data.any = 0;
2286 data.ctx = pet_context_get_ctx(pc);
2287 if (pet_tree_foreach_sub_tree(tree, &extract_declared_arrays,
2288 &data) < 0)
2289 data.scop = pet_scop_free(data.scop);
2290 pet_tree_free(tree);
2292 if (!data.any)
2293 return data.scop;
2295 data.kill_before = pet_scop_prefix(data.kill_before, 0);
2296 data.scop = pet_scop_prefix(data.scop, 1);
2297 data.kill_after = pet_scop_prefix(data.kill_after, 2);
2299 data.scop = pet_scop_add_seq(data.ctx, data.kill_before, data.scop);
2300 data.scop = pet_scop_add_seq(data.ctx, data.scop, data.kill_after);
2302 return data.scop;
2305 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2306 * within the context "pc" by calling the appropriate function
2307 * based on the type of "tree".
2309 * If the initially constructed pet_scop turns out to involve
2310 * dynamic control and if the user has requested an encapsulation
2311 * of all dynamic control, then this pet_scop is discarded and
2312 * a new pet_scop is created with a single statement representing
2313 * the entire "tree".
2315 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2316 __isl_keep pet_context *pc, struct pet_state *state)
2318 isl_ctx *ctx;
2319 struct pet_scop *scop = NULL;
2321 if (!tree)
2322 return NULL;
2324 ctx = pet_tree_get_ctx(tree);
2325 switch (tree->type) {
2326 case pet_tree_error:
2327 return NULL;
2328 case pet_tree_block:
2329 return scop_from_block(tree, pc, state);
2330 case pet_tree_break:
2331 return scop_from_break(tree, pet_context_get_space(pc));
2332 case pet_tree_continue:
2333 return scop_from_continue(tree, pet_context_get_space(pc));
2334 case pet_tree_decl:
2335 case pet_tree_decl_init:
2336 return scop_from_decl(tree, pc, state);
2337 case pet_tree_expr:
2338 return scop_from_unevaluated_tree(pet_tree_copy(tree),
2339 state->n_stmt++, pc);
2340 case pet_tree_if:
2341 case pet_tree_if_else:
2342 scop = scop_from_if(tree, pc, state);
2343 break;
2344 case pet_tree_for:
2345 scop = scop_from_for(tree, pc, state);
2346 break;
2347 case pet_tree_while:
2348 scop = scop_from_while(tree, pc, state);
2349 break;
2350 case pet_tree_infinite_loop:
2351 scop = scop_from_infinite_for(tree, pc, state);
2352 break;
2355 if (!scop)
2356 return NULL;
2358 if (!pet_options_get_encapsulate_dynamic_control(ctx) ||
2359 !pet_scop_has_data_dependent_conditions(scop))
2360 return scop;
2362 pet_scop_free(scop);
2363 return scop_from_tree_macro(pet_tree_copy(tree),
2364 isl_id_copy(tree->label), pc, state);
2367 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2368 * "int_size" is the number of bytes need to represent an integer.
2369 * "extract_array" is a callback that we can use to create a pet_array
2370 * that corresponds to the variable accessed by an expression.
2372 * Initialize the global state, construct a context and then
2373 * construct the pet_scop by recursively visiting the tree.
2375 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2376 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2377 __isl_keep pet_context *pc, void *user), void *user,
2378 __isl_keep pet_context *pc)
2380 struct pet_scop *scop;
2381 struct pet_state state = { 0 };
2383 if (!tree)
2384 return NULL;
2386 state.ctx = pet_tree_get_ctx(tree);
2387 state.int_size = int_size;
2388 state.extract_array = extract_array;
2389 state.user = user;
2391 scop = scop_from_tree(tree, pc, &state);
2392 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2394 pet_tree_free(tree);
2396 if (scop)
2397 scop->context = isl_set_params(scop->context);
2399 return scop;