[PATCH] D127335: [SelectionDAG] Teach computeKnownBits that a nsw self multiply produce a positive value.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 8 12:29:30 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: spatel, RKSimon, nikic.
Herald added subscribers: luke957, foad, StephenFan, frasercrmck, ecnelises, luismarques, apazos, sameer.abuasal, s.egerton, dmgreen, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, 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, MaskRay.
Herald added a project: LLVM.

This matches what we do in IR. For the RISC-V test case, this allows
us to use -8 for the AND mask instead of materializing a constant in a register.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127335

Files:
  llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
  llvm/test/CodeGen/RISCV/rv64i-demanded-bits.ll
  llvm/test/CodeGen/Thumb2/mve-vqdmulh-minmax.ll


Index: llvm/test/CodeGen/Thumb2/mve-vqdmulh-minmax.ll
===================================================================
--- llvm/test/CodeGen/Thumb2/mve-vqdmulh-minmax.ll
+++ llvm/test/CodeGen/Thumb2/mve-vqdmulh-minmax.ll
@@ -486,10 +486,10 @@
 ; CHECK:       @ %bb.0:
 ; CHECK-NEXT:    smulbb r1, r0, r0
 ; CHECK-NEXT:    movs r0, #127
-; CHECK-NEXT:    asrs r2, r1, #7
+; CHECK-NEXT:    lsrs r2, r1, #7
 ; CHECK-NEXT:    cmp r2, #127
 ; CHECK-NEXT:    it lt
-; CHECK-NEXT:    asrlt r0, r1, #7
+; CHECK-NEXT:    lsrlt r0, r1, #7
 ; CHECK-NEXT:    bx lr
   %e = sext i16 %a to i32
   %d = mul nsw i32 %e, %e
Index: llvm/test/CodeGen/RISCV/rv64i-demanded-bits.ll
===================================================================
--- llvm/test/CodeGen/RISCV/rv64i-demanded-bits.ll
+++ llvm/test/CodeGen/RISCV/rv64i-demanded-bits.ll
@@ -24,3 +24,16 @@
   %g = shl i32 %f, %y
   ret i32 %g
 }
+
+; The sign bit of an nsw self multiply is 0. Make sure we can use this to
+; conver the AND constant to -8.
+define i64 @mul_self_nsw_sign(i64 %x) {
+; CHECK-LABEL: mul_self_nsw_sign:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    mul a0, a0, a0
+; CHECK-NEXT:    andi a0, a0, -8
+; CHECK-NEXT:    ret
+  %a = mul nsw i64 %x, %x
+  %b = and i64 %a, 9223372036854775800
+  ret i64 %b
+}
Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -3173,6 +3173,14 @@
       SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
           Op.getOperand(0), DemandedElts, false, Depth + 1);
     Known = KnownBits::mul(Known, Known2, SelfMultiply);
+
+    // If the multiplication is known not to overflow, the product of a number
+    // with itself is non-negative. Only do this if we didn't already compute
+    // the opposite value for the sign bit.
+    if (Op->getFlags().hasNoSignedWrap() &&
+        Op.getOperand(0) == Op.getOperand(1) &&
+        !Known.isNegative())
+      Known.makeNonNegative();
     break;
   }
   case ISD::MULHU: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127335.435291.patch
Type: text/x-patch
Size: 2103 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220608/89d7a984/attachment.bin>


More information about the llvm-commits mailing list