[PATCH] D127080: [DAGCombiner][RISCV] Improve computeKnownBits for (smin X, C) where C is negative.

Pretty-box via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 5 20:22:21 PDT 2022


Pretty-box created this revision.
Pretty-box added reviewers: benshi001, craig.topper.
Herald added subscribers: sunshaoce, VincentWu, luke957, foad, StephenFan, vkmr, frasercrmck, evandro, luismarques, apazos, sameer.abuasal, steven.zhang, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, hiraditya, arichardson.
Herald added a project: All.
Pretty-box requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

[DAGCombiner][RISCV] Improve computeKnownBits for (smin X, C) where C is negative.

If C is negative, the result of the smin must also be
negative, so all sign bits of the result are 1.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127080

Files:
  llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
  llvm/test/CodeGen/RISCV/min-max.ll


Index: llvm/test/CodeGen/RISCV/min-max.ll
===================================================================
--- llvm/test/CodeGen/RISCV/min-max.ll
+++ llvm/test/CodeGen/RISCV/min-max.ll
@@ -638,3 +638,22 @@
   %c = call i32 @llvm.smax.i32(i32 %a, i32 10)
   ret i32 %c
 }
+
+define signext i32 @smin_i32_pos_constant(i32 signext %a) {
+; NOZBB-LABEL: smin_i32_pos_constant:
+; NOZBB:       # %bb.0:
+; NOZBB-NEXT:    li a1, -10
+; NOZBB-NEXT:    blt a0, a1, .LBB25_2
+; NOZBB-NEXT:  # %bb.1:
+; NOZBB-NEXT:    li a0, -10
+; NOZBB-NEXT:  .LBB25_2:
+; NOZBB-NEXT:    ret
+;
+; ZBB-LABEL: smin_i32_pos_constant:
+; ZBB:       # %bb.0:
+; ZBB-NEXT:    li a1, -10
+; ZBB-NEXT:    min a0, a0, a1
+; ZBB-NEXT:    ret
+  %c = call i32 @llvm.smin.i32(i32 %a, i32 -10)
+  ret i32 %c
+}
Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -3699,8 +3699,8 @@
     }
     // For SMAX, if CstLow is non-negative we know the result will be
     // non-negative and thus all sign bits are 0.
-    // TODO: There's an equivalent of this for smin with negative constant for
-    // known ones.
+    // For SMIN, if CstHigh is negative we know the result will be
+    // negative and thus all sign bits are 1.
     if (IsMax && CstLow) {
       const APInt &ValueLow = CstLow->getAPIntValue();
       if (ValueLow.isNonNegative()) {
@@ -3710,6 +3710,15 @@
       }
     }
 
+    if (!IsMax && CstHigh) {
+      const APInt &ValueHigh = CstHigh->getAPIntValue();
+      if (ValueHigh.isNegative()) {
+        unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
+        Known.One.setHighBits(std::min(SignBits, ValueHigh.getNumSignBits()));
+        break;
+      }
+    }
+
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
     if (IsMax)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127080.434378.patch
Type: text/x-patch
Size: 2014 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220606/c848575c/attachment.bin>


More information about the llvm-commits mailing list