[llvm] [InstCombine] Folding `(icmp eq/ne (and X, -P2), INT_MIN)` (PR #110880)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 2 09:21:48 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: None (goldsteinn)
<details>
<summary>Changes</summary>
- **[InstCombine] Add tests for folding `(icmp eq/ne (and X, -P2), INT_MIN)`; NFC**
- **[InstCombine] Folding `(icmp eq/ne (and X, -P2), INT_MIN)`**
Folds to `(icmp slt/sge X, (INT_MIN + P2))`
Proofs: https://alive2.llvm.org/ce/z/vpNFY5
---
Full diff: https://github.com/llvm/llvm-project/pull/110880.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp (+16)
- (added) llvm/test/Transforms/InstCombine/icmp-signmask.ll (+54)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index b0771ffcc38a83..5e1d9a1c52b2f2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5015,6 +5015,22 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
}
}
+ // (icmp eq/ne (X, -P2), INT_MIN)
+ // -> (icmp slt/sge X, INT_MIN + P2)
+ if (ICmpInst::isEquality(Pred) && BO0 &&
+ match(I.getOperand(1), m_SignMask())) {
+ Value *X;
+ if (match(BO0, m_And(m_Value(X), m_CheckedInt([](const APInt &C) {
+ return C.isZero() || C.isNegatedPowerOf2();
+ })))) {
+ // Will Constant fold.
+ Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));
+ return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
+ : ICmpInst::ICMP_SGE,
+ X, NewC);
+ }
+ }
+
{
// Similar to above: an unsigned overflow comparison may use offset + mask:
// ((Op1 + C) & C) u< Op1 --> Op1 != 0
diff --git a/llvm/test/Transforms/InstCombine/icmp-signmask.ll b/llvm/test/Transforms/InstCombine/icmp-signmask.ll
new file mode 100644
index 00000000000000..5424f7d7e8021f
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-signmask.ll
@@ -0,0 +1,54 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i1 @cmp_x_and_negp2_with_eq(i8 %x) {
+; CHECK-LABEL: @cmp_x_and_negp2_with_eq(
+; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X:%.*]], -126
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %andx = and i8 %x, -2
+ %r = icmp eq i8 %andx, 128
+ ret i1 %r
+}
+
+define i1 @cmp_x_and_negp2_with_eq_fail_not_signmask(i8 %x) {
+; CHECK-LABEL: @cmp_x_and_negp2_with_eq_fail_not_signmask(
+; CHECK-NEXT: [[ANDX:%.*]] = and i8 [[X:%.*]], -2
+; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ANDX]], -124
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %andx = and i8 %x, -2
+ %r = icmp eq i8 %andx, 132
+ ret i1 %r
+}
+
+define <2 x i1> @cmp_x_and_negp2_with_ne(<2 x i8> %x) {
+; CHECK-LABEL: @cmp_x_and_negp2_with_ne(
+; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 -121, i8 -113>
+; CHECK-NEXT: ret <2 x i1> [[R]]
+;
+ %andx = and <2 x i8> %x, <i8 -8, i8 -16>
+ %r = icmp ne <2 x i8> %andx, <i8 128, i8 128>
+ ret <2 x i1> %r
+}
+
+define <2 x i1> @cmp_x_and_negp2_with_ne_or_z(<2 x i8> %x) {
+; CHECK-LABEL: @cmp_x_and_negp2_with_ne_or_z(
+; CHECK-NEXT: [[R:%.*]] = icmp sge <2 x i8> [[X:%.*]], <i8 -128, i8 -112>
+; CHECK-NEXT: ret <2 x i1> [[R]]
+;
+ %andx = and <2 x i8> %x, <i8 0, i8 -16>
+ %r = icmp ne <2 x i8> %andx, <i8 128, i8 128>
+ ret <2 x i1> %r
+}
+
+define <2 x i1> @cmp_x_and_negp2_with_ne_fail_not_p2(<2 x i8> %x) {
+; CHECK-LABEL: @cmp_x_and_negp2_with_ne_fail_not_p2(
+; CHECK-NEXT: [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], <i8 -8, i8 -15>
+; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], <i8 -128, i8 -128>
+; CHECK-NEXT: ret <2 x i1> [[R]]
+;
+ %andx = and <2 x i8> %x, <i8 -8, i8 -15>
+ %r = icmp ne <2 x i8> %andx, <i8 128, i8 128>
+ ret <2 x i1> %r
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/110880
More information about the llvm-commits
mailing list