[PATCH] D152088: [InstCombine] add nsw on InstCombineAdd ~X + C --> (C-1) - X
Kohei Asano via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 4 20:59:13 PDT 2023
khei4 updated this revision to Diff 528272.
khei4 added a comment.
rebase for tests update
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D152088/new/
https://reviews.llvm.org/D152088
Files:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/add.ll
Index: llvm/test/Transforms/InstCombine/add.ll
===================================================================
--- llvm/test/Transforms/InstCombine/add.ll
+++ llvm/test/Transforms/InstCombine/add.ll
@@ -360,7 +360,7 @@
; ~X + -127 and (-128) - X with nsw are equally poisonous
define i8 @test18_nsw(i8 %A) {
; CHECK-LABEL: @test18_nsw(
-; CHECK-NEXT: [[C:%.*]] = sub i8 -128, [[A:%.*]]
+; CHECK-NEXT: [[C:%.*]] = sub nsw i8 -128, [[A:%.*]]
; CHECK-NEXT: ret i8 [[C]]
;
%B = xor i8 %A, -1
@@ -402,22 +402,22 @@
define <2 x i8> @test18vec_nsw(<2 x i8> %A) {
; CHECK-LABEL: @test18vec_nsw(
-; CHECK-NEXT: [[C:%.*]] = sub <2 x i8> <i8 -126, i8 -125>, [[A:%.*]]
+; CHECK-NEXT: [[C:%.*]] = sub nsw <2 x i8> <i8 -124, i8 -125>, [[A:%.*]]
; CHECK-NEXT: ret <2 x i8> [[C]]
;
%B = xor <2 x i8> %A, <i8 -1, i8 -1>
- %C = add nsw <2 x i8> %B, <i8 -125, i8 -124>
+ %C = add nsw <2 x i8> %B, <i8 -123, i8 -124>
ret <2 x i8> %C
}
; TODO: fix ValueTracking overflow check for non-splat vector
define <2 x i8> @test18vec_nsw_false(<2 x i8> %A) {
; CHECK-LABEL: @test18vec_nsw_false(
-; CHECK-NEXT: [[C:%.*]] = sub <2 x i8> <i8 -128, i8 -127>, [[A:%.*]]
+; CHECK-NEXT: [[C:%.*]] = sub <2 x i8> <i8 -125, i8 -126>, [[A:%.*]]
; CHECK-NEXT: ret <2 x i8> [[C]]
;
%B = xor <2 x i8> %A, <i8 -1, i8 -1>
- %C = add nsw <2 x i8> %B, <i8 -127, i8 -126>
+ %C = add nsw <2 x i8> %B, <i8 -124, i8 -125>
ret <2 x i8> %C
}
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -880,8 +880,15 @@
return SelectInst::Create(X, InstCombiner::SubOne(Op1C), Op1);
// ~X + C --> (C-1) - X
- if (match(Op0, m_Not(m_Value(X))))
- return BinaryOperator::CreateSub(InstCombiner::SubOne(Op1C), X);
+ if (match(Op0, m_Not(m_Value(X)))) {
+ // ~X + C has NSW and (C-1) won't oveflow => (C-1)-X can have NSW
+ auto *COne = ConstantInt::get(Op1C->getType(), 1);
+ bool WillNotSOV = willNotOverflowSignedSub(Op1C, COne, Add);
+ BinaryOperator *Res =
+ BinaryOperator::CreateSub(ConstantExpr::getSub(Op1C, COne), X);
+ Res->setHasNoSignedWrap(Add.hasNoSignedWrap() && WillNotSOV);
+ return Res;
+ }
// (iN X s>> (N - 1)) + 1 --> zext (X > -1)
const APInt *C;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152088.528272.patch
Type: text/x-patch
Size: 2423 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230605/504d5be6/attachment.bin>
More information about the llvm-commits
mailing list