[llvm] Ensure KnownBits passed when calculating from range md has right size (PR #132985)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 25 13:02:06 PDT 2025
https://github.com/LU-JOHN created https://github.com/llvm/llvm-project/pull/132985
KnownBits passed to computeKnownBitsFromRangeMetadata must have the same bit width as the range metadata bit width. Otherwise the calculated results will be incorrect.
>From 0b5ed292c15ca73144660f0912f0c955abd639e7 Mon Sep 17 00:00:00 2001
From: John Lu <John.Lu at amd.com>
Date: Tue, 25 Mar 2025 14:59:23 -0500
Subject: [PATCH] Ensure KnownBits passed to computeKnownBitsFromRangeMetadata
has correct size
Signed-off-by: John Lu <John.Lu at amd.com>
---
llvm/lib/Analysis/ValueTracking.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 880781742fae0..4e3d5d8f12cbc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -433,6 +433,8 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
// The first CommonPrefixBits of all values in Range are equal.
unsigned CommonPrefixBits =
(Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
+ // BitWidth must equal the Ranges BitWidth for the correct number of high bits to be set.
+ assert(BitWidth == Lower->getBitWidth() );
APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
Known.One &= UnsignedMax & Mask;
More information about the llvm-commits
mailing list