keep track of indentation of extracted code
[pet.git] / include / pet.h
blob065675b25d18f0fcd3308da9004f69b561b9fff1
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>
12 #include <isl/id_to_pw_aff.h>
14 #if defined(__cplusplus)
15 extern "C" {
16 #endif
18 struct pet_options;
19 ISL_ARG_DECL(pet_options, struct pet_options, pet_options_args)
21 /* If autodetect is set, any valid scop is extracted.
22 * Otherwise, the scop needs to be delimited by pragmas.
24 int pet_options_set_autodetect(isl_ctx *ctx, int val);
25 int pet_options_get_autodetect(isl_ctx *ctx);
27 int pet_options_set_detect_conditional_assignment(isl_ctx *ctx, int val);
28 int pet_options_get_detect_conditional_assignment(isl_ctx *ctx);
30 #define PET_OVERFLOW_AVOID 0
31 #define PET_OVERFLOW_IGNORE 1
32 int pet_options_set_signed_overflow(isl_ctx *ctx, int val);
33 int pet_options_get_signed_overflow(isl_ctx *ctx);
35 struct pet_loc;
36 typedef struct pet_loc pet_loc;
38 /* Return an additional reference to "loc". */
39 __isl_give pet_loc *pet_loc_copy(__isl_keep pet_loc *loc);
40 /* Free a reference to "loc". */
41 pet_loc *pet_loc_free(__isl_take pet_loc *loc);
43 /* Return the offset in the input file of the start of "loc". */
44 unsigned pet_loc_get_start(__isl_keep pet_loc *loc);
45 /* Return the offset in the input file of the character after "loc". */
46 unsigned pet_loc_get_end(__isl_keep pet_loc *loc);
47 /* Return the line number of a line within the "loc" region. */
48 int pet_loc_get_line(__isl_keep pet_loc *loc);
49 /* Return the indentation of the "loc" region. */
50 __isl_keep const char *pet_loc_get_indent(__isl_keep pet_loc *loc);
52 enum pet_expr_type {
53 pet_expr_error = -1,
54 pet_expr_access,
55 pet_expr_call,
56 pet_expr_cast,
57 pet_expr_int,
58 pet_expr_double,
59 pet_expr_op
62 enum pet_op_type {
63 /* only compound assignments operators before assignment */
64 pet_op_add_assign,
65 pet_op_sub_assign,
66 pet_op_mul_assign,
67 pet_op_div_assign,
68 pet_op_assign,
69 pet_op_add,
70 pet_op_sub,
71 pet_op_mul,
72 pet_op_div,
73 pet_op_mod,
74 pet_op_shl,
75 pet_op_shr,
76 pet_op_eq,
77 pet_op_ne,
78 pet_op_le,
79 pet_op_ge,
80 pet_op_lt,
81 pet_op_gt,
82 pet_op_minus,
83 pet_op_post_inc,
84 pet_op_post_dec,
85 pet_op_pre_inc,
86 pet_op_pre_dec,
87 pet_op_address_of,
88 pet_op_assume,
89 pet_op_kill,
90 pet_op_and,
91 pet_op_xor,
92 pet_op_or,
93 pet_op_not,
94 pet_op_land,
95 pet_op_lor,
96 pet_op_lnot,
97 pet_op_cond,
98 pet_op_last
101 /* Index into the pet_expr->args array when pet_expr->type == pet_expr_unary
103 enum pet_un_arg_type {
104 pet_un_arg
107 /* Indices into the pet_expr->args array when
108 * pet_expr->type == pet_expr_binary
110 enum pet_bin_arg_type {
111 pet_bin_lhs,
112 pet_bin_rhs
115 /* Indices into the pet_expr->args array when
116 * pet_expr->type == pet_expr_ternary
118 enum pet_ter_arg_type {
119 pet_ter_cond,
120 pet_ter_true,
121 pet_ter_false
124 struct pet_expr;
125 typedef struct pet_expr pet_expr;
127 /* Return an additional reference to "expr". */
128 __isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr);
129 /* Free a reference to "expr". */
130 __isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr);
132 /* Return the isl_ctx in which "expr" was created. */
133 isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr);
135 /* Return the type of "expr". */
136 enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr);
137 /* Return the number of arguments of "expr". */
138 int pet_expr_get_n_arg(__isl_keep pet_expr *expr);
139 /* Set the number of arguments of "expr" to "n". */
140 __isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n);
141 /* Return the argument of "expr" at position "pos". */
142 __isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos);
143 /* Replace the argument of "expr" at position "pos" by "arg". */
144 __isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
145 __isl_take pet_expr *arg);
147 /* Return the operation type of operation expression "expr". */
148 enum pet_op_type pet_expr_op_get_type(__isl_keep pet_expr *expr);
149 /* Replace the operation type of operation expression "expr" by "type". */
150 __isl_give pet_expr *pet_expr_op_set_type(__isl_take pet_expr *expr,
151 enum pet_op_type type);
153 /* Construct a (read) access pet_expr from an index expression. */
154 __isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index);
156 /* Does "expr" represent an affine expression? */
157 int pet_expr_is_affine(__isl_keep pet_expr *expr);
158 /* Does the access expression "expr" read the accessed elements? */
159 int pet_expr_access_is_read(__isl_keep pet_expr *expr);
160 /* Does the access expression "expr" write to the accessed elements? */
161 int pet_expr_access_is_write(__isl_keep pet_expr *expr);
162 /* Mark "expr" as a read dependening on "read". */
163 __isl_give pet_expr *pet_expr_access_set_read(__isl_take pet_expr *expr,
164 int read);
165 /* Mark "expr" as a write dependening on "write". */
166 __isl_give pet_expr *pet_expr_access_set_write(__isl_take pet_expr *expr,
167 int write);
168 /* Return the reference identifier of access expression "expr". */
169 __isl_give isl_id *pet_expr_access_get_ref_id(__isl_keep pet_expr *expr);
170 /* Replace the reference identifier of access expression "expr" by "ref_id". */
171 __isl_give pet_expr *pet_expr_access_set_ref_id(__isl_take pet_expr *expr,
172 __isl_take isl_id *ref_id);
173 /* Return the identifier of the outer array accessed by "expr". */
174 __isl_give isl_id *pet_expr_access_get_id(__isl_keep pet_expr *expr);
175 /* Return the index expression of access expression "expr". */
176 __isl_give isl_multi_pw_aff *pet_expr_access_get_index(
177 __isl_keep pet_expr *expr);
179 /* Return the potential read access relation of access expression "expr". */
180 __isl_give isl_map *pet_expr_access_get_may_access(__isl_keep pet_expr *expr);
181 /* Return the definite access relation of access expression "expr". */
182 __isl_give isl_map *pet_expr_access_get_must_access(__isl_keep pet_expr *expr);
183 /* Return the argument dependent access relation of access expression "expr". */
184 __isl_give isl_map *pet_expr_access_get_dependent_access(
185 __isl_keep pet_expr *expr);
186 /* Return the tagged potential read access relation of access "expr". */
187 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
188 __isl_keep pet_expr *expr);
190 /* Return the name of the function called by "expr". */
191 __isl_keep const char *pet_expr_call_get_name(__isl_keep pet_expr *expr);
192 /* Replace the name of the function called by "expr" by "name". */
193 __isl_give pet_expr *pet_expr_call_set_name(__isl_take pet_expr *expr,
194 __isl_keep const char *name);
196 /* Replace the type of the cast performed by "expr" by "name". */
197 __isl_give pet_expr *pet_expr_cast_set_type_name(__isl_take pet_expr *expr,
198 __isl_keep const char *name);
200 /* Return the value of the integer represented by "expr". */
201 __isl_give isl_val *pet_expr_int_get_val(__isl_keep pet_expr *expr);
202 /* Replace the value of the integer represented by "expr" by "v". */
203 __isl_give pet_expr *pet_expr_int_set_val(__isl_take pet_expr *expr,
204 __isl_take isl_val *v);
206 /* Return a string representation of the double expression "expr". */
207 __isl_give char *pet_expr_double_get_str(__isl_keep pet_expr *expr);
208 /* Replace value and string representation of the double expression "expr" */
209 __isl_give pet_expr *pet_expr_double_set(__isl_take pet_expr *expr,
210 double d, __isl_keep const char *s);
212 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access. */
213 int pet_expr_foreach_access_expr(__isl_keep pet_expr *expr,
214 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user);
215 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call. */
216 int pet_expr_foreach_call_expr(__isl_keep pet_expr *expr,
217 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user);
219 struct pet_context;
220 typedef struct pet_context pet_context;
222 /* Create a context with the given domain. */
223 __isl_give pet_context *pet_context_alloc(__isl_take isl_set *domain);
224 /* Return an additional reference to "pc". */
225 __isl_give pet_context *pet_context_copy(__isl_keep pet_context *pc);
226 /* Free a reference to "pc". */
227 __isl_null pet_context *pet_context_free(__isl_take pet_context *pc);
229 /* Return the isl_ctx in which "pc" was created. */
230 isl_ctx *pet_context_get_ctx(__isl_keep pet_context *pc);
232 /* Extract an affine expression defined over the domain of "pc" from "expr"
233 * or return NaN.
235 __isl_give isl_pw_aff *pet_expr_extract_affine(__isl_keep pet_expr *expr,
236 __isl_keep pet_context *pc);
238 void pet_expr_dump(__isl_keep pet_expr *expr);
240 enum pet_tree_type {
241 pet_tree_error = -1,
242 pet_tree_expr,
243 pet_tree_block,
244 pet_tree_break,
245 pet_tree_continue,
246 pet_tree_decl, /* A declaration without initialization */
247 pet_tree_decl_init, /* A declaration with initialization */
248 pet_tree_if, /* An if without an else branch */
249 pet_tree_if_else, /* An if with an else branch */
250 pet_tree_for,
251 pet_tree_infinite_loop,
252 pet_tree_while
255 struct pet_tree;
256 typedef struct pet_tree pet_tree;
258 /* Return the isl_ctx in which "tree" was created. */
259 isl_ctx *pet_tree_get_ctx(__isl_keep pet_tree *tree);
261 /* Return an additional reference to "tree". */
262 __isl_give pet_tree *pet_tree_copy(__isl_keep pet_tree *tree);
263 /* Free a reference to "tree". */
264 __isl_null pet_tree *pet_tree_free(__isl_take pet_tree *tree);
266 /* Return the location of "tree". */
267 __isl_give pet_loc *pet_tree_get_loc(__isl_keep pet_tree *tree);
269 /* Return the type of "tree". */
270 enum pet_tree_type pet_tree_get_type(__isl_keep pet_tree *tree);
272 /* Return the expression of the expression tree "tree". */
273 __isl_give pet_expr *pet_tree_expr_get_expr(__isl_keep pet_tree *tree);
275 /* Return the number of children of the block tree "tree". */
276 int pet_tree_block_n_child(__isl_keep pet_tree *tree);
277 /* Return child "pos" of the block tree "tree". */
278 __isl_give pet_tree *pet_tree_block_get_child(__isl_keep pet_tree *tree,
279 int pos);
281 /* Is "tree" a declaration (with or without initialization)? */
282 int pet_tree_is_decl(__isl_keep pet_tree *tree);
283 /* Return the variable declared by the declaration tree "tree". */
284 __isl_give pet_expr *pet_tree_decl_get_var(__isl_keep pet_tree *tree);
285 /* Return the initial value of the pet_tree_decl_init tree "tree". */
286 __isl_give pet_expr *pet_tree_decl_get_init(__isl_keep pet_tree *tree);
288 /* Return the condition of the if tree "tree". */
289 __isl_give pet_expr *pet_tree_if_get_cond(__isl_keep pet_tree *tree);
290 /* Return the then branch of the if tree "tree". */
291 __isl_give pet_tree *pet_tree_if_get_then(__isl_keep pet_tree *tree);
292 /* Return the else branch of the if tree with else branch "tree". */
293 __isl_give pet_tree *pet_tree_if_get_else(__isl_keep pet_tree *tree);
295 /* Is "tree" a for loop, a while loop or an infinite loop? */
296 int pet_tree_is_loop(__isl_keep pet_tree *tree);
297 /* Return the induction variable of the for loop "tree" */
298 __isl_give pet_expr *pet_tree_loop_get_var(__isl_keep pet_tree *tree);
299 /* Return the initial value of the induction variable of the for loop "tree" */
300 __isl_give pet_expr *pet_tree_loop_get_init(__isl_keep pet_tree *tree);
301 /* Return the condition of the loop tree "tree" */
302 __isl_give pet_expr *pet_tree_loop_get_cond(__isl_keep pet_tree *tree);
303 /* Return the induction variable of the for loop "tree" */
304 __isl_give pet_expr *pet_tree_loop_get_inc(__isl_keep pet_tree *tree);
305 /* Return the body of the loop tree "tree" */
306 __isl_give pet_tree *pet_tree_loop_get_body(__isl_keep pet_tree *tree);
308 /* Call "fn" on each top-level expression in the nodes of "tree" */
309 int pet_tree_foreach_expr(__isl_keep pet_tree *tree,
310 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user);
311 /* Call "fn" on each access subexpression in the nodes of "tree" */
312 int pet_tree_foreach_access_expr(__isl_keep pet_tree *tree,
313 int (*fn)(__isl_keep pet_expr *expr, void *user), void *user);
315 void pet_tree_dump(__isl_keep pet_tree *tree);
317 /* "loc" represents the region of the source code that is represented
318 * by this statement.
320 * If the statement has arguments, i.e., n_arg != 0, then
321 * "domain" is a wrapped map, mapping the iteration domain
322 * to the values of the arguments for which this statement
323 * is executed.
324 * Otherwise, it is simply the iteration domain.
326 * If one of the arguments is an access expression that accesses
327 * more than one element for a given iteration, then the constraints
328 * on the value of this argument (encoded in "domain") should be satisfied
329 * for all of those accessed elements.
331 struct pet_stmt {
332 pet_loc *loc;
333 isl_set *domain;
334 isl_map *schedule;
335 pet_expr *body;
337 unsigned n_arg;
338 pet_expr **args;
341 /* Return the iteration space of "stmt". */
342 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt);
344 /* Is "stmt" an assignment statement? */
345 int pet_stmt_is_assign(struct pet_stmt *stmt);
346 /* Is "stmt" a kill statement? */
347 int pet_stmt_is_kill(struct pet_stmt *stmt);
349 /* pet_stmt_build_ast_exprs is currently limited to only handle
350 * some forms of data dependent accesses.
351 * If pet_stmt_can_build_ast_exprs returns 1, then pet_stmt_build_ast_exprs
352 * can safely be called on "stmt".
354 int pet_stmt_can_build_ast_exprs(struct pet_stmt *stmt);
355 /* Construct an associative array from reference identifiers of
356 * access expressions in "stmt" to the corresponding isl_ast_expr.
357 * Each index expression is first transformed through "fn_index"
358 * (if not NULL). Then an AST expression is generated using "build".
359 * Finally, the AST expression is transformed using "fn_expr"
360 * (if not NULL).
362 __isl_give isl_id_to_ast_expr *pet_stmt_build_ast_exprs(struct pet_stmt *stmt,
363 __isl_keep isl_ast_build *build,
364 __isl_give isl_multi_pw_aff *(*fn_index)(
365 __isl_take isl_multi_pw_aff *mpa, __isl_keep isl_id *id,
366 void *user), void *user_index,
367 __isl_give isl_ast_expr *(*fn_expr)(__isl_take isl_ast_expr *expr,
368 __isl_keep isl_id *id, void *user), void *user_expr);
370 /* Print "stmt" to "p".
372 * The access expressions in "stmt" are replaced by the isl_ast_expr
373 * associated to its reference identifier in "ref2expr".
375 __isl_give isl_printer *pet_stmt_print_body(struct pet_stmt *stmt,
376 __isl_take isl_printer *p, __isl_keep isl_id_to_ast_expr *ref2expr);
378 /* This structure represents a defined type.
379 * "name" is the name of the type, while "definition" is a string
380 * representation of its definition.
382 struct pet_type {
383 char *name;
384 char *definition;
387 /* context holds constraints on the parameter that ensure that
388 * this array has a valid (i.e., non-negative) size
390 * extent holds constraints on the indices
392 * value_bounds holds constraints on the elements of the array
393 * and may be NULL if no such constraints were specified by the user
395 * element_size is the size in bytes of each array element
396 * element_type is the type of the array elements.
397 * element_is_record is set if this type is a record type.
399 * live_out is set if the array appears in a live-out pragma
401 * if uniquely_defined is set then the array is written by a single access
402 * such that any element that is ever read
403 * is known to be assigned exactly once before the read
405 * declared is set if the array was declared somewhere inside the scop.
406 * exposed is set if the declared array is visible outside the scop.
408 struct pet_array {
409 isl_set *context;
410 isl_set *extent;
411 isl_set *value_bounds;
412 char *element_type;
413 int element_is_record;
414 int element_size;
415 int live_out;
416 int uniquely_defined;
417 int declared;
418 int exposed;
421 /* This structure represents an implication on a boolean filter.
422 * In particular, if the filter value of an element in the domain
423 * of "extension" is equal to "satisfied", then the filter values
424 * of the corresponding images in "extension" are also equal
425 * to "satisfied".
427 struct pet_implication {
428 int satisfied;
429 isl_map *extension;
432 /* "loc" represents the region of the source code that is represented
433 * by this scop.
434 * If the scop was detected based on scop and endscop pragmas, then
435 * the lines containing these pragmas are included in this region.
436 * In the final result, the context describes the set of parameter values
437 * for which the scop can be executed.
438 * During the construction of the pet_scop, the context lives in a set space
439 * where each dimension refers to an outer loop.
440 * context_value describes assignments to the parameters (if any)
441 * outside of the scop.
443 * The n_type types define types that may be referenced from by the arrays.
445 * The n_implication implications describe implications on boolean filters.
447 struct pet_scop {
448 pet_loc *loc;
450 isl_set *context;
451 isl_set *context_value;
453 int n_type;
454 struct pet_type **types;
456 int n_array;
457 struct pet_array **arrays;
459 int n_stmt;
460 struct pet_stmt **stmts;
462 int n_implication;
463 struct pet_implication **implications;
466 /* Return a textual representation of the operator. */
467 const char *pet_op_str(enum pet_op_type op);
468 int pet_op_is_inc_dec(enum pet_op_type op);
470 /* Extract a pet_scop from a C source file.
471 * If function is not NULL, then the pet_scop is extracted from
472 * a function with that name.
474 struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx,
475 const char *filename, const char *function);
477 /* Transform the C source file "input" by rewriting each scop
478 * When autodetecting scops, at most one scop per function is rewritten.
479 * The transformed C code is written to "output".
481 int pet_transform_C_source(isl_ctx *ctx, const char *input, FILE *output,
482 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
483 struct pet_scop *scop, void *user), void *user);
484 /* Given a scop and a printer passed to a pet_transform_C_source callback,
485 * print the original corresponding code to the printer.
487 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
488 __isl_take isl_printer *p);
490 /* Update all isl_sets and isl_maps such that they all have the same
491 * parameters in the same order.
493 struct pet_scop *pet_scop_align_params(struct pet_scop *scop);
495 /* Does "scop" contain any data dependent accesses? */
496 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop);
497 /* Does "scop" contain any data dependent conditions? */
498 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop);
499 /* pet_stmt_build_ast_exprs is currently limited to only handle
500 * some forms of data dependent accesses.
501 * If pet_scop_can_build_ast_exprs returns 1, then pet_stmt_build_ast_exprs
502 * can safely be called on all statements in the scop.
504 int pet_scop_can_build_ast_exprs(struct pet_scop *scop);
506 void pet_scop_dump(struct pet_scop *scop);
507 struct pet_scop *pet_scop_free(struct pet_scop *scop);
509 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop);
510 /* Collect all potential read access relations. */
511 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop);
512 /* Collect all tagged potential read access relations. */
513 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
514 struct pet_scop *scop);
515 /* Collect all potential write access relations. */
516 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop);
517 /* Collect all definite write access relations. */
518 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop);
519 /* Collect all tagged potential write access relations. */
520 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
521 struct pet_scop *scop);
522 /* Collect all tagged definite write access relations. */
523 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
524 struct pet_scop *scop);
525 /* Collect all definite kill access relations. */
526 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop);
527 /* Collect all tagged definite kill access relations. */
528 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
529 struct pet_scop *scop);
530 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop);
532 #if defined(__cplusplus)
534 #endif
536 #endif