Skip to content

Commit d834d83

Browse files
committed
[RISCV] Add RV64F codegen support
This requires a little extra work due tothe fact i32 is not a legal type. When call lowering happens post-legalisation (e.g. when an intrinsic was inserted during legalisation). A bitcast from f32 to i32 can't be introduced. This is similar to the challenges with RV32D. To handle this, we introduce target-specific DAG nodes that perform bitcast+anyext for f32->i64 and trunc+bitcast for i64->f32. Differential Revision: https://reviews.llvm.org/D53235 llvm-svn: 352807
1 parent c0affde commit d834d83

13 files changed

+1466
-2
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

+74-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
137137
setOperationAction(Op, MVT::f32, Expand);
138138
}
139139

140+
if (Subtarget.hasStdExtF() && Subtarget.is64Bit())
141+
setOperationAction(ISD::BITCAST, MVT::i32, Custom);
142+
140143
if (Subtarget.hasStdExtD()) {
141144
setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
142145
setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
@@ -338,6 +341,17 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
338341
return lowerFRAMEADDR(Op, DAG);
339342
case ISD::RETURNADDR:
340343
return lowerRETURNADDR(Op, DAG);
344+
case ISD::BITCAST: {
345+
assert(Subtarget.is64Bit() && Subtarget.hasStdExtF() &&
346+
"Unexpected custom legalisation");
347+
SDLoc DL(Op);
348+
SDValue Op0 = Op.getOperand(0);
349+
if (Op.getValueType() != MVT::f32 || Op0.getValueType() != MVT::i32)
350+
return SDValue();
351+
SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
352+
SDValue FPConv = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, NewOp0);
353+
return FPConv;
354+
}
341355
}
342356
}
343357

@@ -579,6 +593,18 @@ void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
579593
return;
580594
Results.push_back(customLegalizeToWOp(N, DAG));
581595
break;
596+
case ISD::BITCAST: {
597+
assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
598+
Subtarget.hasStdExtF() && "Unexpected custom legalisation");
599+
SDLoc DL(N);
600+
SDValue Op0 = N->getOperand(0);
601+
if (Op0.getValueType() != MVT::f32)
602+
return;
603+
SDValue FPConv =
604+
DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Op0);
605+
Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, FPConv));
606+
break;
607+
}
582608
}
583609
}
584610

@@ -633,6 +659,38 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
633659
return SDValue();
634660
break;
635661
}
662+
case RISCVISD::FMV_X_ANYEXTW_RV64: {
663+
SDLoc DL(N);
664+
SDValue Op0 = N->getOperand(0);
665+
// If the input to FMV_X_ANYEXTW_RV64 is just FMV_W_X_RV64 then the
666+
// conversion is unnecessary and can be replaced with an ANY_EXTEND
667+
// of the FMV_W_X_RV64 operand.
668+
if (Op0->getOpcode() == RISCVISD::FMV_W_X_RV64) {
669+
SDValue AExtOp =
670+
DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0.getOperand(0));
671+
return DCI.CombineTo(N, AExtOp);
672+
}
673+
674+
// This is a target-specific version of a DAGCombine performed in
675+
// DAGCombiner::visitBITCAST. It performs the equivalent of:
676+
// fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
677+
// fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
678+
if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
679+
!Op0.getNode()->hasOneUse())
680+
break;
681+
SDValue NewFMV = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64,
682+
Op0.getOperand(0));
683+
APInt SignBit = APInt::getSignMask(32).sext(64);
684+
if (Op0.getOpcode() == ISD::FNEG) {
685+
return DCI.CombineTo(N,
686+
DAG.getNode(ISD::XOR, DL, MVT::i64, NewFMV,
687+
DAG.getConstant(SignBit, DL, MVT::i64)));
688+
}
689+
assert(Op0.getOpcode() == ISD::FABS);
690+
return DCI.CombineTo(N,
691+
DAG.getNode(ISD::AND, DL, MVT::i64, NewFMV,
692+
DAG.getConstant(~SignBit, DL, MVT::i64)));
693+
}
636694
}
637695

638696
return SDValue();
@@ -874,7 +932,7 @@ static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT,
874932
assert(XLen == 32 || XLen == 64);
875933
MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
876934
if (ValVT == MVT::f32) {
877-
LocVT = MVT::i32;
935+
LocVT = XLenVT;
878936
LocInfo = CCValAssign::BCvt;
879937
}
880938

@@ -1047,6 +1105,10 @@ static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val,
10471105
case CCValAssign::Full:
10481106
break;
10491107
case CCValAssign::BCvt:
1108+
if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32) {
1109+
Val = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, Val);
1110+
break;
1111+
}
10501112
Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
10511113
break;
10521114
}
@@ -1082,6 +1144,10 @@ static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val,
10821144
case CCValAssign::Full:
10831145
break;
10841146
case CCValAssign::BCvt:
1147+
if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32) {
1148+
Val = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Val);
1149+
break;
1150+
}
10851151
Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
10861152
break;
10871153
}
@@ -1108,9 +1174,12 @@ static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
11081174
llvm_unreachable("Unexpected CCValAssign::LocInfo");
11091175
case CCValAssign::Full:
11101176
case CCValAssign::Indirect:
1177+
case CCValAssign::BCvt:
11111178
ExtType = ISD::NON_EXTLOAD;
11121179
break;
11131180
}
1181+
if (ValVT == MVT::f32)
1182+
LocVT = MVT::f32;
11141183
Val = DAG.getExtLoad(
11151184
ExtType, DL, LocVT, Chain, FIN,
11161185
MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
@@ -1759,6 +1828,10 @@ const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
17591828
return "RISCVISD::DIVUW";
17601829
case RISCVISD::REMUW:
17611830
return "RISCVISD::REMUW";
1831+
case RISCVISD::FMV_W_X_RV64:
1832+
return "RISCVISD::FMV_W_X_RV64";
1833+
case RISCVISD::FMV_X_ANYEXTW_RV64:
1834+
return "RISCVISD::FMV_X_ANYEXTW_RV64";
17621835
}
17631836
return nullptr;
17641837
}

