Ensure KnownBits passed when calculating from range md has right size (#132985)
KnownBits passed to computeKnownBitsFromRangeMetadata must have the same
bit width as the range metadata bit width. Otherwise the calculated
results will be incorrect.
---------
Signed-off-by: John Lu <John.Lu@amd.com>
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index fc0c749..3b0249f 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -430,6 +430,10 @@
ConstantInt *Upper =
mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
ConstantRange Range(Lower->getValue(), Upper->getValue());
+ // BitWidth must equal the Ranges BitWidth for the correct number of high
+ // bits to be set.
+ assert(BitWidth == Range.getBitWidth() &&
+ "Known bit width must match range bit width!");
// The first CommonPrefixBits of all values in Range are equal.
unsigned CommonPrefixBits =
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 3526bee..69548d0 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -9176,6 +9176,12 @@
"Cannot use an ext load to change the number of vector elements!");
}
+ assert((!MMO->getRanges() ||
+ (mdconst::extract<ConstantInt>(MMO->getRanges()->getOperand(0))
+ ->getBitWidth() == MemVT.getScalarSizeInBits() &&
+ MemVT.isInteger())) &&
+ "Range metadata and load type must match!");
+
bool Indexed = AM != ISD::UNINDEXED;
assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");