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
))
18 if (scop_then
&& pet_scop_has_skip(scop_then
, type
) &&
19 !pet_scop_has_universal_skip(scop_then
, type
))
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
))
35 if (have_else
&& scop_else
&& pet_scop_has_var_skip(scop_else
, type
))
40 /* Do we need to construct a skip condition of the given type
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
)
47 return if_need_skip_aff(scop_then
, scop_else
, have_else
, type
);
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
)
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
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
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
, struct pet_state
*state
)
105 pet_expr
*expr_then
, *expr_else
, *expr
, *expr_skip
;
106 struct pet_stmt
*stmt
;
107 struct pet_scop
*scop
;
112 if (have_else
&& !scop_else
)
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);
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);
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
,
142 state
->n_stmt
++, expr
);
144 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
145 scop
= pet_scop_add_boolean_array(scop
, skip_index
, state
->int_size
);
149 isl_multi_pw_aff_free(test_index
);
150 isl_multi_pw_aff_free(skip_index
);
154 /* Is scop's skip_now condition equal to its skip_later condition?
155 * In particular, this means that it either has no skip_now condition
156 * or both a skip_now and a skip_later condition (that are equal to each other).
158 static int skip_equals_skip_later(struct pet_scop
*scop
)
160 int has_skip_now
, has_skip_later
;
162 isl_multi_pw_aff
*skip_now
, *skip_later
;
166 has_skip_now
= pet_scop_has_skip(scop
, pet_skip_now
);
167 has_skip_later
= pet_scop_has_skip(scop
, pet_skip_later
);
168 if (has_skip_now
!= has_skip_later
)
173 skip_now
= pet_scop_get_skip(scop
, pet_skip_now
);
174 skip_later
= pet_scop_get_skip(scop
, pet_skip_later
);
175 equal
= isl_multi_pw_aff_is_equal(skip_now
, skip_later
);
176 isl_multi_pw_aff_free(skip_now
);
177 isl_multi_pw_aff_free(skip_later
);
182 /* Drop the skip conditions of type pet_skip_later from scop1 and scop2.
184 static void drop_skip_later(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
186 pet_scop_reset_skip(scop1
, pet_skip_later
);
187 pet_scop_reset_skip(scop2
, pet_skip_later
);
190 /* Do we need to construct any skip condition?
192 int pet_skip_info_has_skip(struct pet_skip_info
*skip
)
194 return skip
->skip
[pet_skip_now
] || skip
->skip
[pet_skip_later
];
197 /* Initialize a pet_skip_info_if structure based on the then and else branches
198 * and based on whether the if condition is affine or not.
200 void pet_skip_info_if_init(struct pet_skip_info
*skip
, isl_ctx
*ctx
,
201 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
202 int have_else
, int affine
)
205 skip
->type
= have_else
? pet_skip_if_else
: pet_skip_if
;
206 skip
->u
.i
.scop_then
= scop_then
;
207 skip
->u
.i
.scop_else
= scop_else
;
209 skip
->skip
[pet_skip_now
] =
210 if_need_skip(scop_then
, scop_else
, have_else
, pet_skip_now
, affine
);
211 skip
->equal
= skip
->skip
[pet_skip_now
] &&
212 skip_equals_skip_later(scop_then
) &&
213 (!have_else
|| skip_equals_skip_later(scop_else
));
214 skip
->skip
[pet_skip_later
] = skip
->skip
[pet_skip_now
] &&
216 if_need_skip(scop_then
, scop_else
, have_else
,
217 pet_skip_later
, affine
);
220 /* If we need to construct a skip condition of the given type,
223 * "mpa" represents the if condition.
225 static void pet_skip_info_if_extract_type(struct pet_skip_info
*skip
,
226 __isl_keep isl_multi_pw_aff
*mpa
, enum pet_skip type
,
227 struct pet_state
*state
)
229 if (!skip
->skip
[type
])
232 skip
->index
[type
] = pet_create_test_index(skip
->ctx
, state
->n_test
++);
233 skip
->scop
[type
] = extract_skip_if(isl_multi_pw_aff_copy(mpa
),
234 isl_multi_pw_aff_copy(skip
->index
[type
]),
235 skip
->u
.i
.scop_then
, skip
->u
.i
.scop_else
,
236 skip
->type
== pet_skip_if_else
, type
, state
);
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
, struct pet_state
*state
)
244 pet_skip_info_if_extract_type(skip
, index
, pet_skip_now
, state
);
245 pet_skip_info_if_extract_type(skip
, index
, pet_skip_later
, state
);
247 drop_skip_later(skip
->u
.i
.scop_then
, skip
->u
.i
.scop_else
);
250 /* Construct the required skip conditions, given the if condition "cond".
252 void pet_skip_info_if_extract_cond(struct pet_skip_info
*skip
,
253 __isl_keep isl_pw_aff
*cond
, struct pet_state
*state
)
255 isl_multi_pw_aff
*test
;
257 if (!skip
->skip
[pet_skip_now
] && !skip
->skip
[pet_skip_later
])
260 test
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_copy(cond
));
261 pet_skip_info_if_extract_index(skip
, test
, state
);
262 isl_multi_pw_aff_free(test
);
265 /* Add the computed skip condition of the give type to "main" and
266 * add the scop for computing the condition at the given offset.
268 * If equal is set, then we only computed a skip condition for pet_skip_now,
269 * but we also need to set it as main's pet_skip_later.
271 struct pet_scop
*pet_skip_info_if_add_type(struct pet_skip_info
*skip
,
272 struct pet_scop
*main
, enum pet_skip type
, int offset
)
274 if (!skip
->skip
[type
])
277 skip
->scop
[type
] = pet_scop_prefix(skip
->scop
[type
], offset
);
278 main
= pet_scop_add_par(skip
->ctx
, main
, skip
->scop
[type
]);
279 skip
->scop
[type
] = NULL
;
282 main
= pet_scop_set_skip(main
, pet_skip_later
,
283 isl_multi_pw_aff_copy(skip
->index
[type
]));
285 main
= pet_scop_set_skip(main
, type
, skip
->index
[type
]);
286 skip
->index
[type
] = NULL
;
291 /* Add the computed skip conditions to "main" and
292 * add the scops for computing the conditions at the given offset.
294 struct pet_scop
*pet_skip_info_if_add(struct pet_skip_info
*skip
,
295 struct pet_scop
*scop
, int offset
)
297 scop
= pet_skip_info_if_add_type(skip
, scop
, pet_skip_now
, offset
);
298 scop
= pet_skip_info_if_add_type(skip
, scop
, pet_skip_later
, offset
);
303 /* Do we need to construct a skip condition of the given type
304 * on a sequence of statements?
306 * There is no need to construct a new skip condition if only
307 * only of the two statements has a skip condition or if both
308 * of their skip conditions are affine.
310 * In principle we also don't need a new continuation variable if
311 * the continuation of scop2 is affine, but then we would need
312 * to allow more complicated forms of continuations.
314 static int seq_need_skip(struct pet_scop
*scop1
, struct pet_scop
*scop2
,
317 if (!scop1
|| !pet_scop_has_skip(scop1
, type
))
319 if (!scop2
|| !pet_scop_has_skip(scop2
, type
))
321 if (pet_scop_has_affine_skip(scop1
, type
) &&
322 pet_scop_has_affine_skip(scop2
, type
))
327 /* Construct a scop for computing the skip condition of the given type and
328 * with index expression "skip_index" for a sequence of two scops "scop1"
331 * The computed scop contains a single statement that essentially does
333 * skip_index = skip_cond_1 ? 1 : skip_cond_2
335 * or, in other words, skip_cond1 || skip_cond2.
336 * In this expression, skip_cond_2 is filtered to reflect that it is
337 * only evaluated when skip_cond_1 is false.
339 * The skip condition on scop1 is not removed because it still needs
340 * to be applied to scop2 when these two scops are combined.
342 static struct pet_scop
*extract_skip_seq(
343 __isl_take isl_multi_pw_aff
*skip_index
,
344 struct pet_scop
*scop1
, struct pet_scop
*scop2
, enum pet_skip type
,
345 struct pet_state
*state
)
347 pet_expr
*expr1
, *expr2
, *expr
, *expr_skip
;
348 struct pet_stmt
*stmt
;
349 struct pet_scop
*scop
;
352 if (!scop1
|| !scop2
)
355 ctx
= isl_multi_pw_aff_get_ctx(skip_index
);
357 expr1
= pet_scop_get_skip_expr(scop1
, type
);
358 expr2
= pet_scop_get_skip_expr(scop2
, type
);
359 pet_scop_reset_skip(scop2
, type
);
361 expr2
= pet_expr_filter(expr2
, pet_expr_access_get_index(expr1
), 0);
363 expr
= universally_true(ctx
);
364 expr
= pet_expr_new_ternary(expr1
, expr
, expr2
);
365 expr_skip
= pet_expr_from_index(isl_multi_pw_aff_copy(skip_index
));
366 expr_skip
= pet_expr_access_set_write(expr_skip
, 1);
367 expr_skip
= pet_expr_access_set_read(expr_skip
, 0);
368 expr
= pet_expr_new_binary(1, pet_op_assign
, expr_skip
, expr
);
369 stmt
= pet_stmt_from_pet_expr(&pet_loc_dummy
, NULL
,
370 state
->n_stmt
++, expr
);
372 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
373 scop
= pet_scop_add_boolean_array(scop
, skip_index
, state
->int_size
);
377 isl_multi_pw_aff_free(skip_index
);
381 /* Initialize a pet_skip_info_seq structure based on
382 * on the two statements that are going to be combined.
384 void pet_skip_info_seq_init(struct pet_skip_info
*skip
, isl_ctx
*ctx
,
385 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
388 skip
->type
= pet_skip_seq
;
389 skip
->u
.s
.scop1
= scop1
;
390 skip
->u
.s
.scop2
= scop2
;
392 skip
->skip
[pet_skip_now
] = seq_need_skip(scop1
, scop2
, pet_skip_now
);
393 skip
->equal
= skip
->skip
[pet_skip_now
] &&
394 skip_equals_skip_later(scop1
) && skip_equals_skip_later(scop2
);
395 skip
->skip
[pet_skip_later
] = skip
->skip
[pet_skip_now
] && !skip
->equal
&&
396 seq_need_skip(scop1
, scop2
, pet_skip_later
);
399 /* If we need to construct a skip condition of the given type,
402 static void pet_skip_info_seq_extract_type(struct pet_skip_info
*skip
,
403 enum pet_skip type
, struct pet_state
*state
)
405 if (!skip
->skip
[type
])
408 skip
->index
[type
] = pet_create_test_index(skip
->ctx
, state
->n_test
++);
409 skip
->scop
[type
] = extract_skip_seq(
410 isl_multi_pw_aff_copy(skip
->index
[type
]),
411 skip
->u
.s
.scop1
, skip
->u
.s
.scop2
, type
, state
);
414 /* Construct the required skip conditions.
416 void pet_skip_info_seq_extract(struct pet_skip_info
*skip
,
417 struct pet_state
*state
)
419 pet_skip_info_seq_extract_type(skip
, pet_skip_now
, state
);
420 pet_skip_info_seq_extract_type(skip
, pet_skip_later
, state
);
422 drop_skip_later(skip
->u
.s
.scop1
, skip
->u
.s
.scop2
);
425 /* Add the computed skip condition of the given type to "main" and
426 * add the scop for computing the condition at the given offset (the statement
427 * number). Within this offset, the condition is computed at position 1
428 * to ensure that it is computed after the corresponding statement.
430 * If equal is set, then we only computed a skip condition for pet_skip_now,
431 * but we also need to set it as main's pet_skip_later.
433 struct pet_scop
*pet_skip_info_seq_add_type(struct pet_skip_info
*skip
,
434 struct pet_scop
*main
, enum pet_skip type
, int offset
)
436 if (!skip
->skip
[type
])
439 skip
->scop
[type
] = pet_scop_prefix(skip
->scop
[type
], 1);
440 skip
->scop
[type
] = pet_scop_prefix(skip
->scop
[type
], offset
);
441 main
= pet_scop_add_par(skip
->ctx
, main
, skip
->scop
[type
]);
442 skip
->scop
[type
] = NULL
;
445 main
= pet_scop_set_skip(main
, pet_skip_later
,
446 isl_multi_pw_aff_copy(skip
->index
[type
]));
448 main
= pet_scop_set_skip(main
, type
, skip
->index
[type
]);
449 skip
->index
[type
] = NULL
;
454 /* Add the computed skip conditions to "main" and
455 * add the scops for computing the conditions at the given offset.
457 struct pet_scop
*pet_skip_info_seq_add(struct pet_skip_info
*skip
,
458 struct pet_scop
*scop
, int offset
)
460 scop
= pet_skip_info_seq_add_type(skip
, scop
, pet_skip_now
, offset
);
461 scop
= pet_skip_info_seq_add_type(skip
, scop
, pet_skip_later
, offset
);