Skip to content

[IR][InstCombine] Add N{U,S}WAddLike matchers. #86082

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 3 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
20 changes: 20 additions & 0 deletions llvm/include/llvm/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,26 @@ m_AddLike(const LHS &L, const RHS &R) {
return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
}

/// Match either "add nsw" or "or disjoint"
template <typename LHS, typename RHS>
inline match_combine_or<
OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
OverflowingBinaryOperator::NoSignedWrap>,
DisjointOr_match<LHS, RHS>>
m_NSWAddLike(const LHS &L, const RHS &R) {
return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
}

/// Match either "add nuw" or "or disjoint"
template <typename LHS, typename RHS>
inline match_combine_or<
OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
OverflowingBinaryOperator::NoUnsignedWrap>,
DisjointOr_match<LHS, RHS>>
m_NUWAddLike(const LHS &L, const RHS &R) {
return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
}

//===----------------------------------------------------------------------===//
// Class that matches a group of binary opcodes.
//
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
Value *X;
const APInt *C1, *C2;
if (match(Op1, m_APInt(C1)) &&
match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_APInt(C2))))) &&
match(Op0, m_OneUse(m_ZExt(m_NUWAddLike(m_Value(X), m_APInt(C2))))) &&
C1->isNegative() && C1->sge(-C2->sext(C1->getBitWidth()))) {
Constant *NewC =
ConstantInt::get(X->getType(), *C2 + C1->trunc(C2->getBitWidth()));
Expand All @@ -829,14 +829,16 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
// More general combining of constants in the wide type.
// (sext (X +nsw NarrowC)) + C --> (sext X) + (sext(NarrowC) + C)
Constant *NarrowC;
if (match(Op0, m_OneUse(m_SExt(m_NSWAdd(m_Value(X), m_Constant(NarrowC)))))) {
if (match(Op0,
m_OneUse(m_SExt(m_NSWAddLike(m_Value(X), m_Constant(NarrowC)))))) {
Value *WideC = Builder.CreateSExt(NarrowC, Ty);
Value *NewC = Builder.CreateAdd(WideC, Op1C);
Value *WideX = Builder.CreateSExt(X, Ty);
return BinaryOperator::CreateAdd(WideX, NewC);
}
// (zext (X +nuw NarrowC)) + C --> (zext X) + (zext(NarrowC) + C)
if (match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_Constant(NarrowC)))))) {
if (match(Op0,
m_OneUse(m_ZExt(m_NUWAddLike(m_Value(X), m_Constant(NarrowC)))))) {
Value *WideC = Builder.CreateZExt(NarrowC, Ty);
Value *NewC = Builder.CreateAdd(WideC, Op1C);
Value *WideX = Builder.CreateZExt(X, Ty);
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2093,8 +2093,9 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
Value *Arg0 = II->getArgOperand(0);
Value *Arg1 = II->getArgOperand(1);
bool IsSigned = IID == Intrinsic::sadd_with_overflow;
bool HasNWAdd = IsSigned ? match(Arg0, m_NSWAdd(m_Value(X), m_APInt(C0)))
: match(Arg0, m_NUWAdd(m_Value(X), m_APInt(C0)));
bool HasNWAdd = IsSigned
? match(Arg0, m_NSWAddLike(m_Value(X), m_APInt(C0)))
: match(Arg0, m_NUWAddLike(m_Value(X), m_APInt(C0)));
if (HasNWAdd && match(Arg1, m_APInt(C1))) {
bool Overflow;
APInt NewC =
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,14 +1160,14 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
// We need a multiple of the divisor for a signed add constant, but
// unsigned is fine with any constant pair.
if (IsSigned &&
match(Op0, m_NSWAdd(m_NSWMul(m_Value(X), m_SpecificInt(*C2)),
m_APInt(C1))) &&
match(Op0, m_NSWAddLike(m_NSWMul(m_Value(X), m_SpecificInt(*C2)),
m_APInt(C1))) &&
isMultiple(*C1, *C2, Quotient, IsSigned)) {
return BinaryOperator::CreateNSWAdd(X, ConstantInt::get(Ty, Quotient));
}
if (!IsSigned &&
match(Op0, m_NUWAdd(m_NUWMul(m_Value(X), m_SpecificInt(*C2)),
m_APInt(C1)))) {
match(Op0, m_NUWAddLike(m_NUWMul(m_Value(X), m_SpecificInt(*C2)),
m_APInt(C1)))) {
return BinaryOperator::CreateNUWAdd(X,
ConstantInt::get(Ty, C1->udiv(*C2)));
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
Value *A;
Constant *C, *C1;
if (match(Op0, m_Constant(C)) &&
match(Op1, m_NUWAdd(m_Value(A), m_Constant(C1)))) {
match(Op1, m_NUWAddLike(m_Value(A), m_Constant(C1)))) {
Value *NewC = Builder.CreateBinOp(I.getOpcode(), C, C1);
BinaryOperator *NewShiftOp = BinaryOperator::Create(I.getOpcode(), NewC, A);
if (I.getOpcode() == Instruction::Shl) {
Expand Down
76 changes: 76 additions & 0 deletions llvm/test/Transforms/InstCombine/add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3986,5 +3986,81 @@ define i32 @add_reduce_sqr_sum_varC_invalid2(i32 %a, i32 %b) {
ret i32 %ab2
}

define i32 @fold_sext_addition_or_disjoint(i8 %x) {
; CHECK-LABEL: @fold_sext_addition_or_disjoint(
; CHECK-NEXT: [[SE:%.*]] = sext i8 [[XX:%.*]] to i32
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], 1246
; CHECK-NEXT: ret i32 [[R]]
;
%xx = or disjoint i8 %x, 12
%se = sext i8 %xx to i32
%r = add i32 %se, 1234
ret i32 %r
}

define i32 @fold_sext_addition_fail(i8 %x) {
; CHECK-LABEL: @fold_sext_addition_fail(
; CHECK-NEXT: [[XX:%.*]] = or i8 [[X:%.*]], 12
; CHECK-NEXT: [[SE:%.*]] = sext i8 [[XX]] to i32
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], 1234
; CHECK-NEXT: ret i32 [[R]]
;
%xx = or i8 %x, 12
%se = sext i8 %xx to i32
%r = add i32 %se, 1234
ret i32 %r
}

define i32 @fold_zext_addition_or_disjoint(i8 %x) {
; CHECK-LABEL: @fold_zext_addition_or_disjoint(
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX:%.*]] to i32
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[SE]], 1246
; CHECK-NEXT: ret i32 [[R]]
;
%xx = or disjoint i8 %x, 12
%se = zext i8 %xx to i32
%r = add i32 %se, 1234
ret i32 %r
}

define i32 @fold_zext_addition_or_disjoint2(i8 %x) {
; CHECK-LABEL: @fold_zext_addition_or_disjoint2(
; CHECK-NEXT: [[XX:%.*]] = add nuw i8 [[X:%.*]], 4
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX]] to i32
; CHECK-NEXT: ret i32 [[SE]]
;
%xx = or disjoint i8 %x, 18
%se = zext i8 %xx to i32
%r = add i32 %se, -14
ret i32 %r
}

define i32 @fold_zext_addition_fail(i8 %x) {
; CHECK-LABEL: @fold_zext_addition_fail(
; CHECK-NEXT: [[XX:%.*]] = or i8 [[X:%.*]], 12
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX]] to i32
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[SE]], 1234
; CHECK-NEXT: ret i32 [[R]]
;
%xx = or i8 %x, 12
%se = zext i8 %xx to i32
%r = add i32 %se, 1234
ret i32 %r
}

define i32 @fold_zext_addition_fail2(i8 %x) {
; CHECK-LABEL: @fold_zext_addition_fail2(
; CHECK-NEXT: [[XX:%.*]] = or i8 [[X:%.*]], 18
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX]] to i32
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], -14
; CHECK-NEXT: ret i32 [[R]]
;
%xx = or i8 %x, 18
%se = zext i8 %xx to i32
%r = add i32 %se, -14
ret i32 %r
}


