clang 24.0.0git
LoopUnrolling.cpp
Go to the documentation of this file.
1//===--- LoopUnrolling.cpp - Unroll loops -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// This file contains functions which are used to decide if a loop worth to be
10/// unrolled. Moreover, these functions manages the stack of loop which is
11/// tracked by the ProgramState.
12///
13//===----------------------------------------------------------------------===//
14
20#include <optional>
21
22using namespace clang;
23using namespace ento;
24using namespace clang::ast_matchers;
25
26using ast_matchers::internal::Matcher;
27
28static const int MAXIMUM_STEP_UNROLLED = 128;
29
30namespace {
31struct LoopState {
32private:
33 enum Kind { Normal, Unrolled } K;
34 const Stmt *LoopStmt;
35 const StackFrame *SF;
36 unsigned maxStep;
37 LoopState(Kind InK, const Stmt *S, const StackFrame *SF, unsigned N)
38 : K(InK), LoopStmt(S), SF(SF), maxStep(N) {}
39
40public:
41 static LoopState getNormal(const Stmt *S, const StackFrame *SF, unsigned N) {
42 return LoopState(Normal, S, SF, N);
43 }
44 static LoopState getUnrolled(const Stmt *S, const StackFrame *SF,
45 unsigned N) {
46 return LoopState(Unrolled, S, SF, N);
47 }
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; }
52 bool operator==(const LoopState &X) const {
53 return K == X.K && LoopStmt == X.LoopStmt;
54 }
55 void Profile(llvm::FoldingSetNodeID &ID) const {
56 ID.AddInteger(K);
57 ID.AddPointer(LoopStmt);
58 ID.AddPointer(SF);
59 ID.AddInteger(maxStep);
60 }
61};
62} // namespace
63
64// The tracked stack of loops. The stack indicates that which loops the
65// simulated element contained by. The loops are marked depending if we decided
66// to unroll them.
67// TODO: The loop stack should not need to be in the program state since it is
68// lexical in nature. Instead, the stack of loops should be tracked in the
69// StackFrame.
70REGISTER_LIST_WITH_PROGRAMSTATE(LoopStack, LoopState)
71
72namespace clang {
73namespace {
74AST_MATCHER(QualType, isIntegralOrEnumerationType) {
75 return Node->isIntegralOrEnumerationType();
76}
77} // namespace
78namespace ento {
79
80static bool isLoopStmt(const Stmt *S) {
81 return isa_and_nonnull<ForStmt, WhileStmt, DoStmt>(S);
82}
83
85 auto LS = State->get<LoopStack>();
86 if (!LS.isEmpty() && LS.getHead().getLoopStmt() == LoopStmt)
87 State = State->set<LoopStack>(LS.getTail());
88 return State;
89}
90
91static Matcher<Stmt> simpleCondition(StringRef BindName, StringRef RefName) {
92 auto LoopVariable = ignoringParenImpCasts(
93 declRefExpr(to(varDecl(hasType(isInteger())).bind(BindName)))
94 .bind(RefName));
95 auto UpperBound = ignoringParenImpCasts(
96 expr(hasType(isIntegralOrEnumerationType())).bind("boundNum"));
97
98 return binaryOperator(
99 anyOf(hasOperatorName("<"), hasOperatorName(">"),
100 hasOperatorName("<="), hasOperatorName(">="),
101 hasOperatorName("!=")),
102 anyOf(binaryOperator(hasLHS(LoopVariable), hasRHS(UpperBound)),
103 binaryOperator(hasRHS(LoopVariable), hasLHS(UpperBound))))
104 .bind("conditionOperator");
105}
106
107static Matcher<Stmt> changeIntBoundNode(Matcher<Decl> VarNodeMatcher) {
108 return anyOf(
109 unaryOperator(anyOf(hasOperatorName("--"), hasOperatorName("++")),
110 hasUnaryOperand(ignoringParenImpCasts(
111 declRefExpr(to(varDecl(VarNodeMatcher)))))),
112 binaryOperator(isAssignmentOperator(),
113 hasLHS(ignoringParenImpCasts(
114 declRefExpr(to(varDecl(VarNodeMatcher)))))));
115}
116
117static Matcher<Stmt> callByRef(Matcher<Decl> VarNodeMatcher) {
118 return callExpr(forEachArgumentWithParam(
119 declRefExpr(to(varDecl(VarNodeMatcher))),
120 parmVarDecl(hasType(references(qualType(unless(isConstQualified())))))));
121}
122
123static Matcher<Stmt> assignedToRef(Matcher<Decl> VarNodeMatcher) {
125 allOf(hasType(referenceType()),
126 hasInitializer(anyOf(
127 initListExpr(has(declRefExpr(to(varDecl(VarNodeMatcher))))),
128 declRefExpr(to(varDecl(VarNodeMatcher)))))))));
129}
130
131static Matcher<Stmt> getAddrTo(Matcher<Decl> VarNodeMatcher) {
132 return unaryOperator(
133 hasOperatorName("&"),
134 hasUnaryOperand(declRefExpr(hasDeclaration(VarNodeMatcher))));
135}
136
137static Matcher<Stmt> hasSuspiciousStmt(StringRef NodeName) {
138 return hasDescendant(stmt(
140 // Escaping and not known mutation of the loop counter is handled
141 // by exclusion of assigning and address-of operators and
142 // pass-by-ref function calls on the loop counter from the body.
143 changeIntBoundNode(equalsBoundNode(std::string(NodeName))),
144 callByRef(equalsBoundNode(std::string(NodeName))),
145 getAddrTo(equalsBoundNode(std::string(NodeName))),
146 assignedToRef(equalsBoundNode(std::string(NodeName))))));
147}
148
149static Matcher<Stmt> forLoopMatcher() {
150 return forStmt(
151 hasCondition(simpleCondition("initVarName", "initVarRef")),
152 // Initialization should match the form: 'int i = 6' or 'i = 42'.
153 hasLoopInit(
154 anyOf(declStmt(hasSingleDecl(
155 varDecl(allOf(hasInitializer(ignoringParenImpCasts(
156 integerLiteral().bind("initNum"))),
157 equalsBoundNode("initVarName"))))),
159 equalsBoundNode("initVarName"))))),
160 hasRHS(ignoringParenImpCasts(
161 integerLiteral().bind("initNum")))))),
162 // Incrementation should be a simple increment or decrement
163 // operator call.
164 hasIncrement(unaryOperator(
165 anyOf(hasOperatorName("++"), hasOperatorName("--")),
166 hasUnaryOperand(declRefExpr(
167 to(varDecl(allOf(equalsBoundNode("initVarName"),
168 hasType(isInteger())))))))),
169 unless(hasBody(hasSuspiciousStmt("initVarName"))))
170 .bind("forLoop");
171}
172
174
175 // Get the lambda CXXRecordDecl
177 const Decl *D = N->getStackFrame()->getDecl();
178 const auto *MD = cast<CXXMethodDecl>(D);
179 assert(MD && MD->getParent()->isLambda() &&
180 "Captured variable should only be seen while evaluating a lambda");
181 const CXXRecordDecl *LambdaCXXRec = MD->getParent();
182
183 // Lookup the fields of the lambda
184 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
185 FieldDecl *LambdaThisCaptureField;
186 LambdaCXXRec->getCaptureFields(LambdaCaptureFields, LambdaThisCaptureField);
187
188 // Check if the counter is captured by reference
189 const VarDecl *VD = cast<VarDecl>(DR->getDecl()->getCanonicalDecl());
190 assert(VD);
191 const FieldDecl *FD = LambdaCaptureFields[VD];
192 assert(FD && "Captured variable without a corresponding field");
193 return FD->getType()->isReferenceType();
194}
195
196static bool isFoundInStmt(const Stmt *S, const VarDecl *VD) {
197 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
198 auto IsVD = [&](const Decl *D) { return D->getCanonicalDecl() == VD; };
199 // Once we reach the declaration of the VD we can return.
200 return llvm::any_of(DS->decls(), IsVD);
201 }
202 return false;
203}
204
205// A loop counter is considered escaped if:
206// case 1: It is a global variable.
207// case 2: It is a reference parameter or a reference capture.
208// case 3: It is assigned to a non-const reference variable or parameter.
209// case 4: Has its address taken.
210static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR) {
211 const VarDecl *VD = cast<VarDecl>(DR->getDecl()->getCanonicalDecl());
212 assert(VD);
213 // Case 1:
214 if (VD->hasGlobalStorage())
215 return true;
216
217 const bool IsRefParamOrCapture =
219 // Case 2:
221 isCapturedByReference(N, DR)) ||
222 (IsRefParamOrCapture && VD->getType()->isReferenceType()))
223 return true;
224
225 // Building the matcher is expensive, so do it once for the whole walk.
226 const auto EscapeMatcher =
227 stmt(anyOf(callByRef(equalsNode(VD)), getAddrTo(equalsNode(VD)),
228 assignedToRef(equalsNode(VD))));
229
230 while (!N->pred_empty()) {
231 // FIXME: getStmtForDiagnostics() does nasty things in order to provide
232 // a valid statement for body farms, do we need this behavior here?
233 const Stmt *S = N->getStmtForDiagnostics();
234 if (!S) {
235 N = N->getFirstPred();
236 continue;
237 }
238
239 if (isFoundInStmt(S, VD)) {
240 return false;
241 }
242
243 if (const auto *SS = dyn_cast<SwitchStmt>(S)) {
244 if (const auto *CST = dyn_cast<CompoundStmt>(SS->getBody())) {
245 auto DeclaresVD = [&](const Stmt *CB) { return isFoundInStmt(CB, VD); };
246 if (llvm::any_of(CST->body(), DeclaresVD))
247 return false;
248 }
249 }
250
251 // Check the usage of the pass-by-ref function calls and adress-of operator
252 // on VD and reference initialized by VD.
253 ASTContext &ASTCtx =
255 // Case 3 and 4:
256 auto Match = match(EscapeMatcher, *S, ASTCtx);
257 if (!Match.empty())
258 return true;
259
260 N = N->getFirstPred();
261 }
262
263 // Reference parameter and reference capture will not be found.
264 if (IsRefParamOrCapture)
265 return false;
266
267 llvm_unreachable("Reached root without finding the declaration of VD");
268}
269
271 0};
272
273/// Returns the loop counter reference of \p LoopStmt and the number of
274/// iterations if the loop has the shape that can be completely unrolled.
275/// This depends only on the AST.
277 ASTContext &ASTCtx) {
278 auto Matches = match(forLoopMatcher(), *LoopStmt, ASTCtx);
279 if (Matches.empty())
281
282 const auto *CounterVarRef = Matches[0].getNodeAs<DeclRefExpr>("initVarRef");
283 const Expr *BoundNumExpr = Matches[0].getNodeAs<Expr>("boundNum");
284
285 Expr::EvalResult BoundNumResult;
286 if (!BoundNumExpr || !BoundNumExpr->EvaluateAsInt(BoundNumResult, ASTCtx,
289 }
290 llvm::APInt InitNum =
291 Matches[0].getNodeAs<IntegerLiteral>("initNum")->getValue();
292 auto CondOp = Matches[0].getNodeAs<BinaryOperator>("conditionOperator");
293 unsigned MaxWidth = std::max(InitNum.getBitWidth(),
294 BoundNumResult.Val.getInt().getBitWidth());
295
296 InitNum = InitNum.zext(MaxWidth);
297 llvm::APInt BoundNum = BoundNumResult.Val.getInt().zext(MaxWidth);
298 unsigned MaxStep;
299 if (CondOp->getOpcode() == BO_GE || CondOp->getOpcode() == BO_LE)
300 MaxStep = (BoundNum - InitNum + 1).abs().getZExtValue();
301 else
302 MaxStep = (BoundNum - InitNum).abs().getZExtValue();
303 return {CounterVarRef, MaxStep};
304}
305
306static bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx,
307 ExplodedNode *Pred, unsigned &maxStep,
309 if (!isLoopStmt(LoopStmt))
310 return false;
311
312 auto [It, Inserted] = Cache.Entries.try_emplace(LoopStmt);
313 if (Inserted)
314 It->second = analyzeLoopShape(LoopStmt, ASTCtx);
315 const DeclRefExpr *CounterVarRef = It->second.CounterVarRef;
316 if (!CounterVarRef)
317 return false;
318 maxStep = It->second.MaxStep;
319
320 // Check if the counter of the loop is not escaped before.
321 return !isPossiblyEscaped(Pred, CounterVarRef);
322}
323
324static bool madeNewBranch(ExplodedNode *N, const Stmt *LoopStmt) {
325 const Stmt *S = nullptr;
326 while (!N->pred_empty()) {
327 if (N->succ_size() > 1)
328 return true;
329
330 ProgramPoint P = N->getLocation();
331 if (std::optional<BlockEntrance> BE = P.getAs<BlockEntrance>())
332 S = BE->getBlock()->getTerminatorStmt();
333
334 if (S == LoopStmt)
335 return false;
336
337 N = N->getFirstPred();
338 }
339
340 llvm_unreachable("Reached root without encountering the previous step");
341}
342
343// updateLoopStack is called on every basic block, therefore it needs to be fast
345 ExplodedNode *Pred, unsigned maxVisitOnPath,
347 auto State = Pred->getState();
348 auto SF = Pred->getStackFrame();
349
350 if (!isLoopStmt(LoopStmt))
351 return State;
352
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));
360 }
361 return State;
362 }
363 unsigned maxStep;
364 if (!shouldCompletelyUnroll(LoopStmt, ASTCtx, Pred, maxStep, Cache)) {
365 State = State->add<LoopStack>(
366 LoopState::getNormal(LoopStmt, SF, maxVisitOnPath));
367 return State;
368 }
369
370 unsigned outerStep = (LS.isEmpty() ? 1 : LS.getHead().getMaxStep());
371
372 unsigned innerMaxStep = maxStep * outerStep;
373 if (innerMaxStep > MAXIMUM_STEP_UNROLLED)
374 State = State->add<LoopStack>(
375 LoopState::getNormal(LoopStmt, SF, maxVisitOnPath));
376 else
377 State = State->add<LoopStack>(
378 LoopState::getUnrolled(LoopStmt, SF, innerMaxStep));
379 return State;
380}
381
383 auto LS = State->get<LoopStack>();
384 if (LS.isEmpty() || !LS.getHead().isUnrolled())
385 return false;
386 return true;
387}
388}
389}
#define AST_MATCHER(Type, DefineMatcher)
AST_MATCHER(Type, DefineMatcher) { ... } defines a zero parameter function named DefineMatcher() that...
#define X(type, name)
Definition Value.h:97
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 ...
Definition ASTContext.h:245
ASTContext & getASTContext() const
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4107
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
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...
Definition DeclCXX.cpp:1792
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2110
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1315
bool refersToEnclosingVariableOrCapture() const
Does this DeclRefExpr refer to an enclosing local or a captured variable?
Definition Expr.h:1519
ValueDecl * getDecl()
Definition Expr.h:1383
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1642
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition DeclBase.h:996
This represents one expression.
Definition Expr.h:114
@ SE_NoSideEffects
Strictly evaluate the expression.
Definition Expr.h:700
Represents a member of a struct/union/class.
Definition Decl.h:3341
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.
Definition TypeBase.h:938
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.
Definition Stmt.h:85
bool isReferenceType() const
Definition TypeBase.h:8941
QualType getType() const
Definition Decl.h:725
Represents a variable declaration or definition.
Definition Decl.h:934
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1249
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.
Definition Sema.h:824
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:218
U cast(CodeGen::Address addr)
Definition Address.h:327
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:667
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.