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