From ae889d36724efb174e8cc05d26e655c4c4ab8867 Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Wed, 27 Apr 2016 19:08:24 +0000 Subject: [PATCH] Add TargetMachine::addEarlyAsPossiblePasses, and call it from opt. Summary: This is a hook to allow TargetMachine to install passes at the EP_EarlyAsPossible PassManagerBuilder extension point. Reviewers: chandlerc Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D18614 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@267763 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Target/TargetMachine.h | 5 +++++ tools/opt/opt.cpp | 31 +++++++++++++++++++---------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/include/llvm/Target/TargetMachine.h b/include/llvm/Target/TargetMachine.h index db0a841abda..c41ba0069fa 100644 --- a/include/llvm/Target/TargetMachine.h +++ b/include/llvm/Target/TargetMachine.h @@ -217,6 +217,11 @@ public: /// uses this to answer queries about the IR. virtual TargetIRAnalysis getTargetIRAnalysis(); + /// Add target-specific function passes that should be run as early as + /// possible in the optimization pipeline. Most TargetMachines have no such + /// passes. + virtual void addEarlyAsPossiblePasses(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. diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp index 91993bfaa32..2286ac9e28d 100644 --- a/tools/opt/opt.cpp +++ b/tools/opt/opt.cpp @@ -230,7 +230,8 @@ static inline void addPass(legacy::PassManagerBase &PM, Pass *P) { /// OptLevel - Optimization Level static void AddOptimizationPasses(legacy::PassManagerBase &MPM, legacy::FunctionPassManager &FPM, - unsigned OptLevel, unsigned SizeLevel) { + TargetMachine *TM, unsigned OptLevel, + unsigned SizeLevel) { if (!NoVerify || VerifyEach) FPM.add(createVerifierPass()); // Verify that input is correct @@ -260,6 +261,14 @@ static void AddOptimizationPasses(legacy::PassManagerBase &MPM, Builder.SLPVectorize = DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; + // Add target-specific passes that need to run as early as possible. + if (TM) + Builder.addExtension( + PassManagerBuilder::EP_EarlyAsPossible, + [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) { + TM->addEarlyAsPossiblePasses(PM); + }); + Builder.populateFunctionPassManager(FPM); Builder.populateModulePassManager(MPM); } @@ -519,27 +528,27 @@ int main(int argc, char **argv) { } if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { - AddOptimizationPasses(Passes, *FPasses, 1, 0); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); OptLevelO1 = false; } if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { - AddOptimizationPasses(Passes, *FPasses, 2, 0); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); OptLevelO2 = false; } if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) { - AddOptimizationPasses(Passes, *FPasses, 2, 1); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); OptLevelOs = false; } if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) { - AddOptimizationPasses(Passes, *FPasses, 2, 2); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); OptLevelOz = false; } if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { - AddOptimizationPasses(Passes, *FPasses, 3, 0); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); OptLevelO3 = false; } @@ -591,19 +600,19 @@ int main(int argc, char **argv) { } if (OptLevelO1) - AddOptimizationPasses(Passes, *FPasses, 1, 0); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); if (OptLevelO2) - AddOptimizationPasses(Passes, *FPasses, 2, 0); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); if (OptLevelOs) - AddOptimizationPasses(Passes, *FPasses, 2, 1); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); if (OptLevelOz) - AddOptimizationPasses(Passes, *FPasses, 2, 2); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); if (OptLevelO3) - AddOptimizationPasses(Passes, *FPasses, 3, 0); + AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) { FPasses->doInitialization();