diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -493,14 +493,53 @@ // redo DCE, etc. FPM.addPass(JumpThreadingPass()); FPM.addPass(CorrelatedValuePropagationPass()); + FPM.addPass(SimplifyCFGPass()); FPM.addPass(DSEPass()); - FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging)); + + if (Level == O1) { + FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging)); + } else { + // Add a late loop simplification pipeline to catch opportunities exposed by redundancy elimination. + LoopPassManager LateLPM(DebugLogging); + LateLPM.addPass(LoopInstSimplifyPass()); + LateLPM.addPass(LoopSimplifyCFGPass()); + LateLPM.addPass(LoopRotatePass(Level != Oz)); + LateLPM.addPass(LICMPass()); + LateLPM.addPass(SimpleLoopUnswitchPass()); + LateLPM.addPass(IndVarSimplifyPass()); + LateLPM.addPass(LoopIdiomRecognizePass()); + LateLPM.addPass(LoopDeletionPass()); + // Do not enable unrolling in PreLinkThinLTO phase during sample PGO + // because it changes IR to makes profile annotation in back compile + // inaccurate. + if (Phase != ThinLTOPhase::PreLink || + !PGOOpt || PGOOpt->SampleProfileFile.empty()) + LateLPM.addPass(LoopFullUnrollPass(Level)); + FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LateLPM), DebugLogging)); + + // Eliminate redundancies. Only use GVN at O3 due to the cost of running it twice. + if (Level == O3) { + // These passes add substantial compile time so skip them at O1. + FPM.addPass(MergedLoadStoreMotionPass()); + if (RunNewGVN) + FPM.addPass(NewGVNPass()); + else + FPM.addPass(GVN()); + + // Combine any opportunities exposed by the second redundancy elimination. + FPM.addPass(InstCombinePass()); + } else { + FPM.addPass(EarlyCSEPass()); + } + } for (auto &C : ScalarOptimizerLateEPCallbacks) C(FPM, Level); // Finally, do an expensive DCE pass to catch all the dead code exposed by // the simplifications and basic cleanup after all the simplifications. + FPM.addPass(SimplifyCFGPass()); + FPM.addPass(DSEPass()); FPM.addPass(ADCEPass()); FPM.addPass(SimplifyCFGPass()); FPM.addPass(InstCombinePass());