support inlining of non-outermost call expressions
[pet.git] / scan.cc
blob212311d61b9efde5bb7dcfb0c35d2803cb33876a
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.h"
59 #include "context.h"
60 #include "expr.h"
61 #include "expr_plus.h"
62 #include "id.h"
63 #include "inliner.h"
64 #include "inlined_calls.h"
65 #include "killed_locals.h"
66 #include "nest.h"
67 #include "options.h"
68 #include "scan.h"
69 #include "scop.h"
70 #include "scop_plus.h"
71 #include "substituter.h"
72 #include "tree.h"
73 #include "tree2scop.h"
75 using namespace std;
76 using namespace clang;
78 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
80 switch (kind) {
81 case UO_Minus:
82 return pet_op_minus;
83 case UO_Not:
84 return pet_op_not;
85 case UO_LNot:
86 return pet_op_lnot;
87 case UO_PostInc:
88 return pet_op_post_inc;
89 case UO_PostDec:
90 return pet_op_post_dec;
91 case UO_PreInc:
92 return pet_op_pre_inc;
93 case UO_PreDec:
94 return pet_op_pre_dec;
95 default:
96 return pet_op_last;
100 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
102 switch (kind) {
103 case BO_AddAssign:
104 return pet_op_add_assign;
105 case BO_SubAssign:
106 return pet_op_sub_assign;
107 case BO_MulAssign:
108 return pet_op_mul_assign;
109 case BO_DivAssign:
110 return pet_op_div_assign;
111 case BO_AndAssign:
112 return pet_op_and_assign;
113 case BO_XorAssign:
114 return pet_op_xor_assign;
115 case BO_OrAssign:
116 return pet_op_or_assign;
117 case BO_Assign:
118 return pet_op_assign;
119 case BO_Add:
120 return pet_op_add;
121 case BO_Sub:
122 return pet_op_sub;
123 case BO_Mul:
124 return pet_op_mul;
125 case BO_Div:
126 return pet_op_div;
127 case BO_Rem:
128 return pet_op_mod;
129 case BO_Shl:
130 return pet_op_shl;
131 case BO_Shr:
132 return pet_op_shr;
133 case BO_EQ:
134 return pet_op_eq;
135 case BO_NE:
136 return pet_op_ne;
137 case BO_LE:
138 return pet_op_le;
139 case BO_GE:
140 return pet_op_ge;
141 case BO_LT:
142 return pet_op_lt;
143 case BO_GT:
144 return pet_op_gt;
145 case BO_And:
146 return pet_op_and;
147 case BO_Xor:
148 return pet_op_xor;
149 case BO_Or:
150 return pet_op_or;
151 case BO_LAnd:
152 return pet_op_land;
153 case BO_LOr:
154 return pet_op_lor;
155 default:
156 return pet_op_last;
160 #ifdef GETTYPEINFORETURNSTYPEINFO
162 static int size_in_bytes(ASTContext &context, QualType type)
164 return context.getTypeInfo(type).Width / 8;
167 #else
169 static int size_in_bytes(ASTContext &context, QualType type)
171 return context.getTypeInfo(type).first / 8;
174 #endif
176 /* Check if the element type corresponding to the given array type
177 * has a const qualifier.
179 static bool const_base(QualType qt)
181 const Type *type = qt.getTypePtr();
183 if (type->isPointerType())
184 return const_base(type->getPointeeType());
185 if (type->isArrayType()) {
186 const ArrayType *atype;
187 type = type->getCanonicalTypeInternal().getTypePtr();
188 atype = cast<ArrayType>(type);
189 return const_base(atype->getElementType());
192 return qt.isConstQualified();
195 PetScan::~PetScan()
197 std::map<const Type *, pet_expr *>::iterator it;
198 std::map<FunctionDecl *, pet_function_summary *>::iterator it_s;
200 for (it = type_size.begin(); it != type_size.end(); ++it)
201 pet_expr_free(it->second);
202 for (it_s = summary_cache.begin(); it_s != summary_cache.end(); ++it_s)
203 pet_function_summary_free(it_s->second);
205 isl_id_to_pet_expr_free(id_size);
206 isl_union_map_free(value_bounds);
209 /* Report a diagnostic on the range "range", unless autodetect is set.
211 void PetScan::report(SourceRange range, unsigned id)
213 if (options->autodetect)
214 return;
216 SourceLocation loc = range.getBegin();
217 DiagnosticsEngine &diag = PP.getDiagnostics();
218 DiagnosticBuilder B = diag.Report(loc, id) << range;
221 /* Report a diagnostic on "stmt", unless autodetect is set.
223 void PetScan::report(Stmt *stmt, unsigned id)
225 report(stmt->getSourceRange(), id);
228 /* Report a diagnostic on "decl", unless autodetect is set.
230 void PetScan::report(Decl *decl, unsigned id)
232 report(decl->getSourceRange(), id);
235 /* Called if we found something we (currently) cannot handle.
236 * We'll provide more informative warnings later.
238 * We only actually complain if autodetect is false.
240 void PetScan::unsupported(Stmt *stmt)
242 DiagnosticsEngine &diag = PP.getDiagnostics();
243 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
244 "unsupported");
245 report(stmt, id);
248 /* Report an unsupported unary operator, unless autodetect is set.
250 void PetScan::report_unsupported_unary_operator(Stmt *stmt)
252 DiagnosticsEngine &diag = PP.getDiagnostics();
253 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
254 "this type of unary operator is not supported");
255 report(stmt, id);
258 /* Report an unsupported binary operator, unless autodetect is set.
260 void PetScan::report_unsupported_binary_operator(Stmt *stmt)
262 DiagnosticsEngine &diag = PP.getDiagnostics();
263 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
264 "this type of binary operator is not supported");
265 report(stmt, id);
268 /* Report an unsupported statement type, unless autodetect is set.
270 void PetScan::report_unsupported_statement_type(Stmt *stmt)
272 DiagnosticsEngine &diag = PP.getDiagnostics();
273 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
274 "this type of statement is not supported");
275 report(stmt, id);
278 /* Report a missing prototype, unless autodetect is set.
280 void PetScan::report_prototype_required(Stmt *stmt)
282 DiagnosticsEngine &diag = PP.getDiagnostics();
283 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
284 "prototype required");
285 report(stmt, id);
288 /* Report a missing increment, unless autodetect is set.
290 void PetScan::report_missing_increment(Stmt *stmt)
292 DiagnosticsEngine &diag = PP.getDiagnostics();
293 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
294 "missing increment");
295 report(stmt, id);
298 /* Report a missing summary function, unless autodetect is set.
300 void PetScan::report_missing_summary_function(Stmt *stmt)
302 DiagnosticsEngine &diag = PP.getDiagnostics();
303 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
304 "missing summary function");
305 report(stmt, id);
308 /* Report a missing summary function body, unless autodetect is set.
310 void PetScan::report_missing_summary_function_body(Stmt *stmt)
312 DiagnosticsEngine &diag = PP.getDiagnostics();
313 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
314 "missing summary function body");
315 report(stmt, id);
318 /* Report an unsupported argument in a call to an inlined function,
319 * unless autodetect is set.
321 void PetScan::report_unsupported_inline_function_argument(Stmt *stmt)
323 DiagnosticsEngine &diag = PP.getDiagnostics();
324 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
325 "unsupported inline function call argument");
326 report(stmt, id);
329 /* Report an unsupported type of declaration, unless autodetect is set.
331 void PetScan::report_unsupported_declaration(Decl *decl)
333 DiagnosticsEngine &diag = PP.getDiagnostics();
334 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
335 "unsupported declaration");
336 report(decl, id);
339 /* Report an unbalanced pair of scop/endscop pragmas, unless autodetect is set.
341 void PetScan::report_unbalanced_pragmas(SourceLocation scop,
342 SourceLocation endscop)
344 if (options->autodetect)
345 return;
347 DiagnosticsEngine &diag = PP.getDiagnostics();
349 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
350 "unbalanced endscop pragma");
351 DiagnosticBuilder B2 = diag.Report(endscop, id);
354 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Note,
355 "corresponding scop pragma");
356 DiagnosticBuilder B = diag.Report(scop, id);
360 /* Report a return statement in an unsupported context,
361 * unless autodetect is set.
363 void PetScan::report_unsupported_return(Stmt *stmt)
365 DiagnosticsEngine &diag = PP.getDiagnostics();
366 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
367 "return statements not supported in this context");
368 report(stmt, id);
371 /* Report a return statement that does not appear at the end of a function,
372 * unless autodetect is set.
374 void PetScan::report_return_not_at_end_of_function(Stmt *stmt)
376 DiagnosticsEngine &diag = PP.getDiagnostics();
377 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
378 "return statement must be final statement in function");
379 report(stmt, id);
382 /* Extract an integer from "val", which is assumed to be non-negative.
384 static __isl_give isl_val *extract_unsigned(isl_ctx *ctx,
385 const llvm::APInt &val)
387 unsigned n;
388 const uint64_t *data;
390 data = val.getRawData();
391 n = val.getNumWords();
392 return isl_val_int_from_chunks(ctx, n, sizeof(uint64_t), data);
395 /* Extract an integer from "val". If "is_signed" is set, then "val"
396 * is signed. Otherwise it it unsigned.
398 static __isl_give isl_val *extract_int(isl_ctx *ctx, bool is_signed,
399 llvm::APInt val)
401 int is_negative = is_signed && val.isNegative();
402 isl_val *v;
404 if (is_negative)
405 val = -val;
407 v = extract_unsigned(ctx, val);
409 if (is_negative)
410 v = isl_val_neg(v);
411 return v;
414 /* Extract an integer from "expr".
416 __isl_give isl_val *PetScan::extract_int(isl_ctx *ctx, IntegerLiteral *expr)
418 const Type *type = expr->getType().getTypePtr();
419 bool is_signed = type->hasSignedIntegerRepresentation();
421 return ::extract_int(ctx, is_signed, expr->getValue());
424 /* Extract an integer from "expr".
425 * Return NULL if "expr" does not (obviously) represent an integer.
427 __isl_give isl_val *PetScan::extract_int(clang::ParenExpr *expr)
429 return extract_int(expr->getSubExpr());
432 /* Extract an integer from "expr".
433 * Return NULL if "expr" does not (obviously) represent an integer.
435 __isl_give isl_val *PetScan::extract_int(clang::Expr *expr)
437 if (expr->getStmtClass() == Stmt::IntegerLiteralClass)
438 return extract_int(ctx, cast<IntegerLiteral>(expr));
439 if (expr->getStmtClass() == Stmt::ParenExprClass)
440 return extract_int(cast<ParenExpr>(expr));
442 unsupported(expr);
443 return NULL;
446 /* Extract a pet_expr from the APInt "val", which is assumed
447 * to be non-negative.
449 __isl_give pet_expr *PetScan::extract_expr(const llvm::APInt &val)
451 return pet_expr_new_int(extract_unsigned(ctx, val));
454 /* Return the number of bits needed to represent the type of "decl",
455 * if it is an integer type. Otherwise return 0.
456 * If qt is signed then return the opposite of the number of bits.
458 static int get_type_size(ValueDecl *decl)
460 return pet_clang_get_type_size(decl->getType(), decl->getASTContext());
463 /* Bound parameter "pos" of "set" to the possible values of "decl".
465 static __isl_give isl_set *set_parameter_bounds(__isl_take isl_set *set,
466 unsigned pos, ValueDecl *decl)
468 int type_size;
469 isl_ctx *ctx;
470 isl_val *bound;
472 ctx = isl_set_get_ctx(set);
473 type_size = get_type_size(decl);
474 if (type_size == 0)
475 isl_die(ctx, isl_error_invalid, "not an integer type",
476 return isl_set_free(set));
477 if (type_size > 0) {
478 set = isl_set_lower_bound_si(set, isl_dim_param, pos, 0);
479 bound = isl_val_int_from_ui(ctx, type_size);
480 bound = isl_val_2exp(bound);
481 bound = isl_val_sub_ui(bound, 1);
482 set = isl_set_upper_bound_val(set, isl_dim_param, pos, bound);
483 } else {
484 bound = isl_val_int_from_ui(ctx, -type_size - 1);
485 bound = isl_val_2exp(bound);
486 bound = isl_val_sub_ui(bound, 1);
487 set = isl_set_upper_bound_val(set, isl_dim_param, pos,
488 isl_val_copy(bound));
489 bound = isl_val_neg(bound);
490 bound = isl_val_sub_ui(bound, 1);
491 set = isl_set_lower_bound_val(set, isl_dim_param, pos, bound);
494 return set;
497 __isl_give pet_expr *PetScan::extract_index_expr(ImplicitCastExpr *expr)
499 return extract_index_expr(expr->getSubExpr());
502 /* Construct a pet_expr representing an index expression for an access
503 * to the variable referenced by "expr".
505 * If "expr" references an enum constant, then return an integer expression
506 * instead, representing the value of the enum constant.
508 __isl_give pet_expr *PetScan::extract_index_expr(DeclRefExpr *expr)
510 return extract_index_expr(expr->getDecl());
513 /* Construct a pet_expr representing an index expression for an access
514 * to the variable "decl".
516 * If "decl" is an enum constant, then we return an integer expression
517 * instead, representing the value of the enum constant.
519 __isl_give pet_expr *PetScan::extract_index_expr(ValueDecl *decl)
521 isl_id *id;
523 if (isa<EnumConstantDecl>(decl))
524 return extract_expr(cast<EnumConstantDecl>(decl));
526 id = pet_id_from_decl(ctx, decl);
527 return pet_id_create_index_expr(id);
530 /* Construct a pet_expr representing the index expression "expr"
531 * Return NULL on error.
533 * If "expr" is a reference to an enum constant, then return
534 * an integer expression instead, representing the value of the enum constant.
536 __isl_give pet_expr *PetScan::extract_index_expr(Expr *expr)
538 switch (expr->getStmtClass()) {
539 case Stmt::ImplicitCastExprClass:
540 return extract_index_expr(cast<ImplicitCastExpr>(expr));
541 case Stmt::DeclRefExprClass:
542 return extract_index_expr(cast<DeclRefExpr>(expr));
543 case Stmt::ArraySubscriptExprClass:
544 return extract_index_expr(cast<ArraySubscriptExpr>(expr));
545 case Stmt::IntegerLiteralClass:
546 return extract_expr(cast<IntegerLiteral>(expr));
547 case Stmt::MemberExprClass:
548 return extract_index_expr(cast<MemberExpr>(expr));
549 default:
550 unsupported(expr);
552 return NULL;
555 /* Extract an index expression from the given array subscript expression.
557 * We first extract an index expression from the base.
558 * This will result in an index expression with a range that corresponds
559 * to the earlier indices.
560 * We then extract the current index and let
561 * pet_expr_access_subscript combine the two.
563 __isl_give pet_expr *PetScan::extract_index_expr(ArraySubscriptExpr *expr)
565 Expr *base = expr->getBase();
566 Expr *idx = expr->getIdx();
567 pet_expr *index;
568 pet_expr *base_expr;
570 base_expr = extract_index_expr(base);
571 index = extract_expr(idx);
573 base_expr = pet_expr_access_subscript(base_expr, index);
575 return base_expr;
578 /* Extract an index expression from a member expression.
580 * If the base access (to the structure containing the member)
581 * is of the form
583 * A[..]
585 * and the member is called "f", then the member access is of
586 * the form
588 * A_f[A[..] -> f[]]
590 * If the member access is to an anonymous struct, then simply return
592 * A[..]
594 * If the member access in the source code is of the form
596 * A->f
598 * then it is treated as
600 * A[0].f
602 __isl_give pet_expr *PetScan::extract_index_expr(MemberExpr *expr)
604 Expr *base = expr->getBase();
605 FieldDecl *field = cast<FieldDecl>(expr->getMemberDecl());
606 pet_expr *base_index;
607 isl_id *id;
609 base_index = extract_index_expr(base);
611 if (expr->isArrow()) {
612 pet_expr *index = pet_expr_new_int(isl_val_zero(ctx));
613 base_index = pet_expr_access_subscript(base_index, index);
616 if (field->isAnonymousStructOrUnion())
617 return base_index;
619 id = pet_id_from_decl(ctx, field);
621 return pet_expr_access_member(base_index, id);
624 /* Mark the given access pet_expr as a write.
626 static __isl_give pet_expr *mark_write(__isl_take pet_expr *access)
628 access = pet_expr_access_set_write(access, 1);
629 access = pet_expr_access_set_read(access, 0);
631 return access;
634 /* Mark the given (read) access pet_expr as also possibly being written.
635 * That is, initialize the may write access relation from the may read relation
636 * and initialize the must write access relation to the empty relation.
638 static __isl_give pet_expr *mark_may_write(__isl_take pet_expr *expr)
640 isl_union_map *access;
641 isl_union_map *empty;
643 access = pet_expr_access_get_dependent_access(expr,
644 pet_expr_access_may_read);
645 empty = isl_union_map_empty(isl_union_map_get_space(access));
646 expr = pet_expr_access_set_access(expr, pet_expr_access_may_write,
647 access);
648 expr = pet_expr_access_set_access(expr, pet_expr_access_must_write,
649 empty);
651 return expr;
654 /* Construct a pet_expr representing a unary operator expression.
656 __isl_give pet_expr *PetScan::extract_expr(UnaryOperator *expr)
658 int type_size;
659 pet_expr *arg;
660 enum pet_op_type op;
662 op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
663 if (op == pet_op_last) {
664 report_unsupported_unary_operator(expr);
665 return NULL;
668 arg = extract_expr(expr->getSubExpr());
670 if (expr->isIncrementDecrementOp() &&
671 pet_expr_get_type(arg) == pet_expr_access) {
672 arg = mark_write(arg);
673 arg = pet_expr_access_set_read(arg, 1);
676 type_size = pet_clang_get_type_size(expr->getType(), ast_context);
677 return pet_expr_new_unary(type_size, op, arg);
680 /* Construct a pet_expr representing a binary operator expression.
682 * If the top level operator is an assignment and the LHS is an access,
683 * then we mark that access as a write. If the operator is a compound
684 * assignment, the access is marked as both a read and a write.
686 __isl_give pet_expr *PetScan::extract_expr(BinaryOperator *expr)
688 int type_size;
689 pet_expr *lhs, *rhs;
690 enum pet_op_type op;
692 op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
693 if (op == pet_op_last) {
694 report_unsupported_binary_operator(expr);
695 return NULL;
698 lhs = extract_expr(expr->getLHS());
699 rhs = extract_expr(expr->getRHS());
701 if (expr->isAssignmentOp() &&
702 pet_expr_get_type(lhs) == pet_expr_access) {
703 lhs = mark_write(lhs);
704 if (expr->isCompoundAssignmentOp())
705 lhs = pet_expr_access_set_read(lhs, 1);
708 type_size = pet_clang_get_type_size(expr->getType(), ast_context);
709 return pet_expr_new_binary(type_size, op, lhs, rhs);
712 /* Construct a pet_tree for a variable declaration and
713 * add the declaration to the list of declarations
714 * inside the current compound statement.
716 __isl_give pet_tree *PetScan::extract(Decl *decl)
718 VarDecl *vd;
719 pet_expr *lhs, *rhs;
720 pet_tree *tree;
722 if (!isa<VarDecl>(decl)) {
723 report_unsupported_declaration(decl);
724 return NULL;
727 vd = cast<VarDecl>(decl);
728 declarations.push_back(vd);
730 lhs = extract_access_expr(vd);
731 lhs = mark_write(lhs);
732 if (!vd->getInit())
733 tree = pet_tree_new_decl(lhs);
734 else {
735 rhs = extract_expr(vd->getInit());
736 tree = pet_tree_new_decl_init(lhs, rhs);
739 return tree;
742 /* Construct a pet_tree for a variable declaration statement.
743 * If the declaration statement declares multiple variables,
744 * then return a group of pet_trees, one for each declared variable.
746 __isl_give pet_tree *PetScan::extract(DeclStmt *stmt)
748 pet_tree *tree;
749 unsigned n;
751 if (!stmt->isSingleDecl()) {
752 const DeclGroup &group = stmt->getDeclGroup().getDeclGroup();
753 n = group.size();
754 tree = pet_tree_new_block(ctx, 0, n);
756 for (unsigned i = 0; i < n; ++i) {
757 pet_tree *tree_i;
758 pet_loc *loc;
760 tree_i = extract(group[i]);
761 loc = construct_pet_loc(group[i]->getSourceRange(),
762 false);
763 tree_i = pet_tree_set_loc(tree_i, loc);
764 tree = pet_tree_block_add_child(tree, tree_i);
767 return tree;
770 return extract(stmt->getSingleDecl());
773 /* Construct a pet_expr representing a conditional operation.
775 __isl_give pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
777 pet_expr *cond, *lhs, *rhs;
779 cond = extract_expr(expr->getCond());
780 lhs = extract_expr(expr->getTrueExpr());
781 rhs = extract_expr(expr->getFalseExpr());
783 return pet_expr_new_ternary(cond, lhs, rhs);
786 __isl_give pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
788 return extract_expr(expr->getSubExpr());
791 /* Construct a pet_expr representing a floating point value.
793 * If the floating point literal does not appear in a macro,
794 * then we use the original representation in the source code
795 * as the string representation. Otherwise, we use the pretty
796 * printer to produce a string representation.
798 __isl_give pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
800 double d;
801 string s;
802 const LangOptions &LO = PP.getLangOpts();
803 SourceLocation loc = expr->getLocation();
805 if (!loc.isMacroID()) {
806 SourceManager &SM = PP.getSourceManager();
807 unsigned len = Lexer::MeasureTokenLength(loc, SM, LO);
808 s = string(SM.getCharacterData(loc), len);
809 } else {
810 llvm::raw_string_ostream S(s);
811 expr->printPretty(S, 0, PrintingPolicy(LO));
812 S.str();
814 d = expr->getValueAsApproximateDouble();
815 return pet_expr_new_double(ctx, d, s.c_str());
818 /* Extract an index expression from "expr" and then convert it into
819 * an access pet_expr.
821 * If "expr" is a reference to an enum constant, then return
822 * an integer expression instead, representing the value of the enum constant.
824 __isl_give pet_expr *PetScan::extract_access_expr(Expr *expr)
826 pet_expr *index;
828 index = extract_index_expr(expr);
830 if (pet_expr_get_type(index) == pet_expr_int)
831 return index;
833 return pet_expr_access_from_index(expr->getType(), index, ast_context);
836 /* Extract an index expression from "decl" and then convert it into
837 * an access pet_expr.
839 __isl_give pet_expr *PetScan::extract_access_expr(ValueDecl *decl)
841 return pet_expr_access_from_index(decl->getType(),
842 extract_index_expr(decl), ast_context);
845 __isl_give pet_expr *PetScan::extract_expr(ParenExpr *expr)
847 return extract_expr(expr->getSubExpr());
850 /* Extract an assume statement from the argument "expr"
851 * of a __builtin_assume or __pencil_assume statement.
853 __isl_give pet_expr *PetScan::extract_assume(Expr *expr)
855 return pet_expr_new_unary(0, pet_op_assume, extract_expr(expr));
858 /* If "expr" is an address-of operator, then return its argument.
859 * Otherwise, return NULL.
861 static Expr *extract_addr_of_arg(Expr *expr)
863 UnaryOperator *op;
865 if (expr->getStmtClass() != Stmt::UnaryOperatorClass)
866 return NULL;
867 op = cast<UnaryOperator>(expr);
868 if (op->getOpcode() != UO_AddrOf)
869 return NULL;
870 return op->getSubExpr();
873 /* Construct a pet_expr corresponding to the function call argument "expr".
874 * The argument appears in position "pos" of a call to function "fd".
876 * If we are passing along a pointer to an array element
877 * or an entire row or even higher dimensional slice of an array,
878 * then the function being called may write into the array.
880 * We assume here that if the function is declared to take a pointer
881 * to a const type, then the function may only perform a read
882 * and that otherwise, it may either perform a read or a write (or both).
883 * We only perform this check if "detect_writes" is set.
885 __isl_give pet_expr *PetScan::extract_argument(FunctionDecl *fd, int pos,
886 Expr *expr, bool detect_writes)
888 Expr *arg;
889 pet_expr *res;
890 int is_addr = 0, is_partial = 0;
892 expr = pet_clang_strip_casts(expr);
893 arg = extract_addr_of_arg(expr);
894 if (arg) {
895 is_addr = 1;
896 expr = arg;
898 res = extract_expr(expr);
899 if (!res)
900 return NULL;
901 if (pet_clang_array_depth(expr->getType()) > 0)
902 is_partial = 1;
903 if (detect_writes && (is_addr || is_partial) &&
904 pet_expr_get_type(res) == pet_expr_access) {
905 ParmVarDecl *parm;
906 if (!fd->hasPrototype()) {
907 report_prototype_required(expr);
908 return pet_expr_free(res);
910 parm = fd->getParamDecl(pos);
911 if (!const_base(parm->getType()))
912 res = mark_may_write(res);
915 if (is_addr)
916 res = pet_expr_new_unary(0, pet_op_address_of, res);
917 return res;
920 /* Find the first FunctionDecl with the given name.
921 * "call" is the corresponding call expression and is only used
922 * for reporting errors.
924 * Return NULL on error.
926 FunctionDecl *PetScan::find_decl_from_name(CallExpr *call, string name)
928 TranslationUnitDecl *tu = ast_context.getTranslationUnitDecl();
929 DeclContext::decl_iterator begin = tu->decls_begin();
930 DeclContext::decl_iterator end = tu->decls_end();
931 for (DeclContext::decl_iterator i = begin; i != end; ++i) {
932 FunctionDecl *fd = dyn_cast<FunctionDecl>(*i);
933 if (!fd)
934 continue;
935 if (fd->getName().str().compare(name) != 0)
936 continue;
937 if (fd->hasBody())
938 return fd;
939 report_missing_summary_function_body(call);
940 return NULL;
942 report_missing_summary_function(call);
943 return NULL;
946 /* Return the FunctionDecl for the summary function associated to the
947 * function called by "call".
949 * In particular, if the pencil option is set, then
950 * search for an annotate attribute formatted as
951 * "pencil_access(name)", where "name" is the name of the summary function.
953 * If no summary function was specified, then return the FunctionDecl
954 * that is actually being called.
956 * Return NULL on error.
958 FunctionDecl *PetScan::get_summary_function(CallExpr *call)
960 FunctionDecl *decl = call->getDirectCallee();
961 if (!decl)
962 return NULL;
964 if (!options->pencil)
965 return decl;
967 specific_attr_iterator<AnnotateAttr> begin, end, i;
968 begin = decl->specific_attr_begin<AnnotateAttr>();
969 end = decl->specific_attr_end<AnnotateAttr>();
970 for (i = begin; i != end; ++i) {
971 string attr = (*i)->getAnnotation().str();
973 const char prefix[] = "pencil_access(";
974 size_t start = attr.find(prefix);
975 if (start == string::npos)
976 continue;
977 start += strlen(prefix);
978 string name = attr.substr(start, attr.find(')') - start);
980 return find_decl_from_name(call, name);
983 return decl;
986 /* Is "name" the name of an assume statement?
987 * "pencil" indicates whether pencil builtins and pragmas should be supported.
988 * "__builtin_assume" is always accepted.
989 * If "pencil" is set, then "__pencil_assume" is also accepted.
991 static bool is_assume(int pencil, const string &name)
993 if (name == "__builtin_assume")
994 return true;
995 return pencil && name == "__pencil_assume";
998 /* Construct a pet_expr representing a function call.
1000 * If this->call2id is not NULL and it contains a mapping for this call,
1001 * then this means that the corresponding function has been inlined.
1002 * Return a pet_expr that reads from the variable that
1003 * stores the return value of the inlined call.
1005 * In the special case of a "call" to __builtin_assume or __pencil_assume,
1006 * construct an assume expression instead.
1008 * In the case of a "call" to __pencil_kill, the arguments
1009 * are neither read nor written (only killed), so there
1010 * is no need to check for writes to these arguments.
1012 * __pencil_assume and __pencil_kill are only recognized
1013 * when the pencil option is set.
1015 __isl_give pet_expr *PetScan::extract_expr(CallExpr *expr)
1017 pet_expr *res = NULL;
1018 FunctionDecl *fd;
1019 string name;
1020 unsigned n_arg;
1021 bool is_kill;
1023 if (call2id && call2id->find(expr) != call2id->end())
1024 return pet_expr_access_from_id(isl_id_copy(call2id[0][expr]),
1025 ast_context);
1027 fd = expr->getDirectCallee();
1028 if (!fd) {
1029 unsupported(expr);
1030 return NULL;
1033 name = fd->getDeclName().getAsString();
1034 n_arg = expr->getNumArgs();
1036 if (n_arg == 1 && is_assume(options->pencil, name))
1037 return extract_assume(expr->getArg(0));
1038 is_kill = options->pencil && name == "__pencil_kill";
1040 res = pet_expr_new_call(ctx, name.c_str(), n_arg);
1041 if (!res)
1042 return NULL;
1044 for (unsigned i = 0; i < n_arg; ++i) {
1045 Expr *arg = expr->getArg(i);
1046 res = pet_expr_set_arg(res, i,
1047 PetScan::extract_argument(fd, i, arg, !is_kill));
1050 fd = get_summary_function(expr);
1051 if (!fd)
1052 return pet_expr_free(res);
1054 res = set_summary(res, fd);
1056 return res;
1059 /* Construct a pet_expr representing a (C style) cast.
1061 __isl_give pet_expr *PetScan::extract_expr(CStyleCastExpr *expr)
1063 pet_expr *arg;
1064 QualType type;
1066 arg = extract_expr(expr->getSubExpr());
1067 if (!arg)
1068 return NULL;
1070 type = expr->getTypeAsWritten();
1071 return pet_expr_new_cast(type.getAsString().c_str(), arg);
1074 /* Construct a pet_expr representing an integer.
1076 __isl_give pet_expr *PetScan::extract_expr(IntegerLiteral *expr)
1078 return pet_expr_new_int(extract_int(expr));
1081 /* Construct a pet_expr representing the integer enum constant "ecd".
1083 __isl_give pet_expr *PetScan::extract_expr(EnumConstantDecl *ecd)
1085 isl_val *v;
1086 const llvm::APSInt &init = ecd->getInitVal();
1087 v = ::extract_int(ctx, init.isSigned(), init);
1088 return pet_expr_new_int(v);
1091 /* Try and construct a pet_expr representing "expr".
1093 __isl_give pet_expr *PetScan::extract_expr(Expr *expr)
1095 switch (expr->getStmtClass()) {
1096 case Stmt::UnaryOperatorClass:
1097 return extract_expr(cast<UnaryOperator>(expr));
1098 case Stmt::CompoundAssignOperatorClass:
1099 case Stmt::BinaryOperatorClass:
1100 return extract_expr(cast<BinaryOperator>(expr));
1101 case Stmt::ImplicitCastExprClass:
1102 return extract_expr(cast<ImplicitCastExpr>(expr));
1103 case Stmt::ArraySubscriptExprClass:
1104 case Stmt::DeclRefExprClass:
1105 case Stmt::MemberExprClass:
1106 return extract_access_expr(expr);
1107 case Stmt::IntegerLiteralClass:
1108 return extract_expr(cast<IntegerLiteral>(expr));
1109 case Stmt::FloatingLiteralClass:
1110 return extract_expr(cast<FloatingLiteral>(expr));
1111 case Stmt::ParenExprClass:
1112 return extract_expr(cast<ParenExpr>(expr));
1113 case Stmt::ConditionalOperatorClass:
1114 return extract_expr(cast<ConditionalOperator>(expr));
1115 case Stmt::CallExprClass:
1116 return extract_expr(cast<CallExpr>(expr));
1117 case Stmt::CStyleCastExprClass:
1118 return extract_expr(cast<CStyleCastExpr>(expr));
1119 default:
1120 unsupported(expr);
1122 return NULL;
1125 /* Check if the given initialization statement is an assignment.
1126 * If so, return that assignment. Otherwise return NULL.
1128 BinaryOperator *PetScan::initialization_assignment(Stmt *init)
1130 BinaryOperator *ass;
1132 if (init->getStmtClass() != Stmt::BinaryOperatorClass)
1133 return NULL;
1135 ass = cast<BinaryOperator>(init);
1136 if (ass->getOpcode() != BO_Assign)
1137 return NULL;
1139 return ass;
1142 /* Check if the given initialization statement is a declaration
1143 * of a single variable.
1144 * If so, return that declaration. Otherwise return NULL.
1146 Decl *PetScan::initialization_declaration(Stmt *init)
1148 DeclStmt *decl;
1150 if (init->getStmtClass() != Stmt::DeclStmtClass)
1151 return NULL;
1153 decl = cast<DeclStmt>(init);
1155 if (!decl->isSingleDecl())
1156 return NULL;
1158 return decl->getSingleDecl();
1161 /* Given the assignment operator in the initialization of a for loop,
1162 * extract the induction variable, i.e., the (integer)variable being
1163 * assigned.
1165 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1167 Expr *lhs;
1168 DeclRefExpr *ref;
1169 ValueDecl *decl;
1170 const Type *type;
1172 lhs = init->getLHS();
1173 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1174 unsupported(init);
1175 return NULL;
1178 ref = cast<DeclRefExpr>(lhs);
1179 decl = ref->getDecl();
1180 type = decl->getType().getTypePtr();
1182 if (!type->isIntegerType()) {
1183 unsupported(lhs);
1184 return NULL;
1187 return decl;
1190 /* Given the initialization statement of a for loop and the single
1191 * declaration in this initialization statement,
1192 * extract the induction variable, i.e., the (integer) variable being
1193 * declared.
1195 VarDecl *PetScan::extract_induction_variable(Stmt *init, Decl *decl)
1197 VarDecl *vd;
1199 vd = cast<VarDecl>(decl);
1201 const QualType type = vd->getType();
1202 if (!type->isIntegerType()) {
1203 unsupported(init);
1204 return NULL;
1207 if (!vd->getInit()) {
1208 unsupported(init);
1209 return NULL;
1212 return vd;
1215 /* Check that op is of the form iv++ or iv--.
1216 * Return a pet_expr representing "1" or "-1" accordingly.
1218 __isl_give pet_expr *PetScan::extract_unary_increment(
1219 clang::UnaryOperator *op, clang::ValueDecl *iv)
1221 Expr *sub;
1222 DeclRefExpr *ref;
1223 isl_val *v;
1225 if (!op->isIncrementDecrementOp()) {
1226 unsupported(op);
1227 return NULL;
1230 sub = op->getSubExpr();
1231 if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1232 unsupported(op);
1233 return NULL;
1236 ref = cast<DeclRefExpr>(sub);
1237 if (ref->getDecl() != iv) {
1238 unsupported(op);
1239 return NULL;
1242 if (op->isIncrementOp())
1243 v = isl_val_one(ctx);
1244 else
1245 v = isl_val_negone(ctx);
1247 return pet_expr_new_int(v);
1250 /* Check if op is of the form
1252 * iv = expr
1254 * and return the increment "expr - iv" as a pet_expr.
1256 __isl_give pet_expr *PetScan::extract_binary_increment(BinaryOperator *op,
1257 clang::ValueDecl *iv)
1259 int type_size;
1260 Expr *lhs;
1261 DeclRefExpr *ref;
1262 pet_expr *expr, *expr_iv;
1264 if (op->getOpcode() != BO_Assign) {
1265 unsupported(op);
1266 return NULL;
1269 lhs = op->getLHS();
1270 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1271 unsupported(op);
1272 return NULL;
1275 ref = cast<DeclRefExpr>(lhs);
1276 if (ref->getDecl() != iv) {
1277 unsupported(op);
1278 return NULL;
1281 expr = extract_expr(op->getRHS());
1282 expr_iv = extract_expr(lhs);
1284 type_size = pet_clang_get_type_size(iv->getType(), ast_context);
1285 return pet_expr_new_binary(type_size, pet_op_sub, expr, expr_iv);
1288 /* Check that op is of the form iv += cst or iv -= cst
1289 * and return a pet_expr corresponding to cst or -cst accordingly.
1291 __isl_give pet_expr *PetScan::extract_compound_increment(
1292 CompoundAssignOperator *op, clang::ValueDecl *iv)
1294 Expr *lhs;
1295 DeclRefExpr *ref;
1296 bool neg = false;
1297 pet_expr *expr;
1298 BinaryOperatorKind opcode;
1300 opcode = op->getOpcode();
1301 if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1302 unsupported(op);
1303 return NULL;
1305 if (opcode == BO_SubAssign)
1306 neg = true;
1308 lhs = op->getLHS();
1309 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1310 unsupported(op);
1311 return NULL;
1314 ref = cast<DeclRefExpr>(lhs);
1315 if (ref->getDecl() != iv) {
1316 unsupported(op);
1317 return NULL;
1320 expr = extract_expr(op->getRHS());
1321 if (neg) {
1322 int type_size;
1323 type_size = pet_clang_get_type_size(op->getType(), ast_context);
1324 expr = pet_expr_new_unary(type_size, pet_op_minus, expr);
1327 return expr;
1330 /* Check that the increment of the given for loop increments
1331 * (or decrements) the induction variable "iv" and return
1332 * the increment as a pet_expr if successful.
1334 __isl_give pet_expr *PetScan::extract_increment(clang::ForStmt *stmt,
1335 ValueDecl *iv)
1337 Stmt *inc = stmt->getInc();
1339 if (!inc) {
1340 report_missing_increment(stmt);
1341 return NULL;
1344 if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1345 return extract_unary_increment(cast<UnaryOperator>(inc), iv);
1346 if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1347 return extract_compound_increment(
1348 cast<CompoundAssignOperator>(inc), iv);
1349 if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1350 return extract_binary_increment(cast<BinaryOperator>(inc), iv);
1352 unsupported(inc);
1353 return NULL;
1356 /* Construct a pet_tree for a while loop.
1358 * If we were only able to extract part of the body, then simply
1359 * return that part.
1361 __isl_give pet_tree *PetScan::extract(WhileStmt *stmt)
1363 pet_expr *pe_cond;
1364 pet_tree *tree;
1366 tree = extract(stmt->getBody());
1367 if (partial)
1368 return tree;
1369 pe_cond = extract_expr(stmt->getCond());
1370 tree = pet_tree_new_while(pe_cond, tree);
1372 return tree;
1375 /* Construct a pet_tree for a for statement.
1376 * The for loop is required to be of one of the following forms
1378 * for (i = init; condition; ++i)
1379 * for (i = init; condition; --i)
1380 * for (i = init; condition; i += constant)
1381 * for (i = init; condition; i -= constant)
1383 * We extract a pet_tree for the body and then include it in a pet_tree
1384 * of type pet_tree_for.
1386 * As a special case, we also allow a for loop of the form
1388 * for (;;)
1390 * in which case we return a pet_tree of type pet_tree_infinite_loop.
1392 * If we were only able to extract part of the body, then simply
1393 * return that part.
1395 __isl_give pet_tree *PetScan::extract_for(ForStmt *stmt)
1397 BinaryOperator *ass;
1398 Decl *decl;
1399 Stmt *init;
1400 Expr *rhs;
1401 ValueDecl *iv;
1402 pet_tree *tree;
1403 int independent;
1404 int declared;
1405 pet_expr *pe_init, *pe_inc, *pe_iv, *pe_cond;
1407 independent = is_current_stmt_marked_independent();
1409 if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc()) {
1410 tree = extract(stmt->getBody());
1411 if (partial)
1412 return tree;
1413 tree = pet_tree_new_infinite_loop(tree);
1414 return tree;
1417 init = stmt->getInit();
1418 if (!init) {
1419 unsupported(stmt);
1420 return NULL;
1422 if ((ass = initialization_assignment(init)) != NULL) {
1423 iv = extract_induction_variable(ass);
1424 if (!iv)
1425 return NULL;
1426 rhs = ass->getRHS();
1427 } else if ((decl = initialization_declaration(init)) != NULL) {
1428 VarDecl *var = extract_induction_variable(init, decl);
1429 if (!var)
1430 return NULL;
1431 iv = var;
1432 rhs = var->getInit();
1433 } else {
1434 unsupported(stmt->getInit());
1435 return NULL;
1438 declared = !initialization_assignment(stmt->getInit());
1439 tree = extract(stmt->getBody());
1440 if (partial)
1441 return tree;
1442 pe_iv = extract_access_expr(iv);
1443 pe_iv = mark_write(pe_iv);
1444 pe_init = extract_expr(rhs);
1445 if (!stmt->getCond())
1446 pe_cond = pet_expr_new_int(isl_val_one(ctx));
1447 else
1448 pe_cond = extract_expr(stmt->getCond());
1449 pe_inc = extract_increment(stmt, iv);
1450 tree = pet_tree_new_for(independent, declared, pe_iv, pe_init, pe_cond,
1451 pe_inc, tree);
1452 return tree;
1455 /* Store the names of the variables declared in decl_context
1456 * in the set declared_names. Make sure to only do this once by
1457 * setting declared_names_collected.
1459 void PetScan::collect_declared_names()
1461 DeclContext *DC = decl_context;
1462 DeclContext::decl_iterator it;
1464 if (declared_names_collected)
1465 return;
1467 for (it = DC->decls_begin(); it != DC->decls_end(); ++it) {
1468 Decl *D = *it;
1469 NamedDecl *named;
1471 if (!isa<NamedDecl>(D))
1472 continue;
1473 named = cast<NamedDecl>(D);
1474 declared_names.insert(named->getName().str());
1477 declared_names_collected = true;
1480 /* Add the names in "names" that are not also in this->declared_names
1481 * to this->used_names.
1482 * It is up to the caller to make sure that declared_names has been
1483 * populated, if needed.
1485 void PetScan::add_new_used_names(const std::set<std::string> &names)
1487 std::set<std::string>::const_iterator it;
1489 for (it = names.begin(); it != names.end(); ++it) {
1490 if (declared_names.find(*it) != declared_names.end())
1491 continue;
1492 used_names.insert(*it);
1496 /* Is the name "name" used in any declaration other than "decl"?
1498 * If the name was found to be in use before, the consider it to be in use.
1499 * Otherwise, check the DeclContext of the function containing the scop
1500 * as well as all ancestors of this DeclContext for declarations
1501 * other than "decl" that declare something called "name".
1503 bool PetScan::name_in_use(const string &name, Decl *decl)
1505 DeclContext *DC;
1506 DeclContext::decl_iterator it;
1508 if (used_names.find(name) != used_names.end())
1509 return true;
1511 for (DC = decl_context; DC; DC = DC->getParent()) {
1512 for (it = DC->decls_begin(); it != DC->decls_end(); ++it) {
1513 Decl *D = *it;
1514 NamedDecl *named;
1516 if (D == decl)
1517 continue;
1518 if (!isa<NamedDecl>(D))
1519 continue;
1520 named = cast<NamedDecl>(D);
1521 if (named->getName().str() == name)
1522 return true;
1526 return false;
1529 /* Generate a new name based on "name" that is not in use.
1530 * Do so by adding a suffix _i, with i an integer.
1532 string PetScan::generate_new_name(const string &name)
1534 string new_name;
1536 do {
1537 std::ostringstream oss;
1538 oss << name << "_" << n_rename++;
1539 new_name = oss.str();
1540 } while (name_in_use(new_name, NULL));
1542 return new_name;
1545 /* Try and construct a pet_tree corresponding to a compound statement.
1547 * "skip_declarations" is set if we should skip initial declarations
1548 * in the children of the compound statements.
1550 * Collect a new set of declarations for the current compound statement.
1551 * If any of the names in these declarations is also used by another
1552 * declaration reachable from the current function, then rename it
1553 * to a name that is not already in use.
1554 * In particular, keep track of the old and new names in a pet_substituter
1555 * and apply the substitutions to the pet_tree corresponding to the
1556 * compound statement.
1558 __isl_give pet_tree *PetScan::extract(CompoundStmt *stmt,
1559 bool skip_declarations)
1561 pet_tree *tree;
1562 std::vector<VarDecl *> saved_declarations;
1563 std::vector<VarDecl *>::iterator it;
1564 pet_substituter substituter;
1566 saved_declarations = declarations;
1567 declarations.clear();
1568 tree = extract(stmt->children(), true, skip_declarations, stmt);
1569 for (it = declarations.begin(); it != declarations.end(); ++it) {
1570 isl_id *id;
1571 pet_expr *expr;
1572 VarDecl *decl = *it;
1573 string name = decl->getName().str();
1574 bool in_use = name_in_use(name, decl);
1576 used_names.insert(name);
1577 if (!in_use)
1578 continue;
1580 name = generate_new_name(name);
1581 id = pet_id_from_name_and_decl(ctx, name.c_str(), decl);
1582 expr = pet_expr_access_from_id(id, ast_context);
1583 id = pet_id_from_decl(ctx, decl);
1584 substituter.add_sub(id, expr);
1585 used_names.insert(name);
1587 tree = substituter.substitute(tree);
1588 declarations = saved_declarations;
1590 return tree;
1593 /* Return the file offset of the expansion location of "Loc".
1595 static unsigned getExpansionOffset(SourceManager &SM, SourceLocation Loc)
1597 return SM.getFileOffset(SM.getExpansionLoc(Loc));
1600 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
1602 /* Return a SourceLocation for the location after the first semicolon
1603 * after "loc". If Lexer::findLocationAfterToken is available, we simply
1604 * call it and also skip trailing spaces and newline.
1606 static SourceLocation location_after_semi(SourceLocation loc, SourceManager &SM,
1607 const LangOptions &LO)
1609 return Lexer::findLocationAfterToken(loc, tok::semi, SM, LO, true);
1612 #else
1614 /* Return a SourceLocation for the location after the first semicolon
1615 * after "loc". If Lexer::findLocationAfterToken is not available,
1616 * we look in the underlying character data for the first semicolon.
1618 static SourceLocation location_after_semi(SourceLocation loc, SourceManager &SM,
1619 const LangOptions &LO)
1621 const char *semi;
1622 const char *s = SM.getCharacterData(loc);
1624 semi = strchr(s, ';');
1625 if (!semi)
1626 return SourceLocation();
1627 return loc.getFileLocWithOffset(semi + 1 - s);
1630 #endif
1632 /* If the token at "loc" is the first token on the line, then return
1633 * a location referring to the start of the line and set *indent
1634 * to the indentation of "loc"
1635 * Otherwise, return "loc" and set *indent to "".
1637 * This function is used to extend a scop to the start of the line
1638 * if the first token of the scop is also the first token on the line.
1640 * We look for the first token on the line. If its location is equal to "loc",
1641 * then the latter is the location of the first token on the line.
1643 static SourceLocation move_to_start_of_line_if_first_token(SourceLocation loc,
1644 SourceManager &SM, const LangOptions &LO, char **indent)
1646 std::pair<FileID, unsigned> file_offset_pair;
1647 llvm::StringRef file;
1648 const char *pos;
1649 Token tok;
1650 SourceLocation token_loc, line_loc;
1651 int col;
1652 const char *s;
1654 loc = SM.getExpansionLoc(loc);
1655 col = SM.getExpansionColumnNumber(loc);
1656 line_loc = loc.getLocWithOffset(1 - col);
1657 file_offset_pair = SM.getDecomposedLoc(line_loc);
1658 file = SM.getBufferData(file_offset_pair.first, NULL);
1659 pos = file.data() + file_offset_pair.second;
1661 Lexer lexer(SM.getLocForStartOfFile(file_offset_pair.first), LO,
1662 file.begin(), pos, file.end());
1663 lexer.LexFromRawLexer(tok);
1664 token_loc = tok.getLocation();
1666 s = SM.getCharacterData(line_loc);
1667 *indent = strndup(s, token_loc == loc ? col - 1 : 0);
1669 if (token_loc == loc)
1670 return line_loc;
1671 else
1672 return loc;
1675 /* Construct a pet_loc corresponding to the region covered by "range".
1676 * If "skip_semi" is set, then we assume "range" is followed by
1677 * a semicolon and also include this semicolon.
1679 __isl_give pet_loc *PetScan::construct_pet_loc(SourceRange range,
1680 bool skip_semi)
1682 SourceLocation loc = range.getBegin();
1683 SourceManager &SM = PP.getSourceManager();
1684 const LangOptions &LO = PP.getLangOpts();
1685 int line = PP.getSourceManager().getExpansionLineNumber(loc);
1686 unsigned start, end;
1687 char *indent;
1689 loc = move_to_start_of_line_if_first_token(loc, SM, LO, &indent);
1690 start = getExpansionOffset(SM, loc);
1691 loc = range.getEnd();
1692 if (skip_semi)
1693 loc = location_after_semi(loc, SM, LO);
1694 else
1695 loc = PP.getLocForEndOfToken(loc);
1696 end = getExpansionOffset(SM, loc);
1698 return pet_loc_alloc(ctx, start, end, line, indent);
1701 /* Convert a top-level pet_expr to an expression pet_tree.
1703 __isl_give pet_tree *PetScan::extract(__isl_take pet_expr *expr,
1704 SourceRange range, bool skip_semi)
1706 pet_loc *loc;
1707 pet_tree *tree;
1709 tree = pet_tree_new_expr(expr);
1710 loc = construct_pet_loc(range, skip_semi);
1711 tree = pet_tree_set_loc(tree, loc);
1713 return tree;
1716 /* Construct a pet_tree for an if statement.
1718 __isl_give pet_tree *PetScan::extract(IfStmt *stmt)
1720 pet_expr *pe_cond;
1721 pet_tree *tree, *tree_else;
1723 pe_cond = extract_expr(stmt->getCond());
1724 tree = extract(stmt->getThen());
1725 if (stmt->getElse()) {
1726 tree_else = extract(stmt->getElse());
1727 if (options->autodetect) {
1728 if (tree && !tree_else) {
1729 partial = true;
1730 pet_expr_free(pe_cond);
1731 return tree;
1733 if (!tree && tree_else) {
1734 partial = true;
1735 pet_expr_free(pe_cond);
1736 return tree_else;
1739 tree = pet_tree_new_if_else(pe_cond, tree, tree_else);
1740 } else
1741 tree = pet_tree_new_if(pe_cond, tree);
1742 return tree;
1745 /* Is "parent" a compound statement that has "stmt" as its final child?
1747 static bool final_in_compound(ReturnStmt *stmt, Stmt *parent)
1749 CompoundStmt *c;
1751 c = dyn_cast<CompoundStmt>(parent);
1752 if (c) {
1753 StmtIterator i;
1754 Stmt *last;
1755 StmtRange range = c->children();
1757 for (i = range.first; i != range.second; ++i)
1758 last = *i;
1759 return last == stmt;
1761 return false;
1764 /* Try and construct a pet_tree for a return statement "stmt".
1766 * Return statements are only allowed in a context where
1767 * this->return_root has been set.
1768 * Furthermore, "stmt" should appear as the last child
1769 * in the compound statement this->return_root.
1771 __isl_give pet_tree *PetScan::extract(ReturnStmt *stmt)
1773 pet_expr *val;
1775 if (!return_root) {
1776 report_unsupported_return(stmt);
1777 return NULL;
1779 if (!final_in_compound(stmt, return_root)) {
1780 report_return_not_at_end_of_function(stmt);
1781 return NULL;
1784 val = extract_expr(stmt->getRetValue());
1785 return pet_tree_new_return(val);
1788 /* Try and construct a pet_tree for a label statement.
1790 __isl_give pet_tree *PetScan::extract(LabelStmt *stmt)
1792 isl_id *label;
1793 pet_tree *tree;
1795 label = isl_id_alloc(ctx, stmt->getName(), NULL);
1797 tree = extract(stmt->getSubStmt());
1798 tree = pet_tree_set_label(tree, label);
1799 return tree;
1802 /* Update the location of "tree" to include the source range of "stmt".
1804 * Actually, we create a new location based on the source range of "stmt" and
1805 * then extend this new location to include the region of the original location.
1806 * This ensures that the line number of the final location refers to "stmt".
1808 __isl_give pet_tree *PetScan::update_loc(__isl_take pet_tree *tree, Stmt *stmt)
1810 pet_loc *loc, *tree_loc;
1812 tree_loc = pet_tree_get_loc(tree);
1813 loc = construct_pet_loc(stmt->getSourceRange(), false);
1814 loc = pet_loc_update_start_end_from_loc(loc, tree_loc);
1815 pet_loc_free(tree_loc);
1817 tree = pet_tree_set_loc(tree, loc);
1818 return tree;
1821 /* Is "expr" of a type that can be converted to an access expression?
1823 static bool is_access_expr_type(Expr *expr)
1825 switch (expr->getStmtClass()) {
1826 case Stmt::ArraySubscriptExprClass:
1827 case Stmt::DeclRefExprClass:
1828 case Stmt::MemberExprClass:
1829 return true;
1830 default:
1831 return false;
1835 /* Tell the pet_inliner "inliner" about the formal arguments
1836 * in "fd" and the corresponding actual arguments in "call".
1837 * Return 0 if this was successful and -1 otherwise.
1839 * Any pointer argument is treated as an array.
1840 * The other arguments are treated as scalars.
1842 * In case of scalars, there is no restriction on the actual argument.
1843 * This actual argument is assigned to a variable with a name
1844 * that is derived from the name of the corresponding formal argument,
1845 * but made not to conflict with any variable names that are
1846 * already in use.
1848 * In case of arrays, the actual argument needs to be an expression
1849 * of a type that can be converted to an access expression or the address
1850 * of such an expression, ignoring implicit and redundant casts.
1852 int PetScan::set_inliner_arguments(pet_inliner &inliner, CallExpr *call,
1853 FunctionDecl *fd)
1855 unsigned n;
1857 n = fd->getNumParams();
1858 for (unsigned i = 0; i < n; ++i) {
1859 ParmVarDecl *parm = fd->getParamDecl(i);
1860 QualType type = parm->getType();
1861 Expr *arg, *sub;
1862 pet_expr *expr;
1863 int is_addr = 0;
1865 arg = call->getArg(i);
1866 if (pet_clang_array_depth(type) == 0) {
1867 string name = parm->getName().str();
1868 if (name_in_use(name, NULL))
1869 name = generate_new_name(name);
1870 used_names.insert(name);
1871 inliner.add_scalar_arg(parm, name, extract_expr(arg));
1872 continue;
1874 arg = pet_clang_strip_casts(arg);
1875 sub = extract_addr_of_arg(arg);
1876 if (sub) {
1877 is_addr = 1;
1878 arg = pet_clang_strip_casts(sub);
1880 if (!is_access_expr_type(arg)) {
1881 report_unsupported_inline_function_argument(arg);
1882 return -1;
1884 expr = extract_access_expr(arg);
1885 if (!expr)
1886 return -1;
1887 inliner.add_array_arg(parm, expr, is_addr);
1890 return 0;
1893 /* Internal data structure for PetScan::substitute_array_sizes.
1894 * ps is the PetScan on which the method was called.
1895 * substituter is the substituter that is used to substitute variables
1896 * in the size expressions.
1898 struct pet_substitute_array_sizes_data {
1899 PetScan *ps;
1900 pet_substituter *substituter;
1903 extern "C" {
1904 static int substitute_array_size(__isl_keep pet_tree *tree, void *user);
1907 /* If "tree" is a declaration, then perform the substitutions
1908 * in data->substituter on its size expression and store the result
1909 * in the size expression cache of data->ps such that the modified expression
1910 * will be used in subsequent calls to get_array_size.
1912 static int substitute_array_size(__isl_keep pet_tree *tree, void *user)
1914 struct pet_substitute_array_sizes_data *data;
1915 isl_id *id;
1916 pet_expr *var, *size;
1918 if (!pet_tree_is_decl(tree))
1919 return 0;
1921 data = (struct pet_substitute_array_sizes_data *) user;
1922 var = pet_tree_decl_get_var(tree);
1923 id = pet_expr_access_get_id(var);
1924 pet_expr_free(var);
1926 size = data->ps->get_array_size(id);
1927 size = data->substituter->substitute(size);
1928 data->ps->set_array_size(id, size);
1930 return 0;
1933 /* Perform the substitutions in "substituter" on all the arrays declared
1934 * inside "tree" and store the results in the size expression cache
1935 * such that the modified expressions will be used in subsequent calls
1936 * to get_array_size.
1938 int PetScan::substitute_array_sizes(__isl_keep pet_tree *tree,
1939 pet_substituter *substituter)
1941 struct pet_substitute_array_sizes_data data = { this, substituter };
1943 return pet_tree_foreach_sub_tree(tree, &substitute_array_size, &data);
1946 /* Try and construct a pet_tree from the body of "fd" using the actual
1947 * arguments in "call" in place of the formal arguments.
1948 * "fd" is assumed to point to the declaration with a function body.
1949 * In particular, construct a block that consists of assignments
1950 * of (parts of) the actual arguments to temporary variables
1951 * followed by the inlined function body with the formal arguments
1952 * replaced by (expressions containing) these temporary variables.
1953 * If "return_id" is set, then it is used to store the return value
1954 * of the inlined function.
1956 * The actual inlining is taken care of by the pet_inliner object.
1957 * This function merely calls set_inliner_arguments to tell
1958 * the pet_inliner about the actual arguments, extracts a pet_tree
1959 * from the body of the called function and then passes this pet_tree
1960 * to the pet_inliner.
1961 * The body of the called function is allowed to have a return statement
1962 * at the end.
1963 * The substitutions performed by the inliner are also applied
1964 * to the size expressions of the arrays declared in the inlined
1965 * function. These size expressions are not stored in the tree
1966 * itself, but rather in the size expression cache.
1968 * During the extraction of the function body, all variables names
1969 * that are declared in the calling function as well all variable
1970 * names that are known to be in use are considered to be in use
1971 * in the called function to ensure that there is no naming conflict.
1972 * Similarly, the additional names that are in use in the called function
1973 * are considered to be in use in the calling function as well.
1975 * The location of the pet_tree is reset to the call site to ensure
1976 * that the extent of the scop does not include the body of the called
1977 * function.
1979 __isl_give pet_tree *PetScan::extract_inlined_call(CallExpr *call,
1980 FunctionDecl *fd, __isl_keep isl_id *return_id)
1982 int save_autodetect;
1983 pet_tree *tree;
1984 pet_loc *tree_loc;
1985 pet_inliner inliner(ctx, n_arg, ast_context);
1987 if (set_inliner_arguments(inliner, call, fd) < 0)
1988 return NULL;
1990 save_autodetect = options->autodetect;
1991 options->autodetect = 0;
1992 PetScan body_scan(PP, ast_context, fd, loc, options,
1993 isl_union_map_copy(value_bounds), independent);
1994 collect_declared_names();
1995 body_scan.add_new_used_names(declared_names);
1996 body_scan.add_new_used_names(used_names);
1997 body_scan.return_root = fd->getBody();
1998 tree = body_scan.extract(fd->getBody(), false);
1999 add_new_used_names(body_scan.used_names);
2000 options->autodetect = save_autodetect;
2002 tree_loc = construct_pet_loc(call->getSourceRange(), true);
2003 tree = pet_tree_set_loc(tree, tree_loc);
2005 substitute_array_sizes(tree, &inliner);
2007 return inliner.inline_tree(tree, return_id);
2010 /* Try and construct a pet_tree corresponding
2011 * to the expression statement "stmt".
2013 * First look for function calls that have corresponding bodies
2014 * marked "inline". Extract the inlined functions in a pet_inlined_calls
2015 * object. Then extract the statement itself, replacing calls
2016 * to inlined function by accesses to the corresponding return variables, and
2017 * return the combined result.
2018 * If the outer expression is itself a call to an inlined function,
2019 * then it already appears as one of the inlined functions and
2020 * no separate pet_tree needs to be extracted for "stmt" itself.
2022 __isl_give pet_tree *PetScan::extract_expr_stmt(Stmt *stmt)
2024 pet_expr *expr;
2025 pet_tree *tree;
2026 pet_inlined_calls ic(this);
2028 ic.collect(stmt);
2029 if (ic.calls.size() >= 1 && ic.calls[0] == stmt) {
2030 tree = pet_tree_new_block(ctx, 0, 0);
2031 } else {
2032 call2id = &ic.call2id;
2033 expr = extract_expr(cast<Expr>(stmt));
2034 tree = extract(expr, stmt->getSourceRange(), true);
2035 call2id = NULL;
2037 tree = ic.add_inlined(tree);
2038 return tree;
2041 /* Try and construct a pet_tree corresponding to "stmt".
2043 * If "stmt" is a compound statement, then "skip_declarations"
2044 * indicates whether we should skip initial declarations in the
2045 * compound statement.
2047 * If the constructed pet_tree is not a (possibly) partial representation
2048 * of "stmt", we update start and end of the pet_scop to those of "stmt".
2049 * In particular, if skip_declarations is set, then we may have skipped
2050 * declarations inside "stmt" and so the pet_scop may not represent
2051 * the entire "stmt".
2052 * Note that this function may be called with "stmt" referring to the entire
2053 * body of the function, including the outer braces. In such cases,
2054 * skip_declarations will be set and the braces will not be taken into
2055 * account in tree->loc.
2057 __isl_give pet_tree *PetScan::extract(Stmt *stmt, bool skip_declarations)
2059 pet_tree *tree;
2061 set_current_stmt(stmt);
2063 if (isa<Expr>(stmt))
2064 return extract_expr_stmt(cast<Expr>(stmt));
2066 switch (stmt->getStmtClass()) {
2067 case Stmt::WhileStmtClass:
2068 tree = extract(cast<WhileStmt>(stmt));
2069 break;
2070 case Stmt::ForStmtClass:
2071 tree = extract_for(cast<ForStmt>(stmt));
2072 break;
2073 case Stmt::IfStmtClass:
2074 tree = extract(cast<IfStmt>(stmt));
2075 break;
2076 case Stmt::CompoundStmtClass:
2077 tree = extract(cast<CompoundStmt>(stmt), skip_declarations);
2078 break;
2079 case Stmt::LabelStmtClass:
2080 tree = extract(cast<LabelStmt>(stmt));
2081 break;
2082 case Stmt::ContinueStmtClass:
2083 tree = pet_tree_new_continue(ctx);
2084 break;
2085 case Stmt::BreakStmtClass:
2086 tree = pet_tree_new_break(ctx);
2087 break;
2088 case Stmt::DeclStmtClass:
2089 tree = extract(cast<DeclStmt>(stmt));
2090 break;
2091 case Stmt::NullStmtClass:
2092 tree = pet_tree_new_block(ctx, 0, 0);
2093 break;
2094 case Stmt::ReturnStmtClass:
2095 tree = extract(cast<ReturnStmt>(stmt));
2096 break;
2097 default:
2098 report_unsupported_statement_type(stmt);
2099 return NULL;
2102 if (partial || skip_declarations)
2103 return tree;
2105 return update_loc(tree, stmt);
2108 /* Given a sequence of statements "stmt_range" of which the first "n_decl"
2109 * are declarations and of which the remaining statements are represented
2110 * by "tree", try and extend "tree" to include the last sequence of
2111 * the initial declarations that can be completely extracted.
2113 * We start collecting the initial declarations and start over
2114 * whenever we come across a declaration that we cannot extract.
2115 * If we have been able to extract any declarations, then we
2116 * copy over the contents of "tree" at the end of the declarations.
2117 * Otherwise, we simply return the original "tree".
2119 __isl_give pet_tree *PetScan::insert_initial_declarations(
2120 __isl_take pet_tree *tree, int n_decl, StmtRange stmt_range)
2122 StmtIterator i;
2123 pet_tree *res;
2124 int n_stmt;
2125 int is_block;
2126 int j;
2128 n_stmt = pet_tree_block_n_child(tree);
2129 is_block = pet_tree_block_get_block(tree);
2130 res = pet_tree_new_block(ctx, is_block, n_decl + n_stmt);
2132 for (i = stmt_range.first; n_decl; ++i, --n_decl) {
2133 Stmt *child = *i;
2134 pet_tree *tree_i;
2136 tree_i = extract(child);
2137 if (tree_i && !partial) {
2138 res = pet_tree_block_add_child(res, tree_i);
2139 continue;
2141 pet_tree_free(tree_i);
2142 partial = false;
2143 if (pet_tree_block_n_child(res) == 0)
2144 continue;
2145 pet_tree_free(res);
2146 res = pet_tree_new_block(ctx, is_block, n_decl + n_stmt);
2149 if (pet_tree_block_n_child(res) == 0) {
2150 pet_tree_free(res);
2151 return tree;
2154 for (j = 0; j < n_stmt; ++j) {
2155 pet_tree *tree_i;
2157 tree_i = pet_tree_block_get_child(tree, j);
2158 res = pet_tree_block_add_child(res, tree_i);
2160 pet_tree_free(tree);
2162 return res;
2165 /* Try and construct a pet_tree corresponding to (part of)
2166 * a sequence of statements.
2168 * "block" is set if the sequence represents the children of
2169 * a compound statement.
2170 * "skip_declarations" is set if we should skip initial declarations
2171 * in the sequence of statements.
2172 * "parent" is the statement that has stmt_range as (some of) its children.
2174 * If autodetect is set, then we allow the extraction of only a subrange
2175 * of the sequence of statements. However, if there is at least one
2176 * kill and there is some subsequent statement for which we could not
2177 * construct a tree, then turn off the "block" property of the tree
2178 * such that no extra kill will be introduced at the end of the (partial)
2179 * block. If, on the other hand, the final range contains
2180 * no statements, then we discard the entire range.
2181 * If only a subrange of the sequence was extracted, but each statement
2182 * in the sequence was extracted completely, and if there are some
2183 * variable declarations in the sequence before or inside
2184 * the extracted subrange, then check if any of these variables are
2185 * not used after the extracted subrange. If so, add kills to these
2186 * variables.
2188 * If the entire range was extracted, apart from some initial declarations,
2189 * then we try and extend the range with the latest of those initial
2190 * declarations.
2192 __isl_give pet_tree *PetScan::extract(StmtRange stmt_range, bool block,
2193 bool skip_declarations, Stmt *parent)
2195 StmtIterator i;
2196 int j, skip;
2197 bool has_kills = false;
2198 bool partial_range = false;
2199 bool outer_partial = false;
2200 pet_tree *tree;
2201 SourceManager &SM = PP.getSourceManager();
2202 pet_killed_locals kl(SM);
2203 unsigned range_start, range_end;
2205 for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j)
2208 tree = pet_tree_new_block(ctx, block, j);
2210 skip = 0;
2211 i = stmt_range.first;
2212 if (skip_declarations)
2213 for (; i != stmt_range.second; ++i) {
2214 if ((*i)->getStmtClass() != Stmt::DeclStmtClass)
2215 break;
2216 if (options->autodetect)
2217 kl.add_locals(cast<DeclStmt>(*i));
2218 ++skip;
2221 for (; i != stmt_range.second; ++i) {
2222 Stmt *child = *i;
2223 pet_tree *tree_i;
2225 tree_i = extract(child);
2226 if (pet_tree_block_n_child(tree) != 0 && partial) {
2227 pet_tree_free(tree_i);
2228 break;
2230 if (child->getStmtClass() == Stmt::DeclStmtClass) {
2231 if (options->autodetect)
2232 kl.add_locals(cast<DeclStmt>(child));
2233 if (tree_i && block)
2234 has_kills = true;
2236 if (options->autodetect) {
2237 if (tree_i) {
2238 range_end = getExpansionOffset(SM,
2239 child->getLocEnd());
2240 if (pet_tree_block_n_child(tree) == 0)
2241 range_start = getExpansionOffset(SM,
2242 child->getLocStart());
2243 tree = pet_tree_block_add_child(tree, tree_i);
2244 } else {
2245 partial_range = true;
2247 if (pet_tree_block_n_child(tree) != 0 && !tree_i)
2248 outer_partial = partial = true;
2249 } else {
2250 tree = pet_tree_block_add_child(tree, tree_i);
2253 if (partial || !tree)
2254 break;
2257 if (!tree)
2258 return NULL;
2260 if (partial) {
2261 if (has_kills)
2262 tree = pet_tree_block_set_block(tree, 0);
2263 if (outer_partial) {
2264 kl.remove_accessed_after(parent,
2265 range_start, range_end);
2266 tree = add_kills(tree, kl.locals);
2268 } else if (partial_range) {
2269 if (pet_tree_block_n_child(tree) == 0) {
2270 pet_tree_free(tree);
2271 return NULL;
2273 partial = true;
2274 } else if (skip > 0)
2275 tree = insert_initial_declarations(tree, skip, stmt_range);
2277 return tree;
2280 extern "C" {
2281 static __isl_give pet_expr *get_array_size(__isl_keep pet_expr *access,
2282 void *user);
2283 static struct pet_array *extract_array(__isl_keep pet_expr *access,
2284 __isl_keep pet_context *pc, void *user);
2287 /* Construct a pet_expr that holds the sizes of the array accessed
2288 * by "access".
2289 * This function is used as a callback to pet_context_add_parameters,
2290 * which is also passed a pointer to the PetScan object.
2292 static __isl_give pet_expr *get_array_size(__isl_keep pet_expr *access,
2293 void *user)
2295 PetScan *ps = (PetScan *) user;
2296 isl_id *id;
2297 pet_expr *size;
2299 id = pet_expr_access_get_id(access);
2300 size = ps->get_array_size(id);
2301 isl_id_free(id);
2303 return size;
2306 /* Construct and return a pet_array corresponding to the variable
2307 * accessed by "access".
2308 * This function is used as a callback to pet_scop_from_pet_tree,
2309 * which is also passed a pointer to the PetScan object.
2311 static struct pet_array *extract_array(__isl_keep pet_expr *access,
2312 __isl_keep pet_context *pc, void *user)
2314 PetScan *ps = (PetScan *) user;
2315 isl_id *id;
2316 pet_array *array;
2318 id = pet_expr_access_get_id(access);
2319 array = ps->extract_array(id, NULL, pc);
2320 isl_id_free(id);
2322 return array;
2325 /* Extract a function summary from the body of "fd".
2327 * We extract a scop from the function body in a context with as
2328 * parameters the integer arguments of the function.
2329 * We turn off autodetection (in case it was set) to ensure that
2330 * the entire function body is considered.
2331 * We then collect the accessed array elements and attach them
2332 * to the corresponding array arguments, taking into account
2333 * that the function body may access members of array elements.
2334 * The function body is allowed to have a return statement at the end.
2336 * The reason for representing the integer arguments as parameters in
2337 * the context is that if we were to instead start with a context
2338 * with the function arguments as initial dimensions, then we would not
2339 * be able to refer to them from the array extents, without turning
2340 * array extents into maps.
2342 * The result is stored in the summary_cache cache so that we can reuse
2343 * it if this method gets called on the same function again later on.
2345 __isl_give pet_function_summary *PetScan::get_summary(FunctionDecl *fd)
2347 isl_space *space;
2348 isl_set *domain;
2349 pet_context *pc;
2350 pet_tree *tree;
2351 pet_function_summary *summary;
2352 unsigned n;
2353 ScopLoc loc;
2354 int save_autodetect;
2355 struct pet_scop *scop;
2356 int int_size;
2357 isl_union_set *may_read, *may_write, *must_write;
2358 isl_union_map *to_inner;
2360 if (summary_cache.find(fd) != summary_cache.end())
2361 return pet_function_summary_copy(summary_cache[fd]);
2363 space = isl_space_set_alloc(ctx, 0, 0);
2365 n = fd->getNumParams();
2366 summary = pet_function_summary_alloc(ctx, n);
2367 for (unsigned i = 0; i < n; ++i) {
2368 ParmVarDecl *parm = fd->getParamDecl(i);
2369 QualType type = parm->getType();
2370 isl_id *id;
2372 if (!type->isIntegerType())
2373 continue;
2374 id = pet_id_from_decl(ctx, parm);
2375 space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2376 space = isl_space_set_dim_id(space, isl_dim_param, 0,
2377 isl_id_copy(id));
2378 summary = pet_function_summary_set_int(summary, i, id);
2381 save_autodetect = options->autodetect;
2382 options->autodetect = 0;
2383 PetScan body_scan(PP, ast_context, fd, loc, options,
2384 isl_union_map_copy(value_bounds), independent);
2386 body_scan.return_root = fd->getBody();
2387 tree = body_scan.extract(fd->getBody(), false);
2389 domain = isl_set_universe(space);
2390 pc = pet_context_alloc(domain);
2391 pc = pet_context_add_parameters(pc, tree,
2392 &::get_array_size, &body_scan);
2393 int_size = size_in_bytes(ast_context, ast_context.IntTy);
2394 scop = pet_scop_from_pet_tree(tree, int_size,
2395 &::extract_array, &body_scan, pc);
2396 scop = scan_arrays(scop, pc);
2397 may_read = isl_union_map_range(pet_scop_get_may_reads(scop));
2398 may_write = isl_union_map_range(pet_scop_get_may_writes(scop));
2399 must_write = isl_union_map_range(pet_scop_get_must_writes(scop));
2400 to_inner = pet_scop_compute_outer_to_inner(scop);
2401 pet_scop_free(scop);
2403 for (unsigned i = 0; i < n; ++i) {
2404 ParmVarDecl *parm = fd->getParamDecl(i);
2405 QualType type = parm->getType();
2406 struct pet_array *array;
2407 isl_space *space;
2408 isl_union_set *data_set;
2409 isl_union_set *may_read_i, *may_write_i, *must_write_i;
2411 if (pet_clang_array_depth(type) == 0)
2412 continue;
2414 array = body_scan.extract_array(parm, NULL, pc);
2415 space = array ? isl_set_get_space(array->extent) : NULL;
2416 pet_array_free(array);
2417 data_set = isl_union_set_from_set(isl_set_universe(space));
2418 data_set = isl_union_set_apply(data_set,
2419 isl_union_map_copy(to_inner));
2420 may_read_i = isl_union_set_intersect(
2421 isl_union_set_copy(may_read),
2422 isl_union_set_copy(data_set));
2423 may_write_i = isl_union_set_intersect(
2424 isl_union_set_copy(may_write),
2425 isl_union_set_copy(data_set));
2426 must_write_i = isl_union_set_intersect(
2427 isl_union_set_copy(must_write), data_set);
2428 summary = pet_function_summary_set_array(summary, i,
2429 may_read_i, may_write_i, must_write_i);
2432 isl_union_set_free(may_read);
2433 isl_union_set_free(may_write);
2434 isl_union_set_free(must_write);
2435 isl_union_map_free(to_inner);
2437 options->autodetect = save_autodetect;
2438 pet_context_free(pc);
2440 summary_cache[fd] = pet_function_summary_copy(summary);
2442 return summary;
2445 /* If "fd" has a function body, then extract a function summary from
2446 * this body and attach it to the call expression "expr".
2448 * Even if a function body is available, "fd" itself may point
2449 * to a declaration without function body. We therefore first
2450 * replace it by the declaration that comes with a body (if any).
2452 __isl_give pet_expr *PetScan::set_summary(__isl_take pet_expr *expr,
2453 FunctionDecl *fd)
2455 pet_function_summary *summary;
2457 if (!expr)
2458 return NULL;
2459 fd = pet_clang_find_function_decl_with_body(fd);
2460 if (!fd)
2461 return expr;
2463 summary = get_summary(fd);
2465 expr = pet_expr_call_set_summary(expr, summary);
2467 return expr;
2470 /* Extract a pet_scop from "tree".
2472 * We simply call pet_scop_from_pet_tree with the appropriate arguments and
2473 * then add pet_arrays for all accessed arrays.
2474 * We populate the pet_context with assignments for all parameters used
2475 * inside "tree" or any of the size expressions for the arrays accessed
2476 * by "tree" so that they can be used in affine expressions.
2478 struct pet_scop *PetScan::extract_scop(__isl_take pet_tree *tree)
2480 int int_size;
2481 isl_set *domain;
2482 pet_context *pc;
2483 pet_scop *scop;
2485 int_size = size_in_bytes(ast_context, ast_context.IntTy);
2487 domain = isl_set_universe(isl_space_set_alloc(ctx, 0, 0));
2488 pc = pet_context_alloc(domain);
2489 pc = pet_context_add_parameters(pc, tree, &::get_array_size, this);
2490 scop = pet_scop_from_pet_tree(tree, int_size,
2491 &::extract_array, this, pc);
2492 scop = scan_arrays(scop, pc);
2493 pet_context_free(pc);
2495 return scop;
2498 /* Add a call to __pencil_kill to the end of "tree" that kills
2499 * all the variables in "locals" and return the result.
2501 * No location is added to the kill because the most natural
2502 * location would lie outside the scop. Attaching such a location
2503 * to this tree would extend the scope of the final result
2504 * to include the location.
2506 __isl_give pet_tree *PetScan::add_kills(__isl_take pet_tree *tree,
2507 set<ValueDecl *> locals)
2509 int i;
2510 pet_expr *expr;
2511 pet_tree *kill, *block;
2512 set<ValueDecl *>::iterator it;
2514 if (locals.size() == 0)
2515 return tree;
2516 expr = pet_expr_new_call(ctx, "__pencil_kill", locals.size());
2517 i = 0;
2518 for (it = locals.begin(); it != locals.end(); ++it) {
2519 pet_expr *arg;
2520 arg = extract_access_expr(*it);
2521 expr = pet_expr_set_arg(expr, i++, arg);
2523 kill = pet_tree_new_expr(expr);
2524 block = pet_tree_new_block(ctx, 0, 2);
2525 block = pet_tree_block_add_child(block, tree);
2526 block = pet_tree_block_add_child(block, kill);
2528 return block;
2531 /* Check if the scop marked by the user is exactly this Stmt
2532 * or part of this Stmt.
2533 * If so, return a pet_scop corresponding to the marked region.
2534 * Otherwise, return NULL.
2536 * If the scop is not further nested inside a child of "stmt",
2537 * then check if there are any variable declarations before the scop
2538 * inside "stmt". If so, and if these variables are not used
2539 * after the scop, then add kills to the variables.
2541 * If the scop starts in the middle of one of the children, without
2542 * also ending in that child, then report an error.
2544 struct pet_scop *PetScan::scan(Stmt *stmt)
2546 SourceManager &SM = PP.getSourceManager();
2547 unsigned start_off, end_off;
2548 pet_tree *tree;
2550 start_off = getExpansionOffset(SM, stmt->getLocStart());
2551 end_off = getExpansionOffset(SM, stmt->getLocEnd());
2553 if (start_off > loc.end)
2554 return NULL;
2555 if (end_off < loc.start)
2556 return NULL;
2558 if (start_off >= loc.start && end_off <= loc.end)
2559 return extract_scop(extract(stmt));
2561 pet_killed_locals kl(SM);
2562 StmtIterator start;
2563 for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
2564 Stmt *child = *start;
2565 if (!child)
2566 continue;
2567 start_off = getExpansionOffset(SM, child->getLocStart());
2568 end_off = getExpansionOffset(SM, child->getLocEnd());
2569 if (start_off < loc.start && end_off >= loc.end)
2570 return scan(child);
2571 if (start_off >= loc.start)
2572 break;
2573 if (loc.start < end_off) {
2574 report_unbalanced_pragmas(loc.scop, loc.endscop);
2575 return NULL;
2577 if (isa<DeclStmt>(child))
2578 kl.add_locals(cast<DeclStmt>(child));
2581 StmtIterator end;
2582 for (end = start; end != stmt->child_end(); ++end) {
2583 Stmt *child = *end;
2584 start_off = SM.getFileOffset(child->getLocStart());
2585 if (start_off >= loc.end)
2586 break;
2589 kl.remove_accessed_after(stmt, loc.start, loc.end);
2591 tree = extract(StmtRange(start, end), false, false, stmt);
2592 tree = add_kills(tree, kl.locals);
2593 return extract_scop(tree);
2596 /* Set the size of index "pos" of "array" to "size".
2597 * In particular, add a constraint of the form
2599 * i_pos < size
2601 * to array->extent and a constraint of the form
2603 * size >= 0
2605 * to array->context.
2607 * The domain of "size" is assumed to be zero-dimensional.
2609 static struct pet_array *update_size(struct pet_array *array, int pos,
2610 __isl_take isl_pw_aff *size)
2612 isl_set *valid;
2613 isl_set *univ;
2614 isl_set *bound;
2615 isl_space *dim;
2616 isl_aff *aff;
2617 isl_pw_aff *index;
2618 isl_id *id;
2620 if (!array)
2621 goto error;
2623 valid = isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size)));
2624 array->context = isl_set_intersect(array->context, valid);
2626 dim = isl_set_get_space(array->extent);
2627 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2628 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, 1);
2629 univ = isl_set_universe(isl_aff_get_domain_space(aff));
2630 index = isl_pw_aff_alloc(univ, aff);
2632 size = isl_pw_aff_add_dims(size, isl_dim_in,
2633 isl_set_dim(array->extent, isl_dim_set));
2634 id = isl_set_get_tuple_id(array->extent);
2635 size = isl_pw_aff_set_tuple_id(size, isl_dim_in, id);
2636 bound = isl_pw_aff_lt_set(index, size);
2638 array->extent = isl_set_intersect(array->extent, bound);
2640 if (!array->context || !array->extent)
2641 return pet_array_free(array);
2643 return array;
2644 error:
2645 isl_pw_aff_free(size);
2646 return NULL;
2649 #ifdef HAVE_DECAYEDTYPE
2651 /* If "qt" is a decayed type, then set *decayed to true and
2652 * return the original type.
2654 static QualType undecay(QualType qt, bool *decayed)
2656 const Type *type = qt.getTypePtr();
2658 *decayed = isa<DecayedType>(type);
2659 if (*decayed)
2660 qt = cast<DecayedType>(type)->getOriginalType();
2661 return qt;
2664 #else
2666 /* If "qt" is a decayed type, then set *decayed to true and
2667 * return the original type.
2668 * Since this version of clang does not define a DecayedType,
2669 * we cannot obtain the original type even if it had been decayed and
2670 * we set *decayed to false.
2672 static QualType undecay(QualType qt, bool *decayed)
2674 *decayed = false;
2675 return qt;
2678 #endif
2680 /* Figure out the size of the array at position "pos" and all
2681 * subsequent positions from "qt" and update the corresponding
2682 * argument of "expr" accordingly.
2684 * The initial type (when pos is zero) may be a pointer type decayed
2685 * from an array type, if this initial type is the type of a function
2686 * argument. This only happens if the original array type has
2687 * a constant size in the outer dimension as otherwise we get
2688 * a VariableArrayType. Try and obtain this original type (if available) and
2689 * take the outer array size into account if it was marked static.
2691 __isl_give pet_expr *PetScan::set_upper_bounds(__isl_take pet_expr *expr,
2692 QualType qt, int pos)
2694 const ArrayType *atype;
2695 pet_expr *size;
2696 bool decayed = false;
2698 if (!expr)
2699 return NULL;
2701 if (pos == 0)
2702 qt = undecay(qt, &decayed);
2704 if (qt->isPointerType()) {
2705 qt = qt->getPointeeType();
2706 return set_upper_bounds(expr, qt, pos + 1);
2708 if (!qt->isArrayType())
2709 return expr;
2711 qt = qt->getCanonicalTypeInternal();
2712 atype = cast<ArrayType>(qt.getTypePtr());
2714 if (decayed && atype->getSizeModifier() != ArrayType::Static) {
2715 qt = atype->getElementType();
2716 return set_upper_bounds(expr, qt, pos + 1);
2719 if (qt->isConstantArrayType()) {
2720 const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
2721 size = extract_expr(ca->getSize());
2722 expr = pet_expr_set_arg(expr, pos, size);
2723 } else if (qt->isVariableArrayType()) {
2724 const VariableArrayType *vla = cast<VariableArrayType>(atype);
2725 size = extract_expr(vla->getSizeExpr());
2726 expr = pet_expr_set_arg(expr, pos, size);
2729 qt = atype->getElementType();
2731 return set_upper_bounds(expr, qt, pos + 1);
2734 /* Construct a pet_expr that holds the sizes of the array represented by "id".
2735 * The returned expression is a call expression with as arguments
2736 * the sizes in each dimension. If we are unable to derive the size
2737 * in a given dimension, then the corresponding argument is set to infinity.
2738 * In fact, we initialize all arguments to infinity and then update
2739 * them if we are able to figure out the size.
2741 * The result is stored in the id_size cache so that it can be reused
2742 * if this method is called on the same array identifier later.
2743 * The result is also stored in the type_size cache in case
2744 * it gets called on a different array identifier with the same type.
2746 __isl_give pet_expr *PetScan::get_array_size(__isl_keep isl_id *id)
2748 QualType qt = pet_id_get_array_type(id);
2749 int depth;
2750 pet_expr *expr, *inf;
2751 const Type *type = qt.getTypePtr();
2752 isl_maybe_pet_expr m;
2754 m = isl_id_to_pet_expr_try_get(id_size, id);
2755 if (m.valid < 0 || m.valid)
2756 return m.value;
2757 if (type_size.find(type) != type_size.end())
2758 return pet_expr_copy(type_size[type]);
2760 depth = pet_clang_array_depth(qt);
2761 inf = pet_expr_new_int(isl_val_infty(ctx));
2762 expr = pet_expr_new_call(ctx, "bounds", depth);
2763 for (int i = 0; i < depth; ++i)
2764 expr = pet_expr_set_arg(expr, i, pet_expr_copy(inf));
2765 pet_expr_free(inf);
2767 expr = set_upper_bounds(expr, qt, 0);
2768 type_size[type] = pet_expr_copy(expr);
2769 id_size = isl_id_to_pet_expr_set(id_size, isl_id_copy(id),
2770 pet_expr_copy(expr));
2772 return expr;
2775 /* Set the array size of the array identified by "id" to "size",
2776 * replacing any previously stored value.
2778 void PetScan::set_array_size(__isl_take isl_id *id, __isl_take pet_expr *size)
2780 id_size = isl_id_to_pet_expr_set(id_size, id, size);
2783 /* Does "expr" represent the "integer" infinity?
2785 static int is_infty(__isl_keep pet_expr *expr)
2787 isl_val *v;
2788 int res;
2790 if (pet_expr_get_type(expr) != pet_expr_int)
2791 return 0;
2792 v = pet_expr_int_get_val(expr);
2793 res = isl_val_is_infty(v);
2794 isl_val_free(v);
2796 return res;
2799 /* Figure out the dimensions of an array "array" and
2800 * update "array" accordingly.
2802 * We first construct a pet_expr that holds the sizes of the array
2803 * in each dimension. The resulting expression may containing
2804 * infinity values for dimension where we are unable to derive
2805 * a size expression.
2807 * The arguments of the size expression that have a value different from
2808 * infinity are then converted to an affine expression
2809 * within the context "pc" and incorporated into the size of "array".
2810 * If we are unable to convert a size expression to an affine expression or
2811 * if the size is not a (symbolic) constant,
2812 * then we leave the corresponding size of "array" untouched.
2814 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
2815 __isl_keep pet_context *pc)
2817 int n;
2818 isl_id *id;
2819 pet_expr *expr;
2821 if (!array)
2822 return NULL;
2824 id = isl_set_get_tuple_id(array->extent);
2825 expr = get_array_size(id);
2826 isl_id_free(id);
2828 n = pet_expr_get_n_arg(expr);
2829 for (int i = 0; i < n; ++i) {
2830 pet_expr *arg;
2831 isl_pw_aff *size;
2833 arg = pet_expr_get_arg(expr, i);
2834 if (!is_infty(arg)) {
2835 int dim;
2837 size = pet_expr_extract_affine(arg, pc);
2838 dim = isl_pw_aff_dim(size, isl_dim_in);
2839 if (!size)
2840 array = pet_array_free(array);
2841 else if (isl_pw_aff_involves_nan(size) ||
2842 isl_pw_aff_involves_dims(size, isl_dim_in, 0, dim))
2843 isl_pw_aff_free(size);
2844 else {
2845 size = isl_pw_aff_drop_dims(size,
2846 isl_dim_in, 0, dim);
2847 array = update_size(array, i, size);
2850 pet_expr_free(arg);
2852 pet_expr_free(expr);
2854 return array;
2857 /* Does "decl" have a definition that we can keep track of in a pet_type?
2859 static bool has_printable_definition(RecordDecl *decl)
2861 if (!decl->getDeclName())
2862 return false;
2863 return decl->getLexicalDeclContext() == decl->getDeclContext();
2866 /* Add all TypedefType objects that appear when dereferencing "type"
2867 * to "types".
2869 static void insert_intermediate_typedefs(PetTypes *types, QualType type)
2871 type = pet_clang_base_or_typedef_type(type);
2872 while (isa<TypedefType>(type)) {
2873 const TypedefType *tt;
2875 tt = cast<TypedefType>(type);
2876 types->insert(tt->getDecl());
2877 type = tt->desugar();
2878 type = pet_clang_base_or_typedef_type(type);
2882 /* Construct and return a pet_array corresponding to the variable
2883 * represented by "id".
2884 * In particular, initialize array->extent to
2886 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2888 * and then call set_upper_bounds to set the upper bounds on the indices
2889 * based on the type of the variable. The upper bounds are converted
2890 * to affine expressions within the context "pc".
2892 * If the base type is that of a record with a top-level definition or
2893 * of a typedef and if "types" is not null, then the RecordDecl or
2894 * TypedefType corresponding to the type, as well as any intermediate
2895 * TypedefType, is added to "types".
2897 * If the base type is that of a record with no top-level definition,
2898 * then we replace it by "<subfield>".
2900 * If the variable is a scalar, i.e., a zero-dimensional array,
2901 * then the "const" qualifier, if any, is removed from the base type.
2902 * This makes it easier for users of pet to turn initializations
2903 * into assignments.
2905 struct pet_array *PetScan::extract_array(__isl_keep isl_id *id,
2906 PetTypes *types, __isl_keep pet_context *pc)
2908 struct pet_array *array;
2909 QualType qt = pet_id_get_array_type(id);
2910 int depth = pet_clang_array_depth(qt);
2911 QualType base = pet_clang_base_type(qt);
2912 string name;
2913 isl_space *space;
2915 array = isl_calloc_type(ctx, struct pet_array);
2916 if (!array)
2917 return NULL;
2919 space = isl_space_set_alloc(ctx, 0, depth);
2920 space = isl_space_set_tuple_id(space, isl_dim_set, isl_id_copy(id));
2922 array->extent = isl_set_nat_universe(space);
2924 space = isl_space_params_alloc(ctx, 0);
2925 array->context = isl_set_universe(space);
2927 array = set_upper_bounds(array, pc);
2928 if (!array)
2929 return NULL;
2931 if (depth == 0)
2932 base.removeLocalConst();
2933 name = base.getAsString();
2935 if (types) {
2936 insert_intermediate_typedefs(types, qt);
2937 if (isa<TypedefType>(base)) {
2938 types->insert(cast<TypedefType>(base)->getDecl());
2939 } else if (base->isRecordType()) {
2940 RecordDecl *decl = pet_clang_record_decl(base);
2941 TypedefNameDecl *typedecl;
2942 typedecl = decl->getTypedefNameForAnonDecl();
2943 if (typedecl)
2944 types->insert(typedecl);
2945 else if (has_printable_definition(decl))
2946 types->insert(decl);
2947 else
2948 name = "<subfield>";
2952 array->element_type = strdup(name.c_str());
2953 array->element_is_record = base->isRecordType();
2954 array->element_size = size_in_bytes(ast_context, base);
2956 return array;
2959 /* Construct and return a pet_array corresponding to the variable "decl".
2961 struct pet_array *PetScan::extract_array(ValueDecl *decl,
2962 PetTypes *types, __isl_keep pet_context *pc)
2964 isl_id *id;
2965 pet_array *array;
2967 id = pet_id_from_decl(ctx, decl);
2968 array = extract_array(id, types, pc);
2969 isl_id_free(id);
2971 return array;
2974 /* Construct and return a pet_array corresponding to the sequence
2975 * of declarations represented by "decls".
2976 * The upper bounds of the array are converted to affine expressions
2977 * within the context "pc".
2978 * If the sequence contains a single declaration, then it corresponds
2979 * to a simple array access. Otherwise, it corresponds to a member access,
2980 * with the declaration for the substructure following that of the containing
2981 * structure in the sequence of declarations.
2982 * We start with the outermost substructure and then combine it with
2983 * information from the inner structures.
2985 * Additionally, keep track of all required types in "types".
2987 struct pet_array *PetScan::extract_array(__isl_keep isl_id_list *decls,
2988 PetTypes *types, __isl_keep pet_context *pc)
2990 int i, n;
2991 isl_id *id;
2992 struct pet_array *array;
2994 id = isl_id_list_get_id(decls, 0);
2995 array = extract_array(id, types, pc);
2996 isl_id_free(id);
2998 n = isl_id_list_n_id(decls);
2999 for (i = 1; i < n; ++i) {
3000 struct pet_array *parent;
3001 const char *base_name, *field_name;
3002 char *product_name;
3004 parent = array;
3005 id = isl_id_list_get_id(decls, i);
3006 array = extract_array(id, types, pc);
3007 isl_id_free(id);
3008 if (!array)
3009 return pet_array_free(parent);
3011 base_name = isl_set_get_tuple_name(parent->extent);
3012 field_name = isl_set_get_tuple_name(array->extent);
3013 product_name = pet_array_member_access_name(ctx,
3014 base_name, field_name);
3016 array->extent = isl_set_product(isl_set_copy(parent->extent),
3017 array->extent);
3018 if (product_name)
3019 array->extent = isl_set_set_tuple_name(array->extent,
3020 product_name);
3021 array->context = isl_set_intersect(array->context,
3022 isl_set_copy(parent->context));
3024 pet_array_free(parent);
3025 free(product_name);
3027 if (!array->extent || !array->context || !product_name)
3028 return pet_array_free(array);
3031 return array;
3034 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3035 RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3036 std::set<TypeDecl *> &types_done);
3037 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3038 TypedefNameDecl *decl, Preprocessor &PP, PetTypes &types,
3039 std::set<TypeDecl *> &types_done);
3041 /* For each of the fields of "decl" that is itself a record type
3042 * or a typedef, or an array of such type, add a corresponding pet_type
3043 * to "scop".
3045 static struct pet_scop *add_field_types(isl_ctx *ctx, struct pet_scop *scop,
3046 RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3047 std::set<TypeDecl *> &types_done)
3049 RecordDecl::field_iterator it;
3051 for (it = decl->field_begin(); it != decl->field_end(); ++it) {
3052 QualType type = it->getType();
3054 type = pet_clang_base_or_typedef_type(type);
3055 if (isa<TypedefType>(type)) {
3056 TypedefNameDecl *typedefdecl;
3058 typedefdecl = cast<TypedefType>(type)->getDecl();
3059 scop = add_type(ctx, scop, typedefdecl,
3060 PP, types, types_done);
3061 } else if (type->isRecordType()) {
3062 RecordDecl *record;
3064 record = pet_clang_record_decl(type);
3065 scop = add_type(ctx, scop, record,
3066 PP, types, types_done);
3070 return scop;
3073 /* Add a pet_type corresponding to "decl" to "scop", provided
3074 * it is a member of types.records and it has not been added before
3075 * (i.e., it is not a member of "types_done").
3077 * Since we want the user to be able to print the types
3078 * in the order in which they appear in the scop, we need to
3079 * make sure that types of fields in a structure appear before
3080 * that structure. We therefore call ourselves recursively
3081 * through add_field_types on the types of all record subfields.
3083 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3084 RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3085 std::set<TypeDecl *> &types_done)
3087 string s;
3088 llvm::raw_string_ostream S(s);
3090 if (types.records.find(decl) == types.records.end())
3091 return scop;
3092 if (types_done.find(decl) != types_done.end())
3093 return scop;
3095 add_field_types(ctx, scop, decl, PP, types, types_done);
3097 if (strlen(decl->getName().str().c_str()) == 0)
3098 return scop;
3100 decl->print(S, PrintingPolicy(PP.getLangOpts()));
3101 S.str();
3103 scop->types[scop->n_type] = pet_type_alloc(ctx,
3104 decl->getName().str().c_str(), s.c_str());
3105 if (!scop->types[scop->n_type])
3106 return pet_scop_free(scop);
3108 types_done.insert(decl);
3110 scop->n_type++;
3112 return scop;
3115 /* Add a pet_type corresponding to "decl" to "scop", provided
3116 * it is a member of types.typedefs and it has not been added before
3117 * (i.e., it is not a member of "types_done").
3119 * If the underlying type is a structure, then we print the typedef
3120 * ourselves since clang does not print the definition of the structure
3121 * in the typedef. We also make sure in this case that the types of
3122 * the fields in the structure are added first.
3123 * Since the definition of the structure also gets printed this way,
3124 * add it to types_done such that it will not be printed again,
3125 * not even without the typedef.
3127 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3128 TypedefNameDecl *decl, Preprocessor &PP, PetTypes &types,
3129 std::set<TypeDecl *> &types_done)
3131 string s;
3132 llvm::raw_string_ostream S(s);
3133 QualType qt = decl->getUnderlyingType();
3135 if (types.typedefs.find(decl) == types.typedefs.end())
3136 return scop;
3137 if (types_done.find(decl) != types_done.end())
3138 return scop;
3140 if (qt->isRecordType()) {
3141 RecordDecl *rec = pet_clang_record_decl(qt);
3143 add_field_types(ctx, scop, rec, PP, types, types_done);
3144 S << "typedef ";
3145 rec->print(S, PrintingPolicy(PP.getLangOpts()));
3146 S << " ";
3147 S << decl->getName();
3148 types_done.insert(rec);
3149 } else {
3150 decl->print(S, PrintingPolicy(PP.getLangOpts()));
3152 S.str();
3154 scop->types[scop->n_type] = pet_type_alloc(ctx,
3155 decl->getName().str().c_str(), s.c_str());
3156 if (!scop->types[scop->n_type])
3157 return pet_scop_free(scop);
3159 types_done.insert(decl);
3161 scop->n_type++;
3163 return scop;
3166 /* Construct a list of pet_arrays, one for each array (or scalar)
3167 * accessed inside "scop", add this list to "scop" and return the result.
3168 * The upper bounds of the arrays are converted to affine expressions
3169 * within the context "pc".
3171 * The context of "scop" is updated with the intersection of
3172 * the contexts of all arrays, i.e., constraints on the parameters
3173 * that ensure that the arrays have a valid (non-negative) size.
3175 * If any of the extracted arrays refers to a member access or
3176 * has a typedef'd type as base type,
3177 * then also add the required types to "scop".
3178 * The typedef types are printed first because their definitions
3179 * may include the definition of a struct and these struct definitions
3180 * should not be printed separately. While the typedef definition
3181 * is being printed, the struct is marked as having been printed as well,
3182 * such that the later printing of the struct by itself can be prevented.
3184 * If the sequence of nested array declarations from which the pet_array
3185 * is extracted appears as the prefix of some other sequence,
3186 * then the pet_array is marked as "outer".
3187 * The arrays that already appear in scop->arrays at the start of
3188 * this function are assumed to be simple arrays, so they are not marked
3189 * as outer.
3191 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop,
3192 __isl_keep pet_context *pc)
3194 int i, n;
3195 array_desc_set arrays, has_sub;
3196 array_desc_set::iterator it;
3197 PetTypes types;
3198 std::set<TypeDecl *> types_done;
3199 std::set<clang::RecordDecl *, less_name>::iterator records_it;
3200 std::set<clang::TypedefNameDecl *, less_name>::iterator typedefs_it;
3201 int n_array;
3202 struct pet_array **scop_arrays;
3204 if (!scop)
3205 return NULL;
3207 pet_scop_collect_arrays(scop, arrays);
3208 if (arrays.size() == 0)
3209 return scop;
3211 n_array = scop->n_array;
3213 scop_arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3214 n_array + arrays.size());
3215 if (!scop_arrays)
3216 goto error;
3217 scop->arrays = scop_arrays;
3219 for (it = arrays.begin(); it != arrays.end(); ++it) {
3220 isl_id_list *list = isl_id_list_copy(*it);
3221 int n = isl_id_list_n_id(list);
3222 list = isl_id_list_drop(list, n - 1, 1);
3223 has_sub.insert(list);
3226 for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
3227 struct pet_array *array;
3228 array = extract_array(*it, &types, pc);
3229 scop->arrays[n_array + i] = array;
3230 if (!scop->arrays[n_array + i])
3231 goto error;
3232 if (has_sub.find(*it) != has_sub.end())
3233 array->outer = 1;
3234 scop->n_array++;
3235 scop->context = isl_set_intersect(scop->context,
3236 isl_set_copy(array->context));
3237 if (!scop->context)
3238 goto error;
3241 n = types.records.size() + types.typedefs.size();
3242 if (n == 0)
3243 return scop;
3245 scop->types = isl_alloc_array(ctx, struct pet_type *, n);
3246 if (!scop->types)
3247 goto error;
3249 for (typedefs_it = types.typedefs.begin();
3250 typedefs_it != types.typedefs.end(); ++typedefs_it)
3251 scop = add_type(ctx, scop, *typedefs_it, PP, types, types_done);
3253 for (records_it = types.records.begin();
3254 records_it != types.records.end(); ++records_it)
3255 scop = add_type(ctx, scop, *records_it, PP, types, types_done);
3257 return scop;
3258 error:
3259 pet_scop_free(scop);
3260 return NULL;
3263 /* Bound all parameters in scop->context to the possible values
3264 * of the corresponding C variable.
3266 static struct pet_scop *add_parameter_bounds(struct pet_scop *scop)
3268 int n;
3270 if (!scop)
3271 return NULL;
3273 n = isl_set_dim(scop->context, isl_dim_param);
3274 for (int i = 0; i < n; ++i) {
3275 isl_id *id;
3276 ValueDecl *decl;
3278 id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
3279 if (pet_nested_in_id(id)) {
3280 isl_id_free(id);
3281 isl_die(isl_set_get_ctx(scop->context),
3282 isl_error_internal,
3283 "unresolved nested parameter", goto error);
3285 decl = pet_id_get_decl(id);
3286 isl_id_free(id);
3288 scop->context = set_parameter_bounds(scop->context, i, decl);
3290 if (!scop->context)
3291 goto error;
3294 return scop;
3295 error:
3296 pet_scop_free(scop);
3297 return NULL;
3300 /* Construct a pet_scop from the given function.
3302 * If the scop was delimited by scop and endscop pragmas, then we override
3303 * the file offsets by those derived from the pragmas.
3305 struct pet_scop *PetScan::scan(FunctionDecl *fd)
3307 pet_scop *scop;
3308 Stmt *stmt;
3310 stmt = fd->getBody();
3312 if (options->autodetect) {
3313 set_current_stmt(stmt);
3314 scop = extract_scop(extract(stmt, true));
3315 } else {
3316 current_line = loc.start_line;
3317 scop = scan(stmt);
3318 scop = pet_scop_update_start_end(scop, loc.start, loc.end);
3320 scop = add_parameter_bounds(scop);
3321 scop = pet_scop_gist(scop, value_bounds);
3323 return scop;
3326 /* Update this->last_line and this->current_line based on the fact
3327 * that we are about to consider "stmt".
3329 void PetScan::set_current_stmt(Stmt *stmt)
3331 SourceLocation loc = stmt->getLocStart();
3332 SourceManager &SM = PP.getSourceManager();
3334 last_line = current_line;
3335 current_line = SM.getExpansionLineNumber(loc);
3338 /* Is the current statement marked by an independent pragma?
3339 * That is, is there an independent pragma on a line between
3340 * the line of the current statement and the line of the previous statement.
3341 * The search is not implemented very efficiently. We currently
3342 * assume that there are only a few independent pragmas, if any.
3344 bool PetScan::is_current_stmt_marked_independent()
3346 for (unsigned i = 0; i < independent.size(); ++i) {
3347 unsigned line = independent[i].line;
3349 if (last_line < line && line < current_line)
3350 return true;
3353 return false;