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