@@ -259,6 +259,11 @@ static cl::opt<bool> PGOInstrumentEntry(
259259 " pgo-instrument-entry" , cl::init(false ), cl::Hidden,
260260 cl::desc(" Force to instrument function entry basicblock." ));
261261
262+ static cl::opt<bool >
263+ PGOInstrumentLoopEntries (" pgo-instrument-loop-entries" , cl::init(false ),
264+ cl::Hidden,
265+ cl::desc(" Force to instrument loop entries." ));
266+
262267static cl::opt<bool > PGOFunctionEntryCoverage (
263268 " pgo-function-entry-coverage" , cl::Hidden,
264269 cl::desc (
@@ -359,6 +364,7 @@ class FunctionInstrumenter final {
359364 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers;
360365 BranchProbabilityInfo *const BPI;
361366 BlockFrequencyInfo *const BFI;
367+ LoopInfo *const LI;
362368
363369 const PGOInstrumentationType InstrumentationType;
364370
@@ -376,14 +382,17 @@ class FunctionInstrumenter final {
376382 InstrumentationType == PGOInstrumentationType::CTXPROF;
377383 }
378384
385+ bool shouldInstrumentLoopEntries () const { return PGOInstrumentLoopEntries; }
386+
379387public:
380388 FunctionInstrumenter (
381389 Module &M, Function &F, TargetLibraryInfo &TLI,
382390 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers,
383391 BranchProbabilityInfo *BPI = nullptr , BlockFrequencyInfo *BFI = nullptr ,
392+ LoopInfo *LI = nullptr ,
384393 PGOInstrumentationType InstrumentationType = PGOInstrumentationType::FDO)
385394 : M(M), F(F), TLI(TLI), ComdatMembers(ComdatMembers), BPI(BPI), BFI(BFI),
386- InstrumentationType (InstrumentationType) {}
395+ LI (LI), InstrumentationType(InstrumentationType) {}
387396
388397 void instrument ();
389398};
@@ -625,12 +634,13 @@ template <class Edge, class BBInfo> class FuncPGOInstrumentation {
625634 Function &Func, TargetLibraryInfo &TLI,
626635 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers,
627636 bool CreateGlobalVar = false , BranchProbabilityInfo *BPI = nullptr ,
628- BlockFrequencyInfo *BFI = nullptr , bool IsCS = false ,
629- bool InstrumentFuncEntry = true , bool HasSingleByteCoverage = false )
637+ BlockFrequencyInfo *BFI = nullptr , LoopInfo *LI = nullptr ,
638+ bool IsCS = false , bool InstrumentFuncEntry = true ,
639+ bool InstrumentLoopEntries = false , bool HasSingleByteCoverage = false )
630640 : F(Func), IsCS(IsCS), ComdatMembers(ComdatMembers), VPC(Func, TLI),
631641 TLI (TLI), ValueSites(IPVK_Last + 1 ),
632642 SIVisitor(Func, HasSingleByteCoverage),
633- MST(F, InstrumentFuncEntry, BPI, BFI),
643+ MST(F, InstrumentFuncEntry, InstrumentLoopEntries, BPI, BFI, LI ),
634644 BCI(constructBCI(Func, HasSingleByteCoverage, InstrumentFuncEntry)) {
635645 if (BCI && PGOViewBlockCoverageGraph)
636646 BCI->viewBlockCoverageGraph ();
@@ -916,9 +926,10 @@ void FunctionInstrumenter::instrument() {
916926
917927 const bool IsCtxProf = InstrumentationType == PGOInstrumentationType::CTXPROF;
918928 FuncPGOInstrumentation<PGOEdge, PGOBBInfo> FuncInfo (
919- F, TLI, ComdatMembers, /* CreateGlobalVar=*/ !IsCtxProf, BPI, BFI,
929+ F, TLI, ComdatMembers, /* CreateGlobalVar=*/ !IsCtxProf, BPI, BFI, LI,
920930 InstrumentationType == PGOInstrumentationType::CSFDO,
921- shouldInstrumentEntryBB (), PGOBlockCoverage);
931+ shouldInstrumentEntryBB (), shouldInstrumentLoopEntries (),
932+ PGOBlockCoverage);
922933
923934 auto *const Name = IsCtxProf ? cast<GlobalValue>(&F) : FuncInfo.FuncNameVar ;
924935 auto *const CFGHash =
@@ -1136,11 +1147,13 @@ class PGOUseFunc {
11361147 PGOUseFunc (Function &Func, Module *Modu, TargetLibraryInfo &TLI,
11371148 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers,
11381149 BranchProbabilityInfo *BPI, BlockFrequencyInfo *BFIin,
1139- ProfileSummaryInfo *PSI, bool IsCS, bool InstrumentFuncEntry,
1150+ LoopInfo *LI, ProfileSummaryInfo *PSI, bool IsCS,
1151+ bool InstrumentFuncEntry, bool InstrumentLoopEntries,
11401152 bool HasSingleByteCoverage)
11411153 : F(Func), M(Modu), BFI(BFIin), PSI(PSI),
1142- FuncInfo (Func, TLI, ComdatMembers, false , BPI, BFIin, IsCS,
1143- InstrumentFuncEntry, HasSingleByteCoverage),
1154+ FuncInfo (Func, TLI, ComdatMembers, false , BPI, BFIin, LI, IsCS,
1155+ InstrumentFuncEntry, InstrumentLoopEntries,
1156+ HasSingleByteCoverage),
11441157 FreqAttr(FFA_Normal), IsCS(IsCS), VPC(Func, TLI) {}
11451158
11461159 void handleInstrProfError (Error Err, uint64_t MismatchedFuncSum);
@@ -1923,6 +1936,7 @@ static bool InstrumentAllFunctions(
19231936 Module &M, function_ref<TargetLibraryInfo &(Function &)> LookupTLI,
19241937 function_ref<BranchProbabilityInfo *(Function &)> LookupBPI,
19251938 function_ref<BlockFrequencyInfo *(Function &)> LookupBFI,
1939+ function_ref<LoopInfo *(Function &)> LookupLI,
19261940 PGOInstrumentationType InstrumentationType) {
19271941 // For the context-sensitve instrumentation, we should have a separated pass
19281942 // (before LTO/ThinLTO linking) to create these variables.
@@ -1943,10 +1957,11 @@ static bool InstrumentAllFunctions(
19431957 for (auto &F : M) {
19441958 if (skipPGOGen (F))
19451959 continue ;
1946- auto &TLI = LookupTLI (F);
1947- auto *BPI = LookupBPI (F);
1948- auto *BFI = LookupBFI (F);
1949- FunctionInstrumenter FI (M, F, TLI, ComdatMembers, BPI, BFI,
1960+ TargetLibraryInfo &TLI = LookupTLI (F);
1961+ BranchProbabilityInfo *BPI = LookupBPI (F);
1962+ BlockFrequencyInfo *BFI = LookupBFI (F);
1963+ LoopInfo *LI = LookupLI (F);
1964+ FunctionInstrumenter FI (M, F, TLI, ComdatMembers, BPI, BFI, LI,
19501965 InstrumentationType);
19511966 FI.instrument ();
19521967 }
@@ -1980,8 +1995,11 @@ PreservedAnalyses PGOInstrumentationGen::run(Module &M,
19801995 auto LookupBFI = [&FAM](Function &F) {
19811996 return &FAM.getResult <BlockFrequencyAnalysis>(F);
19821997 };
1998+ auto LookupLI = [&FAM](Function &F) {
1999+ return &FAM.getResult <LoopAnalysis>(F);
2000+ };
19832001
1984- if (!InstrumentAllFunctions (M, LookupTLI, LookupBPI, LookupBFI,
2002+ if (!InstrumentAllFunctions (M, LookupTLI, LookupBPI, LookupBFI, LookupLI,
19852003 InstrumentationType))
19862004 return PreservedAnalyses::all ();
19872005
@@ -2116,7 +2134,8 @@ static bool annotateAllFunctions(
21162134 function_ref<TargetLibraryInfo &(Function &)> LookupTLI,
21172135 function_ref<BranchProbabilityInfo *(Function &)> LookupBPI,
21182136 function_ref<BlockFrequencyInfo *(Function &)> LookupBFI,
2119- ProfileSummaryInfo *PSI, bool IsCS) {
2137+ function_ref<LoopInfo *(Function &)> LookupLI, ProfileSummaryInfo *PSI,
2138+ bool IsCS) {
21202139 LLVM_DEBUG (dbgs () << " Read in profile counters: " );
21212140 auto &Ctx = M.getContext ();
21222141 // Read the counter array from file.
@@ -2181,22 +2200,26 @@ static bool annotateAllFunctions(
21812200 bool InstrumentFuncEntry = PGOReader->instrEntryBBEnabled ();
21822201 if (PGOInstrumentEntry.getNumOccurrences () > 0 )
21832202 InstrumentFuncEntry = PGOInstrumentEntry;
2203+ bool InstrumentLoopEntries =
2204+ (PGOInstrumentLoopEntries.getNumOccurrences () > 0 );
21842205
21852206 bool HasSingleByteCoverage = PGOReader->hasSingleByteCoverage ();
21862207 for (auto &F : M) {
21872208 if (skipPGOUse (F))
21882209 continue ;
2189- auto &TLI = LookupTLI (F);
2190- auto *BPI = LookupBPI (F);
2191- auto *BFI = LookupBFI (F);
2210+ TargetLibraryInfo &TLI = LookupTLI (F);
2211+ BranchProbabilityInfo *BPI = LookupBPI (F);
2212+ BlockFrequencyInfo *BFI = LookupBFI (F);
2213+ LoopInfo *LI = LookupLI (F);
21922214 if (!HasSingleByteCoverage) {
21932215 // Split indirectbr critical edges here before computing the MST rather
21942216 // than later in getInstrBB() to avoid invalidating it.
21952217 SplitIndirectBrCriticalEdges (F, /* IgnoreBlocksWithoutPHI=*/ false , BPI,
21962218 BFI);
21972219 }
2198- PGOUseFunc Func (F, &M, TLI, ComdatMembers, BPI, BFI, PSI, IsCS,
2199- InstrumentFuncEntry, HasSingleByteCoverage);
2220+ PGOUseFunc Func (F, &M, TLI, ComdatMembers, BPI, BFI, LI, PSI, IsCS,
2221+ InstrumentFuncEntry, InstrumentLoopEntries,
2222+ HasSingleByteCoverage);
22002223 if (HasSingleByteCoverage) {
22012224 Func.populateCoverage (PGOReader.get ());
22022225 continue ;
@@ -2335,10 +2358,14 @@ PreservedAnalyses PGOInstrumentationUse::run(Module &M,
23352358 auto LookupBFI = [&FAM](Function &F) {
23362359 return &FAM.getResult <BlockFrequencyAnalysis>(F);
23372360 };
2361+ auto LookupLI = [&FAM](Function &F) {
2362+ return &FAM.getResult <LoopAnalysis>(F);
2363+ };
23382364
23392365 auto *PSI = &MAM.getResult <ProfileSummaryAnalysis>(M);
23402366 if (!annotateAllFunctions (M, ProfileFileName, ProfileRemappingFileName, *FS,
2341- LookupTLI, LookupBPI, LookupBFI, PSI, IsCS))
2367+ LookupTLI, LookupBPI, LookupBFI, LookupLI, PSI,
2368+ IsCS))
23422369 return PreservedAnalyses::all ();
23432370
23442371 return PreservedAnalyses::none ();
0 commit comments