diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp index 79c42b886fbbe..a6a36ff254029 100644 --- a/llvm/lib/Passes/PassBuilderPipelines.cpp +++ b/llvm/lib/Passes/PassBuilderPipelines.cpp @@ -311,8 +311,6 @@ PassBuilder::buildO1FunctionSimplificationPipeline(OptimizationLevel Level, if (EnableLoopInterchange) LPM2.addPass(LoopInterchangePass()); - if (EnableLoopFlatten) - LPM2.addPass(LoopFlattenPass()); // Do not enable unrolling in PreLinkThinLTO phase during sample PGO // because it changes IR to makes profile annotation in back compile @@ -337,6 +335,8 @@ PassBuilder::buildO1FunctionSimplificationPipeline(OptimizationLevel Level, /*UseBlockFrequencyInfo=*/true)); FPM.addPass(SimplifyCFGPass()); FPM.addPass(InstCombinePass()); + if (EnableLoopFlatten) + FPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); // The loop passes in LPM2 (LoopFullUnrollPass) do not preserve MemorySSA. // *All* loop passes must preserve it, in order to be able to use it. FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), @@ -485,8 +485,6 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, if (EnableLoopInterchange) LPM2.addPass(LoopInterchangePass()); - if (EnableLoopFlatten) - LPM2.addPass(LoopFlattenPass()); // Do not enable unrolling in PreLinkThinLTO phase during sample PGO // because it changes IR to makes profile annotation in back compile @@ -511,6 +509,8 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, /*UseBlockFrequencyInfo=*/true)); FPM.addPass(SimplifyCFGPass()); FPM.addPass(InstCombinePass()); + if (EnableLoopFlatten) + FPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); // The loop passes in LPM2 (LoopIdiomRecognizePass, IndVarSimplifyPass, // LoopDeletionPass and LoopFullUnrollPass) do not preserve MemorySSA. // *All* loop passes must preserve it, in order to be able to use it. @@ -1623,6 +1623,9 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, MainFPM.addPass(DSEPass()); MainFPM.addPass(MergedLoadStoreMotionPass()); + // More loops are countable; try to optimize them. + if (EnableLoopFlatten && Level.getSpeedupLevel() > 1) + MainFPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); if (EnableConstraintElimination) MainFPM.addPass(ConstraintEliminationPass()); @@ -1630,8 +1633,6 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, LoopPassManager LPM; LPM.addPass(IndVarSimplifyPass()); LPM.addPass(LoopDeletionPass()); - if (EnableLoopFlatten && Level.getSpeedupLevel() > 1) - LPM.addPass(LoopFlattenPass()); // FIXME: Add loop interchange. // Unroll small loops and perform peeling.