export pet_expr_is_affine
[pet.git] / scan.h
blobfda485a9ec1d4675f5dd92f7f5123dbad87bede2
1 #include <map>
3 #include <clang/Basic/SourceManager.h>
4 #include <clang/AST/Decl.h>
5 #include <clang/AST/Stmt.h>
6 #include <clang/Lex/Preprocessor.h>
8 #include <isl/ctx.h>
9 #include <isl/map.h>
10 #include <isl/val.h>
12 #include "scop.h"
14 /* The location of the scop, as delimited by scop and endscop
15 * pragmas by the user.
17 struct ScopLoc {
18 ScopLoc() : end(0) {}
20 unsigned start;
21 unsigned end;
24 /* Compare two RecordDecl pointers based on their names.
26 struct less_name {
27 bool operator()(const clang::RecordDecl *x,
28 const clang::RecordDecl *y) {
29 return x->getNameAsString().compare(y->getNameAsString()) < 0;
33 /* A sorted set of RecordDecl pointers. The actual order is not important,
34 * only that it is consistent across platforms.
36 typedef std::set<clang::RecordDecl *, less_name> lex_recorddecl_set;
38 struct PetScan {
39 clang::Preprocessor &PP;
40 clang::ASTContext &ast_context;
41 /* If autodetect is false, then loc contains the location
42 * of the scop to be extracted.
44 ScopLoc &loc;
45 isl_ctx *ctx;
46 pet_options *options;
47 /* The sequence number of the next statement. */
48 int n_stmt;
49 /* The sequence number of the next virtual scalar. */
50 int n_test;
51 /* Set if the pet_scop returned by an extract method only
52 * represents part of the input tree.
54 bool partial;
55 /* Set is nested accesses are allowed in general.
56 * This currently defaults to true.
58 bool allow_nested;
59 /* Set if nested accesses are allowed in that part of the tree
60 * that is currently under investigation.
62 bool nesting_enabled;
63 /* Maps identifiers to the last value that was assigned to them.
64 * If an identifier is mapped to NULL, then something may have
65 * been assigned, but we don't know what.
66 * assigned_value does not take a reference to the isl_pw_aff
67 * object, so each such isl_pw_aff needs to be stored in
68 * the set of "expressions".
70 std::map<clang::ValueDecl *, isl_pw_aff *> assigned_value;
71 /* A collection of isl_pw_affs used in assigned_value or other
72 * temporary maps. expressions holds a reference for each
73 * isl_pw_aff, which is freed in the destructor of PetScan.
75 std::set<isl_pw_aff *> expressions;
77 /* A union of mappings of the form
78 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
80 isl_union_map *value_bounds;
82 PetScan(clang::Preprocessor &PP,
83 clang::ASTContext &ast_context, ScopLoc &loc,
84 pet_options *options, __isl_take isl_union_map *value_bounds) :
85 ctx(isl_union_map_get_ctx(value_bounds)), PP(PP),
86 ast_context(ast_context), loc(loc),
87 options(options), value_bounds(value_bounds),
88 n_stmt(0), n_test(0), partial(0), allow_nested(true),
89 nesting_enabled(false) { }
91 ~PetScan();
93 struct pet_scop *scan(clang::FunctionDecl *fd);
95 static __isl_give isl_val *extract_int(isl_ctx *ctx,
96 clang::IntegerLiteral *expr);
97 static __isl_give isl_val *extract_unsigned(isl_ctx *ctx,
98 const llvm::APInt &val);
99 private:
100 void assign(struct pet_expr *lhs, clang::Expr *rhs);
102 __isl_give isl_pw_aff *signed_overflow(__isl_take isl_pw_aff *pa,
103 unsigned width);
104 void insert_expression(__isl_take isl_pw_aff *expr);
105 struct pet_scop *scan(clang::Stmt *stmt);
107 struct pet_scop *scan_arrays(struct pet_scop *scop);
108 struct pet_array *extract_array(isl_ctx *ctx, clang::ValueDecl *decl,
109 lex_recorddecl_set *types);
110 struct pet_array *extract_array(isl_ctx *ctx,
111 std::vector<clang::ValueDecl *> decls,
112 lex_recorddecl_set *types);
113 struct pet_array *set_upper_bounds(struct pet_array *array,
114 const clang::Type *type, int pos);
116 struct pet_scop *extract_non_affine_condition(clang::Expr *cond,
117 int stmt_nr, __isl_take isl_multi_pw_aff *index);
119 struct pet_scop *extract_conditional_assignment(clang::IfStmt *stmt);
120 struct pet_scop *extract_non_affine_if(clang::Expr *cond,
121 struct pet_scop *scop_then, struct pet_scop *scop_else,
122 bool have_else, int stmt_id);
124 struct pet_scop *kill(clang::Stmt *stmt, struct pet_array *array);
126 struct pet_scop *extract(clang::Stmt *stmt,
127 bool skip_declarations = false);
128 struct pet_scop *extract(clang::StmtRange stmt_range, bool block,
129 bool skip_declarations);
130 struct pet_scop *extract(clang::IfStmt *stmt);
131 struct pet_scop *extract(clang::WhileStmt *stmt);
132 struct pet_scop *extract(clang::CompoundStmt *stmt,
133 bool skip_declarations = false);
134 struct pet_scop *extract(clang::LabelStmt *stmt);
135 struct pet_scop *extract(clang::ContinueStmt *stmt);
136 struct pet_scop *extract(clang::BreakStmt *stmt);
137 struct pet_scop *extract(clang::DeclStmt *expr);
139 struct pet_scop *update_scop_start_end(struct pet_scop *scop,
140 clang::SourceRange range, bool skip_semi);
141 struct pet_scop *extract(clang::Stmt *stmt, struct pet_expr *expr,
142 __isl_take isl_id *label = NULL);
143 struct pet_stmt *extract_kill(struct pet_scop *scop);
145 clang::BinaryOperator *initialization_assignment(clang::Stmt *init);
146 clang::Decl *initialization_declaration(clang::Stmt *init);
147 clang::ValueDecl *extract_induction_variable(clang::BinaryOperator *stmt);
148 clang::VarDecl *extract_induction_variable(clang::Stmt *init,
149 clang::Decl *stmt);
150 __isl_give isl_pw_aff *extract_unary_increment(clang::UnaryOperator *op,
151 clang::ValueDecl *iv);
152 __isl_give isl_pw_aff *extract_binary_increment(
153 clang::BinaryOperator *op,
154 clang::ValueDecl *iv);
155 __isl_give isl_pw_aff *extract_compound_increment(
156 clang::CompoundAssignOperator *op,
157 clang::ValueDecl *iv);
158 __isl_give isl_pw_aff *extract_increment(clang::ForStmt *stmt,
159 clang::ValueDecl *iv);
160 struct pet_scop *extract_for(clang::ForStmt *stmt);
161 struct pet_scop *extract_infinite_loop(clang::Stmt *body);
162 struct pet_scop *extract_infinite_for(clang::ForStmt *stmt);
163 struct pet_scop *extract_affine_while(__isl_take isl_pw_aff *pa,
164 clang::Stmt *body);
166 void mark_write(struct pet_expr *access);
167 struct pet_expr *extract_assume(clang::Expr *expr);
168 struct pet_expr *extract_argument(clang::FunctionDecl *fd, int pos,
169 clang::Expr *expr);
170 struct pet_expr *extract_expr(clang::Expr *expr);
171 struct pet_expr *extract_expr(clang::UnaryOperator *expr);
172 struct pet_expr *extract_expr(clang::BinaryOperator *expr);
173 struct pet_expr *extract_expr(clang::ImplicitCastExpr *expr);
174 struct pet_expr *extract_expr(clang::IntegerLiteral *expr);
175 struct pet_expr *extract_expr(clang::FloatingLiteral *expr);
176 struct pet_expr *extract_expr(clang::ParenExpr *expr);
177 struct pet_expr *extract_expr(clang::ConditionalOperator *expr);
178 struct pet_expr *extract_expr(clang::CallExpr *expr);
179 struct pet_expr *extract_expr(clang::CStyleCastExpr *expr);
181 int extract_nested(__isl_keep isl_space *space,
182 int n_arg, struct pet_expr **args,
183 std::map<int,int> &param2pos);
184 struct pet_expr *extract_nested(struct pet_expr *expr, int n,
185 std::map<int,int> &param2pos);
186 struct pet_stmt *extract_nested(struct pet_stmt *stmt, int n,
187 std::map<int,int> &param2pos);
188 struct pet_expr *resolve_nested(struct pet_expr *expr);
189 struct pet_scop *resolve_nested(struct pet_scop *scop);
190 struct pet_stmt *resolve_nested(struct pet_stmt *stmt);
191 struct pet_expr *extract_access_expr(clang::Expr *expr);
192 struct pet_expr *extract_access_expr(clang::ValueDecl *decl);
194 __isl_give isl_multi_pw_aff *extract_index(
195 clang::ArraySubscriptExpr *expr);
196 __isl_give isl_multi_pw_aff *extract_index(clang::Expr *expr);
197 __isl_give isl_multi_pw_aff *extract_index(
198 clang::ImplicitCastExpr *expr);
199 __isl_give isl_multi_pw_aff *extract_index(clang::DeclRefExpr *expr);
200 __isl_give isl_multi_pw_aff *extract_index(clang::ValueDecl *decl);
201 __isl_give isl_multi_pw_aff *extract_index(
202 clang::IntegerLiteral *expr);
203 __isl_give isl_multi_pw_aff *extract_index(clang::MemberExpr *expr);
205 __isl_give isl_val *extract_int(clang::Expr *expr);
206 __isl_give isl_val *extract_int(clang::ParenExpr *expr);
208 __isl_give isl_pw_aff *extract_affine_add(clang::BinaryOperator *expr);
209 __isl_give isl_pw_aff *extract_affine_div(clang::BinaryOperator *expr);
210 __isl_give isl_pw_aff *extract_affine_mod(clang::BinaryOperator *expr);
211 __isl_give isl_pw_aff *extract_affine_mul(clang::BinaryOperator *expr);
213 isl_pw_aff *nested_access(clang::Expr *expr);
215 __isl_give isl_pw_aff *try_extract_affine(clang::Expr *expr);
216 __isl_give isl_pw_aff *try_extract_affine_condition(clang::Expr *expr);
217 bool is_affine_condition(clang::Expr *expr);
218 __isl_give isl_pw_aff *try_extract_nested_condition(clang::Expr *expr);
219 bool is_nested_allowed(__isl_keep isl_pw_aff *pa, pet_scop *scop);
221 __isl_give isl_pw_aff *extract_affine(const llvm::APInt &val);
222 __isl_give isl_pw_aff *extract_affine(clang::Expr *expr);
223 __isl_give isl_pw_aff *extract_affine(clang::IntegerLiteral *expr);
224 __isl_give isl_pw_aff *extract_affine(clang::ImplicitCastExpr *expr);
225 __isl_give isl_pw_aff *extract_affine(clang::DeclRefExpr *expr);
226 __isl_give isl_pw_aff *extract_affine(clang::BinaryOperator *expr);
227 __isl_give isl_pw_aff *extract_affine(clang::UnaryOperator *expr);
228 __isl_give isl_pw_aff *extract_affine(clang::ParenExpr *expr);
229 __isl_give isl_pw_aff *extract_affine(clang::CallExpr *expr);
230 __isl_give isl_pw_aff *extract_affine(clang::ArraySubscriptExpr *expr);
231 __isl_give isl_pw_aff *extract_affine(clang::MemberExpr *expr);
232 __isl_give isl_pw_aff *extract_affine(clang::ConditionalOperator *expr);
234 __isl_give isl_pw_aff *extract_implicit_condition(clang::Expr *expr);
236 __isl_give isl_pw_aff *extract_condition(clang::UnaryOperator *expr);
237 __isl_give isl_pw_aff *extract_condition(clang::Expr *expr);
238 __isl_give isl_pw_aff *extract_comparison(clang::BinaryOperator *expr);
239 __isl_give isl_pw_aff *extract_comparison(clang::BinaryOperatorKind op,
240 clang::Expr *LHS, clang::Expr *RHS, clang::Stmt *comp);
241 __isl_give isl_pw_aff *extract_boolean(clang::BinaryOperator *expr);
242 __isl_give isl_pw_aff *extract_boolean(clang::UnaryOperator *expr);
244 void report(clang::Stmt *stmt, unsigned id);
245 void unsupported(clang::Stmt *stmt);
246 void report_prototype_required(clang::Stmt *stmt);
247 void report_missing_increment(clang::Stmt *stmt);