Skip to content

[InstCombine] Simplify (add/sub (sub/add) (sub/add)) irrelevant of use-count #105866

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 5 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
33 changes: 33 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,12 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
return R;

Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
if (Instruction *R = foldAddLikeCommutative(LHS, RHS, I.hasNoSignedWrap(),
I.hasNoUnsignedWrap()))
return R;
if (Instruction *R = foldAddLikeCommutative(RHS, LHS, I.hasNoSignedWrap(),
I.hasNoUnsignedWrap()))
return R;
Type *Ty = I.getType();
if (Ty->isIntOrIntVectorTy(1))
return BinaryOperator::CreateXor(LHS, RHS);
Expand Down Expand Up @@ -2286,6 +2292,33 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
}
}

{
Value *W, *Z;
if (match(Op0, m_AddLike(m_Value(W), m_Value(X))) &&
match(Op1, m_AddLike(m_Value(Y), m_Value(Z)))) {
Instruction *R = nullptr;
if (W == Y)
R = BinaryOperator::CreateSub(X, Z);
else if (W == Z)
R = BinaryOperator::CreateSub(X, Y);
else if (X == Y)
R = BinaryOperator::CreateSub(W, Z);
else if (X == Z)
R = BinaryOperator::CreateSub(W, Y);
if (R) {
bool NSW = I.hasNoSignedWrap() &&
match(Op0, m_NSWAddLike(m_Value(), m_Value())) &&
match(Op1, m_NSWAddLike(m_Value(), m_Value()));

bool NUW = I.hasNoUnsignedWrap() &&
match(Op1, m_NUWAddLike(m_Value(), m_Value()));
R->setHasNoSignedWrap(NSW);
R->setHasNoUnsignedWrap(NUW);
return R;
}
}
}

// (~X) - (~Y) --> Y - X
{
// Need to ensure we can consume at least one of the `not` instructions,
Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3580,6 +3580,17 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
if (Instruction *R = tryFoldInstWithCtpopWithNot(&I))
return R;

if (cast<PossiblyDisjointInst>(I).isDisjoint()) {
if (Instruction *R =
foldAddLikeCommutative(I.getOperand(0), I.getOperand(1),
/*NSW=*/true, /*NUW=*/true))
return R;
if (Instruction *R =
foldAddLikeCommutative(I.getOperand(1), I.getOperand(0),
/*NSW=*/true, /*NUW=*/true))
return R;
}

Value *X, *Y;
const APInt *CV;
if (match(&I, m_c_Or(m_OneUse(m_Xor(m_Value(X), m_APInt(CV))), m_Value(Y))) &&
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
FPClassTest DemandedMask, KnownFPClass &Known,
unsigned Depth = 0);

/// Common transforms for add / disjoint or
Instruction *foldAddLikeCommutative(Value *LHS, Value *RHS, bool NSW,
bool NUW);

/// Canonicalize the position of binops relative to shufflevector.
Instruction *foldVectorBinop(BinaryOperator &Inst);
Instruction *foldVectorSelect(SelectInst &Sel);
Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,24 @@ static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src) {
return true;
}

Instruction *InstCombinerImpl::foldAddLikeCommutative(Value *LHS, Value *RHS,
bool NSW, bool NUW) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this into InstCombineAddSub.cpp. It will make it easier to move folds if necessary.

Value *A, *B, *C;
if (match(LHS, m_Sub(m_Value(A), m_Value(B))) &&
match(RHS, m_Sub(m_Value(C), m_Specific(A)))) {
Instruction *R = BinaryOperator::CreateSub(C, B);
bool NSWOut = NSW && match(LHS, m_NSWSub(m_Value(), m_Value())) &&
match(RHS, m_NSWSub(m_Value(), m_Value()));

bool NUWOut = match(LHS, m_NUWSub(m_Value(), m_Value())) &&
match(RHS, m_NUWSub(m_Value(), m_Value()));
R->setHasNoSignedWrap(NSWOut);
R->setHasNoUnsignedWrap(NUWOut);
return R;
}
return nullptr;
}

