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"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2014-04-11 07:37:34 +08:00
|
|
|
#include "llvm/Config/config.h" // for strtoull()/strtoul() define
|
2014-01-07 06:27:43 +08:00
|
|
|
#include "llvm/IR/MDBuilder.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2014-03-13 05:06:31 +08:00
|
|
|
static void ReportBadPGOData(CodeGenModule &CGM, const char *Message) {
|
|
|
|
DiagnosticsEngine &Diags = CGM.getDiags();
|
|
|
|
unsigned diagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0");
|
|
|
|
Diags.Report(diagID) << Message;
|
|
|
|
}
|
|
|
|
|
|
|
|
PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path)
|
|
|
|
: CGM(CGM) {
|
|
|
|
if (llvm::MemoryBuffer::getFile(Path, DataBuffer)) {
|
|
|
|
ReportBadPGOData(CGM, "failed to open pgo data file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DataBuffer->getBufferSize() > std::numeric_limits<unsigned>::max()) {
|
|
|
|
ReportBadPGOData(CGM, "pgo data file too big");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan through the data file and map each function to the corresponding
|
|
|
|
// file offset where its counts are stored.
|
|
|
|
const char *BufferStart = DataBuffer->getBufferStart();
|
|
|
|
const char *BufferEnd = DataBuffer->getBufferEnd();
|
|
|
|
const char *CurPtr = BufferStart;
|
|
|
|
uint64_t MaxCount = 0;
|
|
|
|
while (CurPtr < BufferEnd) {
|
|
|
|
// Read the function name.
|
|
|
|
const char *FuncStart = CurPtr;
|
|
|
|
// For Objective-C methods, the name may include whitespace, so search
|
|
|
|
// backward from the end of the line to find the space that separates the
|
|
|
|
// name from the number of counters. (This is a temporary hack since we are
|
|
|
|
// going to completely replace this file format in the near future.)
|
|
|
|
CurPtr = strchr(CurPtr, '\n');
|
|
|
|
if (!CurPtr) {
|
|
|
|
ReportBadPGOData(CGM, "pgo data file has malformed function entry");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
StringRef FuncName(FuncStart, CurPtr - FuncStart);
|
|
|
|
|
2014-03-19 05:58:06 +08:00
|
|
|
// Skip over the function hash.
|
|
|
|
CurPtr = strchr(++CurPtr, '\n');
|
|
|
|
if (!CurPtr) {
|
|
|
|
ReportBadPGOData(CGM, "pgo data file is missing the function hash");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-13 05:06:31 +08:00
|
|
|
// Read the number of counters.
|
|
|
|
char *EndPtr;
|
2014-04-11 07:37:34 +08:00
|
|
|
unsigned NumCounters = strtoul(++CurPtr, &EndPtr, 10);
|
2014-03-13 05:06:31 +08:00
|
|
|
if (EndPtr == CurPtr || *EndPtr != '\n' || NumCounters <= 0) {
|
|
|
|
ReportBadPGOData(CGM, "pgo data file has unexpected number of counters");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CurPtr = EndPtr;
|
|
|
|
|
|
|
|
// Read function count.
|
2014-04-11 07:37:34 +08:00
|
|
|
uint64_t Count = strtoull(CurPtr, &EndPtr, 10);
|
2014-03-13 05:06:31 +08:00
|
|
|
if (EndPtr == CurPtr || *EndPtr != '\n') {
|
|
|
|
ReportBadPGOData(CGM, "pgo-data file has bad count value");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CurPtr = EndPtr; // Point to '\n'.
|
|
|
|
FunctionCounts[FuncName] = Count;
|
|
|
|
MaxCount = Count > MaxCount ? Count : MaxCount;
|
|
|
|
|
|
|
|
// There is one line for each counter; skip over those lines.
|
|
|
|
// Since function count is already read, we start the loop from 1.
|
|
|
|
for (unsigned N = 1; N < NumCounters; ++N) {
|
|
|
|
CurPtr = strchr(++CurPtr, '\n');
|
|
|
|
if (!CurPtr) {
|
|
|
|
ReportBadPGOData(CGM, "pgo data file is missing some counter info");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over the blank line separating functions.
|
|
|
|
CurPtr += 2;
|
|
|
|
|
|
|
|
DataOffsets[FuncName] = FuncStart - BufferStart;
|
|
|
|
}
|
|
|
|
MaxFunctionCount = MaxCount;
|
|
|
|
}
|
|
|
|
|
2014-03-19 05:58:06 +08:00
|
|
|
bool PGOProfileData::getFunctionCounts(StringRef FuncName, uint64_t &FuncHash,
|
2014-03-13 05:06:31 +08:00
|
|
|
std::vector<uint64_t> &Counts) {
|
|
|
|
// Find the relevant section of the pgo-data file.
|
|
|
|
llvm::StringMap<unsigned>::const_iterator OffsetIter =
|
|
|
|
DataOffsets.find(FuncName);
|
|
|
|
if (OffsetIter == DataOffsets.end())
|
|
|
|
return true;
|
|
|
|
const char *CurPtr = DataBuffer->getBufferStart() + OffsetIter->getValue();
|
|
|
|
|
|
|
|
// Skip over the function name.
|
|
|
|
CurPtr = strchr(CurPtr, '\n');
|
|
|
|
assert(CurPtr && "pgo-data has corrupted function entry");
|
|
|
|
|
|
|
|
char *EndPtr;
|
2014-03-19 05:58:06 +08:00
|
|
|
// Read the function hash.
|
2014-04-11 07:37:34 +08:00
|
|
|
FuncHash = strtoull(++CurPtr, &EndPtr, 10);
|
2014-03-19 05:58:06 +08:00
|
|
|
assert(EndPtr != CurPtr && *EndPtr == '\n' &&
|
|
|
|
"pgo-data file has corrupted function hash");
|
|
|
|
CurPtr = EndPtr;
|
|
|
|
|
|
|
|
// Read the number of counters.
|
2014-04-11 07:37:34 +08:00
|
|
|
unsigned NumCounters = strtoul(++CurPtr, &EndPtr, 10);
|
2014-03-13 05:06:31 +08:00
|
|
|
assert(EndPtr != CurPtr && *EndPtr == '\n' && NumCounters > 0 &&
|
|
|
|
"pgo-data file has corrupted number of counters");
|
|
|
|
CurPtr = EndPtr;
|
|
|
|
|
|
|
|
Counts.reserve(NumCounters);
|
|
|
|
|
|
|
|
for (unsigned N = 0; N < NumCounters; ++N) {
|
|
|
|
// Read the count value.
|
2014-04-11 07:37:34 +08:00
|
|
|
uint64_t Count = strtoull(CurPtr, &EndPtr, 10);
|
2014-03-13 05:06:31 +08:00
|
|
|
if (EndPtr == CurPtr || *EndPtr != '\n') {
|
|
|
|
ReportBadPGOData(CGM, "pgo-data file has bad count value");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Counts.push_back(Count);
|
|
|
|
CurPtr = EndPtr + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the number of counters matches up.
|
|
|
|
if (Counts.size() != NumCounters) {
|
|
|
|
ReportBadPGOData(CGM, "pgo-data file has inconsistent counters");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-06 12:55:41 +08:00
|
|
|
void CodeGenPGO::setFuncName(llvm::Function *Fn) {
|
2014-03-18 05:18:30 +08:00
|
|
|
RawFuncName = Fn->getName();
|
2014-03-06 12:55:41 +08:00
|
|
|
|
|
|
|
// Function names may be prefixed with a binary '1' to indicate
|
|
|
|
// that the backend should not modify the symbols due to any platform
|
|
|
|
// naming convention. Do not include that '1' in the PGO profile name.
|
2014-03-18 05:18:30 +08:00
|
|
|
if (RawFuncName[0] == '\1')
|
|
|
|
RawFuncName = RawFuncName.substr(1);
|
2014-03-06 12:55:41 +08:00
|
|
|
|
|
|
|
if (!Fn->hasLocalLinkage()) {
|
2014-03-27 03:26:05 +08:00
|
|
|
PrefixedFuncName.reset(new std::string(RawFuncName));
|
2014-03-06 12:55:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For local symbols, prepend the main file name to distinguish them.
|
|
|
|
// Do not include the full path in the file name since there's no guarantee
|
|
|
|
// that it will stay the same, e.g., if the files are checked out from
|
|
|
|
// version control in different locations.
|
2014-03-27 03:26:05 +08:00
|
|
|
PrefixedFuncName.reset(new std::string(CGM.getCodeGenOpts().MainFileName));
|
2014-03-18 05:18:30 +08:00
|
|
|
if (PrefixedFuncName->empty())
|
|
|
|
PrefixedFuncName->assign("<unknown>");
|
|
|
|
PrefixedFuncName->append(":");
|
|
|
|
PrefixedFuncName->append(RawFuncName);
|
2014-03-06 12:55:41 +08:00
|
|
|
}
|
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
static llvm::Function *getRegisterFunc(CodeGenModule &CGM) {
|
2014-03-21 04:00:41 +08:00
|
|
|
return CGM.getModule().getFunction("__llvm_profile_register_functions");
|
2014-03-18 05:18:30 +08:00
|
|
|
}
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) {
|
2014-03-20 11:57:11 +08:00
|
|
|
// Don't do this for Darwin. compiler-rt uses linker magic.
|
|
|
|
if (CGM.getTarget().getTriple().isOSDarwin())
|
|
|
|
return nullptr;
|
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
// Only need to insert this once per module.
|
|
|
|
if (llvm::Function *RegisterF = getRegisterFunc(CGM))
|
|
|
|
return &RegisterF->getEntryBlock();
|
|
|
|
|
|
|
|
// Construct the function.
|
|
|
|
auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
|
|
|
|
auto *RegisterFTy = llvm::FunctionType::get(VoidTy, false);
|
|
|
|
auto *RegisterF = llvm::Function::Create(RegisterFTy,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2014-03-21 04:00:41 +08:00
|
|
|
"__llvm_profile_register_functions",
|
2014-03-18 05:18:30 +08:00
|
|
|
&CGM.getModule());
|
|
|
|
RegisterF->setUnnamedAddr(true);
|
2014-01-07 06:27:43 +08:00
|
|
|
if (CGM.getCodeGenOpts().DisableRedZone)
|
2014-03-18 05:18:30 +08:00
|
|
|
RegisterF->addFnAttr(llvm::Attribute::NoRedZone);
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
// Construct and return the entry block.
|
|
|
|
auto *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", RegisterF);
|
|
|
|
CGBuilderTy Builder(BB);
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
return BB;
|
|
|
|
}
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
static llvm::Constant *getOrInsertRuntimeRegister(CodeGenModule &CGM) {
|
|
|
|
auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
|
|
|
|
auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
|
|
|
auto *RuntimeRegisterTy = llvm::FunctionType::get(VoidTy, VoidPtrTy, false);
|
2014-03-21 04:00:41 +08:00
|
|
|
return CGM.getModule().getOrInsertFunction("__llvm_profile_register_function",
|
2014-03-18 05:18:30 +08:00
|
|
|
RuntimeRegisterTy);
|
|
|
|
}
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-20 11:17:15 +08:00
|
|
|
static bool isMachO(const CodeGenModule &CGM) {
|
|
|
|
return CGM.getTarget().getTriple().isOSBinFormatMachO();
|
|
|
|
}
|
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
static StringRef getCountersSection(const CodeGenModule &CGM) {
|
2014-03-21 04:00:41 +08:00
|
|
|
return isMachO(CGM) ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
static StringRef getNameSection(const CodeGenModule &CGM) {
|
2014-03-21 04:00:41 +08:00
|
|
|
return isMachO(CGM) ? "__DATA,__llvm_prf_names" : "__llvm_prf_names";
|
2014-03-18 05:18:30 +08:00
|
|
|
}
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
static StringRef getDataSection(const CodeGenModule &CGM) {
|
2014-03-21 04:00:41 +08:00
|
|
|
return isMachO(CGM) ? "__DATA,__llvm_prf_data" : "__llvm_prf_data";
|
2014-03-18 05:18:30 +08:00
|
|
|
}
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
llvm::GlobalVariable *CodeGenPGO::buildDataVar() {
|
|
|
|
// Create name variable.
|
2014-01-07 06:27:43 +08:00
|
|
|
llvm::LLVMContext &Ctx = CGM.getLLVMContext();
|
2014-03-18 05:18:30 +08:00
|
|
|
auto *VarName = llvm::ConstantDataArray::getString(Ctx, getFuncName(),
|
|
|
|
false);
|
|
|
|
auto *Name = new llvm::GlobalVariable(CGM.getModule(), VarName->getType(),
|
2014-03-21 06:49:50 +08:00
|
|
|
true, VarLinkage, VarName,
|
2014-03-18 05:18:30 +08:00
|
|
|
getFuncVarName("name"));
|
|
|
|
Name->setSection(getNameSection(CGM));
|
|
|
|
Name->setAlignment(1);
|
|
|
|
|
|
|
|
// Create data variable.
|
|
|
|
auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
|
2014-03-19 05:58:06 +08:00
|
|
|
auto *Int64Ty = llvm::Type::getInt64Ty(Ctx);
|
2014-03-18 05:18:30 +08:00
|
|
|
auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
|
|
|
|
auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx);
|
|
|
|
llvm::Type *DataTypes[] = {
|
2014-03-19 05:58:06 +08:00
|
|
|
Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy
|
2014-03-18 05:18:30 +08:00
|
|
|
};
|
|
|
|
auto *DataTy = llvm::StructType::get(Ctx, makeArrayRef(DataTypes));
|
|
|
|
llvm::Constant *DataVals[] = {
|
|
|
|
llvm::ConstantInt::get(Int32Ty, getFuncName().size()),
|
|
|
|
llvm::ConstantInt::get(Int32Ty, NumRegionCounters),
|
2014-03-19 05:58:06 +08:00
|
|
|
llvm::ConstantInt::get(Int64Ty, FunctionHash),
|
2014-03-18 05:18:30 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(Name, Int8PtrTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy)
|
|
|
|
};
|
|
|
|
auto *Data =
|
2014-03-21 06:49:50 +08:00
|
|
|
new llvm::GlobalVariable(CGM.getModule(), DataTy, true, VarLinkage,
|
2014-03-18 05:18:30 +08:00
|
|
|
llvm::ConstantStruct::get(DataTy, DataVals),
|
|
|
|
getFuncVarName("data"));
|
|
|
|
|
|
|
|
// All the data should be packed into an array in its own section.
|
|
|
|
Data->setSection(getDataSection(CGM));
|
|
|
|
Data->setAlignment(8);
|
|
|
|
|
|
|
|
// Make sure the data doesn't get deleted.
|
|
|
|
CGM.addUsedGlobal(Data);
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenPGO::emitInstrumentationData() {
|
|
|
|
if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Build the data.
|
|
|
|
auto *Data = buildDataVar();
|
|
|
|
|
|
|
|
// Register the data.
|
2014-03-20 11:57:11 +08:00
|
|
|
auto *RegisterBB = getOrInsertRegisterBB(CGM);
|
|
|
|
if (!RegisterBB)
|
|
|
|
return;
|
|
|
|
CGBuilderTy Builder(RegisterBB->getTerminator());
|
2014-03-18 05:18:30 +08:00
|
|
|
auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
|
|
|
Builder.CreateCall(getOrInsertRuntimeRegister(CGM),
|
|
|
|
Builder.CreateBitCast(Data, VoidPtrTy));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) {
|
|
|
|
if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
|
2014-03-21 02:40:55 +08:00
|
|
|
return nullptr;
|
2014-03-18 05:18:30 +08:00
|
|
|
|
2014-04-11 02:13:13 +08:00
|
|
|
assert(CGM.getModule().getFunction("__llvm_profile_init") == nullptr &&
|
|
|
|
"profile initialization already emitted");
|
2014-03-18 05:18:30 +08:00
|
|
|
|
2014-03-21 03:23:46 +08:00
|
|
|
// Get the function to call at initialization.
|
2014-03-18 05:18:30 +08:00
|
|
|
llvm::Constant *RegisterF = getRegisterFunc(CGM);
|
2014-03-21 03:23:46 +08:00
|
|
|
if (!RegisterF)
|
2014-03-21 02:40:55 +08:00
|
|
|
return nullptr;
|
2014-03-18 05:18:30 +08:00
|
|
|
|
|
|
|
// Create the initialization function.
|
|
|
|
auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
|
|
|
|
auto *F = llvm::Function::Create(llvm::FunctionType::get(VoidTy, false),
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2014-03-21 04:00:41 +08:00
|
|
|
"__llvm_profile_init", &CGM.getModule());
|
2014-01-07 06:27:43 +08:00
|
|
|
F->setUnnamedAddr(true);
|
|
|
|
F->addFnAttr(llvm::Attribute::NoInline);
|
|
|
|
if (CGM.getCodeGenOpts().DisableRedZone)
|
|
|
|
F->addFnAttr(llvm::Attribute::NoRedZone);
|
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
// Add the basic block and the necessary calls.
|
|
|
|
CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F));
|
2014-03-21 03:23:46 +08:00
|
|
|
Builder.CreateCall(RegisterF);
|
2014-03-18 05:18:30 +08:00
|
|
|
Builder.CreateRetVoid();
|
2014-01-07 06:27:43 +08:00
|
|
|
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2014-04-12 01:16:13 +08:00
|
|
|
/// A RecursiveASTVisitor that fills a map of statements to PGO counters.
|
|
|
|
struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
|
2014-01-07 06:27:43 +08:00
|
|
|
/// The next counter value to assign.
|
|
|
|
unsigned NextCounter;
|
|
|
|
/// The map of statements to counters.
|
2014-03-27 03:26:02 +08:00
|
|
|
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-27 03:26:02 +08:00
|
|
|
MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap)
|
|
|
|
: NextCounter(0), CounterMap(CounterMap) {}
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-04-12 07:06:35 +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; }
|
|
|
|
bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
|
2014-04-12 01:16:13 +08:00
|
|
|
|
|
|
|
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:
|
|
|
|
CounterMap[D->getBody()] = NextCounter++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-12 01:16:13 +08:00
|
|
|
bool VisitStmt(const Stmt *S) {
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Stmt::LabelStmtClass:
|
|
|
|
case Stmt::WhileStmtClass:
|
|
|
|
case Stmt::DoStmtClass:
|
|
|
|
case Stmt::ForStmtClass:
|
|
|
|
case Stmt::CXXForRangeStmtClass:
|
|
|
|
case Stmt::ObjCForCollectionStmtClass:
|
|
|
|
case Stmt::SwitchStmtClass:
|
|
|
|
case Stmt::CaseStmtClass:
|
|
|
|
case Stmt::DefaultStmtClass:
|
|
|
|
case Stmt::IfStmtClass:
|
|
|
|
case Stmt::CXXTryStmtClass:
|
|
|
|
case Stmt::CXXCatchStmtClass:
|
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
case Stmt::BinaryConditionalOperatorClass:
|
|
|
|
CounterMap[S] = NextCounter++;
|
|
|
|
break;
|
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
const BinaryOperator *BO = cast<BinaryOperator>(S);
|
|
|
|
if (BO->getOpcode() == BO_LAnd || BO->getOpcode() == BO_LOr)
|
|
|
|
CounterMap[S] = NextCounter++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2014-01-07 06:27:43 +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
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
/// The map of statements to count values.
|
2014-03-27 03:26:02 +08:00
|
|
|
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
|
|
|
|
|
|
|
/// 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-03-27 03:26:02 +08:00
|
|
|
ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap,
|
|
|
|
CodeGenPGO &PGO)
|
|
|
|
: PGO(PGO), RecordNextStmtCount(false), CountMap(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
|
|
|
|
|
|
|
void RecordStmtCount(const Stmt *S) {
|
|
|
|
if (RecordNextStmtCount) {
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S] = PGO.getCurrentRegionCount();
|
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
|
|
|
RecordNextStmtCount = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitStmt(const Stmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
for (Stmt::const_child_range I = S->children(); I; ++I) {
|
|
|
|
if (*I)
|
|
|
|
this->Visit(*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 07:37:36 +08:00
|
|
|
void VisitFunctionDecl(const FunctionDecl *D) {
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks entry to the function body.
|
2014-04-11 07:37:36 +08:00
|
|
|
RegionCounter Cnt(PGO, 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
|
|
|
Cnt.beginRegion();
|
2014-04-11 07:37:36 +08:00
|
|
|
CountMap[D->getBody()] = PGO.getCurrentRegionCount();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-12 07:06:35 +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) {}
|
|
|
|
|
2014-04-11 07:37:36 +08:00
|
|
|
void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks entry to the method body.
|
2014-04-11 07:37:36 +08:00
|
|
|
RegionCounter Cnt(PGO, D->getBody());
|
2014-03-06 14:10:02 +08:00
|
|
|
Cnt.beginRegion();
|
2014-04-11 07:37:36 +08:00
|
|
|
CountMap[D->getBody()] = PGO.getCurrentRegionCount();
|
|
|
|
Visit(D->getBody());
|
2014-03-06 14:10:02 +08:00
|
|
|
}
|
|
|
|
|
2014-04-11 07:37:36 +08:00
|
|
|
void VisitBlockDecl(const BlockDecl *D) {
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks entry to the block body.
|
2014-04-11 07:37:36 +08:00
|
|
|
RegionCounter Cnt(PGO, D->getBody());
|
2014-03-07 04:24:27 +08:00
|
|
|
Cnt.beginRegion();
|
2014-04-11 07:37:36 +08:00
|
|
|
CountMap[D->getBody()] = PGO.getCurrentRegionCount();
|
|
|
|
Visit(D->getBody());
|
2014-03-07 04:24:27 +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 VisitReturnStmt(const ReturnStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
if (S->getRetValue())
|
|
|
|
Visit(S->getRetValue());
|
|
|
|
PGO.setCurrentRegionUnreachable();
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitGotoStmt(const GotoStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
PGO.setCurrentRegionUnreachable();
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitLabelStmt(const LabelStmt *S) {
|
|
|
|
RecordNextStmtCount = false;
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the block following the label.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S] = PGO.getCurrentRegionCount();
|
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->getSubStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitBreakStmt(const BreakStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
|
|
|
|
BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
|
|
|
|
PGO.setCurrentRegionUnreachable();
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitContinueStmt(const ContinueStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
|
|
|
|
BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
|
|
|
|
PGO.setCurrentRegionUnreachable();
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitWhileStmt(const WhileStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the body of the loop.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
// Visit the body region first so the break/continue adjustments can be
|
|
|
|
// included when visiting the condition.
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getBody()] = PGO.getCurrentRegionCount();
|
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->getBody());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
|
|
|
|
// ...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();
|
|
|
|
Cnt.setCurrentRegionCount(Cnt.getParentCount() +
|
|
|
|
Cnt.getAdjustedCount() + BC.ContinueCount);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getCond()] = PGO.getCurrentRegionCount();
|
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());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDoStmt(const DoStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the body of the loop.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getBody()] = PGO.getCurrentRegionCount();
|
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->getBody());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
|
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
|
|
|
// The count at the start of the condition is equal to the count at the
|
|
|
|
// end of the body. The adjusted count does not include either the
|
|
|
|
// fall-through count coming into the loop or the continue count, so add
|
|
|
|
// both of those separately. This is coincidentally the same equation as
|
|
|
|
// with while loops but for different reasons.
|
|
|
|
Cnt.setCurrentRegionCount(Cnt.getParentCount() +
|
|
|
|
Cnt.getAdjustedCount() + BC.ContinueCount);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getCond()] = PGO.getCurrentRegionCount();
|
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());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitForStmt(const ForStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the body of the loop.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
// Visit the body region first. (This is basically the same as a while
|
|
|
|
// loop; see further comments in VisitWhileStmt.)
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getBody()] = PGO.getCurrentRegionCount();
|
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->getBody());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
|
|
|
|
// The increment is essentially part of the body but it needs to include
|
|
|
|
// the count for all the continue statements.
|
|
|
|
if (S->getInc()) {
|
|
|
|
Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
|
|
|
|
BreakContinueStack.back().ContinueCount);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getInc()] = PGO.getCurrentRegionCount();
|
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());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
}
|
|
|
|
|
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
|
|
|
|
|
|
|
// ...then go back and propagate counts through the condition.
|
|
|
|
if (S->getCond()) {
|
|
|
|
Cnt.setCurrentRegionCount(Cnt.getParentCount() +
|
|
|
|
Cnt.getAdjustedCount() +
|
|
|
|
BC.ContinueCount);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getCond()] = PGO.getCurrentRegionCount();
|
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());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
}
|
|
|
|
Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
Visit(S->getRangeStmt());
|
|
|
|
Visit(S->getBeginEndStmt());
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the body of the loop.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
// Visit the body region first. (This is basically the same as a while
|
|
|
|
// loop; see further comments in VisitWhileStmt.)
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount();
|
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->getLoopVarStmt());
|
|
|
|
Visit(S->getBody());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
|
|
|
|
// The increment is essentially part of the body but it needs to include
|
|
|
|
// the count for all the continue statements.
|
|
|
|
Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
|
|
|
|
BreakContinueStack.back().ContinueCount);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getInc()] = PGO.getCurrentRegionCount();
|
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());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
|
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
|
|
|
|
|
|
|
// ...then go back and propagate counts through the condition.
|
|
|
|
Cnt.setCurrentRegionCount(Cnt.getParentCount() +
|
|
|
|
Cnt.getAdjustedCount() +
|
|
|
|
BC.ContinueCount);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getCond()] = PGO.getCurrentRegionCount();
|
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());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
Visit(S->getElement());
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the body of the loop.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getBody()] = PGO.getCurrentRegionCount();
|
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->getBody());
|
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitSwitchStmt(const SwitchStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
Visit(S->getCond());
|
|
|
|
PGO.setCurrentRegionUnreachable();
|
|
|
|
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;
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the exit block of the switch.
|
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
|
|
|
RegionCounter ExitCnt(PGO, S);
|
|
|
|
ExitCnt.beginRegion();
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCaseStmt(const CaseStmt *S) {
|
|
|
|
RecordNextStmtCount = false;
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter for this particular case. This counts only jumps from the
|
|
|
|
// switch header and does not include fallthrough from the case before
|
|
|
|
// this one.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S] = Cnt.getCount();
|
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
|
|
|
RecordNextStmtCount = true;
|
|
|
|
Visit(S->getSubStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDefaultStmt(const DefaultStmt *S) {
|
|
|
|
RecordNextStmtCount = false;
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter for this default case. This does not include fallthrough from
|
|
|
|
// the previous case.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S] = Cnt.getCount();
|
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
|
|
|
RecordNextStmtCount = true;
|
|
|
|
Visit(S->getSubStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitIfStmt(const IfStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
2014-04-12 01:16:13 +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.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
Visit(S->getCond());
|
|
|
|
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getThen()] = PGO.getCurrentRegionCount();
|
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->getThen());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
|
|
|
|
if (S->getElse()) {
|
|
|
|
Cnt.beginElseRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S->getElse()] = PGO.getCurrentRegionCount();
|
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->getElse());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
}
|
|
|
|
Cnt.applyAdjustmentsToRegion(0);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXTryStmt(const CXXTryStmt *S) {
|
|
|
|
RecordStmtCount(S);
|
|
|
|
Visit(S->getTryBlock());
|
|
|
|
for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
|
|
|
|
Visit(S->getHandler(I));
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the continuation block of the try statement.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
Cnt.beginRegion();
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXCatchStmt(const CXXCatchStmt *S) {
|
|
|
|
RecordNextStmtCount = false;
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the catch statement's handler block.
|
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
|
|
|
RegionCounter Cnt(PGO, S);
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[S] = PGO.getCurrentRegionCount();
|
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->getHandlerBlock());
|
|
|
|
}
|
|
|
|
|
2014-04-11 14:10:10 +08:00
|
|
|
void VisitAbstractConditionalOperator(
|
|
|
|
const AbstractConditionalOperator *E) {
|
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
|
|
|
RecordStmtCount(E);
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the "true" part of a conditional operator. The
|
|
|
|
// count in the "false" part will be calculated from this counter.
|
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
|
|
|
RegionCounter Cnt(PGO, E);
|
|
|
|
Visit(E->getCond());
|
|
|
|
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount();
|
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(E->getTrueExpr());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
|
|
|
|
Cnt.beginElseRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount();
|
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(E->getFalseExpr());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
|
|
|
|
Cnt.applyAdjustmentsToRegion(0);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitBinLAnd(const BinaryOperator *E) {
|
|
|
|
RecordStmtCount(E);
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the right hand side of a logical and operator.
|
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
|
|
|
RegionCounter Cnt(PGO, E);
|
|
|
|
Visit(E->getLHS());
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
|
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(E->getRHS());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
Cnt.applyAdjustmentsToRegion(0);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitBinLOr(const BinaryOperator *E) {
|
|
|
|
RecordStmtCount(E);
|
2014-04-12 01:16:13 +08:00
|
|
|
// Counter tracks the right hand side of a logical or operator.
|
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
|
|
|
RegionCounter Cnt(PGO, E);
|
|
|
|
Visit(E->getLHS());
|
|
|
|
Cnt.beginRegion();
|
2014-03-27 03:26:02 +08:00
|
|
|
CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
|
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(E->getRHS());
|
|
|
|
Cnt.adjustForControlFlow();
|
|
|
|
Cnt.applyAdjustmentsToRegion(0);
|
|
|
|
RecordNextStmtCount = true;
|
|
|
|
}
|
|
|
|
};
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
2014-03-29 01:53:22 +08:00
|
|
|
static void emitRuntimeHook(CodeGenModule &CGM) {
|
2014-04-11 08:43:16 +08:00
|
|
|
const char *const RuntimeVarName = "__llvm_profile_runtime";
|
|
|
|
const char *const RuntimeUserName = "__llvm_profile_runtime_user";
|
2014-03-29 01:53:22 +08:00
|
|
|
if (CGM.getModule().getGlobalVariable(RuntimeVarName))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Declare the runtime hook.
|
|
|
|
llvm::LLVMContext &Ctx = CGM.getLLVMContext();
|
|
|
|
auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
|
|
|
|
auto *Var = new llvm::GlobalVariable(CGM.getModule(), Int32Ty, false,
|
|
|
|
llvm::GlobalValue::ExternalLinkage,
|
|
|
|
nullptr, RuntimeVarName);
|
|
|
|
|
|
|
|
// Make a function that uses it.
|
|
|
|
auto *User = llvm::Function::Create(llvm::FunctionType::get(Int32Ty, false),
|
|
|
|
llvm::GlobalValue::LinkOnceODRLinkage,
|
|
|
|
RuntimeUserName, &CGM.getModule());
|
|
|
|
User->addFnAttr(llvm::Attribute::NoInline);
|
|
|
|
if (CGM.getCodeGenOpts().DisableRedZone)
|
|
|
|
User->addFnAttr(llvm::Attribute::NoRedZone);
|
|
|
|
CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", User));
|
|
|
|
auto *Load = Builder.CreateLoad(Var);
|
|
|
|
Builder.CreateRet(Load);
|
|
|
|
|
|
|
|
// Create a use of the function. Now the definition of the runtime variable
|
|
|
|
// should get pulled in, along with any static initializears.
|
|
|
|
CGM.addUsedGlobal(User);
|
|
|
|
}
|
|
|
|
|
2014-03-06 12:55:41 +08:00
|
|
|
void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
|
2014-01-07 06:27:43 +08:00
|
|
|
bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
|
2014-03-13 05:06:31 +08:00
|
|
|
PGOProfileData *PGOData = CGM.getPGOData();
|
|
|
|
if (!InstrumentRegions && !PGOData)
|
2014-01-07 06:27:43 +08:00
|
|
|
return;
|
|
|
|
if (!D)
|
|
|
|
return;
|
2014-03-06 12:55:41 +08:00
|
|
|
setFuncName(Fn);
|
2014-03-21 06:50:08 +08:00
|
|
|
|
|
|
|
// Set the linkage for variables based on the function linkage. Usually, we
|
|
|
|
// want to match it, but available_externally and extern_weak both have the
|
|
|
|
// wrong semantics.
|
2014-03-21 06:49:50 +08:00
|
|
|
VarLinkage = Fn->getLinkage();
|
2014-03-21 06:50:08 +08:00
|
|
|
switch (VarLinkage) {
|
|
|
|
case llvm::GlobalValue::ExternalWeakLinkage:
|
|
|
|
VarLinkage = llvm::GlobalValue::LinkOnceAnyLinkage;
|
|
|
|
break;
|
|
|
|
case llvm::GlobalValue::AvailableExternallyLinkage:
|
|
|
|
VarLinkage = llvm::GlobalValue::LinkOnceODRLinkage;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
mapRegionCounters(D);
|
2014-03-29 01:53:22 +08:00
|
|
|
if (InstrumentRegions) {
|
|
|
|
emitRuntimeHook(CGM);
|
2014-01-07 06:27:43 +08:00
|
|
|
emitCounterVariables();
|
2014-03-29 01:53:22 +08:00
|
|
|
}
|
2014-03-13 05:06:31 +08:00
|
|
|
if (PGOData) {
|
|
|
|
loadRegionCounts(PGOData);
|
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-03-13 05:06:31 +08:00
|
|
|
applyFunctionAttributes(PGOData, 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) {
|
2014-03-27 03:26:05 +08:00
|
|
|
RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
|
2014-03-27 03:26:02 +08:00
|
|
|
MapRegionCounters Walker(*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-01-07 06:27:43 +08:00
|
|
|
NumRegionCounters = Walker.NextCounter;
|
2014-03-19 05:58:06 +08:00
|
|
|
// FIXME: The number of counters isn't sufficient for the hash
|
|
|
|
FunctionHash = NumRegionCounters;
|
2014-01-07 06:27:43 +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);
|
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-03-13 05:06:31 +08:00
|
|
|
void CodeGenPGO::applyFunctionAttributes(PGOProfileData *PGOData,
|
2014-03-13 02:14:32 +08:00
|
|
|
llvm::Function *Fn) {
|
|
|
|
if (!haveRegionCounts())
|
|
|
|
return;
|
|
|
|
|
2014-03-13 05:06:31 +08:00
|
|
|
uint64_t MaxFunctionCount = PGOData->getMaximumFunctionCount();
|
2014-03-13 02:14:32 +08:00
|
|
|
uint64_t FunctionCount = getRegionCount(0);
|
|
|
|
if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount))
|
|
|
|
// Turn on InlineHint attribute for hot functions.
|
|
|
|
// FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal.
|
|
|
|
Fn->addFnAttr(llvm::Attribute::InlineHint);
|
|
|
|
else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount))
|
|
|
|
// Turn on Cold attribute for cold functions.
|
|
|
|
// FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal.
|
|
|
|
Fn->addFnAttr(llvm::Attribute::Cold);
|
|
|
|
}
|
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
void CodeGenPGO::emitCounterVariables() {
|
|
|
|
llvm::LLVMContext &Ctx = CGM.getLLVMContext();
|
|
|
|
llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
|
|
|
|
NumRegionCounters);
|
|
|
|
RegionCounters =
|
2014-03-21 06:49:50 +08:00
|
|
|
new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, VarLinkage,
|
2014-01-07 06:27:43 +08:00
|
|
|
llvm::Constant::getNullValue(CounterTy),
|
2014-03-18 05:18:30 +08:00
|
|
|
getFuncVarName("counters"));
|
|
|
|
RegionCounters->setAlignment(8);
|
|
|
|
RegionCounters->setSection(getCountersSection(CGM));
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
|
2014-03-06 12:55:28 +08:00
|
|
|
if (!RegionCounters)
|
2014-01-07 06:27:43 +08:00
|
|
|
return;
|
|
|
|
llvm::Value *Addr =
|
|
|
|
Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter);
|
|
|
|
llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount");
|
|
|
|
Count = Builder.CreateAdd(Count, Builder.getInt64(1));
|
|
|
|
Builder.CreateStore(Count, Addr);
|
|
|
|
}
|
|
|
|
|
2014-03-13 05:06:31 +08:00
|
|
|
void CodeGenPGO::loadRegionCounts(PGOProfileData *PGOData) {
|
2014-01-07 06:27:43 +08:00
|
|
|
// For now, ignore the counts from the PGO data file only if the number of
|
|
|
|
// counters does not match. This could be tightened down in the future to
|
|
|
|
// ignore counts when the input changes in various ways, e.g., by comparing a
|
|
|
|
// hash value based on some characteristics of the input.
|
2014-03-27 03:26:05 +08:00
|
|
|
RegionCounts.reset(new std::vector<uint64_t>);
|
2014-03-19 05:58:06 +08:00
|
|
|
uint64_t Hash;
|
|
|
|
if (PGOData->getFunctionCounts(getFuncName(), Hash, *RegionCounts) ||
|
2014-03-27 03:26:05 +08:00
|
|
|
Hash != FunctionHash || RegionCounts->size() != NumRegionCounters)
|
|
|
|
RegionCounts.reset();
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenPGO::destroyRegionCounters() {
|
2014-03-27 03:26:05 +08:00
|
|
|
RegionCounterMap.reset();
|
|
|
|
StmtCountMap.reset();
|
|
|
|
RegionCounts.reset();
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 02:18:10 +08:00
|
|
|
/// \brief Calculate what to divide by to scale weights.
|
|
|
|
///
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Scale an individual branch weight (and add 1).
|
|
|
|
///
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
llvm::MDNode *CodeGenPGO::createBranchWeights(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
|
|
|
}
|
|
|
|
|
2014-02-18 03:20:59 +08:00
|
|
|
llvm::MDNode *CodeGenPGO::createBranchWeights(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
|
|
|
|
|
|
|
llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
|
|
|
|
RegionCounter &Cnt) {
|
|
|
|
if (!haveRegionCounts())
|
2014-03-21 02:40:55 +08:00
|
|
|
return nullptr;
|
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
|
|
|
uint64_t LoopCount = Cnt.getCount();
|
|
|
|
uint64_t CondCount = 0;
|
|
|
|
bool Found = getStmtCount(Cond, CondCount);
|
|
|
|
assert(Found && "missing expected loop condition count");
|
|
|
|
(void)Found;
|
|
|
|
if (CondCount == 0)
|
2014-03-21 02:40:55 +08:00
|
|
|
return nullptr;
|
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
|
|
|
return createBranchWeights(LoopCount,
|
|
|
|
std::max(CondCount, LoopCount) - LoopCount);
|
|
|
|
}
|