introduce pet_type
[pet.git] / include / pet.h
blob16427636afb2d0c4688e4d4feb200a83341737b9
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 #define PET_OVERFLOW_AVOID 0
27 #define PET_OVERFLOW_IGNORE 1
28 int pet_options_set_signed_overflow(isl_ctx *ctx, int val);
29 int pet_options_get_signed_overflow(isl_ctx *ctx);
31 enum pet_expr_type {
32 pet_expr_access,
33 pet_expr_call,
34 pet_expr_cast,
35 pet_expr_double,
36 pet_expr_unary,
37 pet_expr_binary,
38 pet_expr_ternary
41 enum pet_op_type {
42 /* only compound assignments operators before assignment */
43 pet_op_add_assign,
44 pet_op_sub_assign,
45 pet_op_mul_assign,
46 pet_op_div_assign,
47 pet_op_assign,
48 pet_op_add,
49 pet_op_sub,
50 pet_op_mul,
51 pet_op_div,
52 pet_op_mod,
53 pet_op_eq,
54 pet_op_le,
55 pet_op_lt,
56 pet_op_gt,
57 pet_op_minus,
58 pet_op_post_inc,
59 pet_op_post_dec,
60 pet_op_pre_inc,
61 pet_op_pre_dec,
62 pet_op_address_of,
63 pet_op_kill,
64 pet_op_last
67 /* Index into the pet_expr->args array when pet_expr->type == pet_expr_unary
69 enum pet_un_arg_type {
70 pet_un_arg
73 /* Indices into the pet_expr->args array when
74 * pet_expr->type == pet_expr_binary
76 enum pet_bin_arg_type {
77 pet_bin_lhs,
78 pet_bin_rhs
81 /* Indices into the pet_expr->args array when
82 * pet_expr->type == pet_expr_ternary
84 enum pet_ter_arg_type {
85 pet_ter_cond,
86 pet_ter_true,
87 pet_ter_false
90 /* d is valid when type == pet_expr_double
91 * acc is valid when type == pet_expr_access
92 * name is valid when type == pet_expr_call
93 * type is valid when type == pet_expr_cast
94 * op is valid otherwise
96 * For each access expression inside the body of a statement, acc.ref_id
97 * is a unique reference identifier.
98 * acc.index represents the index expression, while acc.access
99 * represents the corresponding access relation.
100 * The output dimension of the index expression may be smaller
101 * than the number of dimensions of the accessed array.
102 * The target space of the access relation, on the other hand,
103 * is equal to the array space.
104 * Both acc.index and acc.access usually map an iteration space
105 * to a (partial) data space.
106 * If the access has arguments, however, then the domain of the
107 * mapping is a wrapped mapping from the iteration space
108 * to a space of dimensionality equal to the number of arguments.
109 * Each dimension in this space corresponds to the value of the
110 * corresponding argument.
112 * If the data space is unnamed (and 1D), then it represents
113 * the set of integers. That is, the access represents a value that
114 * is equal to the index.
116 * A double is represented as both an (approximate) value "val" and
117 * a string representation "s".
119 struct pet_expr {
120 enum pet_expr_type type;
122 unsigned n_arg;
123 struct pet_expr **args;
125 union {
126 struct {
127 isl_id *ref_id;
128 isl_map *access;
129 isl_multi_pw_aff *index;
130 int read;
131 int write;
132 } acc;
133 enum pet_op_type op;
134 char *name;
135 char *type_name;
136 struct {
137 double val;
138 char *s;
139 } d;
143 /* If the statement has arguments, i.e., n_arg != 0, then
144 * "domain" is a wrapped map, mapping the iteration domain
145 * to the values of the arguments for which this statement
146 * is executed.
147 * Otherwise, it is simply the iteration domain.
149 * If one of the arguments is an access expression that accesses
150 * more than one element for a given iteration, then the constraints
151 * on the value of this argument (encoded in "domain") should be satisfied
152 * for all of those accessed elements.
154 struct pet_stmt {
155 int line;
156 isl_set *domain;
157 isl_map *schedule;
158 struct pet_expr *body;
160 unsigned n_arg;
161 struct pet_expr **args;
164 /* Construct an associative array from reference identifiers of
165 * access expressions in "stmt" to the corresponding isl_ast_expr.
166 * Each index expression is first transformed through "fn_index"
167 * (if not NULL). Then an AST expression is generated using "build".
168 * Finally, the AST expression is transformed using "fn_expr"
169 * (if not NULL).
171 __isl_give isl_id_to_ast_expr *pet_stmt_build_ast_exprs(struct pet_stmt *stmt,
172 __isl_keep isl_ast_build *build,
173 __isl_give isl_multi_pw_aff *(*fn_index)(
174 __isl_take isl_multi_pw_aff *mpa, __isl_keep isl_id *id,
175 void *user), void *user_index,
176 __isl_give isl_ast_expr *(*fn_expr)(__isl_take isl_ast_expr *expr,
177 __isl_keep isl_id *id, void *user), void *user_expr);
179 /* Print "stmt" to "p".
181 * The access expressions in "stmt" are replaced by the isl_ast_expr
182 * associated to its reference identifier in "ref2expr".
184 __isl_give isl_printer *pet_stmt_print_body(struct pet_stmt *stmt,
185 __isl_take isl_printer *p, __isl_keep isl_id_to_ast_expr *ref2expr);
187 /* This structure represents a defined type.
188 * "name" is the name of the type, while "definition" is a string
189 * representation of its definition.
191 struct pet_type {
192 char *name;
193 char *definition;
196 /* context holds constraints on the parameter that ensure that
197 * this array has a valid (i.e., non-negative) size
199 * extent holds constraints on the indices
201 * value_bounds holds constraints on the elements of the array
202 * and may be NULL if no such constraints were specified by the user
204 * element_size is the size in bytes of each array element
206 * live_out is set if the array appears in a live-out pragma
208 * if uniquely_defined is set then the array is written by a single access
209 * such that any element that is ever read
210 * is known to be assigned exactly once before the read
212 * declared is set if the array was declared somewhere inside the scop.
213 * exposed is set if the declared array is visible outside the scop.
215 struct pet_array {
216 isl_set *context;
217 isl_set *extent;
218 isl_set *value_bounds;
219 char *element_type;
220 int element_size;
221 int live_out;
222 int uniquely_defined;
223 int declared;
224 int exposed;
227 /* This structure represents an implication on a boolean filter.
228 * In particular, if the filter value of an element in the domain
229 * of "extension" is equal to "satisfied", then the filter values
230 * of the corresponding images in "extension" are also equal
231 * to "satisfied".
233 struct pet_implication {
234 int satisfied;
235 isl_map *extension;
238 /* The start and end fields contain the offsets in the input file
239 * of the scop, where end points to the first character after the scop.
240 * If the scop was detected based on scop and endscop pragmas, then
241 * the lines containing these pragmas are included in this range.
242 * Internally, end may be zero to indicate that no offset information is
243 * available (yet).
244 * The context describes the set of parameter values for which
245 * the scop can be executed.
246 * context_value describes assignments to the parameters (if any)
247 * outside of the scop.
249 * The n_type types define types that may be referenced from by the arrays.
251 * The n_implication implications describe implications on boolean filters.
253 struct pet_scop {
254 unsigned start;
255 unsigned end;
257 isl_set *context;
258 isl_set *context_value;
260 int n_type;
261 struct pet_type **types;
263 int n_array;
264 struct pet_array **arrays;
266 int n_stmt;
267 struct pet_stmt **stmts;
269 int n_implication;
270 struct pet_implication **implications;
273 /* Return a textual representation of the operator. */
274 const char *pet_op_str(enum pet_op_type op);
275 int pet_op_is_inc_dec(enum pet_op_type op);
277 /* Extract a pet_scop from a C source file.
278 * If function is not NULL, then the pet_scop is extracted from
279 * a function with that name.
281 struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx,
282 const char *filename, const char *function);
284 /* Transform the C source file "input" by rewriting each scop
285 * When autodetecting scops, at most one scop per function is rewritten.
286 * The transformed C code is written to "output".
288 int pet_transform_C_source(isl_ctx *ctx, const char *input, FILE *output,
289 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
290 struct pet_scop *scop, void *user), void *user);
291 /* Given a scop and a printer passed to a pet_transform_C_source callback,
292 * print the original corresponding code to the printer.
294 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
295 __isl_take isl_printer *p);
297 /* Update all isl_sets and isl_maps such that they all have the same
298 * parameters in the same order.
300 struct pet_scop *pet_scop_align_params(struct pet_scop *scop);
302 /* Does "scop" contain any data dependent accesses? */
303 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop);
304 /* Does "scop" contain any data dependent conditions? */
305 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop);
307 void pet_scop_dump(struct pet_scop *scop);
308 struct pet_scop *pet_scop_free(struct pet_scop *scop);
310 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop);
311 /* Collect all potential read access relations. */
312 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop);
313 /* Collect all potential write access relations. */
314 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop);
315 /* Collect all definite write access relations. */
316 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop);
317 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop);
319 #if defined(__cplusplus)
321 #endif
323 #endif