@@ -113,6 +113,8 @@ static cl::opt<bool>
113113GVNEnableSplitBackedgeInLoadPRE (" enable-split-backedge-in-load-pre" ,
114114 cl::init (false ));
115115static cl::opt<bool > GVNEnableMemDep (" enable-gvn-memdep" , cl::init(true ));
116+ static cl::opt<bool > GVNEnableMemorySSA (" enable-gvn-memoryssa" ,
117+ cl::init (false ));
116118
117119static cl::opt<uint32_t > MaxNumDeps (
118120 " gvn-max-num-deps" , cl::Hidden, cl::init(100 ),
@@ -820,6 +822,10 @@ bool GVNPass::isMemDepEnabled() const {
820822 return Options.AllowMemDep .value_or (GVNEnableMemDep);
821823}
822824
825+ bool GVNPass::isMemorySSAEnabled () const {
826+ return Options.AllowMemorySSA .value_or (GVNEnableMemorySSA);
827+ }
828+
823829PreservedAnalyses GVNPass::run (Function &F, FunctionAnalysisManager &AM) {
824830 // FIXME: The order of evaluation of these 'getResult' calls is very
825831 // significant! Re-ordering these variables will cause GVN when run alone to
@@ -833,6 +839,11 @@ PreservedAnalyses GVNPass::run(Function &F, FunctionAnalysisManager &AM) {
833839 isMemDepEnabled () ? &AM.getResult <MemoryDependenceAnalysis>(F) : nullptr ;
834840 auto &LI = AM.getResult <LoopAnalysis>(F);
835841 auto *MSSA = AM.getCachedResult <MemorySSAAnalysis>(F);
842+ if (isMemorySSAEnabled () && !MSSA) {
843+ assert (!MemDep &&
844+ " On-demand computation of MemSSA implies that MemDep is disabled!" );
845+ MSSA = &AM.getResult <MemorySSAAnalysis>(F);
846+ }
836847 auto &ORE = AM.getResult <OptimizationRemarkEmitterAnalysis>(F);
837848 bool Changed = runImpl (F, AC, DT, TLI, AA, MemDep, LI, &ORE,
838849 MSSA ? &MSSA->getMSSA () : nullptr );
@@ -861,7 +872,9 @@ void GVNPass::printPipeline(
861872 OS << (*Options.AllowLoadPRESplitBackedge ? " " : " no-" )
862873 << " split-backedge-load-pre;" ;
863874 if (Options.AllowMemDep != std::nullopt )
864- OS << (*Options.AllowMemDep ? " " : " no-" ) << " memdep" ;
875+ OS << (*Options.AllowMemDep ? " " : " no-" ) << " memdep;" ;
876+ if (Options.AllowMemorySSA != std::nullopt )
877+ OS << (*Options.AllowMemorySSA ? " " : " no-" ) << " memoryssa" ;
865878 OS << ' >' ;
866879}
867880
@@ -3293,8 +3306,11 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass {
32933306public:
32943307 static char ID; // Pass identification, replacement for typeid
32953308
3296- explicit GVNLegacyPass (bool NoMemDepAnalysis = !GVNEnableMemDep)
3297- : FunctionPass(ID), Impl(GVNOptions().setMemDep(!NoMemDepAnalysis)) {
3309+ explicit GVNLegacyPass (bool MemDepAnalysis = GVNEnableMemDep,
3310+ bool MemSSAAnalysis = GVNEnableMemorySSA)
3311+ : FunctionPass(ID), Impl(GVNOptions()
3312+ .setMemDep(MemDepAnalysis)
3313+ .setMemorySSA(MemSSAAnalysis)) {
32983314 initializeGVNLegacyPassPass (*PassRegistry::getPassRegistry ());
32993315 }
33003316
@@ -3303,6 +3319,9 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass {
33033319 return false ;
33043320
33053321 auto *MSSAWP = getAnalysisIfAvailable<MemorySSAWrapperPass>();
3322+ if (Impl.isMemorySSAEnabled () && !MSSAWP)
3323+ MSSAWP = &getAnalysis<MemorySSAWrapperPass>();
3324+
33063325 return Impl.runImpl (
33073326 F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache (F),
33083327 getAnalysis<DominatorTreeWrapperPass>().getDomTree (),
@@ -3330,6 +3349,8 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass {
33303349 AU.addPreserved <LoopInfoWrapperPass>();
33313350 AU.addRequired <OptimizationRemarkEmitterWrapperPass>();
33323351 AU.addPreserved <MemorySSAWrapperPass>();
3352+ if (Impl.isMemorySSAEnabled ())
3353+ AU.addRequired <MemorySSAWrapperPass>();
33333354 }
33343355
33353356private:
@@ -3341,6 +3362,7 @@ char GVNLegacyPass::ID = 0;
33413362INITIALIZE_PASS_BEGIN (GVNLegacyPass, " gvn" , " Global Value Numbering" , false , false )
33423363INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
33433364INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
3365+ INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
33443366INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
33453367INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
33463368INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
@@ -3349,6 +3371,4 @@ INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
33493371INITIALIZE_PASS_END(GVNLegacyPass, " gvn" , " Global Value Numbering" , false , false )
33503372
33513373// The public interface to this file...
3352- FunctionPass *llvm::createGVNPass(bool NoMemDepAnalysis) {
3353- return new GVNLegacyPass (NoMemDepAnalysis);
3354- }
3374+ FunctionPass *llvm::createGVNPass() { return new GVNLegacyPass (); }
0 commit comments