[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 10:11:40 PDT 2026


https://github.com/AZero13 updated https://github.com/llvm/llvm-project/pull/207209

>From a90736f99a3a0e4992db2cf49f503bcbc6a77874 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/4] [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 32e6b5331be4ccc000a75056482ffa42ec640260 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/4] [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 84a41e7dc0580d5f5bd7136a45f23c37db7c83d7 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/4] 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

>From d7518145723b6b6684b2f788162e723a1c07565c Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Thu, 2 Jul 2026 12:07:34 -0400
Subject: [PATCH 4/4] Generalize to computeNumBits

---
 .../InstCombine/InstCombineAndOrXor.cpp       | 35 ++++++++++++-------
 .../InstCombine/sign-bitwise-check.ll         | 20 +++++++++++
 2 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 591802b77178c..ba7bb64df7f95 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3533,31 +3533,42 @@ 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)
+  // (icmp sge X, 0) & (icmp ne (X & Mask), 0) -> (icmp sgt X, 0)
+  // (icmp slt X, 0) | (icmp eq (X & Mask), 0) -> (icmp sle X, 0)
+  // Valid if Mask covers all potentially non-zero positive bits of X.
   {
     bool TrueIfSigned;
+    const APInt *MaskC;
+    auto IsValidMask = [&](Value *X, const APInt &Mask) {
+      APInt SignBit = APInt::getSignMask(Mask.getBitWidth());
+      APInt RequiredMask = ~Mask & ~SignBit;
+      return RequiredMask.isZero() ||
+             (RequiredMask & ~computeKnownBits(X, &I, 0).Zero).isZero();
+    };
+
     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()))) {
+        match(RHS0, m_And(m_Specific(LHS0), m_APInt(MaskC))) &&
+        IsValidMask(LHS0, *MaskC)) {
       if (IsAnd && !TrueIfSigned)
-        return Builder.CreateICmpSGT(
-            LHS0, ConstantInt::getNullValue(LHS0->getType()));
+        return Builder.CreateICmpSGT(LHS0,
+                                     ConstantInt::getNullValue(LHS0->getType()));
       if (!IsAnd && TrueIfSigned)
-        return Builder.CreateICmpSLE(
-            LHS0, ConstantInt::getNullValue(LHS0->getType()));
+        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()))) {
+        match(LHS0, m_And(m_Specific(RHS0), m_APInt(MaskC))) &&
+        IsValidMask(RHS0, *MaskC)) {
       if (IsAnd && !TrueIfSigned)
-        return Builder.CreateICmpSGT(
-            RHS0, ConstantInt::getNullValue(RHS0->getType()));
+        return Builder.CreateICmpSGT(RHS0,
+                                     ConstantInt::getNullValue(RHS0->getType()));
       if (!IsAnd && TrueIfSigned)
-        return Builder.CreateICmpSLE(
-            RHS0, ConstantInt::getNullValue(RHS0->getType()));
+        return Builder.CreateICmpSLE(RHS0,
+                                     ConstantInt::getNullValue(RHS0->getType()));
     }
   }
 
diff --git a/llvm/test/Transforms/InstCombine/sign-bitwise-check.ll b/llvm/test/Transforms/InstCombine/sign-bitwise-check.ll
index 2b404e56c58fc..d2dcfa3d4d6bc 100644
--- a/llvm/test/Transforms/InstCombine/sign-bitwise-check.ll
+++ b/llvm/test/Transforms/InstCombine/sign-bitwise-check.ll
@@ -73,3 +73,23 @@ define i1 @test_and_commuted(i32 %A) {
   ret i1 %res
 }
 
+
+define i1 @test_known_bits(i32 %A) {
+  ; clear bit 30
+  %A_and = and i32 %A, -1073741825
+  %cmp1 = icmp sge i32 %A_and, 0
+  %and = and i32 %A_and, 1073741823
+  %cmp2 = icmp ne i32 %and, 0
+  %res = and i1 %cmp1, %cmp2
+  ret i1 %res
+}
+
+define i1 @test_known_bits_or(i32 %A) {
+  ; clear bit 30
+  %A_and = and i32 %A, -1073741825
+  %cmp1 = icmp slt i32 %A_and, 0
+  %and = and i32 %A_and, 1073741823
+  %cmp2 = icmp eq i32 %and, 0
+  %res = or i1 %cmp1, %cmp2
+  ret i1 %res
+}



More information about the llvm-commits mailing list