[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
Thu May 19 23:34:53 PDT 2022


eopXD created this revision.
eopXD added a reviewer: Meinersbur.
Herald added a subscriber: hiraditya.
Herald added a project: All.
eopXD requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

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
@@ -1964,6 +1964,10 @@
   /// SmallDenseSet.
   SetVector<int64_t, SmallVector<int64_t, 8>, SmallSet<int64_t, 8>> Factors;
 
+  /// Baseline costfor 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;
 
@@ -3288,6 +3292,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.
@@ -3358,6 +3366,12 @@
     int64_t Offset = P.second;
     LSRUse &LU = Uses[LUIdx];
 
+    // Create initial SCEV as Formula for baseline cost
+    Formula F;
+    F.initialMatch(S, L, SE);
+    if (!BaselineCost.isLoser())
+      BaselineCost.RateFormula(F, Regs, VisitedRegs, LU);
+
     // Record the fixup.
     LSRFixup &LF = LU.getNewFixup();
     LF.UserInst = UserInst;
@@ -5141,6 +5155,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
@@ -5715,8 +5741,10 @@
                          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), AMK(PreferredAddresingMode.getNumOccurrences() > 0 ?
-        PreferredAddresingMode : TTI.getPreferredAddressingMode(L, &SE)) {
+      MSSAU(MSSAU), AMK(PreferredAddresingMode.getNumOccurrences() > 0
+                            ? PreferredAddresingMode
+                            : TTI.getPreferredAddressingMode(L, &SE)),
+      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.430887.patch
Type: text/x-patch
Size: 2749 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220520/3a05f6c1/attachment.bin>


More information about the llvm-commits mailing list