Skip to content

Commit 154c8a0

Browse files
committed
[InstCombine] Use KnownBits::ashr()
This fixes a consistency violation under -instcombine-verify-known-bits.
1 parent 035e76f commit 154c8a0

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -812,27 +812,22 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
812812
return I;
813813
}
814814

815-
// Compute the new bits that are at the top now plus sign bits.
816-
APInt HighBits(APInt::getHighBitsSet(
817-
BitWidth, std::min(SignBits + ShiftAmt - 1, BitWidth)));
818-
Known.Zero.lshrInPlace(ShiftAmt);
819-
Known.One.lshrInPlace(ShiftAmt);
815+
Known = KnownBits::ashr(
816+
Known, KnownBits::makeConstant(APInt(BitWidth, ShiftAmt)),
817+
ShiftAmt != 0, I->isExact());
820818

821819
// If the input sign bit is known to be zero, or if none of the top bits
822820
// are demanded, turn this into an unsigned shift right.
823821
assert(BitWidth > ShiftAmt && "Shift amount not saturated?");
822+
APInt HighBits(APInt::getHighBitsSet(
823+
BitWidth, std::min(SignBits + ShiftAmt - 1, BitWidth)));
824824
if (Known.Zero[BitWidth-ShiftAmt-1] ||
825825
!DemandedMask.intersects(HighBits)) {
826826
BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
827827
I->getOperand(1));
828828
LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
829829
LShr->takeName(I);
830830
return InsertNewInstWith(LShr, I->getIterator());
831-
} else if (Known.One[BitWidth-ShiftAmt-1]) { // New bits are known one.
832-
Known.One |= HighBits;
833-
// SignBits may be out-of-sync with Known.countMinSignBits(). Mask out
834-
// high bits of Known.Zero to avoid conflicts.
835-
Known.Zero &= ~HighBits;
836831
}
837832
} else {
838833
llvm::computeKnownBits(I, Known, Depth, Q);

llvm/test/Transforms/InstCombine/pr80597.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ define i64 @pr80597(i1 %cond) {
1111
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1212
; CHECK: if.else:
1313
; CHECK-NEXT: [[SEXT2:%.*]] = ashr exact i64 [[ADD]], 1
14-
; CHECK-NEXT: [[ASHR:%.*]] = or i64 [[SEXT2]], 4418419761487020032
14+
; CHECK-NEXT: [[ASHR:%.*]] = or disjoint i64 [[SEXT2]], 4418419761487020032
1515
; CHECK-NEXT: ret i64 [[ASHR]]
1616
; CHECK: if.then:
1717
; CHECK-NEXT: ret i64 0

0 commit comments

Comments
 (0)