@@ -209,6 +209,15 @@ static cl::opt<bool> EnableLoopFlatten("enable-loop-flatten", cl::init(false),
209
209
cl::Hidden,
210
210
cl::desc(" Enable the LoopFlatten Pass" ));
211
211
212
+ // Experimentally allow loop header duplication. This should allow for better
213
+ // optimization at Oz, since loop-idiom recognition can then recognize things
214
+ // like memcpy. If this ends up being useful for many targets, we should drop
215
+ // this flag and make a code generation option that can be controlled
216
+ // independent of the opt level and exposed through the frontend.
217
+ static cl::opt<bool > EnableLoopHeaderDuplication (
218
+ " enable-loop-header-duplication" , cl::init(false ), cl::Hidden,
219
+ cl::desc(" Enable loop header duplication at any optimization level" ));
220
+
212
221
static cl::opt<bool >
213
222
EnableDFAJumpThreading (" enable-dfa-jump-thread" ,
214
223
cl::desc (" Enable DFA jump threading" ),
@@ -630,8 +639,9 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
630
639
/* AllowSpeculation=*/ false ));
631
640
632
641
// Disable header duplication in loop rotation at -Oz.
633
- LPM1.addPass (
634
- LoopRotatePass (Level != OptimizationLevel::Oz, isLTOPreLink (Phase)));
642
+ LPM1.addPass (LoopRotatePass (EnableLoopHeaderDuplication ||
643
+ Level != OptimizationLevel::Oz,
644
+ isLTOPreLink (Phase)));
635
645
// TODO: Investigate promotion cap for O1.
636
646
LPM1.addPass (LICMPass (PTO.LicmMssaOptCap , PTO.LicmMssaNoAccForPromotionCap ,
637
647
/* AllowSpeculation=*/ true ));
@@ -812,7 +822,8 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
812
822
// Disable header duplication in loop rotation at -Oz.
813
823
MPM.addPass (createModuleToFunctionPassAdaptor (
814
824
createFunctionToLoopPassAdaptor (
815
- LoopRotatePass (Level != OptimizationLevel::Oz),
825
+ LoopRotatePass (EnableLoopHeaderDuplication ||
826
+ Level != OptimizationLevel::Oz),
816
827
/* UseMemorySSA=*/ false ,
817
828
/* UseBlockFrequencyInfo=*/ false ),
818
829
PTO.EagerlyInvalidateAnalyses ));
@@ -1422,7 +1433,9 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
1422
1433
LoopPassManager LPM;
1423
1434
// First rotate loops that may have been un-rotated by prior passes.
1424
1435
// Disable header duplication at -Oz.
1425
- LPM.addPass (LoopRotatePass (Level != OptimizationLevel::Oz, LTOPreLink));
1436
+ LPM.addPass (LoopRotatePass (EnableLoopHeaderDuplication ||
1437
+ Level != OptimizationLevel::Oz,
1438
+ LTOPreLink));
1426
1439
// Some loops may have become dead by now. Try to delete them.
1427
1440
// FIXME: see discussion in https://reviews.llvm.org/D112851,
1428
1441
// this may need to be revisited once we run GVN before loop deletion
0 commit comments