[llvm] [InstCombine] Fold `ctpop(X) eq/ne 1` if X is non-zero (PR #67268)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 24 11:09:49 PDT 2023
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/67268
>From 66f4509f061e8818e7806ec521c4b3c8075bb1d8 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 24 Sep 2023 23:19:25 +0800
Subject: [PATCH 1/3] [InstCombine] Simplify pattern `isPow2OrZero` if X is
non-zero
---
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 9 ++++++++-
llvm/test/Transforms/InstCombine/ispow2.ll | 2 +-
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index a219dac7acfbe16..9aafd83d42d0756 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3412,6 +3412,14 @@ static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,
const SimplifyQuery &Q) {
assert(CtpopLhs->getIntrinsicID() == Intrinsic::ctpop &&
"Non-ctpop intrin in ctpop fold");
+
+ const ICmpInst::Predicate Pred = I.getPredicate();
+ // If we know X is non-zero, we can fold isPow2OrZero into isPow2.
+ if (Pred == ICmpInst::ICMP_ULT && CRhs == 2 &&
+ isKnownNonZero(CtpopLhs, Q.DL, /*Depth*/ 0, Q.AC, Q.CxtI, Q.DT))
+ return ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, CtpopLhs,
+ ConstantInt::get(CtpopLhs->getType(), 1));
+
if (!CtpopLhs->hasOneUse())
return nullptr;
@@ -3423,7 +3431,6 @@ static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,
// If we know any bit of X can be folded to:
// IsPow2 : X & (~Bit) == 0
// NotPow2 : X & (~Bit) != 0
- const ICmpInst::Predicate Pred = I.getPredicate();
if (((I.isEquality() || Pred == ICmpInst::ICMP_UGT) && CRhs == 1) ||
(Pred == ICmpInst::ICMP_ULT && CRhs == 2)) {
Value *Op = CtpopLhs->getArgOperand(0);
diff --git a/llvm/test/Transforms/InstCombine/ispow2.ll b/llvm/test/Transforms/InstCombine/ispow2.ll
index 740f79cd32b39e8..501e3c8fcb1813f 100644
--- a/llvm/test/Transforms/InstCombine/ispow2.ll
+++ b/llvm/test/Transforms/InstCombine/ispow2.ll
@@ -198,7 +198,7 @@ define i1 @is_pow2_non_zero_ult_2(i32 %x) {
; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X:%.*]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[NOTZERO]])
; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X]]), !range [[RNG0]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[T0]], 2
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 1
; CHECK-NEXT: ret i1 [[CMP]]
;
%notzero = icmp ne i32 %x, 0
>From 4a055bf437c320ebd60e99adf0db56e7e468b07c Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 25 Sep 2023 01:25:32 +0800
Subject: [PATCH 2/3] [InstCombine] Fold pattern `ctpop(X) == 1` into `ctpop(X)
<u 2` if X is non-zero
---
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 9 +++++----
llvm/test/Transforms/InstCombine/icmp-ne-pow2.ll | 4 ++--
llvm/test/Transforms/InstCombine/ispow2.ll | 6 +++---
3 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 9aafd83d42d0756..2aadc0a4bd7ee00 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3414,11 +3414,12 @@ static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,
"Non-ctpop intrin in ctpop fold");
const ICmpInst::Predicate Pred = I.getPredicate();
- // If we know X is non-zero, we can fold isPow2OrZero into isPow2.
- if (Pred == ICmpInst::ICMP_ULT && CRhs == 2 &&
+ // If we know X is non-zero, we can fold `ctpop(X) == 1` into `ctpop(X) <u 2`
+ // since the latter gives better codegen.
+ if (Pred == ICmpInst::ICMP_EQ && CRhs == 1 &&
isKnownNonZero(CtpopLhs, Q.DL, /*Depth*/ 0, Q.AC, Q.CxtI, Q.DT))
- return ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, CtpopLhs,
- ConstantInt::get(CtpopLhs->getType(), 1));
+ return ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, CtpopLhs,
+ ConstantInt::get(CtpopLhs->getType(), 2));
if (!CtpopLhs->hasOneUse())
return nullptr;
diff --git a/llvm/test/Transforms/InstCombine/icmp-ne-pow2.ll b/llvm/test/Transforms/InstCombine/icmp-ne-pow2.ll
index 224ea3cd76cc6d1..6ee38de4d7fb089 100644
--- a/llvm/test/Transforms/InstCombine/icmp-ne-pow2.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-ne-pow2.ll
@@ -311,7 +311,7 @@ define i32 @pow2_32_nonconst_assume(i32 %x, i32 %y) {
define i32 @pow2_32_gtnonconst_assume(i32 %x, i32 %y) {
; CHECK-LABEL: @pow2_32_gtnonconst_assume(
; CHECK-NEXT: [[CTPOP:%.*]] = call i32 @llvm.ctpop.i32(i32 [[Y:%.*]]), !range [[RNG0]]
-; CHECK-NEXT: [[YP2:%.*]] = icmp eq i32 [[CTPOP]], 1
+; CHECK-NEXT: [[YP2:%.*]] = icmp ult i32 [[CTPOP]], 2
; CHECK-NEXT: call void @llvm.assume(i1 [[YP2]])
; CHECK-NEXT: [[YGT:%.*]] = icmp ugt i32 [[Y]], [[X:%.*]]
; CHECK-NEXT: call void @llvm.assume(i1 [[YGT]])
@@ -518,7 +518,7 @@ define i32 @maybe_pow2_32_noncont(i32 %x, i32 %y) {
; CHECK-NEXT: br i1 [[YGT8]], label [[CONT1:%.*]], label [[CONT2:%.*]]
; CHECK: Cont1:
; CHECK-NEXT: [[CTPOP:%.*]] = call i32 @llvm.ctpop.i32(i32 [[Y]]), !range [[RNG0]]
-; CHECK-NEXT: [[YP2:%.*]] = icmp eq i32 [[CTPOP]], 1
+; CHECK-NEXT: [[YP2:%.*]] = icmp ult i32 [[CTPOP]], 2
; CHECK-NEXT: call void @llvm.assume(i1 [[YP2]])
; CHECK-NEXT: br i1 true, label [[CONT2]], label [[FALSE:%.*]]
; CHECK: Cont2:
diff --git a/llvm/test/Transforms/InstCombine/ispow2.ll b/llvm/test/Transforms/InstCombine/ispow2.ll
index 501e3c8fcb1813f..fc011e33954a3c3 100644
--- a/llvm/test/Transforms/InstCombine/ispow2.ll
+++ b/llvm/test/Transforms/InstCombine/ispow2.ll
@@ -198,7 +198,7 @@ define i1 @is_pow2_non_zero_ult_2(i32 %x) {
; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X:%.*]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[NOTZERO]])
; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X]]), !range [[RNG0]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[T0]], 2
; CHECK-NEXT: ret i1 [[CMP]]
;
%notzero = icmp ne i32 %x, 0
@@ -213,7 +213,7 @@ define i1 @is_pow2_non_zero_eq_1(i32 %x) {
; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X:%.*]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[NOTZERO]])
; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X]]), !range [[RNG0]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[T0]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[T0]], 2
; CHECK-NEXT: ret i1 [[CMP]]
;
%notzero = icmp ne i32 %x, 0
@@ -1466,7 +1466,7 @@ define i1 @is_pow2_nz_known_bits_fail_multiuse(i32 %xin) {
; CHECK-NEXT: [[X:%.*]] = or i32 [[XIN:%.*]], 64
; CHECK-NEXT: [[CNT:%.*]] = call i32 @llvm.ctpop.i32(i32 [[X]]), !range [[RNG3:![0-9]+]]
; CHECK-NEXT: call void @use.i32(i32 [[CNT]])
-; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[CNT]], 1
+; CHECK-NEXT: [[R:%.*]] = icmp ult i32 [[CNT]], 2
; CHECK-NEXT: ret i1 [[R]]
;
%x = or i32 %xin, 64
>From 772ceccd4aab546b40732f45e22b4348c2a28ba1 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 25 Sep 2023 02:08:39 +0800
Subject: [PATCH 3/3] [InstCombine] Fold pattern `ctpop(X) != 1` into `ctpop(X)
>u 1` if X is non-zero
---
.../InstCombine/InstCombineCompares.cpp | 16 +++++++++++-----
llvm/test/Transforms/InstCombine/ispow2.ll | 2 +-
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 2aadc0a4bd7ee00..d6605c40f50be23 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3414,12 +3414,18 @@ static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,
"Non-ctpop intrin in ctpop fold");
const ICmpInst::Predicate Pred = I.getPredicate();
- // If we know X is non-zero, we can fold `ctpop(X) == 1` into `ctpop(X) <u 2`
- // since the latter gives better codegen.
- if (Pred == ICmpInst::ICMP_EQ && CRhs == 1 &&
+ // When X is non-zero:
+ // ctpop(X) == 1 --> ctpop(X) <u 2
+ // ctpop(X) != 1 --> ctpop(X) >u 1
+ // The latter forms give better codegen.
+ if (I.isEquality() && CRhs == 1 &&
isKnownNonZero(CtpopLhs, Q.DL, /*Depth*/ 0, Q.AC, Q.CxtI, Q.DT))
- return ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, CtpopLhs,
- ConstantInt::get(CtpopLhs->getType(), 2));
+ return ICmpInst::Create(
+ Instruction::ICmp,
+ Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGT,
+ CtpopLhs,
+ ConstantInt::get(CtpopLhs->getType(),
+ Pred == ICmpInst::ICMP_EQ ? 2 : 1));
if (!CtpopLhs->hasOneUse())
return nullptr;
diff --git a/llvm/test/Transforms/InstCombine/ispow2.ll b/llvm/test/Transforms/InstCombine/ispow2.ll
index fc011e33954a3c3..4754aa2b8b69f61 100644
--- a/llvm/test/Transforms/InstCombine/ispow2.ll
+++ b/llvm/test/Transforms/InstCombine/ispow2.ll
@@ -243,7 +243,7 @@ define i1 @is_pow2_non_zero_ne_1(i32 %x) {
; CHECK-NEXT: [[NOTZERO:%.*]] = icmp ne i32 [[X:%.*]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[NOTZERO]])
; CHECK-NEXT: [[T0:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[X]]), !range [[RNG0]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[T0]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[T0]], 1
; CHECK-NEXT: ret i1 [[CMP]]
;
%notzero = icmp ne i32 %x, 0
More information about the llvm-commits
mailing list