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