Skip to content

Commit 0d8c7df

Browse files
committed
[InstCombine] Regard zext nneg as sext when folding add(zext neg(add))
Signed-off-by: ZelinMa557 <[email protected]>
1 parent f38e5a5 commit 0d8c7df

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,9 +4093,8 @@ define i32 @fold_zext_addition_fail2(i8 %x) {
40934093

40944094
define i32 @fold_zext_nneg_add_const(i8 %x) {
40954095
; CHECK-LABEL: @fold_zext_nneg_add_const(
4096-
; CHECK-NEXT: [[XX:%.*]] = add nsw i8 [[X:%.*]], 123
4097-
; CHECK-NEXT: [[ZE:%.*]] = zext nneg i8 [[XX]] to i32
4098-
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[ZE]], -25
4096+
; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X:%.*]] to i32
4097+
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], 98
40994098
; CHECK-NEXT: ret i32 [[R]]
41004099
;
41014100
%xx = add nsw i8 %x, 123

0 commit comments

Comments
 (0)