Skip to content

Commit 17ef436

Browse files
authored
[ValueTracking] Take into account whether zero is poison when computing CR for ct{t,l}z (#122548)
1 parent cc995ad commit 17ef436

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

llvm/lib/Analysis/ValueTracking.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -9897,13 +9897,20 @@ static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
98979897
}
98989898
}
98999899

9900-
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) {
9900+
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II,
9901+
bool UseInstrInfo) {
99019902
unsigned Width = II.getType()->getScalarSizeInBits();
99029903
const APInt *C;
99039904
switch (II.getIntrinsicID()) {
9904-
case Intrinsic::ctpop:
99059905
case Intrinsic::ctlz:
9906-
case Intrinsic::cttz:
9906+
case Intrinsic::cttz: {
9907+
APInt Upper(Width, Width);
9908+
if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9909+
Upper += 1;
9910+
// Maximum of set/clear bits is the bit width.
9911+
return ConstantRange::getNonEmpty(APInt::getZero(Width), Upper);
9912+
}
9913+
case Intrinsic::ctpop:
99079914
// Maximum of set/clear bits is the bit width.
99089915
return ConstantRange::getNonEmpty(APInt::getZero(Width),
99099916
APInt(Width, Width) + 1);
@@ -10094,7 +10101,7 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
1009410101
setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
1009510102
CR = ConstantRange::getNonEmpty(Lower, Upper);
1009610103
} else if (auto *II = dyn_cast<IntrinsicInst>(V))
10097-
CR = getRangeForIntrinsic(*II);
10104+
CR = getRangeForIntrinsic(*II, UseInstrInfo);
1009810105
else if (auto *SI = dyn_cast<SelectInst>(V)) {
1009910106
ConstantRange CRTrue = computeConstantRange(
1010010107
SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);

llvm/test/Transforms/InstSimplify/call.ll

+2-6
Original file line numberDiff line numberDiff line change
@@ -1582,9 +1582,7 @@ define i1 @ctlz_i1_non_poison_eq_false(i1 %x) {
15821582

15831583
define i1 @ctlz_i1_poison_eq_false(i1 %x) {
15841584
; CHECK-LABEL: @ctlz_i1_poison_eq_false(
1585-
; CHECK-NEXT: [[CT:%.*]] = call i1 @llvm.ctlz.i1(i1 [[X:%.*]], i1 true)
1586-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i1 [[CT]], false
1587-
; CHECK-NEXT: ret i1 [[CMP]]
1585+
; CHECK-NEXT: ret i1 true
15881586
;
15891587
%ct = call i1 @llvm.ctlz.i1(i1 %x, i1 true)
15901588
%cmp = icmp eq i1 %ct, false
@@ -1604,9 +1602,7 @@ define i1 @cttz_i1_non_poison_eq_false(i1 %x) {
16041602

16051603
define i1 @cttz_i1_poison_eq_false(i1 %x) {
16061604
; CHECK-LABEL: @cttz_i1_poison_eq_false(
1607-
; CHECK-NEXT: [[CT:%.*]] = call i1 @llvm.cttz.i1(i1 [[X:%.*]], i1 true)
1608-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i1 [[CT]], false
1609-
; CHECK-NEXT: ret i1 [[CMP]]
1605+
; CHECK-NEXT: ret i1 true
16101606
;
16111607
%ct = call i1 @llvm.cttz.i1(i1 %x, i1 true)
16121608
%cmp = icmp eq i1 %ct, false

0 commit comments

Comments
 (0)