diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h index 990a0524fe5..e96bbb9cbec 100644 --- a/include/llvm/InitializePasses.h +++ b/include/llvm/InitializePasses.h @@ -40,7 +40,7 @@ void initializeObjCARCOpts(PassRegistry&); void initializeVectorization(PassRegistry&); /// initializeInstCombine - Initialize all passes linked into the -/// ScalarOpts library. +/// InstCombine library. void initializeInstCombine(PassRegistry&); /// initializeIPO - Initialize all passes linked into the IPO library. @@ -145,7 +145,7 @@ void initializeIfConverterPass(PassRegistry&); void initializeInductiveRangeCheckEliminationPass(PassRegistry&); void initializeIndVarSimplifyPass(PassRegistry&); void initializeInlineCostAnalysisPass(PassRegistry&); -void initializeInstCombinerPass(PassRegistry&); +void initializeInstructionCombiningPassPass(PassRegistry&); void initializeInstCountPass(PassRegistry&); void initializeInstNamerPass(PassRegistry&); void initializeInternalizePassPass(PassRegistry&); diff --git a/lib/LTO/LTOCodeGenerator.cpp b/lib/LTO/LTOCodeGenerator.cpp index 5cfbb592ace..ceaddce0bd4 100644 --- a/lib/LTO/LTOCodeGenerator.cpp +++ b/lib/LTO/LTOCodeGenerator.cpp @@ -108,7 +108,7 @@ void LTOCodeGenerator::initializeLTOPasses() { initializeGlobalOptPass(R); initializeConstantMergePass(R); initializeDAHPass(R); - initializeInstCombinerPass(R); + initializeInstructionCombiningPassPass(R); initializeSimpleInlinerPass(R); initializePruneEHPass(R); initializeGlobalDCEPass(R); diff --git a/lib/Transforms/InstCombine/InstCombine.h b/lib/Transforms/InstCombine/InstCombine.h index f22b5b07b25..e404581bd91 100644 --- a/lib/Transforms/InstCombine/InstCombine.h +++ b/lib/Transforms/InstCombine/InstCombine.h @@ -104,8 +104,7 @@ public: /// combine them, as well as the pass infrastructure for running this as part /// of the LLVM pass pipeline. class LLVM_LIBRARY_VISIBILITY InstCombiner - : public FunctionPass, - public InstVisitor { + : public InstVisitor { AssumptionCache *AC; const DataLayout *DL; TargetLibraryInfo *TLI; @@ -124,21 +123,16 @@ public: typedef IRBuilder BuilderTy; BuilderTy *Builder; - static char ID; // Pass identification, replacement for typeid - InstCombiner() - : FunctionPass(ID), DL(nullptr), DT(nullptr), LI(nullptr), - Builder(nullptr) { + InstCombiner() : DL(nullptr), DT(nullptr), LI(nullptr), Builder(nullptr) { MinimizeSize = false; - initializeInstCombinerPass(*PassRegistry::getPassRegistry()); } public: - bool runOnFunction(Function &F) override; + bool run(Function &F, AssumptionCache *AC, const DataLayout *DL, + TargetLibraryInfo *TLI, DominatorTree *DT, LoopInfo *LI); bool DoOneIteration(Function &F, unsigned ItNum); - void getAnalysisUsage(AnalysisUsage &AU) const override; - AssumptionCache *getAssumptionCache() const { return AC; } const DataLayout *getDataLayout() const { return DL; } diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 5e9a22ca12e..a012be0669a 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -72,33 +72,6 @@ STATISTIC(NumExpand, "Number of expansions"); STATISTIC(NumFactor , "Number of factorizations"); STATISTIC(NumReassoc , "Number of reassociations"); -// Initialization Routines -void llvm::initializeInstCombine(PassRegistry &Registry) { - initializeInstCombinerPass(Registry); -} - -void LLVMInitializeInstCombine(LLVMPassRegistryRef R) { - initializeInstCombine(*unwrap(R)); -} - -char InstCombiner::ID = 0; -INITIALIZE_PASS_BEGIN(InstCombiner, "instcombine", - "Combine redundant instructions", false, false) -INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) -INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) -INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) -INITIALIZE_PASS_END(InstCombiner, "instcombine", - "Combine redundant instructions", false, false) - -void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const { - AU.setPreservesCFG(); - AU.addRequired(); - AU.addRequired(); - AU.addRequired(); - AU.addPreserved(); -} - - Value *InstCombiner::EmitGEPOffset(User *GEP) { return llvm::EmitGEPOffset(Builder, *getDataLayout(), GEP); } @@ -2965,17 +2938,19 @@ public: }; } -bool InstCombiner::runOnFunction(Function &F) { - if (skipOptnoneFunction(F)) - return false; - - AC = &getAnalysis().getAssumptionCache(F); - DataLayoutPass *DLP = getAnalysisIfAvailable(); - DL = DLP ? &DLP->getDataLayout() : nullptr; - DT = &getAnalysis().getDomTree(); - auto *LIWP = getAnalysisIfAvailable(); - LI = LIWP ? &LIWP->getLoopInfo() : nullptr; - TLI = &getAnalysis().getTLI(); +// FIXME: Passing all of the analyses here in the run method is ugly. We should +// separate out the worklist from the combiner so that we can construct +// a combiner once per function while re-using the storage of an external +// worklist. +bool InstCombiner::run(Function &F, AssumptionCache *AC, const DataLayout *DL, + TargetLibraryInfo *TLI, DominatorTree *DT, + LoopInfo *LI) { + // Set up our analysis pointers. + this->AC = AC; + this->DL = DL; + this->TLI = TLI; + this->DT = DT; + this->LI = LI; // Minimizing size? MinimizeSize = F.getAttributes().hasAttribute(AttributeSet::FunctionIndex, @@ -3005,6 +2980,67 @@ bool InstCombiner::runOnFunction(Function &F) { return EverMadeChange; } -FunctionPass *llvm::createInstructionCombiningPass() { - return new InstCombiner(); +namespace { +/// \brief The legacy pass manager's instcombine pass. +/// +/// This is a basic whole-function wrapper around the instcombine utility. It +/// will try to combine all instructions in the function. +class InstructionCombiningPass : public FunctionPass { + InstCombiner IC; + +public: + static char ID; // Pass identification, replacement for typeid + + InstructionCombiningPass() : FunctionPass(ID) { + initializeInstructionCombiningPassPass(*PassRegistry::getPassRegistry()); + } + + void getAnalysisUsage(AnalysisUsage &AU) const override; + bool runOnFunction(Function &F) override; +}; +} + +void InstructionCombiningPass::getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesCFG(); + AU.addRequired(); + AU.addRequired(); + AU.addRequired(); + AU.addPreserved(); +} + +bool InstructionCombiningPass::runOnFunction(Function &F) { + if (skipOptnoneFunction(F)) + return false; + + auto &AC = getAnalysis().getAssumptionCache(F); + auto *DLP = getAnalysisIfAvailable(); + auto *DL = DLP ? &DLP->getDataLayout() : nullptr; + auto &TLI = getAnalysis().getTLI(); + auto &DT = getAnalysis().getDomTree(); + auto *LIWP = getAnalysisIfAvailable(); + auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr; + + return IC.run(F, &AC, DL, &TLI, &DT, LI); +} + +char InstructionCombiningPass::ID = 0; +INITIALIZE_PASS_BEGIN(InstructionCombiningPass, "instcombine", + "Combine redundant instructions", false, false) +INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) +INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) +INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) +INITIALIZE_PASS_END(InstructionCombiningPass, "instcombine", + "Combine redundant instructions", false, false) + +// Initialization Routines +void llvm::initializeInstCombine(PassRegistry &Registry) { + initializeInstructionCombiningPassPass(Registry); +} + +void LLVMInitializeInstCombine(LLVMPassRegistryRef R) { + initializeInstructionCombiningPassPass(*unwrap(R)); +} + +FunctionPass *llvm::createInstructionCombiningPass() { + return new InstructionCombiningPass(); }