[llvm] [InstCombine] Infer nuw flags for `C-(X+C2)` -> `(C-C2)-X` (PR #72373)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 15 08:20:29 PST 2023
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/72373
>From 0fa1a86ad0dc9312f20e674e727d7cfcb8d8b8e3 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 15 Nov 2023 19:36:21 +0800
Subject: [PATCH 1/2] [InstCombine] Infer nuw flags for `C-(X+C2)` ->
`(C-C2)-X`
---
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 6 ++++--
llvm/test/Transforms/InstCombine/addsub-constant-folding.ll | 2 +-
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 2bd738c95ecaa7f..318992b55e4f9f8 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2111,14 +2111,16 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
// C-(X+C2) --> (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
+ // C-C2 never overflow, and C-(X+C2), (X+C2) has NSW/NUW
+ // => (C-C2)-X can have NSW/NUW
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);
+ Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap() &&
+ OBO1->hasNoUnsignedWrap());
return Res;
}
}
diff --git a/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll b/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
index 83a45e697e59e16..3a297880b794963 100644
--- a/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
+++ b/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
@@ -175,7 +175,7 @@ define i8 @add_nsw_const_const_sub_nsw_ov(i8 %arg) {
define i8 @add_nuw_const_const_sub_nuw(i8 %arg) {
; CHECK-LABEL: @add_nuw_const_const_sub_nuw(
-; CHECK-NEXT: [[T1:%.*]] = sub i8 -128, [[ARG:%.*]]
+; CHECK-NEXT: [[T1:%.*]] = sub nuw i8 -128, [[ARG:%.*]]
; CHECK-NEXT: ret i8 [[T1]]
;
%t0 = add nuw i8 %arg, 1
>From 5aa78e15b85810099ed42db17980eab8c0638fd1 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 16 Nov 2023 00:19:58 +0800
Subject: [PATCH 2/2] fixup! [InstCombine] Infer nuw flags for `C-(X+C2)` ->
`(C-C2)-X`
Add extra tests
---
.../Transforms/InstCombine/addsub-constant-folding.ll | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll b/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
index 3a297880b794963..86c0143f1d69724 100644
--- a/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
+++ b/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
@@ -183,6 +183,16 @@ define i8 @add_nuw_const_const_sub_nuw(i8 %arg) {
ret i8 %t1
}
+define i8 @add_nuw_const_const_sub(i8 %arg) {
+; CHECK-LABEL: @add_nuw_const_const_sub(
+; CHECK-NEXT: [[T1:%.*]] = sub i8 -128, [[ARG:%.*]]
+; CHECK-NEXT: ret i8 [[T1]]
+;
+ %t0 = add nuw i8 %arg, 1
+ %t1 = sub i8 -127, %t0
+ ret i8 %t1
+}
+
define i8 @add_const_const_sub_nuw(i8 %arg) {
; CHECK-LABEL: @add_const_const_sub_nuw(
; CHECK-NEXT: [[T1:%.*]] = sub i8 -128, [[ARG:%.*]]
More information about the llvm-commits
mailing list