[PATCH] D73716: [SCEV] SCEVExpander::isHighCostExpansionHelper(): begin cost modelling - model cast cost

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 30 08:23:16 PST 2020


lebedev.ri created this revision.
lebedev.ri added reviewers: reames, mkazantsev, wmi, sanjoy.
lebedev.ri added a project: LLVM.
Herald added subscribers: javed.absar, hiraditya.
lebedev.ri added a parent revision: D73712: [SCEV] SCEVExpander::isHighCostExpansionHelper(): bailout if no TTI is present.

This is not a NFC, although it does not change any of the existing tests.
I'm not really sure if we should have specific tests for the cost modelling itself.

This is the first patch that actually makes `SCEVExpander::isHighCostExpansionHelper()`
account for the cost of the SCEV expression, and consider the budget available,
by modelling cast expressions.

I believe the logic itself is "pretty obviously correct" - from budget,
we need to subtract the cost of the cast expression from inner type `Op->getType()`
to the `S->getType()` type, and recurse into the expression we are casting.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73716

Files:
  llvm/include/llvm/Analysis/ScalarEvolutionExpander.h
  llvm/lib/Analysis/ScalarEvolutionExpander.cpp


Index: llvm/lib/Analysis/ScalarEvolutionExpander.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolutionExpander.cpp
+++ llvm/lib/Analysis/ScalarEvolutionExpander.cpp
@@ -2138,6 +2138,9 @@
 bool SCEVExpander::isHighCostExpansionHelper(
     const SCEV *S, Loop *L, const Instruction *At, int &BudgetRemaining,
     const TargetTransformInfo *TTI, SmallPtrSetImpl<const SCEV *> &Processed) {
+  if (BudgetRemaining < 0)
+    return true; // Already run out of budget, give up.
+
   // Was the cost of expansion of this expression already accounted for?
   if (!Processed.insert(S).second)
     return false; // We have already accounted for this expression.
@@ -2157,17 +2160,26 @@
   if (!TTI)
     return true; // No cost model - give up.
 
-  // Zero/One operand expressions
-  switch (S->getSCEVType()) {
-  case scTruncate:
-    return isHighCostExpansionHelper(cast<SCEVTruncateExpr>(S)->getOperand(), L,
-                                     At, BudgetRemaining, TTI, Processed);
-  case scZeroExtend:
-    return isHighCostExpansionHelper(cast<SCEVZeroExtendExpr>(S)->getOperand(),
-                                     L, At, BudgetRemaining, TTI, Processed);
-  case scSignExtend:
-    return isHighCostExpansionHelper(cast<SCEVSignExtendExpr>(S)->getOperand(),
-                                     L, At, BudgetRemaining, TTI, Processed);
+  if (auto *CastExpr = dyn_cast<SCEVCastExpr>(S)) {
+    unsigned Opcode;
+    switch (S->getSCEVType()) {
+    case scTruncate:
+      Opcode = Instruction::Trunc;
+      break;
+    case scZeroExtend:
+      Opcode = Instruction::ZExt;
+      break;
+    case scSignExtend:
+      Opcode = Instruction::SExt;
+      break;
+    default:
+      llvm_unreachable("There are no other cast types.");
+    }
+    const SCEV *Op = CastExpr->getOperand();
+    BudgetRemaining -=
+        TTI->getOperationCost(Opcode, S->getType(), Op->getType());
+    return isHighCostExpansionHelper(Op, L, At, BudgetRemaining, TTI,
+                                     Processed);
   }
 
   if (auto *UDivExpr = dyn_cast<SCEVUDivExpr>(S)) {
Index: llvm/include/llvm/Analysis/ScalarEvolutionExpander.h
===================================================================
--- llvm/include/llvm/Analysis/ScalarEvolutionExpander.h
+++ llvm/include/llvm/Analysis/ScalarEvolutionExpander.h
@@ -173,8 +173,8 @@
       ChainedPhis.clear();
     }
 
-    /// Return true for expressions that may incur non-trivial cost to evaluate
-    /// at runtime.
+    /// Return true for expressions that can't be evaluate at runtime
+    /// within given \b Budged.
     ///
     /// At is an optional parameter which specifies point in code where user is
     /// going to expand this expression. Sometimes this knowledge can lead to a
@@ -184,8 +184,10 @@
                              const Instruction *At = nullptr) {
       SmallPtrSet<const SCEV *, 8> Processed;
       int BudgetRemaining = Budget * TargetTransformInfo::TCC_Basic;
-      return isHighCostExpansionHelper(Expr, L, At, BudgetRemaining, TTI,
-                                       Processed);
+      if (isHighCostExpansionHelper(Expr, L, At, BudgetRemaining, TTI,
+                                    Processed))
+        return true;
+      return BudgetRemaining < 0;
     }
 
     /// This method returns the canonical induction variable of the specified


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73716.241469.patch
Type: text/x-patch
Size: 3402 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200130/3651f01c/attachment.bin>


More information about the llvm-commits mailing list