[PATCH] D59471: [InstCombine] Fold add nuw + uadd.with.overflow
Dan Robertson via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 17 09:08:41 PDT 2019
dlrobertson created this revision.
dlrobertson added reviewers: spatel, nikic.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
Fold `add nuw` and `uadd.with.overflow` with constants if the
addition does not overflow.
Part of https://bugs.llvm.org/show_bug.cgi?id=38146.
https://reviews.llvm.org/D59471
Files:
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/test/Transforms/InstCombine/uadd-with-overflow.ll
Index: llvm/test/Transforms/InstCombine/uadd-with-overflow.ll
===================================================================
--- llvm/test/Transforms/InstCombine/uadd-with-overflow.ll
+++ llvm/test/Transforms/InstCombine/uadd-with-overflow.ll
@@ -9,9 +9,8 @@
define { i32, i1 } @simple_fold(i32) {
; CHECK-LABEL: @simple_fold(
-; CHECK-NEXT: [[TMP2:%.*]] = add nuw i32 [[TMP0:%.*]], 7
-; CHECK-NEXT: [[TMP3:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[TMP2]], i32 13)
-; CHECK-NEXT: ret { i32, i1 } [[TMP3]]
+; CHECK-NEXT: [[TMP2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[TMP0:%.*]], i32 20)
+; CHECK-NEXT: ret { i32, i1 } [[TMP2]]
;
%2 = add nuw i32 %0, 7
%3 = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %2, i32 13)
@@ -20,9 +19,8 @@
define { i8, i1 } @fold_on_constant_add_no_overflow(i8) {
; CHECK-LABEL: @fold_on_constant_add_no_overflow(
-; CHECK-NEXT: [[TMP2:%.*]] = add nuw i8 [[TMP0:%.*]], -56
-; CHECK-NEXT: [[TMP3:%.*]] = tail call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[TMP2]], i8 55)
-; CHECK-NEXT: ret { i8, i1 } [[TMP3]]
+; CHECK-NEXT: [[TMP2:%.*]] = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[TMP0:%.*]], i8 -1)
+; CHECK-NEXT: ret { i8, i1 } [[TMP2]]
;
%2 = add nuw i8 %0, 200
%3 = tail call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %2, i8 55)
@@ -42,9 +40,8 @@
define { <2 x i32>, <2 x i1> } @fold_simple_splat_constant(<2 x i32>) {
; CHECK-LABEL: @fold_simple_splat_constant(
-; CHECK-NEXT: [[TMP2:%.*]] = add nuw <2 x i32> [[TMP0:%.*]], <i32 12, i32 12>
-; CHECK-NEXT: [[TMP3:%.*]] = tail call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> [[TMP2]], <2 x i32> <i32 30, i32 30>)
-; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[TMP3]]
+; CHECK-NEXT: [[TMP2:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> [[TMP0:%.*]], <2 x i32> <i32 42, i32 42>)
+; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[TMP2]]
;
%2 = add nuw <2 x i32> %0, <i32 12, i32 12>
%3 = tail call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> %2, <2 x i32> <i32 30, i32 30>)
@@ -75,9 +72,8 @@
define { i32, i1 } @fold_nuwnsw(i32) {
; CHECK-LABEL: @fold_nuwnsw(
-; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i32 [[TMP0:%.*]], 12
-; CHECK-NEXT: [[TMP3:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[TMP2]], i32 30)
-; CHECK-NEXT: ret { i32, i1 } [[TMP3]]
+; CHECK-NEXT: [[TMP2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[TMP0:%.*]], i32 42)
+; CHECK-NEXT: ret { i32, i1 } [[TMP2]]
;
%2 = add nuw nsw i32 %0, 12
%3 = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %2, i32 30)
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2054,7 +2054,30 @@
break;
}
- case Intrinsic::uadd_with_overflow:
+ case Intrinsic::uadd_with_overflow: {
+ if (Instruction *I = canonicalizeConstantArg0ToArg1(CI))
+ return I;
+ if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
+ return I;
+
+ // Given 2 constant operands whose sum does not overflow:
+ // uaddo (X +nuw C0), C1 -> uaddo X, C0 + C1
+ Value *X;
+ const APInt *C0, *C1;
+ Value *Arg0 = II->getArgOperand(0);
+ Value *Arg1 = II->getArgOperand(1);
+ if (match(Arg0, m_NUWAdd(m_Value(X), m_APInt(C0))) &&
+ match(Arg1, m_APInt(C1))) {
+ bool Overflow;
+ APInt NewC = C1->uadd_ov(*C0, Overflow);
+ if (!Overflow)
+ return replaceInstUsesWith(
+ *II, Builder.CreateBinaryIntrinsic(
+ Intrinsic::uadd_with_overflow, X,
+ ConstantInt::get(Arg1->getType(), NewC)));
+ }
+ break;
+ }
case Intrinsic::umul_with_overflow:
case Intrinsic::smul_with_overflow:
if (Instruction *I = canonicalizeConstantArg0ToArg1(CI))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59471.191021.patch
Type: text/x-patch
Size: 4047 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190317/820fca7b/attachment.bin>
More information about the llvm-commits
mailing list