diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -1032,7 +1032,7 @@ setTargetDAGCombine(ISD::ROTR); setTargetDAGCombine(ISD::ANY_EXTEND); setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); - if (Subtarget.hasStdExtZfh()) + if (Subtarget.hasStdExtZfh() || Subtarget.hasStdExtZbb()) setTargetDAGCombine(ISD::SIGN_EXTEND_INREG); if (Subtarget.hasStdExtF()) { setTargetDAGCombine(ISD::ZERO_EXTEND); @@ -7521,15 +7521,33 @@ return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false); } -static SDValue performSIGN_EXTEND_INREG(SDNode *N, SelectionDAG &DAG) { +static SDValue +performSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG, + const RISCVSubtarget &Subtarget) { SDValue Src = N->getOperand(0); + EVT VT = N->getValueType(0); // Fold (sext_inreg (fmv_x_anyexth X), i16) -> (fmv_x_signexth X) if (Src.getOpcode() == RISCVISD::FMV_X_ANYEXTH && cast(N->getOperand(1))->getVT().bitsGE(MVT::i16)) - return DAG.getNode(RISCVISD::FMV_X_SIGNEXTH, SDLoc(N), N->getValueType(0), + return DAG.getNode(RISCVISD::FMV_X_SIGNEXTH, SDLoc(N), VT, Src.getOperand(0)); + // Fold (sign_extend_inreg (abs X), i32) -> (smax (negw X), X) if X has more + // than 32 sign bits. + if (Subtarget.hasStdExtZbb() && Src.getOpcode() == ISD::ABS && + Src.hasOneUse() && VT == MVT::i64 && + cast(N->getOperand(1))->getVT() == MVT::i32 && + DAG.ComputeNumSignBits(Src.getOperand(0)) > 32) { + SDLoc DL(N); + SDValue Freeze = DAG.getFreeze(Src.getOperand(0)); + SDValue Neg = + DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Freeze); + Neg = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Neg, + DAG.getValueType(MVT::i32)); + return DAG.getNode(ISD::SMAX, DL, VT, Freeze, Neg); + } + return SDValue(); } @@ -8140,7 +8158,7 @@ case ISD::XOR: return performXORCombine(N, DAG); case ISD::SIGN_EXTEND_INREG: - return performSIGN_EXTEND_INREG(N, DAG); + return performSIGN_EXTEND_INREGCombine(N, DAG, Subtarget); case ISD::ANY_EXTEND: return performANY_EXTENDCombine(N, DCI, Subtarget); case ISD::ZERO_EXTEND: diff --git a/llvm/test/CodeGen/RISCV/rv64zbb.ll b/llvm/test/CodeGen/RISCV/rv64zbb.ll --- a/llvm/test/CodeGen/RISCV/rv64zbb.ll +++ b/llvm/test/CodeGen/RISCV/rv64zbb.ll @@ -961,7 +961,6 @@ ret i32 %abs } -; FIXME: We can remove the sext.w on RV64ZBB by using negw. define signext i32 @abs_i32_sext(i32 signext %x) { ; RV64I-LABEL: abs_i32_sext: ; RV64I: # %bb.0: @@ -972,9 +971,8 @@ ; ; RV64ZBB-LABEL: abs_i32_sext: ; RV64ZBB: # %bb.0: -; RV64ZBB-NEXT: neg a1, a0 +; RV64ZBB-NEXT: negw a1, a0 ; RV64ZBB-NEXT: max a0, a0, a1 -; RV64ZBB-NEXT: sext.w a0, a0 ; RV64ZBB-NEXT: ret %abs = tail call i32 @llvm.abs.i32(i32 %x, i1 true) ret i32 %abs