From 2aafd9481460d188ef97bf8c3cf3781cc65b6be3 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Wed, 2 Oct 2024 11:12:17 -0500 Subject: [PATCH 1/4] [InstCombine] Add tests for folding `(icmp eq/ne (and X, -P2), INT_MIN)`; NFC --- .../Transforms/InstCombine/icmp-signmask.ll | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/icmp-signmask.ll diff --git a/llvm/test/Transforms/InstCombine/icmp-signmask.ll b/llvm/test/Transforms/InstCombine/icmp-signmask.ll new file mode 100644 index 0000000000000..bea8da2074ab0 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/icmp-signmask.ll @@ -0,0 +1,57 @@ +; 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: [[ANDX:%.*]] = and i8 [[X:%.*]], -2 +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ANDX]], -128 +; 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: [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], +; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %andx = and <2 x i8> %x, + %r = icmp ne <2 x i8> %andx, + 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: [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], +; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %andx = and <2 x i8> %x, + %r = icmp ne <2 x i8> %andx, + 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:%.*]], +; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %andx = and <2 x i8> %x, + %r = icmp ne <2 x i8> %andx, + ret <2 x i1> %r +} From 4e5124921f18b541053893cc1c1146567e3e9a24 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Wed, 2 Oct 2024 11:12:26 -0500 Subject: [PATCH 2/4] [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 --- .../InstCombine/InstCombineCompares.cpp | 16 ++++++++++++++++ .../test/Transforms/InstCombine/icmp-signmask.ll | 9 +++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index b0771ffcc38a8..5e1d9a1c52b2f 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 index bea8da2074ab0..5424f7d7e8021 100644 --- a/llvm/test/Transforms/InstCombine/icmp-signmask.ll +++ b/llvm/test/Transforms/InstCombine/icmp-signmask.ll @@ -3,8 +3,7 @@ define i1 @cmp_x_and_negp2_with_eq(i8 %x) { ; CHECK-LABEL: @cmp_x_and_negp2_with_eq( -; CHECK-NEXT: [[ANDX:%.*]] = and i8 [[X:%.*]], -2 -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ANDX]], -128 +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X:%.*]], -126 ; CHECK-NEXT: ret i1 [[R]] ; %andx = and i8 %x, -2 @@ -25,8 +24,7 @@ define i1 @cmp_x_and_negp2_with_eq_fail_not_signmask(i8 %x) { define <2 x i1> @cmp_x_and_negp2_with_ne(<2 x i8> %x) { ; CHECK-LABEL: @cmp_x_and_negp2_with_ne( -; CHECK-NEXT: [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], -; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], +; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], ; CHECK-NEXT: ret <2 x i1> [[R]] ; %andx = and <2 x i8> %x, @@ -36,8 +34,7 @@ define <2 x i1> @cmp_x_and_negp2_with_ne(<2 x i8> %x) { 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: [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], -; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], +; CHECK-NEXT: [[R:%.*]] = icmp sge <2 x i8> [[X:%.*]], ; CHECK-NEXT: ret <2 x i1> [[R]] ; %andx = and <2 x i8> %x, From c00badd704265a06c7feccc34e21c53eda0abd9f Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Wed, 2 Oct 2024 14:59:42 -0500 Subject: [PATCH 3/4] Fixup failing test --- llvm/test/Transforms/InstCombine/icmp.ll | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll index ecf21b8a42cf5..17ed64b6e04d9 100644 --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -1116,8 +1116,7 @@ define i1 @test53(i32 %a, i32 %b) { define i1 @test54(i8 %a) { ; CHECK-LABEL: @test54( -; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[A:%.*]], -64 -; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], -128 +; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[A:%.*]], -64 ; CHECK-NEXT: ret i1 [[RET]] ; %ext = zext i8 %a to i32 From 4dc758fd2518e62684526c9acc2a3d15d6cd5433 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Thu, 3 Oct 2024 11:29:55 -0500 Subject: [PATCH 4/4] Better matcher --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 5e1d9a1c52b2f..c780704a6b128 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -5020,9 +5020,7 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I, 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(); - })))) { + if (match(BO0, m_And(m_Value(X), m_NegatedPower2OrZero()))) { // Will Constant fold. Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1)); return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT