PetScan: drop allow_nested attribute
[pet.git] / skip.c
blob1fc0469ae20ec1341e57a2d5b27e5c3490292465
1 #include "expr.h"
2 #include "loc.h"
3 #include "scop.h"
4 #include "skip.h"
6 /* Do we need to construct a skip condition of the given type
7 * on an if statement, given that the if condition is non-affine?
9 * pet_scop_filter_skip can only handle the case where the if condition
10 * holds (the then branch) and the skip condition is universal.
11 * In any other case, we need to construct a new skip condition.
13 static int if_need_skip_non(struct pet_scop *scop_then,
14 struct pet_scop *scop_else, int have_else, enum pet_skip type)
16 if (have_else && scop_else && pet_scop_has_skip(scop_else, type))
17 return 1;
18 if (scop_then && pet_scop_has_skip(scop_then, type) &&
19 !pet_scop_has_universal_skip(scop_then, type))
20 return 1;
21 return 0;
24 /* Do we need to construct a skip condition of the given type
25 * on an if statement, given that the if condition is affine?
27 * There is no need to construct a new skip condition if all
28 * the skip conditions are affine.
30 static int if_need_skip_aff(struct pet_scop *scop_then,
31 struct pet_scop *scop_else, int have_else, enum pet_skip type)
33 if (scop_then && pet_scop_has_var_skip(scop_then, type))
34 return 1;
35 if (have_else && scop_else && pet_scop_has_var_skip(scop_else, type))
36 return 1;
37 return 0;
40 /* Do we need to construct a skip condition of the given type
41 * on an if statement?
43 static int if_need_skip(struct pet_scop *scop_then, struct pet_scop *scop_else,
44 int have_else, enum pet_skip type, int affine)
46 if (affine)
47 return if_need_skip_aff(scop_then, scop_else, have_else, type);
48 else
49 return if_need_skip_non(scop_then, scop_else, have_else, type);
52 /* Construct an affine expression pet_expr that evaluates
53 * to the constant "val".
55 static __isl_give pet_expr *universally(isl_ctx *ctx, int val)
57 isl_local_space *ls;
58 isl_aff *aff;
59 isl_multi_pw_aff *mpa;
61 ls = isl_local_space_from_space(isl_space_set_alloc(ctx, 0, 0));
62 aff = isl_aff_val_on_domain(ls, isl_val_int_from_si(ctx, val));
63 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
65 return pet_expr_from_index(mpa);
68 /* Construct an affine expression pet_expr that evaluates
69 * to the constant 1.
71 static __isl_give pet_expr *universally_true(isl_ctx *ctx)
73 return universally(ctx, 1);
76 /* Construct an affine expression pet_expr that evaluates
77 * to the constant 0.
79 static __isl_give pet_expr *universally_false(isl_ctx *ctx)
81 return universally(ctx, 0);
84 /* Given an index expression "test_index" for the if condition,
85 * an index expression "skip_index" for the skip condition and
86 * scops for the then and else branches, construct a scop for
87 * computing "skip_index".
89 * The computed scop contains a single statement that essentially does
91 * skip_index = test_cond ? skip_cond_then : skip_cond_else
93 * If the skip conditions of the then and/or else branch are not affine,
94 * then they need to be filtered by test_index.
95 * If they are missing, then this means the skip condition is false.
97 * Since we are constructing a skip condition for the if statement,
98 * the skip conditions on the then and else branches are removed.
100 static struct pet_scop *extract_skip_if(__isl_take isl_multi_pw_aff *test_index,
101 __isl_take isl_multi_pw_aff *skip_index,
102 struct pet_scop *scop_then, struct pet_scop *scop_else, int have_else,
103 enum pet_skip type, int int_size, int *n_stmt)
105 pet_expr *expr_then, *expr_else, *expr, *expr_skip;
106 struct pet_stmt *stmt;
107 struct pet_scop *scop;
108 isl_ctx *ctx;
110 if (!scop_then)
111 goto error;
112 if (have_else && !scop_else)
113 goto error;
115 ctx = isl_multi_pw_aff_get_ctx(test_index);
117 if (pet_scop_has_skip(scop_then, type)) {
118 expr_then = pet_scop_get_skip_expr(scop_then, type);
119 pet_scop_reset_skip(scop_then, type);
120 if (!pet_expr_is_affine(expr_then))
121 expr_then = pet_expr_filter(expr_then,
122 isl_multi_pw_aff_copy(test_index), 1);
123 } else
124 expr_then = universally_false(ctx);
126 if (have_else && pet_scop_has_skip(scop_else, type)) {
127 expr_else = pet_scop_get_skip_expr(scop_else, type);
128 pet_scop_reset_skip(scop_else, type);
129 if (!pet_expr_is_affine(expr_else))
130 expr_else = pet_expr_filter(expr_else,
131 isl_multi_pw_aff_copy(test_index), 0);
132 } else
133 expr_else = universally_false(ctx);
135 expr = pet_expr_from_index(test_index);
136 expr = pet_expr_new_ternary(expr, expr_then, expr_else);
137 expr_skip = pet_expr_from_index(isl_multi_pw_aff_copy(skip_index));
138 expr_skip = pet_expr_access_set_write(expr_skip, 1);
139 expr_skip = pet_expr_access_set_read(expr_skip, 0);
140 expr = pet_expr_new_binary(1, pet_op_assign, expr_skip, expr);
141 stmt = pet_stmt_from_pet_expr(&pet_loc_dummy, NULL, (*n_stmt)++, expr);
143 scop = pet_scop_from_pet_stmt(ctx, stmt);
144 scop = pet_scop_add_boolean_array(scop, skip_index, int_size);
146 return scop;
147 error:
148 isl_multi_pw_aff_free(test_index);
149 isl_multi_pw_aff_free(skip_index);
150 return NULL;
153 /* Is scop's skip_now condition equal to its skip_later condition?
154 * In particular, this means that it either has no skip_now condition
155 * or both a skip_now and a skip_later condition (that are equal to each other).
157 static int skip_equals_skip_later(struct pet_scop *scop)
159 int has_skip_now, has_skip_later;
160 int equal;
161 isl_multi_pw_aff *skip_now, *skip_later;
163 if (!scop)
164 return 0;
165 has_skip_now = pet_scop_has_skip(scop, pet_skip_now);
166 has_skip_later = pet_scop_has_skip(scop, pet_skip_later);
167 if (has_skip_now != has_skip_later)
168 return 0;
169 if (!has_skip_now)
170 return 1;
172 skip_now = pet_scop_get_skip(scop, pet_skip_now);
173 skip_later = pet_scop_get_skip(scop, pet_skip_later);
174 equal = isl_multi_pw_aff_is_equal(skip_now, skip_later);
175 isl_multi_pw_aff_free(skip_now);
176 isl_multi_pw_aff_free(skip_later);
178 return equal;
181 /* Drop the skip conditions of type pet_skip_later from scop1 and scop2.
183 static void drop_skip_later(struct pet_scop *scop1, struct pet_scop *scop2)
185 pet_scop_reset_skip(scop1, pet_skip_later);
186 pet_scop_reset_skip(scop2, pet_skip_later);
189 /* Do we need to construct any skip condition?
191 int pet_skip_info_has_skip(struct pet_skip_info *skip)
193 return skip->skip[pet_skip_now] || skip->skip[pet_skip_later];
196 /* Initialize a pet_skip_info_if structure based on the then and else branches
197 * and based on whether the if condition is affine or not.
199 void pet_skip_info_if_init(struct pet_skip_info *skip, isl_ctx *ctx,
200 struct pet_scop *scop_then, struct pet_scop *scop_else,
201 int have_else, int affine)
203 skip->ctx = ctx;
204 skip->type = have_else ? pet_skip_if_else : pet_skip_if;
205 skip->u.i.scop_then = scop_then;
206 skip->u.i.scop_else = scop_else;
208 skip->skip[pet_skip_now] =
209 if_need_skip(scop_then, scop_else, have_else, pet_skip_now, affine);
210 skip->equal = skip->skip[pet_skip_now] &&
211 skip_equals_skip_later(scop_then) &&
212 (!have_else || skip_equals_skip_later(scop_else));
213 skip->skip[pet_skip_later] = skip->skip[pet_skip_now] &&
214 !skip->equal &&
215 if_need_skip(scop_then, scop_else, have_else,
216 pet_skip_later, affine);
219 /* If we need to construct a skip condition of the given type,
220 * then do so now.
222 * "mpa" represents the if condition.
224 static void pet_skip_info_if_extract_type(struct pet_skip_info *skip,
225 __isl_keep isl_multi_pw_aff *mpa, enum pet_skip type,
226 int int_size, int *n_stmt, int *n_test)
228 if (!skip->skip[type])
229 return;
231 skip->index[type] = pet_create_test_index(skip->ctx, (*n_test)++);
232 skip->scop[type] = extract_skip_if(isl_multi_pw_aff_copy(mpa),
233 isl_multi_pw_aff_copy(skip->index[type]),
234 skip->u.i.scop_then, skip->u.i.scop_else,
235 skip->type == pet_skip_if_else, type,
236 int_size, n_stmt);
239 /* Construct the required skip conditions, given the if condition "index".
241 void pet_skip_info_if_extract_index(struct pet_skip_info *skip,
242 __isl_keep isl_multi_pw_aff *index,
243 int int_size, int *n_stmt, int *n_test)
245 pet_skip_info_if_extract_type(skip, index, pet_skip_now,
246 int_size, n_stmt, n_test);
247 pet_skip_info_if_extract_type(skip, index, pet_skip_later,
248 int_size, n_stmt, n_test);
249 if (skip->equal)
250 drop_skip_later(skip->u.i.scop_then, skip->u.i.scop_else);
253 /* Construct the required skip conditions, given the if condition "cond".
255 void pet_skip_info_if_extract_cond(struct pet_skip_info *skip,
256 __isl_keep isl_pw_aff *cond, int int_size, int *n_stmt, int *n_test)
258 isl_multi_pw_aff *test;
260 if (!skip->skip[pet_skip_now] && !skip->skip[pet_skip_later])
261 return;
263 test = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_copy(cond));
264 pet_skip_info_if_extract_index(skip, test, int_size, n_stmt, n_test);
265 isl_multi_pw_aff_free(test);
268 /* Add the computed skip condition of the give type to "main" and
269 * add the scop for computing the condition at the given offset.
271 * If equal is set, then we only computed a skip condition for pet_skip_now,
272 * but we also need to set it as main's pet_skip_later.
274 struct pet_scop *pet_skip_info_if_add_type(struct pet_skip_info *skip,
275 struct pet_scop *main, enum pet_skip type, int offset)
277 if (!skip->skip[type])
278 return main;
280 skip->scop[type] = pet_scop_prefix(skip->scop[type], offset);
281 main = pet_scop_add_par(skip->ctx, main, skip->scop[type]);
282 skip->scop[type] = NULL;
284 if (skip->equal)
285 main = pet_scop_set_skip(main, pet_skip_later,
286 isl_multi_pw_aff_copy(skip->index[type]));
288 main = pet_scop_set_skip(main, type, skip->index[type]);
289 skip->index[type] = NULL;
291 return main;
294 /* Add the computed skip conditions to "main" and
295 * add the scops for computing the conditions at the given offset.
297 struct pet_scop *pet_skip_info_if_add(struct pet_skip_info *skip,
298 struct pet_scop *scop, int offset)
300 scop = pet_skip_info_if_add_type(skip, scop, pet_skip_now, offset);
301 scop = pet_skip_info_if_add_type(skip, scop, pet_skip_later, offset);
303 return scop;
306 /* Do we need to construct a skip condition of the given type
307 * on a sequence of statements?
309 * There is no need to construct a new skip condition if only
310 * only of the two statements has a skip condition or if both
311 * of their skip conditions are affine.
313 * In principle we also don't need a new continuation variable if
314 * the continuation of scop2 is affine, but then we would need
315 * to allow more complicated forms of continuations.
317 static int seq_need_skip(struct pet_scop *scop1, struct pet_scop *scop2,
318 enum pet_skip type)
320 if (!scop1 || !pet_scop_has_skip(scop1, type))
321 return 0;
322 if (!scop2 || !pet_scop_has_skip(scop2, type))
323 return 0;
324 if (pet_scop_has_affine_skip(scop1, type) &&
325 pet_scop_has_affine_skip(scop2, type))
326 return 0;
327 return 1;
330 /* Construct a scop for computing the skip condition of the given type and
331 * with index expression "skip_index" for a sequence of two scops "scop1"
332 * and "scop2".
334 * The computed scop contains a single statement that essentially does
336 * skip_index = skip_cond_1 ? 1 : skip_cond_2
338 * or, in other words, skip_cond1 || skip_cond2.
339 * In this expression, skip_cond_2 is filtered to reflect that it is
340 * only evaluated when skip_cond_1 is false.
342 * The skip condition on scop1 is not removed because it still needs
343 * to be applied to scop2 when these two scops are combined.
345 static struct pet_scop *extract_skip_seq(
346 __isl_take isl_multi_pw_aff *skip_index,
347 struct pet_scop *scop1, struct pet_scop *scop2, enum pet_skip type,
348 int int_size, int *n_stmt)
350 pet_expr *expr1, *expr2, *expr, *expr_skip;
351 struct pet_stmt *stmt;
352 struct pet_scop *scop;
353 isl_ctx *ctx;
355 if (!scop1 || !scop2)
356 goto error;
358 ctx = isl_multi_pw_aff_get_ctx(skip_index);
360 expr1 = pet_scop_get_skip_expr(scop1, type);
361 expr2 = pet_scop_get_skip_expr(scop2, type);
362 pet_scop_reset_skip(scop2, type);
364 expr2 = pet_expr_filter(expr2, pet_expr_access_get_index(expr1), 0);
366 expr = universally_true(ctx);
367 expr = pet_expr_new_ternary(expr1, expr, expr2);
368 expr_skip = pet_expr_from_index(isl_multi_pw_aff_copy(skip_index));
369 expr_skip = pet_expr_access_set_write(expr_skip, 1);
370 expr_skip = pet_expr_access_set_read(expr_skip, 0);
371 expr = pet_expr_new_binary(1, pet_op_assign, expr_skip, expr);
372 stmt = pet_stmt_from_pet_expr(&pet_loc_dummy, NULL, (*n_stmt)++, expr);
374 scop = pet_scop_from_pet_stmt(ctx, stmt);
375 scop = pet_scop_add_boolean_array(scop, skip_index, int_size);
377 return scop;
378 error:
379 isl_multi_pw_aff_free(skip_index);
380 return NULL;
383 /* Initialize a pet_skip_info_seq structure based on
384 * on the two statements that are going to be combined.
386 void pet_skip_info_seq_init(struct pet_skip_info *skip, isl_ctx *ctx,
387 struct pet_scop *scop1, struct pet_scop *scop2)
389 skip->ctx = ctx;
390 skip->type = pet_skip_seq;
391 skip->u.s.scop1 = scop1;
392 skip->u.s.scop2 = scop2;
394 skip->skip[pet_skip_now] = seq_need_skip(scop1, scop2, pet_skip_now);
395 skip->equal = skip->skip[pet_skip_now] &&
396 skip_equals_skip_later(scop1) && skip_equals_skip_later(scop2);
397 skip->skip[pet_skip_later] = skip->skip[pet_skip_now] && !skip->equal &&
398 seq_need_skip(scop1, scop2, pet_skip_later);
401 /* If we need to construct a skip condition of the given type,
402 * then do so now.
404 static void pet_skip_info_seq_extract_type(struct pet_skip_info *skip,
405 enum pet_skip type, int int_size, int *n_stmt, int *n_test)
407 if (!skip->skip[type])
408 return;
410 skip->index[type] = pet_create_test_index(skip->ctx, (*n_test)++);
411 skip->scop[type] = extract_skip_seq(
412 isl_multi_pw_aff_copy(skip->index[type]),
413 skip->u.s.scop1, skip->u.s.scop2, type,
414 int_size, n_stmt);
417 /* Construct the required skip conditions.
419 void pet_skip_info_seq_extract(struct pet_skip_info *skip,
420 int int_size, int *n_stmt, int *n_test)
422 pet_skip_info_seq_extract_type(skip, pet_skip_now,
423 int_size, n_stmt, n_test);
424 pet_skip_info_seq_extract_type(skip, pet_skip_later,
425 int_size, n_stmt, n_test);
426 if (skip->equal)
427 drop_skip_later(skip->u.s.scop1, skip->u.s.scop2);
430 /* Add the computed skip condition of the given type to "main" and
431 * add the scop for computing the condition at the given offset (the statement
432 * number). Within this offset, the condition is computed at position 1
433 * to ensure that it is computed after the corresponding statement.
435 * If equal is set, then we only computed a skip condition for pet_skip_now,
436 * but we also need to set it as main's pet_skip_later.
438 struct pet_scop *pet_skip_info_seq_add_type(struct pet_skip_info *skip,
439 struct pet_scop *main, enum pet_skip type, int offset)
441 if (!skip->skip[type])
442 return main;
444 skip->scop[type] = pet_scop_prefix(skip->scop[type], 1);
445 skip->scop[type] = pet_scop_prefix(skip->scop[type], offset);
446 main = pet_scop_add_par(skip->ctx, main, skip->scop[type]);
447 skip->scop[type] = NULL;
449 if (skip->equal)
450 main = pet_scop_set_skip(main, pet_skip_later,
451 isl_multi_pw_aff_copy(skip->index[type]));
453 main = pet_scop_set_skip(main, type, skip->index[type]);
454 skip->index[type] = NULL;
456 return main;
459 /* Add the computed skip conditions to "main" and
460 * add the scops for computing the conditions at the given offset.
462 struct pet_scop *pet_skip_info_seq_add(struct pet_skip_info *skip,
463 struct pet_scop *scop, int offset)
465 scop = pet_skip_info_seq_add_type(skip, scop, pet_skip_now, offset);
466 scop = pet_skip_info_seq_add_type(skip, scop, pet_skip_later, offset);
468 return scop;