diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp --- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -1975,6 +1975,10 @@ /// SmallDenseSet. SetVector, SmallSet> Factors; + /// Baseline cost for currently used SCEV. Drop the best solution by LSR if + /// the solution is not profitable. + Cost BaselineCost; + /// Interesting use types, to facilitate truncation reuse. SmallSetVector Types; @@ -3294,6 +3298,10 @@ BranchInst *ExitBranch = nullptr; bool SaveCmp = TTI.canSaveCmp(L, &ExitBranch, &SE, &LI, &DT, &AC, &TLI); + // For calculating InitialSolutionCost + SmallPtrSet Regs; + DenseSet VisitedRegs; + for (const IVStrideUse &U : IU) { Instruction *UserInst = U.getUser(); // Skip IV users that are part of profitable IV Chains. @@ -3387,6 +3395,14 @@ LF.Offset = Offset; LU.AllFixupsOutsideLoop &= LF.isUseFullyOutsideLoop(L); + // Create initial SCEV as Formula for baseline cost + if (!LF.isUseFullyOutsideLoop(L)) { + Formula F; + F.initialMatch(S, L, SE); + if (!BaselineCost.isLoser()) + BaselineCost.RateFormula(F, Regs, VisitedRegs, LU); + } + if (!LU.WidestFixupType || SE.getTypeSizeInBits(LU.WidestFixupType) < SE.getTypeSizeInBits(LF.OperandValToReplace->getType())) @@ -5162,6 +5178,18 @@ }); assert(Solution.size() == Uses.size() && "Malformed solution!"); + + if (!SolutionCost.isLess(const_cast(BaselineCost))) { + LLVM_DEBUG(dbgs() << "\n" + "The baseline solution requires "; + BaselineCost.print(dbgs()); dbgs() << "\n";); + + LLVM_DEBUG( + dbgs() + << "Baseline solution is more profitable than chosen solution.\n"); + LLVM_DEBUG(dbgs() << "Dropping LSR chosen solution.\n"); + Solution.clear(); + } } /// Helper for AdjustInsertPositionForExpand. Climb up the dominator tree far as @@ -5706,7 +5734,8 @@ MSSAU(MSSAU), AMK(PreferredAddresingMode.getNumOccurrences() > 0 ? PreferredAddresingMode : TTI.getPreferredAddressingMode(L, &SE)), - Rewriter(SE, L->getHeader()->getModule()->getDataLayout(), "lsr", false) { + Rewriter(SE, L->getHeader()->getModule()->getDataLayout(), "lsr", false), + BaselineCost(L, SE, TTI, AMK) { // If LoopSimplify form is not available, stay out of trouble. if (!L->isLoopSimplifyForm()) return;