Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[InstCombine] Folding (icmp eq/ne (and X, -P2), INT_MIN) #110880

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a Value *X declaration above, so you can drop this one and merge both ifs.

if (match(BO0, m_And(m_Value(X), m_CheckedInt([](const APInt &C) {
return C.isZero() || C.isNegatedPowerOf2();
})))) {
goldsteinn marked this conversation as resolved.
Show resolved Hide resolved
// 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
Expand Down
54 changes: 54 additions & 0 deletions llvm/test/Transforms/InstCombine/icmp-signmask.ll
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstCombine/icmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading