Skip to content

[InstCombine] Fold (X & C1) - (X & C2) --> X & (C1 ^ C2) if (C1 & C2) == C2 #119316

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

Merged
merged 2 commits into from
Dec 10, 2024
Merged
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
10 changes: 10 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,16 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
if (match(Op0, m_OneUse(m_Add(m_Value(X), m_AllOnes()))))
return BinaryOperator::CreateAdd(Builder.CreateNot(Op1), X);

// if (C1 & C2) == C2 then (X & C1) - (X & C2) -> X & (C1 ^ C2)
Constant *C1, *C2;
if (match(Op0, m_And(m_Value(X), m_ImmConstant(C1))) &&
match(Op1, m_And(m_Specific(X), m_ImmConstant(C2)))) {
Value *AndC = ConstantFoldBinaryInstruction(Instruction::And, C1, C2);
if (C2->isElementWiseEqual(AndC))
return BinaryOperator::CreateAnd(
X, ConstantFoldBinaryInstruction(Instruction::Xor, C1, C2));
}

// Reassociate sub/add sequences to create more add instructions and
// reduce dependency chains:
// ((X - Y) + Z) - Op1 --> (X + Z) - (Y + Op1)
Expand Down
83 changes: 83 additions & 0 deletions llvm/test/Transforms/InstCombine/and-sub-combine.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define i8 @and_sub(i8 %a) {
; CHECK-LABEL: @and_sub(
; CHECK-NEXT: [[RET:%.*]] = and i8 [[A:%.*]], 12
; CHECK-NEXT: ret i8 [[RET]]
;
%and1 = and i8 %a, 15
%and2 = and i8 %a, 3

%ret = sub i8 %and1, %and2
ret i8 %ret
}

declare void @use(i8)
define i8 @and_sub_multi_use(i8 %a) {
; CHECK-LABEL: @and_sub_multi_use(
; CHECK-NEXT: [[AND1:%.*]] = and i8 [[A:%.*]], 15
; CHECK-NEXT: call void @use(i8 [[AND1]])
; CHECK-NEXT: [[AND2:%.*]] = and i8 [[A]], 3
; CHECK-NEXT: call void @use(i8 [[AND2]])
; CHECK-NEXT: [[RET:%.*]] = and i8 [[A]], 12
; CHECK-NEXT: ret i8 [[RET]]
;
%and1 = and i8 %a, 15
call void @use(i8 %and1)
%and2 = and i8 %a, 3
call void @use(i8 %and2)
%ret = sub i8 %and1, %and2
ret i8 %ret
}

define <2 x i32> @and_sub_vec(<2 x i32> %a) {
; CHECK-LABEL: @and_sub_vec(
; CHECK-NEXT: [[RET:%.*]] = and <2 x i32> [[A:%.*]], <i32 3, i32 8>
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%and1 = and <2 x i32> %a, <i32 11, i32 10>
%and2 = and <2 x i32> %a, <i32 8, i32 2>

%ret = sub <2 x i32> %and1, %and2
ret <2 x i32> %ret
}

define <2 x i32> @and_sub_vec_posion(<2 x i32> %a) {
; CHECK-LABEL: @and_sub_vec_posion(
; CHECK-NEXT: ret <2 x i32> poison
;
%and1 = and <2 x i32> %a, <i32 11, i32 poison>
%and2 = and <2 x i32> %a, <i32 poison, i32 2>

%ret = sub <2 x i32> %and1, %and2
ret <2 x i32> %ret
}

define <2 x i32> @and_sub_vec_undef(<2 x i32> %a) {
; CHECK-LABEL: @and_sub_vec_undef(
; CHECK-NEXT: [[AND1:%.*]] = and <2 x i32> [[A:%.*]], <i32 11, i32 undef>
; CHECK-NEXT: [[AND2:%.*]] = and <2 x i32> [[A]], <i32 undef, i32 2>
; CHECK-NEXT: [[RET:%.*]] = sub <2 x i32> [[AND1]], [[AND2]]
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%and1 = and <2 x i32> %a, <i32 11, i32 undef>
%and2 = and <2 x i32> %a, <i32 undef, i32 2>

%ret = sub <2 x i32> %and1, %and2
ret <2 x i32> %ret
}

define i8 @and_sub_negtive(i8 %a) {
; CHECK-LABEL: @and_sub_negtive(
; CHECK-NEXT: [[AND1:%.*]] = and i8 [[A:%.*]], 10
; CHECK-NEXT: [[AND2:%.*]] = and i8 [[A]], 9
; CHECK-NEXT: [[RET:%.*]] = sub nsw i8 [[AND1]], [[AND2]]
; CHECK-NEXT: ret i8 [[RET]]
;
%and1 = and i8 %a, 10
%and2 = and i8 %a, 9

%ret = sub i8 %and1, %and2
ret i8 %ret
}
Loading