2014-01-07 06:27:43 +08:00
|
|
|
//===--- CodeGenPGO.cpp - PGO Instrumentation for LLVM CodeGen --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Instrumentation-based profile-guided optimization
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenPGO.h"
|
|
|
|
#include "CodeGenFunction.h"
|
2014-08-05 02:41:51 +08:00
|
|
|
#include "CoverageMappingGen.h"
|
2014-01-07 06:27:43 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2014-12-09 03:04:51 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2014-01-07 06:27:43 +08:00
|
|
|
#include "llvm/IR/MDBuilder.h"
|
2014-04-17 00:03:27 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2014-01-07 06:27:43 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-04-17 00:03:27 +08:00
|
|
|
#include "llvm/Support/MD5.h"
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2017-12-01 08:53:10 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
EnableValueProfiling("enable-value-profiling", llvm::cl::ZeroOrMore,
|
|
|
|
llvm::cl::desc("Enable value profiling"),
|
|
|
|
llvm::cl::Hidden, llvm::cl::init(false));
|
2016-01-24 06:50:44 +08:00
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
void CodeGenPGO::setFuncName(StringRef Name,
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
2015-12-05 13:37:15 +08:00
|
|
|
llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
|
|
|
|
FuncName = llvm::getPGOFuncName(
|
|
|
|
Name, Linkage, CGM.getCodeGenOpts().MainFileName,
|
|
|
|
PGOReader ? PGOReader->getVersion() : llvm::IndexedInstrProf::Version);
|
2014-12-09 03:04:51 +08:00
|
|
|
|
|
|
|
// If we're generating a profile, create a variable for the name.
|
2016-02-05 02:39:09 +08:00
|
|
|
if (CGM.getCodeGenOpts().hasProfileClangInstr())
|
2015-11-09 08:04:16 +08:00
|
|
|
FuncNameVar = llvm::createPGOFuncNameVar(CGM.getModule(), Linkage, FuncName);
|
2014-03-06 12:55:41 +08:00
|
|
|
}
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
void CodeGenPGO::setFuncName(llvm::Function *Fn) {
|
|
|
|
setFuncName(Fn->getName(), Fn->getLinkage());
|
2016-04-23 05:19:05 +08:00
|
|
|
// Create PGOFuncName meta data.
|
|
|
|
llvm::createPGOFuncNameMetadata(*Fn, FuncName);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2017-11-15 07:56:53 +08:00
|
|
|
/// The version of the PGO hash algorithm.
|
|
|
|
enum PGOHashVersion : unsigned {
|
|
|
|
PGO_HASH_V1,
|
|
|
|
PGO_HASH_V2,
|
|
|
|
|
|
|
|
// Keep this set to the latest hash version.
|
|
|
|
PGO_HASH_LATEST = PGO_HASH_V2
|
|
|
|
};
|
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
namespace {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Stable hasher for PGO region counters.
|
2014-04-17 00:03:27 +08:00
|
|
|
///
|
|
|
|
/// PGOHash produces a stable hash of a given function's control flow.
|
|
|
|
///
|
|
|
|
/// Changing the output of this hash will invalidate all previously generated
|
|
|
|
/// profiles -- i.e., don't do it.
|
|
|
|
///
|
|
|
|
/// \note When this hash does eventually change (years?), we still need to
|
|
|
|
/// support old hashes. We'll need to pull in the version number from the
|
|
|
|
/// profile data format and use the matching hash function.
|
|
|
|
class PGOHash {
|
|
|
|
uint64_t Working;
|
|
|
|
unsigned Count;
|
2017-11-15 07:56:53 +08:00
|
|
|
PGOHashVersion HashVersion;
|
2014-04-17 00:03:27 +08:00
|
|
|
llvm::MD5 MD5;
|
|
|
|
|
|
|
|
static const int NumBitsPerType = 6;
|
|
|
|
static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType;
|
|
|
|
static const unsigned TooBig = 1u << NumBitsPerType;
|
|
|
|
|
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Hash values for AST nodes.
|
2014-04-17 00:03:27 +08:00
|
|
|
///
|
|
|
|
/// Distinct values for AST nodes that have region counters attached.
|
|
|
|
///
|
|
|
|
/// These values must be stable. All new members must be added at the end,
|
|
|
|
/// and no members should be removed. Changing the enumeration value for an
|
|
|
|
/// AST node will affect the hash of every function that contains that node.
|
|
|
|
enum HashType : unsigned char {
|
|
|
|
None = 0,
|
|
|
|
LabelStmt = 1,
|
|
|
|
WhileStmt,
|
|
|
|
DoStmt,
|
|
|
|
ForStmt,
|
|
|
|
CXXForRangeStmt,
|
|
|
|
ObjCForCollectionStmt,
|
|
|
|
SwitchStmt,
|
|
|
|
CaseStmt,
|
|
|
|
DefaultStmt,
|
|
|
|
IfStmt,
|
|
|
|
CXXTryStmt,
|
|
|
|
CXXCatchStmt,
|
|
|
|
ConditionalOperator,
|
|
|
|
BinaryOperatorLAnd,
|
|
|
|
BinaryOperatorLOr,
|
|
|
|
BinaryConditionalOperator,
|
2017-11-15 07:56:53 +08:00
|
|
|
// The preceding values are available with PGO_HASH_V1.
|
|
|
|
|
|
|
|
EndOfScope,
|
|
|
|
IfThenBranch,
|
|
|
|
IfElseBranch,
|
|
|
|
GotoStmt,
|
|
|
|
IndirectGotoStmt,
|
|
|
|
BreakStmt,
|
|
|
|
ContinueStmt,
|
|
|
|
ReturnStmt,
|
|
|
|
ThrowExpr,
|
|
|
|
UnaryOperatorLNot,
|
|
|
|
BinaryOperatorLT,
|
|
|
|
BinaryOperatorGT,
|
|
|
|
BinaryOperatorLE,
|
|
|
|
BinaryOperatorGE,
|
|
|
|
BinaryOperatorEQ,
|
|
|
|
BinaryOperatorNE,
|
|
|
|
// The preceding values are available with PGO_HASH_V2.
|
2014-04-17 00:03:27 +08:00
|
|
|
|
|
|
|
// Keep this last. It's for the static assert that follows.
|
|
|
|
LastHashType
|
|
|
|
};
|
|
|
|
static_assert(LastHashType <= TooBig, "Too many types in HashType");
|
|
|
|
|
2017-11-15 07:56:53 +08:00
|
|
|
PGOHash(PGOHashVersion HashVersion)
|
|
|
|
: Working(0), Count(0), HashVersion(HashVersion), MD5() {}
|
2014-04-17 00:03:27 +08:00
|
|
|
void combine(HashType Type);
|
|
|
|
uint64_t finalize();
|
2017-11-15 07:56:53 +08:00
|
|
|
PGOHashVersion getHashVersion() const { return HashVersion; }
|
2014-04-17 00:03:27 +08:00
|
|
|
};
|
|
|
|
const int PGOHash::NumBitsPerType;
|
|
|
|
const unsigned PGOHash::NumTypesPerWord;
|
|
|
|
const unsigned PGOHash::TooBig;
|
|
|
|
|
2017-11-15 07:56:53 +08:00
|
|
|
/// Get the PGO hash version used in the given indexed profile.
|
|
|
|
static PGOHashVersion getPGOHashVersion(llvm::IndexedInstrProfReader *PGOReader,
|
|
|
|
CodeGenModule &CGM) {
|
|
|
|
if (PGOReader->getVersion() <= 4)
|
|
|
|
return PGO_HASH_V1;
|
|
|
|
return PGO_HASH_V2;
|
|
|
|
}
|
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
/// A RecursiveASTVisitor that fills a map of statements to PGO counters.
|
|
|
|
struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
|
2017-11-15 07:56:53 +08:00
|
|
|
using Base = RecursiveASTVisitor<MapRegionCounters>;
|
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
/// The next counter value to assign.
|
|
|
|
unsigned NextCounter;
|
|
|
|
/// The function hash.
|
|
|
|
PGOHash Hash;
|
|
|
|
/// The map of statements to counters.
|
|
|
|
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
|
|
|
|
|
2017-11-15 07:56:53 +08:00
|
|
|
MapRegionCounters(PGOHashVersion HashVersion,
|
|
|
|
llvm::DenseMap<const Stmt *, unsigned> &CounterMap)
|
|
|
|
: NextCounter(0), Hash(HashVersion), CounterMap(CounterMap) {}
|
2015-02-24 03:27:00 +08:00
|
|
|
|
|
|
|
// Blocks and lambdas are handled as separate functions, so we need not
|
|
|
|
// traverse them in the parent context.
|
|
|
|
bool TraverseBlockExpr(BlockExpr *BE) { return true; }
|
2019-01-14 18:31:42 +08:00
|
|
|
bool TraverseLambdaExpr(LambdaExpr *LE) {
|
|
|
|
// Traverse the captures, but not the body.
|
|
|
|
for (const auto &C : zip(LE->captures(), LE->capture_inits()))
|
|
|
|
TraverseLambdaCapture(LE, &std::get<0>(C), std::get<1>(C));
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-24 03:27:00 +08:00
|
|
|
bool TraverseCapturedStmt(CapturedStmt *CS) { return true; }
|
|
|
|
|
|
|
|
bool VisitDecl(const Decl *D) {
|
|
|
|
switch (D->getKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Decl::Function:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion:
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::Block:
|
|
|
|
case Decl::Captured:
|
|
|
|
CounterMap[D->getBody()] = NextCounter++;
|
|
|
|
break;
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
2015-02-24 03:27:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2017-11-15 07:56:53 +08:00
|
|
|
/// If \p S gets a fresh counter, update the counter mappings. Return the
|
|
|
|
/// V1 hash of \p S.
|
|
|
|
PGOHash::HashType updateCounterMappings(Stmt *S) {
|
|
|
|
auto Type = getHashType(PGO_HASH_V1, S);
|
|
|
|
if (Type != PGOHash::None)
|
|
|
|
CounterMap[S] = NextCounter++;
|
|
|
|
return Type;
|
|
|
|
}
|
2015-02-24 03:27:00 +08:00
|
|
|
|
2017-11-15 07:56:53 +08:00
|
|
|
/// Include \p S in the function hash.
|
|
|
|
bool VisitStmt(Stmt *S) {
|
|
|
|
auto Type = updateCounterMappings(S);
|
|
|
|
if (Hash.getHashVersion() != PGO_HASH_V1)
|
|
|
|
Type = getHashType(Hash.getHashVersion(), S);
|
|
|
|
if (Type != PGOHash::None)
|
|
|
|
Hash.combine(Type);
|
2015-02-24 03:27:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 07:56:53 +08:00
|
|
|
|
|
|
|
bool TraverseIfStmt(IfStmt *If) {
|
|
|
|
// If we used the V1 hash, use the default traversal.
|
|
|
|
if (Hash.getHashVersion() == PGO_HASH_V1)
|
|
|
|
return Base::TraverseIfStmt(If);
|
|
|
|
|
|
|
|
// Otherwise, keep track of which branch we're in while traversing.
|
|
|
|
VisitStmt(If);
|
|
|
|
for (Stmt *CS : If->children()) {
|
|
|
|
if (!CS)
|
|
|
|
continue;
|
|
|
|
if (CS == If->getThen())
|
|
|
|
Hash.combine(PGOHash::IfThenBranch);
|
|
|
|
else if (CS == If->getElse())
|
|
|
|
Hash.combine(PGOHash::IfElseBranch);
|
|
|
|
TraverseStmt(CS);
|
|
|
|
}
|
|
|
|
Hash.combine(PGOHash::EndOfScope);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the statement type \p N is nestable, and its nesting impacts profile
|
|
|
|
// stability, define a custom traversal which tracks the end of the statement
|
|
|
|
// in the hash (provided we're not using the V1 hash).
|
|
|
|
#define DEFINE_NESTABLE_TRAVERSAL(N) \
|
|
|
|
bool Traverse##N(N *S) { \
|
|
|
|
Base::Traverse##N(S); \
|
|
|
|
if (Hash.getHashVersion() != PGO_HASH_V1) \
|
|
|
|
Hash.combine(PGOHash::EndOfScope); \
|
|
|
|
return true; \
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_NESTABLE_TRAVERSAL(WhileStmt)
|
|
|
|
DEFINE_NESTABLE_TRAVERSAL(DoStmt)
|
|
|
|
DEFINE_NESTABLE_TRAVERSAL(ForStmt)
|
|
|
|
DEFINE_NESTABLE_TRAVERSAL(CXXForRangeStmt)
|
|
|
|
DEFINE_NESTABLE_TRAVERSAL(ObjCForCollectionStmt)
|
|
|
|
DEFINE_NESTABLE_TRAVERSAL(CXXTryStmt)
|
|
|
|
DEFINE_NESTABLE_TRAVERSAL(CXXCatchStmt)
|
|
|
|
|
|
|
|
/// Get version \p HashVersion of the PGO hash for \p S.
|
|
|
|
PGOHash::HashType getHashType(PGOHashVersion HashVersion, const Stmt *S) {
|
2015-02-24 03:27:00 +08:00
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Stmt::LabelStmtClass:
|
|
|
|
return PGOHash::LabelStmt;
|
|
|
|
case Stmt::WhileStmtClass:
|
|
|
|
return PGOHash::WhileStmt;
|
|
|
|
case Stmt::DoStmtClass:
|
|
|
|
return PGOHash::DoStmt;
|
|
|
|
case Stmt::ForStmtClass:
|
|
|
|
return PGOHash::ForStmt;
|
|
|
|
case Stmt::CXXForRangeStmtClass:
|
|
|
|
return PGOHash::CXXForRangeStmt;
|
|
|
|
case Stmt::ObjCForCollectionStmtClass:
|
|
|
|
return PGOHash::ObjCForCollectionStmt;
|
|
|
|
case Stmt::SwitchStmtClass:
|
|
|
|
return PGOHash::SwitchStmt;
|
|
|
|
case Stmt::CaseStmtClass:
|
|
|
|
return PGOHash::CaseStmt;
|
|
|
|
case Stmt::DefaultStmtClass:
|
|
|
|
return PGOHash::DefaultStmt;
|
|
|
|
case Stmt::IfStmtClass:
|
|
|
|
return PGOHash::IfStmt;
|
|
|
|
case Stmt::CXXTryStmtClass:
|
|
|
|
return PGOHash::CXXTryStmt;
|
|
|
|
case Stmt::CXXCatchStmtClass:
|
|
|
|
return PGOHash::CXXCatchStmt;
|
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
return PGOHash::ConditionalOperator;
|
|
|
|
case Stmt::BinaryConditionalOperatorClass:
|
|
|
|
return PGOHash::BinaryConditionalOperator;
|
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
const BinaryOperator *BO = cast<BinaryOperator>(S);
|
|
|
|
if (BO->getOpcode() == BO_LAnd)
|
|
|
|
return PGOHash::BinaryOperatorLAnd;
|
|
|
|
if (BO->getOpcode() == BO_LOr)
|
|
|
|
return PGOHash::BinaryOperatorLOr;
|
2017-11-15 07:56:53 +08:00
|
|
|
if (HashVersion == PGO_HASH_V2) {
|
|
|
|
switch (BO->getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case BO_LT:
|
|
|
|
return PGOHash::BinaryOperatorLT;
|
|
|
|
case BO_GT:
|
|
|
|
return PGOHash::BinaryOperatorGT;
|
|
|
|
case BO_LE:
|
|
|
|
return PGOHash::BinaryOperatorLE;
|
|
|
|
case BO_GE:
|
|
|
|
return PGOHash::BinaryOperatorGE;
|
|
|
|
case BO_EQ:
|
|
|
|
return PGOHash::BinaryOperatorEQ;
|
|
|
|
case BO_NE:
|
|
|
|
return PGOHash::BinaryOperatorNE;
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 03:27:00 +08:00
|
|
|
break;
|
2014-04-17 00:03:27 +08:00
|
|
|
}
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
2017-11-15 07:56:53 +08:00
|
|
|
|
|
|
|
if (HashVersion == PGO_HASH_V2) {
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Stmt::GotoStmtClass:
|
|
|
|
return PGOHash::GotoStmt;
|
|
|
|
case Stmt::IndirectGotoStmtClass:
|
|
|
|
return PGOHash::IndirectGotoStmt;
|
|
|
|
case Stmt::BreakStmtClass:
|
|
|
|
return PGOHash::BreakStmt;
|
|
|
|
case Stmt::ContinueStmtClass:
|
|
|
|
return PGOHash::ContinueStmt;
|
|
|
|
case Stmt::ReturnStmtClass:
|
|
|
|
return PGOHash::ReturnStmt;
|
|
|
|
case Stmt::CXXThrowExprClass:
|
|
|
|
return PGOHash::ThrowExpr;
|
|
|
|
case Stmt::UnaryOperatorClass: {
|
|
|
|
const UnaryOperator *UO = cast<UnaryOperator>(S);
|
|
|
|
if (UO->getOpcode() == UO_LNot)
|
|
|
|
return PGOHash::UnaryOperatorLNot;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
return PGOHash::None;
|
|
|
|
}
|
|
|
|
};
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
/// A StmtVisitor that propagates the raw counts through the AST and
|
|
|
|
/// records the count at statements where the value may change.
|
|
|
|
struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
|
|
|
|
/// PGO state.
|
|
|
|
CodeGenPGO &PGO;
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
/// A flag that is set when the current count should be recorded on the
|
|
|
|
/// next statement, such as at the exit of a loop.
|
|
|
|
bool RecordNextStmtCount;
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-05-02 08:48:27 +08:00
|
|
|
/// The count at the current location in the traversal.
|
|
|
|
uint64_t CurrentCount;
|
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
/// The map of statements to count values.
|
|
|
|
llvm::DenseMap<const Stmt *, uint64_t> &CountMap;
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
/// BreakContinueStack - Keep counts of breaks and continues inside loops.
|
|
|
|
struct BreakContinue {
|
|
|
|
uint64_t BreakCount;
|
|
|
|
uint64_t ContinueCount;
|
|
|
|
BreakContinue() : BreakCount(0), ContinueCount(0) {}
|
|
|
|
};
|
|
|
|
SmallVector<BreakContinue, 8> BreakContinueStack;
|
2014-04-15 08:50:54 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap,
|
|
|
|
CodeGenPGO &PGO)
|
|
|
|
: PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {}
|
2014-03-06 14:10:02 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void RecordStmtCount(const Stmt *S) {
|
|
|
|
if (RecordNextStmtCount) {
|
2015-05-02 08:48:27 +08:00
|
|
|
CountMap[S] = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = false;
|
2014-03-07 04:24:27 +08:00
|
|
|
}
|
2015-02-24 03:27:00 +08:00
|
|
|
}
|
2014-03-07 04:24:27 +08:00
|
|
|
|
2015-05-02 07:41:09 +08:00
|
|
|
/// Set and return the current count.
|
|
|
|
uint64_t setCount(uint64_t Count) {
|
2015-05-02 08:48:27 +08:00
|
|
|
CurrentCount = Count;
|
2015-05-02 07:41:09 +08:00
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitStmt(const Stmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2015-07-03 05:03:14 +08:00
|
|
|
for (const Stmt *Child : S->children())
|
|
|
|
if (Child)
|
|
|
|
this->Visit(Child);
|
2015-02-24 03:27:00 +08:00
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitFunctionDecl(const FunctionDecl *D) {
|
|
|
|
// Counter tracks entry to the function body.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
|
|
|
|
CountMap[D->getBody()] = BodyCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(D->getBody());
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
// Skip lambda expressions. We visit these as FunctionDecls when we're
|
|
|
|
// generating them and aren't interested in the body when generating a
|
|
|
|
// parent context.
|
|
|
|
void VisitLambdaExpr(const LambdaExpr *LE) {}
|
|
|
|
|
|
|
|
void VisitCapturedDecl(const CapturedDecl *D) {
|
|
|
|
// Counter tracks entry to the capture body.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
|
|
|
|
CountMap[D->getBody()] = BodyCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(D->getBody());
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
|
|
|
|
// Counter tracks entry to the method body.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
|
|
|
|
CountMap[D->getBody()] = BodyCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(D->getBody());
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitBlockDecl(const BlockDecl *D) {
|
|
|
|
// Counter tracks entry to the block body.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
|
|
|
|
CountMap[D->getBody()] = BodyCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(D->getBody());
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitReturnStmt(const ReturnStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
if (S->getRetValue())
|
|
|
|
Visit(S->getRetValue());
|
2015-05-02 08:48:27 +08:00
|
|
|
CurrentCount = 0;
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-04-28 14:31:55 +08:00
|
|
|
void VisitCXXThrowExpr(const CXXThrowExpr *E) {
|
|
|
|
RecordStmtCount(E);
|
|
|
|
if (E->getSubExpr())
|
|
|
|
Visit(E->getSubExpr());
|
2015-05-02 08:48:27 +08:00
|
|
|
CurrentCount = 0;
|
2015-04-28 14:31:55 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitGotoStmt(const GotoStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2015-05-02 08:48:27 +08:00
|
|
|
CurrentCount = 0;
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitLabelStmt(const LabelStmt *S) {
|
|
|
|
RecordNextStmtCount = false;
|
|
|
|
// Counter tracks the block following the label.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t BlockCount = setCount(PGO.getRegionCount(S));
|
|
|
|
CountMap[S] = BlockCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getSubStmt());
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitBreakStmt(const BreakStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
|
2015-05-02 08:48:27 +08:00
|
|
|
BreakContinueStack.back().BreakCount += CurrentCount;
|
|
|
|
CurrentCount = 0;
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitContinueStmt(const ContinueStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
|
2015-05-02 08:48:27 +08:00
|
|
|
BreakContinueStack.back().ContinueCount += CurrentCount;
|
|
|
|
CurrentCount = 0;
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitWhileStmt(const WhileStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t ParentCount = CurrentCount;
|
2015-05-02 07:41:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
// Visit the body region first so the break/continue adjustments can be
|
|
|
|
// included when visiting the condition.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t BodyCount = setCount(PGO.getRegionCount(S));
|
2015-05-02 08:48:27 +08:00
|
|
|
CountMap[S->getBody()] = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getBody());
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t BackedgeCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
|
|
|
|
// ...then go back and propagate counts through the condition. The count
|
|
|
|
// at the start of the condition is the sum of the incoming edges,
|
|
|
|
// the backedge from the end of the loop body, and the edges from
|
|
|
|
// continue statements.
|
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t CondCount =
|
|
|
|
setCount(ParentCount + BackedgeCount + BC.ContinueCount);
|
|
|
|
CountMap[S->getCond()] = CondCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getCond());
|
2015-05-02 07:41:09 +08:00
|
|
|
setCount(BC.BreakCount + CondCount - BodyCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitDoStmt(const DoStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t LoopCount = PGO.getRegionCount(S);
|
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
2015-05-02 07:41:09 +08:00
|
|
|
// The count doesn't include the fallthrough from the parent scope. Add it.
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t BodyCount = setCount(LoopCount + CurrentCount);
|
2015-05-02 07:41:09 +08:00
|
|
|
CountMap[S->getBody()] = BodyCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getBody());
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t BackedgeCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
|
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
|
|
|
// The count at the start of the condition is equal to the count at the
|
2015-05-02 07:41:09 +08:00
|
|
|
// end of the body, plus any continues.
|
|
|
|
uint64_t CondCount = setCount(BackedgeCount + BC.ContinueCount);
|
|
|
|
CountMap[S->getCond()] = CondCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getCond());
|
2015-05-02 07:41:09 +08:00
|
|
|
setCount(BC.BreakCount + CondCount - LoopCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitForStmt(const ForStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
2015-05-02 07:41:09 +08:00
|
|
|
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t ParentCount = CurrentCount;
|
2015-05-02 07:41:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
// Visit the body region first. (This is basically the same as a while
|
|
|
|
// loop; see further comments in VisitWhileStmt.)
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t BodyCount = setCount(PGO.getRegionCount(S));
|
|
|
|
CountMap[S->getBody()] = BodyCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getBody());
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t BackedgeCount = CurrentCount;
|
2015-05-02 07:41:09 +08:00
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-02-24 03:27:00 +08:00
|
|
|
|
|
|
|
// The increment is essentially part of the body but it needs to include
|
|
|
|
// the count for all the continue statements.
|
|
|
|
if (S->getInc()) {
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
|
|
|
|
CountMap[S->getInc()] = IncCount;
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
Visit(S->getInc());
|
2015-02-24 03:27:00 +08:00
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
// ...then go back and propagate counts through the condition.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t CondCount =
|
|
|
|
setCount(ParentCount + BackedgeCount + BC.ContinueCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
if (S->getCond()) {
|
2015-05-02 07:41:09 +08:00
|
|
|
CountMap[S->getCond()] = CondCount;
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
Visit(S->getCond());
|
|
|
|
}
|
2015-05-02 07:41:09 +08:00
|
|
|
setCount(BC.BreakCount + CondCount - BodyCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2018-09-29 02:44:09 +08:00
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
2015-05-01 06:58:28 +08:00
|
|
|
Visit(S->getLoopVarStmt());
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getRangeStmt());
|
2016-03-20 18:33:40 +08:00
|
|
|
Visit(S->getBeginStmt());
|
|
|
|
Visit(S->getEndStmt());
|
2015-05-02 07:41:09 +08:00
|
|
|
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t ParentCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
// Visit the body region first. (This is basically the same as a while
|
|
|
|
// loop; see further comments in VisitWhileStmt.)
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t BodyCount = setCount(PGO.getRegionCount(S));
|
|
|
|
CountMap[S->getBody()] = BodyCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getBody());
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t BackedgeCount = CurrentCount;
|
2015-05-02 07:41:09 +08:00
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-02-24 03:27:00 +08:00
|
|
|
|
|
|
|
// The increment is essentially part of the body but it needs to include
|
|
|
|
// the count for all the continue statements.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
|
|
|
|
CountMap[S->getInc()] = IncCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getInc());
|
|
|
|
|
|
|
|
// ...then go back and propagate counts through the condition.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t CondCount =
|
|
|
|
setCount(ParentCount + BackedgeCount + BC.ContinueCount);
|
|
|
|
CountMap[S->getCond()] = CondCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getCond());
|
2015-05-02 07:41:09 +08:00
|
|
|
setCount(BC.BreakCount + CondCount - BodyCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
Visit(S->getElement());
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t ParentCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
2015-05-02 07:41:09 +08:00
|
|
|
// Counter tracks the body of the loop.
|
|
|
|
uint64_t BodyCount = setCount(PGO.getRegionCount(S));
|
|
|
|
CountMap[S->getBody()] = BodyCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getBody());
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t BackedgeCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-05-02 07:41:09 +08:00
|
|
|
|
|
|
|
setCount(BC.BreakCount + ParentCount + BackedgeCount + BC.ContinueCount -
|
|
|
|
BodyCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitSwitchStmt(const SwitchStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2016-10-15 07:38:13 +08:00
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getCond());
|
2015-05-02 08:48:27 +08:00
|
|
|
CurrentCount = 0;
|
2015-02-24 03:27:00 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
Visit(S->getBody());
|
|
|
|
// If the switch is inside a loop, add the continue counts.
|
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
|
|
|
if (!BreakContinueStack.empty())
|
|
|
|
BreakContinueStack.back().ContinueCount += BC.ContinueCount;
|
|
|
|
// Counter tracks the exit block of the switch.
|
2015-05-02 07:41:09 +08:00
|
|
|
setCount(PGO.getRegionCount(S));
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-05-02 07:41:09 +08:00
|
|
|
void VisitSwitchCase(const SwitchCase *S) {
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = false;
|
|
|
|
// Counter for this particular case. This counts only jumps from the
|
|
|
|
// switch header and does not include fallthrough from the case before
|
|
|
|
// this one.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t CaseCount = PGO.getRegionCount(S);
|
2015-05-02 08:48:27 +08:00
|
|
|
setCount(CurrentCount + CaseCount);
|
2015-05-02 07:41:09 +08:00
|
|
|
// We need the count without fallthrough in the mapping, so it's more useful
|
|
|
|
// for branch probabilities.
|
|
|
|
CountMap[S] = CaseCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
Visit(S->getSubStmt());
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitIfStmt(const IfStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t ParentCount = CurrentCount;
|
2016-10-15 07:38:16 +08:00
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getCond());
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-05-02 07:41:09 +08:00
|
|
|
// Counter tracks the "then" part of an if statement. The count for
|
|
|
|
// the "else" part, if it exists, will be calculated from this counter.
|
|
|
|
uint64_t ThenCount = setCount(PGO.getRegionCount(S));
|
|
|
|
CountMap[S->getThen()] = ThenCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getThen());
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t OutCount = CurrentCount;
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t ElseCount = ParentCount - ThenCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
if (S->getElse()) {
|
2015-05-02 07:41:09 +08:00
|
|
|
setCount(ElseCount);
|
|
|
|
CountMap[S->getElse()] = ElseCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getElse());
|
2015-05-02 08:48:27 +08:00
|
|
|
OutCount += CurrentCount;
|
2015-05-02 07:41:09 +08:00
|
|
|
} else
|
|
|
|
OutCount += ElseCount;
|
|
|
|
setCount(OutCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitCXXTryStmt(const CXXTryStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
Visit(S->getTryBlock());
|
|
|
|
for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
|
|
|
|
Visit(S->getHandler(I));
|
|
|
|
// Counter tracks the continuation block of the try statement.
|
2015-05-02 07:41:09 +08:00
|
|
|
setCount(PGO.getRegionCount(S));
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitCXXCatchStmt(const CXXCatchStmt *S) {
|
|
|
|
RecordNextStmtCount = false;
|
|
|
|
// Counter tracks the catch statement's handler block.
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t CatchCount = setCount(PGO.getRegionCount(S));
|
|
|
|
CountMap[S] = CatchCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(S->getHandlerBlock());
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
|
|
|
|
RecordStmtCount(E);
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t ParentCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(E->getCond());
|
|
|
|
|
2015-05-02 07:41:09 +08:00
|
|
|
// Counter tracks the "true" part of a conditional operator. The
|
|
|
|
// count in the "false" part will be calculated from this counter.
|
|
|
|
uint64_t TrueCount = setCount(PGO.getRegionCount(E));
|
|
|
|
CountMap[E->getTrueExpr()] = TrueCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(E->getTrueExpr());
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t OutCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
|
2015-05-02 07:41:09 +08:00
|
|
|
uint64_t FalseCount = setCount(ParentCount - TrueCount);
|
|
|
|
CountMap[E->getFalseExpr()] = FalseCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(E->getFalseExpr());
|
2015-05-02 08:48:27 +08:00
|
|
|
OutCount += CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
|
2015-05-02 07:41:09 +08:00
|
|
|
setCount(OutCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitBinLAnd(const BinaryOperator *E) {
|
|
|
|
RecordStmtCount(E);
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t ParentCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(E->getLHS());
|
2015-05-02 07:41:09 +08:00
|
|
|
// Counter tracks the right hand side of a logical and operator.
|
|
|
|
uint64_t RHSCount = setCount(PGO.getRegionCount(E));
|
|
|
|
CountMap[E->getRHS()] = RHSCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(E->getRHS());
|
2015-05-02 08:48:27 +08:00
|
|
|
setCount(ParentCount + RHSCount - CurrentCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-02-24 03:27:00 +08:00
|
|
|
void VisitBinLOr(const BinaryOperator *E) {
|
|
|
|
RecordStmtCount(E);
|
2015-05-02 08:48:27 +08:00
|
|
|
uint64_t ParentCount = CurrentCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(E->getLHS());
|
2015-05-02 07:41:09 +08:00
|
|
|
// Counter tracks the right hand side of a logical or operator.
|
|
|
|
uint64_t RHSCount = setCount(PGO.getRegionCount(E));
|
|
|
|
CountMap[E->getRHS()] = RHSCount;
|
2015-02-24 03:27:00 +08:00
|
|
|
Visit(E->getRHS());
|
2015-05-02 08:48:27 +08:00
|
|
|
setCount(ParentCount + RHSCount - CurrentCount);
|
2015-02-24 03:27:00 +08:00
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
};
|
2015-10-07 07:40:43 +08:00
|
|
|
} // end anonymous namespace
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-04-17 00:03:27 +08:00
|
|
|
void PGOHash::combine(HashType Type) {
|
|
|
|
// Check that we never combine 0 and only have six bits.
|
|
|
|
assert(Type && "Hash is invalid: unexpected type 0");
|
|
|
|
assert(unsigned(Type) < TooBig && "Hash is invalid: too many types");
|
|
|
|
|
|
|
|
// Pass through MD5 if enough work has built up.
|
|
|
|
if (Count && Count % NumTypesPerWord == 0) {
|
|
|
|
using namespace llvm::support;
|
|
|
|
uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working);
|
|
|
|
MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
|
|
|
|
Working = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate the current type.
|
|
|
|
++Count;
|
|
|
|
Working = Working << NumBitsPerType | Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t PGOHash::finalize() {
|
|
|
|
// Use Working as the hash directly if we never used MD5.
|
|
|
|
if (Count <= NumTypesPerWord)
|
|
|
|
// No need to byte swap here, since none of the math was endian-dependent.
|
|
|
|
// This number will be byte-swapped as required on endianness transitions,
|
|
|
|
// so we will see the same value on the other side.
|
|
|
|
return Working;
|
|
|
|
|
|
|
|
// Check for remaining work in Working.
|
|
|
|
if (Working)
|
|
|
|
MD5.update(Working);
|
|
|
|
|
|
|
|
// Finalize the MD5 and return the hash.
|
|
|
|
llvm::MD5::MD5Result Result;
|
|
|
|
MD5.final(Result);
|
|
|
|
using namespace llvm::support;
|
2017-03-21 07:33:18 +08:00
|
|
|
return Result.low();
|
2014-04-17 00:03:27 +08:00
|
|
|
}
|
|
|
|
|
2015-12-06 22:32:39 +08:00
|
|
|
void CodeGenPGO::assignRegionCounters(GlobalDecl GD, llvm::Function *Fn) {
|
|
|
|
const Decl *D = GD.getDecl();
|
2017-07-01 05:02:14 +08:00
|
|
|
if (!D->hasBody())
|
|
|
|
return;
|
|
|
|
|
2016-02-05 02:39:09 +08:00
|
|
|
bool InstrumentRegions = CGM.getCodeGenOpts().hasProfileClangInstr();
|
2014-04-19 05:52:00 +08:00
|
|
|
llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
|
|
|
|
if (!InstrumentRegions && !PGOReader)
|
2014-01-07 06:27:43 +08:00
|
|
|
return;
|
2014-04-25 15:20:05 +08:00
|
|
|
if (D->isImplicit())
|
2014-01-07 06:27:43 +08:00
|
|
|
return;
|
2015-12-06 22:32:39 +08:00
|
|
|
// Constructors and destructors may be represented by several functions in IR.
|
|
|
|
// If so, instrument only base variant, others are implemented by delegation
|
|
|
|
// to the base one, it would be counted twice otherwise.
|
2017-02-24 09:15:19 +08:00
|
|
|
if (CGM.getTarget().getCXXABI().hasConstructorVariants()) {
|
|
|
|
if (isa<CXXDestructorDecl>(D) && GD.getDtorType() != Dtor_Base)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const auto *CCD = dyn_cast<CXXConstructorDecl>(D))
|
|
|
|
if (GD.getCtorType() != Ctor_Base &&
|
|
|
|
CodeGenFunction::IsConstructorDelegationValid(CCD))
|
|
|
|
return;
|
2015-12-06 22:32:39 +08:00
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
CGM.ClearUnusedCoverageMapping(D);
|
2014-03-06 12:55:41 +08:00
|
|
|
setFuncName(Fn);
|
2014-03-21 06:50:08 +08:00
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
mapRegionCounters(D);
|
2014-12-09 03:04:51 +08:00
|
|
|
if (CGM.getCodeGenOpts().CoverageMapping)
|
|
|
|
emitCounterRegionMapping(D);
|
2014-04-19 05:52:00 +08:00
|
|
|
if (PGOReader) {
|
2014-06-26 09:45:07 +08:00
|
|
|
SourceManager &SM = CGM.getContext().getSourceManager();
|
|
|
|
loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation()));
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
computeRegionCounts(D);
|
2014-04-19 05:52:00 +08:00
|
|
|
applyFunctionAttributes(PGOReader, Fn);
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
}
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenPGO::mapRegionCounters(const Decl *D) {
|
2017-11-15 07:56:53 +08:00
|
|
|
// Use the latest hash version when inserting instrumentation, but use the
|
|
|
|
// version in the indexed profile if we're reading PGO data.
|
|
|
|
PGOHashVersion HashVersion = PGO_HASH_LATEST;
|
|
|
|
if (auto *PGOReader = CGM.getPGOReader())
|
|
|
|
HashVersion = getPGOHashVersion(PGOReader, CGM);
|
|
|
|
|
2014-03-27 03:26:05 +08:00
|
|
|
RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
|
2017-11-15 07:56:53 +08:00
|
|
|
MapRegionCounters Walker(HashVersion, *RegionCounterMap);
|
2014-01-07 06:27:43 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
|
2014-04-12 01:16:13 +08:00
|
|
|
Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
|
2014-03-06 14:10:02 +08:00
|
|
|
else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
|
2014-04-12 01:16:13 +08:00
|
|
|
Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD));
|
2014-03-07 04:24:27 +08:00
|
|
|
else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
|
2014-04-12 01:16:13 +08:00
|
|
|
Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
|
2014-04-15 08:50:54 +08:00
|
|
|
else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
|
|
|
|
Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
|
2014-04-25 15:20:05 +08:00
|
|
|
assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
|
2014-01-07 06:27:43 +08:00
|
|
|
NumRegionCounters = Walker.NextCounter;
|
2014-04-17 00:03:27 +08:00
|
|
|
FunctionHash = Walker.Hash.finalize();
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
2016-07-12 06:57:44 +08:00
|
|
|
bool CodeGenPGO::skipRegionMappingForDecl(const Decl *D) {
|
2017-04-25 04:52:04 +08:00
|
|
|
if (!D->getBody())
|
|
|
|
return true;
|
|
|
|
|
2016-07-12 06:57:44 +08:00
|
|
|
// Don't map the functions in system headers.
|
|
|
|
const auto &SM = CGM.getContext().getSourceManager();
|
2018-08-10 05:08:08 +08:00
|
|
|
auto Loc = D->getBody()->getBeginLoc();
|
2016-07-12 06:57:44 +08:00
|
|
|
return SM.isInSystemHeader(Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
|
|
|
|
if (skipRegionMappingForDecl(D))
|
2014-08-05 02:41:51 +08:00
|
|
|
return;
|
|
|
|
|
2014-12-09 03:04:51 +08:00
|
|
|
std::string CoverageMapping;
|
2014-08-05 02:41:51 +08:00
|
|
|
llvm::raw_string_ostream OS(CoverageMapping);
|
|
|
|
CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
|
|
|
|
CGM.getContext().getSourceManager(),
|
2014-10-03 00:44:01 +08:00
|
|
|
CGM.getLangOpts(), RegionCounterMap.get());
|
2014-08-05 02:41:51 +08:00
|
|
|
MappingGen.emitCounterMapping(D, OS);
|
|
|
|
OS.flush();
|
2014-12-09 03:04:51 +08:00
|
|
|
|
|
|
|
if (CoverageMapping.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CGM.getCoverageMapping()->addFunctionMappingRecord(
|
|
|
|
FuncNameVar, FuncName, FunctionHash, CoverageMapping);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-23 08:31:16 +08:00
|
|
|
CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef Name,
|
2014-08-05 02:41:51 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
2016-07-12 06:57:44 +08:00
|
|
|
if (skipRegionMappingForDecl(D))
|
2014-08-05 02:41:51 +08:00
|
|
|
return;
|
|
|
|
|
2014-12-09 03:04:51 +08:00
|
|
|
std::string CoverageMapping;
|
2014-08-05 02:41:51 +08:00
|
|
|
llvm::raw_string_ostream OS(CoverageMapping);
|
|
|
|
CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
|
|
|
|
CGM.getContext().getSourceManager(),
|
|
|
|
CGM.getLangOpts());
|
|
|
|
MappingGen.emitEmptyMapping(D, OS);
|
|
|
|
OS.flush();
|
2014-12-09 03:04:51 +08:00
|
|
|
|
|
|
|
if (CoverageMapping.empty())
|
|
|
|
return;
|
|
|
|
|
2015-04-23 08:31:16 +08:00
|
|
|
setFuncName(Name, Linkage);
|
2014-12-09 03:04:51 +08:00
|
|
|
CGM.getCoverageMapping()->addFunctionMappingRecord(
|
2016-01-08 04:05:55 +08:00
|
|
|
FuncNameVar, FuncName, FunctionHash, CoverageMapping, false);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
void CodeGenPGO::computeRegionCounts(const Decl *D) {
|
2014-03-27 03:26:05 +08:00
|
|
|
StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>);
|
2014-03-27 03:26:02 +08:00
|
|
|
ComputeRegionCounts Walker(*StmtCountMap, *this);
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
|
|
|
|
Walker.VisitFunctionDecl(FD);
|
2014-03-06 14:10:02 +08:00
|
|
|
else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
|
|
|
|
Walker.VisitObjCMethodDecl(MD);
|
2014-03-07 04:24:27 +08:00
|
|
|
else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
|
|
|
|
Walker.VisitBlockDecl(BD);
|
2014-04-15 08:50:54 +08:00
|
|
|
else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
|
|
|
|
Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD));
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
}
|
|
|
|
|
2014-04-19 05:52:00 +08:00
|
|
|
void
|
|
|
|
CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
|
|
|
|
llvm::Function *Fn) {
|
2014-03-13 02:14:32 +08:00
|
|
|
if (!haveRegionCounts())
|
|
|
|
return;
|
|
|
|
|
2015-10-07 07:40:43 +08:00
|
|
|
uint64_t FunctionCount = getRegionCount(nullptr);
|
2015-05-28 05:58:42 +08:00
|
|
|
Fn->setEntryCount(FunctionCount);
|
2014-03-13 02:14:32 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 14:35:45 +08:00
|
|
|
void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S,
|
|
|
|
llvm::Value *StepV) {
|
2016-02-05 02:39:09 +08:00
|
|
|
if (!CGM.getCodeGenOpts().hasProfileClangInstr() || !RegionCounterMap)
|
2014-01-07 06:27:43 +08:00
|
|
|
return;
|
2015-11-07 07:00:41 +08:00
|
|
|
if (!Builder.GetInsertBlock())
|
2015-01-09 09:46:40 +08:00
|
|
|
return;
|
2015-04-24 07:06:47 +08:00
|
|
|
|
|
|
|
unsigned Counter = (*RegionCounterMap)[S];
|
2014-12-09 03:04:51 +08:00
|
|
|
auto *I8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
2017-02-25 14:35:45 +08:00
|
|
|
|
|
|
|
llvm::Value *Args[] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
|
|
|
|
Builder.getInt64(FunctionHash),
|
|
|
|
Builder.getInt32(NumRegionCounters),
|
|
|
|
Builder.getInt32(Counter), StepV};
|
|
|
|
if (!StepV)
|
|
|
|
Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment),
|
|
|
|
makeArrayRef(Args, 4));
|
|
|
|
else
|
|
|
|
Builder.CreateCall(
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment_step),
|
|
|
|
makeArrayRef(Args));
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
2016-01-24 06:50:44 +08:00
|
|
|
// This method either inserts a call to the profile run-time during
|
|
|
|
// instrumentation or puts profile data into metadata for PGO use.
|
|
|
|
void CodeGenPGO::valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
|
|
|
|
llvm::Instruction *ValueSite, llvm::Value *ValuePtr) {
|
|
|
|
|
|
|
|
if (!EnableValueProfiling)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ValuePtr || !ValueSite || !Builder.GetInsertBlock())
|
|
|
|
return;
|
|
|
|
|
2016-04-01 02:41:34 +08:00
|
|
|
if (isa<llvm::Constant>(ValuePtr))
|
|
|
|
return;
|
|
|
|
|
2016-02-05 02:39:09 +08:00
|
|
|
bool InstrumentValueSites = CGM.getCodeGenOpts().hasProfileClangInstr();
|
2016-01-24 06:50:44 +08:00
|
|
|
if (InstrumentValueSites && RegionCounterMap) {
|
2016-03-30 04:44:09 +08:00
|
|
|
auto BuilderInsertPoint = Builder.saveIP();
|
|
|
|
Builder.SetInsertPoint(ValueSite);
|
2016-01-24 06:50:44 +08:00
|
|
|
llvm::Value *Args[5] = {
|
2016-03-30 04:44:09 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(FuncNameVar, Builder.getInt8PtrTy()),
|
2016-01-24 06:50:44 +08:00
|
|
|
Builder.getInt64(FunctionHash),
|
|
|
|
Builder.CreatePtrToInt(ValuePtr, Builder.getInt64Ty()),
|
|
|
|
Builder.getInt32(ValueKind),
|
|
|
|
Builder.getInt32(NumValueSites[ValueKind]++)
|
|
|
|
};
|
|
|
|
Builder.CreateCall(
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::instrprof_value_profile), Args);
|
2016-03-30 04:44:09 +08:00
|
|
|
Builder.restoreIP(BuilderInsertPoint);
|
2016-01-24 06:50:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
|
|
|
|
if (PGOReader && haveRegionCounts()) {
|
|
|
|
// We record the top most called three functions at each call site.
|
|
|
|
// Profile metadata contains "VP" string identifying this metadata
|
|
|
|
// as value profiling data, then a uint32_t value for the value profiling
|
|
|
|
// kind, a uint64_t value for the total number of times the call is
|
|
|
|
// executed, followed by the function hash and execution count (uint64_t)
|
|
|
|
// pairs for each function.
|
|
|
|
if (NumValueSites[ValueKind] >= ProfRecord->getNumValueSites(ValueKind))
|
|
|
|
return;
|
|
|
|
|
2016-02-05 03:54:17 +08:00
|
|
|
llvm::annotateValueSite(CGM.getModule(), *ValueSite, *ProfRecord,
|
|
|
|
(llvm::InstrProfValueKind)ValueKind,
|
|
|
|
NumValueSites[ValueKind]);
|
2016-01-24 06:50:44 +08:00
|
|
|
|
|
|
|
NumValueSites[ValueKind]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 09:45:07 +08:00
|
|
|
void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
|
|
|
|
bool IsInMainFile) {
|
|
|
|
CGM.getPGOStats().addVisited(IsInMainFile);
|
2014-12-03 06:38:52 +08:00
|
|
|
RegionCounts.clear();
|
2016-05-19 11:54:54 +08:00
|
|
|
llvm::Expected<llvm::InstrProfRecord> RecordExpected =
|
2016-01-24 06:50:44 +08:00
|
|
|
PGOReader->getInstrProfRecord(FuncName, FunctionHash);
|
2016-05-19 11:54:54 +08:00
|
|
|
if (auto E = RecordExpected.takeError()) {
|
|
|
|
auto IPE = llvm::InstrProfError::take(std::move(E));
|
|
|
|
if (IPE == llvm::instrprof_error::unknown_function)
|
2014-08-02 06:50:16 +08:00
|
|
|
CGM.getPGOStats().addMissing(IsInMainFile);
|
2016-05-19 11:54:54 +08:00
|
|
|
else if (IPE == llvm::instrprof_error::hash_mismatch)
|
2014-08-02 06:50:16 +08:00
|
|
|
CGM.getPGOStats().addMismatched(IsInMainFile);
|
2016-05-19 11:54:54 +08:00
|
|
|
else if (IPE == llvm::instrprof_error::malformed)
|
2014-08-02 06:50:16 +08:00
|
|
|
// TODO: Consider a more specific warning for this case.
|
|
|
|
CGM.getPGOStats().addMismatched(IsInMainFile);
|
2016-01-24 06:50:44 +08:00
|
|
|
return;
|
2014-04-16 05:22:35 +08:00
|
|
|
}
|
2016-01-24 06:50:44 +08:00
|
|
|
ProfRecord =
|
2016-05-19 11:54:54 +08:00
|
|
|
llvm::make_unique<llvm::InstrProfRecord>(std::move(RecordExpected.get()));
|
2016-01-24 06:50:44 +08:00
|
|
|
RegionCounts = ProfRecord->Counts;
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Calculate what to divide by to scale weights.
|
2014-03-12 02:18:10 +08:00
|
|
|
///
|
|
|
|
/// Given the maximum weight, calculate a divisor that will scale all the
|
|
|
|
/// weights to strictly less than UINT32_MAX.
|
|
|
|
static uint64_t calculateWeightScale(uint64_t MaxWeight) {
|
|
|
|
return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Scale an individual branch weight (and add 1).
|
2014-03-12 02:18:10 +08:00
|
|
|
///
|
|
|
|
/// Scale a 64-bit weight down to 32-bits using \c Scale.
|
|
|
|
///
|
|
|
|
/// According to Laplace's Rule of Succession, it is better to compute the
|
|
|
|
/// weight based on the count plus 1, so universally add 1 to the value.
|
|
|
|
///
|
|
|
|
/// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
|
|
|
|
/// greater than \c Weight.
|
|
|
|
static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
|
|
|
|
assert(Scale && "scale by 0?");
|
|
|
|
uint64_t Scaled = Weight / Scale + 1;
|
|
|
|
assert(Scaled <= UINT32_MAX && "overflow 32-bits");
|
|
|
|
return Scaled;
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:00:55 +08:00
|
|
|
llvm::MDNode *CodeGenFunction::createProfileWeights(uint64_t TrueCount,
|
|
|
|
uint64_t FalseCount) {
|
2014-03-12 02:18:10 +08:00
|
|
|
// Check for empty weights.
|
2014-01-07 06:27:43 +08:00
|
|
|
if (!TrueCount && !FalseCount)
|
2014-03-21 02:40:55 +08:00
|
|
|
return nullptr;
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-12 02:18:10 +08:00
|
|
|
// Calculate how to scale down to 32-bits.
|
|
|
|
uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
|
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
llvm::MDBuilder MDHelper(CGM.getLLVMContext());
|
2014-03-12 02:18:10 +08:00
|
|
|
return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
|
|
|
|
scaleBranchWeight(FalseCount, Scale));
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
2015-05-02 13:00:55 +08:00
|
|
|
llvm::MDNode *
|
|
|
|
CodeGenFunction::createProfileWeights(ArrayRef<uint64_t> Weights) {
|
2014-03-12 02:18:10 +08:00
|
|
|
// We need at least two elements to create meaningful weights.
|
|
|
|
if (Weights.size() < 2)
|
2014-03-21 02:40:55 +08:00
|
|
|
return nullptr;
|
2014-03-12 02:18:10 +08:00
|
|
|
|
2014-04-04 10:48:51 +08:00
|
|
|
// Check for empty weights.
|
|
|
|
uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
|
|
|
|
if (MaxWeight == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
2014-03-12 02:18:10 +08:00
|
|
|
// Calculate how to scale down to 32-bits.
|
2014-04-04 10:48:51 +08:00
|
|
|
uint64_t Scale = calculateWeightScale(MaxWeight);
|
2014-03-12 02:18:10 +08:00
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
SmallVector<uint32_t, 16> ScaledWeights;
|
|
|
|
ScaledWeights.reserve(Weights.size());
|
2014-03-12 02:18:10 +08:00
|
|
|
for (uint64_t W : Weights)
|
|
|
|
ScaledWeights.push_back(scaleBranchWeight(W, Scale));
|
|
|
|
|
|
|
|
llvm::MDBuilder MDHelper(CGM.getLLVMContext());
|
2014-01-07 06:27:43 +08:00
|
|
|
return MDHelper.createBranchWeights(ScaledWeights);
|
|
|
|
}
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
|
2015-05-02 13:00:55 +08:00
|
|
|
llvm::MDNode *CodeGenFunction::createProfileWeightsForLoop(const Stmt *Cond,
|
|
|
|
uint64_t LoopCount) {
|
|
|
|
if (!PGO.haveRegionCounts())
|
2014-03-21 02:40:55 +08:00
|
|
|
return nullptr;
|
2015-05-02 13:00:55 +08:00
|
|
|
Optional<uint64_t> CondCount = PGO.getStmtCount(Cond);
|
2015-04-13 20:23:19 +08:00
|
|
|
assert(CondCount.hasValue() && "missing expected loop condition count");
|
|
|
|
if (*CondCount == 0)
|
2014-03-21 02:40:55 +08:00
|
|
|
return nullptr;
|
2015-05-02 13:00:55 +08:00
|
|
|
return createProfileWeights(LoopCount,
|
|
|
|
std::max(*CondCount, LoopCount) - LoopCount);
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
}
|