Index: llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp +++ llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp @@ -162,7 +162,6 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/MemoryBuiltins.h" -#include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Analysis/ValueTracking.h" @@ -355,7 +354,6 @@ void getAnalysisUsage(AnalysisUsage &AU) const override { AU.addRequired(); - AU.addRequired(); AU.addRequired(); AU.addRequired(); AU.setPreservesCFG(); @@ -374,14 +372,16 @@ class SeparateConstOffsetFromGEP { public: SeparateConstOffsetFromGEP( - DominatorTree *DT, ScalarEvolution *SE, LoopInfo *LI, - TargetLibraryInfo *TLI, + DominatorTree *DT, LoopInfo *LI, TargetLibraryInfo *TLI, function_ref GetTTI, bool LowerGEP) - : DT(DT), SE(SE), LI(LI), TLI(TLI), GetTTI(GetTTI), LowerGEP(LowerGEP) {} + : DT(DT), LI(LI), TLI(TLI), GetTTI(GetTTI), LowerGEP(LowerGEP) {} bool run(Function &F); private: + /// Track the operands of an add or sub. + using BinOpOperands = std::pair; + /// Tries to split the given GEP into a variadic base and a constant offset, /// and returns true if the splitting succeeds. bool splitGEP(GetElementPtrInst *GEP); @@ -446,8 +446,8 @@ /// Find the closest dominator of that is equivalent to . Instruction *findClosestMatchingDominator( - const SCEV *Key, Instruction *Dominatee, - DenseMap> &DominatingExprs); + BinOpOperands Key, Instruction *Dominatee, + DenseMap> &DominatingExprs); /// Verify F is free of dead code. void verifyNoDeadCode(Function &F); @@ -463,7 +463,6 @@ const DataLayout *DL = nullptr; DominatorTree *DT = nullptr; - ScalarEvolution *SE; LoopInfo *LI; TargetLibraryInfo *TLI; // Retrieved lazily since not always used. @@ -473,8 +472,8 @@ /// multiple GEPs with a single index. bool LowerGEP; - DenseMap> DominatingAdds; - DenseMap> DominatingSubs; + DenseMap> DominatingAdds; + DenseMap> DominatingSubs; }; } // end anonymous namespace @@ -1171,13 +1170,12 @@ if (skipFunction(F)) return false; auto *DT = &getAnalysis().getDomTree(); - auto *SE = &getAnalysis().getSE(); auto *LI = &getAnalysis().getLoopInfo(); auto *TLI = &getAnalysis().getTLI(F); auto GetTTI = [this](Function &F) -> TargetTransformInfo & { return this->getAnalysis().getTTI(F); }; - SeparateConstOffsetFromGEP Impl(DT, SE, LI, TLI, GetTTI, LowerGEP); + SeparateConstOffsetFromGEP Impl(DT, LI, TLI, GetTTI, LowerGEP); return Impl.run(F); } @@ -1207,8 +1205,8 @@ } Instruction *SeparateConstOffsetFromGEP::findClosestMatchingDominator( - const SCEV *Key, Instruction *Dominatee, - DenseMap> &DominatingExprs) { + BinOpOperands Key, Instruction *Dominatee, + DenseMap> &DominatingExprs) { auto Pos = DominatingExprs.find(Key); if (Pos == DominatingExprs.end()) return nullptr; @@ -1228,7 +1226,7 @@ } bool SeparateConstOffsetFromGEP::reuniteExts(Instruction *I) { - if (!SE->isSCEVable(I->getType())) + if (!I->getType()->isIntOrIntVectorTy()) return false; // Dom: LHS+RHS @@ -1238,9 +1236,8 @@ Value *LHS = nullptr, *RHS = nullptr; if (match(I, m_Add(m_SExt(m_Value(LHS)), m_SExt(m_Value(RHS))))) { if (LHS->getType() == RHS->getType()) { - const SCEV *Key = - SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS)); - if (auto *Dom = findClosestMatchingDominator(Key, I, DominatingAdds)) { + if (auto *Dom = + findClosestMatchingDominator({LHS, RHS}, I, DominatingAdds)) { Instruction *NewSExt = new SExtInst(Dom, I->getType(), "", I); NewSExt->takeName(I); I->replaceAllUsesWith(NewSExt); @@ -1250,9 +1247,8 @@ } } else if (match(I, m_Sub(m_SExt(m_Value(LHS)), m_SExt(m_Value(RHS))))) { if (LHS->getType() == RHS->getType()) { - const SCEV *Key = SE->getAddExpr( - SE->getUnknown(LHS), SE->getNegativeSCEV(SE->getUnknown(RHS))); - if (auto *Dom = findClosestMatchingDominator(Key, I, DominatingSubs)) { + if (auto *Dom = + findClosestMatchingDominator({LHS, RHS}, I, DominatingSubs)) { Instruction *NewSExt = new SExtInst(Dom, I->getType(), "", I); NewSExt->takeName(I); I->replaceAllUsesWith(NewSExt); @@ -1264,17 +1260,11 @@ // Add I to DominatingExprs if it's an add/sub that can't sign overflow. if (match(I, m_NSWAdd(m_Value(LHS), m_Value(RHS)))) { - if (programUndefinedIfPoison(I)) { - const SCEV *Key = - SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS)); - DominatingAdds[Key].push_back(I); - } + if (programUndefinedIfPoison(I)) + DominatingAdds[{LHS, RHS}].push_back(I); } else if (match(I, m_NSWSub(m_Value(LHS), m_Value(RHS)))) { - if (programUndefinedIfPoison(I)) { - const SCEV *Key = SE->getAddExpr( - SE->getUnknown(LHS), SE->getNegativeSCEV(SE->getUnknown(RHS))); - DominatingSubs[Key].push_back(I); - } + if (programUndefinedIfPoison(I)) + DominatingSubs[{LHS, RHS}].push_back(I); } return false; } @@ -1407,13 +1397,12 @@ PreservedAnalyses SeparateConstOffsetFromGEPPass::run(Function &F, FunctionAnalysisManager &AM) { auto *DT = &AM.getResult(F); - auto *SE = &AM.getResult(F); auto *LI = &AM.getResult(F); auto *TLI = &AM.getResult(F); auto GetTTI = [&AM](Function &F) -> TargetTransformInfo & { return AM.getResult(F); }; - SeparateConstOffsetFromGEP Impl(DT, SE, LI, TLI, GetTTI, LowerGEP); + SeparateConstOffsetFromGEP Impl(DT, LI, TLI, GetTTI, LowerGEP); if (!Impl.run(F)) return PreservedAnalyses::all(); PreservedAnalyses PA; Index: llvm/test/CodeGen/AMDGPU/llc-pipeline.ll =================================================================== --- llvm/test/CodeGen/AMDGPU/llc-pipeline.ll +++ llvm/test/CodeGen/AMDGPU/llc-pipeline.ll @@ -468,7 +468,6 @@ ; GCN-O1-OPTS-NEXT: Dominator Tree Construction ; GCN-O1-OPTS-NEXT: SROA ; GCN-O1-OPTS-NEXT: Natural Loop Information -; GCN-O1-OPTS-NEXT: Scalar Evolution Analysis ; GCN-O1-OPTS-NEXT: Split GEPs to a variadic base and a constant offset for better CSE ; GCN-O1-OPTS-NEXT: Scalar Evolution Analysis ; GCN-O1-OPTS-NEXT: Straight line strength reduction @@ -767,7 +766,6 @@ ; GCN-O2-NEXT: Dominator Tree Construction ; GCN-O2-NEXT: SROA ; GCN-O2-NEXT: Natural Loop Information -; GCN-O2-NEXT: Scalar Evolution Analysis ; GCN-O2-NEXT: Split GEPs to a variadic base and a constant offset for better CSE ; GCN-O2-NEXT: Scalar Evolution Analysis ; GCN-O2-NEXT: Straight line strength reduction @@ -1076,7 +1074,6 @@ ; GCN-O3-NEXT: Dominator Tree Construction ; GCN-O3-NEXT: SROA ; GCN-O3-NEXT: Natural Loop Information -; GCN-O3-NEXT: Scalar Evolution Analysis ; GCN-O3-NEXT: Split GEPs to a variadic base and a constant offset for better CSE ; GCN-O3-NEXT: Scalar Evolution Analysis ; GCN-O3-NEXT: Straight line strength reduction Index: llvm/test/CodeGen/PowerPC/O3-pipeline.ll =================================================================== --- llvm/test/CodeGen/PowerPC/O3-pipeline.ll +++ llvm/test/CodeGen/PowerPC/O3-pipeline.ll @@ -27,7 +27,6 @@ ; CHECK-NEXT: FunctionPass Manager ; CHECK-NEXT: Dominator Tree Construction ; CHECK-NEXT: Natural Loop Information -; CHECK-NEXT: Scalar Evolution Analysis ; CHECK-NEXT: Split GEPs to a variadic base and a constant offset for better CSE ; CHECK-NEXT: Early CSE ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)