drop pet_scop_writes
[pet.git] / tree2scop.c
blobb03a6ff2a15174879ba79e88168d5b5834fc3722
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
35 #include <isl/id_to_pw_aff.h>
37 #include "aff.h"
38 #include "expr.h"
39 #include "expr_arg.h"
40 #include "nest.h"
41 #include "scop.h"
42 #include "skip.h"
43 #include "state.h"
44 #include "tree2scop.h"
46 /* Update "pc" by taking into account the writes in "stmt".
47 * That is, first mark all scalar variables that are written by "stmt"
48 * as having an unknown value. Afterwards,
49 * if "stmt" is a top-level (i.e., unconditional) assignment
50 * to a scalar variable, then update "pc" accordingly.
52 * In particular, if the lhs of the assignment is a scalar variable, then mark
53 * the variable as having been assigned. If, furthermore, the rhs
54 * is an affine expression, then keep track of this value in "pc"
55 * so that we can plug it in when we later come across the same variable.
57 * We skip assignments to virtual arrays (those with NULL user pointer).
59 static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
60 __isl_take pet_context *pc)
62 pet_expr *body = stmt->body;
63 pet_expr *arg;
64 isl_id *id;
65 isl_pw_aff *pa;
67 pc = pet_context_clear_writes_in_expr(pc, body);
68 if (!pc)
69 return NULL;
71 if (pet_expr_get_type(body) != pet_expr_op)
72 return pc;
73 if (pet_expr_op_get_type(body) != pet_op_assign)
74 return pc;
75 if (!isl_set_plain_is_universe(stmt->domain))
76 return pc;
77 arg = pet_expr_get_arg(body, 0);
78 if (!pet_expr_is_scalar_access(arg)) {
79 pet_expr_free(arg);
80 return pc;
83 id = pet_expr_access_get_id(arg);
84 pet_expr_free(arg);
86 if (!isl_id_get_user(id)) {
87 isl_id_free(id);
88 return pc;
91 arg = pet_expr_get_arg(body, 1);
92 pa = pet_expr_extract_affine(arg, pc);
93 pc = pet_context_mark_assigned(pc, isl_id_copy(id));
94 pet_expr_free(arg);
96 if (pa && isl_pw_aff_involves_nan(pa)) {
97 isl_id_free(id);
98 isl_pw_aff_free(pa);
99 return pc;
102 pc = pet_context_set_value(pc, id, pa);
104 return pc;
107 /* Update "pc" based on the write accesses (and, in particular,
108 * assignments) in "scop".
110 static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
111 __isl_take pet_context *pc)
113 int i;
115 if (!scop)
116 return pet_context_free(pc);
117 for (i = 0; i < scop->n_stmt; ++i)
118 pc = handle_writes(scop->stmts[i], pc);
120 return pc;
123 /* Convert a top-level pet_expr to a pet_scop with one statement
124 * within the context "pc".
125 * This mainly involves resolving nested expression parameters
126 * and setting the name of the iteration space.
127 * The name is given by "label" if it is non-NULL. Otherwise,
128 * it is of the form S_<stmt_nr>.
129 * The location of the statement is set to "loc".
131 static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
132 __isl_take isl_id *label, int stmt_nr, __isl_take pet_loc *loc,
133 __isl_keep pet_context *pc)
135 isl_ctx *ctx;
136 struct pet_stmt *ps;
138 ctx = pet_expr_get_ctx(expr);
140 expr = pet_expr_plug_in_args(expr, pc);
141 expr = pet_expr_resolve_nested(expr);
142 expr = pet_expr_resolve_assume(expr, pc);
143 ps = pet_stmt_from_pet_expr(loc, label, stmt_nr, expr);
144 return pet_scop_from_pet_stmt(ctx, ps);
147 /* Construct a pet_scop with a single statement killing the entire
148 * array "array".
149 * The location of the statement is set to "loc".
151 static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
152 __isl_keep pet_context *pc, struct pet_state *state)
154 isl_ctx *ctx;
155 isl_id *id;
156 isl_space *space;
157 isl_multi_pw_aff *index;
158 isl_map *access;
159 pet_expr *expr;
160 struct pet_scop *scop;
162 if (!array)
163 goto error;
164 ctx = isl_set_get_ctx(array->extent);
165 access = isl_map_from_range(isl_set_copy(array->extent));
166 id = isl_set_get_tuple_id(array->extent);
167 space = isl_space_alloc(ctx, 0, 0, 0);
168 space = isl_space_set_tuple_id(space, isl_dim_out, id);
169 index = isl_multi_pw_aff_zero(space);
170 expr = pet_expr_kill_from_access_and_index(access, index);
171 return scop_from_expr(expr, NULL, state->n_stmt++, loc, pc);
172 error:
173 pet_loc_free(loc);
174 return NULL;
177 /* Construct and return a pet_array corresponding to the variable
178 * accessed by "access" by calling the extract_array callback.
180 static struct pet_array *extract_array(__isl_keep pet_expr *access,
181 __isl_keep pet_context *pc, struct pet_state *state)
183 return state->extract_array(access, pc, state->user);
186 /* Construct a pet_scop for a (single) variable declaration
187 * within the context "pc".
189 * The scop contains the variable being declared (as an array)
190 * and a statement killing the array.
192 * If the declaration comes with an initialization, then the scop
193 * also contains an assignment to the variable.
195 static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
196 __isl_keep pet_context *pc, struct pet_state *state)
198 int type_size;
199 isl_ctx *ctx;
200 struct pet_array *array;
201 struct pet_scop *scop_decl, *scop;
202 pet_expr *lhs, *rhs, *pe;
204 array = extract_array(tree->u.d.var, pc, state);
205 if (array)
206 array->declared = 1;
207 scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
208 scop_decl = pet_scop_add_array(scop_decl, array);
210 if (tree->type != pet_tree_decl_init)
211 return scop_decl;
213 lhs = pet_expr_copy(tree->u.d.var);
214 rhs = pet_expr_copy(tree->u.d.init);
215 type_size = pet_expr_get_type_size(lhs);
216 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
217 scop = scop_from_expr(pe, NULL, state->n_stmt++,
218 pet_tree_get_loc(tree), pc);
220 scop_decl = pet_scop_prefix(scop_decl, 0);
221 scop = pet_scop_prefix(scop, 1);
223 ctx = pet_tree_get_ctx(tree);
224 scop = pet_scop_add_seq(ctx, scop_decl, scop);
226 return scop;
229 /* Embed the given iteration domain in an extra outer loop
230 * with induction variable "var".
231 * If this variable appeared as a parameter in the constraints,
232 * it is replaced by the new outermost dimension.
234 static __isl_give isl_set *embed(__isl_take isl_set *set,
235 __isl_take isl_id *var)
237 int pos;
239 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
240 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
241 if (pos >= 0) {
242 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
243 set = isl_set_project_out(set, isl_dim_param, pos, 1);
246 isl_id_free(var);
247 return set;
250 /* Return those elements in the space of "cond" that come after
251 * (based on "sign") an element in "cond".
253 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
255 isl_map *previous_to_this;
257 if (sign > 0)
258 previous_to_this = isl_map_lex_lt(isl_set_get_space(cond));
259 else
260 previous_to_this = isl_map_lex_gt(isl_set_get_space(cond));
262 cond = isl_set_apply(cond, previous_to_this);
264 return cond;
267 /* Create the infinite iteration domain
269 * { [id] : id >= 0 }
271 * If "scop" has an affine skip of type pet_skip_later,
272 * then remove those iterations i that have an earlier iteration
273 * where the skip condition is satisfied, meaning that iteration i
274 * is not executed.
275 * Since we are dealing with a loop without loop iterator,
276 * the skip condition cannot refer to the current loop iterator and
277 * so effectively, the returned set is of the form
279 * { [0]; [id] : id >= 1 and not skip }
281 static __isl_give isl_set *infinite_domain(__isl_take isl_id *id,
282 struct pet_scop *scop)
284 isl_ctx *ctx = isl_id_get_ctx(id);
285 isl_set *domain;
286 isl_set *skip;
288 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
289 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, id);
291 if (!pet_scop_has_affine_skip(scop, pet_skip_later))
292 return domain;
294 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
295 skip = embed(skip, isl_id_copy(id));
296 skip = isl_set_intersect(skip , isl_set_copy(domain));
297 domain = isl_set_subtract(domain, after(skip, 1));
299 return domain;
302 /* Create an identity affine expression on the space containing "domain",
303 * which is assumed to be one-dimensional.
305 static __isl_give isl_aff *identity_aff(__isl_keep isl_set *domain)
307 isl_local_space *ls;
309 ls = isl_local_space_from_space(isl_set_get_space(domain));
310 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
313 /* Create an affine expression that maps elements
314 * of a single-dimensional array "id_test" to the previous element
315 * (according to "inc"), provided this element belongs to "domain".
316 * That is, create the affine expression
318 * { id[x] -> id[x - inc] : x - inc in domain }
320 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
321 __isl_take isl_set *domain, __isl_take isl_val *inc)
323 isl_space *space;
324 isl_local_space *ls;
325 isl_aff *aff;
326 isl_multi_pw_aff *prev;
328 space = isl_set_get_space(domain);
329 ls = isl_local_space_from_space(space);
330 aff = isl_aff_var_on_domain(ls, isl_dim_set, 0);
331 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
332 prev = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
333 domain = isl_set_preimage_multi_pw_aff(domain,
334 isl_multi_pw_aff_copy(prev));
335 prev = isl_multi_pw_aff_intersect_domain(prev, domain);
336 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
338 return prev;
341 /* Add an implication to "scop" expressing that if an element of
342 * virtual array "id_test" has value "satisfied" then all previous elements
343 * of this array also have that value. The set of previous elements
344 * is bounded by "domain". If "sign" is negative then the iterator
345 * is decreasing and we express that all subsequent array elements
346 * (but still defined previously) have the same value.
348 static struct pet_scop *add_implication(struct pet_scop *scop,
349 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
350 int satisfied)
352 isl_space *space;
353 isl_map *map;
355 domain = isl_set_set_tuple_id(domain, id_test);
356 space = isl_set_get_space(domain);
357 if (sign > 0)
358 map = isl_map_lex_ge(space);
359 else
360 map = isl_map_lex_le(space);
361 map = isl_map_intersect_range(map, domain);
362 scop = pet_scop_add_implication(scop, map, satisfied);
364 return scop;
367 /* Add a filter to "scop" that imposes that it is only executed
368 * when the variable identified by "id_test" has a zero value
369 * for all previous iterations of "domain".
371 * In particular, add a filter that imposes that the array
372 * has a zero value at the previous iteration of domain and
373 * add an implication that implies that it then has that
374 * value for all previous iterations.
376 static struct pet_scop *scop_add_break(struct pet_scop *scop,
377 __isl_take isl_id *id_test, __isl_take isl_set *domain,
378 __isl_take isl_val *inc)
380 isl_multi_pw_aff *prev;
381 int sign = isl_val_sgn(inc);
383 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
384 scop = add_implication(scop, id_test, domain, sign, 0);
385 scop = pet_scop_filter(scop, prev, 0);
387 return scop;
390 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
391 __isl_keep pet_context *pc, struct pet_state *state);
393 /* Construct a pet_scop for an infinite loop around the given body
394 * within the context "pc".
396 * We extract a pet_scop for the body and then embed it in a loop with
397 * iteration domain
399 * { [t] : t >= 0 }
401 * and schedule
403 * { [t] -> [t] }
405 * If the body contains any break, then it is taken into
406 * account in infinite_domain (if the skip condition is affine)
407 * or in scop_add_break (if the skip condition is not affine).
409 static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
410 __isl_keep pet_context *pc, struct pet_state *state)
412 isl_ctx *ctx;
413 isl_id *id, *id_test;
414 isl_set *domain;
415 isl_aff *ident;
416 struct pet_scop *scop;
417 int has_var_break;
419 scop = scop_from_tree(body, pc, state);
421 ctx = pet_tree_get_ctx(body);
422 id = isl_id_alloc(ctx, "t", NULL);
423 domain = infinite_domain(isl_id_copy(id), scop);
424 ident = identity_aff(domain);
426 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
427 if (has_var_break)
428 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
430 scop = pet_scop_embed(scop, isl_set_copy(domain),
431 isl_aff_copy(ident), ident, id);
432 if (has_var_break)
433 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
434 else
435 isl_set_free(domain);
437 return scop;
440 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
442 * for (;;)
443 * body
445 * within the context "pc".
447 static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
448 __isl_keep pet_context *pc, struct pet_state *state)
450 struct pet_scop *scop;
452 pc = pet_context_copy(pc);
453 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
455 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
457 pet_context_free(pc);
459 return scop;
462 /* Construct a pet_scop for a while loop of the form
464 * while (pa)
465 * body
467 * within the context "pc".
468 * In particular, construct a scop for an infinite loop around body and
469 * intersect the domain with the affine expression.
470 * Note that this intersection may result in an empty loop.
472 static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
473 __isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
474 struct pet_state *state)
476 struct pet_scop *scop;
477 isl_set *dom;
478 isl_set *valid;
480 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
481 dom = isl_pw_aff_non_zero_set(pa);
482 scop = scop_from_infinite_loop(tree->u.l.body, pc, state);
483 scop = pet_scop_restrict(scop, isl_set_params(dom));
484 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
486 pet_context_free(pc);
487 return scop;
490 /* Construct a scop for a while, given the scops for the condition
491 * and the body, the filter identifier and the iteration domain of
492 * the while loop.
494 * In particular, the scop for the condition is filtered to depend
495 * on "id_test" evaluating to true for all previous iterations
496 * of the loop, while the scop for the body is filtered to depend
497 * on "id_test" evaluating to true for all iterations up to the
498 * current iteration.
499 * The actual filter only imposes that this virtual array has
500 * value one on the previous or the current iteration.
501 * The fact that this condition also applies to the previous
502 * iterations is enforced by an implication.
504 * These filtered scops are then combined into a single scop.
506 * "sign" is positive if the iterator increases and negative
507 * if it decreases.
509 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
510 struct pet_scop *scop_body, __isl_take isl_id *id_test,
511 __isl_take isl_set *domain, __isl_take isl_val *inc)
513 isl_ctx *ctx = isl_set_get_ctx(domain);
514 isl_space *space;
515 isl_multi_pw_aff *test_index;
516 isl_multi_pw_aff *prev;
517 int sign = isl_val_sgn(inc);
518 struct pet_scop *scop;
520 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
521 scop_cond = pet_scop_filter(scop_cond, prev, 1);
523 space = isl_space_map_from_set(isl_set_get_space(domain));
524 test_index = isl_multi_pw_aff_identity(space);
525 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
526 isl_id_copy(id_test));
527 scop_body = pet_scop_filter(scop_body, test_index, 1);
529 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
530 scop = add_implication(scop, id_test, domain, sign, 1);
532 return scop;
535 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
536 * evaluating "cond" and writing the result to a virtual scalar,
537 * as expressed by "index".
538 * Do so within the context "pc".
539 * The location of the statement is set to "loc".
541 static struct pet_scop *scop_from_non_affine_condition(
542 __isl_take pet_expr *cond, int stmt_nr,
543 __isl_take isl_multi_pw_aff *index,
544 __isl_take pet_loc *loc, __isl_keep pet_context *pc)
546 pet_expr *expr, *write;
548 write = pet_expr_from_index(index);
549 write = pet_expr_access_set_write(write, 1);
550 write = pet_expr_access_set_read(write, 0);
551 expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
553 return scop_from_expr(expr, NULL, stmt_nr, loc, pc);
556 /* Construct a generic while scop, with iteration domain
557 * { [t] : t >= 0 } around "scop_body" within the context "pc".
558 * The scop consists of two parts,
559 * one for evaluating the condition "cond" and one for the body.
560 * "test_nr" is the sequence number of the virtual test variable that contains
561 * the result of the condition and "stmt_nr" is the sequence number
562 * of the statement that evaluates the condition.
563 * If "scop_inc" is not NULL, then it is added at the end of the body,
564 * after replacing any skip conditions resulting from continue statements
565 * by the skip conditions resulting from break statements (if any).
567 * The schedule is adjusted to reflect that the condition is evaluated
568 * before the body is executed and the body is filtered to depend
569 * on the result of the condition evaluating to true on all iterations
570 * up to the current iteration, while the evaluation of the condition itself
571 * is filtered to depend on the result of the condition evaluating to true
572 * on all previous iterations.
573 * The context of the scop representing the body is dropped
574 * because we don't know how many times the body will be executed,
575 * if at all.
577 * If the body contains any break, then it is taken into
578 * account in infinite_domain (if the skip condition is affine)
579 * or in scop_add_break (if the skip condition is not affine).
581 static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
582 int test_nr, int stmt_nr, __isl_take pet_loc *loc,
583 struct pet_scop *scop_body, struct pet_scop *scop_inc,
584 __isl_take pet_context *pc, struct pet_state *state)
586 isl_ctx *ctx;
587 isl_id *id, *id_test, *id_break_test;
588 isl_multi_pw_aff *test_index;
589 isl_set *domain;
590 isl_aff *ident;
591 struct pet_scop *scop;
592 int has_var_break;
594 ctx = state->ctx;
595 test_index = pet_create_test_index(ctx, test_nr);
596 scop = scop_from_non_affine_condition(cond, stmt_nr,
597 isl_multi_pw_aff_copy(test_index), loc, pc);
598 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
599 scop = pet_scop_add_boolean_array(scop, test_index, state->int_size);
601 id = isl_id_alloc(ctx, "t", NULL);
602 domain = infinite_domain(isl_id_copy(id), scop_body);
603 ident = identity_aff(domain);
605 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
606 if (has_var_break)
607 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
609 scop = pet_scop_prefix(scop, 0);
610 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(ident),
611 isl_aff_copy(ident), isl_id_copy(id));
612 scop_body = pet_scop_reset_context(scop_body);
613 scop_body = pet_scop_prefix(scop_body, 1);
614 if (scop_inc) {
615 scop_inc = pet_scop_prefix(scop_inc, 2);
616 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
617 isl_multi_pw_aff *skip;
618 skip = pet_scop_get_skip(scop_body, pet_skip_later);
619 scop_body = pet_scop_set_skip(scop_body,
620 pet_skip_now, skip);
621 } else
622 pet_scop_reset_skip(scop_body, pet_skip_now);
623 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
625 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain),
626 isl_aff_copy(ident), ident, id);
628 if (has_var_break) {
629 scop = scop_add_break(scop, isl_id_copy(id_break_test),
630 isl_set_copy(domain), isl_val_one(ctx));
631 scop_body = scop_add_break(scop_body, id_break_test,
632 isl_set_copy(domain), isl_val_one(ctx));
634 scop = scop_add_while(scop, scop_body, id_test, domain,
635 isl_val_one(ctx));
637 pet_context_free(pc);
638 return scop;
641 /* Check if the while loop is of the form
643 * while (affine expression)
644 * body
646 * If so, call scop_from_affine_while to construct a scop.
648 * Otherwise, extract the body and pass control to scop_from_non_affine_while
649 * to extend the iteration domain with an infinite loop.
651 * "pc" is the context in which the affine expressions in the scop are created.
653 static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
654 __isl_keep pet_context *pc, struct pet_state *state)
656 pet_expr *cond_expr;
657 int test_nr, stmt_nr;
658 isl_pw_aff *pa;
659 struct pet_scop *scop_body;
661 if (!tree)
662 return NULL;
664 pc = pet_context_copy(pc);
665 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
667 cond_expr = pet_expr_copy(tree->u.l.cond);
668 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
669 pa = pet_expr_extract_affine_condition(cond_expr, pc);
670 pet_expr_free(cond_expr);
672 if (!pa)
673 goto error;
675 if (!isl_pw_aff_involves_nan(pa))
676 return scop_from_affine_while(tree, pa, pc, state);
677 isl_pw_aff_free(pa);
678 test_nr = state->n_test++;
679 stmt_nr = state->n_stmt++;
680 scop_body = scop_from_tree(tree->u.l.body, pc, state);
681 return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
682 test_nr, stmt_nr, pet_tree_get_loc(tree),
683 scop_body, NULL, pc, state);
684 error:
685 pet_context_free(pc);
686 return NULL;
689 /* Check whether "cond" expresses a simple loop bound
690 * on the only set dimension.
691 * In particular, if "up" is set then "cond" should contain only
692 * upper bounds on the set dimension.
693 * Otherwise, it should contain only lower bounds.
695 static int is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
697 if (isl_val_is_pos(inc))
698 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, 0);
699 else
700 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, 0);
703 /* Extend a condition on a given iteration of a loop to one that
704 * imposes the same condition on all previous iterations.
705 * "domain" expresses the lower [upper] bound on the iterations
706 * when inc is positive [negative].
708 * In particular, we construct the condition (when inc is positive)
710 * forall i' : (domain(i') and i' <= i) => cond(i')
712 * which is equivalent to
714 * not exists i' : domain(i') and i' <= i and not cond(i')
716 * We construct this set by negating cond, applying a map
718 * { [i'] -> [i] : domain(i') and i' <= i }
720 * and then negating the result again.
722 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
723 __isl_take isl_set *domain, __isl_take isl_val *inc)
725 isl_map *previous_to_this;
727 if (isl_val_is_pos(inc))
728 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
729 else
730 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
732 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
734 cond = isl_set_complement(cond);
735 cond = isl_set_apply(cond, previous_to_this);
736 cond = isl_set_complement(cond);
738 isl_val_free(inc);
740 return cond;
743 /* Construct a domain of the form
745 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
747 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
748 __isl_take isl_pw_aff *init, __isl_take isl_val *inc)
750 isl_aff *aff;
751 isl_space *dim;
752 isl_set *set;
754 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
755 dim = isl_pw_aff_get_domain_space(init);
756 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
757 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, 0, inc);
758 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
760 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
761 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
762 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
763 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
765 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
767 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
769 return isl_set_params(set);
772 /* Assuming "cond" represents a bound on a loop where the loop
773 * iterator "iv" is incremented (or decremented) by one, check if wrapping
774 * is possible.
776 * Under the given assumptions, wrapping is only possible if "cond" allows
777 * for the last value before wrapping, i.e., 2^width - 1 in case of an
778 * increasing iterator and 0 in case of a decreasing iterator.
780 static int can_wrap(__isl_keep isl_set *cond, __isl_keep pet_expr *iv,
781 __isl_keep isl_val *inc)
783 int cw;
784 isl_ctx *ctx;
785 isl_val *limit;
786 isl_set *test;
788 test = isl_set_copy(cond);
790 ctx = isl_set_get_ctx(test);
791 if (isl_val_is_neg(inc))
792 limit = isl_val_zero(ctx);
793 else {
794 limit = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
795 limit = isl_val_2exp(limit);
796 limit = isl_val_sub_ui(limit, 1);
799 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
800 cw = !isl_set_is_empty(test);
801 isl_set_free(test);
803 return cw;
806 /* Given a one-dimensional space, construct the following affine expression
807 * on this space
809 * { [v] -> [v mod 2^width] }
811 * where width is the number of bits used to represent the values
812 * of the unsigned variable "iv".
814 static __isl_give isl_aff *compute_wrapping(__isl_take isl_space *dim,
815 __isl_keep pet_expr *iv)
817 isl_ctx *ctx;
818 isl_val *mod;
819 isl_aff *aff;
821 ctx = isl_space_get_ctx(dim);
822 mod = isl_val_int_from_ui(ctx, pet_expr_get_type_size(iv));
823 mod = isl_val_2exp(mod);
825 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
826 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
827 aff = isl_aff_mod_val(aff, mod);
829 return aff;
832 /* Project out the parameter "id" from "set".
834 static __isl_give isl_set *set_project_out_by_id(__isl_take isl_set *set,
835 __isl_keep isl_id *id)
837 int pos;
839 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
840 if (pos >= 0)
841 set = isl_set_project_out(set, isl_dim_param, pos, 1);
843 return set;
846 /* Compute the set of parameters for which "set1" is a subset of "set2".
848 * set1 is a subset of set2 if
850 * forall i in set1 : i in set2
852 * or
854 * not exists i in set1 and i not in set2
856 * i.e.,
858 * not exists i in set1 \ set2
860 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
861 __isl_take isl_set *set2)
863 return isl_set_complement(isl_set_params(isl_set_subtract(set1, set2)));
866 /* Compute the set of parameter values for which "cond" holds
867 * on the next iteration for each element of "dom".
869 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
870 * and then compute the set of parameters for which the result is a subset
871 * of "cond".
873 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
874 __isl_take isl_set *dom, __isl_take isl_val *inc)
876 isl_space *space;
877 isl_aff *aff;
878 isl_map *next;
880 space = isl_set_get_space(dom);
881 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
882 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
883 aff = isl_aff_add_constant_val(aff, inc);
884 next = isl_map_from_basic_map(isl_basic_map_from_aff(aff));
886 dom = isl_set_apply(dom, next);
888 return enforce_subset(dom, cond);
891 /* Extract the for loop "tree" as a while loop within the context "pc".
893 * That is, the for loop has the form
895 * for (iv = init; cond; iv += inc)
896 * body;
898 * and is treated as
900 * iv = init;
901 * while (cond) {
902 * body;
903 * iv += inc;
906 * except that the skips resulting from any continue statements
907 * in body do not apply to the increment, but are replaced by the skips
908 * resulting from break statements.
910 * If the loop iterator is declared in the for loop, then it is killed before
911 * and after the loop.
913 static struct pet_scop *scop_from_non_affine_for(__isl_keep pet_tree *tree,
914 __isl_take pet_context *pc, struct pet_state *state)
916 int declared;
917 int test_nr, stmt_nr;
918 isl_id *iv;
919 pet_expr *expr_iv, *init, *inc;
920 struct pet_scop *scop_init, *scop_inc, *scop, *scop_body;
921 int type_size;
922 struct pet_array *array;
923 struct pet_scop *scop_kill;
925 iv = pet_expr_access_get_id(tree->u.l.iv);
926 pc = pet_context_mark_assigned(pc, iv);
928 declared = tree->u.l.declared;
930 expr_iv = pet_expr_copy(tree->u.l.iv);
931 type_size = pet_expr_get_type_size(expr_iv);
932 init = pet_expr_copy(tree->u.l.init);
933 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
934 scop_init = scop_from_expr(init, NULL, state->n_stmt++,
935 pet_tree_get_loc(tree), pc);
936 scop_init = pet_scop_prefix(scop_init, declared);
938 test_nr = state->n_test++;
939 stmt_nr = state->n_stmt++;
940 scop_body = scop_from_tree(tree->u.l.body, pc, state);
942 expr_iv = pet_expr_copy(tree->u.l.iv);
943 type_size = pet_expr_get_type_size(expr_iv);
944 inc = pet_expr_copy(tree->u.l.inc);
945 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
946 scop_inc = scop_from_expr(inc, NULL, state->n_stmt++,
947 pet_tree_get_loc(tree), pc);
949 scop = scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),
950 test_nr, stmt_nr, pet_tree_get_loc(tree),
951 scop_body, scop_inc, pet_context_copy(pc), state);
953 scop = pet_scop_prefix(scop, declared + 1);
954 scop = pet_scop_add_seq(state->ctx, scop_init, scop);
956 if (!declared) {
957 pet_context_free(pc);
958 return scop;
961 array = extract_array(tree->u.l.iv, pc, state);
962 if (array)
963 array->declared = 1;
964 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
965 scop_kill = pet_scop_prefix(scop_kill, 0);
966 scop = pet_scop_add_seq(state->ctx, scop_kill, scop);
967 scop_kill = kill(pet_tree_get_loc(tree), array, pc, state);
968 scop_kill = pet_scop_add_array(scop_kill, array);
969 scop_kill = pet_scop_prefix(scop_kill, 3);
970 scop = pet_scop_add_seq(state->ctx, scop, scop_kill);
972 pet_context_free(pc);
973 return scop;
976 /* Given an access expression "expr", is the variable accessed by
977 * "expr" assigned anywhere inside "tree"?
979 static int is_assigned(__isl_keep pet_expr *expr, __isl_keep pet_tree *tree)
981 int assigned = 0;
982 isl_id *id;
984 id = pet_expr_access_get_id(expr);
985 assigned = pet_tree_writes(tree, id);
986 isl_id_free(id);
988 return assigned;
991 /* Are all nested access parameters in "pa" allowed given "tree".
992 * In particular, is none of them written by anywhere inside "tree".
994 * If "tree" has any continue nodes in the current loop level,
995 * then no nested access parameters are allowed.
996 * In particular, if there is any nested access in a guard
997 * for a piece of code containing a "continue", then we want to introduce
998 * a separate statement for evaluating this guard so that we can express
999 * that the result is false for all previous iterations.
1001 static int is_nested_allowed(__isl_keep isl_pw_aff *pa,
1002 __isl_keep pet_tree *tree)
1004 int i, nparam;
1006 if (!tree)
1007 return -1;
1009 if (!pet_nested_any_in_pw_aff(pa))
1010 return 1;
1012 if (pet_tree_has_continue(tree))
1013 return 0;
1015 nparam = isl_pw_aff_dim(pa, isl_dim_param);
1016 for (i = 0; i < nparam; ++i) {
1017 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
1018 pet_expr *expr;
1019 int allowed;
1021 if (!pet_nested_in_id(id)) {
1022 isl_id_free(id);
1023 continue;
1026 expr = pet_nested_extract_expr(id);
1027 allowed = pet_expr_get_type(expr) == pet_expr_access &&
1028 !is_assigned(expr, tree);
1030 pet_expr_free(expr);
1031 isl_id_free(id);
1033 if (!allowed)
1034 return 0;
1037 return 1;
1040 /* Construct a pet_scop for a for tree with static affine initialization
1041 * and constant increment within the context "pc".
1043 * The condition is allowed to contain nested accesses, provided
1044 * they are not being written to inside the body of the loop.
1045 * Otherwise, or if the condition is otherwise non-affine, the for loop is
1046 * essentially treated as a while loop, with iteration domain
1047 * { [i] : i >= init }.
1049 * We extract a pet_scop for the body and then embed it in a loop with
1050 * iteration domain and schedule
1052 * { [i] : i >= init and condition' }
1053 * { [i] -> [i] }
1055 * or
1057 * { [i] : i <= init and condition' }
1058 * { [i] -> [-i] }
1060 * Where condition' is equal to condition if the latter is
1061 * a simple upper [lower] bound and a condition that is extended
1062 * to apply to all previous iterations otherwise.
1064 * If the condition is non-affine, then we drop the condition from the
1065 * iteration domain and instead create a separate statement
1066 * for evaluating the condition. The body is then filtered to depend
1067 * on the result of the condition evaluating to true on all iterations
1068 * up to the current iteration, while the evaluation the condition itself
1069 * is filtered to depend on the result of the condition evaluating to true
1070 * on all previous iterations.
1071 * The context of the scop representing the body is dropped
1072 * because we don't know how many times the body will be executed,
1073 * if at all.
1075 * If the stride of the loop is not 1, then "i >= init" is replaced by
1077 * (exists a: i = init + stride * a and a >= 0)
1079 * If the loop iterator i is unsigned, then wrapping may occur.
1080 * We therefore use a virtual iterator instead that does not wrap.
1081 * However, the condition in the code applies
1082 * to the wrapped value, so we need to change condition(i)
1083 * into condition([i % 2^width]). Similarly, we replace all accesses
1084 * to the original iterator by the wrapping of the virtual iterator.
1085 * Note that there may be no need to perform this final wrapping
1086 * if the loop condition (after wrapping) satisfies certain conditions.
1087 * However, the is_simple_bound condition is not enough since it doesn't
1088 * check if there even is an upper bound.
1090 * Wrapping on unsigned iterators can be avoided entirely if
1091 * loop condition is simple, the loop iterator is incremented
1092 * [decremented] by one and the last value before wrapping cannot
1093 * possibly satisfy the loop condition.
1095 * Valid parameters for a for loop are those for which the initial
1096 * value itself, the increment on each domain iteration and
1097 * the condition on both the initial value and
1098 * the result of incrementing the iterator for each iteration of the domain
1099 * can be evaluated.
1100 * If the loop condition is non-affine, then we only consider validity
1101 * of the initial value.
1103 * If the body contains any break, then we keep track of it in "skip"
1104 * (if the skip condition is affine) or it is handled in scop_add_break
1105 * (if the skip condition is not affine).
1106 * Note that the affine break condition needs to be considered with
1107 * respect to previous iterations in the virtual domain (if any).
1109 static struct pet_scop *scop_from_affine_for(__isl_keep pet_tree *tree,
1110 __isl_take isl_pw_aff *init_val, __isl_take isl_pw_aff *pa_inc,
1111 __isl_take isl_val *inc, __isl_take pet_context *pc,
1112 struct pet_state *state)
1114 isl_local_space *ls;
1115 isl_set *domain;
1116 isl_aff *sched;
1117 isl_set *cond = NULL;
1118 isl_set *skip = NULL;
1119 isl_id *id, *id_test = NULL, *id_break_test;
1120 struct pet_scop *scop, *scop_cond = NULL;
1121 int is_one;
1122 int is_unsigned;
1123 int is_simple;
1124 int is_virtual;
1125 int has_affine_break;
1126 int has_var_break;
1127 isl_aff *wrap = NULL;
1128 isl_pw_aff *pa;
1129 isl_set *valid_init;
1130 isl_set *valid_cond;
1131 isl_set *valid_cond_init;
1132 isl_set *valid_cond_next;
1133 isl_set *valid_inc;
1134 int stmt_id;
1135 pet_expr *cond_expr;
1136 pet_context *pc_nested;
1138 id = pet_expr_access_get_id(tree->u.l.iv);
1140 cond_expr = pet_expr_copy(tree->u.l.cond);
1141 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1142 pc_nested = pet_context_copy(pc);
1143 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1144 pa = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1145 pet_context_free(pc_nested);
1146 pet_expr_free(cond_expr);
1147 if (isl_pw_aff_involves_nan(pa) || pet_nested_any_in_pw_aff(pa))
1148 stmt_id = state->n_stmt++;
1150 scop = scop_from_tree(tree->u.l.body, pc, state);
1152 valid_inc = isl_pw_aff_domain(pa_inc);
1154 is_unsigned = pet_expr_get_type_size(tree->u.l.iv) > 0;
1156 has_affine_break = scop &&
1157 pet_scop_has_affine_skip(scop, pet_skip_later);
1158 if (has_affine_break)
1159 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1160 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
1161 if (has_var_break)
1162 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
1164 if (isl_pw_aff_involves_nan(pa) ||
1165 !is_nested_allowed(pa, tree->u.l.body))
1166 pa = isl_pw_aff_free(pa);
1168 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1169 cond = isl_pw_aff_non_zero_set(pa);
1170 if (!cond) {
1171 isl_multi_pw_aff *test_index;
1172 int save_n_stmt = state->n_stmt;
1173 test_index = pet_create_test_index(state->ctx, state->n_test++);
1174 state->n_stmt = stmt_id;
1175 scop_cond = scop_from_non_affine_condition(
1176 pet_expr_copy(tree->u.l.cond), state->n_stmt++,
1177 isl_multi_pw_aff_copy(test_index),
1178 pet_tree_get_loc(tree), pc);
1179 state->n_stmt = save_n_stmt;
1180 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
1181 isl_dim_out);
1182 scop_cond = pet_scop_add_boolean_array(scop_cond, test_index,
1183 state->int_size);
1184 scop_cond = pet_scop_prefix(scop_cond, 0);
1185 scop = pet_scop_reset_context(scop);
1186 scop = pet_scop_prefix(scop, 1);
1187 cond = isl_set_universe(isl_space_set_alloc(state->ctx, 0, 0));
1190 cond = embed(cond, isl_id_copy(id));
1191 skip = embed(skip, isl_id_copy(id));
1192 valid_cond = isl_set_coalesce(valid_cond);
1193 valid_cond = embed(valid_cond, isl_id_copy(id));
1194 valid_inc = embed(valid_inc, isl_id_copy(id));
1195 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
1196 is_virtual = is_unsigned &&
1197 (!is_one || can_wrap(cond, tree->u.l.iv, inc));
1199 valid_cond_init = enforce_subset(
1200 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val))),
1201 isl_set_copy(valid_cond));
1202 if (is_one && !is_virtual) {
1203 isl_pw_aff_free(init_val);
1204 pa = pet_expr_extract_comparison(
1205 isl_val_is_pos(inc) ? pet_op_ge : pet_op_le,
1206 tree->u.l.iv, tree->u.l.init, pc);
1207 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1208 valid_init = set_project_out_by_id(valid_init, id);
1209 domain = isl_pw_aff_non_zero_set(pa);
1210 } else {
1211 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
1212 domain = strided_domain(isl_id_copy(id), init_val,
1213 isl_val_copy(inc));
1216 domain = embed(domain, isl_id_copy(id));
1217 if (is_virtual) {
1218 isl_map *rev_wrap;
1219 wrap = compute_wrapping(isl_set_get_space(cond), tree->u.l.iv);
1220 rev_wrap = isl_map_from_aff(isl_aff_copy(wrap));
1221 rev_wrap = isl_map_reverse(rev_wrap);
1222 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
1223 skip = isl_set_apply(skip, isl_map_copy(rev_wrap));
1224 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
1225 valid_inc = isl_set_apply(valid_inc, rev_wrap);
1227 is_simple = is_simple_bound(cond, inc);
1228 if (!is_simple) {
1229 cond = isl_set_gist(cond, isl_set_copy(domain));
1230 is_simple = is_simple_bound(cond, inc);
1232 if (!is_simple)
1233 cond = valid_for_each_iteration(cond,
1234 isl_set_copy(domain), isl_val_copy(inc));
1235 domain = isl_set_intersect(domain, cond);
1236 if (has_affine_break) {
1237 skip = isl_set_intersect(skip , isl_set_copy(domain));
1238 skip = after(skip, isl_val_sgn(inc));
1239 domain = isl_set_subtract(domain, skip);
1241 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1242 ls = isl_local_space_from_space(isl_set_get_space(domain));
1243 sched = isl_aff_var_on_domain(ls, isl_dim_set, 0);
1244 if (isl_val_is_neg(inc))
1245 sched = isl_aff_neg(sched);
1247 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
1248 isl_val_copy(inc));
1249 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
1251 if (!is_virtual)
1252 wrap = identity_aff(domain);
1254 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
1255 isl_aff_copy(sched), isl_aff_copy(wrap), isl_id_copy(id));
1256 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
1257 scop = pet_scop_resolve_nested(scop);
1258 if (has_var_break)
1259 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
1260 isl_val_copy(inc));
1261 if (id_test) {
1262 scop = scop_add_while(scop_cond, scop, id_test, domain,
1263 isl_val_copy(inc));
1264 isl_set_free(valid_inc);
1265 } else {
1266 scop = pet_scop_restrict_context(scop, valid_inc);
1267 scop = pet_scop_restrict_context(scop, valid_cond_next);
1268 scop = pet_scop_restrict_context(scop, valid_cond_init);
1269 isl_set_free(domain);
1272 isl_val_free(inc);
1274 scop = pet_scop_restrict_context(scop, isl_set_params(valid_init));
1276 pet_context_free(pc);
1277 return scop;
1280 /* Construct a pet_scop for a for statement within the context of "pc".
1282 * We update the context to reflect the writes to the loop variable and
1283 * the writes inside the body.
1285 * Then we check if the initialization of the for loop
1286 * is a static affine value and the increment is a constant.
1287 * If so, we construct the pet_scop using scop_from_affine_for.
1288 * Otherwise, we treat the for loop as a while loop
1289 * in scop_from_non_affine_for.
1291 static struct pet_scop *scop_from_for(__isl_keep pet_tree *tree,
1292 __isl_keep pet_context *pc, struct pet_state *state)
1294 isl_id *iv;
1295 isl_val *inc;
1296 isl_pw_aff *pa_inc, *init_val;
1297 pet_context *pc_init_val;
1299 if (!tree)
1300 return NULL;
1302 iv = pet_expr_access_get_id(tree->u.l.iv);
1303 pc = pet_context_copy(pc);
1304 pc = pet_context_clear_value(pc, iv);
1305 pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
1307 pc_init_val = pet_context_copy(pc);
1308 pc_init_val = pet_context_mark_unknown(pc_init_val, isl_id_copy(iv));
1309 init_val = pet_expr_extract_affine(tree->u.l.init, pc_init_val);
1310 pet_context_free(pc_init_val);
1311 pa_inc = pet_expr_extract_affine(tree->u.l.inc, pc);
1312 inc = pet_extract_cst(pa_inc);
1313 if (!pa_inc || !init_val || !inc)
1314 goto error;
1315 if (!isl_pw_aff_involves_nan(pa_inc) &&
1316 !isl_pw_aff_involves_nan(init_val) && !isl_val_is_nan(inc))
1317 return scop_from_affine_for(tree, init_val, pa_inc, inc,
1318 pc, state);
1320 isl_pw_aff_free(pa_inc);
1321 isl_pw_aff_free(init_val);
1322 isl_val_free(inc);
1323 return scop_from_non_affine_for(tree, pc, state);
1324 error:
1325 isl_pw_aff_free(pa_inc);
1326 isl_pw_aff_free(init_val);
1327 isl_val_free(inc);
1328 pet_context_free(pc);
1329 return NULL;
1332 /* Check whether "expr" is an affine constraint within the context "pc".
1334 static int is_affine_condition(__isl_keep pet_expr *expr,
1335 __isl_keep pet_context *pc)
1337 isl_pw_aff *pa;
1338 int is_affine;
1340 pa = pet_expr_extract_affine_condition(expr, pc);
1341 if (!pa)
1342 return -1;
1343 is_affine = !isl_pw_aff_involves_nan(pa);
1344 isl_pw_aff_free(pa);
1346 return is_affine;
1349 /* Check if the given if statement is a conditional assignement
1350 * with a non-affine condition.
1352 * In particular we check if "stmt" is of the form
1354 * if (condition)
1355 * a = f(...);
1356 * else
1357 * a = g(...);
1359 * where the condition is non-affine and a is some array or scalar access.
1361 static int is_conditional_assignment(__isl_keep pet_tree *tree,
1362 __isl_keep pet_context *pc)
1364 int equal;
1365 isl_ctx *ctx;
1366 pet_expr *expr1, *expr2;
1368 ctx = pet_tree_get_ctx(tree);
1369 if (!pet_options_get_detect_conditional_assignment(ctx))
1370 return 0;
1371 if (tree->type != pet_tree_if_else)
1372 return 0;
1373 if (tree->u.i.then_body->type != pet_tree_expr)
1374 return 0;
1375 if (tree->u.i.else_body->type != pet_tree_expr)
1376 return 0;
1377 expr1 = tree->u.i.then_body->u.e.expr;
1378 expr2 = tree->u.i.else_body->u.e.expr;
1379 if (pet_expr_get_type(expr1) != pet_expr_op)
1380 return 0;
1381 if (pet_expr_get_type(expr2) != pet_expr_op)
1382 return 0;
1383 if (pet_expr_op_get_type(expr1) != pet_op_assign)
1384 return 0;
1385 if (pet_expr_op_get_type(expr2) != pet_op_assign)
1386 return 0;
1387 expr1 = pet_expr_get_arg(expr1, 0);
1388 expr2 = pet_expr_get_arg(expr2, 0);
1389 equal = pet_expr_is_equal(expr1, expr2);
1390 pet_expr_free(expr1);
1391 pet_expr_free(expr2);
1392 if (equal < 0 || !equal)
1393 return 0;
1394 if (is_affine_condition(tree->u.i.cond, pc))
1395 return 0;
1397 return 1;
1400 /* Given that "tree" is of the form
1402 * if (condition)
1403 * a = f(...);
1404 * else
1405 * a = g(...);
1407 * where a is some array or scalar access, construct a pet_scop
1408 * corresponding to this conditional assignment within the context "pc".
1410 * The constructed pet_scop then corresponds to the expression
1412 * a = condition ? f(...) : g(...)
1414 * All access relations in f(...) are intersected with condition
1415 * while all access relation in g(...) are intersected with the complement.
1417 static struct pet_scop *scop_from_conditional_assignment(
1418 __isl_keep pet_tree *tree, __isl_take pet_context *pc,
1419 struct pet_state *state)
1421 int type_size;
1422 isl_pw_aff *pa;
1423 isl_set *cond, *comp;
1424 isl_multi_pw_aff *index;
1425 pet_expr *expr1, *expr2;
1426 pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1427 pet_context *pc_nested;
1428 struct pet_scop *scop;
1430 pe_cond = pet_expr_copy(tree->u.i.cond);
1431 pe_cond = pet_expr_plug_in_args(pe_cond, pc);
1432 pc_nested = pet_context_copy(pc);
1433 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1434 pa = pet_expr_extract_affine_condition(pe_cond, pc_nested);
1435 pet_context_free(pc_nested);
1436 pet_expr_free(pe_cond);
1437 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
1438 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
1439 index = isl_multi_pw_aff_from_pw_aff(pa);
1441 expr1 = tree->u.i.then_body->u.e.expr;
1442 expr2 = tree->u.i.else_body->u.e.expr;
1444 pe_cond = pet_expr_from_index(index);
1446 pe_then = pet_expr_get_arg(expr1, 1);
1447 pe_then = pet_expr_restrict(pe_then, cond);
1448 pe_else = pet_expr_get_arg(expr2, 1);
1449 pe_else = pet_expr_restrict(pe_else, comp);
1450 pe_write = pet_expr_get_arg(expr1, 0);
1452 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
1453 type_size = pet_expr_get_type_size(pe_write);
1454 pe = pet_expr_new_binary(type_size, pet_op_assign, pe_write, pe);
1456 scop = scop_from_expr(pe, NULL, state->n_stmt++,
1457 pet_tree_get_loc(tree), pc);
1459 pet_context_free(pc);
1461 return scop;
1464 /* Construct a pet_scop for a non-affine if statement within the context "pc".
1466 * We create a separate statement that writes the result
1467 * of the non-affine condition to a virtual scalar.
1468 * A constraint requiring the value of this virtual scalar to be one
1469 * is added to the iteration domains of the then branch.
1470 * Similarly, a constraint requiring the value of this virtual scalar
1471 * to be zero is added to the iteration domains of the else branch, if any.
1472 * We adjust the schedules to ensure that the virtual scalar is written
1473 * before it is read.
1475 * If there are any breaks or continues in the then and/or else
1476 * branches, then we may have to compute a new skip condition.
1477 * This is handled using a pet_skip_info object.
1478 * On initialization, the object checks if skip conditions need
1479 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
1480 * adds them in pet_skip_info_if_add.
1482 static struct pet_scop *scop_from_non_affine_if(__isl_keep pet_tree *tree,
1483 struct pet_scop *scop_then, struct pet_scop *scop_else, int stmt_id,
1484 __isl_take pet_context *pc, struct pet_state *state)
1486 int has_else;
1487 int save_n_stmt = state->n_stmt;
1488 isl_multi_pw_aff *test_index;
1489 struct pet_skip_info skip;
1490 struct pet_scop *scop;
1492 has_else = tree->type == pet_tree_if_else;
1494 test_index = pet_create_test_index(state->ctx, state->n_test++);
1495 state->n_stmt = stmt_id;
1496 scop = scop_from_non_affine_condition(pet_expr_copy(tree->u.i.cond),
1497 state->n_stmt++, isl_multi_pw_aff_copy(test_index),
1498 pet_tree_get_loc(tree), pc);
1499 state->n_stmt = save_n_stmt;
1500 scop = pet_scop_add_boolean_array(scop,
1501 isl_multi_pw_aff_copy(test_index), state->int_size);
1503 pet_skip_info_if_init(&skip, state->ctx, scop_then, scop_else,
1504 has_else, 0);
1505 pet_skip_info_if_extract_index(&skip, test_index, state);
1507 scop = pet_scop_prefix(scop, 0);
1508 scop_then = pet_scop_prefix(scop_then, 1);
1509 scop_then = pet_scop_filter(scop_then,
1510 isl_multi_pw_aff_copy(test_index), 1);
1511 if (has_else) {
1512 scop_else = pet_scop_prefix(scop_else, 1);
1513 scop_else = pet_scop_filter(scop_else, test_index, 0);
1514 scop_then = pet_scop_add_par(state->ctx, scop_then, scop_else);
1515 } else
1516 isl_multi_pw_aff_free(test_index);
1518 scop = pet_scop_add_seq(state->ctx, scop, scop_then);
1520 scop = pet_skip_info_if_add(&skip, scop, 2);
1522 pet_context_free(pc);
1523 return scop;
1526 /* Construct a pet_scop for an affine if statement within the context "pc".
1528 * The condition is added to the iteration domains of the then branch,
1529 * while the opposite of the condition in added to the iteration domains
1530 * of the else branch, if any.
1532 * If there are any breaks or continues in the then and/or else
1533 * branches, then we may have to compute a new skip condition.
1534 * This is handled using a pet_skip_info_if object.
1535 * On initialization, the object checks if skip conditions need
1536 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
1537 * adds them in pet_skip_info_if_add.
1539 static struct pet_scop *scop_from_affine_if(__isl_keep pet_tree *tree,
1540 __isl_take isl_pw_aff *cond,
1541 struct pet_scop *scop_then, struct pet_scop *scop_else,
1542 struct pet_state *state)
1544 int has_else;
1545 isl_ctx *ctx;
1546 isl_set *set;
1547 isl_set *valid;
1548 struct pet_skip_info skip;
1549 struct pet_scop *scop;
1551 ctx = pet_tree_get_ctx(tree);
1553 has_else = tree->type == pet_tree_if_else;
1555 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, has_else, 1);
1556 pet_skip_info_if_extract_cond(&skip, cond, state);
1558 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1559 set = isl_pw_aff_non_zero_set(cond);
1560 scop = pet_scop_restrict(scop_then, isl_set_params(isl_set_copy(set)));
1562 if (has_else) {
1563 set = isl_set_subtract(isl_set_copy(valid), set);
1564 scop_else = pet_scop_restrict(scop_else, isl_set_params(set));
1565 scop = pet_scop_add_par(ctx, scop, scop_else);
1566 } else
1567 isl_set_free(set);
1568 scop = pet_scop_resolve_nested(scop);
1569 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
1571 if (pet_skip_info_has_skip(&skip))
1572 scop = pet_scop_prefix(scop, 0);
1573 scop = pet_skip_info_if_add(&skip, scop, 1);
1575 return scop;
1578 /* Construct a pet_scop for an if statement within the context "pc".
1580 * If the condition fits the pattern of a conditional assignment,
1581 * then it is handled by scop_from_conditional_assignment.
1583 * Otherwise, we check if the condition is affine.
1584 * If so, we construct the scop in scop_from_affine_if.
1585 * Otherwise, we construct the scop in scop_from_non_affine_if.
1587 * We allow the condition to be dynamic, i.e., to refer to
1588 * scalars or array elements that may be written to outside
1589 * of the given if statement. These nested accesses are then represented
1590 * as output dimensions in the wrapping iteration domain.
1591 * If it is also written _inside_ the then or else branch, then
1592 * we treat the condition as non-affine.
1593 * As explained in extract_non_affine_if, this will introduce
1594 * an extra statement.
1595 * For aesthetic reasons, we want this statement to have a statement
1596 * number that is lower than those of the then and else branches.
1597 * In order to evaluate if we will need such a statement, however, we
1598 * first construct scops for the then and else branches.
1599 * We therefore reserve a statement number if we might have to
1600 * introduce such an extra statement.
1602 static struct pet_scop *scop_from_if(__isl_keep pet_tree *tree,
1603 __isl_keep pet_context *pc, struct pet_state *state)
1605 int has_else;
1606 int stmt_id;
1607 isl_pw_aff *cond;
1608 pet_expr *cond_expr;
1609 struct pet_scop *scop_then, *scop_else = NULL;
1610 pet_context *pc_nested;
1612 if (!tree)
1613 return NULL;
1615 has_else = tree->type == pet_tree_if_else;
1617 pc = pet_context_copy(pc);
1618 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.then_body);
1619 if (has_else)
1620 pc = pet_context_clear_writes_in_tree(pc, tree->u.i.else_body);
1622 if (is_conditional_assignment(tree, pc))
1623 return scop_from_conditional_assignment(tree, pc, state);
1625 cond_expr = pet_expr_copy(tree->u.i.cond);
1626 cond_expr = pet_expr_plug_in_args(cond_expr, pc);
1627 pc_nested = pet_context_copy(pc);
1628 pc_nested = pet_context_set_allow_nested(pc_nested, 1);
1629 cond = pet_expr_extract_affine_condition(cond_expr, pc_nested);
1630 pet_context_free(pc_nested);
1631 pet_expr_free(cond_expr);
1633 if (!cond) {
1634 pet_context_free(pc);
1635 return NULL;
1638 if (isl_pw_aff_involves_nan(cond) || pet_nested_any_in_pw_aff(cond))
1639 stmt_id = state->n_stmt++;
1641 scop_then = scop_from_tree(tree->u.i.then_body, pc, state);
1642 if (has_else)
1643 scop_else = scop_from_tree(tree->u.i.else_body, pc, state);
1645 if (isl_pw_aff_involves_nan(cond)) {
1646 isl_pw_aff_free(cond);
1647 return scop_from_non_affine_if(tree, scop_then, scop_else,
1648 stmt_id, pc, state);
1651 if ((!is_nested_allowed(cond, tree->u.i.then_body) ||
1652 (has_else && !is_nested_allowed(cond, tree->u.i.else_body)))) {
1653 isl_pw_aff_free(cond);
1654 return scop_from_non_affine_if(tree, scop_then, scop_else,
1655 stmt_id, pc, state);
1658 pet_context_free(pc);
1659 return scop_from_affine_if(tree, cond, scop_then, scop_else, state);
1662 /* Return a one-dimensional multi piecewise affine expression that is equal
1663 * to the constant 1 and is defined over a zero-dimensional domain.
1665 static __isl_give isl_multi_pw_aff *one_mpa(isl_ctx *ctx)
1667 isl_space *space;
1668 isl_local_space *ls;
1669 isl_aff *aff;
1671 space = isl_space_set_alloc(ctx, 0, 0);
1672 ls = isl_local_space_from_space(space);
1673 aff = isl_aff_zero_on_domain(ls);
1674 aff = isl_aff_set_constant_si(aff, 1);
1676 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1679 /* Construct a pet_scop for a continue statement.
1681 * We simply create an empty scop with a universal pet_skip_now
1682 * skip condition. This skip condition will then be taken into
1683 * account by the enclosing loop construct, possibly after
1684 * being incorporated into outer skip conditions.
1686 static struct pet_scop *scop_from_continue(__isl_keep pet_tree *tree)
1688 struct pet_scop *scop;
1689 isl_ctx *ctx;
1691 ctx = pet_tree_get_ctx(tree);
1692 scop = pet_scop_empty(ctx);
1693 if (!scop)
1694 return NULL;
1696 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(ctx));
1698 return scop;
1701 /* Construct a pet_scop for a break statement.
1703 * We simply create an empty scop with both a universal pet_skip_now
1704 * skip condition and a universal pet_skip_later skip condition.
1705 * These skip conditions will then be taken into
1706 * account by the enclosing loop construct, possibly after
1707 * being incorporated into outer skip conditions.
1709 static struct pet_scop *scop_from_break(__isl_keep pet_tree *tree)
1711 struct pet_scop *scop;
1712 isl_ctx *ctx;
1713 isl_multi_pw_aff *skip;
1715 ctx = pet_tree_get_ctx(tree);
1716 scop = pet_scop_empty(ctx);
1717 if (!scop)
1718 return NULL;
1720 skip = one_mpa(ctx);
1721 scop = pet_scop_set_skip(scop, pet_skip_now,
1722 isl_multi_pw_aff_copy(skip));
1723 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
1725 return scop;
1728 /* Extract a clone of the kill statement in "scop".
1729 * "scop" is expected to have been created from a DeclStmt
1730 * and should have the kill as its first statement.
1732 static struct pet_scop *extract_kill(isl_ctx *ctx, struct pet_scop *scop,
1733 struct pet_state *state)
1735 pet_expr *kill;
1736 struct pet_stmt *stmt;
1737 isl_multi_pw_aff *index;
1738 isl_map *access;
1739 pet_expr *arg;
1741 if (!scop)
1742 return NULL;
1743 if (scop->n_stmt < 1)
1744 isl_die(ctx, isl_error_internal,
1745 "expecting at least one statement", return NULL);
1746 stmt = scop->stmts[0];
1747 if (!pet_stmt_is_kill(stmt))
1748 isl_die(ctx, isl_error_internal,
1749 "expecting kill statement", return NULL);
1751 arg = pet_expr_get_arg(stmt->body, 0);
1752 index = pet_expr_access_get_index(arg);
1753 access = pet_expr_access_get_access(arg);
1754 pet_expr_free(arg);
1755 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
1756 access = isl_map_reset_tuple_id(access, isl_dim_in);
1757 kill = pet_expr_kill_from_access_and_index(access, index);
1758 stmt = pet_stmt_from_pet_expr(pet_loc_copy(stmt->loc),
1759 NULL, state->n_stmt++, kill);
1760 return pet_scop_from_pet_stmt(ctx, stmt);
1763 /* Mark all arrays in "scop" as being exposed.
1765 static struct pet_scop *mark_exposed(struct pet_scop *scop)
1767 int i;
1769 if (!scop)
1770 return NULL;
1771 for (i = 0; i < scop->n_array; ++i)
1772 scop->arrays[i]->exposed = 1;
1773 return scop;
1776 /* Try and construct a pet_scop corresponding to (part of)
1777 * a sequence of statements within the context "pc".
1779 * After extracting a statement, we update "pc"
1780 * based on the top-level assignments in the statement
1781 * so that we can exploit them in subsequent statements in the same block.
1783 * If there are any breaks or continues in the individual statements,
1784 * then we may have to compute a new skip condition.
1785 * This is handled using a pet_skip_info object.
1786 * On initialization, the object checks if skip conditions need
1787 * to be computed. If so, it does so in pet_skip_info_seq_extract and
1788 * adds them in pet_skip_info_seq_add.
1790 * If "block" is set, then we need to insert kill statements at
1791 * the end of the block for any array that has been declared by
1792 * one of the statements in the sequence. Each of these declarations
1793 * results in the construction of a kill statement at the place
1794 * of the declaration, so we simply collect duplicates of
1795 * those kill statements and append these duplicates to the constructed scop.
1797 * If "block" is not set, then any array declared by one of the statements
1798 * in the sequence is marked as being exposed.
1800 * If autodetect is set, then we allow the extraction of only a subrange
1801 * of the sequence of statements. However, if there is at least one statement
1802 * for which we could not construct a scop and the final range contains
1803 * either no statements or at least one kill, then we discard the entire
1804 * range.
1806 static struct pet_scop *scop_from_block(__isl_keep pet_tree *tree,
1807 __isl_keep pet_context *pc, struct pet_state *state)
1809 int i;
1810 isl_ctx *ctx;
1811 struct pet_scop *scop, *kills;
1813 ctx = pet_tree_get_ctx(tree);
1815 pc = pet_context_copy(pc);
1816 scop = pet_scop_empty(ctx);
1817 kills = pet_scop_empty(ctx);
1818 for (i = 0; i < tree->u.b.n; ++i) {
1819 struct pet_scop *scop_i;
1821 scop_i = scop_from_tree(tree->u.b.child[i], pc, state);
1822 pc = scop_handle_writes(scop_i, pc);
1823 struct pet_skip_info skip;
1824 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
1825 pet_skip_info_seq_extract(&skip, state);
1826 if (pet_skip_info_has_skip(&skip))
1827 scop_i = pet_scop_prefix(scop_i, 0);
1828 if (scop_i && pet_tree_is_decl(tree->u.b.child[i])) {
1829 if (tree->u.b.block) {
1830 struct pet_scop *kill;
1831 kill = extract_kill(ctx, scop_i, state);
1832 kills = pet_scop_add_par(ctx, kills, kill);
1833 } else
1834 scop_i = mark_exposed(scop_i);
1836 scop_i = pet_scop_prefix(scop_i, i);
1837 scop = pet_scop_add_seq(ctx, scop, scop_i);
1839 scop = pet_skip_info_seq_add(&skip, scop, i);
1841 if (!scop)
1842 break;
1845 kills = pet_scop_prefix(kills, tree->u.b.n);
1846 scop = pet_scop_add_seq(ctx, scop, kills);
1848 pet_context_free(pc);
1850 return scop;
1853 /* Construct a pet_scop that corresponds to the pet_tree "tree"
1854 * within the context "pc" by calling the appropriate function
1855 * based on the type of "tree".
1857 static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
1858 __isl_keep pet_context *pc, struct pet_state *state)
1860 if (!tree)
1861 return NULL;
1863 switch (tree->type) {
1864 case pet_tree_error:
1865 return NULL;
1866 case pet_tree_block:
1867 return scop_from_block(tree, pc, state);
1868 case pet_tree_break:
1869 return scop_from_break(tree);
1870 case pet_tree_continue:
1871 return scop_from_continue(tree);
1872 case pet_tree_decl:
1873 case pet_tree_decl_init:
1874 return scop_from_decl(tree, pc, state);
1875 case pet_tree_expr:
1876 return scop_from_expr(pet_expr_copy(tree->u.e.expr),
1877 isl_id_copy(tree->label),
1878 state->n_stmt++,
1879 pet_tree_get_loc(tree), pc);
1880 case pet_tree_if:
1881 case pet_tree_if_else:
1882 return scop_from_if(tree, pc, state);
1883 case pet_tree_for:
1884 return scop_from_for(tree, pc, state);
1885 case pet_tree_while:
1886 return scop_from_while(tree, pc, state);
1887 case pet_tree_infinite_loop:
1888 return scop_from_infinite_for(tree, pc, state);
1891 isl_die(tree->ctx, isl_error_internal, "unhandled type",
1892 return NULL);
1895 /* Construct a pet_scop that corresponds to the pet_tree "tree".
1896 * "int_size" is the number of bytes need to represent an integer.
1897 * "extract_array" is a callback that we can use to create a pet_array
1898 * that corresponds to the variable accessed by an expression.
1900 * Initialize the global state, construct a context and then
1901 * construct the pet_scop by recursively visiting the tree.
1903 struct pet_scop *pet_scop_from_pet_tree(__isl_take pet_tree *tree, int int_size,
1904 struct pet_array *(*extract_array)(__isl_keep pet_expr *access,
1905 __isl_keep pet_context *pc, void *user), void *user,
1906 __isl_keep pet_context *pc)
1908 struct pet_scop *scop;
1909 struct pet_state state = { 0 };
1911 if (!tree)
1912 return NULL;
1914 state.ctx = pet_tree_get_ctx(tree);
1915 state.int_size = int_size;
1916 state.extract_array = extract_array;
1917 state.user = user;
1919 scop = scop_from_tree(tree, pc, &state);
1920 scop = pet_scop_set_loc(scop, pet_tree_get_loc(tree));
1922 pet_tree_free(tree);
1924 return scop;