diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.h b/llvm/lib/Target/RISCV/RISCVISelLowering.h --- a/llvm/lib/Target/RISCV/RISCVISelLowering.h +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.h @@ -477,6 +477,11 @@ EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const override; + bool shouldFormOverflowOp(unsigned Opcode, EVT VT, + bool MathUsed) const override { + return TargetLowering::shouldFormOverflowOp(Opcode, VT, false); + } + bool convertSetCCLogicToBitwiseLogic(EVT VT) const override { return VT.isScalarInteger(); } diff --git a/llvm/test/CodeGen/RISCV/overflow-intrinsics.ll b/llvm/test/CodeGen/RISCV/overflow-intrinsics.ll --- a/llvm/test/CodeGen/RISCV/overflow-intrinsics.ll +++ b/llvm/test/CodeGen/RISCV/overflow-intrinsics.ll @@ -48,33 +48,36 @@ define i64 @uaddo1_math_overflow_used(i64 %a, i64 %b, ptr %res) nounwind ssp { ; RV32-LABEL: uaddo1_math_overflow_used: ; RV32: # %bb.0: -; RV32-NEXT: add a5, a3, a1 -; RV32-NEXT: add a0, a2, a0 -; RV32-NEXT: sltu a1, a0, a2 -; RV32-NEXT: add a5, a5, a1 -; RV32-NEXT: beq a5, a3, .LBB1_2 +; RV32-NEXT: add a6, a3, a1 +; RV32-NEXT: add a5, a2, a0 +; RV32-NEXT: sltu a7, a5, a2 +; RV32-NEXT: add a6, a6, a7 +; RV32-NEXT: beq a6, a1, .LBB1_2 ; RV32-NEXT: # %bb.1: -; RV32-NEXT: sltu a1, a5, a3 +; RV32-NEXT: sltu a0, a6, a1 +; RV32-NEXT: beqz a0, .LBB1_3 +; RV32-NEXT: j .LBB1_4 ; RV32-NEXT: .LBB1_2: -; RV32-NEXT: bnez a1, .LBB1_4 -; RV32-NEXT: # %bb.3: +; RV32-NEXT: sltu a0, a5, a0 +; RV32-NEXT: bnez a0, .LBB1_4 +; RV32-NEXT: .LBB1_3: ; RV32-NEXT: li a2, 42 ; RV32-NEXT: .LBB1_4: -; RV32-NEXT: neg a1, a1 +; RV32-NEXT: neg a1, a0 ; RV32-NEXT: and a1, a1, a3 -; RV32-NEXT: sw a0, 0(a4) -; RV32-NEXT: sw a5, 4(a4) +; RV32-NEXT: sw a5, 0(a4) +; RV32-NEXT: sw a6, 4(a4) ; RV32-NEXT: mv a0, a2 ; RV32-NEXT: ret ; ; RV64-LABEL: uaddo1_math_overflow_used: ; RV64: # %bb.0: -; RV64-NEXT: add a0, a1, a0 -; RV64-NEXT: bltu a0, a1, .LBB1_2 +; RV64-NEXT: add a3, a1, a0 +; RV64-NEXT: bltu a3, a0, .LBB1_2 ; RV64-NEXT: # %bb.1: ; RV64-NEXT: li a1, 42 ; RV64-NEXT: .LBB1_2: -; RV64-NEXT: sd a0, 0(a2) +; RV64-NEXT: sd a3, 0(a2) ; RV64-NEXT: mv a0, a1 ; RV64-NEXT: ret %add = add i64 %b, %a @@ -200,7 +203,7 @@ ; RV32-NEXT: add a0, a2, a0 ; RV32-NEXT: sltu a1, a0, a2 ; RV32-NEXT: add a5, a5, a1 -; RV32-NEXT: beq a5, a3, .LBB5_2 +; RV32-NEXT: beq a3, a5, .LBB5_2 ; RV32-NEXT: # %bb.1: ; RV32-NEXT: sltu a1, a5, a3 ; RV32-NEXT: .LBB5_2: @@ -519,16 +522,13 @@ define i1 @uaddo_i64_increment(i64 %x, ptr %p) { ; RV32-LABEL: uaddo_i64_increment: ; RV32: # %bb.0: -; RV32-NEXT: mv a3, a0 -; RV32-NEXT: addi a4, a0, 1 -; RV32-NEXT: sltu a0, a4, a0 -; RV32-NEXT: add a5, a1, a0 -; RV32-NEXT: bgeu a4, a3, .LBB12_2 -; RV32-NEXT: # %bb.1: -; RV32-NEXT: sltu a0, a5, a1 -; RV32-NEXT: .LBB12_2: -; RV32-NEXT: sw a4, 0(a2) -; RV32-NEXT: sw a5, 4(a2) +; RV32-NEXT: addi a3, a0, 1 +; RV32-NEXT: sltu a0, a3, a0 +; RV32-NEXT: add a1, a1, a0 +; RV32-NEXT: or a0, a3, a1 +; RV32-NEXT: seqz a0, a0 +; RV32-NEXT: sw a3, 0(a2) +; RV32-NEXT: sw a1, 4(a2) ; RV32-NEXT: ret ; ; RV64-LABEL: uaddo_i64_increment: @@ -546,21 +546,17 @@ define i1 @uaddo_i8_increment_noncanonical_1(i8 %x, ptr %p) { ; RV32-LABEL: uaddo_i8_increment_noncanonical_1: ; RV32: # %bb.0: -; RV32-NEXT: andi a0, a0, 255 ; RV32-NEXT: addi a2, a0, 1 ; RV32-NEXT: andi a0, a2, 255 -; RV32-NEXT: xor a0, a0, a2 -; RV32-NEXT: snez a0, a0 +; RV32-NEXT: seqz a0, a0 ; RV32-NEXT: sb a2, 0(a1) ; RV32-NEXT: ret ; ; RV64-LABEL: uaddo_i8_increment_noncanonical_1: ; RV64: # %bb.0: -; RV64-NEXT: andi a0, a0, 255 -; RV64-NEXT: addi a2, a0, 1 +; RV64-NEXT: addiw a2, a0, 1 ; RV64-NEXT: andi a0, a2, 255 -; RV64-NEXT: xor a0, a0, a2 -; RV64-NEXT: snez a0, a0 +; RV64-NEXT: seqz a0, a0 ; RV64-NEXT: sb a2, 0(a1) ; RV64-NEXT: ret %a = add i8 1, %x ; commute @@ -592,26 +588,20 @@ define i1 @uaddo_i16_increment_noncanonical_3(i16 %x, ptr %p) { ; RV32-LABEL: uaddo_i16_increment_noncanonical_3: ; RV32: # %bb.0: -; RV32-NEXT: lui a2, 16 -; RV32-NEXT: addi a2, a2, -1 -; RV32-NEXT: and a0, a0, a2 -; RV32-NEXT: addi a3, a0, 1 -; RV32-NEXT: and a2, a3, a2 -; RV32-NEXT: xor a2, a2, a3 -; RV32-NEXT: snez a0, a2 -; RV32-NEXT: sh a3, 0(a1) +; RV32-NEXT: addi a2, a0, 1 +; RV32-NEXT: slli a0, a2, 16 +; RV32-NEXT: srli a0, a0, 16 +; RV32-NEXT: seqz a0, a0 +; RV32-NEXT: sh a2, 0(a1) ; RV32-NEXT: ret ; ; RV64-LABEL: uaddo_i16_increment_noncanonical_3: ; RV64: # %bb.0: -; RV64-NEXT: lui a2, 16 -; RV64-NEXT: addiw a2, a2, -1 -; RV64-NEXT: and a0, a0, a2 -; RV64-NEXT: addi a3, a0, 1 -; RV64-NEXT: and a2, a3, a2 -; RV64-NEXT: xor a2, a2, a3 -; RV64-NEXT: snez a0, a2 -; RV64-NEXT: sh a3, 0(a1) +; RV64-NEXT: addiw a2, a0, 1 +; RV64-NEXT: slli a0, a2, 48 +; RV64-NEXT: srli a0, a0, 48 +; RV64-NEXT: seqz a0, a0 +; RV64-NEXT: sh a2, 0(a1) ; RV64-NEXT: ret %a = add i16 1, %x ; commute %ov = icmp eq i16 0, %a ; commute