Skip to content

Commit a0987c0

Browse files
goldsteinnchencha3
authored andcommitted
[InstCombine] integrate N{U,S}WAddLike into existing folds
Just went a quick replacement of `N{U,S}WAdd` with the `Like` variant that old matches `or disjoint` Closes llvm#86082
1 parent 55ecff1 commit a0987c0

File tree

9 files changed

+26
-36
lines changed

9 files changed

+26
-36
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
819819
Value *X;
820820
const APInt *C1, *C2;
821821
if (match(Op1, m_APInt(C1)) &&
822-
match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_APInt(C2))))) &&
822+
match(Op0, m_OneUse(m_ZExt(m_NUWAddLike(m_Value(X), m_APInt(C2))))) &&
823823
C1->isNegative() && C1->sge(-C2->sext(C1->getBitWidth()))) {
824824
Constant *NewC =
825825
ConstantInt::get(X->getType(), *C2 + C1->trunc(C2->getBitWidth()));
@@ -829,14 +829,16 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
829829
// More general combining of constants in the wide type.
830830
// (sext (X +nsw NarrowC)) + C --> (sext X) + (sext(NarrowC) + C)
831831
Constant *NarrowC;
832-
if (match(Op0, m_OneUse(m_SExt(m_NSWAdd(m_Value(X), m_Constant(NarrowC)))))) {
832+
if (match(Op0,
833+
m_OneUse(m_SExt(m_NSWAddLike(m_Value(X), m_Constant(NarrowC)))))) {
833834
Value *WideC = Builder.CreateSExt(NarrowC, Ty);
834835
Value *NewC = Builder.CreateAdd(WideC, Op1C);
835836
Value *WideX = Builder.CreateSExt(X, Ty);
836837
return BinaryOperator::CreateAdd(WideX, NewC);
837838
}
838839
// (zext (X +nuw NarrowC)) + C --> (zext X) + (zext(NarrowC) + C)
839-
if (match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_Constant(NarrowC)))))) {
840+
if (match(Op0,
841+
m_OneUse(m_ZExt(m_NUWAddLike(m_Value(X), m_Constant(NarrowC)))))) {
840842
Value *WideC = Builder.CreateZExt(NarrowC, Ty);
841843
Value *NewC = Builder.CreateAdd(WideC, Op1C);
842844
Value *WideX = Builder.CreateZExt(X, Ty);

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,8 +2093,9 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
20932093
Value *Arg0 = II->getArgOperand(0);
20942094
Value *Arg1 = II->getArgOperand(1);
20952095
bool IsSigned = IID == Intrinsic::sadd_with_overflow;
2096-
bool HasNWAdd = IsSigned ? match(Arg0, m_NSWAdd(m_Value(X), m_APInt(C0)))
2097-
: match(Arg0, m_NUWAdd(m_Value(X), m_APInt(C0)));
2096+
bool HasNWAdd = IsSigned
2097+
? match(Arg0, m_NSWAddLike(m_Value(X), m_APInt(C0)))
2098+
: match(Arg0, m_NUWAddLike(m_Value(X), m_APInt(C0)));
20982099
if (HasNWAdd && match(Arg1, m_APInt(C1))) {
20992100
bool Overflow;
21002101
APInt NewC =

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,14 +1171,14 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
11711171
// We need a multiple of the divisor for a signed add constant, but
11721172
// unsigned is fine with any constant pair.
11731173
if (IsSigned &&
1174-
match(Op0, m_NSWAdd(m_NSWMul(m_Value(X), m_SpecificInt(*C2)),
1175-
m_APInt(C1))) &&
1174+
match(Op0, m_NSWAddLike(m_NSWMul(m_Value(X), m_SpecificInt(*C2)),
1175+
m_APInt(C1))) &&
11761176
isMultiple(*C1, *C2, Quotient, IsSigned)) {
11771177
return BinaryOperator::CreateNSWAdd(X, ConstantInt::get(Ty, Quotient));
11781178
}
11791179
if (!IsSigned &&
1180-
match(Op0, m_NUWAdd(m_NUWMul(m_Value(X), m_SpecificInt(*C2)),
1181-
m_APInt(C1)))) {
1180+
match(Op0, m_NUWAddLike(m_NUWMul(m_Value(X), m_SpecificInt(*C2)),
1181+
m_APInt(C1)))) {
11821182
return BinaryOperator::CreateNUWAdd(X,
11831183
ConstantInt::get(Ty, C1->udiv(*C2)));
11841184
}

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
437437
Value *A;
438438
Constant *C, *C1;
439439
if (match(Op0, m_Constant(C)) &&
440-
match(Op1, m_NUWAdd(m_Value(A), m_Constant(C1)))) {
440+
match(Op1, m_NUWAddLike(m_Value(A), m_Constant(C1)))) {
441441
Value *NewC = Builder.CreateBinOp(I.getOpcode(), C, C1);
442442
BinaryOperator *NewShiftOp = BinaryOperator::Create(I.getOpcode(), NewC, A);
443443
if (I.getOpcode() == Instruction::Shl) {

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3988,9 +3988,8 @@ define i32 @add_reduce_sqr_sum_varC_invalid2(i32 %a, i32 %b) {
39883988

39893989
define i32 @fold_sext_addition_or_disjoint(i8 %x) {
39903990
; CHECK-LABEL: @fold_sext_addition_or_disjoint(
3991-
; CHECK-NEXT: [[XX:%.*]] = or disjoint i8 [[X:%.*]], 12
3992-
; CHECK-NEXT: [[SE:%.*]] = sext i8 [[XX]] to i32
3993-
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], 1234
3991+
; CHECK-NEXT: [[SE:%.*]] = sext i8 [[XX:%.*]] to i32
3992+
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], 1246
39943993
; CHECK-NEXT: ret i32 [[R]]
39953994
;
39963995
%xx = or disjoint i8 %x, 12
@@ -4014,9 +4013,8 @@ define i32 @fold_sext_addition_fail(i8 %x) {
40144013

40154014
define i32 @fold_zext_addition_or_disjoint(i8 %x) {
40164015
; CHECK-LABEL: @fold_zext_addition_or_disjoint(
4017-
; CHECK-NEXT: [[XX:%.*]] = or disjoint i8 [[X:%.*]], 12
4018-
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX]] to i32
4019-
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[SE]], 1234
4016+
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX:%.*]] to i32
4017+
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[SE]], 1246
40204018
; CHECK-NEXT: ret i32 [[R]]
40214019
;
40224020
%xx = or disjoint i8 %x, 12
@@ -4027,10 +4025,9 @@ define i32 @fold_zext_addition_or_disjoint(i8 %x) {
40274025

40284026
define i32 @fold_zext_addition_or_disjoint2(i8 %x) {
40294027
; CHECK-LABEL: @fold_zext_addition_or_disjoint2(
4030-
; CHECK-NEXT: [[XX:%.*]] = or disjoint i8 [[X:%.*]], 18
4028+
; CHECK-NEXT: [[XX:%.*]] = add nuw i8 [[X:%.*]], 4
40314029
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX]] to i32
4032-
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], -14
4033-
; CHECK-NEXT: ret i32 [[R]]
4030+
; CHECK-NEXT: ret i32 [[SE]]
40344031
;
40354032
%xx = or disjoint i8 %x, 18
40364033
%se = zext i8 %xx to i32

llvm/test/Transforms/InstCombine/div.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,9 +1813,7 @@ define i6 @udiv_distribute_mul_nsw_add_nuw(i6 %x) {
18131813

18141814
define i32 @fold_disjoint_or_over_sdiv(i32 %x) {
18151815
; CHECK-LABEL: @fold_disjoint_or_over_sdiv(
1816-
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[X:%.*]], 9
1817-
; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[MUL]], 81
1818-
; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[OR]], 9
1816+
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[X:%.*]], 9
18191817
; CHECK-NEXT: ret i32 [[R]]
18201818
;
18211819
%mul = mul nsw i32 %x, 9
@@ -1826,9 +1824,7 @@ define i32 @fold_disjoint_or_over_sdiv(i32 %x) {
18261824

18271825
define i32 @fold_disjoint_or_over_udiv(i32 %x) {
18281826
; CHECK-LABEL: @fold_disjoint_or_over_udiv(
1829-
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i32 [[X:%.*]], 9
1830-
; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[MUL]], 81
1831-
; CHECK-NEXT: [[R:%.*]] = udiv i32 [[OR]], 9
1827+
; CHECK-NEXT: [[R:%.*]] = add nuw i32 [[X:%.*]], 9
18321828
; CHECK-NEXT: ret i32 [[R]]
18331829
;
18341830
%mul = mul nuw i32 %x, 9

llvm/test/Transforms/InstCombine/sadd-with-overflow.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ define { i32, i1 } @fold_with_distjoin_or(i32 %x) {
136136

137137
define { i32, i1 } @fold_with_disjoint_or2(i32 %x) {
138138
; CHECK-LABEL: @fold_with_disjoint_or2(
139-
; CHECK-NEXT: [[A:%.*]] = or disjoint i32 [[X:%.*]], 100
140-
; CHECK-NEXT: [[B:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A]], i32 27)
139+
; CHECK-NEXT: [[B:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 127)
141140
; CHECK-NEXT: ret { i32, i1 } [[B]]
142141
;
143142
%a = or disjoint i32 %x, 100

llvm/test/Transforms/InstCombine/shift-add.ll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,7 @@ define <3 x i32> @add3_i96(<3 x i32> %0, <3 x i32> %1) {
778778

779779
define i8 @shl_fold_or_disjoint_cnt(i8 %x) {
780780
; CHECK-LABEL: @shl_fold_or_disjoint_cnt(
781-
; CHECK-NEXT: [[A:%.*]] = or disjoint i8 [[X:%.*]], 3
782-
; CHECK-NEXT: [[R:%.*]] = shl i8 2, [[A]]
781+
; CHECK-NEXT: [[R:%.*]] = shl i8 16, [[X:%.*]]
783782
; CHECK-NEXT: ret i8 [[R]]
784783
;
785784
%a = or disjoint i8 %x, 3
@@ -789,8 +788,7 @@ define i8 @shl_fold_or_disjoint_cnt(i8 %x) {
789788

790789
define <2 x i8> @ashr_fold_or_disjoint_cnt(<2 x i8> %x) {
791790
; CHECK-LABEL: @ashr_fold_or_disjoint_cnt(
792-
; CHECK-NEXT: [[A:%.*]] = or disjoint <2 x i8> [[X:%.*]], <i8 3, i8 1>
793-
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i8> <i8 2, i8 3>, [[A]]
791+
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i8> <i8 0, i8 1>, [[X:%.*]]
794792
; CHECK-NEXT: ret <2 x i8> [[R]]
795793
;
796794
%a = or disjoint <2 x i8> %x, <i8 3, i8 1>
@@ -800,9 +798,7 @@ define <2 x i8> @ashr_fold_or_disjoint_cnt(<2 x i8> %x) {
800798

801799
define <2 x i8> @lshr_fold_or_disjoint_cnt_out_of_bounds(<2 x i8> %x) {
802800
; CHECK-LABEL: @lshr_fold_or_disjoint_cnt_out_of_bounds(
803-
; CHECK-NEXT: [[A:%.*]] = or disjoint <2 x i8> [[X:%.*]], <i8 3, i8 8>
804-
; CHECK-NEXT: [[R:%.*]] = lshr <2 x i8> <i8 2, i8 3>, [[A]]
805-
; CHECK-NEXT: ret <2 x i8> [[R]]
801+
; CHECK-NEXT: ret <2 x i8> zeroinitializer
806802
;
807803
%a = or disjoint <2 x i8> %x, <i8 3, i8 8>
808804
%r = lshr <2 x i8> <i8 2, i8 3>, %a

llvm/test/Transforms/InstCombine/uadd-with-overflow.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ define { i32, i1 } @no_fold_wrapped_add(i32 %x) {
128128

129129
define { <2 x i32>, <2 x i1> } @fold_simple_splat_with_disjoint_or_constant(<2 x i32> %x) {
130130
; CHECK-LABEL: @fold_simple_splat_with_disjoint_or_constant(
131-
; CHECK-NEXT: [[A:%.*]] = or disjoint <2 x i32> [[X:%.*]], <i32 12, i32 12>
132-
; 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>)
131+
; 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>)
133132
; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[B]]
134133
;
135134
%a = or disjoint <2 x i32> %x, <i32 12, i32 12>

0 commit comments

Comments
 (0)