blob: 535cee25a9810ed415d1677ab347bc0c66e3096d [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).
Shiva Chenb48b0272018-04-12 11:30:59 +0000145 unsigned FunctionAlignment = Subtarget.hasStdExtC() ? 1 : 2;
146 setMinFunctionAlignment(FunctionAlignment);
147 setPrefFunctionAlignment(FunctionAlignment);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000148
149 // Effectively disable jump table generation.
150 setMinimumJumpTableEntries(INT_MAX);
Alex Bradbury89718422017-10-19 21:37:38 +0000151}
152
Shiva Chenbbf4c5c2018-02-02 02:43:18 +0000153EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
154 EVT VT) const {
155 if (!VT.isVector())
156 return getPointerTy(DL);
157 return VT.changeVectorElementTypeToInteger();
158}
159
Alex Bradbury65385162017-11-21 07:51:32 +0000160// Changes the condition code and swaps operands if necessary, so the SetCC
161// operation matches one of the comparisons supported directly in the RISC-V
162// ISA.
163static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
164 switch (CC) {
165 default:
166 break;
167 case ISD::SETGT:
168 case ISD::SETLE:
169 case ISD::SETUGT:
170 case ISD::SETULE:
171 CC = ISD::getSetCCSwappedOperands(CC);
172 std::swap(LHS, RHS);
173 break;
174 }
175}
176
177// Return the RISC-V branch opcode that matches the given DAG integer
178// condition code. The CondCode must be one of those supported by the RISC-V
179// ISA (see normaliseSetCC).
180static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
181 switch (CC) {
182 default:
183 llvm_unreachable("Unsupported CondCode");
184 case ISD::SETEQ:
185 return RISCV::BEQ;
186 case ISD::SETNE:
187 return RISCV::BNE;
188 case ISD::SETLT:
189 return RISCV::BLT;
190 case ISD::SETGE:
191 return RISCV::BGE;
192 case ISD::SETULT:
193 return RISCV::BLTU;
194 case ISD::SETUGE:
195 return RISCV::BGEU;
196 }
197}
198
Alex Bradbury89718422017-10-19 21:37:38 +0000199SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
200 SelectionDAG &DAG) const {
201 switch (Op.getOpcode()) {
202 default:
203 report_fatal_error("unimplemented operand");
Alex Bradburyec8aa912017-11-08 13:24:21 +0000204 case ISD::GlobalAddress:
205 return lowerGlobalAddress(Op, DAG);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000206 case ISD::BlockAddress:
207 return lowerBlockAddress(Op, DAG);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000208 case ISD::ConstantPool:
209 return lowerConstantPool(Op, DAG);
Alex Bradbury65385162017-11-21 07:51:32 +0000210 case ISD::SELECT:
211 return lowerSELECT(Op, DAG);
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000212 case ISD::VASTART:
213 return lowerVASTART(Op, DAG);
Alex Bradbury70f137b2018-01-10 20:12:00 +0000214 case ISD::FRAMEADDR:
215 return LowerFRAMEADDR(Op, DAG);
216 case ISD::RETURNADDR:
217 return LowerRETURNADDR(Op, DAG);
Alex Bradburyec8aa912017-11-08 13:24:21 +0000218 }
219}
220
221SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
222 SelectionDAG &DAG) const {
223 SDLoc DL(Op);
224 EVT Ty = Op.getValueType();
225 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
226 const GlobalValue *GV = N->getGlobal();
227 int64_t Offset = N->getOffset();
228
Alex Bradburyffc435e2017-11-21 08:11:03 +0000229 if (isPositionIndependent() || Subtarget.is64Bit())
Alex Bradburyec8aa912017-11-08 13:24:21 +0000230 report_fatal_error("Unable to lowerGlobalAddress");
Alex Bradburyffc435e2017-11-21 08:11:03 +0000231
232 SDValue GAHi =
233 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_HI);
234 SDValue GALo =
235 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_LO);
236 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
237 SDValue MNLo =
238 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
239 return MNLo;
240}
241
242SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
243 SelectionDAG &DAG) const {
244 SDLoc DL(Op);
245 EVT Ty = Op.getValueType();
246 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
247 const BlockAddress *BA = N->getBlockAddress();
248 int64_t Offset = N->getOffset();
249
250 if (isPositionIndependent() || Subtarget.is64Bit())
251 report_fatal_error("Unable to lowerBlockAddress");
252
253 SDValue BAHi = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_HI);
254 SDValue BALo = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_LO);
255 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, BAHi), 0);
256 SDValue MNLo =
257 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, BALo), 0);
258 return MNLo;
259}
260
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000261SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
262 SelectionDAG &DAG) const {
263 SDLoc DL(Op);
264 EVT Ty = Op.getValueType();
265 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
266 const Constant *CPA = N->getConstVal();
267 int64_t Offset = N->getOffset();
268 unsigned Alignment = N->getAlignment();
269
270 if (!isPositionIndependent()) {
271 SDValue CPAHi =
272 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_HI);
273 SDValue CPALo =
274 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_LO);
275 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, CPAHi), 0);
276 SDValue MNLo =
277 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, CPALo), 0);
278 return MNLo;
279 } else {
280 report_fatal_error("Unable to lowerConstantPool");
281 }
282}
283
Alex Bradburyffc435e2017-11-21 08:11:03 +0000284SDValue RISCVTargetLowering::lowerExternalSymbol(SDValue Op,
285 SelectionDAG &DAG) const {
286 SDLoc DL(Op);
287 EVT Ty = Op.getValueType();
288 ExternalSymbolSDNode *N = cast<ExternalSymbolSDNode>(Op);
289 const char *Sym = N->getSymbol();
290
291 // TODO: should also handle gp-relative loads.
292
293 if (isPositionIndependent() || Subtarget.is64Bit())
294 report_fatal_error("Unable to lowerExternalSymbol");
295
296 SDValue GAHi = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_HI);
297 SDValue GALo = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_LO);
298 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
299 SDValue MNLo =
300 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
301 return MNLo;
Alex Bradbury89718422017-10-19 21:37:38 +0000302}
303
Alex Bradbury65385162017-11-21 07:51:32 +0000304SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
305 SDValue CondV = Op.getOperand(0);
306 SDValue TrueV = Op.getOperand(1);
307 SDValue FalseV = Op.getOperand(2);
308 SDLoc DL(Op);
309 MVT XLenVT = Subtarget.getXLenVT();
310
311 // If the result type is XLenVT and CondV is the output of a SETCC node
312 // which also operated on XLenVT inputs, then merge the SETCC node into the
313 // lowered RISCVISD::SELECT_CC to take advantage of the integer
314 // compare+branch instructions. i.e.:
315 // (select (setcc lhs, rhs, cc), truev, falsev)
316 // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
317 if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
318 CondV.getOperand(0).getSimpleValueType() == XLenVT) {
319 SDValue LHS = CondV.getOperand(0);
320 SDValue RHS = CondV.getOperand(1);
321 auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
322 ISD::CondCode CCVal = CC->get();
323
324 normaliseSetCC(LHS, RHS, CCVal);
325
326 SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
327 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
328 SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
329 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
330 }
331
332 // Otherwise:
333 // (select condv, truev, falsev)
334 // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
335 SDValue Zero = DAG.getConstant(0, DL, XLenVT);
336 SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
337
338 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
339 SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
340
341 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
342}
343
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000344SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
345 MachineFunction &MF = DAG.getMachineFunction();
346 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
347
348 SDLoc DL(Op);
349 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
350 getPointerTy(MF.getDataLayout()));
351
352 // vastart just stores the address of the VarArgsFrameIndex slot into the
353 // memory location argument.
354 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
355 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
356 MachinePointerInfo(SV));
357}
358
Alex Bradbury70f137b2018-01-10 20:12:00 +0000359SDValue RISCVTargetLowering::LowerFRAMEADDR(SDValue Op,
360 SelectionDAG &DAG) const {
361 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
362 MachineFunction &MF = DAG.getMachineFunction();
363 MachineFrameInfo &MFI = MF.getFrameInfo();
364 MFI.setFrameAddressIsTaken(true);
365 unsigned FrameReg = RI.getFrameRegister(MF);
366 int XLenInBytes = Subtarget.getXLen() / 8;
367
368 EVT VT = Op.getValueType();
369 SDLoc DL(Op);
370 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
371 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
372 while (Depth--) {
373 int Offset = -(XLenInBytes * 2);
374 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
375 DAG.getIntPtrConstant(Offset, DL));
376 FrameAddr =
377 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
378 }
379 return FrameAddr;
380}
381
382SDValue RISCVTargetLowering::LowerRETURNADDR(SDValue Op,
383 SelectionDAG &DAG) const {
384 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
385 MachineFunction &MF = DAG.getMachineFunction();
386 MachineFrameInfo &MFI = MF.getFrameInfo();
387 MFI.setReturnAddressIsTaken(true);
388 MVT XLenVT = Subtarget.getXLenVT();
389 int XLenInBytes = Subtarget.getXLen() / 8;
390
391 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
392 return SDValue();
393
394 EVT VT = Op.getValueType();
395 SDLoc DL(Op);
396 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
397 if (Depth) {
398 int Off = -XLenInBytes;
399 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
400 SDValue Offset = DAG.getConstant(Off, DL, VT);
401 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
402 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
403 MachinePointerInfo());
404 }
405
406 // Return the value of the return address register, marking it an implicit
407 // live-in.
408 unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
409 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
410}
411
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000412static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
413 MachineBasicBlock *BB) {
414 assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
415
416 MachineFunction &MF = *BB->getParent();
417 DebugLoc DL = MI.getDebugLoc();
418 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
419 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
420 unsigned LoReg = MI.getOperand(0).getReg();
421 unsigned HiReg = MI.getOperand(1).getReg();
422 unsigned SrcReg = MI.getOperand(2).getReg();
423 const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
424 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
425
426 TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
427 RI);
428 MachineMemOperand *MMO =
429 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
430 MachineMemOperand::MOLoad, 8, 8);
431 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
432 .addFrameIndex(FI)
433 .addImm(0)
434 .addMemOperand(MMO);
435 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
436 .addFrameIndex(FI)
437 .addImm(4)
438 .addMemOperand(MMO);
439 MI.eraseFromParent(); // The pseudo instruction is gone now.
440 return BB;
441}
442
443static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
444 MachineBasicBlock *BB) {
445 assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
446 "Unexpected instruction");
447
448 MachineFunction &MF = *BB->getParent();
449 DebugLoc DL = MI.getDebugLoc();
450 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
451 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
452 unsigned DstReg = MI.getOperand(0).getReg();
453 unsigned LoReg = MI.getOperand(1).getReg();
454 unsigned HiReg = MI.getOperand(2).getReg();
455 const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
456 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
457
458 MachineMemOperand *MMO =
459 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
460 MachineMemOperand::MOStore, 8, 8);
461 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
462 .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
463 .addFrameIndex(FI)
464 .addImm(0)
465 .addMemOperand(MMO);
466 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
467 .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
468 .addFrameIndex(FI)
469 .addImm(4)
470 .addMemOperand(MMO);
471 TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
472 MI.eraseFromParent(); // The pseudo instruction is gone now.
473 return BB;
474}
475
Alex Bradbury65385162017-11-21 07:51:32 +0000476MachineBasicBlock *
477RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
478 MachineBasicBlock *BB) const {
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000479 switch (MI.getOpcode()) {
480 default:
481 llvm_unreachable("Unexpected instr type to insert");
482 case RISCV::Select_GPR_Using_CC_GPR:
483 case RISCV::Select_FPR32_Using_CC_GPR:
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000484 case RISCV::Select_FPR64_Using_CC_GPR:
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000485 break;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000486 case RISCV::BuildPairF64Pseudo:
487 return emitBuildPairF64Pseudo(MI, BB);
488 case RISCV::SplitF64Pseudo:
489 return emitSplitF64Pseudo(MI, BB);
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000490 }
Alex Bradbury65385162017-11-21 07:51:32 +0000491
492 // To "insert" a SELECT instruction, we actually have to insert the triangle
493 // control-flow pattern. The incoming instruction knows the destination vreg
494 // to set, the condition code register to branch on, the true/false values to
495 // select between, and the condcode to use to select the appropriate branch.
496 //
497 // We produce the following control flow:
498 // HeadMBB
499 // | \
500 // | IfFalseMBB
501 // | /
502 // TailMBB
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000503 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
Alex Bradbury65385162017-11-21 07:51:32 +0000504 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000505 DebugLoc DL = MI.getDebugLoc();
Alex Bradbury65385162017-11-21 07:51:32 +0000506 MachineFunction::iterator I = ++BB->getIterator();
507
508 MachineBasicBlock *HeadMBB = BB;
509 MachineFunction *F = BB->getParent();
510 MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
511 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
512
513 F->insert(I, IfFalseMBB);
514 F->insert(I, TailMBB);
515 // Move all remaining instructions to TailMBB.
516 TailMBB->splice(TailMBB->begin(), HeadMBB,
517 std::next(MachineBasicBlock::iterator(MI)), HeadMBB->end());
518 // Update machine-CFG edges by transferring all successors of the current
519 // block to the new block which will contain the Phi node for the select.
520 TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
521 // Set the successors for HeadMBB.
522 HeadMBB->addSuccessor(IfFalseMBB);
523 HeadMBB->addSuccessor(TailMBB);
524
525 // Insert appropriate branch.
526 unsigned LHS = MI.getOperand(1).getReg();
527 unsigned RHS = MI.getOperand(2).getReg();
528 auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
529 unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
530
531 BuildMI(HeadMBB, DL, TII.get(Opcode))
532 .addReg(LHS)
533 .addReg(RHS)
534 .addMBB(TailMBB);
535
536 // IfFalseMBB just falls through to TailMBB.
537 IfFalseMBB->addSuccessor(TailMBB);
538
539 // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
540 BuildMI(*TailMBB, TailMBB->begin(), DL, TII.get(RISCV::PHI),
541 MI.getOperand(0).getReg())
542 .addReg(MI.getOperand(4).getReg())
543 .addMBB(HeadMBB)
544 .addReg(MI.getOperand(5).getReg())
545 .addMBB(IfFalseMBB);
546
547 MI.eraseFromParent(); // The pseudo instruction is gone now.
548 return TailMBB;
549}
550
Alex Bradbury89718422017-10-19 21:37:38 +0000551// Calling Convention Implementation.
Alex Bradburydc31c612017-12-11 12:49:02 +0000552// The expectations for frontend ABI lowering vary from target to target.
553// Ideally, an LLVM frontend would be able to avoid worrying about many ABI
554// details, but this is a longer term goal. For now, we simply try to keep the
555// role of the frontend as simple and well-defined as possible. The rules can
556// be summarised as:
557// * Never split up large scalar arguments. We handle them here.
558// * If a hardfloat calling convention is being used, and the struct may be
559// passed in a pair of registers (fp+fp, int+fp), and both registers are
560// available, then pass as two separate arguments. If either the GPRs or FPRs
561// are exhausted, then pass according to the rule below.
562// * If a struct could never be passed in registers or directly in a stack
563// slot (as it is larger than 2*XLEN and the floating point rules don't
564// apply), then pass it using a pointer with the byval attribute.
565// * If a struct is less than 2*XLEN, then coerce to either a two-element
566// word-sized array or a 2*XLEN scalar (depending on alignment).
567// * The frontend can determine whether a struct is returned by reference or
568// not based on its size and fields. If it will be returned by reference, the
569// frontend must modify the prototype so a pointer with the sret annotation is
570// passed as the first argument. This is not necessary for large scalar
571// returns.
572// * Struct return values and varargs should be coerced to structs containing
573// register-size fields in the same situations they would be for fixed
574// arguments.
575
576static const MCPhysReg ArgGPRs[] = {
577 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
578 RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
579};
580
581// Pass a 2*XLEN argument that has been split into two XLEN values through
582// registers or the stack as necessary.
583static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
584 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
585 MVT ValVT2, MVT LocVT2,
586 ISD::ArgFlagsTy ArgFlags2) {
587 unsigned XLenInBytes = XLen / 8;
588 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
589 // At least one half can be passed via register.
590 State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
591 VA1.getLocVT(), CCValAssign::Full));
592 } else {
593 // Both halves must be passed on the stack, with proper alignment.
594 unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign());
595 State.addLoc(
596 CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
597 State.AllocateStack(XLenInBytes, StackAlign),
598 VA1.getLocVT(), CCValAssign::Full));
599 State.addLoc(CCValAssign::getMem(
600 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
601 CCValAssign::Full));
602 return false;
603 }
604
605 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
606 // The second half can also be passed via register.
607 State.addLoc(
608 CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
609 } else {
610 // The second half is passed via the stack, without additional alignment.
611 State.addLoc(CCValAssign::getMem(
612 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
613 CCValAssign::Full));
614 }
615
616 return false;
617}
618
619// Implements the RISC-V calling convention. Returns true upon failure.
620static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT,
621 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000622 CCState &State, bool IsFixed, bool IsRet, Type *OrigTy) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000623 unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
624 assert(XLen == 32 || XLen == 64);
625 MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000626 if (ValVT == MVT::f32) {
627 LocVT = MVT::i32;
628 LocInfo = CCValAssign::BCvt;
629 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000630
631 // Any return value split in to more than two values can't be returned
632 // directly.
633 if (IsRet && ValNo > 1)
634 return true;
635
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000636 // If this is a variadic argument, the RISC-V calling convention requires
637 // that it is assigned an 'even' or 'aligned' register if it has 8-byte
638 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
639 // be used regardless of whether the original argument was split during
640 // legalisation or not. The argument will not be passed by registers if the
641 // original type is larger than 2*XLEN, so the register alignment rule does
642 // not apply.
643 unsigned TwoXLenInBytes = (2 * XLen) / 8;
644 if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes &&
645 DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
646 unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
647 // Skip 'odd' register if necessary.
648 if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
649 State.AllocateReg(ArgGPRs);
650 }
651
Alex Bradburydc31c612017-12-11 12:49:02 +0000652 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
653 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
654 State.getPendingArgFlags();
655
656 assert(PendingLocs.size() == PendingArgFlags.size() &&
657 "PendingLocs and PendingArgFlags out of sync");
658
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000659 // Handle passing f64 on RV32D with a soft float ABI.
660 if (XLen == 32 && ValVT == MVT::f64) {
Mandeep Singh Grang88a8b262018-04-16 18:56:10 +0000661 assert(!ArgFlags.isSplit() && PendingLocs.empty() &&
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000662 "Can't lower f64 if it is split");
663 // Depending on available argument GPRS, f64 may be passed in a pair of
664 // GPRs, split between a GPR and the stack, or passed completely on the
665 // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
666 // cases.
667 unsigned Reg = State.AllocateReg(ArgGPRs);
668 LocVT = MVT::i32;
669 if (!Reg) {
670 unsigned StackOffset = State.AllocateStack(8, 8);
671 State.addLoc(
672 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
673 return false;
674 }
675 if (!State.AllocateReg(ArgGPRs))
676 State.AllocateStack(4, 4);
677 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
678 return false;
679 }
680
Alex Bradburydc31c612017-12-11 12:49:02 +0000681 // Split arguments might be passed indirectly, so keep track of the pending
682 // values.
683 if (ArgFlags.isSplit() || !PendingLocs.empty()) {
684 LocVT = XLenVT;
685 LocInfo = CCValAssign::Indirect;
686 PendingLocs.push_back(
687 CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
688 PendingArgFlags.push_back(ArgFlags);
689 if (!ArgFlags.isSplitEnd()) {
690 return false;
691 }
692 }
693
694 // If the split argument only had two elements, it should be passed directly
695 // in registers or on the stack.
696 if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
697 assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
698 // Apply the normal calling convention rules to the first half of the
699 // split argument.
700 CCValAssign VA = PendingLocs[0];
701 ISD::ArgFlagsTy AF = PendingArgFlags[0];
702 PendingLocs.clear();
703 PendingArgFlags.clear();
704 return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
705 ArgFlags);
706 }
707
708 // Allocate to a register if possible, or else a stack slot.
709 unsigned Reg = State.AllocateReg(ArgGPRs);
710 unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8);
711
712 // If we reach this point and PendingLocs is non-empty, we must be at the
713 // end of a split argument that must be passed indirectly.
714 if (!PendingLocs.empty()) {
715 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
716 assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
717
718 for (auto &It : PendingLocs) {
719 if (Reg)
720 It.convertToReg(Reg);
721 else
722 It.convertToMem(StackOffset);
723 State.addLoc(It);
724 }
725 PendingLocs.clear();
726 PendingArgFlags.clear();
727 return false;
728 }
729
730 assert(LocVT == XLenVT && "Expected an XLenVT at this stage");
731
732 if (Reg) {
733 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
734 } else {
735 State.addLoc(
736 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
737 }
738 return false;
739}
740
741void RISCVTargetLowering::analyzeInputArgs(
742 MachineFunction &MF, CCState &CCInfo,
743 const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
744 unsigned NumArgs = Ins.size();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000745 FunctionType *FType = MF.getFunction().getFunctionType();
Alex Bradburydc31c612017-12-11 12:49:02 +0000746
747 for (unsigned i = 0; i != NumArgs; ++i) {
748 MVT ArgVT = Ins[i].VT;
749 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
750
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000751 Type *ArgTy = nullptr;
752 if (IsRet)
753 ArgTy = FType->getReturnType();
754 else if (Ins[i].isOrigArg())
755 ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
756
Alex Bradburydc31c612017-12-11 12:49:02 +0000757 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000758 ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000759 DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
760 << EVT(ArgVT).getEVTString() << '\n');
761 llvm_unreachable(nullptr);
762 }
763 }
764}
765
766void RISCVTargetLowering::analyzeOutputArgs(
767 MachineFunction &MF, CCState &CCInfo,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000768 const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
769 CallLoweringInfo *CLI) const {
Alex Bradburydc31c612017-12-11 12:49:02 +0000770 unsigned NumArgs = Outs.size();
771
772 for (unsigned i = 0; i != NumArgs; i++) {
773 MVT ArgVT = Outs[i].VT;
774 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000775 Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
Alex Bradburydc31c612017-12-11 12:49:02 +0000776
777 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000778 ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000779 DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
780 << EVT(ArgVT).getEVTString() << "\n");
781 llvm_unreachable(nullptr);
782 }
783 }
784}
785
786// The caller is responsible for loading the full value if the argument is
787// passed with CCValAssign::Indirect.
788static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
789 const CCValAssign &VA, const SDLoc &DL) {
790 MachineFunction &MF = DAG.getMachineFunction();
791 MachineRegisterInfo &RegInfo = MF.getRegInfo();
792 EVT LocVT = VA.getLocVT();
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000793 EVT ValVT = VA.getValVT();
Alex Bradburydc31c612017-12-11 12:49:02 +0000794 SDValue Val;
795
796 unsigned VReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
797 RegInfo.addLiveIn(VA.getLocReg(), VReg);
798 Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
799
800 switch (VA.getLocInfo()) {
801 default:
802 llvm_unreachable("Unexpected CCValAssign::LocInfo");
803 case CCValAssign::Full:
804 case CCValAssign::Indirect:
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000805 break;
806 case CCValAssign::BCvt:
807 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
808 break;
Alex Bradburydc31c612017-12-11 12:49:02 +0000809 }
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000810 return Val;
Alex Bradburydc31c612017-12-11 12:49:02 +0000811}
812
813// The caller is responsible for loading the full value if the argument is
814// passed with CCValAssign::Indirect.
815static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
816 const CCValAssign &VA, const SDLoc &DL) {
817 MachineFunction &MF = DAG.getMachineFunction();
818 MachineFrameInfo &MFI = MF.getFrameInfo();
819 EVT LocVT = VA.getLocVT();
820 EVT ValVT = VA.getValVT();
821 EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
822 int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
823 VA.getLocMemOffset(), /*Immutable=*/true);
824 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
825 SDValue Val;
826
827 ISD::LoadExtType ExtType;
828 switch (VA.getLocInfo()) {
829 default:
830 llvm_unreachable("Unexpected CCValAssign::LocInfo");
831 case CCValAssign::Full:
832 case CCValAssign::Indirect:
833 ExtType = ISD::NON_EXTLOAD;
834 break;
835 }
836 Val = DAG.getExtLoad(
837 ExtType, DL, LocVT, Chain, FIN,
838 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
839 return Val;
840}
Alex Bradbury89718422017-10-19 21:37:38 +0000841
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000842static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
843 const CCValAssign &VA, const SDLoc &DL) {
844 assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
845 "Unexpected VA");
846 MachineFunction &MF = DAG.getMachineFunction();
847 MachineFrameInfo &MFI = MF.getFrameInfo();
848 MachineRegisterInfo &RegInfo = MF.getRegInfo();
849
850 if (VA.isMemLoc()) {
851 // f64 is passed on the stack.
852 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
853 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
854 return DAG.getLoad(MVT::f64, DL, Chain, FIN,
855 MachinePointerInfo::getFixedStack(MF, FI));
856 }
857
858 assert(VA.isRegLoc() && "Expected register VA assignment");
859
860 unsigned LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
861 RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
862 SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
863 SDValue Hi;
864 if (VA.getLocReg() == RISCV::X17) {
865 // Second half of f64 is passed on the stack.
866 int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
867 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
868 Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
869 MachinePointerInfo::getFixedStack(MF, FI));
870 } else {
871 // Second half of f64 is passed in another GPR.
872 unsigned HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
873 RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
874 Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
875 }
876 return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
877}
878
Alex Bradbury89718422017-10-19 21:37:38 +0000879// Transform physical registers into virtual registers.
880SDValue RISCVTargetLowering::LowerFormalArguments(
881 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
882 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
883 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
884
885 switch (CallConv) {
886 default:
887 report_fatal_error("Unsupported calling convention");
888 case CallingConv::C:
Alex Bradburya3376752017-11-08 13:41:21 +0000889 case CallingConv::Fast:
Alex Bradbury89718422017-10-19 21:37:38 +0000890 break;
891 }
892
893 MachineFunction &MF = DAG.getMachineFunction();
Alex Bradburydc31c612017-12-11 12:49:02 +0000894 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000895 MVT XLenVT = Subtarget.getXLenVT();
896 unsigned XLenInBytes = Subtarget.getXLen() / 8;
897 // Used with vargs to acumulate store chains.
898 std::vector<SDValue> OutChains;
Alex Bradbury89718422017-10-19 21:37:38 +0000899
900 // Assign locations to all of the incoming arguments.
901 SmallVector<CCValAssign, 16> ArgLocs;
902 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +0000903 analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
Alex Bradbury89718422017-10-19 21:37:38 +0000904
Alex Bradburydc31c612017-12-11 12:49:02 +0000905 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
906 CCValAssign &VA = ArgLocs[i];
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000907 assert(VA.getLocVT() == XLenVT && "Unhandled argument type");
Alex Bradburydc31c612017-12-11 12:49:02 +0000908 SDValue ArgValue;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000909 // Passing f64 on RV32D with a soft float ABI must be handled as a special
910 // case.
911 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
912 ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
913 else if (VA.isRegLoc())
Alex Bradburydc31c612017-12-11 12:49:02 +0000914 ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL);
915 else
916 ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
Alex Bradbury89718422017-10-19 21:37:38 +0000917
Alex Bradburydc31c612017-12-11 12:49:02 +0000918 if (VA.getLocInfo() == CCValAssign::Indirect) {
919 // If the original argument was split and passed by reference (e.g. i128
920 // on RV32), we need to load all parts of it here (using the same
921 // address).
922 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
923 MachinePointerInfo()));
924 unsigned ArgIndex = Ins[i].OrigArgIndex;
925 assert(Ins[i].PartOffset == 0);
926 while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
927 CCValAssign &PartVA = ArgLocs[i + 1];
928 unsigned PartOffset = Ins[i + 1].PartOffset;
929 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
930 DAG.getIntPtrConstant(PartOffset, DL));
931 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
932 MachinePointerInfo()));
933 ++i;
934 }
935 continue;
Alex Bradbury89718422017-10-19 21:37:38 +0000936 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000937 InVals.push_back(ArgValue);
Alex Bradbury89718422017-10-19 21:37:38 +0000938 }
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000939
940 if (IsVarArg) {
941 ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
942 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
943 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
944 MachineFrameInfo &MFI = MF.getFrameInfo();
945 MachineRegisterInfo &RegInfo = MF.getRegInfo();
946 RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
947
948 // Offset of the first variable argument from stack pointer, and size of
949 // the vararg save area. For now, the varargs save area is either zero or
950 // large enough to hold a0-a7.
951 int VaArgOffset, VarArgsSaveSize;
952
953 // If all registers are allocated, then all varargs must be passed on the
954 // stack and we don't need to save any argregs.
955 if (ArgRegs.size() == Idx) {
956 VaArgOffset = CCInfo.getNextStackOffset();
957 VarArgsSaveSize = 0;
958 } else {
959 VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
960 VaArgOffset = -VarArgsSaveSize;
961 }
962
963 // Record the frame index of the first variable argument
964 // which is a value necessary to VASTART.
965 int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
966 RVFI->setVarArgsFrameIndex(FI);
967
968 // If saving an odd number of registers then create an extra stack slot to
969 // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
970 // offsets to even-numbered registered remain 2*XLEN-aligned.
971 if (Idx % 2) {
972 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes,
973 true);
974 VarArgsSaveSize += XLenInBytes;
975 }
976
977 // Copy the integer registers that may have been used for passing varargs
978 // to the vararg save area.
979 for (unsigned I = Idx; I < ArgRegs.size();
980 ++I, VaArgOffset += XLenInBytes) {
981 const unsigned Reg = RegInfo.createVirtualRegister(RC);
982 RegInfo.addLiveIn(ArgRegs[I], Reg);
983 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
984 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
985 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
986 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
987 MachinePointerInfo::getFixedStack(MF, FI));
988 cast<StoreSDNode>(Store.getNode())
989 ->getMemOperand()
990 ->setValue((Value *)nullptr);
991 OutChains.push_back(Store);
992 }
993 RVFI->setVarArgsSaveSize(VarArgsSaveSize);
994 }
995
996 // All stores are grouped in one node to allow the matching between
997 // the size of Ins and InVals. This only happens for vararg functions.
998 if (!OutChains.empty()) {
999 OutChains.push_back(Chain);
1000 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
1001 }
1002
Alex Bradbury89718422017-10-19 21:37:38 +00001003 return Chain;
1004}
1005
Alex Bradburya3376752017-11-08 13:41:21 +00001006// Lower a call to a callseq_start + CALL + callseq_end chain, and add input
1007// and output parameter nodes.
1008SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1009 SmallVectorImpl<SDValue> &InVals) const {
1010 SelectionDAG &DAG = CLI.DAG;
1011 SDLoc &DL = CLI.DL;
1012 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1013 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1014 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1015 SDValue Chain = CLI.Chain;
1016 SDValue Callee = CLI.Callee;
1017 CLI.IsTailCall = false;
1018 CallingConv::ID CallConv = CLI.CallConv;
1019 bool IsVarArg = CLI.IsVarArg;
1020 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburydc31c612017-12-11 12:49:02 +00001021 MVT XLenVT = Subtarget.getXLenVT();
Alex Bradburya3376752017-11-08 13:41:21 +00001022
Alex Bradburya3376752017-11-08 13:41:21 +00001023 MachineFunction &MF = DAG.getMachineFunction();
1024
1025 // Analyze the operands of the call, assigning locations to each operand.
1026 SmallVector<CCValAssign, 16> ArgLocs;
1027 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001028 analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
Alex Bradburya3376752017-11-08 13:41:21 +00001029
1030 // Get a count of how many bytes are to be pushed on the stack.
1031 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1032
Alex Bradburydc31c612017-12-11 12:49:02 +00001033 // Create local copies for byval args
1034 SmallVector<SDValue, 8> ByValArgs;
1035 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1036 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1037 if (!Flags.isByVal())
Alex Bradburya3376752017-11-08 13:41:21 +00001038 continue;
Alex Bradburydc31c612017-12-11 12:49:02 +00001039
1040 SDValue Arg = OutVals[i];
1041 unsigned Size = Flags.getByValSize();
1042 unsigned Align = Flags.getByValAlign();
1043
1044 int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false);
1045 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1046 SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
1047
1048 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
1049 /*IsVolatile=*/false,
1050 /*AlwaysInline=*/false,
1051 /*isTailCall=*/false, MachinePointerInfo(),
1052 MachinePointerInfo());
1053 ByValArgs.push_back(FIPtr);
Alex Bradburya3376752017-11-08 13:41:21 +00001054 }
1055
1056 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
1057
1058 // Copy argument values to their designated locations.
1059 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Alex Bradburydc31c612017-12-11 12:49:02 +00001060 SmallVector<SDValue, 8> MemOpChains;
Alex Bradburya3376752017-11-08 13:41:21 +00001061 SDValue StackPtr;
Alex Bradburydc31c612017-12-11 12:49:02 +00001062 for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
1063 CCValAssign &VA = ArgLocs[i];
1064 SDValue ArgValue = OutVals[i];
1065 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Alex Bradburya3376752017-11-08 13:41:21 +00001066
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001067 // Handle passing f64 on RV32D with a soft float ABI as a special case.
1068 bool IsF64OnRV32DSoftABI =
1069 VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
1070 if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
1071 SDValue SplitF64 = DAG.getNode(
1072 RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
1073 SDValue Lo = SplitF64.getValue(0);
1074 SDValue Hi = SplitF64.getValue(1);
1075
1076 unsigned RegLo = VA.getLocReg();
1077 RegsToPass.push_back(std::make_pair(RegLo, Lo));
1078
1079 if (RegLo == RISCV::X17) {
1080 // Second half of f64 is passed on the stack.
1081 // Work out the address of the stack slot.
1082 if (!StackPtr.getNode())
1083 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1084 // Emit the store.
1085 MemOpChains.push_back(
1086 DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
1087 } else {
1088 // Second half of f64 is passed in another GPR.
1089 unsigned RegHigh = RegLo + 1;
1090 RegsToPass.push_back(std::make_pair(RegHigh, Hi));
1091 }
1092 continue;
1093 }
1094
1095 // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
1096 // as any other MemLoc.
1097
Alex Bradburya3376752017-11-08 13:41:21 +00001098 // Promote the value if needed.
Alex Bradburydc31c612017-12-11 12:49:02 +00001099 // For now, only handle fully promoted and indirect arguments.
Alex Bradburya3376752017-11-08 13:41:21 +00001100 switch (VA.getLocInfo()) {
1101 case CCValAssign::Full:
1102 break;
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001103 case CCValAssign::BCvt:
1104 ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), ArgValue);
1105 break;
Alex Bradburydc31c612017-12-11 12:49:02 +00001106 case CCValAssign::Indirect: {
1107 // Store the argument in a stack slot and pass its address.
1108 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
1109 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1110 MemOpChains.push_back(
1111 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1112 MachinePointerInfo::getFixedStack(MF, FI)));
1113 // If the original argument was split (e.g. i128), we need
1114 // to store all parts of it here (and pass just one address).
1115 unsigned ArgIndex = Outs[i].OrigArgIndex;
1116 assert(Outs[i].PartOffset == 0);
1117 while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
1118 SDValue PartValue = OutVals[i + 1];
1119 unsigned PartOffset = Outs[i + 1].PartOffset;
1120 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1121 DAG.getIntPtrConstant(PartOffset, DL));
1122 MemOpChains.push_back(
1123 DAG.getStore(Chain, DL, PartValue, Address,
1124 MachinePointerInfo::getFixedStack(MF, FI)));
1125 ++i;
1126 }
1127 ArgValue = SpillSlot;
1128 break;
1129 }
Alex Bradburya3376752017-11-08 13:41:21 +00001130 default:
1131 llvm_unreachable("Unknown loc info!");
1132 }
1133
Alex Bradburydc31c612017-12-11 12:49:02 +00001134 // Use local copy if it is a byval arg.
1135 if (Flags.isByVal())
1136 ArgValue = ByValArgs[j++];
1137
Alex Bradburya3376752017-11-08 13:41:21 +00001138 if (VA.isRegLoc()) {
1139 // Queue up the argument copies and emit them at the end.
1140 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1141 } else {
1142 assert(VA.isMemLoc() && "Argument not register or memory");
Alex Bradburydc31c612017-12-11 12:49:02 +00001143
1144 // Work out the address of the stack slot.
1145 if (!StackPtr.getNode())
1146 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1147 SDValue Address =
1148 DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
1149 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
1150
1151 // Emit the store.
1152 MemOpChains.push_back(
1153 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Alex Bradburya3376752017-11-08 13:41:21 +00001154 }
1155 }
1156
Alex Bradburydc31c612017-12-11 12:49:02 +00001157 // Join the stores, which are independent of one another.
1158 if (!MemOpChains.empty())
1159 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
1160
Alex Bradburya3376752017-11-08 13:41:21 +00001161 SDValue Glue;
1162
1163 // Build a sequence of copy-to-reg nodes, chained and glued together.
1164 for (auto &Reg : RegsToPass) {
1165 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
1166 Glue = Chain.getValue(1);
1167 }
1168
Shiva Chend58bd8d2018-04-25 14:19:12 +00001169 // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
1170 // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
1171 // split it and then direct call can be matched by PseudoCALL.
1172 if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
1173 Callee = DAG.getTargetGlobalAddress(S->getGlobal(), DL, PtrVT, 0, 0);
1174 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1175 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, 0);
Alex Bradburya3376752017-11-08 13:41:21 +00001176 }
1177
1178 // The first call operand is the chain and the second is the target address.
1179 SmallVector<SDValue, 8> Ops;
1180 Ops.push_back(Chain);
1181 Ops.push_back(Callee);
1182
1183 // Add argument registers to the end of the list so that they are
1184 // known live into the call.
1185 for (auto &Reg : RegsToPass)
1186 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
1187
1188 // Add a register mask operand representing the call-preserved registers.
1189 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1190 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
1191 assert(Mask && "Missing call preserved mask for calling convention");
1192 Ops.push_back(DAG.getRegisterMask(Mask));
1193
1194 // Glue the call to the argument copies, if any.
1195 if (Glue.getNode())
1196 Ops.push_back(Glue);
1197
1198 // Emit the call.
1199 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1200 Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
1201 Glue = Chain.getValue(1);
1202
1203 // Mark the end of the call, which is glued to the call itself.
1204 Chain = DAG.getCALLSEQ_END(Chain,
1205 DAG.getConstant(NumBytes, DL, PtrVT, true),
1206 DAG.getConstant(0, DL, PtrVT, true),
1207 Glue, DL);
1208 Glue = Chain.getValue(1);
1209
1210 // Assign locations to each value returned by this call.
1211 SmallVector<CCValAssign, 16> RVLocs;
1212 CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +00001213 analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
Alex Bradburya3376752017-11-08 13:41:21 +00001214
1215 // Copy all of the result registers out of their specified physreg.
1216 for (auto &VA : RVLocs) {
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001217 // Copy the value out
1218 SDValue RetValue =
1219 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
1220 // Glue the RetValue to the end of the call sequence
Alex Bradburya3376752017-11-08 13:41:21 +00001221 Chain = RetValue.getValue(1);
1222 Glue = RetValue.getValue(2);
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001223 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1224 assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
1225 SDValue RetValue2 =
1226 DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
1227 Chain = RetValue2.getValue(1);
1228 Glue = RetValue2.getValue(2);
1229 RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
1230 RetValue2);
1231 }
Alex Bradburya3376752017-11-08 13:41:21 +00001232
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001233 switch (VA.getLocInfo()) {
1234 default:
1235 llvm_unreachable("Unknown loc info!");
1236 case CCValAssign::Full:
1237 break;
1238 case CCValAssign::BCvt:
1239 RetValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), RetValue);
1240 break;
1241 }
1242
Alex Bradburydc31c612017-12-11 12:49:02 +00001243 InVals.push_back(RetValue);
Alex Bradburya3376752017-11-08 13:41:21 +00001244 }
1245
1246 return Chain;
1247}
1248
Alex Bradburydc31c612017-12-11 12:49:02 +00001249bool RISCVTargetLowering::CanLowerReturn(
1250 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
1251 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
1252 SmallVector<CCValAssign, 16> RVLocs;
1253 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
1254 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1255 MVT VT = Outs[i].VT;
1256 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1257 if (CC_RISCV(MF.getDataLayout(), i, VT, VT, CCValAssign::Full, ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001258 CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr))
Alex Bradburydc31c612017-12-11 12:49:02 +00001259 return false;
1260 }
1261 return true;
1262}
1263
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001264static SDValue packIntoRegLoc(SelectionDAG &DAG, SDValue Val,
1265 const CCValAssign &VA, const SDLoc &DL) {
1266 EVT LocVT = VA.getLocVT();
1267
1268 switch (VA.getLocInfo()) {
1269 default:
1270 llvm_unreachable("Unexpected CCValAssign::LocInfo");
1271 case CCValAssign::Full:
1272 break;
1273 case CCValAssign::BCvt:
1274 Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
1275 break;
1276 }
1277 return Val;
1278}
1279
Alex Bradbury89718422017-10-19 21:37:38 +00001280SDValue
1281RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1282 bool IsVarArg,
1283 const SmallVectorImpl<ISD::OutputArg> &Outs,
1284 const SmallVectorImpl<SDValue> &OutVals,
1285 const SDLoc &DL, SelectionDAG &DAG) const {
Alex Bradbury89718422017-10-19 21:37:38 +00001286 // Stores the assignment of the return value to a location.
1287 SmallVector<CCValAssign, 16> RVLocs;
1288
1289 // Info about the registers and stack slot.
1290 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
1291 *DAG.getContext());
1292
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001293 analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
1294 nullptr);
Alex Bradbury89718422017-10-19 21:37:38 +00001295
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001296 SDValue Glue;
Alex Bradbury89718422017-10-19 21:37:38 +00001297 SmallVector<SDValue, 4> RetOps(1, Chain);
1298
1299 // Copy the result values into the output registers.
1300 for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
Alex Bradburydc31c612017-12-11 12:49:02 +00001301 SDValue Val = OutVals[i];
Alex Bradbury89718422017-10-19 21:37:38 +00001302 CCValAssign &VA = RVLocs[i];
1303 assert(VA.isRegLoc() && "Can only return in registers!");
1304
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001305 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1306 // Handle returning f64 on RV32D with a soft float ABI.
1307 assert(VA.isRegLoc() && "Expected return via registers");
1308 SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
1309 DAG.getVTList(MVT::i32, MVT::i32), Val);
1310 SDValue Lo = SplitF64.getValue(0);
1311 SDValue Hi = SplitF64.getValue(1);
1312 unsigned RegLo = VA.getLocReg();
1313 unsigned RegHi = RegLo + 1;
1314 Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
1315 Glue = Chain.getValue(1);
1316 RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
1317 Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
1318 Glue = Chain.getValue(1);
1319 RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
1320 } else {
1321 // Handle a 'normal' return.
1322 Val = packIntoRegLoc(DAG, Val, VA, DL);
1323 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001324
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001325 // Guarantee that all emitted copies are stuck together.
1326 Glue = Chain.getValue(1);
1327 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1328 }
Alex Bradbury89718422017-10-19 21:37:38 +00001329 }
1330
1331 RetOps[0] = Chain; // Update chain.
1332
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001333 // Add the glue node if we have it.
1334 if (Glue.getNode()) {
1335 RetOps.push_back(Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001336 }
1337
1338 return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
1339}
1340
1341const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
1342 switch ((RISCVISD::NodeType)Opcode) {
1343 case RISCVISD::FIRST_NUMBER:
1344 break;
1345 case RISCVISD::RET_FLAG:
1346 return "RISCVISD::RET_FLAG";
Alex Bradburya3376752017-11-08 13:41:21 +00001347 case RISCVISD::CALL:
1348 return "RISCVISD::CALL";
Alex Bradbury65385162017-11-21 07:51:32 +00001349 case RISCVISD::SELECT_CC:
1350 return "RISCVISD::SELECT_CC";
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001351 case RISCVISD::BuildPairF64:
1352 return "RISCVISD::BuildPairF64";
1353 case RISCVISD::SplitF64:
1354 return "RISCVISD::SplitF64";
Alex Bradbury89718422017-10-19 21:37:38 +00001355 }
1356 return nullptr;
1357}
Alex Bradbury9330e642018-01-10 20:05:09 +00001358
1359std::pair<unsigned, const TargetRegisterClass *>
1360RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1361 StringRef Constraint,
1362 MVT VT) const {
1363 // First, see if this is a constraint that directly corresponds to a
1364 // RISCV register class.
1365 if (Constraint.size() == 1) {
1366 switch (Constraint[0]) {
1367 case 'r':
1368 return std::make_pair(0U, &RISCV::GPRRegClass);
1369 default:
1370 break;
1371 }
1372 }
1373
1374 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1375}