blob: 27e0d7f008551bd42f55dfa75b5e8b43b2f24cb8 [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 Bradbury76c29ee2018-03-20 12:45:35 +0000111 if (Subtarget.hasStdExtF()) {
112 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
113 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000114 for (auto CC :
115 {ISD::SETOGT, ISD::SETOGE, ISD::SETONE, ISD::SETO, ISD::SETUEQ,
116 ISD::SETUGT, ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUNE,
117 ISD::SETGT, ISD::SETGE, ISD::SETNE})
118 setCondCodeAction(CC, MVT::f32, Expand);
119 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
120 setOperationAction(ISD::SELECT, MVT::f32, Custom);
121 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000122 }
123
Alex Bradbury5d0dfa52018-04-12 05:42:42 +0000124 if (Subtarget.hasStdExtD()) {
125 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
126 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000127 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
Alex Bradbury60baa2e2018-04-12 05:47:15 +0000128 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Alex Bradbury5d0dfa52018-04-12 05:42:42 +0000129 }
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000130
Alex Bradburyffc435e2017-11-21 08:11:03 +0000131 setOperationAction(ISD::GlobalAddress, XLenVT, Custom);
132 setOperationAction(ISD::BlockAddress, XLenVT, Custom);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000133 setOperationAction(ISD::ConstantPool, XLenVT, Custom);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000134
Alex Bradbury89718422017-10-19 21:37:38 +0000135 setBooleanContents(ZeroOrOneBooleanContent);
136
137 // Function alignments (log2).
138 setMinFunctionAlignment(3);
139 setPrefFunctionAlignment(3);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000140
141 // Effectively disable jump table generation.
142 setMinimumJumpTableEntries(INT_MAX);
Alex Bradbury89718422017-10-19 21:37:38 +0000143}
144
Shiva Chenbbf4c5c2018-02-02 02:43:18 +0000145EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
146 EVT VT) const {
147 if (!VT.isVector())
148 return getPointerTy(DL);
149 return VT.changeVectorElementTypeToInteger();
150}
151
Alex Bradbury65385162017-11-21 07:51:32 +0000152// Changes the condition code and swaps operands if necessary, so the SetCC
153// operation matches one of the comparisons supported directly in the RISC-V
154// ISA.
155static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
156 switch (CC) {
157 default:
158 break;
159 case ISD::SETGT:
160 case ISD::SETLE:
161 case ISD::SETUGT:
162 case ISD::SETULE:
163 CC = ISD::getSetCCSwappedOperands(CC);
164 std::swap(LHS, RHS);
165 break;
166 }
167}
168
169// Return the RISC-V branch opcode that matches the given DAG integer
170// condition code. The CondCode must be one of those supported by the RISC-V
171// ISA (see normaliseSetCC).
172static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
173 switch (CC) {
174 default:
175 llvm_unreachable("Unsupported CondCode");
176 case ISD::SETEQ:
177 return RISCV::BEQ;
178 case ISD::SETNE:
179 return RISCV::BNE;
180 case ISD::SETLT:
181 return RISCV::BLT;
182 case ISD::SETGE:
183 return RISCV::BGE;
184 case ISD::SETULT:
185 return RISCV::BLTU;
186 case ISD::SETUGE:
187 return RISCV::BGEU;
188 }
189}
190
Alex Bradbury89718422017-10-19 21:37:38 +0000191SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
192 SelectionDAG &DAG) const {
193 switch (Op.getOpcode()) {
194 default:
195 report_fatal_error("unimplemented operand");
Alex Bradburyec8aa912017-11-08 13:24:21 +0000196 case ISD::GlobalAddress:
197 return lowerGlobalAddress(Op, DAG);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000198 case ISD::BlockAddress:
199 return lowerBlockAddress(Op, DAG);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000200 case ISD::ConstantPool:
201 return lowerConstantPool(Op, DAG);
Alex Bradbury65385162017-11-21 07:51:32 +0000202 case ISD::SELECT:
203 return lowerSELECT(Op, DAG);
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000204 case ISD::VASTART:
205 return lowerVASTART(Op, DAG);
Alex Bradbury70f137b2018-01-10 20:12:00 +0000206 case ISD::FRAMEADDR:
207 return LowerFRAMEADDR(Op, DAG);
208 case ISD::RETURNADDR:
209 return LowerRETURNADDR(Op, DAG);
Alex Bradburyec8aa912017-11-08 13:24:21 +0000210 }
211}
212
213SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
214 SelectionDAG &DAG) const {
215 SDLoc DL(Op);
216 EVT Ty = Op.getValueType();
217 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
218 const GlobalValue *GV = N->getGlobal();
219 int64_t Offset = N->getOffset();
220
Alex Bradburyffc435e2017-11-21 08:11:03 +0000221 if (isPositionIndependent() || Subtarget.is64Bit())
Alex Bradburyec8aa912017-11-08 13:24:21 +0000222 report_fatal_error("Unable to lowerGlobalAddress");
Alex Bradburyffc435e2017-11-21 08:11:03 +0000223
224 SDValue GAHi =
225 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_HI);
226 SDValue GALo =
227 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_LO);
228 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
229 SDValue MNLo =
230 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
231 return MNLo;
232}
233
234SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
235 SelectionDAG &DAG) const {
236 SDLoc DL(Op);
237 EVT Ty = Op.getValueType();
238 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
239 const BlockAddress *BA = N->getBlockAddress();
240 int64_t Offset = N->getOffset();
241
242 if (isPositionIndependent() || Subtarget.is64Bit())
243 report_fatal_error("Unable to lowerBlockAddress");
244
245 SDValue BAHi = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_HI);
246 SDValue BALo = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_LO);
247 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, BAHi), 0);
248 SDValue MNLo =
249 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, BALo), 0);
250 return MNLo;
251}
252
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000253SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
254 SelectionDAG &DAG) const {
255 SDLoc DL(Op);
256 EVT Ty = Op.getValueType();
257 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
258 const Constant *CPA = N->getConstVal();
259 int64_t Offset = N->getOffset();
260 unsigned Alignment = N->getAlignment();
261
262 if (!isPositionIndependent()) {
263 SDValue CPAHi =
264 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_HI);
265 SDValue CPALo =
266 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_LO);
267 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, CPAHi), 0);
268 SDValue MNLo =
269 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, CPALo), 0);
270 return MNLo;
271 } else {
272 report_fatal_error("Unable to lowerConstantPool");
273 }
274}
275
Alex Bradburyffc435e2017-11-21 08:11:03 +0000276SDValue RISCVTargetLowering::lowerExternalSymbol(SDValue Op,
277 SelectionDAG &DAG) const {
278 SDLoc DL(Op);
279 EVT Ty = Op.getValueType();
280 ExternalSymbolSDNode *N = cast<ExternalSymbolSDNode>(Op);
281 const char *Sym = N->getSymbol();
282
283 // TODO: should also handle gp-relative loads.
284
285 if (isPositionIndependent() || Subtarget.is64Bit())
286 report_fatal_error("Unable to lowerExternalSymbol");
287
288 SDValue GAHi = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_HI);
289 SDValue GALo = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_LO);
290 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
291 SDValue MNLo =
292 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
293 return MNLo;
Alex Bradbury89718422017-10-19 21:37:38 +0000294}
295
Alex Bradbury65385162017-11-21 07:51:32 +0000296SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
297 SDValue CondV = Op.getOperand(0);
298 SDValue TrueV = Op.getOperand(1);
299 SDValue FalseV = Op.getOperand(2);
300 SDLoc DL(Op);
301 MVT XLenVT = Subtarget.getXLenVT();
302
303 // If the result type is XLenVT and CondV is the output of a SETCC node
304 // which also operated on XLenVT inputs, then merge the SETCC node into the
305 // lowered RISCVISD::SELECT_CC to take advantage of the integer
306 // compare+branch instructions. i.e.:
307 // (select (setcc lhs, rhs, cc), truev, falsev)
308 // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
309 if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
310 CondV.getOperand(0).getSimpleValueType() == XLenVT) {
311 SDValue LHS = CondV.getOperand(0);
312 SDValue RHS = CondV.getOperand(1);
313 auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
314 ISD::CondCode CCVal = CC->get();
315
316 normaliseSetCC(LHS, RHS, CCVal);
317
318 SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
319 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
320 SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
321 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
322 }
323
324 // Otherwise:
325 // (select condv, truev, falsev)
326 // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
327 SDValue Zero = DAG.getConstant(0, DL, XLenVT);
328 SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
329
330 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
331 SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
332
333 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
334}
335
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000336SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
337 MachineFunction &MF = DAG.getMachineFunction();
338 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
339
340 SDLoc DL(Op);
341 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
342 getPointerTy(MF.getDataLayout()));
343
344 // vastart just stores the address of the VarArgsFrameIndex slot into the
345 // memory location argument.
346 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
347 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
348 MachinePointerInfo(SV));
349}
350
Alex Bradbury70f137b2018-01-10 20:12:00 +0000351SDValue RISCVTargetLowering::LowerFRAMEADDR(SDValue Op,
352 SelectionDAG &DAG) const {
353 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
354 MachineFunction &MF = DAG.getMachineFunction();
355 MachineFrameInfo &MFI = MF.getFrameInfo();
356 MFI.setFrameAddressIsTaken(true);
357 unsigned FrameReg = RI.getFrameRegister(MF);
358 int XLenInBytes = Subtarget.getXLen() / 8;
359
360 EVT VT = Op.getValueType();
361 SDLoc DL(Op);
362 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
363 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
364 while (Depth--) {
365 int Offset = -(XLenInBytes * 2);
366 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
367 DAG.getIntPtrConstant(Offset, DL));
368 FrameAddr =
369 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
370 }
371 return FrameAddr;
372}
373
374SDValue RISCVTargetLowering::LowerRETURNADDR(SDValue Op,
375 SelectionDAG &DAG) const {
376 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
377 MachineFunction &MF = DAG.getMachineFunction();
378 MachineFrameInfo &MFI = MF.getFrameInfo();
379 MFI.setReturnAddressIsTaken(true);
380 MVT XLenVT = Subtarget.getXLenVT();
381 int XLenInBytes = Subtarget.getXLen() / 8;
382
383 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
384 return SDValue();
385
386 EVT VT = Op.getValueType();
387 SDLoc DL(Op);
388 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
389 if (Depth) {
390 int Off = -XLenInBytes;
391 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
392 SDValue Offset = DAG.getConstant(Off, DL, VT);
393 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
394 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
395 MachinePointerInfo());
396 }
397
398 // Return the value of the return address register, marking it an implicit
399 // live-in.
400 unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
401 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
402}
403
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000404static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
405 MachineBasicBlock *BB) {
406 assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
407
408 MachineFunction &MF = *BB->getParent();
409 DebugLoc DL = MI.getDebugLoc();
410 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
411 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
412 unsigned LoReg = MI.getOperand(0).getReg();
413 unsigned HiReg = MI.getOperand(1).getReg();
414 unsigned SrcReg = MI.getOperand(2).getReg();
415 const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
416 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
417
418 TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
419 RI);
420 MachineMemOperand *MMO =
421 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
422 MachineMemOperand::MOLoad, 8, 8);
423 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
424 .addFrameIndex(FI)
425 .addImm(0)
426 .addMemOperand(MMO);
427 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
428 .addFrameIndex(FI)
429 .addImm(4)
430 .addMemOperand(MMO);
431 MI.eraseFromParent(); // The pseudo instruction is gone now.
432 return BB;
433}
434
435static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
436 MachineBasicBlock *BB) {
437 assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
438 "Unexpected instruction");
439
440 MachineFunction &MF = *BB->getParent();
441 DebugLoc DL = MI.getDebugLoc();
442 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
443 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
444 unsigned DstReg = MI.getOperand(0).getReg();
445 unsigned LoReg = MI.getOperand(1).getReg();
446 unsigned HiReg = MI.getOperand(2).getReg();
447 const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
448 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
449
450 MachineMemOperand *MMO =
451 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
452 MachineMemOperand::MOStore, 8, 8);
453 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
454 .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
455 .addFrameIndex(FI)
456 .addImm(0)
457 .addMemOperand(MMO);
458 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
459 .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
460 .addFrameIndex(FI)
461 .addImm(4)
462 .addMemOperand(MMO);
463 TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
464 MI.eraseFromParent(); // The pseudo instruction is gone now.
465 return BB;
466}
467
Alex Bradbury65385162017-11-21 07:51:32 +0000468MachineBasicBlock *
469RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
470 MachineBasicBlock *BB) const {
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000471 switch (MI.getOpcode()) {
472 default:
473 llvm_unreachable("Unexpected instr type to insert");
474 case RISCV::Select_GPR_Using_CC_GPR:
475 case RISCV::Select_FPR32_Using_CC_GPR:
476 break;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000477 case RISCV::BuildPairF64Pseudo:
478 return emitBuildPairF64Pseudo(MI, BB);
479 case RISCV::SplitF64Pseudo:
480 return emitSplitF64Pseudo(MI, BB);
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000481 }
Alex Bradbury65385162017-11-21 07:51:32 +0000482
483 // To "insert" a SELECT instruction, we actually have to insert the triangle
484 // control-flow pattern. The incoming instruction knows the destination vreg
485 // to set, the condition code register to branch on, the true/false values to
486 // select between, and the condcode to use to select the appropriate branch.
487 //
488 // We produce the following control flow:
489 // HeadMBB
490 // | \
491 // | IfFalseMBB
492 // | /
493 // TailMBB
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000494 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
Alex Bradbury65385162017-11-21 07:51:32 +0000495 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000496 DebugLoc DL = MI.getDebugLoc();
Alex Bradbury65385162017-11-21 07:51:32 +0000497 MachineFunction::iterator I = ++BB->getIterator();
498
499 MachineBasicBlock *HeadMBB = BB;
500 MachineFunction *F = BB->getParent();
501 MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
502 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
503
504 F->insert(I, IfFalseMBB);
505 F->insert(I, TailMBB);
506 // Move all remaining instructions to TailMBB.
507 TailMBB->splice(TailMBB->begin(), HeadMBB,
508 std::next(MachineBasicBlock::iterator(MI)), HeadMBB->end());
509 // Update machine-CFG edges by transferring all successors of the current
510 // block to the new block which will contain the Phi node for the select.
511 TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
512 // Set the successors for HeadMBB.
513 HeadMBB->addSuccessor(IfFalseMBB);
514 HeadMBB->addSuccessor(TailMBB);
515
516 // Insert appropriate branch.
517 unsigned LHS = MI.getOperand(1).getReg();
518 unsigned RHS = MI.getOperand(2).getReg();
519 auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
520 unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
521
522 BuildMI(HeadMBB, DL, TII.get(Opcode))
523 .addReg(LHS)
524 .addReg(RHS)
525 .addMBB(TailMBB);
526
527 // IfFalseMBB just falls through to TailMBB.
528 IfFalseMBB->addSuccessor(TailMBB);
529
530 // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
531 BuildMI(*TailMBB, TailMBB->begin(), DL, TII.get(RISCV::PHI),
532 MI.getOperand(0).getReg())
533 .addReg(MI.getOperand(4).getReg())
534 .addMBB(HeadMBB)
535 .addReg(MI.getOperand(5).getReg())
536 .addMBB(IfFalseMBB);
537
538 MI.eraseFromParent(); // The pseudo instruction is gone now.
539 return TailMBB;
540}
541
Alex Bradbury89718422017-10-19 21:37:38 +0000542// Calling Convention Implementation.
Alex Bradburydc31c612017-12-11 12:49:02 +0000543// The expectations for frontend ABI lowering vary from target to target.
544// Ideally, an LLVM frontend would be able to avoid worrying about many ABI
545// details, but this is a longer term goal. For now, we simply try to keep the
546// role of the frontend as simple and well-defined as possible. The rules can
547// be summarised as:
548// * Never split up large scalar arguments. We handle them here.
549// * If a hardfloat calling convention is being used, and the struct may be
550// passed in a pair of registers (fp+fp, int+fp), and both registers are
551// available, then pass as two separate arguments. If either the GPRs or FPRs
552// are exhausted, then pass according to the rule below.
553// * If a struct could never be passed in registers or directly in a stack
554// slot (as it is larger than 2*XLEN and the floating point rules don't
555// apply), then pass it using a pointer with the byval attribute.
556// * If a struct is less than 2*XLEN, then coerce to either a two-element
557// word-sized array or a 2*XLEN scalar (depending on alignment).
558// * The frontend can determine whether a struct is returned by reference or
559// not based on its size and fields. If it will be returned by reference, the
560// frontend must modify the prototype so a pointer with the sret annotation is
561// passed as the first argument. This is not necessary for large scalar
562// returns.
563// * Struct return values and varargs should be coerced to structs containing
564// register-size fields in the same situations they would be for fixed
565// arguments.
566
567static const MCPhysReg ArgGPRs[] = {
568 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
569 RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
570};
571
572// Pass a 2*XLEN argument that has been split into two XLEN values through
573// registers or the stack as necessary.
574static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
575 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
576 MVT ValVT2, MVT LocVT2,
577 ISD::ArgFlagsTy ArgFlags2) {
578 unsigned XLenInBytes = XLen / 8;
579 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
580 // At least one half can be passed via register.
581 State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
582 VA1.getLocVT(), CCValAssign::Full));
583 } else {
584 // Both halves must be passed on the stack, with proper alignment.
585 unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign());
586 State.addLoc(
587 CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
588 State.AllocateStack(XLenInBytes, StackAlign),
589 VA1.getLocVT(), CCValAssign::Full));
590 State.addLoc(CCValAssign::getMem(
591 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
592 CCValAssign::Full));
593 return false;
594 }
595
596 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
597 // The second half can also be passed via register.
598 State.addLoc(
599 CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
600 } else {
601 // The second half is passed via the stack, without additional alignment.
602 State.addLoc(CCValAssign::getMem(
603 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
604 CCValAssign::Full));
605 }
606
607 return false;
608}
609
610// Implements the RISC-V calling convention. Returns true upon failure.
611static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT,
612 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000613 CCState &State, bool IsFixed, bool IsRet, Type *OrigTy) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000614 unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
615 assert(XLen == 32 || XLen == 64);
616 MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000617 if (ValVT == MVT::f32) {
618 LocVT = MVT::i32;
619 LocInfo = CCValAssign::BCvt;
620 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000621
622 // Any return value split in to more than two values can't be returned
623 // directly.
624 if (IsRet && ValNo > 1)
625 return true;
626
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000627 // If this is a variadic argument, the RISC-V calling convention requires
628 // that it is assigned an 'even' or 'aligned' register if it has 8-byte
629 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
630 // be used regardless of whether the original argument was split during
631 // legalisation or not. The argument will not be passed by registers if the
632 // original type is larger than 2*XLEN, so the register alignment rule does
633 // not apply.
634 unsigned TwoXLenInBytes = (2 * XLen) / 8;
635 if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes &&
636 DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
637 unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
638 // Skip 'odd' register if necessary.
639 if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
640 State.AllocateReg(ArgGPRs);
641 }
642
Alex Bradburydc31c612017-12-11 12:49:02 +0000643 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
644 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
645 State.getPendingArgFlags();
646
647 assert(PendingLocs.size() == PendingArgFlags.size() &&
648 "PendingLocs and PendingArgFlags out of sync");
649
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000650 // Handle passing f64 on RV32D with a soft float ABI.
651 if (XLen == 32 && ValVT == MVT::f64) {
652 assert(!ArgFlags.isSplit() && PendingLocs.empty() ||
653 "Can't lower f64 if it is split");
654 // Depending on available argument GPRS, f64 may be passed in a pair of
655 // GPRs, split between a GPR and the stack, or passed completely on the
656 // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
657 // cases.
658 unsigned Reg = State.AllocateReg(ArgGPRs);
659 LocVT = MVT::i32;
660 if (!Reg) {
661 unsigned StackOffset = State.AllocateStack(8, 8);
662 State.addLoc(
663 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
664 return false;
665 }
666 if (!State.AllocateReg(ArgGPRs))
667 State.AllocateStack(4, 4);
668 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
669 return false;
670 }
671
Alex Bradburydc31c612017-12-11 12:49:02 +0000672 // Split arguments might be passed indirectly, so keep track of the pending
673 // values.
674 if (ArgFlags.isSplit() || !PendingLocs.empty()) {
675 LocVT = XLenVT;
676 LocInfo = CCValAssign::Indirect;
677 PendingLocs.push_back(
678 CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
679 PendingArgFlags.push_back(ArgFlags);
680 if (!ArgFlags.isSplitEnd()) {
681 return false;
682 }
683 }
684
685 // If the split argument only had two elements, it should be passed directly
686 // in registers or on the stack.
687 if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
688 assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
689 // Apply the normal calling convention rules to the first half of the
690 // split argument.
691 CCValAssign VA = PendingLocs[0];
692 ISD::ArgFlagsTy AF = PendingArgFlags[0];
693 PendingLocs.clear();
694 PendingArgFlags.clear();
695 return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
696 ArgFlags);
697 }
698
699 // Allocate to a register if possible, or else a stack slot.
700 unsigned Reg = State.AllocateReg(ArgGPRs);
701 unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8);
702
703 // If we reach this point and PendingLocs is non-empty, we must be at the
704 // end of a split argument that must be passed indirectly.
705 if (!PendingLocs.empty()) {
706 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
707 assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
708
709 for (auto &It : PendingLocs) {
710 if (Reg)
711 It.convertToReg(Reg);
712 else
713 It.convertToMem(StackOffset);
714 State.addLoc(It);
715 }
716 PendingLocs.clear();
717 PendingArgFlags.clear();
718 return false;
719 }
720
721 assert(LocVT == XLenVT && "Expected an XLenVT at this stage");
722
723 if (Reg) {
724 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
725 } else {
726 State.addLoc(
727 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
728 }
729 return false;
730}
731
732void RISCVTargetLowering::analyzeInputArgs(
733 MachineFunction &MF, CCState &CCInfo,
734 const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
735 unsigned NumArgs = Ins.size();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000736 FunctionType *FType = MF.getFunction().getFunctionType();
Alex Bradburydc31c612017-12-11 12:49:02 +0000737
738 for (unsigned i = 0; i != NumArgs; ++i) {
739 MVT ArgVT = Ins[i].VT;
740 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
741
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000742 Type *ArgTy = nullptr;
743 if (IsRet)
744 ArgTy = FType->getReturnType();
745 else if (Ins[i].isOrigArg())
746 ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
747
Alex Bradburydc31c612017-12-11 12:49:02 +0000748 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000749 ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000750 DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
751 << EVT(ArgVT).getEVTString() << '\n');
752 llvm_unreachable(nullptr);
753 }
754 }
755}
756
757void RISCVTargetLowering::analyzeOutputArgs(
758 MachineFunction &MF, CCState &CCInfo,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000759 const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
760 CallLoweringInfo *CLI) const {
Alex Bradburydc31c612017-12-11 12:49:02 +0000761 unsigned NumArgs = Outs.size();
762
763 for (unsigned i = 0; i != NumArgs; i++) {
764 MVT ArgVT = Outs[i].VT;
765 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000766 Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
Alex Bradburydc31c612017-12-11 12:49:02 +0000767
768 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000769 ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000770 DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
771 << EVT(ArgVT).getEVTString() << "\n");
772 llvm_unreachable(nullptr);
773 }
774 }
775}
776
777// The caller is responsible for loading the full value if the argument is
778// passed with CCValAssign::Indirect.
779static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
780 const CCValAssign &VA, const SDLoc &DL) {
781 MachineFunction &MF = DAG.getMachineFunction();
782 MachineRegisterInfo &RegInfo = MF.getRegInfo();
783 EVT LocVT = VA.getLocVT();
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000784 EVT ValVT = VA.getValVT();
Alex Bradburydc31c612017-12-11 12:49:02 +0000785 SDValue Val;
786
787 unsigned VReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
788 RegInfo.addLiveIn(VA.getLocReg(), VReg);
789 Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
790
791 switch (VA.getLocInfo()) {
792 default:
793 llvm_unreachable("Unexpected CCValAssign::LocInfo");
794 case CCValAssign::Full:
795 case CCValAssign::Indirect:
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000796 break;
797 case CCValAssign::BCvt:
798 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
799 break;
Alex Bradburydc31c612017-12-11 12:49:02 +0000800 }
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000801 return Val;
Alex Bradburydc31c612017-12-11 12:49:02 +0000802}
803
804// The caller is responsible for loading the full value if the argument is
805// passed with CCValAssign::Indirect.
806static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
807 const CCValAssign &VA, const SDLoc &DL) {
808 MachineFunction &MF = DAG.getMachineFunction();
809 MachineFrameInfo &MFI = MF.getFrameInfo();
810 EVT LocVT = VA.getLocVT();
811 EVT ValVT = VA.getValVT();
812 EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
813 int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
814 VA.getLocMemOffset(), /*Immutable=*/true);
815 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
816 SDValue Val;
817
818 ISD::LoadExtType ExtType;
819 switch (VA.getLocInfo()) {
820 default:
821 llvm_unreachable("Unexpected CCValAssign::LocInfo");
822 case CCValAssign::Full:
823 case CCValAssign::Indirect:
824 ExtType = ISD::NON_EXTLOAD;
825 break;
826 }
827 Val = DAG.getExtLoad(
828 ExtType, DL, LocVT, Chain, FIN,
829 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
830 return Val;
831}
Alex Bradbury89718422017-10-19 21:37:38 +0000832
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000833static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
834 const CCValAssign &VA, const SDLoc &DL) {
835 assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
836 "Unexpected VA");
837 MachineFunction &MF = DAG.getMachineFunction();
838 MachineFrameInfo &MFI = MF.getFrameInfo();
839 MachineRegisterInfo &RegInfo = MF.getRegInfo();
840
841 if (VA.isMemLoc()) {
842 // f64 is passed on the stack.
843 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
844 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
845 return DAG.getLoad(MVT::f64, DL, Chain, FIN,
846 MachinePointerInfo::getFixedStack(MF, FI));
847 }
848
849 assert(VA.isRegLoc() && "Expected register VA assignment");
850
851 unsigned LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
852 RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
853 SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
854 SDValue Hi;
855 if (VA.getLocReg() == RISCV::X17) {
856 // Second half of f64 is passed on the stack.
857 int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
858 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
859 Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
860 MachinePointerInfo::getFixedStack(MF, FI));
861 } else {
862 // Second half of f64 is passed in another GPR.
863 unsigned HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
864 RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
865 Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
866 }
867 return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
868}
869
Alex Bradbury89718422017-10-19 21:37:38 +0000870// Transform physical registers into virtual registers.
871SDValue RISCVTargetLowering::LowerFormalArguments(
872 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
873 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
874 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
875
876 switch (CallConv) {
877 default:
878 report_fatal_error("Unsupported calling convention");
879 case CallingConv::C:
Alex Bradburya3376752017-11-08 13:41:21 +0000880 case CallingConv::Fast:
Alex Bradbury89718422017-10-19 21:37:38 +0000881 break;
882 }
883
884 MachineFunction &MF = DAG.getMachineFunction();
Alex Bradburydc31c612017-12-11 12:49:02 +0000885 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000886 MVT XLenVT = Subtarget.getXLenVT();
887 unsigned XLenInBytes = Subtarget.getXLen() / 8;
888 // Used with vargs to acumulate store chains.
889 std::vector<SDValue> OutChains;
Alex Bradbury89718422017-10-19 21:37:38 +0000890
891 // Assign locations to all of the incoming arguments.
892 SmallVector<CCValAssign, 16> ArgLocs;
893 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +0000894 analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
Alex Bradbury89718422017-10-19 21:37:38 +0000895
Alex Bradburydc31c612017-12-11 12:49:02 +0000896 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
897 CCValAssign &VA = ArgLocs[i];
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000898 assert(VA.getLocVT() == XLenVT && "Unhandled argument type");
Alex Bradburydc31c612017-12-11 12:49:02 +0000899 SDValue ArgValue;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000900 // Passing f64 on RV32D with a soft float ABI must be handled as a special
901 // case.
902 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
903 ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
904 else if (VA.isRegLoc())
Alex Bradburydc31c612017-12-11 12:49:02 +0000905 ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL);
906 else
907 ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
Alex Bradbury89718422017-10-19 21:37:38 +0000908
Alex Bradburydc31c612017-12-11 12:49:02 +0000909 if (VA.getLocInfo() == CCValAssign::Indirect) {
910 // If the original argument was split and passed by reference (e.g. i128
911 // on RV32), we need to load all parts of it here (using the same
912 // address).
913 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
914 MachinePointerInfo()));
915 unsigned ArgIndex = Ins[i].OrigArgIndex;
916 assert(Ins[i].PartOffset == 0);
917 while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
918 CCValAssign &PartVA = ArgLocs[i + 1];
919 unsigned PartOffset = Ins[i + 1].PartOffset;
920 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
921 DAG.getIntPtrConstant(PartOffset, DL));
922 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
923 MachinePointerInfo()));
924 ++i;
925 }
926 continue;
Alex Bradbury89718422017-10-19 21:37:38 +0000927 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000928 InVals.push_back(ArgValue);
Alex Bradbury89718422017-10-19 21:37:38 +0000929 }
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000930
931 if (IsVarArg) {
932 ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
933 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
934 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
935 MachineFrameInfo &MFI = MF.getFrameInfo();
936 MachineRegisterInfo &RegInfo = MF.getRegInfo();
937 RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
938
939 // Offset of the first variable argument from stack pointer, and size of
940 // the vararg save area. For now, the varargs save area is either zero or
941 // large enough to hold a0-a7.
942 int VaArgOffset, VarArgsSaveSize;
943
944 // If all registers are allocated, then all varargs must be passed on the
945 // stack and we don't need to save any argregs.
946 if (ArgRegs.size() == Idx) {
947 VaArgOffset = CCInfo.getNextStackOffset();
948 VarArgsSaveSize = 0;
949 } else {
950 VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
951 VaArgOffset = -VarArgsSaveSize;
952 }
953
954 // Record the frame index of the first variable argument
955 // which is a value necessary to VASTART.
956 int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
957 RVFI->setVarArgsFrameIndex(FI);
958
959 // If saving an odd number of registers then create an extra stack slot to
960 // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
961 // offsets to even-numbered registered remain 2*XLEN-aligned.
962 if (Idx % 2) {
963 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes,
964 true);
965 VarArgsSaveSize += XLenInBytes;
966 }
967
968 // Copy the integer registers that may have been used for passing varargs
969 // to the vararg save area.
970 for (unsigned I = Idx; I < ArgRegs.size();
971 ++I, VaArgOffset += XLenInBytes) {
972 const unsigned Reg = RegInfo.createVirtualRegister(RC);
973 RegInfo.addLiveIn(ArgRegs[I], Reg);
974 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
975 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
976 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
977 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
978 MachinePointerInfo::getFixedStack(MF, FI));
979 cast<StoreSDNode>(Store.getNode())
980 ->getMemOperand()
981 ->setValue((Value *)nullptr);
982 OutChains.push_back(Store);
983 }
984 RVFI->setVarArgsSaveSize(VarArgsSaveSize);
985 }
986
987 // All stores are grouped in one node to allow the matching between
988 // the size of Ins and InVals. This only happens for vararg functions.
989 if (!OutChains.empty()) {
990 OutChains.push_back(Chain);
991 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
992 }
993
Alex Bradbury89718422017-10-19 21:37:38 +0000994 return Chain;
995}
996
Alex Bradburya3376752017-11-08 13:41:21 +0000997// Lower a call to a callseq_start + CALL + callseq_end chain, and add input
998// and output parameter nodes.
999SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1000 SmallVectorImpl<SDValue> &InVals) const {
1001 SelectionDAG &DAG = CLI.DAG;
1002 SDLoc &DL = CLI.DL;
1003 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1004 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1005 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1006 SDValue Chain = CLI.Chain;
1007 SDValue Callee = CLI.Callee;
1008 CLI.IsTailCall = false;
1009 CallingConv::ID CallConv = CLI.CallConv;
1010 bool IsVarArg = CLI.IsVarArg;
1011 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburydc31c612017-12-11 12:49:02 +00001012 MVT XLenVT = Subtarget.getXLenVT();
Alex Bradburya3376752017-11-08 13:41:21 +00001013
Alex Bradburya3376752017-11-08 13:41:21 +00001014 MachineFunction &MF = DAG.getMachineFunction();
1015
1016 // Analyze the operands of the call, assigning locations to each operand.
1017 SmallVector<CCValAssign, 16> ArgLocs;
1018 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001019 analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
Alex Bradburya3376752017-11-08 13:41:21 +00001020
1021 // Get a count of how many bytes are to be pushed on the stack.
1022 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1023
Alex Bradburydc31c612017-12-11 12:49:02 +00001024 // Create local copies for byval args
1025 SmallVector<SDValue, 8> ByValArgs;
1026 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1027 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1028 if (!Flags.isByVal())
Alex Bradburya3376752017-11-08 13:41:21 +00001029 continue;
Alex Bradburydc31c612017-12-11 12:49:02 +00001030
1031 SDValue Arg = OutVals[i];
1032 unsigned Size = Flags.getByValSize();
1033 unsigned Align = Flags.getByValAlign();
1034
1035 int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false);
1036 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1037 SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
1038
1039 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
1040 /*IsVolatile=*/false,
1041 /*AlwaysInline=*/false,
1042 /*isTailCall=*/false, MachinePointerInfo(),
1043 MachinePointerInfo());
1044 ByValArgs.push_back(FIPtr);
Alex Bradburya3376752017-11-08 13:41:21 +00001045 }
1046
1047 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
1048
1049 // Copy argument values to their designated locations.
1050 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Alex Bradburydc31c612017-12-11 12:49:02 +00001051 SmallVector<SDValue, 8> MemOpChains;
Alex Bradburya3376752017-11-08 13:41:21 +00001052 SDValue StackPtr;
Alex Bradburydc31c612017-12-11 12:49:02 +00001053 for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
1054 CCValAssign &VA = ArgLocs[i];
1055 SDValue ArgValue = OutVals[i];
1056 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Alex Bradburya3376752017-11-08 13:41:21 +00001057
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001058 // Handle passing f64 on RV32D with a soft float ABI as a special case.
1059 bool IsF64OnRV32DSoftABI =
1060 VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
1061 if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
1062 SDValue SplitF64 = DAG.getNode(
1063 RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
1064 SDValue Lo = SplitF64.getValue(0);
1065 SDValue Hi = SplitF64.getValue(1);
1066
1067 unsigned RegLo = VA.getLocReg();
1068 RegsToPass.push_back(std::make_pair(RegLo, Lo));
1069
1070 if (RegLo == RISCV::X17) {
1071 // Second half of f64 is passed on the stack.
1072 // Work out the address of the stack slot.
1073 if (!StackPtr.getNode())
1074 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1075 // Emit the store.
1076 MemOpChains.push_back(
1077 DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
1078 } else {
1079 // Second half of f64 is passed in another GPR.
1080 unsigned RegHigh = RegLo + 1;
1081 RegsToPass.push_back(std::make_pair(RegHigh, Hi));
1082 }
1083 continue;
1084 }
1085
1086 // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
1087 // as any other MemLoc.
1088
Alex Bradburya3376752017-11-08 13:41:21 +00001089 // Promote the value if needed.
Alex Bradburydc31c612017-12-11 12:49:02 +00001090 // For now, only handle fully promoted and indirect arguments.
Alex Bradburya3376752017-11-08 13:41:21 +00001091 switch (VA.getLocInfo()) {
1092 case CCValAssign::Full:
1093 break;
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001094 case CCValAssign::BCvt:
1095 ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), ArgValue);
1096 break;
Alex Bradburydc31c612017-12-11 12:49:02 +00001097 case CCValAssign::Indirect: {
1098 // Store the argument in a stack slot and pass its address.
1099 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
1100 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1101 MemOpChains.push_back(
1102 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1103 MachinePointerInfo::getFixedStack(MF, FI)));
1104 // If the original argument was split (e.g. i128), we need
1105 // to store all parts of it here (and pass just one address).
1106 unsigned ArgIndex = Outs[i].OrigArgIndex;
1107 assert(Outs[i].PartOffset == 0);
1108 while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
1109 SDValue PartValue = OutVals[i + 1];
1110 unsigned PartOffset = Outs[i + 1].PartOffset;
1111 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1112 DAG.getIntPtrConstant(PartOffset, DL));
1113 MemOpChains.push_back(
1114 DAG.getStore(Chain, DL, PartValue, Address,
1115 MachinePointerInfo::getFixedStack(MF, FI)));
1116 ++i;
1117 }
1118 ArgValue = SpillSlot;
1119 break;
1120 }
Alex Bradburya3376752017-11-08 13:41:21 +00001121 default:
1122 llvm_unreachable("Unknown loc info!");
1123 }
1124
Alex Bradburydc31c612017-12-11 12:49:02 +00001125 // Use local copy if it is a byval arg.
1126 if (Flags.isByVal())
1127 ArgValue = ByValArgs[j++];
1128
Alex Bradburya3376752017-11-08 13:41:21 +00001129 if (VA.isRegLoc()) {
1130 // Queue up the argument copies and emit them at the end.
1131 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1132 } else {
1133 assert(VA.isMemLoc() && "Argument not register or memory");
Alex Bradburydc31c612017-12-11 12:49:02 +00001134
1135 // Work out the address of the stack slot.
1136 if (!StackPtr.getNode())
1137 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1138 SDValue Address =
1139 DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
1140 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
1141
1142 // Emit the store.
1143 MemOpChains.push_back(
1144 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Alex Bradburya3376752017-11-08 13:41:21 +00001145 }
1146 }
1147
Alex Bradburydc31c612017-12-11 12:49:02 +00001148 // Join the stores, which are independent of one another.
1149 if (!MemOpChains.empty())
1150 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
1151
Alex Bradburya3376752017-11-08 13:41:21 +00001152 SDValue Glue;
1153
1154 // Build a sequence of copy-to-reg nodes, chained and glued together.
1155 for (auto &Reg : RegsToPass) {
1156 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
1157 Glue = Chain.getValue(1);
1158 }
1159
1160 if (isa<GlobalAddressSDNode>(Callee)) {
1161 Callee = lowerGlobalAddress(Callee, DAG);
1162 } else if (isa<ExternalSymbolSDNode>(Callee)) {
Alex Bradburyffc435e2017-11-21 08:11:03 +00001163 Callee = lowerExternalSymbol(Callee, DAG);
Alex Bradburya3376752017-11-08 13:41:21 +00001164 }
1165
1166 // The first call operand is the chain and the second is the target address.
1167 SmallVector<SDValue, 8> Ops;
1168 Ops.push_back(Chain);
1169 Ops.push_back(Callee);
1170
1171 // Add argument registers to the end of the list so that they are
1172 // known live into the call.
1173 for (auto &Reg : RegsToPass)
1174 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
1175
1176 // Add a register mask operand representing the call-preserved registers.
1177 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1178 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
1179 assert(Mask && "Missing call preserved mask for calling convention");
1180 Ops.push_back(DAG.getRegisterMask(Mask));
1181
1182 // Glue the call to the argument copies, if any.
1183 if (Glue.getNode())
1184 Ops.push_back(Glue);
1185
1186 // Emit the call.
1187 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1188 Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
1189 Glue = Chain.getValue(1);
1190
1191 // Mark the end of the call, which is glued to the call itself.
1192 Chain = DAG.getCALLSEQ_END(Chain,
1193 DAG.getConstant(NumBytes, DL, PtrVT, true),
1194 DAG.getConstant(0, DL, PtrVT, true),
1195 Glue, DL);
1196 Glue = Chain.getValue(1);
1197
1198 // Assign locations to each value returned by this call.
1199 SmallVector<CCValAssign, 16> RVLocs;
1200 CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +00001201 analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
Alex Bradburya3376752017-11-08 13:41:21 +00001202
1203 // Copy all of the result registers out of their specified physreg.
1204 for (auto &VA : RVLocs) {
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001205 // Copy the value out
1206 SDValue RetValue =
1207 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
1208 // Glue the RetValue to the end of the call sequence
Alex Bradburya3376752017-11-08 13:41:21 +00001209 Chain = RetValue.getValue(1);
1210 Glue = RetValue.getValue(2);
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001211 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1212 assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
1213 SDValue RetValue2 =
1214 DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
1215 Chain = RetValue2.getValue(1);
1216 Glue = RetValue2.getValue(2);
1217 RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
1218 RetValue2);
1219 }
Alex Bradburya3376752017-11-08 13:41:21 +00001220
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001221 switch (VA.getLocInfo()) {
1222 default:
1223 llvm_unreachable("Unknown loc info!");
1224 case CCValAssign::Full:
1225 break;
1226 case CCValAssign::BCvt:
1227 RetValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), RetValue);
1228 break;
1229 }
1230
Alex Bradburydc31c612017-12-11 12:49:02 +00001231 InVals.push_back(RetValue);
Alex Bradburya3376752017-11-08 13:41:21 +00001232 }
1233
1234 return Chain;
1235}
1236
Alex Bradburydc31c612017-12-11 12:49:02 +00001237bool RISCVTargetLowering::CanLowerReturn(
1238 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
1239 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
1240 SmallVector<CCValAssign, 16> RVLocs;
1241 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
1242 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1243 MVT VT = Outs[i].VT;
1244 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1245 if (CC_RISCV(MF.getDataLayout(), i, VT, VT, CCValAssign::Full, ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001246 CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr))
Alex Bradburydc31c612017-12-11 12:49:02 +00001247 return false;
1248 }
1249 return true;
1250}
1251
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001252static SDValue packIntoRegLoc(SelectionDAG &DAG, SDValue Val,
1253 const CCValAssign &VA, const SDLoc &DL) {
1254 EVT LocVT = VA.getLocVT();
1255
1256 switch (VA.getLocInfo()) {
1257 default:
1258 llvm_unreachable("Unexpected CCValAssign::LocInfo");
1259 case CCValAssign::Full:
1260 break;
1261 case CCValAssign::BCvt:
1262 Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
1263 break;
1264 }
1265 return Val;
1266}
1267
Alex Bradbury89718422017-10-19 21:37:38 +00001268SDValue
1269RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1270 bool IsVarArg,
1271 const SmallVectorImpl<ISD::OutputArg> &Outs,
1272 const SmallVectorImpl<SDValue> &OutVals,
1273 const SDLoc &DL, SelectionDAG &DAG) const {
Alex Bradbury89718422017-10-19 21:37:38 +00001274 // Stores the assignment of the return value to a location.
1275 SmallVector<CCValAssign, 16> RVLocs;
1276
1277 // Info about the registers and stack slot.
1278 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
1279 *DAG.getContext());
1280
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001281 analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
1282 nullptr);
Alex Bradbury89718422017-10-19 21:37:38 +00001283
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001284 SDValue Glue;
Alex Bradbury89718422017-10-19 21:37:38 +00001285 SmallVector<SDValue, 4> RetOps(1, Chain);
1286
1287 // Copy the result values into the output registers.
1288 for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
Alex Bradburydc31c612017-12-11 12:49:02 +00001289 SDValue Val = OutVals[i];
Alex Bradbury89718422017-10-19 21:37:38 +00001290 CCValAssign &VA = RVLocs[i];
1291 assert(VA.isRegLoc() && "Can only return in registers!");
1292
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001293 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1294 // Handle returning f64 on RV32D with a soft float ABI.
1295 assert(VA.isRegLoc() && "Expected return via registers");
1296 SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
1297 DAG.getVTList(MVT::i32, MVT::i32), Val);
1298 SDValue Lo = SplitF64.getValue(0);
1299 SDValue Hi = SplitF64.getValue(1);
1300 unsigned RegLo = VA.getLocReg();
1301 unsigned RegHi = RegLo + 1;
1302 Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
1303 Glue = Chain.getValue(1);
1304 RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
1305 Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
1306 Glue = Chain.getValue(1);
1307 RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
1308 } else {
1309 // Handle a 'normal' return.
1310 Val = packIntoRegLoc(DAG, Val, VA, DL);
1311 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001312
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001313 // Guarantee that all emitted copies are stuck together.
1314 Glue = Chain.getValue(1);
1315 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1316 }
Alex Bradbury89718422017-10-19 21:37:38 +00001317 }
1318
1319 RetOps[0] = Chain; // Update chain.
1320
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001321 // Add the glue node if we have it.
1322 if (Glue.getNode()) {
1323 RetOps.push_back(Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001324 }
1325
1326 return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
1327}
1328
1329const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
1330 switch ((RISCVISD::NodeType)Opcode) {
1331 case RISCVISD::FIRST_NUMBER:
1332 break;
1333 case RISCVISD::RET_FLAG:
1334 return "RISCVISD::RET_FLAG";
Alex Bradburya3376752017-11-08 13:41:21 +00001335 case RISCVISD::CALL:
1336 return "RISCVISD::CALL";
Alex Bradbury65385162017-11-21 07:51:32 +00001337 case RISCVISD::SELECT_CC:
1338 return "RISCVISD::SELECT_CC";
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001339 case RISCVISD::BuildPairF64:
1340 return "RISCVISD::BuildPairF64";
1341 case RISCVISD::SplitF64:
1342 return "RISCVISD::SplitF64";
Alex Bradbury89718422017-10-19 21:37:38 +00001343 }
1344 return nullptr;
1345}
Alex Bradbury9330e642018-01-10 20:05:09 +00001346
1347std::pair<unsigned, const TargetRegisterClass *>
1348RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1349 StringRef Constraint,
1350 MVT VT) const {
1351 // First, see if this is a constraint that directly corresponds to a
1352 // RISCV register class.
1353 if (Constraint.size() == 1) {
1354 switch (Constraint[0]) {
1355 case 'r':
1356 return std::make_pair(0U, &RISCV::GPRRegClass);
1357 default:
1358 break;
1359 }
1360 }
1361
1362 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1363}