Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -2054,7 +2054,30 @@ break; } - case Intrinsic::uadd_with_overflow: + case Intrinsic::uadd_with_overflow: { + if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) + return I; + if (Instruction *I = foldIntrinsicWithOverflowCommon(II)) + return I; + + // Given 2 constant operands whose sum does not overflow: + // uaddo (X +nuw C0), C1 -> uaddo X, C0 + C1 + Value *X; + const APInt *C0, *C1; + Value *Arg0 = II->getArgOperand(0); + Value *Arg1 = II->getArgOperand(1); + if (match(Arg0, m_NUWAdd(m_Value(X), m_APInt(C0))) && + match(Arg1, m_APInt(C1))) { + bool Overflow; + APInt NewC = C1->uadd_ov(*C0, Overflow); + if (!Overflow) + return replaceInstUsesWith( + *II, Builder.CreateBinaryIntrinsic( + Intrinsic::uadd_with_overflow, X, + ConstantInt::get(Arg1->getType(), NewC))); + } + break; + } case Intrinsic::umul_with_overflow: case Intrinsic::smul_with_overflow: if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) Index: llvm/test/Transforms/InstCombine/uadd-with-overflow.ll =================================================================== --- llvm/test/Transforms/InstCombine/uadd-with-overflow.ll +++ llvm/test/Transforms/InstCombine/uadd-with-overflow.ll @@ -9,9 +9,8 @@ define { i32, i1 } @simple_fold(i32) { ; CHECK-LABEL: @simple_fold( -; CHECK-NEXT: [[TMP2:%.*]] = add nuw i32 [[TMP0:%.*]], 7 -; CHECK-NEXT: [[TMP3:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[TMP2]], i32 13) -; CHECK-NEXT: ret { i32, i1 } [[TMP3]] +; CHECK-NEXT: [[TMP2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[TMP0:%.*]], i32 20) +; CHECK-NEXT: ret { i32, i1 } [[TMP2]] ; %2 = add nuw i32 %0, 7 %3 = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %2, i32 13) @@ -20,9 +19,8 @@ define { i8, i1 } @fold_on_constant_add_no_overflow(i8) { ; CHECK-LABEL: @fold_on_constant_add_no_overflow( -; CHECK-NEXT: [[TMP2:%.*]] = add nuw i8 [[TMP0:%.*]], -56 -; CHECK-NEXT: [[TMP3:%.*]] = tail call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[TMP2]], i8 55) -; CHECK-NEXT: ret { i8, i1 } [[TMP3]] +; CHECK-NEXT: [[TMP2:%.*]] = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[TMP0:%.*]], i8 -1) +; CHECK-NEXT: ret { i8, i1 } [[TMP2]] ; %2 = add nuw i8 %0, 200 %3 = tail call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %2, i8 55) @@ -42,9 +40,8 @@ define { <2 x i32>, <2 x i1> } @fold_simple_splat_constant(<2 x i32>) { ; CHECK-LABEL: @fold_simple_splat_constant( -; CHECK-NEXT: [[TMP2:%.*]] = add nuw <2 x i32> [[TMP0:%.*]], -; CHECK-NEXT: [[TMP3:%.*]] = tail call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> [[TMP2]], <2 x i32> ) -; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[TMP3]] +; CHECK-NEXT: [[TMP2:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> [[TMP0:%.*]], <2 x i32> ) +; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[TMP2]] ; %2 = add nuw <2 x i32> %0, %3 = tail call { <2 x i32>, <2 x i1> } @llvm.uadd.with.overflow.v2i32(<2 x i32> %2, <2 x i32> ) @@ -75,9 +72,8 @@ define { i32, i1 } @fold_nuwnsw(i32) { ; CHECK-LABEL: @fold_nuwnsw( -; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i32 [[TMP0:%.*]], 12 -; CHECK-NEXT: [[TMP3:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[TMP2]], i32 30) -; CHECK-NEXT: ret { i32, i1 } [[TMP3]] +; CHECK-NEXT: [[TMP2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[TMP0:%.*]], i32 42) +; CHECK-NEXT: ret { i32, i1 } [[TMP2]] ; %2 = add nuw nsw i32 %0, 12 %3 = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %2, i32 30)