Index: lib/Target/RISCV/RISCVISelLowering.cpp =================================================================== --- lib/Target/RISCV/RISCVISelLowering.cpp +++ lib/Target/RISCV/RISCVISelLowering.cpp @@ -100,6 +100,8 @@ setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); if (Subtarget.is64Bit()) { + setOperationAction(ISD::ADD, MVT::i32, Custom); + setOperationAction(ISD::SUB, MVT::i32, Custom); setOperationAction(ISD::SHL, MVT::i32, Custom); setOperationAction(ISD::SRA, MVT::i32, Custom); setOperationAction(ISD::SRL, MVT::i32, Custom); @@ -116,6 +118,7 @@ } if (Subtarget.is64Bit() && Subtarget.hasStdExtM()) { + setOperationAction(ISD::MUL, MVT::i32, Custom); setOperationAction(ISD::SDIV, MVT::i32, Custom); setOperationAction(ISD::UDIV, MVT::i32, Custom); setOperationAction(ISD::UREM, MVT::i32, Custom); @@ -834,6 +837,18 @@ return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes); } +// Converts the given 32-bit operation to a i64 operation with signed extension +// semantic to reduce the signed extension instructions. +static SDValue customLegalizeToWOpWithSExt(SDNode *N, SelectionDAG &DAG) { + SDLoc DL(N); + SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0)); + SDValue NewOp1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1)); + SDValue NewWOp = DAG.getNode(N->getOpcode(), DL, MVT::i64, NewOp0, NewOp1); + SDValue NewRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i64, NewWOp, + DAG.getValueType(MVT::i32)); + return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes); +} + void RISCVTargetLowering::ReplaceNodeResults(SDNode *N, SmallVectorImpl &Results, SelectionDAG &DAG) const { @@ -854,6 +869,15 @@ Results.push_back(RCW.getValue(2)); break; } + case ISD::ADD: + case ISD::SUB: + case ISD::MUL: + assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() && + "Unexpected custom legalisation"); + if (N->getOperand(1).getOpcode() == ISD::Constant) + return; + Results.push_back(customLegalizeToWOpWithSExt(N, DAG)); + break; case ISD::SHL: case ISD::SRA: case ISD::SRL: Index: test/CodeGen/RISCV/rv64i-w-insts-legalization.ll =================================================================== --- /dev/null +++ test/CodeGen/RISCV/rv64i-w-insts-legalization.ll @@ -0,0 +1,97 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -mtriple=riscv64 -mattr=+m -verify-machineinstrs < %s | FileCheck %s + +define signext i32 @addw(i32 signext %s, i32 signext %n, i32 signext %k) nounwind { +; CHECK-LABEL: addw: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: bge a0, a1, .LBB0_2 +; CHECK-NEXT: # %bb.1: # %for.body.preheader +; CHECK-NEXT: not a2, a0 +; CHECK-NEXT: add a2, a2, a1 +; CHECK-NEXT: sub a1, a1, a0 +; CHECK-NEXT: addi a1, a1, -2 +; CHECK-NEXT: slli a1, a1, 32 +; CHECK-NEXT: srli a1, a1, 32 +; CHECK-NEXT: slli a3, a2, 32 +; CHECK-NEXT: srli a3, a3, 32 +; CHECK-NEXT: mul a1, a3, a1 +; CHECK-NEXT: addi a3, a0, 1 +; CHECK-NEXT: mul a2, a2, a3 +; CHECK-NEXT: add a0, a2, a0 +; CHECK-NEXT: srli a1, a1, 1 +; CHECK-NEXT: addw a0, a0, a1 +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB0_2: +; CHECK-NEXT: mv a0, zero +; CHECK-NEXT: ret +entry: + %cmp6 = icmp slt i32 %s, %n + br i1 %cmp6, label %for.body.preheader, label %for.cond.cleanup + +for.body.preheader: ; preds = %entry + %0 = xor i32 %s, -1 + %1 = add i32 %0, %n + %2 = add i32 %s, 1 + %3 = mul i32 %1, %2 + %4 = zext i32 %1 to i33 + %5 = add i32 %n, -2 + %6 = sub i32 %5, %s + %7 = zext i32 %6 to i33 + %8 = mul i33 %4, %7 + %9 = lshr i33 %8, 1 + %10 = trunc i33 %9 to i32 + %11 = add i32 %3, %s + %12 = add i32 %11, %10 + br label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body.preheader, %entry + %sum.0.lcssa = phi i32 [ 0, %entry ], [ %12, %for.body.preheader ] + ret i32 %sum.0.lcssa +} + +define signext i32 @subw(i32 signext %s, i32 signext %n, i32 signext %k) nounwind { +; CHECK-LABEL: subw: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: bge a0, a1, .LBB1_2 +; CHECK-NEXT: # %bb.1: # %for.body.preheader +; CHECK-NEXT: sub a2, a1, a0 +; CHECK-NEXT: addi a2, a2, -2 +; CHECK-NEXT: slli a2, a2, 32 +; CHECK-NEXT: srli a2, a2, 32 +; CHECK-NEXT: not a3, a0 +; CHECK-NEXT: add a1, a3, a1 +; CHECK-NEXT: slli a4, a1, 32 +; CHECK-NEXT: srli a4, a4, 32 +; CHECK-NEXT: mul a2, a4, a2 +; CHECK-NEXT: mul a1, a1, a3 +; CHECK-NEXT: sub a0, a1, a0 +; CHECK-NEXT: srli a1, a2, 1 +; CHECK-NEXT: subw a0, a0, a1 +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB1_2: +; CHECK-NEXT: mv a0, zero +; CHECK-NEXT: ret +entry: + %cmp6 = icmp slt i32 %s, %n + br i1 %cmp6, label %for.body.preheader, label %for.cond.cleanup + +for.body.preheader: ; preds = %entry + %0 = xor i32 %s, -1 + %1 = add i32 %0, %n + %2 = xor i32 %s, -1 + %3 = mul i32 %1, %2 + %4 = zext i32 %1 to i33 + %5 = add i32 %n, -2 + %6 = sub i32 %5, %s + %7 = zext i32 %6 to i33 + %8 = mul i33 %4, %7 + %9 = lshr i33 %8, 1 + %10 = trunc i33 %9 to i32 + %11 = sub i32 %3, %s + %12 = sub i32 %11, %10 + br label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body.preheader, %entry + %sum.0.lcssa = phi i32 [ 0, %entry ], [ %12, %for.body.preheader ] + ret i32 %sum.0.lcssa +} Index: test/CodeGen/RISCV/rv64m-w-insts-legalization.ll =================================================================== --- /dev/null +++ test/CodeGen/RISCV/rv64m-w-insts-legalization.ll @@ -0,0 +1,34 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -mtriple=riscv64 -mattr=+m -verify-machineinstrs < %s | FileCheck %s + +define signext i32 @mulw(i32 signext %s, i32 signext %n, i32 signext %k) nounwind { +; CHECK-LABEL: mulw: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: addi a2, zero, 1 +; CHECK-NEXT: bge a0, a1, .LBB0_3 +; CHECK-NEXT: # %bb.1: # %for.body.preheader +; CHECK-NEXT: addi a2, zero, 1 +; CHECK-NEXT: .LBB0_2: # %for.body +; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 +; CHECK-NEXT: mulw a2, a0, a2 +; CHECK-NEXT: addiw a0, a0, 1 +; CHECK-NEXT: blt a0, a1, .LBB0_2 +; CHECK-NEXT: .LBB0_3: # %for.cond.cleanup +; CHECK-NEXT: mv a0, a2 +; CHECK-NEXT: ret +entry: + %cmp6 = icmp slt i32 %s, %n + br i1 %cmp6, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + %sum.0.lcssa = phi i32 [ 1, %entry ], [ %mul, %for.body ] + ret i32 %sum.0.lcssa + +for.body: ; preds = %entry, %for.body + %i.08 = phi i32 [ %inc, %for.body ], [ %s, %entry ] + %sum.07 = phi i32 [ %mul, %for.body ], [ 1, %entry ] + %mul = mul nsw i32 %i.08, %sum.07 + %inc = add nsw i32 %i.08, 1 + %cmp = icmp slt i32 %inc, %n + br i1 %cmp, label %for.body, label %for.cond.cleanup +}