blob: f1cbc56dda4bac685a9b3834592071665521c871 [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 {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000274 // Treat an enum type as its underlying type.
275 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
276 Ty = EnumTy->getDecl()->getIntegerType();
277
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000278 return (Ty->isPromotableIntegerType() ?
279 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
280 }
281}
282
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000283/// X86_32ABIInfo - The X86-32 ABI information.
284class X86_32ABIInfo : public ABIInfo {
285 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000286 bool IsDarwinVectorABI;
287 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000288
289 static bool isRegisterSize(unsigned Size) {
290 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
291 }
292
293 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
294
Eli Friedmana1e6de92009-06-13 21:37:10 +0000295 static unsigned getIndirectArgumentAlignment(QualType Ty,
296 ASTContext &Context);
297
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000298public:
299 ABIArgInfo classifyReturnType(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
303 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000304 ASTContext &Context,
305 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000306
Owen Andersona1cf15f2009-07-14 23:10:40 +0000307 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
308 llvm::LLVMContext &VMContext) const {
309 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
310 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000311 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
312 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000313 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000314 }
315
316 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
317 CodeGenFunction &CGF) const;
318
David Chisnall1e4249c2009-08-17 23:08:21 +0000319 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000320 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000321 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000322};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000323
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000324class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
325public:
326 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000327 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000328
329 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
330 CodeGen::CodeGenModule &CGM) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000331};
332
333}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000334
335/// shouldReturnTypeInRegister - Determine if the given type should be
336/// passed in a register (for the Darwin ABI).
337bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
338 ASTContext &Context) {
339 uint64_t Size = Context.getTypeSize(Ty);
340
341 // Type must be register sized.
342 if (!isRegisterSize(Size))
343 return false;
344
345 if (Ty->isVectorType()) {
346 // 64- and 128- bit vectors inside structures are not returned in
347 // registers.
348 if (Size == 64 || Size == 128)
349 return false;
350
351 return true;
352 }
353
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000354 // If this is a builtin, pointer, enum, or complex type, it is ok.
355 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
356 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
357 Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000358 return true;
359
360 // Arrays are treated like records.
361 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
362 return shouldReturnTypeInRegister(AT->getElementType(), Context);
363
364 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000365 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000366 if (!RT) return false;
367
Anders Carlssona8874232010-01-27 03:25:19 +0000368 // FIXME: Traverse bases here too.
369
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000370 // Structure types are passed in register if all fields would be
371 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000372 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
373 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000374 const FieldDecl *FD = *i;
375
376 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000377 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000378 continue;
379
380 // Check fields recursively.
381 if (!shouldReturnTypeInRegister(FD->getType(), Context))
382 return false;
383 }
384
385 return true;
386}
387
388ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000389 ASTContext &Context,
390 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000391 if (RetTy->isVoidType()) {
392 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000393 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000394 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000395 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000396 uint64_t Size = Context.getTypeSize(RetTy);
397
398 // 128-bit vectors are a special case; they are returned in
399 // registers and we need to make sure to pick a type the LLVM
400 // backend will like.
401 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000402 return ABIArgInfo::getCoerce(llvm::VectorType::get(
403 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000404
405 // Always return in register if it fits in a general purpose
406 // register, or if it is 64 bits and has a single element.
407 if ((Size == 8 || Size == 16 || Size == 32) ||
408 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000409 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000410
411 return ABIArgInfo::getIndirect(0);
412 }
413
414 return ABIArgInfo::getDirect();
415 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000416 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000417 // Structures with either a non-trivial destructor or a non-trivial
418 // copy constructor are always indirect.
419 if (hasNonTrivialDestructorOrCopyConstructor(RT))
420 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
421
422 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000423 if (RT->getDecl()->hasFlexibleArrayMember())
424 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000425 }
426
David Chisnall1e4249c2009-08-17 23:08:21 +0000427 // If specified, structs and unions are always indirect.
428 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000429 return ABIArgInfo::getIndirect(0);
430
431 // Classify "single element" structs as their element type.
432 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000433 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000434 if (BT->isIntegerType()) {
435 // We need to use the size of the structure, padding
436 // bit-fields can adjust that to be larger than the single
437 // element type.
438 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000439 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000440 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000441 } else if (BT->getKind() == BuiltinType::Float) {
442 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
443 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000444 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000445 } else if (BT->getKind() == BuiltinType::Double) {
446 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
447 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000448 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000449 }
450 } else if (SeltTy->isPointerType()) {
451 // FIXME: It would be really nice if this could come out as the proper
452 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000453 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000454 return ABIArgInfo::getCoerce(PtrTy);
455 } else if (SeltTy->isVectorType()) {
456 // 64- and 128-bit vectors are never returned in a
457 // register when inside a structure.
458 uint64_t Size = Context.getTypeSize(RetTy);
459 if (Size == 64 || Size == 128)
460 return ABIArgInfo::getIndirect(0);
461
Owen Andersona1cf15f2009-07-14 23:10:40 +0000462 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000463 }
464 }
465
466 // Small structures which are register sized are generally returned
467 // in a register.
468 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
469 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000470 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000471 }
472
473 return ABIArgInfo::getIndirect(0);
474 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000475 // Treat an enum type as its underlying type.
476 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
477 RetTy = EnumTy->getDecl()->getIntegerType();
478
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000479 return (RetTy->isPromotableIntegerType() ?
480 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000481 }
482}
483
Eli Friedmana1e6de92009-06-13 21:37:10 +0000484unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
485 ASTContext &Context) {
486 unsigned Align = Context.getTypeAlign(Ty);
487 if (Align < 128) return 0;
Ted Kremenek6217b802009-07-29 21:53:49 +0000488 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000489 if (typeContainsSSEVector(RT->getDecl(), Context))
490 return 16;
491 return 0;
492}
493
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000495 ASTContext &Context,
496 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000497 // FIXME: Set alignment on indirect arguments.
498 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
499 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000500 if (const RecordType *RT = Ty->getAs<RecordType>()) {
501 // Structures with either a non-trivial destructor or a non-trivial
502 // copy constructor are always indirect.
503 if (hasNonTrivialDestructorOrCopyConstructor(RT))
504 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
505
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000506 if (RT->getDecl()->hasFlexibleArrayMember())
Mike Stump1eb44332009-09-09 15:08:12 +0000507 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
Eli Friedmana1e6de92009-06-13 21:37:10 +0000508 Context));
Anders Carlssona8874232010-01-27 03:25:19 +0000509 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000510
511 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000512 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000513 return ABIArgInfo::getIgnore();
514
Daniel Dunbar53012f42009-11-09 01:33:53 +0000515 // Expand small (<= 128-bit) record types when we know that the stack layout
516 // of those arguments will match the struct. This is important because the
517 // LLVM backend isn't smart enough to remove byval, which inhibits many
518 // optimizations.
519 if (Context.getTypeSize(Ty) <= 4*32 &&
520 canExpandIndirectArgument(Ty, Context))
521 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000522
Eli Friedmana1e6de92009-06-13 21:37:10 +0000523 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000524 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000525 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
526 Ty = EnumTy->getDecl()->getIntegerType();
527
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000528 return (Ty->isPromotableIntegerType() ?
529 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000530 }
531}
532
533llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
534 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000535 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000536 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537
538 CGBuilderTy &Builder = CGF.Builder;
539 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
540 "ap");
541 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
542 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000543 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000544 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
545
546 uint64_t Offset =
547 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
548 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +0000549 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
550 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000551 "ap.next");
552 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
553
554 return AddrTyped;
555}
556
Charles Davis74f72932010-02-13 15:54:06 +0000557void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
558 llvm::GlobalValue *GV,
559 CodeGen::CodeGenModule &CGM) const {
560 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
561 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
562 // Get the LLVM function.
563 llvm::Function *Fn = cast<llvm::Function>(GV);
564
565 // Now add the 'alignstack' attribute with a value of 16.
566 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
567 }
568 }
569}
570
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571namespace {
572/// X86_64ABIInfo - The X86_64 ABI information.
573class X86_64ABIInfo : public ABIInfo {
574 enum Class {
575 Integer = 0,
576 SSE,
577 SSEUp,
578 X87,
579 X87Up,
580 ComplexX87,
581 NoClass,
582 Memory
583 };
584
585 /// merge - Implement the X86_64 ABI merging algorithm.
586 ///
587 /// Merge an accumulating classification \arg Accum with a field
588 /// classification \arg Field.
589 ///
590 /// \param Accum - The accumulating classification. This should
591 /// always be either NoClass or the result of a previous merge
592 /// call. In addition, this should never be Memory (the caller
593 /// should just return Memory for the aggregate).
594 Class merge(Class Accum, Class Field) const;
595
596 /// classify - Determine the x86_64 register classes in which the
597 /// given type T should be passed.
598 ///
599 /// \param Lo - The classification for the parts of the type
600 /// residing in the low word of the containing object.
601 ///
602 /// \param Hi - The classification for the parts of the type
603 /// residing in the high word of the containing object.
604 ///
605 /// \param OffsetBase - The bit offset of this type in the
606 /// containing object. Some parameters are classified different
607 /// depending on whether they straddle an eightbyte boundary.
608 ///
609 /// If a word is unused its result will be NoClass; if a type should
610 /// be passed in Memory then at least the classification of \arg Lo
611 /// will be Memory.
612 ///
613 /// The \arg Lo class will be NoClass iff the argument is ignored.
614 ///
615 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
616 /// also be ComplexX87.
617 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
618 Class &Lo, Class &Hi) const;
619
620 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
621 /// to coerce to, chose the best way to pass Ty in the same place
622 /// that \arg CoerceTo would be passed, but while keeping the
623 /// emitted code as simple as possible.
624 ///
625 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
626 /// the ways we might want to pass things, instead of constructing an LLVM
627 /// type. This makes this code more explicit, and it makes it clearer that we
628 /// are also doing this for correctness in the case of passing scalar types.
629 ABIArgInfo getCoerceResult(QualType Ty,
630 const llvm::Type *CoerceTo,
631 ASTContext &Context) const;
632
633 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
634 /// such that the argument will be passed in memory.
635 ABIArgInfo getIndirectResult(QualType Ty,
636 ASTContext &Context) const;
637
638 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000639 ASTContext &Context,
640 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000641
642 ABIArgInfo classifyArgumentType(QualType Ty,
643 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000644 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645 unsigned &neededInt,
646 unsigned &neededSSE) const;
647
648public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000649 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
650 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000651
652 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
653 CodeGenFunction &CGF) const;
654};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000655
656class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
657public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000658 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000659};
660
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000661}
662
663X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
664 Class Field) const {
665 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
666 // classified recursively so that always two fields are
667 // considered. The resulting class is calculated according to
668 // the classes of the fields in the eightbyte:
669 //
670 // (a) If both classes are equal, this is the resulting class.
671 //
672 // (b) If one of the classes is NO_CLASS, the resulting class is
673 // the other class.
674 //
675 // (c) If one of the classes is MEMORY, the result is the MEMORY
676 // class.
677 //
678 // (d) If one of the classes is INTEGER, the result is the
679 // INTEGER.
680 //
681 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
682 // MEMORY is used as class.
683 //
684 // (f) Otherwise class SSE is used.
685
686 // Accum should never be memory (we should have returned) or
687 // ComplexX87 (because this cannot be passed in a structure).
688 assert((Accum != Memory && Accum != ComplexX87) &&
689 "Invalid accumulated classification during merge.");
690 if (Accum == Field || Field == NoClass)
691 return Accum;
692 else if (Field == Memory)
693 return Memory;
694 else if (Accum == NoClass)
695 return Field;
696 else if (Accum == Integer || Field == Integer)
697 return Integer;
698 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
699 Accum == X87 || Accum == X87Up)
700 return Memory;
701 else
702 return SSE;
703}
704
705void X86_64ABIInfo::classify(QualType Ty,
706 ASTContext &Context,
707 uint64_t OffsetBase,
708 Class &Lo, Class &Hi) const {
709 // FIXME: This code can be simplified by introducing a simple value class for
710 // Class pairs with appropriate constructor methods for the various
711 // situations.
712
713 // FIXME: Some of the split computations are wrong; unaligned vectors
714 // shouldn't be passed in registers for example, so there is no chance they
715 // can straddle an eightbyte. Verify & simplify.
716
717 Lo = Hi = NoClass;
718
719 Class &Current = OffsetBase < 64 ? Lo : Hi;
720 Current = Memory;
721
John McCall183700f2009-09-21 23:43:11 +0000722 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000723 BuiltinType::Kind k = BT->getKind();
724
725 if (k == BuiltinType::Void) {
726 Current = NoClass;
727 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
728 Lo = Integer;
729 Hi = Integer;
730 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
731 Current = Integer;
732 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
733 Current = SSE;
734 } else if (k == BuiltinType::LongDouble) {
735 Lo = X87;
736 Hi = X87Up;
737 }
738 // FIXME: _Decimal32 and _Decimal64 are SSE.
739 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall183700f2009-09-21 23:43:11 +0000740 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000741 // Classify the underlying integer type.
742 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
743 } else if (Ty->hasPointerRepresentation()) {
744 Current = Integer;
John McCall183700f2009-09-21 23:43:11 +0000745 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000746 uint64_t Size = Context.getTypeSize(VT);
747 if (Size == 32) {
748 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
749 // float> as integer.
750 Current = Integer;
751
752 // If this type crosses an eightbyte boundary, it should be
753 // split.
754 uint64_t EB_Real = (OffsetBase) / 64;
755 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
756 if (EB_Real != EB_Imag)
757 Hi = Lo;
758 } else if (Size == 64) {
759 // gcc passes <1 x double> in memory. :(
760 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
761 return;
762
763 // gcc passes <1 x long long> as INTEGER.
764 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
765 Current = Integer;
766 else
767 Current = SSE;
768
769 // If this type crosses an eightbyte boundary, it should be
770 // split.
771 if (OffsetBase && OffsetBase != 64)
772 Hi = Lo;
773 } else if (Size == 128) {
774 Lo = SSE;
775 Hi = SSEUp;
776 }
John McCall183700f2009-09-21 23:43:11 +0000777 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000778 QualType ET = Context.getCanonicalType(CT->getElementType());
779
780 uint64_t Size = Context.getTypeSize(Ty);
781 if (ET->isIntegralType()) {
782 if (Size <= 64)
783 Current = Integer;
784 else if (Size <= 128)
785 Lo = Hi = Integer;
786 } else if (ET == Context.FloatTy)
787 Current = SSE;
788 else if (ET == Context.DoubleTy)
789 Lo = Hi = SSE;
790 else if (ET == Context.LongDoubleTy)
791 Current = ComplexX87;
792
793 // If this complex type crosses an eightbyte boundary then it
794 // should be split.
795 uint64_t EB_Real = (OffsetBase) / 64;
796 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
797 if (Hi == NoClass && EB_Real != EB_Imag)
798 Hi = Lo;
799 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
800 // Arrays are treated like structures.
801
802 uint64_t Size = Context.getTypeSize(Ty);
803
804 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
805 // than two eightbytes, ..., it has class MEMORY.
806 if (Size > 128)
807 return;
808
809 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
810 // fields, it has class MEMORY.
811 //
812 // Only need to check alignment of array base.
813 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
814 return;
815
816 // Otherwise implement simplified merge. We could be smarter about
817 // this, but it isn't worth it and would be harder to verify.
818 Current = NoClass;
819 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
820 uint64_t ArraySize = AT->getSize().getZExtValue();
821 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
822 Class FieldLo, FieldHi;
823 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
824 Lo = merge(Lo, FieldLo);
825 Hi = merge(Hi, FieldHi);
826 if (Lo == Memory || Hi == Memory)
827 break;
828 }
829
830 // Do post merger cleanup (see below). Only case we worry about is Memory.
831 if (Hi == Memory)
832 Lo = Memory;
833 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenek6217b802009-07-29 21:53:49 +0000834 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000835 uint64_t Size = Context.getTypeSize(Ty);
836
837 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
838 // than two eightbytes, ..., it has class MEMORY.
839 if (Size > 128)
840 return;
841
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000842 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
843 // copy constructor or a non-trivial destructor, it is passed by invisible
844 // reference.
845 if (hasNonTrivialDestructorOrCopyConstructor(RT))
846 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000847
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000848 const RecordDecl *RD = RT->getDecl();
849
850 // Assume variable sized types are passed in memory.
851 if (RD->hasFlexibleArrayMember())
852 return;
853
854 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
855
856 // Reset Lo class, this will be recomputed.
857 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000858
859 // If this is a C++ record, classify the bases first.
860 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
861 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
862 e = CXXRD->bases_end(); i != e; ++i) {
863 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
864 "Unexpected base class!");
865 const CXXRecordDecl *Base =
866 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
867
868 // Classify this field.
869 //
870 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
871 // single eightbyte, each is classified separately. Each eightbyte gets
872 // initialized to class NO_CLASS.
873 Class FieldLo, FieldHi;
874 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
875 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
876 Lo = merge(Lo, FieldLo);
877 Hi = merge(Hi, FieldHi);
878 if (Lo == Memory || Hi == Memory)
879 break;
880 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +0000881
882 // If this record has no fields but isn't empty, classify as INTEGER.
883 if (RD->field_empty() && Size)
884 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000885 }
886
887 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000888 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000889 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
890 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000891 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
892 bool BitField = i->isBitField();
893
894 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
895 // fields, it has class MEMORY.
896 //
897 // Note, skip this test for bit-fields, see below.
898 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
899 Lo = Memory;
900 return;
901 }
902
903 // Classify this field.
904 //
905 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
906 // exceeds a single eightbyte, each is classified
907 // separately. Each eightbyte gets initialized to class
908 // NO_CLASS.
909 Class FieldLo, FieldHi;
910
911 // Bit-fields require special handling, they do not force the
912 // structure to be passed in memory even if unaligned, and
913 // therefore they can straddle an eightbyte.
914 if (BitField) {
915 // Ignore padding bit-fields.
916 if (i->isUnnamedBitfield())
917 continue;
918
919 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
920 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
921
922 uint64_t EB_Lo = Offset / 64;
923 uint64_t EB_Hi = (Offset + Size - 1) / 64;
924 FieldLo = FieldHi = NoClass;
925 if (EB_Lo) {
926 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
927 FieldLo = NoClass;
928 FieldHi = Integer;
929 } else {
930 FieldLo = Integer;
931 FieldHi = EB_Hi ? Integer : NoClass;
932 }
933 } else
934 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
935 Lo = merge(Lo, FieldLo);
936 Hi = merge(Hi, FieldHi);
937 if (Lo == Memory || Hi == Memory)
938 break;
939 }
940
941 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
942 //
943 // (a) If one of the classes is MEMORY, the whole argument is
944 // passed in memory.
945 //
946 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
947
948 // The first of these conditions is guaranteed by how we implement
949 // the merge (just bail).
950 //
951 // The second condition occurs in the case of unions; for example
952 // union { _Complex double; unsigned; }.
953 if (Hi == Memory)
954 Lo = Memory;
955 if (Hi == SSEUp && Lo != SSE)
956 Hi = SSE;
957 }
958}
959
960ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
961 const llvm::Type *CoerceTo,
962 ASTContext &Context) const {
Owen Anderson0032b272009-08-13 21:57:51 +0000963 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000964 // Integer and pointer types will end up in a general purpose
965 // register.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000966
967 // Treat an enum type as its underlying type.
968 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
969 Ty = EnumTy->getDecl()->getIntegerType();
970
Anders Carlsson5b3a2fc2009-09-26 03:56:53 +0000971 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000972 return (Ty->isPromotableIntegerType() ?
973 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson0032b272009-08-13 21:57:51 +0000974 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000975 // FIXME: It would probably be better to make CGFunctionInfo only map using
976 // canonical types than to canonize here.
977 QualType CTy = Context.getCanonicalType(Ty);
978
979 // Float and double end up in a single SSE reg.
980 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
981 return ABIArgInfo::getDirect();
982
983 }
984
985 return ABIArgInfo::getCoerce(CoerceTo);
986}
987
988ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
989 ASTContext &Context) const {
990 // If this is a scalar LLVM value then assume LLVM will pass it in the right
991 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000992 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
993 // Treat an enum type as its underlying type.
994 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
995 Ty = EnumTy->getDecl()->getIntegerType();
996
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000997 return (Ty->isPromotableIntegerType() ?
998 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000999 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001000
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001001 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
1002
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001003 // FIXME: Set alignment correctly.
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001004 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001005}
1006
1007ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001008 ASTContext &Context,
1009 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001010 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1011 // classification algorithm.
1012 X86_64ABIInfo::Class Lo, Hi;
1013 classify(RetTy, Context, 0, Lo, Hi);
1014
1015 // Check some invariants.
1016 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1017 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1018 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1019
1020 const llvm::Type *ResType = 0;
1021 switch (Lo) {
1022 case NoClass:
1023 return ABIArgInfo::getIgnore();
1024
1025 case SSEUp:
1026 case X87Up:
1027 assert(0 && "Invalid classification for lo word.");
1028
1029 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1030 // hidden argument.
1031 case Memory:
1032 return getIndirectResult(RetTy, Context);
1033
1034 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1035 // available register of the sequence %rax, %rdx is used.
1036 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001037 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001038
1039 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1040 // available SSE register of the sequence %xmm0, %xmm1 is used.
1041 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001042 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001043
1044 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1045 // returned on the X87 stack in %st0 as 80-bit x87 number.
1046 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001047 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001048
1049 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1050 // part of the value is returned in %st0 and the imaginary part in
1051 // %st1.
1052 case ComplexX87:
1053 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001054 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
1055 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001056 NULL);
1057 break;
1058 }
1059
1060 switch (Hi) {
1061 // Memory was handled previously and X87 should
1062 // never occur as a hi class.
1063 case Memory:
1064 case X87:
1065 assert(0 && "Invalid classification for hi word.");
1066
1067 case ComplexX87: // Previously handled.
1068 case NoClass: break;
1069
1070 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001071 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001072 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001073 break;
1074 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001075 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001076 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001077 break;
1078
1079 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1080 // is passed in the upper half of the last used SSE register.
1081 //
1082 // SSEUP should always be preceeded by SSE, just widen.
1083 case SSEUp:
1084 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001085 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001086 break;
1087
1088 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1089 // returned together with the previous X87 value in %st0.
1090 case X87Up:
1091 // If X87Up is preceeded by X87, we don't need to do
1092 // anything. However, in some cases with unions it may not be
1093 // preceeded by X87. In such situations we follow gcc and pass the
1094 // extra bits in an SSE reg.
1095 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001096 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001097 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001098 break;
1099 }
1100
1101 return getCoerceResult(RetTy, ResType, Context);
1102}
1103
1104ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001105 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001106 unsigned &neededInt,
1107 unsigned &neededSSE) const {
1108 X86_64ABIInfo::Class Lo, Hi;
1109 classify(Ty, Context, 0, Lo, Hi);
1110
1111 // Check some invariants.
1112 // FIXME: Enforce these by construction.
1113 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1114 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1115 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1116
1117 neededInt = 0;
1118 neededSSE = 0;
1119 const llvm::Type *ResType = 0;
1120 switch (Lo) {
1121 case NoClass:
1122 return ABIArgInfo::getIgnore();
1123
1124 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1125 // on the stack.
1126 case Memory:
1127
1128 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1129 // COMPLEX_X87, it is passed in memory.
1130 case X87:
1131 case ComplexX87:
1132 return getIndirectResult(Ty, Context);
1133
1134 case SSEUp:
1135 case X87Up:
1136 assert(0 && "Invalid classification for lo word.");
1137
1138 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1139 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1140 // and %r9 is used.
1141 case Integer:
1142 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001143 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001144 break;
1145
1146 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1147 // available SSE register is used, the registers are taken in the
1148 // order from %xmm0 to %xmm7.
1149 case SSE:
1150 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001151 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001152 break;
1153 }
1154
1155 switch (Hi) {
1156 // Memory was handled previously, ComplexX87 and X87 should
1157 // never occur as hi classes, and X87Up must be preceed by X87,
1158 // which is passed in memory.
1159 case Memory:
1160 case X87:
1161 case ComplexX87:
1162 assert(0 && "Invalid classification for hi word.");
1163 break;
1164
1165 case NoClass: break;
1166 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001167 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001168 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001169 ++neededInt;
1170 break;
1171
1172 // X87Up generally doesn't occur here (long double is passed in
1173 // memory), except in situations involving unions.
1174 case X87Up:
1175 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001176 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001177 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001178 ++neededSSE;
1179 break;
1180
1181 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1182 // eightbyte is passed in the upper half of the last used SSE
1183 // register.
1184 case SSEUp:
1185 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001186 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001187 break;
1188 }
1189
1190 return getCoerceResult(Ty, ResType, Context);
1191}
1192
Owen Andersona1cf15f2009-07-14 23:10:40 +00001193void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1194 llvm::LLVMContext &VMContext) const {
1195 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1196 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001197
1198 // Keep track of the number of assigned registers.
1199 unsigned freeIntRegs = 6, freeSSERegs = 8;
1200
1201 // If the return value is indirect, then the hidden argument is consuming one
1202 // integer register.
1203 if (FI.getReturnInfo().isIndirect())
1204 --freeIntRegs;
1205
1206 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1207 // get assigned (in left-to-right order) for passing as follows...
1208 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1209 it != ie; ++it) {
1210 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001211 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001212 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001213
1214 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1215 // eightbyte of an argument, the whole argument is passed on the
1216 // stack. If registers have already been assigned for some
1217 // eightbytes of such an argument, the assignments get reverted.
1218 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1219 freeIntRegs -= neededInt;
1220 freeSSERegs -= neededSSE;
1221 } else {
1222 it->info = getIndirectResult(it->type, Context);
1223 }
1224 }
1225}
1226
1227static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1228 QualType Ty,
1229 CodeGenFunction &CGF) {
1230 llvm::Value *overflow_arg_area_p =
1231 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1232 llvm::Value *overflow_arg_area =
1233 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1234
1235 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1236 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1237 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1238 if (Align > 8) {
1239 // Note that we follow the ABI & gcc here, even though the type
1240 // could in theory have an alignment greater than 16. This case
1241 // shouldn't ever matter in practice.
1242
1243 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001244 llvm::Value *Offset =
1245 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001246 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1247 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson0032b272009-08-13 21:57:51 +00001248 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1249 llvm::Value *Mask = llvm::ConstantInt::get(
1250 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001251 overflow_arg_area =
1252 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1253 overflow_arg_area->getType(),
1254 "overflow_arg_area.align");
1255 }
1256
1257 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1258 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1259 llvm::Value *Res =
1260 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001261 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001262
1263 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1264 // l->overflow_arg_area + sizeof(type).
1265 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1266 // an 8 byte boundary.
1267
1268 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001269 llvm::Value *Offset =
1270 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001271 (SizeInBytes + 7) & ~7);
1272 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1273 "overflow_arg_area.next");
1274 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1275
1276 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1277 return Res;
1278}
1279
1280llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1281 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001282 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001283 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1284 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001286 // Assume that va_list type is correct; should be pointer to LLVM type:
1287 // struct {
1288 // i32 gp_offset;
1289 // i32 fp_offset;
1290 // i8* overflow_arg_area;
1291 // i8* reg_save_area;
1292 // };
1293 unsigned neededInt, neededSSE;
Owen Andersona1cf15f2009-07-14 23:10:40 +00001294 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001295 neededInt, neededSSE);
1296
1297 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1298 // in the registers. If not go to step 7.
1299 if (!neededInt && !neededSSE)
1300 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1301
1302 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1303 // general purpose registers needed to pass type and num_fp to hold
1304 // the number of floating point registers needed.
1305
1306 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1307 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1308 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1309 //
1310 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1311 // register save space).
1312
1313 llvm::Value *InRegs = 0;
1314 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1315 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1316 if (neededInt) {
1317 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1318 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1319 InRegs =
1320 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001321 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001322 48 - neededInt * 8),
1323 "fits_in_gp");
1324 }
1325
1326 if (neededSSE) {
1327 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1328 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1329 llvm::Value *FitsInFP =
1330 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001331 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001332 176 - neededSSE * 16),
1333 "fits_in_fp");
1334 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1335 }
1336
1337 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1338 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1339 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1340 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1341
1342 // Emit code to load the value if it was passed in registers.
1343
1344 CGF.EmitBlock(InRegBlock);
1345
1346 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1347 // an offset of l->gp_offset and/or l->fp_offset. This may require
1348 // copying to a temporary location in case the parameter is passed
1349 // in different register classes or requires an alignment greater
1350 // than 8 for general purpose registers and 16 for XMM registers.
1351 //
1352 // FIXME: This really results in shameful code when we end up needing to
1353 // collect arguments from different places; often what should result in a
1354 // simple assembling of a structure from scattered addresses has many more
1355 // loads than necessary. Can we clean this up?
1356 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1357 llvm::Value *RegAddr =
1358 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1359 "reg_save_area");
1360 if (neededInt && neededSSE) {
1361 // FIXME: Cleanup.
1362 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1363 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1364 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1365 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1366 const llvm::Type *TyLo = ST->getElementType(0);
1367 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001368 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001369 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001370 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1371 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001372 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1373 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001374 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1375 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001376 llvm::Value *V =
1377 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1378 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1379 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1380 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1381
Owen Andersona1cf15f2009-07-14 23:10:40 +00001382 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001383 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001384 } else if (neededInt) {
1385 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1386 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001387 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001388 } else {
1389 if (neededSSE == 1) {
1390 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1391 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001392 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001393 } else {
1394 assert(neededSSE == 2 && "Invalid number of needed registers!");
1395 // SSE registers are spaced 16 bytes apart in the register save
1396 // area, we need to collect the two eightbytes together.
1397 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1398 llvm::Value *RegAddrHi =
1399 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001400 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001401 const llvm::Type *DblPtrTy =
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001402 llvm::PointerType::getUnqual(DoubleTy);
1403 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1404 DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001405 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1406 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1407 DblPtrTy));
1408 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1409 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1410 DblPtrTy));
1411 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1412 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001413 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001414 }
1415 }
1416
1417 // AMD64-ABI 3.5.7p5: Step 5. Set:
1418 // l->gp_offset = l->gp_offset + num_gp * 8
1419 // l->fp_offset = l->fp_offset + num_fp * 16.
1420 if (neededInt) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001421 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001422 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1423 gp_offset_p);
1424 }
1425 if (neededSSE) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001426 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001427 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1428 fp_offset_p);
1429 }
1430 CGF.EmitBranch(ContBlock);
1431
1432 // Emit code to load the value if it was passed in memory.
1433
1434 CGF.EmitBlock(InMemBlock);
1435 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1436
1437 // Return the appropriate result.
1438
1439 CGF.EmitBlock(ContBlock);
1440 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1441 "vaarg.addr");
1442 ResAddr->reserveOperandSpace(2);
1443 ResAddr->addIncoming(RegAddr, InRegBlock);
1444 ResAddr->addIncoming(MemAddr, InMemBlock);
1445
1446 return ResAddr;
1447}
1448
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001449// PIC16 ABI Implementation
1450
1451namespace {
1452
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001453class PIC16ABIInfo : public ABIInfo {
1454 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001455 ASTContext &Context,
1456 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001457
1458 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001459 ASTContext &Context,
1460 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001461
Owen Andersona1cf15f2009-07-14 23:10:40 +00001462 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1463 llvm::LLVMContext &VMContext) const {
1464 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1465 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001466 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1467 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001468 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001469 }
1470
1471 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1472 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001473};
1474
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001475class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1476public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001477 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001478};
1479
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001480}
1481
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482ABIArgInfo PIC16ABIInfo::classifyReturnType(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 if (RetTy->isVoidType()) {
1486 return ABIArgInfo::getIgnore();
1487 } else {
1488 return ABIArgInfo::getDirect();
1489 }
1490}
1491
1492ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001493 ASTContext &Context,
1494 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001495 return ABIArgInfo::getDirect();
1496}
1497
1498llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1499 CodeGenFunction &CGF) const {
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001500 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext()));
1501 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1502
1503 CGBuilderTy &Builder = CGF.Builder;
1504 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1505 "ap");
1506 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1507 llvm::Type *PTy =
1508 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1509 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1510
1511 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1512
1513 llvm::Value *NextAddr =
1514 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1515 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1516 "ap.next");
1517 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1518
1519 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001520}
1521
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001522
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001523// ARM ABI Implementation
1524
1525namespace {
1526
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001527class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001528public:
1529 enum ABIKind {
1530 APCS = 0,
1531 AAPCS = 1,
1532 AAPCS_VFP
1533 };
1534
1535private:
1536 ABIKind Kind;
1537
1538public:
1539 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1540
1541private:
1542 ABIKind getABIKind() const { return Kind; }
1543
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001544 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001545 ASTContext &Context,
1546 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547
1548 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001549 ASTContext &Context,
1550 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001551
Owen Andersona1cf15f2009-07-14 23:10:40 +00001552 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1553 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001554
1555 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1556 CodeGenFunction &CGF) const;
1557};
1558
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001559class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1560public:
1561 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001562 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001563};
1564
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001565}
1566
Owen Andersona1cf15f2009-07-14 23:10:40 +00001567void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1568 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001569 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001570 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001571 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1572 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001573 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001574 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001575
1576 // ARM always overrides the calling convention.
1577 switch (getABIKind()) {
1578 case APCS:
1579 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1580 break;
1581
1582 case AAPCS:
1583 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1584 break;
1585
1586 case AAPCS_VFP:
1587 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1588 break;
1589 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001590}
1591
1592ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001593 ASTContext &Context,
1594 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001595 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1596 // Treat an enum type as its underlying type.
1597 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1598 Ty = EnumTy->getDecl()->getIntegerType();
1599
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001600 return (Ty->isPromotableIntegerType() ?
1601 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001602 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001603
Daniel Dunbar42025572009-09-14 21:54:03 +00001604 // Ignore empty records.
1605 if (isEmptyRecord(Context, Ty, true))
1606 return ABIArgInfo::getIgnore();
1607
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001608 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1609 // backend doesn't support byval.
1610 // FIXME: This doesn't handle alignment > 64 bits.
1611 const llvm::Type* ElemTy;
1612 unsigned SizeRegs;
1613 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001614 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001615 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1616 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001617 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001618 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1619 }
1620 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001621 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001622 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001623 return ABIArgInfo::getCoerce(STy);
1624}
1625
Daniel Dunbar98303b92009-09-13 08:03:58 +00001626static bool isIntegerLikeType(QualType Ty,
1627 ASTContext &Context,
1628 llvm::LLVMContext &VMContext) {
1629 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1630 // is called integer-like if its size is less than or equal to one word, and
1631 // the offset of each of its addressable sub-fields is zero.
1632
1633 uint64_t Size = Context.getTypeSize(Ty);
1634
1635 // Check that the type fits in a word.
1636 if (Size > 32)
1637 return false;
1638
1639 // FIXME: Handle vector types!
1640 if (Ty->isVectorType())
1641 return false;
1642
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001643 // Float types are never treated as "integer like".
1644 if (Ty->isRealFloatingType())
1645 return false;
1646
Daniel Dunbar98303b92009-09-13 08:03:58 +00001647 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001648 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001649 return true;
1650
Daniel Dunbar45815812010-02-01 23:31:26 +00001651 // Small complex integer types are "integer like".
1652 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1653 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001654
1655 // Single element and zero sized arrays should be allowed, by the definition
1656 // above, but they are not.
1657
1658 // Otherwise, it must be a record type.
1659 const RecordType *RT = Ty->getAs<RecordType>();
1660 if (!RT) return false;
1661
1662 // Ignore records with flexible arrays.
1663 const RecordDecl *RD = RT->getDecl();
1664 if (RD->hasFlexibleArrayMember())
1665 return false;
1666
1667 // Check that all sub-fields are at offset 0, and are themselves "integer
1668 // like".
1669 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1670
1671 bool HadField = false;
1672 unsigned idx = 0;
1673 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1674 i != e; ++i, ++idx) {
1675 const FieldDecl *FD = *i;
1676
Daniel Dunbar679855a2010-01-29 03:22:29 +00001677 // Bit-fields are not addressable, we only need to verify they are "integer
1678 // like". We still have to disallow a subsequent non-bitfield, for example:
1679 // struct { int : 0; int x }
1680 // is non-integer like according to gcc.
1681 if (FD->isBitField()) {
1682 if (!RD->isUnion())
1683 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001684
Daniel Dunbar679855a2010-01-29 03:22:29 +00001685 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1686 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001687
Daniel Dunbar679855a2010-01-29 03:22:29 +00001688 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001689 }
1690
Daniel Dunbar679855a2010-01-29 03:22:29 +00001691 // Check if this field is at offset 0.
1692 if (Layout.getFieldOffset(idx) != 0)
1693 return false;
1694
Daniel Dunbar98303b92009-09-13 08:03:58 +00001695 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1696 return false;
1697
Daniel Dunbar679855a2010-01-29 03:22:29 +00001698 // Only allow at most one field in a structure. This doesn't match the
1699 // wording above, but follows gcc in situations with a field following an
1700 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001701 if (!RD->isUnion()) {
1702 if (HadField)
1703 return false;
1704
1705 HadField = true;
1706 }
1707 }
1708
1709 return true;
1710}
1711
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001712ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001713 ASTContext &Context,
1714 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001715 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001716 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001717
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001718 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1719 // Treat an enum type as its underlying type.
1720 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1721 RetTy = EnumTy->getDecl()->getIntegerType();
1722
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001723 return (RetTy->isPromotableIntegerType() ?
1724 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001725 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001726
1727 // Are we following APCS?
1728 if (getABIKind() == APCS) {
1729 if (isEmptyRecord(Context, RetTy, false))
1730 return ABIArgInfo::getIgnore();
1731
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00001732 // Complex types are all returned as packed integers.
1733 //
1734 // FIXME: Consider using 2 x vector types if the back end handles them
1735 // correctly.
1736 if (RetTy->isAnyComplexType())
1737 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1738 VMContext, Context.getTypeSize(RetTy)));
1739
Daniel Dunbar98303b92009-09-13 08:03:58 +00001740 // Integer like structures are returned in r0.
1741 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1742 // Return in the smallest viable integer type.
1743 uint64_t Size = Context.getTypeSize(RetTy);
1744 if (Size <= 8)
1745 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1746 if (Size <= 16)
1747 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1748 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1749 }
1750
1751 // Otherwise return in memory.
1752 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001753 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001754
1755 // Otherwise this is an AAPCS variant.
1756
Daniel Dunbar16a08082009-09-14 00:56:55 +00001757 if (isEmptyRecord(Context, RetTy, true))
1758 return ABIArgInfo::getIgnore();
1759
Daniel Dunbar98303b92009-09-13 08:03:58 +00001760 // Aggregates <= 4 bytes are returned in r0; other aggregates
1761 // are returned indirectly.
1762 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00001763 if (Size <= 32) {
1764 // Return in the smallest viable integer type.
1765 if (Size <= 8)
1766 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1767 if (Size <= 16)
1768 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00001769 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00001770 }
1771
Daniel Dunbar98303b92009-09-13 08:03:58 +00001772 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001773}
1774
1775llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1776 CodeGenFunction &CGF) const {
1777 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001778 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001779 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001780
1781 CGBuilderTy &Builder = CGF.Builder;
1782 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1783 "ap");
1784 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1785 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001786 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001787 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1788
1789 uint64_t Offset =
1790 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1791 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +00001792 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1793 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001794 "ap.next");
1795 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1796
1797 return AddrTyped;
1798}
1799
1800ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001801 ASTContext &Context,
1802 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001803 if (RetTy->isVoidType()) {
1804 return ABIArgInfo::getIgnore();
1805 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1806 return ABIArgInfo::getIndirect(0);
1807 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001808 // Treat an enum type as its underlying type.
1809 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1810 RetTy = EnumTy->getDecl()->getIntegerType();
1811
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001812 return (RetTy->isPromotableIntegerType() ?
1813 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001814 }
1815}
1816
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001817// SystemZ ABI Implementation
1818
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001819namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001820
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001821class SystemZABIInfo : public ABIInfo {
1822 bool isPromotableIntegerType(QualType Ty) const;
1823
1824 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1825 llvm::LLVMContext &VMContext) const;
1826
1827 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1828 llvm::LLVMContext &VMContext) const;
1829
1830 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1831 llvm::LLVMContext &VMContext) const {
1832 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1833 Context, VMContext);
1834 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1835 it != ie; ++it)
1836 it->info = classifyArgumentType(it->type, Context, VMContext);
1837 }
1838
1839 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1840 CodeGenFunction &CGF) const;
1841};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001842
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001843class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
1844public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001845 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001846};
1847
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001848}
1849
1850bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1851 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00001852 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001853 switch (BT->getKind()) {
1854 case BuiltinType::Bool:
1855 case BuiltinType::Char_S:
1856 case BuiltinType::Char_U:
1857 case BuiltinType::SChar:
1858 case BuiltinType::UChar:
1859 case BuiltinType::Short:
1860 case BuiltinType::UShort:
1861 case BuiltinType::Int:
1862 case BuiltinType::UInt:
1863 return true;
1864 default:
1865 return false;
1866 }
1867 return false;
1868}
1869
1870llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1871 CodeGenFunction &CGF) const {
1872 // FIXME: Implement
1873 return 0;
1874}
1875
1876
1877ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1878 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001879 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001880 if (RetTy->isVoidType()) {
1881 return ABIArgInfo::getIgnore();
1882 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1883 return ABIArgInfo::getIndirect(0);
1884 } else {
1885 return (isPromotableIntegerType(RetTy) ?
1886 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1887 }
1888}
1889
1890ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1891 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001892 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001893 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1894 return ABIArgInfo::getIndirect(0);
1895 } else {
1896 return (isPromotableIntegerType(Ty) ?
1897 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1898 }
1899}
1900
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001901// MSP430 ABI Implementation
1902
1903namespace {
1904
1905class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
1906public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001907 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001908 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1909 CodeGen::CodeGenModule &M) const;
1910};
1911
1912}
1913
1914void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1915 llvm::GlobalValue *GV,
1916 CodeGen::CodeGenModule &M) const {
1917 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1918 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
1919 // Handle 'interrupt' attribute:
1920 llvm::Function *F = cast<llvm::Function>(GV);
1921
1922 // Step 1: Set ISR calling convention.
1923 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
1924
1925 // Step 2: Add attributes goodness.
1926 F->addFnAttr(llvm::Attribute::NoInline);
1927
1928 // Step 3: Emit ISR vector alias.
1929 unsigned Num = attr->getNumber() + 0xffe0;
1930 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
1931 "vector_" +
1932 llvm::LowercaseString(llvm::utohexstr(Num)),
1933 GV, &M.getModule());
1934 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001935 }
1936}
1937
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001938const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
1939 if (TheTargetCodeGenInfo)
1940 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001941
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001942 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
1943 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001944
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001945 const llvm::Triple &Triple(getContext().Target.getTriple());
1946 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001947 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001948 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001949
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001950 case llvm::Triple::arm:
1951 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001952 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00001953 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001954 return *(TheTargetCodeGenInfo =
1955 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001956
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001957 return *(TheTargetCodeGenInfo =
1958 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001959
1960 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001961 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001962
1963 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001964 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
1965
1966 case llvm::Triple::msp430:
1967 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001968
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001969 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001970 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00001971 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001972 return *(TheTargetCodeGenInfo =
1973 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001974 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001975 case llvm::Triple::MinGW32:
1976 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00001977 case llvm::Triple::AuroraUX:
1978 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00001979 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001980 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001981 return *(TheTargetCodeGenInfo =
1982 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001983
1984 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001985 return *(TheTargetCodeGenInfo =
1986 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001987 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001988
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001989 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001990 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001991 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001992}