Skip to content

Commit fa58847

Browse files
committed
[GVN] Always require LoopInfo
GVN behavior depends on LoopInfo in multiple ways, most notably as a dependency for loop load PRE. As this is not a preserved-only analysis, using a cached analysis is inappropriate. In the actual optimization pipeline LoopInfo is always available at the point GVN runs, but this will ensure it stays this way even if the pipeline changes.
1 parent a21abc7 commit fa58847

File tree

2 files changed

+9
-16
lines changed
  • llvm
    • include/llvm/Transforms/Scalar
    • lib/Transforms/Scalar

2 files changed

+9
-16
lines changed

llvm/include/llvm/Transforms/Scalar/GVN.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class GVNPass : public PassInfoMixin<GVNPass> {
261261

262262
bool runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
263263
const TargetLibraryInfo &RunTLI, AAResults &RunAA,
264-
MemoryDependenceResults *RunMD, LoopInfo *LI,
264+
MemoryDependenceResults *RunMD, LoopInfo &LI,
265265
OptimizationRemarkEmitter *ORE, MemorySSA *MSSA = nullptr);
266266

267267
/// Push a new Value to the LeaderTable onto the list for its value number.

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ PreservedAnalyses GVNPass::run(Function &F, FunctionAnalysisManager &AM) {
760760
auto &AA = AM.getResult<AAManager>(F);
761761
auto *MemDep =
762762
isMemDepEnabled() ? &AM.getResult<MemoryDependenceAnalysis>(F) : nullptr;
763-
auto *LI = AM.getCachedResult<LoopAnalysis>(F);
763+
auto &LI = AM.getResult<LoopAnalysis>(F);
764764
auto *MSSA = AM.getCachedResult<MemorySSAAnalysis>(F);
765765
auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
766766
bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE,
@@ -772,8 +772,7 @@ PreservedAnalyses GVNPass::run(Function &F, FunctionAnalysisManager &AM) {
772772
PA.preserve<TargetLibraryAnalysis>();
773773
if (MSSA)
774774
PA.preserve<MemorySSAAnalysis>();
775-
if (LI)
776-
PA.preserve<LoopAnalysis>();
775+
PA.preserve<LoopAnalysis>();
777776
return PA;
778777
}
779778

@@ -1434,8 +1433,7 @@ void GVNPass::eliminatePartiallyRedundantLoad(
14341433
if (auto *RangeMD = Load->getMetadata(LLVMContext::MD_range))
14351434
NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
14361435
if (auto *AccessMD = Load->getMetadata(LLVMContext::MD_access_group))
1437-
if (LI &&
1438-
LI->getLoopFor(Load->getParent()) == LI->getLoopFor(UnavailableBlock))
1436+
if (LI->getLoopFor(Load->getParent()) == LI->getLoopFor(UnavailableBlock))
14391437
NewLoad->setMetadata(LLVMContext::MD_access_group, AccessMD);
14401438

14411439
// We do not propagate the old load's debug location, because the new
@@ -1743,9 +1741,6 @@ bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
17431741
bool GVNPass::performLoopLoadPRE(LoadInst *Load,
17441742
AvailValInBlkVect &ValuesPerBlock,
17451743
UnavailBlkVect &UnavailableBlocks) {
1746-
if (!LI)
1747-
return false;
1748-
17491744
const Loop *L = LI->getLoopFor(Load->getParent());
17501745
// TODO: Generalize to other loop blocks that dominate the latch.
17511746
if (!L || L->getHeader() != Load->getParent())
@@ -1914,7 +1909,7 @@ bool GVNPass::processNonLocalLoad(LoadInst *Load) {
19141909
// Step 4: Eliminate partial redundancy.
19151910
if (!isPREEnabled() || !isLoadPREEnabled())
19161911
return Changed;
1917-
if (!isLoadInLoopPREEnabled() && LI && LI->getLoopFor(Load->getParent()))
1912+
if (!isLoadInLoopPREEnabled() && LI->getLoopFor(Load->getParent()))
19181913
return Changed;
19191914

19201915
if (performLoopLoadPRE(Load, ValuesPerBlock, UnavailableBlocks) ||
@@ -2686,7 +2681,7 @@ bool GVNPass::processInstruction(Instruction *I) {
26862681
/// runOnFunction - This is the main transformation entry point for a function.
26872682
bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
26882683
const TargetLibraryInfo &RunTLI, AAResults &RunAA,
2689-
MemoryDependenceResults *RunMD, LoopInfo *LI,
2684+
MemoryDependenceResults *RunMD, LoopInfo &LI,
26902685
OptimizationRemarkEmitter *RunORE, MemorySSA *MSSA) {
26912686
AC = &RunAC;
26922687
DT = &RunDT;
@@ -2696,7 +2691,7 @@ bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
26962691
MD = RunMD;
26972692
ImplicitControlFlowTracking ImplicitCFT;
26982693
ICF = &ImplicitCFT;
2699-
this->LI = LI;
2694+
this->LI = &LI;
27002695
VN.setMemDep(MD);
27012696
ORE = RunORE;
27022697
InvalidBlockRPONumbers = true;
@@ -2710,7 +2705,7 @@ bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
27102705
// Merge unconditional branches, allowing PRE to catch more
27112706
// optimization opportunities.
27122707
for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
2713-
bool removedBlock = MergeBlockIntoPredecessor(&BB, &DTU, LI, MSSAU, MD);
2708+
bool removedBlock = MergeBlockIntoPredecessor(&BB, &DTU, &LI, MSSAU, MD);
27142709
if (removedBlock)
27152710
++NumGVNBlocks;
27162711

@@ -3286,8 +3281,6 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass {
32863281
if (skipFunction(F))
32873282
return false;
32883283

3289-
auto &LIWP = getAnalysis<LoopInfoWrapperPass>();
3290-
32913284
auto *MSSAWP = getAnalysisIfAvailable<MemorySSAWrapperPass>();
32923285
return Impl.runImpl(
32933286
F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
@@ -3297,7 +3290,7 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass {
32973290
Impl.isMemDepEnabled()
32983291
? &getAnalysis<MemoryDependenceWrapperPass>().getMemDep()
32993292
: nullptr,
3300-
&LIWP.getLoopInfo(),
3293+
getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
33013294
&getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(),
33023295
MSSAWP ? &MSSAWP->getMSSA() : nullptr);
33033296
}

0 commit comments

Comments
 (0)