[llvm] [ValueTracking] Use `ConstantRange::toKnownBits` when computing from Range Metadata; NFC (PR #85574)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 17 10:35:16 PDT 2024


================
@@ -419,27 +419,16 @@ static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
 
 void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
                                              KnownBits &Known) {
-  unsigned BitWidth = Known.getBitWidth();
   unsigned NumRanges = Ranges.getNumOperands() / 2;
   assert(NumRanges >= 1);
 
-  Known.Zero.setAllBits();
-  Known.One.setAllBits();
-
   for (unsigned i = 0; i < NumRanges; ++i) {
     ConstantInt *Lower =
         mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
     ConstantInt *Upper =
         mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
     ConstantRange Range(Lower->getValue(), Upper->getValue());
-
-    // The first CommonPrefixBits of all values in Range are equal.
-    unsigned CommonPrefixBits =
-        (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
-    APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
-    APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
-    Known.One &= UnsignedMax & Mask;
-    Known.Zero &= ~UnsignedMax & Mask;
+    Known = Known.unionWith(Range.toKnownBits());
----------------
nikic wrote:

Both are imprecise in different ways. Though afaik multi-range `!range` is never actually used, so it doesn't really matter.

https://github.com/llvm/llvm-project/pull/85574


More information about the llvm-commits mailing list