26using ast_matchers::internal::Matcher;
33 enum Kind {
Normal, Unrolled } K;
37 LoopState(Kind InK,
const Stmt *S,
const StackFrame *SF,
unsigned N)
38 : K(InK), LoopStmt(S), SF(SF), maxStep(N) {}
41 static LoopState getNormal(
const Stmt *S,
const StackFrame *SF,
unsigned N) {
42 return LoopState(
Normal, S, SF, N);
46 return LoopState(Unrolled, S, SF, N);
48 bool isUnrolled()
const {
return K == Unrolled; }
49 unsigned getMaxStep()
const {
return maxStep; }
50 const Stmt *getLoopStmt()
const {
return LoopStmt; }
51 const StackFrame *getStackFrame()
const {
return SF; }
53 return K ==
X.K && LoopStmt ==
X.LoopStmt;
55 void Profile(llvm::FoldingSetNodeID &ID)
const {
57 ID.AddPointer(LoopStmt);
59 ID.AddInteger(maxStep);
75 return Node->isIntegralOrEnumerationType();
81 return isa_and_nonnull<ForStmt, WhileStmt, DoStmt>(S);
85 auto LS = State->get<LoopStack>();
86 if (!LS.isEmpty() && LS.getHead().getLoopStmt() == LoopStmt)
87 State = State->set<LoopStack>(LS.getTail());
92 auto LoopVariable = ignoringParenImpCasts(
95 auto UpperBound = ignoringParenImpCasts(
96 expr(hasType(isIntegralOrEnumerationType())).bind(
"boundNum"));
99 anyOf(hasOperatorName(
"<"), hasOperatorName(
">"),
100 hasOperatorName(
"<="), hasOperatorName(
">="),
101 hasOperatorName(
"!=")),
104 .bind(
"conditionOperator");
110 hasUnaryOperand(ignoringParenImpCasts(
113 hasLHS(ignoringParenImpCasts(
117static Matcher<Stmt>
callByRef(Matcher<Decl> VarNodeMatcher) {
118 return callExpr(forEachArgumentWithParam(
126 hasInitializer(
anyOf(
131static Matcher<Stmt>
getAddrTo(Matcher<Decl> VarNodeMatcher) {
133 hasOperatorName(
"&"),
144 callByRef(equalsBoundNode(std::string(NodeName))),
145 getAddrTo(equalsBoundNode(std::string(NodeName))),
157 equalsBoundNode(
"initVarName"))))),
159 equalsBoundNode(
"initVarName"))))),
160 hasRHS(ignoringParenImpCasts(
165 anyOf(hasOperatorName(
"++"), hasOperatorName(
"--")),
168 hasType(isInteger())))))))),
179 assert(MD && MD->getParent()->isLambda() &&
180 "Captured variable should only be seen while evaluating a lambda");
184 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
186 LambdaCXXRec->
getCaptureFields(LambdaCaptureFields, LambdaThisCaptureField);
191 const FieldDecl *FD = LambdaCaptureFields[VD];
192 assert(FD &&
"Captured variable without a corresponding field");
197 if (
const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
198 auto IsVD = [&](
const Decl *D) {
return D->getCanonicalDecl() == VD; };
200 return llvm::any_of(DS->decls(), IsVD);
217 const bool IsRefParamOrCapture =
226 const auto EscapeMatcher =
243 if (
const auto *SS = dyn_cast<SwitchStmt>(S)) {
244 if (
const auto *CST = dyn_cast<CompoundStmt>(SS->getBody())) {
246 if (llvm::any_of(CST->body(), DeclaresVD))
256 auto Match =
match(EscapeMatcher, *S, ASTCtx);
264 if (IsRefParamOrCapture)
267 llvm_unreachable(
"Reached root without finding the declaration of VD");
282 const auto *CounterVarRef = Matches[0].getNodeAs<
DeclRefExpr>(
"initVarRef");
283 const Expr *BoundNumExpr = Matches[0].getNodeAs<
Expr>(
"boundNum");
286 if (!BoundNumExpr || !BoundNumExpr->EvaluateAsInt(BoundNumResult, ASTCtx,
290 llvm::APInt InitNum =
292 auto CondOp = Matches[0].getNodeAs<
BinaryOperator>(
"conditionOperator");
293 unsigned MaxWidth = std::max(InitNum.getBitWidth(),
294 BoundNumResult.Val.getInt().getBitWidth());
296 InitNum = InitNum.zext(MaxWidth);
297 llvm::APInt BoundNum = BoundNumResult.Val.getInt().zext(MaxWidth);
299 if (CondOp->getOpcode() == BO_GE || CondOp->getOpcode() == BO_LE)
300 MaxStep = (BoundNum - InitNum + 1).
abs().getZExtValue();
302 MaxStep = (BoundNum - InitNum).
abs().getZExtValue();
303 return {CounterVarRef, MaxStep};
312 auto [It, Inserted] =
Cache.Entries.try_emplace(LoopStmt);
315 const DeclRefExpr *CounterVarRef = It->second.CounterVarRef;
318 maxStep = It->second.MaxStep;
325 const Stmt *S =
nullptr;
332 S = BE->getBlock()->getTerminatorStmt();
340 llvm_unreachable(
"Reached root without encountering the previous step");
353 auto LS = State->get<LoopStack>();
354 if (!LS.isEmpty() && LoopStmt == LS.getHead().getLoopStmt() &&
355 SF == LS.getHead().getStackFrame()) {
356 if (LS.getHead().isUnrolled() &&
madeNewBranch(Pred, LoopStmt)) {
357 State = State->set<LoopStack>(LS.getTail());
358 State = State->add<LoopStack>(
359 LoopState::getNormal(LoopStmt, SF, maxVisitOnPath));
365 State = State->add<LoopStack>(
366 LoopState::getNormal(LoopStmt, SF, maxVisitOnPath));
370 unsigned outerStep = (LS.isEmpty() ? 1 : LS.getHead().getMaxStep());
372 unsigned innerMaxStep = maxStep * outerStep;
374 State = State->add<LoopStack>(
375 LoopState::getNormal(LoopStmt, SF, maxVisitOnPath));
377 State = State->add<LoopStack>(
378 LoopState::getUnrolled(LoopStmt, SF, innerMaxStep));
383 auto LS = State->get<LoopStack>();
384 if (LS.isEmpty() || !LS.getHead().isUnrolled())
#define AST_MATCHER(Type, DefineMatcher)
AST_MATCHER(Type, DefineMatcher) { ... } defines a zero parameter function named DefineMatcher() that...
static const int MAXIMUM_STEP_UNROLLED
This header contains the declarations of functions which are used to decide which loops should be com...
#define REGISTER_LIST_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable list type NameTy, suitable for placement into the ProgramState.
__DEVICE__ long long abs(long long __n)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
ASTContext & getASTContext() const
A builtin binary operation expression such as "x + y" or "x <= y".
Represents a C++ struct/union/class.
void getCaptureFields(llvm::DenseMap< const ValueDecl *, FieldDecl * > &Captures, FieldDecl *&ThisCapture) const
For a closure type, retrieve the mapping from captured variables and this to the non-static data memb...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
A reference to a declared variable, function, enum, etc.
bool refersToEnclosingVariableOrCapture() const
Does this DeclRefExpr refer to an enclosing local or a captured variable?
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Decl - This represents one declaration (or definition), e.g.
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
This represents one expression.
@ SE_NoSideEffects
Strictly evaluate the expression.
Represents a member of a struct/union/class.
std::optional< T > getAs() const
Convert to the specified ProgramPoint type, returning std::nullopt if this ProgramPoint is not of the...
A (possibly-)qualified type.
It represents a stack frame of the call stack.
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
const Decl * getDecl() const
Stmt - This represents one statement.
bool isReferenceType() const
Represents a variable declaration or definition.
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
const ProgramStateRef & getState() const
const Stmt * getStmtForDiagnostics() const
If the node's program point corresponds to a statement, retrieve that statement.
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
ExplodedNode * getFirstPred()
unsigned succ_size() const
const StackFrame * getStackFrame() const
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
const internal::VariadicOperatorMatcherFunc< 1, 1 > unless
Matches if the provided matcher does not match.
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
const internal::VariadicDynCastAllOfMatcher< Decl, ParmVarDecl > parmVarDecl
Matches parameter variable declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, ReturnStmt > returnStmt
Matches return statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr
Matches call expressions.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const internal::VariadicDynCastAllOfMatcher< Stmt, UnaryOperator > unaryOperator
Matches unary operator expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, InitListExpr > initListExpr
Matches init list expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, ForStmt > forStmt
Matches for statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, GotoStmt > gotoStmt
Matches goto statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, BinaryOperator > binaryOperator
Matches binary operator expressions.
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> allOf
Matches if all given matchers match.
const internal::VariadicDynCastAllOfMatcher< Stmt, SwitchStmt > switchStmt
Matches switch statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, IntegerLiteral > integerLiteral
Matches integer literals of all sizes / encodings, e.g.
internal::PolymorphicMatcher< internal::HasDeclarationMatcher, void(internal::HasDeclarationSupportedTypes), internal::Matcher< Decl > > hasDeclaration(const internal::Matcher< Decl > &InnerMatcher)
Matches a node if the declaration associated with that node matches the given matcher.
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclStmt > declStmt
Matches declaration statements.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> anyOf
Matches if any of the given matchers matches.
const internal::VariadicAllOfMatcher< QualType > qualType
Matches QualTypes in the clang AST.
const AstTypeMatcher< ReferenceType > referenceType
static bool madeNewBranch(ExplodedNode *N, const Stmt *LoopStmt)
static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR)
static LoopUnrollingCache::Entry analyzeLoopShape(const Stmt *LoopStmt, ASTContext &ASTCtx)
Returns the loop counter reference of LoopStmt and the number of iterations if the loop has the shape...
static Matcher< Stmt > changeIntBoundNode(Matcher< Decl > VarNodeMatcher)
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
static bool isLoopStmt(const Stmt *S)
static Matcher< Stmt > forLoopMatcher()
ProgramStateRef processLoopEnd(const Stmt *LoopStmt, ProgramStateRef State)
Updates the given ProgramState.
static Matcher< Stmt > hasSuspiciousStmt(StringRef NodeName)
ProgramStateRef updateLoopStack(const Stmt *LoopStmt, ASTContext &ASTCtx, ExplodedNode *Pred, unsigned maxVisitOnPath, LoopUnrollingCache &Cache)
Updates the stack of loops contained by the ProgramState.
static Matcher< Stmt > getAddrTo(Matcher< Decl > VarNodeMatcher)
static bool isFoundInStmt(const Stmt *S, const VarDecl *VD)
static Matcher< Stmt > simpleCondition(StringRef BindName, StringRef RefName)
bool isUnrolledState(ProgramStateRef State)
Returns if the given State indicates that is inside a completely unrolled loop.
static bool isCapturedByReference(ExplodedNode *N, const DeclRefExpr *DR)
static bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx, ExplodedNode *Pred, unsigned &maxStep, LoopUnrollingCache &Cache)
static Matcher< Stmt > assignedToRef(Matcher< Decl > VarNodeMatcher)
static Matcher< Stmt > callByRef(Matcher< Decl > VarNodeMatcher)
Top level wrappers for InstallAPI frontend operations.
@ Match
This is not an overload because the signature exactly matches an existing declaration.
bool isa(CodeGen::Address addr)
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
U cast(CodeGen::Address addr)
EvalResult is a struct with detailed info about an evaluated expression.
Memoizes the part of the loop unrolling decision that depends only on the AST of a loop (as opposed t...
static const Entry NotUnrollable
The entry of a loop that is not a candidate for complete unrolling.