[PATCH] D126896: [DAGCombiner][RISCV] Improve computeKnownBits for (smax X, C) where C is non-negative.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 2 10:12:31 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: spatel, RKSimon, nikic, efriedma.
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.
craig.topper requested review of this revision.
Herald added subscribers: pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

If C is non-negative, the result of the smax must also be
non-negative, so all sign bits of the result are 0.

This allows DAGCombiner to remove a zext_inreg in the modified test.
This zext_inreg started as a sext that became zext before type
legalization then was promoted to a zext_inreg.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126896

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
@@ -621,39 +621,20 @@
 }
 
 define signext i32 @smax_i32_pos_constant(i32 signext %a) {
-; RV32I-LABEL: smax_i32_pos_constant:
-; RV32I:       # %bb.0:
-; RV32I-NEXT:    li a1, 10
-; RV32I-NEXT:    blt a1, a0, .LBB24_2
-; RV32I-NEXT:  # %bb.1:
-; RV32I-NEXT:    li a0, 10
-; RV32I-NEXT:  .LBB24_2:
-; RV32I-NEXT:    ret
-;
-; RV64I-LABEL: smax_i32_pos_constant:
-; RV64I:       # %bb.0:
-; RV64I-NEXT:    li a1, 10
-; RV64I-NEXT:    blt a1, a0, .LBB24_2
-; RV64I-NEXT:  # %bb.1:
-; RV64I-NEXT:    li a0, 10
-; RV64I-NEXT:  .LBB24_2:
-; RV64I-NEXT:    slli a0, a0, 32
-; RV64I-NEXT:    srli a0, a0, 32
-; RV64I-NEXT:    ret
-;
-; RV32ZBB-LABEL: smax_i32_pos_constant:
-; RV32ZBB:       # %bb.0:
-; RV32ZBB-NEXT:    li a1, 10
-; RV32ZBB-NEXT:    max a0, a0, a1
-; RV32ZBB-NEXT:    ret
+; NOZBB-LABEL: smax_i32_pos_constant:
+; NOZBB:       # %bb.0:
+; NOZBB-NEXT:    li a1, 10
+; NOZBB-NEXT:    blt a1, a0, .LBB24_2
+; NOZBB-NEXT:  # %bb.1:
+; NOZBB-NEXT:    li a0, 10
+; NOZBB-NEXT:  .LBB24_2:
+; NOZBB-NEXT:    ret
 ;
-; RV64ZBB-LABEL: smax_i32_pos_constant:
-; RV64ZBB:       # %bb.0:
-; RV64ZBB-NEXT:    li a1, 10
-; RV64ZBB-NEXT:    max a0, a0, a1
-; RV64ZBB-NEXT:    slli a0, a0, 32
-; RV64ZBB-NEXT:    srli a0, a0, 32
-; RV64ZBB-NEXT:    ret
+; ZBB-LABEL: smax_i32_pos_constant:
+; ZBB:       # %bb.0:
+; ZBB-NEXT:    li a1, 10
+; ZBB-NEXT:    max a0, a0, a1
+; ZBB-NEXT:    ret
   %c = call i32 @llvm.smax.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
@@ -3697,6 +3697,16 @@
         }
       }
     }
+    // For SMAX, if CstLow is non-negative we know the result will be
+    // non-negative and thus all sign bits are 0.
+    if (IsMax && CstLow) {
+      const APInt &ValueLow = CstLow->getAPIntValue();
+      if (ValueLow.isNonNegative()) {
+        unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
+        Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
+        break;
+      }
+    }
 
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126896.433789.patch
Type: text/x-patch
Size: 2497 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220602/76edb378/attachment-0001.bin>


More information about the llvm-commits mailing list