Skip to content

Commit d30be96

Browse files
lukel97kcloudy0717
authored andcommitted
[ConstantRange] Allow casting to the same bitwidth. NFC (llvm#170102)
From the review in llvm#169527 (comment), there are some users where we want to extend or truncate a ConstantRange only if it's not already the destination bitwidth. Previously this asserted, so this PR relaxes it to just be a no-op, similar to IRBuilder::createZExt and friends.
1 parent bdac5fd commit d30be96

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

llvm/lib/IR/ConstantRange.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
841841
if (isEmptySet()) return getEmpty(DstTySize);
842842

843843
unsigned SrcTySize = getBitWidth();
844+
if (DstTySize == SrcTySize)
845+
return *this;
844846
assert(SrcTySize < DstTySize && "Not a value extension");
845847
if (isFullSet() || isUpperWrapped()) {
846848
// Change into [0, 1 << src bit width)
@@ -858,6 +860,8 @@ ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
858860
if (isEmptySet()) return getEmpty(DstTySize);
859861

860862
unsigned SrcTySize = getBitWidth();
863+
if (DstTySize == SrcTySize)
864+
return *this;
861865
assert(SrcTySize < DstTySize && "Not a value extension");
862866

863867
// special case: [X, INT_MIN) -- not really wrapping around
@@ -874,6 +878,8 @@ ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
874878

875879
ConstantRange ConstantRange::truncate(uint32_t DstTySize,
876880
unsigned NoWrapKind) const {
881+
if (DstTySize == getBitWidth())
882+
return *this;
877883
assert(getBitWidth() > DstTySize && "Not a value truncation");
878884
if (isEmptySet())
879885
return getEmpty(DstTySize);

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,10 +2109,10 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {
21092109

21102110
ConstantRange Count = getValueState(CountArg)
21112111
.asConstantRange(CountArg->getType(), false)
2112-
.zextOrTrunc(BitWidth);
2112+
.zeroExtend(BitWidth);
21132113
ConstantRange MaxLanes = getValueState(VF)
21142114
.asConstantRange(VF->getType(), false)
2115-
.zextOrTrunc(BitWidth);
2115+
.zeroExtend(BitWidth);
21162116
if (Scalable)
21172117
MaxLanes =
21182118
MaxLanes.multiply(getVScaleRange(II->getFunction(), BitWidth));
@@ -2126,7 +2126,7 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {
21262126
if (Count.icmp(CmpInst::ICMP_ULE, MaxLanes))
21272127
Result = Count;
21282128

2129-
Result = Result.zextOrTrunc(II->getType()->getScalarSizeInBits());
2129+
Result = Result.truncate(II->getType()->getScalarSizeInBits());
21302130
return (void)mergeInValue(ValueState[II], II,
21312131
ValueLatticeElement::getRange(Result));
21322132
}

llvm/unittests/IR/ConstantRangeTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ TEST_F(ConstantRangeTest, Trunc) {
449449
// trunc([7, 1), 3->2) = [3, 1)
450450
ConstantRange SevenOne(APInt(3, 7), APInt(3, 1));
451451
EXPECT_EQ(SevenOne.truncate(2), ConstantRange(APInt(2, 3), APInt(2, 1)));
452+
453+
ConstantRange Nop = Full.truncate(Full.getBitWidth());
454+
EXPECT_EQ(Full, Nop);
452455
}
453456

454457
TEST_F(ConstantRangeTest, TruncNuw) {
@@ -527,6 +530,9 @@ TEST_F(ConstantRangeTest, ZExt) {
527530
// zext([5, 0), 3->7) = [5, 8)
528531
ConstantRange FiveZero(APInt(3, 5), APInt(3, 0));
529532
EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8)));
533+
534+
ConstantRange Nop = Full.zeroExtend(Full.getBitWidth());
535+
EXPECT_EQ(Full, Nop);
530536
}
531537

532538
TEST_F(ConstantRangeTest, SExt) {
@@ -550,6 +556,9 @@ TEST_F(ConstantRangeTest, SExt) {
550556

551557
EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19),
552558
ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000)));
559+
560+
ConstantRange Nop = Full.signExtend(Full.getBitWidth());
561+
EXPECT_EQ(Full, Nop);
553562
}
554563

555564
TEST_F(ConstantRangeTest, IntersectWith) {

0 commit comments

Comments
 (0)