Index: include/llvm/Target/TargetMachine.h =================================================================== --- include/llvm/Target/TargetMachine.h +++ include/llvm/Target/TargetMachine.h @@ -210,6 +210,11 @@ /// passes. virtual void addEarlyAsPossiblePasses(PassManagerBase &) {} + /// Add target-specific passes that run before the loop idiom recognition + /// passes. This could be used to implement target-specific loop idiom + /// recognition. + virtual void addBeforeLoopIdiomPasses(PassManagerBase &) {} + /// These enums are meant to be passed into addPassesToEmitFile to indicate /// what type of file to emit, and returned by it to indicate what type of /// file could actually be made. Index: include/llvm/Transforms/IPO/PassManagerBuilder.h =================================================================== --- include/llvm/Transforms/IPO/PassManagerBuilder.h +++ include/llvm/Transforms/IPO/PassManagerBuilder.h @@ -100,6 +100,11 @@ /// will be inserted after each instance of the instruction combiner pass. EP_Peephole, + /// EP_BeforeLoopIdiom - This extension point allows adding passes before + /// loop-idiom recognition passes. It can be used, for example, by a + /// target to add a specific loop-idiom recognition pass. + EP_BeforeLoopIdiom, + /// EP_CGSCCOptimizerLate - This extension point allows adding CallGraphSCC /// passes at the end of the main CallGraphSCC passes and before any /// function simplification passes run by CGPassManager. Index: lib/Transforms/IPO/PassManagerBuilder.cpp =================================================================== --- lib/Transforms/IPO/PassManagerBuilder.cpp +++ lib/Transforms/IPO/PassManagerBuilder.cpp @@ -315,6 +315,9 @@ MPM.add(createCFGSimplificationPass()); addInstructionCombiningPass(MPM); MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars + + addExtensionsToPM(EP_BeforeLoopIdiom, MPM); + MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. MPM.add(createLoopDeletionPass()); // Delete dead loops if (EnableLoopInterchange) { Index: tools/opt/opt.cpp =================================================================== --- tools/opt/opt.cpp +++ tools/opt/opt.cpp @@ -288,12 +288,18 @@ DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; // Add target-specific passes that need to run as early as possible. - if (TM) + if (TM) { Builder.addExtension( PassManagerBuilder::EP_EarlyAsPossible, [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) { TM->addEarlyAsPossiblePasses(PM); }); + Builder.addExtension( + PassManagerBuilder::EP_BeforeLoopIdiom, + [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) { + TM->addBeforeLoopIdiomPasses(PM); + }); + } if (Coroutines) addCoroutinePassesToExtensionPoints(Builder);