blob: 4454662a93046d25b7291aa56c8a7a3ed9b9bf9a [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +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// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000023using namespace clang;
24using namespace CodeGen;
25
26ABIInfo::~ABIInfo() {}
27
28void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000029 llvm::raw_ostream &OS = llvm::errs();
30 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000031 switch (TheKind) {
32 case Direct:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000033 OS << "Direct";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000034 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000035 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000036 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000037 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000038 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000039 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000040 break;
41 case Coerce:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000042 OS << "Coerce Type=";
43 getCoerceToType()->print(OS);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044 break;
45 case Indirect:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000046 OS << "Indirect Align=" << getIndirectAlign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000047 break;
48 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000049 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000050 break;
51 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000052 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000053}
54
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000055TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
56
Daniel Dunbar98303b92009-09-13 08:03:58 +000057static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000058
59/// isEmptyField - Return true iff a the field is "empty", that is it
60/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000061static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
62 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000063 if (FD->isUnnamedBitfield())
64 return true;
65
66 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000067
Daniel Dunbar98303b92009-09-13 08:03:58 +000068 // Constant arrays of empty records count as empty, strip them off.
69 if (AllowArrays)
70 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
71 FT = AT->getElementType();
72
73 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000074}
75
76/// isEmptyRecord - Return true iff a structure contains only empty
77/// fields. Note that a structure with a flexible array member is not
78/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +000079static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +000080 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000081 if (!RT)
82 return 0;
83 const RecordDecl *RD = RT->getDecl();
84 if (RD->hasFlexibleArrayMember())
85 return false;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000086 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
87 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +000088 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000089 return false;
90 return true;
91}
92
Anders Carlsson0a8f8472009-09-16 15:53:40 +000093/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
94/// a non-trivial destructor or a non-trivial copy constructor.
95static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
96 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
97 if (!RD)
98 return false;
99
100 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
101}
102
103/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
104/// a record type with either a non-trivial destructor or a non-trivial copy
105/// constructor.
106static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
107 const RecordType *RT = T->getAs<RecordType>();
108 if (!RT)
109 return false;
110
111 return hasNonTrivialDestructorOrCopyConstructor(RT);
112}
113
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000114/// isSingleElementStruct - Determine if a structure is a "single
115/// element struct", i.e. it has exactly one non-empty field or
116/// exactly one field which is itself a single element
117/// struct. Structures with flexible array members are never
118/// considered single element structs.
119///
120/// \return The field declaration for the single non-empty field, if
121/// it exists.
122static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
123 const RecordType *RT = T->getAsStructureType();
124 if (!RT)
125 return 0;
126
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return 0;
130
131 const Type *Found = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000132 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
133 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000134 const FieldDecl *FD = *i;
135 QualType FT = FD->getType();
136
137 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000138 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000139 continue;
140
141 // If we already found an element then this isn't a single-element
142 // struct.
143 if (Found)
144 return 0;
145
146 // Treat single element arrays as the element.
147 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
148 if (AT->getSize().getZExtValue() != 1)
149 break;
150 FT = AT->getElementType();
151 }
152
153 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
154 Found = FT.getTypePtr();
155 } else {
156 Found = isSingleElementStruct(FT, Context);
157 if (!Found)
158 return 0;
159 }
160 }
161
162 return Found;
163}
164
165static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000166 if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() &&
167 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
168 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000169 return false;
170
171 uint64_t Size = Context.getTypeSize(Ty);
172 return Size == 32 || Size == 64;
173}
174
Daniel Dunbar53012f42009-11-09 01:33:53 +0000175/// canExpandIndirectArgument - Test whether an argument type which is to be
176/// passed indirectly (on the stack) would have the equivalent layout if it was
177/// expanded into separate arguments. If so, we prefer to do the latter to avoid
178/// inhibiting optimizations.
179///
180// FIXME: This predicate is missing many cases, currently it just follows
181// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
182// should probably make this smarter, or better yet make the LLVM backend
183// capable of handling it.
184static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
185 // We can only expand structure types.
186 const RecordType *RT = Ty->getAs<RecordType>();
187 if (!RT)
188 return false;
189
190 // We can only expand (C) structures.
191 //
192 // FIXME: This needs to be generalized to handle classes as well.
193 const RecordDecl *RD = RT->getDecl();
194 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
195 return false;
196
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000197 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
198 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000199 const FieldDecl *FD = *i;
200
201 if (!is32Or64BitBasicType(FD->getType(), Context))
202 return false;
203
204 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
205 // how to expand them yet, and the predicate for telling if a bitfield still
206 // counts as "basic" is more complicated than what we were doing previously.
207 if (FD->isBitField())
208 return false;
209 }
210
211 return true;
212}
213
Eli Friedmana1e6de92009-06-13 21:37:10 +0000214static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000215 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
216 i != e; ++i) {
Eli Friedmana1e6de92009-06-13 21:37:10 +0000217 const FieldDecl *FD = *i;
218
219 if (FD->getType()->isVectorType() &&
220 Context.getTypeSize(FD->getType()) >= 128)
221 return true;
222
Ted Kremenek6217b802009-07-29 21:53:49 +0000223 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000224 if (typeContainsSSEVector(RT->getDecl(), Context))
225 return true;
226 }
227
228 return false;
229}
230
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000231namespace {
232/// DefaultABIInfo - The default implementation for ABI specific
233/// details. This implementation provides information which results in
234/// self-consistent and sensible LLVM IR generation, but does not
235/// conform to any particular ABI.
236class DefaultABIInfo : public ABIInfo {
237 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000238 ASTContext &Context,
239 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000240
241 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000242 ASTContext &Context,
243 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000244
Owen Andersona1cf15f2009-07-14 23:10:40 +0000245 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
246 llvm::LLVMContext &VMContext) const {
247 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
248 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000249 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
250 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000251 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000252 }
253
254 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
255 CodeGenFunction &CGF) const;
256};
257
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000258class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
259public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000260 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000261};
262
263llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
264 CodeGenFunction &CGF) const {
265 return 0;
266}
267
268ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
269 ASTContext &Context,
270 llvm::LLVMContext &VMContext) const {
271 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
272 return ABIArgInfo::getIndirect(0);
273 } else {
274 return (Ty->isPromotableIntegerType() ?
275 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
276 }
277}
278
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000279/// X86_32ABIInfo - The X86-32 ABI information.
280class X86_32ABIInfo : public ABIInfo {
281 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000282 bool IsDarwinVectorABI;
283 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000284
285 static bool isRegisterSize(unsigned Size) {
286 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
287 }
288
289 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
290
Eli Friedmana1e6de92009-06-13 21:37:10 +0000291 static unsigned getIndirectArgumentAlignment(QualType Ty,
292 ASTContext &Context);
293
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000294public:
295 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000296 ASTContext &Context,
297 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000298
299 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000300 ASTContext &Context,
301 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000302
Owen Andersona1cf15f2009-07-14 23:10:40 +0000303 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
304 llvm::LLVMContext &VMContext) const {
305 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
306 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000307 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
308 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000309 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000310 }
311
312 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
313 CodeGenFunction &CGF) const;
314
David Chisnall1e4249c2009-08-17 23:08:21 +0000315 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000316 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000317 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000318};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000319
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000320class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
321public:
322 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000323 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000324};
325
326}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000327
328/// shouldReturnTypeInRegister - Determine if the given type should be
329/// passed in a register (for the Darwin ABI).
330bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
331 ASTContext &Context) {
332 uint64_t Size = Context.getTypeSize(Ty);
333
334 // Type must be register sized.
335 if (!isRegisterSize(Size))
336 return false;
337
338 if (Ty->isVectorType()) {
339 // 64- and 128- bit vectors inside structures are not returned in
340 // registers.
341 if (Size == 64 || Size == 128)
342 return false;
343
344 return true;
345 }
346
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000347 // If this is a builtin, pointer, enum, or complex type, it is ok.
348 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
349 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
350 Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000351 return true;
352
353 // Arrays are treated like records.
354 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
355 return shouldReturnTypeInRegister(AT->getElementType(), Context);
356
357 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000358 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000359 if (!RT) return false;
360
361 // Structure types are passed in register if all fields would be
362 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000363 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
364 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000365 const FieldDecl *FD = *i;
366
367 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000368 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000369 continue;
370
371 // Check fields recursively.
372 if (!shouldReturnTypeInRegister(FD->getType(), Context))
373 return false;
374 }
375
376 return true;
377}
378
379ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000380 ASTContext &Context,
381 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000382 if (RetTy->isVoidType()) {
383 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000384 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000385 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000386 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000387 uint64_t Size = Context.getTypeSize(RetTy);
388
389 // 128-bit vectors are a special case; they are returned in
390 // registers and we need to make sure to pick a type the LLVM
391 // backend will like.
392 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000393 return ABIArgInfo::getCoerce(llvm::VectorType::get(
394 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000395
396 // Always return in register if it fits in a general purpose
397 // register, or if it is 64 bits and has a single element.
398 if ((Size == 8 || Size == 16 || Size == 32) ||
399 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000400 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000401
402 return ABIArgInfo::getIndirect(0);
403 }
404
405 return ABIArgInfo::getDirect();
406 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40092972009-10-20 22:07:59 +0000407 if (const RecordType *RT = RetTy->getAsStructureType()) {
408 // Structures with either a non-trivial destructor or a non-trivial
409 // copy constructor are always indirect.
410 if (hasNonTrivialDestructorOrCopyConstructor(RT))
411 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
412
413 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000414 if (RT->getDecl()->hasFlexibleArrayMember())
415 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000416 }
417
David Chisnall1e4249c2009-08-17 23:08:21 +0000418 // If specified, structs and unions are always indirect.
419 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000420 return ABIArgInfo::getIndirect(0);
421
422 // Classify "single element" structs as their element type.
423 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000424 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000425 if (BT->isIntegerType()) {
426 // We need to use the size of the structure, padding
427 // bit-fields can adjust that to be larger than the single
428 // element type.
429 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000430 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000431 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000432 } else if (BT->getKind() == BuiltinType::Float) {
433 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
434 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000435 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000436 } else if (BT->getKind() == BuiltinType::Double) {
437 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
438 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000439 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000440 }
441 } else if (SeltTy->isPointerType()) {
442 // FIXME: It would be really nice if this could come out as the proper
443 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000444 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000445 return ABIArgInfo::getCoerce(PtrTy);
446 } else if (SeltTy->isVectorType()) {
447 // 64- and 128-bit vectors are never returned in a
448 // register when inside a structure.
449 uint64_t Size = Context.getTypeSize(RetTy);
450 if (Size == 64 || Size == 128)
451 return ABIArgInfo::getIndirect(0);
452
Owen Andersona1cf15f2009-07-14 23:10:40 +0000453 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000454 }
455 }
456
457 // Small structures which are register sized are generally returned
458 // in a register.
459 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
460 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000461 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000462 }
463
464 return ABIArgInfo::getIndirect(0);
465 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000466 return (RetTy->isPromotableIntegerType() ?
467 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000468 }
469}
470
Eli Friedmana1e6de92009-06-13 21:37:10 +0000471unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
472 ASTContext &Context) {
473 unsigned Align = Context.getTypeAlign(Ty);
474 if (Align < 128) return 0;
Ted Kremenek6217b802009-07-29 21:53:49 +0000475 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000476 if (typeContainsSSEVector(RT->getDecl(), Context))
477 return 16;
478 return 0;
479}
480
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000481ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000482 ASTContext &Context,
483 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000484 // FIXME: Set alignment on indirect arguments.
485 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
486 // Structures with flexible arrays are always indirect.
487 if (const RecordType *RT = Ty->getAsStructureType())
488 if (RT->getDecl()->hasFlexibleArrayMember())
Mike Stump1eb44332009-09-09 15:08:12 +0000489 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
Eli Friedmana1e6de92009-06-13 21:37:10 +0000490 Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000491
492 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000493 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494 return ABIArgInfo::getIgnore();
495
Daniel Dunbar53012f42009-11-09 01:33:53 +0000496 // Expand small (<= 128-bit) record types when we know that the stack layout
497 // of those arguments will match the struct. This is important because the
498 // LLVM backend isn't smart enough to remove byval, which inhibits many
499 // optimizations.
500 if (Context.getTypeSize(Ty) <= 4*32 &&
501 canExpandIndirectArgument(Ty, Context))
502 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000503
Eli Friedmana1e6de92009-06-13 21:37:10 +0000504 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000505 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000506 return (Ty->isPromotableIntegerType() ?
507 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000508 }
509}
510
511llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
512 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000513 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000514 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000515
516 CGBuilderTy &Builder = CGF.Builder;
517 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
518 "ap");
519 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
520 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000521 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000522 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
523
524 uint64_t Offset =
525 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
526 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +0000527 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
528 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000529 "ap.next");
530 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
531
532 return AddrTyped;
533}
534
535namespace {
536/// X86_64ABIInfo - The X86_64 ABI information.
537class X86_64ABIInfo : public ABIInfo {
538 enum Class {
539 Integer = 0,
540 SSE,
541 SSEUp,
542 X87,
543 X87Up,
544 ComplexX87,
545 NoClass,
546 Memory
547 };
548
549 /// merge - Implement the X86_64 ABI merging algorithm.
550 ///
551 /// Merge an accumulating classification \arg Accum with a field
552 /// classification \arg Field.
553 ///
554 /// \param Accum - The accumulating classification. This should
555 /// always be either NoClass or the result of a previous merge
556 /// call. In addition, this should never be Memory (the caller
557 /// should just return Memory for the aggregate).
558 Class merge(Class Accum, Class Field) const;
559
560 /// classify - Determine the x86_64 register classes in which the
561 /// given type T should be passed.
562 ///
563 /// \param Lo - The classification for the parts of the type
564 /// residing in the low word of the containing object.
565 ///
566 /// \param Hi - The classification for the parts of the type
567 /// residing in the high word of the containing object.
568 ///
569 /// \param OffsetBase - The bit offset of this type in the
570 /// containing object. Some parameters are classified different
571 /// depending on whether they straddle an eightbyte boundary.
572 ///
573 /// If a word is unused its result will be NoClass; if a type should
574 /// be passed in Memory then at least the classification of \arg Lo
575 /// will be Memory.
576 ///
577 /// The \arg Lo class will be NoClass iff the argument is ignored.
578 ///
579 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
580 /// also be ComplexX87.
581 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
582 Class &Lo, Class &Hi) const;
583
584 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
585 /// to coerce to, chose the best way to pass Ty in the same place
586 /// that \arg CoerceTo would be passed, but while keeping the
587 /// emitted code as simple as possible.
588 ///
589 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
590 /// the ways we might want to pass things, instead of constructing an LLVM
591 /// type. This makes this code more explicit, and it makes it clearer that we
592 /// are also doing this for correctness in the case of passing scalar types.
593 ABIArgInfo getCoerceResult(QualType Ty,
594 const llvm::Type *CoerceTo,
595 ASTContext &Context) const;
596
597 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
598 /// such that the argument will be passed in memory.
599 ABIArgInfo getIndirectResult(QualType Ty,
600 ASTContext &Context) const;
601
602 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000603 ASTContext &Context,
604 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000605
606 ABIArgInfo classifyArgumentType(QualType Ty,
607 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000608 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000609 unsigned &neededInt,
610 unsigned &neededSSE) const;
611
612public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000613 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
614 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000615
616 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
617 CodeGenFunction &CGF) const;
618};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000619
620class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
621public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000622 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000623};
624
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000625}
626
627X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
628 Class Field) const {
629 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
630 // classified recursively so that always two fields are
631 // considered. The resulting class is calculated according to
632 // the classes of the fields in the eightbyte:
633 //
634 // (a) If both classes are equal, this is the resulting class.
635 //
636 // (b) If one of the classes is NO_CLASS, the resulting class is
637 // the other class.
638 //
639 // (c) If one of the classes is MEMORY, the result is the MEMORY
640 // class.
641 //
642 // (d) If one of the classes is INTEGER, the result is the
643 // INTEGER.
644 //
645 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
646 // MEMORY is used as class.
647 //
648 // (f) Otherwise class SSE is used.
649
650 // Accum should never be memory (we should have returned) or
651 // ComplexX87 (because this cannot be passed in a structure).
652 assert((Accum != Memory && Accum != ComplexX87) &&
653 "Invalid accumulated classification during merge.");
654 if (Accum == Field || Field == NoClass)
655 return Accum;
656 else if (Field == Memory)
657 return Memory;
658 else if (Accum == NoClass)
659 return Field;
660 else if (Accum == Integer || Field == Integer)
661 return Integer;
662 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
663 Accum == X87 || Accum == X87Up)
664 return Memory;
665 else
666 return SSE;
667}
668
669void X86_64ABIInfo::classify(QualType Ty,
670 ASTContext &Context,
671 uint64_t OffsetBase,
672 Class &Lo, Class &Hi) const {
673 // FIXME: This code can be simplified by introducing a simple value class for
674 // Class pairs with appropriate constructor methods for the various
675 // situations.
676
677 // FIXME: Some of the split computations are wrong; unaligned vectors
678 // shouldn't be passed in registers for example, so there is no chance they
679 // can straddle an eightbyte. Verify & simplify.
680
681 Lo = Hi = NoClass;
682
683 Class &Current = OffsetBase < 64 ? Lo : Hi;
684 Current = Memory;
685
John McCall183700f2009-09-21 23:43:11 +0000686 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000687 BuiltinType::Kind k = BT->getKind();
688
689 if (k == BuiltinType::Void) {
690 Current = NoClass;
691 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
692 Lo = Integer;
693 Hi = Integer;
694 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
695 Current = Integer;
696 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
697 Current = SSE;
698 } else if (k == BuiltinType::LongDouble) {
699 Lo = X87;
700 Hi = X87Up;
701 }
702 // FIXME: _Decimal32 and _Decimal64 are SSE.
703 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall183700f2009-09-21 23:43:11 +0000704 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000705 // Classify the underlying integer type.
706 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
707 } else if (Ty->hasPointerRepresentation()) {
708 Current = Integer;
John McCall183700f2009-09-21 23:43:11 +0000709 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000710 uint64_t Size = Context.getTypeSize(VT);
711 if (Size == 32) {
712 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
713 // float> as integer.
714 Current = Integer;
715
716 // If this type crosses an eightbyte boundary, it should be
717 // split.
718 uint64_t EB_Real = (OffsetBase) / 64;
719 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
720 if (EB_Real != EB_Imag)
721 Hi = Lo;
722 } else if (Size == 64) {
723 // gcc passes <1 x double> in memory. :(
724 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
725 return;
726
727 // gcc passes <1 x long long> as INTEGER.
728 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
729 Current = Integer;
730 else
731 Current = SSE;
732
733 // If this type crosses an eightbyte boundary, it should be
734 // split.
735 if (OffsetBase && OffsetBase != 64)
736 Hi = Lo;
737 } else if (Size == 128) {
738 Lo = SSE;
739 Hi = SSEUp;
740 }
John McCall183700f2009-09-21 23:43:11 +0000741 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000742 QualType ET = Context.getCanonicalType(CT->getElementType());
743
744 uint64_t Size = Context.getTypeSize(Ty);
745 if (ET->isIntegralType()) {
746 if (Size <= 64)
747 Current = Integer;
748 else if (Size <= 128)
749 Lo = Hi = Integer;
750 } else if (ET == Context.FloatTy)
751 Current = SSE;
752 else if (ET == Context.DoubleTy)
753 Lo = Hi = SSE;
754 else if (ET == Context.LongDoubleTy)
755 Current = ComplexX87;
756
757 // If this complex type crosses an eightbyte boundary then it
758 // should be split.
759 uint64_t EB_Real = (OffsetBase) / 64;
760 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
761 if (Hi == NoClass && EB_Real != EB_Imag)
762 Hi = Lo;
763 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
764 // Arrays are treated like structures.
765
766 uint64_t Size = Context.getTypeSize(Ty);
767
768 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
769 // than two eightbytes, ..., it has class MEMORY.
770 if (Size > 128)
771 return;
772
773 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
774 // fields, it has class MEMORY.
775 //
776 // Only need to check alignment of array base.
777 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
778 return;
779
780 // Otherwise implement simplified merge. We could be smarter about
781 // this, but it isn't worth it and would be harder to verify.
782 Current = NoClass;
783 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
784 uint64_t ArraySize = AT->getSize().getZExtValue();
785 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
786 Class FieldLo, FieldHi;
787 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
788 Lo = merge(Lo, FieldLo);
789 Hi = merge(Hi, FieldHi);
790 if (Lo == Memory || Hi == Memory)
791 break;
792 }
793
794 // Do post merger cleanup (see below). Only case we worry about is Memory.
795 if (Hi == Memory)
796 Lo = Memory;
797 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenek6217b802009-07-29 21:53:49 +0000798 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000799 uint64_t Size = Context.getTypeSize(Ty);
800
801 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
802 // than two eightbytes, ..., it has class MEMORY.
803 if (Size > 128)
804 return;
805
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000806 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
807 // copy constructor or a non-trivial destructor, it is passed by invisible
808 // reference.
809 if (hasNonTrivialDestructorOrCopyConstructor(RT))
810 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000811
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000812 const RecordDecl *RD = RT->getDecl();
813
814 // Assume variable sized types are passed in memory.
815 if (RD->hasFlexibleArrayMember())
816 return;
817
818 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
819
820 // Reset Lo class, this will be recomputed.
821 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000822
823 // If this is a C++ record, classify the bases first.
824 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
825 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
826 e = CXXRD->bases_end(); i != e; ++i) {
827 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
828 "Unexpected base class!");
829 const CXXRecordDecl *Base =
830 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
831
832 // Classify this field.
833 //
834 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
835 // single eightbyte, each is classified separately. Each eightbyte gets
836 // initialized to class NO_CLASS.
837 Class FieldLo, FieldHi;
838 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
839 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
840 Lo = merge(Lo, FieldLo);
841 Hi = merge(Hi, FieldHi);
842 if (Lo == Memory || Hi == Memory)
843 break;
844 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +0000845
846 // If this record has no fields but isn't empty, classify as INTEGER.
847 if (RD->field_empty() && Size)
848 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000849 }
850
851 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000852 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000853 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
854 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000855 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
856 bool BitField = i->isBitField();
857
858 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
859 // fields, it has class MEMORY.
860 //
861 // Note, skip this test for bit-fields, see below.
862 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
863 Lo = Memory;
864 return;
865 }
866
867 // Classify this field.
868 //
869 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
870 // exceeds a single eightbyte, each is classified
871 // separately. Each eightbyte gets initialized to class
872 // NO_CLASS.
873 Class FieldLo, FieldHi;
874
875 // Bit-fields require special handling, they do not force the
876 // structure to be passed in memory even if unaligned, and
877 // therefore they can straddle an eightbyte.
878 if (BitField) {
879 // Ignore padding bit-fields.
880 if (i->isUnnamedBitfield())
881 continue;
882
883 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
884 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
885
886 uint64_t EB_Lo = Offset / 64;
887 uint64_t EB_Hi = (Offset + Size - 1) / 64;
888 FieldLo = FieldHi = NoClass;
889 if (EB_Lo) {
890 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
891 FieldLo = NoClass;
892 FieldHi = Integer;
893 } else {
894 FieldLo = Integer;
895 FieldHi = EB_Hi ? Integer : NoClass;
896 }
897 } else
898 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
899 Lo = merge(Lo, FieldLo);
900 Hi = merge(Hi, FieldHi);
901 if (Lo == Memory || Hi == Memory)
902 break;
903 }
904
905 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
906 //
907 // (a) If one of the classes is MEMORY, the whole argument is
908 // passed in memory.
909 //
910 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
911
912 // The first of these conditions is guaranteed by how we implement
913 // the merge (just bail).
914 //
915 // The second condition occurs in the case of unions; for example
916 // union { _Complex double; unsigned; }.
917 if (Hi == Memory)
918 Lo = Memory;
919 if (Hi == SSEUp && Lo != SSE)
920 Hi = SSE;
921 }
922}
923
924ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
925 const llvm::Type *CoerceTo,
926 ASTContext &Context) const {
Owen Anderson0032b272009-08-13 21:57:51 +0000927 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000928 // Integer and pointer types will end up in a general purpose
929 // register.
Anders Carlsson5b3a2fc2009-09-26 03:56:53 +0000930 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000931 return (Ty->isPromotableIntegerType() ?
932 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson0032b272009-08-13 21:57:51 +0000933 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000934 // FIXME: It would probably be better to make CGFunctionInfo only map using
935 // canonical types than to canonize here.
936 QualType CTy = Context.getCanonicalType(Ty);
937
938 // Float and double end up in a single SSE reg.
939 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
940 return ABIArgInfo::getDirect();
941
942 }
943
944 return ABIArgInfo::getCoerce(CoerceTo);
945}
946
947ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
948 ASTContext &Context) const {
949 // If this is a scalar LLVM value then assume LLVM will pass it in the right
950 // place naturally.
951 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000952 return (Ty->isPromotableIntegerType() ?
953 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000954
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000955 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
956
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000957 // FIXME: Set alignment correctly.
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000958 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000959}
960
961ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000962 ASTContext &Context,
963 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000964 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
965 // classification algorithm.
966 X86_64ABIInfo::Class Lo, Hi;
967 classify(RetTy, Context, 0, Lo, Hi);
968
969 // Check some invariants.
970 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
971 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
972 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
973
974 const llvm::Type *ResType = 0;
975 switch (Lo) {
976 case NoClass:
977 return ABIArgInfo::getIgnore();
978
979 case SSEUp:
980 case X87Up:
981 assert(0 && "Invalid classification for lo word.");
982
983 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
984 // hidden argument.
985 case Memory:
986 return getIndirectResult(RetTy, Context);
987
988 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
989 // available register of the sequence %rax, %rdx is used.
990 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +0000991 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000992
993 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
994 // available SSE register of the sequence %xmm0, %xmm1 is used.
995 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +0000996 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000997
998 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
999 // returned on the X87 stack in %st0 as 80-bit x87 number.
1000 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001001 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001002
1003 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1004 // part of the value is returned in %st0 and the imaginary part in
1005 // %st1.
1006 case ComplexX87:
1007 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001008 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
1009 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001010 NULL);
1011 break;
1012 }
1013
1014 switch (Hi) {
1015 // Memory was handled previously and X87 should
1016 // never occur as a hi class.
1017 case Memory:
1018 case X87:
1019 assert(0 && "Invalid classification for hi word.");
1020
1021 case ComplexX87: // Previously handled.
1022 case NoClass: break;
1023
1024 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001025 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001026 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001027 break;
1028 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001029 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001030 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001031 break;
1032
1033 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1034 // is passed in the upper half of the last used SSE register.
1035 //
1036 // SSEUP should always be preceeded by SSE, just widen.
1037 case SSEUp:
1038 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001039 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001040 break;
1041
1042 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1043 // returned together with the previous X87 value in %st0.
1044 case X87Up:
1045 // If X87Up is preceeded by X87, we don't need to do
1046 // anything. However, in some cases with unions it may not be
1047 // preceeded by X87. In such situations we follow gcc and pass the
1048 // extra bits in an SSE reg.
1049 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001050 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001051 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001052 break;
1053 }
1054
1055 return getCoerceResult(RetTy, ResType, Context);
1056}
1057
1058ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001059 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001060 unsigned &neededInt,
1061 unsigned &neededSSE) const {
1062 X86_64ABIInfo::Class Lo, Hi;
1063 classify(Ty, Context, 0, Lo, Hi);
1064
1065 // Check some invariants.
1066 // FIXME: Enforce these by construction.
1067 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1068 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1069 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1070
1071 neededInt = 0;
1072 neededSSE = 0;
1073 const llvm::Type *ResType = 0;
1074 switch (Lo) {
1075 case NoClass:
1076 return ABIArgInfo::getIgnore();
1077
1078 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1079 // on the stack.
1080 case Memory:
1081
1082 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1083 // COMPLEX_X87, it is passed in memory.
1084 case X87:
1085 case ComplexX87:
1086 return getIndirectResult(Ty, Context);
1087
1088 case SSEUp:
1089 case X87Up:
1090 assert(0 && "Invalid classification for lo word.");
1091
1092 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1093 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1094 // and %r9 is used.
1095 case Integer:
1096 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001097 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001098 break;
1099
1100 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1101 // available SSE register is used, the registers are taken in the
1102 // order from %xmm0 to %xmm7.
1103 case SSE:
1104 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001105 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001106 break;
1107 }
1108
1109 switch (Hi) {
1110 // Memory was handled previously, ComplexX87 and X87 should
1111 // never occur as hi classes, and X87Up must be preceed by X87,
1112 // which is passed in memory.
1113 case Memory:
1114 case X87:
1115 case ComplexX87:
1116 assert(0 && "Invalid classification for hi word.");
1117 break;
1118
1119 case NoClass: break;
1120 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001121 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001122 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001123 ++neededInt;
1124 break;
1125
1126 // X87Up generally doesn't occur here (long double is passed in
1127 // memory), except in situations involving unions.
1128 case X87Up:
1129 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001130 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001131 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001132 ++neededSSE;
1133 break;
1134
1135 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1136 // eightbyte is passed in the upper half of the last used SSE
1137 // register.
1138 case SSEUp:
1139 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001140 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141 break;
1142 }
1143
1144 return getCoerceResult(Ty, ResType, Context);
1145}
1146
Owen Andersona1cf15f2009-07-14 23:10:40 +00001147void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1148 llvm::LLVMContext &VMContext) const {
1149 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1150 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001151
1152 // Keep track of the number of assigned registers.
1153 unsigned freeIntRegs = 6, freeSSERegs = 8;
1154
1155 // If the return value is indirect, then the hidden argument is consuming one
1156 // integer register.
1157 if (FI.getReturnInfo().isIndirect())
1158 --freeIntRegs;
1159
1160 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1161 // get assigned (in left-to-right order) for passing as follows...
1162 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1163 it != ie; ++it) {
1164 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001165 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001166 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001167
1168 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1169 // eightbyte of an argument, the whole argument is passed on the
1170 // stack. If registers have already been assigned for some
1171 // eightbytes of such an argument, the assignments get reverted.
1172 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1173 freeIntRegs -= neededInt;
1174 freeSSERegs -= neededSSE;
1175 } else {
1176 it->info = getIndirectResult(it->type, Context);
1177 }
1178 }
1179}
1180
1181static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1182 QualType Ty,
1183 CodeGenFunction &CGF) {
1184 llvm::Value *overflow_arg_area_p =
1185 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1186 llvm::Value *overflow_arg_area =
1187 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1188
1189 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1190 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1191 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1192 if (Align > 8) {
1193 // Note that we follow the ABI & gcc here, even though the type
1194 // could in theory have an alignment greater than 16. This case
1195 // shouldn't ever matter in practice.
1196
1197 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001198 llvm::Value *Offset =
1199 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001200 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1201 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson0032b272009-08-13 21:57:51 +00001202 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1203 llvm::Value *Mask = llvm::ConstantInt::get(
1204 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001205 overflow_arg_area =
1206 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1207 overflow_arg_area->getType(),
1208 "overflow_arg_area.align");
1209 }
1210
1211 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1212 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1213 llvm::Value *Res =
1214 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001215 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001216
1217 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1218 // l->overflow_arg_area + sizeof(type).
1219 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1220 // an 8 byte boundary.
1221
1222 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001223 llvm::Value *Offset =
1224 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001225 (SizeInBytes + 7) & ~7);
1226 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1227 "overflow_arg_area.next");
1228 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1229
1230 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1231 return Res;
1232}
1233
1234llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1235 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001236 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001237 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1238 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001240 // Assume that va_list type is correct; should be pointer to LLVM type:
1241 // struct {
1242 // i32 gp_offset;
1243 // i32 fp_offset;
1244 // i8* overflow_arg_area;
1245 // i8* reg_save_area;
1246 // };
1247 unsigned neededInt, neededSSE;
Owen Andersona1cf15f2009-07-14 23:10:40 +00001248 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001249 neededInt, neededSSE);
1250
1251 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1252 // in the registers. If not go to step 7.
1253 if (!neededInt && !neededSSE)
1254 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1255
1256 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1257 // general purpose registers needed to pass type and num_fp to hold
1258 // the number of floating point registers needed.
1259
1260 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1261 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1262 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1263 //
1264 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1265 // register save space).
1266
1267 llvm::Value *InRegs = 0;
1268 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1269 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1270 if (neededInt) {
1271 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1272 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1273 InRegs =
1274 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001275 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001276 48 - neededInt * 8),
1277 "fits_in_gp");
1278 }
1279
1280 if (neededSSE) {
1281 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1282 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1283 llvm::Value *FitsInFP =
1284 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001285 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001286 176 - neededSSE * 16),
1287 "fits_in_fp");
1288 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1289 }
1290
1291 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1292 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1293 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1294 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1295
1296 // Emit code to load the value if it was passed in registers.
1297
1298 CGF.EmitBlock(InRegBlock);
1299
1300 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1301 // an offset of l->gp_offset and/or l->fp_offset. This may require
1302 // copying to a temporary location in case the parameter is passed
1303 // in different register classes or requires an alignment greater
1304 // than 8 for general purpose registers and 16 for XMM registers.
1305 //
1306 // FIXME: This really results in shameful code when we end up needing to
1307 // collect arguments from different places; often what should result in a
1308 // simple assembling of a structure from scattered addresses has many more
1309 // loads than necessary. Can we clean this up?
1310 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1311 llvm::Value *RegAddr =
1312 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1313 "reg_save_area");
1314 if (neededInt && neededSSE) {
1315 // FIXME: Cleanup.
1316 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1317 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1318 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1319 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1320 const llvm::Type *TyLo = ST->getElementType(0);
1321 const llvm::Type *TyHi = ST->getElementType(1);
1322 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1323 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001324 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1325 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001326 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1327 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1328 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1329 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1330 llvm::Value *V =
1331 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1332 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1333 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1334 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1335
Owen Andersona1cf15f2009-07-14 23:10:40 +00001336 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001337 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001338 } else if (neededInt) {
1339 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1340 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001341 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001342 } else {
1343 if (neededSSE == 1) {
1344 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1345 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001346 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001347 } else {
1348 assert(neededSSE == 2 && "Invalid number of needed registers!");
1349 // SSE registers are spaced 16 bytes apart in the register save
1350 // area, we need to collect the two eightbytes together.
1351 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1352 llvm::Value *RegAddrHi =
1353 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001354 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001355 const llvm::Type *DblPtrTy =
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001356 llvm::PointerType::getUnqual(DoubleTy);
1357 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1358 DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001359 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1360 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1361 DblPtrTy));
1362 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1363 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1364 DblPtrTy));
1365 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1366 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001367 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001368 }
1369 }
1370
1371 // AMD64-ABI 3.5.7p5: Step 5. Set:
1372 // l->gp_offset = l->gp_offset + num_gp * 8
1373 // l->fp_offset = l->fp_offset + num_fp * 16.
1374 if (neededInt) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001375 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001376 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1377 gp_offset_p);
1378 }
1379 if (neededSSE) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001380 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001381 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1382 fp_offset_p);
1383 }
1384 CGF.EmitBranch(ContBlock);
1385
1386 // Emit code to load the value if it was passed in memory.
1387
1388 CGF.EmitBlock(InMemBlock);
1389 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1390
1391 // Return the appropriate result.
1392
1393 CGF.EmitBlock(ContBlock);
1394 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1395 "vaarg.addr");
1396 ResAddr->reserveOperandSpace(2);
1397 ResAddr->addIncoming(RegAddr, InRegBlock);
1398 ResAddr->addIncoming(MemAddr, InMemBlock);
1399
1400 return ResAddr;
1401}
1402
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001403// PIC16 ABI Implementation
1404
1405namespace {
1406
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001407class PIC16ABIInfo : public ABIInfo {
1408 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001409 ASTContext &Context,
1410 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001411
1412 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001413 ASTContext &Context,
1414 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001415
Owen Andersona1cf15f2009-07-14 23:10:40 +00001416 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1417 llvm::LLVMContext &VMContext) const {
1418 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1419 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001420 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1421 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001422 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001423 }
1424
1425 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1426 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001427};
1428
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001429class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1430public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001431 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001432};
1433
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001434}
1435
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001436ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001437 ASTContext &Context,
1438 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001439 if (RetTy->isVoidType()) {
1440 return ABIArgInfo::getIgnore();
1441 } else {
1442 return ABIArgInfo::getDirect();
1443 }
1444}
1445
1446ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001447 ASTContext &Context,
1448 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001449 return ABIArgInfo::getDirect();
1450}
1451
1452llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1453 CodeGenFunction &CGF) const {
1454 return 0;
1455}
1456
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001457// ARM ABI Implementation
1458
1459namespace {
1460
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001461class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001462public:
1463 enum ABIKind {
1464 APCS = 0,
1465 AAPCS = 1,
1466 AAPCS_VFP
1467 };
1468
1469private:
1470 ABIKind Kind;
1471
1472public:
1473 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1474
1475private:
1476 ABIKind getABIKind() const { return Kind; }
1477
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001478 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001479 ASTContext &Context,
1480 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001481
1482 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001483 ASTContext &Context,
1484 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001485
Owen Andersona1cf15f2009-07-14 23:10:40 +00001486 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1487 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001488
1489 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1490 CodeGenFunction &CGF) const;
1491};
1492
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001493class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1494public:
1495 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001496 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001497};
1498
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001499}
1500
Owen Andersona1cf15f2009-07-14 23:10:40 +00001501void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1502 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001503 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001504 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001505 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1506 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001507 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001508 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001509
1510 // ARM always overrides the calling convention.
1511 switch (getABIKind()) {
1512 case APCS:
1513 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1514 break;
1515
1516 case AAPCS:
1517 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1518 break;
1519
1520 case AAPCS_VFP:
1521 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1522 break;
1523 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001524}
1525
1526ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001527 ASTContext &Context,
1528 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001529 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001530 return (Ty->isPromotableIntegerType() ?
1531 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar98303b92009-09-13 08:03:58 +00001532
Daniel Dunbar42025572009-09-14 21:54:03 +00001533 // Ignore empty records.
1534 if (isEmptyRecord(Context, Ty, true))
1535 return ABIArgInfo::getIgnore();
1536
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001537 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1538 // backend doesn't support byval.
1539 // FIXME: This doesn't handle alignment > 64 bits.
1540 const llvm::Type* ElemTy;
1541 unsigned SizeRegs;
1542 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001543 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001544 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1545 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001546 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1548 }
1549 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001550 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001551 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001552 return ABIArgInfo::getCoerce(STy);
1553}
1554
Daniel Dunbar98303b92009-09-13 08:03:58 +00001555static bool isIntegerLikeType(QualType Ty,
1556 ASTContext &Context,
1557 llvm::LLVMContext &VMContext) {
1558 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1559 // is called integer-like if its size is less than or equal to one word, and
1560 // the offset of each of its addressable sub-fields is zero.
1561
1562 uint64_t Size = Context.getTypeSize(Ty);
1563
1564 // Check that the type fits in a word.
1565 if (Size > 32)
1566 return false;
1567
1568 // FIXME: Handle vector types!
1569 if (Ty->isVectorType())
1570 return false;
1571
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001572 // Float types are never treated as "integer like".
1573 if (Ty->isRealFloatingType())
1574 return false;
1575
Daniel Dunbar98303b92009-09-13 08:03:58 +00001576 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001577 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001578 return true;
1579
1580 // Complex types "should" be ok by the definition above, but they are not.
1581 if (Ty->isAnyComplexType())
1582 return false;
1583
1584 // Single element and zero sized arrays should be allowed, by the definition
1585 // above, but they are not.
1586
1587 // Otherwise, it must be a record type.
1588 const RecordType *RT = Ty->getAs<RecordType>();
1589 if (!RT) return false;
1590
1591 // Ignore records with flexible arrays.
1592 const RecordDecl *RD = RT->getDecl();
1593 if (RD->hasFlexibleArrayMember())
1594 return false;
1595
1596 // Check that all sub-fields are at offset 0, and are themselves "integer
1597 // like".
1598 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1599
1600 bool HadField = false;
1601 unsigned idx = 0;
1602 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1603 i != e; ++i, ++idx) {
1604 const FieldDecl *FD = *i;
1605
1606 // Check if this field is at offset 0.
1607 uint64_t Offset = Layout.getFieldOffset(idx);
1608 if (Offset != 0) {
1609 // Allow padding bit-fields, but only if they are all at the end of the
1610 // structure (despite the wording above, this matches gcc).
1611 if (FD->isBitField() &&
1612 !FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
1613 for (; i != e; ++i)
1614 if (!i->isBitField() ||
1615 i->getBitWidth()->EvaluateAsInt(Context).getZExtValue())
1616 return false;
1617
1618 // All remaining fields are padding, allow this.
1619 return true;
1620 }
1621
1622 return false;
1623 }
1624
1625 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1626 return false;
1627
1628 // Only allow at most one field in a structure. Again this doesn't match the
1629 // wording above, but follows gcc.
1630 if (!RD->isUnion()) {
1631 if (HadField)
1632 return false;
1633
1634 HadField = true;
1635 }
1636 }
1637
1638 return true;
1639}
1640
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001641ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001642 ASTContext &Context,
1643 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001644 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001645 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001646
1647 if (!CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001648 return (RetTy->isPromotableIntegerType() ?
1649 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar98303b92009-09-13 08:03:58 +00001650
1651 // Are we following APCS?
1652 if (getABIKind() == APCS) {
1653 if (isEmptyRecord(Context, RetTy, false))
1654 return ABIArgInfo::getIgnore();
1655
1656 // Integer like structures are returned in r0.
1657 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1658 // Return in the smallest viable integer type.
1659 uint64_t Size = Context.getTypeSize(RetTy);
1660 if (Size <= 8)
1661 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1662 if (Size <= 16)
1663 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1664 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1665 }
1666
1667 // Otherwise return in memory.
1668 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001669 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001670
1671 // Otherwise this is an AAPCS variant.
1672
Daniel Dunbar16a08082009-09-14 00:56:55 +00001673 if (isEmptyRecord(Context, RetTy, true))
1674 return ABIArgInfo::getIgnore();
1675
Daniel Dunbar98303b92009-09-13 08:03:58 +00001676 // Aggregates <= 4 bytes are returned in r0; other aggregates
1677 // are returned indirectly.
1678 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00001679 if (Size <= 32) {
1680 // Return in the smallest viable integer type.
1681 if (Size <= 8)
1682 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1683 if (Size <= 16)
1684 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00001685 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00001686 }
1687
Daniel Dunbar98303b92009-09-13 08:03:58 +00001688 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001689}
1690
1691llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1692 CodeGenFunction &CGF) const {
1693 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001694 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001695 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001696
1697 CGBuilderTy &Builder = CGF.Builder;
1698 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1699 "ap");
1700 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1701 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001702 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001703 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1704
1705 uint64_t Offset =
1706 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1707 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +00001708 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1709 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001710 "ap.next");
1711 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1712
1713 return AddrTyped;
1714}
1715
1716ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001717 ASTContext &Context,
1718 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001719 if (RetTy->isVoidType()) {
1720 return ABIArgInfo::getIgnore();
1721 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1722 return ABIArgInfo::getIndirect(0);
1723 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001724 return (RetTy->isPromotableIntegerType() ?
1725 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001726 }
1727}
1728
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001729// SystemZ ABI Implementation
1730
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001731namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001732
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001733class SystemZABIInfo : public ABIInfo {
1734 bool isPromotableIntegerType(QualType Ty) const;
1735
1736 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1737 llvm::LLVMContext &VMContext) const;
1738
1739 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1740 llvm::LLVMContext &VMContext) const;
1741
1742 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1743 llvm::LLVMContext &VMContext) const {
1744 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1745 Context, VMContext);
1746 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1747 it != ie; ++it)
1748 it->info = classifyArgumentType(it->type, Context, VMContext);
1749 }
1750
1751 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1752 CodeGenFunction &CGF) const;
1753};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001754
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001755class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
1756public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001757 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001758};
1759
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001760}
1761
1762bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1763 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00001764 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001765 switch (BT->getKind()) {
1766 case BuiltinType::Bool:
1767 case BuiltinType::Char_S:
1768 case BuiltinType::Char_U:
1769 case BuiltinType::SChar:
1770 case BuiltinType::UChar:
1771 case BuiltinType::Short:
1772 case BuiltinType::UShort:
1773 case BuiltinType::Int:
1774 case BuiltinType::UInt:
1775 return true;
1776 default:
1777 return false;
1778 }
1779 return false;
1780}
1781
1782llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1783 CodeGenFunction &CGF) const {
1784 // FIXME: Implement
1785 return 0;
1786}
1787
1788
1789ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1790 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001791 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001792 if (RetTy->isVoidType()) {
1793 return ABIArgInfo::getIgnore();
1794 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1795 return ABIArgInfo::getIndirect(0);
1796 } else {
1797 return (isPromotableIntegerType(RetTy) ?
1798 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1799 }
1800}
1801
1802ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1803 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001804 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001805 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1806 return ABIArgInfo::getIndirect(0);
1807 } else {
1808 return (isPromotableIntegerType(Ty) ?
1809 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1810 }
1811}
1812
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001813// MSP430 ABI Implementation
1814
1815namespace {
1816
1817class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
1818public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001819 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001820 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1821 CodeGen::CodeGenModule &M) const;
1822};
1823
1824}
1825
1826void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1827 llvm::GlobalValue *GV,
1828 CodeGen::CodeGenModule &M) const {
1829 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1830 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
1831 // Handle 'interrupt' attribute:
1832 llvm::Function *F = cast<llvm::Function>(GV);
1833
1834 // Step 1: Set ISR calling convention.
1835 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
1836
1837 // Step 2: Add attributes goodness.
1838 F->addFnAttr(llvm::Attribute::NoInline);
1839
1840 // Step 3: Emit ISR vector alias.
1841 unsigned Num = attr->getNumber() + 0xffe0;
1842 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
1843 "vector_" +
1844 llvm::LowercaseString(llvm::utohexstr(Num)),
1845 GV, &M.getModule());
1846 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001847 }
1848}
1849
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001850const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
1851 if (TheTargetCodeGenInfo)
1852 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001853
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001854 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
1855 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001856
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001857 const llvm::Triple &Triple(getContext().Target.getTriple());
1858 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001859 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001860 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001861
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001862 case llvm::Triple::arm:
1863 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001864 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00001865 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001866 return *(TheTargetCodeGenInfo =
1867 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001868
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001869 return *(TheTargetCodeGenInfo =
1870 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001871
1872 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001873 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001874
1875 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001876 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
1877
1878 case llvm::Triple::msp430:
1879 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001880
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001881 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001882 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00001883 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001884 return *(TheTargetCodeGenInfo =
1885 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001886 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001887 case llvm::Triple::MinGW32:
1888 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00001889 case llvm::Triple::AuroraUX:
1890 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00001891 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001892 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001893 return *(TheTargetCodeGenInfo =
1894 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001895
1896 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001897 return *(TheTargetCodeGenInfo =
1898 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001899 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001900
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001901 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001902 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001903 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001904}