diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp index 07c50d866544b..e659b5de395b3 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 408b0c6559b00..e75a9eb51f398 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)