2014-12-08 19:02:35 +01:00
|
|
|
//===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-11-18 19:14:55 +01:00
|
|
|
// This pass lowers instrprof_* intrinsics emitted by a frontend for profiling.
|
|
|
|
// It also builds the data structures and initialization code needed for
|
|
|
|
// updating execution counts and emitting the profile at runtime.
|
2014-12-08 19:02:35 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-06-21 04:39:08 +02:00
|
|
|
#include "llvm/Transforms/InstrProfiling.h"
|
2014-12-08 19:02:35 +01:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2016-11-21 12:57:19 +01:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2014-12-08 19:02:35 +01:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2015-11-18 19:14:55 +01:00
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
2014-12-08 19:02:35 +01:00
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "instrprof"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-02-08 19:13:49 +01:00
|
|
|
cl::opt<bool> DoNameCompression("enable-name-compression",
|
|
|
|
cl::desc("Enable name string compression"),
|
|
|
|
cl::init(true));
|
|
|
|
|
2017-01-11 21:19:41 +01:00
|
|
|
cl::opt<bool> DoHashBasedCounterSplit(
|
|
|
|
"hash-based-counter-split",
|
|
|
|
cl::desc("Rename counter variable of a comdat function based on cfg hash"),
|
|
|
|
cl::init(true));
|
|
|
|
|
2016-05-22 00:55:34 +02:00
|
|
|
cl::opt<bool> ValueProfileStaticAlloc(
|
|
|
|
"vp-static-alloc",
|
|
|
|
cl::desc("Do static counter allocation for value profiler"),
|
|
|
|
cl::init(true));
|
|
|
|
cl::opt<double> NumCountersPerValueSite(
|
|
|
|
"vp-counters-per-site",
|
|
|
|
cl::desc("The average number of profile counters allocated "
|
|
|
|
"per value profiling site."),
|
|
|
|
// This is set to a very small value because in real programs, only
|
|
|
|
// a very small percentage of value sites have non-zero targets, e.g, 1/30.
|
|
|
|
// For those sites with non-zero profile, the average number of targets
|
|
|
|
// is usually smaller than 2.
|
|
|
|
cl::init(1.0));
|
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
class InstrProfilingLegacyPass : public ModulePass {
|
|
|
|
InstrProfiling InstrProf;
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
public:
|
|
|
|
static char ID;
|
2016-04-18 19:47:38 +02:00
|
|
|
InstrProfilingLegacyPass() : ModulePass(ID), InstrProf() {}
|
|
|
|
InstrProfilingLegacyPass(const InstrProfOptions &Options)
|
|
|
|
: ModulePass(ID), InstrProf(Options) {}
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override {
|
2014-12-08 19:02:35 +01:00
|
|
|
return "Frontend instrumentation-based coverage lowering";
|
|
|
|
}
|
|
|
|
|
2016-11-21 12:57:19 +01:00
|
|
|
bool runOnModule(Module &M) override {
|
|
|
|
return InstrProf.run(M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
|
|
|
|
}
|
2014-12-08 19:02:35 +01:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
2016-11-21 12:57:19 +01:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2014-12-08 19:02:35 +01:00
|
|
|
}
|
2016-04-18 19:47:38 +02:00
|
|
|
};
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
} // anonymous namespace
|
2015-02-11 03:52:44 +01:00
|
|
|
|
2016-08-09 02:28:38 +02:00
|
|
|
PreservedAnalyses InstrProfiling::run(Module &M, ModuleAnalysisManager &AM) {
|
2016-11-21 12:57:19 +01:00
|
|
|
auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
|
|
|
|
if (!run(M, TLI))
|
2016-04-18 19:47:38 +02:00
|
|
|
return PreservedAnalyses::all();
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|
2016-02-08 19:13:49 +01:00
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
char InstrProfilingLegacyPass::ID = 0;
|
2016-11-21 12:57:19 +01:00
|
|
|
INITIALIZE_PASS_BEGIN(
|
|
|
|
InstrProfilingLegacyPass, "instrprof",
|
|
|
|
"Frontend instrumentation-based coverage lowering.", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(
|
|
|
|
InstrProfilingLegacyPass, "instrprof",
|
|
|
|
"Frontend instrumentation-based coverage lowering.", false, false)
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2016-06-21 04:39:08 +02:00
|
|
|
ModulePass *
|
|
|
|
llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options) {
|
2016-04-18 19:47:38 +02:00
|
|
|
return new InstrProfilingLegacyPass(Options);
|
|
|
|
}
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
bool InstrProfiling::isMachO() const {
|
|
|
|
return Triple(M->getTargetTriple()).isOSBinFormatMachO();
|
|
|
|
}
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
/// Get the section name for the counter variables.
|
|
|
|
StringRef InstrProfiling::getCountersSection() const {
|
|
|
|
return getInstrProfCountersSectionName(isMachO());
|
|
|
|
}
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
/// Get the section name for the name variables.
|
|
|
|
StringRef InstrProfiling::getNameSection() const {
|
|
|
|
return getInstrProfNameSectionName(isMachO());
|
|
|
|
}
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
/// Get the section name for the profile data variables.
|
|
|
|
StringRef InstrProfiling::getDataSection() const {
|
|
|
|
return getInstrProfDataSectionName(isMachO());
|
|
|
|
}
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2016-04-18 19:47:38 +02:00
|
|
|
/// Get the section name for the coverage mapping data.
|
|
|
|
StringRef InstrProfiling::getCoverageSection() const {
|
|
|
|
return getInstrProfCoverageSectionName(isMachO());
|
2014-12-08 19:02:35 +01:00
|
|
|
}
|
|
|
|
|
2016-09-18 20:34:07 +02:00
|
|
|
static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) {
|
|
|
|
InstrProfIncrementInst *Inc = dyn_cast<InstrProfIncrementInstStep>(Instr);
|
|
|
|
if (Inc)
|
|
|
|
return Inc;
|
|
|
|
return dyn_cast<InstrProfIncrementInst>(Instr);
|
|
|
|
}
|
|
|
|
|
2016-11-21 12:57:19 +01:00
|
|
|
bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) {
|
2014-12-08 19:02:35 +01:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
this->M = &M;
|
2016-11-21 12:57:19 +01:00
|
|
|
this->TLI = &TLI;
|
2016-02-08 19:13:49 +01:00
|
|
|
NamesVar = nullptr;
|
|
|
|
NamesSize = 0;
|
2015-11-18 19:14:55 +01:00
|
|
|
ProfileDataMap.clear();
|
2014-12-08 19:02:35 +01:00
|
|
|
UsedVars.clear();
|
|
|
|
|
2015-11-18 19:14:55 +01:00
|
|
|
// We did not know how many value sites there would be inside
|
|
|
|
// the instrumented function. This is counting the number of instrumented
|
|
|
|
// target value sites to enter it as field in the profile data variable.
|
2016-01-19 19:29:54 +01:00
|
|
|
for (Function &F : M) {
|
|
|
|
InstrProfIncrementInst *FirstProfIncInst = nullptr;
|
2014-12-08 19:02:35 +01:00
|
|
|
for (BasicBlock &BB : F)
|
2016-01-19 19:29:54 +01:00
|
|
|
for (auto I = BB.begin(), E = BB.end(); I != E; I++)
|
|
|
|
if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I))
|
2015-11-18 19:14:55 +01:00
|
|
|
computeNumValueSiteCounts(Ind);
|
2016-01-19 19:29:54 +01:00
|
|
|
else if (FirstProfIncInst == nullptr)
|
|
|
|
FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I);
|
|
|
|
|
|
|
|
// Value profiling intrinsic lowering requires per-function profile data
|
|
|
|
// variable to be created first.
|
|
|
|
if (FirstProfIncInst != nullptr)
|
|
|
|
static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst));
|
|
|
|
}
|
2015-11-18 19:14:55 +01:00
|
|
|
|
|
|
|
for (Function &F : M)
|
|
|
|
for (BasicBlock &BB : F)
|
|
|
|
for (auto I = BB.begin(), E = BB.end(); I != E;) {
|
|
|
|
auto Instr = I++;
|
2016-09-18 20:34:07 +02:00
|
|
|
InstrProfIncrementInst *Inc = castToIncrementInst(&*Instr);
|
|
|
|
if (Inc) {
|
2014-12-08 19:02:35 +01:00
|
|
|
lowerIncrement(Inc);
|
|
|
|
MadeChange = true;
|
2015-11-18 19:14:55 +01:00
|
|
|
} else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) {
|
|
|
|
lowerValueProfileInst(Ind);
|
|
|
|
MadeChange = true;
|
2014-12-08 19:02:35 +01:00
|
|
|
}
|
2015-11-18 19:14:55 +01:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:05:49 +01:00
|
|
|
if (GlobalVariable *CoverageNamesVar =
|
2016-01-20 01:24:36 +01:00
|
|
|
M.getNamedGlobal(getCoverageUnusedNamesVarName())) {
|
2016-01-07 21:05:49 +01:00
|
|
|
lowerCoverageData(CoverageNamesVar);
|
2015-02-11 03:52:44 +01:00
|
|
|
MadeChange = true;
|
|
|
|
}
|
2015-11-18 19:14:55 +01:00
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
if (!MadeChange)
|
|
|
|
return false;
|
|
|
|
|
2016-05-22 00:55:34 +02:00
|
|
|
emitVNodes();
|
2016-02-08 19:13:49 +01:00
|
|
|
emitNameData();
|
2014-12-08 19:02:35 +01:00
|
|
|
emitRegistration();
|
|
|
|
emitRuntimeHook();
|
|
|
|
emitUses();
|
|
|
|
emitInitialization();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-21 12:57:19 +01:00
|
|
|
static Constant *getOrInsertValueProfilingCall(Module &M,
|
|
|
|
const TargetLibraryInfo &TLI) {
|
2015-11-22 01:22:07 +01:00
|
|
|
LLVMContext &Ctx = M.getContext();
|
|
|
|
auto *ReturnTy = Type::getVoidTy(M.getContext());
|
|
|
|
Type *ParamTypes[] = {
|
|
|
|
#define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType
|
|
|
|
#include "llvm/ProfileData/InstrProfData.inc"
|
|
|
|
};
|
2015-11-18 19:14:55 +01:00
|
|
|
auto *ValueProfilingCallTy =
|
2015-11-22 01:22:07 +01:00
|
|
|
FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false);
|
2016-11-21 12:57:19 +01:00
|
|
|
Constant *Res = M.getOrInsertFunction(getInstrProfValueProfFuncName(),
|
|
|
|
ValueProfilingCallTy);
|
|
|
|
if (Function *FunRes = dyn_cast<Function>(Res)) {
|
|
|
|
if (auto AK = TLI.getExtAttrForI32Param(false))
|
|
|
|
FunRes->addAttribute(3, AK);
|
|
|
|
}
|
|
|
|
return Res;
|
2015-11-18 19:14:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) {
|
|
|
|
|
|
|
|
GlobalVariable *Name = Ind->getName();
|
|
|
|
uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
|
|
|
|
uint64_t Index = Ind->getIndex()->getZExtValue();
|
|
|
|
auto It = ProfileDataMap.find(Name);
|
|
|
|
if (It == ProfileDataMap.end()) {
|
|
|
|
PerFunctionProfileData PD;
|
|
|
|
PD.NumValueSites[ValueKind] = Index + 1;
|
|
|
|
ProfileDataMap[Name] = PD;
|
|
|
|
} else if (It->second.NumValueSites[ValueKind] <= Index)
|
|
|
|
It->second.NumValueSites[ValueKind] = Index + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) {
|
|
|
|
|
|
|
|
GlobalVariable *Name = Ind->getName();
|
|
|
|
auto It = ProfileDataMap.find(Name);
|
|
|
|
assert(It != ProfileDataMap.end() && It->second.DataVar &&
|
2016-06-21 04:39:08 +02:00
|
|
|
"value profiling detected in function with no counter incerement");
|
2015-11-18 19:14:55 +01:00
|
|
|
|
|
|
|
GlobalVariable *DataVar = It->second.DataVar;
|
|
|
|
uint64_t ValueKind = Ind->getValueKind()->getZExtValue();
|
|
|
|
uint64_t Index = Ind->getIndex()->getZExtValue();
|
|
|
|
for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind)
|
|
|
|
Index += It->second.NumValueSites[Kind];
|
|
|
|
|
|
|
|
IRBuilder<> Builder(Ind);
|
2016-06-21 04:39:08 +02:00
|
|
|
Value *Args[3] = {Ind->getTargetValue(),
|
|
|
|
Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()),
|
|
|
|
Builder.getInt32(Index)};
|
2016-11-21 12:57:19 +01:00
|
|
|
CallInst *Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI),
|
|
|
|
Args);
|
|
|
|
if (auto AK = TLI->getExtAttrForI32Param(false))
|
|
|
|
Call->addAttribute(3, AK);
|
|
|
|
Ind->replaceAllUsesWith(Call);
|
2015-11-18 19:14:55 +01:00
|
|
|
Ind->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
|
|
|
|
GlobalVariable *Counters = getOrCreateRegionCounters(Inc);
|
|
|
|
|
2015-10-13 19:39:10 +02:00
|
|
|
IRBuilder<> Builder(Inc);
|
2014-12-08 19:02:35 +01:00
|
|
|
uint64_t Index = Inc->getIndex()->getZExtValue();
|
2015-06-04 13:45:32 +02:00
|
|
|
Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index);
|
|
|
|
Value *Count = Builder.CreateLoad(Addr, "pgocount");
|
2016-09-20 21:07:22 +02:00
|
|
|
Count = Builder.CreateAdd(Count, Inc->getStep());
|
2014-12-08 19:02:35 +01:00
|
|
|
Inc->replaceAllUsesWith(Builder.CreateStore(Count, Addr));
|
|
|
|
Inc->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:05:49 +01:00
|
|
|
void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
|
2015-02-11 03:52:44 +01:00
|
|
|
|
2016-01-07 21:05:49 +01:00
|
|
|
ConstantArray *Names =
|
|
|
|
cast<ConstantArray>(CoverageNamesVar->getInitializer());
|
|
|
|
for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
|
|
|
|
Constant *NC = Names->getOperand(I);
|
|
|
|
Value *V = NC->stripPointerCasts();
|
2015-02-11 03:52:44 +01:00
|
|
|
assert(isa<GlobalVariable>(V) && "Missing reference to function name");
|
|
|
|
GlobalVariable *Name = cast<GlobalVariable>(V);
|
|
|
|
|
2016-02-08 19:13:49 +01:00
|
|
|
Name->setLinkage(GlobalValue::PrivateLinkage);
|
|
|
|
ReferencedNames.push_back(Name);
|
2015-02-11 03:52:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
/// Get the name of a profiling variable for a particular function.
|
2015-10-22 22:32:12 +02:00
|
|
|
static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) {
|
2015-12-12 18:28:03 +01:00
|
|
|
StringRef NamePrefix = getInstrProfNameVarPrefix();
|
|
|
|
StringRef Name = Inc->getName()->getName().substr(NamePrefix.size());
|
2017-01-11 21:19:41 +01:00
|
|
|
Function *F = Inc->getParent()->getParent();
|
|
|
|
Module *M = F->getParent();
|
|
|
|
if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) ||
|
|
|
|
!canRenameComdatFunc(*F))
|
|
|
|
return (Prefix + Name).str();
|
|
|
|
uint64_t FuncHash = Inc->getHash()->getZExtValue();
|
|
|
|
SmallVector<char, 24> HashPostfix;
|
|
|
|
if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix)))
|
|
|
|
return (Prefix + Name).str();
|
|
|
|
return (Prefix + Name + "." + Twine(FuncHash)).str();
|
2014-12-08 19:02:35 +01:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:14:55 +01:00
|
|
|
static inline bool shouldRecordFunctionAddr(Function *F) {
|
|
|
|
// Check the linkage
|
|
|
|
if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
|
|
|
|
!F->hasAvailableExternallyLinkage())
|
|
|
|
return true;
|
2016-04-27 23:17:30 +02:00
|
|
|
// Prohibit function address recording if the function is both internal and
|
|
|
|
// COMDAT. This avoids the profile data variable referencing internal symbols
|
|
|
|
// in COMDAT.
|
|
|
|
if (F->hasLocalLinkage() && F->hasComdat())
|
|
|
|
return false;
|
2015-11-18 19:14:55 +01:00
|
|
|
// Check uses of this function for other than direct calls or invokes to it.
|
2016-06-02 18:33:41 +02:00
|
|
|
// Inline virtual functions have linkeOnceODR linkage. When a key method
|
|
|
|
// exists, the vtable will only be emitted in the TU where the key method
|
|
|
|
// is defined. In a TU where vtable is not available, the function won't
|
2016-06-04 01:02:28 +02:00
|
|
|
// be 'addresstaken'. If its address is not recorded here, the profile data
|
2016-06-21 04:39:08 +02:00
|
|
|
// with missing address may be picked by the linker leading to missing
|
2016-06-04 01:02:28 +02:00
|
|
|
// indirect call target info.
|
|
|
|
return F->hasAddressTaken() || F->hasLinkOnceLinkage();
|
2015-11-18 19:14:55 +01:00
|
|
|
}
|
|
|
|
|
2016-02-28 00:11:30 +01:00
|
|
|
static inline Comdat *getOrCreateProfileComdat(Module &M, Function &F,
|
2015-12-21 22:52:27 +01:00
|
|
|
InstrProfIncrementInst *Inc) {
|
2016-02-28 00:11:30 +01:00
|
|
|
if (!needsComdatForCounter(F, M))
|
|
|
|
return nullptr;
|
|
|
|
|
2015-12-21 22:52:27 +01:00
|
|
|
// COFF format requires a COMDAT section to have a key symbol with the same
|
2016-02-04 00:22:43 +01:00
|
|
|
// name. The linker targeting COFF also requires that the COMDAT
|
2015-12-22 01:11:15 +01:00
|
|
|
// a section is associated to must precede the associating section. For this
|
2016-02-08 19:13:49 +01:00
|
|
|
// reason, we must choose the counter var's name as the name of the comdat.
|
2015-12-21 22:52:27 +01:00
|
|
|
StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF()
|
2016-02-08 19:13:49 +01:00
|
|
|
? getInstrProfCountersVarPrefix()
|
2015-12-21 22:52:27 +01:00
|
|
|
: getInstrProfComdatPrefix());
|
|
|
|
return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix)));
|
|
|
|
}
|
|
|
|
|
2016-05-22 00:55:34 +02:00
|
|
|
static bool needsRuntimeRegistrationOfSectionRange(const Module &M) {
|
|
|
|
// Don't do this for Darwin. compiler-rt uses linker magic.
|
|
|
|
if (Triple(M.getTargetTriple()).isOSDarwin())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Use linker script magic to get data/cnts/name start/end.
|
|
|
|
if (Triple(M.getTargetTriple()).isOSLinux() ||
|
|
|
|
Triple(M.getTargetTriple()).isOSFreeBSD() ||
|
|
|
|
Triple(M.getTargetTriple()).isPS4CPU())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
GlobalVariable *
|
|
|
|
InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
|
2015-11-05 01:47:26 +01:00
|
|
|
GlobalVariable *NamePtr = Inc->getName();
|
2015-11-18 19:14:55 +01:00
|
|
|
auto It = ProfileDataMap.find(NamePtr);
|
|
|
|
PerFunctionProfileData PD;
|
|
|
|
if (It != ProfileDataMap.end()) {
|
|
|
|
if (It->second.RegionCounters)
|
|
|
|
return It->second.RegionCounters;
|
|
|
|
PD = It->second;
|
|
|
|
}
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2015-09-24 00:40:45 +02:00
|
|
|
// Move the name variable to the right section. Place them in a COMDAT group
|
|
|
|
// if the associated function is a COMDAT. This will make sure that
|
|
|
|
// only one copy of counters of the COMDAT function will be emitted after
|
|
|
|
// linking.
|
2015-05-27 21:34:01 +02:00
|
|
|
Function *Fn = Inc->getParent()->getParent();
|
2015-09-24 00:40:45 +02:00
|
|
|
Comdat *ProfileVarsComdat = nullptr;
|
2016-02-28 00:11:30 +01:00
|
|
|
ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc);
|
2014-12-08 19:02:35 +01:00
|
|
|
|
|
|
|
uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
|
|
|
|
LLVMContext &Ctx = M->getContext();
|
|
|
|
ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
|
|
|
|
|
|
|
|
// Create the counters variable.
|
2015-11-05 01:47:26 +01:00
|
|
|
auto *CounterPtr =
|
|
|
|
new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(),
|
2015-10-22 22:32:12 +02:00
|
|
|
Constant::getNullValue(CounterTy),
|
|
|
|
getVarName(Inc, getInstrProfCountersVarPrefix()));
|
2015-11-05 01:47:26 +01:00
|
|
|
CounterPtr->setVisibility(NamePtr->getVisibility());
|
|
|
|
CounterPtr->setSection(getCountersSection());
|
|
|
|
CounterPtr->setAlignment(8);
|
|
|
|
CounterPtr->setComdat(ProfileVarsComdat);
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2015-11-18 19:14:55 +01:00
|
|
|
auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
|
2016-05-22 00:55:34 +02:00
|
|
|
// Allocate statically the array of pointers to value profile nodes for
|
|
|
|
// the current function.
|
|
|
|
Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy);
|
|
|
|
if (ValueProfileStaticAlloc && !needsRuntimeRegistrationOfSectionRange(*M)) {
|
|
|
|
|
|
|
|
uint64_t NS = 0;
|
|
|
|
for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
|
|
|
|
NS += PD.NumValueSites[Kind];
|
|
|
|
if (NS) {
|
|
|
|
ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS);
|
|
|
|
|
|
|
|
auto *ValuesVar =
|
|
|
|
new GlobalVariable(*M, ValuesTy, false, NamePtr->getLinkage(),
|
|
|
|
Constant::getNullValue(ValuesTy),
|
|
|
|
getVarName(Inc, getInstrProfValuesVarPrefix()));
|
|
|
|
ValuesVar->setVisibility(NamePtr->getVisibility());
|
|
|
|
ValuesVar->setSection(getInstrProfValuesSectionName(isMachO()));
|
|
|
|
ValuesVar->setAlignment(8);
|
|
|
|
ValuesVar->setComdat(ProfileVarsComdat);
|
|
|
|
ValuesPtrExpr =
|
|
|
|
ConstantExpr::getBitCast(ValuesVar, llvm::Type::getInt8PtrTy(Ctx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create data variable.
|
2015-11-18 19:14:55 +01:00
|
|
|
auto *Int16Ty = Type::getInt16Ty(Ctx);
|
2016-05-22 00:55:34 +02:00
|
|
|
auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1);
|
2015-11-05 01:47:26 +01:00
|
|
|
Type *DataTypes[] = {
|
2016-06-21 04:39:08 +02:00
|
|
|
#define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
|
|
|
|
#include "llvm/ProfileData/InstrProfData.inc"
|
2015-11-05 01:47:26 +01:00
|
|
|
};
|
2014-12-08 19:02:35 +01:00
|
|
|
auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
|
2015-11-05 01:47:26 +01:00
|
|
|
|
2016-06-21 04:39:08 +02:00
|
|
|
Constant *FunctionAddr = shouldRecordFunctionAddr(Fn)
|
|
|
|
? ConstantExpr::getBitCast(Fn, Int8PtrTy)
|
|
|
|
: ConstantPointerNull::get(Int8PtrTy);
|
2015-11-18 19:14:55 +01:00
|
|
|
|
2016-06-21 04:39:08 +02:00
|
|
|
Constant *Int16ArrayVals[IPVK_Last + 1];
|
2015-11-18 19:14:55 +01:00
|
|
|
for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
|
|
|
|
Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]);
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
Constant *DataVals[] = {
|
2016-06-21 04:39:08 +02:00
|
|
|
#define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
|
|
|
|
#include "llvm/ProfileData/InstrProfData.inc"
|
2015-11-05 01:47:26 +01:00
|
|
|
};
|
2015-11-18 19:14:55 +01:00
|
|
|
auto *Data = new GlobalVariable(*M, DataTy, false, NamePtr->getLinkage(),
|
2014-12-08 19:02:35 +01:00
|
|
|
ConstantStruct::get(DataTy, DataVals),
|
2015-10-22 22:32:12 +02:00
|
|
|
getVarName(Inc, getInstrProfDataVarPrefix()));
|
2015-11-05 01:47:26 +01:00
|
|
|
Data->setVisibility(NamePtr->getVisibility());
|
2014-12-08 19:02:35 +01:00
|
|
|
Data->setSection(getDataSection());
|
2015-11-23 19:02:59 +01:00
|
|
|
Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT);
|
2015-09-24 00:40:45 +02:00
|
|
|
Data->setComdat(ProfileVarsComdat);
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2015-11-18 19:14:55 +01:00
|
|
|
PD.RegionCounters = CounterPtr;
|
|
|
|
PD.DataVar = Data;
|
|
|
|
ProfileDataMap[NamePtr] = PD;
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
// Mark the data variable as used so that it isn't stripped out.
|
|
|
|
UsedVars.push_back(Data);
|
2016-02-08 19:13:49 +01:00
|
|
|
// Now that the linkage set by the FE has been passed to the data and counter
|
|
|
|
// variables, reset Name variable's linkage and visibility to private so that
|
|
|
|
// it can be removed later by the compiler.
|
|
|
|
NamePtr->setLinkage(GlobalValue::PrivateLinkage);
|
|
|
|
// Collect the referenced names to be used by emitNameData.
|
|
|
|
ReferencedNames.push_back(NamePtr);
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2015-11-05 01:47:26 +01:00
|
|
|
return CounterPtr;
|
2014-12-08 19:02:35 +01:00
|
|
|
}
|
|
|
|
|
2016-05-22 00:55:34 +02:00
|
|
|
void InstrProfiling::emitVNodes() {
|
|
|
|
if (!ValueProfileStaticAlloc)
|
|
|
|
return;
|
2016-05-17 22:19:03 +02:00
|
|
|
|
2016-05-22 00:55:34 +02:00
|
|
|
// For now only support this on platforms that do
|
|
|
|
// not require runtime registration to discover
|
|
|
|
// named section start/end.
|
|
|
|
if (needsRuntimeRegistrationOfSectionRange(*M))
|
|
|
|
return;
|
2016-05-17 22:19:03 +02:00
|
|
|
|
2016-05-22 00:55:34 +02:00
|
|
|
size_t TotalNS = 0;
|
|
|
|
for (auto &PD : ProfileDataMap) {
|
|
|
|
for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
|
|
|
|
TotalNS += PD.second.NumValueSites[Kind];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TotalNS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint64_t NumCounters = TotalNS * NumCountersPerValueSite;
|
2016-06-21 04:39:08 +02:00
|
|
|
// Heuristic for small programs with very few total value sites.
|
|
|
|
// The default value of vp-counters-per-site is chosen based on
|
|
|
|
// the observation that large apps usually have a low percentage
|
|
|
|
// of value sites that actually have any profile data, and thus
|
|
|
|
// the average number of counters per site is low. For small
|
|
|
|
// apps with very few sites, this may not be true. Bump up the
|
|
|
|
// number of counters in this case.
|
2016-05-23 21:29:26 +02:00
|
|
|
#define INSTR_PROF_MIN_VAL_COUNTS 10
|
|
|
|
if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS)
|
2016-06-21 04:39:08 +02:00
|
|
|
NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS, (int)NumCounters * 2);
|
2016-05-22 00:55:34 +02:00
|
|
|
|
|
|
|
auto &Ctx = M->getContext();
|
|
|
|
Type *VNodeTypes[] = {
|
|
|
|
#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType,
|
|
|
|
#include "llvm/ProfileData/InstrProfData.inc"
|
|
|
|
};
|
|
|
|
auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes));
|
|
|
|
|
|
|
|
ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters);
|
|
|
|
auto *VNodesVar = new GlobalVariable(
|
|
|
|
*M, VNodesTy, false, llvm::GlobalValue::PrivateLinkage,
|
|
|
|
Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
|
|
|
|
VNodesVar->setSection(getInstrProfVNodesSectionName(isMachO()));
|
|
|
|
UsedVars.push_back(VNodesVar);
|
2016-05-17 22:19:03 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 19:13:49 +01:00
|
|
|
void InstrProfiling::emitNameData() {
|
|
|
|
std::string UncompressedData;
|
|
|
|
|
|
|
|
if (ReferencedNames.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string CompressedNameStr;
|
2016-05-19 05:54:45 +02:00
|
|
|
if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
|
2016-05-03 18:53:17 +02:00
|
|
|
DoNameCompression)) {
|
2016-05-19 05:54:45 +02:00
|
|
|
llvm::report_fatal_error(toString(std::move(E)), false);
|
2016-05-03 18:53:17 +02:00
|
|
|
}
|
2016-02-08 19:13:49 +01:00
|
|
|
|
|
|
|
auto &Ctx = M->getContext();
|
|
|
|
auto *NamesVal = llvm::ConstantDataArray::getString(
|
|
|
|
Ctx, StringRef(CompressedNameStr), false);
|
|
|
|
NamesVar = new llvm::GlobalVariable(*M, NamesVal->getType(), true,
|
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
|
|
|
NamesVal, getInstrProfNamesVarName());
|
|
|
|
NamesSize = CompressedNameStr.size();
|
|
|
|
NamesVar->setSection(getNameSection());
|
|
|
|
UsedVars.push_back(NamesVar);
|
|
|
|
}
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
void InstrProfiling::emitRegistration() {
|
2016-05-17 22:19:03 +02:00
|
|
|
if (!needsRuntimeRegistrationOfSectionRange(*M))
|
2015-10-19 06:17:10 +02:00
|
|
|
return;
|
2015-10-13 20:39:48 +02:00
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
// Construct the function.
|
|
|
|
auto *VoidTy = Type::getVoidTy(M->getContext());
|
|
|
|
auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext());
|
2016-02-08 19:13:49 +01:00
|
|
|
auto *Int64Ty = Type::getInt64Ty(M->getContext());
|
2014-12-08 19:02:35 +01:00
|
|
|
auto *RegisterFTy = FunctionType::get(VoidTy, false);
|
|
|
|
auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
|
2015-10-23 06:22:58 +02:00
|
|
|
getInstrProfRegFuncsName(), M);
|
2016-06-14 23:01:22 +02:00
|
|
|
RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
|
2016-06-21 04:39:08 +02:00
|
|
|
if (Options.NoRedZone)
|
|
|
|
RegisterF->addFnAttr(Attribute::NoRedZone);
|
2014-12-08 19:02:35 +01:00
|
|
|
|
2015-06-04 13:45:32 +02:00
|
|
|
auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
|
2014-12-08 19:02:35 +01:00
|
|
|
auto *RuntimeRegisterF =
|
|
|
|
Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage,
|
2015-10-23 06:22:58 +02:00
|
|
|
getInstrProfRegFuncName(), M);
|
2014-12-08 19:02:35 +01:00
|
|
|
|
|
|
|
IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF));
|
|
|
|
for (Value *Data : UsedVars)
|
2016-02-08 19:13:49 +01:00
|
|
|
if (Data != NamesVar)
|
|
|
|
IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy));
|
|
|
|
|
|
|
|
if (NamesVar) {
|
|
|
|
Type *ParamTypes[] = {VoidPtrTy, Int64Ty};
|
|
|
|
auto *NamesRegisterTy =
|
|
|
|
FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false);
|
|
|
|
auto *NamesRegisterF =
|
|
|
|
Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage,
|
|
|
|
getInstrProfNamesRegFuncName(), M);
|
|
|
|
IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy),
|
|
|
|
IRB.getInt64(NamesSize)});
|
|
|
|
}
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
IRB.CreateRetVoid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfiling::emitRuntimeHook() {
|
|
|
|
|
2015-10-29 05:08:31 +01:00
|
|
|
// We expect the linker to be invoked with -u<hook_var> flag for linux,
|
|
|
|
// for which case there is no need to emit the user function.
|
|
|
|
if (Triple(M->getTargetTriple()).isOSLinux())
|
|
|
|
return;
|
|
|
|
|
2014-12-08 19:02:35 +01:00
|
|
|
// If the module's provided its own runtime, we don't need to do anything.
|
2016-06-21 04:39:08 +02:00
|
|
|
if (M->getGlobalVariable(getInstrProfRuntimeHookVarName()))
|
|
|
|
return;
|
2014-12-08 19:02:35 +01:00
|
|
|
|
|
|
|
// Declare an external variable that will pull in the runtime initialization.
|
|
|
|
auto *Int32Ty = Type::getInt32Ty(M->getContext());
|
|
|
|
auto *Var =
|
|
|
|
new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
|
2015-10-23 06:22:58 +02:00
|
|
|
nullptr, getInstrProfRuntimeHookVarName());
|
2014-12-08 19:02:35 +01:00
|
|
|
|
|
|
|
// Make a function that uses it.
|
2015-10-23 06:22:58 +02:00
|
|
|
auto *User = Function::Create(FunctionType::get(Int32Ty, false),
|
|
|
|
GlobalValue::LinkOnceODRLinkage,
|
|
|
|
getInstrProfRuntimeHookVarUseFuncName(), M);
|
2014-12-08 19:02:35 +01:00
|
|
|
User->addFnAttr(Attribute::NoInline);
|
2016-06-21 04:39:08 +02:00
|
|
|
if (Options.NoRedZone)
|
|
|
|
User->addFnAttr(Attribute::NoRedZone);
|
2015-02-25 23:52:20 +01:00
|
|
|
User->setVisibility(GlobalValue::HiddenVisibility);
|
2016-05-25 19:17:51 +02:00
|
|
|
if (Triple(M->getTargetTriple()).supportsCOMDAT())
|
2016-05-24 20:47:38 +02:00
|
|
|
User->setComdat(M->getOrInsertComdat(User->getName()));
|
2014-12-08 19:02:35 +01:00
|
|
|
|
|
|
|
IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User));
|
|
|
|
auto *Load = IRB.CreateLoad(Var);
|
|
|
|
IRB.CreateRet(Load);
|
|
|
|
|
|
|
|
// Mark the user variable as used so that it isn't stripped out.
|
|
|
|
UsedVars.push_back(User);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfiling::emitUses() {
|
2016-10-26 01:53:31 +02:00
|
|
|
if (!UsedVars.empty())
|
|
|
|
appendToUsed(*M, UsedVars);
|
2014-12-08 19:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfiling::emitInitialization() {
|
2016-07-21 19:50:07 +02:00
|
|
|
StringRef InstrProfileOutput = Options.InstrProfileOutput;
|
2015-05-01 01:49:23 +02:00
|
|
|
|
2016-07-22 01:19:10 +02:00
|
|
|
if (!InstrProfileOutput.empty()) {
|
|
|
|
// Create variable for profile name.
|
|
|
|
Constant *ProfileNameConst =
|
|
|
|
ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true);
|
|
|
|
GlobalVariable *ProfileNameVar = new GlobalVariable(
|
|
|
|
*M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
|
|
|
|
ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
|
|
|
|
Triple TT(M->getTargetTriple());
|
|
|
|
if (TT.supportsCOMDAT()) {
|
|
|
|
ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
ProfileNameVar->setComdat(M->getOrInsertComdat(
|
|
|
|
StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 06:22:58 +02:00
|
|
|
Constant *RegisterF = M->getFunction(getInstrProfRegFuncsName());
|
2016-07-22 01:19:10 +02:00
|
|
|
if (!RegisterF)
|
2016-06-21 04:39:08 +02:00
|
|
|
return;
|
2014-12-08 19:02:35 +01:00
|
|
|
|
|
|
|
// Create the initialization function.
|
|
|
|
auto *VoidTy = Type::getVoidTy(M->getContext());
|
2015-10-23 06:22:58 +02:00
|
|
|
auto *F = Function::Create(FunctionType::get(VoidTy, false),
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
getInstrProfInitFuncName(), M);
|
2016-06-14 23:01:22 +02:00
|
|
|
F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
|
2014-12-08 19:02:35 +01:00
|
|
|
F->addFnAttr(Attribute::NoInline);
|
2016-06-21 04:39:08 +02:00
|
|
|
if (Options.NoRedZone)
|
|
|
|
F->addFnAttr(Attribute::NoRedZone);
|
2014-12-08 19:02:35 +01:00
|
|
|
|
|
|
|
// Add the basic block and the necessary calls.
|
|
|
|
IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F));
|
2015-05-01 01:49:23 +02:00
|
|
|
if (RegisterF)
|
2015-05-19 00:13:54 +02:00
|
|
|
IRB.CreateCall(RegisterF, {});
|
2014-12-08 19:02:35 +01:00
|
|
|
IRB.CreateRetVoid();
|
|
|
|
|
|
|
|
appendToGlobalCtors(*M, F, 0);
|
|
|
|
}
|