Skip to content

Commit 97c7124

Browse files
authored
[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>
1 parent 3a4bc11 commit 97c7124

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,10 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
828828

829829
// More general combining of constants in the wide type.
830830
// (sext (X +nsw NarrowC)) + C --> (sext X) + (sext(NarrowC) + C)
831+
// or (zext nneg (X +nsw NarrowC)) + C --> (sext X) + (sext(NarrowC) + C)
831832
Constant *NarrowC;
832-
if (match(Op0,
833-
m_OneUse(m_SExt(m_NSWAddLike(m_Value(X), m_Constant(NarrowC)))))) {
833+
if (match(Op0, m_OneUse(m_SExtLike(
834+
m_NSWAddLike(m_Value(X), m_Constant(NarrowC)))))) {
834835
Value *WideC = Builder.CreateSExt(NarrowC, Ty);
835836
Value *NewC = Builder.CreateAdd(WideC, Op1C);
836837
Value *WideX = Builder.CreateSExt(X, Ty);
@@ -844,7 +845,6 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
844845
Value *WideX = Builder.CreateZExt(X, Ty);
845846
return BinaryOperator::CreateAdd(WideX, NewC);
846847
}
847-
848848
return nullptr;
849849
}
850850

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4091,6 +4091,43 @@ define i32 @fold_zext_addition_fail2(i8 %x) {
40914091
ret i32 %r
40924092
}
40934093

4094+
define i32 @fold_zext_nneg_add_const(i8 %x) {
4095+
; CHECK-LABEL: @fold_zext_nneg_add_const(
4096+
; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X:%.*]] to i32
4097+
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], 98
4098+
; CHECK-NEXT: ret i32 [[R]]
4099+
;
4100+
%xx = add nsw i8 %x, 123
4101+
%ze = zext nneg i8 %xx to i32
4102+
%r = add nsw i32 %ze, -25
4103+
ret i32 %r
4104+
}
4105+
4106+
define i32 @fold_zext_nneg_add_const_fail1(i8 %x) {
4107+
; CHECK-LABEL: @fold_zext_nneg_add_const_fail1(
4108+
; CHECK-NEXT: [[XX:%.*]] = add nsw i8 [[X:%.*]], 123
4109+
; CHECK-NEXT: [[ZE:%.*]] = zext i8 [[XX]] to i32
4110+
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[ZE]], -25
4111+
; CHECK-NEXT: ret i32 [[R]]
4112+
;
4113+
%xx = add nsw i8 %x, 123
4114+
%ze = zext i8 %xx to i32
4115+
%r = add nsw i32 %ze, -25
4116+
ret i32 %r
4117+
}
4118+
4119+
define i32 @fold_zext_nneg_add_const_fail2(i8 %x) {
4120+
; CHECK-LABEL: @fold_zext_nneg_add_const_fail2(
4121+
; CHECK-NEXT: [[XX:%.*]] = add i8 [[X:%.*]], 123
4122+
; CHECK-NEXT: [[ZE:%.*]] = zext nneg i8 [[XX]] to i32
4123+
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[ZE]], -25
4124+
; CHECK-NEXT: ret i32 [[R]]
4125+
;
4126+
%xx = add i8 %x, 123
4127+
%ze = zext nneg i8 %xx to i32
4128+
%r = add nsw i32 %ze, -25
4129+
ret i32 %r
4130+
}
40944131

40954132
declare void @llvm.assume(i1)
40964133
declare void @fake_func(i32)

0 commit comments

Comments
 (0)