Skip to content

[InstCombine] Extend (icmp eq/ne (and Z, X), (and Z, Y)) folds #94867

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
27 changes: 21 additions & 6 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5548,8 +5548,8 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
}

// (X&Z) == (Y&Z) -> (X^Y) & Z == 0
if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
match(Op1, m_And(m_Value(C), m_Value(D)))) {
Value *X = nullptr, *Y = nullptr, *Z = nullptr;

if (A == C) {
Expand All @@ -5570,10 +5570,25 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
Z = B;
}

if (X) { // Build (X^Y) & Z
Op1 = Builder.CreateXor(X, Y);
Op1 = Builder.CreateAnd(Op1, Z);
return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
if (X) {
// If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
// will fold to `icmp ult/uge Z, -NegP2` incuring no additional uses.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// will fold to `icmp ult/uge Z, -NegP2` incuring no additional uses.
// will fold to `icmp ult/uge Z, -NegP2` incurring no additional instructions.

const APInt *C0, *C1;
bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
(*C0 ^ *C1).isNegatedPowerOf2();

// If either Op0/Op1 are both one use or X^Y will constant fold and one of
// Op0/Op1 are one use proceeed. In those cases we are instruction neutral
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Op0/Op1 are one use proceeed. In those cases we are instruction neutral
// Op0/Op1 are one use, proceed. In those cases we are instruction neutral

// but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`
// but `icmp eq/ne A, 0` is easier to analyze than `icmp eq/ne A, B`.

int UseCnt =
int(Op0->hasOneUse()) + int(Op1->hasOneUse()) +
(int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
(int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant())));
int(match(X, m_ImmConstant()) && match(Y, m_ImmConstant()));

if (XorIsNegP2 || UseCnt >= 2) {
// Build (X^Y) & Z
Op1 = Builder.CreateXor(X, Y);
Op1 = Builder.CreateAnd(Op1, Z);
return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
}
}
}

Expand Down
97 changes: 97 additions & 0 deletions llvm/test/Transforms/InstCombine/and-compare.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

declare void @use.i8(i8)

; Should be optimized to one and.
define i1 @test1(i32 %a, i32 %b) {
; CHECK-LABEL: @test1(
Expand Down Expand Up @@ -75,3 +77,98 @@ define <2 x i1> @test3vec(<2 x i64> %A) {
ret <2 x i1> %cmp
}

define i1 @test_ne_cp2(i8 %x, i8 %yy) {
; CHECK-LABEL: @test_ne_cp2(
; CHECK-NEXT: [[AND_X_NEG_Y:%.*]] = and i8 [[X:%.*]], -16
; CHECK-NEXT: [[AND_X_Y:%.*]] = and i8 [[X]], 16
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_NEG_Y]])
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_Y]])
; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[X]], 31
; CHECK-NEXT: ret i1 [[R]]
;
%and_x_neg_y = and i8 %x, -16
%and_x_y = and i8 %x, 16
call void @use.i8(i8 %and_x_neg_y)
call void @use.i8(i8 %and_x_y)
%r = icmp ne i8 %and_x_neg_y, %and_x_y
ret i1 %r
}

define i1 @test_ne_cp2_2(i8 %x, i8 %yy) {
; CHECK-LABEL: @test_ne_cp2_2(
; CHECK-NEXT: [[AND_X_NEG_Y:%.*]] = and i8 [[X:%.*]], -4
; CHECK-NEXT: [[AND_X_Y:%.*]] = and i8 [[X]], 4
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_NEG_Y]])
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_Y]])
; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[X]], 8
; CHECK-NEXT: ret i1 [[R]]
;
%and_x_neg_y = and i8 %x, -4
%and_x_y = and i8 %x, 4
call void @use.i8(i8 %and_x_neg_y)
call void @use.i8(i8 %and_x_y)
%r = icmp eq i8 %and_x_y, %and_x_neg_y
ret i1 %r
}

define i1 @test_ne_cp2_other_okay_all_ones(i8 %x, i8 %yy) {
; CHECK-LABEL: @test_ne_cp2_other_okay_all_ones(
; CHECK-NEXT: [[AND_X_NEG_Y:%.*]] = and i8 [[X:%.*]], -17
; CHECK-NEXT: [[AND_X_Y:%.*]] = and i8 [[X]], 16
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_NEG_Y]])
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_Y]])
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%and_x_neg_y = and i8 %x, -17
%and_x_y = and i8 %x, 16
call void @use.i8(i8 %and_x_neg_y)
call void @use.i8(i8 %and_x_y)
%r = icmp ne i8 %and_x_neg_y, %and_x_y
ret i1 %r
}

define i1 @test_ne_cp2_other_fail2(i8 %x, i8 %yy) {
; CHECK-LABEL: @test_ne_cp2_other_fail2(
; CHECK-NEXT: [[AND_X_NEG_Y:%.*]] = and i8 [[X:%.*]], -16
; CHECK-NEXT: [[AND_X_Y:%.*]] = and i8 [[X]], 17
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_NEG_Y]])
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_Y]])
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[AND_X_NEG_Y]], [[AND_X_Y]]
; CHECK-NEXT: ret i1 [[R]]
;
%and_x_neg_y = and i8 %x, -16
%and_x_y = and i8 %x, 17
call void @use.i8(i8 %and_x_neg_y)
call void @use.i8(i8 %and_x_y)
%r = icmp ne i8 %and_x_neg_y, %and_x_y
ret i1 %r
}

define i1 @test_ne_cp2_other_okay(i8 %x, i8 %yy) {
; CHECK-LABEL: @test_ne_cp2_other_okay(
; CHECK-NEXT: [[AND_X_Y:%.*]] = and i8 [[X:%.*]], 16
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_Y]])
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%and_x_neg_y = and i8 %x, -17
%and_x_y = and i8 %x, 16
call void @use.i8(i8 %and_x_y)
%r = icmp ne i8 %and_x_neg_y, %and_x_y
ret i1 %r
}

define i1 @test_ne_cp2_other_okay2(i8 %x, i8 %yy) {
; CHECK-LABEL: @test_ne_cp2_other_okay2(
; CHECK-NEXT: [[AND_X_Y:%.*]] = and i8 [[X:%.*]], 16
; CHECK-NEXT: call void @use.i8(i8 [[AND_X_Y]])
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%and_x_neg_y = and i8 %x, -17
%and_x_y = and i8 %x, 16
call void @use.i8(i8 %and_x_y)
%r = icmp ne i8 %and_x_y, %and_x_neg_y
ret i1 %r
}
Loading