link against isl libraries first
[pet.git] / scan.cc
blob3f69a36b81f5a5be36a6a68c5b2876d0cd40642b
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2015 Ecole Normale Superieure. All rights reserved.
4 * Copyright 2015-2017 Sven Verdoolaege. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
25 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation
31 * are those of the authors and should not be interpreted as
32 * representing official policies, either expressed or implied, of
33 * Leiden University.
34 */
36 #include "config.h"
38 #include <string.h>
39 #include <set>
40 #include <map>
41 #include <iostream>
42 #include <sstream>
43 #include <llvm/Support/raw_ostream.h>
44 #include <clang/AST/ASTContext.h>
45 #include <clang/AST/ASTDiagnostic.h>
46 #include <clang/AST/Attr.h>
47 #include <clang/AST/Expr.h>
48 #include <clang/AST/RecursiveASTVisitor.h>
50 #include <isl/id.h>
51 #include <isl/space.h>
52 #include <isl/aff.h>
53 #include <isl/set.h>
54 #include <isl/union_set.h>
56 #include "aff.h"
57 #include "array.h"
58 #include "clang_compatibility.h"
59 #include "clang.h"
60 #include "context.h"
61 #include "expr.h"
62 #include "expr_plus.h"
63 #include "id.h"
64 #include "inliner.h"
65 #include "inlined_calls.h"
66 #include "killed_locals.h"
67 #include "nest.h"
68 #include "options.h"
69 #include "scan.h"
70 #include "scop.h"
71 #include "scop_plus.h"
72 #include "substituter.h"
73 #include "tree.h"
74 #include "tree2scop.h"
76 using namespace std;
77 using namespace clang;
79 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
81 switch (kind) {
82 case UO_Minus:
83 return pet_op_minus;
84 case UO_Not:
85 return pet_op_not;
86 case UO_LNot:
87 return pet_op_lnot;
88 case UO_PostInc:
89 return pet_op_post_inc;
90 case UO_PostDec:
91 return pet_op_post_dec;
92 case UO_PreInc:
93 return pet_op_pre_inc;
94 case UO_PreDec:
95 return pet_op_pre_dec;
96 default:
97 return pet_op_last;
101 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
103 switch (kind) {
104 case BO_AddAssign:
105 return pet_op_add_assign;
106 case BO_SubAssign:
107 return pet_op_sub_assign;
108 case BO_MulAssign:
109 return pet_op_mul_assign;
110 case BO_DivAssign:
111 return pet_op_div_assign;
112 case BO_AndAssign:
113 return pet_op_and_assign;
114 case BO_XorAssign:
115 return pet_op_xor_assign;
116 case BO_OrAssign:
117 return pet_op_or_assign;
118 case BO_Assign:
119 return pet_op_assign;
120 case BO_Add:
121 return pet_op_add;
122 case BO_Sub:
123 return pet_op_sub;
124 case BO_Mul:
125 return pet_op_mul;
126 case BO_Div:
127 return pet_op_div;
128 case BO_Rem:
129 return pet_op_mod;
130 case BO_Shl:
131 return pet_op_shl;
132 case BO_Shr:
133 return pet_op_shr;
134 case BO_EQ:
135 return pet_op_eq;
136 case BO_NE:
137 return pet_op_ne;
138 case BO_LE:
139 return pet_op_le;
140 case BO_GE:
141 return pet_op_ge;
142 case BO_LT:
143 return pet_op_lt;
144 case BO_GT:
145 return pet_op_gt;
146 case BO_And:
147 return pet_op_and;
148 case BO_Xor:
149 return pet_op_xor;
150 case BO_Or:
151 return pet_op_or;
152 case BO_LAnd:
153 return pet_op_land;
154 case BO_LOr:
155 return pet_op_lor;
156 default:
157 return pet_op_last;
161 #ifdef GETTYPEINFORETURNSTYPEINFO
163 static int size_in_bytes(ASTContext &context, QualType type)
165 return context.getTypeInfo(type).Width / 8;
168 #else
170 static int size_in_bytes(ASTContext &context, QualType type)
172 return context.getTypeInfo(type).first / 8;
175 #endif
177 /* Check if the element type corresponding to the given array type
178 * has a const qualifier.
180 static bool const_base(QualType qt)
182 const Type *type = qt.getTypePtr();
184 if (type->isPointerType())
185 return const_base(type->getPointeeType());
186 if (type->isArrayType()) {
187 const ArrayType *atype;
188 type = type->getCanonicalTypeInternal().getTypePtr();
189 atype = cast<ArrayType>(type);
190 return const_base(atype->getElementType());
193 return qt.isConstQualified();
196 PetScan::~PetScan()
198 std::map<const Type *, pet_expr *>::iterator it;
199 std::map<FunctionDecl *, pet_function_summary *>::iterator it_s;
201 for (it = type_size.begin(); it != type_size.end(); ++it)
202 pet_expr_free(it->second);
203 pet_function_summary_free(no_summary);
204 for (it_s = summary_cache.begin(); it_s != summary_cache.end(); ++it_s)
205 pet_function_summary_free(it_s->second);
207 isl_id_to_pet_expr_free(id_size);
208 isl_union_map_free(value_bounds);
211 /* Report a diagnostic on the range "range", unless autodetect is set.
213 void PetScan::report(SourceRange range, unsigned id)
215 if (options->autodetect)
216 return;
218 SourceLocation loc = range.getBegin();
219 DiagnosticsEngine &diag = PP.getDiagnostics();
220 DiagnosticBuilder B = diag.Report(loc, id) << range;
223 /* Report a diagnostic on "stmt", unless autodetect is set.
225 void PetScan::report(Stmt *stmt, unsigned id)
227 report(stmt->getSourceRange(), id);
230 /* Report a diagnostic on "decl", unless autodetect is set.
232 void PetScan::report(Decl *decl, unsigned id)
234 report(decl->getSourceRange(), id);
237 /* Called if we found something we (currently) cannot handle.
238 * We'll provide more informative warnings later.
240 * We only actually complain if autodetect is false.
242 void PetScan::unsupported(Stmt *stmt)
244 DiagnosticsEngine &diag = PP.getDiagnostics();
245 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
246 "unsupported");
247 report(stmt, id);
250 /* Report an unsupported unary operator, unless autodetect is set.
252 void PetScan::report_unsupported_unary_operator(Stmt *stmt)
254 DiagnosticsEngine &diag = PP.getDiagnostics();
255 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
256 "this type of unary operator is not supported");
257 report(stmt, id);
260 /* Report an unsupported binary operator, unless autodetect is set.
262 void PetScan::report_unsupported_binary_operator(Stmt *stmt)
264 DiagnosticsEngine &diag = PP.getDiagnostics();
265 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
266 "this type of binary operator is not supported");
267 report(stmt, id);
270 /* Report an unsupported statement type, unless autodetect is set.
272 void PetScan::report_unsupported_statement_type(Stmt *stmt)
274 DiagnosticsEngine &diag = PP.getDiagnostics();
275 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
276 "this type of statement is not supported");
277 report(stmt, id);
280 /* Report a missing prototype, unless autodetect is set.
282 void PetScan::report_prototype_required(Stmt *stmt)
284 DiagnosticsEngine &diag = PP.getDiagnostics();
285 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
286 "prototype required");
287 report(stmt, id);
290 /* Report a missing increment, unless autodetect is set.
292 void PetScan::report_missing_increment(Stmt *stmt)
294 DiagnosticsEngine &diag = PP.getDiagnostics();
295 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
296 "missing increment");
297 report(stmt, id);
300 /* Report a missing summary function, unless autodetect is set.
302 void PetScan::report_missing_summary_function(Stmt *stmt)
304 DiagnosticsEngine &diag = PP.getDiagnostics();
305 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
306 "missing summary function");
307 report(stmt, id);
310 /* Report a missing summary function body, unless autodetect is set.
312 void PetScan::report_missing_summary_function_body(Stmt *stmt)
314 DiagnosticsEngine &diag = PP.getDiagnostics();
315 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
316 "missing summary function body");
317 report(stmt, id);
320 /* Report an unsupported argument in a call to an inlined function,
321 * unless autodetect is set.
323 void PetScan::report_unsupported_inline_function_argument(Stmt *stmt)
325 DiagnosticsEngine &diag = PP.getDiagnostics();
326 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
327 "unsupported inline function call argument");
328 report(stmt, id);
331 /* Report an unsupported type of declaration, unless autodetect is set.
333 void PetScan::report_unsupported_declaration(Decl *decl)
335 DiagnosticsEngine &diag = PP.getDiagnostics();
336 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
337 "unsupported declaration");
338 report(decl, id);
341 /* Report an unbalanced pair of scop/endscop pragmas, unless autodetect is set.
343 void PetScan::report_unbalanced_pragmas(SourceLocation scop,
344 SourceLocation endscop)
346 if (options->autodetect)
347 return;
349 DiagnosticsEngine &diag = PP.getDiagnostics();
351 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
352 "unbalanced endscop pragma");
353 DiagnosticBuilder B2 = diag.Report(endscop, id);
356 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Note,
357 "corresponding scop pragma");
358 DiagnosticBuilder B = diag.Report(scop, id);
362 /* Report a return statement in an unsupported context,
363 * unless autodetect is set.
365 void PetScan::report_unsupported_return(Stmt *stmt)
367 DiagnosticsEngine &diag = PP.getDiagnostics();
368 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
369 "return statements not supported in this context");
370 report(stmt, id);
373 /* Report a return statement that does not appear at the end of a function,
374 * unless autodetect is set.
376 void PetScan::report_return_not_at_end_of_function(Stmt *stmt)
378 DiagnosticsEngine &diag = PP.getDiagnostics();
379 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
380 "return statement must be final statement in function");
381 report(stmt, id);
384 /* Extract an integer from "val", which is assumed to be non-negative.
386 static __isl_give isl_val *extract_unsigned(isl_ctx *ctx,
387 const llvm::APInt &val)
389 unsigned n;
390 const uint64_t *data;
392 data = val.getRawData();
393 n = val.getNumWords();
394 return isl_val_int_from_chunks(ctx, n, sizeof(uint64_t), data);
397 /* Extract an integer from "val". If "is_signed" is set, then "val"
398 * is signed. Otherwise it it unsigned.
400 static __isl_give isl_val *extract_int(isl_ctx *ctx, bool is_signed,
401 llvm::APInt val)
403 int is_negative = is_signed && val.isNegative();
404 isl_val *v;
406 if (is_negative)
407 val = -val;
409 v = extract_unsigned(ctx, val);
411 if (is_negative)
412 v = isl_val_neg(v);
413 return v;
416 /* Extract an integer from "expr".
418 __isl_give isl_val *PetScan::extract_int(isl_ctx *ctx, IntegerLiteral *expr)
420 const Type *type = expr->getType().getTypePtr();
421 bool is_signed = type->hasSignedIntegerRepresentation();
423 return ::extract_int(ctx, is_signed, expr->getValue());
426 /* Extract an integer from "expr".
427 * Return NULL if "expr" does not (obviously) represent an integer.
429 __isl_give isl_val *PetScan::extract_int(clang::ParenExpr *expr)
431 return extract_int(expr->getSubExpr());
434 /* Extract an integer from "expr".
435 * Return NULL if "expr" does not (obviously) represent an integer.
437 __isl_give isl_val *PetScan::extract_int(clang::Expr *expr)
439 if (expr->getStmtClass() == Stmt::IntegerLiteralClass)
440 return extract_int(ctx, cast<IntegerLiteral>(expr));
441 if (expr->getStmtClass() == Stmt::ParenExprClass)
442 return extract_int(cast<ParenExpr>(expr));
444 unsupported(expr);
445 return NULL;
448 /* Extract a pet_expr from the APInt "val", which is assumed
449 * to be non-negative.
451 __isl_give pet_expr *PetScan::extract_expr(const llvm::APInt &val)
453 return pet_expr_new_int(extract_unsigned(ctx, val));
456 /* Return the number of bits needed to represent the type of "decl",
457 * if it is an integer type. Otherwise return 0.
458 * If qt is signed then return the opposite of the number of bits.
460 static int get_type_size(ValueDecl *decl)
462 return pet_clang_get_type_size(decl->getType(), decl->getASTContext());
465 /* Bound parameter "pos" of "set" to the possible values of "decl".
467 static __isl_give isl_set *set_parameter_bounds(__isl_take isl_set *set,
468 unsigned pos, ValueDecl *decl)
470 int type_size;
471 isl_ctx *ctx;
472 isl_val *bound;
474 ctx = isl_set_get_ctx(set);
475 type_size = get_type_size(decl);
476 if (type_size == 0)
477 isl_die(ctx, isl_error_invalid, "not an integer type",
478 return isl_set_free(set));
479 if (type_size > 0) {
480 set = isl_set_lower_bound_si(set, isl_dim_param, pos, 0);
481 bound = isl_val_int_from_ui(ctx, type_size);
482 bound = isl_val_2exp(bound);
483 bound = isl_val_sub_ui(bound, 1);
484 set = isl_set_upper_bound_val(set, isl_dim_param, pos, bound);
485 } else {
486 bound = isl_val_int_from_ui(ctx, -type_size - 1);
487 bound = isl_val_2exp(bound);
488 bound = isl_val_sub_ui(bound, 1);
489 set = isl_set_upper_bound_val(set, isl_dim_param, pos,
490 isl_val_copy(bound));
491 bound = isl_val_neg(bound);
492 bound = isl_val_sub_ui(bound, 1);
493 set = isl_set_lower_bound_val(set, isl_dim_param, pos, bound);
496 return set;
499 __isl_give pet_expr *PetScan::extract_index_expr(ImplicitCastExpr *expr)
501 return extract_index_expr(expr->getSubExpr());
504 /* Construct a pet_expr representing an index expression for an access
505 * to the variable referenced by "expr".
507 * If "expr" references an enum constant, then return an integer expression
508 * instead, representing the value of the enum constant.
510 __isl_give pet_expr *PetScan::extract_index_expr(DeclRefExpr *expr)
512 return extract_index_expr(expr->getDecl());
515 /* Construct a pet_expr representing an index expression for an access
516 * to the variable "decl".
518 * If "decl" is an enum constant, then we return an integer expression
519 * instead, representing the value of the enum constant.
521 __isl_give pet_expr *PetScan::extract_index_expr(ValueDecl *decl)
523 isl_id *id;
525 if (isa<EnumConstantDecl>(decl))
526 return extract_expr(cast<EnumConstantDecl>(decl));
528 id = pet_id_from_decl(ctx, decl);
529 return pet_id_create_index_expr(id);
532 /* Construct a pet_expr representing the index expression "expr"
533 * Return NULL on error.
535 * If "expr" is a reference to an enum constant, then return
536 * an integer expression instead, representing the value of the enum constant.
538 __isl_give pet_expr *PetScan::extract_index_expr(Expr *expr)
540 switch (expr->getStmtClass()) {
541 case Stmt::ImplicitCastExprClass:
542 return extract_index_expr(cast<ImplicitCastExpr>(expr));
543 case Stmt::DeclRefExprClass:
544 return extract_index_expr(cast<DeclRefExpr>(expr));
545 case Stmt::ArraySubscriptExprClass:
546 return extract_index_expr(cast<ArraySubscriptExpr>(expr));
547 case Stmt::IntegerLiteralClass:
548 return extract_expr(cast<IntegerLiteral>(expr));
549 case Stmt::MemberExprClass:
550 return extract_index_expr(cast<MemberExpr>(expr));
551 default:
552 unsupported(expr);
554 return NULL;
557 /* Extract an index expression from the given array subscript expression.
559 * We first extract an index expression from the base.
560 * This will result in an index expression with a range that corresponds
561 * to the earlier indices.
562 * We then extract the current index and let
563 * pet_expr_access_subscript combine the two.
565 __isl_give pet_expr *PetScan::extract_index_expr(ArraySubscriptExpr *expr)
567 Expr *base = expr->getBase();
568 Expr *idx = expr->getIdx();
569 pet_expr *index;
570 pet_expr *base_expr;
572 base_expr = extract_index_expr(base);
573 index = extract_expr(idx);
575 base_expr = pet_expr_access_subscript(base_expr, index);
577 return base_expr;
580 /* Extract an index expression from a member expression.
582 * If the base access (to the structure containing the member)
583 * is of the form
585 * A[..]
587 * and the member is called "f", then the member access is of
588 * the form
590 * A_f[A[..] -> f[]]
592 * If the member access is to an anonymous struct, then simply return
594 * A[..]
596 * If the member access in the source code is of the form
598 * A->f
600 * then it is treated as
602 * A[0].f
604 __isl_give pet_expr *PetScan::extract_index_expr(MemberExpr *expr)
606 Expr *base = expr->getBase();
607 FieldDecl *field = cast<FieldDecl>(expr->getMemberDecl());
608 pet_expr *base_index;
609 isl_id *id;
611 base_index = extract_index_expr(base);
613 if (expr->isArrow()) {
614 pet_expr *index = pet_expr_new_int(isl_val_zero(ctx));
615 base_index = pet_expr_access_subscript(base_index, index);
618 if (field->isAnonymousStructOrUnion())
619 return base_index;
621 id = pet_id_from_decl(ctx, field);
623 return pet_expr_access_member(base_index, id);
626 /* Mark the given access pet_expr as a write.
628 static __isl_give pet_expr *mark_write(__isl_take pet_expr *access)
630 access = pet_expr_access_set_write(access, 1);
631 access = pet_expr_access_set_read(access, 0);
633 return access;
636 /* Mark the given (read) access pet_expr as also possibly being written.
637 * That is, initialize the may write access relation from the may read relation
638 * and initialize the must write access relation to the empty relation.
640 static __isl_give pet_expr *mark_may_write(__isl_take pet_expr *expr)
642 isl_union_map *access;
643 isl_union_map *empty;
645 access = pet_expr_access_get_dependent_access(expr,
646 pet_expr_access_may_read);
647 empty = isl_union_map_empty(isl_union_map_get_space(access));
648 expr = pet_expr_access_set_access(expr, pet_expr_access_may_write,
649 access);
650 expr = pet_expr_access_set_access(expr, pet_expr_access_must_write,
651 empty);
653 return expr;
656 /* Construct a pet_expr representing a unary operator expression.
658 __isl_give pet_expr *PetScan::extract_expr(UnaryOperator *expr)
660 int type_size;
661 pet_expr *arg;
662 enum pet_op_type op;
664 op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
665 if (op == pet_op_last) {
666 report_unsupported_unary_operator(expr);
667 return NULL;
670 arg = extract_expr(expr->getSubExpr());
672 if (expr->isIncrementDecrementOp() &&
673 pet_expr_get_type(arg) == pet_expr_access) {
674 arg = mark_write(arg);
675 arg = pet_expr_access_set_read(arg, 1);
678 type_size = pet_clang_get_type_size(expr->getType(), ast_context);
679 return pet_expr_new_unary(type_size, op, arg);
682 /* Construct a pet_expr representing a binary operator expression.
684 * If the top level operator is an assignment and the LHS is an access,
685 * then we mark that access as a write. If the operator is a compound
686 * assignment, the access is marked as both a read and a write.
688 __isl_give pet_expr *PetScan::extract_expr(BinaryOperator *expr)
690 int type_size;
691 pet_expr *lhs, *rhs;
692 enum pet_op_type op;
694 op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
695 if (op == pet_op_last) {
696 report_unsupported_binary_operator(expr);
697 return NULL;
700 lhs = extract_expr(expr->getLHS());
701 rhs = extract_expr(expr->getRHS());
703 if (expr->isAssignmentOp() &&
704 pet_expr_get_type(lhs) == pet_expr_access) {
705 lhs = mark_write(lhs);
706 if (expr->isCompoundAssignmentOp())
707 lhs = pet_expr_access_set_read(lhs, 1);
710 type_size = pet_clang_get_type_size(expr->getType(), ast_context);
711 return pet_expr_new_binary(type_size, op, lhs, rhs);
714 /* Construct a pet_tree for a variable declaration and
715 * add the declaration to the list of declarations
716 * inside the current compound statement.
718 __isl_give pet_tree *PetScan::extract(Decl *decl)
720 VarDecl *vd;
721 pet_expr *lhs, *rhs;
722 pet_tree *tree;
724 if (!isa<VarDecl>(decl)) {
725 report_unsupported_declaration(decl);
726 return NULL;
729 vd = cast<VarDecl>(decl);
730 declarations.push_back(vd);
732 lhs = extract_access_expr(vd);
733 lhs = mark_write(lhs);
734 if (!vd->getInit())
735 tree = pet_tree_new_decl(lhs);
736 else {
737 rhs = extract_expr(vd->getInit());
738 tree = pet_tree_new_decl_init(lhs, rhs);
741 return tree;
744 /* Construct a pet_tree for a variable declaration statement.
745 * If the declaration statement declares multiple variables,
746 * then return a group of pet_trees, one for each declared variable.
748 __isl_give pet_tree *PetScan::extract(DeclStmt *stmt)
750 pet_tree *tree;
751 unsigned n;
753 if (!stmt->isSingleDecl()) {
754 const DeclGroup &group = stmt->getDeclGroup().getDeclGroup();
755 n = group.size();
756 tree = pet_tree_new_block(ctx, 0, n);
758 for (unsigned i = 0; i < n; ++i) {
759 pet_tree *tree_i;
760 pet_loc *loc;
762 tree_i = extract(group[i]);
763 loc = construct_pet_loc(group[i]->getSourceRange(),
764 false);
765 tree_i = pet_tree_set_loc(tree_i, loc);
766 tree = pet_tree_block_add_child(tree, tree_i);
769 return tree;
772 return extract(stmt->getSingleDecl());
775 /* Construct a pet_expr representing a conditional operation.
777 __isl_give pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
779 pet_expr *cond, *lhs, *rhs;
781 cond = extract_expr(expr->getCond());
782 lhs = extract_expr(expr->getTrueExpr());
783 rhs = extract_expr(expr->getFalseExpr());
785 return pet_expr_new_ternary(cond, lhs, rhs);
788 __isl_give pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
790 return extract_expr(expr->getSubExpr());
793 /* Construct a pet_expr representing a floating point value.
795 * If the floating point literal does not appear in a macro,
796 * then we use the original representation in the source code
797 * as the string representation. Otherwise, we use the pretty
798 * printer to produce a string representation.
800 __isl_give pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
802 double d;
803 string s;
804 const LangOptions &LO = PP.getLangOpts();
805 SourceLocation loc = expr->getLocation();
807 if (!loc.isMacroID()) {
808 SourceManager &SM = PP.getSourceManager();
809 unsigned len = Lexer::MeasureTokenLength(loc, SM, LO);
810 s = string(SM.getCharacterData(loc), len);
811 } else {
812 llvm::raw_string_ostream S(s);
813 expr->printPretty(S, 0, PrintingPolicy(LO));
814 S.str();
816 d = expr->getValueAsApproximateDouble();
817 return pet_expr_new_double(ctx, d, s.c_str());
820 /* Extract an index expression from "expr" and then convert it into
821 * an access pet_expr.
823 * If "expr" is a reference to an enum constant, then return
824 * an integer expression instead, representing the value of the enum constant.
826 __isl_give pet_expr *PetScan::extract_access_expr(Expr *expr)
828 pet_expr *index;
830 index = extract_index_expr(expr);
832 if (pet_expr_get_type(index) == pet_expr_int)
833 return index;
835 return pet_expr_access_from_index(expr->getType(), index, ast_context);
838 /* Extract an index expression from "decl" and then convert it into
839 * an access pet_expr.
841 __isl_give pet_expr *PetScan::extract_access_expr(ValueDecl *decl)
843 return pet_expr_access_from_index(decl->getType(),
844 extract_index_expr(decl), ast_context);
847 __isl_give pet_expr *PetScan::extract_expr(ParenExpr *expr)
849 return extract_expr(expr->getSubExpr());
852 /* Extract an assume statement from the argument "expr"
853 * of a __builtin_assume or __pencil_assume statement.
855 __isl_give pet_expr *PetScan::extract_assume(Expr *expr)
857 return pet_expr_new_unary(0, pet_op_assume, extract_expr(expr));
860 /* If "expr" is an address-of operator, then return its argument.
861 * Otherwise, return NULL.
863 static Expr *extract_addr_of_arg(Expr *expr)
865 UnaryOperator *op;
867 if (expr->getStmtClass() != Stmt::UnaryOperatorClass)
868 return NULL;
869 op = cast<UnaryOperator>(expr);
870 if (op->getOpcode() != UO_AddrOf)
871 return NULL;
872 return op->getSubExpr();
875 /* Construct a pet_expr corresponding to the function call argument "expr".
876 * The argument appears in position "pos" of a call to function "fd".
878 * If we are passing along a pointer to an array element
879 * or an entire row or even higher dimensional slice of an array,
880 * then the function being called may write into the array.
882 * We assume here that if the function is declared to take a pointer
883 * to a const type, then the function may only perform a read
884 * and that otherwise, it may either perform a read or a write (or both).
885 * We only perform this check if "detect_writes" is set.
887 __isl_give pet_expr *PetScan::extract_argument(FunctionDecl *fd, int pos,
888 Expr *expr, bool detect_writes)
890 Expr *arg;
891 pet_expr *res;
892 int is_addr = 0, is_partial = 0;
894 expr = pet_clang_strip_casts(expr);
895 arg = extract_addr_of_arg(expr);
896 if (arg) {
897 is_addr = 1;
898 expr = arg;
900 res = extract_expr(expr);
901 if (!res)
902 return NULL;
903 if (pet_clang_array_depth(expr->getType()) > 0)
904 is_partial = 1;
905 if (detect_writes && (is_addr || is_partial) &&
906 pet_expr_get_type(res) == pet_expr_access) {
907 ParmVarDecl *parm;
908 if (!fd->hasPrototype()) {
909 report_prototype_required(expr);
910 return pet_expr_free(res);
912 parm = fd->getParamDecl(pos);
913 if (!const_base(parm->getType()))
914 res = mark_may_write(res);
917 if (is_addr)
918 res = pet_expr_new_unary(0, pet_op_address_of, res);
919 return res;
922 /* Find the first FunctionDecl with the given name.
923 * "call" is the corresponding call expression and is only used
924 * for reporting errors.
926 * Return NULL on error.
928 FunctionDecl *PetScan::find_decl_from_name(CallExpr *call, string name)
930 TranslationUnitDecl *tu = ast_context.getTranslationUnitDecl();
931 DeclContext::decl_iterator begin = tu->decls_begin();
932 DeclContext::decl_iterator end = tu->decls_end();
933 for (DeclContext::decl_iterator i = begin; i != end; ++i) {
934 FunctionDecl *fd = dyn_cast<FunctionDecl>(*i);
935 if (!fd)
936 continue;
937 if (fd->getName().str().compare(name) != 0)
938 continue;
939 if (fd->hasBody())
940 return fd;
941 report_missing_summary_function_body(call);
942 return NULL;
944 report_missing_summary_function(call);
945 return NULL;
948 /* Return the FunctionDecl for the summary function associated to the
949 * function called by "call".
951 * In particular, if the pencil option is set, then
952 * search for an annotate attribute formatted as
953 * "pencil_access(name)", where "name" is the name of the summary function.
955 * If no summary function was specified, then return the FunctionDecl
956 * that is actually being called.
958 * Return NULL on error.
960 FunctionDecl *PetScan::get_summary_function(CallExpr *call)
962 FunctionDecl *decl = call->getDirectCallee();
963 if (!decl)
964 return NULL;
966 if (!options->pencil)
967 return decl;
969 specific_attr_iterator<AnnotateAttr> begin, end, i;
970 begin = decl->specific_attr_begin<AnnotateAttr>();
971 end = decl->specific_attr_end<AnnotateAttr>();
972 for (i = begin; i != end; ++i) {
973 string attr = (*i)->getAnnotation().str();
975 const char prefix[] = "pencil_access(";
976 size_t start = attr.find(prefix);
977 if (start == string::npos)
978 continue;
979 start += strlen(prefix);
980 string name = attr.substr(start, attr.find(')') - start);
982 return find_decl_from_name(call, name);
985 return decl;
988 /* Is "name" the name of an assume statement?
989 * "pencil" indicates whether pencil builtins and pragmas should be supported.
990 * "__builtin_assume" is always accepted.
991 * If "pencil" is set, then "__pencil_assume" is also accepted.
993 static bool is_assume(int pencil, const string &name)
995 if (name == "__builtin_assume")
996 return true;
997 return pencil && name == "__pencil_assume";
1000 /* Construct a pet_expr representing a function call.
1002 * If this->call2id is not NULL and it contains a mapping for this call,
1003 * then this means that the corresponding function has been inlined.
1004 * Return a pet_expr that reads from the variable that
1005 * stores the return value of the inlined call.
1007 * In the special case of a "call" to __builtin_assume or __pencil_assume,
1008 * construct an assume expression instead.
1010 * In the case of a "call" to __pencil_kill, the arguments
1011 * are neither read nor written (only killed), so there
1012 * is no need to check for writes to these arguments.
1014 * __pencil_assume and __pencil_kill are only recognized
1015 * when the pencil option is set.
1017 __isl_give pet_expr *PetScan::extract_expr(CallExpr *expr)
1019 pet_expr *res = NULL;
1020 FunctionDecl *fd;
1021 string name;
1022 unsigned n_arg;
1023 bool is_kill;
1025 if (call2id && call2id->find(expr) != call2id->end())
1026 return pet_expr_access_from_id(isl_id_copy(call2id[0][expr]),
1027 ast_context);
1029 fd = expr->getDirectCallee();
1030 if (!fd) {
1031 unsupported(expr);
1032 return NULL;
1035 name = fd->getDeclName().getAsString();
1036 n_arg = expr->getNumArgs();
1038 if (n_arg == 1 && is_assume(options->pencil, name))
1039 return extract_assume(expr->getArg(0));
1040 is_kill = options->pencil && name == "__pencil_kill";
1042 res = pet_expr_new_call(ctx, name.c_str(), n_arg);
1043 if (!res)
1044 return NULL;
1046 for (unsigned i = 0; i < n_arg; ++i) {
1047 Expr *arg = expr->getArg(i);
1048 res = pet_expr_set_arg(res, i,
1049 PetScan::extract_argument(fd, i, arg, !is_kill));
1052 fd = get_summary_function(expr);
1053 if (!fd)
1054 return pet_expr_free(res);
1056 res = set_summary(res, fd);
1058 return res;
1061 /* Construct a pet_expr representing a (C style) cast.
1063 __isl_give pet_expr *PetScan::extract_expr(CStyleCastExpr *expr)
1065 pet_expr *arg;
1066 QualType type;
1068 arg = extract_expr(expr->getSubExpr());
1069 if (!arg)
1070 return NULL;
1072 type = expr->getTypeAsWritten();
1073 return pet_expr_new_cast(type.getAsString().c_str(), arg);
1076 /* Construct a pet_expr representing an integer.
1078 __isl_give pet_expr *PetScan::extract_expr(IntegerLiteral *expr)
1080 return pet_expr_new_int(extract_int(expr));
1083 /* Construct a pet_expr representing the integer enum constant "ecd".
1085 __isl_give pet_expr *PetScan::extract_expr(EnumConstantDecl *ecd)
1087 isl_val *v;
1088 const llvm::APSInt &init = ecd->getInitVal();
1089 v = ::extract_int(ctx, init.isSigned(), init);
1090 return pet_expr_new_int(v);
1093 /* Try and construct a pet_expr representing "expr".
1095 __isl_give pet_expr *PetScan::extract_expr(Expr *expr)
1097 switch (expr->getStmtClass()) {
1098 case Stmt::UnaryOperatorClass:
1099 return extract_expr(cast<UnaryOperator>(expr));
1100 case Stmt::CompoundAssignOperatorClass:
1101 case Stmt::BinaryOperatorClass:
1102 return extract_expr(cast<BinaryOperator>(expr));
1103 case Stmt::ImplicitCastExprClass:
1104 return extract_expr(cast<ImplicitCastExpr>(expr));
1105 case Stmt::ArraySubscriptExprClass:
1106 case Stmt::DeclRefExprClass:
1107 case Stmt::MemberExprClass:
1108 return extract_access_expr(expr);
1109 case Stmt::IntegerLiteralClass:
1110 return extract_expr(cast<IntegerLiteral>(expr));
1111 case Stmt::FloatingLiteralClass:
1112 return extract_expr(cast<FloatingLiteral>(expr));
1113 case Stmt::ParenExprClass:
1114 return extract_expr(cast<ParenExpr>(expr));
1115 case Stmt::ConditionalOperatorClass:
1116 return extract_expr(cast<ConditionalOperator>(expr));
1117 case Stmt::CallExprClass:
1118 return extract_expr(cast<CallExpr>(expr));
1119 case Stmt::CStyleCastExprClass:
1120 return extract_expr(cast<CStyleCastExpr>(expr));
1121 default:
1122 unsupported(expr);
1124 return NULL;
1127 /* Check if the given initialization statement is an assignment.
1128 * If so, return that assignment. Otherwise return NULL.
1130 BinaryOperator *PetScan::initialization_assignment(Stmt *init)
1132 BinaryOperator *ass;
1134 if (init->getStmtClass() != Stmt::BinaryOperatorClass)
1135 return NULL;
1137 ass = cast<BinaryOperator>(init);
1138 if (ass->getOpcode() != BO_Assign)
1139 return NULL;
1141 return ass;
1144 /* Check if the given initialization statement is a declaration
1145 * of a single variable.
1146 * If so, return that declaration. Otherwise return NULL.
1148 Decl *PetScan::initialization_declaration(Stmt *init)
1150 DeclStmt *decl;
1152 if (init->getStmtClass() != Stmt::DeclStmtClass)
1153 return NULL;
1155 decl = cast<DeclStmt>(init);
1157 if (!decl->isSingleDecl())
1158 return NULL;
1160 return decl->getSingleDecl();
1163 /* Given the assignment operator in the initialization of a for loop,
1164 * extract the induction variable, i.e., the (integer)variable being
1165 * assigned.
1167 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1169 Expr *lhs;
1170 DeclRefExpr *ref;
1171 ValueDecl *decl;
1172 const Type *type;
1174 lhs = init->getLHS();
1175 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1176 unsupported(init);
1177 return NULL;
1180 ref = cast<DeclRefExpr>(lhs);
1181 decl = ref->getDecl();
1182 type = decl->getType().getTypePtr();
1184 if (!type->isIntegerType()) {
1185 unsupported(lhs);
1186 return NULL;
1189 return decl;
1192 /* Given the initialization statement of a for loop and the single
1193 * declaration in this initialization statement,
1194 * extract the induction variable, i.e., the (integer) variable being
1195 * declared.
1197 VarDecl *PetScan::extract_induction_variable(Stmt *init, Decl *decl)
1199 VarDecl *vd;
1201 vd = cast<VarDecl>(decl);
1203 const QualType type = vd->getType();
1204 if (!type->isIntegerType()) {
1205 unsupported(init);
1206 return NULL;
1209 if (!vd->getInit()) {
1210 unsupported(init);
1211 return NULL;
1214 return vd;
1217 /* Check that op is of the form iv++ or iv--.
1218 * Return a pet_expr representing "1" or "-1" accordingly.
1220 __isl_give pet_expr *PetScan::extract_unary_increment(
1221 clang::UnaryOperator *op, clang::ValueDecl *iv)
1223 Expr *sub;
1224 DeclRefExpr *ref;
1225 isl_val *v;
1227 if (!op->isIncrementDecrementOp()) {
1228 unsupported(op);
1229 return NULL;
1232 sub = op->getSubExpr();
1233 if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1234 unsupported(op);
1235 return NULL;
1238 ref = cast<DeclRefExpr>(sub);
1239 if (ref->getDecl() != iv) {
1240 unsupported(op);
1241 return NULL;
1244 if (op->isIncrementOp())
1245 v = isl_val_one(ctx);
1246 else
1247 v = isl_val_negone(ctx);
1249 return pet_expr_new_int(v);
1252 /* Check if op is of the form
1254 * iv = expr
1256 * and return the increment "expr - iv" as a pet_expr.
1258 __isl_give pet_expr *PetScan::extract_binary_increment(BinaryOperator *op,
1259 clang::ValueDecl *iv)
1261 int type_size;
1262 Expr *lhs;
1263 DeclRefExpr *ref;
1264 pet_expr *expr, *expr_iv;
1266 if (op->getOpcode() != BO_Assign) {
1267 unsupported(op);
1268 return NULL;
1271 lhs = op->getLHS();
1272 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1273 unsupported(op);
1274 return NULL;
1277 ref = cast<DeclRefExpr>(lhs);
1278 if (ref->getDecl() != iv) {
1279 unsupported(op);
1280 return NULL;
1283 expr = extract_expr(op->getRHS());
1284 expr_iv = extract_expr(lhs);
1286 type_size = pet_clang_get_type_size(iv->getType(), ast_context);
1287 return pet_expr_new_binary(type_size, pet_op_sub, expr, expr_iv);
1290 /* Check that op is of the form iv += cst or iv -= cst
1291 * and return a pet_expr corresponding to cst or -cst accordingly.
1293 __isl_give pet_expr *PetScan::extract_compound_increment(
1294 CompoundAssignOperator *op, clang::ValueDecl *iv)
1296 Expr *lhs;
1297 DeclRefExpr *ref;
1298 bool neg = false;
1299 pet_expr *expr;
1300 BinaryOperatorKind opcode;
1302 opcode = op->getOpcode();
1303 if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1304 unsupported(op);
1305 return NULL;
1307 if (opcode == BO_SubAssign)
1308 neg = true;
1310 lhs = op->getLHS();
1311 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1312 unsupported(op);
1313 return NULL;
1316 ref = cast<DeclRefExpr>(lhs);
1317 if (ref->getDecl() != iv) {
1318 unsupported(op);
1319 return NULL;
1322 expr = extract_expr(op->getRHS());
1323 if (neg) {
1324 int type_size;
1325 type_size = pet_clang_get_type_size(op->getType(), ast_context);
1326 expr = pet_expr_new_unary(type_size, pet_op_minus, expr);
1329 return expr;
1332 /* Check that the increment of the given for loop increments
1333 * (or decrements) the induction variable "iv" and return
1334 * the increment as a pet_expr if successful.
1336 __isl_give pet_expr *PetScan::extract_increment(clang::ForStmt *stmt,
1337 ValueDecl *iv)
1339 Stmt *inc = stmt->getInc();
1341 if (!inc) {
1342 report_missing_increment(stmt);
1343 return NULL;
1346 if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1347 return extract_unary_increment(cast<UnaryOperator>(inc), iv);
1348 if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1349 return extract_compound_increment(
1350 cast<CompoundAssignOperator>(inc), iv);
1351 if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1352 return extract_binary_increment(cast<BinaryOperator>(inc), iv);
1354 unsupported(inc);
1355 return NULL;
1358 /* Construct a pet_tree for a while loop.
1360 * If we were only able to extract part of the body, then simply
1361 * return that part.
1363 __isl_give pet_tree *PetScan::extract(WhileStmt *stmt)
1365 pet_expr *pe_cond;
1366 pet_tree *tree;
1368 tree = extract(stmt->getBody());
1369 if (partial)
1370 return tree;
1371 pe_cond = extract_expr(stmt->getCond());
1372 tree = pet_tree_new_while(pe_cond, tree);
1374 return tree;
1377 /* Construct a pet_tree for a for statement.
1378 * The for loop is required to be of one of the following forms
1380 * for (i = init; condition; ++i)
1381 * for (i = init; condition; --i)
1382 * for (i = init; condition; i += constant)
1383 * for (i = init; condition; i -= constant)
1385 * We extract a pet_tree for the body and then include it in a pet_tree
1386 * of type pet_tree_for.
1388 * As a special case, we also allow a for loop of the form
1390 * for (;;)
1392 * in which case we return a pet_tree of type pet_tree_infinite_loop.
1394 * If we were only able to extract part of the body, then simply
1395 * return that part.
1397 __isl_give pet_tree *PetScan::extract_for(ForStmt *stmt)
1399 BinaryOperator *ass;
1400 Decl *decl;
1401 Stmt *init;
1402 Expr *rhs;
1403 ValueDecl *iv;
1404 pet_tree *tree;
1405 int independent;
1406 int declared;
1407 pet_expr *pe_init, *pe_inc, *pe_iv, *pe_cond;
1409 independent = is_current_stmt_marked_independent();
1411 if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc()) {
1412 tree = extract(stmt->getBody());
1413 if (partial)
1414 return tree;
1415 tree = pet_tree_new_infinite_loop(tree);
1416 return tree;
1419 init = stmt->getInit();
1420 if (!init) {
1421 unsupported(stmt);
1422 return NULL;
1424 if ((ass = initialization_assignment(init)) != NULL) {
1425 iv = extract_induction_variable(ass);
1426 if (!iv)
1427 return NULL;
1428 rhs = ass->getRHS();
1429 } else if ((decl = initialization_declaration(init)) != NULL) {
1430 VarDecl *var = extract_induction_variable(init, decl);
1431 if (!var)
1432 return NULL;
1433 iv = var;
1434 rhs = var->getInit();
1435 } else {
1436 unsupported(stmt->getInit());
1437 return NULL;
1440 declared = !initialization_assignment(stmt->getInit());
1441 tree = extract(stmt->getBody());
1442 if (partial)
1443 return tree;
1444 pe_iv = extract_access_expr(iv);
1445 pe_iv = mark_write(pe_iv);
1446 pe_init = extract_expr(rhs);
1447 if (!stmt->getCond())
1448 pe_cond = pet_expr_new_int(isl_val_one(ctx));
1449 else
1450 pe_cond = extract_expr(stmt->getCond());
1451 pe_inc = extract_increment(stmt, iv);
1452 tree = pet_tree_new_for(independent, declared, pe_iv, pe_init, pe_cond,
1453 pe_inc, tree);
1454 return tree;
1457 /* Store the names of the variables declared in decl_context
1458 * in the set declared_names. Make sure to only do this once by
1459 * setting declared_names_collected.
1461 void PetScan::collect_declared_names()
1463 DeclContext *DC = decl_context;
1464 DeclContext::decl_iterator it;
1466 if (declared_names_collected)
1467 return;
1469 for (it = DC->decls_begin(); it != DC->decls_end(); ++it) {
1470 Decl *D = *it;
1471 NamedDecl *named;
1473 if (!isa<NamedDecl>(D))
1474 continue;
1475 named = cast<NamedDecl>(D);
1476 declared_names.insert(named->getName().str());
1479 declared_names_collected = true;
1482 /* Add the names in "names" that are not also in this->declared_names
1483 * to this->used_names.
1484 * It is up to the caller to make sure that declared_names has been
1485 * populated, if needed.
1487 void PetScan::add_new_used_names(const std::set<std::string> &names)
1489 std::set<std::string>::const_iterator it;
1491 for (it = names.begin(); it != names.end(); ++it) {
1492 if (declared_names.find(*it) != declared_names.end())
1493 continue;
1494 used_names.insert(*it);
1498 /* Is the name "name" used in any declaration other than "decl"?
1500 * If the name was found to be in use before, the consider it to be in use.
1501 * Otherwise, check the DeclContext of the function containing the scop
1502 * as well as all ancestors of this DeclContext for declarations
1503 * other than "decl" that declare something called "name".
1505 bool PetScan::name_in_use(const string &name, Decl *decl)
1507 DeclContext *DC;
1508 DeclContext::decl_iterator it;
1510 if (used_names.find(name) != used_names.end())
1511 return true;
1513 for (DC = decl_context; DC; DC = DC->getParent()) {
1514 for (it = DC->decls_begin(); it != DC->decls_end(); ++it) {
1515 Decl *D = *it;
1516 NamedDecl *named;
1518 if (D == decl)
1519 continue;
1520 if (!isa<NamedDecl>(D))
1521 continue;
1522 named = cast<NamedDecl>(D);
1523 if (named->getName().str() == name)
1524 return true;
1528 return false;
1531 /* Generate a new name based on "name" that is not in use.
1532 * Do so by adding a suffix _i, with i an integer.
1534 string PetScan::generate_new_name(const string &name)
1536 string new_name;
1538 do {
1539 std::ostringstream oss;
1540 oss << name << "_" << n_rename++;
1541 new_name = oss.str();
1542 } while (name_in_use(new_name, NULL));
1544 return new_name;
1547 /* Try and construct a pet_tree corresponding to a compound statement.
1549 * "skip_declarations" is set if we should skip initial declarations
1550 * in the children of the compound statements.
1552 * Collect a new set of declarations for the current compound statement.
1553 * If any of the names in these declarations is also used by another
1554 * declaration reachable from the current function, then rename it
1555 * to a name that is not already in use.
1556 * In particular, keep track of the old and new names in a pet_substituter
1557 * and apply the substitutions to the pet_tree corresponding to the
1558 * compound statement.
1560 __isl_give pet_tree *PetScan::extract(CompoundStmt *stmt,
1561 bool skip_declarations)
1563 pet_tree *tree;
1564 std::vector<VarDecl *> saved_declarations;
1565 std::vector<VarDecl *>::iterator it;
1566 pet_substituter substituter;
1568 saved_declarations = declarations;
1569 declarations.clear();
1570 tree = extract(stmt->children(), true, skip_declarations, stmt);
1571 for (it = declarations.begin(); it != declarations.end(); ++it) {
1572 isl_id *id;
1573 pet_expr *expr;
1574 VarDecl *decl = *it;
1575 string name = decl->getName().str();
1576 bool in_use = name_in_use(name, decl);
1578 used_names.insert(name);
1579 if (!in_use)
1580 continue;
1582 name = generate_new_name(name);
1583 id = pet_id_from_name_and_decl(ctx, name.c_str(), decl);
1584 expr = pet_expr_access_from_id(id, ast_context);
1585 id = pet_id_from_decl(ctx, decl);
1586 substituter.add_sub(id, expr);
1587 used_names.insert(name);
1589 tree = substituter.substitute(tree);
1590 declarations = saved_declarations;
1592 return tree;
1595 /* Return the file offset of the expansion location of "Loc".
1597 static unsigned getExpansionOffset(SourceManager &SM, SourceLocation Loc)
1599 return SM.getFileOffset(SM.getExpansionLoc(Loc));
1602 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
1604 /* Return a SourceLocation for the location after the first semicolon
1605 * after "loc". If Lexer::findLocationAfterToken is available, we simply
1606 * call it and also skip trailing spaces and newline.
1608 static SourceLocation location_after_semi(SourceLocation loc, SourceManager &SM,
1609 const LangOptions &LO)
1611 return Lexer::findLocationAfterToken(loc, tok::semi, SM, LO, true);
1614 #else
1616 /* Return a SourceLocation for the location after the first semicolon
1617 * after "loc". If Lexer::findLocationAfterToken is not available,
1618 * we look in the underlying character data for the first semicolon.
1620 static SourceLocation location_after_semi(SourceLocation loc, SourceManager &SM,
1621 const LangOptions &LO)
1623 const char *semi;
1624 const char *s = SM.getCharacterData(loc);
1626 semi = strchr(s, ';');
1627 if (!semi)
1628 return SourceLocation();
1629 return loc.getFileLocWithOffset(semi + 1 - s);
1632 #endif
1634 /* If the token at "loc" is the first token on the line, then return
1635 * a location referring to the start of the line and set *indent
1636 * to the indentation of "loc"
1637 * Otherwise, return "loc" and set *indent to "".
1639 * This function is used to extend a scop to the start of the line
1640 * if the first token of the scop is also the first token on the line.
1642 * We look for the first token on the line. If its location is equal to "loc",
1643 * then the latter is the location of the first token on the line.
1645 static SourceLocation move_to_start_of_line_if_first_token(SourceLocation loc,
1646 SourceManager &SM, const LangOptions &LO, char **indent)
1648 std::pair<FileID, unsigned> file_offset_pair;
1649 llvm::StringRef file;
1650 const char *pos;
1651 Token tok;
1652 SourceLocation token_loc, line_loc;
1653 int col;
1654 const char *s;
1656 loc = SM.getExpansionLoc(loc);
1657 col = SM.getExpansionColumnNumber(loc);
1658 line_loc = loc.getLocWithOffset(1 - col);
1659 file_offset_pair = SM.getDecomposedLoc(line_loc);
1660 file = SM.getBufferData(file_offset_pair.first, NULL);
1661 pos = file.data() + file_offset_pair.second;
1663 Lexer lexer(SM.getLocForStartOfFile(file_offset_pair.first), LO,
1664 file.begin(), pos, file.end());
1665 lexer.LexFromRawLexer(tok);
1666 token_loc = tok.getLocation();
1668 s = SM.getCharacterData(line_loc);
1669 *indent = strndup(s, token_loc == loc ? col - 1 : 0);
1671 if (token_loc == loc)
1672 return line_loc;
1673 else
1674 return loc;
1677 /* Construct a pet_loc corresponding to the region covered by "range".
1678 * If "skip_semi" is set, then we assume "range" is followed by
1679 * a semicolon and also include this semicolon.
1681 __isl_give pet_loc *PetScan::construct_pet_loc(SourceRange range,
1682 bool skip_semi)
1684 SourceLocation loc = range.getBegin();
1685 SourceManager &SM = PP.getSourceManager();
1686 const LangOptions &LO = PP.getLangOpts();
1687 int line = PP.getSourceManager().getExpansionLineNumber(loc);
1688 unsigned start, end;
1689 char *indent;
1691 loc = move_to_start_of_line_if_first_token(loc, SM, LO, &indent);
1692 start = getExpansionOffset(SM, loc);
1693 loc = range.getEnd();
1694 if (skip_semi)
1695 loc = location_after_semi(loc, SM, LO);
1696 else
1697 loc = PP.getLocForEndOfToken(loc);
1698 end = getExpansionOffset(SM, loc);
1700 return pet_loc_alloc(ctx, start, end, line, indent);
1703 /* Convert a top-level pet_expr to an expression pet_tree.
1705 __isl_give pet_tree *PetScan::extract(__isl_take pet_expr *expr,
1706 SourceRange range, bool skip_semi)
1708 pet_loc *loc;
1709 pet_tree *tree;
1711 tree = pet_tree_new_expr(expr);
1712 loc = construct_pet_loc(range, skip_semi);
1713 tree = pet_tree_set_loc(tree, loc);
1715 return tree;
1718 /* Construct a pet_tree for an if statement.
1720 __isl_give pet_tree *PetScan::extract(IfStmt *stmt)
1722 pet_expr *pe_cond;
1723 pet_tree *tree, *tree_else;
1725 pe_cond = extract_expr(stmt->getCond());
1726 tree = extract(stmt->getThen());
1727 if (stmt->getElse()) {
1728 tree_else = extract(stmt->getElse());
1729 if (options->autodetect) {
1730 if (tree && !tree_else) {
1731 partial = true;
1732 pet_expr_free(pe_cond);
1733 return tree;
1735 if (!tree && tree_else) {
1736 partial = true;
1737 pet_expr_free(pe_cond);
1738 return tree_else;
1741 tree = pet_tree_new_if_else(pe_cond, tree, tree_else);
1742 } else
1743 tree = pet_tree_new_if(pe_cond, tree);
1744 return tree;
1747 /* Is "parent" a compound statement that has "stmt" as its final child?
1749 static bool final_in_compound(ReturnStmt *stmt, Stmt *parent)
1751 CompoundStmt *c;
1753 c = dyn_cast<CompoundStmt>(parent);
1754 if (c) {
1755 StmtIterator i;
1756 Stmt *last;
1757 StmtRange range = c->children();
1759 for (i = range.first; i != range.second; ++i)
1760 last = *i;
1761 return last == stmt;
1763 return false;
1766 /* Try and construct a pet_tree for a return statement "stmt".
1768 * Return statements are only allowed in a context where
1769 * this->return_root has been set.
1770 * Furthermore, "stmt" should appear as the last child
1771 * in the compound statement this->return_root.
1773 __isl_give pet_tree *PetScan::extract(ReturnStmt *stmt)
1775 pet_expr *val;
1777 if (!return_root) {
1778 report_unsupported_return(stmt);
1779 return NULL;
1781 if (!final_in_compound(stmt, return_root)) {
1782 report_return_not_at_end_of_function(stmt);
1783 return NULL;
1786 val = extract_expr(stmt->getRetValue());
1787 return pet_tree_new_return(val);
1790 /* Try and construct a pet_tree for a label statement.
1792 __isl_give pet_tree *PetScan::extract(LabelStmt *stmt)
1794 isl_id *label;
1795 pet_tree *tree;
1797 label = isl_id_alloc(ctx, stmt->getName(), NULL);
1799 tree = extract(stmt->getSubStmt());
1800 tree = pet_tree_set_label(tree, label);
1801 return tree;
1804 /* Update the location of "tree" to include the source range of "stmt".
1806 * Actually, we create a new location based on the source range of "stmt" and
1807 * then extend this new location to include the region of the original location.
1808 * This ensures that the line number of the final location refers to "stmt".
1810 __isl_give pet_tree *PetScan::update_loc(__isl_take pet_tree *tree, Stmt *stmt)
1812 pet_loc *loc, *tree_loc;
1814 tree_loc = pet_tree_get_loc(tree);
1815 loc = construct_pet_loc(stmt->getSourceRange(), false);
1816 loc = pet_loc_update_start_end_from_loc(loc, tree_loc);
1817 pet_loc_free(tree_loc);
1819 tree = pet_tree_set_loc(tree, loc);
1820 return tree;
1823 /* Is "expr" of a type that can be converted to an access expression?
1825 static bool is_access_expr_type(Expr *expr)
1827 switch (expr->getStmtClass()) {
1828 case Stmt::ArraySubscriptExprClass:
1829 case Stmt::DeclRefExprClass:
1830 case Stmt::MemberExprClass:
1831 return true;
1832 default:
1833 return false;
1837 /* Tell the pet_inliner "inliner" about the formal arguments
1838 * in "fd" and the corresponding actual arguments in "call".
1839 * Return 0 if this was successful and -1 otherwise.
1841 * Any pointer argument is treated as an array.
1842 * The other arguments are treated as scalars.
1844 * In case of scalars, there is no restriction on the actual argument.
1845 * This actual argument is assigned to a variable with a name
1846 * that is derived from the name of the corresponding formal argument,
1847 * but made not to conflict with any variable names that are
1848 * already in use.
1850 * In case of arrays, the actual argument needs to be an expression
1851 * of a type that can be converted to an access expression or the address
1852 * of such an expression, ignoring implicit and redundant casts.
1854 int PetScan::set_inliner_arguments(pet_inliner &inliner, CallExpr *call,
1855 FunctionDecl *fd)
1857 unsigned n;
1859 n = fd->getNumParams();
1860 for (unsigned i = 0; i < n; ++i) {
1861 ParmVarDecl *parm = fd->getParamDecl(i);
1862 QualType type = parm->getType();
1863 Expr *arg, *sub;
1864 pet_expr *expr;
1865 int is_addr = 0;
1867 arg = call->getArg(i);
1868 if (pet_clang_array_depth(type) == 0) {
1869 string name = parm->getName().str();
1870 if (name_in_use(name, NULL))
1871 name = generate_new_name(name);
1872 used_names.insert(name);
1873 inliner.add_scalar_arg(parm, name, extract_expr(arg));
1874 continue;
1876 arg = pet_clang_strip_casts(arg);
1877 sub = extract_addr_of_arg(arg);
1878 if (sub) {
1879 is_addr = 1;
1880 arg = pet_clang_strip_casts(sub);
1882 if (!is_access_expr_type(arg)) {
1883 report_unsupported_inline_function_argument(arg);
1884 return -1;
1886 expr = extract_access_expr(arg);
1887 if (!expr)
1888 return -1;
1889 inliner.add_array_arg(parm, expr, is_addr);
1892 return 0;
1895 /* Internal data structure for PetScan::substitute_array_sizes.
1896 * ps is the PetScan on which the method was called.
1897 * substituter is the substituter that is used to substitute variables
1898 * in the size expressions.
1900 struct pet_substitute_array_sizes_data {
1901 PetScan *ps;
1902 pet_substituter *substituter;
1905 extern "C" {
1906 static int substitute_array_size(__isl_keep pet_tree *tree, void *user);
1909 /* If "tree" is a declaration, then perform the substitutions
1910 * in data->substituter on its size expression and store the result
1911 * in the size expression cache of data->ps such that the modified expression
1912 * will be used in subsequent calls to get_array_size.
1914 static int substitute_array_size(__isl_keep pet_tree *tree, void *user)
1916 struct pet_substitute_array_sizes_data *data;
1917 isl_id *id;
1918 pet_expr *var, *size;
1920 if (!pet_tree_is_decl(tree))
1921 return 0;
1923 data = (struct pet_substitute_array_sizes_data *) user;
1924 var = pet_tree_decl_get_var(tree);
1925 id = pet_expr_access_get_id(var);
1926 pet_expr_free(var);
1928 size = data->ps->get_array_size(id);
1929 size = data->substituter->substitute(size);
1930 data->ps->set_array_size(id, size);
1932 return 0;
1935 /* Perform the substitutions in "substituter" on all the arrays declared
1936 * inside "tree" and store the results in the size expression cache
1937 * such that the modified expressions will be used in subsequent calls
1938 * to get_array_size.
1940 int PetScan::substitute_array_sizes(__isl_keep pet_tree *tree,
1941 pet_substituter *substituter)
1943 struct pet_substitute_array_sizes_data data = { this, substituter };
1945 return pet_tree_foreach_sub_tree(tree, &substitute_array_size, &data);
1948 /* Try and construct a pet_tree from the body of "fd" using the actual
1949 * arguments in "call" in place of the formal arguments.
1950 * "fd" is assumed to point to the declaration with a function body.
1951 * In particular, construct a block that consists of assignments
1952 * of (parts of) the actual arguments to temporary variables
1953 * followed by the inlined function body with the formal arguments
1954 * replaced by (expressions containing) these temporary variables.
1955 * If "return_id" is set, then it is used to store the return value
1956 * of the inlined function.
1958 * The actual inlining is taken care of by the pet_inliner object.
1959 * This function merely calls set_inliner_arguments to tell
1960 * the pet_inliner about the actual arguments, extracts a pet_tree
1961 * from the body of the called function and then passes this pet_tree
1962 * to the pet_inliner.
1963 * The body of the called function is allowed to have a return statement
1964 * at the end.
1965 * The substitutions performed by the inliner are also applied
1966 * to the size expressions of the arrays declared in the inlined
1967 * function. These size expressions are not stored in the tree
1968 * itself, but rather in the size expression cache.
1970 * During the extraction of the function body, all variables names
1971 * that are declared in the calling function as well all variable
1972 * names that are known to be in use are considered to be in use
1973 * in the called function to ensure that there is no naming conflict.
1974 * Similarly, the additional names that are in use in the called function
1975 * are considered to be in use in the calling function as well.
1977 * The location of the pet_tree is reset to the call site to ensure
1978 * that the extent of the scop does not include the body of the called
1979 * function.
1981 __isl_give pet_tree *PetScan::extract_inlined_call(CallExpr *call,
1982 FunctionDecl *fd, __isl_keep isl_id *return_id)
1984 int save_autodetect;
1985 pet_tree *tree;
1986 pet_loc *tree_loc;
1987 pet_inliner inliner(ctx, n_arg, ast_context);
1989 if (set_inliner_arguments(inliner, call, fd) < 0)
1990 return NULL;
1992 save_autodetect = options->autodetect;
1993 options->autodetect = 0;
1994 PetScan body_scan(PP, ast_context, fd, loc, options,
1995 isl_union_map_copy(value_bounds), independent);
1996 collect_declared_names();
1997 body_scan.add_new_used_names(declared_names);
1998 body_scan.add_new_used_names(used_names);
1999 body_scan.return_root = fd->getBody();
2000 tree = body_scan.extract(fd->getBody(), false);
2001 add_new_used_names(body_scan.used_names);
2002 options->autodetect = save_autodetect;
2004 tree_loc = construct_pet_loc(call->getSourceRange(), true);
2005 tree = pet_tree_set_loc(tree, tree_loc);
2007 substitute_array_sizes(tree, &inliner);
2009 return inliner.inline_tree(tree, return_id);
2012 /* Try and construct a pet_tree corresponding
2013 * to the expression statement "stmt".
2015 * First look for function calls that have corresponding bodies
2016 * marked "inline". Extract the inlined functions in a pet_inlined_calls
2017 * object. Then extract the statement itself, replacing calls
2018 * to inlined function by accesses to the corresponding return variables, and
2019 * return the combined result.
2020 * If the outer expression is itself a call to an inlined function,
2021 * then it already appears as one of the inlined functions and
2022 * no separate pet_tree needs to be extracted for "stmt" itself.
2024 __isl_give pet_tree *PetScan::extract_expr_stmt(Stmt *stmt)
2026 pet_expr *expr;
2027 pet_tree *tree;
2028 pet_inlined_calls ic(this);
2030 ic.collect(stmt);
2031 if (ic.calls.size() >= 1 && ic.calls[0] == stmt) {
2032 tree = pet_tree_new_block(ctx, 0, 0);
2033 } else {
2034 call2id = &ic.call2id;
2035 expr = extract_expr(cast<Expr>(stmt));
2036 tree = extract(expr, stmt->getSourceRange(), true);
2037 call2id = NULL;
2039 tree = ic.add_inlined(tree);
2040 return tree;
2043 /* Try and construct a pet_tree corresponding to "stmt".
2045 * If "stmt" is a compound statement, then "skip_declarations"
2046 * indicates whether we should skip initial declarations in the
2047 * compound statement.
2049 * If the constructed pet_tree is not a (possibly) partial representation
2050 * of "stmt", we update start and end of the pet_scop to those of "stmt".
2051 * In particular, if skip_declarations is set, then we may have skipped
2052 * declarations inside "stmt" and so the pet_scop may not represent
2053 * the entire "stmt".
2054 * Note that this function may be called with "stmt" referring to the entire
2055 * body of the function, including the outer braces. In such cases,
2056 * skip_declarations will be set and the braces will not be taken into
2057 * account in tree->loc.
2059 __isl_give pet_tree *PetScan::extract(Stmt *stmt, bool skip_declarations)
2061 pet_tree *tree;
2063 set_current_stmt(stmt);
2065 if (isa<Expr>(stmt))
2066 return extract_expr_stmt(cast<Expr>(stmt));
2068 switch (stmt->getStmtClass()) {
2069 case Stmt::WhileStmtClass:
2070 tree = extract(cast<WhileStmt>(stmt));
2071 break;
2072 case Stmt::ForStmtClass:
2073 tree = extract_for(cast<ForStmt>(stmt));
2074 break;
2075 case Stmt::IfStmtClass:
2076 tree = extract(cast<IfStmt>(stmt));
2077 break;
2078 case Stmt::CompoundStmtClass:
2079 tree = extract(cast<CompoundStmt>(stmt), skip_declarations);
2080 break;
2081 case Stmt::LabelStmtClass:
2082 tree = extract(cast<LabelStmt>(stmt));
2083 break;
2084 case Stmt::ContinueStmtClass:
2085 tree = pet_tree_new_continue(ctx);
2086 break;
2087 case Stmt::BreakStmtClass:
2088 tree = pet_tree_new_break(ctx);
2089 break;
2090 case Stmt::DeclStmtClass:
2091 tree = extract(cast<DeclStmt>(stmt));
2092 break;
2093 case Stmt::NullStmtClass:
2094 tree = pet_tree_new_block(ctx, 0, 0);
2095 break;
2096 case Stmt::ReturnStmtClass:
2097 tree = extract(cast<ReturnStmt>(stmt));
2098 break;
2099 default:
2100 report_unsupported_statement_type(stmt);
2101 return NULL;
2104 if (partial || skip_declarations)
2105 return tree;
2107 return update_loc(tree, stmt);
2110 /* Given a sequence of statements "stmt_range" of which the first "n_decl"
2111 * are declarations and of which the remaining statements are represented
2112 * by "tree", try and extend "tree" to include the last sequence of
2113 * the initial declarations that can be completely extracted.
2115 * We start collecting the initial declarations and start over
2116 * whenever we come across a declaration that we cannot extract.
2117 * If we have been able to extract any declarations, then we
2118 * copy over the contents of "tree" at the end of the declarations.
2119 * Otherwise, we simply return the original "tree".
2121 __isl_give pet_tree *PetScan::insert_initial_declarations(
2122 __isl_take pet_tree *tree, int n_decl, StmtRange stmt_range)
2124 StmtIterator i;
2125 pet_tree *res;
2126 int n_stmt;
2127 int is_block;
2128 int j;
2130 n_stmt = pet_tree_block_n_child(tree);
2131 is_block = pet_tree_block_get_block(tree);
2132 res = pet_tree_new_block(ctx, is_block, n_decl + n_stmt);
2134 for (i = stmt_range.first; n_decl; ++i, --n_decl) {
2135 Stmt *child = *i;
2136 pet_tree *tree_i;
2138 tree_i = extract(child);
2139 if (tree_i && !partial) {
2140 res = pet_tree_block_add_child(res, tree_i);
2141 continue;
2143 pet_tree_free(tree_i);
2144 partial = false;
2145 if (pet_tree_block_n_child(res) == 0)
2146 continue;
2147 pet_tree_free(res);
2148 res = pet_tree_new_block(ctx, is_block, n_decl + n_stmt);
2151 if (pet_tree_block_n_child(res) == 0) {
2152 pet_tree_free(res);
2153 return tree;
2156 for (j = 0; j < n_stmt; ++j) {
2157 pet_tree *tree_i;
2159 tree_i = pet_tree_block_get_child(tree, j);
2160 res = pet_tree_block_add_child(res, tree_i);
2162 pet_tree_free(tree);
2164 return res;
2167 /* Try and construct a pet_tree corresponding to (part of)
2168 * a sequence of statements.
2170 * "block" is set if the sequence represents the children of
2171 * a compound statement.
2172 * "skip_declarations" is set if we should skip initial declarations
2173 * in the sequence of statements.
2174 * "parent" is the statement that has stmt_range as (some of) its children.
2176 * If autodetect is set, then we allow the extraction of only a subrange
2177 * of the sequence of statements. However, if there is at least one
2178 * kill and there is some subsequent statement for which we could not
2179 * construct a tree, then turn off the "block" property of the tree
2180 * such that no extra kill will be introduced at the end of the (partial)
2181 * block. If, on the other hand, the final range contains
2182 * no statements, then we discard the entire range.
2183 * If only a subrange of the sequence was extracted, but each statement
2184 * in the sequence was extracted completely, and if there are some
2185 * variable declarations in the sequence before or inside
2186 * the extracted subrange, then check if any of these variables are
2187 * not used after the extracted subrange. If so, add kills to these
2188 * variables.
2190 * If the entire range was extracted, apart from some initial declarations,
2191 * then we try and extend the range with the latest of those initial
2192 * declarations.
2194 __isl_give pet_tree *PetScan::extract(StmtRange stmt_range, bool block,
2195 bool skip_declarations, Stmt *parent)
2197 StmtIterator i;
2198 int j, skip;
2199 bool has_kills = false;
2200 bool partial_range = false;
2201 bool outer_partial = false;
2202 pet_tree *tree;
2203 SourceManager &SM = PP.getSourceManager();
2204 pet_killed_locals kl(SM);
2205 unsigned range_start, range_end;
2207 for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j)
2210 tree = pet_tree_new_block(ctx, block, j);
2212 skip = 0;
2213 i = stmt_range.first;
2214 if (skip_declarations)
2215 for (; i != stmt_range.second; ++i) {
2216 if ((*i)->getStmtClass() != Stmt::DeclStmtClass)
2217 break;
2218 if (options->autodetect)
2219 kl.add_locals(cast<DeclStmt>(*i));
2220 ++skip;
2223 for (; i != stmt_range.second; ++i) {
2224 Stmt *child = *i;
2225 pet_tree *tree_i;
2227 tree_i = extract(child);
2228 if (pet_tree_block_n_child(tree) != 0 && partial) {
2229 pet_tree_free(tree_i);
2230 break;
2232 if (child->getStmtClass() == Stmt::DeclStmtClass) {
2233 if (options->autodetect)
2234 kl.add_locals(cast<DeclStmt>(child));
2235 if (tree_i && block)
2236 has_kills = true;
2238 if (options->autodetect) {
2239 if (tree_i) {
2240 range_end = getExpansionOffset(SM,
2241 end_loc(child));
2242 if (pet_tree_block_n_child(tree) == 0)
2243 range_start = getExpansionOffset(SM,
2244 begin_loc(child));
2245 tree = pet_tree_block_add_child(tree, tree_i);
2246 } else {
2247 partial_range = true;
2249 if (pet_tree_block_n_child(tree) != 0 && !tree_i)
2250 outer_partial = partial = true;
2251 } else {
2252 tree = pet_tree_block_add_child(tree, tree_i);
2255 if (partial || !tree)
2256 break;
2259 if (!tree)
2260 return NULL;
2262 if (partial) {
2263 if (has_kills)
2264 tree = pet_tree_block_set_block(tree, 0);
2265 if (outer_partial) {
2266 kl.remove_accessed_after(parent,
2267 range_start, range_end);
2268 tree = add_kills(tree, kl.locals);
2270 } else if (partial_range) {
2271 if (pet_tree_block_n_child(tree) == 0) {
2272 pet_tree_free(tree);
2273 return NULL;
2275 partial = true;
2276 } else if (skip > 0)
2277 tree = insert_initial_declarations(tree, skip, stmt_range);
2279 return tree;
2282 extern "C" {
2283 static __isl_give pet_expr *get_array_size(__isl_keep pet_expr *access,
2284 void *user);
2285 static struct pet_array *extract_array(__isl_keep pet_expr *access,
2286 __isl_keep pet_context *pc, void *user);
2289 /* Construct a pet_expr that holds the sizes of the array accessed
2290 * by "access".
2291 * This function is used as a callback to pet_context_add_parameters,
2292 * which is also passed a pointer to the PetScan object.
2294 static __isl_give pet_expr *get_array_size(__isl_keep pet_expr *access,
2295 void *user)
2297 PetScan *ps = (PetScan *) user;
2298 isl_id *id;
2299 pet_expr *size;
2301 id = pet_expr_access_get_id(access);
2302 size = ps->get_array_size(id);
2303 isl_id_free(id);
2305 return size;
2308 /* Construct and return a pet_array corresponding to the variable
2309 * accessed by "access".
2310 * This function is used as a callback to pet_scop_from_pet_tree,
2311 * which is also passed a pointer to the PetScan object.
2313 static struct pet_array *extract_array(__isl_keep pet_expr *access,
2314 __isl_keep pet_context *pc, void *user)
2316 PetScan *ps = (PetScan *) user;
2317 isl_id *id;
2318 pet_array *array;
2320 id = pet_expr_access_get_id(access);
2321 array = ps->extract_array(id, NULL, pc);
2322 isl_id_free(id);
2324 return array;
2327 /* Store (a copy of) "summary" in the cache of function summaries
2328 * for function declaration "fd" and then give it back to the caller.
2330 __isl_give pet_function_summary *PetScan::cache_summary(clang::FunctionDecl *fd,
2331 __isl_take pet_function_summary *summary)
2333 summary_cache[fd] = pet_function_summary_copy(summary);
2335 return summary;
2338 /* Extract a function summary from the body of "fd",
2339 * with pet_tree representation "tree", extracted using "body_scan".
2341 * We extract a scop from the function body in a context with as
2342 * parameters the integer arguments of the function.
2343 * We then collect the accessed array elements and attach them
2344 * to the corresponding array arguments, taking into account
2345 * that the function body may access members of array elements.
2347 * The reason for representing the integer arguments as parameters in
2348 * the context is that if we were to instead start with a context
2349 * with the function arguments as initial dimensions, then we would not
2350 * be able to refer to them from the array extents, without turning
2351 * array extents into maps.
2353 __isl_give pet_function_summary *PetScan::get_summary_from_tree(
2354 __isl_take pet_tree *tree, clang::FunctionDecl *fd,
2355 PetScan &body_scan)
2357 isl_space *space;
2358 isl_set *domain;
2359 pet_context *pc;
2360 pet_function_summary *summary;
2361 unsigned n;
2362 struct pet_scop *scop;
2363 int int_size;
2364 isl_union_set *may_read, *may_write, *must_write;
2365 isl_union_map *to_inner;
2367 space = isl_space_set_alloc(ctx, 0, 0);
2369 n = fd->getNumParams();
2370 summary = pet_function_summary_alloc(ctx, n);
2371 for (unsigned i = 0; i < n; ++i) {
2372 ParmVarDecl *parm = fd->getParamDecl(i);
2373 QualType type = parm->getType();
2374 isl_id *id;
2376 if (!type->isIntegerType())
2377 continue;
2378 id = pet_id_from_decl(ctx, parm);
2379 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2380 space = isl_space_set_dim_id(space, isl_dim_param, 0,
2381 isl_id_copy(id));
2382 summary = pet_function_summary_set_int(summary, i, id);
2385 domain = isl_set_universe(space);
2386 pc = pet_context_alloc(domain);
2387 pc = pet_context_add_parameters(pc, tree,
2388 &::get_array_size, &body_scan);
2389 int_size = size_in_bytes(ast_context, ast_context.IntTy);
2390 scop = pet_scop_from_pet_tree(tree, int_size,
2391 &::extract_array, &body_scan, pc);
2392 scop = scan_arrays(scop, pc);
2393 may_read = isl_union_map_range(pet_scop_get_may_reads(scop));
2394 may_write = isl_union_map_range(pet_scop_get_may_writes(scop));
2395 must_write = isl_union_map_range(pet_scop_get_must_writes(scop));
2396 to_inner = pet_scop_compute_outer_to_inner(scop);
2397 pet_scop_free(scop);
2399 for (unsigned i = 0; i < n; ++i) {
2400 ParmVarDecl *parm = fd->getParamDecl(i);
2401 QualType type = parm->getType();
2402 struct pet_array *array;
2403 isl_space *space;
2404 isl_union_set *data_set;
2405 isl_union_set *may_read_i, *may_write_i, *must_write_i;
2407 if (pet_clang_array_depth(type) == 0)
2408 continue;
2410 array = body_scan.extract_array(parm, NULL, pc);
2411 space = array ? isl_set_get_space(array->extent) : NULL;
2412 pet_array_free(array);
2413 data_set = isl_union_set_from_set(isl_set_universe(space));
2414 data_set = isl_union_set_apply(data_set,
2415 isl_union_map_copy(to_inner));
2416 may_read_i = isl_union_set_intersect(
2417 isl_union_set_copy(may_read),
2418 isl_union_set_copy(data_set));
2419 may_write_i = isl_union_set_intersect(
2420 isl_union_set_copy(may_write),
2421 isl_union_set_copy(data_set));
2422 must_write_i = isl_union_set_intersect(
2423 isl_union_set_copy(must_write), data_set);
2424 summary = pet_function_summary_set_array(summary, i,
2425 may_read_i, may_write_i, must_write_i);
2428 isl_union_set_free(may_read);
2429 isl_union_set_free(may_write);
2430 isl_union_set_free(must_write);
2431 isl_union_map_free(to_inner);
2433 pet_context_free(pc);
2435 return summary;
2438 /* Extract a function summary from the body of "fd", if possible.
2439 * Return this->no_summary if the body cannot be fully analyzed.
2441 * Turn on autodetection to avoid printing warnings
2442 * if the body cannot be fully analyzed,
2443 * but return this->no_summary if the extracted pet_tree only
2444 * represents part of the function body.
2445 * The function body is allowed to have a return statement at the end.
2447 * The result is stored in the summary_cache cache so that we can reuse
2448 * it if this method gets called on the same function again later on.
2450 __isl_give pet_function_summary *PetScan::get_summary(FunctionDecl *fd)
2452 pet_tree *tree;
2453 pet_function_summary *summary;
2454 ScopLoc loc;
2455 int save_autodetect;
2457 if (summary_cache.find(fd) != summary_cache.end())
2458 return pet_function_summary_copy(summary_cache[fd]);
2460 save_autodetect = options->autodetect;
2461 options->autodetect = 1;
2462 PetScan body_scan(PP, ast_context, fd, loc, options,
2463 isl_union_map_copy(value_bounds), independent);
2465 body_scan.return_root = fd->getBody();
2466 tree = body_scan.extract(fd->getBody(), false);
2467 options->autodetect = save_autodetect;
2469 if (body_scan.partial) {
2470 pet_tree_free(tree);
2471 return cache_summary(fd, pet_function_summary_copy(no_summary));
2474 summary = get_summary_from_tree(tree, fd, body_scan);
2476 return cache_summary(fd, summary);
2479 /* If "fd" has a function body, then try and extract a function summary from
2480 * this body and, if successful, attach it to the call expression "expr".
2482 * Even if a function body is available, "fd" itself may point
2483 * to a declaration without function body. We therefore first
2484 * replace it by the declaration that comes with a body (if any).
2486 __isl_give pet_expr *PetScan::set_summary(__isl_take pet_expr *expr,
2487 FunctionDecl *fd)
2489 pet_function_summary *summary;
2491 if (!expr)
2492 return NULL;
2493 fd = pet_clang_find_function_decl_with_body(fd);
2494 if (!fd)
2495 return expr;
2497 summary = get_summary(fd);
2498 if (summary == no_summary) {
2499 pet_function_summary_free(summary);
2500 return expr;
2503 expr = pet_expr_call_set_summary(expr, summary);
2505 return expr;
2508 /* Extract a pet_scop from "tree".
2510 * We simply call pet_scop_from_pet_tree with the appropriate arguments and
2511 * then add pet_arrays for all accessed arrays.
2512 * We populate the pet_context with assignments for all parameters used
2513 * inside "tree" or any of the size expressions for the arrays accessed
2514 * by "tree" so that they can be used in affine expressions.
2516 struct pet_scop *PetScan::extract_scop(__isl_take pet_tree *tree)
2518 int int_size;
2519 isl_set *domain;
2520 pet_context *pc;
2521 pet_scop *scop;
2523 int_size = size_in_bytes(ast_context, ast_context.IntTy);
2525 domain = isl_set_universe(isl_space_set_alloc(ctx, 0, 0));
2526 pc = pet_context_alloc(domain);
2527 pc = pet_context_add_parameters(pc, tree, &::get_array_size, this);
2528 scop = pet_scop_from_pet_tree(tree, int_size,
2529 &::extract_array, this, pc);
2530 scop = scan_arrays(scop, pc);
2531 pet_context_free(pc);
2533 return scop;
2536 /* Add a call to __pencil_kill to the end of "tree" that kills
2537 * all the variables in "locals" and return the result.
2539 * No location is added to the kill because the most natural
2540 * location would lie outside the scop. Attaching such a location
2541 * to this tree would extend the scope of the final result
2542 * to include the location.
2544 __isl_give pet_tree *PetScan::add_kills(__isl_take pet_tree *tree,
2545 set<ValueDecl *> locals)
2547 int i;
2548 pet_expr *expr;
2549 pet_tree *kill, *block;
2550 set<ValueDecl *>::iterator it;
2552 if (locals.size() == 0)
2553 return tree;
2554 expr = pet_expr_new_call(ctx, "__pencil_kill", locals.size());
2555 i = 0;
2556 for (it = locals.begin(); it != locals.end(); ++it) {
2557 pet_expr *arg;
2558 arg = extract_access_expr(*it);
2559 expr = pet_expr_set_arg(expr, i++, arg);
2561 kill = pet_tree_new_expr(expr);
2562 block = pet_tree_new_block(ctx, 0, 2);
2563 block = pet_tree_block_add_child(block, tree);
2564 block = pet_tree_block_add_child(block, kill);
2566 return block;
2569 /* Check if the scop marked by the user is exactly this Stmt
2570 * or part of this Stmt.
2571 * If so, return a pet_scop corresponding to the marked region.
2572 * Otherwise, return NULL.
2574 * If the scop is not further nested inside a child of "stmt",
2575 * then check if there are any variable declarations before the scop
2576 * inside "stmt". If so, and if these variables are not used
2577 * after the scop, then add kills to the variables.
2579 * If the scop starts in the middle of one of the children, without
2580 * also ending in that child, then report an error.
2582 struct pet_scop *PetScan::scan(Stmt *stmt)
2584 SourceManager &SM = PP.getSourceManager();
2585 unsigned start_off, end_off;
2586 pet_tree *tree;
2588 start_off = getExpansionOffset(SM, begin_loc(stmt));
2589 end_off = getExpansionOffset(SM, end_loc(stmt));
2591 if (start_off > loc.end)
2592 return NULL;
2593 if (end_off < loc.start)
2594 return NULL;
2596 if (start_off >= loc.start && end_off <= loc.end)
2597 return extract_scop(extract(stmt));
2599 pet_killed_locals kl(SM);
2600 StmtIterator start;
2601 for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
2602 Stmt *child = *start;
2603 if (!child)
2604 continue;
2605 start_off = getExpansionOffset(SM, begin_loc(child));
2606 end_off = getExpansionOffset(SM, end_loc(child));
2607 if (start_off < loc.start && end_off >= loc.end)
2608 return scan(child);
2609 if (start_off >= loc.start)
2610 break;
2611 if (loc.start < end_off) {
2612 report_unbalanced_pragmas(loc.scop, loc.endscop);
2613 return NULL;
2615 if (isa<DeclStmt>(child))
2616 kl.add_locals(cast<DeclStmt>(child));
2619 StmtIterator end;
2620 for (end = start; end != stmt->child_end(); ++end) {
2621 Stmt *child = *end;
2622 start_off = SM.getFileOffset(begin_loc(child));
2623 if (start_off >= loc.end)
2624 break;
2627 kl.remove_accessed_after(stmt, loc.start, loc.end);
2629 tree = extract(StmtRange(start, end), false, false, stmt);
2630 tree = add_kills(tree, kl.locals);
2631 return extract_scop(tree);
2634 /* Set the size of index "pos" of "array" to "size".
2635 * In particular, add a constraint of the form
2637 * i_pos < size
2639 * to array->extent and a constraint of the form
2641 * size >= 0
2643 * to array->context.
2645 * The domain of "size" is assumed to be zero-dimensional.
2647 static struct pet_array *update_size(struct pet_array *array, int pos,
2648 __isl_take isl_pw_aff *size)
2650 isl_set *valid;
2651 isl_set *univ;
2652 isl_set *bound;
2653 isl_space *dim;
2654 isl_aff *aff;
2655 isl_pw_aff *index;
2656 isl_id *id;
2658 if (!array)
2659 goto error;
2661 valid = isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size)));
2662 array->context = isl_set_intersect(array->context, valid);
2664 dim = isl_set_get_space(array->extent);
2665 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2666 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, 1);
2667 univ = isl_set_universe(isl_aff_get_domain_space(aff));
2668 index = isl_pw_aff_alloc(univ, aff);
2670 size = isl_pw_aff_add_dims(size, isl_dim_in,
2671 isl_set_dim(array->extent, isl_dim_set));
2672 id = isl_set_get_tuple_id(array->extent);
2673 size = isl_pw_aff_set_tuple_id(size, isl_dim_in, id);
2674 bound = isl_pw_aff_lt_set(index, size);
2676 array->extent = isl_set_intersect(array->extent, bound);
2678 if (!array->context || !array->extent)
2679 return pet_array_free(array);
2681 return array;
2682 error:
2683 isl_pw_aff_free(size);
2684 return NULL;
2687 #ifdef HAVE_DECAYEDTYPE
2689 /* If "qt" is a decayed type, then set *decayed to true and
2690 * return the original type.
2692 static QualType undecay(QualType qt, bool *decayed)
2694 const Type *type = qt.getTypePtr();
2696 *decayed = isa<DecayedType>(type);
2697 if (*decayed)
2698 qt = cast<DecayedType>(type)->getOriginalType();
2699 return qt;
2702 #else
2704 /* If "qt" is a decayed type, then set *decayed to true and
2705 * return the original type.
2706 * Since this version of clang does not define a DecayedType,
2707 * we cannot obtain the original type even if it had been decayed and
2708 * we set *decayed to false.
2710 static QualType undecay(QualType qt, bool *decayed)
2712 *decayed = false;
2713 return qt;
2716 #endif
2718 /* Figure out the size of the array at position "pos" and all
2719 * subsequent positions from "qt" and update the corresponding
2720 * argument of "expr" accordingly.
2722 * The initial type (when pos is zero) may be a pointer type decayed
2723 * from an array type, if this initial type is the type of a function
2724 * argument. This only happens if the original array type has
2725 * a constant size in the outer dimension as otherwise we get
2726 * a VariableArrayType. Try and obtain this original type (if available) and
2727 * take the outer array size into account if it was marked static.
2729 __isl_give pet_expr *PetScan::set_upper_bounds(__isl_take pet_expr *expr,
2730 QualType qt, int pos)
2732 const ArrayType *atype;
2733 pet_expr *size;
2734 bool decayed = false;
2736 if (!expr)
2737 return NULL;
2739 if (pos == 0)
2740 qt = undecay(qt, &decayed);
2742 if (qt->isPointerType()) {
2743 qt = qt->getPointeeType();
2744 return set_upper_bounds(expr, qt, pos + 1);
2746 if (!qt->isArrayType())
2747 return expr;
2749 qt = qt->getCanonicalTypeInternal();
2750 atype = cast<ArrayType>(qt.getTypePtr());
2752 if (decayed && atype->getSizeModifier() != ArrayType::Static) {
2753 qt = atype->getElementType();
2754 return set_upper_bounds(expr, qt, pos + 1);
2757 if (qt->isConstantArrayType()) {
2758 const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
2759 size = extract_expr(ca->getSize());
2760 expr = pet_expr_set_arg(expr, pos, size);
2761 } else if (qt->isVariableArrayType()) {
2762 const VariableArrayType *vla = cast<VariableArrayType>(atype);
2763 size = extract_expr(vla->getSizeExpr());
2764 expr = pet_expr_set_arg(expr, pos, size);
2767 qt = atype->getElementType();
2769 return set_upper_bounds(expr, qt, pos + 1);
2772 /* Construct a pet_expr that holds the sizes of the array represented by "id".
2773 * The returned expression is a call expression with as arguments
2774 * the sizes in each dimension. If we are unable to derive the size
2775 * in a given dimension, then the corresponding argument is set to infinity.
2776 * In fact, we initialize all arguments to infinity and then update
2777 * them if we are able to figure out the size.
2779 * The result is stored in the id_size cache so that it can be reused
2780 * if this method is called on the same array identifier later.
2781 * The result is also stored in the type_size cache in case
2782 * it gets called on a different array identifier with the same type.
2784 __isl_give pet_expr *PetScan::get_array_size(__isl_keep isl_id *id)
2786 QualType qt = pet_id_get_array_type(id);
2787 int depth;
2788 pet_expr *expr, *inf;
2789 const Type *type = qt.getTypePtr();
2790 isl_maybe_pet_expr m;
2792 m = isl_id_to_pet_expr_try_get(id_size, id);
2793 if (m.valid < 0 || m.valid)
2794 return m.value;
2795 if (type_size.find(type) != type_size.end())
2796 return pet_expr_copy(type_size[type]);
2798 depth = pet_clang_array_depth(qt);
2799 inf = pet_expr_new_int(isl_val_infty(ctx));
2800 expr = pet_expr_new_call(ctx, "bounds", depth);
2801 for (int i = 0; i < depth; ++i)
2802 expr = pet_expr_set_arg(expr, i, pet_expr_copy(inf));
2803 pet_expr_free(inf);
2805 expr = set_upper_bounds(expr, qt, 0);
2806 type_size[type] = pet_expr_copy(expr);
2807 id_size = isl_id_to_pet_expr_set(id_size, isl_id_copy(id),
2808 pet_expr_copy(expr));
2810 return expr;
2813 /* Set the array size of the array identified by "id" to "size",
2814 * replacing any previously stored value.
2816 void PetScan::set_array_size(__isl_take isl_id *id, __isl_take pet_expr *size)
2818 id_size = isl_id_to_pet_expr_set(id_size, id, size);
2821 /* Does "expr" represent the "integer" infinity?
2823 static int is_infty(__isl_keep pet_expr *expr)
2825 isl_val *v;
2826 int res;
2828 if (pet_expr_get_type(expr) != pet_expr_int)
2829 return 0;
2830 v = pet_expr_int_get_val(expr);
2831 res = isl_val_is_infty(v);
2832 isl_val_free(v);
2834 return res;
2837 /* Figure out the dimensions of an array "array" and
2838 * update "array" accordingly.
2840 * We first construct a pet_expr that holds the sizes of the array
2841 * in each dimension. The resulting expression may containing
2842 * infinity values for dimension where we are unable to derive
2843 * a size expression.
2845 * The arguments of the size expression that have a value different from
2846 * infinity are then converted to an affine expression
2847 * within the context "pc" and incorporated into the size of "array".
2848 * If we are unable to convert a size expression to an affine expression or
2849 * if the size is not a (symbolic) constant,
2850 * then we leave the corresponding size of "array" untouched.
2852 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
2853 __isl_keep pet_context *pc)
2855 int n;
2856 isl_id *id;
2857 pet_expr *expr;
2859 if (!array)
2860 return NULL;
2862 id = isl_set_get_tuple_id(array->extent);
2863 if (!id)
2864 return pet_array_free(array);
2865 expr = get_array_size(id);
2866 isl_id_free(id);
2868 n = pet_expr_get_n_arg(expr);
2869 for (int i = 0; i < n; ++i) {
2870 pet_expr *arg;
2871 isl_pw_aff *size;
2873 arg = pet_expr_get_arg(expr, i);
2874 if (!is_infty(arg)) {
2875 int dim;
2877 size = pet_expr_extract_affine(arg, pc);
2878 dim = isl_pw_aff_dim(size, isl_dim_in);
2879 if (!size)
2880 array = pet_array_free(array);
2881 else if (isl_pw_aff_involves_nan(size) ||
2882 isl_pw_aff_involves_dims(size, isl_dim_in, 0, dim))
2883 isl_pw_aff_free(size);
2884 else {
2885 size = isl_pw_aff_drop_dims(size,
2886 isl_dim_in, 0, dim);
2887 array = update_size(array, i, size);
2890 pet_expr_free(arg);
2892 pet_expr_free(expr);
2894 return array;
2897 /* Does "decl" have a definition that we can keep track of in a pet_type?
2899 static bool has_printable_definition(RecordDecl *decl)
2901 if (!decl->getDeclName())
2902 return false;
2903 return decl->getLexicalDeclContext() == decl->getDeclContext();
2906 /* Add all TypedefType objects that appear when dereferencing "type"
2907 * to "types".
2909 static void insert_intermediate_typedefs(PetTypes *types, QualType type)
2911 type = pet_clang_base_or_typedef_type(type);
2912 while (isa<TypedefType>(type)) {
2913 const TypedefType *tt;
2915 tt = cast<TypedefType>(type);
2916 types->insert(tt->getDecl());
2917 type = tt->desugar();
2918 type = pet_clang_base_or_typedef_type(type);
2922 /* Construct and return a pet_array corresponding to the variable
2923 * represented by "id".
2924 * In particular, initialize array->extent to
2926 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2928 * and then call set_upper_bounds to set the upper bounds on the indices
2929 * based on the type of the variable. The upper bounds are converted
2930 * to affine expressions within the context "pc".
2932 * If the base type is that of a record with a top-level definition or
2933 * of a typedef and if "types" is not null, then the RecordDecl or
2934 * TypedefType corresponding to the type, as well as any intermediate
2935 * TypedefType, is added to "types".
2937 * If the base type is that of a record with no top-level definition,
2938 * then we replace it by "<subfield>".
2940 * If the variable is a scalar, i.e., a zero-dimensional array,
2941 * then the "const" qualifier, if any, is removed from the base type.
2942 * This makes it easier for users of pet to turn initializations
2943 * into assignments.
2945 struct pet_array *PetScan::extract_array(__isl_keep isl_id *id,
2946 PetTypes *types, __isl_keep pet_context *pc)
2948 struct pet_array *array;
2949 QualType qt = pet_id_get_array_type(id);
2950 int depth = pet_clang_array_depth(qt);
2951 QualType base = pet_clang_base_type(qt);
2952 string name;
2953 isl_space *space;
2955 array = isl_calloc_type(ctx, struct pet_array);
2956 if (!array)
2957 return NULL;
2959 space = isl_space_set_alloc(ctx, 0, depth);
2960 space = isl_space_set_tuple_id(space, isl_dim_set, isl_id_copy(id));
2962 array->extent = isl_set_nat_universe(space);
2964 space = isl_space_params_alloc(ctx, 0);
2965 array->context = isl_set_universe(space);
2967 array = set_upper_bounds(array, pc);
2968 if (!array)
2969 return NULL;
2971 if (depth == 0)
2972 base.removeLocalConst();
2973 name = base.getAsString();
2975 if (types) {
2976 insert_intermediate_typedefs(types, qt);
2977 if (isa<TypedefType>(base)) {
2978 types->insert(cast<TypedefType>(base)->getDecl());
2979 } else if (base->isRecordType()) {
2980 RecordDecl *decl = pet_clang_record_decl(base);
2981 TypedefNameDecl *typedecl;
2982 typedecl = decl->getTypedefNameForAnonDecl();
2983 if (typedecl)
2984 types->insert(typedecl);
2985 else if (has_printable_definition(decl))
2986 types->insert(decl);
2987 else
2988 name = "<subfield>";
2992 array->element_type = strdup(name.c_str());
2993 array->element_is_record = base->isRecordType();
2994 array->element_size = size_in_bytes(ast_context, base);
2996 return array;
2999 /* Construct and return a pet_array corresponding to the variable "decl".
3001 struct pet_array *PetScan::extract_array(ValueDecl *decl,
3002 PetTypes *types, __isl_keep pet_context *pc)
3004 isl_id *id;
3005 pet_array *array;
3007 id = pet_id_from_decl(ctx, decl);
3008 array = extract_array(id, types, pc);
3009 isl_id_free(id);
3011 return array;
3014 /* Construct and return a pet_array corresponding to the sequence
3015 * of declarations represented by "decls".
3016 * The upper bounds of the array are converted to affine expressions
3017 * within the context "pc".
3018 * If the sequence contains a single declaration, then it corresponds
3019 * to a simple array access. Otherwise, it corresponds to a member access,
3020 * with the declaration for the substructure following that of the containing
3021 * structure in the sequence of declarations.
3022 * We start with the outermost substructure and then combine it with
3023 * information from the inner structures.
3025 * Additionally, keep track of all required types in "types".
3027 struct pet_array *PetScan::extract_array(__isl_keep isl_id_list *decls,
3028 PetTypes *types, __isl_keep pet_context *pc)
3030 int i, n;
3031 isl_id *id;
3032 struct pet_array *array;
3034 id = isl_id_list_get_id(decls, 0);
3035 array = extract_array(id, types, pc);
3036 isl_id_free(id);
3038 n = isl_id_list_n_id(decls);
3039 for (i = 1; i < n; ++i) {
3040 struct pet_array *parent;
3041 const char *base_name, *field_name;
3042 char *product_name;
3044 parent = array;
3045 id = isl_id_list_get_id(decls, i);
3046 array = extract_array(id, types, pc);
3047 isl_id_free(id);
3048 if (!array)
3049 return pet_array_free(parent);
3051 base_name = isl_set_get_tuple_name(parent->extent);
3052 field_name = isl_set_get_tuple_name(array->extent);
3053 product_name = pet_array_member_access_name(ctx,
3054 base_name, field_name);
3056 array->extent = isl_set_product(isl_set_copy(parent->extent),
3057 array->extent);
3058 if (product_name)
3059 array->extent = isl_set_set_tuple_name(array->extent,
3060 product_name);
3061 array->context = isl_set_intersect(array->context,
3062 isl_set_copy(parent->context));
3064 pet_array_free(parent);
3065 free(product_name);
3067 if (!array->extent || !array->context || !product_name)
3068 return pet_array_free(array);
3071 return array;
3074 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3075 RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3076 std::set<TypeDecl *> &types_done);
3077 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3078 TypedefNameDecl *decl, Preprocessor &PP, PetTypes &types,
3079 std::set<TypeDecl *> &types_done);
3081 /* For each of the fields of "decl" that is itself a record type
3082 * or a typedef, or an array of such type, add a corresponding pet_type
3083 * to "scop".
3085 static struct pet_scop *add_field_types(isl_ctx *ctx, struct pet_scop *scop,
3086 RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3087 std::set<TypeDecl *> &types_done)
3089 RecordDecl::field_iterator it;
3091 for (it = decl->field_begin(); it != decl->field_end(); ++it) {
3092 QualType type = it->getType();
3094 type = pet_clang_base_or_typedef_type(type);
3095 if (isa<TypedefType>(type)) {
3096 TypedefNameDecl *typedefdecl;
3098 typedefdecl = cast<TypedefType>(type)->getDecl();
3099 scop = add_type(ctx, scop, typedefdecl,
3100 PP, types, types_done);
3101 } else if (type->isRecordType()) {
3102 RecordDecl *record;
3104 record = pet_clang_record_decl(type);
3105 scop = add_type(ctx, scop, record,
3106 PP, types, types_done);
3110 return scop;
3113 /* Add a pet_type corresponding to "decl" to "scop", provided
3114 * it is a member of types.records and it has not been added before
3115 * (i.e., it is not a member of "types_done").
3117 * Since we want the user to be able to print the types
3118 * in the order in which they appear in the scop, we need to
3119 * make sure that types of fields in a structure appear before
3120 * that structure. We therefore call ourselves recursively
3121 * through add_field_types on the types of all record subfields.
3123 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3124 RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3125 std::set<TypeDecl *> &types_done)
3127 string s;
3128 llvm::raw_string_ostream S(s);
3130 if (types.records.find(decl) == types.records.end())
3131 return scop;
3132 if (types_done.find(decl) != types_done.end())
3133 return scop;
3135 add_field_types(ctx, scop, decl, PP, types, types_done);
3137 if (strlen(decl->getName().str().c_str()) == 0)
3138 return scop;
3140 decl->print(S, PrintingPolicy(PP.getLangOpts()));
3141 S.str();
3143 scop->types[scop->n_type] = pet_type_alloc(ctx,
3144 decl->getName().str().c_str(), s.c_str());
3145 if (!scop->types[scop->n_type])
3146 return pet_scop_free(scop);
3148 types_done.insert(decl);
3150 scop->n_type++;
3152 return scop;
3155 /* Add a pet_type corresponding to "decl" to "scop", provided
3156 * it is a member of types.typedefs and it has not been added before
3157 * (i.e., it is not a member of "types_done").
3159 * If the underlying type is a structure, then we print the typedef
3160 * ourselves since clang does not print the definition of the structure
3161 * in the typedef. We also make sure in this case that the types of
3162 * the fields in the structure are added first.
3163 * Since the definition of the structure also gets printed this way,
3164 * add it to types_done such that it will not be printed again,
3165 * not even without the typedef.
3167 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3168 TypedefNameDecl *decl, Preprocessor &PP, PetTypes &types,
3169 std::set<TypeDecl *> &types_done)
3171 string s;
3172 llvm::raw_string_ostream S(s);
3173 QualType qt = decl->getUnderlyingType();
3175 if (types.typedefs.find(decl) == types.typedefs.end())
3176 return scop;
3177 if (types_done.find(decl) != types_done.end())
3178 return scop;
3180 if (qt->isRecordType()) {
3181 RecordDecl *rec = pet_clang_record_decl(qt);
3183 add_field_types(ctx, scop, rec, PP, types, types_done);
3184 S << "typedef ";
3185 rec->print(S, PrintingPolicy(PP.getLangOpts()));
3186 S << " ";
3187 S << decl->getName();
3188 types_done.insert(rec);
3189 } else {
3190 decl->print(S, PrintingPolicy(PP.getLangOpts()));
3192 S.str();
3194 scop->types[scop->n_type] = pet_type_alloc(ctx,
3195 decl->getName().str().c_str(), s.c_str());
3196 if (!scop->types[scop->n_type])
3197 return pet_scop_free(scop);
3199 types_done.insert(decl);
3201 scop->n_type++;
3203 return scop;
3206 /* Construct a list of pet_arrays, one for each array (or scalar)
3207 * accessed inside "scop", add this list to "scop" and return the result.
3208 * The upper bounds of the arrays are converted to affine expressions
3209 * within the context "pc".
3211 * The context of "scop" is updated with the intersection of
3212 * the contexts of all arrays, i.e., constraints on the parameters
3213 * that ensure that the arrays have a valid (non-negative) size.
3215 * If any of the extracted arrays refers to a member access or
3216 * has a typedef'd type as base type,
3217 * then also add the required types to "scop".
3218 * The typedef types are printed first because their definitions
3219 * may include the definition of a struct and these struct definitions
3220 * should not be printed separately. While the typedef definition
3221 * is being printed, the struct is marked as having been printed as well,
3222 * such that the later printing of the struct by itself can be prevented.
3224 * If the sequence of nested array declarations from which the pet_array
3225 * is extracted appears as the prefix of some other sequence,
3226 * then the pet_array is marked as "outer".
3227 * The arrays that already appear in scop->arrays at the start of
3228 * this function are assumed to be simple arrays, so they are not marked
3229 * as outer.
3231 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop,
3232 __isl_keep pet_context *pc)
3234 int i, n;
3235 array_desc_set arrays, has_sub;
3236 array_desc_set::iterator it;
3237 PetTypes types;
3238 std::set<TypeDecl *> types_done;
3239 std::set<clang::RecordDecl *, less_name>::iterator records_it;
3240 std::set<clang::TypedefNameDecl *, less_name>::iterator typedefs_it;
3241 int n_array;
3242 struct pet_array **scop_arrays;
3244 if (!scop)
3245 return NULL;
3247 pet_scop_collect_arrays(scop, arrays);
3248 if (arrays.size() == 0)
3249 return scop;
3251 n_array = scop->n_array;
3253 scop_arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3254 n_array + arrays.size());
3255 if (!scop_arrays)
3256 goto error;
3257 scop->arrays = scop_arrays;
3259 for (it = arrays.begin(); it != arrays.end(); ++it) {
3260 isl_id_list *list = isl_id_list_copy(*it);
3261 int n = isl_id_list_n_id(list);
3262 list = isl_id_list_drop(list, n - 1, 1);
3263 has_sub.insert(list);
3266 for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
3267 struct pet_array *array;
3268 array = extract_array(*it, &types, pc);
3269 scop->arrays[n_array + i] = array;
3270 if (!scop->arrays[n_array + i])
3271 goto error;
3272 if (has_sub.find(*it) != has_sub.end())
3273 array->outer = 1;
3274 scop->n_array++;
3275 scop->context = isl_set_intersect(scop->context,
3276 isl_set_copy(array->context));
3277 if (!scop->context)
3278 goto error;
3281 n = types.records.size() + types.typedefs.size();
3282 if (n == 0)
3283 return scop;
3285 scop->types = isl_alloc_array(ctx, struct pet_type *, n);
3286 if (!scop->types)
3287 goto error;
3289 for (typedefs_it = types.typedefs.begin();
3290 typedefs_it != types.typedefs.end(); ++typedefs_it)
3291 scop = add_type(ctx, scop, *typedefs_it, PP, types, types_done);
3293 for (records_it = types.records.begin();
3294 records_it != types.records.end(); ++records_it)
3295 scop = add_type(ctx, scop, *records_it, PP, types, types_done);
3297 return scop;
3298 error:
3299 pet_scop_free(scop);
3300 return NULL;
3303 /* Bound all parameters in scop->context to the possible values
3304 * of the corresponding C variable.
3306 static struct pet_scop *add_parameter_bounds(struct pet_scop *scop)
3308 int n;
3310 if (!scop)
3311 return NULL;
3313 n = isl_set_dim(scop->context, isl_dim_param);
3314 for (int i = 0; i < n; ++i) {
3315 isl_id *id;
3316 ValueDecl *decl;
3318 id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
3319 if (pet_nested_in_id(id)) {
3320 isl_id_free(id);
3321 isl_die(isl_set_get_ctx(scop->context),
3322 isl_error_internal,
3323 "unresolved nested parameter", goto error);
3325 decl = pet_id_get_decl(id);
3326 isl_id_free(id);
3328 scop->context = set_parameter_bounds(scop->context, i, decl);
3330 if (!scop->context)
3331 goto error;
3334 return scop;
3335 error:
3336 pet_scop_free(scop);
3337 return NULL;
3340 /* Construct a pet_scop from the given function.
3342 * If the scop was delimited by scop and endscop pragmas, then we override
3343 * the file offsets by those derived from the pragmas.
3345 struct pet_scop *PetScan::scan(FunctionDecl *fd)
3347 pet_scop *scop;
3348 Stmt *stmt;
3350 stmt = fd->getBody();
3352 if (options->autodetect) {
3353 set_current_stmt(stmt);
3354 scop = extract_scop(extract(stmt, true));
3355 } else {
3356 current_line = loc.start_line;
3357 scop = scan(stmt);
3358 scop = pet_scop_update_start_end(scop, loc.start, loc.end);
3360 scop = add_parameter_bounds(scop);
3361 scop = pet_scop_gist(scop, value_bounds);
3363 return scop;
3366 /* Update this->last_line and this->current_line based on the fact
3367 * that we are about to consider "stmt".
3369 void PetScan::set_current_stmt(Stmt *stmt)
3371 SourceLocation loc = begin_loc(stmt);
3372 SourceManager &SM = PP.getSourceManager();
3374 last_line = current_line;
3375 current_line = SM.getExpansionLineNumber(loc);
3378 /* Is the current statement marked by an independent pragma?
3379 * That is, is there an independent pragma on a line between
3380 * the line of the current statement and the line of the previous statement.
3381 * The search is not implemented very efficiently. We currently
3382 * assume that there are only a few independent pragmas, if any.
3384 bool PetScan::is_current_stmt_marked_independent()
3386 for (unsigned i = 0; i < independent.size(); ++i) {
3387 unsigned line = independent[i].line;
3389 if (last_line < line && line < current_line)
3390 return true;
3393 return false;