[PATCH] D126043: [LSR] Drop LSR solution if it is less profitable than baseline

Yueh-Ting (eop) Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 28 11:03:02 PDT 2022


eopXD updated this revision to Diff 463625.
eopXD added a comment.

Rebase to latest main.

Don't include baseline cost when SCEV is not used inside the loop. (`isUseFullyOutsideLoop(L) == false`)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126043/new/

https://reviews.llvm.org/D126043

Files:
  llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp


Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -1975,6 +1975,10 @@
   /// SmallDenseSet.
   SetVector<int64_t, SmallVector<int64_t, 8>, SmallSet<int64_t, 8>> 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<Type *, 4> Types;
 
@@ -3294,6 +3298,10 @@
   BranchInst *ExitBranch = nullptr;
   bool SaveCmp = TTI.canSaveCmp(L, &ExitBranch, &SE, &LI, &DT, &AC, &TLI);
 
+  // For calculating InitialSolutionCost
+  SmallPtrSet<const SCEV *, 16> Regs;
+  DenseSet<const SCEV *> 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<Cost &>(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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126043.463625.patch
Type: text/x-patch
Size: 2671 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220928/75b371c7/attachment.bin>


More information about the llvm-commits mailing list