[llvm] r355530 - [InstCombine] Fold add nsw + sadd.with.overflow
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 6 10:30:00 PST 2019
Author: nikic
Date: Wed Mar 6 10:30:00 2019
New Revision: 355530
URL: http://llvm.org/viewvc/llvm-project?rev=355530&view=rev
Log:
[InstCombine] Fold add nsw + sadd.with.overflow
Fold `add nsw` and `sadd.with.overflow` with constants if the addition
does not overflow.
Part of https://bugs.llvm.org/show_bug.cgi?id=38146.
Patch by Dan Robertson.
Differential Revision: https://reviews.llvm.org/D58881
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/trunk/test/Transforms/InstCombine/sadd-with-overflow.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=355530&r1=355529&r2=355530&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Wed Mar 6 10:30:00 2019
@@ -1814,6 +1814,19 @@ static Instruction *canonicalizeConstant
return nullptr;
}
+Instruction *InstCombiner::foldIntrinsicWithOverflowCommon(IntrinsicInst *II) {
+ OverflowCheckFlavor OCF =
+ IntrinsicIDToOverflowCheckFlavor(II->getIntrinsicID());
+ assert(OCF != OCF_INVALID && "unexpected!");
+
+ Value *OperationResult = nullptr;
+ Constant *OverflowResult = nullptr;
+ if (OptimizeOverflowCheck(OCF, II->getArgOperand(0), II->getArgOperand(1),
+ *II, OperationResult, OverflowResult))
+ return CreateOverflowTuple(II, OperationResult, OverflowResult);
+ return nullptr;
+}
+
/// CallInst simplification. This mostly only handles folding of intrinsic
/// instructions. For normal calls, it allows visitCallBase to do the heavy
/// lifting.
@@ -2016,8 +2029,32 @@ Instruction *InstCombiner::visitCallInst
return &CI;
break;
}
+ case Intrinsic::sadd_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:
+ // saddo (X +nsw C0), C1 -> saddo X, C0 + C1
+ Value *X;
+ const APInt *C0, *C1;
+ Value *Arg0 = II->getArgOperand(0);
+ Value *Arg1 = II->getArgOperand(1);
+ if (match(Arg0, m_NSWAdd(m_Value(X), m_APInt(C0))) &&
+ match(Arg1, m_APInt(C1))) {
+ bool Overflow;
+ APInt NewC = C1->sadd_ov(*C0, Overflow);
+ if (!Overflow)
+ return replaceInstUsesWith(
+ *II, Builder.CreateBinaryIntrinsic(
+ Intrinsic::sadd_with_overflow, X,
+ ConstantInt::get(Arg1->getType(), NewC)));
+ }
+
+ break;
+ }
case Intrinsic::uadd_with_overflow:
- case Intrinsic::sadd_with_overflow:
case Intrinsic::umul_with_overflow:
case Intrinsic::smul_with_overflow:
if (Instruction *I = canonicalizeConstantArg0ToArg1(CI))
@@ -2026,15 +2063,8 @@ Instruction *InstCombiner::visitCallInst
case Intrinsic::usub_with_overflow:
case Intrinsic::ssub_with_overflow: {
- OverflowCheckFlavor OCF =
- IntrinsicIDToOverflowCheckFlavor(II->getIntrinsicID());
- assert(OCF != OCF_INVALID && "unexpected!");
-
- Value *OperationResult = nullptr;
- Constant *OverflowResult = nullptr;
- if (OptimizeOverflowCheck(OCF, II->getArgOperand(0), II->getArgOperand(1),
- *II, OperationResult, OverflowResult))
- return CreateOverflowTuple(II, OperationResult, OverflowResult);
+ if (Instruction *I = foldIntrinsicWithOverflowCommon(II))
+ return I;
break;
}
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h?rev=355530&r1=355529&r2=355530&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h Wed Mar 6 10:30:00 2019
@@ -592,6 +592,8 @@ private:
Value *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D);
Value *getSelectCondition(Value *A, Value *B);
+ Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
+
public:
/// Inserts an instruction \p New before instruction \p Old
///
Modified: llvm/trunk/test/Transforms/InstCombine/sadd-with-overflow.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sadd-with-overflow.ll?rev=355530&r1=355529&r2=355530&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/sadd-with-overflow.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/sadd-with-overflow.ll Wed Mar 6 10:30:00 2019
@@ -9,9 +9,8 @@ declare { i8, i1 } @llvm.sadd.with.overf
define { i32, i1 } @simple_fold(i32) {
; CHECK-LABEL: @simple_fold(
-; CHECK-NEXT: [[TMP2:%.*]] = add nsw i32 [[TMP0:%.*]], 7
-; CHECK-NEXT: [[TMP3:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[TMP2]], i32 13)
-; CHECK-NEXT: ret { i32, i1 } [[TMP3]]
+; CHECK-NEXT: [[TMP2:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[TMP0:%.*]], i32 20)
+; CHECK-NEXT: ret { i32, i1 } [[TMP2]]
;
%2 = add nsw i32 %0, 7
%3 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %2, i32 13)
@@ -20,31 +19,39 @@ define { i32, i1 } @simple_fold(i32) {
define { i32, i1 } @fold_mixed_signs(i32) {
; CHECK-LABEL: @fold_mixed_signs(
-; CHECK-NEXT: [[TMP2:%.*]] = add nsw i32 [[TMP0:%.*]], 13
-; CHECK-NEXT: [[TMP3:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[TMP2]], i32 -7)
-; CHECK-NEXT: ret { i32, i1 } [[TMP3]]
+; CHECK-NEXT: [[TMP2:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[TMP0:%.*]], i32 6)
+; CHECK-NEXT: ret { i32, i1 } [[TMP2]]
;
%2 = add nsw i32 %0, 13
%3 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %2, i32 -7)
ret { i32, i1 } %3
}
+define { i8, i1 } @fold_on_constant_add_no_overflow(i8) {
+; CHECK-LABEL: @fold_on_constant_add_no_overflow(
+; CHECK-NEXT: [[TMP2:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[TMP0:%.*]], i8 127)
+; CHECK-NEXT: ret { i8, i1 } [[TMP2]]
+;
+ %2 = add nsw i8 %0, 100
+ %3 = tail call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %2, i8 27)
+ ret { i8, i1 } %3
+}
+
define { i8, i1 } @no_fold_on_constant_add_overflow(i8) {
; CHECK-LABEL: @no_fold_on_constant_add_overflow(
-; CHECK-NEXT: [[TMP2:%.*]] = add nsw i8 [[TMP0:%.*]], 127
-; CHECK-NEXT: [[TMP3:%.*]] = tail call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[TMP2]], i8 127)
+; CHECK-NEXT: [[TMP2:%.*]] = add nsw i8 [[TMP0:%.*]], 100
+; CHECK-NEXT: [[TMP3:%.*]] = tail call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[TMP2]], i8 28)
; CHECK-NEXT: ret { i8, i1 } [[TMP3]]
;
- %2 = add nsw i8 %0, 127
- %3 = tail call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %2, i8 127)
+ %2 = add nsw i8 %0, 100
+ %3 = tail call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %2, i8 28)
ret { i8, i1 } %3
}
define { <2 x i32>, <2 x i1> } @fold_simple_splat_constant(<2 x i32>) {
; CHECK-LABEL: @fold_simple_splat_constant(
-; CHECK-NEXT: [[TMP2:%.*]] = add nsw <2 x i32> [[TMP0:%.*]], <i32 12, i32 12>
-; CHECK-NEXT: [[TMP3:%.*]] = tail call { <2 x i32>, <2 x i1> } @llvm.sadd.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.sadd.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 nsw <2 x i32> %0, <i32 12, i32 12>
%3 = tail call { <2 x i32>, <2 x i1> } @llvm.sadd.with.overflow.v2i32(<2 x i32> %2, <2 x i32> <i32 30, i32 30>)
@@ -75,9 +82,8 @@ define { <2 x i32>, <2 x i1> } @no_fold_
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.sadd.with.overflow.i32(i32 [[TMP2]], i32 30)
-; CHECK-NEXT: ret { i32, i1 } [[TMP3]]
+; CHECK-NEXT: [[TMP2:%.*]] = call { i32, i1 } @llvm.sadd.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.sadd.with.overflow.i32(i32 %2, i32 30)
More information about the llvm-commits
mailing list