1 //===--- AArch64CallLowering.cpp - Call lowering --------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the lowering of LLVM calls to machine code calls for
13 //===----------------------------------------------------------------------===//
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"
47 #define DEBUG_TYPE "aarch64-call-lowering"
51 AArch64CallLowering::AArch64CallLowering(const AArch64TargetLowering
&TLI
)
52 : CallLowering(&TLI
) {}
55 struct IncomingArgHandler
: public CallLowering::ValueHandler
{
56 IncomingArgHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
58 : ValueHandler(MIRBuilder
, MRI
, AssignFn
), StackUsed(0) {}
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 Register AddrReg
= MRI
.createGenericVirtualRegister(LLT::pointer(0, 64));
66 MIRBuilder
.buildFrameIndex(AddrReg
, FI
);
67 StackUsed
= std::max(StackUsed
, Size
+ Offset
);
71 void assignValueToReg(Register ValVReg
, Register PhysReg
,
72 CCValAssign
&VA
) override
{
73 markPhysRegUsed(PhysReg
);
74 switch (VA
.getLocInfo()) {
76 MIRBuilder
.buildCopy(ValVReg
, PhysReg
);
78 case CCValAssign::LocInfo::SExt
:
79 case CCValAssign::LocInfo::ZExt
:
80 case CCValAssign::LocInfo::AExt
: {
81 auto Copy
= MIRBuilder
.buildCopy(LLT
{VA
.getLocVT()}, PhysReg
);
82 MIRBuilder
.buildTrunc(ValVReg
, Copy
);
88 void assignValueToAddress(Register ValVReg
, Register Addr
, uint64_t Size
,
89 MachinePointerInfo
&MPO
, CCValAssign
&VA
) override
{
90 // FIXME: Get alignment
91 auto MMO
= MIRBuilder
.getMF().getMachineMemOperand(
92 MPO
, MachineMemOperand::MOLoad
| MachineMemOperand::MOInvariant
, Size
,
94 MIRBuilder
.buildLoad(ValVReg
, Addr
, *MMO
);
97 /// How the physical register gets marked varies between formal
98 /// parameters (it's a basic-block live-in), and a call instruction
99 /// (it's an implicit-def of the BL).
100 virtual void markPhysRegUsed(unsigned PhysReg
) = 0;
102 bool isIncomingArgumentHandler() const override
{ return true; }
107 struct FormalArgHandler
: public IncomingArgHandler
{
108 FormalArgHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
109 CCAssignFn
*AssignFn
)
110 : IncomingArgHandler(MIRBuilder
, MRI
, AssignFn
) {}
112 void markPhysRegUsed(unsigned PhysReg
) override
{
113 MIRBuilder
.getMRI()->addLiveIn(PhysReg
);
114 MIRBuilder
.getMBB().addLiveIn(PhysReg
);
118 struct CallReturnHandler
: public IncomingArgHandler
{
119 CallReturnHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
120 MachineInstrBuilder MIB
, CCAssignFn
*AssignFn
)
121 : IncomingArgHandler(MIRBuilder
, MRI
, AssignFn
), MIB(MIB
) {}
123 void markPhysRegUsed(unsigned PhysReg
) override
{
124 MIB
.addDef(PhysReg
, RegState::Implicit
);
127 MachineInstrBuilder MIB
;
130 struct OutgoingArgHandler
: public CallLowering::ValueHandler
{
131 OutgoingArgHandler(MachineIRBuilder
&MIRBuilder
, MachineRegisterInfo
&MRI
,
132 MachineInstrBuilder MIB
, CCAssignFn
*AssignFn
,
133 CCAssignFn
*AssignFnVarArg
, bool IsTailCall
= false,
135 : ValueHandler(MIRBuilder
, MRI
, AssignFn
), MIB(MIB
),
136 AssignFnVarArg(AssignFnVarArg
), IsTailCall(IsTailCall
), FPDiff(FPDiff
),
139 Register
getStackAddress(uint64_t Size
, int64_t Offset
,
140 MachinePointerInfo
&MPO
) override
{
141 MachineFunction
&MF
= MIRBuilder
.getMF();
142 LLT p0
= LLT::pointer(0, 64);
143 LLT s64
= LLT::scalar(64);
147 int FI
= MF
.getFrameInfo().CreateFixedObject(Size
, Offset
, true);
148 Register FIReg
= MRI
.createGenericVirtualRegister(p0
);
149 MIRBuilder
.buildFrameIndex(FIReg
, FI
);
150 MPO
= MachinePointerInfo::getFixedStack(MF
, FI
);
154 Register SPReg
= MRI
.createGenericVirtualRegister(p0
);
155 MIRBuilder
.buildCopy(SPReg
, Register(AArch64::SP
));
157 Register OffsetReg
= MRI
.createGenericVirtualRegister(s64
);
158 MIRBuilder
.buildConstant(OffsetReg
, Offset
);
160 Register AddrReg
= MRI
.createGenericVirtualRegister(p0
);
161 MIRBuilder
.buildGEP(AddrReg
, SPReg
, OffsetReg
);
163 MPO
= MachinePointerInfo::getStack(MF
, Offset
);
167 void assignValueToReg(Register ValVReg
, Register PhysReg
,
168 CCValAssign
&VA
) override
{
169 MIB
.addUse(PhysReg
, RegState::Implicit
);
170 Register ExtReg
= extendRegister(ValVReg
, VA
);
171 MIRBuilder
.buildCopy(PhysReg
, ExtReg
);
174 void assignValueToAddress(Register ValVReg
, Register Addr
, uint64_t Size
,
175 MachinePointerInfo
&MPO
, CCValAssign
&VA
) override
{
176 if (VA
.getLocInfo() == CCValAssign::LocInfo::AExt
) {
177 Size
= VA
.getLocVT().getSizeInBits() / 8;
178 ValVReg
= MIRBuilder
.buildAnyExt(LLT::scalar(Size
* 8), ValVReg
)
182 auto MMO
= MIRBuilder
.getMF().getMachineMemOperand(
183 MPO
, MachineMemOperand::MOStore
, Size
, 1);
184 MIRBuilder
.buildStore(ValVReg
, Addr
, *MMO
);
187 bool assignArg(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
188 CCValAssign::LocInfo LocInfo
,
189 const CallLowering::ArgInfo
&Info
,
190 ISD::ArgFlagsTy Flags
,
191 CCState
&State
) override
{
194 Res
= AssignFn(ValNo
, ValVT
, LocVT
, LocInfo
, Flags
, State
);
196 Res
= AssignFnVarArg(ValNo
, ValVT
, LocVT
, LocInfo
, Flags
, State
);
198 StackSize
= State
.getNextStackOffset();
202 MachineInstrBuilder MIB
;
203 CCAssignFn
*AssignFnVarArg
;
206 /// For tail calls, the byte offset of the call's argument area from the
207 /// callee's. Unused elsewhere.
213 static bool doesCalleeRestoreStack(CallingConv::ID CallConv
, bool TailCallOpt
) {
214 return CallConv
== CallingConv::Fast
&& TailCallOpt
;
217 void AArch64CallLowering::splitToValueTypes(
218 const ArgInfo
&OrigArg
, SmallVectorImpl
<ArgInfo
> &SplitArgs
,
219 const DataLayout
&DL
, MachineRegisterInfo
&MRI
, CallingConv::ID CallConv
) const {
220 const AArch64TargetLowering
&TLI
= *getTLI
<AArch64TargetLowering
>();
221 LLVMContext
&Ctx
= OrigArg
.Ty
->getContext();
223 if (OrigArg
.Ty
->isVoidTy())
226 SmallVector
<EVT
, 4> SplitVTs
;
227 SmallVector
<uint64_t, 4> Offsets
;
228 ComputeValueVTs(TLI
, DL
, OrigArg
.Ty
, SplitVTs
, &Offsets
, 0);
230 if (SplitVTs
.size() == 1) {
231 // No splitting to do, but we want to replace the original type (e.g. [1 x
232 // double] -> double).
233 SplitArgs
.emplace_back(OrigArg
.Regs
[0], SplitVTs
[0].getTypeForEVT(Ctx
),
234 OrigArg
.Flags
[0], OrigArg
.IsFixed
);
238 // Create one ArgInfo for each virtual register in the original ArgInfo.
239 assert(OrigArg
.Regs
.size() == SplitVTs
.size() && "Regs / types mismatch");
241 bool NeedsRegBlock
= TLI
.functionArgumentNeedsConsecutiveRegisters(
242 OrigArg
.Ty
, CallConv
, false);
243 for (unsigned i
= 0, e
= SplitVTs
.size(); i
< e
; ++i
) {
244 Type
*SplitTy
= SplitVTs
[i
].getTypeForEVT(Ctx
);
245 SplitArgs
.emplace_back(OrigArg
.Regs
[i
], SplitTy
, OrigArg
.Flags
[0],
248 SplitArgs
.back().Flags
[0].setInConsecutiveRegs();
251 SplitArgs
.back().Flags
[0].setInConsecutiveRegsLast();
254 bool AArch64CallLowering::lowerReturn(MachineIRBuilder
&MIRBuilder
,
256 ArrayRef
<Register
> VRegs
,
257 Register SwiftErrorVReg
) const {
258 auto MIB
= MIRBuilder
.buildInstrNoInsert(AArch64::RET_ReallyLR
);
259 assert(((Val
&& !VRegs
.empty()) || (!Val
&& VRegs
.empty())) &&
260 "Return value without a vreg");
263 if (!VRegs
.empty()) {
264 MachineFunction
&MF
= MIRBuilder
.getMF();
265 const Function
&F
= MF
.getFunction();
267 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
268 const AArch64TargetLowering
&TLI
= *getTLI
<AArch64TargetLowering
>();
269 CCAssignFn
*AssignFn
= TLI
.CCAssignFnForReturn(F
.getCallingConv());
270 auto &DL
= F
.getParent()->getDataLayout();
271 LLVMContext
&Ctx
= Val
->getType()->getContext();
273 SmallVector
<EVT
, 4> SplitEVTs
;
274 ComputeValueVTs(TLI
, DL
, Val
->getType(), SplitEVTs
);
275 assert(VRegs
.size() == SplitEVTs
.size() &&
276 "For each split Type there should be exactly one VReg.");
278 SmallVector
<ArgInfo
, 8> SplitArgs
;
279 CallingConv::ID CC
= F
.getCallingConv();
281 for (unsigned i
= 0; i
< SplitEVTs
.size(); ++i
) {
282 if (TLI
.getNumRegistersForCallingConv(Ctx
, CC
, SplitEVTs
[i
]) > 1) {
283 LLVM_DEBUG(dbgs() << "Can't handle extended arg types which need split");
287 Register CurVReg
= VRegs
[i
];
288 ArgInfo CurArgInfo
= ArgInfo
{CurVReg
, SplitEVTs
[i
].getTypeForEVT(Ctx
)};
289 setArgFlags(CurArgInfo
, AttributeList::ReturnIndex
, DL
, F
);
291 // i1 is a special case because SDAG i1 true is naturally zero extended
292 // when widened using ANYEXT. We need to do it explicitly here.
293 if (MRI
.getType(CurVReg
).getSizeInBits() == 1) {
294 CurVReg
= MIRBuilder
.buildZExt(LLT::scalar(8), CurVReg
).getReg(0);
296 // Some types will need extending as specified by the CC.
297 MVT NewVT
= TLI
.getRegisterTypeForCallingConv(Ctx
, CC
, SplitEVTs
[i
]);
298 if (EVT(NewVT
) != SplitEVTs
[i
]) {
299 unsigned ExtendOp
= TargetOpcode::G_ANYEXT
;
300 if (F
.getAttributes().hasAttribute(AttributeList::ReturnIndex
,
302 ExtendOp
= TargetOpcode::G_SEXT
;
303 else if (F
.getAttributes().hasAttribute(AttributeList::ReturnIndex
,
305 ExtendOp
= TargetOpcode::G_ZEXT
;
308 LLT
OldLLT(MVT::getVT(CurArgInfo
.Ty
));
309 CurArgInfo
.Ty
= EVT(NewVT
).getTypeForEVT(Ctx
);
310 // Instead of an extend, we might have a vector type which needs
311 // padding with more elements, e.g. <2 x half> -> <4 x half>.
312 if (NewVT
.isVector()) {
313 if (OldLLT
.isVector()) {
314 if (NewLLT
.getNumElements() > OldLLT
.getNumElements()) {
315 // We don't handle VA types which are not exactly twice the
316 // size, but can easily be done in future.
317 if (NewLLT
.getNumElements() != OldLLT
.getNumElements() * 2) {
318 LLVM_DEBUG(dbgs() << "Outgoing vector ret has too many elts");
321 auto Undef
= MIRBuilder
.buildUndef({OldLLT
});
323 MIRBuilder
.buildMerge({NewLLT
}, {CurVReg
, Undef
.getReg(0)})
326 // Just do a vector extend.
327 CurVReg
= MIRBuilder
.buildInstr(ExtendOp
, {NewLLT
}, {CurVReg
})
330 } else if (NewLLT
.getNumElements() == 2) {
331 // We need to pad a <1 x S> type to <2 x S>. Since we don't have
332 // <1 x S> vector types in GISel we use a build_vector instead
333 // of a vector merge/concat.
334 auto Undef
= MIRBuilder
.buildUndef({OldLLT
});
337 .buildBuildVector({NewLLT
}, {CurVReg
, Undef
.getReg(0)})
340 LLVM_DEBUG(dbgs() << "Could not handle ret ty");
346 MIRBuilder
.buildInstr(ExtendOp
, {NewLLT
}, {CurVReg
}).getReg(0);
350 if (CurVReg
!= CurArgInfo
.Regs
[0]) {
351 CurArgInfo
.Regs
[0] = CurVReg
;
352 // Reset the arg flags after modifying CurVReg.
353 setArgFlags(CurArgInfo
, AttributeList::ReturnIndex
, DL
, F
);
355 splitToValueTypes(CurArgInfo
, SplitArgs
, DL
, MRI
, CC
);
358 OutgoingArgHandler
Handler(MIRBuilder
, MRI
, MIB
, AssignFn
, AssignFn
);
359 Success
= handleAssignments(MIRBuilder
, SplitArgs
, Handler
);
362 if (SwiftErrorVReg
) {
363 MIB
.addUse(AArch64::X21
, RegState::Implicit
);
364 MIRBuilder
.buildCopy(AArch64::X21
, SwiftErrorVReg
);
367 MIRBuilder
.insertInstr(MIB
);
371 bool AArch64CallLowering::lowerFormalArguments(
372 MachineIRBuilder
&MIRBuilder
, const Function
&F
,
373 ArrayRef
<ArrayRef
<Register
>> VRegs
) const {
374 MachineFunction
&MF
= MIRBuilder
.getMF();
375 MachineBasicBlock
&MBB
= MIRBuilder
.getMBB();
376 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
377 auto &DL
= F
.getParent()->getDataLayout();
379 SmallVector
<ArgInfo
, 8> SplitArgs
;
381 for (auto &Arg
: F
.args()) {
382 if (DL
.getTypeStoreSize(Arg
.getType()) == 0)
385 ArgInfo OrigArg
{VRegs
[i
], Arg
.getType()};
386 setArgFlags(OrigArg
, i
+ AttributeList::FirstArgIndex
, DL
, F
);
388 splitToValueTypes(OrigArg
, SplitArgs
, DL
, MRI
, F
.getCallingConv());
393 MIRBuilder
.setInstr(*MBB
.begin());
395 const AArch64TargetLowering
&TLI
= *getTLI
<AArch64TargetLowering
>();
396 CCAssignFn
*AssignFn
=
397 TLI
.CCAssignFnForCall(F
.getCallingConv(), /*IsVarArg=*/false);
399 FormalArgHandler
Handler(MIRBuilder
, MRI
, AssignFn
);
400 if (!handleAssignments(MIRBuilder
, SplitArgs
, Handler
))
403 AArch64FunctionInfo
*FuncInfo
= MF
.getInfo
<AArch64FunctionInfo
>();
404 uint64_t StackOffset
= Handler
.StackUsed
;
406 auto &Subtarget
= MF
.getSubtarget
<AArch64Subtarget
>();
407 if (!Subtarget
.isTargetDarwin()) {
408 // FIXME: we need to reimplement saveVarArgsRegisters from
409 // AArch64ISelLowering.
413 // We currently pass all varargs at 8-byte alignment, or 4 in ILP32.
414 StackOffset
= alignTo(Handler
.StackUsed
, Subtarget
.isTargetILP32() ? 4 : 8);
416 auto &MFI
= MIRBuilder
.getMF().getFrameInfo();
417 FuncInfo
->setVarArgsStackIndex(MFI
.CreateFixedObject(4, StackOffset
, true));
420 if (doesCalleeRestoreStack(F
.getCallingConv(),
421 MF
.getTarget().Options
.GuaranteedTailCallOpt
)) {
422 // We have a non-standard ABI, so why not make full use of the stack that
423 // we're going to pop? It must be aligned to 16 B in any case.
424 StackOffset
= alignTo(StackOffset
, 16);
426 // If we're expected to restore the stack (e.g. fastcc), then we'll be
427 // adding a multiple of 16.
428 FuncInfo
->setArgumentStackToRestore(StackOffset
);
430 // Our own callers will guarantee that the space is free by giving an
431 // aligned value to CALLSEQ_START.
434 // When we tail call, we need to check if the callee's arguments
435 // will fit on the caller's stack. So, whenever we lower formal arguments,
436 // we should keep track of this information, since we might lower a tail call
437 // in this function later.
438 FuncInfo
->setBytesInStackArgArea(StackOffset
);
440 auto &Subtarget
= MF
.getSubtarget
<AArch64Subtarget
>();
441 if (Subtarget
.hasCustomCallingConv())
442 Subtarget
.getRegisterInfo()->UpdateCustomCalleeSavedRegs(MF
);
444 // Move back to the end of the basic block.
445 MIRBuilder
.setMBB(MBB
);
450 /// Return true if the calling convention is one that we can guarantee TCO for.
451 static bool canGuaranteeTCO(CallingConv::ID CC
) {
452 return CC
== CallingConv::Fast
;
455 /// Return true if we might ever do TCO for calls with this calling convention.
456 static bool mayTailCallThisCC(CallingConv::ID CC
) {
459 case CallingConv::PreserveMost
:
460 case CallingConv::Swift
:
463 return canGuaranteeTCO(CC
);
467 bool AArch64CallLowering::doCallerAndCalleePassArgsTheSameWay(
468 CallLoweringInfo
&Info
, MachineFunction
&MF
,
469 SmallVectorImpl
<ArgInfo
> &InArgs
) const {
470 const Function
&CallerF
= MF
.getFunction();
471 CallingConv::ID CalleeCC
= Info
.CallConv
;
472 CallingConv::ID CallerCC
= CallerF
.getCallingConv();
474 // If the calling conventions match, then everything must be the same.
475 if (CalleeCC
== CallerCC
)
478 // Check if the caller and callee will handle arguments in the same way.
479 const AArch64TargetLowering
&TLI
= *getTLI
<AArch64TargetLowering
>();
480 CCAssignFn
*CalleeAssignFn
= TLI
.CCAssignFnForCall(CalleeCC
, Info
.IsVarArg
);
481 CCAssignFn
*CallerAssignFn
=
482 TLI
.CCAssignFnForCall(CallerCC
, CallerF
.isVarArg());
484 if (!resultsCompatible(Info
, MF
, InArgs
, *CalleeAssignFn
, *CallerAssignFn
))
487 // Make sure that the caller and callee preserve all of the same registers.
488 auto TRI
= MF
.getSubtarget
<AArch64Subtarget
>().getRegisterInfo();
489 const uint32_t *CallerPreserved
= TRI
->getCallPreservedMask(MF
, CallerCC
);
490 const uint32_t *CalleePreserved
= TRI
->getCallPreservedMask(MF
, CalleeCC
);
491 if (MF
.getSubtarget
<AArch64Subtarget
>().hasCustomCallingConv()) {
492 TRI
->UpdateCustomCallPreservedMask(MF
, &CallerPreserved
);
493 TRI
->UpdateCustomCallPreservedMask(MF
, &CalleePreserved
);
496 return TRI
->regmaskSubsetEqual(CallerPreserved
, CalleePreserved
);
499 bool AArch64CallLowering::areCalleeOutgoingArgsTailCallable(
500 CallLoweringInfo
&Info
, MachineFunction
&MF
,
501 SmallVectorImpl
<ArgInfo
> &OutArgs
) const {
502 // If there are no outgoing arguments, then we are done.
506 const Function
&CallerF
= MF
.getFunction();
507 CallingConv::ID CalleeCC
= Info
.CallConv
;
508 CallingConv::ID CallerCC
= CallerF
.getCallingConv();
509 const AArch64TargetLowering
&TLI
= *getTLI
<AArch64TargetLowering
>();
511 // We have outgoing arguments. Make sure that we can tail call with them.
512 SmallVector
<CCValAssign
, 16> OutLocs
;
513 CCState
OutInfo(CalleeCC
, false, MF
, OutLocs
, CallerF
.getContext());
515 if (!analyzeArgInfo(OutInfo
, OutArgs
,
516 *TLI
.CCAssignFnForCall(CalleeCC
, Info
.IsVarArg
))) {
517 LLVM_DEBUG(dbgs() << "... Could not analyze call operands.\n");
521 // Make sure that they can fit on the caller's stack.
522 const AArch64FunctionInfo
*FuncInfo
= MF
.getInfo
<AArch64FunctionInfo
>();
523 if (OutInfo
.getNextStackOffset() > FuncInfo
->getBytesInStackArgArea()) {
524 LLVM_DEBUG(dbgs() << "... Cannot fit call operands on caller's stack.\n");
528 // Verify that the parameters in callee-saved registers match.
529 // TODO: Port this over to CallLowering as general code once swiftself is
531 auto TRI
= MF
.getSubtarget
<AArch64Subtarget
>().getRegisterInfo();
532 const uint32_t *CallerPreservedMask
= TRI
->getCallPreservedMask(MF
, CallerCC
);
533 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
535 for (unsigned i
= 0; i
< OutLocs
.size(); ++i
) {
536 auto &ArgLoc
= OutLocs
[i
];
537 // If it's not a register, it's fine.
538 if (!ArgLoc
.isRegLoc()) {
540 // Be conservative and disallow variadic memory operands to match SDAG's
542 // FIXME: If the caller's calling convention is C, then we can
543 // potentially use its argument area. However, for cases like fastcc,
544 // we can't do anything.
547 << "... Cannot tail call vararg function with stack arguments\n");
553 Register Reg
= ArgLoc
.getLocReg();
555 // Only look at callee-saved registers.
556 if (MachineOperand::clobbersPhysReg(CallerPreservedMask
, Reg
))
561 << "... Call has an argument passed in a callee-saved register.\n");
563 // Check if it was copied from.
564 ArgInfo
&OutInfo
= OutArgs
[i
];
566 if (OutInfo
.Regs
.size() > 1) {
568 dbgs() << "... Cannot handle arguments in multiple registers.\n");
572 // Check if we copy the register, walking through copies from virtual
573 // registers. Note that getDefIgnoringCopies does not ignore copies from
574 // physical registers.
575 MachineInstr
*RegDef
= getDefIgnoringCopies(OutInfo
.Regs
[0], MRI
);
576 if (!RegDef
|| RegDef
->getOpcode() != TargetOpcode::COPY
) {
579 << "... Parameter was not copied into a VReg, cannot tail call.\n");
583 // Got a copy. Verify that it's the same as the register we want.
584 Register CopyRHS
= RegDef
->getOperand(1).getReg();
585 if (CopyRHS
!= Reg
) {
586 LLVM_DEBUG(dbgs() << "... Callee-saved register was not copied into "
587 "VReg, cannot tail call.\n");
595 bool AArch64CallLowering::isEligibleForTailCallOptimization(
596 MachineIRBuilder
&MIRBuilder
, CallLoweringInfo
&Info
,
597 SmallVectorImpl
<ArgInfo
> &InArgs
,
598 SmallVectorImpl
<ArgInfo
> &OutArgs
) const {
600 // Must pass all target-independent checks in order to tail call optimize.
601 if (!Info
.IsTailCall
)
604 CallingConv::ID CalleeCC
= Info
.CallConv
;
605 MachineFunction
&MF
= MIRBuilder
.getMF();
606 const Function
&CallerF
= MF
.getFunction();
608 LLVM_DEBUG(dbgs() << "Attempting to lower call as tail call\n");
610 if (Info
.SwiftErrorVReg
) {
611 // TODO: We should handle this.
612 // Note that this is also handled by the check for no outgoing arguments.
613 // Proactively disabling this though, because the swifterror handling in
614 // lowerCall inserts a COPY *after* the location of the call.
615 LLVM_DEBUG(dbgs() << "... Cannot handle tail calls with swifterror yet.\n");
619 if (!mayTailCallThisCC(CalleeCC
)) {
620 LLVM_DEBUG(dbgs() << "... Calling convention cannot be tail called.\n");
624 // Byval parameters hand the function a pointer directly into the stack area
625 // we want to reuse during a tail call. Working around this *is* possible (see
628 // FIXME: In AArch64ISelLowering, this isn't worked around. Can/should we try
631 // On Windows, "inreg" attributes signify non-aggregate indirect returns.
632 // In this case, it is necessary to save/restore X0 in the callee. Tail
633 // call opt interferes with this. So we disable tail call opt when the
634 // caller has an argument with "inreg" attribute.
636 // FIXME: Check whether the callee also has an "inreg" argument.
638 // When the caller has a swifterror argument, we don't want to tail call
639 // because would have to move into the swifterror register before the
641 if (any_of(CallerF
.args(), [](const Argument
&A
) {
642 return A
.hasByValAttr() || A
.hasInRegAttr() || A
.hasSwiftErrorAttr();
644 LLVM_DEBUG(dbgs() << "... Cannot tail call from callers with byval, "
645 "inreg, or swifterror arguments\n");
649 // Externally-defined functions with weak linkage should not be
650 // tail-called on AArch64 when the OS does not support dynamic
651 // pre-emption of symbols, as the AAELF spec requires normal calls
652 // to undefined weak functions to be replaced with a NOP or jump to the
653 // next instruction. The behaviour of branch instructions in this
654 // situation (as used for tail calls) is implementation-defined, so we
655 // cannot rely on the linker replacing the tail call with a return.
656 if (Info
.Callee
.isGlobal()) {
657 const GlobalValue
*GV
= Info
.Callee
.getGlobal();
658 const Triple
&TT
= MF
.getTarget().getTargetTriple();
659 if (GV
->hasExternalWeakLinkage() &&
660 (!TT
.isOSWindows() || TT
.isOSBinFormatELF() ||
661 TT
.isOSBinFormatMachO())) {
662 LLVM_DEBUG(dbgs() << "... Cannot tail call externally-defined function "
663 "with weak linkage for this OS.\n");
668 // If we have -tailcallopt, then we're done.
669 if (MF
.getTarget().Options
.GuaranteedTailCallOpt
)
670 return canGuaranteeTCO(CalleeCC
) && CalleeCC
== CallerF
.getCallingConv();
672 // We don't have -tailcallopt, so we're allowed to change the ABI (sibcall).
673 // Try to find cases where we can do that.
675 // I want anyone implementing a new calling convention to think long and hard
676 // about this assert.
677 assert((!Info
.IsVarArg
|| CalleeCC
== CallingConv::C
) &&
678 "Unexpected variadic calling convention");
680 // Before we can musttail varargs, we need to forward parameters like in
681 // r345641. Make sure that we don't enable musttail with varargs without
683 if (Info
.IsVarArg
&& Info
.IsMustTailCall
) {
686 << "... Cannot handle vararg musttail functions yet.\n");
690 // Verify that the incoming and outgoing arguments from the callee are
691 // safe to tail call.
692 if (!doCallerAndCalleePassArgsTheSameWay(Info
, MF
, InArgs
)) {
695 << "... Caller and callee have incompatible calling conventions.\n");
699 if (!areCalleeOutgoingArgsTailCallable(Info
, MF
, OutArgs
))
703 dbgs() << "... Call is eligible for tail call optimization.\n");
707 static unsigned getCallOpcode(const Function
&CallerF
, bool IsIndirect
,
710 return IsIndirect
? AArch64::BLR
: AArch64::BL
;
713 return AArch64::TCRETURNdi
;
715 // When BTI is enabled, we need to use TCRETURNriBTI to make sure that we use
717 if (CallerF
.hasFnAttribute("branch-target-enforcement"))
718 return AArch64::TCRETURNriBTI
;
720 return AArch64::TCRETURNri
;
723 bool AArch64CallLowering::lowerTailCall(
724 MachineIRBuilder
&MIRBuilder
, CallLoweringInfo
&Info
,
725 SmallVectorImpl
<ArgInfo
> &OutArgs
) const {
726 MachineFunction
&MF
= MIRBuilder
.getMF();
727 const Function
&F
= MF
.getFunction();
728 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
729 const AArch64TargetLowering
&TLI
= *getTLI
<AArch64TargetLowering
>();
731 // True when we're tail calling, but without -tailcallopt.
732 bool IsSibCall
= !MF
.getTarget().Options
.GuaranteedTailCallOpt
;
734 // TODO: Right now, regbankselect doesn't know how to handle the rtcGPR64
735 // register class. Until we can do that, we should fall back here.
736 if (F
.hasFnAttribute("branch-target-enforcement")) {
738 dbgs() << "Cannot lower indirect tail calls with BTI enabled yet.\n");
742 // Find out which ABI gets to decide where things go.
743 CallingConv::ID CalleeCC
= Info
.CallConv
;
744 CCAssignFn
*AssignFnFixed
=
745 TLI
.CCAssignFnForCall(CalleeCC
, /*IsVarArg=*/false);
746 CCAssignFn
*AssignFnVarArg
=
747 TLI
.CCAssignFnForCall(CalleeCC
, /*IsVarArg=*/true);
749 MachineInstrBuilder CallSeqStart
;
751 CallSeqStart
= MIRBuilder
.buildInstr(AArch64::ADJCALLSTACKDOWN
);
753 unsigned Opc
= getCallOpcode(F
, Info
.Callee
.isReg(), true);
754 auto MIB
= MIRBuilder
.buildInstrNoInsert(Opc
);
755 MIB
.add(Info
.Callee
);
757 // Byte offset for the tail call. When we are sibcalling, this will always
761 // Tell the call which registers are clobbered.
762 auto TRI
= MF
.getSubtarget
<AArch64Subtarget
>().getRegisterInfo();
763 const uint32_t *Mask
= TRI
->getCallPreservedMask(MF
, F
.getCallingConv());
764 if (MF
.getSubtarget
<AArch64Subtarget
>().hasCustomCallingConv())
765 TRI
->UpdateCustomCallPreservedMask(MF
, &Mask
);
766 MIB
.addRegMask(Mask
);
768 if (TRI
->isAnyArgRegReserved(MF
))
769 TRI
->emitReservedArgRegCallError(MF
);
771 // FPDiff is the byte offset of the call's argument area from the callee's.
772 // Stores to callee stack arguments will be placed in FixedStackSlots offset
773 // by this amount for a tail call. In a sibling call it must be 0 because the
774 // caller will deallocate the entire stack and the callee still expects its
775 // arguments to begin at SP+0.
778 // This will be 0 for sibcalls, potentially nonzero for tail calls produced
779 // by -tailcallopt. For sibcalls, the memory operands for the call are
780 // already available in the caller's incoming argument space.
781 unsigned NumBytes
= 0;
783 // We aren't sibcalling, so we need to compute FPDiff. We need to do this
784 // before handling assignments, because FPDiff must be known for memory
786 AArch64FunctionInfo
*FuncInfo
= MF
.getInfo
<AArch64FunctionInfo
>();
787 unsigned NumReusableBytes
= FuncInfo
->getBytesInStackArgArea();
788 SmallVector
<CCValAssign
, 16> OutLocs
;
789 CCState
OutInfo(CalleeCC
, false, MF
, OutLocs
, F
.getContext());
790 analyzeArgInfo(OutInfo
, OutArgs
,
791 *TLI
.CCAssignFnForCall(CalleeCC
, Info
.IsVarArg
));
793 // The callee will pop the argument stack as a tail call. Thus, we must
794 // keep it 16-byte aligned.
795 NumBytes
= alignTo(OutInfo
.getNextStackOffset(), 16);
797 // FPDiff will be negative if this tail call requires more space than we
798 // would automatically have in our incoming argument space. Positive if we
799 // actually shrink the stack.
800 FPDiff
= NumReusableBytes
- NumBytes
;
802 // The stack pointer must be 16-byte aligned at all times it's used for a
803 // memory operation, which in practice means at *all* times and in
804 // particular across call boundaries. Therefore our own arguments started at
805 // a 16-byte aligned SP and the delta applied for the tail call should
806 // satisfy the same constraint.
807 assert(FPDiff
% 16 == 0 && "unaligned stack on tail call");
810 // Do the actual argument marshalling.
811 SmallVector
<unsigned, 8> PhysRegs
;
812 OutgoingArgHandler
Handler(MIRBuilder
, MRI
, MIB
, AssignFnFixed
,
813 AssignFnVarArg
, true, FPDiff
);
814 if (!handleAssignments(MIRBuilder
, OutArgs
, Handler
))
817 // If we have -tailcallopt, we need to adjust the stack. We'll do the call
818 // sequence start and end here.
820 MIB
->getOperand(1).setImm(FPDiff
);
821 CallSeqStart
.addImm(NumBytes
).addImm(0);
822 // End the call sequence *before* emitting the call. Normally, we would
823 // tidy the frame up after the call. However, here, we've laid out the
824 // parameters so that when SP is reset, they will be in the correct
826 MIRBuilder
.buildInstr(AArch64::ADJCALLSTACKUP
).addImm(NumBytes
).addImm(0);
829 // Now we can add the actual call instruction to the correct basic block.
830 MIRBuilder
.insertInstr(MIB
);
832 // If Callee is a reg, since it is used by a target specific instruction,
833 // it must have a register class matching the constraint of that instruction.
834 if (Info
.Callee
.isReg())
835 MIB
->getOperand(0).setReg(constrainOperandRegClass(
836 MF
, *TRI
, MRI
, *MF
.getSubtarget().getInstrInfo(),
837 *MF
.getSubtarget().getRegBankInfo(), *MIB
, MIB
->getDesc(), Info
.Callee
,
840 MF
.getFrameInfo().setHasTailCall();
841 Info
.LoweredTailCall
= true;
845 bool AArch64CallLowering::lowerCall(MachineIRBuilder
&MIRBuilder
,
846 CallLoweringInfo
&Info
) const {
847 MachineFunction
&MF
= MIRBuilder
.getMF();
848 const Function
&F
= MF
.getFunction();
849 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
850 auto &DL
= F
.getParent()->getDataLayout();
851 const AArch64TargetLowering
&TLI
= *getTLI
<AArch64TargetLowering
>();
853 SmallVector
<ArgInfo
, 8> OutArgs
;
854 for (auto &OrigArg
: Info
.OrigArgs
) {
855 splitToValueTypes(OrigArg
, OutArgs
, DL
, MRI
, Info
.CallConv
);
856 // AAPCS requires that we zero-extend i1 to 8 bits by the caller.
857 if (OrigArg
.Ty
->isIntegerTy(1))
858 OutArgs
.back().Flags
[0].setZExt();
861 SmallVector
<ArgInfo
, 8> InArgs
;
862 if (!Info
.OrigRet
.Ty
->isVoidTy())
863 splitToValueTypes(Info
.OrigRet
, InArgs
, DL
, MRI
, F
.getCallingConv());
865 // If we can lower as a tail call, do that instead.
866 bool CanTailCallOpt
=
867 isEligibleForTailCallOptimization(MIRBuilder
, Info
, InArgs
, OutArgs
);
869 // We must emit a tail call if we have musttail.
870 if (Info
.IsMustTailCall
&& !CanTailCallOpt
) {
871 // There are types of incoming/outgoing arguments we can't handle yet, so
872 // it doesn't make sense to actually die here like in ISelLowering. Instead,
873 // fall back to SelectionDAG and let it try to handle this.
874 LLVM_DEBUG(dbgs() << "Failed to lower musttail call as tail call\n");
879 return lowerTailCall(MIRBuilder
, Info
, OutArgs
);
881 // Find out which ABI gets to decide where things go.
882 CCAssignFn
*AssignFnFixed
=
883 TLI
.CCAssignFnForCall(Info
.CallConv
, /*IsVarArg=*/false);
884 CCAssignFn
*AssignFnVarArg
=
885 TLI
.CCAssignFnForCall(Info
.CallConv
, /*IsVarArg=*/true);
887 MachineInstrBuilder CallSeqStart
;
888 CallSeqStart
= MIRBuilder
.buildInstr(AArch64::ADJCALLSTACKDOWN
);
890 // Create a temporarily-floating call instruction so we can add the implicit
891 // uses of arg registers.
892 unsigned Opc
= getCallOpcode(F
, Info
.Callee
.isReg(), false);
894 auto MIB
= MIRBuilder
.buildInstrNoInsert(Opc
);
895 MIB
.add(Info
.Callee
);
897 // Tell the call which registers are clobbered.
898 auto TRI
= MF
.getSubtarget
<AArch64Subtarget
>().getRegisterInfo();
899 const uint32_t *Mask
= TRI
->getCallPreservedMask(MF
, F
.getCallingConv());
900 if (MF
.getSubtarget
<AArch64Subtarget
>().hasCustomCallingConv())
901 TRI
->UpdateCustomCallPreservedMask(MF
, &Mask
);
902 MIB
.addRegMask(Mask
);
904 if (TRI
->isAnyArgRegReserved(MF
))
905 TRI
->emitReservedArgRegCallError(MF
);
907 // Do the actual argument marshalling.
908 SmallVector
<unsigned, 8> PhysRegs
;
909 OutgoingArgHandler
Handler(MIRBuilder
, MRI
, MIB
, AssignFnFixed
,
910 AssignFnVarArg
, false);
911 if (!handleAssignments(MIRBuilder
, OutArgs
, Handler
))
914 // Now we can add the actual call instruction to the correct basic block.
915 MIRBuilder
.insertInstr(MIB
);
917 // If Callee is a reg, since it is used by a target specific
918 // instruction, it must have a register class matching the
919 // constraint of that instruction.
920 if (Info
.Callee
.isReg())
921 MIB
->getOperand(0).setReg(constrainOperandRegClass(
922 MF
, *TRI
, MRI
, *MF
.getSubtarget().getInstrInfo(),
923 *MF
.getSubtarget().getRegBankInfo(), *MIB
, MIB
->getDesc(), Info
.Callee
,
926 // Finally we can copy the returned value back into its virtual-register. In
927 // symmetry with the arugments, the physical register must be an
928 // implicit-define of the call instruction.
929 if (!Info
.OrigRet
.Ty
->isVoidTy()) {
930 CCAssignFn
*RetAssignFn
= TLI
.CCAssignFnForReturn(F
.getCallingConv());
931 CallReturnHandler
Handler(MIRBuilder
, MRI
, MIB
, RetAssignFn
);
932 if (!handleAssignments(MIRBuilder
, InArgs
, Handler
))
936 if (Info
.SwiftErrorVReg
) {
937 MIB
.addDef(AArch64::X21
, RegState::Implicit
);
938 MIRBuilder
.buildCopy(Info
.SwiftErrorVReg
, Register(AArch64::X21
));
941 uint64_t CalleePopBytes
=
942 doesCalleeRestoreStack(Info
.CallConv
,
943 MF
.getTarget().Options
.GuaranteedTailCallOpt
)
944 ? alignTo(Handler
.StackSize
, 16)
947 CallSeqStart
.addImm(Handler
.StackSize
).addImm(0);
948 MIRBuilder
.buildInstr(AArch64::ADJCALLSTACKUP
)
949 .addImm(Handler
.StackSize
)
950 .addImm(CalleePopBytes
);