blob: 8e9ab54bf009cc16a9adea57ecdcc79267870647 [file] [log] [blame]
Alex Bradbury89718422017-10-19 21:37:38 +00001//===-- RISCVISelLowering.cpp - RISCV DAG Lowering Implementation --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that RISCV uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "RISCVISelLowering.h"
16#include "RISCV.h"
Alex Bradburyc85be0d2018-01-10 19:41:03 +000017#include "RISCVMachineFunctionInfo.h"
Alex Bradbury89718422017-10-19 21:37:38 +000018#include "RISCVRegisterInfo.h"
19#include "RISCVSubtarget.h"
20#include "RISCVTargetMachine.h"
21#include "llvm/CodeGen/CallingConvLower.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Craig Topper2fa14362018-03-29 17:21:10 +000028#include "llvm/CodeGen/ValueTypes.h"
Alex Bradbury89718422017-10-19 21:37:38 +000029#include "llvm/IR/DiagnosticInfo.h"
30#include "llvm/IR/DiagnosticPrinter.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
34
35using namespace llvm;
36
37#define DEBUG_TYPE "riscv-lower"
38
39RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
40 const RISCVSubtarget &STI)
41 : TargetLowering(TM), Subtarget(STI) {
42
43 MVT XLenVT = Subtarget.getXLenVT();
44
45 // Set up the register classes.
46 addRegisterClass(XLenVT, &RISCV::GPRRegClass);
47
Alex Bradbury76c29ee2018-03-20 12:45:35 +000048 if (Subtarget.hasStdExtF())
49 addRegisterClass(MVT::f32, &RISCV::FPR32RegClass);
Alex Bradbury0b4175f2018-04-12 05:34:25 +000050 if (Subtarget.hasStdExtD())
51 addRegisterClass(MVT::f64, &RISCV::FPR64RegClass);
Alex Bradbury76c29ee2018-03-20 12:45:35 +000052
Alex Bradbury89718422017-10-19 21:37:38 +000053 // Compute derived properties from the register classes.
54 computeRegisterProperties(STI.getRegisterInfo());
55
56 setStackPointerRegisterToSaveRestore(RISCV::X2);
57
Alex Bradburycfa62912017-11-08 12:20:01 +000058 for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD})
59 setLoadExtAction(N, XLenVT, MVT::i1, Promote);
60
Alex Bradbury89718422017-10-19 21:37:38 +000061 // TODO: add all necessary setOperationAction calls.
Alex Bradburybfb00d42017-12-11 12:38:17 +000062 setOperationAction(ISD::DYNAMIC_STACKALLOC, XLenVT, Expand);
63
Alex Bradburyffc435e2017-11-21 08:11:03 +000064 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Alex Bradbury74913e12017-11-08 13:31:40 +000065 setOperationAction(ISD::BR_CC, XLenVT, Expand);
Alex Bradbury65385162017-11-21 07:51:32 +000066 setOperationAction(ISD::SELECT, XLenVT, Custom);
67 setOperationAction(ISD::SELECT_CC, XLenVT, Expand);
68
Alex Bradburybfb00d42017-12-11 12:38:17 +000069 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
70 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
71
Alex Bradburyc85be0d2018-01-10 19:41:03 +000072 setOperationAction(ISD::VASTART, MVT::Other, Custom);
73 setOperationAction(ISD::VAARG, MVT::Other, Expand);
74 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
75 setOperationAction(ISD::VAEND, MVT::Other, Expand);
76
Alex Bradburyffc435e2017-11-21 08:11:03 +000077 for (auto VT : {MVT::i1, MVT::i8, MVT::i16})
78 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
79
80 setOperationAction(ISD::ADDC, XLenVT, Expand);
81 setOperationAction(ISD::ADDE, XLenVT, Expand);
82 setOperationAction(ISD::SUBC, XLenVT, Expand);
83 setOperationAction(ISD::SUBE, XLenVT, Expand);
84
Alex Bradbury92138382018-01-18 12:36:38 +000085 if (!Subtarget.hasStdExtM()) {
86 setOperationAction(ISD::MUL, XLenVT, Expand);
87 setOperationAction(ISD::MULHS, XLenVT, Expand);
88 setOperationAction(ISD::MULHU, XLenVT, Expand);
89 setOperationAction(ISD::SDIV, XLenVT, Expand);
90 setOperationAction(ISD::UDIV, XLenVT, Expand);
91 setOperationAction(ISD::SREM, XLenVT, Expand);
92 setOperationAction(ISD::UREM, XLenVT, Expand);
93 }
Alex Bradburyffc435e2017-11-21 08:11:03 +000094
Alex Bradbury92138382018-01-18 12:36:38 +000095 setOperationAction(ISD::SDIVREM, XLenVT, Expand);
96 setOperationAction(ISD::UDIVREM, XLenVT, Expand);
Alex Bradburyffc435e2017-11-21 08:11:03 +000097 setOperationAction(ISD::SMUL_LOHI, XLenVT, Expand);
98 setOperationAction(ISD::UMUL_LOHI, XLenVT, Expand);
Alex Bradburyffc435e2017-11-21 08:11:03 +000099
100 setOperationAction(ISD::SHL_PARTS, XLenVT, Expand);
101 setOperationAction(ISD::SRL_PARTS, XLenVT, Expand);
102 setOperationAction(ISD::SRA_PARTS, XLenVT, Expand);
103
104 setOperationAction(ISD::ROTL, XLenVT, Expand);
105 setOperationAction(ISD::ROTR, XLenVT, Expand);
106 setOperationAction(ISD::BSWAP, XLenVT, Expand);
107 setOperationAction(ISD::CTTZ, XLenVT, Expand);
108 setOperationAction(ISD::CTLZ, XLenVT, Expand);
109 setOperationAction(ISD::CTPOP, XLenVT, Expand);
110
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000111 ISD::CondCode FPCCToExtend[] = {
112 ISD::SETOGT, ISD::SETOGE, ISD::SETONE, ISD::SETO, ISD::SETUEQ,
113 ISD::SETUGT, ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUNE,
114 ISD::SETGT, ISD::SETGE, ISD::SETNE};
115
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000116 if (Subtarget.hasStdExtF()) {
117 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
118 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000119 for (auto CC : FPCCToExtend)
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000120 setCondCodeAction(CC, MVT::f32, Expand);
121 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
122 setOperationAction(ISD::SELECT, MVT::f32, Custom);
123 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000124 }
125
Alex Bradbury5d0dfa52018-04-12 05:42:42 +0000126 if (Subtarget.hasStdExtD()) {
127 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
128 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000129 for (auto CC : FPCCToExtend)
130 setCondCodeAction(CC, MVT::f64, Expand);
131 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
132 setOperationAction(ISD::SELECT, MVT::f64, Custom);
133 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000134 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
Alex Bradbury60baa2e2018-04-12 05:47:15 +0000135 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Alex Bradbury5d0dfa52018-04-12 05:42:42 +0000136 }
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000137
Alex Bradburyffc435e2017-11-21 08:11:03 +0000138 setOperationAction(ISD::GlobalAddress, XLenVT, Custom);
139 setOperationAction(ISD::BlockAddress, XLenVT, Custom);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000140 setOperationAction(ISD::ConstantPool, XLenVT, Custom);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000141
Alex Bradbury89718422017-10-19 21:37:38 +0000142 setBooleanContents(ZeroOrOneBooleanContent);
143
144 // Function alignments (log2).
145 setMinFunctionAlignment(3);
146 setPrefFunctionAlignment(3);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000147
148 // Effectively disable jump table generation.
149 setMinimumJumpTableEntries(INT_MAX);
Alex Bradbury89718422017-10-19 21:37:38 +0000150}
151
Shiva Chenbbf4c5c2018-02-02 02:43:18 +0000152EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
153 EVT VT) const {
154 if (!VT.isVector())
155 return getPointerTy(DL);
156 return VT.changeVectorElementTypeToInteger();
157}
158
Alex Bradbury65385162017-11-21 07:51:32 +0000159// Changes the condition code and swaps operands if necessary, so the SetCC
160// operation matches one of the comparisons supported directly in the RISC-V
161// ISA.
162static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
163 switch (CC) {
164 default:
165 break;
166 case ISD::SETGT:
167 case ISD::SETLE:
168 case ISD::SETUGT:
169 case ISD::SETULE:
170 CC = ISD::getSetCCSwappedOperands(CC);
171 std::swap(LHS, RHS);
172 break;
173 }
174}
175
176// Return the RISC-V branch opcode that matches the given DAG integer
177// condition code. The CondCode must be one of those supported by the RISC-V
178// ISA (see normaliseSetCC).
179static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
180 switch (CC) {
181 default:
182 llvm_unreachable("Unsupported CondCode");
183 case ISD::SETEQ:
184 return RISCV::BEQ;
185 case ISD::SETNE:
186 return RISCV::BNE;
187 case ISD::SETLT:
188 return RISCV::BLT;
189 case ISD::SETGE:
190 return RISCV::BGE;
191 case ISD::SETULT:
192 return RISCV::BLTU;
193 case ISD::SETUGE:
194 return RISCV::BGEU;
195 }
196}
197
Alex Bradbury89718422017-10-19 21:37:38 +0000198SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
199 SelectionDAG &DAG) const {
200 switch (Op.getOpcode()) {
201 default:
202 report_fatal_error("unimplemented operand");
Alex Bradburyec8aa912017-11-08 13:24:21 +0000203 case ISD::GlobalAddress:
204 return lowerGlobalAddress(Op, DAG);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000205 case ISD::BlockAddress:
206 return lowerBlockAddress(Op, DAG);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000207 case ISD::ConstantPool:
208 return lowerConstantPool(Op, DAG);
Alex Bradbury65385162017-11-21 07:51:32 +0000209 case ISD::SELECT:
210 return lowerSELECT(Op, DAG);
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000211 case ISD::VASTART:
212 return lowerVASTART(Op, DAG);
Alex Bradbury70f137b2018-01-10 20:12:00 +0000213 case ISD::FRAMEADDR:
214 return LowerFRAMEADDR(Op, DAG);
215 case ISD::RETURNADDR:
216 return LowerRETURNADDR(Op, DAG);
Alex Bradburyec8aa912017-11-08 13:24:21 +0000217 }
218}
219
220SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
221 SelectionDAG &DAG) const {
222 SDLoc DL(Op);
223 EVT Ty = Op.getValueType();
224 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
225 const GlobalValue *GV = N->getGlobal();
226 int64_t Offset = N->getOffset();
227
Alex Bradburyffc435e2017-11-21 08:11:03 +0000228 if (isPositionIndependent() || Subtarget.is64Bit())
Alex Bradburyec8aa912017-11-08 13:24:21 +0000229 report_fatal_error("Unable to lowerGlobalAddress");
Alex Bradburyffc435e2017-11-21 08:11:03 +0000230
231 SDValue GAHi =
232 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_HI);
233 SDValue GALo =
234 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_LO);
235 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
236 SDValue MNLo =
237 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
238 return MNLo;
239}
240
241SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
242 SelectionDAG &DAG) const {
243 SDLoc DL(Op);
244 EVT Ty = Op.getValueType();
245 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
246 const BlockAddress *BA = N->getBlockAddress();
247 int64_t Offset = N->getOffset();
248
249 if (isPositionIndependent() || Subtarget.is64Bit())
250 report_fatal_error("Unable to lowerBlockAddress");
251
252 SDValue BAHi = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_HI);
253 SDValue BALo = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_LO);
254 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, BAHi), 0);
255 SDValue MNLo =
256 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, BALo), 0);
257 return MNLo;
258}
259
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000260SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
261 SelectionDAG &DAG) const {
262 SDLoc DL(Op);
263 EVT Ty = Op.getValueType();
264 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
265 const Constant *CPA = N->getConstVal();
266 int64_t Offset = N->getOffset();
267 unsigned Alignment = N->getAlignment();
268
269 if (!isPositionIndependent()) {
270 SDValue CPAHi =
271 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_HI);
272 SDValue CPALo =
273 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_LO);
274 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, CPAHi), 0);
275 SDValue MNLo =
276 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, CPALo), 0);
277 return MNLo;
278 } else {
279 report_fatal_error("Unable to lowerConstantPool");
280 }
281}
282
Alex Bradburyffc435e2017-11-21 08:11:03 +0000283SDValue RISCVTargetLowering::lowerExternalSymbol(SDValue Op,
284 SelectionDAG &DAG) const {
285 SDLoc DL(Op);
286 EVT Ty = Op.getValueType();
287 ExternalSymbolSDNode *N = cast<ExternalSymbolSDNode>(Op);
288 const char *Sym = N->getSymbol();
289
290 // TODO: should also handle gp-relative loads.
291
292 if (isPositionIndependent() || Subtarget.is64Bit())
293 report_fatal_error("Unable to lowerExternalSymbol");
294
295 SDValue GAHi = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_HI);
296 SDValue GALo = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_LO);
297 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
298 SDValue MNLo =
299 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
300 return MNLo;
Alex Bradbury89718422017-10-19 21:37:38 +0000301}
302
Alex Bradbury65385162017-11-21 07:51:32 +0000303SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
304 SDValue CondV = Op.getOperand(0);
305 SDValue TrueV = Op.getOperand(1);
306 SDValue FalseV = Op.getOperand(2);
307 SDLoc DL(Op);
308 MVT XLenVT = Subtarget.getXLenVT();
309
310 // If the result type is XLenVT and CondV is the output of a SETCC node
311 // which also operated on XLenVT inputs, then merge the SETCC node into the
312 // lowered RISCVISD::SELECT_CC to take advantage of the integer
313 // compare+branch instructions. i.e.:
314 // (select (setcc lhs, rhs, cc), truev, falsev)
315 // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
316 if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
317 CondV.getOperand(0).getSimpleValueType() == XLenVT) {
318 SDValue LHS = CondV.getOperand(0);
319 SDValue RHS = CondV.getOperand(1);
320 auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
321 ISD::CondCode CCVal = CC->get();
322
323 normaliseSetCC(LHS, RHS, CCVal);
324
325 SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
326 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
327 SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
328 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
329 }
330
331 // Otherwise:
332 // (select condv, truev, falsev)
333 // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
334 SDValue Zero = DAG.getConstant(0, DL, XLenVT);
335 SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
336
337 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
338 SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
339
340 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
341}
342
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000343SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
344 MachineFunction &MF = DAG.getMachineFunction();
345 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
346
347 SDLoc DL(Op);
348 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
349 getPointerTy(MF.getDataLayout()));
350
351 // vastart just stores the address of the VarArgsFrameIndex slot into the
352 // memory location argument.
353 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
354 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
355 MachinePointerInfo(SV));
356}
357
Alex Bradbury70f137b2018-01-10 20:12:00 +0000358SDValue RISCVTargetLowering::LowerFRAMEADDR(SDValue Op,
359 SelectionDAG &DAG) const {
360 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
361 MachineFunction &MF = DAG.getMachineFunction();
362 MachineFrameInfo &MFI = MF.getFrameInfo();
363 MFI.setFrameAddressIsTaken(true);
364 unsigned FrameReg = RI.getFrameRegister(MF);
365 int XLenInBytes = Subtarget.getXLen() / 8;
366
367 EVT VT = Op.getValueType();
368 SDLoc DL(Op);
369 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
370 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
371 while (Depth--) {
372 int Offset = -(XLenInBytes * 2);
373 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
374 DAG.getIntPtrConstant(Offset, DL));
375 FrameAddr =
376 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
377 }
378 return FrameAddr;
379}
380
381SDValue RISCVTargetLowering::LowerRETURNADDR(SDValue Op,
382 SelectionDAG &DAG) const {
383 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
384 MachineFunction &MF = DAG.getMachineFunction();
385 MachineFrameInfo &MFI = MF.getFrameInfo();
386 MFI.setReturnAddressIsTaken(true);
387 MVT XLenVT = Subtarget.getXLenVT();
388 int XLenInBytes = Subtarget.getXLen() / 8;
389
390 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
391 return SDValue();
392
393 EVT VT = Op.getValueType();
394 SDLoc DL(Op);
395 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
396 if (Depth) {
397 int Off = -XLenInBytes;
398 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
399 SDValue Offset = DAG.getConstant(Off, DL, VT);
400 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
401 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
402 MachinePointerInfo());
403 }
404
405 // Return the value of the return address register, marking it an implicit
406 // live-in.
407 unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
408 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
409}
410
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000411static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
412 MachineBasicBlock *BB) {
413 assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
414
415 MachineFunction &MF = *BB->getParent();
416 DebugLoc DL = MI.getDebugLoc();
417 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
418 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
419 unsigned LoReg = MI.getOperand(0).getReg();
420 unsigned HiReg = MI.getOperand(1).getReg();
421 unsigned SrcReg = MI.getOperand(2).getReg();
422 const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
423 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
424
425 TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
426 RI);
427 MachineMemOperand *MMO =
428 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
429 MachineMemOperand::MOLoad, 8, 8);
430 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
431 .addFrameIndex(FI)
432 .addImm(0)
433 .addMemOperand(MMO);
434 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
435 .addFrameIndex(FI)
436 .addImm(4)
437 .addMemOperand(MMO);
438 MI.eraseFromParent(); // The pseudo instruction is gone now.
439 return BB;
440}
441
442static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
443 MachineBasicBlock *BB) {
444 assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
445 "Unexpected instruction");
446
447 MachineFunction &MF = *BB->getParent();
448 DebugLoc DL = MI.getDebugLoc();
449 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
450 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
451 unsigned DstReg = MI.getOperand(0).getReg();
452 unsigned LoReg = MI.getOperand(1).getReg();
453 unsigned HiReg = MI.getOperand(2).getReg();
454 const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
455 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
456
457 MachineMemOperand *MMO =
458 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
459 MachineMemOperand::MOStore, 8, 8);
460 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
461 .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
462 .addFrameIndex(FI)
463 .addImm(0)
464 .addMemOperand(MMO);
465 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
466 .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
467 .addFrameIndex(FI)
468 .addImm(4)
469 .addMemOperand(MMO);
470 TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
471 MI.eraseFromParent(); // The pseudo instruction is gone now.
472 return BB;
473}
474
Alex Bradbury65385162017-11-21 07:51:32 +0000475MachineBasicBlock *
476RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
477 MachineBasicBlock *BB) const {
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000478 switch (MI.getOpcode()) {
479 default:
480 llvm_unreachable("Unexpected instr type to insert");
481 case RISCV::Select_GPR_Using_CC_GPR:
482 case RISCV::Select_FPR32_Using_CC_GPR:
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000483 case RISCV::Select_FPR64_Using_CC_GPR:
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000484 break;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000485 case RISCV::BuildPairF64Pseudo:
486 return emitBuildPairF64Pseudo(MI, BB);
487 case RISCV::SplitF64Pseudo:
488 return emitSplitF64Pseudo(MI, BB);
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000489 }
Alex Bradbury65385162017-11-21 07:51:32 +0000490
491 // To "insert" a SELECT instruction, we actually have to insert the triangle
492 // control-flow pattern. The incoming instruction knows the destination vreg
493 // to set, the condition code register to branch on, the true/false values to
494 // select between, and the condcode to use to select the appropriate branch.
495 //
496 // We produce the following control flow:
497 // HeadMBB
498 // | \
499 // | IfFalseMBB
500 // | /
501 // TailMBB
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000502 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
Alex Bradbury65385162017-11-21 07:51:32 +0000503 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000504 DebugLoc DL = MI.getDebugLoc();
Alex Bradbury65385162017-11-21 07:51:32 +0000505 MachineFunction::iterator I = ++BB->getIterator();
506
507 MachineBasicBlock *HeadMBB = BB;
508 MachineFunction *F = BB->getParent();
509 MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
510 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
511
512 F->insert(I, IfFalseMBB);
513 F->insert(I, TailMBB);
514 // Move all remaining instructions to TailMBB.
515 TailMBB->splice(TailMBB->begin(), HeadMBB,
516 std::next(MachineBasicBlock::iterator(MI)), HeadMBB->end());
517 // Update machine-CFG edges by transferring all successors of the current
518 // block to the new block which will contain the Phi node for the select.
519 TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
520 // Set the successors for HeadMBB.
521 HeadMBB->addSuccessor(IfFalseMBB);
522 HeadMBB->addSuccessor(TailMBB);
523
524 // Insert appropriate branch.
525 unsigned LHS = MI.getOperand(1).getReg();
526 unsigned RHS = MI.getOperand(2).getReg();
527 auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
528 unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
529
530 BuildMI(HeadMBB, DL, TII.get(Opcode))
531 .addReg(LHS)
532 .addReg(RHS)
533 .addMBB(TailMBB);
534
535 // IfFalseMBB just falls through to TailMBB.
536 IfFalseMBB->addSuccessor(TailMBB);
537
538 // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
539 BuildMI(*TailMBB, TailMBB->begin(), DL, TII.get(RISCV::PHI),
540 MI.getOperand(0).getReg())
541 .addReg(MI.getOperand(4).getReg())
542 .addMBB(HeadMBB)
543 .addReg(MI.getOperand(5).getReg())
544 .addMBB(IfFalseMBB);
545
546 MI.eraseFromParent(); // The pseudo instruction is gone now.
547 return TailMBB;
548}
549
Alex Bradbury89718422017-10-19 21:37:38 +0000550// Calling Convention Implementation.
Alex Bradburydc31c612017-12-11 12:49:02 +0000551// The expectations for frontend ABI lowering vary from target to target.
552// Ideally, an LLVM frontend would be able to avoid worrying about many ABI
553// details, but this is a longer term goal. For now, we simply try to keep the
554// role of the frontend as simple and well-defined as possible. The rules can
555// be summarised as:
556// * Never split up large scalar arguments. We handle them here.
557// * If a hardfloat calling convention is being used, and the struct may be
558// passed in a pair of registers (fp+fp, int+fp), and both registers are
559// available, then pass as two separate arguments. If either the GPRs or FPRs
560// are exhausted, then pass according to the rule below.
561// * If a struct could never be passed in registers or directly in a stack
562// slot (as it is larger than 2*XLEN and the floating point rules don't
563// apply), then pass it using a pointer with the byval attribute.
564// * If a struct is less than 2*XLEN, then coerce to either a two-element
565// word-sized array or a 2*XLEN scalar (depending on alignment).
566// * The frontend can determine whether a struct is returned by reference or
567// not based on its size and fields. If it will be returned by reference, the
568// frontend must modify the prototype so a pointer with the sret annotation is
569// passed as the first argument. This is not necessary for large scalar
570// returns.
571// * Struct return values and varargs should be coerced to structs containing
572// register-size fields in the same situations they would be for fixed
573// arguments.
574
575static const MCPhysReg ArgGPRs[] = {
576 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
577 RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
578};
579
580// Pass a 2*XLEN argument that has been split into two XLEN values through
581// registers or the stack as necessary.
582static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
583 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
584 MVT ValVT2, MVT LocVT2,
585 ISD::ArgFlagsTy ArgFlags2) {
586 unsigned XLenInBytes = XLen / 8;
587 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
588 // At least one half can be passed via register.
589 State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
590 VA1.getLocVT(), CCValAssign::Full));
591 } else {
592 // Both halves must be passed on the stack, with proper alignment.
593 unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign());
594 State.addLoc(
595 CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
596 State.AllocateStack(XLenInBytes, StackAlign),
597 VA1.getLocVT(), CCValAssign::Full));
598 State.addLoc(CCValAssign::getMem(
599 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
600 CCValAssign::Full));
601 return false;
602 }
603
604 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
605 // The second half can also be passed via register.
606 State.addLoc(
607 CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
608 } else {
609 // The second half is passed via the stack, without additional alignment.
610 State.addLoc(CCValAssign::getMem(
611 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
612 CCValAssign::Full));
613 }
614
615 return false;
616}
617
618// Implements the RISC-V calling convention. Returns true upon failure.
619static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT,
620 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000621 CCState &State, bool IsFixed, bool IsRet, Type *OrigTy) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000622 unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
623 assert(XLen == 32 || XLen == 64);
624 MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000625 if (ValVT == MVT::f32) {
626 LocVT = MVT::i32;
627 LocInfo = CCValAssign::BCvt;
628 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000629
630 // Any return value split in to more than two values can't be returned
631 // directly.
632 if (IsRet && ValNo > 1)
633 return true;
634
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000635 // If this is a variadic argument, the RISC-V calling convention requires
636 // that it is assigned an 'even' or 'aligned' register if it has 8-byte
637 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
638 // be used regardless of whether the original argument was split during
639 // legalisation or not. The argument will not be passed by registers if the
640 // original type is larger than 2*XLEN, so the register alignment rule does
641 // not apply.
642 unsigned TwoXLenInBytes = (2 * XLen) / 8;
643 if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes &&
644 DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
645 unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
646 // Skip 'odd' register if necessary.
647 if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
648 State.AllocateReg(ArgGPRs);
649 }
650
Alex Bradburydc31c612017-12-11 12:49:02 +0000651 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
652 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
653 State.getPendingArgFlags();
654
655 assert(PendingLocs.size() == PendingArgFlags.size() &&
656 "PendingLocs and PendingArgFlags out of sync");
657
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000658 // Handle passing f64 on RV32D with a soft float ABI.
659 if (XLen == 32 && ValVT == MVT::f64) {
660 assert(!ArgFlags.isSplit() && PendingLocs.empty() ||
661 "Can't lower f64 if it is split");
662 // Depending on available argument GPRS, f64 may be passed in a pair of
663 // GPRs, split between a GPR and the stack, or passed completely on the
664 // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
665 // cases.
666 unsigned Reg = State.AllocateReg(ArgGPRs);
667 LocVT = MVT::i32;
668 if (!Reg) {
669 unsigned StackOffset = State.AllocateStack(8, 8);
670 State.addLoc(
671 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
672 return false;
673 }
674 if (!State.AllocateReg(ArgGPRs))
675 State.AllocateStack(4, 4);
676 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
677 return false;
678 }
679
Alex Bradburydc31c612017-12-11 12:49:02 +0000680 // Split arguments might be passed indirectly, so keep track of the pending
681 // values.
682 if (ArgFlags.isSplit() || !PendingLocs.empty()) {
683 LocVT = XLenVT;
684 LocInfo = CCValAssign::Indirect;
685 PendingLocs.push_back(
686 CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
687 PendingArgFlags.push_back(ArgFlags);
688 if (!ArgFlags.isSplitEnd()) {
689 return false;
690 }
691 }
692
693 // If the split argument only had two elements, it should be passed directly
694 // in registers or on the stack.
695 if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
696 assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
697 // Apply the normal calling convention rules to the first half of the
698 // split argument.
699 CCValAssign VA = PendingLocs[0];
700 ISD::ArgFlagsTy AF = PendingArgFlags[0];
701 PendingLocs.clear();
702 PendingArgFlags.clear();
703 return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
704 ArgFlags);
705 }
706
707 // Allocate to a register if possible, or else a stack slot.
708 unsigned Reg = State.AllocateReg(ArgGPRs);
709 unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8);
710
711 // If we reach this point and PendingLocs is non-empty, we must be at the
712 // end of a split argument that must be passed indirectly.
713 if (!PendingLocs.empty()) {
714 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
715 assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
716
717 for (auto &It : PendingLocs) {
718 if (Reg)
719 It.convertToReg(Reg);
720 else
721 It.convertToMem(StackOffset);
722 State.addLoc(It);
723 }
724 PendingLocs.clear();
725 PendingArgFlags.clear();
726 return false;
727 }
728
729 assert(LocVT == XLenVT && "Expected an XLenVT at this stage");
730
731 if (Reg) {
732 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
733 } else {
734 State.addLoc(
735 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
736 }
737 return false;
738}
739
740void RISCVTargetLowering::analyzeInputArgs(
741 MachineFunction &MF, CCState &CCInfo,
742 const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
743 unsigned NumArgs = Ins.size();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000744 FunctionType *FType = MF.getFunction().getFunctionType();
Alex Bradburydc31c612017-12-11 12:49:02 +0000745
746 for (unsigned i = 0; i != NumArgs; ++i) {
747 MVT ArgVT = Ins[i].VT;
748 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
749
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000750 Type *ArgTy = nullptr;
751 if (IsRet)
752 ArgTy = FType->getReturnType();
753 else if (Ins[i].isOrigArg())
754 ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
755
Alex Bradburydc31c612017-12-11 12:49:02 +0000756 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000757 ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000758 DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
759 << EVT(ArgVT).getEVTString() << '\n');
760 llvm_unreachable(nullptr);
761 }
762 }
763}
764
765void RISCVTargetLowering::analyzeOutputArgs(
766 MachineFunction &MF, CCState &CCInfo,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000767 const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
768 CallLoweringInfo *CLI) const {
Alex Bradburydc31c612017-12-11 12:49:02 +0000769 unsigned NumArgs = Outs.size();
770
771 for (unsigned i = 0; i != NumArgs; i++) {
772 MVT ArgVT = Outs[i].VT;
773 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000774 Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
Alex Bradburydc31c612017-12-11 12:49:02 +0000775
776 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000777 ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000778 DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
779 << EVT(ArgVT).getEVTString() << "\n");
780 llvm_unreachable(nullptr);
781 }
782 }
783}
784
785// The caller is responsible for loading the full value if the argument is
786// passed with CCValAssign::Indirect.
787static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
788 const CCValAssign &VA, const SDLoc &DL) {
789 MachineFunction &MF = DAG.getMachineFunction();
790 MachineRegisterInfo &RegInfo = MF.getRegInfo();
791 EVT LocVT = VA.getLocVT();
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000792 EVT ValVT = VA.getValVT();
Alex Bradburydc31c612017-12-11 12:49:02 +0000793 SDValue Val;
794
795 unsigned VReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
796 RegInfo.addLiveIn(VA.getLocReg(), VReg);
797 Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
798
799 switch (VA.getLocInfo()) {
800 default:
801 llvm_unreachable("Unexpected CCValAssign::LocInfo");
802 case CCValAssign::Full:
803 case CCValAssign::Indirect:
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000804 break;
805 case CCValAssign::BCvt:
806 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
807 break;
Alex Bradburydc31c612017-12-11 12:49:02 +0000808 }
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000809 return Val;
Alex Bradburydc31c612017-12-11 12:49:02 +0000810}
811
812// The caller is responsible for loading the full value if the argument is
813// passed with CCValAssign::Indirect.
814static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
815 const CCValAssign &VA, const SDLoc &DL) {
816 MachineFunction &MF = DAG.getMachineFunction();
817 MachineFrameInfo &MFI = MF.getFrameInfo();
818 EVT LocVT = VA.getLocVT();
819 EVT ValVT = VA.getValVT();
820 EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
821 int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
822 VA.getLocMemOffset(), /*Immutable=*/true);
823 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
824 SDValue Val;
825
826 ISD::LoadExtType ExtType;
827 switch (VA.getLocInfo()) {
828 default:
829 llvm_unreachable("Unexpected CCValAssign::LocInfo");
830 case CCValAssign::Full:
831 case CCValAssign::Indirect:
832 ExtType = ISD::NON_EXTLOAD;
833 break;
834 }
835 Val = DAG.getExtLoad(
836 ExtType, DL, LocVT, Chain, FIN,
837 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
838 return Val;
839}
Alex Bradbury89718422017-10-19 21:37:38 +0000840
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000841static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
842 const CCValAssign &VA, const SDLoc &DL) {
843 assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
844 "Unexpected VA");
845 MachineFunction &MF = DAG.getMachineFunction();
846 MachineFrameInfo &MFI = MF.getFrameInfo();
847 MachineRegisterInfo &RegInfo = MF.getRegInfo();
848
849 if (VA.isMemLoc()) {
850 // f64 is passed on the stack.
851 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
852 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
853 return DAG.getLoad(MVT::f64, DL, Chain, FIN,
854 MachinePointerInfo::getFixedStack(MF, FI));
855 }
856
857 assert(VA.isRegLoc() && "Expected register VA assignment");
858
859 unsigned LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
860 RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
861 SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
862 SDValue Hi;
863 if (VA.getLocReg() == RISCV::X17) {
864 // Second half of f64 is passed on the stack.
865 int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
866 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
867 Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
868 MachinePointerInfo::getFixedStack(MF, FI));
869 } else {
870 // Second half of f64 is passed in another GPR.
871 unsigned HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
872 RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
873 Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
874 }
875 return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
876}
877
Alex Bradbury89718422017-10-19 21:37:38 +0000878// Transform physical registers into virtual registers.
879SDValue RISCVTargetLowering::LowerFormalArguments(
880 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
881 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
882 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
883
884 switch (CallConv) {
885 default:
886 report_fatal_error("Unsupported calling convention");
887 case CallingConv::C:
Alex Bradburya3376752017-11-08 13:41:21 +0000888 case CallingConv::Fast:
Alex Bradbury89718422017-10-19 21:37:38 +0000889 break;
890 }
891
892 MachineFunction &MF = DAG.getMachineFunction();
Alex Bradburydc31c612017-12-11 12:49:02 +0000893 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000894 MVT XLenVT = Subtarget.getXLenVT();
895 unsigned XLenInBytes = Subtarget.getXLen() / 8;
896 // Used with vargs to acumulate store chains.
897 std::vector<SDValue> OutChains;
Alex Bradbury89718422017-10-19 21:37:38 +0000898
899 // Assign locations to all of the incoming arguments.
900 SmallVector<CCValAssign, 16> ArgLocs;
901 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +0000902 analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
Alex Bradbury89718422017-10-19 21:37:38 +0000903
Alex Bradburydc31c612017-12-11 12:49:02 +0000904 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
905 CCValAssign &VA = ArgLocs[i];
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000906 assert(VA.getLocVT() == XLenVT && "Unhandled argument type");
Alex Bradburydc31c612017-12-11 12:49:02 +0000907 SDValue ArgValue;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000908 // Passing f64 on RV32D with a soft float ABI must be handled as a special
909 // case.
910 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
911 ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
912 else if (VA.isRegLoc())
Alex Bradburydc31c612017-12-11 12:49:02 +0000913 ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL);
914 else
915 ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
Alex Bradbury89718422017-10-19 21:37:38 +0000916
Alex Bradburydc31c612017-12-11 12:49:02 +0000917 if (VA.getLocInfo() == CCValAssign::Indirect) {
918 // If the original argument was split and passed by reference (e.g. i128
919 // on RV32), we need to load all parts of it here (using the same
920 // address).
921 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
922 MachinePointerInfo()));
923 unsigned ArgIndex = Ins[i].OrigArgIndex;
924 assert(Ins[i].PartOffset == 0);
925 while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
926 CCValAssign &PartVA = ArgLocs[i + 1];
927 unsigned PartOffset = Ins[i + 1].PartOffset;
928 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
929 DAG.getIntPtrConstant(PartOffset, DL));
930 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
931 MachinePointerInfo()));
932 ++i;
933 }
934 continue;
Alex Bradbury89718422017-10-19 21:37:38 +0000935 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000936 InVals.push_back(ArgValue);
Alex Bradbury89718422017-10-19 21:37:38 +0000937 }
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000938
939 if (IsVarArg) {
940 ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
941 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
942 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
943 MachineFrameInfo &MFI = MF.getFrameInfo();
944 MachineRegisterInfo &RegInfo = MF.getRegInfo();
945 RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
946
947 // Offset of the first variable argument from stack pointer, and size of
948 // the vararg save area. For now, the varargs save area is either zero or
949 // large enough to hold a0-a7.
950 int VaArgOffset, VarArgsSaveSize;
951
952 // If all registers are allocated, then all varargs must be passed on the
953 // stack and we don't need to save any argregs.
954 if (ArgRegs.size() == Idx) {
955 VaArgOffset = CCInfo.getNextStackOffset();
956 VarArgsSaveSize = 0;
957 } else {
958 VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
959 VaArgOffset = -VarArgsSaveSize;
960 }
961
962 // Record the frame index of the first variable argument
963 // which is a value necessary to VASTART.
964 int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
965 RVFI->setVarArgsFrameIndex(FI);
966
967 // If saving an odd number of registers then create an extra stack slot to
968 // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
969 // offsets to even-numbered registered remain 2*XLEN-aligned.
970 if (Idx % 2) {
971 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes,
972 true);
973 VarArgsSaveSize += XLenInBytes;
974 }
975
976 // Copy the integer registers that may have been used for passing varargs
977 // to the vararg save area.
978 for (unsigned I = Idx; I < ArgRegs.size();
979 ++I, VaArgOffset += XLenInBytes) {
980 const unsigned Reg = RegInfo.createVirtualRegister(RC);
981 RegInfo.addLiveIn(ArgRegs[I], Reg);
982 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
983 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
984 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
985 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
986 MachinePointerInfo::getFixedStack(MF, FI));
987 cast<StoreSDNode>(Store.getNode())
988 ->getMemOperand()
989 ->setValue((Value *)nullptr);
990 OutChains.push_back(Store);
991 }
992 RVFI->setVarArgsSaveSize(VarArgsSaveSize);
993 }
994
995 // All stores are grouped in one node to allow the matching between
996 // the size of Ins and InVals. This only happens for vararg functions.
997 if (!OutChains.empty()) {
998 OutChains.push_back(Chain);
999 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
1000 }
1001
Alex Bradbury89718422017-10-19 21:37:38 +00001002 return Chain;
1003}
1004
Alex Bradburya3376752017-11-08 13:41:21 +00001005// Lower a call to a callseq_start + CALL + callseq_end chain, and add input
1006// and output parameter nodes.
1007SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1008 SmallVectorImpl<SDValue> &InVals) const {
1009 SelectionDAG &DAG = CLI.DAG;
1010 SDLoc &DL = CLI.DL;
1011 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1012 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1013 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1014 SDValue Chain = CLI.Chain;
1015 SDValue Callee = CLI.Callee;
1016 CLI.IsTailCall = false;
1017 CallingConv::ID CallConv = CLI.CallConv;
1018 bool IsVarArg = CLI.IsVarArg;
1019 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburydc31c612017-12-11 12:49:02 +00001020 MVT XLenVT = Subtarget.getXLenVT();
Alex Bradburya3376752017-11-08 13:41:21 +00001021
Alex Bradburya3376752017-11-08 13:41:21 +00001022 MachineFunction &MF = DAG.getMachineFunction();
1023
1024 // Analyze the operands of the call, assigning locations to each operand.
1025 SmallVector<CCValAssign, 16> ArgLocs;
1026 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001027 analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
Alex Bradburya3376752017-11-08 13:41:21 +00001028
1029 // Get a count of how many bytes are to be pushed on the stack.
1030 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1031
Alex Bradburydc31c612017-12-11 12:49:02 +00001032 // Create local copies for byval args
1033 SmallVector<SDValue, 8> ByValArgs;
1034 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1035 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1036 if (!Flags.isByVal())
Alex Bradburya3376752017-11-08 13:41:21 +00001037 continue;
Alex Bradburydc31c612017-12-11 12:49:02 +00001038
1039 SDValue Arg = OutVals[i];
1040 unsigned Size = Flags.getByValSize();
1041 unsigned Align = Flags.getByValAlign();
1042
1043 int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false);
1044 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1045 SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
1046
1047 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
1048 /*IsVolatile=*/false,
1049 /*AlwaysInline=*/false,
1050 /*isTailCall=*/false, MachinePointerInfo(),
1051 MachinePointerInfo());
1052 ByValArgs.push_back(FIPtr);
Alex Bradburya3376752017-11-08 13:41:21 +00001053 }
1054
1055 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
1056
1057 // Copy argument values to their designated locations.
1058 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Alex Bradburydc31c612017-12-11 12:49:02 +00001059 SmallVector<SDValue, 8> MemOpChains;
Alex Bradburya3376752017-11-08 13:41:21 +00001060 SDValue StackPtr;
Alex Bradburydc31c612017-12-11 12:49:02 +00001061 for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
1062 CCValAssign &VA = ArgLocs[i];
1063 SDValue ArgValue = OutVals[i];
1064 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Alex Bradburya3376752017-11-08 13:41:21 +00001065
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001066 // Handle passing f64 on RV32D with a soft float ABI as a special case.
1067 bool IsF64OnRV32DSoftABI =
1068 VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
1069 if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
1070 SDValue SplitF64 = DAG.getNode(
1071 RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
1072 SDValue Lo = SplitF64.getValue(0);
1073 SDValue Hi = SplitF64.getValue(1);
1074
1075 unsigned RegLo = VA.getLocReg();
1076 RegsToPass.push_back(std::make_pair(RegLo, Lo));
1077
1078 if (RegLo == RISCV::X17) {
1079 // Second half of f64 is passed on the stack.
1080 // Work out the address of the stack slot.
1081 if (!StackPtr.getNode())
1082 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1083 // Emit the store.
1084 MemOpChains.push_back(
1085 DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
1086 } else {
1087 // Second half of f64 is passed in another GPR.
1088 unsigned RegHigh = RegLo + 1;
1089 RegsToPass.push_back(std::make_pair(RegHigh, Hi));
1090 }
1091 continue;
1092 }
1093
1094 // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
1095 // as any other MemLoc.
1096
Alex Bradburya3376752017-11-08 13:41:21 +00001097 // Promote the value if needed.
Alex Bradburydc31c612017-12-11 12:49:02 +00001098 // For now, only handle fully promoted and indirect arguments.
Alex Bradburya3376752017-11-08 13:41:21 +00001099 switch (VA.getLocInfo()) {
1100 case CCValAssign::Full:
1101 break;
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001102 case CCValAssign::BCvt:
1103 ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), ArgValue);
1104 break;
Alex Bradburydc31c612017-12-11 12:49:02 +00001105 case CCValAssign::Indirect: {
1106 // Store the argument in a stack slot and pass its address.
1107 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
1108 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1109 MemOpChains.push_back(
1110 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1111 MachinePointerInfo::getFixedStack(MF, FI)));
1112 // If the original argument was split (e.g. i128), we need
1113 // to store all parts of it here (and pass just one address).
1114 unsigned ArgIndex = Outs[i].OrigArgIndex;
1115 assert(Outs[i].PartOffset == 0);
1116 while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
1117 SDValue PartValue = OutVals[i + 1];
1118 unsigned PartOffset = Outs[i + 1].PartOffset;
1119 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1120 DAG.getIntPtrConstant(PartOffset, DL));
1121 MemOpChains.push_back(
1122 DAG.getStore(Chain, DL, PartValue, Address,
1123 MachinePointerInfo::getFixedStack(MF, FI)));
1124 ++i;
1125 }
1126 ArgValue = SpillSlot;
1127 break;
1128 }
Alex Bradburya3376752017-11-08 13:41:21 +00001129 default:
1130 llvm_unreachable("Unknown loc info!");
1131 }
1132
Alex Bradburydc31c612017-12-11 12:49:02 +00001133 // Use local copy if it is a byval arg.
1134 if (Flags.isByVal())
1135 ArgValue = ByValArgs[j++];
1136
Alex Bradburya3376752017-11-08 13:41:21 +00001137 if (VA.isRegLoc()) {
1138 // Queue up the argument copies and emit them at the end.
1139 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1140 } else {
1141 assert(VA.isMemLoc() && "Argument not register or memory");
Alex Bradburydc31c612017-12-11 12:49:02 +00001142
1143 // Work out the address of the stack slot.
1144 if (!StackPtr.getNode())
1145 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1146 SDValue Address =
1147 DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
1148 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
1149
1150 // Emit the store.
1151 MemOpChains.push_back(
1152 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Alex Bradburya3376752017-11-08 13:41:21 +00001153 }
1154 }
1155
Alex Bradburydc31c612017-12-11 12:49:02 +00001156 // Join the stores, which are independent of one another.
1157 if (!MemOpChains.empty())
1158 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
1159
Alex Bradburya3376752017-11-08 13:41:21 +00001160 SDValue Glue;
1161
1162 // Build a sequence of copy-to-reg nodes, chained and glued together.
1163 for (auto &Reg : RegsToPass) {
1164 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
1165 Glue = Chain.getValue(1);
1166 }
1167
1168 if (isa<GlobalAddressSDNode>(Callee)) {
1169 Callee = lowerGlobalAddress(Callee, DAG);
1170 } else if (isa<ExternalSymbolSDNode>(Callee)) {
Alex Bradburyffc435e2017-11-21 08:11:03 +00001171 Callee = lowerExternalSymbol(Callee, DAG);
Alex Bradburya3376752017-11-08 13:41:21 +00001172 }
1173
1174 // The first call operand is the chain and the second is the target address.
1175 SmallVector<SDValue, 8> Ops;
1176 Ops.push_back(Chain);
1177 Ops.push_back(Callee);
1178
1179 // Add argument registers to the end of the list so that they are
1180 // known live into the call.
1181 for (auto &Reg : RegsToPass)
1182 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
1183
1184 // Add a register mask operand representing the call-preserved registers.
1185 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1186 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
1187 assert(Mask && "Missing call preserved mask for calling convention");
1188 Ops.push_back(DAG.getRegisterMask(Mask));
1189
1190 // Glue the call to the argument copies, if any.
1191 if (Glue.getNode())
1192 Ops.push_back(Glue);
1193
1194 // Emit the call.
1195 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1196 Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
1197 Glue = Chain.getValue(1);
1198
1199 // Mark the end of the call, which is glued to the call itself.
1200 Chain = DAG.getCALLSEQ_END(Chain,
1201 DAG.getConstant(NumBytes, DL, PtrVT, true),
1202 DAG.getConstant(0, DL, PtrVT, true),
1203 Glue, DL);
1204 Glue = Chain.getValue(1);
1205
1206 // Assign locations to each value returned by this call.
1207 SmallVector<CCValAssign, 16> RVLocs;
1208 CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +00001209 analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
Alex Bradburya3376752017-11-08 13:41:21 +00001210
1211 // Copy all of the result registers out of their specified physreg.
1212 for (auto &VA : RVLocs) {
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001213 // Copy the value out
1214 SDValue RetValue =
1215 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
1216 // Glue the RetValue to the end of the call sequence
Alex Bradburya3376752017-11-08 13:41:21 +00001217 Chain = RetValue.getValue(1);
1218 Glue = RetValue.getValue(2);
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001219 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1220 assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
1221 SDValue RetValue2 =
1222 DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
1223 Chain = RetValue2.getValue(1);
1224 Glue = RetValue2.getValue(2);
1225 RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
1226 RetValue2);
1227 }
Alex Bradburya3376752017-11-08 13:41:21 +00001228
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001229 switch (VA.getLocInfo()) {
1230 default:
1231 llvm_unreachable("Unknown loc info!");
1232 case CCValAssign::Full:
1233 break;
1234 case CCValAssign::BCvt:
1235 RetValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), RetValue);
1236 break;
1237 }
1238
Alex Bradburydc31c612017-12-11 12:49:02 +00001239 InVals.push_back(RetValue);
Alex Bradburya3376752017-11-08 13:41:21 +00001240 }
1241
1242 return Chain;
1243}
1244
Alex Bradburydc31c612017-12-11 12:49:02 +00001245bool RISCVTargetLowering::CanLowerReturn(
1246 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
1247 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
1248 SmallVector<CCValAssign, 16> RVLocs;
1249 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
1250 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1251 MVT VT = Outs[i].VT;
1252 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1253 if (CC_RISCV(MF.getDataLayout(), i, VT, VT, CCValAssign::Full, ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001254 CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr))
Alex Bradburydc31c612017-12-11 12:49:02 +00001255 return false;
1256 }
1257 return true;
1258}
1259
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001260static SDValue packIntoRegLoc(SelectionDAG &DAG, SDValue Val,
1261 const CCValAssign &VA, const SDLoc &DL) {
1262 EVT LocVT = VA.getLocVT();
1263
1264 switch (VA.getLocInfo()) {
1265 default:
1266 llvm_unreachable("Unexpected CCValAssign::LocInfo");
1267 case CCValAssign::Full:
1268 break;
1269 case CCValAssign::BCvt:
1270 Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
1271 break;
1272 }
1273 return Val;
1274}
1275
Alex Bradbury89718422017-10-19 21:37:38 +00001276SDValue
1277RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1278 bool IsVarArg,
1279 const SmallVectorImpl<ISD::OutputArg> &Outs,
1280 const SmallVectorImpl<SDValue> &OutVals,
1281 const SDLoc &DL, SelectionDAG &DAG) const {
Alex Bradbury89718422017-10-19 21:37:38 +00001282 // Stores the assignment of the return value to a location.
1283 SmallVector<CCValAssign, 16> RVLocs;
1284
1285 // Info about the registers and stack slot.
1286 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
1287 *DAG.getContext());
1288
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001289 analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
1290 nullptr);
Alex Bradbury89718422017-10-19 21:37:38 +00001291
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001292 SDValue Glue;
Alex Bradbury89718422017-10-19 21:37:38 +00001293 SmallVector<SDValue, 4> RetOps(1, Chain);
1294
1295 // Copy the result values into the output registers.
1296 for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
Alex Bradburydc31c612017-12-11 12:49:02 +00001297 SDValue Val = OutVals[i];
Alex Bradbury89718422017-10-19 21:37:38 +00001298 CCValAssign &VA = RVLocs[i];
1299 assert(VA.isRegLoc() && "Can only return in registers!");
1300
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001301 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1302 // Handle returning f64 on RV32D with a soft float ABI.
1303 assert(VA.isRegLoc() && "Expected return via registers");
1304 SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
1305 DAG.getVTList(MVT::i32, MVT::i32), Val);
1306 SDValue Lo = SplitF64.getValue(0);
1307 SDValue Hi = SplitF64.getValue(1);
1308 unsigned RegLo = VA.getLocReg();
1309 unsigned RegHi = RegLo + 1;
1310 Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
1311 Glue = Chain.getValue(1);
1312 RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
1313 Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
1314 Glue = Chain.getValue(1);
1315 RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
1316 } else {
1317 // Handle a 'normal' return.
1318 Val = packIntoRegLoc(DAG, Val, VA, DL);
1319 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001320
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001321 // Guarantee that all emitted copies are stuck together.
1322 Glue = Chain.getValue(1);
1323 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1324 }
Alex Bradbury89718422017-10-19 21:37:38 +00001325 }
1326
1327 RetOps[0] = Chain; // Update chain.
1328
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001329 // Add the glue node if we have it.
1330 if (Glue.getNode()) {
1331 RetOps.push_back(Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001332 }
1333
1334 return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
1335}
1336
1337const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
1338 switch ((RISCVISD::NodeType)Opcode) {
1339 case RISCVISD::FIRST_NUMBER:
1340 break;
1341 case RISCVISD::RET_FLAG:
1342 return "RISCVISD::RET_FLAG";
Alex Bradburya3376752017-11-08 13:41:21 +00001343 case RISCVISD::CALL:
1344 return "RISCVISD::CALL";
Alex Bradbury65385162017-11-21 07:51:32 +00001345 case RISCVISD::SELECT_CC:
1346 return "RISCVISD::SELECT_CC";
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001347 case RISCVISD::BuildPairF64:
1348 return "RISCVISD::BuildPairF64";
1349 case RISCVISD::SplitF64:
1350 return "RISCVISD::SplitF64";
Alex Bradbury89718422017-10-19 21:37:38 +00001351 }
1352 return nullptr;
1353}
Alex Bradbury9330e642018-01-10 20:05:09 +00001354
1355std::pair<unsigned, const TargetRegisterClass *>
1356RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1357 StringRef Constraint,
1358 MVT VT) const {
1359 // First, see if this is a constraint that directly corresponds to a
1360 // RISCV register class.
1361 if (Constraint.size() == 1) {
1362 switch (Constraint[0]) {
1363 case 'r':
1364 return std::make_pair(0U, &RISCV::GPRRegClass);
1365 default:
1366 break;
1367 }
1368 }
1369
1370 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1371}