[llvm] [ValueTracking] Take into account whether zero is poison when computing CR for `ct{t,l}z` (PR #122548)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 11 10:51:04 PST 2025
https://github.com/goldsteinn updated https://github.com/llvm/llvm-project/pull/122548
>From 35cc01ed6f7e3fb7485f633fe6deae393111f7e3 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Fri, 10 Jan 2025 17:31:10 -0600
Subject: [PATCH 1/2] [ValueTracking] Take into account wether zero is poison
when computing CR for `ct{t,l}z`
---
llvm/lib/Analysis/ValueTracking.cpp | 15 +++++++++++----
llvm/test/Transforms/InstSimplify/call.ll | 8 ++------
2 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 4b246c013e96f1..15057901cd74c7 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9898,13 +9898,20 @@ static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
}
}
-static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) {
+static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II,
+ bool UseInstrInfo) {
unsigned Width = II.getType()->getScalarSizeInBits();
const APInt *C;
switch (II.getIntrinsicID()) {
- case Intrinsic::ctpop:
case Intrinsic::ctlz:
- case Intrinsic::cttz:
+ case Intrinsic::cttz: {
+ APInt Upper = APInt(Width, Width);
+ if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
+ Upper += 1;
+ // Maximum of set/clear bits is the bit width.
+ return ConstantRange::getNonEmpty(APInt::getZero(Width), Upper);
+ }
+ case Intrinsic::ctpop:
// Maximum of set/clear bits is the bit width.
return ConstantRange::getNonEmpty(APInt::getZero(Width),
APInt(Width, Width) + 1);
@@ -10095,7 +10102,7 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
CR = ConstantRange::getNonEmpty(Lower, Upper);
} else if (auto *II = dyn_cast<IntrinsicInst>(V))
- CR = getRangeForIntrinsic(*II);
+ CR = getRangeForIntrinsic(*II, UseInstrInfo);
else if (auto *SI = dyn_cast<SelectInst>(V)) {
ConstantRange CRTrue = computeConstantRange(
SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
diff --git a/llvm/test/Transforms/InstSimplify/call.ll b/llvm/test/Transforms/InstSimplify/call.ll
index 67d5c4dbfb2e7d..910750adf9d427 100644
--- a/llvm/test/Transforms/InstSimplify/call.ll
+++ b/llvm/test/Transforms/InstSimplify/call.ll
@@ -1582,9 +1582,7 @@ define i1 @ctlz_i1_non_poison_eq_false(i1 %x) {
define i1 @ctlz_i1_poison_eq_false(i1 %x) {
; CHECK-LABEL: @ctlz_i1_poison_eq_false(
-; CHECK-NEXT: [[CT:%.*]] = call i1 @llvm.ctlz.i1(i1 [[X:%.*]], i1 true)
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i1 [[CT]], false
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%ct = call i1 @llvm.ctlz.i1(i1 %x, i1 true)
%cmp = icmp eq i1 %ct, false
@@ -1604,9 +1602,7 @@ define i1 @cttz_i1_non_poison_eq_false(i1 %x) {
define i1 @cttz_i1_poison_eq_false(i1 %x) {
; CHECK-LABEL: @cttz_i1_poison_eq_false(
-; CHECK-NEXT: [[CT:%.*]] = call i1 @llvm.cttz.i1(i1 [[X:%.*]], i1 true)
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i1 [[CT]], false
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%ct = call i1 @llvm.cttz.i1(i1 %x, i1 true)
%cmp = icmp eq i1 %ct, false
>From 8efb95f0e02a0c424aa5dd605e62657092eba5c5 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sat, 11 Jan 2025 12:50:49 -0600
Subject: [PATCH 2/2] Apply Suggestion
---
llvm/lib/Analysis/ValueTracking.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 15057901cd74c7..88aa270612e942 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9905,7 +9905,7 @@ static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II,
switch (II.getIntrinsicID()) {
case Intrinsic::ctlz:
case Intrinsic::cttz: {
- APInt Upper = APInt(Width, Width);
+ APInt Upper(Width, Width);
if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
Upper += 1;
// Maximum of set/clear bits is the bit width.
More information about the llvm-commits
mailing list