blob: a4f2bac8d604d2b06a94334f3a7e63417f578e61 [file] [log] [blame]
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +00001//===--- AArch64CallLowering.cpp - Call lowering --------------------------===//
Quentin Colombetba2a0162016-02-16 19:26:02 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Quentin Colombetba2a0162016-02-16 19:26:02 +00006//
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"
Tim Northovere9600d82017-02-08 17:57:27 +000017#include "AArch64MachineFunctionInfo.h"
18#include "AArch64Subtarget.h"
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +000019#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/SmallVector.h"
Tim Northoverb18ea162016-09-20 15:20:36 +000021#include "llvm/CodeGen/Analysis.h"
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +000022#include "llvm/CodeGen/CallingConvLower.h"
Quentin Colombetf38015e2016-12-22 21:56:31 +000023#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Quentin Colombetf38015e2016-12-22 21:56:31 +000024#include "llvm/CodeGen/GlobalISel/Utils.h"
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +000025#include "llvm/CodeGen/LowLevelType.h"
26#include "llvm/CodeGen/MachineBasicBlock.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
Quentin Colombetba2a0162016-02-16 19:26:02 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +000030#include "llvm/CodeGen/MachineMemOperand.h"
31#include "llvm/CodeGen/MachineOperand.h"
Tim Northoverb18ea162016-09-20 15:20:36 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000033#include "llvm/CodeGen/TargetRegisterInfo.h"
34#include "llvm/CodeGen/TargetSubtargetInfo.h"
Craig Topper2fa14362018-03-29 17:21:10 +000035#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +000036#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"
David Blaikie13e77db2018-03-23 23:58:25 +000041#include "llvm/Support/MachineValueType.h"
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +000042#include <algorithm>
43#include <cassert>
44#include <cstdint>
45#include <iterator>
46
Amara Emerson2b523f82019-04-09 21:22:33 +000047#define DEBUG_TYPE "aarch64-call-lowering"
48
Quentin Colombetba2a0162016-02-16 19:26:02 +000049using namespace llvm;
50
51AArch64CallLowering::AArch64CallLowering(const AArch64TargetLowering &TLI)
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +000052 : CallLowering(&TLI) {}
Quentin Colombetba2a0162016-02-16 19:26:02 +000053
Benjamin Kramer49a49fe2017-08-20 13:03:48 +000054namespace {
Diana Picusf11f0422016-12-05 10:40:33 +000055struct IncomingArgHandler : public CallLowering::ValueHandler {
Tim Northoverd9433542017-01-17 22:30:10 +000056 IncomingArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
57 CCAssignFn *AssignFn)
Tim Northovere9600d82017-02-08 17:57:27 +000058 : ValueHandler(MIRBuilder, MRI, AssignFn), StackUsed(0) {}
Tim Northovera5e38fa2016-09-22 13:49:25 +000059
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +000060 Register getStackAddress(uint64_t Size, int64_t Offset,
Tim Northovera5e38fa2016-09-22 13:49:25 +000061 MachinePointerInfo &MPO) override {
62 auto &MFI = MIRBuilder.getMF().getFrameInfo();
63 int FI = MFI.CreateFixedObject(Size, Offset, true);
64 MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +000065 Register AddrReg = MRI.createGenericVirtualRegister(LLT::pointer(0, 64));
Tim Northovera5e38fa2016-09-22 13:49:25 +000066 MIRBuilder.buildFrameIndex(AddrReg, FI);
Tim Northovere9600d82017-02-08 17:57:27 +000067 StackUsed = std::max(StackUsed, Size + Offset);
Tim Northovera5e38fa2016-09-22 13:49:25 +000068 return AddrReg;
69 }
70
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +000071 void assignValueToReg(Register ValVReg, Register PhysReg,
Tim Northovera5e38fa2016-09-22 13:49:25 +000072 CCValAssign &VA) override {
73 markPhysRegUsed(PhysReg);
Aditya Nandakumarc3bfc812017-10-09 20:07:43 +000074 switch (VA.getLocInfo()) {
75 default:
76 MIRBuilder.buildCopy(ValVReg, PhysReg);
77 break;
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);
83 break;
84 }
85 }
Tim Northovera5e38fa2016-09-22 13:49:25 +000086 }
87
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +000088 void assignValueToAddress(Register ValVReg, Register Addr, uint64_t Size,
Tim Northovera5e38fa2016-09-22 13:49:25 +000089 MachinePointerInfo &MPO, CCValAssign &VA) override {
Matt Arsenault2a645982019-01-31 01:38:47 +000090 // FIXME: Get alignment
Tim Northovera5e38fa2016-09-22 13:49:25 +000091 auto MMO = MIRBuilder.getMF().getMachineMemOperand(
92 MPO, MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, Size,
Matt Arsenault2a645982019-01-31 01:38:47 +000093 1);
Tim Northovera5e38fa2016-09-22 13:49:25 +000094 MIRBuilder.buildLoad(ValVReg, Addr, *MMO);
95 }
96
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;
Tim Northovere9600d82017-02-08 17:57:27 +0000101
Amara Emersonbc1172d2019-08-05 23:05:28 +0000102 bool isIncomingArgumentHandler() const override { return true; }
Amara Emerson2b523f82019-04-09 21:22:33 +0000103
Tim Northovere9600d82017-02-08 17:57:27 +0000104 uint64_t StackUsed;
Tim Northovera5e38fa2016-09-22 13:49:25 +0000105};
106
107struct FormalArgHandler : public IncomingArgHandler {
Tim Northoverd9433542017-01-17 22:30:10 +0000108 FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
109 CCAssignFn *AssignFn)
110 : IncomingArgHandler(MIRBuilder, MRI, AssignFn) {}
Tim Northovera5e38fa2016-09-22 13:49:25 +0000111
112 void markPhysRegUsed(unsigned PhysReg) override {
Tim Northover522fb7e2019-08-02 14:09:49 +0000113 MIRBuilder.getMRI()->addLiveIn(PhysReg);
Tim Northovera5e38fa2016-09-22 13:49:25 +0000114 MIRBuilder.getMBB().addLiveIn(PhysReg);
115 }
116};
117
118struct CallReturnHandler : public IncomingArgHandler {
119 CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
Tim Northoverd9433542017-01-17 22:30:10 +0000120 MachineInstrBuilder MIB, CCAssignFn *AssignFn)
121 : IncomingArgHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {}
Tim Northovera5e38fa2016-09-22 13:49:25 +0000122
123 void markPhysRegUsed(unsigned PhysReg) override {
124 MIB.addDef(PhysReg, RegState::Implicit);
125 }
126
127 MachineInstrBuilder MIB;
128};
129
Diana Picusf11f0422016-12-05 10:40:33 +0000130struct OutgoingArgHandler : public CallLowering::ValueHandler {
Tim Northovera5e38fa2016-09-22 13:49:25 +0000131 OutgoingArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
Tim Northoverd9433542017-01-17 22:30:10 +0000132 MachineInstrBuilder MIB, CCAssignFn *AssignFn,
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000133 CCAssignFn *AssignFnVarArg, bool IsTailCall = false,
134 int FPDiff = 0)
Tim Northoverd9433542017-01-17 22:30:10 +0000135 : ValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB),
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000136 AssignFnVarArg(AssignFnVarArg), IsTailCall(IsTailCall), FPDiff(FPDiff),
137 StackSize(0) {}
Tim Northovera5e38fa2016-09-22 13:49:25 +0000138
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +0000139 Register getStackAddress(uint64_t Size, int64_t Offset,
Tim Northovera5e38fa2016-09-22 13:49:25 +0000140 MachinePointerInfo &MPO) override {
Jessica Paquettea42070a2019-09-12 22:10:36 +0000141 MachineFunction &MF = MIRBuilder.getMF();
Tim Northovera5e38fa2016-09-22 13:49:25 +0000142 LLT p0 = LLT::pointer(0, 64);
143 LLT s64 = LLT::scalar(64);
Jessica Paquettea42070a2019-09-12 22:10:36 +0000144
145 if (IsTailCall) {
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000146 Offset += FPDiff;
Jessica Paquettea42070a2019-09-12 22:10:36 +0000147 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);
151 return FIReg;
152 }
153
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +0000154 Register SPReg = MRI.createGenericVirtualRegister(p0);
155 MIRBuilder.buildCopy(SPReg, Register(AArch64::SP));
Tim Northovera5e38fa2016-09-22 13:49:25 +0000156
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +0000157 Register OffsetReg = MRI.createGenericVirtualRegister(s64);
Tim Northovera5e38fa2016-09-22 13:49:25 +0000158 MIRBuilder.buildConstant(OffsetReg, Offset);
159
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +0000160 Register AddrReg = MRI.createGenericVirtualRegister(p0);
Tim Northovera5e38fa2016-09-22 13:49:25 +0000161 MIRBuilder.buildGEP(AddrReg, SPReg, OffsetReg);
162
Jessica Paquettea42070a2019-09-12 22:10:36 +0000163 MPO = MachinePointerInfo::getStack(MF, Offset);
Tim Northovera5e38fa2016-09-22 13:49:25 +0000164 return AddrReg;
165 }
166
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +0000167 void assignValueToReg(Register ValVReg, Register PhysReg,
Tim Northovera5e38fa2016-09-22 13:49:25 +0000168 CCValAssign &VA) override {
169 MIB.addUse(PhysReg, RegState::Implicit);
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +0000170 Register ExtReg = extendRegister(ValVReg, VA);
Tim Northovera5e38fa2016-09-22 13:49:25 +0000171 MIRBuilder.buildCopy(PhysReg, ExtReg);
172 }
173
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +0000174 void assignValueToAddress(Register ValVReg, Register Addr, uint64_t Size,
Tim Northovera5e38fa2016-09-22 13:49:25 +0000175 MachinePointerInfo &MPO, CCValAssign &VA) override {
Amara Emersond912ffa2018-07-03 15:59:26 +0000176 if (VA.getLocInfo() == CCValAssign::LocInfo::AExt) {
Amara Emerson846f2432018-07-02 16:39:09 +0000177 Size = VA.getLocVT().getSizeInBits() / 8;
Amara Emersond912ffa2018-07-03 15:59:26 +0000178 ValVReg = MIRBuilder.buildAnyExt(LLT::scalar(Size * 8), ValVReg)
179 ->getOperand(0)
180 .getReg();
181 }
Tim Northovera5e38fa2016-09-22 13:49:25 +0000182 auto MMO = MIRBuilder.getMF().getMachineMemOperand(
Matt Arsenault2a645982019-01-31 01:38:47 +0000183 MPO, MachineMemOperand::MOStore, Size, 1);
Tim Northovera5e38fa2016-09-22 13:49:25 +0000184 MIRBuilder.buildStore(ValVReg, Addr, *MMO);
185 }
186
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +0000187 bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT,
188 CCValAssign::LocInfo LocInfo,
189 const CallLowering::ArgInfo &Info,
Amara Emersonfbaf4252019-09-03 21:42:28 +0000190 ISD::ArgFlagsTy Flags,
Eugene Zelenkoc5eb8e22017-02-01 22:56:06 +0000191 CCState &State) override {
Tim Northovere80d6d12017-03-02 15:34:18 +0000192 bool Res;
Tim Northoverd9433542017-01-17 22:30:10 +0000193 if (Info.IsFixed)
Amara Emersonfbaf4252019-09-03 21:42:28 +0000194 Res = AssignFn(ValNo, ValVT, LocVT, LocInfo, Flags, State);
Tim Northovere80d6d12017-03-02 15:34:18 +0000195 else
Amara Emersonfbaf4252019-09-03 21:42:28 +0000196 Res = AssignFnVarArg(ValNo, ValVT, LocVT, LocInfo, Flags, State);
Tim Northovere80d6d12017-03-02 15:34:18 +0000197
198 StackSize = State.getNextStackOffset();
199 return Res;
Tim Northoverd9433542017-01-17 22:30:10 +0000200 }
201
Tim Northovera5e38fa2016-09-22 13:49:25 +0000202 MachineInstrBuilder MIB;
Tim Northoverd9433542017-01-17 22:30:10 +0000203 CCAssignFn *AssignFnVarArg;
Jessica Paquettea42070a2019-09-12 22:10:36 +0000204 bool IsTailCall;
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000205
206 /// For tail calls, the byte offset of the call's argument area from the
207 /// callee's. Unused elsewhere.
208 int FPDiff;
Tim Northover509091f2017-01-17 22:43:34 +0000209 uint64_t StackSize;
Tim Northovera5e38fa2016-09-22 13:49:25 +0000210};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000211} // namespace
Tim Northovera5e38fa2016-09-22 13:49:25 +0000212
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000213static bool doesCalleeRestoreStack(CallingConv::ID CallConv, bool TailCallOpt) {
214 return CallConv == CallingConv::Fast && TailCallOpt;
215}
216
Benjamin Kramer061f4a52017-01-13 14:39:03 +0000217void AArch64CallLowering::splitToValueTypes(
218 const ArgInfo &OrigArg, SmallVectorImpl<ArgInfo> &SplitArgs,
Diana Picus253b53b2019-06-27 09:24:30 +0000219 const DataLayout &DL, MachineRegisterInfo &MRI, CallingConv::ID CallConv) const {
Tim Northoverb18ea162016-09-20 15:20:36 +0000220 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
Tim Northover9a467182016-09-21 12:57:45 +0000221 LLVMContext &Ctx = OrigArg.Ty->getContext();
Tim Northoverb18ea162016-09-20 15:20:36 +0000222
Amara Emerson0d6a26d2018-05-16 10:32:02 +0000223 if (OrigArg.Ty->isVoidTy())
224 return;
225
Tim Northoverb18ea162016-09-20 15:20:36 +0000226 SmallVector<EVT, 4> SplitVTs;
227 SmallVector<uint64_t, 4> Offsets;
Tim Northover9a467182016-09-21 12:57:45 +0000228 ComputeValueVTs(TLI, DL, OrigArg.Ty, SplitVTs, &Offsets, 0);
Tim Northoverb18ea162016-09-20 15:20:36 +0000229
230 if (SplitVTs.size() == 1) {
Tim Northoverd1fd3832016-12-05 21:25:33 +0000231 // No splitting to do, but we want to replace the original type (e.g. [1 x
232 // double] -> double).
Diana Picus69ce1c132019-06-27 08:50:53 +0000233 SplitArgs.emplace_back(OrigArg.Regs[0], SplitVTs[0].getTypeForEVT(Ctx),
Amara Emersonfbaf4252019-09-03 21:42:28 +0000234 OrigArg.Flags[0], OrigArg.IsFixed);
Tim Northoverb18ea162016-09-20 15:20:36 +0000235 return;
236 }
237
Diana Picus253b53b2019-06-27 09:24:30 +0000238 // Create one ArgInfo for each virtual register in the original ArgInfo.
239 assert(OrigArg.Regs.size() == SplitVTs.size() && "Regs / types mismatch");
Diana Picusc3dbe232019-06-27 08:54:17 +0000240
Tim Northoveref1fc5a2017-08-21 21:56:11 +0000241 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
242 OrigArg.Ty, CallConv, false);
Diana Picus253b53b2019-06-27 09:24:30 +0000243 for (unsigned i = 0, e = SplitVTs.size(); i < e; ++i) {
244 Type *SplitTy = SplitVTs[i].getTypeForEVT(Ctx);
Amara Emersonfbaf4252019-09-03 21:42:28 +0000245 SplitArgs.emplace_back(OrigArg.Regs[i], SplitTy, OrigArg.Flags[0],
Diana Picus253b53b2019-06-27 09:24:30 +0000246 OrigArg.IsFixed);
Tim Northoveref1fc5a2017-08-21 21:56:11 +0000247 if (NeedsRegBlock)
Amara Emersonfbaf4252019-09-03 21:42:28 +0000248 SplitArgs.back().Flags[0].setInConsecutiveRegs();
Tim Northoverb18ea162016-09-20 15:20:36 +0000249 }
250
Amara Emersonfbaf4252019-09-03 21:42:28 +0000251 SplitArgs.back().Flags[0].setInConsecutiveRegsLast();
Tim Northoverb18ea162016-09-20 15:20:36 +0000252}
253
254bool AArch64CallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000255 const Value *Val,
Matt Arsenaulte3a676e2019-06-24 15:50:29 +0000256 ArrayRef<Register> VRegs,
257 Register SwiftErrorVReg) const {
Tim Northover05cc4852016-12-07 21:05:38 +0000258 auto MIB = MIRBuilder.buildInstrNoInsert(AArch64::RET_ReallyLR);
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000259 assert(((Val && !VRegs.empty()) || (!Val && VRegs.empty())) &&
260 "Return value without a vreg");
261
Tim Northover05cc4852016-12-07 21:05:38 +0000262 bool Success = true;
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000263 if (!VRegs.empty()) {
264 MachineFunction &MF = MIRBuilder.getMF();
265 const Function &F = MF.getFunction();
266
Amara Emerson5a3bb682018-06-01 13:20:32 +0000267 MachineRegisterInfo &MRI = MF.getRegInfo();
Tim Northoverb18ea162016-09-20 15:20:36 +0000268 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
269 CCAssignFn *AssignFn = TLI.CCAssignFnForReturn(F.getCallingConv());
Tim Northoverb18ea162016-09-20 15:20:36 +0000270 auto &DL = F.getParent()->getDataLayout();
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000271 LLVMContext &Ctx = Val->getType()->getContext();
Tim Northoverb18ea162016-09-20 15:20:36 +0000272
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000273 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.");
Tim Northover9a467182016-09-21 12:57:45 +0000277
278 SmallVector<ArgInfo, 8> SplitArgs;
Amara Emerson2b523f82019-04-09 21:22:33 +0000279 CallingConv::ID CC = F.getCallingConv();
280
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000281 for (unsigned i = 0; i < SplitEVTs.size(); ++i) {
Amara Emerson2b523f82019-04-09 21:22:33 +0000282 if (TLI.getNumRegistersForCallingConv(Ctx, CC, SplitEVTs[i]) > 1) {
283 LLVM_DEBUG(dbgs() << "Can't handle extended arg types which need split");
284 return false;
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000285 }
286
Matt Arsenaultfaeaedf2019-06-24 16:16:12 +0000287 Register CurVReg = VRegs[i];
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000288 ArgInfo CurArgInfo = ArgInfo{CurVReg, SplitEVTs[i].getTypeForEVT(Ctx)};
289 setArgFlags(CurArgInfo, AttributeList::ReturnIndex, DL, F);
Amara Emerson2b523f82019-04-09 21:22:33 +0000290
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);
295 } else {
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,
301 Attribute::SExt))
302 ExtendOp = TargetOpcode::G_SEXT;
303 else if (F.getAttributes().hasAttribute(AttributeList::ReturnIndex,
304 Attribute::ZExt))
305 ExtendOp = TargetOpcode::G_ZEXT;
306
307 LLT NewLLT(NewVT);
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
Amara Emerson3d1128c2019-05-06 19:41:01 +0000311 // 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");
319 return false;
320 }
321 auto Undef = MIRBuilder.buildUndef({OldLLT});
322 CurVReg =
323 MIRBuilder.buildMerge({NewLLT}, {CurVReg, Undef.getReg(0)})
324 .getReg(0);
325 } else {
326 // Just do a vector extend.
327 CurVReg = MIRBuilder.buildInstr(ExtendOp, {NewLLT}, {CurVReg})
328 .getReg(0);
329 }
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});
335 CurVReg =
336 MIRBuilder
337 .buildBuildVector({NewLLT}, {CurVReg, Undef.getReg(0)})
338 .getReg(0);
339 } else {
340 LLVM_DEBUG(dbgs() << "Could not handle ret ty");
Amara Emerson2b523f82019-04-09 21:22:33 +0000341 return false;
342 }
Amara Emerson2b523f82019-04-09 21:22:33 +0000343 } else {
Amara Emerson3d1128c2019-05-06 19:41:01 +0000344 // A scalar extend.
Amara Emerson2b523f82019-04-09 21:22:33 +0000345 CurVReg =
346 MIRBuilder.buildInstr(ExtendOp, {NewLLT}, {CurVReg}).getReg(0);
347 }
348 }
349 }
Diana Picus69ce1c132019-06-27 08:50:53 +0000350 if (CurVReg != CurArgInfo.Regs[0]) {
351 CurArgInfo.Regs[0] = CurVReg;
Amara Emerson2b523f82019-04-09 21:22:33 +0000352 // Reset the arg flags after modifying CurVReg.
353 setArgFlags(CurArgInfo, AttributeList::ReturnIndex, DL, F);
354 }
Diana Picus253b53b2019-06-27 09:24:30 +0000355 splitToValueTypes(CurArgInfo, SplitArgs, DL, MRI, CC);
Alexander Ivchenko49168f62018-08-02 08:33:31 +0000356 }
Tim Northoverb18ea162016-09-20 15:20:36 +0000357
Tim Northoverd9433542017-01-17 22:30:10 +0000358 OutgoingArgHandler Handler(MIRBuilder, MRI, MIB, AssignFn, AssignFn);
359 Success = handleAssignments(MIRBuilder, SplitArgs, Handler);
Tim Northoverb18ea162016-09-20 15:20:36 +0000360 }
Tim Northover05cc4852016-12-07 21:05:38 +0000361
Tim Northover3b2157a2019-05-24 08:40:13 +0000362 if (SwiftErrorVReg) {
363 MIB.addUse(AArch64::X21, RegState::Implicit);
364 MIRBuilder.buildCopy(AArch64::X21, SwiftErrorVReg);
365 }
366
Tim Northover05cc4852016-12-07 21:05:38 +0000367 MIRBuilder.insertInstr(MIB);
368 return Success;
Tim Northoverb18ea162016-09-20 15:20:36 +0000369}
370
Diana Picusc3dbe232019-06-27 08:54:17 +0000371bool AArch64CallLowering::lowerFormalArguments(
372 MachineIRBuilder &MIRBuilder, const Function &F,
373 ArrayRef<ArrayRef<Register>> VRegs) const {
Tim Northover406024a2016-08-10 21:44:01 +0000374 MachineFunction &MF = MIRBuilder.getMF();
Tim Northoverb18ea162016-09-20 15:20:36 +0000375 MachineBasicBlock &MBB = MIRBuilder.getMBB();
376 MachineRegisterInfo &MRI = MF.getRegInfo();
Tim Northoverb18ea162016-09-20 15:20:36 +0000377 auto &DL = F.getParent()->getDataLayout();
Tim Northover406024a2016-08-10 21:44:01 +0000378
Tim Northover9a467182016-09-21 12:57:45 +0000379 SmallVector<ArgInfo, 8> SplitArgs;
Tim Northoverb18ea162016-09-20 15:20:36 +0000380 unsigned i = 0;
Reid Kleckner45707d42017-03-16 22:59:15 +0000381 for (auto &Arg : F.args()) {
Amara Emersond78d65c2017-11-30 20:06:02 +0000382 if (DL.getTypeStoreSize(Arg.getType()) == 0)
383 continue;
Diana Picusc3dbe232019-06-27 08:54:17 +0000384
Tim Northover9a467182016-09-21 12:57:45 +0000385 ArgInfo OrigArg{VRegs[i], Arg.getType()};
Reid Klecknera0b45f42017-05-03 18:17:31 +0000386 setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, F);
Tim Northoverc2c545b2017-03-06 23:50:28 +0000387
Diana Picus253b53b2019-06-27 09:24:30 +0000388 splitToValueTypes(OrigArg, SplitArgs, DL, MRI, F.getCallingConv());
Tim Northoverb18ea162016-09-20 15:20:36 +0000389 ++i;
390 }
391
392 if (!MBB.empty())
393 MIRBuilder.setInstr(*MBB.begin());
Tim Northover406024a2016-08-10 21:44:01 +0000394
395 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
396 CCAssignFn *AssignFn =
397 TLI.CCAssignFnForCall(F.getCallingConv(), /*IsVarArg=*/false);
398
Tim Northoverd9433542017-01-17 22:30:10 +0000399 FormalArgHandler Handler(MIRBuilder, MRI, AssignFn);
400 if (!handleAssignments(MIRBuilder, SplitArgs, Handler))
Tim Northover9a467182016-09-21 12:57:45 +0000401 return false;
Tim Northoverb18ea162016-09-20 15:20:36 +0000402
Jessica Paquettea42070a2019-09-12 22:10:36 +0000403 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
404 uint64_t StackOffset = Handler.StackUsed;
Tim Northovere9600d82017-02-08 17:57:27 +0000405 if (F.isVarArg()) {
Tim Northoverf1c28922019-09-12 10:22:23 +0000406 auto &Subtarget = MF.getSubtarget<AArch64Subtarget>();
407 if (!Subtarget.isTargetDarwin()) {
408 // FIXME: we need to reimplement saveVarArgsRegisters from
Tim Northovere9600d82017-02-08 17:57:27 +0000409 // AArch64ISelLowering.
410 return false;
411 }
412
Tim Northoverf1c28922019-09-12 10:22:23 +0000413 // We currently pass all varargs at 8-byte alignment, or 4 in ILP32.
Jessica Paquettea42070a2019-09-12 22:10:36 +0000414 StackOffset = alignTo(Handler.StackUsed, Subtarget.isTargetILP32() ? 4 : 8);
Tim Northovere9600d82017-02-08 17:57:27 +0000415
416 auto &MFI = MIRBuilder.getMF().getFrameInfo();
Tim Northovere9600d82017-02-08 17:57:27 +0000417 FuncInfo->setVarArgsStackIndex(MFI.CreateFixedObject(4, StackOffset, true));
418 }
419
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000420 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);
425
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);
429
430 // Our own callers will guarantee that the space is free by giving an
431 // aligned value to CALLSEQ_START.
432 }
433
434 // When we tail call, we need to check if the callee's arguments
Jessica Paquettea42070a2019-09-12 22:10:36 +0000435 // 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);
439
Tri Vo6c47c622018-09-22 22:17:50 +0000440 auto &Subtarget = MF.getSubtarget<AArch64Subtarget>();
441 if (Subtarget.hasCustomCallingConv())
442 Subtarget.getRegisterInfo()->UpdateCustomCalleeSavedRegs(MF);
443
Tim Northoverb18ea162016-09-20 15:20:36 +0000444 // Move back to the end of the basic block.
445 MIRBuilder.setMBB(MBB);
446
Tim Northover9a467182016-09-21 12:57:45 +0000447 return true;
Tim Northover406024a2016-08-10 21:44:01 +0000448}
449
Jessica Paquette20e86672019-09-05 20:18:34 +0000450/// Return true if the calling convention is one that we can guarantee TCO for.
451static bool canGuaranteeTCO(CallingConv::ID CC) {
452 return CC == CallingConv::Fast;
453}
454
455/// Return true if we might ever do TCO for calls with this calling convention.
456static bool mayTailCallThisCC(CallingConv::ID CC) {
457 switch (CC) {
458 case CallingConv::C:
459 case CallingConv::PreserveMost:
460 case CallingConv::Swift:
461 return true;
462 default:
463 return canGuaranteeTCO(CC);
464 }
465}
466
Jessica Paquette2af5b192019-09-10 23:25:12 +0000467bool 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();
473
474 // If the calling conventions match, then everything must be the same.
475 if (CalleeCC == CallerCC)
476 return true;
477
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());
483
484 if (!resultsCompatible(Info, MF, InArgs, *CalleeAssignFn, *CallerAssignFn))
485 return false;
486
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);
494 }
495
496 return TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved);
497}
498
Jessica Paquettea42070a2019-09-12 22:10:36 +0000499bool AArch64CallLowering::areCalleeOutgoingArgsTailCallable(
500 CallLoweringInfo &Info, MachineFunction &MF,
501 SmallVectorImpl<ArgInfo> &OutArgs) const {
502 // If there are no outgoing arguments, then we are done.
503 if (OutArgs.empty())
504 return true;
505
506 const Function &CallerF = MF.getFunction();
507 CallingConv::ID CalleeCC = Info.CallConv;
508 CallingConv::ID CallerCC = CallerF.getCallingConv();
509 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
510
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());
514
515 if (!analyzeArgInfo(OutInfo, OutArgs,
516 *TLI.CCAssignFnForCall(CalleeCC, Info.IsVarArg))) {
517 LLVM_DEBUG(dbgs() << "... Could not analyze call operands.\n");
518 return false;
519 }
520
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");
525 return false;
526 }
527
528 // Verify that the parameters in callee-saved registers match.
529 // TODO: Port this over to CallLowering as general code once swiftself is
530 // supported.
531 auto TRI = MF.getSubtarget<AArch64Subtarget>().getRegisterInfo();
532 const uint32_t *CallerPreservedMask = TRI->getCallPreservedMask(MF, CallerCC);
Jessica Paquette0c283cb2019-09-12 23:00:59 +0000533 MachineRegisterInfo &MRI = MF.getRegInfo();
Jessica Paquettea42070a2019-09-12 22:10:36 +0000534
Jessica Paquette0c283cb2019-09-12 23:00:59 +0000535 for (unsigned i = 0; i < OutLocs.size(); ++i) {
536 auto &ArgLoc = OutLocs[i];
Jessica Paquettea42070a2019-09-12 22:10:36 +0000537 // If it's not a register, it's fine.
Jessica Paquette14bfb562019-09-13 16:10:19 +0000538 if (!ArgLoc.isRegLoc()) {
539 if (Info.IsVarArg) {
540 // Be conservative and disallow variadic memory operands to match SDAG's
541 // behaviour.
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.
545 LLVM_DEBUG(
546 dbgs()
547 << "... Cannot tail call vararg function with stack arguments\n");
548 return false;
549 }
Jessica Paquettea42070a2019-09-12 22:10:36 +0000550 continue;
Jessica Paquette14bfb562019-09-13 16:10:19 +0000551 }
Jessica Paquettea42070a2019-09-12 22:10:36 +0000552
553 Register Reg = ArgLoc.getLocReg();
554
555 // Only look at callee-saved registers.
556 if (MachineOperand::clobbersPhysReg(CallerPreservedMask, Reg))
557 continue;
558
Jessica Paquettea42070a2019-09-12 22:10:36 +0000559 LLVM_DEBUG(
560 dbgs()
Jessica Paquette0c283cb2019-09-12 23:00:59 +0000561 << "... Call has an argument passed in a callee-saved register.\n");
562
563 // Check if it was copied from.
564 ArgInfo &OutInfo = OutArgs[i];
565
566 if (OutInfo.Regs.size() > 1) {
567 LLVM_DEBUG(
568 dbgs() << "... Cannot handle arguments in multiple registers.\n");
569 return false;
570 }
571
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) {
577 LLVM_DEBUG(
578 dbgs()
579 << "... Parameter was not copied into a VReg, cannot tail call.\n");
580 return false;
581 }
582
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");
588 return false;
589 }
Jessica Paquettea42070a2019-09-12 22:10:36 +0000590 }
591
592 return true;
593}
594
Jessica Paquette20e86672019-09-05 20:18:34 +0000595bool AArch64CallLowering::isEligibleForTailCallOptimization(
Jessica Paquette2af5b192019-09-10 23:25:12 +0000596 MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info,
Jessica Paquettea42070a2019-09-12 22:10:36 +0000597 SmallVectorImpl<ArgInfo> &InArgs,
598 SmallVectorImpl<ArgInfo> &OutArgs) const {
Jessica Paquettece65ebc2019-09-18 22:42:25 +0000599
600 // Must pass all target-independent checks in order to tail call optimize.
601 if (!Info.IsTailCall)
602 return false;
603
Jessica Paquette20e86672019-09-05 20:18:34 +0000604 CallingConv::ID CalleeCC = Info.CallConv;
605 MachineFunction &MF = MIRBuilder.getMF();
606 const Function &CallerF = MF.getFunction();
Jessica Paquette20e86672019-09-05 20:18:34 +0000607
608 LLVM_DEBUG(dbgs() << "Attempting to lower call as tail call\n");
609
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");
616 return false;
617 }
618
Jessica Paquette20e86672019-09-05 20:18:34 +0000619 if (!mayTailCallThisCC(CalleeCC)) {
620 LLVM_DEBUG(dbgs() << "... Calling convention cannot be tail called.\n");
621 return false;
622 }
623
Jessica Paquette20e86672019-09-05 20:18:34 +0000624 // 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
626 // X86).
627 //
628 // FIXME: In AArch64ISelLowering, this isn't worked around. Can/should we try
629 // it?
630 //
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.
635 //
636 // FIXME: Check whether the callee also has an "inreg" argument.
Jessica Paquettee297ad12019-09-11 23:44:16 +0000637 //
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
640 // tail call.
Jessica Paquette20e86672019-09-05 20:18:34 +0000641 if (any_of(CallerF.args(), [](const Argument &A) {
Jessica Paquettee297ad12019-09-11 23:44:16 +0000642 return A.hasByValAttr() || A.hasInRegAttr() || A.hasSwiftErrorAttr();
Jessica Paquette20e86672019-09-05 20:18:34 +0000643 })) {
Jessica Paquettee297ad12019-09-11 23:44:16 +0000644 LLVM_DEBUG(dbgs() << "... Cannot tail call from callers with byval, "
645 "inreg, or swifterror arguments\n");
Jessica Paquette20e86672019-09-05 20:18:34 +0000646 return false;
647 }
648
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");
664 return false;
665 }
666 }
667
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000668 // If we have -tailcallopt, then we're done.
669 if (MF.getTarget().Options.GuaranteedTailCallOpt)
670 return canGuaranteeTCO(CalleeCC) && CalleeCC == CallerF.getCallingConv();
671
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.
Jessica Paquette20e86672019-09-05 20:18:34 +0000674
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");
679
Jessica Paquette14bfb562019-09-13 16:10:19 +0000680 // 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
682 // addressing that!
Jessica Paquettece65ebc2019-09-18 22:42:25 +0000683 if (Info.IsVarArg && Info.IsMustTailCall) {
684 LLVM_DEBUG(
685 dbgs()
686 << "... Cannot handle vararg musttail functions yet.\n");
687 return false;
688 }
Jessica Paquette14bfb562019-09-13 16:10:19 +0000689
Jessica Paquettea42070a2019-09-12 22:10:36 +0000690 // Verify that the incoming and outgoing arguments from the callee are
691 // safe to tail call.
Jessica Paquette2af5b192019-09-10 23:25:12 +0000692 if (!doCallerAndCalleePassArgsTheSameWay(Info, MF, InArgs)) {
Jessica Paquette20e86672019-09-05 20:18:34 +0000693 LLVM_DEBUG(
694 dbgs()
Jessica Paquette2af5b192019-09-10 23:25:12 +0000695 << "... Caller and callee have incompatible calling conventions.\n");
Jessica Paquette20e86672019-09-05 20:18:34 +0000696 return false;
697 }
698
Jessica Paquettea42070a2019-09-12 22:10:36 +0000699 if (!areCalleeOutgoingArgsTailCallable(Info, MF, OutArgs))
Jessica Paquette20e86672019-09-05 20:18:34 +0000700 return false;
Jessica Paquette20e86672019-09-05 20:18:34 +0000701
702 LLVM_DEBUG(
703 dbgs() << "... Call is eligible for tail call optimization.\n");
704 return true;
705}
706
707static unsigned getCallOpcode(const Function &CallerF, bool IsIndirect,
708 bool IsTailCall) {
709 if (!IsTailCall)
710 return IsIndirect ? AArch64::BLR : AArch64::BL;
711
712 if (!IsIndirect)
713 return AArch64::TCRETURNdi;
714
715 // When BTI is enabled, we need to use TCRETURNriBTI to make sure that we use
716 // x16 or x17.
717 if (CallerF.hasFnAttribute("branch-target-enforcement"))
718 return AArch64::TCRETURNriBTI;
719
720 return AArch64::TCRETURNri;
721}
722
Jessica Paquetted16cf402019-09-17 19:08:44 +0000723bool 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>();
730
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000731 // True when we're tail calling, but without -tailcallopt.
732 bool IsSibCall = !MF.getTarget().Options.GuaranteedTailCallOpt;
733
Jessica Paquetted16cf402019-09-17 19:08:44 +0000734 // 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")) {
737 LLVM_DEBUG(
738 dbgs() << "Cannot lower indirect tail calls with BTI enabled yet.\n");
739 return false;
740 }
741
742 // Find out which ABI gets to decide where things go.
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000743 CallingConv::ID CalleeCC = Info.CallConv;
Jessica Paquetted16cf402019-09-17 19:08:44 +0000744 CCAssignFn *AssignFnFixed =
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000745 TLI.CCAssignFnForCall(CalleeCC, /*IsVarArg=*/false);
Jessica Paquetted16cf402019-09-17 19:08:44 +0000746 CCAssignFn *AssignFnVarArg =
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000747 TLI.CCAssignFnForCall(CalleeCC, /*IsVarArg=*/true);
748
749 MachineInstrBuilder CallSeqStart;
750 if (!IsSibCall)
751 CallSeqStart = MIRBuilder.buildInstr(AArch64::ADJCALLSTACKDOWN);
Jessica Paquetted16cf402019-09-17 19:08:44 +0000752
753 unsigned Opc = getCallOpcode(F, Info.Callee.isReg(), true);
754 auto MIB = MIRBuilder.buildInstrNoInsert(Opc);
755 MIB.add(Info.Callee);
756
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000757 // Byte offset for the tail call. When we are sibcalling, this will always
758 // be 0.
Jessica Paquetted16cf402019-09-17 19:08:44 +0000759 MIB.addImm(0);
760
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);
767
768 if (TRI->isAnyArgRegReserved(MF))
769 TRI->emitReservedArgRegCallError(MF);
770
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000771 // 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.
776 int FPDiff = 0;
777
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;
782 if (!IsSibCall) {
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
785 // arguments.
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));
792
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);
796
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;
801
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");
808 }
809
Jessica Paquetted16cf402019-09-17 19:08:44 +0000810 // Do the actual argument marshalling.
811 SmallVector<unsigned, 8> PhysRegs;
812 OutgoingArgHandler Handler(MIRBuilder, MRI, MIB, AssignFnFixed,
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000813 AssignFnVarArg, true, FPDiff);
Jessica Paquetted16cf402019-09-17 19:08:44 +0000814 if (!handleAssignments(MIRBuilder, OutArgs, Handler))
815 return false;
816
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000817 // If we have -tailcallopt, we need to adjust the stack. We'll do the call
818 // sequence start and end here.
819 if (!IsSibCall) {
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
825 // location.
826 MIRBuilder.buildInstr(AArch64::ADJCALLSTACKUP).addImm(NumBytes).addImm(0);
827 }
828
Jessica Paquetted16cf402019-09-17 19:08:44 +0000829 // Now we can add the actual call instruction to the correct basic block.
830 MIRBuilder.insertInstr(MIB);
831
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,
838 0));
839
840 MF.getFrameInfo().setHasTailCall();
841 Info.LoweredTailCall = true;
842 return true;
843}
844
Tim Northover406024a2016-08-10 21:44:01 +0000845bool AArch64CallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
Tim Northovere1a5f662019-08-09 08:26:38 +0000846 CallLoweringInfo &Info) const {
Tim Northover406024a2016-08-10 21:44:01 +0000847 MachineFunction &MF = MIRBuilder.getMF();
Matthias Braunf1caa282017-12-15 22:22:58 +0000848 const Function &F = MF.getFunction();
Tim Northoverb18ea162016-09-20 15:20:36 +0000849 MachineRegisterInfo &MRI = MF.getRegInfo();
850 auto &DL = F.getParent()->getDataLayout();
Jessica Paquette2af5b192019-09-10 23:25:12 +0000851 const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
Tim Northoverb18ea162016-09-20 15:20:36 +0000852
Jessica Paquettea42070a2019-09-12 22:10:36 +0000853 SmallVector<ArgInfo, 8> OutArgs;
Tim Northovere1a5f662019-08-09 08:26:38 +0000854 for (auto &OrigArg : Info.OrigArgs) {
Jessica Paquettea42070a2019-09-12 22:10:36 +0000855 splitToValueTypes(OrigArg, OutArgs, DL, MRI, Info.CallConv);
Amara Emerson7a05d1c2019-03-08 22:17:00 +0000856 // AAPCS requires that we zero-extend i1 to 8 bits by the caller.
857 if (OrigArg.Ty->isIntegerTy(1))
Jessica Paquettea42070a2019-09-12 22:10:36 +0000858 OutArgs.back().Flags[0].setZExt();
Tim Northoverb18ea162016-09-20 15:20:36 +0000859 }
Tim Northover406024a2016-08-10 21:44:01 +0000860
Jessica Paquette2af5b192019-09-10 23:25:12 +0000861 SmallVector<ArgInfo, 8> InArgs;
862 if (!Info.OrigRet.Ty->isVoidTy())
863 splitToValueTypes(Info.OrigRet, InArgs, DL, MRI, F.getCallingConv());
864
Jessica Paquetted16cf402019-09-17 19:08:44 +0000865 // If we can lower as a tail call, do that instead.
Jessica Paquettece65ebc2019-09-18 22:42:25 +0000866 bool CanTailCallOpt =
867 isEligibleForTailCallOptimization(MIRBuilder, Info, InArgs, OutArgs);
868
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");
875 return false;
876 }
877
878 if (CanTailCallOpt)
Roman Lebedevad0c2e002019-09-17 19:37:07 +0000879 return lowerTailCall(MIRBuilder, Info, OutArgs);
Jessica Paquette20e86672019-09-05 20:18:34 +0000880
Tim Northover406024a2016-08-10 21:44:01 +0000881 // Find out which ABI gets to decide where things go.
Tim Northoverd9433542017-01-17 22:30:10 +0000882 CCAssignFn *AssignFnFixed =
Tim Northovere1a5f662019-08-09 08:26:38 +0000883 TLI.CCAssignFnForCall(Info.CallConv, /*IsVarArg=*/false);
Tim Northoverd9433542017-01-17 22:30:10 +0000884 CCAssignFn *AssignFnVarArg =
Tim Northovere1a5f662019-08-09 08:26:38 +0000885 TLI.CCAssignFnForCall(Info.CallConv, /*IsVarArg=*/true);
Tim Northover406024a2016-08-10 21:44:01 +0000886
Jessica Paquette20e86672019-09-05 20:18:34 +0000887 MachineInstrBuilder CallSeqStart;
Jessica Paquetted16cf402019-09-17 19:08:44 +0000888 CallSeqStart = MIRBuilder.buildInstr(AArch64::ADJCALLSTACKDOWN);
Tim Northover509091f2017-01-17 22:43:34 +0000889
Tim Northovera5e38fa2016-09-22 13:49:25 +0000890 // Create a temporarily-floating call instruction so we can add the implicit
891 // uses of arg registers.
Jessica Paquetted16cf402019-09-17 19:08:44 +0000892 unsigned Opc = getCallOpcode(F, Info.Callee.isReg(), false);
Jessica Paquette20e86672019-09-05 20:18:34 +0000893
894 auto MIB = MIRBuilder.buildInstrNoInsert(Opc);
Tim Northovere1a5f662019-08-09 08:26:38 +0000895 MIB.add(Info.Callee);
Tim Northover406024a2016-08-10 21:44:01 +0000896
897 // Tell the call which registers are clobbered.
Nick Desaulniers287a3be2018-09-07 20:58:57 +0000898 auto TRI = MF.getSubtarget<AArch64Subtarget>().getRegisterInfo();
Tri Vo6c47c622018-09-22 22:17:50 +0000899 const uint32_t *Mask = TRI->getCallPreservedMask(MF, F.getCallingConv());
900 if (MF.getSubtarget<AArch64Subtarget>().hasCustomCallingConv())
901 TRI->UpdateCustomCallPreservedMask(MF, &Mask);
902 MIB.addRegMask(Mask);
Tim Northover406024a2016-08-10 21:44:01 +0000903
Nick Desaulniers287a3be2018-09-07 20:58:57 +0000904 if (TRI->isAnyArgRegReserved(MF))
905 TRI->emitReservedArgRegCallError(MF);
906
Tim Northovera5e38fa2016-09-22 13:49:25 +0000907 // Do the actual argument marshalling.
908 SmallVector<unsigned, 8> PhysRegs;
Tim Northoverd9433542017-01-17 22:30:10 +0000909 OutgoingArgHandler Handler(MIRBuilder, MRI, MIB, AssignFnFixed,
Jessica Paquetted16cf402019-09-17 19:08:44 +0000910 AssignFnVarArg, false);
Jessica Paquettea42070a2019-09-12 22:10:36 +0000911 if (!handleAssignments(MIRBuilder, OutArgs, Handler))
Tim Northovera5e38fa2016-09-22 13:49:25 +0000912 return false;
913
914 // Now we can add the actual call instruction to the correct basic block.
915 MIRBuilder.insertInstr(MIB);
Tim Northover406024a2016-08-10 21:44:01 +0000916
Quentin Colombetf38015e2016-12-22 21:56:31 +0000917 // 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.
Tim Northovere1a5f662019-08-09 08:26:38 +0000920 if (Info.Callee.isReg())
Quentin Colombetf38015e2016-12-22 21:56:31 +0000921 MIB->getOperand(0).setReg(constrainOperandRegClass(
922 MF, *TRI, MRI, *MF.getSubtarget().getInstrInfo(),
Tim Northovere1a5f662019-08-09 08:26:38 +0000923 *MF.getSubtarget().getRegBankInfo(), *MIB, MIB->getDesc(), Info.Callee,
924 0));
Quentin Colombetf38015e2016-12-22 21:56:31 +0000925
Tim Northover406024a2016-08-10 21:44:01 +0000926 // 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.
Tim Northovere1a5f662019-08-09 08:26:38 +0000929 if (!Info.OrigRet.Ty->isVoidTy()) {
Jessica Paquette2af5b192019-09-10 23:25:12 +0000930 CCAssignFn *RetAssignFn = TLI.CCAssignFnForReturn(F.getCallingConv());
Tim Northoverd9433542017-01-17 22:30:10 +0000931 CallReturnHandler Handler(MIRBuilder, MRI, MIB, RetAssignFn);
Jessica Paquette2af5b192019-09-10 23:25:12 +0000932 if (!handleAssignments(MIRBuilder, InArgs, Handler))
Tim Northover9a467182016-09-21 12:57:45 +0000933 return false;
Tim Northoverb18ea162016-09-20 15:20:36 +0000934 }
935
Tim Northovere1a5f662019-08-09 08:26:38 +0000936 if (Info.SwiftErrorVReg) {
Tim Northover3b2157a2019-05-24 08:40:13 +0000937 MIB.addDef(AArch64::X21, RegState::Implicit);
Tim Northovere1a5f662019-08-09 08:26:38 +0000938 MIRBuilder.buildCopy(Info.SwiftErrorVReg, Register(AArch64::X21));
Tim Northover3b2157a2019-05-24 08:40:13 +0000939 }
940
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000941 uint64_t CalleePopBytes =
942 doesCalleeRestoreStack(Info.CallConv,
943 MF.getTarget().Options.GuaranteedTailCallOpt)
944 ? alignTo(Handler.StackSize, 16)
945 : 0;
946
Jessica Paquettebfb00e32019-09-09 17:15:56 +0000947 CallSeqStart.addImm(Handler.StackSize).addImm(0);
948 MIRBuilder.buildInstr(AArch64::ADJCALLSTACKUP)
949 .addImm(Handler.StackSize)
Jessica Paquette8a4d9f02019-09-17 20:24:23 +0000950 .addImm(CalleePopBytes);
Tim Northover509091f2017-01-17 22:43:34 +0000951
Tim Northover406024a2016-08-10 21:44:01 +0000952 return true;
953}