Refactor PGO code in preparation for handling non-C/C++ code.

Move the PGO.assignRegionCounters() call out of StartFunction, because that
function is called from many places where it does not make sense to do PGO
instrumentation (e.g., compiler-generated helper functions). Change several
functions to take a StringRef argument for the unique name associated with
a function, so that the name can be set differently for things like Objective-C
methods and block literals.

llvm-svn: 203073
This commit is contained in:
Bob Wilson 2014-03-06 04:55:35 +00:00
parent 749ebc7f33
commit 68f475faf7
3 changed files with 11 additions and 12 deletions

View File

@ -589,7 +589,6 @@ void CodeGenFunction::StartFunction(GlobalDecl GD,
if (CGM.getCodeGenOpts().InstrumentForProfiling) if (CGM.getCodeGenOpts().InstrumentForProfiling)
EmitMCountInstrumentation(); EmitMCountInstrumentation();
PGO.assignRegionCounters(GD);
if (CGM.getPGOData() && D) { if (CGM.getPGOData() && D) {
// Turn on InlineHint attribute for hot functions. // Turn on InlineHint attribute for hot functions.
if (CGM.getPGOData()->isHotFunction(CGM.getMangledName(GD))) if (CGM.getPGOData()->isHotFunction(CGM.getMangledName(GD)))
@ -771,6 +770,7 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin()); StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin());
// Generate the body of the function. // Generate the body of the function.
PGO.assignRegionCounters(GD.getDecl(), CGM.getMangledName(GD));
if (isa<CXXDestructorDecl>(FD)) if (isa<CXXDestructorDecl>(FD))
EmitDestructorBody(Args); EmitDestructorBody(Args);
else if (isa<CXXConstructorDecl>(FD)) else if (isa<CXXConstructorDecl>(FD))
@ -831,7 +831,7 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
if (!CurFn->doesNotThrow()) if (!CurFn->doesNotThrow())
TryMarkNoThrow(CurFn); TryMarkNoThrow(CurFn);
PGO.emitWriteoutFunction(CurGD); PGO.emitWriteoutFunction(CGM.getMangledName(CurGD));
PGO.destroyRegionCounters(); PGO.destroyRegionCounters();
} }

View File

@ -162,7 +162,7 @@ bool PGOProfileData::getFunctionCounts(StringRef MangledName,
return false; return false;
} }
void CodeGenPGO::emitWriteoutFunction(GlobalDecl &GD) { void CodeGenPGO::emitWriteoutFunction(StringRef Name) {
if (!CGM.getCodeGenOpts().ProfileInstrGenerate) if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
return; return;
@ -207,7 +207,7 @@ void CodeGenPGO::emitWriteoutFunction(GlobalDecl &GD) {
CGM.getModule().getOrInsertFunction("llvm_pgo_emit", FTy); CGM.getModule().getOrInsertFunction("llvm_pgo_emit", FTy);
llvm::Constant *MangledName = llvm::Constant *MangledName =
CGM.GetAddrOfConstantCString(CGM.getMangledName(GD), "__llvm_pgo_name"); CGM.GetAddrOfConstantCString(Name, "__llvm_pgo_name");
MangledName = llvm::ConstantExpr::getBitCast(MangledName, Int8PtrTy); MangledName = llvm::ConstantExpr::getBitCast(MangledName, Int8PtrTy);
PGOBuilder.CreateCall3(EmitFunc, MangledName, PGOBuilder.CreateCall3(EmitFunc, MangledName,
PGOBuilder.getInt32(NumRegionCounters), PGOBuilder.getInt32(NumRegionCounters),
@ -728,19 +728,18 @@ namespace {
}; };
} }
void CodeGenPGO::assignRegionCounters(GlobalDecl &GD) { void CodeGenPGO::assignRegionCounters(const Decl *D, StringRef Name) {
bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate; bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
PGOProfileData *PGOData = CGM.getPGOData(); PGOProfileData *PGOData = CGM.getPGOData();
if (!InstrumentRegions && !PGOData) if (!InstrumentRegions && !PGOData)
return; return;
const Decl *D = GD.getDecl();
if (!D) if (!D)
return; return;
mapRegionCounters(D); mapRegionCounters(D);
if (InstrumentRegions) if (InstrumentRegions)
emitCounterVariables(); emitCounterVariables();
if (PGOData) { if (PGOData) {
loadRegionCounts(GD, PGOData); loadRegionCounts(Name, PGOData);
computeRegionCounts(D); computeRegionCounts(D);
} }
} }
@ -781,13 +780,13 @@ void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
Builder.CreateStore(Count, Addr); Builder.CreateStore(Count, Addr);
} }
void CodeGenPGO::loadRegionCounts(GlobalDecl &GD, PGOProfileData *PGOData) { void CodeGenPGO::loadRegionCounts(StringRef Name, PGOProfileData *PGOData) {
// For now, ignore the counts from the PGO data file only if the number of // 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 // 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 // ignore counts when the input changes in various ways, e.g., by comparing a
// hash value based on some characteristics of the input. // hash value based on some characteristics of the input.
RegionCounts = new std::vector<uint64_t>(); RegionCounts = new std::vector<uint64_t>();
if (PGOData->getFunctionCounts(CGM.getMangledName(GD), *RegionCounts) || if (PGOData->getFunctionCounts(Name, *RegionCounts) ||
RegionCounts->size() != NumRegionCounters) { RegionCounts->size() != NumRegionCounters) {
delete RegionCounts; delete RegionCounts;
RegionCounts = 0; RegionCounts = 0;

View File

@ -118,9 +118,9 @@ public:
/// function. Does nothing if instrumentation is not enabled and either /// function. Does nothing if instrumentation is not enabled and either
/// generates global variables or associates PGO data with each of the /// generates global variables or associates PGO data with each of the
/// counters depending on whether we are generating or using instrumentation. /// counters depending on whether we are generating or using instrumentation.
void assignRegionCounters(GlobalDecl &GD); void assignRegionCounters(const Decl *D, StringRef Name);
/// Emit code to write counts for a given function to disk, if necessary. /// Emit code to write counts for a given function to disk, if necessary.
void emitWriteoutFunction(GlobalDecl &GD); void emitWriteoutFunction(StringRef Name);
/// Clean up region counter state. Must be called if assignRegionCounters is /// Clean up region counter state. Must be called if assignRegionCounters is
/// used. /// used.
void destroyRegionCounters(); void destroyRegionCounters();
@ -131,7 +131,7 @@ public:
private: private:
void mapRegionCounters(const Decl *D); void mapRegionCounters(const Decl *D);
void computeRegionCounts(const Decl *D); void computeRegionCounts(const Decl *D);
void loadRegionCounts(GlobalDecl &GD, PGOProfileData *PGOData); void loadRegionCounts(StringRef Name, PGOProfileData *PGOData);
void emitCounterVariables(); void emitCounterVariables();
/// Emit code to increment the counter at the given index /// Emit code to increment the counter at the given index