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