clang 23.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 for (const Decl *D : DS->decls()) {
199 // Once we reach the declaration of the VD we can return.
200 if (D->getCanonicalDecl() == VD)
201 return true;
202 }
203 }
204 return false;
205}
206
207// A loop counter is considered escaped if:
208// case 1: It is a global variable.
209// case 2: It is a reference parameter or a reference capture.
210// case 3: It is assigned to a non-const reference variable or parameter.
211// case 4: Has its address taken.
212static bool isPossiblyEscaped(ExplodedNode *N, const DeclRefExpr *DR) {
213 const VarDecl *VD = cast<VarDecl>(DR->getDecl()->getCanonicalDecl());
214 assert(VD);
215 // Case 1:
216 if (VD->hasGlobalStorage())
217 return true;
218
219 const bool IsRefParamOrCapture =
221 // Case 2:
223 isCapturedByReference(N, DR)) ||
224 (IsRefParamOrCapture && VD->getType()->isReferenceType()))
225 return true;
226
227 while (!N->pred_empty()) {
228 // FIXME: getStmtForDiagnostics() does nasty things in order to provide
229 // a valid statement for body farms, do we need this behavior here?
230 const Stmt *S = N->getStmtForDiagnostics();
231 if (!S) {
232 N = N->getFirstPred();
233 continue;
234 }
235
236 if (isFoundInStmt(S, VD)) {
237 return false;
238 }
239
240 if (const auto *SS = dyn_cast<SwitchStmt>(S)) {
241 if (const auto *CST = dyn_cast<CompoundStmt>(SS->getBody())) {
242 for (const Stmt *CB : CST->body()) {
243 if (isFoundInStmt(CB, VD))
244 return false;
245 }
246 }
247 }
248
249 // Check the usage of the pass-by-ref function calls and adress-of operator
250 // on VD and reference initialized by VD.
251 ASTContext &ASTCtx =
253 // Case 3 and 4:
254 auto Match =
255 match(stmt(anyOf(callByRef(equalsNode(VD)), getAddrTo(equalsNode(VD)),
256 assignedToRef(equalsNode(VD)))),
257 *S, ASTCtx);
258 if (!Match.empty())
259 return true;
260
261 N = N->getFirstPred();
262 }
263
264 // Reference parameter and reference capture will not be found.
265 if (IsRefParamOrCapture)
266 return false;
267
268 llvm_unreachable("Reached root without finding the declaration of VD");
269}
270
271static bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx,
272 ExplodedNode *Pred, unsigned &maxStep) {
273
274 if (!isLoopStmt(LoopStmt))
275 return false;
276
277 auto Matches = match(forLoopMatcher(), *LoopStmt, ASTCtx);
278 if (Matches.empty())
279 return false;
280
281 const auto *CounterVarRef = Matches[0].getNodeAs<DeclRefExpr>("initVarRef");
282 const Expr *BoundNumExpr = Matches[0].getNodeAs<Expr>("boundNum");
283
284 Expr::EvalResult BoundNumResult;
285 if (!BoundNumExpr || !BoundNumExpr->EvaluateAsInt(BoundNumResult, ASTCtx,
287 return false;
288 }
289 llvm::APInt InitNum =
290 Matches[0].getNodeAs<IntegerLiteral>("initNum")->getValue();
291 auto CondOp = Matches[0].getNodeAs<BinaryOperator>("conditionOperator");
292 unsigned MaxWidth = std::max(InitNum.getBitWidth(),
293 BoundNumResult.Val.getInt().getBitWidth());
294
295 InitNum = InitNum.zext(MaxWidth);
296 llvm::APInt BoundNum = BoundNumResult.Val.getInt().zext(MaxWidth);
297 if (CondOp->getOpcode() == BO_GE || CondOp->getOpcode() == BO_LE)
298 maxStep = (BoundNum - InitNum + 1).abs().getZExtValue();
299 else
300 maxStep = (BoundNum - InitNum).abs().getZExtValue();
301
302 // Check if the counter of the loop is not escaped before.
303 return !isPossiblyEscaped(Pred, CounterVarRef);
304}
305
306static bool madeNewBranch(ExplodedNode *N, const Stmt *LoopStmt) {
307 const Stmt *S = nullptr;
308 while (!N->pred_empty()) {
309 if (N->succ_size() > 1)
310 return true;
311
312 ProgramPoint P = N->getLocation();
313 if (std::optional<BlockEntrance> BE = P.getAs<BlockEntrance>())
314 S = BE->getBlock()->getTerminatorStmt();
315
316 if (S == LoopStmt)
317 return false;
318
319 N = N->getFirstPred();
320 }
321
322 llvm_unreachable("Reached root without encountering the previous step");
323}
324
325// updateLoopStack is called on every basic block, therefore it needs to be fast
327 ExplodedNode *Pred, unsigned maxVisitOnPath) {
328 auto State = Pred->getState();
329 auto SF = Pred->getStackFrame();
330
331 if (!isLoopStmt(LoopStmt))
332 return State;
333
334 auto LS = State->get<LoopStack>();
335 if (!LS.isEmpty() && LoopStmt == LS.getHead().getLoopStmt() &&
336 SF == LS.getHead().getStackFrame()) {
337 if (LS.getHead().isUnrolled() && madeNewBranch(Pred, LoopStmt)) {
338 State = State->set<LoopStack>(LS.getTail());
339 State = State->add<LoopStack>(
340 LoopState::getNormal(LoopStmt, SF, maxVisitOnPath));
341 }
342 return State;
343 }
344 unsigned maxStep;
345 if (!shouldCompletelyUnroll(LoopStmt, ASTCtx, Pred, maxStep)) {
346 State = State->add<LoopStack>(
347 LoopState::getNormal(LoopStmt, SF, maxVisitOnPath));
348 return State;
349 }
350
351 unsigned outerStep = (LS.isEmpty() ? 1 : LS.getHead().getMaxStep());
352
353 unsigned innerMaxStep = maxStep * outerStep;
354 if (innerMaxStep > MAXIMUM_STEP_UNROLLED)
355 State = State->add<LoopStack>(
356 LoopState::getNormal(LoopStmt, SF, maxVisitOnPath));
357 else
358 State = State->add<LoopStack>(
359 LoopState::getUnrolled(LoopStmt, SF, innerMaxStep));
360 return State;
361}
362
364 auto LS = State->get<LoopStack>();
365 if (LS.isEmpty() || !LS.getHead().isUnrolled())
366 return false;
367 return true;
368}
369}
370}
#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:226
ASTContext & getASTContext() const
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4041
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:1789
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2122
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1273
bool refersToEnclosingVariableOrCapture() const
Does this DeclRefExpr refer to an enclosing local or a captured variable?
Definition Expr.h:1477
ValueDecl * getDecl()
Definition Expr.h:1341
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1641
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:991
This represents one expression.
Definition Expr.h:112
@ SE_NoSideEffects
Strictly evaluate the expression.
Definition Expr.h:674
Represents a member of a struct/union/class.
Definition Decl.h:3182
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:937
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:86
bool isReferenceType() const
Definition TypeBase.h:8706
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:924
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1239
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 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)
static Matcher< Stmt > getAddrTo(Matcher< Decl > VarNodeMatcher)
static bool isFoundInStmt(const Stmt *S, const VarDecl *VD)
static bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx, ExplodedNode *Pred, unsigned &maxStep)
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 Matcher< Stmt > assignedToRef(Matcher< Decl > VarNodeMatcher)
static Matcher< Stmt > callByRef(Matcher< Decl > VarNodeMatcher)
ProgramStateRef updateLoopStack(const Stmt *LoopStmt, ASTContext &ASTCtx, ExplodedNode *Pred, unsigned maxVisitOnPath)
Updates the stack of loops contained by the ProgramState.
The JSON file list parser is used to communicate input to InstallAPI.
@ Match
This is not an overload because the signature exactly matches an existing declaration.
Definition Sema.h:830
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:206
U cast(CodeGen::Address addr)
Definition Address.h:327
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:648