blob: e498f70b820dbcbd5b403a4ef8fb2039772dbf72 [file] [log] [blame]
Diana Picus22274932016-11-11 08:27:37 +00001//===-- llvm/lib/Target/ARM/ARMCallLowering.cpp - Call lowering -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// This file implements the lowering of LLVM calls to machine code calls for
12/// GlobalISel.
13///
14//===----------------------------------------------------------------------===//
15
16#include "ARMCallLowering.h"
17
18#include "ARMBaseInstrInfo.h"
19#include "ARMISelLowering.h"
Diana Picus1d8eaf42017-01-25 07:08:53 +000020#include "ARMSubtarget.h"
Diana Picus22274932016-11-11 08:27:37 +000021
Diana Picus32cd9b42017-02-02 14:01:00 +000022#include "llvm/CodeGen/Analysis.h"
Diana Picus22274932016-11-11 08:27:37 +000023#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Diana Picus0091cc32017-06-05 12:54:53 +000024#include "llvm/CodeGen/GlobalISel/Utils.h"
Diana Picus1437f6d2016-12-19 11:55:41 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000026
27using namespace llvm;
28
29#ifndef LLVM_BUILD_GLOBAL_ISEL
30#error "This shouldn't be built without GISel"
31#endif
32
33ARMCallLowering::ARMCallLowering(const ARMTargetLowering &TLI)
34 : CallLowering(&TLI) {}
35
Benjamin Kramer061f4a52017-01-13 14:39:03 +000036static bool isSupportedType(const DataLayout &DL, const ARMTargetLowering &TLI,
Diana Picus812caee2016-12-16 12:54:46 +000037 Type *T) {
Diana Picus8fd16012017-06-15 09:42:02 +000038 if (T->isArrayTy())
Diana Picus8cca8cb2017-05-29 07:01:52 +000039 return true;
40
Diana Picus8fd16012017-06-15 09:42:02 +000041 if (T->isStructTy()) {
42 // For now we only allow homogeneous structs that we can manipulate with
43 // G_MERGE_VALUES and G_UNMERGE_VALUES
44 auto StructT = cast<StructType>(T);
45 for (unsigned i = 1, e = StructT->getNumElements(); i != e; ++i)
46 if (StructT->getElementType(i) != StructT->getElementType(0))
47 return false;
48 return true;
49 }
50
Diana Picus0c11c7b2017-02-02 14:00:54 +000051 EVT VT = TLI.getValueType(DL, T, true);
Diana Picusf941ec02017-04-21 11:53:01 +000052 if (!VT.isSimple() || VT.isVector() ||
53 !(VT.isInteger() || VT.isFloatingPoint()))
Diana Picus97ae95c2016-12-19 14:08:02 +000054 return false;
55
56 unsigned VTSize = VT.getSimpleVT().getSizeInBits();
Diana Picusca6a8902017-02-16 07:53:07 +000057
58 if (VTSize == 64)
59 // FIXME: Support i64 too
60 return VT.isFloatingPoint();
61
Diana Picusd83df5d2017-01-25 08:47:40 +000062 return VTSize == 1 || VTSize == 8 || VTSize == 16 || VTSize == 32;
Diana Picus812caee2016-12-16 12:54:46 +000063}
64
65namespace {
Diana Picusa6067132017-02-23 13:25:43 +000066/// Helper class for values going out through an ABI boundary (used for handling
67/// function return values and call parameters).
68struct OutgoingValueHandler : public CallLowering::ValueHandler {
69 OutgoingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
70 MachineInstrBuilder &MIB, CCAssignFn *AssignFn)
Diana Picus1ffca2a2017-02-28 14:17:53 +000071 : ValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB), StackSize(0) {}
Diana Picus812caee2016-12-16 12:54:46 +000072
73 unsigned getStackAddress(uint64_t Size, int64_t Offset,
74 MachinePointerInfo &MPO) override {
Diana Picus38415222017-03-01 15:54:21 +000075 assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) &&
76 "Unsupported size");
Diana Picus1ffca2a2017-02-28 14:17:53 +000077
78 LLT p0 = LLT::pointer(0, 32);
79 LLT s32 = LLT::scalar(32);
80 unsigned SPReg = MRI.createGenericVirtualRegister(p0);
81 MIRBuilder.buildCopy(SPReg, ARM::SP);
82
83 unsigned OffsetReg = MRI.createGenericVirtualRegister(s32);
84 MIRBuilder.buildConstant(OffsetReg, Offset);
85
86 unsigned AddrReg = MRI.createGenericVirtualRegister(p0);
87 MIRBuilder.buildGEP(AddrReg, SPReg, OffsetReg);
88
89 MPO = MachinePointerInfo::getStack(MIRBuilder.getMF(), Offset);
Diana Picus1ffca2a2017-02-28 14:17:53 +000090 return AddrReg;
Diana Picus812caee2016-12-16 12:54:46 +000091 }
92
93 void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
94 CCValAssign &VA) override {
95 assert(VA.isRegLoc() && "Value shouldn't be assigned to reg");
96 assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?");
97
Diana Picusca6a8902017-02-16 07:53:07 +000098 assert(VA.getValVT().getSizeInBits() <= 64 && "Unsupported value size");
99 assert(VA.getLocVT().getSizeInBits() <= 64 && "Unsupported location size");
Diana Picus812caee2016-12-16 12:54:46 +0000100
Diana Picus8b6c6be2017-01-25 08:10:40 +0000101 unsigned ExtReg = extendRegister(ValVReg, VA);
102 MIRBuilder.buildCopy(PhysReg, ExtReg);
Diana Picus812caee2016-12-16 12:54:46 +0000103 MIB.addUse(PhysReg, RegState::Implicit);
104 }
105
106 void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size,
107 MachinePointerInfo &MPO, CCValAssign &VA) override {
Diana Picus9c523092017-03-01 15:35:14 +0000108 assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) &&
109 "Unsupported size");
Diana Picus1ffca2a2017-02-28 14:17:53 +0000110
Diana Picus9c523092017-03-01 15:35:14 +0000111 unsigned ExtReg = extendRegister(ValVReg, VA);
Diana Picus1ffca2a2017-02-28 14:17:53 +0000112 auto MMO = MIRBuilder.getMF().getMachineMemOperand(
Diana Picus9c523092017-03-01 15:35:14 +0000113 MPO, MachineMemOperand::MOStore, VA.getLocVT().getStoreSize(),
114 /* Alignment */ 0);
115 MIRBuilder.buildStore(ExtReg, Addr, *MMO);
Diana Picus812caee2016-12-16 12:54:46 +0000116 }
117
Diana Picusca6a8902017-02-16 07:53:07 +0000118 unsigned assignCustomValue(const CallLowering::ArgInfo &Arg,
119 ArrayRef<CCValAssign> VAs) override {
120 CCValAssign VA = VAs[0];
121 assert(VA.needsCustom() && "Value doesn't need custom handling");
122 assert(VA.getValVT() == MVT::f64 && "Unsupported type");
123
124 CCValAssign NextVA = VAs[1];
125 assert(NextVA.needsCustom() && "Value doesn't need custom handling");
126 assert(NextVA.getValVT() == MVT::f64 && "Unsupported type");
127
128 assert(VA.getValNo() == NextVA.getValNo() &&
129 "Values belong to different arguments");
130
131 assert(VA.isRegLoc() && "Value should be in reg");
132 assert(NextVA.isRegLoc() && "Value should be in reg");
133
134 unsigned NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)),
135 MRI.createGenericVirtualRegister(LLT::scalar(32))};
Diana Picus0b4190a2017-06-07 12:35:05 +0000136 MIRBuilder.buildUnmerge(NewRegs, Arg.Reg);
Diana Picusca6a8902017-02-16 07:53:07 +0000137
138 bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle();
139 if (!IsLittle)
140 std::swap(NewRegs[0], NewRegs[1]);
141
142 assignValueToReg(NewRegs[0], VA.getLocReg(), VA);
143 assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA);
144
145 return 1;
146 }
147
Diana Picus9c523092017-03-01 15:35:14 +0000148 bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT,
Diana Picus38415222017-03-01 15:54:21 +0000149 CCValAssign::LocInfo LocInfo,
150 const CallLowering::ArgInfo &Info, CCState &State) override {
Diana Picus9c523092017-03-01 15:35:14 +0000151 if (AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State))
152 return true;
153
Diana Picus38415222017-03-01 15:54:21 +0000154 StackSize =
155 std::max(StackSize, static_cast<uint64_t>(State.getNextStackOffset()));
Diana Picus9c523092017-03-01 15:35:14 +0000156 return false;
157 }
158
Diana Picus812caee2016-12-16 12:54:46 +0000159 MachineInstrBuilder &MIB;
Diana Picus1ffca2a2017-02-28 14:17:53 +0000160 uint64_t StackSize;
Diana Picus812caee2016-12-16 12:54:46 +0000161};
162} // End anonymous namespace.
163
Diana Picus8cca8cb2017-05-29 07:01:52 +0000164void ARMCallLowering::splitToValueTypes(
165 const ArgInfo &OrigArg, SmallVectorImpl<ArgInfo> &SplitArgs,
166 MachineFunction &MF, const SplitArgTy &PerformArgSplit) const {
Diana Picus32cd9b42017-02-02 14:01:00 +0000167 const ARMTargetLowering &TLI = *getTLI<ARMTargetLowering>();
168 LLVMContext &Ctx = OrigArg.Ty->getContext();
Diana Picus8cca8cb2017-05-29 07:01:52 +0000169 const DataLayout &DL = MF.getDataLayout();
170 MachineRegisterInfo &MRI = MF.getRegInfo();
171 const Function *F = MF.getFunction();
Diana Picus32cd9b42017-02-02 14:01:00 +0000172
173 SmallVector<EVT, 4> SplitVTs;
174 SmallVector<uint64_t, 4> Offsets;
175 ComputeValueVTs(TLI, DL, OrigArg.Ty, SplitVTs, &Offsets, 0);
176
Diana Picus8cca8cb2017-05-29 07:01:52 +0000177 if (SplitVTs.size() == 1) {
178 // Even if there is no splitting to do, we still want to replace the
179 // original type (e.g. pointer type -> integer).
Diana Picuse7aa9092017-06-02 10:16:48 +0000180 auto Flags = OrigArg.Flags;
181 unsigned OriginalAlignment = DL.getABITypeAlignment(OrigArg.Ty);
182 Flags.setOrigAlign(OriginalAlignment);
183 SplitArgs.emplace_back(OrigArg.Reg, SplitVTs[0].getTypeForEVT(Ctx), Flags,
184 OrigArg.IsFixed);
Diana Picus8cca8cb2017-05-29 07:01:52 +0000185 return;
186 }
Diana Picus32cd9b42017-02-02 14:01:00 +0000187
Diana Picus8cca8cb2017-05-29 07:01:52 +0000188 unsigned FirstRegIdx = SplitArgs.size();
189 for (unsigned i = 0, e = SplitVTs.size(); i != e; ++i) {
190 EVT SplitVT = SplitVTs[i];
191 Type *SplitTy = SplitVT.getTypeForEVT(Ctx);
192 auto Flags = OrigArg.Flags;
Diana Picuse7aa9092017-06-02 10:16:48 +0000193
194 unsigned OriginalAlignment = DL.getABITypeAlignment(SplitTy);
195 Flags.setOrigAlign(OriginalAlignment);
196
Diana Picus8cca8cb2017-05-29 07:01:52 +0000197 bool NeedsConsecutiveRegisters =
198 TLI.functionArgumentNeedsConsecutiveRegisters(
199 SplitTy, F->getCallingConv(), F->isVarArg());
200 if (NeedsConsecutiveRegisters) {
201 Flags.setInConsecutiveRegs();
202 if (i == e - 1)
203 Flags.setInConsecutiveRegsLast();
204 }
Diana Picuse7aa9092017-06-02 10:16:48 +0000205
Diana Picus8cca8cb2017-05-29 07:01:52 +0000206 SplitArgs.push_back(
207 ArgInfo{MRI.createGenericVirtualRegister(getLLTForType(*SplitTy, DL)),
208 SplitTy, Flags, OrigArg.IsFixed});
209 }
210
211 for (unsigned i = 0; i < Offsets.size(); ++i)
212 PerformArgSplit(SplitArgs[FirstRegIdx + i].Reg, Offsets[i] * 8);
Diana Picus32cd9b42017-02-02 14:01:00 +0000213}
214
Diana Picus812caee2016-12-16 12:54:46 +0000215/// Lower the return value for the already existing \p Ret. This assumes that
216/// \p MIRBuilder's insertion point is correct.
217bool ARMCallLowering::lowerReturnVal(MachineIRBuilder &MIRBuilder,
218 const Value *Val, unsigned VReg,
219 MachineInstrBuilder &Ret) const {
220 if (!Val)
221 // Nothing to do here.
222 return true;
223
224 auto &MF = MIRBuilder.getMF();
225 const auto &F = *MF.getFunction();
226
227 auto DL = MF.getDataLayout();
228 auto &TLI = *getTLI<ARMTargetLowering>();
229 if (!isSupportedType(DL, TLI, Val->getType()))
Diana Picus22274932016-11-11 08:27:37 +0000230 return false;
231
Diana Picus32cd9b42017-02-02 14:01:00 +0000232 SmallVector<ArgInfo, 4> SplitVTs;
Diana Picus8fd16012017-06-15 09:42:02 +0000233 SmallVector<unsigned, 4> Regs;
Diana Picus32cd9b42017-02-02 14:01:00 +0000234 ArgInfo RetInfo(VReg, Val->getType());
Reid Klecknerb5180542017-03-21 16:57:19 +0000235 setArgFlags(RetInfo, AttributeList::ReturnIndex, DL, F);
Diana Picusbf4aed22017-05-29 08:19:19 +0000236 splitToValueTypes(RetInfo, SplitVTs, MF, [&](unsigned Reg, uint64_t Offset) {
Diana Picus8fd16012017-06-15 09:42:02 +0000237 Regs.push_back(Reg);
Diana Picusbf4aed22017-05-29 08:19:19 +0000238 });
Diana Picus32cd9b42017-02-02 14:01:00 +0000239
Diana Picus8fd16012017-06-15 09:42:02 +0000240 if (Regs.size() > 1)
241 MIRBuilder.buildUnmerge(Regs, VReg);
242
Diana Picus812caee2016-12-16 12:54:46 +0000243 CCAssignFn *AssignFn =
244 TLI.CCAssignFnForReturn(F.getCallingConv(), F.isVarArg());
Diana Picus22274932016-11-11 08:27:37 +0000245
Diana Picusa6067132017-02-23 13:25:43 +0000246 OutgoingValueHandler RetHandler(MIRBuilder, MF.getRegInfo(), Ret, AssignFn);
Diana Picus32cd9b42017-02-02 14:01:00 +0000247 return handleAssignments(MIRBuilder, SplitVTs, RetHandler);
Diana Picus812caee2016-12-16 12:54:46 +0000248}
249
250bool ARMCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
251 const Value *Val, unsigned VReg) const {
252 assert(!Val == !VReg && "Return value without a vreg");
253
Diana Picus4f8c3e12017-01-13 09:37:56 +0000254 auto Ret = MIRBuilder.buildInstrNoInsert(ARM::BX_RET).add(predOps(ARMCC::AL));
Diana Picus812caee2016-12-16 12:54:46 +0000255
256 if (!lowerReturnVal(MIRBuilder, Val, VReg, Ret))
257 return false;
258
259 MIRBuilder.insertInstr(Ret);
Diana Picus22274932016-11-11 08:27:37 +0000260 return true;
261}
262
Diana Picus812caee2016-12-16 12:54:46 +0000263namespace {
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000264/// Helper class for values coming in through an ABI boundary (used for handling
265/// formal arguments and call return values).
266struct IncomingValueHandler : public CallLowering::ValueHandler {
267 IncomingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
268 CCAssignFn AssignFn)
Tim Northoverd9433542017-01-17 22:30:10 +0000269 : ValueHandler(MIRBuilder, MRI, AssignFn) {}
Diana Picus812caee2016-12-16 12:54:46 +0000270
271 unsigned getStackAddress(uint64_t Size, int64_t Offset,
272 MachinePointerInfo &MPO) override {
Diana Picusca6a8902017-02-16 07:53:07 +0000273 assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) &&
274 "Unsupported size");
Diana Picus1437f6d2016-12-19 11:55:41 +0000275
276 auto &MFI = MIRBuilder.getMF().getFrameInfo();
277
278 int FI = MFI.CreateFixedObject(Size, Offset, true);
279 MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
280
281 unsigned AddrReg =
282 MRI.createGenericVirtualRegister(LLT::pointer(MPO.getAddrSpace(), 32));
283 MIRBuilder.buildFrameIndex(AddrReg, FI);
284
285 return AddrReg;
286 }
287
288 void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size,
289 MachinePointerInfo &MPO, CCValAssign &VA) override {
Diana Picusca6a8902017-02-16 07:53:07 +0000290 assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) &&
291 "Unsupported size");
Diana Picus278c7222017-01-26 09:20:47 +0000292
293 if (VA.getLocInfo() == CCValAssign::SExt ||
294 VA.getLocInfo() == CCValAssign::ZExt) {
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000295 // If the value is zero- or sign-extended, its size becomes 4 bytes, so
296 // that's what we should load.
Diana Picus278c7222017-01-26 09:20:47 +0000297 Size = 4;
298 assert(MRI.getType(ValVReg).isScalar() && "Only scalars supported atm");
Diana Picus1437f6d2016-12-19 11:55:41 +0000299
Diana Picus4f46be32017-04-27 10:23:30 +0000300 auto LoadVReg = MRI.createGenericVirtualRegister(LLT::scalar(32));
301 buildLoad(LoadVReg, Addr, Size, /* Alignment */ 0, MPO);
302 MIRBuilder.buildTrunc(ValVReg, LoadVReg);
303 } else {
304 // If the value is not extended, a simple load will suffice.
305 buildLoad(ValVReg, Addr, Size, /* Alignment */ 0, MPO);
306 }
307 }
308
309 void buildLoad(unsigned Val, unsigned Addr, uint64_t Size, unsigned Alignment,
310 MachinePointerInfo &MPO) {
Diana Picus1437f6d2016-12-19 11:55:41 +0000311 auto MMO = MIRBuilder.getMF().getMachineMemOperand(
Diana Picus4f46be32017-04-27 10:23:30 +0000312 MPO, MachineMemOperand::MOLoad, Size, Alignment);
313 MIRBuilder.buildLoad(Val, Addr, *MMO);
Diana Picus812caee2016-12-16 12:54:46 +0000314 }
315
316 void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
317 CCValAssign &VA) override {
318 assert(VA.isRegLoc() && "Value shouldn't be assigned to reg");
319 assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?");
320
Diana Picusca6a8902017-02-16 07:53:07 +0000321 assert(VA.getValVT().getSizeInBits() <= 64 && "Unsupported value size");
322 assert(VA.getLocVT().getSizeInBits() <= 64 && "Unsupported location size");
Diana Picus812caee2016-12-16 12:54:46 +0000323
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000324 // The necesary extensions are handled on the other side of the ABI
325 // boundary.
326 markPhysRegUsed(PhysReg);
Diana Picus812caee2016-12-16 12:54:46 +0000327 MIRBuilder.buildCopy(ValVReg, PhysReg);
328 }
Diana Picusca6a8902017-02-16 07:53:07 +0000329
Diana Picusa6067132017-02-23 13:25:43 +0000330 unsigned assignCustomValue(const ARMCallLowering::ArgInfo &Arg,
Diana Picusca6a8902017-02-16 07:53:07 +0000331 ArrayRef<CCValAssign> VAs) override {
332 CCValAssign VA = VAs[0];
333 assert(VA.needsCustom() && "Value doesn't need custom handling");
334 assert(VA.getValVT() == MVT::f64 && "Unsupported type");
335
336 CCValAssign NextVA = VAs[1];
337 assert(NextVA.needsCustom() && "Value doesn't need custom handling");
338 assert(NextVA.getValVT() == MVT::f64 && "Unsupported type");
339
340 assert(VA.getValNo() == NextVA.getValNo() &&
341 "Values belong to different arguments");
342
343 assert(VA.isRegLoc() && "Value should be in reg");
344 assert(NextVA.isRegLoc() && "Value should be in reg");
345
346 unsigned NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)),
347 MRI.createGenericVirtualRegister(LLT::scalar(32))};
348
349 assignValueToReg(NewRegs[0], VA.getLocReg(), VA);
350 assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA);
351
352 bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle();
353 if (!IsLittle)
354 std::swap(NewRegs[0], NewRegs[1]);
355
Diana Picus0b4190a2017-06-07 12:35:05 +0000356 MIRBuilder.buildMerge(Arg.Reg, NewRegs);
Diana Picusca6a8902017-02-16 07:53:07 +0000357
358 return 1;
359 }
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000360
361 /// Marking a physical register as used is different between formal
362 /// parameters, where it's a basic block live-in, and call returns, where it's
363 /// an implicit-def of the call instruction.
364 virtual void markPhysRegUsed(unsigned PhysReg) = 0;
365};
366
367struct FormalArgHandler : public IncomingValueHandler {
368 FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
369 CCAssignFn AssignFn)
370 : IncomingValueHandler(MIRBuilder, MRI, AssignFn) {}
371
372 void markPhysRegUsed(unsigned PhysReg) override {
373 MIRBuilder.getMBB().addLiveIn(PhysReg);
374 }
Diana Picus812caee2016-12-16 12:54:46 +0000375};
376} // End anonymous namespace
377
Diana Picus22274932016-11-11 08:27:37 +0000378bool ARMCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
379 const Function &F,
380 ArrayRef<unsigned> VRegs) const {
Diana Picus812caee2016-12-16 12:54:46 +0000381 // Quick exit if there aren't any args
382 if (F.arg_empty())
383 return true;
384
Diana Picus812caee2016-12-16 12:54:46 +0000385 if (F.isVarArg())
386 return false;
387
Diana Picus32cd9b42017-02-02 14:01:00 +0000388 auto &MF = MIRBuilder.getMF();
Diana Picus8cca8cb2017-05-29 07:01:52 +0000389 auto &MBB = MIRBuilder.getMBB();
Diana Picus32cd9b42017-02-02 14:01:00 +0000390 auto DL = MF.getDataLayout();
Diana Picus812caee2016-12-16 12:54:46 +0000391 auto &TLI = *getTLI<ARMTargetLowering>();
392
Diana Picus7232af32017-02-09 13:09:59 +0000393 auto Subtarget = TLI.getSubtarget();
394
395 if (Subtarget->isThumb())
396 return false;
397
Reid Kleckner45707d42017-03-16 22:59:15 +0000398 for (auto &Arg : F.args())
Diana Picus812caee2016-12-16 12:54:46 +0000399 if (!isSupportedType(DL, TLI, Arg.getType()))
400 return false;
401
402 CCAssignFn *AssignFn =
403 TLI.CCAssignFnForCall(F.getCallingConv(), F.isVarArg());
404
Diana Picus0c05cce2017-05-29 09:09:54 +0000405 FormalArgHandler ArgHandler(MIRBuilder, MIRBuilder.getMF().getRegInfo(),
406 AssignFn);
407
Diana Picus812caee2016-12-16 12:54:46 +0000408 SmallVector<ArgInfo, 8> ArgInfos;
Diana Picus0c05cce2017-05-29 09:09:54 +0000409 SmallVector<unsigned, 4> SplitRegs;
Diana Picus812caee2016-12-16 12:54:46 +0000410 unsigned Idx = 0;
Reid Kleckner45707d42017-03-16 22:59:15 +0000411 for (auto &Arg : F.args()) {
Diana Picus812caee2016-12-16 12:54:46 +0000412 ArgInfo AInfo(VRegs[Idx], Arg.getType());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000413 setArgFlags(AInfo, Idx + AttributeList::FirstArgIndex, DL, F);
Diana Picus8cca8cb2017-05-29 07:01:52 +0000414
Diana Picus0c05cce2017-05-29 09:09:54 +0000415 SplitRegs.clear();
Diana Picus0c05cce2017-05-29 09:09:54 +0000416
Diana Picus8cca8cb2017-05-29 07:01:52 +0000417 splitToValueTypes(AInfo, ArgInfos, MF, [&](unsigned Reg, uint64_t Offset) {
Diana Picus0c05cce2017-05-29 09:09:54 +0000418 SplitRegs.push_back(Reg);
Diana Picus8cca8cb2017-05-29 07:01:52 +0000419 });
Diana Picus0c05cce2017-05-29 09:09:54 +0000420
421 if (!SplitRegs.empty())
Diana Picus8fd16012017-06-15 09:42:02 +0000422 MIRBuilder.buildMerge(VRegs[Idx], SplitRegs);
Diana Picus8cca8cb2017-05-29 07:01:52 +0000423
Diana Picus812caee2016-12-16 12:54:46 +0000424 Idx++;
425 }
426
Diana Picus8cca8cb2017-05-29 07:01:52 +0000427 if (!MBB.empty())
428 MIRBuilder.setInstr(*MBB.begin());
429
Tim Northoverd9433542017-01-17 22:30:10 +0000430 return handleAssignments(MIRBuilder, ArgInfos, ArgHandler);
Diana Picus22274932016-11-11 08:27:37 +0000431}
Diana Picus613b6562017-02-21 11:33:59 +0000432
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000433namespace {
434struct CallReturnHandler : public IncomingValueHandler {
435 CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
436 MachineInstrBuilder MIB, CCAssignFn *AssignFn)
437 : IncomingValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {}
438
439 void markPhysRegUsed(unsigned PhysReg) override {
440 MIB.addDef(PhysReg, RegState::Implicit);
441 }
442
443 MachineInstrBuilder MIB;
444};
445} // End anonymous namespace.
446
Diana Picus613b6562017-02-21 11:33:59 +0000447bool ARMCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
Diana Picusd79253a2017-03-20 14:40:18 +0000448 CallingConv::ID CallConv,
Diana Picus613b6562017-02-21 11:33:59 +0000449 const MachineOperand &Callee,
450 const ArgInfo &OrigRet,
451 ArrayRef<ArgInfo> OrigArgs) const {
Diana Picusa6067132017-02-23 13:25:43 +0000452 MachineFunction &MF = MIRBuilder.getMF();
453 const auto &TLI = *getTLI<ARMTargetLowering>();
454 const auto &DL = MF.getDataLayout();
Diana Picus0091cc32017-06-05 12:54:53 +0000455 const auto &STI = MF.getSubtarget();
456 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
Diana Picusa6067132017-02-23 13:25:43 +0000457 MachineRegisterInfo &MRI = MF.getRegInfo();
Diana Picus613b6562017-02-21 11:33:59 +0000458
459 if (MF.getSubtarget<ARMSubtarget>().genLongCalls())
460 return false;
461
Diana Picus1ffca2a2017-02-28 14:17:53 +0000462 auto CallSeqStart = MIRBuilder.buildInstr(ARM::ADJCALLSTACKDOWN);
Diana Picus613b6562017-02-21 11:33:59 +0000463
Diana Picusa6067132017-02-23 13:25:43 +0000464 // Create the call instruction so we can add the implicit uses of arg
465 // registers, but don't insert it yet.
466 auto MIB = MIRBuilder.buildInstrNoInsert(ARM::BLX).add(Callee).addRegMask(
467 TRI->getCallPreservedMask(MF, CallConv));
Diana Picus0091cc32017-06-05 12:54:53 +0000468 if (Callee.isReg()) {
469 auto CalleeReg = Callee.getReg();
470 if (CalleeReg && !TRI->isPhysicalRegister(CalleeReg))
471 MIB->getOperand(0).setReg(constrainOperandRegClass(
472 MF, *TRI, MRI, *STI.getInstrInfo(), *STI.getRegBankInfo(),
473 *MIB.getInstr(), MIB->getDesc(), CalleeReg, 0));
474 }
Diana Picusa6067132017-02-23 13:25:43 +0000475
476 SmallVector<ArgInfo, 8> ArgInfos;
477 for (auto Arg : OrigArgs) {
478 if (!isSupportedType(DL, TLI, Arg.Ty))
479 return false;
480
481 if (!Arg.IsFixed)
482 return false;
483
Diana Picus8fd16012017-06-15 09:42:02 +0000484 SmallVector<unsigned, 8> Regs;
Diana Picus8cca8cb2017-05-29 07:01:52 +0000485 splitToValueTypes(Arg, ArgInfos, MF, [&](unsigned Reg, uint64_t Offset) {
Diana Picus8fd16012017-06-15 09:42:02 +0000486 Regs.push_back(Reg);
Diana Picus8cca8cb2017-05-29 07:01:52 +0000487 });
Diana Picus8fd16012017-06-15 09:42:02 +0000488
489 if (Regs.size() > 1)
490 MIRBuilder.buildUnmerge(Regs, Arg.Reg);
Diana Picusa6067132017-02-23 13:25:43 +0000491 }
492
493 auto ArgAssignFn = TLI.CCAssignFnForCall(CallConv, /*IsVarArg=*/false);
494 OutgoingValueHandler ArgHandler(MIRBuilder, MRI, MIB, ArgAssignFn);
495 if (!handleAssignments(MIRBuilder, ArgInfos, ArgHandler))
496 return false;
497
498 // Now we can add the actual call instruction to the correct basic block.
499 MIRBuilder.insertInstr(MIB);
Diana Picus613b6562017-02-21 11:33:59 +0000500
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000501 if (!OrigRet.Ty->isVoidTy()) {
502 if (!isSupportedType(DL, TLI, OrigRet.Ty))
503 return false;
504
505 ArgInfos.clear();
Diana Picusbf4aed22017-05-29 08:19:19 +0000506 SmallVector<unsigned, 8> SplitRegs;
Diana Picus8cca8cb2017-05-29 07:01:52 +0000507 splitToValueTypes(OrigRet, ArgInfos, MF,
Diana Picusbf4aed22017-05-29 08:19:19 +0000508 [&](unsigned Reg, uint64_t Offset) {
Diana Picusbf4aed22017-05-29 08:19:19 +0000509 SplitRegs.push_back(Reg);
510 });
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000511
512 auto RetAssignFn = TLI.CCAssignFnForReturn(CallConv, /*IsVarArg=*/false);
513 CallReturnHandler RetHandler(MIRBuilder, MRI, MIB, RetAssignFn);
514 if (!handleAssignments(MIRBuilder, ArgInfos, RetHandler))
515 return false;
Diana Picusbf4aed22017-05-29 08:19:19 +0000516
Diana Picus8fd16012017-06-15 09:42:02 +0000517 if (!SplitRegs.empty()) {
Diana Picusbf4aed22017-05-29 08:19:19 +0000518 // We have split the value and allocated each individual piece, now build
519 // it up again.
Diana Picus8fd16012017-06-15 09:42:02 +0000520 MIRBuilder.buildMerge(OrigRet.Reg, SplitRegs);
Diana Picusbf4aed22017-05-29 08:19:19 +0000521 }
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000522 }
523
Diana Picus1ffca2a2017-02-28 14:17:53 +0000524 // We now know the size of the stack - update the ADJCALLSTACKDOWN
525 // accordingly.
Serge Pavlovd526b132017-05-09 13:35:13 +0000526 CallSeqStart.addImm(ArgHandler.StackSize).addImm(0).add(predOps(ARMCC::AL));
Diana Picus1ffca2a2017-02-28 14:17:53 +0000527
Diana Picus613b6562017-02-21 11:33:59 +0000528 MIRBuilder.buildInstr(ARM::ADJCALLSTACKUP)
Diana Picus1ffca2a2017-02-28 14:17:53 +0000529 .addImm(ArgHandler.StackSize)
Diana Picus613b6562017-02-21 11:33:59 +0000530 .addImm(0)
531 .add(predOps(ARMCC::AL));
532
533 return true;
534}