pet_scop_from_pet_tree: extract pet_scop from the root down to the leaves
[pet.git] / include / pet.h
blob5bda348f9d48113e4fc45c87936b988a232f631f
1 #ifndef PET_H
2 #define PET_H
4 #include <isl/aff.h>
5 #include <isl/arg.h>
6 #include <isl/ast_build.h>
7 #include <isl/set.h>
8 #include <isl/map.h>
9 #include <isl/union_map.h>
10 #include <isl/printer.h>
11 #include <isl/id_to_ast_expr.h>
13 #if defined(__cplusplus)
14 extern "C" {
15 #endif
17 struct pet_options;
18 ISL_ARG_DECL(pet_options, struct pet_options, pet_options_args)
20 /* If autodetect is set, any valid scop is extracted.
21 * Otherwise, the scop needs to be delimited by pragmas.
23 int pet_options_set_autodetect(isl_ctx *ctx, int val);
24 int pet_options_get_autodetect(isl_ctx *ctx);
26 int pet_options_set_detect_conditional_assignment(isl_ctx *ctx, int val);
27 int pet_options_get_detect_conditional_assignment(isl_ctx *ctx);
29 #define PET_OVERFLOW_AVOID 0
30 #define PET_OVERFLOW_IGNORE 1
31 int pet_options_set_signed_overflow(isl_ctx *ctx, int val);
32 int pet_options_get_signed_overflow(isl_ctx *ctx);
34 struct pet_loc;
35 typedef struct pet_loc pet_loc;
37 /* Return an additional reference to "loc". */
38 __isl_give pet_loc *pet_loc_copy(__isl_keep pet_loc *loc);
39 /* Free a reference to "loc". */
40 pet_loc *pet_loc_free(__isl_take pet_loc *loc);
42 /* Return the offset in the input file of the start of "loc". */
43 unsigned pet_loc_get_start(__isl_keep pet_loc *loc);
44 /* Return the offset in the input file of the character after "loc". */
45 unsigned pet_loc_get_end(__isl_keep pet_loc *loc);
46 /* Return the line number of a line within the "loc" region. */
47 int pet_loc_get_line(__isl_keep pet_loc *loc);
49 enum pet_expr_type {
50 pet_expr_error = -1,
51 pet_expr_access,
52 pet_expr_call,
53 pet_expr_cast,
54 pet_expr_int,
55 pet_expr_double,
56 pet_expr_op
59 enum pet_op_type {
60 /* only compound assignments operators before assignment */
61 pet_op_add_assign,
62 pet_op_sub_assign,
63 pet_op_mul_assign,
64 pet_op_div_assign,
65 pet_op_assign,
66 pet_op_add,
67 pet_op_sub,
68 pet_op_mul,
69 pet_op_div,
70 pet_op_mod,
71 pet_op_shl,
72 pet_op_shr,
73 pet_op_eq,
74 pet_op_ne,
75 pet_op_le,
76 pet_op_ge,
77 pet_op_lt,
78 pet_op_gt,
79 pet_op_minus,
80 pet_op_post_inc,
81 pet_op_post_dec,
82 pet_op_pre_inc,
83 pet_op_pre_dec,
84 pet_op_address_of,
85 pet_op_assume,
86 pet_op_kill,
87 pet_op_and,
88 pet_op_xor,
89 pet_op_or,
90 pet_op_not,
91 pet_op_land,
92 pet_op_lor,
93 pet_op_lnot,
94 pet_op_cond,
95 pet_op_last
98 /* Index into the pet_expr->args array when pet_expr->type == pet_expr_unary
100 enum pet_un_arg_type {
101 pet_un_arg
104 /* Indices into the pet_expr->args array when
105 * pet_expr->type == pet_expr_binary
107 enum pet_bin_arg_type {
108 pet_bin_lhs,
109 pet_bin_rhs
112 /* Indices into the pet_expr->args array when
113 * pet_expr->type == pet_expr_ternary
115 enum pet_ter_arg_type {
116 pet_ter_cond,
117 pet_ter_true,
118 pet_ter_false
121 struct pet_expr;
122 typedef struct pet_expr pet_expr;
124 /* Return an additional reference to "expr". */
125 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr);
126 /* Free a reference to "expr". */
127 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr);
129 /* Return the isl_ctx in which "expr" was created. */
130 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr);
132 /* Return the type of "expr". */
133 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr);
134 /* Return the number of arguments of "expr". */
135 int pet_expr_get_n_arg(__isl_keep pet_expr *expr);
136 /* Set the number of arguments of "expr" to "n". */
137 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n);
138 /* Return the argument of "expr" at position "pos". */
139 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos);
140 /* Replace the argument of "expr" at position "pos" by "arg". */
141 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
142 __isl_take pet_expr *arg);
144 /* Return the operation type of operation expression "expr". */
145 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr);
146 /* Replace the operation type of operation expression "expr" by "type". */
147 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
148 enum pet_op_type type);
150 /* Construct a (read) access pet_expr from an index expression. */
151 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index);
153 /* Does "expr" represent an affine expression? */
154 int pet_expr_is_affine(__isl_keep pet_expr *expr);
155 /* Does the access expression "expr" read the accessed elements? */
156 int pet_expr_access_is_read(__isl_keep pet_expr *expr);
157 /* Does the access expression "expr" write to the accessed elements? */
158 int pet_expr_access_is_write(__isl_keep pet_expr *expr);
159 /* Mark "expr" as a read dependening on "read". */
160 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
161 int read);
162 /* Mark "expr" as a write dependening on "write". */
163 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
164 int write);
165 /* Return the reference identifier of access expression "expr". */
166 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr);
167 /* Replace the reference identifier of access expression "expr" by "ref_id". */
168 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
169 __isl_take isl_id *ref_id);
170 /* Return the identifier of the outer array accessed by "expr". */
171 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr);
172 /* Return the index expression of access expression "expr". */
173 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
174 __isl_keep pet_expr *expr);
176 /* Return the potential read access relation of access expression "expr". */
177 __isl_give isl_map *pet_expr_access_get_may_access(__isl_keep pet_expr *expr);
178 /* Return the definite access relation of access expression "expr". */
179 __isl_give isl_map *pet_expr_access_get_must_access(__isl_keep pet_expr *expr);
180 /* Return the argument dependent access relation of access expression "expr". */
181 __isl_give isl_map *pet_expr_access_get_dependent_access(
182 __isl_keep pet_expr *expr);
183 /* Return the tagged potential read access relation of access "expr". */
184 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
185 __isl_keep pet_expr *expr);
187 /* Return the name of the function called by "expr". */
188 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr);
189 /* Replace the name of the function called by "expr" by "name". */
190 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
191 __isl_keep const char *name);
193 /* Replace the type of the cast performed by "expr" by "name". */
194 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
195 __isl_keep const char *name);
197 /* Return the value of the integer represented by "expr". */
198 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr);
199 /* Replace the value of the integer represented by "expr" by "v". */
200 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
201 __isl_take isl_val *v);
203 /* Return a string representation of the double expression "expr". */
204 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr);
205 /* Replace value and string representation of the double expression "expr" */
206 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
207 double d, __isl_keep const char *s);
209 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access. */
210 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
211 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user);
212 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call. */
213 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
214 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user);
216 void pet_expr_dump(__isl_keep pet_expr *expr);
218 enum pet_tree_type {
219 pet_tree_error = -1,
220 pet_tree_expr,
221 pet_tree_block,
222 pet_tree_break,
223 pet_tree_continue,
224 pet_tree_decl, /* A declaration without initialization */
225 pet_tree_decl_init, /* A declaration with initialization */
226 pet_tree_if, /* An if without an else branch */
227 pet_tree_if_else, /* An if with an else branch */
228 pet_tree_for,
229 pet_tree_infinite_loop,
230 pet_tree_while
233 struct pet_tree;
234 typedef struct pet_tree pet_tree;
236 /* Return the isl_ctx in which "tree" was created. */
237 isl_ctx *pet_tree_get_ctx(__isl_keep pet_tree *tree);
239 /* Return an additional reference to "tree". */
240 __isl_give pet_tree *pet_tree_copy(__isl_keep pet_tree *tree);
241 /* Free a reference to "tree". */
242 __isl_null pet_tree *pet_tree_free(__isl_take pet_tree *tree);
244 /* Return the location of "tree". */
245 __isl_give pet_loc *pet_tree_get_loc(__isl_keep pet_tree *tree);
247 /* Return the type of "tree". */
248 enum pet_tree_type pet_tree_get_type(__isl_keep pet_tree *tree);
250 /* Return the expression of the expression tree "tree". */
251 __isl_give pet_expr *pet_tree_expr_get_expr(__isl_keep pet_tree *tree);
253 /* Return the number of children of the block tree "tree". */
254 int pet_tree_block_n_child(__isl_keep pet_tree *tree);
255 /* Return child "pos" of the block tree "tree". */
256 __isl_give pet_tree *pet_tree_block_get_child(__isl_keep pet_tree *tree,
257 int pos);
259 /* Is "tree" a declaration (with or without initialization)? */
260 int pet_tree_is_decl(__isl_keep pet_tree *tree);
261 /* Return the variable declared by the declaration tree "tree". */
262 __isl_give pet_expr *pet_tree_decl_get_var(__isl_keep pet_tree *tree);
263 /* Return the initial value of the pet_tree_decl_init tree "tree". */
264 __isl_give pet_expr *pet_tree_decl_get_init(__isl_keep pet_tree *tree);
266 /* Return the condition of the if tree "tree". */
267 __isl_give pet_expr *pet_tree_if_get_cond(__isl_keep pet_tree *tree);
268 /* Return the then branch of the if tree "tree". */
269 __isl_give pet_tree *pet_tree_if_get_then(__isl_keep pet_tree *tree);
270 /* Return the else branch of the if tree with else branch "tree". */
271 __isl_give pet_tree *pet_tree_if_get_else(__isl_keep pet_tree *tree);
273 /* Is "tree" a for loop, a while loop or an infinite loop? */
274 int pet_tree_is_loop(__isl_keep pet_tree *tree);
275 /* Return the induction variable of the for loop "tree" */
276 __isl_give pet_expr *pet_tree_loop_get_var(__isl_keep pet_tree *tree);
277 /* Return the initial value of the induction variable of the for loop "tree" */
278 __isl_give pet_expr *pet_tree_loop_get_init(__isl_keep pet_tree *tree);
279 /* Return the condition of the loop tree "tree" */
280 __isl_give pet_expr *pet_tree_loop_get_cond(__isl_keep pet_tree *tree);
281 /* Return the induction variable of the for loop "tree" */
282 __isl_give pet_expr *pet_tree_loop_get_inc(__isl_keep pet_tree *tree);
283 /* Return the body of the loop tree "tree" */
284 __isl_give pet_tree *pet_tree_loop_get_body(__isl_keep pet_tree *tree);
286 /* Call "fn" on each top-level expression in the nodes of "tree" */
287 int pet_tree_foreach_expr(__isl_keep pet_tree *tree,
288 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user);
289 /* Call "fn" on each access subexpression in the nodes of "tree" */
290 int pet_tree_foreach_access_expr(__isl_keep pet_tree *tree,
291 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user);
293 void pet_tree_dump(__isl_keep pet_tree *tree);
295 /* "loc" represents the region of the source code that is represented
296 * by this statement.
298 * If the statement has arguments, i.e., n_arg != 0, then
299 * "domain" is a wrapped map, mapping the iteration domain
300 * to the values of the arguments for which this statement
301 * is executed.
302 * Otherwise, it is simply the iteration domain.
304 * If one of the arguments is an access expression that accesses
305 * more than one element for a given iteration, then the constraints
306 * on the value of this argument (encoded in "domain") should be satisfied
307 * for all of those accessed elements.
309 struct pet_stmt {
310 pet_loc *loc;
311 isl_set *domain;
312 isl_map *schedule;
313 pet_expr *body;
315 unsigned n_arg;
316 pet_expr **args;
319 /* Return the iteration space of "stmt". */
320 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt);
322 /* Is "stmt" an assignment statement? */
323 int pet_stmt_is_assign(struct pet_stmt *stmt);
324 /* Is "stmt" a kill statement? */
325 int pet_stmt_is_kill(struct pet_stmt *stmt);
327 /* pet_stmt_build_ast_exprs is currently limited to only handle
328 * some forms of data dependent accesses.
329 * If pet_stmt_can_build_ast_exprs returns 1, then pet_stmt_build_ast_exprs
330 * can safely be called on "stmt".
332 int pet_stmt_can_build_ast_exprs(struct pet_stmt *stmt);
333 /* Construct an associative array from reference identifiers of
334 * access expressions in "stmt" to the corresponding isl_ast_expr.
335 * Each index expression is first transformed through "fn_index"
336 * (if not NULL). Then an AST expression is generated using "build".
337 * Finally, the AST expression is transformed using "fn_expr"
338 * (if not NULL).
340 __isl_give isl_id_to_ast_expr *pet_stmt_build_ast_exprs(struct pet_stmt *stmt,
341 __isl_keep isl_ast_build *build,
342 __isl_give isl_multi_pw_aff *(*fn_index)(
343 __isl_take isl_multi_pw_aff *mpa, __isl_keep isl_id *id,
344 void *user), void *user_index,
345 __isl_give isl_ast_expr *(*fn_expr)(__isl_take isl_ast_expr *expr,
346 __isl_keep isl_id *id, void *user), void *user_expr);
348 /* Print "stmt" to "p".
350 * The access expressions in "stmt" are replaced by the isl_ast_expr
351 * associated to its reference identifier in "ref2expr".
353 __isl_give isl_printer *pet_stmt_print_body(struct pet_stmt *stmt,
354 __isl_take isl_printer *p, __isl_keep isl_id_to_ast_expr *ref2expr);
356 /* This structure represents a defined type.
357 * "name" is the name of the type, while "definition" is a string
358 * representation of its definition.
360 struct pet_type {
361 char *name;
362 char *definition;
365 /* context holds constraints on the parameter that ensure that
366 * this array has a valid (i.e., non-negative) size
368 * extent holds constraints on the indices
370 * value_bounds holds constraints on the elements of the array
371 * and may be NULL if no such constraints were specified by the user
373 * element_size is the size in bytes of each array element
374 * element_type is the type of the array elements.
375 * element_is_record is set if this type is a record type.
377 * live_out is set if the array appears in a live-out pragma
379 * if uniquely_defined is set then the array is written by a single access
380 * such that any element that is ever read
381 * is known to be assigned exactly once before the read
383 * declared is set if the array was declared somewhere inside the scop.
384 * exposed is set if the declared array is visible outside the scop.
386 struct pet_array {
387 isl_set *context;
388 isl_set *extent;
389 isl_set *value_bounds;
390 char *element_type;
391 int element_is_record;
392 int element_size;
393 int live_out;
394 int uniquely_defined;
395 int declared;
396 int exposed;
399 /* This structure represents an implication on a boolean filter.
400 * In particular, if the filter value of an element in the domain
401 * of "extension" is equal to "satisfied", then the filter values
402 * of the corresponding images in "extension" are also equal
403 * to "satisfied".
405 struct pet_implication {
406 int satisfied;
407 isl_map *extension;
410 /* "loc" represents the region of the source code that is represented
411 * by this scop.
412 * If the scop was detected based on scop and endscop pragmas, then
413 * the lines containing these pragmas are included in this region.
414 * In the final result, the context describes the set of parameter values
415 * for which the scop can be executed.
416 * During the construction of the pet_scop, the context lives in a set space
417 * where each dimension refers to an outer loop.
418 * context_value describes assignments to the parameters (if any)
419 * outside of the scop.
421 * The n_type types define types that may be referenced from by the arrays.
423 * The n_implication implications describe implications on boolean filters.
425 struct pet_scop {
426 pet_loc *loc;
428 isl_set *context;
429 isl_set *context_value;
431 int n_type;
432 struct pet_type **types;
434 int n_array;
435 struct pet_array **arrays;
437 int n_stmt;
438 struct pet_stmt **stmts;
440 int n_implication;
441 struct pet_implication **implications;
444 /* Return a textual representation of the operator. */
445 const char *pet_op_str(enum pet_op_type op);
446 int pet_op_is_inc_dec(enum pet_op_type op);
448 /* Extract a pet_scop from a C source file.
449 * If function is not NULL, then the pet_scop is extracted from
450 * a function with that name.
452 struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx,
453 const char *filename, const char *function);
455 /* Transform the C source file "input" by rewriting each scop
456 * When autodetecting scops, at most one scop per function is rewritten.
457 * The transformed C code is written to "output".
459 int pet_transform_C_source(isl_ctx *ctx, const char *input, FILE *output,
460 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
461 struct pet_scop *scop, void *user), void *user);
462 /* Given a scop and a printer passed to a pet_transform_C_source callback,
463 * print the original corresponding code to the printer.
465 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
466 __isl_take isl_printer *p);
468 /* Update all isl_sets and isl_maps such that they all have the same
469 * parameters in the same order.
471 struct pet_scop *pet_scop_align_params(struct pet_scop *scop);
473 /* Does "scop" contain any data dependent accesses? */
474 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop);
475 /* Does "scop" contain any data dependent conditions? */
476 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop);
477 /* pet_stmt_build_ast_exprs is currently limited to only handle
478 * some forms of data dependent accesses.
479 * If pet_scop_can_build_ast_exprs returns 1, then pet_stmt_build_ast_exprs
480 * can safely be called on all statements in the scop.
482 int pet_scop_can_build_ast_exprs(struct pet_scop *scop);
484 void pet_scop_dump(struct pet_scop *scop);
485 struct pet_scop *pet_scop_free(struct pet_scop *scop);
487 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop);
488 /* Collect all potential read access relations. */
489 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop);
490 /* Collect all tagged potential read access relations. */
491 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
492 struct pet_scop *scop);
493 /* Collect all potential write access relations. */
494 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop);
495 /* Collect all definite write access relations. */
496 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop);
497 /* Collect all tagged potential write access relations. */
498 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
499 struct pet_scop *scop);
500 /* Collect all tagged definite write access relations. */
501 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
502 struct pet_scop *scop);
503 /* Collect all definite kill access relations. */
504 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop);
505 /* Collect all tagged definite kill access relations. */
506 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
507 struct pet_scop *scop);
508 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop);
510 #if defined(__cplusplus)
512 #endif
514 #endif