2017-07-26 03:23:23 +08:00
|
|
|
//===--- LoopUnrolling.cpp - Unroll loops -----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// This file contains functions which are used to decide if a loop worth to be
|
2017-08-22 00:32:57 +08:00
|
|
|
/// unrolled. Moreover, these functions manages the stack of loop which is
|
|
|
|
/// tracked by the ProgramState.
|
2017-07-26 03:23:23 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/LoopUnrolling.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
2017-08-28 18:50:28 +08:00
|
|
|
static const int MAXIMUM_STEP_UNROLLED = 128;
|
|
|
|
|
2017-08-22 00:32:57 +08:00
|
|
|
struct LoopState {
|
|
|
|
private:
|
|
|
|
enum Kind { Normal, Unrolled } K;
|
|
|
|
const Stmt *LoopStmt;
|
|
|
|
const LocationContext *LCtx;
|
2017-08-28 18:50:28 +08:00
|
|
|
unsigned maxStep;
|
|
|
|
LoopState(Kind InK, const Stmt *S, const LocationContext *L, unsigned N)
|
|
|
|
: K(InK), LoopStmt(S), LCtx(L), maxStep(N) {}
|
2017-07-26 03:23:23 +08:00
|
|
|
|
2017-08-22 00:32:57 +08:00
|
|
|
public:
|
2017-08-28 18:50:28 +08:00
|
|
|
static LoopState getNormal(const Stmt *S, const LocationContext *L,
|
|
|
|
unsigned N) {
|
|
|
|
return LoopState(Normal, S, L, N);
|
2017-08-22 00:32:57 +08:00
|
|
|
}
|
2017-08-28 18:50:28 +08:00
|
|
|
static LoopState getUnrolled(const Stmt *S, const LocationContext *L,
|
|
|
|
unsigned N) {
|
|
|
|
return LoopState(Unrolled, S, L, N);
|
2017-08-22 00:32:57 +08:00
|
|
|
}
|
|
|
|
bool isUnrolled() const { return K == Unrolled; }
|
2017-08-28 18:50:28 +08:00
|
|
|
unsigned getMaxStep() const { return maxStep; }
|
2017-08-22 00:32:57 +08:00
|
|
|
const Stmt *getLoopStmt() const { return LoopStmt; }
|
|
|
|
const LocationContext *getLocationContext() const { return LCtx; }
|
|
|
|
bool operator==(const LoopState &X) const {
|
|
|
|
return K == X.K && LoopStmt == X.LoopStmt;
|
|
|
|
}
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger(K);
|
|
|
|
ID.AddPointer(LoopStmt);
|
|
|
|
ID.AddPointer(LCtx);
|
2017-08-28 18:50:28 +08:00
|
|
|
ID.AddInteger(maxStep);
|
2017-08-22 00:32:57 +08:00
|
|
|
}
|
|
|
|
};
|
2017-07-26 03:23:23 +08:00
|
|
|
|
2017-08-22 00:32:57 +08:00
|
|
|
// The tracked stack of loops. The stack indicates that which loops the
|
|
|
|
// simulated element contained by. The loops are marked depending if we decided
|
|
|
|
// to unroll them.
|
|
|
|
// TODO: The loop stack should not need to be in the program state since it is
|
|
|
|
// lexical in nature. Instead, the stack of loops should be tracked in the
|
|
|
|
// LocationContext.
|
|
|
|
REGISTER_LIST_WITH_PROGRAMSTATE(LoopStack, LoopState)
|
2017-07-26 03:23:23 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace ento {
|
|
|
|
|
|
|
|
static bool isLoopStmt(const Stmt *S) {
|
|
|
|
return S && (isa<ForStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S));
|
|
|
|
}
|
|
|
|
|
2017-08-22 00:32:57 +08:00
|
|
|
ProgramStateRef processLoopEnd(const Stmt *LoopStmt, ProgramStateRef State) {
|
|
|
|
auto LS = State->get<LoopStack>();
|
2017-08-28 18:21:24 +08:00
|
|
|
if (!LS.isEmpty() && LS.getHead().getLoopStmt() == LoopStmt)
|
|
|
|
State = State->set<LoopStack>(LS.getTail());
|
2017-08-22 00:32:57 +08:00
|
|
|
return State;
|
|
|
|
}
|
|
|
|
|
2017-07-26 03:23:23 +08:00
|
|
|
static internal::Matcher<Stmt> simpleCondition(StringRef BindName) {
|
2017-08-28 18:50:28 +08:00
|
|
|
return binaryOperator(anyOf(hasOperatorName("<"), hasOperatorName(">"),
|
|
|
|
hasOperatorName("<="), hasOperatorName(">="),
|
|
|
|
hasOperatorName("!=")),
|
|
|
|
hasEitherOperand(ignoringParenImpCasts(declRefExpr(
|
|
|
|
to(varDecl(hasType(isInteger())).bind(BindName))))),
|
|
|
|
hasEitherOperand(ignoringParenImpCasts(
|
|
|
|
integerLiteral().bind("boundNum"))))
|
|
|
|
.bind("conditionOperator");
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
2017-08-19 18:24:52 +08:00
|
|
|
static internal::Matcher<Stmt>
|
|
|
|
changeIntBoundNode(internal::Matcher<Decl> VarNodeMatcher) {
|
|
|
|
return anyOf(
|
|
|
|
unaryOperator(anyOf(hasOperatorName("--"), hasOperatorName("++")),
|
|
|
|
hasUnaryOperand(ignoringParenImpCasts(
|
|
|
|
declRefExpr(to(varDecl(VarNodeMatcher)))))),
|
2018-03-27 20:16:56 +08:00
|
|
|
binaryOperator(isAssignmentOperator(),
|
2017-08-19 18:24:52 +08:00
|
|
|
hasLHS(ignoringParenImpCasts(
|
|
|
|
declRefExpr(to(varDecl(VarNodeMatcher)))))));
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
2017-08-19 18:24:52 +08:00
|
|
|
static internal::Matcher<Stmt>
|
|
|
|
callByRef(internal::Matcher<Decl> VarNodeMatcher) {
|
|
|
|
return callExpr(forEachArgumentWithParam(
|
|
|
|
declRefExpr(to(varDecl(VarNodeMatcher))),
|
|
|
|
parmVarDecl(hasType(references(qualType(unless(isConstQualified())))))));
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
2017-08-19 18:24:52 +08:00
|
|
|
static internal::Matcher<Stmt>
|
|
|
|
assignedToRef(internal::Matcher<Decl> VarNodeMatcher) {
|
|
|
|
return declStmt(hasDescendant(varDecl(
|
2017-07-26 03:23:23 +08:00
|
|
|
allOf(hasType(referenceType()),
|
2017-08-19 18:24:52 +08:00
|
|
|
hasInitializer(anyOf(
|
|
|
|
initListExpr(has(declRefExpr(to(varDecl(VarNodeMatcher))))),
|
|
|
|
declRefExpr(to(varDecl(VarNodeMatcher)))))))));
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
2017-08-19 18:24:52 +08:00
|
|
|
static internal::Matcher<Stmt>
|
|
|
|
getAddrTo(internal::Matcher<Decl> VarNodeMatcher) {
|
|
|
|
return unaryOperator(
|
2017-07-26 03:23:23 +08:00
|
|
|
hasOperatorName("&"),
|
2017-08-19 18:24:52 +08:00
|
|
|
hasUnaryOperand(declRefExpr(hasDeclaration(VarNodeMatcher))));
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static internal::Matcher<Stmt> hasSuspiciousStmt(StringRef NodeName) {
|
2017-08-19 18:24:52 +08:00
|
|
|
return hasDescendant(stmt(
|
2017-08-22 00:32:57 +08:00
|
|
|
anyOf(gotoStmt(), switchStmt(), returnStmt(),
|
2017-08-19 18:24:52 +08:00
|
|
|
// Escaping and not known mutation of the loop counter is handled
|
|
|
|
// by exclusion of assigning and address-of operators and
|
|
|
|
// pass-by-ref function calls on the loop counter from the body.
|
|
|
|
changeIntBoundNode(equalsBoundNode(NodeName)),
|
|
|
|
callByRef(equalsBoundNode(NodeName)),
|
|
|
|
getAddrTo(equalsBoundNode(NodeName)),
|
|
|
|
assignedToRef(equalsBoundNode(NodeName)))));
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static internal::Matcher<Stmt> forLoopMatcher() {
|
|
|
|
return forStmt(
|
|
|
|
hasCondition(simpleCondition("initVarName")),
|
|
|
|
// Initialization should match the form: 'int i = 6' or 'i = 42'.
|
[analyzer] Unroll the loop when it has a unsigned counter.
Summary:
The original implementation in the `LoopUnrolling.cpp` didn't consider the case where the counter is unsigned. This case is only handled in `simpleCondition()`, but this is not enough, we also need to deal with the unsinged counter with the counter initialization.
Since `IntegerLiteral` is `signed`, there is a `ImplicitCastExpr<IntegralCast>` in `unsigned counter = IntergerLiteral`. This patch add the `ignoringParenImpCasts()` in the `IntegerLiteral` matcher.
Reviewers: szepet, a.sidorin, NoQ, george.karpenkov
Reviewed By: szepet, george.karpenkov
Subscribers: xazax.hun, rnkovacs, cfe-commits, MTC
Differential Revision: https://reviews.llvm.org/D45086
llvm-svn: 328919
2018-03-31 20:46:46 +08:00
|
|
|
hasLoopInit(
|
|
|
|
anyOf(declStmt(hasSingleDecl(
|
|
|
|
varDecl(allOf(hasInitializer(ignoringParenImpCasts(
|
|
|
|
integerLiteral().bind("initNum"))),
|
|
|
|
equalsBoundNode("initVarName"))))),
|
|
|
|
binaryOperator(hasLHS(declRefExpr(to(varDecl(
|
|
|
|
equalsBoundNode("initVarName"))))),
|
|
|
|
hasRHS(ignoringParenImpCasts(
|
|
|
|
integerLiteral().bind("initNum")))))),
|
2017-07-26 03:23:23 +08:00
|
|
|
// Incrementation should be a simple increment or decrement
|
|
|
|
// operator call.
|
|
|
|
hasIncrement(unaryOperator(
|
|
|
|
anyOf(hasOperatorName("++"), hasOperatorName("--")),
|
|
|
|
hasUnaryOperand(declRefExpr(
|
|
|
|
to(varDecl(allOf(equalsBoundNode("initVarName"),
|
|
|
|
hasType(isInteger())))))))),
|
|
|
|
unless(hasBody(hasSuspiciousStmt("initVarName")))).bind("forLoop");
|
|
|
|
}
|
|
|
|
|
2017-08-19 18:24:52 +08:00
|
|
|
static bool isPossiblyEscaped(const VarDecl *VD, ExplodedNode *N) {
|
|
|
|
// Global variables assumed as escaped variables.
|
|
|
|
if (VD->hasGlobalStorage())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
while (!N->pred_empty()) {
|
|
|
|
const Stmt *S = PathDiagnosticLocation::getStmt(N);
|
|
|
|
if (!S) {
|
|
|
|
N = N->getFirstPred();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
|
|
|
|
for (const Decl *D : DS->decls()) {
|
|
|
|
// Once we reach the declaration of the VD we can return.
|
|
|
|
if (D->getCanonicalDecl() == VD)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check the usage of the pass-by-ref function calls and adress-of operator
|
|
|
|
// on VD and reference initialized by VD.
|
|
|
|
ASTContext &ASTCtx =
|
|
|
|
N->getLocationContext()->getAnalysisDeclContext()->getASTContext();
|
|
|
|
auto Match =
|
|
|
|
match(stmt(anyOf(callByRef(equalsNode(VD)), getAddrTo(equalsNode(VD)),
|
|
|
|
assignedToRef(equalsNode(VD)))),
|
|
|
|
*S, ASTCtx);
|
|
|
|
if (!Match.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
N = N->getFirstPred();
|
|
|
|
}
|
|
|
|
llvm_unreachable("Reached root without finding the declaration of VD");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx,
|
2017-08-28 18:50:28 +08:00
|
|
|
ExplodedNode *Pred, unsigned &maxStep) {
|
2017-07-26 03:23:23 +08:00
|
|
|
|
|
|
|
if (!isLoopStmt(LoopStmt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// TODO: Match the cases where the bound is not a concrete literal but an
|
|
|
|
// integer with known value
|
|
|
|
auto Matches = match(forLoopMatcher(), *LoopStmt, ASTCtx);
|
2017-08-19 18:24:52 +08:00
|
|
|
if (Matches.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto CounterVar = Matches[0].getNodeAs<VarDecl>("initVarName");
|
2017-10-28 20:19:08 +08:00
|
|
|
llvm::APInt BoundNum =
|
|
|
|
Matches[0].getNodeAs<IntegerLiteral>("boundNum")->getValue();
|
|
|
|
llvm::APInt InitNum =
|
|
|
|
Matches[0].getNodeAs<IntegerLiteral>("initNum")->getValue();
|
2017-08-28 18:50:28 +08:00
|
|
|
auto CondOp = Matches[0].getNodeAs<BinaryOperator>("conditionOperator");
|
2017-10-28 20:19:08 +08:00
|
|
|
if (InitNum.getBitWidth() != BoundNum.getBitWidth()) {
|
|
|
|
InitNum = InitNum.zextOrSelf(BoundNum.getBitWidth());
|
|
|
|
BoundNum = BoundNum.zextOrSelf(InitNum.getBitWidth());
|
|
|
|
}
|
|
|
|
|
2017-08-28 18:50:28 +08:00
|
|
|
if (CondOp->getOpcode() == BO_GE || CondOp->getOpcode() == BO_LE)
|
|
|
|
maxStep = (BoundNum - InitNum + 1).abs().getZExtValue();
|
|
|
|
else
|
|
|
|
maxStep = (BoundNum - InitNum).abs().getZExtValue();
|
2017-08-19 18:24:52 +08:00
|
|
|
|
|
|
|
// Check if the counter of the loop is not escaped before.
|
|
|
|
return !isPossiblyEscaped(CounterVar->getCanonicalDecl(), Pred);
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
2017-08-28 18:50:28 +08:00
|
|
|
bool madeNewBranch(ExplodedNode *N, const Stmt *LoopStmt) {
|
|
|
|
const Stmt *S = nullptr;
|
|
|
|
while (!N->pred_empty()) {
|
2017-08-28 18:34:50 +08:00
|
|
|
if (N->succ_size() > 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ProgramPoint P = N->getLocation();
|
|
|
|
if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>())
|
|
|
|
S = BE->getBlock()->getTerminator();
|
|
|
|
|
|
|
|
if (S == LoopStmt)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
N = N->getFirstPred();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Reached root without encountering the previous step");
|
|
|
|
}
|
|
|
|
|
2017-08-22 00:32:57 +08:00
|
|
|
// updateLoopStack is called on every basic block, therefore it needs to be fast
|
|
|
|
ProgramStateRef updateLoopStack(const Stmt *LoopStmt, ASTContext &ASTCtx,
|
2017-08-28 18:50:28 +08:00
|
|
|
ExplodedNode *Pred, unsigned maxVisitOnPath) {
|
2017-08-22 00:32:57 +08:00
|
|
|
auto State = Pred->getState();
|
|
|
|
auto LCtx = Pred->getLocationContext();
|
2017-07-26 03:23:23 +08:00
|
|
|
|
2017-08-22 00:32:57 +08:00
|
|
|
if (!isLoopStmt(LoopStmt))
|
|
|
|
return State;
|
2017-07-26 03:23:23 +08:00
|
|
|
|
2017-08-22 00:32:57 +08:00
|
|
|
auto LS = State->get<LoopStack>();
|
|
|
|
if (!LS.isEmpty() && LoopStmt == LS.getHead().getLoopStmt() &&
|
2017-08-28 18:34:50 +08:00
|
|
|
LCtx == LS.getHead().getLocationContext()) {
|
|
|
|
if (LS.getHead().isUnrolled() && madeNewBranch(Pred, LoopStmt)) {
|
|
|
|
State = State->set<LoopStack>(LS.getTail());
|
2017-08-28 18:50:28 +08:00
|
|
|
State = State->add<LoopStack>(
|
|
|
|
LoopState::getNormal(LoopStmt, LCtx, maxVisitOnPath));
|
2017-08-28 18:34:50 +08:00
|
|
|
}
|
2017-08-22 00:32:57 +08:00
|
|
|
return State;
|
2017-08-28 18:34:50 +08:00
|
|
|
}
|
2017-08-28 18:50:28 +08:00
|
|
|
unsigned maxStep;
|
|
|
|
if (!shouldCompletelyUnroll(LoopStmt, ASTCtx, Pred, maxStep)) {
|
|
|
|
State = State->add<LoopStack>(
|
|
|
|
LoopState::getNormal(LoopStmt, LCtx, maxVisitOnPath));
|
2017-08-22 00:32:57 +08:00
|
|
|
return State;
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
2017-08-28 18:50:28 +08:00
|
|
|
unsigned outerStep = (LS.isEmpty() ? 1 : LS.getHead().getMaxStep());
|
|
|
|
|
|
|
|
unsigned innerMaxStep = maxStep * outerStep;
|
|
|
|
if (innerMaxStep > MAXIMUM_STEP_UNROLLED)
|
|
|
|
State = State->add<LoopStack>(
|
|
|
|
LoopState::getNormal(LoopStmt, LCtx, maxVisitOnPath));
|
|
|
|
else
|
|
|
|
State = State->add<LoopStack>(
|
|
|
|
LoopState::getUnrolled(LoopStmt, LCtx, innerMaxStep));
|
2017-08-22 00:32:57 +08:00
|
|
|
return State;
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
|
2017-08-22 00:32:57 +08:00
|
|
|
bool isUnrolledState(ProgramStateRef State) {
|
|
|
|
auto LS = State->get<LoopStack>();
|
|
|
|
if (LS.isEmpty() || !LS.getHead().isUnrolled())
|
|
|
|
return false;
|
|
|
|
return true;
|
2017-07-26 03:23:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|