diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -409,7 +409,7 @@ void initializeStackSafetyGlobalInfoWrapperPassPass(PassRegistry &); void initializeStackSafetyInfoWrapperPassPass(PassRegistry &); void initializeStackSlotColoringPass(PassRegistry&); -void initializeStraightLineStrengthReducePass(PassRegistry&); +void initializeStraightLineStrengthReduceLegacyPassPass(PassRegistry &); void initializeStripDeadDebugInfoPass(PassRegistry&); void initializeStripDeadPrototypesLegacyPassPass(PassRegistry&); void initializeStripDebugDeclarePass(PassRegistry&); diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -192,6 +192,7 @@ #include "llvm/Transforms/Scalar/Sink.h" #include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h" #include "llvm/Transforms/Scalar/SpeculativeExecution.h" +#include "llvm/Transforms/Scalar/StraightLineStrengthReduce.h" #include "llvm/Transforms/Scalar/StructurizeCFG.h" #include "llvm/Transforms/Scalar/TailRecursionElimination.h" #include "llvm/Transforms/Scalar/WarnMissedTransforms.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -283,6 +283,7 @@ FUNCTION_PASS("simplifycfg", SimplifyCFGPass()) FUNCTION_PASS("sink", SinkingPass()) FUNCTION_PASS("slp-vectorizer", SLPVectorizerPass()) +FUNCTION_PASS("slsr", StraightLineStrengthReducePass()) FUNCTION_PASS("speculative-execution", SpeculativeExecutionPass()) FUNCTION_PASS("spec-phis", SpeculateAroundPHIsPass()) FUNCTION_PASS("sroa", SROA()) diff --git a/llvm/lib/Transforms/Scalar/Scalar.cpp b/llvm/lib/Transforms/Scalar/Scalar.cpp --- a/llvm/lib/Transforms/Scalar/Scalar.cpp +++ b/llvm/lib/Transforms/Scalar/Scalar.cpp @@ -103,7 +103,7 @@ initializeTailCallElimPass(Registry); initializeSeparateConstOffsetFromGEPPass(Registry); initializeSpeculativeExecutionLegacyPassPass(Registry); - initializeStraightLineStrengthReducePass(Registry); + initializeStraightLineStrengthReduceLegacyPassPass(Registry); initializePlaceBackedgeSafepointsImplPass(Registry); initializePlaceSafepointsPass(Registry); initializeFloat2IntLegacyPassPass(Registry); diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp --- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp @@ -55,6 +55,7 @@ // - When (i' - i) is constant but i and i' are not, we could still perform // SLSR. +#include "llvm/Transforms/Scalar/StraightLineStrengthReduce.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/SmallVector.h" @@ -95,8 +96,39 @@ namespace { -class StraightLineStrengthReduce : public FunctionPass { +class StraightLineStrengthReduceLegacyPass : public FunctionPass { + const DataLayout *DL = nullptr; + +public: + static char ID; + + StraightLineStrengthReduceLegacyPass() : FunctionPass(ID) { + initializeStraightLineStrengthReduceLegacyPassPass( + *PassRegistry::getPassRegistry()); + } + + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.addRequired(); + AU.addRequired(); + AU.addRequired(); + // We do not modify the shape of the CFG. + AU.setPreservesCFG(); + } + + bool doInitialization(Module &M) override { + DL = &M.getDataLayout(); + return false; + } + + bool runOnFunction(Function &F) override; +}; + +class StraightLineStrengthReduce { public: + StraightLineStrengthReduce(const DataLayout *DL, DominatorTree *DT, + ScalarEvolution *SE, TargetTransformInfo *TTI) + : DL(DL), DT(DT), SE(SE), TTI(TTI) {} + // SLSR candidate. Such a candidate must be in one of the forms described in // the header comments. struct Candidate { @@ -144,26 +176,7 @@ Candidate *Basis = nullptr; }; - static char ID; - - StraightLineStrengthReduce() : FunctionPass(ID) { - initializeStraightLineStrengthReducePass(*PassRegistry::getPassRegistry()); - } - - void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired(); - AU.addRequired(); - AU.addRequired(); - // We do not modify the shape of the CFG. - AU.setPreservesCFG(); - } - - bool doInitialization(Module &M) override { - DL = &M.getDataLayout(); - return false; - } - - bool runOnFunction(Function &F) override; + bool runOnFunction(Function &F); private: // Returns true if Basis is a basis for C, i.e., Basis dominates C and they @@ -243,18 +256,18 @@ } // end anonymous namespace -char StraightLineStrengthReduce::ID = 0; +char StraightLineStrengthReduceLegacyPass::ID = 0; -INITIALIZE_PASS_BEGIN(StraightLineStrengthReduce, "slsr", +INITIALIZE_PASS_BEGIN(StraightLineStrengthReduceLegacyPass, "slsr", "Straight line strength reduction", false, false) INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) -INITIALIZE_PASS_END(StraightLineStrengthReduce, "slsr", +INITIALIZE_PASS_END(StraightLineStrengthReduceLegacyPass, "slsr", "Straight line strength reduction", false, false) FunctionPass *llvm::createStraightLineStrengthReducePass() { - return new StraightLineStrengthReduce(); + return new StraightLineStrengthReduceLegacyPass(); } bool StraightLineStrengthReduce::isBasisFor(const Candidate &Basis, @@ -704,13 +717,17 @@ UnlinkedInstructions.push_back(C.Ins); } -bool StraightLineStrengthReduce::runOnFunction(Function &F) { +bool StraightLineStrengthReduceLegacyPass::runOnFunction(Function &F) { if (skipFunction(F)) return false; - TTI = &getAnalysis().getTTI(F); - DT = &getAnalysis().getDomTree(); - SE = &getAnalysis().getSE(); + auto *TTI = &getAnalysis().getTTI(F); + auto *DT = &getAnalysis().getDomTree(); + auto *SE = &getAnalysis().getSE(); + return StraightLineStrengthReduce(DL, DT, SE, TTI).runOnFunction(F); +} + +bool StraightLineStrengthReduce::runOnFunction(Function &F) { // Traverse the dominator tree in the depth-first order. This order makes sure // all bases of a candidate are in Candidates when we process it. for (const auto Node : depth_first(DT)) @@ -740,3 +757,24 @@ UnlinkedInstructions.clear(); return Ret; } + +namespace llvm { + +PreservedAnalyses +StraightLineStrengthReducePass::run(Function &F, FunctionAnalysisManager &AM) { + const DataLayout *DL = &F.getParent()->getDataLayout(); + auto *DT = &AM.getResult(F); + auto *SE = &AM.getResult(F); + auto *TTI = &AM.getResult(F); + + if (!StraightLineStrengthReduce(DL, DT, SE, TTI).runOnFunction(F)) + return PreservedAnalyses::all(); + + PreservedAnalyses PA; + PA.preserve(); + PA.preserve(); + PA.preserve(); + return PA; +} + +} // namespace llvm diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/AMDGPU/pr23975.ll b/llvm/test/Transforms/StraightLineStrengthReduce/AMDGPU/pr23975.ll --- a/llvm/test/Transforms/StraightLineStrengthReduce/AMDGPU/pr23975.ll +++ b/llvm/test/Transforms/StraightLineStrengthReduce/AMDGPU/pr23975.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -slsr -S | FileCheck %s +; RUN: opt < %s -passes='slsr' -S | FileCheck %s target datalayout = "e-p:32:32-p1:64:64-p2:64:64-p3:32:32-p4:64:64-p5:32:32-p24:64:64-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64" target triple = "amdgcn--" diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll b/llvm/test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll --- a/llvm/test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll +++ b/llvm/test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -slsr -gvn -S | FileCheck %s +; RUN: opt < %s -passes='slsr,gvn' -S | FileCheck %s target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/slsr-add.ll b/llvm/test/Transforms/StraightLineStrengthReduce/slsr-add.ll --- a/llvm/test/Transforms/StraightLineStrengthReduce/slsr-add.ll +++ b/llvm/test/Transforms/StraightLineStrengthReduce/slsr-add.ll @@ -1,5 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -slsr -gvn -S | FileCheck %s +; RUN: opt < %s -passes='slsr,gvn' -S | FileCheck %s target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64" diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll b/llvm/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll --- a/llvm/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll +++ b/llvm/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -slsr -gvn -S | FileCheck %s +; RUN: opt < %s -passes='slsr,gvn' -S | FileCheck %s target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64-p:64:64:64-p1:32:32:32" diff --git a/llvm/test/Transforms/StraightLineStrengthReduce/slsr-mul.ll b/llvm/test/Transforms/StraightLineStrengthReduce/slsr-mul.ll --- a/llvm/test/Transforms/StraightLineStrengthReduce/slsr-mul.ll +++ b/llvm/test/Transforms/StraightLineStrengthReduce/slsr-mul.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -slsr -gvn -S | FileCheck %s +; RUN: opt < %s -passes='slsr,gvn' -S | FileCheck %s target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"