declare void @llvm.assume(i1)
declare void @fake_func(i32)
22 changes: 22 additions & 0 deletions llvm/test/Transforms/InstCombine/div.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1810,3 +1810,25 @@ define i6 @udiv_distribute_mul_nsw_add_nuw(i6 %x) {
%div = udiv i6 %add, 3
ret i6 %div
}

define i32 @fold_disjoint_or_over_sdiv(i32 %x) {
; CHECK-LABEL: @fold_disjoint_or_over_sdiv(
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[X:%.*]], 9
; CHECK-NEXT: ret i32 [[R]]
;
%mul = mul nsw i32 %x, 9
%or = or disjoint i32 %mul, 81
%r = sdiv i32 %or, 9
ret i32 %r
}

define i32 @fold_disjoint_or_over_udiv(i32 %x) {
; CHECK-LABEL: @fold_disjoint_or_over_udiv(
; CHECK-NEXT: [[R:%.*]] = add nuw i32 [[X:%.*]], 9
; CHECK-NEXT: ret i32 [[R]]
;
%mul = mul nuw i32 %x, 9
%or = or disjoint i32 %mul, 81
%r = udiv i32 %or, 9
ret i32 %r
}
32 changes: 32 additions & 0 deletions llvm/test/Transforms/InstCombine/sadd-with-overflow.ll
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,35 @@ define { i32, i1 } @fold_sub_simple(i32 %x) {
%b = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a, i32 30)
ret { i32, i1 } %b
}

