extract_affine_from_call: extract out is_min_or_max_builtin
[pet.git] / tree2scop.c
blob9bfabc95074fe81050af6a607e6b6052159ab369
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 <stdlib.h>
36 #include <string.h>
38 #include <isl/id_to_pw_aff.h>
40 #include "aff.h"
41 #include "expr.h"
42 #include "expr_arg.h"
43 #include "nest.h"
44 #include "scop.h"
45 #include "skip.h"
46 #include "state.h"
47 #include "tree2scop.h"
49 /* Update "pc" by taking into account the writes in "stmt".
50 * That is, clear any previously assigned values to variables
51 * that are written by "stmt".
53 static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
54 __isl_take pet_context *pc)
56 return pet_context_clear_writes_in_tree(pc, stmt->body);
59 /* Update "pc" based on the write accesses in "scop".
61 static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
62 __isl_take pet_context *pc)
64 int i;
66 if (!scop)
67 return pet_context_free(pc);
68 for (i = 0; i < scop->n_stmt; ++i)
69 pc = handle_writes(scop->stmts[i], pc);
71 return pc;
74 /* Wrapper around pet_expr_resolve_assume
75 * for use as a callback to pet_tree_map_expr.
77 static __isl_give pet_expr *resolve_assume(__isl_take pet_expr *expr,
78 void *user)
80 pet_context *pc = user;
82 return pet_expr_resolve_assume(expr, pc);
85 /* Check if any expression inside "tree" is an assume expression and
86 * if its single argument can be converted to an affine expression
87 * in the context of "pc".
88 * If so, replace the argument by the affine expression.
90 __isl_give pet_tree *pet_tree_resolve_assume(__isl_take pet_tree *tree,
91 __isl_keep pet_context *pc)
93 return pet_tree_map_expr(tree, &resolve_assume, pc);
96 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
97 * "tree" has already been evaluated in the context of "pc".
98 * This mainly involves resolving nested expression parameters
99 * and setting the name of the iteration space.
100 * The name is given by tree->label if it is non-NULL. Otherwise,
101 * it is of the form S_<stmt_nr>.
103 static struct pet_scop *scop_from_evaluated_tree(__isl_take pet_tree *tree,
104 int stmt_nr, __isl_keep pet_context *pc)
106 isl_space *space;
107 isl_set *domain;
108 struct pet_stmt *ps;
110 space = pet_context_get_space(pc);
112 tree = pet_tree_resolve_nested(tree, space);
113 tree = pet_tree_resolve_assume(tree, pc);
115 domain = pet_context_get_domain(pc);
116 ps = pet_stmt_from_pet_tree(domain, stmt_nr, tree);
117 return pet_scop_from_pet_stmt(space, ps);
120 /* Convert a top-level pet_expr to a pet_scop with one statement
121 * within the context "pc".
122 * "expr" has already been evaluated in the context of "pc".
123 * We construct a pet_tree from "expr" and continue with
124 * scop_from_evaluated_tree.
125 * The name is of the form S_<stmt_nr>.
126 * The location of the statement is set to "loc".
128 static struct pet_scop *scop_from_evaluated_expr(__isl_take pet_expr *expr,
129 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
131 pet_tree *tree;
133 tree = pet_tree_new_expr(expr);
134 tree = pet_tree_set_loc(tree, loc);
135 return scop_from_evaluated_tree(tree, stmt_nr, pc);
138 /* Convert a pet_tree to a pet_scop with one statement within the context "pc".
139 * "tree" has not yet been evaluated in the context of "pc".
140 * We evaluate "tree" in the context of "pc" and continue with
141 * scop_from_evaluated_tree.
142 * The statement name is given by tree->label if it is non-NULL. Otherwise,
143 * it is of the form S_<stmt_nr>.
145 static struct pet_scop *scop_from_unevaluated_tree(__isl_take pet_tree *tree,
146 int stmt_nr, __isl_keep pet_context *pc)
148 tree = pet_context_evaluate_tree(pc, tree);
149 return scop_from_evaluated_tree(tree, stmt_nr, pc);
152 /* Convert a top-level pet_expr to a pet_scop with one statement
153 * within the context "pc", where "expr" has not yet been evaluated
154 * in the context of "pc".
155 * We construct a pet_tree from "expr" and continue with
156 * scop_from_unevaluated_tree.
157 * The statement name is of the form S_<stmt_nr>.
158 * The location of the statement is set to "loc".
160 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
161 int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
163 pet_tree *tree;
165 tree = pet_tree_new_expr(expr);
166 tree = pet_tree_set_loc(tree, loc);
167 return scop_from_unevaluated_tree(tree, stmt_nr, pc);
170 /* Construct a pet_scop with a single statement killing the entire
171 * array "array".
172 * The location of the statement is set to "loc".
174 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
175 __isl_keep pet_context *pc, struct pet_state *state)
177 isl_ctx *ctx;
178 isl_id *id;
179 isl_space *space;
180 isl_multi_pw_aff *index;
181 isl_map *access;
182 pet_expr *expr;
183 struct pet_scop *scop;
185 if (!array)
186 goto error;
187 ctx = isl_set_get_ctx(array->extent);
188 access = isl_map_from_range(isl_set_copy(array->extent));
189 id = isl_set_get_tuple_id(array->extent);
190 space = isl_space_alloc(ctx, 0, 0, 0);
191 space = isl_space_set_tuple_id(space, isl_dim_out, id);
192 index = isl_multi_pw_aff_zero(space);
193 expr = pet_expr_kill_from_access_and_index(access, index);
194 return scop_from_expr(expr, state->n_stmt++, loc, pc);
195 error:
196 pet_loc_free(loc);
197 return NULL;
200 /* Construct and return a pet_array corresponding to the variable
201 * accessed by "access" by calling the extract_array callback.
203 static struct pet_array *extract_array(__isl_keep pet_expr *access,
204 __isl_keep pet_context *pc, struct pet_state *state)
206 return state->extract_array(access, pc, state->user);
209 /* Construct a pet_scop for a (single) variable declaration
210 * within the context "pc".
212 * The scop contains the variable being declared (as an array)
213 * and a statement killing the array.
215 * If the declaration comes with an initialization, then the scop
216 * also contains an assignment to the variable.
218 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
219 __isl_keep pet_context *pc, struct pet_state *state)
221 int type_size;
222 isl_ctx *ctx;
223 struct pet_array *array;
224 struct pet_scop *scop_decl, *scop;
225 pet_expr *lhs, *rhs, *pe;
227 array = extract_array(tree->u.d.var, pc, state);
228 if (array)
229 array->declared = 1;
230 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
231 scop_decl = pet_scop_add_array(scop_decl, array);
233 if (tree->type != pet_tree_decl_init)
234 return scop_decl;
236 lhs = pet_expr_copy(tree->u.d.var);
237 rhs = pet_expr_copy(tree->u.d.init);
238 type_size = pet_expr_get_type_size(lhs);
239 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
240 scop = scop_from_expr(pe, state->n_stmt++, pet_tree_get_loc(tree), pc);
242 ctx = pet_tree_get_ctx(tree);
243 scop = pet_scop_add_seq(ctx, scop_decl, scop);
245 return scop;
248 /* Does "tree" represent a kill statement?
249 * That is, is it an expression statement that "calls" __pencil_kill?
251 static int is_pencil_kill(__isl_keep pet_tree *tree)
253 pet_expr *expr;
254 const char *name;
256 if (!tree)
257 return -1;
258 if (tree->type != pet_tree_expr)
259 return 0;
260 expr = tree->u.e.expr;
261 if (pet_expr_get_type(expr) != pet_expr_call)
262 return 0;
263 name = pet_expr_call_get_name(expr);
264 if (!name)
265 return -1;
266 return !strcmp(name, "__pencil_kill");
269 /* Add a kill to "scop" that kills what is accessed by
270 * the access expression "expr".
272 * If the access expression has any arguments (after evaluation
273 * in the context of "pc"), then we ignore it, since we cannot
274 * tell which elements are definitely killed.
276 * Otherwise, we extend the index expression to the dimension
277 * of the accessed array and intersect with the extent of the array and
278 * add a kill expression that kills these array elements is added to "scop".
280 static struct pet_scop *scop_add_kill(struct pet_scop *scop,
281 __isl_take pet_expr *expr, __isl_take pet_loc *loc,
282 __isl_keep pet_context *pc, struct pet_state *state)
284 int dim1, dim2;
285 isl_id *id;
286 isl_multi_pw_aff *index;
287 isl_map *map;
288 pet_expr *kill;
289 struct pet_array *array;
290 struct pet_scop *scop_i;
292 expr = pet_context_evaluate_expr(pc, expr);
293 if (!expr)
294 goto error;
295 if (expr->n_arg != 0) {
296 pet_expr_free(expr);
297 return scop;
299 array = extract_array(expr, pc, state);
300 if (!array)
301 goto error;
302 index = pet_expr_access_get_index(expr);
303 pet_expr_free(expr);
304 map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
305 id = isl_map_get_tuple_id(map, isl_dim_out);
306 dim1 = isl_set_dim(array->extent, isl_dim_set);
307 dim2 = isl_map_dim(map, isl_dim_out);
308 map = isl_map_add_dims(map, isl_dim_out, dim1 - dim2);
309 map = isl_map_set_tuple_id(map, isl_dim_out, id);
310 map = isl_map_intersect_range(map, isl_set_copy(array->extent));
311 pet_array_free(array);
312 kill = pet_expr_kill_from_access_and_index(map, index);
313 scop_i = scop_from_evaluated_expr(kill, state->n_stmt++, loc, pc);
314 scop = pet_scop_add_par(state->ctx, scop, scop_i);
316 return scop;
317 error:
318 pet_expr_free(expr);
319 return pet_scop_free(scop);
322 /* For each argument of the __pencil_kill call in "tree" that
323 * represents an access, add a kill statement to "scop" killing the accessed
324 * elements.
326 static struct pet_scop *scop_from_pencil_kill(__isl_keep pet_tree *tree,
327 __isl_keep pet_context *pc, struct pet_state *state)
329 pet_expr *call;
330 struct pet_scop *scop;
331 int i, n;
333 call = tree->u.e.expr;
335 scop = pet_scop_empty(pet_context_get_space(pc));
337 n = pet_expr_get_n_arg(call);
338 for (i = 0; i < n; ++i) {
339 pet_expr *arg;
340 pet_loc *loc;
342 arg = pet_expr_get_arg(call, i);
343 if (!arg)
344 return pet_scop_free(scop);
345 if (pet_expr_get_type(arg) != pet_expr_access) {
346 pet_expr_free(arg);
347 continue;
349 loc = pet_tree_get_loc(tree);
350 scop = scop_add_kill(scop, arg, loc, pc, state);
353 return scop;
356 /* Construct a pet_scop for an expression statement within the context "pc".
358 * If the expression calls __pencil_kill, then it needs to be converted
359 * into zero or more kill statements.
360 * Otherwise, a scop is extracted directly from the tree.
362 static struct pet_scop *scop_from_tree_expr(__isl_keep pet_tree *tree,
363 __isl_keep pet_context *pc, struct pet_state *state)
365 int is_kill;
367 is_kill = is_pencil_kill(tree);
368 if (is_kill < 0)
369 return NULL;
370 if (is_kill)
371 return scop_from_pencil_kill(tree, pc, state);
372 return scop_from_unevaluated_tree(pet_tree_copy(tree),
373 state->n_stmt++, pc);
376 /* Return those elements in the space of "cond" that come after
377 * (based on "sign") an element in "cond" in the final dimension.
379 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
381 isl_space *space;
382 isl_map *previous_to_this;
383 int i, dim;
385 dim = isl_set_dim(cond, isl_dim_set);
386 space = isl_space_map_from_set(isl_set_get_space(cond));
387 previous_to_this = isl_map_universe(space);
388 for (i = 0; i + 1 < dim; ++i)
389 previous_to_this = isl_map_equate(previous_to_this,
390 isl_dim_in, i, isl_dim_out, i);
391 if (sign > 0)
392 previous_to_this = isl_map_order_lt(previous_to_this,
393 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
394 else
395 previous_to_this = isl_map_order_gt(previous_to_this,
396 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
398 cond = isl_set_apply(cond, previous_to_this);
400 return cond;
403 /* Remove those iterations of "domain" that have an earlier iteration
404 * (based on "sign") in the final dimension where "skip" is satisfied.
405 * If "apply_skip_map" is set, then "skip_map" is first applied
406 * to the embedded skip condition before removing it from the domain.
408 static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
409 __isl_take isl_set *skip, int sign,
410 int apply_skip_map, __isl_keep isl_map *skip_map)
412 if (apply_skip_map)
413 skip = isl_set_apply(skip, isl_map_copy(skip_map));
414 skip = isl_set_intersect(skip , isl_set_copy(domain));
415 return isl_set_subtract(domain, after(skip, sign));
418 /* Create a single-dimensional multi-affine expression on the domain space
419 * of "pc" that is equal to the final dimension of this domain.
420 * "loop_nr" is the sequence number of the corresponding loop.
421 * If "id" is not NULL, then it is used as the output tuple name.
422 * Otherwise, the name is constructed as L_<loop_nr>.
424 static __isl_give isl_multi_aff *map_to_last(__isl_keep pet_context *pc,
425 int loop_nr, __isl_keep isl_id *id)
427 int pos;
428 isl_space *space;
429 isl_local_space *ls;
430 isl_aff *aff;
431 isl_multi_aff *ma;
432 char name[50];
433 isl_id *label;
435 space = pet_context_get_space(pc);
436 pos = isl_space_dim(space, isl_dim_set) - 1;
437 ls = isl_local_space_from_space(space);
438 aff = isl_aff_var_on_domain(ls, isl_dim_set, pos);
439 ma = isl_multi_aff_from_aff(aff);
441 if (id) {
442 label = isl_id_copy(id);
443 } else {
444 snprintf(name, sizeof(name), "L_%d", loop_nr);
445 label = isl_id_alloc(pet_context_get_ctx(pc), name, NULL);
447 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, label);
449 return ma;
452 /* Create an affine expression that maps elements
453 * of an array "id_test" to the previous element in the final dimension
454 * (according to "inc"), provided this element belongs to "domain".
455 * That is, create the affine expression
457 * { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
459 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
460 __isl_take isl_set *domain, __isl_take isl_val *inc)
462 int pos;
463 isl_space *space;
464 isl_aff *aff;
465 isl_pw_aff *pa;
466 isl_multi_aff *ma;
467 isl_multi_pw_aff *prev;
469 pos = isl_set_dim(domain, isl_dim_set) - 1;
470 space = isl_set_get_space(domain);
471 space = isl_space_map_from_set(space);
472 ma = isl_multi_aff_identity(space);
473 aff = isl_multi_aff_get_aff(ma, pos);
474 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
475 ma = isl_multi_aff_set_aff(ma, pos, aff);
476 domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
477 prev = isl_multi_pw_aff_from_multi_aff(ma);
478 pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
479 pa = isl_pw_aff_intersect_domain(pa, domain);
480 prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
481 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
483 return prev;
486 /* Add an implication to "scop" expressing that if an element of
487 * virtual array "id_test" has value "satisfied" then all previous elements
488 * of this array (in the final dimension) also have that value.
489 * The set of previous elements is bounded by "domain".
490 * If "sign" is negative then the iterator
491 * is decreasing and we express that all subsequent array elements
492 * (but still defined previously) have the same value.
494 static struct pet_scop *add_implication(struct pet_scop *scop,
495 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
496 int satisfied)
498 int i, dim;
499 isl_space *space;
500 isl_map *map;
502 dim = isl_set_dim(domain, isl_dim_set);
503 domain = isl_set_set_tuple_id(domain, id_test);
504 space = isl_space_map_from_set(isl_set_get_space(domain));
505 map = isl_map_universe(space);
506 for (i = 0; i + 1 < dim; ++i)
507 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
508 if (sign > 0)
509 map = isl_map_order_ge(map,
510 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
511 else
512 map = isl_map_order_le(map,
513 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
514 map = isl_map_intersect_range(map, domain);
515 scop = pet_scop_add_implication(scop, map, satisfied);
517 return scop;
520 /* Add a filter to "scop" that imposes that it is only executed
521 * when the variable identified by "id_test" has a zero value
522 * for all previous iterations of "domain".
524 * In particular, add a filter that imposes that the array
525 * has a zero value at the previous iteration of domain and
526 * add an implication that implies that it then has that
527 * value for all previous iterations.
529 static struct pet_scop *scop_add_break(struct pet_scop *scop,
530 __isl_take isl_id *id_test, __isl_take isl_set *domain,
531 __isl_take isl_val *inc)
533 isl_multi_pw_aff *prev;
534 int sign = isl_val_sgn(inc);
536 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
537 scop = add_implication(scop, id_test, domain, sign, 0);
538 scop = pet_scop_filter(scop, prev, 0);
540 return scop;
543 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
544 __isl_keep pet_context *pc, struct pet_state *state);
546 /* Construct a pet_scop for an infinite loop around the given body
547 * within the context "pc".
548 * "loop_id" is the label on the loop or NULL if there is no such label.
550 * The domain of "pc" has already been extended with an infinite loop
552 * { [t] : t >= 0 }
554 * We extract a pet_scop for the body and then embed it in a loop with
555 * schedule
557 * { [outer,t] -> [t] }
559 * If the body contains any break, then it is taken into
560 * account in apply_affine_break (if the skip condition is affine)
561 * or in scop_add_break (if the skip condition is not affine).
563 * Note that in case of an affine skip condition,
564 * since we are dealing with a loop without loop iterator,
565 * the skip condition cannot refer to the current loop iterator and
566 * so effectively, the effect on the iteration domain is of the form
568 * { [outer,0]; [outer,t] : t >= 1 and not skip }
570 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
571 __isl_keep isl_id *loop_id, __isl_keep pet_context *pc,
572 struct pet_state *state)
574 isl_ctx *ctx;
575 isl_id *id_test;
576 isl_set *domain;
577 isl_set *skip;
578 isl_multi_aff *sched;
579 struct pet_scop *scop;
580 int has_affine_break;
581 int has_var_break;
583 ctx = pet_tree_get_ctx(body);
584 domain = pet_context_get_domain(pc);
585 sched = map_to_last(pc, state->n_loop++, loop_id);
587 scop = scop_from_tree(body, pc, state);
589 has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
590 if (has_affine_break)
591 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
592 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
593 if (has_var_break)
594 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
596 scop = pet_scop_reset_skips(scop);
597 scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
598 if (has_affine_break) {
599 domain = apply_affine_break(domain, skip, 1, 0, NULL);
600 scop = pet_scop_intersect_domain_prefix(scop,
601 isl_set_copy(domain));
603 if (has_var_break)
604 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
605 else
606 isl_set_free(domain);
608 return scop;
611 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
613 * for (;;)
614 * body
616 * within the context "pc".
618 * Extend the domain of "pc" with an extra inner loop
620 * { [t] : t >= 0 }
622 * and construct the scop in scop_from_infinite_loop.
624 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
625 __isl_keep pet_context *pc, struct pet_state *state)
627 struct pet_scop *scop;
629 pc = pet_context_copy(pc);
630 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
632 pc = pet_context_add_infinite_loop(pc);
634 scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
636 pet_context_free(pc);
638 return scop;
641 /* Construct a pet_scop for a while loop of the form
643 * while (pa)
644 * body
646 * within the context "pc".
648 * The domain of "pc" has already been extended with an infinite loop
650 * { [t] : t >= 0 }
652 * Here, we add the constraints on the outer loop iterators
653 * implied by "pa" and construct the scop in scop_from_infinite_loop.
654 * Note that the intersection with these constraints
655 * may result in an empty loop.
657 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
658 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
659 struct pet_state *state)
661 struct pet_scop *scop;
662 isl_set *dom, *local;
663 isl_set *valid;
665 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
666 dom = isl_pw_aff_non_zero_set(pa);
667 local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
668 pc = pet_context_intersect_domain(pc, local);
669 scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
670 scop = pet_scop_restrict(scop, dom);
671 scop = pet_scop_restrict_context(scop, valid);
673 pet_context_free(pc);
674 return scop;
677 /* Construct a scop for a while, given the scops for the condition
678 * and the body, the filter identifier and the iteration domain of
679 * the while loop.
681 * In particular, the scop for the condition is filtered to depend
682 * on "id_test" evaluating to true for all previous iterations
683 * of the loop, while the scop for the body is filtered to depend
684 * on "id_test" evaluating to true for all iterations up to the
685 * current iteration.
686 * The actual filter only imposes that this virtual array has
687 * value one on the previous or the current iteration.
688 * The fact that this condition also applies to the previous
689 * iterations is enforced by an implication.
691 * These filtered scops are then combined into a single scop,
692 * with the condition scop scheduled before the body scop.
694 * "sign" is positive if the iterator increases and negative
695 * if it decreases.
697 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
698 struct pet_scop *scop_body, __isl_take isl_id *id_test,
699 __isl_take isl_set *domain, __isl_take isl_val *inc)
701 isl_ctx *ctx = isl_set_get_ctx(domain);
702 isl_space *space;
703 isl_multi_pw_aff *test_index;
704 isl_multi_pw_aff *prev;
705 int sign = isl_val_sgn(inc);
706 struct pet_scop *scop;
708 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
709 scop_cond = pet_scop_filter(scop_cond, prev, 1);
711 space = isl_space_map_from_set(isl_set_get_space(domain));
712 test_index = isl_multi_pw_aff_identity(space);
713 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
714 isl_id_copy(id_test));
715 scop_body = pet_scop_filter(scop_body, test_index, 1);
717 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
718 scop = add_implication(scop, id_test, domain, sign, 1);
720 return scop;
723 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
724 * evaluating "cond" and writing the result to a virtual scalar,
725 * as expressed by "index".
726 * The expression "cond" has not yet been evaluated in the context of "pc".
727 * Do so within the context "pc".
728 * The location of the statement is set to "loc".
730 static struct pet_scop *scop_from_non_affine_condition(
731 __isl_take pet_expr *cond, int stmt_nr,
732 __isl_take isl_multi_pw_aff *index,
733 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
735 pet_expr *expr, *write;
737 cond = pet_context_evaluate_expr(pc, cond);
739 write = pet_expr_from_index(index);
740 write = pet_expr_access_set_write(write, 1);
741 write = pet_expr_access_set_read(write, 0);
742 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
744 return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
747 /* Given that "scop" has an affine skip condition of type pet_skip_now,
748 * apply this skip condition to the domain of "pc".
749 * That is, remove the elements satisfying the skip condition from
750 * the domain of "pc".
752 static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
753 struct pet_scop *scop)
755 isl_set *domain, *skip;
757 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
758 domain = pet_context_get_domain(pc);
759 domain = isl_set_subtract(domain, skip);
760 pc = pet_context_intersect_domain(pc, domain);
762 return pc;
765 /* Add a scop for evaluating the loop increment "inc" add the end
766 * of a loop body "scop" within the context "pc".
768 * The skip conditions resulting from continue statements inside
769 * the body do not apply to "inc", but those resulting from break
770 * statements do need to get applied.
772 static struct pet_scop *scop_add_inc(struct pet_scop *scop,
773 __isl_take pet_expr *inc, __isl_take pet_loc *loc,
774 __isl_keep pet_context *pc, struct pet_state *state)
776 struct pet_scop *scop_inc;
778 pc = pet_context_copy(pc);
780 if (pet_scop_has_skip(scop, pet_skip_later)) {
781 isl_multi_pw_aff *skip;
782 skip = pet_scop_get_skip(scop, pet_skip_later);
783 scop = pet_scop_set_skip(scop, pet_skip_now, skip);
784 if (pet_scop_has_affine_skip(scop, pet_skip_now))
785 pc = apply_affine_continue(pc, scop);
786 } else
787 pet_scop_reset_skip(scop, pet_skip_now);
788 scop_inc = scop_from_expr(inc, state->n_stmt++, loc, pc);
789 scop = pet_scop_add_seq(state->ctx, scop, scop_inc);
791 pet_context_free(pc);
793 return scop;
796 /* Construct a generic while scop, with iteration domain
797 * { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
798 * "loop_id" is the label on the loop or NULL if there is no such label.
799 * The domain of "pc" has already been extended with this infinite loop
801 * { [t] : t >= 0 }
803 * The scop consists of two parts,
804 * one for evaluating the condition "cond" and one for the body.
805 * If "expr_inc" is not NULL, then a scop for evaluating this expression
806 * is added at the end of the body,
807 * after replacing any skip conditions resulting from continue statements
808 * by the skip conditions resulting from break statements (if any).
810 * The schedules are combined as a sequence to reflect that the condition is
811 * evaluated before the body is executed and the body is filtered to depend
812 * on the result of the condition evaluating to true on all iterations
813 * up to the current iteration, while the evaluation of the condition itself
814 * is filtered to depend on the result of the condition evaluating to true
815 * on all previous iterations.
816 * The context of the scop representing the body is dropped
817 * because we don't know how many times the body will be executed,
818 * if at all.
820 * If the body contains any break, then it is taken into
821 * account in apply_affine_break (if the skip condition is affine)
822 * or in scop_add_break (if the skip condition is not affine).
824 * Note that in case of an affine skip condition,
825 * since we are dealing with a loop without loop iterator,
826 * the skip condition cannot refer to the current loop iterator and
827 * so effectively, the effect on the iteration domain is of the form
829 * { [outer,0]; [outer,t] : t >= 1 and not skip }
831 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
832 __isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
833 __isl_keep isl_id *loop_id, __isl_take pet_expr *expr_inc,
834 __isl_take pet_context *pc, struct pet_state *state)
836 isl_ctx *ctx;
837 isl_id *id_test, *id_break_test;
838 isl_space *space;
839 isl_multi_pw_aff *test_index;
840 isl_set *domain;
841 isl_set *skip;
842 isl_multi_aff *sched;
843 struct pet_scop *scop, *scop_body;
844 int has_affine_break;
845 int has_var_break;
847 ctx = state->ctx;
848 space = pet_context_get_space(pc);
849 test_index = pet_create_test_index(space, state->n_test++);
850 scop = scop_from_non_affine_condition(cond, state->n_stmt++,
851 isl_multi_pw_aff_copy(test_index),
852 pet_loc_copy(loc), pc);
853 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
854 domain = pet_context_get_domain(pc);
855 scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
856 test_index, state->int_size);
858 sched = map_to_last(pc, state->n_loop++, loop_id);
860 scop_body = scop_from_tree(tree_body, pc, state);
862 has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
863 if (has_affine_break)
864 skip = pet_scop_get_affine_skip_domain(scop_body,
865 pet_skip_later);
866 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
867 if (has_var_break)
868 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
870 scop_body = pet_scop_reset_context(scop_body);
871 if (expr_inc) {
872 scop_body = scop_add_inc(scop_body, expr_inc, loc, pc, state);
873 } else
874 pet_loc_free(loc);
875 scop_body = pet_scop_reset_skips(scop_body);
877 if (has_affine_break) {
878 domain = apply_affine_break(domain, skip, 1, 0, NULL);
879 scop = pet_scop_intersect_domain_prefix(scop,
880 isl_set_copy(domain));
881 scop_body = pet_scop_intersect_domain_prefix(scop_body,
882 isl_set_copy(domain));
884 if (has_var_break) {
885 scop = scop_add_break(scop, isl_id_copy(id_break_test),
886 isl_set_copy(domain), isl_val_one(ctx));
887 scop_body = scop_add_break(scop_body, id_break_test,
888 isl_set_copy(domain), isl_val_one(ctx));
890 scop = scop_add_while(scop, scop_body, id_test, isl_set_copy(domain),
891 isl_val_one(ctx));
893 scop = pet_scop_embed(scop, domain, sched);
895 pet_context_free(pc);
896 return scop;
899 /* Check if the while loop is of the form
901 * while (affine expression)
902 * body
904 * If so, call scop_from_affine_while to construct a scop.
906 * Otherwise, pass control to scop_from_non_affine_while.
908 * "pc" is the context in which the affine expressions in the scop are created.
909 * The domain of "pc" is extended with an infinite loop
911 * { [t] : t >= 0 }
913 * before passing control to scop_from_affine_while or
914 * scop_from_non_affine_while.
916 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
917 __isl_keep pet_context *pc, struct pet_state *state)
919 pet_expr *cond_expr;
920 isl_pw_aff *pa;
922 if (!tree)
923 return NULL;
925 pc = pet_context_copy(pc);
926 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
928 cond_expr = pet_expr_copy(tree->u.l.cond);
929 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
930 pa = pet_expr_extract_affine_condition(cond_expr, pc);
931 pet_expr_free(cond_expr);
933 pc = pet_context_add_infinite_loop(pc);
935 if (!pa)
936 goto error;
938 if (!isl_pw_aff_involves_nan(pa))
939 return scop_from_affine_while(tree, pa, pc, state);
940 isl_pw_aff_free(pa);
941 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
942 pet_tree_get_loc(tree), tree->u.l.body,
943 tree->label, NULL, pc, state);
944 error:
945 pet_context_free(pc);
946 return NULL;
949 /* Check whether "cond" expresses a simple loop bound
950 * on the final set dimension.
951 * In particular, if "up" is set then "cond" should contain only
952 * upper bounds on the final set dimension.
953 * Otherwise, it should contain only lower bounds.
955 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
957 int pos;
959 pos = isl_set_dim(cond, isl_dim_set) - 1;
960 if (isl_val_is_pos(inc))
961 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, pos);
962 else
963 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, pos);
966 /* Extend a condition on a given iteration of a loop to one that
967 * imposes the same condition on all previous iterations.
968 * "domain" expresses the lower [upper] bound on the iterations
969 * when inc is positive [negative] in its final dimension.
971 * In particular, we construct the condition (when inc is positive)
973 * forall i' : (domain(i') and i' <= i) => cond(i')
975 * (where "<=" applies to the final dimension)
976 * which is equivalent to
978 * not exists i' : domain(i') and i' <= i and not cond(i')
980 * We construct this set by subtracting the satisfying cond from domain,
981 * applying a map
983 * { [i'] -> [i] : i' <= i }
985 * and then subtracting the result from domain again.
987 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
988 __isl_take isl_set *domain, __isl_take isl_val *inc)
990 isl_space *space;
991 isl_map *previous_to_this;
992 int i, dim;
994 dim = isl_set_dim(cond, isl_dim_set);
995 space = isl_space_map_from_set(isl_set_get_space(cond));
996 previous_to_this = isl_map_universe(space);
997 for (i = 0; i + 1 < dim; ++i)
998 previous_to_this = isl_map_equate(previous_to_this,
999 isl_dim_in, i, isl_dim_out, i);
1000 if (isl_val_is_pos(inc))
1001 previous_to_this = isl_map_order_le(previous_to_this,
1002 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1003 else
1004 previous_to_this = isl_map_order_ge(previous_to_this,
1005 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
1007 cond = isl_set_subtract(isl_set_copy(domain), cond);
1008 cond = isl_set_apply(cond, previous_to_this);
1009 cond = isl_set_subtract(domain, cond);
1011 isl_val_free(inc);
1013 return cond;
1016 /* Given an initial value of the form
1018 * { [outer,i] -> init(outer) }
1020 * construct a domain of the form
1022 * { [outer,i] : exists a: i = init(outer) + a * inc and a >= 0 }
1024 static __isl_give isl_set *strided_domain(__isl_take isl_pw_aff *init,
1025 __isl_take isl_val *inc)
1027 int dim;
1028 isl_aff *aff;
1029 isl_space *space;
1030 isl_local_space *ls;
1031 isl_set *set;
1033 dim = isl_pw_aff_dim(init, isl_dim_in);
1035 init = isl_pw_aff_add_dims(init, isl_dim_in, 1);
1036 space = isl_pw_aff_get_domain_space(init);
1037 ls = isl_local_space_from_space(space);
1038 aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1039 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, dim, inc);
1040 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1042 aff = isl_aff_var_on_domain(ls, isl_dim_set, dim - 1);
1043 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1045 set = isl_set_lower_bound_si(set, isl_dim_set, dim, 0);
1046 set = isl_set_project_out(set, isl_dim_set, dim, 1);
1048 return set;
1051 /* Assuming "cond" represents a bound on a loop where the loop
1052 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1053 * is possible.
1055 * Under the given assumptions, wrapping is only possible if "cond" allows
1056 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1057 * increasing iterator and 0 in case of a decreasing iterator.
1059 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
1060 __isl_keep isl_val *inc)
1062 int cw;
1063 isl_ctx *ctx;
1064 isl_val *limit;
1065 isl_set *test;
1067 test = isl_set_copy(cond);
1069 ctx = isl_set_get_ctx(test);
1070 if (isl_val_is_neg(inc))
1071 limit = isl_val_zero(ctx);
1072 else {
1073 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1074 limit = isl_val_2exp(limit);
1075 limit = isl_val_sub_ui(limit, 1);
1078 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
1079 cw = !isl_set_is_empty(test);
1080 isl_set_free(test);
1082 return cw;
1085 /* Given a space
1087 * { [outer, v] },
1089 * construct the following affine expression on this space
1091 * { [outer, v] -> [outer, v mod 2^width] }
1093 * where width is the number of bits used to represent the values
1094 * of the unsigned variable "iv".
1096 static __isl_give isl_multi_aff *compute_wrapping(__isl_take isl_space *space,
1097 __isl_keep pet_expr *iv)
1099 int dim;
1100 isl_ctx *ctx;
1101 isl_val *mod;
1102 isl_aff *aff;
1103 isl_multi_aff *ma;
1105 dim = isl_space_dim(space, isl_dim_set);
1107 ctx = isl_space_get_ctx(space);
1108 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
1109 mod = isl_val_2exp(mod);
1111 space = isl_space_map_from_set(space);
1112 ma = isl_multi_aff_identity(space);
1114 aff = isl_multi_aff_get_aff(ma, dim - 1);
1115 aff = isl_aff_mod_val(aff, mod);
1116 ma = isl_multi_aff_set_aff(ma, dim - 1, aff);
1118 return ma;
1121 /* Given two sets in the space
1123 * { [l,i] },
1125 * where l represents the outer loop iterators, compute the set
1126 * of values of l that ensure that "set1" is a subset of "set2".
1128 * set1 is a subset of set2 if
1130 * forall i: set1(l,i) => set2(l,i)
1132 * or
1134 * not exists i: set1(l,i) and not set2(l,i)
1136 * i.e.,
1138 * not exists i: (set1 \ set2)(l,i)
1140 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
1141 __isl_take isl_set *set2)
1143 int pos;
1145 pos = isl_set_dim(set1, isl_dim_set) - 1;
1146 set1 = isl_set_subtract(set1, set2);
1147 set1 = isl_set_eliminate(set1, isl_dim_set, pos, 1);
1148 return isl_set_complement(set1);
1151 /* Compute the set of outer iterator values for which "cond" holds
1152 * on the next iteration of the inner loop for each element of "dom".
1154 * We first construct mapping { [l,i] -> [l,i + inc] } (where l refers
1155 * to the outer loop iterators), plug that into "cond"
1156 * and then compute the set of outer iterators for which "dom" is a subset
1157 * of the result.
1159 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
1160 __isl_take isl_set *dom, __isl_take isl_val *inc)
1162 int pos;
1163 isl_space *space;
1164 isl_aff *aff;
1165 isl_multi_aff *ma;
1167 pos = isl_set_dim(dom, isl_dim_set) - 1;
1168 space = isl_set_get_space(dom);
1169 space = isl_space_map_from_set(space);
1170 ma = isl_multi_aff_identity(space);
1171 aff = isl_multi_aff_get_aff(ma, pos);
1172 aff = isl_aff_add_constant_val(aff, inc);
1173 ma = isl_multi_aff_set_aff(ma, pos, aff);
1174 cond = isl_set_preimage_multi_aff(cond, ma);
1176 return enforce_subset(dom, cond);
1179 /* Extract the for loop "tree" as a while loop within the context "pc_init".
1180 * In particular, "pc_init" represents the context of the loop,
1181 * whereas "pc" represents the context of the body of the loop and
1182 * has already had its domain extended with an infinite loop
1184 * { [t] : t >= 0 }
1186 * The for loop has the form
1188 * for (iv = init; cond; iv += inc)
1189 * body;
1191 * and is treated as
1193 * iv = init;
1194 * while (cond) {
1195 * body;
1196 * iv += inc;
1199 * except that the skips resulting from any continue statements
1200 * in body do not apply to the increment, but are replaced by the skips
1201 * resulting from break statements.
1203 * If the loop iterator is declared in the for loop, then it is killed before
1204 * and after the loop.
1206 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
1207 __isl_keep pet_context *init_pc, __isl_take pet_context *pc,
1208 struct pet_state *state)
1210 int declared;
1211 isl_id *iv;
1212 pet_expr *expr_iv, *init, *inc;
1213 struct pet_scop *scop_init, *scop;
1214 int type_size;
1215 struct pet_array *array;
1216 struct pet_scop *scop_kill;
1218 iv = pet_expr_access_get_id(tree->u.l.iv);
1219 pc = pet_context_clear_value(pc, iv);
1221 declared = tree->u.l.declared;
1223 expr_iv = pet_expr_copy(tree->u.l.iv);
1224 type_size = pet_expr_get_type_size(expr_iv);
1225 init = pet_expr_copy(tree->u.l.init);
1226 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
1227 scop_init = scop_from_expr(init, state->n_stmt++,
1228 pet_tree_get_loc(tree), init_pc);
1230 expr_iv = pet_expr_copy(tree->u.l.iv);
1231 type_size = pet_expr_get_type_size(expr_iv);
1232 inc = pet_expr_copy(tree->u.l.inc);
1233 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
1235 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
1236 pet_tree_get_loc(tree), tree->u.l.body, tree->label,
1237 inc, pet_context_copy(pc), state);
1239 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
1241 pet_context_free(pc);
1243 if (!declared)
1244 return scop;
1246 array = extract_array(tree->u.l.iv, init_pc, state);
1247 if (array)
1248 array->declared = 1;
1249 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1250 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
1251 scop_kill = kill(pet_tree_get_loc(tree), array, init_pc, state);
1252 scop_kill = pet_scop_add_array(scop_kill, array);
1253 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
1255 return scop;
1258 /* Given an access expression "expr", is the variable accessed by
1259 * "expr" assigned anywhere inside "tree"?
1261 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
1263 int assigned = 0;
1264 isl_id *id;
1266 id = pet_expr_access_get_id(expr);
1267 assigned = pet_tree_writes(tree, id);
1268 isl_id_free(id);
1270 return assigned;
1273 /* Are all nested access parameters in "pa" allowed given "tree".
1274 * In particular, is none of them written by anywhere inside "tree".
1276 * If "tree" has any continue or break nodes in the current loop level,
1277 * then no nested access parameters are allowed.
1278 * In particular, if there is any nested access in a guard
1279 * for a piece of code containing a "continue", then we want to introduce
1280 * a separate statement for evaluating this guard so that we can express
1281 * that the result is false for all previous iterations.
1283 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1284 __isl_keep pet_tree *tree)
1286 int i, nparam;
1288 if (!tree)
1289 return -1;
1291 if (!pet_nested_any_in_pw_aff(pa))
1292 return 1;
1294 if (pet_tree_has_continue_or_break(tree))
1295 return 0;
1297 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1298 for (i = 0; i < nparam; ++i) {
1299 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1300 pet_expr *expr;
1301 int allowed;
1303 if (!pet_nested_in_id(id)) {
1304 isl_id_free(id);
1305 continue;
1308 expr = pet_nested_extract_expr(id);
1309 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1310 !is_assigned(expr, tree);
1312 pet_expr_free(expr);
1313 isl_id_free(id);
1315 if (!allowed)
1316 return 0;
1319 return 1;
1322 /* Internal data structure for collect_local.
1323 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1324 * "local" collects the results.
1326 struct pet_tree_collect_local_data {
1327 pet_context *pc;
1328 struct pet_state *state;
1329 isl_union_set *local;
1332 /* Add the variable accessed by "var" to data->local.
1333 * We extract a representation of the variable from
1334 * the pet_array constructed using extract_array
1335 * to ensure consistency with the rest of the scop.
1337 static int add_local(struct pet_tree_collect_local_data *data,
1338 __isl_keep pet_expr *var)
1340 struct pet_array *array;
1341 isl_set *universe;
1343 array = extract_array(var, data->pc, data->state);
1344 if (!array)
1345 return -1;
1347 universe = isl_set_universe(isl_set_get_space(array->extent));
1348 data->local = isl_union_set_add_set(data->local, universe);
1349 pet_array_free(array);
1351 return 0;
1354 /* If the node "tree" declares a variable, then add it to
1355 * data->local.
1357 static int extract_local_var(__isl_keep pet_tree *tree, void *user)
1359 enum pet_tree_type type;
1360 struct pet_tree_collect_local_data *data = user;
1362 type = pet_tree_get_type(tree);
1363 if (type == pet_tree_decl || type == pet_tree_decl_init)
1364 return add_local(data, tree->u.d.var);
1366 return 0;
1369 /* If the node "tree" is a for loop that declares its induction variable,
1370 * then add it this induction variable to data->local.
1372 static int extract_local_iterator(__isl_keep pet_tree *tree, void *user)
1374 struct pet_tree_collect_local_data *data = user;
1376 if (pet_tree_get_type(tree) == pet_tree_for && tree->u.l.declared)
1377 return add_local(data, tree->u.l.iv);
1379 return 0;
1382 /* Collect and return all local variables of the for loop represented
1383 * by "tree", with "scop" the corresponding pet_scop.
1384 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1386 * We collect not only the variables that are declared inside "tree",
1387 * but also the loop iterators that are declared anywhere inside
1388 * any possible macro statements in "scop".
1389 * The latter also appear as declared variable in the scop,
1390 * whereas other declared loop iterators only appear implicitly
1391 * in the iteration domains.
1393 static __isl_give isl_union_set *collect_local(struct pet_scop *scop,
1394 __isl_keep pet_tree *tree, __isl_keep pet_context *pc,
1395 struct pet_state *state)
1397 int i;
1398 isl_ctx *ctx;
1399 struct pet_tree_collect_local_data data = { pc, state };
1401 ctx = pet_tree_get_ctx(tree);
1402 data.local = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
1404 if (pet_tree_foreach_sub_tree(tree, &extract_local_var, &data) < 0)
1405 return isl_union_set_free(data.local);
1407 for (i = 0; i < scop->n_stmt; ++i) {
1408 pet_tree *body = scop->stmts[i]->body;
1409 if (pet_tree_foreach_sub_tree(body, &extract_local_iterator,
1410 &data) < 0)
1411 return isl_union_set_free(data.local);
1414 return data.local;
1417 /* Add an independence to "scop" if the for node "tree" was marked
1418 * independent.
1419 * "domain" is the set of loop iterators, with the current for loop
1420 * innermost. If "sign" is positive, then the inner iterator increases.
1421 * Otherwise it decreases.
1422 * "pc" and "state" are needed to extract pet_arrays for the local variables.
1424 * If the tree was marked, then collect all local variables and
1425 * add an independence.
1427 static struct pet_scop *set_independence(struct pet_scop *scop,
1428 __isl_keep pet_tree *tree, __isl_keep isl_set *domain, int sign,
1429 __isl_keep pet_context *pc, struct pet_state *state)
1431 isl_union_set *local;
1433 if (!tree->u.l.independent)
1434 return scop;
1436 local = collect_local(scop, tree, pc, state);
1437 scop = pet_scop_set_independent(scop, domain, local, sign);
1439 return scop;
1442 /* Construct a pet_scop for a for tree with static affine initialization
1443 * and constant increment within the context "pc".
1444 * The domain of "pc" has already been extended with an (at this point
1445 * unbounded) inner loop iterator corresponding to the current for loop.
1447 * The condition is allowed to contain nested accesses, provided
1448 * they are not being written to inside the body of the loop.
1449 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1450 * essentially treated as a while loop, with iteration domain
1451 * { [l,i] : i >= init }, where l refers to the outer loop iterators.
1453 * We extract a pet_scop for the body after intersecting the domain of "pc"
1455 * { [l,i] : i >= init and condition' }
1457 * or
1459 * { [l,i] : i <= init and condition' }
1461 * Where condition' is equal to condition if the latter is
1462 * a simple upper [lower] bound and a condition that is extended
1463 * to apply to all previous iterations otherwise.
1464 * Afterwards, the schedule of the pet_scop is extended with
1466 * { [l,i] -> [i] }
1468 * or
1470 * { [l,i] -> [-i] }
1472 * If the condition is non-affine, then we drop the condition from the
1473 * iteration domain and instead create a separate statement
1474 * for evaluating the condition. The body is then filtered to depend
1475 * on the result of the condition evaluating to true on all iterations
1476 * up to the current iteration, while the evaluation the condition itself
1477 * is filtered to depend on the result of the condition evaluating to true
1478 * on all previous iterations.
1479 * The context of the scop representing the body is dropped
1480 * because we don't know how many times the body will be executed,
1481 * if at all.
1483 * If the stride of the loop is not 1, then "i >= init" is replaced by
1485 * (exists a: i = init + stride * a and a >= 0)
1487 * If the loop iterator i is unsigned, then wrapping may occur.
1488 * We therefore use a virtual iterator instead that does not wrap.
1489 * However, the condition in the code applies
1490 * to the wrapped value, so we need to change condition(l,i)
1491 * into condition([l,i % 2^width]). Similarly, we replace all accesses
1492 * to the original iterator by the wrapping of the virtual iterator.
1493 * Note that there may be no need to perform this final wrapping
1494 * if the loop condition (after wrapping) satisfies certain conditions.
1495 * However, the is_simple_bound condition is not enough since it doesn't
1496 * check if there even is an upper bound.
1498 * Wrapping on unsigned iterators can be avoided entirely if
1499 * loop condition is simple, the loop iterator is incremented
1500 * [decremented] by one and the last value before wrapping cannot
1501 * possibly satisfy the loop condition.
1503 * Valid outer iterators for a for loop are those for which the initial
1504 * value itself, the increment on each domain iteration and
1505 * the condition on both the initial value and
1506 * the result of incrementing the iterator for each iteration of the domain
1507 * can be evaluated.
1508 * If the loop condition is non-affine, then we only consider validity
1509 * of the initial value.
1511 * If the body contains any break, then we keep track of it in "skip"
1512 * (if the skip condition is affine) or it is handled in scop_add_break
1513 * (if the skip condition is not affine).
1514 * Note that the affine break condition needs to be considered with
1515 * respect to previous iterations in the virtual domain (if any).
1517 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1518 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1519 __isl_take isl_val *inc, __isl_take pet_context *pc,
1520 struct pet_state *state)
1522 isl_set *domain;
1523 isl_multi_aff *sched;
1524 isl_set *cond = NULL;
1525 isl_set *skip = NULL;
1526 isl_id *id_test = NULL, *id_break_test;
1527 struct pet_scop *scop, *scop_cond = NULL;
1528 int pos;
1529 int is_one;
1530 int is_unsigned;
1531 int is_simple;
1532 int is_virtual;
1533 int is_non_affine;
1534 int has_affine_break;
1535 int has_var_break;
1536 isl_map *rev_wrap = NULL;
1537 isl_map *init_val_map;
1538 isl_pw_aff *pa;
1539 isl_set *valid_init;
1540 isl_set *valid_cond;
1541 isl_set *valid_cond_init;
1542 isl_set *valid_cond_next;
1543 isl_set *valid_inc;
1544 pet_expr *cond_expr;
1545 pet_context *pc_nested;
1547 pos = pet_context_dim(pc) - 1;
1549 domain = pet_context_get_domain(pc);
1550 cond_expr = pet_expr_copy(tree->u.l.cond);
1551 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
1552 pc_nested = pet_context_copy(pc);
1553 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1554 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1555 pet_context_free(pc_nested);
1556 pet_expr_free(cond_expr);
1558 valid_inc = isl_pw_aff_domain(pa_inc);
1560 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1562 is_non_affine = isl_pw_aff_involves_nan(pa) ||
1563 !is_nested_allowed(pa, tree->u.l.body);
1564 if (is_non_affine)
1565 pa = isl_pw_aff_free(pa);
1567 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1568 cond = isl_pw_aff_non_zero_set(pa);
1569 if (is_non_affine)
1570 cond = isl_set_universe(isl_set_get_space(domain));
1572 valid_cond = isl_set_coalesce(valid_cond);
1573 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1574 is_virtual = is_unsigned &&
1575 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1577 init_val_map = isl_map_from_pw_aff(isl_pw_aff_copy(init_val));
1578 init_val_map = isl_map_equate(init_val_map, isl_dim_in, pos,
1579 isl_dim_out, 0);
1580 valid_cond_init = enforce_subset(isl_map_domain(init_val_map),
1581 isl_set_copy(valid_cond));
1582 if (is_one && !is_virtual) {
1583 isl_set *cond;
1585 isl_pw_aff_free(init_val);
1586 pa = pet_expr_extract_comparison(
1587 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1588 tree->u.l.iv, tree->u.l.init, pc);
1589 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1590 valid_init = isl_set_eliminate(valid_init, isl_dim_set,
1591 isl_set_dim(domain, isl_dim_set) - 1, 1);
1592 cond = isl_pw_aff_non_zero_set(pa);
1593 domain = isl_set_intersect(domain, cond);
1594 } else {
1595 isl_set *strided;
1597 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1598 strided = strided_domain(init_val, isl_val_copy(inc));
1599 domain = isl_set_intersect(domain, strided);
1602 if (is_virtual) {
1603 isl_multi_aff *wrap;
1604 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1605 pc = pet_context_preimage_domain(pc, wrap);
1606 rev_wrap = isl_map_from_multi_aff(wrap);
1607 rev_wrap = isl_map_reverse(rev_wrap);
1608 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1609 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1610 valid_inc = isl_set_apply(valid_inc, isl_map_copy(rev_wrap));
1612 is_simple = is_simple_bound(cond, inc);
1613 if (!is_simple) {
1614 cond = isl_set_gist(cond, isl_set_copy(domain));
1615 is_simple = is_simple_bound(cond, inc);
1617 if (!is_simple)
1618 cond = valid_for_each_iteration(cond,
1619 isl_set_copy(domain), isl_val_copy(inc));
1620 cond = isl_set_align_params(cond, isl_set_get_space(domain));
1621 domain = isl_set_intersect(domain, cond);
1622 sched = map_to_last(pc, state->n_loop++, tree->label);
1623 if (isl_val_is_neg(inc))
1624 sched = isl_multi_aff_neg(sched);
1626 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1627 isl_val_copy(inc));
1628 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1630 pc = pet_context_intersect_domain(pc, isl_set_copy(domain));
1632 if (is_non_affine) {
1633 isl_space *space;
1634 isl_multi_pw_aff *test_index;
1635 space = isl_set_get_space(domain);
1636 test_index = pet_create_test_index(space, state->n_test++);
1637 scop_cond = scop_from_non_affine_condition(
1638 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1639 isl_multi_pw_aff_copy(test_index),
1640 pet_tree_get_loc(tree), pc);
1641 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1642 isl_dim_out);
1643 scop_cond = pet_scop_add_boolean_array(scop_cond,
1644 isl_set_copy(domain), test_index,
1645 state->int_size);
1648 scop = scop_from_tree(tree->u.l.body, pc, state);
1649 has_affine_break = scop &&
1650 pet_scop_has_affine_skip(scop, pet_skip_later);
1651 if (has_affine_break)
1652 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1653 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1654 if (has_var_break)
1655 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1656 if (is_non_affine) {
1657 scop = pet_scop_reset_context(scop);
1659 scop = pet_scop_reset_skips(scop);
1660 scop = pet_scop_resolve_nested(scop);
1661 if (has_affine_break) {
1662 domain = apply_affine_break(domain, skip, isl_val_sgn(inc),
1663 is_virtual, rev_wrap);
1664 scop = pet_scop_intersect_domain_prefix(scop,
1665 isl_set_copy(domain));
1667 isl_map_free(rev_wrap);
1668 if (has_var_break)
1669 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1670 isl_val_copy(inc));
1671 if (is_non_affine)
1672 scop = scop_add_while(scop_cond, scop, id_test,
1673 isl_set_copy(domain),
1674 isl_val_copy(inc));
1675 else
1676 scop = set_independence(scop, tree, domain, isl_val_sgn(inc),
1677 pc, state);
1678 scop = pet_scop_embed(scop, domain, sched);
1679 if (is_non_affine) {
1680 isl_set_free(valid_inc);
1681 } else {
1682 valid_inc = isl_set_intersect(valid_inc, valid_cond_next);
1683 valid_inc = isl_set_intersect(valid_inc, valid_cond_init);
1684 valid_inc = isl_set_project_out(valid_inc, isl_dim_set, pos, 1);
1685 scop = pet_scop_restrict_context(scop, valid_inc);
1688 isl_val_free(inc);
1690 valid_init = isl_set_project_out(valid_init, isl_dim_set, pos, 1);
1691 scop = pet_scop_restrict_context(scop, valid_init);
1693 pet_context_free(pc);
1694 return scop;
1697 /* Construct a pet_scop for a for statement within the context of "pc".
1699 * We update the context to reflect the writes to the loop variable and
1700 * the writes inside the body.
1702 * Then we check if the initialization of the for loop
1703 * is a static affine value and the increment is a constant.
1704 * If so, we construct the pet_scop using scop_from_affine_for.
1705 * Otherwise, we treat the for loop as a while loop
1706 * in scop_from_non_affine_for.
1708 * Note that the initialization and the increment are extracted
1709 * in a context where the current loop iterator has been added
1710 * to the context. If these turn out not be affine, then we
1711 * have reconstruct the body context without an assignment
1712 * to this loop iterator, as this variable will then not be
1713 * treated as a dimension of the iteration domain, but as any
1714 * other variable.
1716 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1717 __isl_keep pet_context *init_pc, struct pet_state *state)
1719 isl_id *iv;
1720 isl_val *inc;
1721 isl_pw_aff *pa_inc, *init_val;
1722 pet_context *pc, *pc_init_val;
1724 if (!tree)
1725 return NULL;
1727 iv = pet_expr_access_get_id(tree->u.l.iv);
1728 pc = pet_context_copy(init_pc);
1729 pc = pet_context_add_inner_iterator(pc, iv);
1730 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1732 pc_init_val = pet_context_copy(pc);
1733 pc_init_val = pet_context_clear_value(pc_init_val, isl_id_copy(iv));
1734 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1735 pet_context_free(pc_init_val);
1736 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1737 inc = pet_extract_cst(pa_inc);
1738 if (!pa_inc || !init_val || !inc)
1739 goto error;
1740 if (!isl_pw_aff_involves_nan(pa_inc) &&
1741 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1742 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1743 pc, state);
1745 isl_pw_aff_free(pa_inc);
1746 isl_pw_aff_free(init_val);
1747 isl_val_free(inc);
1748 pet_context_free(pc);
1750 pc = pet_context_copy(init_pc);
1751 pc = pet_context_add_infinite_loop(pc);
1752 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1753 return scop_from_non_affine_for(tree, init_pc, pc, state);
1754 error:
1755 isl_pw_aff_free(pa_inc);
1756 isl_pw_aff_free(init_val);
1757 isl_val_free(inc);
1758 pet_context_free(pc);
1759 return NULL;
1762 /* Check whether "expr" is an affine constraint within the context "pc".
1764 static int is_affine_condition(__isl_keep pet_expr *expr,
1765 __isl_keep pet_context *pc)
1767 isl_pw_aff *pa;
1768 int is_affine;
1770 pa = pet_expr_extract_affine_condition(expr, pc);
1771 if (!pa)
1772 return -1;
1773 is_affine = !isl_pw_aff_involves_nan(pa);
1774 isl_pw_aff_free(pa);
1776 return is_affine;
1779 /* Check if the given if statement is a conditional assignement
1780 * with a non-affine condition.
1782 * In particular we check if "stmt" is of the form
1784 * if (condition)
1785 * a = f(...);
1786 * else
1787 * a = g(...);
1789 * where the condition is non-affine and a is some array or scalar access.
1791 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1792 __isl_keep pet_context *pc)
1794 int equal;
1795 isl_ctx *ctx;
1796 pet_expr *expr1, *expr2;
1798 ctx = pet_tree_get_ctx(tree);
1799 if (!pet_options_get_detect_conditional_assignment(ctx))
1800 return 0;
1801 if (tree->type != pet_tree_if_else)
1802 return 0;
1803 if (tree->u.i.then_body->type != pet_tree_expr)
1804 return 0;
1805 if (tree->u.i.else_body->type != pet_tree_expr)
1806 return 0;
1807 expr1 = tree->u.i.then_body->u.e.expr;
1808 expr2 = tree->u.i.else_body->u.e.expr;
1809 if (pet_expr_get_type(expr1) != pet_expr_op)
1810 return 0;
1811 if (pet_expr_get_type(expr2) != pet_expr_op)
1812 return 0;
1813 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1814 return 0;
1815 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1816 return 0;
1817 expr1 = pet_expr_get_arg(expr1, 0);
1818 expr2 = pet_expr_get_arg(expr2, 0);
1819 equal = pet_expr_is_equal(expr1, expr2);
1820 pet_expr_free(expr1);
1821 pet_expr_free(expr2);
1822 if (equal < 0 || !equal)
1823 return 0;
1824 if (is_affine_condition(tree->u.i.cond, pc))
1825 return 0;
1827 return 1;
1830 /* Given that "tree" is of the form
1832 * if (condition)
1833 * a = f(...);
1834 * else
1835 * a = g(...);
1837 * where a is some array or scalar access, construct a pet_scop
1838 * corresponding to this conditional assignment within the context "pc".
1839 * "cond_pa" is an affine expression with nested accesses representing
1840 * the condition.
1842 * The constructed pet_scop then corresponds to the expression
1844 * a = condition ? f(...) : g(...)
1846 * All access relations in f(...) are intersected with condition
1847 * while all access relation in g(...) are intersected with the complement.
1849 static struct pet_scop *scop_from_conditional_assignment(
1850 __isl_keep pet_tree *tree, __isl_take isl_pw_aff *cond_pa,
1851 __isl_take pet_context *pc, struct pet_state *state)
1853 int type_size;
1854 isl_set *cond, *comp;
1855 isl_multi_pw_aff *index;
1856 pet_expr *expr1, *expr2;
1857 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1858 struct pet_scop *scop;
1860 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond_pa));
1861 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(cond_pa));
1862 index = isl_multi_pw_aff_from_pw_aff(cond_pa);
1864 expr1 = tree->u.i.then_body->u.e.expr;
1865 expr2 = tree->u.i.else_body->u.e.expr;
1867 pe_cond = pet_expr_from_index(index);
1869 pe_then = pet_expr_get_arg(expr1, 1);
1870 pe_then = pet_context_evaluate_expr(pc, pe_then);
1871 pe_then = pet_expr_restrict(pe_then, cond);
1872 pe_else = pet_expr_get_arg(expr2, 1);
1873 pe_else = pet_context_evaluate_expr(pc, pe_else);
1874 pe_else = pet_expr_restrict(pe_else, comp);
1875 pe_write = pet_expr_get_arg(expr1, 0);
1876 pe_write = pet_context_evaluate_expr(pc, pe_write);
1878 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1879 type_size = pet_expr_get_type_size(pe_write);
1880 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1882 scop = scop_from_evaluated_expr(pe, state->n_stmt++,
1883 pet_tree_get_loc(tree), pc);
1885 pet_context_free(pc);
1887 return scop;
1890 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1892 * We create a separate statement that writes the result
1893 * of the non-affine condition to a virtual scalar.
1894 * A constraint requiring the value of this virtual scalar to be one
1895 * is added to the iteration domains of the then branch.
1896 * Similarly, a constraint requiring the value of this virtual scalar
1897 * to be zero is added to the iteration domains of the else branch, if any.
1898 * We combine the schedules as a sequence to ensure that the virtual scalar
1899 * is written before it is read.
1901 * If there are any breaks or continues in the then and/or else
1902 * branches, then we may have to compute a new skip condition.
1903 * This is handled using a pet_skip_info object.
1904 * On initialization, the object checks if skip conditions need
1905 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1906 * adds them in pet_skip_info_add.
1908 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1909 __isl_take pet_context *pc, struct pet_state *state)
1911 int has_else;
1912 isl_space *space;
1913 isl_set *domain;
1914 isl_multi_pw_aff *test_index;
1915 struct pet_skip_info skip;
1916 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1918 has_else = tree->type == pet_tree_if_else;
1920 space = pet_context_get_space(pc);
1921 test_index = pet_create_test_index(space, state->n_test++);
1922 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1923 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1924 pet_tree_get_loc(tree), pc);
1925 domain = pet_context_get_domain(pc);
1926 scop = pet_scop_add_boolean_array(scop, domain,
1927 isl_multi_pw_aff_copy(test_index), state->int_size);
1929 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1930 if (has_else)
1931 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1933 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1934 has_else, 0);
1935 pet_skip_info_if_extract_index(&skip, test_index, pc, state);
1937 scop_then = pet_scop_filter(scop_then,
1938 isl_multi_pw_aff_copy(test_index), 1);
1939 if (has_else) {
1940 scop_else = pet_scop_filter(scop_else, test_index, 0);
1941 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1942 } else
1943 isl_multi_pw_aff_free(test_index);
1945 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1947 scop = pet_skip_info_add(&skip, scop);
1949 pet_context_free(pc);
1950 return scop;
1953 /* Construct a pet_scop for an affine if statement within the context "pc".
1955 * The condition is added to the iteration domains of the then branch,
1956 * while the opposite of the condition in added to the iteration domains
1957 * of the else branch, if any.
1959 * If there are any breaks or continues in the then and/or else
1960 * branches, then we may have to compute a new skip condition.
1961 * This is handled using a pet_skip_info_if object.
1962 * On initialization, the object checks if skip conditions need
1963 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1964 * adds them in pet_skip_info_add.
1966 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1967 __isl_take isl_pw_aff *cond, __isl_take pet_context *pc,
1968 struct pet_state *state)
1970 int has_else;
1971 isl_ctx *ctx;
1972 isl_set *set, *complement;
1973 isl_set *valid;
1974 struct pet_skip_info skip;
1975 struct pet_scop *scop, *scop_then, *scop_else = NULL;
1976 pet_context *pc_body;
1978 ctx = pet_tree_get_ctx(tree);
1980 has_else = tree->type == pet_tree_if_else;
1982 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1983 set = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1985 pc_body = pet_context_copy(pc);
1986 pc_body = pet_context_intersect_domain(pc_body, isl_set_copy(set));
1987 scop_then = scop_from_tree(tree->u.i.then_body, pc_body, state);
1988 pet_context_free(pc_body);
1989 if (has_else) {
1990 pc_body = pet_context_copy(pc);
1991 complement = isl_set_copy(valid);
1992 complement = isl_set_subtract(valid, isl_set_copy(set));
1993 pc_body = pet_context_intersect_domain(pc_body,
1994 isl_set_copy(complement));
1995 scop_else = scop_from_tree(tree->u.i.else_body, pc_body, state);
1996 pet_context_free(pc_body);
1999 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
2000 pet_skip_info_if_extract_cond(&skip, cond, pc, state);
2001 isl_pw_aff_free(cond);
2003 scop = pet_scop_restrict(scop_then, set);
2005 if (has_else) {
2006 scop_else = pet_scop_restrict(scop_else, complement);
2007 scop = pet_scop_add_par(ctx, scop, scop_else);
2009 scop = pet_scop_resolve_nested(scop);
2010 scop = pet_scop_restrict_context(scop, valid);
2012 scop = pet_skip_info_add(&skip, scop);
2014 pet_context_free(pc);
2015 return scop;
2018 /* Construct a pet_scop for an if statement within the context "pc".
2020 * If the condition fits the pattern of a conditional assignment,
2021 * then it is handled by scop_from_conditional_assignment.
2022 * Note that the condition is only considered for a conditional assignment
2023 * if it is not static-affine. However, it should still convert
2024 * to an affine expression when nesting is allowed.
2026 * Otherwise, we check if the condition is affine.
2027 * If so, we construct the scop in scop_from_affine_if.
2028 * Otherwise, we construct the scop in scop_from_non_affine_if.
2030 * We allow the condition to be dynamic, i.e., to refer to
2031 * scalars or array elements that may be written to outside
2032 * of the given if statement. These nested accesses are then represented
2033 * as output dimensions in the wrapping iteration domain.
2034 * If it is also written _inside_ the then or else branch, then
2035 * we treat the condition as non-affine.
2036 * As explained in extract_non_affine_if, this will introduce
2037 * an extra statement.
2038 * For aesthetic reasons, we want this statement to have a statement
2039 * number that is lower than those of the then and else branches.
2040 * In order to evaluate if we will need such a statement, however, we
2041 * first construct scops for the then and else branches.
2042 * We therefore reserve a statement number if we might have to
2043 * introduce such an extra statement.
2045 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
2046 __isl_keep pet_context *pc, struct pet_state *state)
2048 int has_else;
2049 isl_pw_aff *cond;
2050 pet_expr *cond_expr;
2051 pet_context *pc_nested;
2053 if (!tree)
2054 return NULL;
2056 has_else = tree->type == pet_tree_if_else;
2058 pc = pet_context_copy(pc);
2059 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
2060 if (has_else)
2061 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
2063 cond_expr = pet_expr_copy(tree->u.i.cond);
2064 cond_expr = pet_context_evaluate_expr(pc, cond_expr);
2065 pc_nested = pet_context_copy(pc);
2066 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
2067 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
2068 pet_context_free(pc_nested);
2069 pet_expr_free(cond_expr);
2071 if (!cond) {
2072 pet_context_free(pc);
2073 return NULL;
2076 if (isl_pw_aff_involves_nan(cond)) {
2077 isl_pw_aff_free(cond);
2078 return scop_from_non_affine_if(tree, pc, state);
2081 if (is_conditional_assignment(tree, pc))
2082 return scop_from_conditional_assignment(tree, cond, pc, state);
2084 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
2085 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
2086 isl_pw_aff_free(cond);
2087 return scop_from_non_affine_if(tree, pc, state);
2090 return scop_from_affine_if(tree, cond, pc, state);
2093 /* Return a one-dimensional multi piecewise affine expression that is equal
2094 * to the constant 1 and is defined over the given domain.
2096 static __isl_give isl_multi_pw_aff *one_mpa(__isl_take isl_space *space)
2098 isl_local_space *ls;
2099 isl_aff *aff;
2101 ls = isl_local_space_from_space(space);
2102 aff = isl_aff_zero_on_domain(ls);
2103 aff = isl_aff_set_constant_si(aff, 1);
2105 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
2108 /* Construct a pet_scop for a continue statement with the given domain space.
2110 * We simply create an empty scop with a universal pet_skip_now
2111 * skip condition. This skip condition will then be taken into
2112 * account by the enclosing loop construct, possibly after
2113 * being incorporated into outer skip conditions.
2115 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree,
2116 __isl_take isl_space *space)
2118 struct pet_scop *scop;
2120 scop = pet_scop_empty(isl_space_copy(space));
2122 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(space));
2124 return scop;
2127 /* Construct a pet_scop for a break statement with the given domain space.
2129 * We simply create an empty scop with both a universal pet_skip_now
2130 * skip condition and a universal pet_skip_later skip condition.
2131 * These skip conditions will then be taken into
2132 * account by the enclosing loop construct, possibly after
2133 * being incorporated into outer skip conditions.
2135 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree,
2136 __isl_take isl_space *space)
2138 struct pet_scop *scop;
2139 isl_multi_pw_aff *skip;
2141 scop = pet_scop_empty(isl_space_copy(space));
2143 skip = one_mpa(space);
2144 scop = pet_scop_set_skip(scop, pet_skip_now,
2145 isl_multi_pw_aff_copy(skip));
2146 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
2148 return scop;
2151 /* Extract a clone of the kill statement in "scop".
2152 * The domain of the clone is given by "domain".
2153 * "scop" is expected to have been created from a DeclStmt
2154 * and should have the kill as its first statement.
2156 static struct pet_scop *extract_kill(__isl_keep isl_set *domain,
2157 struct pet_scop *scop, struct pet_state *state)
2159 pet_expr *kill;
2160 struct pet_stmt *stmt;
2161 isl_space *space;
2162 isl_multi_pw_aff *mpa;
2163 pet_tree *tree;
2165 if (!domain || !scop)
2166 return NULL;
2167 if (scop->n_stmt < 1)
2168 isl_die(isl_set_get_ctx(domain), isl_error_internal,
2169 "expecting at least one statement", return NULL);
2170 stmt = scop->stmts[0];
2171 if (!pet_stmt_is_kill(stmt))
2172 isl_die(isl_set_get_ctx(domain), isl_error_internal,
2173 "expecting kill statement", return NULL);
2175 kill = pet_tree_expr_get_expr(stmt->body);
2176 space = pet_stmt_get_space(stmt);
2177 space = isl_space_map_from_set(space);
2178 mpa = isl_multi_pw_aff_identity(space);
2179 mpa = isl_multi_pw_aff_reset_tuple_id(mpa, isl_dim_in);
2180 kill = pet_expr_update_domain(kill, mpa);
2181 tree = pet_tree_new_expr(kill);
2182 tree = pet_tree_set_loc(tree, pet_loc_copy(stmt->loc));
2183 stmt = pet_stmt_from_pet_tree(isl_set_copy(domain),
2184 state->n_stmt++, tree);
2185 return pet_scop_from_pet_stmt(isl_set_get_space(domain), stmt);
2188 /* Does "tree" represent an assignment to a variable?
2190 * The assignment may be one of
2191 * - a declaration with initialization
2192 * - an expression with a top-level assignment operator
2194 static int is_assignment(__isl_keep pet_tree *tree)
2196 if (!tree)
2197 return 0;
2198 if (tree->type == pet_tree_decl_init)
2199 return 1;
2200 return pet_tree_is_assign(tree);
2203 /* Update "pc" by taking into account the assignment performed by "tree",
2204 * where "tree" satisfies is_assignment.
2206 * In particular, if the lhs of the assignment is a scalar variable and
2207 * if the rhs is an affine expression, then keep track of this value in "pc"
2208 * so that we can plug it in when we later come across the same variable.
2210 * Any previously assigned value to the variable has already been removed
2211 * by scop_handle_writes.
2213 static __isl_give pet_context *handle_assignment(__isl_take pet_context *pc,
2214 __isl_keep pet_tree *tree)
2216 pet_expr *var, *val;
2217 isl_id *id;
2218 isl_pw_aff *pa;
2220 if (pet_tree_get_type(tree) == pet_tree_decl_init) {
2221 var = pet_tree_decl_get_var(tree);
2222 val = pet_tree_decl_get_init(tree);
2223 } else {
2224 pet_expr *expr;
2225 expr = pet_tree_expr_get_expr(tree);
2226 var = pet_expr_get_arg(expr, 0);
2227 val = pet_expr_get_arg(expr, 1);
2228 pet_expr_free(expr);
2231 if (!pet_expr_is_scalar_access(var)) {
2232 pet_expr_free(var);
2233 pet_expr_free(val);
2234 return pc;
2237 pa = pet_expr_extract_affine(val, pc);
2238 if (!pa)
2239 pc = pet_context_free(pc);
2241 if (!isl_pw_aff_involves_nan(pa)) {
2242 id = pet_expr_access_get_id(var);
2243 pc = pet_context_set_value(pc, id, pa);
2244 } else {
2245 isl_pw_aff_free(pa);
2247 pet_expr_free(var);
2248 pet_expr_free(val);
2250 return pc;
2253 /* Mark all arrays in "scop" as being exposed.
2255 static struct pet_scop *mark_exposed(struct pet_scop *scop)
2257 int i;
2259 if (!scop)
2260 return NULL;
2261 for (i = 0; i < scop->n_array; ++i)
2262 scop->arrays[i]->exposed = 1;
2263 return scop;
2266 /* Try and construct a pet_scop corresponding to (part of)
2267 * a sequence of statements within the context "pc".
2269 * After extracting a statement, we update "pc"
2270 * based on the top-level assignments in the statement
2271 * so that we can exploit them in subsequent statements in the same block.
2273 * If there are any breaks or continues in the individual statements,
2274 * then we may have to compute a new skip condition.
2275 * This is handled using a pet_skip_info object.
2276 * On initialization, the object checks if skip conditions need
2277 * to be computed. If so, it does so in pet_skip_info_seq_extract and
2278 * adds them in pet_skip_info_add.
2280 * If "block" is set, then we need to insert kill statements at
2281 * the end of the block for any array that has been declared by
2282 * one of the statements in the sequence. Each of these declarations
2283 * results in the construction of a kill statement at the place
2284 * of the declaration, so we simply collect duplicates of
2285 * those kill statements and append these duplicates to the constructed scop.
2287 * If "block" is not set, then any array declared by one of the statements
2288 * in the sequence is marked as being exposed.
2290 * If autodetect is set, then we allow the extraction of only a subrange
2291 * of the sequence of statements. However, if there is at least one statement
2292 * for which we could not construct a scop and the final range contains
2293 * either no statements or at least one kill, then we discard the entire
2294 * range.
2296 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
2297 __isl_keep pet_context *pc, struct pet_state *state)
2299 int i;
2300 isl_ctx *ctx;
2301 isl_space *space;
2302 isl_set *domain;
2303 struct pet_scop *scop, *kills;
2305 ctx = pet_tree_get_ctx(tree);
2307 space = pet_context_get_space(pc);
2308 domain = pet_context_get_domain(pc);
2309 pc = pet_context_copy(pc);
2310 scop = pet_scop_empty(isl_space_copy(space));
2311 kills = pet_scop_empty(space);
2312 for (i = 0; i < tree->u.b.n; ++i) {
2313 struct pet_scop *scop_i;
2315 if (pet_scop_has_affine_skip(scop, pet_skip_now))
2316 pc = apply_affine_continue(pc, scop);
2317 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
2318 pc = scop_handle_writes(scop_i, pc);
2319 if (is_assignment(tree->u.b.child[i]))
2320 pc = handle_assignment(pc, tree->u.b.child[i]);
2321 struct pet_skip_info skip;
2322 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
2323 pet_skip_info_seq_extract(&skip, pc, state);
2324 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
2325 if (tree->u.b.block) {
2326 struct pet_scop *kill;
2327 kill = extract_kill(domain, scop_i, state);
2328 kills = pet_scop_add_par(ctx, kills, kill);
2329 } else
2330 scop_i = mark_exposed(scop_i);
2332 scop = pet_scop_add_seq(ctx, scop, scop_i);
2334 scop = pet_skip_info_add(&skip, scop);
2336 if (!scop)
2337 break;
2339 isl_set_free(domain);
2341 scop = pet_scop_add_seq(ctx, scop, kills);
2343 pet_context_free(pc);
2345 return scop;
2348 /* Internal data structure for extract_declared_arrays.
2350 * "pc" and "state" are used to create pet_array objects and kill statements.
2351 * "any" is initialized to 0 by the caller and set to 1 as soon as we have
2352 * found any declared array.
2353 * "scop" has been initialized by the caller and is used to attach
2354 * the created pet_array objects.
2355 * "kill_before" and "kill_after" are created and updated by
2356 * extract_declared_arrays to collect the kills of the arrays.
2358 struct pet_tree_extract_declared_arrays_data {
2359 pet_context *pc;
2360 struct pet_state *state;
2362 isl_ctx *ctx;
2364 int any;
2365 struct pet_scop *scop;
2366 struct pet_scop *kill_before;
2367 struct pet_scop *kill_after;
2370 /* Check if the node "node" declares any array or scalar.
2371 * If so, create the corresponding pet_array and attach it to data->scop.
2372 * Additionally, create two kill statements for the array and add them
2373 * to data->kill_before and data->kill_after.
2375 static int extract_declared_arrays(__isl_keep pet_tree *node, void *user)
2377 enum pet_tree_type type;
2378 struct pet_tree_extract_declared_arrays_data *data = user;
2379 struct pet_array *array;
2380 struct pet_scop *scop_kill;
2381 pet_expr *var;
2383 type = pet_tree_get_type(node);
2384 if (type == pet_tree_decl || type == pet_tree_decl_init)
2385 var = node->u.d.var;
2386 else if (type == pet_tree_for && node->u.l.declared)
2387 var = node->u.l.iv;
2388 else
2389 return 0;
2391 array = extract_array(var, data->pc, data->state);
2392 if (array)
2393 array->declared = 1;
2394 data->scop = pet_scop_add_array(data->scop, array);
2396 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2397 if (!data->any)
2398 data->kill_before = scop_kill;
2399 else
2400 data->kill_before = pet_scop_add_par(data->ctx,
2401 data->kill_before, scop_kill);
2403 scop_kill = kill(pet_tree_get_loc(node), array, data->pc, data->state);
2404 if (!data->any)
2405 data->kill_after = scop_kill;
2406 else
2407 data->kill_after = pet_scop_add_par(data->ctx,
2408 data->kill_after, scop_kill);
2410 data->any = 1;
2412 return 0;
2415 /* Convert a pet_tree that consists of more than a single leaf
2416 * to a pet_scop with a single statement encapsulating the entire pet_tree.
2417 * Do so within the context of "pc".
2419 * After constructing the core scop, we also look for any arrays (or scalars)
2420 * that are declared inside "tree". Each of those arrays is marked as
2421 * having been declared and kill statements for these arrays
2422 * are introduced before and after the core scop.
2423 * Note that the input tree is not a leaf so that the declaration
2424 * cannot occur at the outer level.
2426 static struct pet_scop *scop_from_tree_macro(__isl_take pet_tree *tree,
2427 __isl_take isl_id *label, __isl_keep pet_context *pc,
2428 struct pet_state *state)
2430 struct pet_tree_extract_declared_arrays_data data = { pc, state };
2432 data.scop = scop_from_unevaluated_tree(pet_tree_copy(tree),
2433 state->n_stmt++, pc);
2435 data.any = 0;
2436 data.ctx = pet_context_get_ctx(pc);
2437 if (pet_tree_foreach_sub_tree(tree, &extract_declared_arrays,
2438 &data) < 0)
2439 data.scop = pet_scop_free(data.scop);
2440 pet_tree_free(tree);
2442 if (!data.any)
2443 return data.scop;
2445 data.scop = pet_scop_add_seq(data.ctx, data.kill_before, data.scop);
2446 data.scop = pet_scop_add_seq(data.ctx, data.scop, data.kill_after);
2448 return data.scop;
2451 /* Construct a pet_scop that corresponds to the pet_tree "tree"
2452 * within the context "pc" by calling the appropriate function
2453 * based on the type of "tree".
2455 * If the initially constructed pet_scop turns out to involve
2456 * dynamic control and if the user has requested an encapsulation
2457 * of all dynamic control, then this pet_scop is discarded and
2458 * a new pet_scop is created with a single statement representing
2459 * the entire "tree".
2460 * However, if the scop contains any active continue or break,
2461 * then we need to include the loop containing the continue or break
2462 * in the encapsulation. We therefore postpone the encapsulation
2463 * until we have constructed a pet_scop for this enclosing loop.
2465 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
2466 __isl_keep pet_context *pc, struct pet_state *state)
2468 isl_ctx *ctx;
2469 struct pet_scop *scop = NULL;
2471 if (!tree)
2472 return NULL;
2474 ctx = pet_tree_get_ctx(tree);
2475 switch (tree->type) {
2476 case pet_tree_error:
2477 return NULL;
2478 case pet_tree_block:
2479 return scop_from_block(tree, pc, state);
2480 case pet_tree_break:
2481 return scop_from_break(tree, pet_context_get_space(pc));
2482 case pet_tree_continue:
2483 return scop_from_continue(tree, pet_context_get_space(pc));
2484 case pet_tree_decl:
2485 case pet_tree_decl_init:
2486 return scop_from_decl(tree, pc, state);
2487 case pet_tree_expr:
2488 return scop_from_tree_expr(tree, pc, state);
2489 case pet_tree_if:
2490 case pet_tree_if_else:
2491 scop = scop_from_if(tree, pc, state);
2492 break;
2493 case pet_tree_for:
2494 scop = scop_from_for(tree, pc, state);
2495 break;
2496 case pet_tree_while:
2497 scop = scop_from_while(tree, pc, state);
2498 break;
2499 case pet_tree_infinite_loop:
2500 scop = scop_from_infinite_for(tree, pc, state);
2501 break;
2504 if (!scop)
2505 return NULL;
2507 if (!pet_options_get_encapsulate_dynamic_control(ctx) ||
2508 !pet_scop_has_data_dependent_conditions(scop) ||
2509 pet_scop_has_var_skip(scop, pet_skip_now))
2510 return scop;
2512 pet_scop_free(scop);
2513 return scop_from_tree_macro(pet_tree_copy(tree),
2514 isl_id_copy(tree->label), pc, state);
2517 /* If "tree" has a label that is of the form S_<nr>, then make
2518 * sure that state->n_stmt is greater than nr to ensure that
2519 * we will not generate S_<nr> ourselves.
2521 static int set_first_stmt(__isl_keep pet_tree *tree, void *user)
2523 struct pet_state *state = user;
2524 const char *name;
2525 int nr;
2527 if (!tree)
2528 return -1;
2529 if (!tree->label)
2530 return 0;
2531 name = isl_id_get_name(tree->label);
2532 if (strncmp(name, "S_", 2) != 0)
2533 return 0;
2534 nr = atoi(name + 2);
2535 if (nr >= state->n_stmt)
2536 state->n_stmt = nr + 1;
2538 return 0;
2541 /* Construct a pet_scop that corresponds to the pet_tree "tree".
2542 * "int_size" is the number of bytes need to represent an integer.
2543 * "extract_array" is a callback that we can use to create a pet_array
2544 * that corresponds to the variable accessed by an expression.
2546 * Initialize the global state, construct a context and then
2547 * construct the pet_scop by recursively visiting the tree.
2549 * state.n_stmt is initialized to point beyond any explicit S_<nr> label.
2551 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
2552 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
2553 __isl_keep pet_context *pc, void *user), void *user,
2554 __isl_keep pet_context *pc)
2556 struct pet_scop *scop;
2557 struct pet_state state = { 0 };
2559 if (!tree)
2560 return NULL;
2562 state.ctx = pet_tree_get_ctx(tree);
2563 state.int_size = int_size;
2564 state.extract_array = extract_array;
2565 state.user = user;
2566 if (pet_tree_foreach_sub_tree(tree, &set_first_stmt, &state) < 0)
2567 tree = pet_tree_free(tree);
2569 scop = scop_from_tree(tree, pc, &state);
2570 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
2572 pet_tree_free(tree);
2574 if (scop)
2575 scop->context = isl_set_params(scop->context);
2577 return scop;