[KnownBits] Return `0` for poison {s,u}div inputs
authorNoah Goldstein <goldstein.w.n@gmail.com>
Thu, 25 May 2023 17:10:31 +0000 (12:10 -0500)
committerNoah Goldstein <goldstein.w.n@gmail.com>
Tue, 6 Jun 2023 20:14:10 +0000 (15:14 -0500)
It seems consistent to always return zero for known poison rather than
varying the value. We do the same elsewhere.

Differential Revision: https://reviews.llvm.org/D150922

llvm/lib/Support/KnownBits.cpp
llvm/test/CodeGen/WebAssembly/pr59626.ll

index 8bc65628e3a2dc5731bac94e47e3e27c5d60c43e..ce2813e2fb3a3c5191773132991c366deda3e775 100644 (file)
@@ -758,6 +758,13 @@ KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
   assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
   KnownBits Known(BitWidth);
 
+  if (LHS.isZero() || RHS.isZero()) {
+    // Result is either known Zero or UB. Return Zero either way.
+    // Checking this earlier saves us a lot of special cases later on.
+    Known.setAllZero();
+    return Known;
+  }
+
   std::optional<APInt> Res;
   if (LHS.isNegative() && RHS.isNegative()) {
     // Result non-negative.
@@ -819,6 +826,13 @@ KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS,
   assert(!LHS.hasConflict() && !RHS.hasConflict());
   KnownBits Known(BitWidth);
 
+  if (LHS.isZero() || RHS.isZero()) {
+    // Result is either known Zero or UB. Return Zero either way.
+    // Checking this earlier saves us a lot of special cases later on.
+    Known.setAllZero();
+    return Known;
+  }
+
   // We can figure out the minimum number of upper zero bits by doing
   // MaxNumerator / MinDenominator. If the Numerator gets smaller or Denominator
   // gets larger, the number of upper zero bits increases.
index a0c4ba7d675984d2d94bce1db52d3ad7e99a4c72..39941dd1024a498da6c746abaf590d723376df75 100644 (file)
@@ -13,6 +13,9 @@ define i8 @f(ptr %0, ptr %1) {
 ; CHECK-32-NEXT:    i32.const 0
 ; CHECK-32-NEXT:    i32.store16 0
 ; CHECK-32-NEXT:    local.get 1
+; CHECK-32-NEXT:    i32.const 0
+; CHECK-32-NEXT:    i32.store8 2
+; CHECK-32-NEXT:    local.get 1
 ; CHECK-32-NEXT:    local.get 0
 ; CHECK-32-NEXT:    i8x16.splat
 ; CHECK-32-NEXT:    v128.store16_lane 0, 0