blob: b00f6605bdc6997b3f9dc7a34bb8ba0547089691 [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 Bradbury09926292018-04-26 12:13:48 +0000160bool RISCVTargetLowering::isLegalAddressingMode(const DataLayout &DL,
161 const AddrMode &AM, Type *Ty,
162 unsigned AS,
163 Instruction *I) const {
164 // No global is ever allowed as a base.
165 if (AM.BaseGV)
166 return false;
167
168 // Require a 12-bit signed offset.
169 if (!isInt<12>(AM.BaseOffs))
170 return false;
171
172 switch (AM.Scale) {
173 case 0: // "r+i" or just "i", depending on HasBaseReg.
174 break;
175 case 1:
176 if (!AM.HasBaseReg) // allow "r+i".
177 break;
178 return false; // disallow "r+r" or "r+r+i".
179 default:
180 return false;
181 }
182
183 return true;
184}
185
Alex Bradbury65385162017-11-21 07:51:32 +0000186// Changes the condition code and swaps operands if necessary, so the SetCC
187// operation matches one of the comparisons supported directly in the RISC-V
188// ISA.
189static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
190 switch (CC) {
191 default:
192 break;
193 case ISD::SETGT:
194 case ISD::SETLE:
195 case ISD::SETUGT:
196 case ISD::SETULE:
197 CC = ISD::getSetCCSwappedOperands(CC);
198 std::swap(LHS, RHS);
199 break;
200 }
201}
202
203// Return the RISC-V branch opcode that matches the given DAG integer
204// condition code. The CondCode must be one of those supported by the RISC-V
205// ISA (see normaliseSetCC).
206static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
207 switch (CC) {
208 default:
209 llvm_unreachable("Unsupported CondCode");
210 case ISD::SETEQ:
211 return RISCV::BEQ;
212 case ISD::SETNE:
213 return RISCV::BNE;
214 case ISD::SETLT:
215 return RISCV::BLT;
216 case ISD::SETGE:
217 return RISCV::BGE;
218 case ISD::SETULT:
219 return RISCV::BLTU;
220 case ISD::SETUGE:
221 return RISCV::BGEU;
222 }
223}
224
Alex Bradbury89718422017-10-19 21:37:38 +0000225SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
226 SelectionDAG &DAG) const {
227 switch (Op.getOpcode()) {
228 default:
229 report_fatal_error("unimplemented operand");
Alex Bradburyec8aa912017-11-08 13:24:21 +0000230 case ISD::GlobalAddress:
231 return lowerGlobalAddress(Op, DAG);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000232 case ISD::BlockAddress:
233 return lowerBlockAddress(Op, DAG);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000234 case ISD::ConstantPool:
235 return lowerConstantPool(Op, DAG);
Alex Bradbury65385162017-11-21 07:51:32 +0000236 case ISD::SELECT:
237 return lowerSELECT(Op, DAG);
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000238 case ISD::VASTART:
239 return lowerVASTART(Op, DAG);
Alex Bradbury70f137b2018-01-10 20:12:00 +0000240 case ISD::FRAMEADDR:
241 return LowerFRAMEADDR(Op, DAG);
242 case ISD::RETURNADDR:
243 return LowerRETURNADDR(Op, DAG);
Alex Bradburyec8aa912017-11-08 13:24:21 +0000244 }
245}
246
247SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
248 SelectionDAG &DAG) const {
249 SDLoc DL(Op);
250 EVT Ty = Op.getValueType();
251 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
252 const GlobalValue *GV = N->getGlobal();
253 int64_t Offset = N->getOffset();
254
Alex Bradburyffc435e2017-11-21 08:11:03 +0000255 if (isPositionIndependent() || Subtarget.is64Bit())
Alex Bradburyec8aa912017-11-08 13:24:21 +0000256 report_fatal_error("Unable to lowerGlobalAddress");
Alex Bradburyffc435e2017-11-21 08:11:03 +0000257
258 SDValue GAHi =
259 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_HI);
260 SDValue GALo =
261 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_LO);
262 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
263 SDValue MNLo =
264 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
265 return MNLo;
266}
267
268SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
269 SelectionDAG &DAG) const {
270 SDLoc DL(Op);
271 EVT Ty = Op.getValueType();
272 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
273 const BlockAddress *BA = N->getBlockAddress();
274 int64_t Offset = N->getOffset();
275
276 if (isPositionIndependent() || Subtarget.is64Bit())
277 report_fatal_error("Unable to lowerBlockAddress");
278
279 SDValue BAHi = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_HI);
280 SDValue BALo = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_LO);
281 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, BAHi), 0);
282 SDValue MNLo =
283 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, BALo), 0);
284 return MNLo;
285}
286
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000287SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
288 SelectionDAG &DAG) const {
289 SDLoc DL(Op);
290 EVT Ty = Op.getValueType();
291 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
292 const Constant *CPA = N->getConstVal();
293 int64_t Offset = N->getOffset();
294 unsigned Alignment = N->getAlignment();
295
296 if (!isPositionIndependent()) {
297 SDValue CPAHi =
298 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_HI);
299 SDValue CPALo =
300 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_LO);
301 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, CPAHi), 0);
302 SDValue MNLo =
303 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, CPALo), 0);
304 return MNLo;
305 } else {
306 report_fatal_error("Unable to lowerConstantPool");
307 }
308}
309
Alex Bradburyffc435e2017-11-21 08:11:03 +0000310SDValue RISCVTargetLowering::lowerExternalSymbol(SDValue Op,
311 SelectionDAG &DAG) const {
312 SDLoc DL(Op);
313 EVT Ty = Op.getValueType();
314 ExternalSymbolSDNode *N = cast<ExternalSymbolSDNode>(Op);
315 const char *Sym = N->getSymbol();
316
317 // TODO: should also handle gp-relative loads.
318
319 if (isPositionIndependent() || Subtarget.is64Bit())
320 report_fatal_error("Unable to lowerExternalSymbol");
321
322 SDValue GAHi = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_HI);
323 SDValue GALo = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_LO);
324 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
325 SDValue MNLo =
326 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
327 return MNLo;
Alex Bradbury89718422017-10-19 21:37:38 +0000328}
329
Alex Bradbury65385162017-11-21 07:51:32 +0000330SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
331 SDValue CondV = Op.getOperand(0);
332 SDValue TrueV = Op.getOperand(1);
333 SDValue FalseV = Op.getOperand(2);
334 SDLoc DL(Op);
335 MVT XLenVT = Subtarget.getXLenVT();
336
337 // If the result type is XLenVT and CondV is the output of a SETCC node
338 // which also operated on XLenVT inputs, then merge the SETCC node into the
339 // lowered RISCVISD::SELECT_CC to take advantage of the integer
340 // compare+branch instructions. i.e.:
341 // (select (setcc lhs, rhs, cc), truev, falsev)
342 // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
343 if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
344 CondV.getOperand(0).getSimpleValueType() == XLenVT) {
345 SDValue LHS = CondV.getOperand(0);
346 SDValue RHS = CondV.getOperand(1);
347 auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
348 ISD::CondCode CCVal = CC->get();
349
350 normaliseSetCC(LHS, RHS, CCVal);
351
352 SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
353 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
354 SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
355 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
356 }
357
358 // Otherwise:
359 // (select condv, truev, falsev)
360 // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
361 SDValue Zero = DAG.getConstant(0, DL, XLenVT);
362 SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
363
364 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
365 SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
366
367 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
368}
369
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000370SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
371 MachineFunction &MF = DAG.getMachineFunction();
372 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
373
374 SDLoc DL(Op);
375 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
376 getPointerTy(MF.getDataLayout()));
377
378 // vastart just stores the address of the VarArgsFrameIndex slot into the
379 // memory location argument.
380 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
381 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
382 MachinePointerInfo(SV));
383}
384
Alex Bradbury70f137b2018-01-10 20:12:00 +0000385SDValue RISCVTargetLowering::LowerFRAMEADDR(SDValue Op,
386 SelectionDAG &DAG) const {
387 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
388 MachineFunction &MF = DAG.getMachineFunction();
389 MachineFrameInfo &MFI = MF.getFrameInfo();
390 MFI.setFrameAddressIsTaken(true);
391 unsigned FrameReg = RI.getFrameRegister(MF);
392 int XLenInBytes = Subtarget.getXLen() / 8;
393
394 EVT VT = Op.getValueType();
395 SDLoc DL(Op);
396 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
397 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
398 while (Depth--) {
399 int Offset = -(XLenInBytes * 2);
400 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
401 DAG.getIntPtrConstant(Offset, DL));
402 FrameAddr =
403 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
404 }
405 return FrameAddr;
406}
407
408SDValue RISCVTargetLowering::LowerRETURNADDR(SDValue Op,
409 SelectionDAG &DAG) const {
410 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
411 MachineFunction &MF = DAG.getMachineFunction();
412 MachineFrameInfo &MFI = MF.getFrameInfo();
413 MFI.setReturnAddressIsTaken(true);
414 MVT XLenVT = Subtarget.getXLenVT();
415 int XLenInBytes = Subtarget.getXLen() / 8;
416
417 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
418 return SDValue();
419
420 EVT VT = Op.getValueType();
421 SDLoc DL(Op);
422 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
423 if (Depth) {
424 int Off = -XLenInBytes;
425 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
426 SDValue Offset = DAG.getConstant(Off, DL, VT);
427 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
428 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
429 MachinePointerInfo());
430 }
431
432 // Return the value of the return address register, marking it an implicit
433 // live-in.
434 unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
435 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
436}
437
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000438static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
439 MachineBasicBlock *BB) {
440 assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
441
442 MachineFunction &MF = *BB->getParent();
443 DebugLoc DL = MI.getDebugLoc();
444 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
445 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
446 unsigned LoReg = MI.getOperand(0).getReg();
447 unsigned HiReg = MI.getOperand(1).getReg();
448 unsigned SrcReg = MI.getOperand(2).getReg();
449 const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
450 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
451
452 TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
453 RI);
454 MachineMemOperand *MMO =
455 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
456 MachineMemOperand::MOLoad, 8, 8);
457 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
458 .addFrameIndex(FI)
459 .addImm(0)
460 .addMemOperand(MMO);
461 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
462 .addFrameIndex(FI)
463 .addImm(4)
464 .addMemOperand(MMO);
465 MI.eraseFromParent(); // The pseudo instruction is gone now.
466 return BB;
467}
468
469static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
470 MachineBasicBlock *BB) {
471 assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
472 "Unexpected instruction");
473
474 MachineFunction &MF = *BB->getParent();
475 DebugLoc DL = MI.getDebugLoc();
476 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
477 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
478 unsigned DstReg = MI.getOperand(0).getReg();
479 unsigned LoReg = MI.getOperand(1).getReg();
480 unsigned HiReg = MI.getOperand(2).getReg();
481 const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
482 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
483
484 MachineMemOperand *MMO =
485 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
486 MachineMemOperand::MOStore, 8, 8);
487 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
488 .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
489 .addFrameIndex(FI)
490 .addImm(0)
491 .addMemOperand(MMO);
492 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
493 .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
494 .addFrameIndex(FI)
495 .addImm(4)
496 .addMemOperand(MMO);
497 TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
498 MI.eraseFromParent(); // The pseudo instruction is gone now.
499 return BB;
500}
501
Alex Bradbury65385162017-11-21 07:51:32 +0000502MachineBasicBlock *
503RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
504 MachineBasicBlock *BB) const {
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000505 switch (MI.getOpcode()) {
506 default:
507 llvm_unreachable("Unexpected instr type to insert");
508 case RISCV::Select_GPR_Using_CC_GPR:
509 case RISCV::Select_FPR32_Using_CC_GPR:
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000510 case RISCV::Select_FPR64_Using_CC_GPR:
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000511 break;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000512 case RISCV::BuildPairF64Pseudo:
513 return emitBuildPairF64Pseudo(MI, BB);
514 case RISCV::SplitF64Pseudo:
515 return emitSplitF64Pseudo(MI, BB);
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000516 }
Alex Bradbury65385162017-11-21 07:51:32 +0000517
518 // To "insert" a SELECT instruction, we actually have to insert the triangle
519 // control-flow pattern. The incoming instruction knows the destination vreg
520 // to set, the condition code register to branch on, the true/false values to
521 // select between, and the condcode to use to select the appropriate branch.
522 //
523 // We produce the following control flow:
524 // HeadMBB
525 // | \
526 // | IfFalseMBB
527 // | /
528 // TailMBB
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000529 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
Alex Bradbury65385162017-11-21 07:51:32 +0000530 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000531 DebugLoc DL = MI.getDebugLoc();
Alex Bradbury65385162017-11-21 07:51:32 +0000532 MachineFunction::iterator I = ++BB->getIterator();
533
534 MachineBasicBlock *HeadMBB = BB;
535 MachineFunction *F = BB->getParent();
536 MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
537 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
538
539 F->insert(I, IfFalseMBB);
540 F->insert(I, TailMBB);
541 // Move all remaining instructions to TailMBB.
542 TailMBB->splice(TailMBB->begin(), HeadMBB,
543 std::next(MachineBasicBlock::iterator(MI)), HeadMBB->end());
544 // Update machine-CFG edges by transferring all successors of the current
545 // block to the new block which will contain the Phi node for the select.
546 TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
547 // Set the successors for HeadMBB.
548 HeadMBB->addSuccessor(IfFalseMBB);
549 HeadMBB->addSuccessor(TailMBB);
550
551 // Insert appropriate branch.
552 unsigned LHS = MI.getOperand(1).getReg();
553 unsigned RHS = MI.getOperand(2).getReg();
554 auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
555 unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
556
557 BuildMI(HeadMBB, DL, TII.get(Opcode))
558 .addReg(LHS)
559 .addReg(RHS)
560 .addMBB(TailMBB);
561
562 // IfFalseMBB just falls through to TailMBB.
563 IfFalseMBB->addSuccessor(TailMBB);
564
565 // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
566 BuildMI(*TailMBB, TailMBB->begin(), DL, TII.get(RISCV::PHI),
567 MI.getOperand(0).getReg())
568 .addReg(MI.getOperand(4).getReg())
569 .addMBB(HeadMBB)
570 .addReg(MI.getOperand(5).getReg())
571 .addMBB(IfFalseMBB);
572
573 MI.eraseFromParent(); // The pseudo instruction is gone now.
574 return TailMBB;
575}
576
Alex Bradbury89718422017-10-19 21:37:38 +0000577// Calling Convention Implementation.
Alex Bradburydc31c612017-12-11 12:49:02 +0000578// The expectations for frontend ABI lowering vary from target to target.
579// Ideally, an LLVM frontend would be able to avoid worrying about many ABI
580// details, but this is a longer term goal. For now, we simply try to keep the
581// role of the frontend as simple and well-defined as possible. The rules can
582// be summarised as:
583// * Never split up large scalar arguments. We handle them here.
584// * If a hardfloat calling convention is being used, and the struct may be
585// passed in a pair of registers (fp+fp, int+fp), and both registers are
586// available, then pass as two separate arguments. If either the GPRs or FPRs
587// are exhausted, then pass according to the rule below.
588// * If a struct could never be passed in registers or directly in a stack
589// slot (as it is larger than 2*XLEN and the floating point rules don't
590// apply), then pass it using a pointer with the byval attribute.
591// * If a struct is less than 2*XLEN, then coerce to either a two-element
592// word-sized array or a 2*XLEN scalar (depending on alignment).
593// * The frontend can determine whether a struct is returned by reference or
594// not based on its size and fields. If it will be returned by reference, the
595// frontend must modify the prototype so a pointer with the sret annotation is
596// passed as the first argument. This is not necessary for large scalar
597// returns.
598// * Struct return values and varargs should be coerced to structs containing
599// register-size fields in the same situations they would be for fixed
600// arguments.
601
602static const MCPhysReg ArgGPRs[] = {
603 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
604 RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
605};
606
607// Pass a 2*XLEN argument that has been split into two XLEN values through
608// registers or the stack as necessary.
609static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
610 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
611 MVT ValVT2, MVT LocVT2,
612 ISD::ArgFlagsTy ArgFlags2) {
613 unsigned XLenInBytes = XLen / 8;
614 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
615 // At least one half can be passed via register.
616 State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
617 VA1.getLocVT(), CCValAssign::Full));
618 } else {
619 // Both halves must be passed on the stack, with proper alignment.
620 unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign());
621 State.addLoc(
622 CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
623 State.AllocateStack(XLenInBytes, StackAlign),
624 VA1.getLocVT(), CCValAssign::Full));
625 State.addLoc(CCValAssign::getMem(
626 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
627 CCValAssign::Full));
628 return false;
629 }
630
631 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
632 // The second half can also be passed via register.
633 State.addLoc(
634 CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
635 } else {
636 // The second half is passed via the stack, without additional alignment.
637 State.addLoc(CCValAssign::getMem(
638 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
639 CCValAssign::Full));
640 }
641
642 return false;
643}
644
645// Implements the RISC-V calling convention. Returns true upon failure.
646static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT,
647 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000648 CCState &State, bool IsFixed, bool IsRet, Type *OrigTy) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000649 unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
650 assert(XLen == 32 || XLen == 64);
651 MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000652 if (ValVT == MVT::f32) {
653 LocVT = MVT::i32;
654 LocInfo = CCValAssign::BCvt;
655 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000656
657 // Any return value split in to more than two values can't be returned
658 // directly.
659 if (IsRet && ValNo > 1)
660 return true;
661
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000662 // If this is a variadic argument, the RISC-V calling convention requires
663 // that it is assigned an 'even' or 'aligned' register if it has 8-byte
664 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
665 // be used regardless of whether the original argument was split during
666 // legalisation or not. The argument will not be passed by registers if the
667 // original type is larger than 2*XLEN, so the register alignment rule does
668 // not apply.
669 unsigned TwoXLenInBytes = (2 * XLen) / 8;
670 if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes &&
671 DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
672 unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
673 // Skip 'odd' register if necessary.
674 if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
675 State.AllocateReg(ArgGPRs);
676 }
677
Alex Bradburydc31c612017-12-11 12:49:02 +0000678 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
679 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
680 State.getPendingArgFlags();
681
682 assert(PendingLocs.size() == PendingArgFlags.size() &&
683 "PendingLocs and PendingArgFlags out of sync");
684
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000685 // Handle passing f64 on RV32D with a soft float ABI.
686 if (XLen == 32 && ValVT == MVT::f64) {
Mandeep Singh Grang88a8b262018-04-16 18:56:10 +0000687 assert(!ArgFlags.isSplit() && PendingLocs.empty() &&
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000688 "Can't lower f64 if it is split");
689 // Depending on available argument GPRS, f64 may be passed in a pair of
690 // GPRs, split between a GPR and the stack, or passed completely on the
691 // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
692 // cases.
693 unsigned Reg = State.AllocateReg(ArgGPRs);
694 LocVT = MVT::i32;
695 if (!Reg) {
696 unsigned StackOffset = State.AllocateStack(8, 8);
697 State.addLoc(
698 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
699 return false;
700 }
701 if (!State.AllocateReg(ArgGPRs))
702 State.AllocateStack(4, 4);
703 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
704 return false;
705 }
706
Alex Bradburydc31c612017-12-11 12:49:02 +0000707 // Split arguments might be passed indirectly, so keep track of the pending
708 // values.
709 if (ArgFlags.isSplit() || !PendingLocs.empty()) {
710 LocVT = XLenVT;
711 LocInfo = CCValAssign::Indirect;
712 PendingLocs.push_back(
713 CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
714 PendingArgFlags.push_back(ArgFlags);
715 if (!ArgFlags.isSplitEnd()) {
716 return false;
717 }
718 }
719
720 // If the split argument only had two elements, it should be passed directly
721 // in registers or on the stack.
722 if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
723 assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
724 // Apply the normal calling convention rules to the first half of the
725 // split argument.
726 CCValAssign VA = PendingLocs[0];
727 ISD::ArgFlagsTy AF = PendingArgFlags[0];
728 PendingLocs.clear();
729 PendingArgFlags.clear();
730 return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
731 ArgFlags);
732 }
733
734 // Allocate to a register if possible, or else a stack slot.
735 unsigned Reg = State.AllocateReg(ArgGPRs);
736 unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8);
737
738 // If we reach this point and PendingLocs is non-empty, we must be at the
739 // end of a split argument that must be passed indirectly.
740 if (!PendingLocs.empty()) {
741 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
742 assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
743
744 for (auto &It : PendingLocs) {
745 if (Reg)
746 It.convertToReg(Reg);
747 else
748 It.convertToMem(StackOffset);
749 State.addLoc(It);
750 }
751 PendingLocs.clear();
752 PendingArgFlags.clear();
753 return false;
754 }
755
756 assert(LocVT == XLenVT && "Expected an XLenVT at this stage");
757
758 if (Reg) {
759 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
760 } else {
761 State.addLoc(
762 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
763 }
764 return false;
765}
766
767void RISCVTargetLowering::analyzeInputArgs(
768 MachineFunction &MF, CCState &CCInfo,
769 const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
770 unsigned NumArgs = Ins.size();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000771 FunctionType *FType = MF.getFunction().getFunctionType();
Alex Bradburydc31c612017-12-11 12:49:02 +0000772
773 for (unsigned i = 0; i != NumArgs; ++i) {
774 MVT ArgVT = Ins[i].VT;
775 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
776
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000777 Type *ArgTy = nullptr;
778 if (IsRet)
779 ArgTy = FType->getReturnType();
780 else if (Ins[i].isOrigArg())
781 ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
782
Alex Bradburydc31c612017-12-11 12:49:02 +0000783 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000784 ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000785 DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
786 << EVT(ArgVT).getEVTString() << '\n');
787 llvm_unreachable(nullptr);
788 }
789 }
790}
791
792void RISCVTargetLowering::analyzeOutputArgs(
793 MachineFunction &MF, CCState &CCInfo,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000794 const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
795 CallLoweringInfo *CLI) const {
Alex Bradburydc31c612017-12-11 12:49:02 +0000796 unsigned NumArgs = Outs.size();
797
798 for (unsigned i = 0; i != NumArgs; i++) {
799 MVT ArgVT = Outs[i].VT;
800 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000801 Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
Alex Bradburydc31c612017-12-11 12:49:02 +0000802
803 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000804 ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000805 DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
806 << EVT(ArgVT).getEVTString() << "\n");
807 llvm_unreachable(nullptr);
808 }
809 }
810}
811
812// The caller is responsible for loading the full value if the argument is
813// passed with CCValAssign::Indirect.
814static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
815 const CCValAssign &VA, const SDLoc &DL) {
816 MachineFunction &MF = DAG.getMachineFunction();
817 MachineRegisterInfo &RegInfo = MF.getRegInfo();
818 EVT LocVT = VA.getLocVT();
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000819 EVT ValVT = VA.getValVT();
Alex Bradburydc31c612017-12-11 12:49:02 +0000820 SDValue Val;
821
822 unsigned VReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
823 RegInfo.addLiveIn(VA.getLocReg(), VReg);
824 Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
825
826 switch (VA.getLocInfo()) {
827 default:
828 llvm_unreachable("Unexpected CCValAssign::LocInfo");
829 case CCValAssign::Full:
830 case CCValAssign::Indirect:
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000831 break;
832 case CCValAssign::BCvt:
833 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
834 break;
Alex Bradburydc31c612017-12-11 12:49:02 +0000835 }
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000836 return Val;
Alex Bradburydc31c612017-12-11 12:49:02 +0000837}
838
839// The caller is responsible for loading the full value if the argument is
840// passed with CCValAssign::Indirect.
841static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
842 const CCValAssign &VA, const SDLoc &DL) {
843 MachineFunction &MF = DAG.getMachineFunction();
844 MachineFrameInfo &MFI = MF.getFrameInfo();
845 EVT LocVT = VA.getLocVT();
846 EVT ValVT = VA.getValVT();
847 EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
848 int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
849 VA.getLocMemOffset(), /*Immutable=*/true);
850 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
851 SDValue Val;
852
853 ISD::LoadExtType ExtType;
854 switch (VA.getLocInfo()) {
855 default:
856 llvm_unreachable("Unexpected CCValAssign::LocInfo");
857 case CCValAssign::Full:
858 case CCValAssign::Indirect:
859 ExtType = ISD::NON_EXTLOAD;
860 break;
861 }
862 Val = DAG.getExtLoad(
863 ExtType, DL, LocVT, Chain, FIN,
864 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
865 return Val;
866}
Alex Bradbury89718422017-10-19 21:37:38 +0000867
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000868static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
869 const CCValAssign &VA, const SDLoc &DL) {
870 assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
871 "Unexpected VA");
872 MachineFunction &MF = DAG.getMachineFunction();
873 MachineFrameInfo &MFI = MF.getFrameInfo();
874 MachineRegisterInfo &RegInfo = MF.getRegInfo();
875
876 if (VA.isMemLoc()) {
877 // f64 is passed on the stack.
878 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
879 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
880 return DAG.getLoad(MVT::f64, DL, Chain, FIN,
881 MachinePointerInfo::getFixedStack(MF, FI));
882 }
883
884 assert(VA.isRegLoc() && "Expected register VA assignment");
885
886 unsigned LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
887 RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
888 SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
889 SDValue Hi;
890 if (VA.getLocReg() == RISCV::X17) {
891 // Second half of f64 is passed on the stack.
892 int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
893 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
894 Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
895 MachinePointerInfo::getFixedStack(MF, FI));
896 } else {
897 // Second half of f64 is passed in another GPR.
898 unsigned HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
899 RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
900 Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
901 }
902 return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
903}
904
Alex Bradbury89718422017-10-19 21:37:38 +0000905// Transform physical registers into virtual registers.
906SDValue RISCVTargetLowering::LowerFormalArguments(
907 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
908 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
909 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
910
911 switch (CallConv) {
912 default:
913 report_fatal_error("Unsupported calling convention");
914 case CallingConv::C:
Alex Bradburya3376752017-11-08 13:41:21 +0000915 case CallingConv::Fast:
Alex Bradbury89718422017-10-19 21:37:38 +0000916 break;
917 }
918
919 MachineFunction &MF = DAG.getMachineFunction();
Alex Bradburydc31c612017-12-11 12:49:02 +0000920 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000921 MVT XLenVT = Subtarget.getXLenVT();
922 unsigned XLenInBytes = Subtarget.getXLen() / 8;
923 // Used with vargs to acumulate store chains.
924 std::vector<SDValue> OutChains;
Alex Bradbury89718422017-10-19 21:37:38 +0000925
926 // Assign locations to all of the incoming arguments.
927 SmallVector<CCValAssign, 16> ArgLocs;
928 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +0000929 analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
Alex Bradbury89718422017-10-19 21:37:38 +0000930
Alex Bradburydc31c612017-12-11 12:49:02 +0000931 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
932 CCValAssign &VA = ArgLocs[i];
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000933 assert(VA.getLocVT() == XLenVT && "Unhandled argument type");
Alex Bradburydc31c612017-12-11 12:49:02 +0000934 SDValue ArgValue;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000935 // Passing f64 on RV32D with a soft float ABI must be handled as a special
936 // case.
937 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
938 ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
939 else if (VA.isRegLoc())
Alex Bradburydc31c612017-12-11 12:49:02 +0000940 ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL);
941 else
942 ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
Alex Bradbury89718422017-10-19 21:37:38 +0000943
Alex Bradburydc31c612017-12-11 12:49:02 +0000944 if (VA.getLocInfo() == CCValAssign::Indirect) {
945 // If the original argument was split and passed by reference (e.g. i128
946 // on RV32), we need to load all parts of it here (using the same
947 // address).
948 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
949 MachinePointerInfo()));
950 unsigned ArgIndex = Ins[i].OrigArgIndex;
951 assert(Ins[i].PartOffset == 0);
952 while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
953 CCValAssign &PartVA = ArgLocs[i + 1];
954 unsigned PartOffset = Ins[i + 1].PartOffset;
955 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
956 DAG.getIntPtrConstant(PartOffset, DL));
957 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
958 MachinePointerInfo()));
959 ++i;
960 }
961 continue;
Alex Bradbury89718422017-10-19 21:37:38 +0000962 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000963 InVals.push_back(ArgValue);
Alex Bradbury89718422017-10-19 21:37:38 +0000964 }
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000965
966 if (IsVarArg) {
967 ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
968 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
969 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
970 MachineFrameInfo &MFI = MF.getFrameInfo();
971 MachineRegisterInfo &RegInfo = MF.getRegInfo();
972 RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
973
974 // Offset of the first variable argument from stack pointer, and size of
975 // the vararg save area. For now, the varargs save area is either zero or
976 // large enough to hold a0-a7.
977 int VaArgOffset, VarArgsSaveSize;
978
979 // If all registers are allocated, then all varargs must be passed on the
980 // stack and we don't need to save any argregs.
981 if (ArgRegs.size() == Idx) {
982 VaArgOffset = CCInfo.getNextStackOffset();
983 VarArgsSaveSize = 0;
984 } else {
985 VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
986 VaArgOffset = -VarArgsSaveSize;
987 }
988
989 // Record the frame index of the first variable argument
990 // which is a value necessary to VASTART.
991 int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
992 RVFI->setVarArgsFrameIndex(FI);
993
994 // If saving an odd number of registers then create an extra stack slot to
995 // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
996 // offsets to even-numbered registered remain 2*XLEN-aligned.
997 if (Idx % 2) {
998 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes,
999 true);
1000 VarArgsSaveSize += XLenInBytes;
1001 }
1002
1003 // Copy the integer registers that may have been used for passing varargs
1004 // to the vararg save area.
1005 for (unsigned I = Idx; I < ArgRegs.size();
1006 ++I, VaArgOffset += XLenInBytes) {
1007 const unsigned Reg = RegInfo.createVirtualRegister(RC);
1008 RegInfo.addLiveIn(ArgRegs[I], Reg);
1009 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
1010 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
1011 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1012 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
1013 MachinePointerInfo::getFixedStack(MF, FI));
1014 cast<StoreSDNode>(Store.getNode())
1015 ->getMemOperand()
1016 ->setValue((Value *)nullptr);
1017 OutChains.push_back(Store);
1018 }
1019 RVFI->setVarArgsSaveSize(VarArgsSaveSize);
1020 }
1021
1022 // All stores are grouped in one node to allow the matching between
1023 // the size of Ins and InVals. This only happens for vararg functions.
1024 if (!OutChains.empty()) {
1025 OutChains.push_back(Chain);
1026 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
1027 }
1028
Alex Bradbury89718422017-10-19 21:37:38 +00001029 return Chain;
1030}
1031
Alex Bradburya3376752017-11-08 13:41:21 +00001032// Lower a call to a callseq_start + CALL + callseq_end chain, and add input
1033// and output parameter nodes.
1034SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1035 SmallVectorImpl<SDValue> &InVals) const {
1036 SelectionDAG &DAG = CLI.DAG;
1037 SDLoc &DL = CLI.DL;
1038 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1039 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1040 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1041 SDValue Chain = CLI.Chain;
1042 SDValue Callee = CLI.Callee;
1043 CLI.IsTailCall = false;
1044 CallingConv::ID CallConv = CLI.CallConv;
1045 bool IsVarArg = CLI.IsVarArg;
1046 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburydc31c612017-12-11 12:49:02 +00001047 MVT XLenVT = Subtarget.getXLenVT();
Alex Bradburya3376752017-11-08 13:41:21 +00001048
Alex Bradburya3376752017-11-08 13:41:21 +00001049 MachineFunction &MF = DAG.getMachineFunction();
1050
1051 // Analyze the operands of the call, assigning locations to each operand.
1052 SmallVector<CCValAssign, 16> ArgLocs;
1053 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001054 analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
Alex Bradburya3376752017-11-08 13:41:21 +00001055
1056 // Get a count of how many bytes are to be pushed on the stack.
1057 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1058
Alex Bradburydc31c612017-12-11 12:49:02 +00001059 // Create local copies for byval args
1060 SmallVector<SDValue, 8> ByValArgs;
1061 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1062 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1063 if (!Flags.isByVal())
Alex Bradburya3376752017-11-08 13:41:21 +00001064 continue;
Alex Bradburydc31c612017-12-11 12:49:02 +00001065
1066 SDValue Arg = OutVals[i];
1067 unsigned Size = Flags.getByValSize();
1068 unsigned Align = Flags.getByValAlign();
1069
1070 int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false);
1071 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1072 SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
1073
1074 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
1075 /*IsVolatile=*/false,
1076 /*AlwaysInline=*/false,
1077 /*isTailCall=*/false, MachinePointerInfo(),
1078 MachinePointerInfo());
1079 ByValArgs.push_back(FIPtr);
Alex Bradburya3376752017-11-08 13:41:21 +00001080 }
1081
1082 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
1083
1084 // Copy argument values to their designated locations.
1085 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Alex Bradburydc31c612017-12-11 12:49:02 +00001086 SmallVector<SDValue, 8> MemOpChains;
Alex Bradburya3376752017-11-08 13:41:21 +00001087 SDValue StackPtr;
Alex Bradburydc31c612017-12-11 12:49:02 +00001088 for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
1089 CCValAssign &VA = ArgLocs[i];
1090 SDValue ArgValue = OutVals[i];
1091 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Alex Bradburya3376752017-11-08 13:41:21 +00001092
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001093 // Handle passing f64 on RV32D with a soft float ABI as a special case.
1094 bool IsF64OnRV32DSoftABI =
1095 VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
1096 if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
1097 SDValue SplitF64 = DAG.getNode(
1098 RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
1099 SDValue Lo = SplitF64.getValue(0);
1100 SDValue Hi = SplitF64.getValue(1);
1101
1102 unsigned RegLo = VA.getLocReg();
1103 RegsToPass.push_back(std::make_pair(RegLo, Lo));
1104
1105 if (RegLo == RISCV::X17) {
1106 // Second half of f64 is passed on the stack.
1107 // Work out the address of the stack slot.
1108 if (!StackPtr.getNode())
1109 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1110 // Emit the store.
1111 MemOpChains.push_back(
1112 DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
1113 } else {
1114 // Second half of f64 is passed in another GPR.
1115 unsigned RegHigh = RegLo + 1;
1116 RegsToPass.push_back(std::make_pair(RegHigh, Hi));
1117 }
1118 continue;
1119 }
1120
1121 // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
1122 // as any other MemLoc.
1123
Alex Bradburya3376752017-11-08 13:41:21 +00001124 // Promote the value if needed.
Alex Bradburydc31c612017-12-11 12:49:02 +00001125 // For now, only handle fully promoted and indirect arguments.
Alex Bradburya3376752017-11-08 13:41:21 +00001126 switch (VA.getLocInfo()) {
1127 case CCValAssign::Full:
1128 break;
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001129 case CCValAssign::BCvt:
1130 ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), ArgValue);
1131 break;
Alex Bradburydc31c612017-12-11 12:49:02 +00001132 case CCValAssign::Indirect: {
1133 // Store the argument in a stack slot and pass its address.
1134 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
1135 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1136 MemOpChains.push_back(
1137 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1138 MachinePointerInfo::getFixedStack(MF, FI)));
1139 // If the original argument was split (e.g. i128), we need
1140 // to store all parts of it here (and pass just one address).
1141 unsigned ArgIndex = Outs[i].OrigArgIndex;
1142 assert(Outs[i].PartOffset == 0);
1143 while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
1144 SDValue PartValue = OutVals[i + 1];
1145 unsigned PartOffset = Outs[i + 1].PartOffset;
1146 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1147 DAG.getIntPtrConstant(PartOffset, DL));
1148 MemOpChains.push_back(
1149 DAG.getStore(Chain, DL, PartValue, Address,
1150 MachinePointerInfo::getFixedStack(MF, FI)));
1151 ++i;
1152 }
1153 ArgValue = SpillSlot;
1154 break;
1155 }
Alex Bradburya3376752017-11-08 13:41:21 +00001156 default:
1157 llvm_unreachable("Unknown loc info!");
1158 }
1159
Alex Bradburydc31c612017-12-11 12:49:02 +00001160 // Use local copy if it is a byval arg.
1161 if (Flags.isByVal())
1162 ArgValue = ByValArgs[j++];
1163
Alex Bradburya3376752017-11-08 13:41:21 +00001164 if (VA.isRegLoc()) {
1165 // Queue up the argument copies and emit them at the end.
1166 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1167 } else {
1168 assert(VA.isMemLoc() && "Argument not register or memory");
Alex Bradburydc31c612017-12-11 12:49:02 +00001169
1170 // Work out the address of the stack slot.
1171 if (!StackPtr.getNode())
1172 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1173 SDValue Address =
1174 DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
1175 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
1176
1177 // Emit the store.
1178 MemOpChains.push_back(
1179 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Alex Bradburya3376752017-11-08 13:41:21 +00001180 }
1181 }
1182
Alex Bradburydc31c612017-12-11 12:49:02 +00001183 // Join the stores, which are independent of one another.
1184 if (!MemOpChains.empty())
1185 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
1186
Alex Bradburya3376752017-11-08 13:41:21 +00001187 SDValue Glue;
1188
1189 // Build a sequence of copy-to-reg nodes, chained and glued together.
1190 for (auto &Reg : RegsToPass) {
1191 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
1192 Glue = Chain.getValue(1);
1193 }
1194
Shiva Chend58bd8d2018-04-25 14:19:12 +00001195 // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
1196 // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
1197 // split it and then direct call can be matched by PseudoCALL.
1198 if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
1199 Callee = DAG.getTargetGlobalAddress(S->getGlobal(), DL, PtrVT, 0, 0);
1200 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1201 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, 0);
Alex Bradburya3376752017-11-08 13:41:21 +00001202 }
1203
1204 // The first call operand is the chain and the second is the target address.
1205 SmallVector<SDValue, 8> Ops;
1206 Ops.push_back(Chain);
1207 Ops.push_back(Callee);
1208
1209 // Add argument registers to the end of the list so that they are
1210 // known live into the call.
1211 for (auto &Reg : RegsToPass)
1212 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
1213
1214 // Add a register mask operand representing the call-preserved registers.
1215 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1216 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
1217 assert(Mask && "Missing call preserved mask for calling convention");
1218 Ops.push_back(DAG.getRegisterMask(Mask));
1219
1220 // Glue the call to the argument copies, if any.
1221 if (Glue.getNode())
1222 Ops.push_back(Glue);
1223
1224 // Emit the call.
1225 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1226 Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
1227 Glue = Chain.getValue(1);
1228
1229 // Mark the end of the call, which is glued to the call itself.
1230 Chain = DAG.getCALLSEQ_END(Chain,
1231 DAG.getConstant(NumBytes, DL, PtrVT, true),
1232 DAG.getConstant(0, DL, PtrVT, true),
1233 Glue, DL);
1234 Glue = Chain.getValue(1);
1235
1236 // Assign locations to each value returned by this call.
1237 SmallVector<CCValAssign, 16> RVLocs;
1238 CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +00001239 analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
Alex Bradburya3376752017-11-08 13:41:21 +00001240
1241 // Copy all of the result registers out of their specified physreg.
1242 for (auto &VA : RVLocs) {
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001243 // Copy the value out
1244 SDValue RetValue =
1245 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
1246 // Glue the RetValue to the end of the call sequence
Alex Bradburya3376752017-11-08 13:41:21 +00001247 Chain = RetValue.getValue(1);
1248 Glue = RetValue.getValue(2);
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001249 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1250 assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
1251 SDValue RetValue2 =
1252 DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
1253 Chain = RetValue2.getValue(1);
1254 Glue = RetValue2.getValue(2);
1255 RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
1256 RetValue2);
1257 }
Alex Bradburya3376752017-11-08 13:41:21 +00001258
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001259 switch (VA.getLocInfo()) {
1260 default:
1261 llvm_unreachable("Unknown loc info!");
1262 case CCValAssign::Full:
1263 break;
1264 case CCValAssign::BCvt:
1265 RetValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), RetValue);
1266 break;
1267 }
1268
Alex Bradburydc31c612017-12-11 12:49:02 +00001269 InVals.push_back(RetValue);
Alex Bradburya3376752017-11-08 13:41:21 +00001270 }
1271
1272 return Chain;
1273}
1274
Alex Bradburydc31c612017-12-11 12:49:02 +00001275bool RISCVTargetLowering::CanLowerReturn(
1276 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
1277 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
1278 SmallVector<CCValAssign, 16> RVLocs;
1279 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
1280 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1281 MVT VT = Outs[i].VT;
1282 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1283 if (CC_RISCV(MF.getDataLayout(), i, VT, VT, CCValAssign::Full, ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001284 CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr))
Alex Bradburydc31c612017-12-11 12:49:02 +00001285 return false;
1286 }
1287 return true;
1288}
1289
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001290static SDValue packIntoRegLoc(SelectionDAG &DAG, SDValue Val,
1291 const CCValAssign &VA, const SDLoc &DL) {
1292 EVT LocVT = VA.getLocVT();
1293
1294 switch (VA.getLocInfo()) {
1295 default:
1296 llvm_unreachable("Unexpected CCValAssign::LocInfo");
1297 case CCValAssign::Full:
1298 break;
1299 case CCValAssign::BCvt:
1300 Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
1301 break;
1302 }
1303 return Val;
1304}
1305
Alex Bradbury89718422017-10-19 21:37:38 +00001306SDValue
1307RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1308 bool IsVarArg,
1309 const SmallVectorImpl<ISD::OutputArg> &Outs,
1310 const SmallVectorImpl<SDValue> &OutVals,
1311 const SDLoc &DL, SelectionDAG &DAG) const {
Alex Bradbury89718422017-10-19 21:37:38 +00001312 // Stores the assignment of the return value to a location.
1313 SmallVector<CCValAssign, 16> RVLocs;
1314
1315 // Info about the registers and stack slot.
1316 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
1317 *DAG.getContext());
1318
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001319 analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
1320 nullptr);
Alex Bradbury89718422017-10-19 21:37:38 +00001321
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001322 SDValue Glue;
Alex Bradbury89718422017-10-19 21:37:38 +00001323 SmallVector<SDValue, 4> RetOps(1, Chain);
1324
1325 // Copy the result values into the output registers.
1326 for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
Alex Bradburydc31c612017-12-11 12:49:02 +00001327 SDValue Val = OutVals[i];
Alex Bradbury89718422017-10-19 21:37:38 +00001328 CCValAssign &VA = RVLocs[i];
1329 assert(VA.isRegLoc() && "Can only return in registers!");
1330
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001331 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1332 // Handle returning f64 on RV32D with a soft float ABI.
1333 assert(VA.isRegLoc() && "Expected return via registers");
1334 SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
1335 DAG.getVTList(MVT::i32, MVT::i32), Val);
1336 SDValue Lo = SplitF64.getValue(0);
1337 SDValue Hi = SplitF64.getValue(1);
1338 unsigned RegLo = VA.getLocReg();
1339 unsigned RegHi = RegLo + 1;
1340 Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
1341 Glue = Chain.getValue(1);
1342 RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
1343 Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
1344 Glue = Chain.getValue(1);
1345 RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
1346 } else {
1347 // Handle a 'normal' return.
1348 Val = packIntoRegLoc(DAG, Val, VA, DL);
1349 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001350
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001351 // Guarantee that all emitted copies are stuck together.
1352 Glue = Chain.getValue(1);
1353 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1354 }
Alex Bradbury89718422017-10-19 21:37:38 +00001355 }
1356
1357 RetOps[0] = Chain; // Update chain.
1358
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001359 // Add the glue node if we have it.
1360 if (Glue.getNode()) {
1361 RetOps.push_back(Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001362 }
1363
1364 return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
1365}
1366
1367const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
1368 switch ((RISCVISD::NodeType)Opcode) {
1369 case RISCVISD::FIRST_NUMBER:
1370 break;
1371 case RISCVISD::RET_FLAG:
1372 return "RISCVISD::RET_FLAG";
Alex Bradburya3376752017-11-08 13:41:21 +00001373 case RISCVISD::CALL:
1374 return "RISCVISD::CALL";
Alex Bradbury65385162017-11-21 07:51:32 +00001375 case RISCVISD::SELECT_CC:
1376 return "RISCVISD::SELECT_CC";
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001377 case RISCVISD::BuildPairF64:
1378 return "RISCVISD::BuildPairF64";
1379 case RISCVISD::SplitF64:
1380 return "RISCVISD::SplitF64";
Alex Bradbury89718422017-10-19 21:37:38 +00001381 }
1382 return nullptr;
1383}
Alex Bradbury9330e642018-01-10 20:05:09 +00001384
1385std::pair<unsigned, const TargetRegisterClass *>
1386RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1387 StringRef Constraint,
1388 MVT VT) const {
1389 // First, see if this is a constraint that directly corresponds to a
1390 // RISCV register class.
1391 if (Constraint.size() == 1) {
1392 switch (Constraint[0]) {
1393 case 'r':
1394 return std::make_pair(0U, &RISCV::GPRRegClass);
1395 default:
1396 break;
1397 }
1398 }
1399
1400 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1401}