1 //===- X86CallingConv.td - Calling Conventions X86 32/64 ---*- tablegen -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This describes the calling conventions for the X86-32 and X86-64
13 //===----------------------------------------------------------------------===//
15 /// CCIfSubtarget - Match if the current subtarget has a feature F.
16 class CCIfSubtarget<string F, CCAction A>
17 : CCIf<!strconcat("State.getTarget().getSubtarget<X86Subtarget>().", F), A>;
19 //===----------------------------------------------------------------------===//
20 // Return Value Calling Conventions
21 //===----------------------------------------------------------------------===//
23 // Return-value conventions common to all X86 CC's.
24 def RetCC_X86Common : CallingConv<[
25 // Scalar values are returned in AX first, then DX. For i8, the ABI
26 // requires the values to be in AL and AH, however this code uses AL and DL
27 // instead. This is because using AH for the second register conflicts with
28 // the way LLVM does multiple return values -- a return of {i16,i8} would end
29 // up in AX and AH, which overlap. Front-ends wishing to conform to the ABI
30 // for functions that return two i8 values are currently expected to pack the
31 // values into an i16 (which uses AX, and thus AL:AH).
32 CCIfType<[i8] , CCAssignToReg<[AL, DL]>>,
33 CCIfType<[i16], CCAssignToReg<[AX, DX]>>,
34 CCIfType<[i32], CCAssignToReg<[EAX, EDX]>>,
35 CCIfType<[i64], CCAssignToReg<[RAX, RDX]>>,
37 // Vector types are returned in XMM0 and XMM1, when they fit. XMM2 and XMM3
38 // can only be used by ABI non-compliant code. If the target doesn't have XMM
39 // registers, it won't have vector types.
40 CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
41 CCAssignToReg<[XMM0,XMM1,XMM2,XMM3]>>,
43 // 256-bit vectors are returned in YMM0 and XMM1, when they fit. YMM2 and YMM3
44 // can only be used by ABI non-compliant code. This vector type is only
45 // supported while using the AVX target feature.
46 CCIfType<[v32i8, v16i16, v8i32, v4i64, v8f32, v4f64],
47 CCAssignToReg<[YMM0,YMM1,YMM2,YMM3]>>,
49 // MMX vector types are always returned in MM0. If the target doesn't have
50 // MM0, it doesn't support these vector types.
51 CCIfType<[x86mmx], CCAssignToReg<[MM0]>>,
53 // Long double types are always returned in ST0 (even with SSE).
54 CCIfType<[f80], CCAssignToReg<[ST0, ST1]>>
57 // X86-32 C return-value convention.
58 def RetCC_X86_32_C : CallingConv<[
59 // The X86-32 calling convention returns FP values in ST0, unless marked
60 // with "inreg" (used here to distinguish one kind of reg from another,
61 // weirdly; this is really the sse-regparm calling convention) in which
62 // case they use XMM0, otherwise it is the same as the common X86 calling
64 CCIfInReg<CCIfSubtarget<"hasXMMInt()",
65 CCIfType<[f32, f64], CCAssignToReg<[XMM0,XMM1,XMM2]>>>>,
66 CCIfType<[f32,f64], CCAssignToReg<[ST0, ST1]>>,
67 CCDelegateTo<RetCC_X86Common>
70 // X86-32 FastCC return-value convention.
71 def RetCC_X86_32_Fast : CallingConv<[
72 // The X86-32 fastcc returns 1, 2, or 3 FP values in XMM0-2 if the target has
74 // This can happen when a float, 2 x float, or 3 x float vector is split by
75 // target lowering, and is returned in 1-3 sse regs.
76 CCIfType<[f32], CCIfSubtarget<"hasXMMInt()", CCAssignToReg<[XMM0,XMM1,XMM2]>>>,
77 CCIfType<[f64], CCIfSubtarget<"hasXMMInt()", CCAssignToReg<[XMM0,XMM1,XMM2]>>>,
79 // For integers, ECX can be used as an extra return register
80 CCIfType<[i8], CCAssignToReg<[AL, DL, CL]>>,
81 CCIfType<[i16], CCAssignToReg<[AX, DX, CX]>>,
82 CCIfType<[i32], CCAssignToReg<[EAX, EDX, ECX]>>,
84 // Otherwise, it is the same as the common X86 calling convention.
85 CCDelegateTo<RetCC_X86Common>
88 // X86-64 C return-value convention.
89 def RetCC_X86_64_C : CallingConv<[
90 // The X86-64 calling convention always returns FP values in XMM0.
91 CCIfType<[f32], CCAssignToReg<[XMM0, XMM1]>>,
92 CCIfType<[f64], CCAssignToReg<[XMM0, XMM1]>>,
94 // MMX vector types are always returned in XMM0.
95 CCIfType<[x86mmx], CCAssignToReg<[XMM0, XMM1]>>,
96 CCDelegateTo<RetCC_X86Common>
99 // X86-Win64 C return-value convention.
100 def RetCC_X86_Win64_C : CallingConv<[
101 // The X86-Win64 calling convention always returns __m64 values in RAX.
102 CCIfType<[x86mmx], CCBitConvertToType<i64>>,
104 // Otherwise, everything is the same as 'normal' X86-64 C CC.
105 CCDelegateTo<RetCC_X86_64_C>
109 // This is the root return-value convention for the X86-32 backend.
110 def RetCC_X86_32 : CallingConv<[
111 // If FastCC, use RetCC_X86_32_Fast.
112 CCIfCC<"CallingConv::Fast", CCDelegateTo<RetCC_X86_32_Fast>>,
113 // Otherwise, use RetCC_X86_32_C.
114 CCDelegateTo<RetCC_X86_32_C>
117 // This is the root return-value convention for the X86-64 backend.
118 def RetCC_X86_64 : CallingConv<[
119 // Mingw64 and native Win64 use Win64 CC
120 CCIfSubtarget<"isTargetWin64()", CCDelegateTo<RetCC_X86_Win64_C>>,
122 // Otherwise, drop to normal X86-64 CC
123 CCDelegateTo<RetCC_X86_64_C>
126 // This is the return-value convention used for the entire X86 backend.
127 def RetCC_X86 : CallingConv<[
128 CCIfSubtarget<"is64Bit()", CCDelegateTo<RetCC_X86_64>>,
129 CCDelegateTo<RetCC_X86_32>
132 //===----------------------------------------------------------------------===//
133 // X86-64 Argument Calling Conventions
134 //===----------------------------------------------------------------------===//
136 def CC_X86_64_C : CallingConv<[
137 // Handles byval parameters.
138 CCIfByVal<CCPassByVal<8, 8>>,
140 // Promote i8/i16 arguments to i32.
141 CCIfType<[i8, i16], CCPromoteToType<i32>>,
143 // The 'nest' parameter, if any, is passed in R10.
144 CCIfNest<CCAssignToReg<[R10]>>,
146 // The first 6 integer arguments are passed in integer registers.
147 CCIfType<[i32], CCAssignToReg<[EDI, ESI, EDX, ECX, R8D, R9D]>>,
148 CCIfType<[i64], CCAssignToReg<[RDI, RSI, RDX, RCX, R8 , R9 ]>>,
150 // The first 8 MMX vector arguments are passed in XMM registers on Darwin.
152 CCIfSubtarget<"isTargetDarwin()",
153 CCIfSubtarget<"hasXMMInt()",
154 CCPromoteToType<v2i64>>>>,
156 // The first 8 FP/Vector arguments are passed in XMM registers.
157 CCIfType<[f32, f64, v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
158 CCIfSubtarget<"hasXMM()",
159 CCAssignToReg<[XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7]>>>,
161 // The first 8 256-bit vector arguments are passed in YMM registers.
162 CCIfType<[v32i8, v16i16, v8i32, v4i64, v8f32, v4f64],
163 CCIfSubtarget<"hasAVX()",
164 CCAssignToReg<[YMM0, YMM1, YMM2, YMM3, YMM4, YMM5, YMM6, YMM7]>>>,
166 // Integer/FP values get stored in stack slots that are 8 bytes in size and
167 // 8-byte aligned if there are no more registers to hold them.
168 CCIfType<[i32, i64, f32, f64], CCAssignToStack<8, 8>>,
170 // Long doubles get stack slots whose size and alignment depends on the
172 CCIfType<[f80], CCAssignToStack<0, 0>>,
174 // Vectors get 16-byte stack slots that are 16-byte aligned.
175 CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64], CCAssignToStack<16, 16>>,
177 // 256-bit vectors get 32-byte stack slots that are 32-byte aligned.
178 CCIfType<[v32i8, v16i16, v8i32, v4i64, v8f32, v4f64],
179 CCAssignToStack<32, 32>>
182 // Calling convention used on Win64
183 def CC_X86_Win64_C : CallingConv<[
184 // FIXME: Handle byval stuff.
185 // FIXME: Handle varargs.
187 // Promote i8/i16 arguments to i32.
188 CCIfType<[i8, i16], CCPromoteToType<i32>>,
190 // The 'nest' parameter, if any, is passed in R10.
191 CCIfNest<CCAssignToReg<[R10]>>,
193 // 128 bit vectors are passed by pointer
194 CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64], CCPassIndirect<i64>>,
196 // The first 4 MMX vector arguments are passed in GPRs.
197 CCIfType<[x86mmx], CCBitConvertToType<i64>>,
199 // The first 4 integer arguments are passed in integer registers.
200 CCIfType<[i32], CCAssignToRegWithShadow<[ECX , EDX , R8D , R9D ],
201 [XMM0, XMM1, XMM2, XMM3]>>,
203 // Do not pass the sret argument in RCX, the Win64 thiscall calling
204 // convention requires "this" to be passed in RCX.
205 CCIfCC<"CallingConv::X86_ThisCall",
206 CCIfSRet<CCIfType<[i64], CCAssignToRegWithShadow<[RDX , R8 , R9 ],
207 [XMM1, XMM2, XMM3]>>>>,
209 CCIfType<[i64], CCAssignToRegWithShadow<[RCX , RDX , R8 , R9 ],
210 [XMM0, XMM1, XMM2, XMM3]>>,
212 // The first 4 FP/Vector arguments are passed in XMM registers.
213 CCIfType<[f32, f64, v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
214 CCAssignToRegWithShadow<[XMM0, XMM1, XMM2, XMM3],
215 [RCX , RDX , R8 , R9 ]>>,
217 // Integer/FP values get stored in stack slots that are 8 bytes in size and
218 // 8-byte aligned if there are no more registers to hold them.
219 CCIfType<[i32, i64, f32, f64], CCAssignToStack<8, 8>>,
221 // Long doubles get stack slots whose size and alignment depends on the
223 CCIfType<[f80], CCAssignToStack<0, 0>>
226 def CC_X86_64_GHC : CallingConv<[
227 // Promote i8/i16/i32 arguments to i64.
228 CCIfType<[i8, i16, i32], CCPromoteToType<i64>>,
230 // Pass in STG registers: Base, Sp, Hp, R1, R2, R3, R4, R5, R6, SpLim
232 CCAssignToReg<[R13, RBP, R12, RBX, R14, RSI, RDI, R8, R9, R15]>>,
234 // Pass in STG registers: F1, F2, F3, F4, D1, D2
235 CCIfType<[f32, f64, v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
236 CCIfSubtarget<"hasXMM()",
237 CCAssignToReg<[XMM1, XMM2, XMM3, XMM4, XMM5, XMM6]>>>
240 //===----------------------------------------------------------------------===//
241 // X86 C Calling Convention
242 //===----------------------------------------------------------------------===//
244 /// CC_X86_32_Common - In all X86-32 calling conventions, extra integers and FP
245 /// values are spilled on the stack, and the first 4 vector values go in XMM
247 def CC_X86_32_Common : CallingConv<[
248 // Handles byval parameters.
249 CCIfByVal<CCPassByVal<4, 4>>,
251 // The first 3 float or double arguments, if marked 'inreg' and if the call
252 // is not a vararg call and if SSE2 is available, are passed in SSE registers.
253 CCIfNotVarArg<CCIfInReg<CCIfType<[f32,f64],
254 CCIfSubtarget<"hasXMMInt()",
255 CCAssignToReg<[XMM0,XMM1,XMM2]>>>>>,
257 // The first 3 __m64 vector arguments are passed in mmx registers if the
258 // call is not a vararg call.
259 CCIfNotVarArg<CCIfType<[x86mmx],
260 CCAssignToReg<[MM0, MM1, MM2]>>>,
262 // Integer/Float values get stored in stack slots that are 4 bytes in
263 // size and 4-byte aligned.
264 CCIfType<[i32, f32], CCAssignToStack<4, 4>>,
266 // Doubles get 8-byte slots that are 4-byte aligned.
267 CCIfType<[f64], CCAssignToStack<8, 4>>,
269 // Long doubles get slots whose size depends on the subtarget.
270 CCIfType<[f80], CCAssignToStack<0, 4>>,
272 // The first 4 SSE vector arguments are passed in XMM registers.
273 CCIfNotVarArg<CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],
274 CCAssignToReg<[XMM0, XMM1, XMM2, XMM3]>>>,
276 // The first 4 AVX 256-bit vector arguments are passed in YMM registers.
277 CCIfNotVarArg<CCIfType<[v32i8, v16i16, v8i32, v4i64, v8f32, v4f64],
278 CCIfSubtarget<"hasAVX()",
279 CCAssignToReg<[YMM0, YMM1, YMM2, YMM3]>>>>,
281 // Other SSE vectors get 16-byte stack slots that are 16-byte aligned.
282 CCIfType<[v16i8, v8i16, v4i32, v2i64, v4f32, v2f64], CCAssignToStack<16, 16>>,
284 // 256-bit AVX vectors get 32-byte stack slots that are 32-byte aligned.
285 CCIfType<[v32i8, v16i16, v8i32, v4i64, v8f32, v4f64],
286 CCAssignToStack<32, 32>>,
288 // __m64 vectors get 8-byte stack slots that are 4-byte aligned. They are
289 // passed in the parameter area.
290 CCIfType<[x86mmx], CCAssignToStack<8, 4>>]>;
292 def CC_X86_32_C : CallingConv<[
293 // Promote i8/i16 arguments to i32.
294 CCIfType<[i8, i16], CCPromoteToType<i32>>,
296 // The 'nest' parameter, if any, is passed in ECX.
297 CCIfNest<CCAssignToReg<[ECX]>>,
299 // The first 3 integer arguments, if marked 'inreg' and if the call is not
300 // a vararg call, are passed in integer registers.
301 CCIfNotVarArg<CCIfInReg<CCIfType<[i32], CCAssignToReg<[EAX, EDX, ECX]>>>>,
303 // Otherwise, same as everything else.
304 CCDelegateTo<CC_X86_32_Common>
307 def CC_X86_32_FastCall : CallingConv<[
308 // Promote i8/i16 arguments to i32.
309 CCIfType<[i8, i16], CCPromoteToType<i32>>,
311 // The 'nest' parameter, if any, is passed in EAX.
312 CCIfNest<CCAssignToReg<[EAX]>>,
314 // The first 2 integer arguments are passed in ECX/EDX
315 CCIfType<[i32], CCAssignToReg<[ECX, EDX]>>,
317 // Otherwise, same as everything else.
318 CCDelegateTo<CC_X86_32_Common>
321 def CC_X86_32_ThisCall : CallingConv<[
322 // Promote i8/i16 arguments to i32.
323 CCIfType<[i8, i16], CCPromoteToType<i32>>,
325 // The 'nest' parameter, if any, is passed in EAX.
326 CCIfNest<CCAssignToReg<[EAX]>>,
328 // The first integer argument is passed in ECX
329 CCIfType<[i32], CCAssignToReg<[ECX]>>,
331 // Otherwise, same as everything else.
332 CCDelegateTo<CC_X86_32_Common>
335 def CC_X86_32_FastCC : CallingConv<[
336 // Handles byval parameters. Note that we can't rely on the delegation
337 // to CC_X86_32_Common for this because that happens after code that
338 // puts arguments in registers.
339 CCIfByVal<CCPassByVal<4, 4>>,
341 // Promote i8/i16 arguments to i32.
342 CCIfType<[i8, i16], CCPromoteToType<i32>>,
344 // The 'nest' parameter, if any, is passed in EAX.
345 CCIfNest<CCAssignToReg<[EAX]>>,
347 // The first 2 integer arguments are passed in ECX/EDX
348 CCIfType<[i32], CCAssignToReg<[ECX, EDX]>>,
350 // The first 3 float or double arguments, if the call is not a vararg
351 // call and if SSE2 is available, are passed in SSE registers.
352 CCIfNotVarArg<CCIfType<[f32,f64],
353 CCIfSubtarget<"hasXMMInt()",
354 CCAssignToReg<[XMM0,XMM1,XMM2]>>>>,
356 // Doubles get 8-byte slots that are 8-byte aligned.
357 CCIfType<[f64], CCAssignToStack<8, 8>>,
359 // Otherwise, same as everything else.
360 CCDelegateTo<CC_X86_32_Common>
363 def CC_X86_32_GHC : CallingConv<[
364 // Promote i8/i16 arguments to i32.
365 CCIfType<[i8, i16], CCPromoteToType<i32>>,
367 // Pass in STG registers: Base, Sp, Hp, R1
368 CCIfType<[i32], CCAssignToReg<[EBX, EBP, EDI, ESI]>>
371 //===----------------------------------------------------------------------===//
372 // X86 Root Argument Calling Conventions
373 //===----------------------------------------------------------------------===//
375 // This is the root argument convention for the X86-32 backend.
376 def CC_X86_32 : CallingConv<[
377 CCIfCC<"CallingConv::X86_FastCall", CCDelegateTo<CC_X86_32_FastCall>>,
378 CCIfCC<"CallingConv::X86_ThisCall", CCDelegateTo<CC_X86_32_ThisCall>>,
379 CCIfCC<"CallingConv::Fast", CCDelegateTo<CC_X86_32_FastCC>>,
380 CCIfCC<"CallingConv::GHC", CCDelegateTo<CC_X86_32_GHC>>,
382 // Otherwise, drop to normal X86-32 CC
383 CCDelegateTo<CC_X86_32_C>
386 // This is the root argument convention for the X86-64 backend.
387 def CC_X86_64 : CallingConv<[
388 CCIfCC<"CallingConv::GHC", CCDelegateTo<CC_X86_64_GHC>>,
390 // Mingw64 and native Win64 use Win64 CC
391 CCIfSubtarget<"isTargetWin64()", CCDelegateTo<CC_X86_Win64_C>>,
393 // Otherwise, drop to normal X86-64 CC
394 CCDelegateTo<CC_X86_64_C>
397 // This is the argument convention used for the entire X86 backend.
398 def CC_X86 : CallingConv<[
399 CCIfSubtarget<"is64Bit()", CCDelegateTo<CC_X86_64>>,
400 CCDelegateTo<CC_X86_32>