blob: f4f3b261bf57d6eb05a410115e69ede9157b6ee0 [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 Picus1437f6d2016-12-19 11:55:41 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000025
26using namespace llvm;
27
28#ifndef LLVM_BUILD_GLOBAL_ISEL
29#error "This shouldn't be built without GISel"
30#endif
31
32ARMCallLowering::ARMCallLowering(const ARMTargetLowering &TLI)
33 : CallLowering(&TLI) {}
34
Benjamin Kramer061f4a52017-01-13 14:39:03 +000035static bool isSupportedType(const DataLayout &DL, const ARMTargetLowering &TLI,
Diana Picus812caee2016-12-16 12:54:46 +000036 Type *T) {
Diana Picus0c11c7b2017-02-02 14:00:54 +000037 EVT VT = TLI.getValueType(DL, T, true);
Diana Picus7232af32017-02-09 13:09:59 +000038 if (!VT.isSimple() || VT.isVector())
Diana Picus97ae95c2016-12-19 14:08:02 +000039 return false;
40
41 unsigned VTSize = VT.getSimpleVT().getSizeInBits();
Diana Picusca6a8902017-02-16 07:53:07 +000042
43 if (VTSize == 64)
44 // FIXME: Support i64 too
45 return VT.isFloatingPoint();
46
Diana Picusd83df5d2017-01-25 08:47:40 +000047 return VTSize == 1 || VTSize == 8 || VTSize == 16 || VTSize == 32;
Diana Picus812caee2016-12-16 12:54:46 +000048}
49
50namespace {
Diana Picusa6067132017-02-23 13:25:43 +000051/// Helper class for values going out through an ABI boundary (used for handling
52/// function return values and call parameters).
53struct OutgoingValueHandler : public CallLowering::ValueHandler {
54 OutgoingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
55 MachineInstrBuilder &MIB, CCAssignFn *AssignFn)
56 : ValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {}
Diana Picus812caee2016-12-16 12:54:46 +000057
58 unsigned getStackAddress(uint64_t Size, int64_t Offset,
59 MachinePointerInfo &MPO) override {
60 llvm_unreachable("Don't know how to get a stack address yet");
61 }
62
63 void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
64 CCValAssign &VA) override {
65 assert(VA.isRegLoc() && "Value shouldn't be assigned to reg");
66 assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?");
67
Diana Picusca6a8902017-02-16 07:53:07 +000068 assert(VA.getValVT().getSizeInBits() <= 64 && "Unsupported value size");
69 assert(VA.getLocVT().getSizeInBits() <= 64 && "Unsupported location size");
Diana Picus812caee2016-12-16 12:54:46 +000070
Diana Picus8b6c6be2017-01-25 08:10:40 +000071 unsigned ExtReg = extendRegister(ValVReg, VA);
72 MIRBuilder.buildCopy(PhysReg, ExtReg);
Diana Picus812caee2016-12-16 12:54:46 +000073 MIB.addUse(PhysReg, RegState::Implicit);
74 }
75
76 void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size,
77 MachinePointerInfo &MPO, CCValAssign &VA) override {
78 llvm_unreachable("Don't know how to assign a value to an address yet");
79 }
80
Diana Picusca6a8902017-02-16 07:53:07 +000081 unsigned assignCustomValue(const CallLowering::ArgInfo &Arg,
82 ArrayRef<CCValAssign> VAs) override {
83 CCValAssign VA = VAs[0];
84 assert(VA.needsCustom() && "Value doesn't need custom handling");
85 assert(VA.getValVT() == MVT::f64 && "Unsupported type");
86
87 CCValAssign NextVA = VAs[1];
88 assert(NextVA.needsCustom() && "Value doesn't need custom handling");
89 assert(NextVA.getValVT() == MVT::f64 && "Unsupported type");
90
91 assert(VA.getValNo() == NextVA.getValNo() &&
92 "Values belong to different arguments");
93
94 assert(VA.isRegLoc() && "Value should be in reg");
95 assert(NextVA.isRegLoc() && "Value should be in reg");
96
97 unsigned NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)),
98 MRI.createGenericVirtualRegister(LLT::scalar(32))};
99
100 MIRBuilder.buildExtract(NewRegs, {0, 32}, Arg.Reg);
101
102 bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle();
103 if (!IsLittle)
104 std::swap(NewRegs[0], NewRegs[1]);
105
106 assignValueToReg(NewRegs[0], VA.getLocReg(), VA);
107 assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA);
108
109 return 1;
110 }
111
Diana Picus812caee2016-12-16 12:54:46 +0000112 MachineInstrBuilder &MIB;
113};
114} // End anonymous namespace.
115
Diana Picus32cd9b42017-02-02 14:01:00 +0000116void ARMCallLowering::splitToValueTypes(const ArgInfo &OrigArg,
117 SmallVectorImpl<ArgInfo> &SplitArgs,
118 const DataLayout &DL,
119 MachineRegisterInfo &MRI) const {
120 const ARMTargetLowering &TLI = *getTLI<ARMTargetLowering>();
121 LLVMContext &Ctx = OrigArg.Ty->getContext();
122
123 SmallVector<EVT, 4> SplitVTs;
124 SmallVector<uint64_t, 4> Offsets;
125 ComputeValueVTs(TLI, DL, OrigArg.Ty, SplitVTs, &Offsets, 0);
126
127 assert(SplitVTs.size() == 1 && "Unsupported type");
128
129 // Even if there is no splitting to do, we still want to replace the original
130 // type (e.g. pointer type -> integer).
131 SplitArgs.emplace_back(OrigArg.Reg, SplitVTs[0].getTypeForEVT(Ctx),
132 OrigArg.Flags, OrigArg.IsFixed);
133}
134
Diana Picus812caee2016-12-16 12:54:46 +0000135/// Lower the return value for the already existing \p Ret. This assumes that
136/// \p MIRBuilder's insertion point is correct.
137bool ARMCallLowering::lowerReturnVal(MachineIRBuilder &MIRBuilder,
138 const Value *Val, unsigned VReg,
139 MachineInstrBuilder &Ret) const {
140 if (!Val)
141 // Nothing to do here.
142 return true;
143
144 auto &MF = MIRBuilder.getMF();
145 const auto &F = *MF.getFunction();
146
147 auto DL = MF.getDataLayout();
148 auto &TLI = *getTLI<ARMTargetLowering>();
149 if (!isSupportedType(DL, TLI, Val->getType()))
Diana Picus22274932016-11-11 08:27:37 +0000150 return false;
151
Diana Picus32cd9b42017-02-02 14:01:00 +0000152 SmallVector<ArgInfo, 4> SplitVTs;
153 ArgInfo RetInfo(VReg, Val->getType());
154 setArgFlags(RetInfo, AttributeSet::ReturnIndex, DL, F);
155 splitToValueTypes(RetInfo, SplitVTs, DL, MF.getRegInfo());
156
Diana Picus812caee2016-12-16 12:54:46 +0000157 CCAssignFn *AssignFn =
158 TLI.CCAssignFnForReturn(F.getCallingConv(), F.isVarArg());
Diana Picus22274932016-11-11 08:27:37 +0000159
Diana Picusa6067132017-02-23 13:25:43 +0000160 OutgoingValueHandler RetHandler(MIRBuilder, MF.getRegInfo(), Ret, AssignFn);
Diana Picus32cd9b42017-02-02 14:01:00 +0000161 return handleAssignments(MIRBuilder, SplitVTs, RetHandler);
Diana Picus812caee2016-12-16 12:54:46 +0000162}
163
164bool ARMCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
165 const Value *Val, unsigned VReg) const {
166 assert(!Val == !VReg && "Return value without a vreg");
167
Diana Picus4f8c3e12017-01-13 09:37:56 +0000168 auto Ret = MIRBuilder.buildInstrNoInsert(ARM::BX_RET).add(predOps(ARMCC::AL));
Diana Picus812caee2016-12-16 12:54:46 +0000169
170 if (!lowerReturnVal(MIRBuilder, Val, VReg, Ret))
171 return false;
172
173 MIRBuilder.insertInstr(Ret);
Diana Picus22274932016-11-11 08:27:37 +0000174 return true;
175}
176
Diana Picus812caee2016-12-16 12:54:46 +0000177namespace {
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000178/// Helper class for values coming in through an ABI boundary (used for handling
179/// formal arguments and call return values).
180struct IncomingValueHandler : public CallLowering::ValueHandler {
181 IncomingValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
182 CCAssignFn AssignFn)
Tim Northoverd9433542017-01-17 22:30:10 +0000183 : ValueHandler(MIRBuilder, MRI, AssignFn) {}
Diana Picus812caee2016-12-16 12:54:46 +0000184
185 unsigned getStackAddress(uint64_t Size, int64_t Offset,
186 MachinePointerInfo &MPO) override {
Diana Picusca6a8902017-02-16 07:53:07 +0000187 assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) &&
188 "Unsupported size");
Diana Picus1437f6d2016-12-19 11:55:41 +0000189
190 auto &MFI = MIRBuilder.getMF().getFrameInfo();
191
192 int FI = MFI.CreateFixedObject(Size, Offset, true);
193 MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
194
195 unsigned AddrReg =
196 MRI.createGenericVirtualRegister(LLT::pointer(MPO.getAddrSpace(), 32));
197 MIRBuilder.buildFrameIndex(AddrReg, FI);
198
199 return AddrReg;
200 }
201
202 void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size,
203 MachinePointerInfo &MPO, CCValAssign &VA) override {
Diana Picusca6a8902017-02-16 07:53:07 +0000204 assert((Size == 1 || Size == 2 || Size == 4 || Size == 8) &&
205 "Unsupported size");
Diana Picus278c7222017-01-26 09:20:47 +0000206
207 if (VA.getLocInfo() == CCValAssign::SExt ||
208 VA.getLocInfo() == CCValAssign::ZExt) {
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000209 // If the value is zero- or sign-extended, its size becomes 4 bytes, so
210 // that's what we should load.
Diana Picus278c7222017-01-26 09:20:47 +0000211 Size = 4;
212 assert(MRI.getType(ValVReg).isScalar() && "Only scalars supported atm");
213 MRI.setType(ValVReg, LLT::scalar(32));
214 }
Diana Picus1437f6d2016-12-19 11:55:41 +0000215
216 auto MMO = MIRBuilder.getMF().getMachineMemOperand(
217 MPO, MachineMemOperand::MOLoad, Size, /* Alignment */ 0);
218 MIRBuilder.buildLoad(ValVReg, Addr, *MMO);
Diana Picus812caee2016-12-16 12:54:46 +0000219 }
220
221 void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
222 CCValAssign &VA) override {
223 assert(VA.isRegLoc() && "Value shouldn't be assigned to reg");
224 assert(VA.getLocReg() == PhysReg && "Assigning to the wrong reg?");
225
Diana Picusca6a8902017-02-16 07:53:07 +0000226 assert(VA.getValVT().getSizeInBits() <= 64 && "Unsupported value size");
227 assert(VA.getLocVT().getSizeInBits() <= 64 && "Unsupported location size");
Diana Picus812caee2016-12-16 12:54:46 +0000228
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000229 // The necesary extensions are handled on the other side of the ABI
230 // boundary.
231 markPhysRegUsed(PhysReg);
Diana Picus812caee2016-12-16 12:54:46 +0000232 MIRBuilder.buildCopy(ValVReg, PhysReg);
233 }
Diana Picusca6a8902017-02-16 07:53:07 +0000234
Diana Picusa6067132017-02-23 13:25:43 +0000235 unsigned assignCustomValue(const ARMCallLowering::ArgInfo &Arg,
Diana Picusca6a8902017-02-16 07:53:07 +0000236 ArrayRef<CCValAssign> VAs) override {
237 CCValAssign VA = VAs[0];
238 assert(VA.needsCustom() && "Value doesn't need custom handling");
239 assert(VA.getValVT() == MVT::f64 && "Unsupported type");
240
241 CCValAssign NextVA = VAs[1];
242 assert(NextVA.needsCustom() && "Value doesn't need custom handling");
243 assert(NextVA.getValVT() == MVT::f64 && "Unsupported type");
244
245 assert(VA.getValNo() == NextVA.getValNo() &&
246 "Values belong to different arguments");
247
248 assert(VA.isRegLoc() && "Value should be in reg");
249 assert(NextVA.isRegLoc() && "Value should be in reg");
250
251 unsigned NewRegs[] = {MRI.createGenericVirtualRegister(LLT::scalar(32)),
252 MRI.createGenericVirtualRegister(LLT::scalar(32))};
253
254 assignValueToReg(NewRegs[0], VA.getLocReg(), VA);
255 assignValueToReg(NewRegs[1], NextVA.getLocReg(), NextVA);
256
257 bool IsLittle = MIRBuilder.getMF().getSubtarget<ARMSubtarget>().isLittle();
258 if (!IsLittle)
259 std::swap(NewRegs[0], NewRegs[1]);
260
261 MIRBuilder.buildSequence(Arg.Reg, NewRegs, {0, 32});
262
263 return 1;
264 }
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000265
266 /// Marking a physical register as used is different between formal
267 /// parameters, where it's a basic block live-in, and call returns, where it's
268 /// an implicit-def of the call instruction.
269 virtual void markPhysRegUsed(unsigned PhysReg) = 0;
270};
271
272struct FormalArgHandler : public IncomingValueHandler {
273 FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
274 CCAssignFn AssignFn)
275 : IncomingValueHandler(MIRBuilder, MRI, AssignFn) {}
276
277 void markPhysRegUsed(unsigned PhysReg) override {
278 MIRBuilder.getMBB().addLiveIn(PhysReg);
279 }
Diana Picus812caee2016-12-16 12:54:46 +0000280};
281} // End anonymous namespace
282
Diana Picus22274932016-11-11 08:27:37 +0000283bool ARMCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
284 const Function &F,
285 ArrayRef<unsigned> VRegs) const {
Diana Picus812caee2016-12-16 12:54:46 +0000286 // Quick exit if there aren't any args
287 if (F.arg_empty())
288 return true;
289
Diana Picus812caee2016-12-16 12:54:46 +0000290 if (F.isVarArg())
291 return false;
292
Diana Picus32cd9b42017-02-02 14:01:00 +0000293 auto &MF = MIRBuilder.getMF();
294 auto DL = MF.getDataLayout();
Diana Picus812caee2016-12-16 12:54:46 +0000295 auto &TLI = *getTLI<ARMTargetLowering>();
296
Diana Picus7232af32017-02-09 13:09:59 +0000297 auto Subtarget = TLI.getSubtarget();
298
299 if (Subtarget->isThumb())
300 return false;
301
302 // FIXME: Support soft float (when we're ready to generate libcalls)
303 if (Subtarget->useSoftFloat() || !Subtarget->hasVFP2())
Diana Picus1d8eaf42017-01-25 07:08:53 +0000304 return false;
305
Diana Picus812caee2016-12-16 12:54:46 +0000306 auto &Args = F.getArgumentList();
Diana Picus278c7222017-01-26 09:20:47 +0000307 for (auto &Arg : Args)
Diana Picus812caee2016-12-16 12:54:46 +0000308 if (!isSupportedType(DL, TLI, Arg.getType()))
309 return false;
310
311 CCAssignFn *AssignFn =
312 TLI.CCAssignFnForCall(F.getCallingConv(), F.isVarArg());
313
314 SmallVector<ArgInfo, 8> ArgInfos;
315 unsigned Idx = 0;
316 for (auto &Arg : Args) {
317 ArgInfo AInfo(VRegs[Idx], Arg.getType());
318 setArgFlags(AInfo, Idx + 1, DL, F);
Diana Picus32cd9b42017-02-02 14:01:00 +0000319 splitToValueTypes(AInfo, ArgInfos, DL, MF.getRegInfo());
Diana Picus812caee2016-12-16 12:54:46 +0000320 Idx++;
321 }
322
Tim Northoverd9433542017-01-17 22:30:10 +0000323 FormalArgHandler ArgHandler(MIRBuilder, MIRBuilder.getMF().getRegInfo(),
324 AssignFn);
325 return handleAssignments(MIRBuilder, ArgInfos, ArgHandler);
Diana Picus22274932016-11-11 08:27:37 +0000326}
Diana Picus613b6562017-02-21 11:33:59 +0000327
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000328namespace {
329struct CallReturnHandler : public IncomingValueHandler {
330 CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
331 MachineInstrBuilder MIB, CCAssignFn *AssignFn)
332 : IncomingValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {}
333
334 void markPhysRegUsed(unsigned PhysReg) override {
335 MIB.addDef(PhysReg, RegState::Implicit);
336 }
337
338 MachineInstrBuilder MIB;
339};
340} // End anonymous namespace.
341
Diana Picus613b6562017-02-21 11:33:59 +0000342bool ARMCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
343 const MachineOperand &Callee,
344 const ArgInfo &OrigRet,
345 ArrayRef<ArgInfo> OrigArgs) const {
Diana Picusa6067132017-02-23 13:25:43 +0000346 MachineFunction &MF = MIRBuilder.getMF();
347 const auto &TLI = *getTLI<ARMTargetLowering>();
348 const auto &DL = MF.getDataLayout();
Diana Picus613b6562017-02-21 11:33:59 +0000349 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Diana Picusa6067132017-02-23 13:25:43 +0000350 MachineRegisterInfo &MRI = MF.getRegInfo();
Diana Picus613b6562017-02-21 11:33:59 +0000351
352 if (MF.getSubtarget<ARMSubtarget>().genLongCalls())
353 return false;
354
Diana Picus613b6562017-02-21 11:33:59 +0000355 MIRBuilder.buildInstr(ARM::ADJCALLSTACKDOWN)
356 .addImm(0)
357 .add(predOps(ARMCC::AL));
358
Diana Picusa6067132017-02-23 13:25:43 +0000359 // FIXME: This is the calling convention of the caller - we should use the
360 // calling convention of the callee instead.
361 auto CallConv = MF.getFunction()->getCallingConv();
362
363 // Create the call instruction so we can add the implicit uses of arg
364 // registers, but don't insert it yet.
365 auto MIB = MIRBuilder.buildInstrNoInsert(ARM::BLX).add(Callee).addRegMask(
366 TRI->getCallPreservedMask(MF, CallConv));
367
368 SmallVector<ArgInfo, 8> ArgInfos;
369 for (auto Arg : OrigArgs) {
370 if (!isSupportedType(DL, TLI, Arg.Ty))
371 return false;
372
373 if (!Arg.IsFixed)
374 return false;
375
376 splitToValueTypes(Arg, ArgInfos, DL, MRI);
377 }
378
379 auto ArgAssignFn = TLI.CCAssignFnForCall(CallConv, /*IsVarArg=*/false);
380 OutgoingValueHandler ArgHandler(MIRBuilder, MRI, MIB, ArgAssignFn);
381 if (!handleAssignments(MIRBuilder, ArgInfos, ArgHandler))
382 return false;
383
384 // Now we can add the actual call instruction to the correct basic block.
385 MIRBuilder.insertInstr(MIB);
Diana Picus613b6562017-02-21 11:33:59 +0000386
Diana Picusa8cb0cd2017-02-23 14:18:41 +0000387 if (!OrigRet.Ty->isVoidTy()) {
388 if (!isSupportedType(DL, TLI, OrigRet.Ty))
389 return false;
390
391 ArgInfos.clear();
392 splitToValueTypes(OrigRet, ArgInfos, DL, MRI);
393
394 auto RetAssignFn = TLI.CCAssignFnForReturn(CallConv, /*IsVarArg=*/false);
395 CallReturnHandler RetHandler(MIRBuilder, MRI, MIB, RetAssignFn);
396 if (!handleAssignments(MIRBuilder, ArgInfos, RetHandler))
397 return false;
398 }
399
Diana Picus613b6562017-02-21 11:33:59 +0000400 MIRBuilder.buildInstr(ARM::ADJCALLSTACKUP)
401 .addImm(0)
402 .addImm(0)
403 .add(predOps(ARMCC::AL));
404
405 return true;
406}