]> granicus.if.org Git - llvm/commitdiff
[KnownBits][ValueTracking] Move the math for calculating known bits for add/sub into...
authorCraig Topper <craig.topper@intel.com>
Tue, 8 Aug 2017 16:29:35 +0000 (16:29 +0000)
committerCraig Topper <craig.topper@intel.com>
Tue, 8 Aug 2017 16:29:35 +0000 (16:29 +0000)
I want to reuse this code in SimplifyDemandedBits handling of Add/Sub. This will make that easier.

Wonder if we should use it in SelectionDAG's computeKnownBits too.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310378 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/KnownBits.h
lib/Analysis/ValueTracking.cpp
lib/Support/CMakeLists.txt
lib/Support/KnownBits.cpp [new file with mode: 0644]

index 5a02168794b33c021e6e4cd2022867e880712e1d..7a4de3e5ff12976164639a05f15ff5390f9dc8e1 100644 (file)
@@ -193,6 +193,10 @@ public:
   unsigned countMaxPopulation() const {
     return getBitWidth() - Zero.countPopulation();
   }
+
+  /// Compute known bits resulting from adding LHS and RHS.
+  static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS,
+                                    KnownBits RHS);
 };
 
 } // end namespace llvm
index 34568713a05d4ffe4fa9d7067870e30643d127c1..5372d9dc7c213e05dae27969bb20f274c9b0d693 100644 (file)
@@ -275,47 +275,7 @@ static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
   computeKnownBits(Op0, LHSKnown, Depth + 1, Q);
   computeKnownBits(Op1, Known2, Depth + 1, Q);
 
-  // Carry in a 1 for a subtract, rather than a 0.
-  uint64_t CarryIn = 0;
-  if (!Add) {
-    // Sum = LHS + ~RHS + 1
-    std::swap(Known2.Zero, Known2.One);
-    CarryIn = 1;
-  }
-
-  APInt PossibleSumZero = ~LHSKnown.Zero + ~Known2.Zero + CarryIn;
-  APInt PossibleSumOne = LHSKnown.One + Known2.One + CarryIn;
-
-  // Compute known bits of the carry.
-  APInt CarryKnownZero = ~(PossibleSumZero ^ LHSKnown.Zero ^ Known2.Zero);
-  APInt CarryKnownOne = PossibleSumOne ^ LHSKnown.One ^ Known2.One;
-
-  // Compute set of known bits (where all three relevant bits are known).
-  APInt LHSKnownUnion = LHSKnown.Zero | LHSKnown.One;
-  APInt RHSKnownUnion = Known2.Zero | Known2.One;
-  APInt CarryKnownUnion = CarryKnownZero | CarryKnownOne;
-  APInt Known = LHSKnownUnion & RHSKnownUnion & CarryKnownUnion;
-
-  assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
-         "known bits of sum differ");
-
-  // Compute known bits of the result.
-  KnownOut.Zero = ~PossibleSumOne & Known;
-  KnownOut.One = PossibleSumOne & Known;
-
-  // Are we still trying to solve for the sign bit?
-  if (!Known.isSignBitSet()) {
-    if (NSW) {
-      // Adding two non-negative numbers, or subtracting a negative number from
-      // a non-negative one, can't wrap into negative.
-      if (LHSKnown.isNonNegative() && Known2.isNonNegative())
-        KnownOut.makeNonNegative();
-      // Adding two negative numbers, or subtracting a non-negative number from
-      // a negative one, can't wrap into non-negative.
-      else if (LHSKnown.isNegative() && Known2.isNegative())
-        KnownOut.makeNegative();
-    }
-  }
+  KnownOut = KnownBits::computeForAddSub(Add, NSW, LHSKnown, Known2);
 }
 
 static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
index 0a8e3897cce92bee0d9e72e8d75c8e5e65476602..bed40a33704d19ea0a0ae4ad21e61dd2e6684db3 100644 (file)
@@ -71,6 +71,7 @@ add_llvm_library(LLVMSupport
   IntEqClasses.cpp
   IntervalMap.cpp
   JamCRC.cpp
+  KnownBits.cpp
   LEB128.cpp
   LineIterator.cpp
   Locale.cpp
diff --git a/lib/Support/KnownBits.cpp b/lib/Support/KnownBits.cpp
new file mode 100644 (file)
index 0000000..ac790eb
--- /dev/null
@@ -0,0 +1,65 @@
+//===-- KnownBits.cpp - Stores known zeros/ones ---------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains a class for representing known zeros and ones used by
+// computeKnownBits.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/KnownBits.h"
+
+using namespace llvm;
+
+KnownBits KnownBits::computeForAddSub(bool Add, bool NSW,
+                                      const KnownBits &LHS, KnownBits RHS) {
+  // Carry in a 1 for a subtract, rather than 0.
+  bool CarryIn = false;
+  if (!Add) {
+    // Sum = LHS + ~RHS + 1
+    std::swap(RHS.Zero, RHS.One);
+    CarryIn = true;
+  }
+
+  APInt PossibleSumZero = ~LHS.Zero + ~RHS.Zero + CarryIn;
+  APInt PossibleSumOne = LHS.One + RHS.One + CarryIn;
+
+  // Compute known bits of the carry.
+  APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
+  APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
+
+  // Compute set of known bits (where all three relevant bits are known).
+  APInt LHSKnownUnion = LHS.Zero | LHS.One;
+  APInt RHSKnownUnion = RHS.Zero | RHS.One;
+  APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne;
+  APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion;
+
+  assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
+         "known bits of sum differ");
+
+  // Compute known bits of the result.
+  KnownBits KnownOut;
+  KnownOut.Zero = ~std::move(PossibleSumZero) & Known;
+  KnownOut.One = std::move(PossibleSumOne) & Known;
+
+  // Are we still trying to solve for the sign bit?
+  if (!Known.isSignBitSet()) {
+    if (NSW) {
+      // Adding two non-negative numbers, or subtracting a negative number from
+      // a non-negative one, can't wrap into negative.
+      if (LHS.isNonNegative() && RHS.isNonNegative())
+        KnownOut.makeNonNegative();
+      // Adding two negative numbers, or subtracting a non-negative number from
+      // a negative one, can't wrap into non-negative.
+      else if (LHS.isNegative() && RHS.isNegative())
+        KnownOut.makeNegative();
+    }
+  }
+
+  return KnownOut;
+}