]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/AArch64/AArch64CallingConvention.td
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r308421, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / AArch64 / AArch64CallingConvention.td
1 //=- AArch64CallingConv.td - Calling Conventions for AArch64 -*- tablegen -*-=//
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 // This describes the calling conventions for AArch64 architecture.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /// CCIfAlign - Match of the original alignment of the arg
15 class CCIfAlign<string Align, CCAction A> :
16   CCIf<!strconcat("ArgFlags.getOrigAlign() == ", Align), A>;
17 /// CCIfBigEndian - Match only if we're in big endian mode.
18 class CCIfBigEndian<CCAction A> :
19   CCIf<"State.getMachineFunction().getDataLayout().isBigEndian()", A>;
20
21 //===----------------------------------------------------------------------===//
22 // ARM AAPCS64 Calling Convention
23 //===----------------------------------------------------------------------===//
24
25 def CC_AArch64_AAPCS : CallingConv<[
26   CCIfType<[iPTR], CCBitConvertToType<i64>>,
27   CCIfType<[v2f32], CCBitConvertToType<v2i32>>,
28   CCIfType<[v2f64, v4f32], CCBitConvertToType<v2i64>>,
29
30   // Big endian vectors must be passed as if they were 1-element vectors so that
31   // their lanes are in a consistent order.
32   CCIfBigEndian<CCIfType<[v2i32, v2f32, v4i16, v4f16, v8i8],
33                          CCBitConvertToType<f64>>>,
34   CCIfBigEndian<CCIfType<[v2i64, v2f64, v4i32, v4f32, v8i16, v8f16, v16i8],
35                          CCBitConvertToType<f128>>>,
36
37   // An SRet is passed in X8, not X0 like a normal pointer parameter.
38   CCIfSRet<CCIfType<[i64], CCAssignToRegWithShadow<[X8], [W8]>>>,
39
40   // Put ByVal arguments directly on the stack. Minimum size and alignment of a
41   // slot is 64-bit.
42   CCIfByVal<CCPassByVal<8, 8>>,
43
44   // The 'nest' parameter, if any, is passed in X18.
45   // Darwin uses X18 as the platform register and hence 'nest' isn't currently
46   // supported there.
47   CCIfNest<CCAssignToReg<[X18]>>,
48
49   // Pass SwiftSelf in a callee saved register.
50   CCIfSwiftSelf<CCIfType<[i64], CCAssignToRegWithShadow<[X20], [W20]>>>,
51
52   CCIfConsecutiveRegs<CCCustom<"CC_AArch64_Custom_Block">>,
53
54   // Handle i1, i8, i16, i32, i64, f32, f64 and v2f64 by passing in registers,
55   // up to eight each of GPR and FPR.
56   CCIfType<[i1, i8, i16], CCPromoteToType<i32>>,
57   CCIfType<[i32], CCAssignToRegWithShadow<[W0, W1, W2, W3, W4, W5, W6, W7],
58                                           [X0, X1, X2, X3, X4, X5, X6, X7]>>,
59   // i128 is split to two i64s, we can't fit half to register X7.
60   CCIfType<[i64], CCIfSplit<CCAssignToRegWithShadow<[X0, X2, X4, X6],
61                                                     [X0, X1, X3, X5]>>>,
62
63   // i128 is split to two i64s, and its stack alignment is 16 bytes.
64   CCIfType<[i64], CCIfSplit<CCAssignToStackWithShadow<8, 16, [X7]>>>,
65
66   CCIfType<[i64], CCAssignToRegWithShadow<[X0, X1, X2, X3, X4, X5, X6, X7],
67                                           [W0, W1, W2, W3, W4, W5, W6, W7]>>,
68   CCIfType<[f16], CCAssignToRegWithShadow<[H0, H1, H2, H3, H4, H5, H6, H7],
69                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
70   CCIfType<[f32], CCAssignToRegWithShadow<[S0, S1, S2, S3, S4, S5, S6, S7],
71                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
72   CCIfType<[f64], CCAssignToRegWithShadow<[D0, D1, D2, D3, D4, D5, D6, D7],
73                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
74   CCIfType<[v1i64, v2i32, v4i16, v8i8, v1f64, v2f32, v4f16],
75            CCAssignToRegWithShadow<[D0, D1, D2, D3, D4, D5, D6, D7],
76                                    [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
77   CCIfType<[f128, v2i64, v4i32, v8i16, v16i8, v4f32, v2f64, v8f16],
78            CCAssignToReg<[Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
79
80   // If more than will fit in registers, pass them on the stack instead.
81   CCIfType<[i1, i8, i16, f16], CCAssignToStack<8, 8>>,
82   CCIfType<[i32, f32], CCAssignToStack<8, 8>>,
83   CCIfType<[i64, f64, v1f64, v2f32, v1i64, v2i32, v4i16, v8i8, v4f16],
84            CCAssignToStack<8, 8>>,
85   CCIfType<[f128, v2i64, v4i32, v8i16, v16i8, v4f32, v2f64, v8f16],
86            CCAssignToStack<16, 16>>
87 ]>;
88
89 def RetCC_AArch64_AAPCS : CallingConv<[
90   CCIfType<[iPTR], CCBitConvertToType<i64>>,
91   CCIfType<[v2f32], CCBitConvertToType<v2i32>>,
92   CCIfType<[v2f64, v4f32], CCBitConvertToType<v2i64>>,
93
94   CCIfSwiftError<CCIfType<[i64], CCAssignToRegWithShadow<[X21], [W21]>>>,
95
96   // Big endian vectors must be passed as if they were 1-element vectors so that
97   // their lanes are in a consistent order.
98   CCIfBigEndian<CCIfType<[v2i32, v2f32, v4i16, v4f16, v8i8],
99                          CCBitConvertToType<f64>>>,
100   CCIfBigEndian<CCIfType<[v2i64, v2f64, v4i32, v4f32, v8i16, v8f16, v16i8],
101                          CCBitConvertToType<f128>>>,
102
103   CCIfType<[i1, i8, i16], CCPromoteToType<i32>>,
104   CCIfType<[i32], CCAssignToRegWithShadow<[W0, W1, W2, W3, W4, W5, W6, W7],
105                                           [X0, X1, X2, X3, X4, X5, X6, X7]>>,
106   CCIfType<[i64], CCAssignToRegWithShadow<[X0, X1, X2, X3, X4, X5, X6, X7],
107                                           [W0, W1, W2, W3, W4, W5, W6, W7]>>,
108   CCIfType<[f16], CCAssignToRegWithShadow<[H0, H1, H2, H3, H4, H5, H6, H7],
109                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
110   CCIfType<[f32], CCAssignToRegWithShadow<[S0, S1, S2, S3, S4, S5, S6, S7],
111                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
112   CCIfType<[f64], CCAssignToRegWithShadow<[D0, D1, D2, D3, D4, D5, D6, D7],
113                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
114   CCIfType<[v1i64, v2i32, v4i16, v8i8, v1f64, v2f32, v4f16],
115       CCAssignToRegWithShadow<[D0, D1, D2, D3, D4, D5, D6, D7],
116                               [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
117   CCIfType<[f128, v2i64, v4i32, v8i16, v16i8, v4f32, v2f64, v8f16],
118       CCAssignToReg<[Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>
119 ]>;
120
121 // Vararg functions on windows pass floats in integer registers
122 def CC_AArch64_Win64_VarArg : CallingConv<[
123   CCIfType<[f16, f32],    CCPromoteToType<f64>>,
124   CCIfType<[f64], CCBitConvertToType<i64>>,
125   CCDelegateTo<CC_AArch64_AAPCS>
126 ]>;
127
128
129 // Darwin uses a calling convention which differs in only two ways
130 // from the standard one at this level:
131 //     + i128s (i.e. split i64s) don't need even registers.
132 //     + Stack slots are sized as needed rather than being at least 64-bit.
133 def CC_AArch64_DarwinPCS : CallingConv<[
134   CCIfType<[iPTR], CCBitConvertToType<i64>>,
135   CCIfType<[v2f32], CCBitConvertToType<v2i32>>,
136   CCIfType<[v2f64, v4f32, f128], CCBitConvertToType<v2i64>>,
137
138   // An SRet is passed in X8, not X0 like a normal pointer parameter.
139   CCIfSRet<CCIfType<[i64], CCAssignToRegWithShadow<[X8], [W8]>>>,
140
141   // Put ByVal arguments directly on the stack. Minimum size and alignment of a
142   // slot is 64-bit.
143   CCIfByVal<CCPassByVal<8, 8>>,
144
145   // Pass SwiftSelf in a callee saved register.
146   CCIfSwiftSelf<CCIfType<[i64], CCAssignToRegWithShadow<[X20], [W20]>>>,
147
148   // A SwiftError is passed in X21.
149   CCIfSwiftError<CCIfType<[i64], CCAssignToRegWithShadow<[X21], [W21]>>>,
150
151   CCIfConsecutiveRegs<CCCustom<"CC_AArch64_Custom_Block">>,
152
153   // Handle i1, i8, i16, i32, i64, f32, f64 and v2f64 by passing in registers,
154   // up to eight each of GPR and FPR.
155   CCIfType<[i1, i8, i16], CCPromoteToType<i32>>,
156   CCIfType<[i32], CCAssignToRegWithShadow<[W0, W1, W2, W3, W4, W5, W6, W7],
157                                           [X0, X1, X2, X3, X4, X5, X6, X7]>>,
158   // i128 is split to two i64s, we can't fit half to register X7.
159   CCIfType<[i64],
160            CCIfSplit<CCAssignToRegWithShadow<[X0, X1, X2, X3, X4, X5, X6],
161                                              [W0, W1, W2, W3, W4, W5, W6]>>>,
162   // i128 is split to two i64s, and its stack alignment is 16 bytes.
163   CCIfType<[i64], CCIfSplit<CCAssignToStackWithShadow<8, 16, [X7]>>>,
164
165   CCIfType<[i64], CCAssignToRegWithShadow<[X0, X1, X2, X3, X4, X5, X6, X7],
166                                           [W0, W1, W2, W3, W4, W5, W6, W7]>>,
167   CCIfType<[f16], CCAssignToRegWithShadow<[H0, H1, H2, H3, H4, H5, H6, H7],
168                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
169   CCIfType<[f32], CCAssignToRegWithShadow<[S0, S1, S2, S3, S4, S5, S6, S7],
170                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
171   CCIfType<[f64], CCAssignToRegWithShadow<[D0, D1, D2, D3, D4, D5, D6, D7],
172                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
173   CCIfType<[v1i64, v2i32, v4i16, v8i8, v1f64, v2f32, v4f16],
174            CCAssignToRegWithShadow<[D0, D1, D2, D3, D4, D5, D6, D7],
175                                    [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
176   CCIfType<[v2i64, v4i32, v8i16, v16i8, v4f32, v2f64, v8f16],
177            CCAssignToReg<[Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
178
179   // If more than will fit in registers, pass them on the stack instead.
180   CCIf<"ValVT == MVT::i1 || ValVT == MVT::i8", CCAssignToStack<1, 1>>,
181   CCIf<"ValVT == MVT::i16 || ValVT == MVT::f16", CCAssignToStack<2, 2>>,
182   CCIfType<[i32, f32], CCAssignToStack<4, 4>>,
183   CCIfType<[i64, f64, v1f64, v2f32, v1i64, v2i32, v4i16, v8i8, v4f16],
184            CCAssignToStack<8, 8>>,
185   CCIfType<[v2i64, v4i32, v8i16, v16i8, v4f32, v2f64, v8f16],
186            CCAssignToStack<16, 16>>
187 ]>;
188
189 def CC_AArch64_DarwinPCS_VarArg : CallingConv<[
190   CCIfType<[iPTR], CCBitConvertToType<i64>>,
191   CCIfType<[v2f32], CCBitConvertToType<v2i32>>,
192   CCIfType<[v2f64, v4f32, f128], CCBitConvertToType<v2i64>>,
193
194   CCIfConsecutiveRegs<CCCustom<"CC_AArch64_Custom_Stack_Block">>,
195
196   // Handle all scalar types as either i64 or f64.
197   CCIfType<[i8, i16, i32], CCPromoteToType<i64>>,
198   CCIfType<[f16, f32],     CCPromoteToType<f64>>,
199
200   // Everything is on the stack.
201   // i128 is split to two i64s, and its stack alignment is 16 bytes.
202   CCIfType<[i64], CCIfSplit<CCAssignToStack<8, 16>>>,
203   CCIfType<[i64, f64, v1i64, v2i32, v4i16, v8i8, v1f64, v2f32, v4f16],
204            CCAssignToStack<8, 8>>,
205   CCIfType<[v2i64, v4i32, v8i16, v16i8, v4f32, v2f64, v8f16],
206            CCAssignToStack<16, 16>>
207 ]>;
208
209 // The WebKit_JS calling convention only passes the first argument (the callee)
210 // in register and the remaining arguments on stack. We allow 32bit stack slots,
211 // so that WebKit can write partial values in the stack and define the other
212 // 32bit quantity as undef.
213 def CC_AArch64_WebKit_JS : CallingConv<[
214   // Handle i1, i8, i16, i32, and i64 passing in register X0 (W0).
215   CCIfType<[i1, i8, i16], CCPromoteToType<i32>>,
216   CCIfType<[i32], CCAssignToRegWithShadow<[W0], [X0]>>,
217   CCIfType<[i64], CCAssignToRegWithShadow<[X0], [W0]>>,
218
219   // Pass the remaining arguments on the stack instead.
220   CCIfType<[i32, f32], CCAssignToStack<4, 4>>,
221   CCIfType<[i64, f64], CCAssignToStack<8, 8>>
222 ]>;
223
224 def RetCC_AArch64_WebKit_JS : CallingConv<[
225   CCIfType<[i32], CCAssignToRegWithShadow<[W0, W1, W2, W3, W4, W5, W6, W7],
226                                           [X0, X1, X2, X3, X4, X5, X6, X7]>>,
227   CCIfType<[i64], CCAssignToRegWithShadow<[X0, X1, X2, X3, X4, X5, X6, X7],
228                                           [W0, W1, W2, W3, W4, W5, W6, W7]>>,
229   CCIfType<[f32], CCAssignToRegWithShadow<[S0, S1, S2, S3, S4, S5, S6, S7],
230                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>,
231   CCIfType<[f64], CCAssignToRegWithShadow<[D0, D1, D2, D3, D4, D5, D6, D7],
232                                           [Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7]>>
233 ]>;
234
235 //===----------------------------------------------------------------------===//
236 // ARM64 Calling Convention for GHC
237 //===----------------------------------------------------------------------===//
238
239 // This calling convention is specific to the Glasgow Haskell Compiler.
240 // The only documentation is the GHC source code, specifically the C header
241 // file:
242 //
243 //     https://github.com/ghc/ghc/blob/master/includes/stg/MachRegs.h
244 //
245 // which defines the registers for the Spineless Tagless G-Machine (STG) that
246 // GHC uses to implement lazy evaluation. The generic STG machine has a set of
247 // registers which are mapped to appropriate set of architecture specific
248 // registers for each CPU architecture.
249 //
250 // The STG Machine is documented here:
251 //
252 //    https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/GeneratedCode
253 //
254 // The AArch64 register mapping is under the heading "The ARMv8/AArch64 ABI
255 // register mapping".
256
257 def CC_AArch64_GHC : CallingConv<[
258   CCIfType<[iPTR], CCBitConvertToType<i64>>,
259
260   // Handle all vector types as either f64 or v2f64.
261   CCIfType<[v1i64, v2i32, v4i16, v8i8, v2f32], CCBitConvertToType<f64>>,
262   CCIfType<[v2i64, v4i32, v8i16, v16i8, v4f32, f128], CCBitConvertToType<v2f64>>,
263
264   CCIfType<[v2f64], CCAssignToReg<[Q4, Q5]>>,
265   CCIfType<[f32], CCAssignToReg<[S8, S9, S10, S11]>>,
266   CCIfType<[f64], CCAssignToReg<[D12, D13, D14, D15]>>,
267
268   // Promote i8/i16/i32 arguments to i64.
269   CCIfType<[i8, i16, i32], CCPromoteToType<i64>>,
270
271   // Pass in STG registers: Base, Sp, Hp, R1, R2, R3, R4, R5, R6, SpLim
272   CCIfType<[i64], CCAssignToReg<[X19, X20, X21, X22, X23, X24, X25, X26, X27, X28]>>
273 ]>;
274
275 // FIXME: LR is only callee-saved in the sense that *we* preserve it and are
276 // presumably a callee to someone. External functions may not do so, but this
277 // is currently safe since BL has LR as an implicit-def and what happens after a
278 // tail call doesn't matter.
279 //
280 // It would be better to model its preservation semantics properly (create a
281 // vreg on entry, use it in RET & tail call generation; make that vreg def if we
282 // end up saving LR as part of a call frame). Watch this space...
283 def CSR_AArch64_AAPCS : CalleeSavedRegs<(add LR, FP, X19, X20, X21, X22,
284                                            X23, X24, X25, X26, X27, X28,
285                                            D8,  D9,  D10, D11,
286                                            D12, D13, D14, D15)>;
287
288 // Constructors and destructors return 'this' in the iOS 64-bit C++ ABI; since
289 // 'this' and the pointer return value are both passed in X0 in these cases,
290 // this can be partially modelled by treating X0 as a callee-saved register;
291 // only the resulting RegMask is used; the SaveList is ignored
292 //
293 // (For generic ARM 64-bit ABI code, clang will not generate constructors or
294 // destructors with 'this' returns, so this RegMask will not be used in that
295 // case)
296 def CSR_AArch64_AAPCS_ThisReturn : CalleeSavedRegs<(add CSR_AArch64_AAPCS, X0)>;
297
298 def CSR_AArch64_AAPCS_SwiftError
299     : CalleeSavedRegs<(sub CSR_AArch64_AAPCS, X21)>;
300
301 // The function used by Darwin to obtain the address of a thread-local variable
302 // guarantees more than a normal AAPCS function. x16 and x17 are used on the
303 // fast path for calculation, but other registers except X0 (argument/return)
304 // and LR (it is a call, after all) are preserved.
305 def CSR_AArch64_TLS_Darwin
306     : CalleeSavedRegs<(add (sub (sequence "X%u", 1, 28), X16, X17),
307                            FP,
308                            (sequence "Q%u", 0, 31))>;
309
310 // We can only handle a register pair with adjacent registers, the register pair
311 // should belong to the same class as well. Since the access function on the
312 // fast path calls a function that follows CSR_AArch64_TLS_Darwin,
313 // CSR_AArch64_CXX_TLS_Darwin should be a subset of CSR_AArch64_TLS_Darwin.
314 def CSR_AArch64_CXX_TLS_Darwin
315     : CalleeSavedRegs<(add CSR_AArch64_AAPCS,
316                            (sub (sequence "X%u", 1, 28), X15, X16, X17, X18),
317                            (sequence "D%u", 0, 31))>;
318
319 // CSRs that are handled by prologue, epilogue.
320 def CSR_AArch64_CXX_TLS_Darwin_PE
321     : CalleeSavedRegs<(add LR, FP)>;
322
323 // CSRs that are handled explicitly via copies.
324 def CSR_AArch64_CXX_TLS_Darwin_ViaCopy
325     : CalleeSavedRegs<(sub CSR_AArch64_CXX_TLS_Darwin, LR, FP)>;
326
327 // The ELF stub used for TLS-descriptor access saves every feasible
328 // register. Only X0 and LR are clobbered.
329 def CSR_AArch64_TLS_ELF
330     : CalleeSavedRegs<(add (sequence "X%u", 1, 28), FP,
331                            (sequence "Q%u", 0, 31))>;
332
333 def CSR_AArch64_AllRegs
334     : CalleeSavedRegs<(add (sequence "W%u", 0, 30), WSP,
335                            (sequence "X%u", 0, 28), FP, LR, SP,
336                            (sequence "B%u", 0, 31), (sequence "H%u", 0, 31),
337                            (sequence "S%u", 0, 31), (sequence "D%u", 0, 31),
338                            (sequence "Q%u", 0, 31))>;
339
340 def CSR_AArch64_NoRegs : CalleeSavedRegs<(add)>;
341
342 def CSR_AArch64_RT_MostRegs :  CalleeSavedRegs<(add CSR_AArch64_AAPCS,
343                                                 (sequence "X%u", 9, 15))>;
344