xref: /freebsd/contrib/llvm-project/llvm/lib/Target/CSKY/CSKYISelLowering.cpp (revision 1165fc9a526630487a1feb63daef65c5aee1a583)
1 //===-- CSKYISelLowering.cpp - CSKY DAG Lowering Implementation  ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the interfaces that CSKY uses to lower LLVM code into a
10 // selection DAG.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CSKYISelLowering.h"
15 #include "CSKYCallingConv.h"
16 #include "CSKYConstantPoolValue.h"
17 #include "CSKYMachineFunctionInfo.h"
18 #include "CSKYRegisterInfo.h"
19 #include "CSKYSubtarget.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/CodeGen/CallingConvLower.h"
22 #include "llvm/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/Support/Debug.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "csky-isel-lowering"
28 
29 STATISTIC(NumTailCalls, "Number of tail calls");
30 
31 #include "CSKYGenCallingConv.inc"
32 
33 static const MCPhysReg GPRArgRegs[] = {CSKY::R0, CSKY::R1, CSKY::R2, CSKY::R3};
34 
35 CSKYTargetLowering::CSKYTargetLowering(const TargetMachine &TM,
36                                        const CSKYSubtarget &STI)
37     : TargetLowering(TM), Subtarget(STI) {
38   // Register Class
39   addRegisterClass(MVT::i32, &CSKY::GPRRegClass);
40 
41   if (STI.useHardFloat()) {
42     if (STI.hasFPUv2SingleFloat())
43       addRegisterClass(MVT::f32, &CSKY::sFPR32RegClass);
44     else if (STI.hasFPUv3SingleFloat())
45       addRegisterClass(MVT::f32, &CSKY::FPR32RegClass);
46 
47     if (STI.hasFPUv2DoubleFloat())
48       addRegisterClass(MVT::f64, &CSKY::sFPR64RegClass);
49     else if (STI.hasFPUv3DoubleFloat())
50       addRegisterClass(MVT::f64, &CSKY::FPR64RegClass);
51   }
52 
53   setOperationAction(ISD::ADDCARRY, MVT::i32, Legal);
54   setOperationAction(ISD::SUBCARRY, MVT::i32, Legal);
55   setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
56 
57   setOperationAction(ISD::SREM, MVT::i32, Expand);
58   setOperationAction(ISD::UREM, MVT::i32, Expand);
59   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
60   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
61   setOperationAction(ISD::CTTZ, MVT::i32, Expand);
62   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
63   setOperationAction(ISD::ROTR, MVT::i32, Expand);
64   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
65   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
66   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
67   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
68   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
69   setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
70   setOperationAction(ISD::BR_CC, MVT::i32, Expand);
71   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
72   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
73   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
74   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
75   setOperationAction(ISD::MULHS, MVT::i32, Expand);
76   setOperationAction(ISD::MULHU, MVT::i32, Expand);
77   setOperationAction(ISD::VAARG, MVT::Other, Expand);
78   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
79   setOperationAction(ISD::VAEND, MVT::Other, Expand);
80 
81   setLoadExtAction(ISD::EXTLOAD, MVT::i32, MVT::i1, Promote);
82   setLoadExtAction(ISD::SEXTLOAD, MVT::i32, MVT::i1, Promote);
83   setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, MVT::i1, Promote);
84 
85   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
86   setOperationAction(ISD::ExternalSymbol, MVT::i32, Custom);
87   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
88   setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
89   setOperationAction(ISD::JumpTable, MVT::i32, Custom);
90   setOperationAction(ISD::VASTART, MVT::Other, Custom);
91 
92   if (!Subtarget.hasE2()) {
93     setLoadExtAction(ISD::SEXTLOAD, MVT::i32, MVT::i8, Expand);
94     setLoadExtAction(ISD::SEXTLOAD, MVT::i32, MVT::i16, Expand);
95     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
96     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
97   }
98 
99   if (!Subtarget.has2E3()) {
100     setOperationAction(ISD::ABS, MVT::i32, Expand);
101     setOperationAction(ISD::BITREVERSE, MVT::i32, Expand);
102     setOperationAction(ISD::SDIV, MVT::i32, Expand);
103     setOperationAction(ISD::UDIV, MVT::i32, Expand);
104   }
105 
106   if (!Subtarget.has3r2E3r3()) {
107     setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
108   }
109 
110   // Float
111 
112   ISD::CondCode FPCCToExtend[] = {
113       ISD::SETONE, ISD::SETUEQ, ISD::SETUGT,
114       ISD::SETUGE, ISD::SETULT, ISD::SETULE,
115   };
116 
117   ISD::NodeType FPOpToExpand[] = {ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
118                                   ISD::FPOW, ISD::FREM, ISD::FCOPYSIGN};
119 
120   if (STI.useHardFloat()) {
121 
122     MVT AllVTy[] = {MVT::f32, MVT::f64};
123 
124     for (auto VT : AllVTy) {
125       setOperationAction(ISD::FREM, VT, Expand);
126       setOperationAction(ISD::SELECT_CC, VT, Expand);
127       setOperationAction(ISD::BR_CC, VT, Expand);
128 
129       for (auto CC : FPCCToExtend)
130         setCondCodeAction(CC, VT, Expand);
131       for (auto Op : FPOpToExpand)
132         setOperationAction(Op, VT, Expand);
133     }
134 
135     if (STI.hasFPUv2SingleFloat() || STI.hasFPUv3SingleFloat()) {
136       setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
137     }
138     if (STI.hasFPUv2DoubleFloat() || STI.hasFPUv3DoubleFloat()) {
139       setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
140       setTruncStoreAction(MVT::f64, MVT::f32, Expand);
141     }
142   }
143 
144   // Compute derived properties from the register classes.
145   computeRegisterProperties(STI.getRegisterInfo());
146 
147   setBooleanContents(UndefinedBooleanContent);
148   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
149 
150   // TODO: Add atomic support fully.
151   setMaxAtomicSizeInBitsSupported(0);
152 
153   setStackPointerRegisterToSaveRestore(CSKY::R14);
154   const Align FunctionAlignment(2);
155   setMinFunctionAlignment(FunctionAlignment);
156   setSchedulingPreference(Sched::Source);
157 }
158 
159 SDValue CSKYTargetLowering::LowerOperation(SDValue Op,
160                                            SelectionDAG &DAG) const {
161   switch (Op.getOpcode()) {
162   default:
163     llvm_unreachable("unimplemented op");
164   case ISD::GlobalAddress:
165     return LowerGlobalAddress(Op, DAG);
166   case ISD::ExternalSymbol:
167     return LowerExternalSymbol(Op, DAG);
168   case ISD::GlobalTLSAddress:
169     return LowerGlobalTLSAddress(Op, DAG);
170   case ISD::JumpTable:
171     return LowerJumpTable(Op, DAG);
172   case ISD::BlockAddress:
173     return LowerBlockAddress(Op, DAG);
174   case ISD::VASTART:
175     return LowerVASTART(Op, DAG);
176   case ISD::FRAMEADDR:
177     return LowerFRAMEADDR(Op, DAG);
178   case ISD::RETURNADDR:
179     return LowerRETURNADDR(Op, DAG);
180   }
181 }
182 
183 EVT CSKYTargetLowering::getSetCCResultType(const DataLayout &DL,
184                                            LLVMContext &Context, EVT VT) const {
185   if (!VT.isVector())
186     return MVT::i32;
187 
188   return VT.changeVectorElementTypeToInteger();
189 }
190 
191 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val,
192                                    const CCValAssign &VA, const SDLoc &DL) {
193   EVT LocVT = VA.getLocVT();
194 
195   switch (VA.getLocInfo()) {
196   default:
197     llvm_unreachable("Unexpected CCValAssign::LocInfo");
198   case CCValAssign::Full:
199     break;
200   case CCValAssign::BCvt:
201     Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
202     break;
203   }
204   return Val;
205 }
206 
207 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val,
208                                    const CCValAssign &VA, const SDLoc &DL) {
209   switch (VA.getLocInfo()) {
210   default:
211     llvm_unreachable("Unexpected CCValAssign::LocInfo");
212   case CCValAssign::Full:
213     break;
214   case CCValAssign::BCvt:
215     Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
216     break;
217   }
218   return Val;
219 }
220 
221 static SDValue unpackFromRegLoc(const CSKYSubtarget &Subtarget,
222                                 SelectionDAG &DAG, SDValue Chain,
223                                 const CCValAssign &VA, const SDLoc &DL) {
224   MachineFunction &MF = DAG.getMachineFunction();
225   MachineRegisterInfo &RegInfo = MF.getRegInfo();
226   EVT LocVT = VA.getLocVT();
227   SDValue Val;
228   const TargetRegisterClass *RC;
229 
230   switch (LocVT.getSimpleVT().SimpleTy) {
231   default:
232     llvm_unreachable("Unexpected register type");
233   case MVT::i32:
234     RC = &CSKY::GPRRegClass;
235     break;
236   case MVT::f32:
237     RC = Subtarget.hasFPUv2SingleFloat() ? &CSKY::sFPR32RegClass
238                                          : &CSKY::FPR32RegClass;
239     break;
240   case MVT::f64:
241     RC = Subtarget.hasFPUv2DoubleFloat() ? &CSKY::sFPR64RegClass
242                                          : &CSKY::FPR64RegClass;
243     break;
244   }
245 
246   Register VReg = RegInfo.createVirtualRegister(RC);
247   RegInfo.addLiveIn(VA.getLocReg(), VReg);
248   Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
249 
250   return convertLocVTToValVT(DAG, Val, VA, DL);
251 }
252 
253 static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
254                                 const CCValAssign &VA, const SDLoc &DL) {
255   MachineFunction &MF = DAG.getMachineFunction();
256   MachineFrameInfo &MFI = MF.getFrameInfo();
257   EVT LocVT = VA.getLocVT();
258   EVT ValVT = VA.getValVT();
259   EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
260   int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
261                                  VA.getLocMemOffset(), /*Immutable=*/true);
262   SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
263   SDValue Val;
264 
265   ISD::LoadExtType ExtType;
266   switch (VA.getLocInfo()) {
267   default:
268     llvm_unreachable("Unexpected CCValAssign::LocInfo");
269   case CCValAssign::Full:
270   case CCValAssign::BCvt:
271     ExtType = ISD::NON_EXTLOAD;
272     break;
273   }
274   Val = DAG.getExtLoad(
275       ExtType, DL, LocVT, Chain, FIN,
276       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
277   return Val;
278 }
279 
280 static SDValue unpack64(SelectionDAG &DAG, SDValue Chain, const CCValAssign &VA,
281                         const SDLoc &DL) {
282   assert(VA.getLocVT() == MVT::i32 &&
283          (VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::i64) &&
284          "Unexpected VA");
285   MachineFunction &MF = DAG.getMachineFunction();
286   MachineFrameInfo &MFI = MF.getFrameInfo();
287   MachineRegisterInfo &RegInfo = MF.getRegInfo();
288 
289   if (VA.isMemLoc()) {
290     // f64/i64 is passed on the stack.
291     int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
292     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
293     return DAG.getLoad(VA.getValVT(), DL, Chain, FIN,
294                        MachinePointerInfo::getFixedStack(MF, FI));
295   }
296 
297   assert(VA.isRegLoc() && "Expected register VA assignment");
298 
299   Register LoVReg = RegInfo.createVirtualRegister(&CSKY::GPRRegClass);
300   RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
301   SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
302   SDValue Hi;
303   if (VA.getLocReg() == CSKY::R3) {
304     // Second half of f64/i64 is passed on the stack.
305     int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
306     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
307     Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
308                      MachinePointerInfo::getFixedStack(MF, FI));
309   } else {
310     // Second half of f64/i64 is passed in another GPR.
311     Register HiVReg = RegInfo.createVirtualRegister(&CSKY::GPRRegClass);
312     RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
313     Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
314   }
315   return DAG.getNode(CSKYISD::BITCAST_FROM_LOHI, DL, VA.getValVT(), Lo, Hi);
316 }
317 
318 // Transform physical registers into virtual registers.
319 SDValue CSKYTargetLowering::LowerFormalArguments(
320     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
321     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
322     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
323 
324   switch (CallConv) {
325   default:
326     report_fatal_error("Unsupported calling convention");
327   case CallingConv::C:
328   case CallingConv::Fast:
329     break;
330   }
331 
332   MachineFunction &MF = DAG.getMachineFunction();
333 
334   // Used with vargs to acumulate store chains.
335   std::vector<SDValue> OutChains;
336 
337   // Assign locations to all of the incoming arguments.
338   SmallVector<CCValAssign, 16> ArgLocs;
339   CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
340 
341   CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, IsVarArg));
342 
343   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
344     CCValAssign &VA = ArgLocs[i];
345     SDValue ArgValue;
346 
347     bool IsF64OnCSKY = VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
348 
349     if (IsF64OnCSKY)
350       ArgValue = unpack64(DAG, Chain, VA, DL);
351     else if (VA.isRegLoc())
352       ArgValue = unpackFromRegLoc(Subtarget, DAG, Chain, VA, DL);
353     else
354       ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
355 
356     InVals.push_back(ArgValue);
357   }
358 
359   if (IsVarArg) {
360     const unsigned XLenInBytes = 4;
361     const MVT XLenVT = MVT::i32;
362 
363     ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(GPRArgRegs);
364     unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
365     const TargetRegisterClass *RC = &CSKY::GPRRegClass;
366     MachineFrameInfo &MFI = MF.getFrameInfo();
367     MachineRegisterInfo &RegInfo = MF.getRegInfo();
368     CSKYMachineFunctionInfo *CSKYFI = MF.getInfo<CSKYMachineFunctionInfo>();
369 
370     // Offset of the first variable argument from stack pointer, and size of
371     // the vararg save area. For now, the varargs save area is either zero or
372     // large enough to hold a0-a4.
373     int VaArgOffset, VarArgsSaveSize;
374 
375     // If all registers are allocated, then all varargs must be passed on the
376     // stack and we don't need to save any argregs.
377     if (ArgRegs.size() == Idx) {
378       VaArgOffset = CCInfo.getNextStackOffset();
379       VarArgsSaveSize = 0;
380     } else {
381       VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
382       VaArgOffset = -VarArgsSaveSize;
383     }
384 
385     // Record the frame index of the first variable argument
386     // which is a value necessary to VASTART.
387     int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
388     CSKYFI->setVarArgsFrameIndex(FI);
389 
390     // Copy the integer registers that may have been used for passing varargs
391     // to the vararg save area.
392     for (unsigned I = Idx; I < ArgRegs.size();
393          ++I, VaArgOffset += XLenInBytes) {
394       const Register Reg = RegInfo.createVirtualRegister(RC);
395       RegInfo.addLiveIn(ArgRegs[I], Reg);
396       SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
397       FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
398       SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
399       SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
400                                    MachinePointerInfo::getFixedStack(MF, FI));
401       cast<StoreSDNode>(Store.getNode())
402           ->getMemOperand()
403           ->setValue((Value *)nullptr);
404       OutChains.push_back(Store);
405     }
406     CSKYFI->setVarArgsSaveSize(VarArgsSaveSize);
407   }
408 
409   // All stores are grouped in one node to allow the matching between
410   // the size of Ins and InVals. This only happens for vararg functions.
411   if (!OutChains.empty()) {
412     OutChains.push_back(Chain);
413     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
414   }
415 
416   return Chain;
417 }
418 
419 bool CSKYTargetLowering::CanLowerReturn(
420     CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
421     const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
422   SmallVector<CCValAssign, 16> CSKYLocs;
423   CCState CCInfo(CallConv, IsVarArg, MF, CSKYLocs, Context);
424   return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, IsVarArg));
425 }
426 
427 SDValue
428 CSKYTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
429                                 bool IsVarArg,
430                                 const SmallVectorImpl<ISD::OutputArg> &Outs,
431                                 const SmallVectorImpl<SDValue> &OutVals,
432                                 const SDLoc &DL, SelectionDAG &DAG) const {
433   // Stores the assignment of the return value to a location.
434   SmallVector<CCValAssign, 16> CSKYLocs;
435 
436   // Info about the registers and stack slot.
437   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), CSKYLocs,
438                  *DAG.getContext());
439   CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, IsVarArg));
440 
441   SDValue Glue;
442   SmallVector<SDValue, 4> RetOps(1, Chain);
443 
444   // Copy the result values into the output registers.
445   for (unsigned i = 0, e = CSKYLocs.size(); i < e; ++i) {
446     SDValue Val = OutVals[i];
447     CCValAssign &VA = CSKYLocs[i];
448     assert(VA.isRegLoc() && "Can only return in registers!");
449 
450     bool IsF64OnCSKY = VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
451 
452     if (IsF64OnCSKY) {
453 
454       assert(VA.isRegLoc() && "Expected return via registers");
455       SDValue Split64 = DAG.getNode(CSKYISD::BITCAST_TO_LOHI, DL,
456                                     DAG.getVTList(MVT::i32, MVT::i32), Val);
457       SDValue Lo = Split64.getValue(0);
458       SDValue Hi = Split64.getValue(1);
459 
460       Register RegLo = VA.getLocReg();
461       assert(RegLo < CSKY::R31 && "Invalid register pair");
462       Register RegHi = RegLo + 1;
463 
464       Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
465       Glue = Chain.getValue(1);
466       RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
467       Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
468       Glue = Chain.getValue(1);
469       RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
470     } else {
471       // Handle a 'normal' return.
472       Val = convertValVTToLocVT(DAG, Val, VA, DL);
473       Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
474 
475       // Guarantee that all emitted copies are stuck together.
476       Glue = Chain.getValue(1);
477       RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
478     }
479   }
480 
481   RetOps[0] = Chain; // Update chain.
482 
483   // Add the glue node if we have it.
484   if (Glue.getNode()) {
485     RetOps.push_back(Glue);
486   }
487 
488   // Interrupt service routines use different return instructions.
489   if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt"))
490     return DAG.getNode(CSKYISD::NIR, DL, MVT::Other, RetOps);
491 
492   return DAG.getNode(CSKYISD::RET, DL, MVT::Other, RetOps);
493 }
494 
495 // Lower a call to a callseq_start + CALL + callseq_end chain, and add input
496 // and output parameter nodes.
497 SDValue CSKYTargetLowering::LowerCall(CallLoweringInfo &CLI,
498                                       SmallVectorImpl<SDValue> &InVals) const {
499   SelectionDAG &DAG = CLI.DAG;
500   SDLoc &DL = CLI.DL;
501   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
502   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
503   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
504   SDValue Chain = CLI.Chain;
505   SDValue Callee = CLI.Callee;
506   bool &IsTailCall = CLI.IsTailCall;
507   CallingConv::ID CallConv = CLI.CallConv;
508   bool IsVarArg = CLI.IsVarArg;
509   EVT PtrVT = getPointerTy(DAG.getDataLayout());
510   MVT XLenVT = MVT::i32;
511 
512   MachineFunction &MF = DAG.getMachineFunction();
513 
514   // Analyze the operands of the call, assigning locations to each operand.
515   SmallVector<CCValAssign, 16> ArgLocs;
516   CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
517 
518   ArgCCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CallConv, IsVarArg));
519 
520   // Check if it's really possible to do a tail call.
521   if (IsTailCall)
522     IsTailCall = false; // TODO: TailCallOptimization;
523 
524   if (IsTailCall)
525     ++NumTailCalls;
526   else if (CLI.CB && CLI.CB->isMustTailCall())
527     report_fatal_error("failed to perform tail call elimination on a call "
528                        "site marked musttail");
529 
530   // Get a count of how many bytes are to be pushed on the stack.
531   unsigned NumBytes = ArgCCInfo.getNextStackOffset();
532 
533   // Create local copies for byval args
534   SmallVector<SDValue, 8> ByValArgs;
535   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
536     ISD::ArgFlagsTy Flags = Outs[i].Flags;
537     if (!Flags.isByVal())
538       continue;
539 
540     SDValue Arg = OutVals[i];
541     unsigned Size = Flags.getByValSize();
542     Align Alignment = Flags.getNonZeroByValAlign();
543 
544     int FI =
545         MF.getFrameInfo().CreateStackObject(Size, Alignment, /*isSS=*/false);
546     SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
547     SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
548 
549     Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Alignment,
550                           /*IsVolatile=*/false,
551                           /*AlwaysInline=*/false, IsTailCall,
552                           MachinePointerInfo(), MachinePointerInfo());
553     ByValArgs.push_back(FIPtr);
554   }
555 
556   if (!IsTailCall)
557     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
558 
559   // Copy argument values to their designated locations.
560   SmallVector<std::pair<Register, SDValue>, 8> RegsToPass;
561   SmallVector<SDValue, 8> MemOpChains;
562   SDValue StackPtr;
563   for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
564     CCValAssign &VA = ArgLocs[i];
565     SDValue ArgValue = OutVals[i];
566     ISD::ArgFlagsTy Flags = Outs[i].Flags;
567 
568     bool IsF64OnCSKY = VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
569 
570     if (IsF64OnCSKY && VA.isRegLoc()) {
571       SDValue Split64 =
572           DAG.getNode(CSKYISD::BITCAST_TO_LOHI, DL,
573                       DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
574       SDValue Lo = Split64.getValue(0);
575       SDValue Hi = Split64.getValue(1);
576 
577       Register RegLo = VA.getLocReg();
578       RegsToPass.push_back(std::make_pair(RegLo, Lo));
579 
580       if (RegLo == CSKY::R3) {
581         // Second half of f64/i64 is passed on the stack.
582         // Work out the address of the stack slot.
583         if (!StackPtr.getNode())
584           StackPtr = DAG.getCopyFromReg(Chain, DL, CSKY::R14, PtrVT);
585         // Emit the store.
586         MemOpChains.push_back(
587             DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
588       } else {
589         // Second half of f64/i64 is passed in another GPR.
590         assert(RegLo < CSKY::R31 && "Invalid register pair");
591         Register RegHigh = RegLo + 1;
592         RegsToPass.push_back(std::make_pair(RegHigh, Hi));
593       }
594       continue;
595     }
596 
597     ArgValue = convertValVTToLocVT(DAG, ArgValue, VA, DL);
598 
599     // Use local copy if it is a byval arg.
600     if (Flags.isByVal())
601       ArgValue = ByValArgs[j++];
602 
603     if (VA.isRegLoc()) {
604       // Queue up the argument copies and emit them at the end.
605       RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
606     } else {
607       assert(VA.isMemLoc() && "Argument not register or memory");
608       assert(!IsTailCall && "Tail call not allowed if stack is used "
609                             "for passing parameters");
610 
611       // Work out the address of the stack slot.
612       if (!StackPtr.getNode())
613         StackPtr = DAG.getCopyFromReg(Chain, DL, CSKY::R14, PtrVT);
614       SDValue Address =
615           DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
616                       DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
617 
618       // Emit the store.
619       MemOpChains.push_back(
620           DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
621     }
622   }
623 
624   // Join the stores, which are independent of one another.
625   if (!MemOpChains.empty())
626     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
627 
628   SDValue Glue;
629 
630   // Build a sequence of copy-to-reg nodes, chained and glued together.
631   for (auto &Reg : RegsToPass) {
632     Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
633     Glue = Chain.getValue(1);
634   }
635 
636   SmallVector<SDValue, 8> Ops;
637   EVT Ty = getPointerTy(DAG.getDataLayout());
638   bool IsRegCall = false;
639 
640   Ops.push_back(Chain);
641 
642   if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
643     const GlobalValue *GV = S->getGlobal();
644     bool IsLocal =
645         getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
646 
647     if (isPositionIndependent() || !Subtarget.has2E3()) {
648       IsRegCall = true;
649       Ops.push_back(getAddr<GlobalAddressSDNode, true>(S, DAG, IsLocal));
650     } else {
651       Ops.push_back(getTargetNode(cast<GlobalAddressSDNode>(Callee), DL, Ty,
652                                   DAG, CSKYII::MO_None));
653       Ops.push_back(getTargetConstantPoolValue(
654           cast<GlobalAddressSDNode>(Callee), Ty, DAG, CSKYII::MO_None));
655     }
656   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
657     bool IsLocal = getTargetMachine().shouldAssumeDSOLocal(
658         *MF.getFunction().getParent(), nullptr);
659 
660     if (isPositionIndependent() || !Subtarget.has2E3()) {
661       IsRegCall = true;
662       Ops.push_back(getAddr<ExternalSymbolSDNode, true>(S, DAG, IsLocal));
663     } else {
664       Ops.push_back(getTargetNode(cast<ExternalSymbolSDNode>(Callee), DL, Ty,
665                                   DAG, CSKYII::MO_None));
666       Ops.push_back(getTargetConstantPoolValue(
667           cast<ExternalSymbolSDNode>(Callee), Ty, DAG, CSKYII::MO_None));
668     }
669   } else {
670     IsRegCall = true;
671     Ops.push_back(Callee);
672   }
673 
674   // Add argument registers to the end of the list so that they are
675   // known live into the call.
676   for (auto &Reg : RegsToPass)
677     Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
678 
679   if (!IsTailCall) {
680     // Add a register mask operand representing the call-preserved registers.
681     const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
682     const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
683     assert(Mask && "Missing call preserved mask for calling convention");
684     Ops.push_back(DAG.getRegisterMask(Mask));
685   }
686 
687   // Glue the call to the argument copies, if any.
688   if (Glue.getNode())
689     Ops.push_back(Glue);
690 
691   // Emit the call.
692   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
693 
694   if (IsTailCall) {
695     MF.getFrameInfo().setHasTailCall();
696     return DAG.getNode(IsRegCall ? CSKYISD::TAILReg : CSKYISD::TAIL, DL,
697                        NodeTys, Ops);
698   }
699 
700   Chain = DAG.getNode(IsRegCall ? CSKYISD::CALLReg : CSKYISD::CALL, DL, NodeTys,
701                       Ops);
702   DAG.addNoMergeSiteInfo(Chain.getNode(), CLI.NoMerge);
703   Glue = Chain.getValue(1);
704 
705   // Mark the end of the call, which is glued to the call itself.
706   Chain = DAG.getCALLSEQ_END(Chain, DAG.getConstant(NumBytes, DL, PtrVT, true),
707                              DAG.getConstant(0, DL, PtrVT, true), Glue, DL);
708   Glue = Chain.getValue(1);
709 
710   // Assign locations to each value returned by this call.
711   SmallVector<CCValAssign, 16> CSKYLocs;
712   CCState RetCCInfo(CallConv, IsVarArg, MF, CSKYLocs, *DAG.getContext());
713   RetCCInfo.AnalyzeCallResult(Ins, CCAssignFnForReturn(CallConv, IsVarArg));
714 
715   // Copy all of the result registers out of their specified physreg.
716   for (auto &VA : CSKYLocs) {
717     // Copy the value out
718     SDValue RetValue =
719         DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
720     // Glue the RetValue to the end of the call sequence
721     Chain = RetValue.getValue(1);
722     Glue = RetValue.getValue(2);
723 
724     bool IsF64OnCSKY = VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
725 
726     if (IsF64OnCSKY) {
727       assert(VA.getLocReg() == GPRArgRegs[0] && "Unexpected reg assignment");
728       SDValue RetValue2 =
729           DAG.getCopyFromReg(Chain, DL, GPRArgRegs[1], MVT::i32, Glue);
730       Chain = RetValue2.getValue(1);
731       Glue = RetValue2.getValue(2);
732       RetValue = DAG.getNode(CSKYISD::BITCAST_FROM_LOHI, DL, VA.getValVT(),
733                              RetValue, RetValue2);
734     }
735 
736     RetValue = convertLocVTToValVT(DAG, RetValue, VA, DL);
737 
738     InVals.push_back(RetValue);
739   }
740 
741   return Chain;
742 }
743 
744 CCAssignFn *CSKYTargetLowering::CCAssignFnForReturn(CallingConv::ID CC,
745                                                     bool IsVarArg) const {
746   if (IsVarArg || !Subtarget.useHardFloatABI())
747     return RetCC_CSKY_ABIV2_SOFT;
748   else
749     return RetCC_CSKY_ABIV2_FP;
750 }
751 
752 CCAssignFn *CSKYTargetLowering::CCAssignFnForCall(CallingConv::ID CC,
753                                                   bool IsVarArg) const {
754   if (IsVarArg || !Subtarget.useHardFloatABI())
755     return CC_CSKY_ABIV2_SOFT;
756   else
757     return CC_CSKY_ABIV2_FP;
758 }
759 
760 static CSKYCP::CSKYCPModifier getModifier(unsigned Flags) {
761 
762   if (Flags == CSKYII::MO_ADDR32)
763     return CSKYCP::ADDR;
764   else if (Flags == CSKYII::MO_GOT32)
765     return CSKYCP::GOT;
766   else if (Flags == CSKYII::MO_GOTOFF)
767     return CSKYCP::GOTOFF;
768   else if (Flags == CSKYII::MO_PLT32)
769     return CSKYCP::PLT;
770   else if (Flags == CSKYII::MO_None)
771     return CSKYCP::NO_MOD;
772   else
773     assert(0 && "unknown CSKYII Modifier");
774   return CSKYCP::NO_MOD;
775 }
776 
777 SDValue CSKYTargetLowering::getTargetConstantPoolValue(GlobalAddressSDNode *N,
778                                                        EVT Ty,
779                                                        SelectionDAG &DAG,
780                                                        unsigned Flags) const {
781   CSKYConstantPoolValue *CPV = CSKYConstantPoolConstant::Create(
782       N->getGlobal(), CSKYCP::CPValue, 0, getModifier(Flags), false);
783 
784   return DAG.getTargetConstantPool(CPV, Ty);
785 }
786 
787 static MachineBasicBlock *
788 emitSelectPseudo(MachineInstr &MI, MachineBasicBlock *BB, unsigned Opcode) {
789 
790   const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
791   DebugLoc DL = MI.getDebugLoc();
792 
793   // To "insert" a SELECT instruction, we actually have to insert the
794   // diamond control-flow pattern.  The incoming instruction knows the
795   // destination vreg to set, the condition code register to branch on, the
796   // true/false values to select between, and a branch opcode to use.
797   const BasicBlock *LLVM_BB = BB->getBasicBlock();
798   MachineFunction::iterator It = ++BB->getIterator();
799 
800   //  thisMBB:
801   //  ...
802   //   TrueVal = ...
803   //   bt32 c, sinkMBB
804   //   fallthrough --> copyMBB
805   MachineBasicBlock *thisMBB = BB;
806   MachineFunction *F = BB->getParent();
807   MachineBasicBlock *copyMBB = F->CreateMachineBasicBlock(LLVM_BB);
808   MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
809   F->insert(It, copyMBB);
810   F->insert(It, sinkMBB);
811 
812   // Transfer the remainder of BB and its successor edges to sinkMBB.
813   sinkMBB->splice(sinkMBB->begin(), BB,
814                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
815   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
816 
817   // Next, add the true and fallthrough blocks as its successors.
818   BB->addSuccessor(copyMBB);
819   BB->addSuccessor(sinkMBB);
820 
821   // bt32 condition, sinkMBB
822   BuildMI(BB, DL, TII.get(Opcode))
823       .addReg(MI.getOperand(1).getReg())
824       .addMBB(sinkMBB);
825 
826   //  copyMBB:
827   //   %FalseValue = ...
828   //   # fallthrough to sinkMBB
829   BB = copyMBB;
830 
831   // Update machine-CFG edges
832   BB->addSuccessor(sinkMBB);
833 
834   //  sinkMBB:
835   //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copyMBB ]
836   //  ...
837   BB = sinkMBB;
838 
839   BuildMI(*BB, BB->begin(), DL, TII.get(CSKY::PHI), MI.getOperand(0).getReg())
840       .addReg(MI.getOperand(2).getReg())
841       .addMBB(thisMBB)
842       .addReg(MI.getOperand(3).getReg())
843       .addMBB(copyMBB);
844 
845   MI.eraseFromParent(); // The pseudo instruction is gone now.
846 
847   return BB;
848 }
849 
850 MachineBasicBlock *
851 CSKYTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
852                                                 MachineBasicBlock *BB) const {
853   switch (MI.getOpcode()) {
854   default:
855     llvm_unreachable("Unexpected instr type to insert");
856   case CSKY::ISEL32:
857     return emitSelectPseudo(MI, BB, CSKY::BT32);
858   case CSKY::ISEL16:
859     return emitSelectPseudo(MI, BB, CSKY::BT16);
860   }
861 }
862 
863 SDValue CSKYTargetLowering::getTargetConstantPoolValue(ExternalSymbolSDNode *N,
864                                                        EVT Ty,
865                                                        SelectionDAG &DAG,
866                                                        unsigned Flags) const {
867   CSKYConstantPoolValue *CPV =
868       CSKYConstantPoolSymbol::Create(Type::getInt32Ty(*DAG.getContext()),
869                                      N->getSymbol(), 0, getModifier(Flags));
870 
871   return DAG.getTargetConstantPool(CPV, Ty);
872 }
873 
874 SDValue CSKYTargetLowering::getTargetConstantPoolValue(JumpTableSDNode *N,
875                                                        EVT Ty,
876                                                        SelectionDAG &DAG,
877                                                        unsigned Flags) const {
878   CSKYConstantPoolValue *CPV =
879       CSKYConstantPoolJT::Create(Type::getInt32Ty(*DAG.getContext()),
880                                  N->getIndex(), 0, getModifier(Flags));
881   return DAG.getTargetConstantPool(CPV, Ty);
882 }
883 
884 SDValue CSKYTargetLowering::getTargetConstantPoolValue(BlockAddressSDNode *N,
885                                                        EVT Ty,
886                                                        SelectionDAG &DAG,
887                                                        unsigned Flags) const {
888   CSKYConstantPoolValue *CPV = CSKYConstantPoolConstant::Create(
889       N->getBlockAddress(), CSKYCP::CPBlockAddress, 0, getModifier(Flags),
890       false);
891   return DAG.getTargetConstantPool(CPV, Ty);
892 }
893 
894 SDValue CSKYTargetLowering::getTargetNode(GlobalAddressSDNode *N, SDLoc DL,
895                                           EVT Ty, SelectionDAG &DAG,
896                                           unsigned Flags) const {
897   return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
898 }
899 
900 SDValue CSKYTargetLowering::getTargetNode(ExternalSymbolSDNode *N, SDLoc DL,
901                                           EVT Ty, SelectionDAG &DAG,
902                                           unsigned Flags) const {
903   return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flags);
904 }
905 
906 SDValue CSKYTargetLowering::getTargetNode(JumpTableSDNode *N, SDLoc DL, EVT Ty,
907                                           SelectionDAG &DAG,
908                                           unsigned Flags) const {
909   return DAG.getTargetJumpTable(N->getIndex(), Ty, Flags);
910 }
911 
912 SDValue CSKYTargetLowering::getTargetNode(BlockAddressSDNode *N, SDLoc DL,
913                                           EVT Ty, SelectionDAG &DAG,
914                                           unsigned Flags) const {
915   return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, N->getOffset(),
916                                    Flags);
917 }
918 
919 const char *CSKYTargetLowering::getTargetNodeName(unsigned Opcode) const {
920   switch (Opcode) {
921   default:
922     llvm_unreachable("unknown CSKYISD node");
923   case CSKYISD::NIE:
924     return "CSKYISD::NIE";
925   case CSKYISD::NIR:
926     return "CSKYISD::NIR";
927   case CSKYISD::RET:
928     return "CSKYISD::RET";
929   case CSKYISD::CALL:
930     return "CSKYISD::CALL";
931   case CSKYISD::CALLReg:
932     return "CSKYISD::CALLReg";
933   case CSKYISD::TAIL:
934     return "CSKYISD::TAIL";
935   case CSKYISD::TAILReg:
936     return "CSKYISD::TAILReg";
937   case CSKYISD::LOAD_ADDR:
938     return "CSKYISD::LOAD_ADDR";
939   case CSKYISD::BITCAST_TO_LOHI:
940     return "CSKYISD::BITCAST_TO_LOHI";
941   case CSKYISD::BITCAST_FROM_LOHI:
942     return "CSKYISD::BITCAST_FROM_LOHI";
943   }
944 }
945 
946 SDValue CSKYTargetLowering::LowerGlobalAddress(SDValue Op,
947                                                SelectionDAG &DAG) const {
948   SDLoc DL(Op);
949   EVT Ty = Op.getValueType();
950   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
951   int64_t Offset = N->getOffset();
952 
953   const GlobalValue *GV = N->getGlobal();
954   bool IsLocal = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
955   SDValue Addr = getAddr<GlobalAddressSDNode, false>(N, DAG, IsLocal);
956 
957   // In order to maximise the opportunity for common subexpression elimination,
958   // emit a separate ADD node for the global address offset instead of folding
959   // it in the global address node. Later peephole optimisations may choose to
960   // fold it back in when profitable.
961   if (Offset != 0)
962     return DAG.getNode(ISD::ADD, DL, Ty, Addr,
963                        DAG.getConstant(Offset, DL, MVT::i32));
964   return Addr;
965 }
966 
967 SDValue CSKYTargetLowering::LowerExternalSymbol(SDValue Op,
968                                                 SelectionDAG &DAG) const {
969   ExternalSymbolSDNode *N = cast<ExternalSymbolSDNode>(Op);
970 
971   return getAddr(N, DAG, false);
972 }
973 
974 SDValue CSKYTargetLowering::LowerJumpTable(SDValue Op,
975                                            SelectionDAG &DAG) const {
976   JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
977 
978   return getAddr<JumpTableSDNode, false>(N, DAG);
979 }
980 
981 SDValue CSKYTargetLowering::LowerBlockAddress(SDValue Op,
982                                               SelectionDAG &DAG) const {
983   BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
984 
985   return getAddr(N, DAG);
986 }
987 
988 SDValue CSKYTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
989   MachineFunction &MF = DAG.getMachineFunction();
990   CSKYMachineFunctionInfo *FuncInfo = MF.getInfo<CSKYMachineFunctionInfo>();
991 
992   SDLoc DL(Op);
993   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
994                                  getPointerTy(MF.getDataLayout()));
995 
996   // vastart just stores the address of the VarArgsFrameIndex slot into the
997   // memory location argument.
998   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
999   return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
1000                       MachinePointerInfo(SV));
1001 }
1002 
1003 SDValue CSKYTargetLowering::LowerFRAMEADDR(SDValue Op,
1004                                            SelectionDAG &DAG) const {
1005   const CSKYRegisterInfo &RI = *Subtarget.getRegisterInfo();
1006   MachineFunction &MF = DAG.getMachineFunction();
1007   MachineFrameInfo &MFI = MF.getFrameInfo();
1008   MFI.setFrameAddressIsTaken(true);
1009 
1010   EVT VT = Op.getValueType();
1011   SDLoc dl(Op);
1012   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1013   Register FrameReg = RI.getFrameRegister(MF);
1014   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1015   while (Depth--)
1016     FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1017                             MachinePointerInfo());
1018   return FrameAddr;
1019 }
1020 
1021 SDValue CSKYTargetLowering::LowerRETURNADDR(SDValue Op,
1022                                             SelectionDAG &DAG) const {
1023   const CSKYRegisterInfo &RI = *Subtarget.getRegisterInfo();
1024   MachineFunction &MF = DAG.getMachineFunction();
1025   MachineFrameInfo &MFI = MF.getFrameInfo();
1026   MFI.setReturnAddressIsTaken(true);
1027 
1028   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1029     return SDValue();
1030 
1031   EVT VT = Op.getValueType();
1032   SDLoc dl(Op);
1033   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1034   if (Depth) {
1035     SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1036     SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
1037     return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1038                        DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1039                        MachinePointerInfo());
1040   }
1041   // Return the value of the return address register, marking it an implicit
1042   // live-in.
1043   unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(MVT::i32));
1044   return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1045 }
1046 
1047 Register CSKYTargetLowering::getExceptionPointerRegister(
1048     const Constant *PersonalityFn) const {
1049   return CSKY::R0;
1050 }
1051 
1052 Register CSKYTargetLowering::getExceptionSelectorRegister(
1053     const Constant *PersonalityFn) const {
1054   return CSKY::R1;
1055 }
1056 
1057 SDValue CSKYTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1058                                                   SelectionDAG &DAG) const {
1059   SDLoc DL(Op);
1060   EVT Ty = Op.getValueType();
1061   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1062   int64_t Offset = N->getOffset();
1063   MVT XLenVT = MVT::i32;
1064 
1065   TLSModel::Model Model = getTargetMachine().getTLSModel(N->getGlobal());
1066   SDValue Addr;
1067   switch (Model) {
1068   case TLSModel::LocalExec:
1069     Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/false);
1070     break;
1071   case TLSModel::InitialExec:
1072     Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/true);
1073     break;
1074   case TLSModel::LocalDynamic:
1075   case TLSModel::GeneralDynamic:
1076     Addr = getDynamicTLSAddr(N, DAG);
1077     break;
1078   }
1079 
1080   // In order to maximise the opportunity for common subexpression elimination,
1081   // emit a separate ADD node for the global address offset instead of folding
1082   // it in the global address node. Later peephole optimisations may choose to
1083   // fold it back in when profitable.
1084   if (Offset != 0)
1085     return DAG.getNode(ISD::ADD, DL, Ty, Addr,
1086                        DAG.getConstant(Offset, DL, XLenVT));
1087   return Addr;
1088 }
1089 
1090 SDValue CSKYTargetLowering::getStaticTLSAddr(GlobalAddressSDNode *N,
1091                                              SelectionDAG &DAG,
1092                                              bool UseGOT) const {
1093   MachineFunction &MF = DAG.getMachineFunction();
1094   CSKYMachineFunctionInfo *CFI = MF.getInfo<CSKYMachineFunctionInfo>();
1095 
1096   unsigned CSKYPCLabelIndex = CFI->createPICLabelUId();
1097 
1098   SDLoc DL(N);
1099   EVT Ty = getPointerTy(DAG.getDataLayout());
1100 
1101   CSKYCP::CSKYCPModifier Flag = UseGOT ? CSKYCP::TLSIE : CSKYCP::TLSLE;
1102   bool AddCurrentAddr = UseGOT ? true : false;
1103   unsigned char PCAjust = UseGOT ? 4 : 0;
1104 
1105   CSKYConstantPoolValue *CPV =
1106       CSKYConstantPoolConstant::Create(N->getGlobal(), CSKYCP::CPValue, PCAjust,
1107                                        Flag, AddCurrentAddr, CSKYPCLabelIndex);
1108   SDValue CAddr = DAG.getTargetConstantPool(CPV, Ty);
1109 
1110   SDValue Load;
1111   if (UseGOT) {
1112     SDValue PICLabel = DAG.getTargetConstant(CSKYPCLabelIndex, DL, MVT::i32);
1113     auto *LRWGRS = DAG.getMachineNode(CSKY::PseudoTLSLA32, DL, {Ty, Ty},
1114                                       {CAddr, PICLabel});
1115     auto LRWADDGRS =
1116         DAG.getNode(ISD::ADD, DL, Ty, SDValue(LRWGRS, 0), SDValue(LRWGRS, 1));
1117     Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), LRWADDGRS,
1118                        MachinePointerInfo(N->getGlobal()));
1119   } else {
1120     Load = SDValue(DAG.getMachineNode(CSKY::LRW32, DL, Ty, CAddr), 0);
1121   }
1122 
1123   // Add the thread pointer.
1124   SDValue TPReg = DAG.getRegister(CSKY::R31, MVT::i32);
1125   return DAG.getNode(ISD::ADD, DL, Ty, Load, TPReg);
1126 }
1127 
1128 SDValue CSKYTargetLowering::getDynamicTLSAddr(GlobalAddressSDNode *N,
1129                                               SelectionDAG &DAG) const {
1130   MachineFunction &MF = DAG.getMachineFunction();
1131   CSKYMachineFunctionInfo *CFI = MF.getInfo<CSKYMachineFunctionInfo>();
1132 
1133   unsigned CSKYPCLabelIndex = CFI->createPICLabelUId();
1134 
1135   SDLoc DL(N);
1136   EVT Ty = getPointerTy(DAG.getDataLayout());
1137   IntegerType *CallTy = Type::getIntNTy(*DAG.getContext(), Ty.getSizeInBits());
1138 
1139   CSKYConstantPoolValue *CPV =
1140       CSKYConstantPoolConstant::Create(N->getGlobal(), CSKYCP::CPValue, 4,
1141                                        CSKYCP::TLSGD, true, CSKYPCLabelIndex);
1142   SDValue Addr = DAG.getTargetConstantPool(CPV, Ty);
1143   SDValue PICLabel = DAG.getTargetConstant(CSKYPCLabelIndex, DL, MVT::i32);
1144 
1145   auto *LRWGRS =
1146       DAG.getMachineNode(CSKY::PseudoTLSLA32, DL, {Ty, Ty}, {Addr, PICLabel});
1147 
1148   auto Load =
1149       DAG.getNode(ISD::ADD, DL, Ty, SDValue(LRWGRS, 0), SDValue(LRWGRS, 1));
1150 
1151   // Prepare argument list to generate call.
1152   ArgListTy Args;
1153   ArgListEntry Entry;
1154   Entry.Node = Load;
1155   Entry.Ty = CallTy;
1156   Args.push_back(Entry);
1157 
1158   // Setup call to __tls_get_addr.
1159   TargetLowering::CallLoweringInfo CLI(DAG);
1160   CLI.setDebugLoc(DL)
1161       .setChain(DAG.getEntryNode())
1162       .setLibCallee(CallingConv::C, CallTy,
1163                     DAG.getExternalSymbol("__tls_get_addr", Ty),
1164                     std::move(Args));
1165   SDValue V = LowerCallTo(CLI).first;
1166 
1167   return V;
1168 }
1169