From 864078facb666068b43086f7617c435cb9099066 Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Fri, 24 Apr 2020 17:02:37 +0700 Subject: [PATCH] [LoopVectorize] Preserve CFG analyses if CFG wasn't modified One of transforms the loop vectorizer makes is LCSSA formation. In some cases it is the only transform it makes. We should not drop CFG analyzes if only LCSSA was formed and no actual CFG changes was made. We should think of expanding this logic to other passes as well, and maybe make it a part of PM framework. Reviewed By: Florian Hahn Differential Revision: https://reviews.llvm.org/D78360 --- .../llvm/Transforms/Vectorize/LoopVectorize.h | 22 ++++++++++++++----- lib/Transforms/Vectorize/LoopVectorize.cpp | 20 +++++++++-------- .../novect-lcssa-cfg-invalidation.ll | 4 ++-- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/include/llvm/Transforms/Vectorize/LoopVectorize.h b/include/llvm/Transforms/Vectorize/LoopVectorize.h index 45313dc1db4..22596b9d0bb 100644 --- a/include/llvm/Transforms/Vectorize/LoopVectorize.h +++ b/include/llvm/Transforms/Vectorize/LoopVectorize.h @@ -116,6 +116,15 @@ struct LoopVectorizeOptions { } }; +/// Storage for information about made changes. +struct LoopVectorizeResult { + bool MadeAnyChange; + bool MadeCFGChange; + + LoopVectorizeResult(bool MadeAnyChange, bool MadeCFGChange) + : MadeAnyChange(MadeAnyChange), MadeCFGChange(MadeCFGChange) {} +}; + /// The LoopVectorize Pass. struct LoopVectorizePass : public PassInfoMixin { private: @@ -146,12 +155,13 @@ public: PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); // Shim for old PM. - bool runImpl(Function &F, ScalarEvolution &SE_, LoopInfo &LI_, - TargetTransformInfo &TTI_, DominatorTree &DT_, - BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_, - DemandedBits &DB_, AliasAnalysis &AA_, AssumptionCache &AC_, - std::function &GetLAA_, - OptimizationRemarkEmitter &ORE_, ProfileSummaryInfo *PSI_); + LoopVectorizeResult + runImpl(Function &F, ScalarEvolution &SE_, LoopInfo &LI_, + TargetTransformInfo &TTI_, DominatorTree &DT_, + BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_, DemandedBits &DB_, + AliasAnalysis &AA_, AssumptionCache &AC_, + std::function &GetLAA_, + OptimizationRemarkEmitter &ORE_, ProfileSummaryInfo *PSI_); bool processLoop(Loop *L); }; diff --git a/lib/Transforms/Vectorize/LoopVectorize.cpp b/lib/Transforms/Vectorize/LoopVectorize.cpp index 7cb1695d05b..c884548eb86 100644 --- a/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -1632,7 +1632,7 @@ struct LoopVectorize : public FunctionPass { [&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); }; return Impl.runImpl(F, *SE, *LI, *TTI, *DT, *BFI, TLI, *DB, *AA, *AC, - GetLAA, *ORE, PSI); + GetLAA, *ORE, PSI).MadeAnyChange; } void getAnalysisUsage(AnalysisUsage &AU) const override { @@ -7954,7 +7954,7 @@ bool LoopVectorizePass::processLoop(Loop *L) { return true; } -bool LoopVectorizePass::runImpl( +LoopVectorizeResult LoopVectorizePass::runImpl( Function &F, ScalarEvolution &SE_, LoopInfo &LI_, TargetTransformInfo &TTI_, DominatorTree &DT_, BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_, DemandedBits &DB_, AliasAnalysis &AA_, AssumptionCache &AC_, @@ -7982,9 +7982,9 @@ bool LoopVectorizePass::runImpl( // interleaving. if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true)) && TTI->getMaxInterleaveFactor(1) < 2) - return false; + return LoopVectorizeResult(false, false); - bool Changed = false; + bool Changed = false, CFGChanged = false; // The vectorizer requires loops to be in simplified form. // Since simplification may add new inner loops, it has to run before the @@ -7992,7 +7992,7 @@ bool LoopVectorizePass::runImpl( // will simplify all loops, regardless of whether anything end up being // vectorized. for (auto &L : *LI) - Changed |= + Changed |= CFGChanged |= simplifyLoop(L, DT, LI, SE, AC, nullptr, false /* PreserveLCSSA */); // Build up a worklist of inner-loops to vectorize. This is necessary as @@ -8013,11 +8013,11 @@ bool LoopVectorizePass::runImpl( // transform. Changed |= formLCSSARecursively(*L, *DT, LI, SE); - Changed |= processLoop(L); + Changed |= CFGChanged |= processLoop(L); } // Process each loop nest in the function. - return Changed; + return LoopVectorizeResult(Changed, CFGChanged); } PreservedAnalyses LoopVectorizePass::run(Function &F, @@ -8046,9 +8046,9 @@ PreservedAnalyses LoopVectorizePass::run(Function &F, AM.getResult(F).getManager(); ProfileSummaryInfo *PSI = MAM.getCachedResult(*F.getParent()); - bool Changed = + LoopVectorizeResult Result = runImpl(F, SE, LI, TTI, DT, BFI, &TLI, DB, AA, AC, GetLAA, ORE, PSI); - if (!Changed) + if (!Result.MadeAnyChange) return PreservedAnalyses::all(); PreservedAnalyses PA; @@ -8062,5 +8062,7 @@ PreservedAnalyses LoopVectorizePass::run(Function &F, } PA.preserve(); PA.preserve(); + if (!Result.MadeCFGChange) + PA.preserveSet(); return PA; } diff --git a/test/Transforms/LoopVectorize/novect-lcssa-cfg-invalidation.ll b/test/Transforms/LoopVectorize/novect-lcssa-cfg-invalidation.ll index b1e424711cf..05b36f8c550 100644 --- a/test/Transforms/LoopVectorize/novect-lcssa-cfg-invalidation.ll +++ b/test/Transforms/LoopVectorize/novect-lcssa-cfg-invalidation.ll @@ -11,8 +11,8 @@ define i32 @novect(i32* %p) { ; CHECK: Invalidating all non-preserved analyses for: novect ; CHECK: Clearing all analysis results for: ; CHECK: Invalidating analysis: ScalarEvolutionAnalysis on novect -; CHECK: Invalidating analysis: BranchProbabilityAnalysis on novect -; CHECK: Invalidating analysis: BlockFrequencyAnalysis on novect +; CHECK-NOT: Invalidating analysis: BranchProbabilityAnalysis on novect +; CHECK-NOT: Invalidating analysis: BlockFrequencyAnalysis on novect ; CHECK: Invalidating analysis: DemandedBitsAnalysis on novect ; CHECK: Invalidating analysis: MemorySSAAnalysis on novect ; CHECK: Running pass: JumpThreadingPass on novect