[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
Sat Jun 3 20:59:52 PDT 2023
khei4 created this revision.
khei4 added reviewers: nikic, goldstein.w.n, spatel.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
khei4 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
On transform `~X + C --> (C-1) - X`, we can safely attach NSW to (C-1) - X if the 1,2 are satisfied
1. `~X + C` has NSW
2. `C-1` won't signed overflow
proof: https://alive2.llvm.org/ce/z/DXXaoC
This will resolve https://reviews.llvm.org/D146903 's InstCombine part. https://alive2.llvm.org/ce/z/-_FdJU
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
@@ -357,6 +357,28 @@
ret i8 %C
}
+; ~X + -127 and (-128) - X with nsw are equally poisonous
+define i8 @test18_nsw(i8 %A) {
+; CHECK-LABEL: @test18_nsw(
+; CHECK-NEXT: [[C:%.*]] = sub nsw i8 -128, [[A:%.*]]
+; CHECK-NEXT: ret i8 [[C]]
+;
+ %B = xor i8 %A, -1
+ %C = add nsw i8 %B, -127
+ ret i8 %C
+}
+
+; 127 - X with nsw will be more poisonous than ~X + -128 with nsw. (see X = -1)
+define i8 @test18_nsw_overflow(i8 %A) {
+; CHECK-LABEL: @test18_nsw_overflow(
+; CHECK-NEXT: [[C:%.*]] = sub i8 127, [[A:%.*]]
+; CHECK-NEXT: ret i8 [[C]]
+;
+ %B = xor i8 %A, -1
+ %C = add nsw i8 %B, -128
+ ret i8 %C
+}
+
define <2 x i64> @test18vec(<2 x i64> %A) {
; CHECK-LABEL: @test18vec(
; CHECK-NEXT: [[ADD:%.*]] = sub <2 x i64> <i64 1, i64 2>, [[A:%.*]]
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.528178.patch
Type: text/x-patch
Size: 1941 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230604/1e318f58/attachment.bin>
More information about the llvm-commits
mailing list