Skip to content

Commit 35cc01e

Browse files
committed
[ValueTracking] Take into account wether zero is poison when computing CR for ct{t,l}z
1 parent 0d9c027 commit 35cc01e

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9898,13 +9898,20 @@ static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
98989898
}
98999899
}
99009900

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

llvm/test/Transforms/InstSimplify/call.ll

Lines changed: 2 additions & 6 deletions
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)