blob: 284b67fd59b6bcd68f80f010de790561b5017129 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//=== ARMCallingConv.h - ARM Custom Calling Convention Routines -*- C++ -*-===//
Eric Christopher1c069172010-09-10 22:42:06 +00002//
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// This file contains the custom routines for the ARM Calling Convention that
11// aren't done by tablegen.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_TARGET_ARM_ARMCALLINGCONV_H
16#define LLVM_LIB_TARGET_ARM_ARMCALLINGCONV_H
Eric Christopher1c069172010-09-10 22:42:06 +000017
Craig Topper188ed9d2012-03-17 07:33:42 +000018#include "ARM.h"
Eric Christopher1c069172010-09-10 22:42:06 +000019#include "ARMBaseInstrInfo.h"
Eric Christopher1c069172010-09-10 22:42:06 +000020#include "ARMSubtarget.h"
Craig Topper188ed9d2012-03-17 07:33:42 +000021#include "llvm/CodeGen/CallingConvLower.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000022#include "llvm/CodeGen/TargetInstrInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/CallingConv.h"
Eric Christopher1c069172010-09-10 22:42:06 +000024
25namespace llvm {
26
27// APCS f64 is in register pairs, possibly split to stack
Duncan Sands71049f72010-11-04 10:49:57 +000028static bool f64AssignAPCS(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Eric Christopher1c069172010-09-10 22:42:06 +000029 CCValAssign::LocInfo &LocInfo,
30 CCState &State, bool CanFail) {
Craig Topper840beec2014-04-04 05:16:06 +000031 static const MCPhysReg RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
Eric Christopher1c069172010-09-10 22:42:06 +000032
33 // Try to get the first register.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000034 if (unsigned Reg = State.AllocateReg(RegList))
Eric Christopher1c069172010-09-10 22:42:06 +000035 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
36 else {
37 // For the 2nd half of a v2f64, do not fail.
38 if (CanFail)
39 return false;
40
41 // Put the whole thing on the stack.
42 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
43 State.AllocateStack(8, 4),
44 LocVT, LocInfo));
45 return true;
46 }
47
48 // Try to get the second register.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000049 if (unsigned Reg = State.AllocateReg(RegList))
Eric Christopher1c069172010-09-10 22:42:06 +000050 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
51 else
52 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
53 State.AllocateStack(4, 4),
54 LocVT, LocInfo));
55 return true;
56}
57
Duncan Sands71049f72010-11-04 10:49:57 +000058static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Eric Christopher1c069172010-09-10 22:42:06 +000059 CCValAssign::LocInfo &LocInfo,
60 ISD::ArgFlagsTy &ArgFlags,
61 CCState &State) {
62 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
63 return false;
64 if (LocVT == MVT::v2f64 &&
65 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
66 return false;
67 return true; // we handled it
68}
69
70// AAPCS f64 is in aligned register pairs
Duncan Sands71049f72010-11-04 10:49:57 +000071static bool f64AssignAAPCS(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Eric Christopher1c069172010-09-10 22:42:06 +000072 CCValAssign::LocInfo &LocInfo,
73 CCState &State, bool CanFail) {
Craig Topper840beec2014-04-04 05:16:06 +000074 static const MCPhysReg HiRegList[] = { ARM::R0, ARM::R2 };
75 static const MCPhysReg LoRegList[] = { ARM::R1, ARM::R3 };
76 static const MCPhysReg ShadowRegList[] = { ARM::R0, ARM::R1 };
77 static const MCPhysReg GPRArgRegs[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
Eric Christopher1c069172010-09-10 22:42:06 +000078
Tim Northover3b6b7ca2015-02-21 02:11:17 +000079 unsigned Reg = State.AllocateReg(HiRegList, ShadowRegList);
Eric Christopher1c069172010-09-10 22:42:06 +000080 if (Reg == 0) {
Stepan Dyatkovskiyf80f9512013-04-22 13:06:52 +000081
82 // If we had R3 unallocated only, now we still must to waste it.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000083 Reg = State.AllocateReg(GPRArgRegs);
Stepan Dyatkovskiyf80f9512013-04-22 13:06:52 +000084 assert((!Reg || Reg == ARM::R3) && "Wrong GPRs usage for f64");
85
Eric Christopher1c069172010-09-10 22:42:06 +000086 // For the 2nd half of a v2f64, do not just fail.
87 if (CanFail)
88 return false;
89
90 // Put the whole thing on the stack.
91 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
92 State.AllocateStack(8, 8),
93 LocVT, LocInfo));
94 return true;
95 }
96
97 unsigned i;
98 for (i = 0; i < 2; ++i)
99 if (HiRegList[i] == Reg)
100 break;
101
102 unsigned T = State.AllocateReg(LoRegList[i]);
103 (void)T;
104 assert(T == LoRegList[i] && "Could not allocate register");
105
106 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
107 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
108 LocVT, LocInfo));
109 return true;
110}
111
Duncan Sands71049f72010-11-04 10:49:57 +0000112static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Eric Christopher1c069172010-09-10 22:42:06 +0000113 CCValAssign::LocInfo &LocInfo,
114 ISD::ArgFlagsTy &ArgFlags,
115 CCState &State) {
116 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
117 return false;
118 if (LocVT == MVT::v2f64 &&
119 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
120 return false;
121 return true; // we handled it
122}
123
Duncan Sands71049f72010-11-04 10:49:57 +0000124static bool f64RetAssign(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Eric Christopher1c069172010-09-10 22:42:06 +0000125 CCValAssign::LocInfo &LocInfo, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +0000126 static const MCPhysReg HiRegList[] = { ARM::R0, ARM::R2 };
127 static const MCPhysReg LoRegList[] = { ARM::R1, ARM::R3 };
Eric Christopher1c069172010-09-10 22:42:06 +0000128
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000129 unsigned Reg = State.AllocateReg(HiRegList, LoRegList);
Eric Christopher1c069172010-09-10 22:42:06 +0000130 if (Reg == 0)
131 return false; // we didn't handle it
132
133 unsigned i;
134 for (i = 0; i < 2; ++i)
135 if (HiRegList[i] == Reg)
136 break;
137
138 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
139 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
140 LocVT, LocInfo));
141 return true;
142}
143
Duncan Sands71049f72010-11-04 10:49:57 +0000144static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Eric Christopher1c069172010-09-10 22:42:06 +0000145 CCValAssign::LocInfo &LocInfo,
146 ISD::ArgFlagsTy &ArgFlags,
147 CCState &State) {
148 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
149 return false;
150 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
151 return false;
152 return true; // we handled it
153}
154
Duncan Sands71049f72010-11-04 10:49:57 +0000155static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Eric Christopher1c069172010-09-10 22:42:06 +0000156 CCValAssign::LocInfo &LocInfo,
157 ISD::ArgFlagsTy &ArgFlags,
158 CCState &State) {
159 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
160 State);
161}
162
Craig Toppere5e035a32015-12-05 07:13:35 +0000163static const MCPhysReg RRegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
Tim Northovere95c5b32015-02-24 17:22:34 +0000164
Craig Toppere5e035a32015-12-05 07:13:35 +0000165static const MCPhysReg SRegList[] = { ARM::S0, ARM::S1, ARM::S2, ARM::S3,
166 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
167 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
168 ARM::S12, ARM::S13, ARM::S14, ARM::S15 };
169static const MCPhysReg DRegList[] = { ARM::D0, ARM::D1, ARM::D2, ARM::D3,
170 ARM::D4, ARM::D5, ARM::D6, ARM::D7 };
171static const MCPhysReg QRegList[] = { ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3 };
Oliver Stannardc24f2172014-05-09 14:01:47 +0000172
Tim Northovere95c5b32015-02-24 17:22:34 +0000173
Oliver Stannardc24f2172014-05-09 14:01:47 +0000174// Allocate part of an AAPCS HFA or HVA. We assume that each member of the HA
175// has InConsecutiveRegs set, and that the last member also has
176// InConsecutiveRegsLast set. We must process all members of the HA before
177// we can allocate it, as we need to know the total number of registers that
178// will be needed in order to (attempt to) allocate a contiguous block.
Tim Northovere95c5b32015-02-24 17:22:34 +0000179static bool CC_ARM_AAPCS_Custom_Aggregate(unsigned &ValNo, MVT &ValVT,
180 MVT &LocVT,
181 CCValAssign::LocInfo &LocInfo,
182 ISD::ArgFlagsTy &ArgFlags,
183 CCState &State) {
184 SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();
Oliver Stannard51b1d462014-08-21 12:50:31 +0000185
Oliver Stannardc24f2172014-05-09 14:01:47 +0000186 // AAPCS HFAs must have 1-4 elements, all of the same type
Tim Northovere95c5b32015-02-24 17:22:34 +0000187 if (PendingMembers.size() > 0)
188 assert(PendingMembers[0].getLocVT() == LocVT);
Oliver Stannardc24f2172014-05-09 14:01:47 +0000189
190 // Add the argument to the list to be allocated once we know the size of the
Tim Northovere95c5b32015-02-24 17:22:34 +0000191 // aggregate. Store the type's required alignmnent as extra info for later: in
192 // the [N x i64] case all trace has been removed by the time we actually get
193 // to do allocation.
194 PendingMembers.push_back(CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo,
195 ArgFlags.getOrigAlign()));
Oliver Stannardc24f2172014-05-09 14:01:47 +0000196
Tim Northovere95c5b32015-02-24 17:22:34 +0000197 if (!ArgFlags.isInConsecutiveRegsLast())
198 return true;
Oliver Stannardc24f2172014-05-09 14:01:47 +0000199
Tim Northovere95c5b32015-02-24 17:22:34 +0000200 // Try to allocate a contiguous block of registers, each of the correct
201 // size to hold one member.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000202 auto &DL = State.getMachineFunction().getDataLayout();
203 unsigned StackAlign = DL.getStackAlignment();
204 unsigned Align = std::min(PendingMembers[0].getExtraInfo(), StackAlign);
Oliver Stannardc24f2172014-05-09 14:01:47 +0000205
Craig Toppere5e035a32015-12-05 07:13:35 +0000206 ArrayRef<MCPhysReg> RegList;
Tim Northovere95c5b32015-02-24 17:22:34 +0000207 switch (LocVT.SimpleTy) {
208 case MVT::i32: {
209 RegList = RRegList;
210 unsigned RegIdx = State.getFirstUnallocated(RegList);
Oliver Stannardc24f2172014-05-09 14:01:47 +0000211
Tim Northovere95c5b32015-02-24 17:22:34 +0000212 // First consume all registers that would give an unaligned object. Whether
213 // we go on stack or in regs, no-one will be using them in future.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000214 unsigned RegAlign = alignTo(Align, 4) / 4;
Tim Northovere95c5b32015-02-24 17:22:34 +0000215 while (RegIdx % RegAlign != 0 && RegIdx < RegList.size())
216 State.AllocateReg(RegList[RegIdx++]);
Oliver Stannardc24f2172014-05-09 14:01:47 +0000217
Tim Northovere95c5b32015-02-24 17:22:34 +0000218 break;
219 }
220 case MVT::f32:
221 RegList = SRegList;
222 break;
223 case MVT::f64:
224 RegList = DRegList;
225 break;
226 case MVT::v2f64:
227 RegList = QRegList;
228 break;
229 default:
230 llvm_unreachable("Unexpected member type for block aggregate");
231 break;
Oliver Stannardc24f2172014-05-09 14:01:47 +0000232 }
233
Tim Northovere95c5b32015-02-24 17:22:34 +0000234 unsigned RegResult = State.AllocateRegBlock(RegList, PendingMembers.size());
235 if (RegResult) {
236 for (SmallVectorImpl<CCValAssign>::iterator It = PendingMembers.begin();
237 It != PendingMembers.end(); ++It) {
238 It->convertToReg(RegResult);
239 State.addLoc(*It);
240 ++RegResult;
241 }
242 PendingMembers.clear();
243 return true;
244 }
245
246 // Register allocation failed, we'll be needing the stack
247 unsigned Size = LocVT.getSizeInBits() / 8;
248 if (LocVT == MVT::i32 && State.getNextStackOffset() == 0) {
249 // If nothing else has used the stack until this point, a non-HFA aggregate
250 // can be split between regs and stack.
251 unsigned RegIdx = State.getFirstUnallocated(RegList);
252 for (auto &It : PendingMembers) {
253 if (RegIdx >= RegList.size())
254 It.convertToMem(State.AllocateStack(Size, Size));
255 else
256 It.convertToReg(State.AllocateReg(RegList[RegIdx++]));
257
258 State.addLoc(It);
259 }
260 PendingMembers.clear();
261 return true;
262 } else if (LocVT != MVT::i32)
263 RegList = SRegList;
264
265 // Mark all regs as unavailable (AAPCS rule C.2.vfp for VFP, C.6 for core)
266 for (auto Reg : RegList)
267 State.AllocateReg(Reg);
268
269 for (auto &It : PendingMembers) {
270 It.convertToMem(State.AllocateStack(Size, Align));
271 State.addLoc(It);
272
273 // After the first item has been allocated, the rest are packed as tightly
274 // as possible. (E.g. an incoming i64 would have starting Align of 8, but
275 // we'll be allocating a bunch of i32 slots).
276 Align = Size;
277 }
278
279 // All pending members have now been allocated
280 PendingMembers.clear();
281
282 // This will be allocated by the last member of the aggregate
Oliver Stannardc24f2172014-05-09 14:01:47 +0000283 return true;
284}
285
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000286} // End llvm namespace
Eric Christopher1c069172010-09-10 22:42:06 +0000287
Eric Christophercc136782010-09-10 22:46:03 +0000288#endif