[PATCH] D86346: [SimplifyCFG] Accumulate cost against budget

Sam Parker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 24 03:31:46 PDT 2020


samparker updated this revision to Diff 287331.
samparker added a comment.

Rebased after reorganising the two parts of code that evaluate phis. The budget now allows a basic cost for each phi within the block, plus a basic cost for the one instruction that we may speculate.


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

https://reviews.llvm.org/D86346

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp


Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -86,14 +86,13 @@
 
 #define DEBUG_TYPE "simplifycfg"
 
-// Chosen as 2 so as to be cheap, but still to have enough power to fold
-// a select, so the "clamp" idiom (of a min followed by a max) will be caught.
-// To catch this, we need to fold a compare and a select, hence '2' being the
-// minimum reasonable default.
+/// The cost of converting each phi to a select, with the total cost allowed
+/// for the block is this value multiplied by the total number of phis, plus
+/// one to allow the speculation of a single instruction.
 static cl::opt<unsigned> PHINodeFoldingThreshold(
-    "phi-node-folding-threshold", cl::Hidden, cl::init(2),
+    "phi-node-folding-threshold", cl::Hidden, cl::init(1),
     cl::desc(
-        "Control the amount of phi node folding to perform (default = 2)"));
+        "Control the amount of phi node folding to perform (default = 1)"));
 
 static cl::opt<unsigned> TwoEntryPHINodeFoldingThreshold(
     "two-entry-phi-node-folding-threshold", cl::Hidden, cl::init(4),
@@ -2037,10 +2036,7 @@
       return false;
     unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0;
     unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0;
-    unsigned MaxCost =
-        2 * PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
-    if (OrigCost + ThenCost > MaxCost)
-      return false;
+    BudgetRemaining -= OrigCost + ThenCost;
 
     // Account for the cost of an unfolded ConstantExpr which could end up
     // getting expanded into Instructions.
@@ -2100,8 +2096,12 @@
 
   BasicBlock *BB = BI->getParent();
   BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0);
-  int BudgetRemaining =
-    PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
+
+  // Enable the cost of speculating a single basic instruction plus a basic cost
+  // for each phi in the block.
+  unsigned NumPHIs = std::distance(EndBB->phis().begin(), EndBB->phis().end());
+  int BudgetRemaining = TargetTransformInfo::TCC_Basic +
+    (TargetTransformInfo::TCC_Basic * NumPHIs * PHINodeFoldingThreshold);
 
   // If ThenBB is actually on the false edge of the conditional branch, remember
   // to swap the select operands later.
@@ -2145,10 +2145,11 @@
         !(HoistCondStores && (SpeculatedStoreValue = isSafeToSpeculateStore(
                                   I, BB, ThenBB, EndBB))))
       return false;
-    if (!SpeculatedStoreValue &&
-        ComputeSpeculationCost(I, TTI) >
-            PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic)
-      return false;
+    if (!SpeculatedStoreValue) {
+      BudgetRemaining -= ComputeSpeculationCost(I, TTI);
+      if (BudgetRemaining < 0)
+        return false;
+    }
 
     // Store the store speculation candidate.
     if (SpeculatedStoreValue)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86346.287331.patch
Type: text/x-patch
Size: 2993 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200824/88d17670/attachment.bin>


More information about the llvm-commits mailing list