define { i32, i1 } @fold_with_distjoin_or(i32 %x) {
; CHECK-LABEL: @fold_with_distjoin_or(
; CHECK-NEXT: [[B:%.*]] = add i32 [[X:%.*]], 6
; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 poison, i1 false }, i32 [[B]], 0
; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
;
%a = or disjoint i32 %x, 13
%b = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a, i32 -7)
ret { i32, i1 } %b
}

define { i32, i1 } @fold_with_disjoint_or2(i32 %x) {
; CHECK-LABEL: @fold_with_disjoint_or2(
; CHECK-NEXT: [[B:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 127)
; CHECK-NEXT: ret { i32, i1 } [[B]]
;
%a = or disjoint i32 %x, 100
%b = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a, i32 27)
ret { i32, i1 } %b
}

define { i32, i1 } @fold_with_or_fail(i32 %x) {
; CHECK-LABEL: @fold_with_or_fail(
; CHECK-NEXT: [[A:%.*]] = or i32 [[X:%.*]], 100
; CHECK-NEXT: [[B:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A]], i32 27)
; CHECK-NEXT: ret { i32, i1 } [[B]]
;
%a = or i32 %x, 100
%b = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a, i32 27)
ret { i32, i1 } %b
}
29 changes: 29 additions & 0 deletions llvm/test/Transforms/InstCombine/shift-add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,32 @@ define <3 x i32> @add3_i96(<3 x i32> %0, <3 x i32> %1) {
%25 = insertelement <3 x i32> %24, i32 %20, i32 2
ret <3 x i32> %25
}

define i8 @shl_fold_or_disjoint_cnt(i8 %x) {
; CHECK-LABEL: @shl_fold_or_disjoint_cnt(
; CHECK-NEXT: [[R:%.*]] = shl i8 16, [[X:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%a = or disjoint i8 %x, 3
%r = shl i8 2, %a
ret i8 %r
}

define <2 x i8> @ashr_fold_or_disjoint_cnt(<2 x i8> %x) {
; CHECK-LABEL: @ashr_fold_or_disjoint_cnt(
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i8> <i8 0, i8 1>, [[X:%.*]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%a = or disjoint <2 x i8> %x, <i8 3, i8 1>
%r = ashr <2 x i8> <i8 2, i8 3>, %a
ret <2 x i8> %r
}

define <2 x i8> @lshr_fold_or_disjoint_cnt_out_of_bounds(<2 x i8> %x) {
; CHECK-LABEL: @lshr_fold_or_disjoint_cnt_out_of_bounds(
; CHECK-NEXT: ret <2 x i8> zeroinitializer
;
%a = or disjoint <2 x i8> %x, <i8 3, i8 8>
%r = lshr <2 x i8> <i8 2, i8 3>, %a
ret <2 x i8> %r
}
23 changes: 23 additions & 0 deletions llvm/test/Transforms/InstCombine/uadd-with-overflow.ll
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,26 @@ define { i32, i1 } @no_fold_wrapped_add(i32 %x) {
%b = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 30, i32 %a)
ret { i32, i1 } %b
}


define { <2 x i32>, <2 x i1> } @fold_simple_splat_with_disjoint_or_constant(<2 x i32> %x) {
; CHECK-LABEL: @fold_simple_splat_with_disjoint_or_constant(
; CHECK-NEXT: [[B:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> [[X:%.*]], <2 x i32> <i32 42, i32 42>)
; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[B]]
;
%a = or disjoint <2 x i32> %x, <i32 12, i32 12>
%b = tail call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> %a, <2 x i32> <i32 30, i32 30>)
ret { <2 x i32>, <2 x i1> } %b
}


define { <2 x i32>, <2 x i1> } @fold_simple_splat_constant_with_or_fail(<2 x i32> %x) {
; CHECK-LABEL: @fold_simple_splat_constant_with_or_fail(
; CHECK-NEXT: [[A:%.*]] = or <2 x i32> [[X:%.*]], <i32 12, i32 12>
; CHECK-NEXT: [[B:%.*]] = tail call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> [[A]], <2 x i32> <i32 30, i32 30>)
; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[B]]
;
%a = or <2 x i32> %x, <i32 12, i32 12>
%b = tail call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> %a, <2 x i32> <i32 30, i32 30>)
ret { <2 x i32>, <2 x i1> } %b
}