1 //===--- AArch64CallLowering.cpp - Call lowering --------------------------===// 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 /// \file 10 /// This file implements the lowering of LLVM calls to machine code calls for 11 /// GlobalISel. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "AArch64CallLowering.h" 16 #include "AArch64ISelLowering.h" 17 #include "AArch64MachineFunctionInfo.h" 18 #include "AArch64Subtarget.h" 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/CodeGen/Analysis.h" 22 #include "llvm/CodeGen/CallingConvLower.h" 23 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 24 #include "llvm/CodeGen/GlobalISel/Utils.h" 25 #include "llvm/CodeGen/LowLevelType.h" 26 #include "llvm/CodeGen/MachineBasicBlock.h" 27 #include "llvm/CodeGen/MachineFrameInfo.h" 28 #include "llvm/CodeGen/MachineFunction.h" 29 #include "llvm/CodeGen/MachineInstrBuilder.h" 30 #include "llvm/CodeGen/MachineMemOperand.h" 31 #include "llvm/CodeGen/MachineOperand.h" 32 #include "llvm/CodeGen/MachineRegisterInfo.h" 33 #include "llvm/CodeGen/TargetRegisterInfo.h" 34 #include "llvm/CodeGen/TargetSubtargetInfo.h" 35 #include "llvm/CodeGen/ValueTypes.h" 36 #include "llvm/IR/Argument.h" 37 #include "llvm/IR/Attributes.h" 38 #include "llvm/IR/Function.h" 39 #include "llvm/IR/Type.h" 40 #include "llvm/IR/Value.h" 41 #include "llvm/Support/MachineValueType.h" 42 #include <algorithm> 43 #include <cassert> 44 #include <cstdint> 45 #include <iterator> 46 47 #define DEBUG_TYPE "aarch64-call-lowering" 48 49 using namespace llvm; 50 51 AArch64CallLowering::AArch64CallLowering(const AArch64TargetLowering &TLI) 52 : CallLowering(&TLI) {} 53 54 static void applyStackPassedSmallTypeDAGHack(EVT OrigVT, MVT &ValVT, 55 MVT &LocVT) { 56 // If ValVT is i1/i8/i16, we should set LocVT to i8/i8/i16. This is a legacy 57 // hack because the DAG calls the assignment function with pre-legalized 58 // register typed values, not the raw type. 59 // 60 // This hack is not applied to return values which are not passed on the 61 // stack. 62 if (OrigVT == MVT::i1 || OrigVT == MVT::i8) 63 ValVT = LocVT = MVT::i8; 64 else if (OrigVT == MVT::i16) 65 ValVT = LocVT = MVT::i16; 66 } 67 68 // Account for i1/i8/i16 stack passed value hack 69 static uint64_t getStackValueStoreSizeHack(const CCValAssign &VA) { 70 const MVT ValVT = VA.getValVT(); 71 return (ValVT == MVT::i8 || ValVT == MVT::i16) ? ValVT.getStoreSize() 72 : VA.getLocVT().getStoreSize(); 73 } 74 75 namespace { 76 struct IncomingArgHandler : public CallLowering::IncomingValueHandler { 77 IncomingArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 78 CCAssignFn *AssignFn) 79 : IncomingValueHandler(MIRBuilder, MRI, AssignFn), StackUsed(0) {} 80 81 Register getStackAddress(uint64_t Size, int64_t Offset, 82 MachinePointerInfo &MPO, 83 ISD::ArgFlagsTy Flags) override { 84 auto &MFI = MIRBuilder.getMF().getFrameInfo(); 85 86 // Byval is assumed to be writable memory, but other stack passed arguments 87 // are not. 88 const bool IsImmutable = !Flags.isByVal(); 89 90 int FI = MFI.CreateFixedObject(Size, Offset, IsImmutable); 91 MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI); 92 auto AddrReg = MIRBuilder.buildFrameIndex(LLT::pointer(0, 64), FI); 93 StackUsed = std::max(StackUsed, Size + Offset); 94 return AddrReg.getReg(0); 95 } 96 97 uint64_t getStackValueStoreSize(const CCValAssign &VA) const override { 98 return getStackValueStoreSizeHack(VA); 99 } 100 101 void assignValueToReg(Register ValVReg, Register PhysReg, 102 CCValAssign &VA) override { 103 markPhysRegUsed(PhysReg); 104 IncomingValueHandler::assignValueToReg(ValVReg, PhysReg, VA); 105 } 106 107 void assignValueToAddress(Register ValVReg, Register Addr, uint64_t MemSize, 108 MachinePointerInfo &MPO, CCValAssign &VA) override { 109 MachineFunction &MF = MIRBuilder.getMF(); 110 111 // The reported memory location may be wider than the value. 112 const LLT RealRegTy = MRI.getType(ValVReg); 113 LLT ValTy(VA.getValVT()); 114 LLT LocTy(VA.getLocVT()); 115 116 // Fixup the types for the DAG compatibility hack. 117 if (VA.getValVT() == MVT::i8 || VA.getValVT() == MVT::i16) 118 std::swap(ValTy, LocTy); 119 120 MemSize = LocTy.getSizeInBytes(); 121 122 auto MMO = MF.getMachineMemOperand( 123 MPO, MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, 124 MemSize, inferAlignFromPtrInfo(MF, MPO)); 125 126 if (RealRegTy.getSizeInBits() == ValTy.getSizeInBits()) { 127 // No extension information, or no extension necessary. Load into the 128 // incoming parameter type directly. 129 MIRBuilder.buildLoad(ValVReg, Addr, *MMO); 130 } else { 131 auto Tmp = MIRBuilder.buildLoad(LocTy, Addr, *MMO); 132 MIRBuilder.buildTrunc(ValVReg, Tmp); 133 } 134 } 135 136 bool assignArg(unsigned ValNo, EVT OrigVT, MVT ValVT, MVT LocVT, 137 CCValAssign::LocInfo LocInfo, 138 const CallLowering::ArgInfo &Info, ISD::ArgFlagsTy Flags, 139 CCState &State) override { 140 applyStackPassedSmallTypeDAGHack(OrigVT, ValVT, LocVT); 141 return AssignFn(ValNo, ValVT, LocVT, LocInfo, Flags, State); 142 } 143 144 /// How the physical register gets marked varies between formal 145 /// parameters (it's a basic-block live-in), and a call instruction 146 /// (it's an implicit-def of the BL). 147 virtual void markPhysRegUsed(MCRegister PhysReg) = 0; 148 149 uint64_t StackUsed; 150 }; 151 152 struct FormalArgHandler : public IncomingArgHandler { 153 FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 154 CCAssignFn *AssignFn) 155 : IncomingArgHandler(MIRBuilder, MRI, AssignFn) {} 156 157 void markPhysRegUsed(MCRegister PhysReg) override { 158 MIRBuilder.getMRI()->addLiveIn(PhysReg); 159 MIRBuilder.getMBB().addLiveIn(PhysReg); 160 } 161 }; 162 163 struct CallReturnHandler : public IncomingArgHandler { 164 CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 165 MachineInstrBuilder MIB, CCAssignFn *AssignFn) 166 : IncomingArgHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {} 167 168 void markPhysRegUsed(MCRegister PhysReg) override { 169 MIB.addDef(PhysReg, RegState::Implicit); 170 } 171 172 MachineInstrBuilder MIB; 173 }; 174 175 /// A special return arg handler for "returned" attribute arg calls. 176 struct ReturnedArgCallReturnHandler : public CallReturnHandler { 177 ReturnedArgCallReturnHandler(MachineIRBuilder &MIRBuilder, 178 MachineRegisterInfo &MRI, 179 MachineInstrBuilder MIB, CCAssignFn *AssignFn) 180 : CallReturnHandler(MIRBuilder, MRI, MIB, AssignFn) {} 181 182 void markPhysRegUsed(MCRegister PhysReg) override {} 183 }; 184 185 struct OutgoingArgHandler : public CallLowering::OutgoingValueHandler { 186 OutgoingArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 187 MachineInstrBuilder MIB, CCAssignFn *AssignFn, 188 CCAssignFn *AssignFnVarArg, bool IsReturn, 189 bool IsTailCall = false, int FPDiff = 0) 190 : OutgoingValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB), 191 AssignFnVarArg(AssignFnVarArg), IsReturn(IsReturn), 192 IsTailCall(IsTailCall), FPDiff(FPDiff), StackSize(0), SPReg(0), 193 Subtarget(MIRBuilder.getMF().getSubtarget<AArch64Subtarget>()) {} 194 195 Register getStackAddress(uint64_t Size, int64_t Offset, 196 MachinePointerInfo &MPO, 197 ISD::ArgFlagsTy Flags) override { 198 MachineFunction &MF = MIRBuilder.getMF(); 199 LLT p0 = LLT::pointer(0, 64); 200 LLT s64 = LLT::scalar(64); 201 202 if (IsTailCall) { 203 assert(!Flags.isByVal() && "byval unhandled with tail calls"); 204 205 Offset += FPDiff; 206 int FI = MF.getFrameInfo().CreateFixedObject(Size, Offset, true); 207 auto FIReg = MIRBuilder.buildFrameIndex(p0, FI); 208 MPO = MachinePointerInfo::getFixedStack(MF, FI); 209 return FIReg.getReg(0); 210 } 211 212 if (!SPReg) 213 SPReg = MIRBuilder.buildCopy(p0, Register(AArch64::SP)).getReg(0); 214 215 auto OffsetReg = MIRBuilder.buildConstant(s64, Offset); 216 217 auto AddrReg = MIRBuilder.buildPtrAdd(p0, SPReg, OffsetReg); 218 219 MPO = MachinePointerInfo::getStack(MF, Offset); 220 return AddrReg.getReg(0); 221 } 222 223 /// We need to fixup the reported store size for certain value types because 224 /// we invert the interpretation of ValVT and LocVT in certain cases. This is 225 /// for compatability with the DAG call lowering implementation, which we're 226 /// currently building on top of. 227 uint64_t getStackValueStoreSize(const CCValAssign &VA) const override { 228 return getStackValueStoreSizeHack(VA); 229 } 230 231 void assignValueToReg(Register ValVReg, Register PhysReg, 232 CCValAssign &VA) override { 233 MIB.addUse(PhysReg, RegState::Implicit); 234 Register ExtReg = extendRegister(ValVReg, VA); 235 MIRBuilder.buildCopy(PhysReg, ExtReg); 236 } 237 238 void assignValueToAddress(Register ValVReg, Register Addr, uint64_t Size, 239 MachinePointerInfo &MPO, CCValAssign &VA) override { 240 MachineFunction &MF = MIRBuilder.getMF(); 241 auto MMO = MF.getMachineMemOperand(MPO, MachineMemOperand::MOStore, Size, 242 inferAlignFromPtrInfo(MF, MPO)); 243 MIRBuilder.buildStore(ValVReg, Addr, *MMO); 244 } 245 246 void assignValueToAddress(const CallLowering::ArgInfo &Arg, unsigned RegIndex, 247 Register Addr, uint64_t MemSize, 248 MachinePointerInfo &MPO, CCValAssign &VA) override { 249 unsigned MaxSize = MemSize * 8; 250 // For varargs, we always want to extend them to 8 bytes, in which case 251 // we disable setting a max. 252 if (!Arg.IsFixed) 253 MaxSize = 0; 254 255 Register ValVReg = Arg.Regs[RegIndex]; 256 if (VA.getLocInfo() != CCValAssign::LocInfo::FPExt) { 257 MVT LocVT = VA.getLocVT(); 258 MVT ValVT = VA.getValVT(); 259 260 if (VA.getValVT() == MVT::i8 || VA.getValVT() == MVT::i16) { 261 std::swap(ValVT, LocVT); 262 MemSize = VA.getValVT().getStoreSize(); 263 } 264 265 ValVReg = extendRegister(ValVReg, VA, MaxSize); 266 const LLT RegTy = MRI.getType(ValVReg); 267 268 if (RegTy.getSizeInBits() < LocVT.getSizeInBits()) 269 ValVReg = MIRBuilder.buildTrunc(RegTy, ValVReg).getReg(0); 270 } else { 271 // The store does not cover the full allocated stack slot. 272 MemSize = VA.getValVT().getStoreSize(); 273 } 274 275 assignValueToAddress(ValVReg, Addr, MemSize, MPO, VA); 276 } 277 278 bool assignArg(unsigned ValNo, EVT OrigVT, MVT ValVT, MVT LocVT, 279 CCValAssign::LocInfo LocInfo, 280 const CallLowering::ArgInfo &Info, ISD::ArgFlagsTy Flags, 281 CCState &State) override { 282 bool IsCalleeWin = Subtarget.isCallingConvWin64(State.getCallingConv()); 283 bool UseVarArgsCCForFixed = IsCalleeWin && State.isVarArg(); 284 285 if (!State.isVarArg() && !UseVarArgsCCForFixed && !IsReturn) 286 applyStackPassedSmallTypeDAGHack(OrigVT, ValVT, LocVT); 287 288 bool Res; 289 if (Info.IsFixed && !UseVarArgsCCForFixed) 290 Res = AssignFn(ValNo, ValVT, LocVT, LocInfo, Flags, State); 291 else 292 Res = AssignFnVarArg(ValNo, ValVT, LocVT, LocInfo, Flags, State); 293 294 StackSize = State.getNextStackOffset(); 295 return Res; 296 } 297 298 MachineInstrBuilder MIB; 299 CCAssignFn *AssignFnVarArg; 300 301 /// Track if this is used for a return instead of function argument 302 /// passing. We apply a hack to i1/i8/i16 stack passed values, but do not use 303 /// stack passed returns for them and cannot apply the type adjustment. 304 bool IsReturn; 305 bool IsTailCall; 306 307 /// For tail calls, the byte offset of the call's argument area from the 308 /// callee's. Unused elsewhere. 309 int FPDiff; 310 uint64_t StackSize; 311 312 // Cache the SP register vreg if we need it more than once in this call site. 313 Register SPReg; 314 315 const AArch64Subtarget &Subtarget; 316 }; 317 } // namespace 318 319 static bool doesCalleeRestoreStack(CallingConv::ID CallConv, bool TailCallOpt) { 320 return CallConv == CallingConv::Fast && TailCallOpt; 321 } 322 323 bool AArch64CallLowering::lowerReturn(MachineIRBuilder &MIRBuilder, 324 const Value *Val, 325 ArrayRef<Register> VRegs, 326 FunctionLoweringInfo &FLI, 327 Register SwiftErrorVReg) const { 328 auto MIB = MIRBuilder.buildInstrNoInsert(AArch64::RET_ReallyLR); 329 assert(((Val && !VRegs.empty()) || (!Val && VRegs.empty())) && 330 "Return value without a vreg"); 331 332 bool Success = true; 333 if (!VRegs.empty()) { 334 MachineFunction &MF = MIRBuilder.getMF(); 335 const Function &F = MF.getFunction(); 336 337 MachineRegisterInfo &MRI = MF.getRegInfo(); 338 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>(); 339 CCAssignFn *AssignFn = TLI.CCAssignFnForReturn(F.getCallingConv()); 340 auto &DL = F.getParent()->getDataLayout(); 341 LLVMContext &Ctx = Val->getType()->getContext(); 342 343 SmallVector<EVT, 4> SplitEVTs; 344 ComputeValueVTs(TLI, DL, Val->getType(), SplitEVTs); 345 assert(VRegs.size() == SplitEVTs.size() && 346 "For each split Type there should be exactly one VReg."); 347 348 SmallVector<ArgInfo, 8> SplitArgs; 349 CallingConv::ID CC = F.getCallingConv(); 350 351 for (unsigned i = 0; i < SplitEVTs.size(); ++i) { 352 if (TLI.getNumRegistersForCallingConv(Ctx, CC, SplitEVTs[i]) > 1) { 353 LLVM_DEBUG(dbgs() << "Can't handle extended arg types which need split"); 354 return false; 355 } 356 357 Register CurVReg = VRegs[i]; 358 ArgInfo CurArgInfo = ArgInfo{CurVReg, SplitEVTs[i].getTypeForEVT(Ctx)}; 359 setArgFlags(CurArgInfo, AttributeList::ReturnIndex, DL, F); 360 361 // i1 is a special case because SDAG i1 true is naturally zero extended 362 // when widened using ANYEXT. We need to do it explicitly here. 363 if (MRI.getType(CurVReg).getSizeInBits() == 1) { 364 CurVReg = MIRBuilder.buildZExt(LLT::scalar(8), CurVReg).getReg(0); 365 } else { 366 // Some types will need extending as specified by the CC. 367 MVT NewVT = TLI.getRegisterTypeForCallingConv(Ctx, CC, SplitEVTs[i]); 368 if (EVT(NewVT) != SplitEVTs[i]) { 369 unsigned ExtendOp = TargetOpcode::G_ANYEXT; 370 if (F.getAttributes().hasAttribute(AttributeList::ReturnIndex, 371 Attribute::SExt)) 372 ExtendOp = TargetOpcode::G_SEXT; 373 else if (F.getAttributes().hasAttribute(AttributeList::ReturnIndex, 374 Attribute::ZExt)) 375 ExtendOp = TargetOpcode::G_ZEXT; 376 377 LLT NewLLT(NewVT); 378 LLT OldLLT(MVT::getVT(CurArgInfo.Ty)); 379 CurArgInfo.Ty = EVT(NewVT).getTypeForEVT(Ctx); 380 // Instead of an extend, we might have a vector type which needs 381 // padding with more elements, e.g. <2 x half> -> <4 x half>. 382 if (NewVT.isVector()) { 383 if (OldLLT.isVector()) { 384 if (NewLLT.getNumElements() > OldLLT.getNumElements()) { 385 // We don't handle VA types which are not exactly twice the 386 // size, but can easily be done in future. 387 if (NewLLT.getNumElements() != OldLLT.getNumElements() * 2) { 388 LLVM_DEBUG(dbgs() << "Outgoing vector ret has too many elts"); 389 return false; 390 } 391 auto Undef = MIRBuilder.buildUndef({OldLLT}); 392 CurVReg = 393 MIRBuilder.buildMerge({NewLLT}, {CurVReg, Undef}).getReg(0); 394 } else { 395 // Just do a vector extend. 396 CurVReg = MIRBuilder.buildInstr(ExtendOp, {NewLLT}, {CurVReg}) 397 .getReg(0); 398 } 399 } else if (NewLLT.getNumElements() == 2) { 400 // We need to pad a <1 x S> type to <2 x S>. Since we don't have 401 // <1 x S> vector types in GISel we use a build_vector instead 402 // of a vector merge/concat. 403 auto Undef = MIRBuilder.buildUndef({OldLLT}); 404 CurVReg = 405 MIRBuilder 406 .buildBuildVector({NewLLT}, {CurVReg, Undef.getReg(0)}) 407 .getReg(0); 408 } else { 409 LLVM_DEBUG(dbgs() << "Could not handle ret ty\n"); 410 return false; 411 } 412 } else { 413 // If the split EVT was a <1 x T> vector, and NewVT is T, then we 414 // don't have to do anything since we don't distinguish between the 415 // two. 416 if (NewLLT != MRI.getType(CurVReg)) { 417 // A scalar extend. 418 CurVReg = MIRBuilder.buildInstr(ExtendOp, {NewLLT}, {CurVReg}) 419 .getReg(0); 420 } 421 } 422 } 423 } 424 if (CurVReg != CurArgInfo.Regs[0]) { 425 CurArgInfo.Regs[0] = CurVReg; 426 // Reset the arg flags after modifying CurVReg. 427 setArgFlags(CurArgInfo, AttributeList::ReturnIndex, DL, F); 428 } 429 splitToValueTypes(CurArgInfo, SplitArgs, DL, CC); 430 } 431 432 OutgoingArgHandler Handler(MIRBuilder, MRI, MIB, AssignFn, AssignFn, 433 /*IsReturn*/ true); 434 Success = 435 handleAssignments(MIRBuilder, SplitArgs, Handler, CC, F.isVarArg()); 436 } 437 438 if (SwiftErrorVReg) { 439 MIB.addUse(AArch64::X21, RegState::Implicit); 440 MIRBuilder.buildCopy(AArch64::X21, SwiftErrorVReg); 441 } 442 443 MIRBuilder.insertInstr(MIB); 444 return Success; 445 } 446 447 /// Helper function to compute forwarded registers for musttail calls. Computes 448 /// the forwarded registers, sets MBB liveness, and emits COPY instructions that 449 /// can be used to save + restore registers later. 450 static void handleMustTailForwardedRegisters(MachineIRBuilder &MIRBuilder, 451 CCAssignFn *AssignFn) { 452 MachineBasicBlock &MBB = MIRBuilder.getMBB(); 453 MachineFunction &MF = MIRBuilder.getMF(); 454 MachineFrameInfo &MFI = MF.getFrameInfo(); 455 456 if (!MFI.hasMustTailInVarArgFunc()) 457 return; 458 459 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 460 const Function &F = MF.getFunction(); 461 assert(F.isVarArg() && "Expected F to be vararg?"); 462 463 // Compute the set of forwarded registers. The rest are scratch. 464 SmallVector<CCValAssign, 16> ArgLocs; 465 CCState CCInfo(F.getCallingConv(), /*IsVarArg=*/true, MF, ArgLocs, 466 F.getContext()); 467 SmallVector<MVT, 2> RegParmTypes; 468 RegParmTypes.push_back(MVT::i64); 469 RegParmTypes.push_back(MVT::f128); 470 471 // Later on, we can use this vector to restore the registers if necessary. 472 SmallVectorImpl<ForwardedRegister> &Forwards = 473 FuncInfo->getForwardedMustTailRegParms(); 474 CCInfo.analyzeMustTailForwardedRegisters(Forwards, RegParmTypes, AssignFn); 475 476 // Conservatively forward X8, since it might be used for an aggregate 477 // return. 478 if (!CCInfo.isAllocated(AArch64::X8)) { 479 Register X8VReg = MF.addLiveIn(AArch64::X8, &AArch64::GPR64RegClass); 480 Forwards.push_back(ForwardedRegister(X8VReg, AArch64::X8, MVT::i64)); 481 } 482 483 // Add the forwards to the MachineBasicBlock and MachineFunction. 484 for (const auto &F : Forwards) { 485 MBB.addLiveIn(F.PReg); 486 MIRBuilder.buildCopy(Register(F.VReg), Register(F.PReg)); 487 } 488 } 489 490 bool AArch64CallLowering::fallBackToDAGISel(const MachineFunction &MF) const { 491 auto &F = MF.getFunction(); 492 if (isa<ScalableVectorType>(F.getReturnType())) 493 return true; 494 if (llvm::any_of(F.args(), [](const Argument &A) { 495 return isa<ScalableVectorType>(A.getType()); 496 })) 497 return true; 498 const auto &ST = MF.getSubtarget<AArch64Subtarget>(); 499 if (!ST.hasNEON() || !ST.hasFPARMv8()) { 500 LLVM_DEBUG(dbgs() << "Falling back to SDAG because we don't support no-NEON\n"); 501 return true; 502 } 503 return false; 504 } 505 506 bool AArch64CallLowering::lowerFormalArguments( 507 MachineIRBuilder &MIRBuilder, const Function &F, 508 ArrayRef<ArrayRef<Register>> VRegs, FunctionLoweringInfo &FLI) const { 509 MachineFunction &MF = MIRBuilder.getMF(); 510 MachineBasicBlock &MBB = MIRBuilder.getMBB(); 511 MachineRegisterInfo &MRI = MF.getRegInfo(); 512 auto &DL = F.getParent()->getDataLayout(); 513 514 SmallVector<ArgInfo, 8> SplitArgs; 515 unsigned i = 0; 516 for (auto &Arg : F.args()) { 517 if (DL.getTypeStoreSize(Arg.getType()).isZero()) 518 continue; 519 520 ArgInfo OrigArg{VRegs[i], Arg}; 521 setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, F); 522 523 splitToValueTypes(OrigArg, SplitArgs, DL, F.getCallingConv()); 524 ++i; 525 } 526 527 if (!MBB.empty()) 528 MIRBuilder.setInstr(*MBB.begin()); 529 530 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>(); 531 CCAssignFn *AssignFn = 532 TLI.CCAssignFnForCall(F.getCallingConv(), /*IsVarArg=*/false); 533 534 FormalArgHandler Handler(MIRBuilder, MRI, AssignFn); 535 if (!handleAssignments(MIRBuilder, SplitArgs, Handler, F.getCallingConv(), 536 F.isVarArg())) 537 return false; 538 539 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 540 uint64_t StackOffset = Handler.StackUsed; 541 if (F.isVarArg()) { 542 auto &Subtarget = MF.getSubtarget<AArch64Subtarget>(); 543 if (!Subtarget.isTargetDarwin()) { 544 // FIXME: we need to reimplement saveVarArgsRegisters from 545 // AArch64ISelLowering. 546 return false; 547 } 548 549 // We currently pass all varargs at 8-byte alignment, or 4 in ILP32. 550 StackOffset = alignTo(Handler.StackUsed, Subtarget.isTargetILP32() ? 4 : 8); 551 552 auto &MFI = MIRBuilder.getMF().getFrameInfo(); 553 FuncInfo->setVarArgsStackIndex(MFI.CreateFixedObject(4, StackOffset, true)); 554 } 555 556 if (doesCalleeRestoreStack(F.getCallingConv(), 557 MF.getTarget().Options.GuaranteedTailCallOpt)) { 558 // We have a non-standard ABI, so why not make full use of the stack that 559 // we're going to pop? It must be aligned to 16 B in any case. 560 StackOffset = alignTo(StackOffset, 16); 561 562 // If we're expected to restore the stack (e.g. fastcc), then we'll be 563 // adding a multiple of 16. 564 FuncInfo->setArgumentStackToRestore(StackOffset); 565 566 // Our own callers will guarantee that the space is free by giving an 567 // aligned value to CALLSEQ_START. 568 } 569 570 // When we tail call, we need to check if the callee's arguments 571 // will fit on the caller's stack. So, whenever we lower formal arguments, 572 // we should keep track of this information, since we might lower a tail call 573 // in this function later. 574 FuncInfo->setBytesInStackArgArea(StackOffset); 575 576 auto &Subtarget = MF.getSubtarget<AArch64Subtarget>(); 577 if (Subtarget.hasCustomCallingConv()) 578 Subtarget.getRegisterInfo()->UpdateCustomCalleeSavedRegs(MF); 579 580 handleMustTailForwardedRegisters(MIRBuilder, AssignFn); 581 582 // Move back to the end of the basic block. 583 MIRBuilder.setMBB(MBB); 584 585 return true; 586 } 587 588 /// Return true if the calling convention is one that we can guarantee TCO for. 589 static bool canGuaranteeTCO(CallingConv::ID CC) { 590 return CC == CallingConv::Fast; 591 } 592 593 /// Return true if we might ever do TCO for calls with this calling convention. 594 static bool mayTailCallThisCC(CallingConv::ID CC) { 595 switch (CC) { 596 case CallingConv::C: 597 case CallingConv::PreserveMost: 598 case CallingConv::Swift: 599 return true; 600 default: 601 return canGuaranteeTCO(CC); 602 } 603 } 604 605 /// Returns a pair containing the fixed CCAssignFn and the vararg CCAssignFn for 606 /// CC. 607 static std::pair<CCAssignFn *, CCAssignFn *> 608 getAssignFnsForCC(CallingConv::ID CC, const AArch64TargetLowering &TLI) { 609 return {TLI.CCAssignFnForCall(CC, false), TLI.CCAssignFnForCall(CC, true)}; 610 } 611 612 bool AArch64CallLowering::doCallerAndCalleePassArgsTheSameWay( 613 CallLoweringInfo &Info, MachineFunction &MF, 614 SmallVectorImpl<ArgInfo> &InArgs) const { 615 const Function &CallerF = MF.getFunction(); 616 CallingConv::ID CalleeCC = Info.CallConv; 617 CallingConv::ID CallerCC = CallerF.getCallingConv(); 618 619 // If the calling conventions match, then everything must be the same. 620 if (CalleeCC == CallerCC) 621 return true; 622 623 // Check if the caller and callee will handle arguments in the same way. 624 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>(); 625 CCAssignFn *CalleeAssignFnFixed; 626 CCAssignFn *CalleeAssignFnVarArg; 627 std::tie(CalleeAssignFnFixed, CalleeAssignFnVarArg) = 628 getAssignFnsForCC(CalleeCC, TLI); 629 630 CCAssignFn *CallerAssignFnFixed; 631 CCAssignFn *CallerAssignFnVarArg; 632 std::tie(CallerAssignFnFixed, CallerAssignFnVarArg) = 633 getAssignFnsForCC(CallerCC, TLI); 634 635 if (!resultsCompatible(Info, MF, InArgs, *CalleeAssignFnFixed, 636 *CalleeAssignFnVarArg, *CallerAssignFnFixed, 637 *CallerAssignFnVarArg)) 638 return false; 639 640 // Make sure that the caller and callee preserve all of the same registers. 641 auto TRI = MF.getSubtarget<AArch64Subtarget>().getRegisterInfo(); 642 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 643 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); 644 if (MF.getSubtarget<AArch64Subtarget>().hasCustomCallingConv()) { 645 TRI->UpdateCustomCallPreservedMask(MF, &CallerPreserved); 646 TRI->UpdateCustomCallPreservedMask(MF, &CalleePreserved); 647 } 648 649 return TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved); 650 } 651 652 bool AArch64CallLowering::areCalleeOutgoingArgsTailCallable( 653 CallLoweringInfo &Info, MachineFunction &MF, 654 SmallVectorImpl<ArgInfo> &OutArgs) const { 655 // If there are no outgoing arguments, then we are done. 656 if (OutArgs.empty()) 657 return true; 658 659 const Function &CallerF = MF.getFunction(); 660 CallingConv::ID CalleeCC = Info.CallConv; 661 CallingConv::ID CallerCC = CallerF.getCallingConv(); 662 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>(); 663 664 CCAssignFn *AssignFnFixed; 665 CCAssignFn *AssignFnVarArg; 666 std::tie(AssignFnFixed, AssignFnVarArg) = getAssignFnsForCC(CalleeCC, TLI); 667 668 // We have outgoing arguments. Make sure that we can tail call with them. 669 SmallVector<CCValAssign, 16> OutLocs; 670 CCState OutInfo(CalleeCC, false, MF, OutLocs, CallerF.getContext()); 671 672 if (!analyzeArgInfo(OutInfo, OutArgs, *AssignFnFixed, *AssignFnVarArg)) { 673 LLVM_DEBUG(dbgs() << "... Could not analyze call operands.\n"); 674 return false; 675 } 676 677 // Make sure that they can fit on the caller's stack. 678 const AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 679 if (OutInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea()) { 680 LLVM_DEBUG(dbgs() << "... Cannot fit call operands on caller's stack.\n"); 681 return false; 682 } 683 684 // Verify that the parameters in callee-saved registers match. 685 // TODO: Port this over to CallLowering as general code once swiftself is 686 // supported. 687 auto TRI = MF.getSubtarget<AArch64Subtarget>().getRegisterInfo(); 688 const uint32_t *CallerPreservedMask = TRI->getCallPreservedMask(MF, CallerCC); 689 MachineRegisterInfo &MRI = MF.getRegInfo(); 690 691 if (Info.IsVarArg) { 692 // Be conservative and disallow variadic memory operands to match SDAG's 693 // behaviour. 694 // FIXME: If the caller's calling convention is C, then we can 695 // potentially use its argument area. However, for cases like fastcc, 696 // we can't do anything. 697 for (unsigned i = 0; i < OutLocs.size(); ++i) { 698 auto &ArgLoc = OutLocs[i]; 699 if (ArgLoc.isRegLoc()) 700 continue; 701 702 LLVM_DEBUG( 703 dbgs() 704 << "... Cannot tail call vararg function with stack arguments\n"); 705 return false; 706 } 707 } 708 709 return parametersInCSRMatch(MRI, CallerPreservedMask, OutLocs, OutArgs); 710 } 711 712 bool AArch64CallLowering::isEligibleForTailCallOptimization( 713 MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info, 714 SmallVectorImpl<ArgInfo> &InArgs, 715 SmallVectorImpl<ArgInfo> &OutArgs) const { 716 717 // Must pass all target-independent checks in order to tail call optimize. 718 if (!Info.IsTailCall) 719 return false; 720 721 CallingConv::ID CalleeCC = Info.CallConv; 722 MachineFunction &MF = MIRBuilder.getMF(); 723 const Function &CallerF = MF.getFunction(); 724 725 LLVM_DEBUG(dbgs() << "Attempting to lower call as tail call\n"); 726 727 if (Info.SwiftErrorVReg) { 728 // TODO: We should handle this. 729 // Note that this is also handled by the check for no outgoing arguments. 730 // Proactively disabling this though, because the swifterror handling in 731 // lowerCall inserts a COPY *after* the location of the call. 732 LLVM_DEBUG(dbgs() << "... Cannot handle tail calls with swifterror yet.\n"); 733 return false; 734 } 735 736 if (!mayTailCallThisCC(CalleeCC)) { 737 LLVM_DEBUG(dbgs() << "... Calling convention cannot be tail called.\n"); 738 return false; 739 } 740 741 // Byval parameters hand the function a pointer directly into the stack area 742 // we want to reuse during a tail call. Working around this *is* possible (see 743 // X86). 744 // 745 // FIXME: In AArch64ISelLowering, this isn't worked around. Can/should we try 746 // it? 747 // 748 // On Windows, "inreg" attributes signify non-aggregate indirect returns. 749 // In this case, it is necessary to save/restore X0 in the callee. Tail 750 // call opt interferes with this. So we disable tail call opt when the 751 // caller has an argument with "inreg" attribute. 752 // 753 // FIXME: Check whether the callee also has an "inreg" argument. 754 // 755 // When the caller has a swifterror argument, we don't want to tail call 756 // because would have to move into the swifterror register before the 757 // tail call. 758 if (any_of(CallerF.args(), [](const Argument &A) { 759 return A.hasByValAttr() || A.hasInRegAttr() || A.hasSwiftErrorAttr(); 760 })) { 761 LLVM_DEBUG(dbgs() << "... Cannot tail call from callers with byval, " 762 "inreg, or swifterror arguments\n"); 763 return false; 764 } 765 766 // Externally-defined functions with weak linkage should not be 767 // tail-called on AArch64 when the OS does not support dynamic 768 // pre-emption of symbols, as the AAELF spec requires normal calls 769 // to undefined weak functions to be replaced with a NOP or jump to the 770 // next instruction. The behaviour of branch instructions in this 771 // situation (as used for tail calls) is implementation-defined, so we 772 // cannot rely on the linker replacing the tail call with a return. 773 if (Info.Callee.isGlobal()) { 774 const GlobalValue *GV = Info.Callee.getGlobal(); 775 const Triple &TT = MF.getTarget().getTargetTriple(); 776 if (GV->hasExternalWeakLinkage() && 777 (!TT.isOSWindows() || TT.isOSBinFormatELF() || 778 TT.isOSBinFormatMachO())) { 779 LLVM_DEBUG(dbgs() << "... Cannot tail call externally-defined function " 780 "with weak linkage for this OS.\n"); 781 return false; 782 } 783 } 784 785 // If we have -tailcallopt, then we're done. 786 if (MF.getTarget().Options.GuaranteedTailCallOpt) 787 return canGuaranteeTCO(CalleeCC) && CalleeCC == CallerF.getCallingConv(); 788 789 // We don't have -tailcallopt, so we're allowed to change the ABI (sibcall). 790 // Try to find cases where we can do that. 791 792 // I want anyone implementing a new calling convention to think long and hard 793 // about this assert. 794 assert((!Info.IsVarArg || CalleeCC == CallingConv::C) && 795 "Unexpected variadic calling convention"); 796 797 // Verify that the incoming and outgoing arguments from the callee are 798 // safe to tail call. 799 if (!doCallerAndCalleePassArgsTheSameWay(Info, MF, InArgs)) { 800 LLVM_DEBUG( 801 dbgs() 802 << "... Caller and callee have incompatible calling conventions.\n"); 803 return false; 804 } 805 806 if (!areCalleeOutgoingArgsTailCallable(Info, MF, OutArgs)) 807 return false; 808 809 LLVM_DEBUG( 810 dbgs() << "... Call is eligible for tail call optimization.\n"); 811 return true; 812 } 813 814 static unsigned getCallOpcode(const MachineFunction &CallerF, bool IsIndirect, 815 bool IsTailCall) { 816 if (!IsTailCall) 817 return IsIndirect ? getBLRCallOpcode(CallerF) : (unsigned)AArch64::BL; 818 819 if (!IsIndirect) 820 return AArch64::TCRETURNdi; 821 822 // When BTI is enabled, we need to use TCRETURNriBTI to make sure that we use 823 // x16 or x17. 824 if (CallerF.getInfo<AArch64FunctionInfo>()->branchTargetEnforcement()) 825 return AArch64::TCRETURNriBTI; 826 827 return AArch64::TCRETURNri; 828 } 829 830 static const uint32_t * 831 getMaskForArgs(SmallVectorImpl<AArch64CallLowering::ArgInfo> &OutArgs, 832 AArch64CallLowering::CallLoweringInfo &Info, 833 const AArch64RegisterInfo &TRI, MachineFunction &MF) { 834 const uint32_t *Mask; 835 if (!OutArgs.empty() && OutArgs[0].Flags[0].isReturned()) { 836 // For 'this' returns, use the X0-preserving mask if applicable 837 Mask = TRI.getThisReturnPreservedMask(MF, Info.CallConv); 838 if (!Mask) { 839 OutArgs[0].Flags[0].setReturned(false); 840 Mask = TRI.getCallPreservedMask(MF, Info.CallConv); 841 } 842 } else { 843 Mask = TRI.getCallPreservedMask(MF, Info.CallConv); 844 } 845 return Mask; 846 } 847 848 bool AArch64CallLowering::lowerTailCall( 849 MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info, 850 SmallVectorImpl<ArgInfo> &OutArgs) const { 851 MachineFunction &MF = MIRBuilder.getMF(); 852 const Function &F = MF.getFunction(); 853 MachineRegisterInfo &MRI = MF.getRegInfo(); 854 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>(); 855 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>(); 856 857 // True when we're tail calling, but without -tailcallopt. 858 bool IsSibCall = !MF.getTarget().Options.GuaranteedTailCallOpt; 859 860 // TODO: Right now, regbankselect doesn't know how to handle the rtcGPR64 861 // register class. Until we can do that, we should fall back here. 862 if (MF.getInfo<AArch64FunctionInfo>()->branchTargetEnforcement()) { 863 LLVM_DEBUG( 864 dbgs() << "Cannot lower indirect tail calls with BTI enabled yet.\n"); 865 return false; 866 } 867 868 // Find out which ABI gets to decide where things go. 869 CallingConv::ID CalleeCC = Info.CallConv; 870 CCAssignFn *AssignFnFixed; 871 CCAssignFn *AssignFnVarArg; 872 std::tie(AssignFnFixed, AssignFnVarArg) = getAssignFnsForCC(CalleeCC, TLI); 873 874 MachineInstrBuilder CallSeqStart; 875 if (!IsSibCall) 876 CallSeqStart = MIRBuilder.buildInstr(AArch64::ADJCALLSTACKDOWN); 877 878 unsigned Opc = getCallOpcode(MF, Info.Callee.isReg(), true); 879 auto MIB = MIRBuilder.buildInstrNoInsert(Opc); 880 MIB.add(Info.Callee); 881 882 // Byte offset for the tail call. When we are sibcalling, this will always 883 // be 0. 884 MIB.addImm(0); 885 886 // Tell the call which registers are clobbered. 887 auto TRI = MF.getSubtarget<AArch64Subtarget>().getRegisterInfo(); 888 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CalleeCC); 889 if (MF.getSubtarget<AArch64Subtarget>().hasCustomCallingConv()) 890 TRI->UpdateCustomCallPreservedMask(MF, &Mask); 891 MIB.addRegMask(Mask); 892 893 if (TRI->isAnyArgRegReserved(MF)) 894 TRI->emitReservedArgRegCallError(MF); 895 896 // FPDiff is the byte offset of the call's argument area from the callee's. 897 // Stores to callee stack arguments will be placed in FixedStackSlots offset 898 // by this amount for a tail call. In a sibling call it must be 0 because the 899 // caller will deallocate the entire stack and the callee still expects its 900 // arguments to begin at SP+0. 901 int FPDiff = 0; 902 903 // This will be 0 for sibcalls, potentially nonzero for tail calls produced 904 // by -tailcallopt. For sibcalls, the memory operands for the call are 905 // already available in the caller's incoming argument space. 906 unsigned NumBytes = 0; 907 if (!IsSibCall) { 908 // We aren't sibcalling, so we need to compute FPDiff. We need to do this 909 // before handling assignments, because FPDiff must be known for memory 910 // arguments. 911 unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea(); 912 SmallVector<CCValAssign, 16> OutLocs; 913 CCState OutInfo(CalleeCC, false, MF, OutLocs, F.getContext()); 914 analyzeArgInfo(OutInfo, OutArgs, *AssignFnFixed, *AssignFnVarArg); 915 916 // The callee will pop the argument stack as a tail call. Thus, we must 917 // keep it 16-byte aligned. 918 NumBytes = alignTo(OutInfo.getNextStackOffset(), 16); 919 920 // FPDiff will be negative if this tail call requires more space than we 921 // would automatically have in our incoming argument space. Positive if we 922 // actually shrink the stack. 923 FPDiff = NumReusableBytes - NumBytes; 924 925 // The stack pointer must be 16-byte aligned at all times it's used for a 926 // memory operation, which in practice means at *all* times and in 927 // particular across call boundaries. Therefore our own arguments started at 928 // a 16-byte aligned SP and the delta applied for the tail call should 929 // satisfy the same constraint. 930 assert(FPDiff % 16 == 0 && "unaligned stack on tail call"); 931 } 932 933 const auto &Forwards = FuncInfo->getForwardedMustTailRegParms(); 934 935 // Do the actual argument marshalling. 936 OutgoingArgHandler Handler(MIRBuilder, MRI, MIB, AssignFnFixed, 937 AssignFnVarArg, /*IsReturn*/ false, 938 /*IsTailCall*/ true, FPDiff); 939 if (!handleAssignments(MIRBuilder, OutArgs, Handler, CalleeCC, Info.IsVarArg)) 940 return false; 941 942 Mask = getMaskForArgs(OutArgs, Info, *TRI, MF); 943 944 if (Info.IsVarArg && Info.IsMustTailCall) { 945 // Now we know what's being passed to the function. Add uses to the call for 946 // the forwarded registers that we *aren't* passing as parameters. This will 947 // preserve the copies we build earlier. 948 for (const auto &F : Forwards) { 949 Register ForwardedReg = F.PReg; 950 // If the register is already passed, or aliases a register which is 951 // already being passed, then skip it. 952 if (any_of(MIB->uses(), [&ForwardedReg, &TRI](const MachineOperand &Use) { 953 if (!Use.isReg()) 954 return false; 955 return TRI->regsOverlap(Use.getReg(), ForwardedReg); 956 })) 957 continue; 958 959 // We aren't passing it already, so we should add it to the call. 960 MIRBuilder.buildCopy(ForwardedReg, Register(F.VReg)); 961 MIB.addReg(ForwardedReg, RegState::Implicit); 962 } 963 } 964 965 // If we have -tailcallopt, we need to adjust the stack. We'll do the call 966 // sequence start and end here. 967 if (!IsSibCall) { 968 MIB->getOperand(1).setImm(FPDiff); 969 CallSeqStart.addImm(NumBytes).addImm(0); 970 // End the call sequence *before* emitting the call. Normally, we would 971 // tidy the frame up after the call. However, here, we've laid out the 972 // parameters so that when SP is reset, they will be in the correct 973 // location. 974 MIRBuilder.buildInstr(AArch64::ADJCALLSTACKUP).addImm(NumBytes).addImm(0); 975 } 976 977 // Now we can add the actual call instruction to the correct basic block. 978 MIRBuilder.insertInstr(MIB); 979 980 // If Callee is a reg, since it is used by a target specific instruction, 981 // it must have a register class matching the constraint of that instruction. 982 if (Info.Callee.isReg()) 983 constrainOperandRegClass(MF, *TRI, MRI, *MF.getSubtarget().getInstrInfo(), 984 *MF.getSubtarget().getRegBankInfo(), *MIB, 985 MIB->getDesc(), Info.Callee, 0); 986 987 MF.getFrameInfo().setHasTailCall(); 988 Info.LoweredTailCall = true; 989 return true; 990 } 991 992 bool AArch64CallLowering::lowerCall(MachineIRBuilder &MIRBuilder, 993 CallLoweringInfo &Info) const { 994 MachineFunction &MF = MIRBuilder.getMF(); 995 const Function &F = MF.getFunction(); 996 MachineRegisterInfo &MRI = MF.getRegInfo(); 997 auto &DL = F.getParent()->getDataLayout(); 998 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>(); 999 1000 SmallVector<ArgInfo, 8> OutArgs; 1001 for (auto &OrigArg : Info.OrigArgs) { 1002 splitToValueTypes(OrigArg, OutArgs, DL, Info.CallConv); 1003 // AAPCS requires that we zero-extend i1 to 8 bits by the caller. 1004 if (OrigArg.Ty->isIntegerTy(1)) 1005 OutArgs.back().Flags[0].setZExt(); 1006 } 1007 1008 SmallVector<ArgInfo, 8> InArgs; 1009 if (!Info.OrigRet.Ty->isVoidTy()) 1010 splitToValueTypes(Info.OrigRet, InArgs, DL, Info.CallConv); 1011 1012 // If we can lower as a tail call, do that instead. 1013 bool CanTailCallOpt = 1014 isEligibleForTailCallOptimization(MIRBuilder, Info, InArgs, OutArgs); 1015 1016 // We must emit a tail call if we have musttail. 1017 if (Info.IsMustTailCall && !CanTailCallOpt) { 1018 // There are types of incoming/outgoing arguments we can't handle yet, so 1019 // it doesn't make sense to actually die here like in ISelLowering. Instead, 1020 // fall back to SelectionDAG and let it try to handle this. 1021 LLVM_DEBUG(dbgs() << "Failed to lower musttail call as tail call\n"); 1022 return false; 1023 } 1024 1025 if (CanTailCallOpt) 1026 return lowerTailCall(MIRBuilder, Info, OutArgs); 1027 1028 // Find out which ABI gets to decide where things go. 1029 CCAssignFn *AssignFnFixed; 1030 CCAssignFn *AssignFnVarArg; 1031 std::tie(AssignFnFixed, AssignFnVarArg) = 1032 getAssignFnsForCC(Info.CallConv, TLI); 1033 1034 MachineInstrBuilder CallSeqStart; 1035 CallSeqStart = MIRBuilder.buildInstr(AArch64::ADJCALLSTACKDOWN); 1036 1037 // Create a temporarily-floating call instruction so we can add the implicit 1038 // uses of arg registers. 1039 unsigned Opc = getCallOpcode(MF, Info.Callee.isReg(), false); 1040 1041 auto MIB = MIRBuilder.buildInstrNoInsert(Opc); 1042 MIB.add(Info.Callee); 1043 1044 // Tell the call which registers are clobbered. 1045 const uint32_t *Mask; 1046 const auto *TRI = MF.getSubtarget<AArch64Subtarget>().getRegisterInfo(); 1047 1048 // Do the actual argument marshalling. 1049 OutgoingArgHandler Handler(MIRBuilder, MRI, MIB, AssignFnFixed, 1050 AssignFnVarArg, /*IsReturn*/ false); 1051 if (!handleAssignments(MIRBuilder, OutArgs, Handler, Info.CallConv, 1052 Info.IsVarArg)) 1053 return false; 1054 1055 Mask = getMaskForArgs(OutArgs, Info, *TRI, MF); 1056 1057 if (MF.getSubtarget<AArch64Subtarget>().hasCustomCallingConv()) 1058 TRI->UpdateCustomCallPreservedMask(MF, &Mask); 1059 MIB.addRegMask(Mask); 1060 1061 if (TRI->isAnyArgRegReserved(MF)) 1062 TRI->emitReservedArgRegCallError(MF); 1063 1064 // Now we can add the actual call instruction to the correct basic block. 1065 MIRBuilder.insertInstr(MIB); 1066 1067 // If Callee is a reg, since it is used by a target specific 1068 // instruction, it must have a register class matching the 1069 // constraint of that instruction. 1070 if (Info.Callee.isReg()) 1071 constrainOperandRegClass(MF, *TRI, MRI, *MF.getSubtarget().getInstrInfo(), 1072 *MF.getSubtarget().getRegBankInfo(), *MIB, 1073 MIB->getDesc(), Info.Callee, 0); 1074 1075 // Finally we can copy the returned value back into its virtual-register. In 1076 // symmetry with the arguments, the physical register must be an 1077 // implicit-define of the call instruction. 1078 if (!Info.OrigRet.Ty->isVoidTy()) { 1079 CCAssignFn *RetAssignFn = TLI.CCAssignFnForReturn(Info.CallConv); 1080 CallReturnHandler Handler(MIRBuilder, MRI, MIB, RetAssignFn); 1081 bool UsingReturnedArg = 1082 !OutArgs.empty() && OutArgs[0].Flags[0].isReturned(); 1083 ReturnedArgCallReturnHandler ReturnedArgHandler(MIRBuilder, MRI, MIB, 1084 RetAssignFn); 1085 if (!handleAssignments(MIRBuilder, InArgs, 1086 UsingReturnedArg ? ReturnedArgHandler : Handler, 1087 Info.CallConv, Info.IsVarArg, 1088 UsingReturnedArg ? OutArgs[0].Regs[0] : Register())) 1089 return false; 1090 } 1091 1092 if (Info.SwiftErrorVReg) { 1093 MIB.addDef(AArch64::X21, RegState::Implicit); 1094 MIRBuilder.buildCopy(Info.SwiftErrorVReg, Register(AArch64::X21)); 1095 } 1096 1097 uint64_t CalleePopBytes = 1098 doesCalleeRestoreStack(Info.CallConv, 1099 MF.getTarget().Options.GuaranteedTailCallOpt) 1100 ? alignTo(Handler.StackSize, 16) 1101 : 0; 1102 1103 CallSeqStart.addImm(Handler.StackSize).addImm(0); 1104 MIRBuilder.buildInstr(AArch64::ADJCALLSTACKUP) 1105 .addImm(Handler.StackSize) 1106 .addImm(CalleePopBytes); 1107 1108 return true; 1109 } 1110 1111 bool AArch64CallLowering::isTypeIsValidForThisReturn(EVT Ty) const { 1112 return Ty.getSizeInBits() == 64; 1113 } 1114