Skip to content

Commit 6a46c6c

Browse files
authored
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 <[email protected]>
1 parent dcc2182 commit 6a46c6c

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
430430
ConstantInt *Upper =
431431
mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
432432
ConstantRange Range(Lower->getValue(), Upper->getValue());
433+
// BitWidth must equal the Ranges BitWidth for the correct number of high
434+
// bits to be set.
435+
assert(BitWidth == Range.getBitWidth() &&
436+
"Known bit width must match range bit width!");
433437

434438
// The first CommonPrefixBits of all values in Range are equal.
435439
unsigned CommonPrefixBits =

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9176,6 +9176,12 @@ SDValue SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
91769176
"Cannot use an ext load to change the number of vector elements!");
91779177
}
91789178

9179+
assert((!MMO->getRanges() ||
9180+
(mdconst::extract<ConstantInt>(MMO->getRanges()->getOperand(0))
9181+
->getBitWidth() == MemVT.getScalarSizeInBits() &&
9182+
MemVT.isInteger())) &&
9183+
"Range metadata and load type must match!");
9184+
91799185
bool Indexed = AM != ISD::UNINDEXED;
91809186
assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
91819187

0 commit comments

Comments
 (0)