[llvm] d816499 - [KnownBits] Move SelectionDAG::computeKnownBits ISD::ABS handling to KnownBits::abs

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 9 05:25:50 PDT 2020


Author: Simon Pilgrim
Date: 2020-09-09T13:22:58+01:00
New Revision: d816499f95d673bbad297d0231cbeaf5efbbc5de

URL: https://github.com/llvm/llvm-project/commit/d816499f95d673bbad297d0231cbeaf5efbbc5de
DIFF: https://github.com/llvm/llvm-project/commit/d816499f95d673bbad297d0231cbeaf5efbbc5de.diff

LOG: [KnownBits] Move SelectionDAG::computeKnownBits ISD::ABS handling to KnownBits::abs

Move the ISD::ABS handling to a KnownBits::abs handler, to simplify future implementations in ValueTracking/GlobalISel.

Added: 
    

Modified: 
    llvm/include/llvm/Support/KnownBits.h
    llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
    llvm/lib/Support/KnownBits.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index a29e150b904a..8da6c7d98ba5 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -278,6 +278,9 @@ struct KnownBits {
   /// Update known bits based on XORing with RHS.
   KnownBits &operator^=(const KnownBits &RHS);
 
+  /// Compute known bits for the absolute value.
+  KnownBits abs() const;
+
   KnownBits byteSwap() {
     return KnownBits(Zero.byteSwap(), One.byteSwap());
   }

diff  --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 2350248626c7..1cc2ec77ebce 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -3370,21 +3370,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
   }
   case ISD::ABS: {
     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
-
-    // If the source's MSB is zero then we know the rest of the bits already.
-    if (Known2.isNonNegative()) {
-      Known.Zero = Known2.Zero;
-      Known.One = Known2.One;
-      break;
-    }
-
-    // We only know that the absolute values's MSB will be zero iff there is
-    // a set bit that isn't the sign bit (otherwise it could be INT_MIN).
-    Known2.One.clearSignBit();
-    if (Known2.One.getBoolValue()) {
-      Known.Zero = APInt::getSignMask(BitWidth);
-      break;
-    }
+    Known = Known2.abs();
     break;
   }
   case ISD::UMIN: {

diff  --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 03843687c10a..ed32a80a061d 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -145,6 +145,24 @@ KnownBits KnownBits::smin(const KnownBits &LHS, const KnownBits &RHS) {
   return Flip(umax(Flip(LHS), Flip(RHS)));
 }
 
+KnownBits KnownBits::abs() const {
+  // If the source's MSB is zero then we know the rest of the bits already.
+  if (isNonNegative())
+    return *this;
+
+  // Assume we know nothing.
+  KnownBits KnownAbs(getBitWidth());
+
+  // We only know that the absolute values's MSB will be zero iff there is
+  // a set bit that isn't the sign bit (otherwise it could be INT_MIN).
+  APInt Val = One;
+  Val.clearSignBit();
+  if (!Val.isNullValue())
+    KnownAbs.Zero.setSignBit();
+
+  return KnownAbs;
+}
+
 KnownBits &KnownBits::operator&=(const KnownBits &RHS) {
   // Result bit is 0 if either operand bit is 0.
   Zero |= RHS.Zero;


        


More information about the llvm-commits mailing list