[llvm] [InstCombine] Regard zext nneg as sext when folding add(zext neg(add)) (PR #88887)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 19 06:55:17 PDT 2024
https://github.com/ZelinMa557 updated https://github.com/llvm/llvm-project/pull/88887
>From f38e5a5314131a5d03af68246301f73041336b6a Mon Sep 17 00:00:00 2001
From: ZelinMa557 <3388706467 at qq.com>
Date: Fri, 19 Apr 2024 21:51:49 +0800
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests.
Signed-off-by: ZelinMa557 <3388706467 at qq.com>
---
llvm/test/Transforms/InstCombine/add.ll | 38 +++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/add.ll b/llvm/test/Transforms/InstCombine/add.ll
index 408b0c6559b001..107023034e05ad 100644
--- a/llvm/test/Transforms/InstCombine/add.ll
+++ b/llvm/test/Transforms/InstCombine/add.ll
@@ -4091,6 +4091,44 @@ define i32 @fold_zext_addition_fail2(i8 %x) {
ret i32 %r
}
+define i32 @fold_zext_nneg_add_const(i8 %x) {
+; CHECK-LABEL: @fold_zext_nneg_add_const(
+; CHECK-NEXT: [[XX:%.*]] = add nsw i8 [[X:%.*]], 123
+; CHECK-NEXT: [[ZE:%.*]] = zext nneg i8 [[XX]] to i32
+; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[ZE]], -25
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %xx = add nsw i8 %x, 123
+ %ze = zext nneg i8 %xx to i32
+ %r = add nsw i32 %ze, -25
+ ret i32 %r
+}
+
+define i32 @fold_zext_nneg_add_const_fail1(i8 %x) {
+; CHECK-LABEL: @fold_zext_nneg_add_const_fail1(
+; CHECK-NEXT: [[XX:%.*]] = add nsw i8 [[X:%.*]], 123
+; CHECK-NEXT: [[ZE:%.*]] = zext i8 [[XX]] to i32
+; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[ZE]], -25
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %xx = add nsw i8 %x, 123
+ %ze = zext i8 %xx to i32
+ %r = add nsw i32 %ze, -25
+ ret i32 %r
+}
+
+define i32 @fold_zext_nneg_add_const_fail2(i8 %x) {
+; CHECK-LABEL: @fold_zext_nneg_add_const_fail2(
+; CHECK-NEXT: [[XX:%.*]] = add i8 [[X:%.*]], 123
+; CHECK-NEXT: [[ZE:%.*]] = zext nneg i8 [[XX]] to i32
+; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[ZE]], -25
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %xx = add i8 %x, 123
+ %ze = zext nneg i8 %xx to i32
+ %r = add nsw i32 %ze, -25
+ ret i32 %r
+}
declare void @llvm.assume(i1)
declare void @fake_func(i32)
>From 0d8c7df2e5987afc156c3823e22916d3923d70be Mon Sep 17 00:00:00 2001
From: ZelinMa557 <3388706467 at qq.com>
Date: Fri, 19 Apr 2024 21:52:57 +0800
Subject: [PATCH 2/2] [InstCombine] Regard zext nneg as sext when folding
add(zext neg(add))
Signed-off-by: ZelinMa557 <3388706467 at qq.com>
---
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 6 +++---
llvm/test/Transforms/InstCombine/add.ll | 5 ++---
2 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 07c50d866544b3..e659b5de395b35 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -828,9 +828,10 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
// More general combining of constants in the wide type.
// (sext (X +nsw NarrowC)) + C --> (sext X) + (sext(NarrowC) + C)
+ // or (zext nneg (X +nsw NarrowC)) + C --> (sext X) + (sext(NarrowC) + C)
Constant *NarrowC;
- if (match(Op0,
- m_OneUse(m_SExt(m_NSWAddLike(m_Value(X), m_Constant(NarrowC)))))) {
+ if (match(Op0, m_OneUse(m_SExtLike(
+ m_NSWAddLike(m_Value(X), m_Constant(NarrowC)))))) {
Value *WideC = Builder.CreateSExt(NarrowC, Ty);
Value *NewC = Builder.CreateAdd(WideC, Op1C);
Value *WideX = Builder.CreateSExt(X, Ty);
@@ -844,7 +845,6 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
Value *WideX = Builder.CreateZExt(X, Ty);
return BinaryOperator::CreateAdd(WideX, NewC);
}
-
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/add.ll b/llvm/test/Transforms/InstCombine/add.ll
index 107023034e05ad..e75a9eb51f3983 100644
--- a/llvm/test/Transforms/InstCombine/add.ll
+++ b/llvm/test/Transforms/InstCombine/add.ll
@@ -4093,9 +4093,8 @@ define i32 @fold_zext_addition_fail2(i8 %x) {
define i32 @fold_zext_nneg_add_const(i8 %x) {
; CHECK-LABEL: @fold_zext_nneg_add_const(
-; CHECK-NEXT: [[XX:%.*]] = add nsw i8 [[X:%.*]], 123
-; CHECK-NEXT: [[ZE:%.*]] = zext nneg i8 [[XX]] to i32
-; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[ZE]], -25
+; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X:%.*]] to i32
+; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], 98
; CHECK-NEXT: ret i32 [[R]]
;
%xx = add nsw i8 %x, 123
More information about the llvm-commits
mailing list