diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp --- a/llvm/lib/Passes/PassBuilderPipelines.cpp +++ b/llvm/lib/Passes/PassBuilderPipelines.cpp @@ -182,6 +182,10 @@ "enable-merge-functions", cl::init(false), cl::Hidden, cl::desc("Enable function merging as part of the optimization pipeline")); +static cl::opt UseFuncPassNonTrivialLoopUnswitch( + "use-funcpass-nontrivial-unswitch", cl::init(false), cl::Hidden, + cl::desc("Use FuncPass implementation for non-trivial LoopUnswitch")); + PipelineTuningOptions::PipelineTuningOptions() { LoopInterleaving = true; LoopVectorization = true; @@ -487,11 +491,19 @@ // TODO: Investigate promotion cap for O1. LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, /*AllowSpeculation=*/true)); - LPM1.addPass( - SimpleLoopUnswitchPass(/* NonTrivial */ Level == OptimizationLevel::O3 && - EnableO3NonTrivialUnswitching)); - if (EnableLoopFlatten) - LPM1.addPass(LoopFlattenPass()); + bool RequireFuncNonTrivialUnswitching = (Level == OptimizationLevel::O3) && + EnableO3NonTrivialUnswitching && + UseFuncPassNonTrivialLoopUnswitch; + if (!RequireFuncNonTrivialUnswitching) { + LPM1.addPass( + SimpleLoopUnswitchPass(/* NonTrivial */ Level == OptimizationLevel::O3 && + EnableO3NonTrivialUnswitching)); + if (EnableLoopFlatten) + LPM1.addPass(LoopFlattenPass()); + } else { + LPM1.addPass(SimpleLoopUnswitchPass( + /* NonTrivial */ false)); + } LPM2.addPass(LoopIdiomRecognizePass()); LPM2.addPass(IndVarSimplifyPass()); @@ -525,6 +537,17 @@ FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), /*UseMemorySSA=*/true, /*UseBlockFrequencyInfo=*/true)); + if (RequireFuncNonTrivialUnswitching) { + FPM.addPass(FuncSimpleLoopUnswitchPass()); + if (EnableLoopFlatten) { + // CFGs generated by FuncSLU should be simplified before LoopFlatten + FPM.addPass( + SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); + FPM.addPass(createFunctionToLoopPassAdaptor( + LoopFlattenPass(), + /*UseMemorySSA=*/true, /*UseBlockFrequencyInfo=*/true)); + } + } FPM.addPass( SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); FPM.addPass(InstCombinePass());