[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 14:56:20 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4bcfc418464b: [SelectionDAG] Teach computeKnownBits that a nsw self multiply produce a… (authored by craig.topper).

Changed prior to commit:
  https://reviews.llvm.org/D127335?vs=435291&id=435348#toc

Repository:
  rG LLVM Github Monorepo

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

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
+; convert 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 computed
+    // 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.435348.patch
Type: text/x-patch
Size: 2105 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220608/040e9546/attachment.bin>


More information about the llvm-commits mailing list