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