1 //===- ARCISelLowering.cpp - ARC DAG Lowering Impl --------------*- C++ -*-===// 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 implements the ARCTargetLowering class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "ARCISelLowering.h" 14 #include "ARC.h" 15 #include "ARCMachineFunctionInfo.h" 16 #include "ARCSubtarget.h" 17 #include "ARCTargetMachine.h" 18 #include "MCTargetDesc/ARCInfo.h" 19 #include "llvm/CodeGen/CallingConvLower.h" 20 #include "llvm/CodeGen/MachineFrameInfo.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineInstrBuilder.h" 23 #include "llvm/CodeGen/MachineJumpTableInfo.h" 24 #include "llvm/CodeGen/MachineRegisterInfo.h" 25 #include "llvm/CodeGen/ValueTypes.h" 26 #include "llvm/IR/CallingConv.h" 27 #include "llvm/IR/Intrinsics.h" 28 #include "llvm/Support/Debug.h" 29 #include <algorithm> 30 31 #define DEBUG_TYPE "arc-lower" 32 33 using namespace llvm; 34 35 static SDValue lowerCallResult(SDValue Chain, SDValue InFlag, 36 const SmallVectorImpl<CCValAssign> &RVLocs, 37 SDLoc dl, SelectionDAG &DAG, 38 SmallVectorImpl<SDValue> &InVals); 39 40 static ARCCC::CondCode ISDCCtoARCCC(ISD::CondCode isdCC) { 41 switch (isdCC) { 42 case ISD::SETUEQ: 43 return ARCCC::EQ; 44 case ISD::SETUGT: 45 return ARCCC::HI; 46 case ISD::SETUGE: 47 return ARCCC::HS; 48 case ISD::SETULT: 49 return ARCCC::LO; 50 case ISD::SETULE: 51 return ARCCC::LS; 52 case ISD::SETUNE: 53 return ARCCC::NE; 54 case ISD::SETEQ: 55 return ARCCC::EQ; 56 case ISD::SETGT: 57 return ARCCC::GT; 58 case ISD::SETGE: 59 return ARCCC::GE; 60 case ISD::SETLT: 61 return ARCCC::LT; 62 case ISD::SETLE: 63 return ARCCC::LE; 64 case ISD::SETNE: 65 return ARCCC::NE; 66 default: 67 llvm_unreachable("Unhandled ISDCC code."); 68 } 69 } 70 71 ARCTargetLowering::ARCTargetLowering(const TargetMachine &TM, 72 const ARCSubtarget &Subtarget) 73 : TargetLowering(TM), Subtarget(Subtarget) { 74 // Set up the register classes. 75 addRegisterClass(MVT::i32, &ARC::GPR32RegClass); 76 77 // Compute derived properties from the register classes 78 computeRegisterProperties(Subtarget.getRegisterInfo()); 79 80 setStackPointerRegisterToSaveRestore(ARC::SP); 81 82 setSchedulingPreference(Sched::Source); 83 84 // Use i32 for setcc operations results (slt, sgt, ...). 85 setBooleanContents(ZeroOrOneBooleanContent); 86 setBooleanVectorContents(ZeroOrOneBooleanContent); 87 88 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc) 89 setOperationAction(Opc, MVT::i32, Expand); 90 91 // Operations to get us off of the ground. 92 // Basic. 93 setOperationAction(ISD::ADD, MVT::i32, Legal); 94 setOperationAction(ISD::SUB, MVT::i32, Legal); 95 setOperationAction(ISD::AND, MVT::i32, Legal); 96 setOperationAction(ISD::SMAX, MVT::i32, Legal); 97 setOperationAction(ISD::SMIN, MVT::i32, Legal); 98 99 // Need barrel shifter. 100 setOperationAction(ISD::SHL, MVT::i32, Legal); 101 setOperationAction(ISD::SRA, MVT::i32, Legal); 102 setOperationAction(ISD::SRL, MVT::i32, Legal); 103 setOperationAction(ISD::ROTR, MVT::i32, Legal); 104 105 setOperationAction(ISD::Constant, MVT::i32, Legal); 106 setOperationAction(ISD::UNDEF, MVT::i32, Legal); 107 108 // Need multiplier 109 setOperationAction(ISD::MUL, MVT::i32, Legal); 110 setOperationAction(ISD::MULHS, MVT::i32, Legal); 111 setOperationAction(ISD::MULHU, MVT::i32, Legal); 112 setOperationAction(ISD::LOAD, MVT::i32, Legal); 113 setOperationAction(ISD::STORE, MVT::i32, Legal); 114 115 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 116 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 117 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 118 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 119 setOperationAction(ISD::JumpTable, MVT::i32, Custom); 120 121 // Have pseudo instruction for frame addresses. 122 setOperationAction(ISD::FRAMEADDR, MVT::i32, Legal); 123 // Custom lower global addresses. 124 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 125 126 // Expand var-args ops. 127 setOperationAction(ISD::VASTART, MVT::Other, Custom); 128 setOperationAction(ISD::VAEND, MVT::Other, Expand); 129 setOperationAction(ISD::VAARG, MVT::Other, Expand); 130 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 131 132 // Other expansions 133 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 134 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 135 136 // Sign extend inreg 137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Custom); 138 139 // TODO: Predicate with `options.hasBitScan() ? Legal : Expand` when 140 // the HasBitScan predicate is available. 141 setOperationAction(ISD::CTLZ, MVT::i32, Legal); 142 } 143 144 const char *ARCTargetLowering::getTargetNodeName(unsigned Opcode) const { 145 switch (Opcode) { 146 case ARCISD::BL: 147 return "ARCISD::BL"; 148 case ARCISD::CMOV: 149 return "ARCISD::CMOV"; 150 case ARCISD::CMP: 151 return "ARCISD::CMP"; 152 case ARCISD::BRcc: 153 return "ARCISD::BRcc"; 154 case ARCISD::RET: 155 return "ARCISD::RET"; 156 case ARCISD::GAWRAPPER: 157 return "ARCISD::GAWRAPPER"; 158 } 159 return nullptr; 160 } 161 162 //===----------------------------------------------------------------------===// 163 // Misc Lower Operation implementation 164 //===----------------------------------------------------------------------===// 165 166 SDValue ARCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 167 SDValue LHS = Op.getOperand(0); 168 SDValue RHS = Op.getOperand(1); 169 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 170 SDValue TVal = Op.getOperand(2); 171 SDValue FVal = Op.getOperand(3); 172 SDLoc dl(Op); 173 ARCCC::CondCode ArcCC = ISDCCtoARCCC(CC); 174 assert(LHS.getValueType() == MVT::i32 && "Only know how to SELECT_CC i32"); 175 SDValue Cmp = DAG.getNode(ARCISD::CMP, dl, MVT::Glue, LHS, RHS); 176 return DAG.getNode(ARCISD::CMOV, dl, TVal.getValueType(), TVal, FVal, 177 DAG.getConstant(ArcCC, dl, MVT::i32), Cmp); 178 } 179 180 SDValue ARCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, 181 SelectionDAG &DAG) const { 182 SDValue Op0 = Op.getOperand(0); 183 SDLoc dl(Op); 184 assert(Op.getValueType() == MVT::i32 && 185 "Unhandled target sign_extend_inreg."); 186 // These are legal 187 unsigned Width = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); 188 if (Width == 16 || Width == 8) 189 return Op; 190 if (Width >= 32) { 191 return {}; 192 } 193 SDValue LS = DAG.getNode(ISD::SHL, dl, MVT::i32, Op0, 194 DAG.getConstant(32 - Width, dl, MVT::i32)); 195 SDValue SR = DAG.getNode(ISD::SRA, dl, MVT::i32, LS, 196 DAG.getConstant(32 - Width, dl, MVT::i32)); 197 return SR; 198 } 199 200 SDValue ARCTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 201 SDValue Chain = Op.getOperand(0); 202 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 203 SDValue LHS = Op.getOperand(2); 204 SDValue RHS = Op.getOperand(3); 205 SDValue Dest = Op.getOperand(4); 206 SDLoc dl(Op); 207 ARCCC::CondCode arcCC = ISDCCtoARCCC(CC); 208 assert(LHS.getValueType() == MVT::i32 && "Only know how to BR_CC i32"); 209 return DAG.getNode(ARCISD::BRcc, dl, MVT::Other, Chain, Dest, LHS, RHS, 210 DAG.getConstant(arcCC, dl, MVT::i32)); 211 } 212 213 SDValue ARCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { 214 auto *N = cast<JumpTableSDNode>(Op); 215 SDValue GA = DAG.getTargetJumpTable(N->getIndex(), MVT::i32); 216 return DAG.getNode(ARCISD::GAWRAPPER, SDLoc(N), MVT::i32, GA); 217 } 218 219 #include "ARCGenCallingConv.inc" 220 221 //===----------------------------------------------------------------------===// 222 // Call Calling Convention Implementation 223 //===----------------------------------------------------------------------===// 224 225 /// ARC call implementation 226 SDValue ARCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 227 SmallVectorImpl<SDValue> &InVals) const { 228 SelectionDAG &DAG = CLI.DAG; 229 SDLoc &dl = CLI.DL; 230 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 231 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 232 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 233 SDValue Chain = CLI.Chain; 234 SDValue Callee = CLI.Callee; 235 CallingConv::ID CallConv = CLI.CallConv; 236 bool IsVarArg = CLI.IsVarArg; 237 bool &IsTailCall = CLI.IsTailCall; 238 239 IsTailCall = false; // Do not support tail calls yet. 240 241 SmallVector<CCValAssign, 16> ArgLocs; 242 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, 243 *DAG.getContext()); 244 245 CCInfo.AnalyzeCallOperands(Outs, CC_ARC); 246 247 SmallVector<CCValAssign, 16> RVLocs; 248 // Analyze return values to determine the number of bytes of stack required. 249 CCState RetCCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs, 250 *DAG.getContext()); 251 RetCCInfo.AllocateStack(CCInfo.getNextStackOffset(), Align(4)); 252 RetCCInfo.AnalyzeCallResult(Ins, RetCC_ARC); 253 254 // Get a count of how many bytes are to be pushed on the stack. 255 unsigned NumBytes = RetCCInfo.getNextStackOffset(); 256 auto PtrVT = getPointerTy(DAG.getDataLayout()); 257 258 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl); 259 260 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass; 261 SmallVector<SDValue, 12> MemOpChains; 262 263 SDValue StackPtr; 264 // Walk the register/memloc assignments, inserting copies/loads. 265 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 266 CCValAssign &VA = ArgLocs[i]; 267 SDValue Arg = OutVals[i]; 268 269 // Promote the value if needed. 270 switch (VA.getLocInfo()) { 271 default: 272 llvm_unreachable("Unknown loc info!"); 273 case CCValAssign::Full: 274 break; 275 case CCValAssign::SExt: 276 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 277 break; 278 case CCValAssign::ZExt: 279 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 280 break; 281 case CCValAssign::AExt: 282 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 283 break; 284 } 285 286 // Arguments that can be passed on register must be kept at 287 // RegsToPass vector 288 if (VA.isRegLoc()) { 289 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 290 } else { 291 assert(VA.isMemLoc() && "Must be register or memory argument."); 292 if (!StackPtr.getNode()) 293 StackPtr = DAG.getCopyFromReg(Chain, dl, ARC::SP, 294 getPointerTy(DAG.getDataLayout())); 295 // Calculate the stack position. 296 SDValue SOffset = DAG.getIntPtrConstant(VA.getLocMemOffset(), dl); 297 SDValue PtrOff = DAG.getNode( 298 ISD::ADD, dl, getPointerTy(DAG.getDataLayout()), StackPtr, SOffset); 299 300 SDValue Store = 301 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); 302 MemOpChains.push_back(Store); 303 IsTailCall = false; 304 } 305 } 306 307 // Transform all store nodes into one single node because 308 // all store nodes are independent of each other. 309 if (!MemOpChains.empty()) 310 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 311 312 // Build a sequence of copy-to-reg nodes chained together with token 313 // chain and flag operands which copy the outgoing args into registers. 314 // The InFlag in necessary since all emitted instructions must be 315 // stuck together. 316 SDValue Glue; 317 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 318 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 319 RegsToPass[i].second, Glue); 320 Glue = Chain.getValue(1); 321 } 322 323 // If the callee is a GlobalAddress node (quite common, every direct call is) 324 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 325 // Likewise ExternalSymbol -> TargetExternalSymbol. 326 bool IsDirect = true; 327 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) 328 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32); 329 else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) 330 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32); 331 else 332 IsDirect = false; 333 // Branch + Link = #chain, #target_address, #opt_in_flags... 334 // = Chain, Callee, Reg#1, Reg#2, ... 335 // 336 // Returns a chain & a flag for retval copy to use. 337 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 338 SmallVector<SDValue, 8> Ops; 339 Ops.push_back(Chain); 340 Ops.push_back(Callee); 341 342 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 343 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 344 RegsToPass[i].second.getValueType())); 345 346 // Add a register mask operand representing the call-preserved registers. 347 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 348 const uint32_t *Mask = 349 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv); 350 assert(Mask && "Missing call preserved mask for calling convention"); 351 Ops.push_back(DAG.getRegisterMask(Mask)); 352 353 if (Glue.getNode()) 354 Ops.push_back(Glue); 355 356 Chain = DAG.getNode(IsDirect ? ARCISD::BL : ARCISD::JL, dl, NodeTys, Ops); 357 Glue = Chain.getValue(1); 358 359 // Create the CALLSEQ_END node. 360 Chain = DAG.getCALLSEQ_END(Chain, DAG.getConstant(NumBytes, dl, PtrVT, true), 361 DAG.getConstant(0, dl, PtrVT, true), Glue, dl); 362 Glue = Chain.getValue(1); 363 364 // Handle result values, copying them out of physregs into vregs that we 365 // return. 366 if (IsTailCall) 367 return Chain; 368 return lowerCallResult(Chain, Glue, RVLocs, dl, DAG, InVals); 369 } 370 371 /// Lower the result values of a call into the appropriate copies out of 372 /// physical registers / memory locations. 373 static SDValue lowerCallResult(SDValue Chain, SDValue Glue, 374 const SmallVectorImpl<CCValAssign> &RVLocs, 375 SDLoc dl, SelectionDAG &DAG, 376 SmallVectorImpl<SDValue> &InVals) { 377 SmallVector<std::pair<int, unsigned>, 4> ResultMemLocs; 378 // Copy results out of physical registers. 379 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { 380 const CCValAssign &VA = RVLocs[i]; 381 if (VA.isRegLoc()) { 382 SDValue RetValue; 383 RetValue = 384 DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getValVT(), Glue); 385 Chain = RetValue.getValue(1); 386 Glue = RetValue.getValue(2); 387 InVals.push_back(RetValue); 388 } else { 389 assert(VA.isMemLoc() && "Must be memory location."); 390 ResultMemLocs.push_back( 391 std::make_pair(VA.getLocMemOffset(), InVals.size())); 392 393 // Reserve space for this result. 394 InVals.push_back(SDValue()); 395 } 396 } 397 398 // Copy results out of memory. 399 SmallVector<SDValue, 4> MemOpChains; 400 for (unsigned i = 0, e = ResultMemLocs.size(); i != e; ++i) { 401 int Offset = ResultMemLocs[i].first; 402 unsigned Index = ResultMemLocs[i].second; 403 SDValue StackPtr = DAG.getRegister(ARC::SP, MVT::i32); 404 SDValue SpLoc = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, 405 DAG.getConstant(Offset, dl, MVT::i32)); 406 SDValue Load = 407 DAG.getLoad(MVT::i32, dl, Chain, SpLoc, MachinePointerInfo()); 408 InVals[Index] = Load; 409 MemOpChains.push_back(Load.getValue(1)); 410 } 411 412 // Transform all loads nodes into one single node because 413 // all load nodes are independent of each other. 414 if (!MemOpChains.empty()) 415 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 416 417 return Chain; 418 } 419 420 //===----------------------------------------------------------------------===// 421 // Formal Arguments Calling Convention Implementation 422 //===----------------------------------------------------------------------===// 423 424 namespace { 425 426 struct ArgDataPair { 427 SDValue SDV; 428 ISD::ArgFlagsTy Flags; 429 }; 430 431 } // end anonymous namespace 432 433 /// ARC formal arguments implementation 434 SDValue ARCTargetLowering::LowerFormalArguments( 435 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 436 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, 437 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 438 switch (CallConv) { 439 default: 440 llvm_unreachable("Unsupported calling convention"); 441 case CallingConv::C: 442 case CallingConv::Fast: 443 return LowerCallArguments(Chain, CallConv, IsVarArg, Ins, dl, DAG, InVals); 444 } 445 } 446 447 /// Transform physical registers into virtual registers, and generate load 448 /// operations for argument places on the stack. 449 SDValue ARCTargetLowering::LowerCallArguments( 450 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 451 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc dl, SelectionDAG &DAG, 452 SmallVectorImpl<SDValue> &InVals) const { 453 MachineFunction &MF = DAG.getMachineFunction(); 454 MachineFrameInfo &MFI = MF.getFrameInfo(); 455 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 456 auto *AFI = MF.getInfo<ARCFunctionInfo>(); 457 458 // Assign locations to all of the incoming arguments. 459 SmallVector<CCValAssign, 16> ArgLocs; 460 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, 461 *DAG.getContext()); 462 463 CCInfo.AnalyzeFormalArguments(Ins, CC_ARC); 464 465 unsigned StackSlotSize = 4; 466 467 if (!IsVarArg) 468 AFI->setReturnStackOffset(CCInfo.getNextStackOffset()); 469 470 // All getCopyFromReg ops must precede any getMemcpys to prevent the 471 // scheduler clobbering a register before it has been copied. 472 // The stages are: 473 // 1. CopyFromReg (and load) arg & vararg registers. 474 // 2. Chain CopyFromReg nodes into a TokenFactor. 475 // 3. Memcpy 'byVal' args & push final InVals. 476 // 4. Chain mem ops nodes into a TokenFactor. 477 SmallVector<SDValue, 4> CFRegNode; 478 SmallVector<ArgDataPair, 4> ArgData; 479 SmallVector<SDValue, 4> MemOps; 480 481 // 1a. CopyFromReg (and load) arg registers. 482 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 483 CCValAssign &VA = ArgLocs[i]; 484 SDValue ArgIn; 485 486 if (VA.isRegLoc()) { 487 // Arguments passed in registers 488 EVT RegVT = VA.getLocVT(); 489 switch (RegVT.getSimpleVT().SimpleTy) { 490 default: { 491 LLVM_DEBUG(errs() << "LowerFormalArguments Unhandled argument type: " 492 << (unsigned)RegVT.getSimpleVT().SimpleTy << "\n"); 493 llvm_unreachable("Unhandled LowerFormalArguments type."); 494 } 495 case MVT::i32: 496 unsigned VReg = RegInfo.createVirtualRegister(&ARC::GPR32RegClass); 497 RegInfo.addLiveIn(VA.getLocReg(), VReg); 498 ArgIn = DAG.getCopyFromReg(Chain, dl, VReg, RegVT); 499 CFRegNode.push_back(ArgIn.getValue(ArgIn->getNumValues() - 1)); 500 } 501 } else { 502 // sanity check 503 assert(VA.isMemLoc()); 504 // Load the argument to a virtual register 505 unsigned ObjSize = VA.getLocVT().getStoreSize(); 506 assert((ObjSize <= StackSlotSize) && "Unhandled argument"); 507 508 // Create the frame index object for this incoming parameter... 509 int FI = MFI.CreateFixedObject(ObjSize, VA.getLocMemOffset(), true); 510 511 // Create the SelectionDAG nodes corresponding to a load 512 // from this parameter 513 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); 514 ArgIn = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN, 515 MachinePointerInfo::getFixedStack(MF, FI)); 516 } 517 const ArgDataPair ADP = {ArgIn, Ins[i].Flags}; 518 ArgData.push_back(ADP); 519 } 520 521 // 1b. CopyFromReg vararg registers. 522 if (IsVarArg) { 523 // Argument registers 524 static const MCPhysReg ArgRegs[] = {ARC::R0, ARC::R1, ARC::R2, ARC::R3, 525 ARC::R4, ARC::R5, ARC::R6, ARC::R7}; 526 auto *AFI = MF.getInfo<ARCFunctionInfo>(); 527 unsigned FirstVAReg = CCInfo.getFirstUnallocated(ArgRegs); 528 if (FirstVAReg < array_lengthof(ArgRegs)) { 529 int Offset = 0; 530 // Save remaining registers, storing higher register numbers at a higher 531 // address 532 // There are (array_lengthof(ArgRegs) - FirstVAReg) registers which 533 // need to be saved. 534 int VarFI = 535 MFI.CreateFixedObject((array_lengthof(ArgRegs) - FirstVAReg) * 4, 536 CCInfo.getNextStackOffset(), true); 537 AFI->setVarArgsFrameIndex(VarFI); 538 SDValue FIN = DAG.getFrameIndex(VarFI, MVT::i32); 539 for (unsigned i = FirstVAReg; i < array_lengthof(ArgRegs); i++) { 540 // Move argument from phys reg -> virt reg 541 unsigned VReg = RegInfo.createVirtualRegister(&ARC::GPR32RegClass); 542 RegInfo.addLiveIn(ArgRegs[i], VReg); 543 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 544 CFRegNode.push_back(Val.getValue(Val->getNumValues() - 1)); 545 SDValue VAObj = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN, 546 DAG.getConstant(Offset, dl, MVT::i32)); 547 // Move argument from virt reg -> stack 548 SDValue Store = 549 DAG.getStore(Val.getValue(1), dl, Val, VAObj, MachinePointerInfo()); 550 MemOps.push_back(Store); 551 Offset += 4; 552 } 553 } else { 554 llvm_unreachable("Too many var args parameters."); 555 } 556 } 557 558 // 2. Chain CopyFromReg nodes into a TokenFactor. 559 if (!CFRegNode.empty()) 560 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, CFRegNode); 561 562 // 3. Memcpy 'byVal' args & push final InVals. 563 // Aggregates passed "byVal" need to be copied by the callee. 564 // The callee will use a pointer to this copy, rather than the original 565 // pointer. 566 for (const auto &ArgDI : ArgData) { 567 if (ArgDI.Flags.isByVal() && ArgDI.Flags.getByValSize()) { 568 unsigned Size = ArgDI.Flags.getByValSize(); 569 Align Alignment = 570 std::max(Align(StackSlotSize), ArgDI.Flags.getNonZeroByValAlign()); 571 // Create a new object on the stack and copy the pointee into it. 572 int FI = MFI.CreateStackObject(Size, Alignment, false); 573 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); 574 InVals.push_back(FIN); 575 MemOps.push_back(DAG.getMemcpy( 576 Chain, dl, FIN, ArgDI.SDV, DAG.getConstant(Size, dl, MVT::i32), 577 Alignment, false, false, false, MachinePointerInfo(), 578 MachinePointerInfo())); 579 } else { 580 InVals.push_back(ArgDI.SDV); 581 } 582 } 583 584 // 4. Chain mem ops nodes into a TokenFactor. 585 if (!MemOps.empty()) { 586 MemOps.push_back(Chain); 587 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 588 } 589 590 return Chain; 591 } 592 593 //===----------------------------------------------------------------------===// 594 // Return Value Calling Convention Implementation 595 //===----------------------------------------------------------------------===// 596 597 bool ARCTargetLowering::CanLowerReturn( 598 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg, 599 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const { 600 SmallVector<CCValAssign, 16> RVLocs; 601 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context); 602 if (!CCInfo.CheckReturn(Outs, RetCC_ARC)) 603 return false; 604 if (CCInfo.getNextStackOffset() != 0 && IsVarArg) 605 return false; 606 return true; 607 } 608 609 SDValue 610 ARCTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 611 bool IsVarArg, 612 const SmallVectorImpl<ISD::OutputArg> &Outs, 613 const SmallVectorImpl<SDValue> &OutVals, 614 const SDLoc &dl, SelectionDAG &DAG) const { 615 auto *AFI = DAG.getMachineFunction().getInfo<ARCFunctionInfo>(); 616 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 617 618 // CCValAssign - represent the assignment of 619 // the return value to a location 620 SmallVector<CCValAssign, 16> RVLocs; 621 622 // CCState - Info about the registers and stack slot. 623 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs, 624 *DAG.getContext()); 625 626 // Analyze return values. 627 if (!IsVarArg) 628 CCInfo.AllocateStack(AFI->getReturnStackOffset(), Align(4)); 629 630 CCInfo.AnalyzeReturn(Outs, RetCC_ARC); 631 632 SDValue Flag; 633 SmallVector<SDValue, 4> RetOps(1, Chain); 634 SmallVector<SDValue, 4> MemOpChains; 635 // Handle return values that must be copied to memory. 636 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { 637 CCValAssign &VA = RVLocs[i]; 638 if (VA.isRegLoc()) 639 continue; 640 assert(VA.isMemLoc()); 641 if (IsVarArg) { 642 report_fatal_error("Can't return value from vararg function in memory"); 643 } 644 645 int Offset = VA.getLocMemOffset(); 646 unsigned ObjSize = VA.getLocVT().getStoreSize(); 647 // Create the frame index object for the memory location. 648 int FI = MFI.CreateFixedObject(ObjSize, Offset, false); 649 650 // Create a SelectionDAG node corresponding to a store 651 // to this memory location. 652 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); 653 MemOpChains.push_back(DAG.getStore( 654 Chain, dl, OutVals[i], FIN, 655 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI))); 656 } 657 658 // Transform all store nodes into one single node because 659 // all stores are independent of each other. 660 if (!MemOpChains.empty()) 661 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 662 663 // Now handle return values copied to registers. 664 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { 665 CCValAssign &VA = RVLocs[i]; 666 if (!VA.isRegLoc()) 667 continue; 668 // Copy the result values into the output registers. 669 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag); 670 671 // guarantee that all emitted copies are 672 // stuck together, avoiding something bad 673 Flag = Chain.getValue(1); 674 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 675 } 676 677 RetOps[0] = Chain; // Update chain. 678 679 // Add the flag if we have it. 680 if (Flag.getNode()) 681 RetOps.push_back(Flag); 682 683 // What to do with the RetOps? 684 return DAG.getNode(ARCISD::RET, dl, MVT::Other, RetOps); 685 } 686 687 //===----------------------------------------------------------------------===// 688 // Target Optimization Hooks 689 //===----------------------------------------------------------------------===// 690 691 SDValue ARCTargetLowering::PerformDAGCombine(SDNode *N, 692 DAGCombinerInfo &DCI) const { 693 return {}; 694 } 695 696 //===----------------------------------------------------------------------===// 697 // Addressing mode description hooks 698 //===----------------------------------------------------------------------===// 699 700 /// Return true if the addressing mode represented by AM is legal for this 701 /// target, for a load/store of the specified type. 702 bool ARCTargetLowering::isLegalAddressingMode(const DataLayout &DL, 703 const AddrMode &AM, Type *Ty, 704 unsigned AS, 705 Instruction *I) const { 706 return AM.Scale == 0; 707 } 708 709 // Don't emit tail calls for the time being. 710 bool ARCTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const { 711 return false; 712 } 713 714 SDValue ARCTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 715 const ARCRegisterInfo &ARI = *Subtarget.getRegisterInfo(); 716 MachineFunction &MF = DAG.getMachineFunction(); 717 MachineFrameInfo &MFI = MF.getFrameInfo(); 718 MFI.setFrameAddressIsTaken(true); 719 720 EVT VT = Op.getValueType(); 721 SDLoc dl(Op); 722 assert(cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0 && 723 "Only support lowering frame addr of current frame."); 724 Register FrameReg = ARI.getFrameRegister(MF); 725 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 726 } 727 728 SDValue ARCTargetLowering::LowerGlobalAddress(SDValue Op, 729 SelectionDAG &DAG) const { 730 const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op); 731 const GlobalValue *GV = GN->getGlobal(); 732 SDLoc dl(GN); 733 int64_t Offset = GN->getOffset(); 734 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, Offset); 735 return DAG.getNode(ARCISD::GAWRAPPER, dl, MVT::i32, GA); 736 } 737 738 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 739 MachineFunction &MF = DAG.getMachineFunction(); 740 auto *FuncInfo = MF.getInfo<ARCFunctionInfo>(); 741 742 // vastart just stores the address of the VarArgsFrameIndex slot into the 743 // memory location argument. 744 SDLoc dl(Op); 745 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 746 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 747 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 748 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 749 MachinePointerInfo(SV)); 750 } 751 752 SDValue ARCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 753 switch (Op.getOpcode()) { 754 case ISD::GlobalAddress: 755 return LowerGlobalAddress(Op, DAG); 756 case ISD::FRAMEADDR: 757 return LowerFRAMEADDR(Op, DAG); 758 case ISD::SELECT_CC: 759 return LowerSELECT_CC(Op, DAG); 760 case ISD::BR_CC: 761 return LowerBR_CC(Op, DAG); 762 case ISD::SIGN_EXTEND_INREG: 763 return LowerSIGN_EXTEND_INREG(Op, DAG); 764 case ISD::JumpTable: 765 return LowerJumpTable(Op, DAG); 766 case ISD::VASTART: 767 return LowerVASTART(Op, DAG); 768 default: 769 llvm_unreachable("unimplemented operand"); 770 } 771 } 772