aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZelinMa557 <72912470+ZelinMa557@users.noreply.github.com>2024-04-19 22:59:07 +0800
committerGitHub <noreply@github.com>2024-04-19 22:59:07 +0800
commit97c71247312c7e5261168283c13cd7e3ecd039a5 (patch)
tree885c31fdb852b542d7e1b80b99b60bee615df1d7
parent3a4bc11b675c0511319c2843221133e986825b3b (diff)
downloadllvm-97c71247312c7e5261168283c13cd7e3ecd039a5.zip
llvm-97c71247312c7e5261168283c13cd7e3ecd039a5.tar.gz
llvm-97c71247312c7e5261168283c13cd7e3ecd039a5.tar.bz2
[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@qq.com>
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp6
-rw-r--r--llvm/test/Transforms/InstCombine/add.ll37
2 files changed, 40 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 9a16ca9..fc284bc 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 39b4ad8..56ee54d 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)