[llvm] 4db8d4f - [InstCombine] add overflow checking on AddSub `C-(X+C2) --> (C-C2)-X`
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 5 04:05:18 PDT 2023
Author: khei4
Date: 2023-06-05T20:05:06+09:00
New Revision: 4db8d4f8391001bf72458ed2fc8f5d60c167bbe7
URL: https://github.com/llvm/llvm-project/commit/4db8d4f8391001bf72458ed2fc8f5d60c167bbe7
DIFF: https://github.com/llvm/llvm-project/commit/4db8d4f8391001bf72458ed2fc8f5d60c167bbe7.diff
LOG: [InstCombine] add overflow checking on AddSub `C-(X+C2) --> (C-C2)-X`
Differential Revision: https://reviews.llvm.org/D152068
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 8f6934c6af083..29409e63ab8e0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1995,8 +1995,17 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
Constant *C2;
// C-(X+C2) --> (C-C2)-X
- if (match(Op1, m_Add(m_Value(X), m_ImmConstant(C2))))
- return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
+ if (match(Op1, m_Add(m_Value(X), m_ImmConstant(C2)))) {
+ // C-C2 never overflow, and C-(X+C2), (X+C2) has NSW
+ // => (C-C2)-X can have NSW
+ bool WillNotSOV = willNotOverflowSignedSub(C, C2, I);
+ BinaryOperator *Res =
+ BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
+ auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
+ Res->setHasNoSignedWrap(I.hasNoSignedWrap() && OBO1->hasNoSignedWrap() &&
+ WillNotSOV);
+ return Res;
+ }
}
auto TryToNarrowDeduceFlags = [this, &I, &Op0, &Op1]() -> Instruction * {
diff --git a/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll b/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
index 73f7180ba4eae..83a45e697e59e 100644
--- a/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
+++ b/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
@@ -134,7 +134,7 @@ define i32 @add_const_const_sub(i32 %arg) {
define i8 @add_nsw_const_const_sub_nsw(i8 %arg) {
; CHECK-LABEL: @add_nsw_const_const_sub_nsw(
-; CHECK-NEXT: [[T1:%.*]] = sub i8 -128, [[ARG:%.*]]
+; CHECK-NEXT: [[T1:%.*]] = sub nsw i8 -128, [[ARG:%.*]]
; CHECK-NEXT: ret i8 [[T1]]
;
%t0 = add nsw i8 %arg, 1
@@ -193,14 +193,13 @@ define i8 @add_const_const_sub_nuw(i8 %arg) {
ret i8 %t1
}
-
define <2 x i8> @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov1(<2 x i8> %arg) {
; CHECK-LABEL: @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov1(
-; CHECK-NEXT: [[T1:%.*]] = sub <2 x i8> <i8 -2, i8 -126>, [[ARG:%.*]]
+; CHECK-NEXT: [[T1:%.*]] = sub nsw <2 x i8> <i8 -127, i8 -126>, [[ARG:%.*]]
; CHECK-NEXT: ret <2 x i8> [[T1]]
;
- %t0 = add nsw <2 x i8> %arg, <i8 1, i8 0>
- %t1 = sub nsw <2 x i8> <i8 -1, i8 -126>, %t0
+ %t0 = add nsw <2 x i8> %arg, <i8 2, i8 0>
+ %t1 = sub nsw <2 x i8> <i8 -125, i8 -126>, %t0
ret <2 x i8> %t1
}
More information about the llvm-commits
mailing list