Instruction *InstCombinerImpl::foldVectorBinop(BinaryOperator &Inst) {
if (!isa<VectorType>(Inst.getType()))
return nullptr;
Expand Down
207 changes: 207 additions & 0 deletions llvm/test/Transforms/InstCombine/fold-add-sub.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

declare void @use.i8(i8)
define i8 @test_add_nsw(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_add_nsw(
; CHECK-NEXT: [[LHS:%.*]] = add nsw i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = add nsw i8 [[X]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub nsw i8 [[Y]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = add nsw i8 %x, %y
%rhs = add nsw i8 %x, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = sub nsw i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_add_nsw_no_prop(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_add_nsw_no_prop(
; CHECK-NEXT: [[LHS:%.*]] = add nsw i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = add nuw i8 [[X]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[Y]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = add nsw i8 %x, %y
%rhs = add nuw i8 %x, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = sub nsw i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_add(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_add(
; CHECK-NEXT: [[LHS:%.*]] = add i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = add i8 [[X]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[Y]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = add i8 %x, %y
%rhs = add i8 %x, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = sub i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_add_fail(i8 %w, i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_add_fail(
; CHECK-NEXT: [[LHS:%.*]] = add i8 [[W:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = add i8 [[X:%.*]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[LHS]], [[RHS]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = add i8 %w, %y
%rhs = add i8 %x, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = sub i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_add_nuw(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_add_nuw(
; CHECK-NEXT: [[LHS:%.*]] = add i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = or disjoint i8 [[X]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub nuw i8 [[Y]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = add i8 %x, %y
%rhs = or disjoint i8 %x, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = sub nuw i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_add_nuw_no_prop(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_add_nuw_no_prop(
; CHECK-NEXT: [[LHS:%.*]] = add i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = or disjoint i8 [[X]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[Y]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = add i8 %x, %y
%rhs = or disjoint i8 %x, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = sub i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_sub_nuw(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_sub_nuw(
; CHECK-NEXT: [[LHS:%.*]] = sub nuw i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = sub nuw i8 [[Y]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub nuw i8 [[X]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = sub nuw i8 %x, %y
%rhs = sub nuw i8 %y, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = add i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_sub_nuw_no_prop(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_sub_nuw_no_prop(
; CHECK-NEXT: [[LHS:%.*]] = sub nuw i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = sub i8 [[Y]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = sub nuw i8 %x, %y
%rhs = sub i8 %y, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = add nuw i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_sub_nsw(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_sub_nsw(
; CHECK-NEXT: [[LHS:%.*]] = sub nsw i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = sub nsw i8 [[Y]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub nsw i8 [[X]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = sub nsw i8 %x, %y
%rhs = sub nsw i8 %y, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = or disjoint i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_sub_nsw_no_prop(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_sub_nsw_no_prop(
; CHECK-NEXT: [[LHS:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = sub nsw i8 [[Y]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = sub i8 %x, %y
%rhs = sub nsw i8 %y, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = or disjoint i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_sub_none(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_sub_none(
; CHECK-NEXT: [[LHS:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = sub i8 [[Y]], [[Z:%.*]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X]], [[Z]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = sub i8 %x, %y
%rhs = sub i8 %y, %z
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = add i8 %lhs, %rhs
ret i8 %r
}

define i8 @test_sub_none_fail(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @test_sub_none_fail(
; CHECK-NEXT: [[LHS:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = sub i8 [[Z:%.*]], [[Y]]
; CHECK-NEXT: call void @use.i8(i8 [[LHS]])
; CHECK-NEXT: call void @use.i8(i8 [[RHS]])
; CHECK-NEXT: [[R:%.*]] = add i8 [[LHS]], [[RHS]]
; CHECK-NEXT: ret i8 [[R]]
;
%lhs = sub i8 %x, %y
%rhs = sub i8 %z, %y
call void @use.i8(i8 %lhs)
call void @use.i8(i8 %rhs)
%r = add i8 %lhs, %rhs
ret i8 %r
}
Loading