blob: f4ec914a4e0542ec2c862fa274f8d0e3c48c2ad8 [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())) {
John McCall0b0ef0a2010-02-24 07:14:12 +0000975 assert(Ty.isCanonical() && "should always have a canonical type here");
976 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000977
978 // Float and double end up in a single SSE reg.
John McCall0b0ef0a2010-02-24 07:14:12 +0000979 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000980 return ABIArgInfo::getDirect();
981
982 }
983
984 return ABIArgInfo::getCoerce(CoerceTo);
985}
986
987ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
988 ASTContext &Context) const {
989 // If this is a scalar LLVM value then assume LLVM will pass it in the right
990 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000991 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
992 // Treat an enum type as its underlying type.
993 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
994 Ty = EnumTy->getDecl()->getIntegerType();
995
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000996 return (Ty->isPromotableIntegerType() ?
997 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000998 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000999
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001000 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
1001
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001002 // FIXME: Set alignment correctly.
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001003 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001004}
1005
1006ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001007 ASTContext &Context,
1008 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001009 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1010 // classification algorithm.
1011 X86_64ABIInfo::Class Lo, Hi;
1012 classify(RetTy, Context, 0, Lo, Hi);
1013
1014 // Check some invariants.
1015 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1016 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1017 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1018
1019 const llvm::Type *ResType = 0;
1020 switch (Lo) {
1021 case NoClass:
1022 return ABIArgInfo::getIgnore();
1023
1024 case SSEUp:
1025 case X87Up:
1026 assert(0 && "Invalid classification for lo word.");
1027
1028 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1029 // hidden argument.
1030 case Memory:
1031 return getIndirectResult(RetTy, Context);
1032
1033 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1034 // available register of the sequence %rax, %rdx is used.
1035 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001036 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001037
1038 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1039 // available SSE register of the sequence %xmm0, %xmm1 is used.
1040 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001041 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001042
1043 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1044 // returned on the X87 stack in %st0 as 80-bit x87 number.
1045 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001046 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001047
1048 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1049 // part of the value is returned in %st0 and the imaginary part in
1050 // %st1.
1051 case ComplexX87:
1052 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001053 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
1054 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001055 NULL);
1056 break;
1057 }
1058
1059 switch (Hi) {
1060 // Memory was handled previously and X87 should
1061 // never occur as a hi class.
1062 case Memory:
1063 case X87:
1064 assert(0 && "Invalid classification for hi word.");
1065
1066 case ComplexX87: // Previously handled.
1067 case NoClass: break;
1068
1069 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001070 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001071 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001072 break;
1073 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001074 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001075 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001076 break;
1077
1078 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1079 // is passed in the upper half of the last used SSE register.
1080 //
1081 // SSEUP should always be preceeded by SSE, just widen.
1082 case SSEUp:
1083 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001084 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001085 break;
1086
1087 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1088 // returned together with the previous X87 value in %st0.
1089 case X87Up:
1090 // If X87Up is preceeded by X87, we don't need to do
1091 // anything. However, in some cases with unions it may not be
1092 // preceeded by X87. In such situations we follow gcc and pass the
1093 // extra bits in an SSE reg.
1094 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001095 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001096 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001097 break;
1098 }
1099
1100 return getCoerceResult(RetTy, ResType, Context);
1101}
1102
1103ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001104 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001105 unsigned &neededInt,
1106 unsigned &neededSSE) const {
1107 X86_64ABIInfo::Class Lo, Hi;
1108 classify(Ty, Context, 0, Lo, Hi);
1109
1110 // Check some invariants.
1111 // FIXME: Enforce these by construction.
1112 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1113 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1114 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1115
1116 neededInt = 0;
1117 neededSSE = 0;
1118 const llvm::Type *ResType = 0;
1119 switch (Lo) {
1120 case NoClass:
1121 return ABIArgInfo::getIgnore();
1122
1123 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1124 // on the stack.
1125 case Memory:
1126
1127 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1128 // COMPLEX_X87, it is passed in memory.
1129 case X87:
1130 case ComplexX87:
1131 return getIndirectResult(Ty, Context);
1132
1133 case SSEUp:
1134 case X87Up:
1135 assert(0 && "Invalid classification for lo word.");
1136
1137 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1138 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1139 // and %r9 is used.
1140 case Integer:
1141 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001142 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001143 break;
1144
1145 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1146 // available SSE register is used, the registers are taken in the
1147 // order from %xmm0 to %xmm7.
1148 case SSE:
1149 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001150 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001151 break;
1152 }
1153
1154 switch (Hi) {
1155 // Memory was handled previously, ComplexX87 and X87 should
1156 // never occur as hi classes, and X87Up must be preceed by X87,
1157 // which is passed in memory.
1158 case Memory:
1159 case X87:
1160 case ComplexX87:
1161 assert(0 && "Invalid classification for hi word.");
1162 break;
1163
1164 case NoClass: break;
1165 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001166 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001167 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001168 ++neededInt;
1169 break;
1170
1171 // X87Up generally doesn't occur here (long double is passed in
1172 // memory), except in situations involving unions.
1173 case X87Up:
1174 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001175 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001176 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001177 ++neededSSE;
1178 break;
1179
1180 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1181 // eightbyte is passed in the upper half of the last used SSE
1182 // register.
1183 case SSEUp:
1184 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001185 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001186 break;
1187 }
1188
1189 return getCoerceResult(Ty, ResType, Context);
1190}
1191
Owen Andersona1cf15f2009-07-14 23:10:40 +00001192void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1193 llvm::LLVMContext &VMContext) const {
1194 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1195 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001196
1197 // Keep track of the number of assigned registers.
1198 unsigned freeIntRegs = 6, freeSSERegs = 8;
1199
1200 // If the return value is indirect, then the hidden argument is consuming one
1201 // integer register.
1202 if (FI.getReturnInfo().isIndirect())
1203 --freeIntRegs;
1204
1205 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1206 // get assigned (in left-to-right order) for passing as follows...
1207 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1208 it != ie; ++it) {
1209 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001210 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001211 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001212
1213 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1214 // eightbyte of an argument, the whole argument is passed on the
1215 // stack. If registers have already been assigned for some
1216 // eightbytes of such an argument, the assignments get reverted.
1217 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1218 freeIntRegs -= neededInt;
1219 freeSSERegs -= neededSSE;
1220 } else {
1221 it->info = getIndirectResult(it->type, Context);
1222 }
1223 }
1224}
1225
1226static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1227 QualType Ty,
1228 CodeGenFunction &CGF) {
1229 llvm::Value *overflow_arg_area_p =
1230 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1231 llvm::Value *overflow_arg_area =
1232 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1233
1234 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1235 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1236 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1237 if (Align > 8) {
1238 // Note that we follow the ABI & gcc here, even though the type
1239 // could in theory have an alignment greater than 16. This case
1240 // shouldn't ever matter in practice.
1241
1242 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001243 llvm::Value *Offset =
1244 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001245 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1246 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson0032b272009-08-13 21:57:51 +00001247 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1248 llvm::Value *Mask = llvm::ConstantInt::get(
1249 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001250 overflow_arg_area =
1251 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1252 overflow_arg_area->getType(),
1253 "overflow_arg_area.align");
1254 }
1255
1256 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1257 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1258 llvm::Value *Res =
1259 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001260 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001261
1262 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1263 // l->overflow_arg_area + sizeof(type).
1264 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1265 // an 8 byte boundary.
1266
1267 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001268 llvm::Value *Offset =
1269 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001270 (SizeInBytes + 7) & ~7);
1271 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1272 "overflow_arg_area.next");
1273 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1274
1275 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1276 return Res;
1277}
1278
1279llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1280 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001281 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001282 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1283 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001285 // Assume that va_list type is correct; should be pointer to LLVM type:
1286 // struct {
1287 // i32 gp_offset;
1288 // i32 fp_offset;
1289 // i8* overflow_arg_area;
1290 // i8* reg_save_area;
1291 // };
1292 unsigned neededInt, neededSSE;
Owen Andersona1cf15f2009-07-14 23:10:40 +00001293 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001294 neededInt, neededSSE);
1295
1296 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1297 // in the registers. If not go to step 7.
1298 if (!neededInt && !neededSSE)
1299 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1300
1301 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1302 // general purpose registers needed to pass type and num_fp to hold
1303 // the number of floating point registers needed.
1304
1305 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1306 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1307 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1308 //
1309 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1310 // register save space).
1311
1312 llvm::Value *InRegs = 0;
1313 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1314 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1315 if (neededInt) {
1316 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1317 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1318 InRegs =
1319 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001320 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001321 48 - neededInt * 8),
1322 "fits_in_gp");
1323 }
1324
1325 if (neededSSE) {
1326 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1327 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1328 llvm::Value *FitsInFP =
1329 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001330 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001331 176 - neededSSE * 16),
1332 "fits_in_fp");
1333 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1334 }
1335
1336 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1337 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1338 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1339 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1340
1341 // Emit code to load the value if it was passed in registers.
1342
1343 CGF.EmitBlock(InRegBlock);
1344
1345 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1346 // an offset of l->gp_offset and/or l->fp_offset. This may require
1347 // copying to a temporary location in case the parameter is passed
1348 // in different register classes or requires an alignment greater
1349 // than 8 for general purpose registers and 16 for XMM registers.
1350 //
1351 // FIXME: This really results in shameful code when we end up needing to
1352 // collect arguments from different places; often what should result in a
1353 // simple assembling of a structure from scattered addresses has many more
1354 // loads than necessary. Can we clean this up?
1355 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1356 llvm::Value *RegAddr =
1357 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1358 "reg_save_area");
1359 if (neededInt && neededSSE) {
1360 // FIXME: Cleanup.
1361 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1362 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1363 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1364 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1365 const llvm::Type *TyLo = ST->getElementType(0);
1366 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001367 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001368 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001369 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1370 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001371 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1372 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001373 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1374 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001375 llvm::Value *V =
1376 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1377 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1378 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1379 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1380
Owen Andersona1cf15f2009-07-14 23:10:40 +00001381 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001382 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001383 } else if (neededInt) {
1384 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1385 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001386 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001387 } else {
1388 if (neededSSE == 1) {
1389 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1390 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001391 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392 } else {
1393 assert(neededSSE == 2 && "Invalid number of needed registers!");
1394 // SSE registers are spaced 16 bytes apart in the register save
1395 // area, we need to collect the two eightbytes together.
1396 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1397 llvm::Value *RegAddrHi =
1398 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001399 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001400 const llvm::Type *DblPtrTy =
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001401 llvm::PointerType::getUnqual(DoubleTy);
1402 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1403 DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001404 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1405 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1406 DblPtrTy));
1407 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1408 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1409 DblPtrTy));
1410 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1411 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001412 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001413 }
1414 }
1415
1416 // AMD64-ABI 3.5.7p5: Step 5. Set:
1417 // l->gp_offset = l->gp_offset + num_gp * 8
1418 // l->fp_offset = l->fp_offset + num_fp * 16.
1419 if (neededInt) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001420 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001421 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1422 gp_offset_p);
1423 }
1424 if (neededSSE) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001425 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001426 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1427 fp_offset_p);
1428 }
1429 CGF.EmitBranch(ContBlock);
1430
1431 // Emit code to load the value if it was passed in memory.
1432
1433 CGF.EmitBlock(InMemBlock);
1434 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1435
1436 // Return the appropriate result.
1437
1438 CGF.EmitBlock(ContBlock);
1439 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1440 "vaarg.addr");
1441 ResAddr->reserveOperandSpace(2);
1442 ResAddr->addIncoming(RegAddr, InRegBlock);
1443 ResAddr->addIncoming(MemAddr, InMemBlock);
1444
1445 return ResAddr;
1446}
1447
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001448// PIC16 ABI Implementation
1449
1450namespace {
1451
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001452class PIC16ABIInfo : public ABIInfo {
1453 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001454 ASTContext &Context,
1455 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001456
1457 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001458 ASTContext &Context,
1459 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001460
Owen Andersona1cf15f2009-07-14 23:10:40 +00001461 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1462 llvm::LLVMContext &VMContext) const {
1463 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1464 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001465 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1466 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001467 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001468 }
1469
1470 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1471 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001472};
1473
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001474class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1475public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001476 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001477};
1478
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001479}
1480
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001481ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001482 ASTContext &Context,
1483 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001484 if (RetTy->isVoidType()) {
1485 return ABIArgInfo::getIgnore();
1486 } else {
1487 return ABIArgInfo::getDirect();
1488 }
1489}
1490
1491ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001492 ASTContext &Context,
1493 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001494 return ABIArgInfo::getDirect();
1495}
1496
1497llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1498 CodeGenFunction &CGF) const {
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001499 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext()));
1500 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1501
1502 CGBuilderTy &Builder = CGF.Builder;
1503 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1504 "ap");
1505 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1506 llvm::Type *PTy =
1507 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1508 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1509
1510 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1511
1512 llvm::Value *NextAddr =
1513 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1514 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1515 "ap.next");
1516 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1517
1518 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001519}
1520
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001521
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001522// ARM ABI Implementation
1523
1524namespace {
1525
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001526class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001527public:
1528 enum ABIKind {
1529 APCS = 0,
1530 AAPCS = 1,
1531 AAPCS_VFP
1532 };
1533
1534private:
1535 ABIKind Kind;
1536
1537public:
1538 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1539
1540private:
1541 ABIKind getABIKind() const { return Kind; }
1542
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001543 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001544 ASTContext &Context,
1545 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001546
1547 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001548 ASTContext &Context,
1549 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001550
Owen Andersona1cf15f2009-07-14 23:10:40 +00001551 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1552 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001553
1554 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1555 CodeGenFunction &CGF) const;
1556};
1557
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001558class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1559public:
1560 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001561 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001562};
1563
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001564}
1565
Owen Andersona1cf15f2009-07-14 23:10:40 +00001566void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1567 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001568 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001569 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001570 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1571 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001572 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001573 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001574
1575 // ARM always overrides the calling convention.
1576 switch (getABIKind()) {
1577 case APCS:
1578 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1579 break;
1580
1581 case AAPCS:
1582 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1583 break;
1584
1585 case AAPCS_VFP:
1586 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1587 break;
1588 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001589}
1590
1591ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001592 ASTContext &Context,
1593 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001594 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1595 // Treat an enum type as its underlying type.
1596 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1597 Ty = EnumTy->getDecl()->getIntegerType();
1598
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001599 return (Ty->isPromotableIntegerType() ?
1600 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001601 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001602
Daniel Dunbar42025572009-09-14 21:54:03 +00001603 // Ignore empty records.
1604 if (isEmptyRecord(Context, Ty, true))
1605 return ABIArgInfo::getIgnore();
1606
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001607 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1608 // backend doesn't support byval.
1609 // FIXME: This doesn't handle alignment > 64 bits.
1610 const llvm::Type* ElemTy;
1611 unsigned SizeRegs;
1612 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001613 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001614 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1615 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001616 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001617 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1618 }
1619 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001620 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001621 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001622 return ABIArgInfo::getCoerce(STy);
1623}
1624
Daniel Dunbar98303b92009-09-13 08:03:58 +00001625static bool isIntegerLikeType(QualType Ty,
1626 ASTContext &Context,
1627 llvm::LLVMContext &VMContext) {
1628 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1629 // is called integer-like if its size is less than or equal to one word, and
1630 // the offset of each of its addressable sub-fields is zero.
1631
1632 uint64_t Size = Context.getTypeSize(Ty);
1633
1634 // Check that the type fits in a word.
1635 if (Size > 32)
1636 return false;
1637
1638 // FIXME: Handle vector types!
1639 if (Ty->isVectorType())
1640 return false;
1641
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001642 // Float types are never treated as "integer like".
1643 if (Ty->isRealFloatingType())
1644 return false;
1645
Daniel Dunbar98303b92009-09-13 08:03:58 +00001646 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001647 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001648 return true;
1649
Daniel Dunbar45815812010-02-01 23:31:26 +00001650 // Small complex integer types are "integer like".
1651 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1652 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001653
1654 // Single element and zero sized arrays should be allowed, by the definition
1655 // above, but they are not.
1656
1657 // Otherwise, it must be a record type.
1658 const RecordType *RT = Ty->getAs<RecordType>();
1659 if (!RT) return false;
1660
1661 // Ignore records with flexible arrays.
1662 const RecordDecl *RD = RT->getDecl();
1663 if (RD->hasFlexibleArrayMember())
1664 return false;
1665
1666 // Check that all sub-fields are at offset 0, and are themselves "integer
1667 // like".
1668 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1669
1670 bool HadField = false;
1671 unsigned idx = 0;
1672 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1673 i != e; ++i, ++idx) {
1674 const FieldDecl *FD = *i;
1675
Daniel Dunbar679855a2010-01-29 03:22:29 +00001676 // Bit-fields are not addressable, we only need to verify they are "integer
1677 // like". We still have to disallow a subsequent non-bitfield, for example:
1678 // struct { int : 0; int x }
1679 // is non-integer like according to gcc.
1680 if (FD->isBitField()) {
1681 if (!RD->isUnion())
1682 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001683
Daniel Dunbar679855a2010-01-29 03:22:29 +00001684 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1685 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001686
Daniel Dunbar679855a2010-01-29 03:22:29 +00001687 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001688 }
1689
Daniel Dunbar679855a2010-01-29 03:22:29 +00001690 // Check if this field is at offset 0.
1691 if (Layout.getFieldOffset(idx) != 0)
1692 return false;
1693
Daniel Dunbar98303b92009-09-13 08:03:58 +00001694 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1695 return false;
1696
Daniel Dunbar679855a2010-01-29 03:22:29 +00001697 // Only allow at most one field in a structure. This doesn't match the
1698 // wording above, but follows gcc in situations with a field following an
1699 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001700 if (!RD->isUnion()) {
1701 if (HadField)
1702 return false;
1703
1704 HadField = true;
1705 }
1706 }
1707
1708 return true;
1709}
1710
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001711ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001712 ASTContext &Context,
1713 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001714 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001715 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001716
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001717 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1718 // Treat an enum type as its underlying type.
1719 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1720 RetTy = EnumTy->getDecl()->getIntegerType();
1721
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001722 return (RetTy->isPromotableIntegerType() ?
1723 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001724 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001725
1726 // Are we following APCS?
1727 if (getABIKind() == APCS) {
1728 if (isEmptyRecord(Context, RetTy, false))
1729 return ABIArgInfo::getIgnore();
1730
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00001731 // Complex types are all returned as packed integers.
1732 //
1733 // FIXME: Consider using 2 x vector types if the back end handles them
1734 // correctly.
1735 if (RetTy->isAnyComplexType())
1736 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1737 VMContext, Context.getTypeSize(RetTy)));
1738
Daniel Dunbar98303b92009-09-13 08:03:58 +00001739 // Integer like structures are returned in r0.
1740 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1741 // Return in the smallest viable integer type.
1742 uint64_t Size = Context.getTypeSize(RetTy);
1743 if (Size <= 8)
1744 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1745 if (Size <= 16)
1746 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1747 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1748 }
1749
1750 // Otherwise return in memory.
1751 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001752 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001753
1754 // Otherwise this is an AAPCS variant.
1755
Daniel Dunbar16a08082009-09-14 00:56:55 +00001756 if (isEmptyRecord(Context, RetTy, true))
1757 return ABIArgInfo::getIgnore();
1758
Daniel Dunbar98303b92009-09-13 08:03:58 +00001759 // Aggregates <= 4 bytes are returned in r0; other aggregates
1760 // are returned indirectly.
1761 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00001762 if (Size <= 32) {
1763 // Return in the smallest viable integer type.
1764 if (Size <= 8)
1765 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1766 if (Size <= 16)
1767 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00001768 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00001769 }
1770
Daniel Dunbar98303b92009-09-13 08:03:58 +00001771 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001772}
1773
1774llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1775 CodeGenFunction &CGF) const {
1776 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001777 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001778 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001779
1780 CGBuilderTy &Builder = CGF.Builder;
1781 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1782 "ap");
1783 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1784 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001785 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001786 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1787
1788 uint64_t Offset =
1789 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1790 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +00001791 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1792 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001793 "ap.next");
1794 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1795
1796 return AddrTyped;
1797}
1798
1799ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001800 ASTContext &Context,
1801 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001802 if (RetTy->isVoidType()) {
1803 return ABIArgInfo::getIgnore();
1804 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1805 return ABIArgInfo::getIndirect(0);
1806 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001807 // Treat an enum type as its underlying type.
1808 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1809 RetTy = EnumTy->getDecl()->getIntegerType();
1810
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001811 return (RetTy->isPromotableIntegerType() ?
1812 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001813 }
1814}
1815
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001816// SystemZ ABI Implementation
1817
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001818namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001819
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001820class SystemZABIInfo : public ABIInfo {
1821 bool isPromotableIntegerType(QualType Ty) const;
1822
1823 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1824 llvm::LLVMContext &VMContext) const;
1825
1826 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1827 llvm::LLVMContext &VMContext) const;
1828
1829 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1830 llvm::LLVMContext &VMContext) const {
1831 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1832 Context, VMContext);
1833 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1834 it != ie; ++it)
1835 it->info = classifyArgumentType(it->type, Context, VMContext);
1836 }
1837
1838 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1839 CodeGenFunction &CGF) const;
1840};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001841
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001842class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
1843public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001844 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001845};
1846
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001847}
1848
1849bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1850 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00001851 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001852 switch (BT->getKind()) {
1853 case BuiltinType::Bool:
1854 case BuiltinType::Char_S:
1855 case BuiltinType::Char_U:
1856 case BuiltinType::SChar:
1857 case BuiltinType::UChar:
1858 case BuiltinType::Short:
1859 case BuiltinType::UShort:
1860 case BuiltinType::Int:
1861 case BuiltinType::UInt:
1862 return true;
1863 default:
1864 return false;
1865 }
1866 return false;
1867}
1868
1869llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1870 CodeGenFunction &CGF) const {
1871 // FIXME: Implement
1872 return 0;
1873}
1874
1875
1876ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1877 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001878 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001879 if (RetTy->isVoidType()) {
1880 return ABIArgInfo::getIgnore();
1881 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1882 return ABIArgInfo::getIndirect(0);
1883 } else {
1884 return (isPromotableIntegerType(RetTy) ?
1885 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1886 }
1887}
1888
1889ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1890 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001891 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001892 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1893 return ABIArgInfo::getIndirect(0);
1894 } else {
1895 return (isPromotableIntegerType(Ty) ?
1896 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1897 }
1898}
1899
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001900// MSP430 ABI Implementation
1901
1902namespace {
1903
1904class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
1905public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001906 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001907 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1908 CodeGen::CodeGenModule &M) const;
1909};
1910
1911}
1912
1913void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1914 llvm::GlobalValue *GV,
1915 CodeGen::CodeGenModule &M) const {
1916 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1917 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
1918 // Handle 'interrupt' attribute:
1919 llvm::Function *F = cast<llvm::Function>(GV);
1920
1921 // Step 1: Set ISR calling convention.
1922 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
1923
1924 // Step 2: Add attributes goodness.
1925 F->addFnAttr(llvm::Attribute::NoInline);
1926
1927 // Step 3: Emit ISR vector alias.
1928 unsigned Num = attr->getNumber() + 0xffe0;
1929 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
1930 "vector_" +
1931 llvm::LowercaseString(llvm::utohexstr(Num)),
1932 GV, &M.getModule());
1933 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001934 }
1935}
1936
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001937const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
1938 if (TheTargetCodeGenInfo)
1939 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001940
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001941 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
1942 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001943
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001944 const llvm::Triple &Triple(getContext().Target.getTriple());
1945 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001946 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001947 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001948
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001949 case llvm::Triple::arm:
1950 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001951 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00001952 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001953 return *(TheTargetCodeGenInfo =
1954 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001955
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001956 return *(TheTargetCodeGenInfo =
1957 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001958
1959 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001960 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001961
1962 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001963 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
1964
1965 case llvm::Triple::msp430:
1966 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001967
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001968 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001969 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00001970 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001971 return *(TheTargetCodeGenInfo =
1972 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001973 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001974 case llvm::Triple::MinGW32:
1975 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00001976 case llvm::Triple::AuroraUX:
1977 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00001978 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001979 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001980 return *(TheTargetCodeGenInfo =
1981 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001982
1983 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001984 return *(TheTargetCodeGenInfo =
1985 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001986 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001987
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001988 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001989 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001990 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001991}