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