Skip to content

[InstCombine] Propagate nsw in (sub (and Op1, (neg X)), Op1) --> neg (and Op1, (add X, -1)) #149366

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 19 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2588,10 +2588,25 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
{
// (sub (and Op1, (neg X)), Op1) --> neg (and Op1, (add X, -1))
Value *X;
if (match(Op0, m_OneUse(m_c_And(m_Specific(Op1),
m_OneUse(m_Neg(m_Value(X))))))) {
return BinaryOperator::CreateNeg(Builder.CreateAnd(
Op1, Builder.CreateAdd(X, Constant::getAllOnesValue(I.getType()))));
BinaryOperator *NegInst;
if (match(Op0,
m_OneUse(m_c_And(m_Specific(Op1), m_OneUse(m_BinOp(NegInst))))) &&
match(NegInst, m_Neg(m_Value(X)))) {

// If neg X is nsw, then X - 1 must be too
bool AddHasNSW = NegInst->hasNoSignedWrap();

// If the original sub is nsw, then the final neg must be too
bool NegHasNSW = I.hasNoSignedWrap();

Value *Add =
Builder.CreateAdd(X, Constant::getAllOnesValue(I.getType()), "",
/*HasNUW=*/false, /*HasNSW=*/AddHasNSW);
Value *And = Builder.CreateAnd(Op1, Add);

// Use CreateNSWNeg or CreateNeg based on the flag
return NegHasNSW ? BinaryOperator::CreateNSWNeg(And)
: BinaryOperator::CreateNeg(And);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,45 @@ define i8 @t0(i8 %x, i8 %y) {
ret i8 %negbias
}

define i8 @t0_nsw(i8 %x, i8 %y) {
; CHECK-LABEL: @t0_nsw(
; CHECK-NEXT: [[TMP1:%.*]] = add nsw i8 [[Y:%.*]], -1
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X:%.*]], [[TMP1]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub i8 0, [[TMP2]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub nsw i8 0, %y
%unbiasedx = and i8 %negy, %x
%negbias = sub i8 %unbiasedx, %x
ret i8 %negbias
}

define i8 @t0_nsw_2(i8 %x, i8 %y) {
; CHECK-LABEL: @t0_nsw_2(
; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], -1
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X:%.*]], [[TMP1]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub nsw i8 0, [[TMP2]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub i8 0, %y
%unbiasedx = and i8 %negy, %x
%negbias = sub nsw i8 %unbiasedx, %x
ret i8 %negbias
}

define i8 @t0_nsw_3(i8 %x, i8 %y) {
; CHECK-LABEL: @t0_nsw_3(
; CHECK-NEXT: [[TMP1:%.*]] = add nsw i8 [[Y:%.*]], -1
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X:%.*]], [[TMP1]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub nsw i8 0, [[TMP2]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub nsw i8 0, %y
%unbiasedx = and i8 %negy, %x
%negbias = sub nsw i8 %unbiasedx, %x
ret i8 %negbias
}

declare i8 @gen8()

define i8 @t1_commutative(i8 %y) {
Expand Down
Loading