Skip to content

[InstCombine] Regard zext nneg as sext when folding add(zext neg(add)) #88887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -844,7 +845,6 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
Value *WideX = Builder.CreateZExt(X, Ty);
return BinaryOperator::CreateAdd(WideX, NewC);
}

return nullptr;
}

Expand Down
37 changes: 37 additions & 0 deletions llvm/test/Transforms/InstCombine/add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading