Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -1011,11 +1011,13 @@ ScalarEvolution *SE = nullptr; const TargetTransformInfo *TTI = nullptr; TargetTransformInfo::LSRCost C; + TTI::AddressingModeKind AMK = TTI::AMK_None; public: Cost() = delete; - Cost(const Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI) : - L(L), SE(&SE), TTI(&TTI) { + Cost(const Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, + TTI::AddressingModeKind AMK) : + L(L), SE(&SE), TTI(&TTI), AMK(AMK) { C.Insns = 0; C.NumRegs = 0; C.AddRecCost = 0; @@ -1227,8 +1229,6 @@ /// Tally up interesting quantities from the given register. void Cost::RateRegister(const Formula &F, const SCEV *Reg, SmallPtrSetImpl &Regs) { - TTI::AddressingModeKind AMK = TTI->getPreferredAddressingMode(L, SE); - if (const SCEVAddRecExpr *AR = dyn_cast(Reg)) { // If this is an addrec for another loop, it should be an invariant // with respect to L since L is the innermost loop (at least @@ -1908,7 +1908,7 @@ const TargetTransformInfo &TTI; Loop *const L; MemorySSAUpdater *MSSAU; - bool FavorBackedgeIndex = false; + TTI::AddressingModeKind AMK; bool Changed = false; /// This is the insert position that the current loop's induction variable @@ -3575,7 +3575,6 @@ // may generate a post-increment operator. The reason is that the // reassociations cause extra base+register formula to be created, // and possibly chosen, but the post-increment is more efficient. - TTI::AddressingModeKind AMK = TTI.getPreferredAddressingMode(L, &SE); if (AMK == TTI::AMK_PostIndexed && mayUsePostIncMode(TTI, LU, BaseReg, L, SE)) return; SmallVector AddOps; @@ -3811,7 +3810,9 @@ // means that a single pre-indexed access can be generated to become the new // base pointer for each iteration of the loop, resulting in no extra add/sub // instructions for pointer updating. - if (FavorBackedgeIndex && LU.Kind == LSRUse::Address) { + bool FavorPreIndexed = EnableBackedgeIndexing && + AMK == TTI::AMK_PreIndexed; + if (FavorPreIndexed && LU.Kind == LSRUse::Address) { if (auto *GAR = dyn_cast(G)) { if (auto *StepRec = dyn_cast(GAR->getStepRecurrence(SE))) { @@ -4240,8 +4241,7 @@ NewF.BaseOffset = (uint64_t)NewF.BaseOffset + Imm; if (!isLegalUse(TTI, LU.MinOffset, LU.MaxOffset, LU.Kind, LU.AccessTy, NewF)) { - if (TTI.getPreferredAddressingMode(this->L, &SE) == - TTI::AMK_PostIndexed && + if (AMK == TTI::AMK_PostIndexed && mayUsePostIncMode(TTI, LU, OrigReg, this->L, SE)) continue; if (!TTI.isLegalAddImmediate((uint64_t)NewF.UnfoldedOffset + Imm)) @@ -4344,7 +4344,7 @@ // avoids the need to recompute this information across formulae using the // same bad AddRec. Passing LoserRegs is also essential unless we remove // the corresponding bad register from the Regs set. - Cost CostF(L, SE, TTI); + Cost CostF(L, SE, TTI, AMK); Regs.clear(); CostF.RateFormula(F, Regs, VisitedRegs, LU, &LoserRegs); if (CostF.isLoser()) { @@ -4377,7 +4377,7 @@ Formula &Best = LU.Formulae[P.first->second]; - Cost CostBest(L, SE, TTI); + Cost CostBest(L, SE, TTI, AMK); Regs.clear(); CostBest.RateFormula(Best, Regs, VisitedRegs, LU); if (CostF.isLess(CostBest)) @@ -4630,8 +4630,8 @@ // If the new register numbers are the same, choose the Formula with // less Cost. - Cost CostFA(L, SE, TTI); - Cost CostFB(L, SE, TTI); + Cost CostFA(L, SE, TTI, AMK); + Cost CostFB(L, SE, TTI, AMK); Regs.clear(); CostFA.RateFormula(FA, Regs, VisitedRegs, LU); Regs.clear(); @@ -4681,7 +4681,7 @@ /// If we are over the complexity limit, filter out any post-inc prefering /// variables to only post-inc values. void LSRInstance::NarrowSearchSpaceByFilterPostInc() { - if (TTI.getPreferredAddressingMode(L, &SE) != TTI::AMK_PostIndexed) + if (AMK != TTI::AMK_PostIndexed) return; if (EstimateSearchSpaceComplexity() < ComplexityLimit) return; @@ -4972,7 +4972,7 @@ ReqRegs.insert(S); SmallPtrSet NewRegs; - Cost NewCost(L, SE, TTI); + Cost NewCost(L, SE, TTI, AMK); for (const Formula &F : LU.Formulae) { // Ignore formulae which may not be ideal in terms of register reuse of // ReqRegs. The formula should use all required registers before @@ -4980,8 +4980,7 @@ // This can sometimes (notably when trying to favour postinc) lead to // sub-optimial decisions. There it is best left to the cost modelling to // get correct. - if (TTI.getPreferredAddressingMode(L, &SE) != TTI::AMK_PostIndexed || - LU.Kind != LSRUse::Address) { + if (AMK != TTI::AMK_PostIndexed || LU.Kind != LSRUse::Address) { int NumReqRegsToFind = std::min(F.getNumRegs(), ReqRegs.size()); for (const SCEV *Reg : ReqRegs) { if ((F.ScaledReg && F.ScaledReg == Reg) || @@ -5029,9 +5028,9 @@ /// vector. void LSRInstance::Solve(SmallVectorImpl &Solution) const { SmallVector Workspace; - Cost SolutionCost(L, SE, TTI); + Cost SolutionCost(L, SE, TTI, AMK); SolutionCost.Lose(); - Cost CurCost(L, SE, TTI); + Cost CurCost(L, SE, TTI, AMK); SmallPtrSet CurRegs; DenseSet VisitedRegs; Workspace.reserve(Uses.size()); @@ -5562,9 +5561,7 @@ const TargetTransformInfo &TTI, AssumptionCache &AC, TargetLibraryInfo &TLI, MemorySSAUpdater *MSSAU) : IU(IU), SE(SE), DT(DT), LI(LI), AC(AC), TLI(TLI), TTI(TTI), L(L), - MSSAU(MSSAU), FavorBackedgeIndex(EnableBackedgeIndexing && - TTI.getPreferredAddressingMode(L, &SE) == - TTI::AMK_PreIndexed) { + MSSAU(MSSAU), AMK(TTI.getPreferredAddressingMode(L, &SE)) { // If LoopSimplify form is not available, stay out of trouble. if (!L->isLoopSimplifyForm()) return;