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