[llvm] [InstCombine] Simplify (A >= 0) & ((A & INT_MAX) != 0) to A > 0 (PR #207209)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 2 08:42:07 PDT 2026
https://github.com/AZero13 created https://github.com/llvm/llvm-project/pull/207209
This patch implements simplifications for bitwise and logical AND/OR combinations of sign-bit checks and lower-bits checks.
>From a0adabbd5075c30a40adc2619736c5e832523b4e Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Thu, 2 Jul 2026 11:28:17 -0400
Subject: [PATCH 1/3] [InstCombine] Precommit tests for PR112390
---
llvm/test/Transforms/InstCombine/pr112390.ll | 93 ++++++++++++++++++++
1 file changed, 93 insertions(+)
create mode 100644 llvm/test/Transforms/InstCombine/pr112390.ll
diff --git a/llvm/test/Transforms/InstCombine/pr112390.ll b/llvm/test/Transforms/InstCombine/pr112390.ll
new file mode 100644
index 0000000000000..74669f35f0246
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/pr112390.ll
@@ -0,0 +1,93 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+define i1 @test_and(i32 %A) {
+; CHECK-LABEL: @test_and(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[A:%.*]], -1
+; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[AND]], 0
+; CHECK-NEXT: [[RES:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cmp1 = icmp sge i32 %A, 0
+ %and = and i32 %A, 2147483647
+ %cmp2 = icmp ne i32 %and, 0
+ %res = and i1 %cmp1, %cmp2
+ ret i1 %res
+}
+
+define i1 @test_or(i32 %A) {
+; CHECK-LABEL: @test_or(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A:%.*]], 0
+; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
+; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND]], 0
+; CHECK-NEXT: [[RES:%.*]] = or i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cmp1 = icmp slt i32 %A, 0
+ %and = and i32 %A, 2147483647
+ %cmp2 = icmp eq i32 %and, 0
+ %res = or i1 %cmp1, %cmp2
+ ret i1 %res
+}
+
+define i1 @test_and_logical(i32 %A) {
+; CHECK-LABEL: @test_and_logical(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[A:%.*]], -1
+; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[AND]], 0
+; CHECK-NEXT: [[RES:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cmp1 = icmp sge i32 %A, 0
+ %and = and i32 %A, 2147483647
+ %cmp2 = icmp ne i32 %and, 0
+ %res = select i1 %cmp1, i1 %cmp2, i1 false
+ ret i1 %res
+}
+
+define i1 @test_or_logical(i32 %A) {
+; CHECK-LABEL: @test_or_logical(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A:%.*]], 0
+; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
+; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND]], 0
+; CHECK-NEXT: [[RES:%.*]] = or i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cmp1 = icmp slt i32 %A, 0
+ %and = and i32 %A, 2147483647
+ %cmp2 = icmp eq i32 %and, 0
+ %res = select i1 %cmp1, i1 true, i1 %cmp2
+ ret i1 %res
+}
+
+define <2 x i1> @test_and_vec(<2 x i32> %A) {
+; CHECK-LABEL: @test_and_vec(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt <2 x i32> [[A:%.*]], splat (i32 -1)
+; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[A]], splat (i32 2147483647)
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ne <2 x i32> [[AND]], zeroinitializer
+; CHECK-NEXT: [[RES:%.*]] = and <2 x i1> [[CMP1]], [[CMP2]]
+; CHECK-NEXT: ret <2 x i1> [[RES]]
+;
+ %cmp1 = icmp sge <2 x i32> %A, zeroinitializer
+ %and = and <2 x i32> %A, <i32 2147483647, i32 2147483647>
+ %cmp2 = icmp ne <2 x i32> %and, zeroinitializer
+ %res = and <2 x i1> %cmp1, %cmp2
+ ret <2 x i1> %res
+}
+
+define i1 @test_and_commuted(i32 %A) {
+; CHECK-LABEL: @test_and_commuted(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[A:%.*]], -1
+; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[AND]], 0
+; CHECK-NEXT: [[RES:%.*]] = and i1 [[CMP2]], [[CMP1]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cmp1 = icmp sge i32 %A, 0
+ %and = and i32 %A, 2147483647
+ %cmp2 = icmp ne i32 %and, 0
+ %res = and i1 %cmp2, %cmp1
+ ret i1 %res
+}
+
>From ccee4c01582d791664d5807128d9868894d72c21 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Thu, 2 Jul 2026 11:30:40 -0400
Subject: [PATCH 2/3] [InstCombine] Simplify (A >= 0) & ((A & INT_MAX) != 0) to
A > 0
This patch implements simplifications for bitwise and logical AND/OR combinations of sign-bit checks and lower-bits checks.
---
.../InstCombine/InstCombineAndOrXor.cpp | 28 +++++++++++++++++
llvm/test/Transforms/InstCombine/pr112390.ll | 30 ++++---------------
2 files changed, 34 insertions(+), 24 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index b6f4a55c07e8a..591802b77178c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3533,6 +3533,34 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
}
}
+ // (icmp sge X, 0) & (icmp ne (X & INT_MAX), 0) -> (icmp sgt X, 0)
+ // (icmp slt X, 0) | (icmp eq (X & INT_MAX), 0) -> (icmp sle X, 0)
+ {
+ bool TrueIfSigned;
+ if (isSignBitCheck(PredL, *LHSC, TrueIfSigned) &&
+ PredR == (IsAnd ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ) &&
+ match(RHS1, m_Zero()) &&
+ match(RHS0, m_And(m_Specific(LHS0), m_MaxSignedValue()))) {
+ if (IsAnd && !TrueIfSigned)
+ return Builder.CreateICmpSGT(
+ LHS0, ConstantInt::getNullValue(LHS0->getType()));
+ if (!IsAnd && TrueIfSigned)
+ return Builder.CreateICmpSLE(
+ LHS0, ConstantInt::getNullValue(LHS0->getType()));
+ }
+ if (isSignBitCheck(PredR, *RHSC, TrueIfSigned) &&
+ PredL == (IsAnd ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ) &&
+ match(LHS1, m_Zero()) &&
+ match(LHS0, m_And(m_Specific(RHS0), m_MaxSignedValue()))) {
+ if (IsAnd && !TrueIfSigned)
+ return Builder.CreateICmpSGT(
+ RHS0, ConstantInt::getNullValue(RHS0->getType()));
+ if (!IsAnd && TrueIfSigned)
+ return Builder.CreateICmpSLE(
+ RHS0, ConstantInt::getNullValue(RHS0->getType()));
+ }
+ }
+
// Match naive pattern (and its inverted form) for checking if two values
// share same sign. An example of the pattern:
// (icmp slt (X & Y), 0) | (icmp sgt (X | Y), -1) -> (icmp sgt (X ^ Y), -1)
diff --git a/llvm/test/Transforms/InstCombine/pr112390.ll b/llvm/test/Transforms/InstCombine/pr112390.ll
index 74669f35f0246..2b404e56c58fc 100644
--- a/llvm/test/Transforms/InstCombine/pr112390.ll
+++ b/llvm/test/Transforms/InstCombine/pr112390.ll
@@ -3,10 +3,7 @@
define i1 @test_and(i32 %A) {
; CHECK-LABEL: @test_and(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
-; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[AND]], 0
-; CHECK-NEXT: [[RES:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: [[RES:%.*]] = icmp sgt i32 [[A:%.*]], 0
; CHECK-NEXT: ret i1 [[RES]]
;
%cmp1 = icmp sge i32 %A, 0
@@ -18,10 +15,7 @@ define i1 @test_and(i32 %A) {
define i1 @test_or(i32 %A) {
; CHECK-LABEL: @test_or(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A:%.*]], 0
-; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
-; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND]], 0
-; CHECK-NEXT: [[RES:%.*]] = or i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: [[RES:%.*]] = icmp slt i32 [[A:%.*]], 1
; CHECK-NEXT: ret i1 [[RES]]
;
%cmp1 = icmp slt i32 %A, 0
@@ -33,10 +27,7 @@ define i1 @test_or(i32 %A) {
define i1 @test_and_logical(i32 %A) {
; CHECK-LABEL: @test_and_logical(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
-; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[AND]], 0
-; CHECK-NEXT: [[RES:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: [[RES:%.*]] = icmp sgt i32 [[A:%.*]], 0
; CHECK-NEXT: ret i1 [[RES]]
;
%cmp1 = icmp sge i32 %A, 0
@@ -48,10 +39,7 @@ define i1 @test_and_logical(i32 %A) {
define i1 @test_or_logical(i32 %A) {
; CHECK-LABEL: @test_or_logical(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A:%.*]], 0
-; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
-; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND]], 0
-; CHECK-NEXT: [[RES:%.*]] = or i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: [[RES:%.*]] = icmp slt i32 [[A:%.*]], 1
; CHECK-NEXT: ret i1 [[RES]]
;
%cmp1 = icmp slt i32 %A, 0
@@ -63,10 +51,7 @@ define i1 @test_or_logical(i32 %A) {
define <2 x i1> @test_and_vec(<2 x i32> %A) {
; CHECK-LABEL: @test_and_vec(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt <2 x i32> [[A:%.*]], splat (i32 -1)
-; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[A]], splat (i32 2147483647)
-; CHECK-NEXT: [[CMP2:%.*]] = icmp ne <2 x i32> [[AND]], zeroinitializer
-; CHECK-NEXT: [[RES:%.*]] = and <2 x i1> [[CMP1]], [[CMP2]]
+; CHECK-NEXT: [[RES:%.*]] = icmp sgt <2 x i32> [[A:%.*]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[RES]]
;
%cmp1 = icmp sge <2 x i32> %A, zeroinitializer
@@ -78,10 +63,7 @@ define <2 x i1> @test_and_vec(<2 x i32> %A) {
define i1 @test_and_commuted(i32 %A) {
; CHECK-LABEL: @test_and_commuted(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i32 [[A]], 2147483647
-; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[AND]], 0
-; CHECK-NEXT: [[RES:%.*]] = and i1 [[CMP2]], [[CMP1]]
+; CHECK-NEXT: [[RES:%.*]] = icmp sgt i32 [[A:%.*]], 0
; CHECK-NEXT: ret i1 [[RES]]
;
%cmp1 = icmp sge i32 %A, 0
>From 1065b4f9960e02d47121a9183df58c69e9918f6a Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Thu, 2 Jul 2026 11:31:24 -0400
Subject: [PATCH 3/3] Rename
---
.../Transforms/InstCombine/{pr112390.ll => sign-bitwise-check.ll} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename llvm/test/Transforms/InstCombine/{pr112390.ll => sign-bitwise-check.ll} (100%)
diff --git a/llvm/test/Transforms/InstCombine/pr112390.ll b/llvm/test/Transforms/InstCombine/sign-bitwise-check.ll
similarity index 100%
rename from llvm/test/Transforms/InstCombine/pr112390.ll
rename to llvm/test/Transforms/InstCombine/sign-bitwise-check.ll
More information about the llvm-commits
mailing list