update isl for __isl_null memory management annotation
[pet.git] / include / pet.h
blob9fd4219e6df09775b0bdcf0e32aeb75d24509f8e
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 enum pet_expr_type {
35 pet_expr_access,
36 pet_expr_call,
37 pet_expr_cast,
38 pet_expr_int,
39 pet_expr_double,
40 pet_expr_op
43 enum pet_op_type {
44 /* only compound assignments operators before assignment */
45 pet_op_add_assign,
46 pet_op_sub_assign,
47 pet_op_mul_assign,
48 pet_op_div_assign,
49 pet_op_assign,
50 pet_op_add,
51 pet_op_sub,
52 pet_op_mul,
53 pet_op_div,
54 pet_op_mod,
55 pet_op_shl,
56 pet_op_shr,
57 pet_op_eq,
58 pet_op_ne,
59 pet_op_le,
60 pet_op_ge,
61 pet_op_lt,
62 pet_op_gt,
63 pet_op_minus,
64 pet_op_post_inc,
65 pet_op_post_dec,
66 pet_op_pre_inc,
67 pet_op_pre_dec,
68 pet_op_address_of,
69 pet_op_assume,
70 pet_op_kill,
71 pet_op_and,
72 pet_op_xor,
73 pet_op_or,
74 pet_op_not,
75 pet_op_land,
76 pet_op_lor,
77 pet_op_lnot,
78 pet_op_cond,
79 pet_op_last
82 /* Index into the pet_expr->args array when pet_expr->type == pet_expr_unary
84 enum pet_un_arg_type {
85 pet_un_arg
88 /* Indices into the pet_expr->args array when
89 * pet_expr->type == pet_expr_binary
91 enum pet_bin_arg_type {
92 pet_bin_lhs,
93 pet_bin_rhs
96 /* Indices into the pet_expr->args array when
97 * pet_expr->type == pet_expr_ternary
99 enum pet_ter_arg_type {
100 pet_ter_cond,
101 pet_ter_true,
102 pet_ter_false
105 /* d is valid when type == pet_expr_double
106 * i isl valid when type == pet_expr_int
107 * acc is valid when type == pet_expr_access
108 * name is valid when type == pet_expr_call
109 * type is valid when type == pet_expr_cast
110 * op is valid otherwise
112 * For each access expression inside the body of a statement, acc.ref_id
113 * is a unique reference identifier.
114 * acc.index represents the index expression, while acc.access
115 * represents the corresponding access relation.
116 * The output dimension of the index expression may be smaller
117 * than the number of dimensions of the accessed array.
118 * The target space of the access relation, on the other hand,
119 * is equal to the array space.
120 * Both acc.index and acc.access usually map an iteration space
121 * to a (partial) data space.
122 * If the access has arguments, however, then the domain of the
123 * mapping is a wrapped mapping from the iteration space
124 * to a space of dimensionality equal to the number of arguments.
125 * Each dimension in this space corresponds to the value of the
126 * corresponding argument.
128 * The ranges of the index expressions and access relations may
129 * also be wrapped relations, in which case the expression represents
130 * a member access, with the structure represented by the domain
131 * of this wrapped relation and the member represented by the range.
132 * In case of nested member accesses, the domain is itself a wrapped
133 * relation.
135 * If the data space is unnamed (and 1D), then it represents
136 * the set of integers. That is, the access represents a value that
137 * is equal to the index.
139 * A double is represented as both an (approximate) value "val" and
140 * a string representation "s".
142 struct pet_expr {
143 enum pet_expr_type type;
145 unsigned n_arg;
146 struct pet_expr **args;
148 union {
149 struct {
150 isl_id *ref_id;
151 isl_map *access;
152 isl_multi_pw_aff *index;
153 int read;
154 int write;
155 } acc;
156 enum pet_op_type op;
157 char *name;
158 char *type_name;
159 struct {
160 double val;
161 char *s;
162 } d;
163 isl_val *i;
167 struct pet_expr *pet_expr_free(struct pet_expr *expr);
169 /* Construct a (read) access pet_expr from an index expression. */
170 struct pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index);
172 /* Does "expr" represent an affine expression? */
173 int pet_expr_is_affine(struct pet_expr *expr);
174 /* Does the access expression "expr" read the accessed elements? */
175 int pet_expr_access_is_read(struct pet_expr *expr);
176 /* Does the access expression "expr" write to the accessed elements? */
177 int pet_expr_access_is_write(struct pet_expr *expr);
178 /* Return the identifier of the outer array accessed by "expr". */
179 __isl_give isl_id *pet_expr_access_get_id(struct pet_expr *expr);
181 /* Return the reference identifier of access expression "expr". */
182 __isl_give isl_id *pet_expr_access_get_ref_id(struct pet_expr *expr);
184 /* Return the potential read access relation of access expression "expr". */
185 __isl_give isl_map *pet_expr_access_get_may_access(struct pet_expr *expr);
186 /* Return the definite access relation of access expression "expr". */
187 __isl_give isl_map *pet_expr_access_get_must_access(struct pet_expr *expr);
188 /* Return the argument dependent access relation of access expression "expr". */
189 __isl_give isl_map *pet_expr_access_get_dependent_access(struct pet_expr *expr);
190 /* Return the tagged potential read access relation of access "expr". */
191 __isl_give isl_map *pet_expr_access_get_tagged_may_access(
192 struct pet_expr *expr);
194 /* Return a string representation of the double expression "expr". */
195 __isl_give char *pet_expr_double_get_str(struct pet_expr *expr);
197 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_access. */
198 int pet_expr_foreach_access_expr(struct pet_expr *expr,
199 int (*fn)(struct pet_expr *expr, void *user), void *user);
200 /* Call "fn" on each of the subexpressions of "expr" of type pet_expr_call. */
201 int pet_expr_foreach_call_expr(struct pet_expr *expr,
202 int (*fn)(struct pet_expr *expr, void *user), void *user);
204 void pet_expr_dump(struct pet_expr *expr);
206 /* If the statement has arguments, i.e., n_arg != 0, then
207 * "domain" is a wrapped map, mapping the iteration domain
208 * to the values of the arguments for which this statement
209 * is executed.
210 * Otherwise, it is simply the iteration domain.
212 * If one of the arguments is an access expression that accesses
213 * more than one element for a given iteration, then the constraints
214 * on the value of this argument (encoded in "domain") should be satisfied
215 * for all of those accessed elements.
217 struct pet_stmt {
218 int line;
219 isl_set *domain;
220 isl_map *schedule;
221 struct pet_expr *body;
223 unsigned n_arg;
224 struct pet_expr **args;
227 /* Return the iteration space of "stmt". */
228 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt);
230 /* Is "stmt" an assignment statement? */
231 int pet_stmt_is_assign(struct pet_stmt *stmt);
232 /* Is "stmt" a kill statement? */
233 int pet_stmt_is_kill(struct pet_stmt *stmt);
235 /* Construct an associative array from reference identifiers of
236 * access expressions in "stmt" to the corresponding isl_ast_expr.
237 * Each index expression is first transformed through "fn_index"
238 * (if not NULL). Then an AST expression is generated using "build".
239 * Finally, the AST expression is transformed using "fn_expr"
240 * (if not NULL).
242 __isl_give isl_id_to_ast_expr *pet_stmt_build_ast_exprs(struct pet_stmt *stmt,
243 __isl_keep isl_ast_build *build,
244 __isl_give isl_multi_pw_aff *(*fn_index)(
245 __isl_take isl_multi_pw_aff *mpa, __isl_keep isl_id *id,
246 void *user), void *user_index,
247 __isl_give isl_ast_expr *(*fn_expr)(__isl_take isl_ast_expr *expr,
248 __isl_keep isl_id *id, void *user), void *user_expr);
250 /* Print "stmt" to "p".
252 * The access expressions in "stmt" are replaced by the isl_ast_expr
253 * associated to its reference identifier in "ref2expr".
255 __isl_give isl_printer *pet_stmt_print_body(struct pet_stmt *stmt,
256 __isl_take isl_printer *p, __isl_keep isl_id_to_ast_expr *ref2expr);
258 /* This structure represents a defined type.
259 * "name" is the name of the type, while "definition" is a string
260 * representation of its definition.
262 struct pet_type {
263 char *name;
264 char *definition;
267 /* context holds constraints on the parameter that ensure that
268 * this array has a valid (i.e., non-negative) size
270 * extent holds constraints on the indices
272 * value_bounds holds constraints on the elements of the array
273 * and may be NULL if no such constraints were specified by the user
275 * element_size is the size in bytes of each array element
276 * element_type is the type of the array elements.
277 * element_is_record is set if this type is a record type.
279 * live_out is set if the array appears in a live-out pragma
281 * if uniquely_defined is set then the array is written by a single access
282 * such that any element that is ever read
283 * is known to be assigned exactly once before the read
285 * declared is set if the array was declared somewhere inside the scop.
286 * exposed is set if the declared array is visible outside the scop.
288 struct pet_array {
289 isl_set *context;
290 isl_set *extent;
291 isl_set *value_bounds;
292 char *element_type;
293 int element_is_record;
294 int element_size;
295 int live_out;
296 int uniquely_defined;
297 int declared;
298 int exposed;
301 /* This structure represents an implication on a boolean filter.
302 * In particular, if the filter value of an element in the domain
303 * of "extension" is equal to "satisfied", then the filter values
304 * of the corresponding images in "extension" are also equal
305 * to "satisfied".
307 struct pet_implication {
308 int satisfied;
309 isl_map *extension;
312 /* The start and end fields contain the offsets in the input file
313 * of the scop, where end points to the first character after the scop.
314 * If the scop was detected based on scop and endscop pragmas, then
315 * the lines containing these pragmas are included in this range.
316 * Internally, end may be zero to indicate that no offset information is
317 * available (yet).
318 * The context describes the set of parameter values for which
319 * the scop can be executed.
320 * context_value describes assignments to the parameters (if any)
321 * outside of the scop.
323 * The n_type types define types that may be referenced from by the arrays.
325 * The n_implication implications describe implications on boolean filters.
327 struct pet_scop {
328 unsigned start;
329 unsigned end;
331 isl_set *context;
332 isl_set *context_value;
334 int n_type;
335 struct pet_type **types;
337 int n_array;
338 struct pet_array **arrays;
340 int n_stmt;
341 struct pet_stmt **stmts;
343 int n_implication;
344 struct pet_implication **implications;
347 /* Return a textual representation of the operator. */
348 const char *pet_op_str(enum pet_op_type op);
349 int pet_op_is_inc_dec(enum pet_op_type op);
351 /* Extract a pet_scop from a C source file.
352 * If function is not NULL, then the pet_scop is extracted from
353 * a function with that name.
355 struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx,
356 const char *filename, const char *function);
358 /* Transform the C source file "input" by rewriting each scop
359 * When autodetecting scops, at most one scop per function is rewritten.
360 * The transformed C code is written to "output".
362 int pet_transform_C_source(isl_ctx *ctx, const char *input, FILE *output,
363 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
364 struct pet_scop *scop, void *user), void *user);
365 /* Given a scop and a printer passed to a pet_transform_C_source callback,
366 * print the original corresponding code to the printer.
368 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
369 __isl_take isl_printer *p);
371 /* Update all isl_sets and isl_maps such that they all have the same
372 * parameters in the same order.
374 struct pet_scop *pet_scop_align_params(struct pet_scop *scop);
376 /* Does "scop" contain any data dependent accesses? */
377 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop);
378 /* Does "scop" contain any data dependent conditions? */
379 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop);
381 void pet_scop_dump(struct pet_scop *scop);
382 struct pet_scop *pet_scop_free(struct pet_scop *scop);
384 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop);
385 /* Collect all potential read access relations. */
386 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop);
387 /* Collect all tagged potential read access relations. */
388 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
389 struct pet_scop *scop);
390 /* Collect all potential write access relations. */
391 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop);
392 /* Collect all definite write access relations. */
393 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop);
394 /* Collect all tagged potential write access relations. */
395 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
396 struct pet_scop *scop);
397 /* Collect all tagged definite write access relations. */
398 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
399 struct pet_scop *scop);
400 /* Collect all definite kill access relations. */
401 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop);
402 /* Collect all tagged definite kill access relations. */
403 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
404 struct pet_scop *scop);
405 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop);
407 #if defined(__cplusplus)
409 #endif
411 #endif