2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2015 Ecole Normale Superieure. All rights reserved.
4 * Copyright 2015 Sven Verdoolaege. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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>
51 #include <isl/space.h>
54 #include <isl/union_set.h>
67 #include "scop_plus.h"
68 #include "substituter.h"
70 #include "tree2scop.h"
73 using namespace clang
;
75 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
85 return pet_op_post_inc
;
87 return pet_op_post_dec
;
89 return pet_op_pre_inc
;
91 return pet_op_pre_dec
;
97 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
101 return pet_op_add_assign
;
103 return pet_op_sub_assign
;
105 return pet_op_mul_assign
;
107 return pet_op_div_assign
;
109 return pet_op_assign
;
151 #if defined(DECLREFEXPR_CREATE_REQUIRES_BOOL)
152 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
154 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
155 SourceLocation(), var
, false, var
->getInnerLocStart(),
156 var
->getType(), VK_LValue
);
158 #elif defined(DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION)
159 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
161 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
162 SourceLocation(), var
, var
->getInnerLocStart(), var
->getType(),
166 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
168 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
169 var
, var
->getInnerLocStart(), var
->getType(), VK_LValue
);
173 #ifdef GETTYPEINFORETURNSTYPEINFO
175 static int size_in_bytes(ASTContext
&context
, QualType type
)
177 return context
.getTypeInfo(type
).Width
/ 8;
182 static int size_in_bytes(ASTContext
&context
, QualType type
)
184 return context
.getTypeInfo(type
).first
/ 8;
189 /* Check if the element type corresponding to the given array type
190 * has a const qualifier.
192 static bool const_base(QualType qt
)
194 const Type
*type
= qt
.getTypePtr();
196 if (type
->isPointerType())
197 return const_base(type
->getPointeeType());
198 if (type
->isArrayType()) {
199 const ArrayType
*atype
;
200 type
= type
->getCanonicalTypeInternal().getTypePtr();
201 atype
= cast
<ArrayType
>(type
);
202 return const_base(atype
->getElementType());
205 return qt
.isConstQualified();
210 std::map
<const Type
*, pet_expr
*>::iterator it
;
211 std::map
<FunctionDecl
*, pet_function_summary
*>::iterator it_s
;
213 for (it
= type_size
.begin(); it
!= type_size
.end(); ++it
)
214 pet_expr_free(it
->second
);
215 for (it_s
= summary_cache
.begin(); it_s
!= summary_cache
.end(); ++it_s
)
216 pet_function_summary_free(it_s
->second
);
218 isl_union_map_free(value_bounds
);
221 /* Report a diagnostic on the range "range", unless autodetect is set.
223 void PetScan::report(SourceRange range
, unsigned id
)
225 if (options
->autodetect
)
228 SourceLocation loc
= range
.getBegin();
229 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
230 DiagnosticBuilder B
= diag
.Report(loc
, id
) << range
;
233 /* Report a diagnostic on "stmt", unless autodetect is set.
235 void PetScan::report(Stmt
*stmt
, unsigned id
)
237 report(stmt
->getSourceRange(), id
);
240 /* Report a diagnostic on "decl", unless autodetect is set.
242 void PetScan::report(Decl
*decl
, unsigned id
)
244 report(decl
->getSourceRange(), id
);
247 /* Called if we found something we (currently) cannot handle.
248 * We'll provide more informative warnings later.
250 * We only actually complain if autodetect is false.
252 void PetScan::unsupported(Stmt
*stmt
)
254 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
255 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
260 /* Report an unsupported unary operator, unless autodetect is set.
262 void PetScan::report_unsupported_unary_operator(Stmt
*stmt
)
264 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
265 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
266 "this type of unary operator is not supported");
270 /* Report an unsupported statement type, unless autodetect is set.
272 void PetScan::report_unsupported_statement_type(Stmt
*stmt
)
274 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
275 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
276 "this type of statement is not supported");
280 /* Report a missing prototype, unless autodetect is set.
282 void PetScan::report_prototype_required(Stmt
*stmt
)
284 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
285 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
286 "prototype required");
290 /* Report a missing increment, unless autodetect is set.
292 void PetScan::report_missing_increment(Stmt
*stmt
)
294 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
295 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
296 "missing increment");
300 /* Report a missing summary function, unless autodetect is set.
302 void PetScan::report_missing_summary_function(Stmt
*stmt
)
304 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
305 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
306 "missing summary function");
310 /* Report a missing summary function body, unless autodetect is set.
312 void PetScan::report_missing_summary_function_body(Stmt
*stmt
)
314 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
315 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
316 "missing summary function body");
320 /* Report an unsupported argument in a call to an inlined function,
321 * unless autodetect is set.
323 void PetScan::report_unsupported_inline_function_argument(Stmt
*stmt
)
325 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
326 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
327 "unsupported inline function call argument");
331 /* Report an unsupported type of declaration, unless autodetect is set.
333 void PetScan::report_unsupported_declaration(Decl
*decl
)
335 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
336 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
337 "unsupported declaration");
341 /* Extract an integer from "val", which is assumed to be non-negative.
343 static __isl_give isl_val
*extract_unsigned(isl_ctx
*ctx
,
344 const llvm::APInt
&val
)
347 const uint64_t *data
;
349 data
= val
.getRawData();
350 n
= val
.getNumWords();
351 return isl_val_int_from_chunks(ctx
, n
, sizeof(uint64_t), data
);
354 /* Extract an integer from "val". If "is_signed" is set, then "val"
355 * is signed. Otherwise it it unsigned.
357 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
, bool is_signed
,
360 int is_negative
= is_signed
&& val
.isNegative();
366 v
= extract_unsigned(ctx
, val
);
373 /* Extract an integer from "expr".
375 __isl_give isl_val
*PetScan::extract_int(isl_ctx
*ctx
, IntegerLiteral
*expr
)
377 const Type
*type
= expr
->getType().getTypePtr();
378 bool is_signed
= type
->hasSignedIntegerRepresentation();
380 return ::extract_int(ctx
, is_signed
, expr
->getValue());
383 /* Extract an integer from "expr".
384 * Return NULL if "expr" does not (obviously) represent an integer.
386 __isl_give isl_val
*PetScan::extract_int(clang::ParenExpr
*expr
)
388 return extract_int(expr
->getSubExpr());
391 /* Extract an integer from "expr".
392 * Return NULL if "expr" does not (obviously) represent an integer.
394 __isl_give isl_val
*PetScan::extract_int(clang::Expr
*expr
)
396 if (expr
->getStmtClass() == Stmt::IntegerLiteralClass
)
397 return extract_int(ctx
, cast
<IntegerLiteral
>(expr
));
398 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
399 return extract_int(cast
<ParenExpr
>(expr
));
405 /* Extract a pet_expr from the APInt "val", which is assumed
406 * to be non-negative.
408 __isl_give pet_expr
*PetScan::extract_expr(const llvm::APInt
&val
)
410 return pet_expr_new_int(extract_unsigned(ctx
, val
));
413 /* Return the number of bits needed to represent the type of "decl",
414 * if it is an integer type. Otherwise return 0.
415 * If qt is signed then return the opposite of the number of bits.
417 static int get_type_size(ValueDecl
*decl
)
419 return pet_clang_get_type_size(decl
->getType(), decl
->getASTContext());
422 /* Bound parameter "pos" of "set" to the possible values of "decl".
424 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
425 unsigned pos
, ValueDecl
*decl
)
431 ctx
= isl_set_get_ctx(set
);
432 type_size
= get_type_size(decl
);
434 isl_die(ctx
, isl_error_invalid
, "not an integer type",
435 return isl_set_free(set
));
437 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, 0);
438 bound
= isl_val_int_from_ui(ctx
, type_size
);
439 bound
= isl_val_2exp(bound
);
440 bound
= isl_val_sub_ui(bound
, 1);
441 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
, bound
);
443 bound
= isl_val_int_from_ui(ctx
, -type_size
- 1);
444 bound
= isl_val_2exp(bound
);
445 bound
= isl_val_sub_ui(bound
, 1);
446 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
,
447 isl_val_copy(bound
));
448 bound
= isl_val_neg(bound
);
449 bound
= isl_val_sub_ui(bound
, 1);
450 set
= isl_set_lower_bound_val(set
, isl_dim_param
, pos
, bound
);
456 __isl_give pet_expr
*PetScan::extract_index_expr(ImplicitCastExpr
*expr
)
458 return extract_index_expr(expr
->getSubExpr());
461 /* Return the depth of an array of the given type.
463 static int array_depth(QualType qt
)
465 const Type
*type
= qt
.getTypePtr();
467 if (type
->isPointerType())
468 return 1 + array_depth(type
->getPointeeType());
469 if (type
->isArrayType()) {
470 const ArrayType
*atype
;
471 type
= type
->getCanonicalTypeInternal().getTypePtr();
472 atype
= cast
<ArrayType
>(type
);
473 return 1 + array_depth(atype
->getElementType());
478 /* Return the depth of the array accessed by the index expression "index".
479 * If "index" is an affine expression, i.e., if it does not access
480 * any array, then return 1.
481 * If "index" represent a member access, i.e., if its range is a wrapped
482 * relation, then return the sum of the depth of the array of structures
483 * and that of the member inside the structure.
485 static int extract_depth(__isl_keep isl_multi_pw_aff
*index
)
493 if (isl_multi_pw_aff_range_is_wrapping(index
)) {
494 int domain_depth
, range_depth
;
495 isl_multi_pw_aff
*domain
, *range
;
497 domain
= isl_multi_pw_aff_copy(index
);
498 domain
= isl_multi_pw_aff_range_factor_domain(domain
);
499 domain_depth
= extract_depth(domain
);
500 isl_multi_pw_aff_free(domain
);
501 range
= isl_multi_pw_aff_copy(index
);
502 range
= isl_multi_pw_aff_range_factor_range(range
);
503 range_depth
= extract_depth(range
);
504 isl_multi_pw_aff_free(range
);
506 return domain_depth
+ range_depth
;
509 if (!isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
512 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
515 decl
= pet_id_get_decl(id
);
518 return array_depth(decl
->getType());
521 /* Return the depth of the array accessed by the access expression "expr".
523 static int extract_depth(__isl_keep pet_expr
*expr
)
525 isl_multi_pw_aff
*index
;
528 index
= pet_expr_access_get_index(expr
);
529 depth
= extract_depth(index
);
530 isl_multi_pw_aff_free(index
);
535 /* Construct a pet_expr representing an index expression for an access
536 * to the variable referenced by "expr".
538 * If "expr" references an enum constant, then return an integer expression
539 * instead, representing the value of the enum constant.
541 __isl_give pet_expr
*PetScan::extract_index_expr(DeclRefExpr
*expr
)
543 return extract_index_expr(expr
->getDecl());
546 /* Construct a pet_expr representing an index expression for an access
547 * to the variable "decl".
549 * If "decl" is an enum constant, then we return an integer expression
550 * instead, representing the value of the enum constant.
552 __isl_give pet_expr
*PetScan::extract_index_expr(ValueDecl
*decl
)
556 if (isa
<EnumConstantDecl
>(decl
))
557 return extract_expr(cast
<EnumConstantDecl
>(decl
));
559 id
= pet_id_from_decl(ctx
, decl
);
560 return pet_id_create_index_expr(id
);
563 /* Construct a pet_expr representing the index expression "expr"
564 * Return NULL on error.
566 * If "expr" is a reference to an enum constant, then return
567 * an integer expression instead, representing the value of the enum constant.
569 __isl_give pet_expr
*PetScan::extract_index_expr(Expr
*expr
)
571 switch (expr
->getStmtClass()) {
572 case Stmt::ImplicitCastExprClass
:
573 return extract_index_expr(cast
<ImplicitCastExpr
>(expr
));
574 case Stmt::DeclRefExprClass
:
575 return extract_index_expr(cast
<DeclRefExpr
>(expr
));
576 case Stmt::ArraySubscriptExprClass
:
577 return extract_index_expr(cast
<ArraySubscriptExpr
>(expr
));
578 case Stmt::IntegerLiteralClass
:
579 return extract_expr(cast
<IntegerLiteral
>(expr
));
580 case Stmt::MemberExprClass
:
581 return extract_index_expr(cast
<MemberExpr
>(expr
));
588 /* Extract an index expression from the given array subscript expression.
590 * We first extract an index expression from the base.
591 * This will result in an index expression with a range that corresponds
592 * to the earlier indices.
593 * We then extract the current index and let
594 * pet_expr_access_subscript combine the two.
596 __isl_give pet_expr
*PetScan::extract_index_expr(ArraySubscriptExpr
*expr
)
598 Expr
*base
= expr
->getBase();
599 Expr
*idx
= expr
->getIdx();
603 base_expr
= extract_index_expr(base
);
604 index
= extract_expr(idx
);
606 base_expr
= pet_expr_access_subscript(base_expr
, index
);
611 /* Extract an index expression from a member expression.
613 * If the base access (to the structure containing the member)
618 * and the member is called "f", then the member access is of
623 * If the member access is to an anonymous struct, then simply return
627 * If the member access in the source code is of the form
631 * then it is treated as
635 __isl_give pet_expr
*PetScan::extract_index_expr(MemberExpr
*expr
)
637 Expr
*base
= expr
->getBase();
638 FieldDecl
*field
= cast
<FieldDecl
>(expr
->getMemberDecl());
639 pet_expr
*base_index
;
642 base_index
= extract_index_expr(base
);
644 if (expr
->isArrow()) {
645 pet_expr
*index
= pet_expr_new_int(isl_val_zero(ctx
));
646 base_index
= pet_expr_access_subscript(base_index
, index
);
649 if (field
->isAnonymousStructOrUnion())
652 id
= pet_id_from_decl(ctx
, field
);
654 return pet_expr_access_member(base_index
, id
);
657 /* Mark the given access pet_expr as a write.
659 static __isl_give pet_expr
*mark_write(__isl_take pet_expr
*access
)
661 access
= pet_expr_access_set_write(access
, 1);
662 access
= pet_expr_access_set_read(access
, 0);
667 /* Mark the given (read) access pet_expr as also possibly being written.
668 * That is, initialize the may write access relation from the may read relation
669 * and initialize the must write access relation to the empty relation.
671 static __isl_give pet_expr
*mark_may_write(__isl_take pet_expr
*expr
)
673 isl_union_map
*access
;
674 isl_union_map
*empty
;
676 access
= pet_expr_access_get_dependent_access(expr
,
677 pet_expr_access_may_read
);
678 empty
= isl_union_map_empty(isl_union_map_get_space(access
));
679 expr
= pet_expr_access_set_access(expr
, pet_expr_access_may_write
,
681 expr
= pet_expr_access_set_access(expr
, pet_expr_access_must_write
,
687 /* Construct a pet_expr representing a unary operator expression.
689 __isl_give pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
695 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
696 if (op
== pet_op_last
) {
697 report_unsupported_unary_operator(expr
);
701 arg
= extract_expr(expr
->getSubExpr());
703 if (expr
->isIncrementDecrementOp() &&
704 pet_expr_get_type(arg
) == pet_expr_access
) {
705 arg
= mark_write(arg
);
706 arg
= pet_expr_access_set_read(arg
, 1);
709 type_size
= pet_clang_get_type_size(expr
->getType(), ast_context
);
710 return pet_expr_new_unary(type_size
, op
, arg
);
713 /* Construct a pet_expr representing a binary operator expression.
715 * If the top level operator is an assignment and the LHS is an access,
716 * then we mark that access as a write. If the operator is a compound
717 * assignment, the access is marked as both a read and a write.
719 __isl_give pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
725 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
726 if (op
== pet_op_last
) {
731 lhs
= extract_expr(expr
->getLHS());
732 rhs
= extract_expr(expr
->getRHS());
734 if (expr
->isAssignmentOp() &&
735 pet_expr_get_type(lhs
) == pet_expr_access
) {
736 lhs
= mark_write(lhs
);
737 if (expr
->isCompoundAssignmentOp())
738 lhs
= pet_expr_access_set_read(lhs
, 1);
741 type_size
= pet_clang_get_type_size(expr
->getType(), ast_context
);
742 return pet_expr_new_binary(type_size
, op
, lhs
, rhs
);
745 /* Construct a pet_tree for a variable declaration and
746 * add the declaration to the list of declarations
747 * inside the current compound statement.
749 __isl_give pet_tree
*PetScan::extract(Decl
*decl
)
755 if (!isa
<VarDecl
>(decl
)) {
756 report_unsupported_declaration(decl
);
760 vd
= cast
<VarDecl
>(decl
);
761 declarations
.push_back(vd
);
763 lhs
= extract_access_expr(vd
);
764 lhs
= mark_write(lhs
);
766 tree
= pet_tree_new_decl(lhs
);
768 rhs
= extract_expr(vd
->getInit());
769 tree
= pet_tree_new_decl_init(lhs
, rhs
);
775 /* Construct a pet_tree for a variable declaration statement.
776 * If the declaration statement declares multiple variables,
777 * then return a group of pet_trees, one for each declared variable.
779 __isl_give pet_tree
*PetScan::extract(DeclStmt
*stmt
)
784 if (!stmt
->isSingleDecl()) {
785 const DeclGroup
&group
= stmt
->getDeclGroup().getDeclGroup();
787 tree
= pet_tree_new_block(ctx
, 0, n
);
789 for (unsigned i
= 0; i
< n
; ++i
) {
793 tree_i
= extract(group
[i
]);
794 loc
= construct_pet_loc(group
[i
]->getSourceRange(),
796 tree_i
= pet_tree_set_loc(tree_i
, loc
);
797 tree
= pet_tree_block_add_child(tree
, tree_i
);
803 return extract(stmt
->getSingleDecl());
806 /* Construct a pet_expr representing a conditional operation.
808 __isl_give pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
810 pet_expr
*cond
, *lhs
, *rhs
;
812 cond
= extract_expr(expr
->getCond());
813 lhs
= extract_expr(expr
->getTrueExpr());
814 rhs
= extract_expr(expr
->getFalseExpr());
816 return pet_expr_new_ternary(cond
, lhs
, rhs
);
819 __isl_give pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
821 return extract_expr(expr
->getSubExpr());
824 /* Construct a pet_expr representing a floating point value.
826 * If the floating point literal does not appear in a macro,
827 * then we use the original representation in the source code
828 * as the string representation. Otherwise, we use the pretty
829 * printer to produce a string representation.
831 __isl_give pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
835 const LangOptions
&LO
= PP
.getLangOpts();
836 SourceLocation loc
= expr
->getLocation();
838 if (!loc
.isMacroID()) {
839 SourceManager
&SM
= PP
.getSourceManager();
840 unsigned len
= Lexer::MeasureTokenLength(loc
, SM
, LO
);
841 s
= string(SM
.getCharacterData(loc
), len
);
843 llvm::raw_string_ostream
S(s
);
844 expr
->printPretty(S
, 0, PrintingPolicy(LO
));
847 d
= expr
->getValueAsApproximateDouble();
848 return pet_expr_new_double(ctx
, d
, s
.c_str());
851 /* Convert the index expression "index" into an access pet_expr of type "qt".
853 __isl_give pet_expr
*PetScan::extract_access_expr(QualType qt
,
854 __isl_take pet_expr
*index
)
859 depth
= extract_depth(index
);
860 type_size
= pet_clang_get_type_size(qt
, ast_context
);
862 index
= pet_expr_set_type_size(index
, type_size
);
863 index
= pet_expr_access_set_depth(index
, depth
);
868 /* Extract an index expression from "expr" and then convert it into
869 * an access pet_expr.
871 * If "expr" is a reference to an enum constant, then return
872 * an integer expression instead, representing the value of the enum constant.
874 __isl_give pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
878 index
= extract_index_expr(expr
);
880 if (pet_expr_get_type(index
) == pet_expr_int
)
883 return extract_access_expr(expr
->getType(), index
);
886 /* Extract an index expression from "decl" and then convert it into
887 * an access pet_expr.
889 __isl_give pet_expr
*PetScan::extract_access_expr(ValueDecl
*decl
)
891 return extract_access_expr(decl
->getType(), extract_index_expr(decl
));
894 __isl_give pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
896 return extract_expr(expr
->getSubExpr());
899 /* Extract an assume statement from the argument "expr"
900 * of a __pencil_assume statement.
902 __isl_give pet_expr
*PetScan::extract_assume(Expr
*expr
)
904 return pet_expr_new_unary(0, pet_op_assume
, extract_expr(expr
));
907 /* If "expr" is an address-of operator, then return its argument.
908 * Otherwise, return NULL.
910 static Expr
*extract_addr_of_arg(Expr
*expr
)
914 if (expr
->getStmtClass() != Stmt::UnaryOperatorClass
)
916 op
= cast
<UnaryOperator
>(expr
);
917 if (op
->getOpcode() != UO_AddrOf
)
919 return op
->getSubExpr();
922 /* Construct a pet_expr corresponding to the function call argument "expr".
923 * The argument appears in position "pos" of a call to function "fd".
925 * If we are passing along a pointer to an array element
926 * or an entire row or even higher dimensional slice of an array,
927 * then the function being called may write into the array.
929 * We assume here that if the function is declared to take a pointer
930 * to a const type, then the function may only perform a read
931 * and that otherwise, it may either perform a read or a write (or both).
932 * We only perform this check if "detect_writes" is set.
934 __isl_give pet_expr
*PetScan::extract_argument(FunctionDecl
*fd
, int pos
,
935 Expr
*expr
, bool detect_writes
)
939 int is_addr
= 0, is_partial
= 0;
941 expr
= pet_clang_strip_casts(expr
);
942 arg
= extract_addr_of_arg(expr
);
947 res
= extract_expr(expr
);
950 if (array_depth(expr
->getType()) > 0)
952 if (detect_writes
&& (is_addr
|| is_partial
) &&
953 pet_expr_get_type(res
) == pet_expr_access
) {
955 if (!fd
->hasPrototype()) {
956 report_prototype_required(expr
);
957 return pet_expr_free(res
);
959 parm
= fd
->getParamDecl(pos
);
960 if (!const_base(parm
->getType()))
961 res
= mark_may_write(res
);
965 res
= pet_expr_new_unary(0, pet_op_address_of
, res
);
969 /* Find the first FunctionDecl with the given name.
970 * "call" is the corresponding call expression and is only used
971 * for reporting errors.
973 * Return NULL on error.
975 FunctionDecl
*PetScan::find_decl_from_name(CallExpr
*call
, string name
)
977 TranslationUnitDecl
*tu
= ast_context
.getTranslationUnitDecl();
978 DeclContext::decl_iterator begin
= tu
->decls_begin();
979 DeclContext::decl_iterator end
= tu
->decls_end();
980 for (DeclContext::decl_iterator i
= begin
; i
!= end
; ++i
) {
981 FunctionDecl
*fd
= dyn_cast
<FunctionDecl
>(*i
);
984 if (fd
->getName().str().compare(name
) != 0)
988 report_missing_summary_function_body(call
);
991 report_missing_summary_function(call
);
995 /* Return the FunctionDecl for the summary function associated to the
996 * function called by "call".
998 * In particular, if the pencil option is set, then
999 * search for an annotate attribute formatted as
1000 * "pencil_access(name)", where "name" is the name of the summary function.
1002 * If no summary function was specified, then return the FunctionDecl
1003 * that is actually being called.
1005 * Return NULL on error.
1007 FunctionDecl
*PetScan::get_summary_function(CallExpr
*call
)
1009 FunctionDecl
*decl
= call
->getDirectCallee();
1013 if (!options
->pencil
)
1016 specific_attr_iterator
<AnnotateAttr
> begin
, end
, i
;
1017 begin
= decl
->specific_attr_begin
<AnnotateAttr
>();
1018 end
= decl
->specific_attr_end
<AnnotateAttr
>();
1019 for (i
= begin
; i
!= end
; ++i
) {
1020 string attr
= (*i
)->getAnnotation().str();
1022 const char prefix
[] = "pencil_access(";
1023 size_t start
= attr
.find(prefix
);
1024 if (start
== string::npos
)
1026 start
+= strlen(prefix
);
1027 string name
= attr
.substr(start
, attr
.find(')') - start
);
1029 return find_decl_from_name(call
, name
);
1035 /* Construct a pet_expr representing a function call.
1037 * In the special case of a "call" to __pencil_assume,
1038 * construct an assume expression instead.
1040 * In the case of a "call" to __pencil_kill, the arguments
1041 * are neither read nor written (only killed), so there
1042 * is no need to check for writes to these arguments.
1044 * __pencil_assume and __pencil_kill are only recognized
1045 * when the pencil option is set.
1047 __isl_give pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1049 pet_expr
*res
= NULL
;
1055 fd
= expr
->getDirectCallee();
1061 name
= fd
->getDeclName().getAsString();
1062 n_arg
= expr
->getNumArgs();
1064 if (options
->pencil
&& n_arg
== 1 && name
== "__pencil_assume")
1065 return extract_assume(expr
->getArg(0));
1066 is_kill
= options
->pencil
&& name
== "__pencil_kill";
1068 res
= pet_expr_new_call(ctx
, name
.c_str(), n_arg
);
1072 for (unsigned i
= 0; i
< n_arg
; ++i
) {
1073 Expr
*arg
= expr
->getArg(i
);
1074 res
= pet_expr_set_arg(res
, i
,
1075 PetScan::extract_argument(fd
, i
, arg
, !is_kill
));
1078 fd
= get_summary_function(expr
);
1080 return pet_expr_free(res
);
1082 res
= set_summary(res
, fd
);
1087 /* Construct a pet_expr representing a (C style) cast.
1089 __isl_give pet_expr
*PetScan::extract_expr(CStyleCastExpr
*expr
)
1094 arg
= extract_expr(expr
->getSubExpr());
1098 type
= expr
->getTypeAsWritten();
1099 return pet_expr_new_cast(type
.getAsString().c_str(), arg
);
1102 /* Construct a pet_expr representing an integer.
1104 __isl_give pet_expr
*PetScan::extract_expr(IntegerLiteral
*expr
)
1106 return pet_expr_new_int(extract_int(expr
));
1109 /* Construct a pet_expr representing the integer enum constant "ecd".
1111 __isl_give pet_expr
*PetScan::extract_expr(EnumConstantDecl
*ecd
)
1114 const llvm::APSInt
&init
= ecd
->getInitVal();
1115 v
= ::extract_int(ctx
, init
.isSigned(), init
);
1116 return pet_expr_new_int(v
);
1119 /* Try and construct a pet_expr representing "expr".
1121 __isl_give pet_expr
*PetScan::extract_expr(Expr
*expr
)
1123 switch (expr
->getStmtClass()) {
1124 case Stmt::UnaryOperatorClass
:
1125 return extract_expr(cast
<UnaryOperator
>(expr
));
1126 case Stmt::CompoundAssignOperatorClass
:
1127 case Stmt::BinaryOperatorClass
:
1128 return extract_expr(cast
<BinaryOperator
>(expr
));
1129 case Stmt::ImplicitCastExprClass
:
1130 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1131 case Stmt::ArraySubscriptExprClass
:
1132 case Stmt::DeclRefExprClass
:
1133 case Stmt::MemberExprClass
:
1134 return extract_access_expr(expr
);
1135 case Stmt::IntegerLiteralClass
:
1136 return extract_expr(cast
<IntegerLiteral
>(expr
));
1137 case Stmt::FloatingLiteralClass
:
1138 return extract_expr(cast
<FloatingLiteral
>(expr
));
1139 case Stmt::ParenExprClass
:
1140 return extract_expr(cast
<ParenExpr
>(expr
));
1141 case Stmt::ConditionalOperatorClass
:
1142 return extract_expr(cast
<ConditionalOperator
>(expr
));
1143 case Stmt::CallExprClass
:
1144 return extract_expr(cast
<CallExpr
>(expr
));
1145 case Stmt::CStyleCastExprClass
:
1146 return extract_expr(cast
<CStyleCastExpr
>(expr
));
1153 /* Check if the given initialization statement is an assignment.
1154 * If so, return that assignment. Otherwise return NULL.
1156 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1158 BinaryOperator
*ass
;
1160 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1163 ass
= cast
<BinaryOperator
>(init
);
1164 if (ass
->getOpcode() != BO_Assign
)
1170 /* Check if the given initialization statement is a declaration
1171 * of a single variable.
1172 * If so, return that declaration. Otherwise return NULL.
1174 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1178 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1181 decl
= cast
<DeclStmt
>(init
);
1183 if (!decl
->isSingleDecl())
1186 return decl
->getSingleDecl();
1189 /* Given the assignment operator in the initialization of a for loop,
1190 * extract the induction variable, i.e., the (integer)variable being
1193 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1200 lhs
= init
->getLHS();
1201 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1206 ref
= cast
<DeclRefExpr
>(lhs
);
1207 decl
= ref
->getDecl();
1208 type
= decl
->getType().getTypePtr();
1210 if (!type
->isIntegerType()) {
1218 /* Given the initialization statement of a for loop and the single
1219 * declaration in this initialization statement,
1220 * extract the induction variable, i.e., the (integer) variable being
1223 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1227 vd
= cast
<VarDecl
>(decl
);
1229 const QualType type
= vd
->getType();
1230 if (!type
->isIntegerType()) {
1235 if (!vd
->getInit()) {
1243 /* Check that op is of the form iv++ or iv--.
1244 * Return a pet_expr representing "1" or "-1" accordingly.
1246 __isl_give pet_expr
*PetScan::extract_unary_increment(
1247 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
1253 if (!op
->isIncrementDecrementOp()) {
1258 sub
= op
->getSubExpr();
1259 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1264 ref
= cast
<DeclRefExpr
>(sub
);
1265 if (ref
->getDecl() != iv
) {
1270 if (op
->isIncrementOp())
1271 v
= isl_val_one(ctx
);
1273 v
= isl_val_negone(ctx
);
1275 return pet_expr_new_int(v
);
1278 /* Check if op is of the form
1282 * and return the increment "expr - iv" as a pet_expr.
1284 __isl_give pet_expr
*PetScan::extract_binary_increment(BinaryOperator
*op
,
1285 clang::ValueDecl
*iv
)
1290 pet_expr
*expr
, *expr_iv
;
1292 if (op
->getOpcode() != BO_Assign
) {
1298 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1303 ref
= cast
<DeclRefExpr
>(lhs
);
1304 if (ref
->getDecl() != iv
) {
1309 expr
= extract_expr(op
->getRHS());
1310 expr_iv
= extract_expr(lhs
);
1312 type_size
= pet_clang_get_type_size(iv
->getType(), ast_context
);
1313 return pet_expr_new_binary(type_size
, pet_op_sub
, expr
, expr_iv
);
1316 /* Check that op is of the form iv += cst or iv -= cst
1317 * and return a pet_expr corresponding to cst or -cst accordingly.
1319 __isl_give pet_expr
*PetScan::extract_compound_increment(
1320 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
1326 BinaryOperatorKind opcode
;
1328 opcode
= op
->getOpcode();
1329 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1333 if (opcode
== BO_SubAssign
)
1337 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1342 ref
= cast
<DeclRefExpr
>(lhs
);
1343 if (ref
->getDecl() != iv
) {
1348 expr
= extract_expr(op
->getRHS());
1351 type_size
= pet_clang_get_type_size(op
->getType(), ast_context
);
1352 expr
= pet_expr_new_unary(type_size
, pet_op_minus
, expr
);
1358 /* Check that the increment of the given for loop increments
1359 * (or decrements) the induction variable "iv" and return
1360 * the increment as a pet_expr if successful.
1362 __isl_give pet_expr
*PetScan::extract_increment(clang::ForStmt
*stmt
,
1365 Stmt
*inc
= stmt
->getInc();
1368 report_missing_increment(stmt
);
1372 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1373 return extract_unary_increment(cast
<UnaryOperator
>(inc
), iv
);
1374 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1375 return extract_compound_increment(
1376 cast
<CompoundAssignOperator
>(inc
), iv
);
1377 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1378 return extract_binary_increment(cast
<BinaryOperator
>(inc
), iv
);
1384 /* Construct a pet_tree for a while loop.
1386 * If we were only able to extract part of the body, then simply
1389 __isl_give pet_tree
*PetScan::extract(WhileStmt
*stmt
)
1394 tree
= extract(stmt
->getBody());
1397 pe_cond
= extract_expr(stmt
->getCond());
1398 tree
= pet_tree_new_while(pe_cond
, tree
);
1403 /* Construct a pet_tree for a for statement.
1404 * The for loop is required to be of one of the following forms
1406 * for (i = init; condition; ++i)
1407 * for (i = init; condition; --i)
1408 * for (i = init; condition; i += constant)
1409 * for (i = init; condition; i -= constant)
1411 * We extract a pet_tree for the body and then include it in a pet_tree
1412 * of type pet_tree_for.
1414 * As a special case, we also allow a for loop of the form
1418 * in which case we return a pet_tree of type pet_tree_infinite_loop.
1420 * If we were only able to extract part of the body, then simply
1423 __isl_give pet_tree
*PetScan::extract_for(ForStmt
*stmt
)
1425 BinaryOperator
*ass
;
1433 pet_expr
*pe_init
, *pe_inc
, *pe_iv
, *pe_cond
;
1435 independent
= is_current_stmt_marked_independent();
1437 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc()) {
1438 tree
= extract(stmt
->getBody());
1441 tree
= pet_tree_new_infinite_loop(tree
);
1445 init
= stmt
->getInit();
1450 if ((ass
= initialization_assignment(init
)) != NULL
) {
1451 iv
= extract_induction_variable(ass
);
1454 lhs
= ass
->getLHS();
1455 rhs
= ass
->getRHS();
1456 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
1457 VarDecl
*var
= extract_induction_variable(init
, decl
);
1461 rhs
= var
->getInit();
1462 lhs
= create_DeclRefExpr(var
);
1464 unsupported(stmt
->getInit());
1468 declared
= !initialization_assignment(stmt
->getInit());
1469 tree
= extract(stmt
->getBody());
1472 pe_iv
= extract_access_expr(iv
);
1473 pe_iv
= mark_write(pe_iv
);
1474 pe_init
= extract_expr(rhs
);
1475 if (!stmt
->getCond())
1476 pe_cond
= pet_expr_new_int(isl_val_one(ctx
));
1478 pe_cond
= extract_expr(stmt
->getCond());
1479 pe_inc
= extract_increment(stmt
, iv
);
1480 tree
= pet_tree_new_for(independent
, declared
, pe_iv
, pe_init
, pe_cond
,
1485 /* Store the names of the variables declared in decl_context
1486 * in the set declared_names. Make sure to only do this once by
1487 * setting declared_names_collected.
1489 void PetScan::collect_declared_names()
1491 DeclContext
*DC
= decl_context
;
1492 DeclContext::decl_iterator it
;
1494 if (declared_names_collected
)
1497 for (it
= DC
->decls_begin(); it
!= DC
->decls_end(); ++it
) {
1501 if (!isa
<NamedDecl
>(D
))
1503 named
= cast
<NamedDecl
>(D
);
1504 declared_names
.insert(named
->getName().str());
1507 declared_names_collected
= true;
1510 /* Add the names in "names" that are not also in this->declared_names
1511 * to this->used_names.
1512 * It is up to the caller to make sure that declared_names has been
1513 * populated, if needed.
1515 void PetScan::add_new_used_names(const std::set
<std::string
> &names
)
1517 std::set
<std::string
>::const_iterator it
;
1519 for (it
= names
.begin(); it
!= names
.end(); ++it
) {
1520 if (declared_names
.find(*it
) != declared_names
.end())
1522 used_names
.insert(*it
);
1526 /* Is the name "name" used in any declaration other than "decl"?
1528 * If the name was found to be in use before, the consider it to be in use.
1529 * Otherwise, check the DeclContext of the function containing the scop
1530 * as well as all ancestors of this DeclContext for declarations
1531 * other than "decl" that declare something called "name".
1533 bool PetScan::name_in_use(const string
&name
, Decl
*decl
)
1536 DeclContext::decl_iterator it
;
1538 if (used_names
.find(name
) != used_names
.end())
1541 for (DC
= decl_context
; DC
; DC
= DC
->getParent()) {
1542 for (it
= DC
->decls_begin(); it
!= DC
->decls_end(); ++it
) {
1548 if (!isa
<NamedDecl
>(D
))
1550 named
= cast
<NamedDecl
>(D
);
1551 if (named
->getName().str() == name
)
1559 /* Generate a new name based on "name" that is not in use.
1560 * Do so by adding a suffix _i, with i an integer.
1562 string
PetScan::generate_new_name(const string
&name
)
1567 std::ostringstream oss
;
1568 oss
<< name
<< "_" << n_rename
++;
1569 new_name
= oss
.str();
1570 } while (name_in_use(new_name
, NULL
));
1575 /* Try and construct a pet_tree corresponding to a compound statement.
1577 * "skip_declarations" is set if we should skip initial declarations
1578 * in the children of the compound statements.
1580 * Collect a new set of declarations for the current compound statement.
1581 * If any of the names in these declarations is also used by another
1582 * declaration reachable from the current function, then rename it
1583 * to a name that is not already in use.
1584 * In particular, keep track of the old and new names in a pet_substituter
1585 * and apply the substitutions to the pet_tree corresponding to the
1586 * compound statement.
1588 __isl_give pet_tree
*PetScan::extract(CompoundStmt
*stmt
,
1589 bool skip_declarations
)
1592 std::vector
<VarDecl
*> saved_declarations
;
1593 std::vector
<VarDecl
*>::iterator it
;
1594 pet_substituter substituter
;
1596 saved_declarations
= declarations
;
1597 declarations
.clear();
1598 tree
= extract(stmt
->children(), true, skip_declarations
);
1599 for (it
= declarations
.begin(); it
!= declarations
.end(); ++it
) {
1602 VarDecl
*decl
= *it
;
1603 string name
= decl
->getName().str();
1604 bool in_use
= name_in_use(name
, decl
);
1606 used_names
.insert(name
);
1610 name
= generate_new_name(name
);
1611 id
= pet_id_from_name_and_decl(ctx
, name
.c_str(), decl
);
1612 expr
= pet_id_create_index_expr(id
);
1613 expr
= extract_access_expr(decl
->getType(), expr
);
1614 id
= pet_id_from_decl(ctx
, decl
);
1615 substituter
.add_sub(id
, expr
);
1616 used_names
.insert(name
);
1618 tree
= substituter
.substitute(tree
);
1619 declarations
= saved_declarations
;
1624 /* Return the file offset of the expansion location of "Loc".
1626 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
1628 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
1631 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
1633 /* Return a SourceLocation for the location after the first semicolon
1634 * after "loc". If Lexer::findLocationAfterToken is available, we simply
1635 * call it and also skip trailing spaces and newline.
1637 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
1638 const LangOptions
&LO
)
1640 return Lexer::findLocationAfterToken(loc
, tok::semi
, SM
, LO
, true);
1645 /* Return a SourceLocation for the location after the first semicolon
1646 * after "loc". If Lexer::findLocationAfterToken is not available,
1647 * we look in the underlying character data for the first semicolon.
1649 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
1650 const LangOptions
&LO
)
1653 const char *s
= SM
.getCharacterData(loc
);
1655 semi
= strchr(s
, ';');
1657 return SourceLocation();
1658 return loc
.getFileLocWithOffset(semi
+ 1 - s
);
1663 /* If the token at "loc" is the first token on the line, then return
1664 * a location referring to the start of the line and set *indent
1665 * to the indentation of "loc"
1666 * Otherwise, return "loc" and set *indent to "".
1668 * This function is used to extend a scop to the start of the line
1669 * if the first token of the scop is also the first token on the line.
1671 * We look for the first token on the line. If its location is equal to "loc",
1672 * then the latter is the location of the first token on the line.
1674 static SourceLocation
move_to_start_of_line_if_first_token(SourceLocation loc
,
1675 SourceManager
&SM
, const LangOptions
&LO
, char **indent
)
1677 std::pair
<FileID
, unsigned> file_offset_pair
;
1678 llvm::StringRef file
;
1681 SourceLocation token_loc
, line_loc
;
1685 loc
= SM
.getExpansionLoc(loc
);
1686 col
= SM
.getExpansionColumnNumber(loc
);
1687 line_loc
= loc
.getLocWithOffset(1 - col
);
1688 file_offset_pair
= SM
.getDecomposedLoc(line_loc
);
1689 file
= SM
.getBufferData(file_offset_pair
.first
, NULL
);
1690 pos
= file
.data() + file_offset_pair
.second
;
1692 Lexer
lexer(SM
.getLocForStartOfFile(file_offset_pair
.first
), LO
,
1693 file
.begin(), pos
, file
.end());
1694 lexer
.LexFromRawLexer(tok
);
1695 token_loc
= tok
.getLocation();
1697 s
= SM
.getCharacterData(line_loc
);
1698 *indent
= strndup(s
, token_loc
== loc
? col
- 1 : 0);
1700 if (token_loc
== loc
)
1706 /* Construct a pet_loc corresponding to the region covered by "range".
1707 * If "skip_semi" is set, then we assume "range" is followed by
1708 * a semicolon and also include this semicolon.
1710 __isl_give pet_loc
*PetScan::construct_pet_loc(SourceRange range
,
1713 SourceLocation loc
= range
.getBegin();
1714 SourceManager
&SM
= PP
.getSourceManager();
1715 const LangOptions
&LO
= PP
.getLangOpts();
1716 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
1717 unsigned start
, end
;
1720 loc
= move_to_start_of_line_if_first_token(loc
, SM
, LO
, &indent
);
1721 start
= getExpansionOffset(SM
, loc
);
1722 loc
= range
.getEnd();
1724 loc
= location_after_semi(loc
, SM
, LO
);
1726 loc
= PP
.getLocForEndOfToken(loc
);
1727 end
= getExpansionOffset(SM
, loc
);
1729 return pet_loc_alloc(ctx
, start
, end
, line
, indent
);
1732 /* Convert a top-level pet_expr to an expression pet_tree.
1734 __isl_give pet_tree
*PetScan::extract(__isl_take pet_expr
*expr
,
1735 SourceRange range
, bool skip_semi
)
1740 tree
= pet_tree_new_expr(expr
);
1741 loc
= construct_pet_loc(range
, skip_semi
);
1742 tree
= pet_tree_set_loc(tree
, loc
);
1747 /* Construct a pet_tree for an if statement.
1749 __isl_give pet_tree
*PetScan::extract(IfStmt
*stmt
)
1752 pet_tree
*tree
, *tree_else
;
1754 pe_cond
= extract_expr(stmt
->getCond());
1755 tree
= extract(stmt
->getThen());
1756 if (stmt
->getElse()) {
1757 tree_else
= extract(stmt
->getElse());
1758 if (options
->autodetect
) {
1759 if (tree
&& !tree_else
) {
1761 pet_expr_free(pe_cond
);
1764 if (!tree
&& tree_else
) {
1766 pet_expr_free(pe_cond
);
1770 tree
= pet_tree_new_if_else(pe_cond
, tree
, tree_else
);
1772 tree
= pet_tree_new_if(pe_cond
, tree
);
1776 /* Try and construct a pet_tree for a label statement.
1778 __isl_give pet_tree
*PetScan::extract(LabelStmt
*stmt
)
1783 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
1785 tree
= extract(stmt
->getSubStmt());
1786 tree
= pet_tree_set_label(tree
, label
);
1790 /* Update the location of "tree" to include the source range of "stmt".
1792 * Actually, we create a new location based on the source range of "stmt" and
1793 * then extend this new location to include the region of the original location.
1794 * This ensures that the line number of the final location refers to "stmt".
1796 __isl_give pet_tree
*PetScan::update_loc(__isl_take pet_tree
*tree
, Stmt
*stmt
)
1798 pet_loc
*loc
, *tree_loc
;
1800 tree_loc
= pet_tree_get_loc(tree
);
1801 loc
= construct_pet_loc(stmt
->getSourceRange(), false);
1802 loc
= pet_loc_update_start_end_from_loc(loc
, tree_loc
);
1803 pet_loc_free(tree_loc
);
1805 tree
= pet_tree_set_loc(tree
, loc
);
1809 /* Is "expr" of a type that can be converted to an access expression?
1811 static bool is_access_expr_type(Expr
*expr
)
1813 switch (expr
->getStmtClass()) {
1814 case Stmt::ArraySubscriptExprClass
:
1815 case Stmt::DeclRefExprClass
:
1816 case Stmt::MemberExprClass
:
1823 /* Tell the pet_inliner "inliner" about the formal arguments
1824 * in "fd" and the corresponding actual arguments in "call".
1825 * Return 0 if this was successful and -1 otherwise.
1827 * Any pointer argument is treated as an array.
1828 * The other arguments are treated as scalars.
1830 * In case of scalars, there is no restriction on the actual argument.
1831 * This actual argument is assigned to a variable with a name
1832 * that is derived from the name of the corresponding formal argument,
1833 * but made not to conflict with any variable names that are
1836 * In case of arrays, the actual argument needs to be an expression
1837 * of a type that can be converted to an access expression or the address
1838 * of such an expression, ignoring implicit and redundant casts.
1840 int PetScan::set_inliner_arguments(pet_inliner
&inliner
, CallExpr
*call
,
1845 n
= fd
->getNumParams();
1846 for (unsigned i
= 0; i
< n
; ++i
) {
1847 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
1848 QualType type
= parm
->getType();
1853 arg
= call
->getArg(i
);
1854 if (array_depth(type
) == 0) {
1855 string name
= parm
->getName().str();
1856 if (name_in_use(name
, NULL
))
1857 name
= generate_new_name(name
);
1858 inliner
.add_scalar_arg(parm
, name
, extract_expr(arg
));
1861 arg
= pet_clang_strip_casts(arg
);
1862 sub
= extract_addr_of_arg(arg
);
1865 arg
= pet_clang_strip_casts(sub
);
1867 if (!is_access_expr_type(arg
)) {
1868 report_unsupported_inline_function_argument(arg
);
1871 expr
= extract_access_expr(arg
);
1874 inliner
.add_array_arg(parm
, expr
, is_addr
);
1880 /* Try and construct a pet_tree from the body of "fd" using the actual
1881 * arguments in "call" in place of the formal arguments.
1882 * "fd" is assumed to point to the declaration with a function body.
1883 * In particular, construct a block that consists of assignments
1884 * of (parts of) the actual arguments to temporary variables
1885 * followed by the inlined function body with the formal arguments
1886 * replaced by (expressions containing) these temporary variables.
1888 * The actual inlining is taken care of by the pet_inliner function.
1889 * This function merely calls set_inliner_arguments to tell
1890 * the pet_inliner about the actual arguments, extracts a pet_tree
1891 * from the body of the called function and then passes this pet_tree
1892 * to the pet_inliner.
1894 * During the extraction of the function body, all variables names
1895 * that are declared in the calling function as well all variable
1896 * names that are known to be in use are considered to be in use
1897 * in the called function to ensure that there is no naming conflict.
1898 * Similarly, the additional names that are in use in the called function
1899 * are considered to be in use in the calling function as well.
1901 * The location of the pet_tree is reset to the call site to ensure
1902 * that the extent of the scop does not include the body of the called
1905 __isl_give pet_tree
*PetScan::extract_inlined_call(CallExpr
*call
,
1908 int save_autodetect
;
1911 pet_inliner
inliner(ctx
, n_arg
, ast_context
);
1913 if (set_inliner_arguments(inliner
, call
, fd
) < 0)
1916 save_autodetect
= options
->autodetect
;
1917 options
->autodetect
= 0;
1918 PetScan
body_scan(PP
, ast_context
, fd
, loc
, options
,
1919 isl_union_map_copy(value_bounds
), independent
);
1920 collect_declared_names();
1921 body_scan
.add_new_used_names(declared_names
);
1922 body_scan
.add_new_used_names(used_names
);
1923 tree
= body_scan
.extract(fd
->getBody(), false);
1924 add_new_used_names(body_scan
.used_names
);
1925 options
->autodetect
= save_autodetect
;
1927 tree_loc
= construct_pet_loc(call
->getSourceRange(), true);
1928 tree
= pet_tree_set_loc(tree
, tree_loc
);
1930 return inliner
.inline_tree(tree
);
1933 /* Try and construct a pet_tree corresponding
1934 * to the expression statement "stmt".
1936 * If the outer expression is a function call and if the corresponding
1937 * function body is marked "inline", then return a pet_tree
1938 * corresponding to the inlined function.
1940 __isl_give pet_tree
*PetScan::extract_expr_stmt(Stmt
*stmt
)
1944 if (stmt
->getStmtClass() == Stmt::CallExprClass
) {
1945 CallExpr
*call
= cast
<CallExpr
>(stmt
);
1946 FunctionDecl
*fd
= call
->getDirectCallee();
1947 fd
= pet_clang_find_function_decl_with_body(fd
);
1948 if (fd
&& fd
->isInlineSpecified())
1949 return extract_inlined_call(call
, fd
);
1952 expr
= extract_expr(cast
<Expr
>(stmt
));
1953 return extract(expr
, stmt
->getSourceRange(), true);
1956 /* Try and construct a pet_tree corresponding to "stmt".
1958 * If "stmt" is a compound statement, then "skip_declarations"
1959 * indicates whether we should skip initial declarations in the
1960 * compound statement.
1962 * If the constructed pet_tree is not a (possibly) partial representation
1963 * of "stmt", we update start and end of the pet_scop to those of "stmt".
1964 * In particular, if skip_declarations is set, then we may have skipped
1965 * declarations inside "stmt" and so the pet_scop may not represent
1966 * the entire "stmt".
1967 * Note that this function may be called with "stmt" referring to the entire
1968 * body of the function, including the outer braces. In such cases,
1969 * skip_declarations will be set and the braces will not be taken into
1970 * account in tree->loc.
1972 __isl_give pet_tree
*PetScan::extract(Stmt
*stmt
, bool skip_declarations
)
1976 set_current_stmt(stmt
);
1978 if (isa
<Expr
>(stmt
))
1979 return extract_expr_stmt(cast
<Expr
>(stmt
));
1981 switch (stmt
->getStmtClass()) {
1982 case Stmt::WhileStmtClass
:
1983 tree
= extract(cast
<WhileStmt
>(stmt
));
1985 case Stmt::ForStmtClass
:
1986 tree
= extract_for(cast
<ForStmt
>(stmt
));
1988 case Stmt::IfStmtClass
:
1989 tree
= extract(cast
<IfStmt
>(stmt
));
1991 case Stmt::CompoundStmtClass
:
1992 tree
= extract(cast
<CompoundStmt
>(stmt
), skip_declarations
);
1994 case Stmt::LabelStmtClass
:
1995 tree
= extract(cast
<LabelStmt
>(stmt
));
1997 case Stmt::ContinueStmtClass
:
1998 tree
= pet_tree_new_continue(ctx
);
2000 case Stmt::BreakStmtClass
:
2001 tree
= pet_tree_new_break(ctx
);
2003 case Stmt::DeclStmtClass
:
2004 tree
= extract(cast
<DeclStmt
>(stmt
));
2007 report_unsupported_statement_type(stmt
);
2011 if (partial
|| skip_declarations
)
2014 return update_loc(tree
, stmt
);
2017 /* Given a sequence of statements "stmt_range" of which the first "n_decl"
2018 * are declarations and of which the remaining statements are represented
2019 * by "tree", try and extend "tree" to include the last sequence of
2020 * the initial declarations that can be completely extracted.
2022 * We start collecting the initial declarations and start over
2023 * whenever we come across a declaration that we cannot extract.
2024 * If we have been able to extract any declarations, then we
2025 * copy over the contents of "tree" at the end of the declarations.
2026 * Otherwise, we simply return the original "tree".
2028 __isl_give pet_tree
*PetScan::insert_initial_declarations(
2029 __isl_take pet_tree
*tree
, int n_decl
, StmtRange stmt_range
)
2037 n_stmt
= pet_tree_block_n_child(tree
);
2038 is_block
= pet_tree_block_get_block(tree
);
2039 res
= pet_tree_new_block(ctx
, is_block
, n_decl
+ n_stmt
);
2041 for (i
= stmt_range
.first
; n_decl
; ++i
, --n_decl
) {
2045 tree_i
= extract(child
);
2046 if (tree_i
&& !partial
) {
2047 res
= pet_tree_block_add_child(res
, tree_i
);
2050 pet_tree_free(tree_i
);
2052 if (pet_tree_block_n_child(res
) == 0)
2055 res
= pet_tree_new_block(ctx
, is_block
, n_decl
+ n_stmt
);
2058 if (pet_tree_block_n_child(res
) == 0) {
2063 for (j
= 0; j
< n_stmt
; ++j
) {
2066 tree_i
= pet_tree_block_get_child(tree
, j
);
2067 res
= pet_tree_block_add_child(res
, tree_i
);
2069 pet_tree_free(tree
);
2074 /* Try and construct a pet_tree corresponding to (part of)
2075 * a sequence of statements.
2077 * "block" is set if the sequence represents the children of
2078 * a compound statement.
2079 * "skip_declarations" is set if we should skip initial declarations
2080 * in the sequence of statements.
2082 * If autodetect is set, then we allow the extraction of only a subrange
2083 * of the sequence of statements. However, if there is at least one
2084 * kill and there is some subsequent statement for which we could not
2085 * construct a tree, then turn off the "block" property of the tree
2086 * such that no extra kill will be introduced at the end of the (partial)
2087 * block. If, on the other hand, the final range contains
2088 * no statements, then we discard the entire range.
2090 * If the entire range was extracted, apart from some initial declarations,
2091 * then we try and extend the range with the latest of those initial
2094 __isl_give pet_tree
*PetScan::extract(StmtRange stmt_range
, bool block
,
2095 bool skip_declarations
)
2099 bool has_kills
= false;
2100 bool partial_range
= false;
2103 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
)
2106 tree
= pet_tree_new_block(ctx
, block
, j
);
2109 i
= stmt_range
.first
;
2110 if (skip_declarations
)
2111 for (; i
!= stmt_range
.second
; ++i
) {
2112 if ((*i
)->getStmtClass() != Stmt::DeclStmtClass
)
2117 for (; i
!= stmt_range
.second
; ++i
) {
2121 tree_i
= extract(child
);
2122 if (pet_tree_block_n_child(tree
) != 0 && partial
) {
2123 pet_tree_free(tree_i
);
2126 if (tree_i
&& child
->getStmtClass() == Stmt::DeclStmtClass
&&
2129 if (options
->autodetect
) {
2131 tree
= pet_tree_block_add_child(tree
, tree_i
);
2133 partial_range
= true;
2134 if (pet_tree_block_n_child(tree
) != 0 && !tree_i
)
2137 tree
= pet_tree_block_add_child(tree
, tree_i
);
2140 if (partial
|| !tree
)
2149 tree
= pet_tree_block_set_block(tree
, 0);
2150 } else if (partial_range
) {
2151 if (pet_tree_block_n_child(tree
) == 0) {
2152 pet_tree_free(tree
);
2156 } else if (skip
> 0)
2157 tree
= insert_initial_declarations(tree
, skip
, stmt_range
);
2163 static __isl_give pet_expr
*get_array_size(__isl_keep pet_expr
*access
,
2165 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
2166 __isl_keep pet_context
*pc
, void *user
);
2169 /* Construct a pet_expr that holds the sizes of the array accessed
2171 * This function is used as a callback to pet_context_add_parameters,
2172 * which is also passed a pointer to the PetScan object.
2174 static __isl_give pet_expr
*get_array_size(__isl_keep pet_expr
*access
,
2177 PetScan
*ps
= (PetScan
*) user
;
2181 id
= pet_expr_access_get_id(access
);
2182 qt
= pet_id_get_array_type(id
);
2184 return ps
->get_array_size(qt
);
2187 /* Construct and return a pet_array corresponding to the variable
2188 * accessed by "access".
2189 * This function is used as a callback to pet_scop_from_pet_tree,
2190 * which is also passed a pointer to the PetScan object.
2192 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
2193 __isl_keep pet_context
*pc
, void *user
)
2195 PetScan
*ps
= (PetScan
*) user
;
2200 ctx
= pet_expr_get_ctx(access
);
2201 id
= pet_expr_access_get_id(access
);
2202 array
= ps
->extract_array(id
, NULL
, pc
);
2208 /* Extract a function summary from the body of "fd".
2210 * We extract a scop from the function body in a context with as
2211 * parameters the integer arguments of the function.
2212 * We turn off autodetection (in case it was set) to ensure that
2213 * the entire function body is considered.
2214 * We then collect the accessed array elements and attach them
2215 * to the corresponding array arguments, taking into account
2216 * that the function body may access members of array elements.
2218 * The reason for representing the integer arguments as parameters in
2219 * the context is that if we were to instead start with a context
2220 * with the function arguments as initial dimensions, then we would not
2221 * be able to refer to them from the array extents, without turning
2222 * array extents into maps.
2224 * The result is stored in the summary_cache cache so that we can reuse
2225 * it if this method gets called on the same function again later on.
2227 __isl_give pet_function_summary
*PetScan::get_summary(FunctionDecl
*fd
)
2233 pet_function_summary
*summary
;
2236 int save_autodetect
;
2237 struct pet_scop
*scop
;
2239 isl_union_set
*may_read
, *may_write
, *must_write
;
2240 isl_union_map
*to_inner
;
2242 if (summary_cache
.find(fd
) != summary_cache
.end())
2243 return pet_function_summary_copy(summary_cache
[fd
]);
2245 space
= isl_space_set_alloc(ctx
, 0, 0);
2247 n
= fd
->getNumParams();
2248 summary
= pet_function_summary_alloc(ctx
, n
);
2249 for (unsigned i
= 0; i
< n
; ++i
) {
2250 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
2251 QualType type
= parm
->getType();
2254 if (!type
->isIntegerType())
2256 id
= pet_id_from_decl(ctx
, parm
);
2257 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2258 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0,
2260 summary
= pet_function_summary_set_int(summary
, i
, id
);
2263 save_autodetect
= options
->autodetect
;
2264 options
->autodetect
= 0;
2265 PetScan
body_scan(PP
, ast_context
, fd
, loc
, options
,
2266 isl_union_map_copy(value_bounds
), independent
);
2268 tree
= body_scan
.extract(fd
->getBody(), false);
2270 domain
= isl_set_universe(space
);
2271 pc
= pet_context_alloc(domain
);
2272 pc
= pet_context_add_parameters(pc
, tree
,
2273 &::get_array_size
, &body_scan
);
2274 int_size
= size_in_bytes(ast_context
, ast_context
.IntTy
);
2275 scop
= pet_scop_from_pet_tree(tree
, int_size
,
2276 &::extract_array
, &body_scan
, pc
);
2277 scop
= scan_arrays(scop
, pc
);
2278 may_read
= isl_union_map_range(pet_scop_get_may_reads(scop
));
2279 may_write
= isl_union_map_range(pet_scop_get_may_writes(scop
));
2280 must_write
= isl_union_map_range(pet_scop_get_must_writes(scop
));
2281 to_inner
= pet_scop_compute_outer_to_inner(scop
);
2282 pet_scop_free(scop
);
2284 for (unsigned i
= 0; i
< n
; ++i
) {
2285 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
2286 QualType type
= parm
->getType();
2287 struct pet_array
*array
;
2289 isl_union_set
*data_set
;
2290 isl_union_set
*may_read_i
, *may_write_i
, *must_write_i
;
2292 if (array_depth(type
) == 0)
2295 array
= body_scan
.extract_array(parm
, NULL
, pc
);
2296 space
= array
? isl_set_get_space(array
->extent
) : NULL
;
2297 pet_array_free(array
);
2298 data_set
= isl_union_set_from_set(isl_set_universe(space
));
2299 data_set
= isl_union_set_apply(data_set
,
2300 isl_union_map_copy(to_inner
));
2301 may_read_i
= isl_union_set_intersect(
2302 isl_union_set_copy(may_read
),
2303 isl_union_set_copy(data_set
));
2304 may_write_i
= isl_union_set_intersect(
2305 isl_union_set_copy(may_write
),
2306 isl_union_set_copy(data_set
));
2307 must_write_i
= isl_union_set_intersect(
2308 isl_union_set_copy(must_write
), data_set
);
2309 summary
= pet_function_summary_set_array(summary
, i
,
2310 may_read_i
, may_write_i
, must_write_i
);
2313 isl_union_set_free(may_read
);
2314 isl_union_set_free(may_write
);
2315 isl_union_set_free(must_write
);
2316 isl_union_map_free(to_inner
);
2318 options
->autodetect
= save_autodetect
;
2319 pet_context_free(pc
);
2321 summary_cache
[fd
] = pet_function_summary_copy(summary
);
2326 /* If "fd" has a function body, then extract a function summary from
2327 * this body and attach it to the call expression "expr".
2329 * Even if a function body is available, "fd" itself may point
2330 * to a declaration without function body. We therefore first
2331 * replace it by the declaration that comes with a body (if any).
2333 __isl_give pet_expr
*PetScan::set_summary(__isl_take pet_expr
*expr
,
2336 pet_function_summary
*summary
;
2340 fd
= pet_clang_find_function_decl_with_body(fd
);
2344 summary
= get_summary(fd
);
2346 expr
= pet_expr_call_set_summary(expr
, summary
);
2351 /* Extract a pet_scop from "tree".
2353 * We simply call pet_scop_from_pet_tree with the appropriate arguments and
2354 * then add pet_arrays for all accessed arrays.
2355 * We populate the pet_context with assignments for all parameters used
2356 * inside "tree" or any of the size expressions for the arrays accessed
2357 * by "tree" so that they can be used in affine expressions.
2359 struct pet_scop
*PetScan::extract_scop(__isl_take pet_tree
*tree
)
2366 int_size
= size_in_bytes(ast_context
, ast_context
.IntTy
);
2368 domain
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
2369 pc
= pet_context_alloc(domain
);
2370 pc
= pet_context_add_parameters(pc
, tree
, &::get_array_size
, this);
2371 scop
= pet_scop_from_pet_tree(tree
, int_size
,
2372 &::extract_array
, this, pc
);
2373 scop
= scan_arrays(scop
, pc
);
2374 pet_context_free(pc
);
2379 /* Given a DeclRefExpr or an ArraySubscriptExpr, return a pointer
2380 * to the base DeclRefExpr.
2381 * If the expression is something other than a nested ArraySubscriptExpr
2382 * with a DeclRefExpr at the base, then return NULL.
2384 static DeclRefExpr
*extract_array_base(Expr
*expr
)
2386 while (isa
<ArraySubscriptExpr
>(expr
)) {
2387 expr
= (cast
<ArraySubscriptExpr
>(expr
))->getBase();
2388 expr
= pet_clang_strip_casts(expr
);
2390 return dyn_cast
<DeclRefExpr
>(expr
);
2393 /* Structure for keeping track of local variables that can be killed
2395 * In particular, variables of interest are first added to "locals"
2396 * Then the Stmt in which the variable declaration appears is scanned
2397 * for any possible leak of a pointer or any use after a specified scop.
2398 * In such cases, the variable is removed from "locals".
2399 * The scop is assumed to appear at the same level of the declaration.
2400 * In particular, it does not appear inside a nested control structure,
2401 * meaning that it is sufficient to look at uses of the variables
2402 * that textually appear after the specified scop.
2404 * locals is the set of variables of interest.
2405 * accessed keeps track of the variables that are accessed inside the scop.
2406 * scop_start is the start of the scop
2407 * scop_end is the end of the scop
2408 * addr_end is the end of the latest visited address_of expression.
2409 * expr_end is the end of the latest handled expression.
2411 struct killed_locals
: RecursiveASTVisitor
<killed_locals
> {
2413 set
<ValueDecl
*> locals
;
2414 set
<ValueDecl
*> accessed
;
2415 unsigned scop_start
;
2420 killed_locals(SourceManager
&SM
) : SM(SM
) {}
2422 void add_local(Decl
*decl
);
2423 void add_locals(DeclStmt
*stmt
);
2424 void set_addr_end(UnaryOperator
*expr
);
2425 bool check_decl_in_expr(Expr
*expr
);
2426 void remove_accessed_after(Stmt
*stmt
, unsigned start
, unsigned end
);
2427 bool VisitUnaryOperator(UnaryOperator
*expr
) {
2428 if (expr
->getOpcode() == UO_AddrOf
)
2432 bool VisitArraySubscriptExpr(ArraySubscriptExpr
*expr
) {
2433 return check_decl_in_expr(expr
);
2435 bool VisitDeclRefExpr(DeclRefExpr
*expr
) {
2436 return check_decl_in_expr(expr
);
2439 set
<ValueDecl
*>::iterator it
;
2440 cerr
<< "local" << endl
;
2441 for (it
= locals
.begin(); it
!= locals
.end(); ++it
)
2443 cerr
<< "accessed" << endl
;
2444 for (it
= accessed
.begin(); it
!= accessed
.end(); ++it
)
2449 /* Add "decl" to the set of local variables, provided it is a ValueDecl.
2451 void killed_locals::add_local(Decl
*decl
)
2455 vd
= dyn_cast
<ValueDecl
>(decl
);
2460 /* Add all variables declared by "stmt" to the set of local variables.
2462 void killed_locals::add_locals(DeclStmt
*stmt
)
2464 if (stmt
->isSingleDecl()) {
2465 add_local(stmt
->getSingleDecl());
2467 const DeclGroup
&group
= stmt
->getDeclGroup().getDeclGroup();
2468 unsigned n
= group
.size();
2469 for (unsigned i
= 0; i
< n
; ++i
)
2470 add_local(group
[i
]);
2474 /* Set this->addr_end to the end of the address_of expression "expr".
2476 void killed_locals::set_addr_end(UnaryOperator
*expr
)
2478 addr_end
= getExpansionOffset(SM
, expr
->getLocEnd());
2481 /* Given an expression of type ArraySubscriptExpr or DeclRefExpr,
2483 * - is the variable used inside the scop?
2484 * - is the variable used after the scop or can a pointer be taken?
2485 * Return true if the traversal should continue.
2487 * Reset the pointer to the end of the latest address-of expression
2488 * such that only the first array or scalar is considered to have
2489 * its address taken. In particular, accesses inside the indices
2490 * of the array should not be considered to have their address taken.
2492 * If the variable is not one of the local variables or
2493 * if the access appears inside an expression that was already handled,
2494 * then simply return.
2496 * Otherwise, the expression is handled and "expr_end" is updated
2497 * to prevent subexpressions with the same base expression
2498 * from being handled as well.
2500 * If a higher-dimensional slice of an array is accessed or
2501 * if the access appears inside an address-of expression,
2502 * then a pointer may leak, so the variable should not be killed.
2503 * Similarly, if the access appears after the end of the scop,
2504 * then the variable should not be killed.
2506 * Otherwise, if the access appears inside the scop, then
2507 * keep track of the fact that the variable was accessed at least once
2510 bool killed_locals::check_decl_in_expr(Expr
*expr
)
2516 unsigned old_addr_end
;
2518 ref
= extract_array_base(expr
);
2522 old_addr_end
= addr_end
;
2525 decl
= ref
->getDecl();
2526 if (locals
.find(decl
) == locals
.end())
2528 loc
= getExpansionOffset(SM
, expr
->getLocStart());
2529 if (loc
<= expr_end
)
2532 expr_end
= getExpansionOffset(SM
, ref
->getLocEnd());
2533 depth
= array_depth(expr
->getType());
2534 if (loc
>= scop_end
|| loc
<= old_addr_end
|| depth
!= 0)
2536 if (loc
>= scop_start
&& loc
<= scop_end
)
2537 accessed
.insert(decl
);
2539 return locals
.size() != 0;
2542 /* Remove the local variables that may be accessed inside "stmt" after
2543 * the scop starting at "start" and ending at "end", or that
2544 * are not accessed at all inside that scop.
2546 * If there are no local variables that could potentially be killed,
2547 * then simply return.
2549 * Otherwise, scan "stmt" for any potential use of the variables
2550 * after the scop. This includes a possible pointer being taken
2551 * to (part of) the variable. If there is any such use, then
2552 * the variable is removed from the set of local variables.
2554 * At the same time, keep track of the variables that are
2555 * used anywhere inside the scop. At the end, replace the local
2556 * variables with the intersection with these accessed variables.
2558 void killed_locals::remove_accessed_after(Stmt
*stmt
, unsigned start
,
2561 set
<ValueDecl
*> accessed_local
;
2563 if (locals
.size() == 0)
2570 set_intersection(locals
.begin(), locals
.end(),
2571 accessed
.begin(), accessed
.end(),
2572 inserter(accessed_local
, accessed_local
.begin()));
2573 locals
= accessed_local
;
2576 /* Add a call to __pencil_kill to the end of "tree" that kills
2577 * all the variables in "locals" and return the result.
2579 * No location is added to the kill because the most natural
2580 * location would lie outside the scop. Attaching such a location
2581 * to this tree would extend the scope of the final result
2582 * to include the location.
2584 __isl_give pet_tree
*PetScan::add_kills(__isl_take pet_tree
*tree
,
2585 set
<ValueDecl
*> locals
)
2589 pet_tree
*kill
, *block
;
2590 set
<ValueDecl
*>::iterator it
;
2592 if (locals
.size() == 0)
2594 expr
= pet_expr_new_call(ctx
, "__pencil_kill", locals
.size());
2596 for (it
= locals
.begin(); it
!= locals
.end(); ++it
) {
2598 arg
= extract_access_expr(*it
);
2599 expr
= pet_expr_set_arg(expr
, i
++, arg
);
2601 kill
= pet_tree_new_expr(expr
);
2602 block
= pet_tree_new_block(ctx
, 0, 2);
2603 block
= pet_tree_block_add_child(block
, tree
);
2604 block
= pet_tree_block_add_child(block
, kill
);
2609 /* Check if the scop marked by the user is exactly this Stmt
2610 * or part of this Stmt.
2611 * If so, return a pet_scop corresponding to the marked region.
2612 * Otherwise, return NULL.
2614 * If the scop is not further nested inside a child of "stmt",
2615 * then check if there are any variable declarations before the scop
2616 * inside "stmt". If so, and if these variables are not used
2617 * after the scop, then add kills to the variables.
2619 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
2621 SourceManager
&SM
= PP
.getSourceManager();
2622 unsigned start_off
, end_off
;
2625 start_off
= getExpansionOffset(SM
, stmt
->getLocStart());
2626 end_off
= getExpansionOffset(SM
, stmt
->getLocEnd());
2628 if (start_off
> loc
.end
)
2630 if (end_off
< loc
.start
)
2633 if (start_off
>= loc
.start
&& end_off
<= loc
.end
)
2634 return extract_scop(extract(stmt
));
2636 killed_locals
kl(SM
);
2638 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
2639 Stmt
*child
= *start
;
2642 start_off
= getExpansionOffset(SM
, child
->getLocStart());
2643 end_off
= getExpansionOffset(SM
, child
->getLocEnd());
2644 if (start_off
< loc
.start
&& end_off
>= loc
.end
)
2646 if (start_off
>= loc
.start
)
2648 if (isa
<DeclStmt
>(child
))
2649 kl
.add_locals(cast
<DeclStmt
>(child
));
2653 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
2655 start_off
= SM
.getFileOffset(child
->getLocStart());
2656 if (start_off
>= loc
.end
)
2660 kl
.remove_accessed_after(stmt
, loc
.start
, loc
.end
);
2662 tree
= extract(StmtRange(start
, end
), false, false);
2663 tree
= add_kills(tree
, kl
.locals
);
2664 return extract_scop(tree
);
2667 /* Set the size of index "pos" of "array" to "size".
2668 * In particular, add a constraint of the form
2672 * to array->extent and a constraint of the form
2676 * to array->context.
2678 * The domain of "size" is assumed to be zero-dimensional.
2680 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
2681 __isl_take isl_pw_aff
*size
)
2694 valid
= isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
)));
2695 array
->context
= isl_set_intersect(array
->context
, valid
);
2697 dim
= isl_set_get_space(array
->extent
);
2698 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2699 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
2700 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
2701 index
= isl_pw_aff_alloc(univ
, aff
);
2703 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
2704 isl_set_dim(array
->extent
, isl_dim_set
));
2705 id
= isl_set_get_tuple_id(array
->extent
);
2706 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
2707 bound
= isl_pw_aff_lt_set(index
, size
);
2709 array
->extent
= isl_set_intersect(array
->extent
, bound
);
2711 if (!array
->context
|| !array
->extent
)
2712 return pet_array_free(array
);
2716 isl_pw_aff_free(size
);
2720 #ifdef HAVE_DECAYEDTYPE
2722 /* If "qt" is a decayed type, then set *decayed to true and
2723 * return the original type.
2725 static QualType
undecay(QualType qt
, bool *decayed
)
2727 const Type
*type
= qt
.getTypePtr();
2729 *decayed
= isa
<DecayedType
>(type
);
2731 qt
= cast
<DecayedType
>(type
)->getOriginalType();
2737 /* If "qt" is a decayed type, then set *decayed to true and
2738 * return the original type.
2739 * Since this version of clang does not define a DecayedType,
2740 * we cannot obtain the original type even if it had been decayed and
2741 * we set *decayed to false.
2743 static QualType
undecay(QualType qt
, bool *decayed
)
2751 /* Figure out the size of the array at position "pos" and all
2752 * subsequent positions from "qt" and update the corresponding
2753 * argument of "expr" accordingly.
2755 * The initial type (when pos is zero) may be a pointer type decayed
2756 * from an array type, if this initial type is the type of a function
2757 * argument. This only happens if the original array type has
2758 * a constant size in the outer dimension as otherwise we get
2759 * a VariableArrayType. Try and obtain this original type (if available) and
2760 * take the outer array size into account if it was marked static.
2762 __isl_give pet_expr
*PetScan::set_upper_bounds(__isl_take pet_expr
*expr
,
2763 QualType qt
, int pos
)
2765 const ArrayType
*atype
;
2767 bool decayed
= false;
2773 qt
= undecay(qt
, &decayed
);
2775 if (qt
->isPointerType()) {
2776 qt
= qt
->getPointeeType();
2777 return set_upper_bounds(expr
, qt
, pos
+ 1);
2779 if (!qt
->isArrayType())
2782 qt
= qt
->getCanonicalTypeInternal();
2783 atype
= cast
<ArrayType
>(qt
.getTypePtr());
2785 if (decayed
&& atype
->getSizeModifier() != ArrayType::Static
) {
2786 qt
= atype
->getElementType();
2787 return set_upper_bounds(expr
, qt
, pos
+ 1);
2790 if (qt
->isConstantArrayType()) {
2791 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
2792 size
= extract_expr(ca
->getSize());
2793 expr
= pet_expr_set_arg(expr
, pos
, size
);
2794 } else if (qt
->isVariableArrayType()) {
2795 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
2796 size
= extract_expr(vla
->getSizeExpr());
2797 expr
= pet_expr_set_arg(expr
, pos
, size
);
2800 qt
= atype
->getElementType();
2802 return set_upper_bounds(expr
, qt
, pos
+ 1);
2805 /* Construct a pet_expr that holds the sizes of an array of the given type.
2806 * The returned expression is a call expression with as arguments
2807 * the sizes in each dimension. If we are unable to derive the size
2808 * in a given dimension, then the corresponding argument is set to infinity.
2809 * In fact, we initialize all arguments to infinity and then update
2810 * them if we are able to figure out the size.
2812 * The result is stored in the type_size cache so that we can reuse
2813 * it if this method gets called on the same type again later on.
2815 __isl_give pet_expr
*PetScan::get_array_size(QualType qt
)
2818 pet_expr
*expr
, *inf
;
2819 const Type
*type
= qt
.getTypePtr();
2821 if (type_size
.find(type
) != type_size
.end())
2822 return pet_expr_copy(type_size
[type
]);
2824 depth
= array_depth(qt
);
2825 inf
= pet_expr_new_int(isl_val_infty(ctx
));
2826 expr
= pet_expr_new_call(ctx
, "bounds", depth
);
2827 for (int i
= 0; i
< depth
; ++i
)
2828 expr
= pet_expr_set_arg(expr
, i
, pet_expr_copy(inf
));
2831 expr
= set_upper_bounds(expr
, qt
, 0);
2832 type_size
[type
] = pet_expr_copy(expr
);
2837 /* Does "expr" represent the "integer" infinity?
2839 static int is_infty(__isl_keep pet_expr
*expr
)
2844 if (pet_expr_get_type(expr
) != pet_expr_int
)
2846 v
= pet_expr_int_get_val(expr
);
2847 res
= isl_val_is_infty(v
);
2853 /* Figure out the dimensions of an array "array" based on its type
2854 * "qt" and update "array" accordingly.
2856 * We first construct a pet_expr that holds the sizes of the array
2857 * in each dimension. The resulting expression may containing
2858 * infinity values for dimension where we are unable to derive
2859 * a size expression.
2861 * The arguments of the size expression that have a value different from
2862 * infinity are then converted to an affine expression
2863 * within the context "pc" and incorporated into the size of "array".
2864 * If we are unable to convert a size expression to an affine expression or
2865 * if the size is not a (symbolic) constant,
2866 * then we leave the corresponding size of "array" untouched.
2868 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
2869 QualType qt
, __isl_keep pet_context
*pc
)
2877 expr
= get_array_size(qt
);
2879 n
= pet_expr_get_n_arg(expr
);
2880 for (int i
= 0; i
< n
; ++i
) {
2884 arg
= pet_expr_get_arg(expr
, i
);
2885 if (!is_infty(arg
)) {
2888 size
= pet_expr_extract_affine(arg
, pc
);
2889 dim
= isl_pw_aff_dim(size
, isl_dim_in
);
2891 array
= pet_array_free(array
);
2892 else if (isl_pw_aff_involves_nan(size
) ||
2893 isl_pw_aff_involves_dims(size
, isl_dim_in
, 0, dim
))
2894 isl_pw_aff_free(size
);
2896 size
= isl_pw_aff_drop_dims(size
,
2897 isl_dim_in
, 0, dim
);
2898 array
= update_size(array
, i
, size
);
2903 pet_expr_free(expr
);
2908 /* Does "decl" have a definition that we can keep track of in a pet_type?
2910 static bool has_printable_definition(RecordDecl
*decl
)
2912 if (!decl
->getDeclName())
2914 return decl
->getLexicalDeclContext() == decl
->getDeclContext();
2917 /* Construct and return a pet_array corresponding to the variable
2918 * represented by "id".
2919 * In particular, initialize array->extent to
2921 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2923 * and then call set_upper_bounds to set the upper bounds on the indices
2924 * based on the type of the variable. The upper bounds are converted
2925 * to affine expressions within the context "pc".
2927 * If the base type is that of a record with a top-level definition or
2928 * of a typedef and if "types" is not null, then the RecordDecl or
2929 * TypedefType corresponding to the type
2930 * is added to "types".
2932 * If the base type is that of a record with no top-level definition,
2933 * then we replace it by "<subfield>".
2935 struct pet_array
*PetScan::extract_array(__isl_keep isl_id
*id
,
2936 PetTypes
*types
, __isl_keep pet_context
*pc
)
2938 struct pet_array
*array
;
2939 QualType qt
= pet_id_get_array_type(id
);
2940 int depth
= array_depth(qt
);
2941 QualType base
= pet_clang_base_type(qt
);
2945 array
= isl_calloc_type(ctx
, struct pet_array
);
2949 space
= isl_space_set_alloc(ctx
, 0, depth
);
2950 space
= isl_space_set_tuple_id(space
, isl_dim_set
, isl_id_copy(id
));
2952 array
->extent
= isl_set_nat_universe(space
);
2954 space
= isl_space_params_alloc(ctx
, 0);
2955 array
->context
= isl_set_universe(space
);
2957 array
= set_upper_bounds(array
, qt
, pc
);
2961 name
= base
.getAsString();
2964 if (isa
<TypedefType
>(base
)) {
2965 types
->insert(cast
<TypedefType
>(base
)->getDecl());
2966 } else if (base
->isRecordType()) {
2967 RecordDecl
*decl
= pet_clang_record_decl(base
);
2968 TypedefNameDecl
*typedecl
;
2969 typedecl
= decl
->getTypedefNameForAnonDecl();
2971 types
->insert(typedecl
);
2972 else if (has_printable_definition(decl
))
2973 types
->insert(decl
);
2975 name
= "<subfield>";
2979 array
->element_type
= strdup(name
.c_str());
2980 array
->element_is_record
= base
->isRecordType();
2981 array
->element_size
= size_in_bytes(ast_context
, base
);
2986 /* Construct and return a pet_array corresponding to the variable "decl".
2988 struct pet_array
*PetScan::extract_array(ValueDecl
*decl
,
2989 PetTypes
*types
, __isl_keep pet_context
*pc
)
2994 id
= pet_id_from_decl(ctx
, decl
);
2995 array
= extract_array(id
, types
, pc
);
3001 /* Construct and return a pet_array corresponding to the sequence
3002 * of declarations represented by "decls".
3003 * The upper bounds of the array are converted to affine expressions
3004 * within the context "pc".
3005 * If the sequence contains a single declaration, then it corresponds
3006 * to a simple array access. Otherwise, it corresponds to a member access,
3007 * with the declaration for the substructure following that of the containing
3008 * structure in the sequence of declarations.
3009 * We start with the outermost substructure and then combine it with
3010 * information from the inner structures.
3012 * Additionally, keep track of all required types in "types".
3014 struct pet_array
*PetScan::extract_array(__isl_keep isl_id_list
*decls
,
3015 PetTypes
*types
, __isl_keep pet_context
*pc
)
3019 struct pet_array
*array
;
3021 id
= isl_id_list_get_id(decls
, 0);
3022 array
= extract_array(id
, types
, pc
);
3025 n
= isl_id_list_n_id(decls
);
3026 for (i
= 1; i
< n
; ++i
) {
3027 struct pet_array
*parent
;
3028 const char *base_name
, *field_name
;
3032 id
= isl_id_list_get_id(decls
, i
);
3033 array
= extract_array(id
, types
, pc
);
3036 return pet_array_free(parent
);
3038 base_name
= isl_set_get_tuple_name(parent
->extent
);
3039 field_name
= isl_set_get_tuple_name(array
->extent
);
3040 product_name
= pet_array_member_access_name(ctx
,
3041 base_name
, field_name
);
3043 array
->extent
= isl_set_product(isl_set_copy(parent
->extent
),
3046 array
->extent
= isl_set_set_tuple_name(array
->extent
,
3048 array
->context
= isl_set_intersect(array
->context
,
3049 isl_set_copy(parent
->context
));
3051 pet_array_free(parent
);
3054 if (!array
->extent
|| !array
->context
|| !product_name
)
3055 return pet_array_free(array
);
3061 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3062 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3063 std::set
<TypeDecl
*> &types_done
);
3064 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3065 TypedefNameDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3066 std::set
<TypeDecl
*> &types_done
);
3068 /* For each of the fields of "decl" that is itself a record type
3069 * or a typedef, add a corresponding pet_type to "scop".
3071 static struct pet_scop
*add_field_types(isl_ctx
*ctx
, struct pet_scop
*scop
,
3072 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3073 std::set
<TypeDecl
*> &types_done
)
3075 RecordDecl::field_iterator it
;
3077 for (it
= decl
->field_begin(); it
!= decl
->field_end(); ++it
) {
3078 QualType type
= it
->getType();
3080 if (isa
<TypedefType
>(type
)) {
3081 TypedefNameDecl
*typedefdecl
;
3083 typedefdecl
= cast
<TypedefType
>(type
)->getDecl();
3084 scop
= add_type(ctx
, scop
, typedefdecl
,
3085 PP
, types
, types_done
);
3086 } else if (type
->isRecordType()) {
3089 record
= pet_clang_record_decl(type
);
3090 scop
= add_type(ctx
, scop
, record
,
3091 PP
, types
, types_done
);
3098 /* Add a pet_type corresponding to "decl" to "scop", provided
3099 * it is a member of types.records and it has not been added before
3100 * (i.e., it is not a member of "types_done").
3102 * Since we want the user to be able to print the types
3103 * in the order in which they appear in the scop, we need to
3104 * make sure that types of fields in a structure appear before
3105 * that structure. We therefore call ourselves recursively
3106 * through add_field_types on the types of all record subfields.
3108 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3109 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3110 std::set
<TypeDecl
*> &types_done
)
3113 llvm::raw_string_ostream
S(s
);
3115 if (types
.records
.find(decl
) == types
.records
.end())
3117 if (types_done
.find(decl
) != types_done
.end())
3120 add_field_types(ctx
, scop
, decl
, PP
, types
, types_done
);
3122 if (strlen(decl
->getName().str().c_str()) == 0)
3125 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3128 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
3129 decl
->getName().str().c_str(), s
.c_str());
3130 if (!scop
->types
[scop
->n_type
])
3131 return pet_scop_free(scop
);
3133 types_done
.insert(decl
);
3140 /* Add a pet_type corresponding to "decl" to "scop", provided
3141 * it is a member of types.typedefs and it has not been added before
3142 * (i.e., it is not a member of "types_done").
3144 * If the underlying type is a structure, then we print the typedef
3145 * ourselves since clang does not print the definition of the structure
3146 * in the typedef. We also make sure in this case that the types of
3147 * the fields in the structure are added first.
3149 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3150 TypedefNameDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3151 std::set
<TypeDecl
*> &types_done
)
3154 llvm::raw_string_ostream
S(s
);
3155 QualType qt
= decl
->getUnderlyingType();
3157 if (types
.typedefs
.find(decl
) == types
.typedefs
.end())
3159 if (types_done
.find(decl
) != types_done
.end())
3162 if (qt
->isRecordType()) {
3163 RecordDecl
*rec
= pet_clang_record_decl(qt
);
3165 add_field_types(ctx
, scop
, rec
, PP
, types
, types_done
);
3167 rec
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3169 S
<< decl
->getName();
3171 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3175 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
3176 decl
->getName().str().c_str(), s
.c_str());
3177 if (!scop
->types
[scop
->n_type
])
3178 return pet_scop_free(scop
);
3180 types_done
.insert(decl
);
3187 /* Construct a list of pet_arrays, one for each array (or scalar)
3188 * accessed inside "scop", add this list to "scop" and return the result.
3189 * The upper bounds of the arrays are converted to affine expressions
3190 * within the context "pc".
3192 * The context of "scop" is updated with the intersection of
3193 * the contexts of all arrays, i.e., constraints on the parameters
3194 * that ensure that the arrays have a valid (non-negative) size.
3196 * If any of the extracted arrays refers to a member access or
3197 * has a typedef'd type as base type,
3198 * then also add the required types to "scop".
3200 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
,
3201 __isl_keep pet_context
*pc
)
3204 array_desc_set arrays
;
3205 array_desc_set::iterator it
;
3207 std::set
<TypeDecl
*> types_done
;
3208 std::set
<clang::RecordDecl
*, less_name
>::iterator records_it
;
3209 std::set
<clang::TypedefNameDecl
*, less_name
>::iterator typedefs_it
;
3211 struct pet_array
**scop_arrays
;
3216 pet_scop_collect_arrays(scop
, arrays
);
3217 if (arrays
.size() == 0)
3220 n_array
= scop
->n_array
;
3222 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3223 n_array
+ arrays
.size());
3226 scop
->arrays
= scop_arrays
;
3228 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
3229 struct pet_array
*array
;
3230 array
= extract_array(*it
, &types
, pc
);
3231 scop
->arrays
[n_array
+ i
] = array
;
3232 if (!scop
->arrays
[n_array
+ i
])
3235 scop
->context
= isl_set_intersect(scop
->context
,
3236 isl_set_copy(array
->context
));
3241 n
= types
.records
.size() + types
.typedefs
.size();
3245 scop
->types
= isl_alloc_array(ctx
, struct pet_type
*, n
);
3249 for (records_it
= types
.records
.begin();
3250 records_it
!= types
.records
.end(); ++records_it
)
3251 scop
= add_type(ctx
, scop
, *records_it
, PP
, types
, types_done
);
3253 for (typedefs_it
= types
.typedefs
.begin();
3254 typedefs_it
!= types
.typedefs
.end(); ++typedefs_it
)
3255 scop
= add_type(ctx
, scop
, *typedefs_it
, PP
, types
, types_done
);
3259 pet_scop_free(scop
);
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
)
3273 n
= isl_set_dim(scop
->context
, isl_dim_param
);
3274 for (int i
= 0; i
< n
; ++i
) {
3278 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
3279 if (pet_nested_in_id(id
)) {
3281 isl_die(isl_set_get_ctx(scop
->context
),
3283 "unresolved nested parameter", goto error
);
3285 decl
= pet_id_get_decl(id
);
3288 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
3296 pet_scop_free(scop
);
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
)
3310 stmt
= fd
->getBody();
3312 if (options
->autodetect
) {
3313 set_current_stmt(stmt
);
3314 scop
= extract_scop(extract(stmt
, true));
3316 current_line
= loc
.start_line
;
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
);
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
)