[llvm] 97c7124 - [InstCombine] Regard zext nneg as sext when folding add(zext neg(add)) (#88887)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 19 07:59:10 PDT 2024
Author: ZelinMa557
Date: 2024-04-19T22:59:07+08:00
New Revision: 97c71247312c7e5261168283c13cd7e3ecd039a5
URL: https://github.com/llvm/llvm-project/commit/97c71247312c7e5261168283c13cd7e3ecd039a5
DIFF: https://github.com/llvm/llvm-project/commit/97c71247312c7e5261168283c13cd7e3ecd039a5.diff
LOG: [InstCombine] Regard zext nneg as sext when folding add(zext neg(add)) (#88887)
fixes #88348
proof:
https://alive2.llvm.org/ce/z/fJnM7t
test will be added later
---------
Signed-off-by: ZelinMa557 <3388706467 at qq.com>
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/add.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 9a16ca96db76b6..fc284bc61cce97 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 39b4ad80550889..56ee54d351e762 100644
--- a/llvm/test/Transforms/InstCombine/add.ll
+++ b/llvm/test/Transforms/InstCombine/add.ll
@@ -4091,6 +4091,43 @@ 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: [[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
+ %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)
More information about the llvm-commits
mailing list