pet_scop_collect_arrays: only collect subfields of outer array once
[pet.git] / scan.h
blobdfbe3da77bd95f34e836c6e548a7eefdd36cef9c
1 #ifndef PET_SCAN_H
2 #define PET_SCAN_H
4 #include <set>
5 #include <map>
7 #include <clang/Basic/SourceManager.h>
8 #include <clang/AST/Decl.h>
9 #include <clang/AST/Stmt.h>
10 #include <clang/Lex/Preprocessor.h>
12 #include <isl/ctx.h>
13 #include <isl/map.h>
14 #include <isl/val.h>
16 #include "context.h"
17 #include "inliner.h"
18 #include "isl_id_to_pet_expr.h"
19 #include "loc.h"
20 #include "scop.h"
21 #include "summary.h"
22 #include "tree.h"
24 namespace clang {
26 #ifndef HAVE_STMTRANGE
27 /* StmtRange was replaced by iterator_range in more recent versions of clang.
28 * Implement a StmtRange in terms of this iterator_range if StmtRange
29 * is not available.
31 struct StmtRange : std::pair<StmtIterator,StmtIterator> {
32 StmtRange(const StmtIterator &begin, const StmtIterator &end) :
33 std::pair<StmtIterator,StmtIterator>(begin, end) {}
34 StmtRange(Stmt::child_range range) :
35 std::pair<StmtIterator,StmtIterator>(range.begin(),
36 range.end()) {}
38 #endif
42 /* The location of the scop, as delimited by scop and endscop
43 * pragmas by the user.
44 * "scop" and "endscop" are the source locations of the scop and
45 * endscop pragmas.
46 * "start_line" is the line number of the start position.
48 struct ScopLoc {
49 ScopLoc() : end(0) {}
51 clang::SourceLocation scop;
52 clang::SourceLocation endscop;
53 unsigned start_line;
54 unsigned start;
55 unsigned end;
58 /* The information extracted from a pragma pencil independent.
59 * We currently only keep track of the line number where
60 * the pragma appears.
62 struct Independent {
63 Independent(unsigned line) : line(line) {}
65 unsigned line;
68 /* Compare two TypeDecl pointers based on their names.
70 struct less_name {
71 bool operator()(const clang::TypeDecl *x,
72 const clang::TypeDecl *y) {
73 return x->getNameAsString().compare(y->getNameAsString()) < 0;
77 /* The PetTypes structure collects a set of RecordDecl and
78 * TypedefNameDecl pointers.
79 * The pointers are sorted using a fixed order. The actual order
80 * is not important, only that it is consistent across platforms.
82 struct PetTypes {
83 std::set<clang::RecordDecl *, less_name> records;
84 std::set<clang::TypedefNameDecl *, less_name> typedefs;
86 void insert(clang::RecordDecl *decl) {
87 records.insert(decl);
89 void insert(clang::TypedefNameDecl *decl) {
90 typedefs.insert(decl);
94 struct PetScan {
95 clang::Preprocessor &PP;
96 clang::ASTContext &ast_context;
97 /* The DeclContext of the function containing the scop.
99 clang::DeclContext *decl_context;
100 /* If autodetect is false, then loc contains the location
101 * of the scop to be extracted.
103 ScopLoc &loc;
104 isl_ctx *ctx;
105 pet_options *options;
106 /* Set if the pet_scop returned by an extract method only
107 * represents part of the input tree.
109 bool partial;
111 /* A cache of size expressions for array identifiers as computed
112 * by PetScan::get_array_size, or set by PetScan::set_array_size.
114 isl_id_to_pet_expr *id_size;
115 /* A cache of size expressions for array types as computed
116 * by PetScan::get_array_size.
118 std::map<const clang::Type *, pet_expr *> type_size;
120 /* A cache of funtion summaries for function declarations
121 * as extracted by PetScan::get_summary.
123 std::map<clang::FunctionDecl *, pet_function_summary *> summary_cache;
125 /* A union of mappings of the form
126 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
128 isl_union_map *value_bounds;
130 /* The line number of the previously considered Stmt. */
131 unsigned last_line;
132 /* The line number of the Stmt currently being considered. */
133 unsigned current_line;
134 /* Information about the independent pragmas in the source code. */
135 std::vector<Independent> &independent;
137 /* All variables that have already been declared
138 * in the current compound statement.
140 std::vector<clang::VarDecl *> declarations;
141 /* Sequence number of the next rename. */
142 int n_rename;
143 /* Have the declared names been collected? */
144 bool declared_names_collected;
145 /* The names of the variables declared in decl_context,
146 * if declared_names_collected is set.
148 std::set<std::string> declared_names;
149 /* A set of names known to be in use. */
150 std::set<std::string> used_names;
152 /* Sequence number of the next temporary inlined argument variable. */
153 int n_arg;
155 PetScan(clang::Preprocessor &PP, clang::ASTContext &ast_context,
156 clang::DeclContext *decl_context, ScopLoc &loc,
157 pet_options *options, __isl_take isl_union_map *value_bounds,
158 std::vector<Independent> &independent) :
159 PP(PP),
160 ast_context(ast_context), decl_context(decl_context), loc(loc),
161 ctx(isl_union_map_get_ctx(value_bounds)),
162 options(options), partial(false), value_bounds(value_bounds),
163 last_line(0), current_line(0),
164 independent(independent), n_rename(0),
165 declared_names_collected(false), n_arg(0) {
166 id_size = isl_id_to_pet_expr_alloc(ctx, 0);
169 ~PetScan();
171 struct pet_scop *scan(clang::FunctionDecl *fd);
173 static __isl_give isl_val *extract_int(isl_ctx *ctx,
174 clang::IntegerLiteral *expr);
175 __isl_give pet_expr *get_array_size(__isl_keep isl_id *id);
176 void set_array_size(__isl_take isl_id *id, __isl_take pet_expr *size);
177 struct pet_array *extract_array(__isl_keep isl_id *id,
178 PetTypes *types, __isl_keep pet_context *pc);
179 private:
180 void set_current_stmt(clang::Stmt *stmt);
181 bool is_current_stmt_marked_independent();
183 void collect_declared_names();
184 void add_new_used_names(const std::set<std::string> &used_names);
185 bool name_in_use(const std::string &name, clang::Decl *decl);
186 std::string generate_new_name(const std::string &name);
188 __isl_give pet_tree *add_kills(__isl_take pet_tree *tree,
189 std::set<clang::ValueDecl *> locals);
191 struct pet_scop *scan(clang::Stmt *stmt);
193 struct pet_scop *scan_arrays(struct pet_scop *scop,
194 __isl_keep pet_context *pc);
195 struct pet_array *extract_array(clang::ValueDecl *decl,
196 PetTypes *types, __isl_keep pet_context *pc);
197 struct pet_array *extract_array(__isl_keep isl_id_list *decls,
198 PetTypes *types, __isl_keep pet_context *pc);
199 __isl_give pet_expr *set_upper_bounds(__isl_take pet_expr *expr,
200 clang::QualType qt, int pos);
201 struct pet_array *set_upper_bounds(struct pet_array *array,
202 __isl_keep pet_context *pc);
203 int substitute_array_sizes(__isl_keep pet_tree *tree,
204 pet_substituter *substituter);
206 __isl_give pet_tree *insert_initial_declarations(
207 __isl_take pet_tree *tree, int n_decl,
208 clang::StmtRange stmt_range);
209 __isl_give pet_tree *extract(clang::Stmt *stmt,
210 bool skip_declarations = false);
211 __isl_give pet_tree *extract(clang::StmtRange stmt_range, bool block,
212 bool skip_declarations, clang::Stmt *parent);
213 __isl_give pet_tree *extract(clang::IfStmt *stmt);
214 __isl_give pet_tree *extract(clang::WhileStmt *stmt);
215 __isl_give pet_tree *extract(clang::CompoundStmt *stmt,
216 bool skip_declarations = false);
217 __isl_give pet_tree *extract(clang::LabelStmt *stmt);
218 __isl_give pet_tree *extract(clang::Decl *decl);
219 __isl_give pet_tree *extract(clang::DeclStmt *expr);
221 __isl_give pet_loc *construct_pet_loc(clang::SourceRange range,
222 bool skip_semi);
223 __isl_give pet_tree *extract(__isl_take pet_expr *expr,
224 clang::SourceRange range, bool skip_semi);
225 __isl_give pet_tree *update_loc(__isl_take pet_tree *tree,
226 clang::Stmt *stmt);
228 struct pet_scop *extract_scop(__isl_take pet_tree *tree);
230 clang::BinaryOperator *initialization_assignment(clang::Stmt *init);
231 clang::Decl *initialization_declaration(clang::Stmt *init);
232 clang::ValueDecl *extract_induction_variable(clang::BinaryOperator *stmt);
233 clang::VarDecl *extract_induction_variable(clang::Stmt *init,
234 clang::Decl *stmt);
235 __isl_give pet_expr *extract_unary_increment(clang::UnaryOperator *op,
236 clang::ValueDecl *iv);
237 __isl_give pet_expr *extract_binary_increment(
238 clang::BinaryOperator *op,
239 clang::ValueDecl *iv);
240 __isl_give pet_expr *extract_compound_increment(
241 clang::CompoundAssignOperator *op,
242 clang::ValueDecl *iv);
243 __isl_give pet_expr *extract_increment(clang::ForStmt *stmt,
244 clang::ValueDecl *iv);
245 __isl_give pet_tree *extract_for(clang::ForStmt *stmt);
246 __isl_give pet_tree *extract_expr_stmt(clang::Stmt *stmt);
247 __isl_give pet_tree *extract_inlined_call(clang::CallExpr *call,
248 clang::FunctionDecl *fd);
249 int set_inliner_arguments(pet_inliner &inliner, clang::CallExpr *call,
250 clang::FunctionDecl *fd);
252 __isl_give pet_expr *extract_assume(clang::Expr *expr);
253 __isl_give pet_function_summary *get_summary(clang::FunctionDecl *fd);
254 __isl_give pet_expr *set_summary(__isl_take pet_expr *expr,
255 clang::FunctionDecl *fd);
256 __isl_give pet_expr *extract_argument(clang::FunctionDecl *fd, int pos,
257 clang::Expr *expr, bool detect_writes);
258 __isl_give pet_expr *extract_expr(const llvm::APInt &val);
259 __isl_give pet_expr *extract_expr(clang::Expr *expr);
260 __isl_give pet_expr *extract_expr(clang::UnaryOperator *expr);
261 __isl_give pet_expr *extract_expr(clang::BinaryOperator *expr);
262 __isl_give pet_expr *extract_expr(clang::ImplicitCastExpr *expr);
263 __isl_give pet_expr *extract_expr(clang::IntegerLiteral *expr);
264 __isl_give pet_expr *extract_expr(clang::EnumConstantDecl *expr);
265 __isl_give pet_expr *extract_expr(clang::FloatingLiteral *expr);
266 __isl_give pet_expr *extract_expr(clang::ParenExpr *expr);
267 __isl_give pet_expr *extract_expr(clang::ConditionalOperator *expr);
268 __isl_give pet_expr *extract_expr(clang::CallExpr *expr);
269 __isl_give pet_expr *extract_expr(clang::CStyleCastExpr *expr);
271 __isl_give pet_expr *extract_access_expr(clang::QualType qt,
272 __isl_take pet_expr *index);
273 __isl_give pet_expr *extract_access_expr(clang::Expr *expr);
274 __isl_give pet_expr *extract_access_expr(clang::ValueDecl *decl);
276 __isl_give pet_expr *extract_index_expr(
277 clang::ArraySubscriptExpr *expr);
278 __isl_give pet_expr *extract_index_expr(clang::Expr *expr);
279 __isl_give pet_expr *extract_index_expr(clang::ImplicitCastExpr *expr);
280 __isl_give pet_expr *extract_index_expr(clang::DeclRefExpr *expr);
281 __isl_give pet_expr *extract_index_expr(clang::ValueDecl *decl);
282 __isl_give pet_expr *extract_index_expr(clang::MemberExpr *expr);
284 __isl_give isl_val *extract_int(clang::Expr *expr);
285 __isl_give isl_val *extract_int(clang::ParenExpr *expr);
287 clang::FunctionDecl *find_decl_from_name(clang::CallExpr *call,
288 std::string name);
289 clang::FunctionDecl *get_summary_function(clang::CallExpr *call);
291 void report(clang::SourceRange range, unsigned id);
292 void report(clang::Stmt *stmt, unsigned id);
293 void report(clang::Decl *decl, unsigned id);
294 void unsupported(clang::Stmt *stmt);
295 void report_unsupported_unary_operator(clang::Stmt *stmt);
296 void report_unsupported_binary_operator(clang::Stmt *stmt);
297 void report_unsupported_statement_type(clang::Stmt *stmt);
298 void report_prototype_required(clang::Stmt *stmt);
299 void report_missing_increment(clang::Stmt *stmt);
300 void report_missing_summary_function(clang::Stmt *stmt);
301 void report_missing_summary_function_body(clang::Stmt *stmt);
302 void report_unsupported_inline_function_argument(clang::Stmt *stmt);
303 void report_unsupported_declaration(clang::Decl *decl);
304 void report_unbalanced_pragmas(clang::SourceLocation scop,
305 clang::SourceLocation endscop);
308 #endif