[PATCH] D105209: [SCEVExpander] Discount cost of umin(1, x) expressions

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 12 08:44:40 PDT 2021


reames updated this revision to Diff 357955.
reames retitled this revision from "[WIP][SCEVExpander] Discount cost of umin(x,1) expressions" to "[SCEVExpander] Discount cost of umin(1, x) expressions".
reames edited the summary of this revision.
reames added reviewers: efriedma, fhahn.
reames set the repository for this revision to rG LLVM Github Monorepo.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D105209

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


Index: llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
===================================================================
--- llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -1819,6 +1819,9 @@
 }
 
 Value *SCEVExpander::visitUMinExpr(const SCEVUMinExpr *S) {
+  // For the umin(1,X1...Xn) pattern described in the cost modeling, we let
+  // instcombine do the actual transform, relying on the order of SCEV
+  // canonicalization to put the constant in the outermost umin.
   Value *LHS = expand(S->getOperand(S->getNumOperands() - 1));
   Type *Ty = LHS->getType();
   for (int i = S->getNumOperands() - 2; i >= 0; --i) {
@@ -2312,10 +2315,21 @@
     // see SCEVExpander::visitMulExpr(), ExpandOpBinPowN().
     Cost = ArithCost(Instruction::Mul, S->getNumOperands() - 1);
     break;
+  case scUMinExpr:
+    // For umin(1, x2, ..., xn), the form we generate will be converted by
+    // instcombine to: zext(icmp ne umin(x1..xn), 0).  This saves one select.
+    if (S->getOperand(0)->getType()->isIntegerTy() &&
+        S->getOperand(0)->isOne()) {
+      Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 1);
+      if (S->getNumOperands() > 2)
+        Cost += CmpSelCost(Instruction::Select, S->getNumOperands() - 2, 0, 2);
+      Cost += CastCost(Instruction::ZExt);
+      break;
+    }
+    LLVM_FALLTHROUGH;
   case scSMaxExpr:
   case scUMaxExpr:
-  case scSMinExpr:
-  case scUMinExpr: {
+  case scSMinExpr: {
     // FIXME: should this ask the cost for Intrinsic's?
     Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 1);
     Cost += CmpSelCost(Instruction::Select, S->getNumOperands() - 1, 0, 2);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105209.357955.patch
Type: text/x-patch
Size: 1724 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210712/6aec4ec9/attachment.bin>


More information about the llvm-commits mailing list