blob: ead9f0bdc632c054110cecb019d237d031976b78 [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 Bradburydcbff632018-04-26 13:15:17 +0000186bool RISCVTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
187 return isInt<12>(Imm);
188}
189
Alex Bradbury5c41ece2018-04-26 13:00:37 +0000190bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const {
191 return isInt<12>(Imm);
192}
193
Alex Bradbury130b8b32018-04-26 13:37:00 +0000194// On RV32, 64-bit integers are split into their high and low parts and held
195// in two different registers, so the trunc is free since the low register can
196// just be used.
197bool RISCVTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const {
198 if (Subtarget.is64Bit() || !SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
199 return false;
200 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();
201 unsigned DestBits = DstTy->getPrimitiveSizeInBits();
202 return (SrcBits == 64 && DestBits == 32);
203}
204
205bool RISCVTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const {
206 if (Subtarget.is64Bit() || SrcVT.isVector() || DstVT.isVector() ||
207 !SrcVT.isInteger() || !DstVT.isInteger())
208 return false;
209 unsigned SrcBits = SrcVT.getSizeInBits();
210 unsigned DestBits = DstVT.getSizeInBits();
211 return (SrcBits == 64 && DestBits == 32);
212}
213
Alex Bradbury15e894b2018-04-26 14:04:18 +0000214bool RISCVTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
215 // Zexts are free if they can be combined with a load.
216 if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
217 EVT MemVT = LD->getMemoryVT();
218 if ((MemVT == MVT::i8 || MemVT == MVT::i16 ||
219 (Subtarget.is64Bit() && MemVT == MVT::i32)) &&
220 (LD->getExtensionType() == ISD::NON_EXTLOAD ||
221 LD->getExtensionType() == ISD::ZEXTLOAD))
222 return true;
223 }
224
225 return TargetLowering::isZExtFree(Val, VT2);
226}
227
Alex Bradbury65385162017-11-21 07:51:32 +0000228// Changes the condition code and swaps operands if necessary, so the SetCC
229// operation matches one of the comparisons supported directly in the RISC-V
230// ISA.
231static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
232 switch (CC) {
233 default:
234 break;
235 case ISD::SETGT:
236 case ISD::SETLE:
237 case ISD::SETUGT:
238 case ISD::SETULE:
239 CC = ISD::getSetCCSwappedOperands(CC);
240 std::swap(LHS, RHS);
241 break;
242 }
243}
244
245// Return the RISC-V branch opcode that matches the given DAG integer
246// condition code. The CondCode must be one of those supported by the RISC-V
247// ISA (see normaliseSetCC).
248static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
249 switch (CC) {
250 default:
251 llvm_unreachable("Unsupported CondCode");
252 case ISD::SETEQ:
253 return RISCV::BEQ;
254 case ISD::SETNE:
255 return RISCV::BNE;
256 case ISD::SETLT:
257 return RISCV::BLT;
258 case ISD::SETGE:
259 return RISCV::BGE;
260 case ISD::SETULT:
261 return RISCV::BLTU;
262 case ISD::SETUGE:
263 return RISCV::BGEU;
264 }
265}
266
Alex Bradbury89718422017-10-19 21:37:38 +0000267SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
268 SelectionDAG &DAG) const {
269 switch (Op.getOpcode()) {
270 default:
271 report_fatal_error("unimplemented operand");
Alex Bradburyec8aa912017-11-08 13:24:21 +0000272 case ISD::GlobalAddress:
273 return lowerGlobalAddress(Op, DAG);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000274 case ISD::BlockAddress:
275 return lowerBlockAddress(Op, DAG);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000276 case ISD::ConstantPool:
277 return lowerConstantPool(Op, DAG);
Alex Bradbury65385162017-11-21 07:51:32 +0000278 case ISD::SELECT:
279 return lowerSELECT(Op, DAG);
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000280 case ISD::VASTART:
281 return lowerVASTART(Op, DAG);
Alex Bradbury70f137b2018-01-10 20:12:00 +0000282 case ISD::FRAMEADDR:
283 return LowerFRAMEADDR(Op, DAG);
284 case ISD::RETURNADDR:
285 return LowerRETURNADDR(Op, DAG);
Alex Bradburyec8aa912017-11-08 13:24:21 +0000286 }
287}
288
289SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
290 SelectionDAG &DAG) const {
291 SDLoc DL(Op);
292 EVT Ty = Op.getValueType();
293 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
294 const GlobalValue *GV = N->getGlobal();
295 int64_t Offset = N->getOffset();
296
Alex Bradburyffc435e2017-11-21 08:11:03 +0000297 if (isPositionIndependent() || Subtarget.is64Bit())
Alex Bradburyec8aa912017-11-08 13:24:21 +0000298 report_fatal_error("Unable to lowerGlobalAddress");
Alex Bradburyffc435e2017-11-21 08:11:03 +0000299
300 SDValue GAHi =
301 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_HI);
302 SDValue GALo =
303 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_LO);
304 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
305 SDValue MNLo =
306 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
307 return MNLo;
308}
309
310SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
311 SelectionDAG &DAG) const {
312 SDLoc DL(Op);
313 EVT Ty = Op.getValueType();
314 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
315 const BlockAddress *BA = N->getBlockAddress();
316 int64_t Offset = N->getOffset();
317
318 if (isPositionIndependent() || Subtarget.is64Bit())
319 report_fatal_error("Unable to lowerBlockAddress");
320
321 SDValue BAHi = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_HI);
322 SDValue BALo = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_LO);
323 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, BAHi), 0);
324 SDValue MNLo =
325 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, BALo), 0);
326 return MNLo;
327}
328
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000329SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
330 SelectionDAG &DAG) const {
331 SDLoc DL(Op);
332 EVT Ty = Op.getValueType();
333 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
334 const Constant *CPA = N->getConstVal();
335 int64_t Offset = N->getOffset();
336 unsigned Alignment = N->getAlignment();
337
338 if (!isPositionIndependent()) {
339 SDValue CPAHi =
340 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_HI);
341 SDValue CPALo =
342 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_LO);
343 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, CPAHi), 0);
344 SDValue MNLo =
345 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, CPALo), 0);
346 return MNLo;
347 } else {
348 report_fatal_error("Unable to lowerConstantPool");
349 }
350}
351
Alex Bradburyffc435e2017-11-21 08:11:03 +0000352SDValue RISCVTargetLowering::lowerExternalSymbol(SDValue Op,
353 SelectionDAG &DAG) const {
354 SDLoc DL(Op);
355 EVT Ty = Op.getValueType();
356 ExternalSymbolSDNode *N = cast<ExternalSymbolSDNode>(Op);
357 const char *Sym = N->getSymbol();
358
359 // TODO: should also handle gp-relative loads.
360
361 if (isPositionIndependent() || Subtarget.is64Bit())
362 report_fatal_error("Unable to lowerExternalSymbol");
363
364 SDValue GAHi = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_HI);
365 SDValue GALo = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_LO);
366 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
367 SDValue MNLo =
368 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
369 return MNLo;
Alex Bradbury89718422017-10-19 21:37:38 +0000370}
371
Alex Bradbury65385162017-11-21 07:51:32 +0000372SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
373 SDValue CondV = Op.getOperand(0);
374 SDValue TrueV = Op.getOperand(1);
375 SDValue FalseV = Op.getOperand(2);
376 SDLoc DL(Op);
377 MVT XLenVT = Subtarget.getXLenVT();
378
379 // If the result type is XLenVT and CondV is the output of a SETCC node
380 // which also operated on XLenVT inputs, then merge the SETCC node into the
381 // lowered RISCVISD::SELECT_CC to take advantage of the integer
382 // compare+branch instructions. i.e.:
383 // (select (setcc lhs, rhs, cc), truev, falsev)
384 // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
385 if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
386 CondV.getOperand(0).getSimpleValueType() == XLenVT) {
387 SDValue LHS = CondV.getOperand(0);
388 SDValue RHS = CondV.getOperand(1);
389 auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
390 ISD::CondCode CCVal = CC->get();
391
392 normaliseSetCC(LHS, RHS, CCVal);
393
394 SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
395 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
396 SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
397 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
398 }
399
400 // Otherwise:
401 // (select condv, truev, falsev)
402 // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
403 SDValue Zero = DAG.getConstant(0, DL, XLenVT);
404 SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
405
406 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
407 SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
408
409 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
410}
411
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000412SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
413 MachineFunction &MF = DAG.getMachineFunction();
414 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
415
416 SDLoc DL(Op);
417 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
418 getPointerTy(MF.getDataLayout()));
419
420 // vastart just stores the address of the VarArgsFrameIndex slot into the
421 // memory location argument.
422 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
423 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
424 MachinePointerInfo(SV));
425}
426
Alex Bradbury70f137b2018-01-10 20:12:00 +0000427SDValue RISCVTargetLowering::LowerFRAMEADDR(SDValue Op,
428 SelectionDAG &DAG) const {
429 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
430 MachineFunction &MF = DAG.getMachineFunction();
431 MachineFrameInfo &MFI = MF.getFrameInfo();
432 MFI.setFrameAddressIsTaken(true);
433 unsigned FrameReg = RI.getFrameRegister(MF);
434 int XLenInBytes = Subtarget.getXLen() / 8;
435
436 EVT VT = Op.getValueType();
437 SDLoc DL(Op);
438 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
439 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
440 while (Depth--) {
441 int Offset = -(XLenInBytes * 2);
442 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
443 DAG.getIntPtrConstant(Offset, DL));
444 FrameAddr =
445 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
446 }
447 return FrameAddr;
448}
449
450SDValue RISCVTargetLowering::LowerRETURNADDR(SDValue Op,
451 SelectionDAG &DAG) const {
452 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
453 MachineFunction &MF = DAG.getMachineFunction();
454 MachineFrameInfo &MFI = MF.getFrameInfo();
455 MFI.setReturnAddressIsTaken(true);
456 MVT XLenVT = Subtarget.getXLenVT();
457 int XLenInBytes = Subtarget.getXLen() / 8;
458
459 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
460 return SDValue();
461
462 EVT VT = Op.getValueType();
463 SDLoc DL(Op);
464 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
465 if (Depth) {
466 int Off = -XLenInBytes;
467 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
468 SDValue Offset = DAG.getConstant(Off, DL, VT);
469 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
470 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
471 MachinePointerInfo());
472 }
473
474 // Return the value of the return address register, marking it an implicit
475 // live-in.
476 unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
477 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
478}
479
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000480static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
481 MachineBasicBlock *BB) {
482 assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
483
484 MachineFunction &MF = *BB->getParent();
485 DebugLoc DL = MI.getDebugLoc();
486 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
487 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
488 unsigned LoReg = MI.getOperand(0).getReg();
489 unsigned HiReg = MI.getOperand(1).getReg();
490 unsigned SrcReg = MI.getOperand(2).getReg();
491 const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
492 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
493
494 TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
495 RI);
496 MachineMemOperand *MMO =
497 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
498 MachineMemOperand::MOLoad, 8, 8);
499 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
500 .addFrameIndex(FI)
501 .addImm(0)
502 .addMemOperand(MMO);
503 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
504 .addFrameIndex(FI)
505 .addImm(4)
506 .addMemOperand(MMO);
507 MI.eraseFromParent(); // The pseudo instruction is gone now.
508 return BB;
509}
510
511static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
512 MachineBasicBlock *BB) {
513 assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
514 "Unexpected instruction");
515
516 MachineFunction &MF = *BB->getParent();
517 DebugLoc DL = MI.getDebugLoc();
518 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
519 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
520 unsigned DstReg = MI.getOperand(0).getReg();
521 unsigned LoReg = MI.getOperand(1).getReg();
522 unsigned HiReg = MI.getOperand(2).getReg();
523 const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
524 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
525
526 MachineMemOperand *MMO =
527 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
528 MachineMemOperand::MOStore, 8, 8);
529 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
530 .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
531 .addFrameIndex(FI)
532 .addImm(0)
533 .addMemOperand(MMO);
534 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
535 .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
536 .addFrameIndex(FI)
537 .addImm(4)
538 .addMemOperand(MMO);
539 TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
540 MI.eraseFromParent(); // The pseudo instruction is gone now.
541 return BB;
542}
543
Alex Bradbury65385162017-11-21 07:51:32 +0000544MachineBasicBlock *
545RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
546 MachineBasicBlock *BB) const {
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000547 switch (MI.getOpcode()) {
548 default:
549 llvm_unreachable("Unexpected instr type to insert");
550 case RISCV::Select_GPR_Using_CC_GPR:
551 case RISCV::Select_FPR32_Using_CC_GPR:
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000552 case RISCV::Select_FPR64_Using_CC_GPR:
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000553 break;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000554 case RISCV::BuildPairF64Pseudo:
555 return emitBuildPairF64Pseudo(MI, BB);
556 case RISCV::SplitF64Pseudo:
557 return emitSplitF64Pseudo(MI, BB);
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000558 }
Alex Bradbury65385162017-11-21 07:51:32 +0000559
560 // To "insert" a SELECT instruction, we actually have to insert the triangle
561 // control-flow pattern. The incoming instruction knows the destination vreg
562 // to set, the condition code register to branch on, the true/false values to
563 // select between, and the condcode to use to select the appropriate branch.
564 //
565 // We produce the following control flow:
566 // HeadMBB
567 // | \
568 // | IfFalseMBB
569 // | /
570 // TailMBB
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000571 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
Alex Bradbury65385162017-11-21 07:51:32 +0000572 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000573 DebugLoc DL = MI.getDebugLoc();
Alex Bradbury65385162017-11-21 07:51:32 +0000574 MachineFunction::iterator I = ++BB->getIterator();
575
576 MachineBasicBlock *HeadMBB = BB;
577 MachineFunction *F = BB->getParent();
578 MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
579 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
580
581 F->insert(I, IfFalseMBB);
582 F->insert(I, TailMBB);
583 // Move all remaining instructions to TailMBB.
584 TailMBB->splice(TailMBB->begin(), HeadMBB,
585 std::next(MachineBasicBlock::iterator(MI)), HeadMBB->end());
586 // Update machine-CFG edges by transferring all successors of the current
587 // block to the new block which will contain the Phi node for the select.
588 TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
589 // Set the successors for HeadMBB.
590 HeadMBB->addSuccessor(IfFalseMBB);
591 HeadMBB->addSuccessor(TailMBB);
592
593 // Insert appropriate branch.
594 unsigned LHS = MI.getOperand(1).getReg();
595 unsigned RHS = MI.getOperand(2).getReg();
596 auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
597 unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
598
599 BuildMI(HeadMBB, DL, TII.get(Opcode))
600 .addReg(LHS)
601 .addReg(RHS)
602 .addMBB(TailMBB);
603
604 // IfFalseMBB just falls through to TailMBB.
605 IfFalseMBB->addSuccessor(TailMBB);
606
607 // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
608 BuildMI(*TailMBB, TailMBB->begin(), DL, TII.get(RISCV::PHI),
609 MI.getOperand(0).getReg())
610 .addReg(MI.getOperand(4).getReg())
611 .addMBB(HeadMBB)
612 .addReg(MI.getOperand(5).getReg())
613 .addMBB(IfFalseMBB);
614
615 MI.eraseFromParent(); // The pseudo instruction is gone now.
616 return TailMBB;
617}
618
Alex Bradbury89718422017-10-19 21:37:38 +0000619// Calling Convention Implementation.
Alex Bradburydc31c612017-12-11 12:49:02 +0000620// The expectations for frontend ABI lowering vary from target to target.
621// Ideally, an LLVM frontend would be able to avoid worrying about many ABI
622// details, but this is a longer term goal. For now, we simply try to keep the
623// role of the frontend as simple and well-defined as possible. The rules can
624// be summarised as:
625// * Never split up large scalar arguments. We handle them here.
626// * If a hardfloat calling convention is being used, and the struct may be
627// passed in a pair of registers (fp+fp, int+fp), and both registers are
628// available, then pass as two separate arguments. If either the GPRs or FPRs
629// are exhausted, then pass according to the rule below.
630// * If a struct could never be passed in registers or directly in a stack
631// slot (as it is larger than 2*XLEN and the floating point rules don't
632// apply), then pass it using a pointer with the byval attribute.
633// * If a struct is less than 2*XLEN, then coerce to either a two-element
634// word-sized array or a 2*XLEN scalar (depending on alignment).
635// * The frontend can determine whether a struct is returned by reference or
636// not based on its size and fields. If it will be returned by reference, the
637// frontend must modify the prototype so a pointer with the sret annotation is
638// passed as the first argument. This is not necessary for large scalar
639// returns.
640// * Struct return values and varargs should be coerced to structs containing
641// register-size fields in the same situations they would be for fixed
642// arguments.
643
644static const MCPhysReg ArgGPRs[] = {
645 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
646 RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
647};
648
649// Pass a 2*XLEN argument that has been split into two XLEN values through
650// registers or the stack as necessary.
651static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
652 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
653 MVT ValVT2, MVT LocVT2,
654 ISD::ArgFlagsTy ArgFlags2) {
655 unsigned XLenInBytes = XLen / 8;
656 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
657 // At least one half can be passed via register.
658 State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
659 VA1.getLocVT(), CCValAssign::Full));
660 } else {
661 // Both halves must be passed on the stack, with proper alignment.
662 unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign());
663 State.addLoc(
664 CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
665 State.AllocateStack(XLenInBytes, StackAlign),
666 VA1.getLocVT(), CCValAssign::Full));
667 State.addLoc(CCValAssign::getMem(
668 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
669 CCValAssign::Full));
670 return false;
671 }
672
673 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
674 // The second half can also be passed via register.
675 State.addLoc(
676 CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
677 } else {
678 // The second half is passed via the stack, without additional alignment.
679 State.addLoc(CCValAssign::getMem(
680 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
681 CCValAssign::Full));
682 }
683
684 return false;
685}
686
687// Implements the RISC-V calling convention. Returns true upon failure.
688static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT,
689 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000690 CCState &State, bool IsFixed, bool IsRet, Type *OrigTy) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000691 unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
692 assert(XLen == 32 || XLen == 64);
693 MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000694 if (ValVT == MVT::f32) {
695 LocVT = MVT::i32;
696 LocInfo = CCValAssign::BCvt;
697 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000698
699 // Any return value split in to more than two values can't be returned
700 // directly.
701 if (IsRet && ValNo > 1)
702 return true;
703
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000704 // If this is a variadic argument, the RISC-V calling convention requires
705 // that it is assigned an 'even' or 'aligned' register if it has 8-byte
706 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
707 // be used regardless of whether the original argument was split during
708 // legalisation or not. The argument will not be passed by registers if the
709 // original type is larger than 2*XLEN, so the register alignment rule does
710 // not apply.
711 unsigned TwoXLenInBytes = (2 * XLen) / 8;
712 if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes &&
713 DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
714 unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
715 // Skip 'odd' register if necessary.
716 if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
717 State.AllocateReg(ArgGPRs);
718 }
719
Alex Bradburydc31c612017-12-11 12:49:02 +0000720 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
721 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
722 State.getPendingArgFlags();
723
724 assert(PendingLocs.size() == PendingArgFlags.size() &&
725 "PendingLocs and PendingArgFlags out of sync");
726
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000727 // Handle passing f64 on RV32D with a soft float ABI.
728 if (XLen == 32 && ValVT == MVT::f64) {
Mandeep Singh Grang88a8b262018-04-16 18:56:10 +0000729 assert(!ArgFlags.isSplit() && PendingLocs.empty() &&
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000730 "Can't lower f64 if it is split");
731 // Depending on available argument GPRS, f64 may be passed in a pair of
732 // GPRs, split between a GPR and the stack, or passed completely on the
733 // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
734 // cases.
735 unsigned Reg = State.AllocateReg(ArgGPRs);
736 LocVT = MVT::i32;
737 if (!Reg) {
738 unsigned StackOffset = State.AllocateStack(8, 8);
739 State.addLoc(
740 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
741 return false;
742 }
743 if (!State.AllocateReg(ArgGPRs))
744 State.AllocateStack(4, 4);
745 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
746 return false;
747 }
748
Alex Bradburydc31c612017-12-11 12:49:02 +0000749 // Split arguments might be passed indirectly, so keep track of the pending
750 // values.
751 if (ArgFlags.isSplit() || !PendingLocs.empty()) {
752 LocVT = XLenVT;
753 LocInfo = CCValAssign::Indirect;
754 PendingLocs.push_back(
755 CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
756 PendingArgFlags.push_back(ArgFlags);
757 if (!ArgFlags.isSplitEnd()) {
758 return false;
759 }
760 }
761
762 // If the split argument only had two elements, it should be passed directly
763 // in registers or on the stack.
764 if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
765 assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
766 // Apply the normal calling convention rules to the first half of the
767 // split argument.
768 CCValAssign VA = PendingLocs[0];
769 ISD::ArgFlagsTy AF = PendingArgFlags[0];
770 PendingLocs.clear();
771 PendingArgFlags.clear();
772 return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
773 ArgFlags);
774 }
775
776 // Allocate to a register if possible, or else a stack slot.
777 unsigned Reg = State.AllocateReg(ArgGPRs);
778 unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8);
779
780 // If we reach this point and PendingLocs is non-empty, we must be at the
781 // end of a split argument that must be passed indirectly.
782 if (!PendingLocs.empty()) {
783 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
784 assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
785
786 for (auto &It : PendingLocs) {
787 if (Reg)
788 It.convertToReg(Reg);
789 else
790 It.convertToMem(StackOffset);
791 State.addLoc(It);
792 }
793 PendingLocs.clear();
794 PendingArgFlags.clear();
795 return false;
796 }
797
798 assert(LocVT == XLenVT && "Expected an XLenVT at this stage");
799
800 if (Reg) {
801 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
802 } else {
803 State.addLoc(
804 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
805 }
806 return false;
807}
808
809void RISCVTargetLowering::analyzeInputArgs(
810 MachineFunction &MF, CCState &CCInfo,
811 const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
812 unsigned NumArgs = Ins.size();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000813 FunctionType *FType = MF.getFunction().getFunctionType();
Alex Bradburydc31c612017-12-11 12:49:02 +0000814
815 for (unsigned i = 0; i != NumArgs; ++i) {
816 MVT ArgVT = Ins[i].VT;
817 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
818
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000819 Type *ArgTy = nullptr;
820 if (IsRet)
821 ArgTy = FType->getReturnType();
822 else if (Ins[i].isOrigArg())
823 ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
824
Alex Bradburydc31c612017-12-11 12:49:02 +0000825 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000826 ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000827 DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
828 << EVT(ArgVT).getEVTString() << '\n');
829 llvm_unreachable(nullptr);
830 }
831 }
832}
833
834void RISCVTargetLowering::analyzeOutputArgs(
835 MachineFunction &MF, CCState &CCInfo,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000836 const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
837 CallLoweringInfo *CLI) const {
Alex Bradburydc31c612017-12-11 12:49:02 +0000838 unsigned NumArgs = Outs.size();
839
840 for (unsigned i = 0; i != NumArgs; i++) {
841 MVT ArgVT = Outs[i].VT;
842 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000843 Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
Alex Bradburydc31c612017-12-11 12:49:02 +0000844
845 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000846 ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000847 DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
848 << EVT(ArgVT).getEVTString() << "\n");
849 llvm_unreachable(nullptr);
850 }
851 }
852}
853
854// The caller is responsible for loading the full value if the argument is
855// passed with CCValAssign::Indirect.
856static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
857 const CCValAssign &VA, const SDLoc &DL) {
858 MachineFunction &MF = DAG.getMachineFunction();
859 MachineRegisterInfo &RegInfo = MF.getRegInfo();
860 EVT LocVT = VA.getLocVT();
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000861 EVT ValVT = VA.getValVT();
Alex Bradburydc31c612017-12-11 12:49:02 +0000862 SDValue Val;
863
864 unsigned VReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
865 RegInfo.addLiveIn(VA.getLocReg(), VReg);
866 Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
867
868 switch (VA.getLocInfo()) {
869 default:
870 llvm_unreachable("Unexpected CCValAssign::LocInfo");
871 case CCValAssign::Full:
872 case CCValAssign::Indirect:
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000873 break;
874 case CCValAssign::BCvt:
875 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
876 break;
Alex Bradburydc31c612017-12-11 12:49:02 +0000877 }
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000878 return Val;
Alex Bradburydc31c612017-12-11 12:49:02 +0000879}
880
881// The caller is responsible for loading the full value if the argument is
882// passed with CCValAssign::Indirect.
883static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
884 const CCValAssign &VA, const SDLoc &DL) {
885 MachineFunction &MF = DAG.getMachineFunction();
886 MachineFrameInfo &MFI = MF.getFrameInfo();
887 EVT LocVT = VA.getLocVT();
888 EVT ValVT = VA.getValVT();
889 EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
890 int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
891 VA.getLocMemOffset(), /*Immutable=*/true);
892 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
893 SDValue Val;
894
895 ISD::LoadExtType ExtType;
896 switch (VA.getLocInfo()) {
897 default:
898 llvm_unreachable("Unexpected CCValAssign::LocInfo");
899 case CCValAssign::Full:
900 case CCValAssign::Indirect:
901 ExtType = ISD::NON_EXTLOAD;
902 break;
903 }
904 Val = DAG.getExtLoad(
905 ExtType, DL, LocVT, Chain, FIN,
906 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
907 return Val;
908}
Alex Bradbury89718422017-10-19 21:37:38 +0000909
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000910static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
911 const CCValAssign &VA, const SDLoc &DL) {
912 assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
913 "Unexpected VA");
914 MachineFunction &MF = DAG.getMachineFunction();
915 MachineFrameInfo &MFI = MF.getFrameInfo();
916 MachineRegisterInfo &RegInfo = MF.getRegInfo();
917
918 if (VA.isMemLoc()) {
919 // f64 is passed on the stack.
920 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
921 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
922 return DAG.getLoad(MVT::f64, DL, Chain, FIN,
923 MachinePointerInfo::getFixedStack(MF, FI));
924 }
925
926 assert(VA.isRegLoc() && "Expected register VA assignment");
927
928 unsigned LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
929 RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
930 SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
931 SDValue Hi;
932 if (VA.getLocReg() == RISCV::X17) {
933 // Second half of f64 is passed on the stack.
934 int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
935 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
936 Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
937 MachinePointerInfo::getFixedStack(MF, FI));
938 } else {
939 // Second half of f64 is passed in another GPR.
940 unsigned HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
941 RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
942 Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
943 }
944 return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
945}
946
Alex Bradbury89718422017-10-19 21:37:38 +0000947// Transform physical registers into virtual registers.
948SDValue RISCVTargetLowering::LowerFormalArguments(
949 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
950 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
951 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
952
953 switch (CallConv) {
954 default:
955 report_fatal_error("Unsupported calling convention");
956 case CallingConv::C:
Alex Bradburya3376752017-11-08 13:41:21 +0000957 case CallingConv::Fast:
Alex Bradbury89718422017-10-19 21:37:38 +0000958 break;
959 }
960
961 MachineFunction &MF = DAG.getMachineFunction();
Alex Bradburydc31c612017-12-11 12:49:02 +0000962 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000963 MVT XLenVT = Subtarget.getXLenVT();
964 unsigned XLenInBytes = Subtarget.getXLen() / 8;
965 // Used with vargs to acumulate store chains.
966 std::vector<SDValue> OutChains;
Alex Bradbury89718422017-10-19 21:37:38 +0000967
968 // Assign locations to all of the incoming arguments.
969 SmallVector<CCValAssign, 16> ArgLocs;
970 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +0000971 analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
Alex Bradbury89718422017-10-19 21:37:38 +0000972
Alex Bradburydc31c612017-12-11 12:49:02 +0000973 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
974 CCValAssign &VA = ArgLocs[i];
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000975 assert(VA.getLocVT() == XLenVT && "Unhandled argument type");
Alex Bradburydc31c612017-12-11 12:49:02 +0000976 SDValue ArgValue;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000977 // Passing f64 on RV32D with a soft float ABI must be handled as a special
978 // case.
979 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
980 ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
981 else if (VA.isRegLoc())
Alex Bradburydc31c612017-12-11 12:49:02 +0000982 ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL);
983 else
984 ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
Alex Bradbury89718422017-10-19 21:37:38 +0000985
Alex Bradburydc31c612017-12-11 12:49:02 +0000986 if (VA.getLocInfo() == CCValAssign::Indirect) {
987 // If the original argument was split and passed by reference (e.g. i128
988 // on RV32), we need to load all parts of it here (using the same
989 // address).
990 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
991 MachinePointerInfo()));
992 unsigned ArgIndex = Ins[i].OrigArgIndex;
993 assert(Ins[i].PartOffset == 0);
994 while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
995 CCValAssign &PartVA = ArgLocs[i + 1];
996 unsigned PartOffset = Ins[i + 1].PartOffset;
997 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
998 DAG.getIntPtrConstant(PartOffset, DL));
999 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1000 MachinePointerInfo()));
1001 ++i;
1002 }
1003 continue;
Alex Bradbury89718422017-10-19 21:37:38 +00001004 }
Alex Bradburydc31c612017-12-11 12:49:02 +00001005 InVals.push_back(ArgValue);
Alex Bradbury89718422017-10-19 21:37:38 +00001006 }
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001007
1008 if (IsVarArg) {
1009 ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
1010 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
1011 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
1012 MachineFrameInfo &MFI = MF.getFrameInfo();
1013 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1014 RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1015
1016 // Offset of the first variable argument from stack pointer, and size of
1017 // the vararg save area. For now, the varargs save area is either zero or
1018 // large enough to hold a0-a7.
1019 int VaArgOffset, VarArgsSaveSize;
1020
1021 // If all registers are allocated, then all varargs must be passed on the
1022 // stack and we don't need to save any argregs.
1023 if (ArgRegs.size() == Idx) {
1024 VaArgOffset = CCInfo.getNextStackOffset();
1025 VarArgsSaveSize = 0;
1026 } else {
1027 VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
1028 VaArgOffset = -VarArgsSaveSize;
1029 }
1030
1031 // Record the frame index of the first variable argument
1032 // which is a value necessary to VASTART.
1033 int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
1034 RVFI->setVarArgsFrameIndex(FI);
1035
1036 // If saving an odd number of registers then create an extra stack slot to
1037 // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
1038 // offsets to even-numbered registered remain 2*XLEN-aligned.
1039 if (Idx % 2) {
1040 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes,
1041 true);
1042 VarArgsSaveSize += XLenInBytes;
1043 }
1044
1045 // Copy the integer registers that may have been used for passing varargs
1046 // to the vararg save area.
1047 for (unsigned I = Idx; I < ArgRegs.size();
1048 ++I, VaArgOffset += XLenInBytes) {
1049 const unsigned Reg = RegInfo.createVirtualRegister(RC);
1050 RegInfo.addLiveIn(ArgRegs[I], Reg);
1051 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
1052 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
1053 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1054 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
1055 MachinePointerInfo::getFixedStack(MF, FI));
1056 cast<StoreSDNode>(Store.getNode())
1057 ->getMemOperand()
1058 ->setValue((Value *)nullptr);
1059 OutChains.push_back(Store);
1060 }
1061 RVFI->setVarArgsSaveSize(VarArgsSaveSize);
1062 }
1063
1064 // All stores are grouped in one node to allow the matching between
1065 // the size of Ins and InVals. This only happens for vararg functions.
1066 if (!OutChains.empty()) {
1067 OutChains.push_back(Chain);
1068 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
1069 }
1070
Alex Bradbury89718422017-10-19 21:37:38 +00001071 return Chain;
1072}
1073
Alex Bradburya3376752017-11-08 13:41:21 +00001074// Lower a call to a callseq_start + CALL + callseq_end chain, and add input
1075// and output parameter nodes.
1076SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1077 SmallVectorImpl<SDValue> &InVals) const {
1078 SelectionDAG &DAG = CLI.DAG;
1079 SDLoc &DL = CLI.DL;
1080 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1081 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1082 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1083 SDValue Chain = CLI.Chain;
1084 SDValue Callee = CLI.Callee;
1085 CLI.IsTailCall = false;
1086 CallingConv::ID CallConv = CLI.CallConv;
1087 bool IsVarArg = CLI.IsVarArg;
1088 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburydc31c612017-12-11 12:49:02 +00001089 MVT XLenVT = Subtarget.getXLenVT();
Alex Bradburya3376752017-11-08 13:41:21 +00001090
Alex Bradburya3376752017-11-08 13:41:21 +00001091 MachineFunction &MF = DAG.getMachineFunction();
1092
1093 // Analyze the operands of the call, assigning locations to each operand.
1094 SmallVector<CCValAssign, 16> ArgLocs;
1095 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001096 analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
Alex Bradburya3376752017-11-08 13:41:21 +00001097
1098 // Get a count of how many bytes are to be pushed on the stack.
1099 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1100
Alex Bradburydc31c612017-12-11 12:49:02 +00001101 // Create local copies for byval args
1102 SmallVector<SDValue, 8> ByValArgs;
1103 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1104 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1105 if (!Flags.isByVal())
Alex Bradburya3376752017-11-08 13:41:21 +00001106 continue;
Alex Bradburydc31c612017-12-11 12:49:02 +00001107
1108 SDValue Arg = OutVals[i];
1109 unsigned Size = Flags.getByValSize();
1110 unsigned Align = Flags.getByValAlign();
1111
1112 int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false);
1113 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1114 SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
1115
1116 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
1117 /*IsVolatile=*/false,
1118 /*AlwaysInline=*/false,
1119 /*isTailCall=*/false, MachinePointerInfo(),
1120 MachinePointerInfo());
1121 ByValArgs.push_back(FIPtr);
Alex Bradburya3376752017-11-08 13:41:21 +00001122 }
1123
1124 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
1125
1126 // Copy argument values to their designated locations.
1127 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Alex Bradburydc31c612017-12-11 12:49:02 +00001128 SmallVector<SDValue, 8> MemOpChains;
Alex Bradburya3376752017-11-08 13:41:21 +00001129 SDValue StackPtr;
Alex Bradburydc31c612017-12-11 12:49:02 +00001130 for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
1131 CCValAssign &VA = ArgLocs[i];
1132 SDValue ArgValue = OutVals[i];
1133 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Alex Bradburya3376752017-11-08 13:41:21 +00001134
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001135 // Handle passing f64 on RV32D with a soft float ABI as a special case.
1136 bool IsF64OnRV32DSoftABI =
1137 VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
1138 if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
1139 SDValue SplitF64 = DAG.getNode(
1140 RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
1141 SDValue Lo = SplitF64.getValue(0);
1142 SDValue Hi = SplitF64.getValue(1);
1143
1144 unsigned RegLo = VA.getLocReg();
1145 RegsToPass.push_back(std::make_pair(RegLo, Lo));
1146
1147 if (RegLo == RISCV::X17) {
1148 // Second half of f64 is passed on the stack.
1149 // Work out the address of the stack slot.
1150 if (!StackPtr.getNode())
1151 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1152 // Emit the store.
1153 MemOpChains.push_back(
1154 DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
1155 } else {
1156 // Second half of f64 is passed in another GPR.
1157 unsigned RegHigh = RegLo + 1;
1158 RegsToPass.push_back(std::make_pair(RegHigh, Hi));
1159 }
1160 continue;
1161 }
1162
1163 // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
1164 // as any other MemLoc.
1165
Alex Bradburya3376752017-11-08 13:41:21 +00001166 // Promote the value if needed.
Alex Bradburydc31c612017-12-11 12:49:02 +00001167 // For now, only handle fully promoted and indirect arguments.
Alex Bradburya3376752017-11-08 13:41:21 +00001168 switch (VA.getLocInfo()) {
1169 case CCValAssign::Full:
1170 break;
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001171 case CCValAssign::BCvt:
1172 ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), ArgValue);
1173 break;
Alex Bradburydc31c612017-12-11 12:49:02 +00001174 case CCValAssign::Indirect: {
1175 // Store the argument in a stack slot and pass its address.
1176 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
1177 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1178 MemOpChains.push_back(
1179 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1180 MachinePointerInfo::getFixedStack(MF, FI)));
1181 // If the original argument was split (e.g. i128), we need
1182 // to store all parts of it here (and pass just one address).
1183 unsigned ArgIndex = Outs[i].OrigArgIndex;
1184 assert(Outs[i].PartOffset == 0);
1185 while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
1186 SDValue PartValue = OutVals[i + 1];
1187 unsigned PartOffset = Outs[i + 1].PartOffset;
1188 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1189 DAG.getIntPtrConstant(PartOffset, DL));
1190 MemOpChains.push_back(
1191 DAG.getStore(Chain, DL, PartValue, Address,
1192 MachinePointerInfo::getFixedStack(MF, FI)));
1193 ++i;
1194 }
1195 ArgValue = SpillSlot;
1196 break;
1197 }
Alex Bradburya3376752017-11-08 13:41:21 +00001198 default:
1199 llvm_unreachable("Unknown loc info!");
1200 }
1201
Alex Bradburydc31c612017-12-11 12:49:02 +00001202 // Use local copy if it is a byval arg.
1203 if (Flags.isByVal())
1204 ArgValue = ByValArgs[j++];
1205
Alex Bradburya3376752017-11-08 13:41:21 +00001206 if (VA.isRegLoc()) {
1207 // Queue up the argument copies and emit them at the end.
1208 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1209 } else {
1210 assert(VA.isMemLoc() && "Argument not register or memory");
Alex Bradburydc31c612017-12-11 12:49:02 +00001211
1212 // Work out the address of the stack slot.
1213 if (!StackPtr.getNode())
1214 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1215 SDValue Address =
1216 DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
1217 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
1218
1219 // Emit the store.
1220 MemOpChains.push_back(
1221 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Alex Bradburya3376752017-11-08 13:41:21 +00001222 }
1223 }
1224
Alex Bradburydc31c612017-12-11 12:49:02 +00001225 // Join the stores, which are independent of one another.
1226 if (!MemOpChains.empty())
1227 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
1228
Alex Bradburya3376752017-11-08 13:41:21 +00001229 SDValue Glue;
1230
1231 // Build a sequence of copy-to-reg nodes, chained and glued together.
1232 for (auto &Reg : RegsToPass) {
1233 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
1234 Glue = Chain.getValue(1);
1235 }
1236
Shiva Chend58bd8d2018-04-25 14:19:12 +00001237 // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
1238 // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
1239 // split it and then direct call can be matched by PseudoCALL.
1240 if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
1241 Callee = DAG.getTargetGlobalAddress(S->getGlobal(), DL, PtrVT, 0, 0);
1242 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1243 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, 0);
Alex Bradburya3376752017-11-08 13:41:21 +00001244 }
1245
1246 // The first call operand is the chain and the second is the target address.
1247 SmallVector<SDValue, 8> Ops;
1248 Ops.push_back(Chain);
1249 Ops.push_back(Callee);
1250
1251 // Add argument registers to the end of the list so that they are
1252 // known live into the call.
1253 for (auto &Reg : RegsToPass)
1254 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
1255
1256 // Add a register mask operand representing the call-preserved registers.
1257 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1258 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
1259 assert(Mask && "Missing call preserved mask for calling convention");
1260 Ops.push_back(DAG.getRegisterMask(Mask));
1261
1262 // Glue the call to the argument copies, if any.
1263 if (Glue.getNode())
1264 Ops.push_back(Glue);
1265
1266 // Emit the call.
1267 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1268 Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
1269 Glue = Chain.getValue(1);
1270
1271 // Mark the end of the call, which is glued to the call itself.
1272 Chain = DAG.getCALLSEQ_END(Chain,
1273 DAG.getConstant(NumBytes, DL, PtrVT, true),
1274 DAG.getConstant(0, DL, PtrVT, true),
1275 Glue, DL);
1276 Glue = Chain.getValue(1);
1277
1278 // Assign locations to each value returned by this call.
1279 SmallVector<CCValAssign, 16> RVLocs;
1280 CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +00001281 analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
Alex Bradburya3376752017-11-08 13:41:21 +00001282
1283 // Copy all of the result registers out of their specified physreg.
1284 for (auto &VA : RVLocs) {
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001285 // Copy the value out
1286 SDValue RetValue =
1287 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
1288 // Glue the RetValue to the end of the call sequence
Alex Bradburya3376752017-11-08 13:41:21 +00001289 Chain = RetValue.getValue(1);
1290 Glue = RetValue.getValue(2);
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001291 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1292 assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
1293 SDValue RetValue2 =
1294 DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
1295 Chain = RetValue2.getValue(1);
1296 Glue = RetValue2.getValue(2);
1297 RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
1298 RetValue2);
1299 }
Alex Bradburya3376752017-11-08 13:41:21 +00001300
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001301 switch (VA.getLocInfo()) {
1302 default:
1303 llvm_unreachable("Unknown loc info!");
1304 case CCValAssign::Full:
1305 break;
1306 case CCValAssign::BCvt:
1307 RetValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), RetValue);
1308 break;
1309 }
1310
Alex Bradburydc31c612017-12-11 12:49:02 +00001311 InVals.push_back(RetValue);
Alex Bradburya3376752017-11-08 13:41:21 +00001312 }
1313
1314 return Chain;
1315}
1316
Alex Bradburydc31c612017-12-11 12:49:02 +00001317bool RISCVTargetLowering::CanLowerReturn(
1318 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
1319 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
1320 SmallVector<CCValAssign, 16> RVLocs;
1321 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
1322 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1323 MVT VT = Outs[i].VT;
1324 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1325 if (CC_RISCV(MF.getDataLayout(), i, VT, VT, CCValAssign::Full, ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001326 CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr))
Alex Bradburydc31c612017-12-11 12:49:02 +00001327 return false;
1328 }
1329 return true;
1330}
1331
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001332static SDValue packIntoRegLoc(SelectionDAG &DAG, SDValue Val,
1333 const CCValAssign &VA, const SDLoc &DL) {
1334 EVT LocVT = VA.getLocVT();
1335
1336 switch (VA.getLocInfo()) {
1337 default:
1338 llvm_unreachable("Unexpected CCValAssign::LocInfo");
1339 case CCValAssign::Full:
1340 break;
1341 case CCValAssign::BCvt:
1342 Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
1343 break;
1344 }
1345 return Val;
1346}
1347
Alex Bradbury89718422017-10-19 21:37:38 +00001348SDValue
1349RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1350 bool IsVarArg,
1351 const SmallVectorImpl<ISD::OutputArg> &Outs,
1352 const SmallVectorImpl<SDValue> &OutVals,
1353 const SDLoc &DL, SelectionDAG &DAG) const {
Alex Bradbury89718422017-10-19 21:37:38 +00001354 // Stores the assignment of the return value to a location.
1355 SmallVector<CCValAssign, 16> RVLocs;
1356
1357 // Info about the registers and stack slot.
1358 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
1359 *DAG.getContext());
1360
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001361 analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
1362 nullptr);
Alex Bradbury89718422017-10-19 21:37:38 +00001363
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001364 SDValue Glue;
Alex Bradbury89718422017-10-19 21:37:38 +00001365 SmallVector<SDValue, 4> RetOps(1, Chain);
1366
1367 // Copy the result values into the output registers.
1368 for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
Alex Bradburydc31c612017-12-11 12:49:02 +00001369 SDValue Val = OutVals[i];
Alex Bradbury89718422017-10-19 21:37:38 +00001370 CCValAssign &VA = RVLocs[i];
1371 assert(VA.isRegLoc() && "Can only return in registers!");
1372
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001373 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1374 // Handle returning f64 on RV32D with a soft float ABI.
1375 assert(VA.isRegLoc() && "Expected return via registers");
1376 SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
1377 DAG.getVTList(MVT::i32, MVT::i32), Val);
1378 SDValue Lo = SplitF64.getValue(0);
1379 SDValue Hi = SplitF64.getValue(1);
1380 unsigned RegLo = VA.getLocReg();
1381 unsigned RegHi = RegLo + 1;
1382 Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
1383 Glue = Chain.getValue(1);
1384 RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
1385 Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
1386 Glue = Chain.getValue(1);
1387 RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
1388 } else {
1389 // Handle a 'normal' return.
1390 Val = packIntoRegLoc(DAG, Val, VA, DL);
1391 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001392
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001393 // Guarantee that all emitted copies are stuck together.
1394 Glue = Chain.getValue(1);
1395 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1396 }
Alex Bradbury89718422017-10-19 21:37:38 +00001397 }
1398
1399 RetOps[0] = Chain; // Update chain.
1400
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001401 // Add the glue node if we have it.
1402 if (Glue.getNode()) {
1403 RetOps.push_back(Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001404 }
1405
1406 return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
1407}
1408
1409const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
1410 switch ((RISCVISD::NodeType)Opcode) {
1411 case RISCVISD::FIRST_NUMBER:
1412 break;
1413 case RISCVISD::RET_FLAG:
1414 return "RISCVISD::RET_FLAG";
Alex Bradburya3376752017-11-08 13:41:21 +00001415 case RISCVISD::CALL:
1416 return "RISCVISD::CALL";
Alex Bradbury65385162017-11-21 07:51:32 +00001417 case RISCVISD::SELECT_CC:
1418 return "RISCVISD::SELECT_CC";
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001419 case RISCVISD::BuildPairF64:
1420 return "RISCVISD::BuildPairF64";
1421 case RISCVISD::SplitF64:
1422 return "RISCVISD::SplitF64";
Alex Bradbury89718422017-10-19 21:37:38 +00001423 }
1424 return nullptr;
1425}
Alex Bradbury9330e642018-01-10 20:05:09 +00001426
1427std::pair<unsigned, const TargetRegisterClass *>
1428RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1429 StringRef Constraint,
1430 MVT VT) const {
1431 // First, see if this is a constraint that directly corresponds to a
1432 // RISCV register class.
1433 if (Constraint.size() == 1) {
1434 switch (Constraint[0]) {
1435 case 'r':
1436 return std::make_pair(0U, &RISCV::GPRRegClass);
1437 default:
1438 break;
1439 }
1440 }
1441
1442 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1443}