llvm/lib/Target/RISCV/RISCVISelLowering.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ enum NodeType : unsigned {
4141
// at instruction selection time.
4242
DIVW,
4343
DIVUW,
44-
REMUW
44+
REMUW,
45+
// FPR32<->GPR transfer operations for RV64. Needed as an i32<->f32 bitcast
46+
// is not legal on RV64. FMV_W_X_RV64 matches the semantics of the FMV.W.X.
47+
// FMV_X_ANYEXTW_RV64 is similar to FMV.X.W but has an any-extended result.
48+
// This is a more convenient semantic for producing dagcombines that remove
49+
// unnecessary GPR->FPR->GPR moves.
50+
FMV_W_X_RV64,
51+
FMV_X_ANYEXTW_RV64
4552
};
4653
}
4754

llvm/lib/Target/RISCV/RISCVInstrInfoF.td

+48
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
//===----------------------------------------------------------------------===//
15+
// RISC-V specific DAG Nodes.
16+
//===----------------------------------------------------------------------===//
17+
18+
def SDT_RISCVFMV_W_X_RV64
19+
: SDTypeProfile<1, 1, [SDTCisVT<0, f32>, SDTCisVT<1, i64>]>;
20+
def SDT_RISCVFMV_X_ANYEXTW_RV64
21+
: SDTypeProfile<1, 1, [SDTCisVT<0, i64>, SDTCisVT<1, f32>]>;
22+
23+
def riscv_fmv_w_x_rv64
24+
: SDNode<"RISCVISD::FMV_W_X_RV64", SDT_RISCVFMV_W_X_RV64>;
25+
def riscv_fmv_x_anyextw_rv64
26+
: SDNode<"RISCVISD::FMV_X_ANYEXTW_RV64", SDT_RISCVFMV_X_ANYEXTW_RV64>;
27+
1428
//===----------------------------------------------------------------------===//
1529
// Operand and SDNode transformation definitions.
1630
//===----------------------------------------------------------------------===//
@@ -333,3 +347,37 @@ def : Pat<(fp_to_uint FPR32:$rs1), (FCVT_WU_S $rs1, 0b001)>;
333347
def : Pat<(sint_to_fp GPR:$rs1), (FCVT_S_W $rs1, 0b111)>;
334348
def : Pat<(uint_to_fp GPR:$rs1), (FCVT_S_WU $rs1, 0b111)>;
335349
} // Predicates = [HasStdExtF, IsRV32]
350+
351+
let Predicates = [HasStdExtF, IsRV32] in {
352+
// FP->[u]int. Round-to-zero must be used
353+
def : Pat<(fp_to_sint FPR32:$rs1), (FCVT_W_S $rs1, 0b001)>;
354+
def : Pat<(fp_to_uint FPR32:$rs1), (FCVT_WU_S $rs1, 0b001)>;
355+
356+
// [u]int->fp. Match GCC and default to using dynamic rounding mode.
357+
def : Pat<(sint_to_fp GPR:$rs1), (FCVT_S_W $rs1, 0b111)>;
358+
def : Pat<(uint_to_fp GPR:$rs1), (FCVT_S_WU $rs1, 0b111)>;
359+
} // Predicates = [HasStdExtF, IsRV32]
360+
361+
let Predicates = [HasStdExtF, IsRV64] in {
362+
def : Pat<(riscv_fmv_w_x_rv64 GPR:$src), (FMV_W_X GPR:$src)>;
363+
def : Pat<(riscv_fmv_x_anyextw_rv64 FPR32:$src), (FMV_X_W FPR32:$src)>;
364+
def : Pat<(sexti32 (riscv_fmv_x_anyextw_rv64 FPR32:$src)),
365+
(FMV_X_W FPR32:$src)>;
366+
367+
// FP->[u]int32 is mostly handled by the FP->[u]int64 patterns. This is safe
368+
// because fpto[u|s]i produces poison if the value can't fit into the target.
369+
// We match the single case below because fcvt.wu.s sign-extends its result so
370+
// is cheaper than fcvt.lu.s+sext.w.
371+
def : Pat<(sext_inreg (assertzexti32 (fp_to_uint FPR32:$rs1)), i32),
372+
(FCVT_WU_S $rs1, 0b001)>;
373+
374+
// FP->[u]int64
375+
def : Pat<(fp_to_sint FPR32:$rs1), (FCVT_L_S $rs1, 0b001)>;
376+
def : Pat<(fp_to_uint FPR32:$rs1), (FCVT_LU_S $rs1, 0b001)>;
377+
378+
// [u]int->fp. Match GCC and default to using dynamic rounding mode.
379+
def : Pat<(sint_to_fp (sext_inreg GPR:$rs1, i32)), (FCVT_S_W $rs1, 0b111)>;
380+
def : Pat<(uint_to_fp (zexti32 GPR:$rs1)), (FCVT_S_WU $rs1, 0b111)>;
381+
def : Pat<(sint_to_fp GPR:$rs1), (FCVT_S_L $rs1, 0b111)>;
382+
def : Pat<(uint_to_fp GPR:$rs1), (FCVT_S_LU $rs1, 0b111)>;
383+
} // Predicates = [HasStdExtF, IsRV64]

0 commit comments

Comments
 (0)