diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp --- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp +++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp @@ -3436,12 +3436,16 @@ } case G_UADDE: { auto [Res, CarryOut, LHS, RHS, CarryIn] = MI.getFirst5Regs(); - LLT Ty = MRI.getType(Res); + const LLT CondTy = MRI.getType(CarryOut); + const LLT Ty = MRI.getType(Res); auto TmpRes = MIRBuilder.buildAdd(Ty, LHS, RHS); auto ZExtCarryIn = MIRBuilder.buildZExt(Ty, CarryIn); MIRBuilder.buildAdd(Res, TmpRes, ZExtCarryIn); - MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, LHS); + + auto Res_EQ_LHS = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, CondTy, Res, LHS); + auto Res_ULT_LHS = MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CondTy, Res, LHS); + MIRBuilder.buildSelect(CarryOut, Res_EQ_LHS, CarryIn, Res_ULT_LHS); MI.eraseFromParent(); return Legalized; diff --git a/llvm/test/CodeGen/Mips/GlobalISel/legalizer/add.mir b/llvm/test/CodeGen/Mips/GlobalISel/legalizer/add.mir --- a/llvm/test/CodeGen/Mips/GlobalISel/legalizer/add.mir +++ b/llvm/test/CodeGen/Mips/GlobalISel/legalizer/add.mir @@ -265,14 +265,20 @@ ; MIPS32: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1 ; MIPS32: [[AND:%[0-9]+]]:_(s32) = G_AND [[ICMP]], [[C]] ; MIPS32: [[ADD2:%[0-9]+]]:_(s32) = G_ADD [[ADD1]], [[AND]] - ; MIPS32: [[ICMP1:%[0-9]+]]:_(s32) = G_ICMP intpred(ult), [[ADD2]](s32), [[LOAD1]] - ; MIPS32: [[ADD3:%[0-9]+]]:_(s32) = G_ADD [[LOAD2]], [[COPY2]] + ; MIPS32: [[ICMP1:%[0-9]+]]:_(s32) = G_ICMP intpred(eq), [[ADD2]](s32), [[LOAD1]] + ; MIPS32: [[ICMP2:%[0-9]+]]:_(s32) = G_ICMP intpred(ult), [[ADD2]](s32), [[LOAD1]] ; MIPS32: [[AND1:%[0-9]+]]:_(s32) = G_AND [[ICMP1]], [[C]] - ; MIPS32: [[ADD4:%[0-9]+]]:_(s32) = G_ADD [[ADD3]], [[AND1]] - ; MIPS32: [[ICMP2:%[0-9]+]]:_(s32) = G_ICMP intpred(ult), [[ADD4]](s32), [[LOAD2]] + ; MIPS32: [[SELECT:%[0-9]+]]:_(s32) = G_SELECT [[AND1]](s32), [[ICMP]], [[ICMP2]] + ; MIPS32: [[ADD3:%[0-9]+]]:_(s32) = G_ADD [[LOAD2]], [[COPY2]] + ; MIPS32: [[AND2:%[0-9]+]]:_(s32) = G_AND [[SELECT]], [[C]] + ; MIPS32: [[ADD4:%[0-9]+]]:_(s32) = G_ADD [[ADD3]], [[AND2]] + ; MIPS32: [[ICMP3:%[0-9]+]]:_(s32) = G_ICMP intpred(eq), [[ADD4]](s32), [[LOAD2]] + ; MIPS32: [[ICMP4:%[0-9]+]]:_(s32) = G_ICMP intpred(ult), [[ADD4]](s32), [[LOAD2]] + ; MIPS32: [[AND3:%[0-9]+]]:_(s32) = G_AND [[ICMP3]], [[C]] + ; MIPS32: [[SELECT1:%[0-9]+]]:_(s32) = G_SELECT [[AND3]](s32), [[SELECT]], [[ICMP4]] ; MIPS32: [[ADD5:%[0-9]+]]:_(s32) = G_ADD [[LOAD3]], [[COPY3]] - ; MIPS32: [[AND2:%[0-9]+]]:_(s32) = G_AND [[ICMP2]], [[C]] - ; MIPS32: [[ADD6:%[0-9]+]]:_(s32) = G_ADD [[ADD5]], [[AND2]] + ; MIPS32: [[AND4:%[0-9]+]]:_(s32) = G_AND [[SELECT1]], [[C]] + ; MIPS32: [[ADD6:%[0-9]+]]:_(s32) = G_ADD [[ADD5]], [[AND4]] ; MIPS32: $v0 = COPY [[ADD]](s32) ; MIPS32: $v1 = COPY [[ADD2]](s32) ; MIPS32: $a0 = COPY [[ADD4]](s32) diff --git a/llvm/test/CodeGen/Mips/GlobalISel/llvm-ir/add.ll b/llvm/test/CodeGen/Mips/GlobalISel/llvm-ir/add.ll --- a/llvm/test/CodeGen/Mips/GlobalISel/llvm-ir/add.ll +++ b/llvm/test/CodeGen/Mips/GlobalISel/llvm-ir/add.ll @@ -115,13 +115,21 @@ ; MIPS32-NEXT: addu $2, $2, $8 ; MIPS32-NEXT: sltu $8, $2, $8 ; MIPS32-NEXT: addu $3, $6, $3 -; MIPS32-NEXT: andi $8, $8, 1 -; MIPS32-NEXT: addu $3, $3, $8 +; MIPS32-NEXT: andi $9, $8, 1 +; MIPS32-NEXT: addu $3, $3, $9 +; MIPS32-NEXT: xor $9, $3, $6 +; MIPS32-NEXT: sltiu $9, $9, 1 ; MIPS32-NEXT: sltu $6, $3, $6 +; MIPS32-NEXT: andi $9, $9, 1 +; MIPS32-NEXT: movn $6, $8, $9 ; MIPS32-NEXT: addu $4, $5, $4 -; MIPS32-NEXT: andi $6, $6, 1 -; MIPS32-NEXT: addu $4, $4, $6 +; MIPS32-NEXT: andi $8, $6, 1 +; MIPS32-NEXT: addu $4, $4, $8 +; MIPS32-NEXT: xor $8, $4, $5 +; MIPS32-NEXT: sltiu $8, $8, 1 ; MIPS32-NEXT: sltu $5, $4, $5 +; MIPS32-NEXT: andi $8, $8, 1 +; MIPS32-NEXT: movn $5, $6, $8 ; MIPS32-NEXT: addu $1, $1, $7 ; MIPS32-NEXT: andi $5, $5, 1 ; MIPS32-NEXT: addu $5, $1, $5