blob: 80163b3772eb9cc529b6f2edb47bc22109b51623 [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
John McCallaeeb7012010-05-27 06:19:26 +000026static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
27 llvm::Value *Array,
28 llvm::Value *Value,
29 unsigned FirstIndex,
30 unsigned LastIndex) {
31 // Alternatively, we could emit this as a loop in the source.
32 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
33 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
34 Builder.CreateStore(Value, Cell);
35 }
36}
37
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000038ABIInfo::~ABIInfo() {}
39
40void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000041 llvm::raw_ostream &OS = llvm::errs();
42 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000043 switch (TheKind) {
44 case Direct:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000045 OS << "Direct";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000046 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000047 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000048 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000049 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000050 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000051 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000052 break;
53 case Coerce:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000054 OS << "Coerce Type=";
55 getCoerceToType()->print(OS);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000056 break;
57 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000058 OS << "Indirect Align=" << getIndirectAlign()
59 << " Byal=" << getIndirectByVal();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000060 break;
61 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000062 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000063 break;
64 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000065 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000066}
67
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000068TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
69
Daniel Dunbar98303b92009-09-13 08:03:58 +000070static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000071
72/// isEmptyField - Return true iff a the field is "empty", that is it
73/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000074static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
75 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 if (FD->isUnnamedBitfield())
77 return true;
78
79 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080
Daniel Dunbar98303b92009-09-13 08:03:58 +000081 // Constant arrays of empty records count as empty, strip them off.
82 if (AllowArrays)
83 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
84 FT = AT->getElementType();
85
Daniel Dunbar5ea68612010-05-17 16:46:00 +000086 const RecordType *RT = FT->getAs<RecordType>();
87 if (!RT)
88 return false;
89
90 // C++ record fields are never empty, at least in the Itanium ABI.
91 //
92 // FIXME: We should use a predicate for whether this behavior is true in the
93 // current ABI.
94 if (isa<CXXRecordDecl>(RT->getDecl()))
95 return false;
96
Daniel Dunbar98303b92009-09-13 08:03:58 +000097 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000098}
99
100/// isEmptyRecord - Return true iff a structure contains only empty
101/// fields. Note that a structure with a flexible array member is not
102/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000103static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000104 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000105 if (!RT)
106 return 0;
107 const RecordDecl *RD = RT->getDecl();
108 if (RD->hasFlexibleArrayMember())
109 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000110
111 // If this is a C++ record, check the bases first.
112 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
113 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
114 e = CXXRD->bases_end(); i != e; ++i)
115 if (!isEmptyRecord(Context, i->getType(), true))
116 return false;
117
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000118 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
119 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000120 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000121 return false;
122 return true;
123}
124
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000125/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
126/// a non-trivial destructor or a non-trivial copy constructor.
127static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
128 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
129 if (!RD)
130 return false;
131
132 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
133}
134
135/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
136/// a record type with either a non-trivial destructor or a non-trivial copy
137/// constructor.
138static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
139 const RecordType *RT = T->getAs<RecordType>();
140 if (!RT)
141 return false;
142
143 return hasNonTrivialDestructorOrCopyConstructor(RT);
144}
145
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000146/// isSingleElementStruct - Determine if a structure is a "single
147/// element struct", i.e. it has exactly one non-empty field or
148/// exactly one field which is itself a single element
149/// struct. Structures with flexible array members are never
150/// considered single element structs.
151///
152/// \return The field declaration for the single non-empty field, if
153/// it exists.
154static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
155 const RecordType *RT = T->getAsStructureType();
156 if (!RT)
157 return 0;
158
159 const RecordDecl *RD = RT->getDecl();
160 if (RD->hasFlexibleArrayMember())
161 return 0;
162
163 const Type *Found = 0;
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000164
165 // If this is a C++ record, check the bases first.
166 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
167 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
168 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000169 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000170 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000171 continue;
172
173 // If we already found an element then this isn't a single-element struct.
174 if (Found)
175 return 0;
176
177 // If this is non-empty and not a single element struct, the composite
178 // cannot be a single element struct.
179 Found = isSingleElementStruct(i->getType(), Context);
180 if (!Found)
181 return 0;
182 }
183 }
184
185 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000186 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
187 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000188 const FieldDecl *FD = *i;
189 QualType FT = FD->getType();
190
191 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000192 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000193 continue;
194
195 // If we already found an element then this isn't a single-element
196 // struct.
197 if (Found)
198 return 0;
199
200 // Treat single element arrays as the element.
201 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
202 if (AT->getSize().getZExtValue() != 1)
203 break;
204 FT = AT->getElementType();
205 }
206
207 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
208 Found = FT.getTypePtr();
209 } else {
210 Found = isSingleElementStruct(FT, Context);
211 if (!Found)
212 return 0;
213 }
214 }
215
216 return Found;
217}
218
219static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000220 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000221 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
222 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000223 return false;
224
225 uint64_t Size = Context.getTypeSize(Ty);
226 return Size == 32 || Size == 64;
227}
228
Daniel Dunbar53012f42009-11-09 01:33:53 +0000229/// canExpandIndirectArgument - Test whether an argument type which is to be
230/// passed indirectly (on the stack) would have the equivalent layout if it was
231/// expanded into separate arguments. If so, we prefer to do the latter to avoid
232/// inhibiting optimizations.
233///
234// FIXME: This predicate is missing many cases, currently it just follows
235// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
236// should probably make this smarter, or better yet make the LLVM backend
237// capable of handling it.
238static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
239 // We can only expand structure types.
240 const RecordType *RT = Ty->getAs<RecordType>();
241 if (!RT)
242 return false;
243
244 // We can only expand (C) structures.
245 //
246 // FIXME: This needs to be generalized to handle classes as well.
247 const RecordDecl *RD = RT->getDecl();
248 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
249 return false;
250
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000251 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
252 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000253 const FieldDecl *FD = *i;
254
255 if (!is32Or64BitBasicType(FD->getType(), Context))
256 return false;
257
258 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
259 // how to expand them yet, and the predicate for telling if a bitfield still
260 // counts as "basic" is more complicated than what we were doing previously.
261 if (FD->isBitField())
262 return false;
263 }
264
265 return true;
266}
267
268namespace {
269/// DefaultABIInfo - The default implementation for ABI specific
270/// details. This implementation provides information which results in
271/// self-consistent and sensible LLVM IR generation, but does not
272/// conform to any particular ABI.
273class DefaultABIInfo : public ABIInfo {
274 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000275 ASTContext &Context,
276 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000277
278 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000279 ASTContext &Context,
280 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000281
Owen Andersona1cf15f2009-07-14 23:10:40 +0000282 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
283 llvm::LLVMContext &VMContext) const {
284 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
285 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000286 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
287 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000288 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000289 }
290
291 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
292 CodeGenFunction &CGF) const;
293};
294
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000295class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
296public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000297 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000298};
299
300llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
301 CodeGenFunction &CGF) const {
302 return 0;
303}
304
305ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
306 ASTContext &Context,
307 llvm::LLVMContext &VMContext) const {
Chris Lattnera14db752010-03-11 18:19:55 +0000308 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000309 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000310
Chris Lattnera14db752010-03-11 18:19:55 +0000311 // Treat an enum type as its underlying type.
312 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
313 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000314
Chris Lattnera14db752010-03-11 18:19:55 +0000315 return (Ty->isPromotableIntegerType() ?
316 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000317}
318
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000319//===----------------------------------------------------------------------===//
320// X86-32 ABI Implementation
321//===----------------------------------------------------------------------===//
322
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000323/// X86_32ABIInfo - The X86-32 ABI information.
324class X86_32ABIInfo : public ABIInfo {
325 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000326 bool IsDarwinVectorABI;
327 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000328
329 static bool isRegisterSize(unsigned Size) {
330 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
331 }
332
333 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
334
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000335 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
336 /// such that the argument will be passed in memory.
337 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
338 bool ByVal = true) const;
339
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000340public:
341 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000342 ASTContext &Context,
343 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000344
345 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000346 ASTContext &Context,
347 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000348
Owen Andersona1cf15f2009-07-14 23:10:40 +0000349 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
350 llvm::LLVMContext &VMContext) const {
351 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
352 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000353 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
354 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000355 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000356 }
357
358 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
359 CodeGenFunction &CGF) const;
360
David Chisnall1e4249c2009-08-17 23:08:21 +0000361 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000362 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000363 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000364};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000365
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000366class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
367public:
368 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000369 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000370
371 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
372 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000373
374 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
375 // Darwin uses different dwarf register numbers for EH.
376 if (CGM.isTargetDarwin()) return 5;
377
378 return 4;
379 }
380
381 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
382 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000383};
384
385}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000386
387/// shouldReturnTypeInRegister - Determine if the given type should be
388/// passed in a register (for the Darwin ABI).
389bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
390 ASTContext &Context) {
391 uint64_t Size = Context.getTypeSize(Ty);
392
393 // Type must be register sized.
394 if (!isRegisterSize(Size))
395 return false;
396
397 if (Ty->isVectorType()) {
398 // 64- and 128- bit vectors inside structures are not returned in
399 // registers.
400 if (Size == 64 || Size == 128)
401 return false;
402
403 return true;
404 }
405
Daniel Dunbar77115232010-05-15 00:00:30 +0000406 // If this is a builtin, pointer, enum, complex type, member pointer, or
407 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000408 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000409 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000410 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000411 return true;
412
413 // Arrays are treated like records.
414 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
415 return shouldReturnTypeInRegister(AT->getElementType(), Context);
416
417 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000418 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000419 if (!RT) return false;
420
Anders Carlssona8874232010-01-27 03:25:19 +0000421 // FIXME: Traverse bases here too.
422
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000423 // Structure types are passed in register if all fields would be
424 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000425 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
426 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000427 const FieldDecl *FD = *i;
428
429 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000430 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000431 continue;
432
433 // Check fields recursively.
434 if (!shouldReturnTypeInRegister(FD->getType(), Context))
435 return false;
436 }
437
438 return true;
439}
440
441ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000442 ASTContext &Context,
443 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000444 if (RetTy->isVoidType()) {
445 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000446 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000447 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000448 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000449 uint64_t Size = Context.getTypeSize(RetTy);
450
451 // 128-bit vectors are a special case; they are returned in
452 // registers and we need to make sure to pick a type the LLVM
453 // backend will like.
454 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000455 return ABIArgInfo::getCoerce(llvm::VectorType::get(
456 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000457
458 // Always return in register if it fits in a general purpose
459 // register, or if it is 64 bits and has a single element.
460 if ((Size == 8 || Size == 16 || Size == 32) ||
461 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000462 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000463
464 return ABIArgInfo::getIndirect(0);
465 }
466
467 return ABIArgInfo::getDirect();
468 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000469 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000470 // Structures with either a non-trivial destructor or a non-trivial
471 // copy constructor are always indirect.
472 if (hasNonTrivialDestructorOrCopyConstructor(RT))
473 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
474
475 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000476 if (RT->getDecl()->hasFlexibleArrayMember())
477 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000478 }
479
David Chisnall1e4249c2009-08-17 23:08:21 +0000480 // If specified, structs and unions are always indirect.
481 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000482 return ABIArgInfo::getIndirect(0);
483
484 // Classify "single element" structs as their element type.
485 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000486 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000487 if (BT->isIntegerType()) {
488 // We need to use the size of the structure, padding
489 // bit-fields can adjust that to be larger than the single
490 // element type.
491 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000492 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000493 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494 } else if (BT->getKind() == BuiltinType::Float) {
495 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
496 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000497 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498 } else if (BT->getKind() == BuiltinType::Double) {
499 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
500 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000501 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000502 }
503 } else if (SeltTy->isPointerType()) {
504 // FIXME: It would be really nice if this could come out as the proper
505 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000506 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000507 return ABIArgInfo::getCoerce(PtrTy);
508 } else if (SeltTy->isVectorType()) {
509 // 64- and 128-bit vectors are never returned in a
510 // register when inside a structure.
511 uint64_t Size = Context.getTypeSize(RetTy);
512 if (Size == 64 || Size == 128)
513 return ABIArgInfo::getIndirect(0);
514
Owen Andersona1cf15f2009-07-14 23:10:40 +0000515 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000516 }
517 }
518
519 // Small structures which are register sized are generally returned
520 // in a register.
521 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
522 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000523 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000524 }
525
526 return ABIArgInfo::getIndirect(0);
527 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000528 // Treat an enum type as its underlying type.
529 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
530 RetTy = EnumTy->getDecl()->getIntegerType();
531
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000532 return (RetTy->isPromotableIntegerType() ?
533 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000534 }
535}
536
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000537ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
538 ASTContext &Context,
539 bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000540 if (!ByVal)
541 return ABIArgInfo::getIndirect(0, false);
542
543 // Compute the byval alignment. We trust the back-end to honor the
544 // minimum ABI alignment for byval, to make cleaner IR.
545 const unsigned MinABIAlign = 4;
546 unsigned Align = Context.getTypeAlign(Ty) / 8;
547 if (Align > MinABIAlign)
548 return ABIArgInfo::getIndirect(Align);
549 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000550}
551
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000552ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000553 ASTContext &Context,
554 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000555 // FIXME: Set alignment on indirect arguments.
556 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
557 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000558 if (const RecordType *RT = Ty->getAs<RecordType>()) {
559 // Structures with either a non-trivial destructor or a non-trivial
560 // copy constructor are always indirect.
561 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000562 return getIndirectResult(Ty, Context, /*ByVal=*/false);
563
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000564 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000565 return getIndirectResult(Ty, Context);
Anders Carlssona8874232010-01-27 03:25:19 +0000566 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000567
568 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000569 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000570 return ABIArgInfo::getIgnore();
571
Daniel Dunbar53012f42009-11-09 01:33:53 +0000572 // Expand small (<= 128-bit) record types when we know that the stack layout
573 // of those arguments will match the struct. This is important because the
574 // LLVM backend isn't smart enough to remove byval, which inhibits many
575 // optimizations.
576 if (Context.getTypeSize(Ty) <= 4*32 &&
577 canExpandIndirectArgument(Ty, Context))
578 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000579
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000580 return getIndirectResult(Ty, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000581 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000582 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
583 Ty = EnumTy->getDecl()->getIntegerType();
584
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000585 return (Ty->isPromotableIntegerType() ?
586 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000587 }
588}
589
590llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
591 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000592 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000593 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000594
595 CGBuilderTy &Builder = CGF.Builder;
596 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
597 "ap");
598 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
599 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000600 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000601 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
602
603 uint64_t Offset =
604 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
605 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000606 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607 "ap.next");
608 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
609
610 return AddrTyped;
611}
612
Charles Davis74f72932010-02-13 15:54:06 +0000613void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
614 llvm::GlobalValue *GV,
615 CodeGen::CodeGenModule &CGM) const {
616 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
617 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
618 // Get the LLVM function.
619 llvm::Function *Fn = cast<llvm::Function>(GV);
620
621 // Now add the 'alignstack' attribute with a value of 16.
622 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
623 }
624 }
625}
626
John McCall6374c332010-03-06 00:35:14 +0000627bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
628 CodeGen::CodeGenFunction &CGF,
629 llvm::Value *Address) const {
630 CodeGen::CGBuilderTy &Builder = CGF.Builder;
631 llvm::LLVMContext &Context = CGF.getLLVMContext();
632
633 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
634 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
635
636 // 0-7 are the eight integer registers; the order is different
637 // on Darwin (for EH), but the range is the same.
638 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000639 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000640
641 if (CGF.CGM.isTargetDarwin()) {
642 // 12-16 are st(0..4). Not sure why we stop at 4.
643 // These have size 16, which is sizeof(long double) on
644 // platforms with 8-byte alignment for that type.
645 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000646 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCall6374c332010-03-06 00:35:14 +0000647
648 } else {
649 // 9 is %eflags, which doesn't get a size on Darwin for some
650 // reason.
651 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
652
653 // 11-16 are st(0..5). Not sure why we stop at 5.
654 // These have size 12, which is sizeof(long double) on
655 // platforms with 4-byte alignment for that type.
656 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000657 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
658 }
John McCall6374c332010-03-06 00:35:14 +0000659
660 return false;
661}
662
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000663//===----------------------------------------------------------------------===//
664// X86-64 ABI Implementation
665//===----------------------------------------------------------------------===//
666
667
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000668namespace {
669/// X86_64ABIInfo - The X86_64 ABI information.
670class X86_64ABIInfo : public ABIInfo {
671 enum Class {
672 Integer = 0,
673 SSE,
674 SSEUp,
675 X87,
676 X87Up,
677 ComplexX87,
678 NoClass,
679 Memory
680 };
681
682 /// merge - Implement the X86_64 ABI merging algorithm.
683 ///
684 /// Merge an accumulating classification \arg Accum with a field
685 /// classification \arg Field.
686 ///
687 /// \param Accum - The accumulating classification. This should
688 /// always be either NoClass or the result of a previous merge
689 /// call. In addition, this should never be Memory (the caller
690 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000691 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000692
693 /// classify - Determine the x86_64 register classes in which the
694 /// given type T should be passed.
695 ///
696 /// \param Lo - The classification for the parts of the type
697 /// residing in the low word of the containing object.
698 ///
699 /// \param Hi - The classification for the parts of the type
700 /// residing in the high word of the containing object.
701 ///
702 /// \param OffsetBase - The bit offset of this type in the
703 /// containing object. Some parameters are classified different
704 /// depending on whether they straddle an eightbyte boundary.
705 ///
706 /// If a word is unused its result will be NoClass; if a type should
707 /// be passed in Memory then at least the classification of \arg Lo
708 /// will be Memory.
709 ///
710 /// The \arg Lo class will be NoClass iff the argument is ignored.
711 ///
712 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
713 /// also be ComplexX87.
714 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
715 Class &Lo, Class &Hi) const;
716
717 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
718 /// to coerce to, chose the best way to pass Ty in the same place
719 /// that \arg CoerceTo would be passed, but while keeping the
720 /// emitted code as simple as possible.
721 ///
722 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
723 /// the ways we might want to pass things, instead of constructing an LLVM
724 /// type. This makes this code more explicit, and it makes it clearer that we
725 /// are also doing this for correctness in the case of passing scalar types.
726 ABIArgInfo getCoerceResult(QualType Ty,
727 const llvm::Type *CoerceTo,
728 ASTContext &Context) const;
729
730 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000731 /// such that the argument will be returned in memory.
732 ABIArgInfo getIndirectReturnResult(QualType Ty, ASTContext &Context) const;
733
734 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000735 /// such that the argument will be passed in memory.
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000736 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737
738 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000739 ASTContext &Context,
740 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000741
742 ABIArgInfo classifyArgumentType(QualType Ty,
743 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000744 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000745 unsigned &neededInt,
746 unsigned &neededSSE) const;
747
748public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000749 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
750 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000751
752 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
753 CodeGenFunction &CGF) const;
754};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000755
756class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
757public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000758 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCall6374c332010-03-06 00:35:14 +0000759
760 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
761 return 7;
762 }
763
764 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
765 llvm::Value *Address) const {
766 CodeGen::CGBuilderTy &Builder = CGF.Builder;
767 llvm::LLVMContext &Context = CGF.getLLVMContext();
768
769 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
770 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
771
John McCallaeeb7012010-05-27 06:19:26 +0000772 // 0-15 are the 16 integer registers.
773 // 16 is %rip.
774 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000775
776 return false;
777 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000778};
779
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000780}
781
Chris Lattner1090a9b2010-06-28 21:43:59 +0000782X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000783 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
784 // classified recursively so that always two fields are
785 // considered. The resulting class is calculated according to
786 // the classes of the fields in the eightbyte:
787 //
788 // (a) If both classes are equal, this is the resulting class.
789 //
790 // (b) If one of the classes is NO_CLASS, the resulting class is
791 // the other class.
792 //
793 // (c) If one of the classes is MEMORY, the result is the MEMORY
794 // class.
795 //
796 // (d) If one of the classes is INTEGER, the result is the
797 // INTEGER.
798 //
799 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
800 // MEMORY is used as class.
801 //
802 // (f) Otherwise class SSE is used.
803
804 // Accum should never be memory (we should have returned) or
805 // ComplexX87 (because this cannot be passed in a structure).
806 assert((Accum != Memory && Accum != ComplexX87) &&
807 "Invalid accumulated classification during merge.");
808 if (Accum == Field || Field == NoClass)
809 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000810 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000811 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000812 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000813 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000814 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000815 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000816 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
817 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000818 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000819 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000820}
821
822void X86_64ABIInfo::classify(QualType Ty,
823 ASTContext &Context,
824 uint64_t OffsetBase,
825 Class &Lo, Class &Hi) const {
826 // FIXME: This code can be simplified by introducing a simple value class for
827 // Class pairs with appropriate constructor methods for the various
828 // situations.
829
830 // FIXME: Some of the split computations are wrong; unaligned vectors
831 // shouldn't be passed in registers for example, so there is no chance they
832 // can straddle an eightbyte. Verify & simplify.
833
834 Lo = Hi = NoClass;
835
836 Class &Current = OffsetBase < 64 ? Lo : Hi;
837 Current = Memory;
838
John McCall183700f2009-09-21 23:43:11 +0000839 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000840 BuiltinType::Kind k = BT->getKind();
841
842 if (k == BuiltinType::Void) {
843 Current = NoClass;
844 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
845 Lo = Integer;
846 Hi = Integer;
847 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
848 Current = Integer;
849 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
850 Current = SSE;
851 } else if (k == BuiltinType::LongDouble) {
852 Lo = X87;
853 Hi = X87Up;
854 }
855 // FIXME: _Decimal32 and _Decimal64 are SSE.
856 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000857 return;
858 }
859
860 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000861 // Classify the underlying integer type.
862 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000863 return;
864 }
865
866 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000867 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000868 return;
869 }
870
871 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000872 if (Ty->isMemberFunctionPointerType())
873 Lo = Hi = Integer;
874 else
875 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000876 return;
877 }
878
879 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000880 uint64_t Size = Context.getTypeSize(VT);
881 if (Size == 32) {
882 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
883 // float> as integer.
884 Current = Integer;
885
886 // If this type crosses an eightbyte boundary, it should be
887 // split.
888 uint64_t EB_Real = (OffsetBase) / 64;
889 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
890 if (EB_Real != EB_Imag)
891 Hi = Lo;
892 } else if (Size == 64) {
893 // gcc passes <1 x double> in memory. :(
894 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
895 return;
896
897 // gcc passes <1 x long long> as INTEGER.
898 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
899 Current = Integer;
900 else
901 Current = SSE;
902
903 // If this type crosses an eightbyte boundary, it should be
904 // split.
905 if (OffsetBase && OffsetBase != 64)
906 Hi = Lo;
907 } else if (Size == 128) {
908 Lo = SSE;
909 Hi = SSEUp;
910 }
Chris Lattner1090a9b2010-06-28 21:43:59 +0000911 return;
912 }
913
914 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000915 QualType ET = Context.getCanonicalType(CT->getElementType());
916
917 uint64_t Size = Context.getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000918 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000919 if (Size <= 64)
920 Current = Integer;
921 else if (Size <= 128)
922 Lo = Hi = Integer;
923 } else if (ET == Context.FloatTy)
924 Current = SSE;
925 else if (ET == Context.DoubleTy)
926 Lo = Hi = SSE;
927 else if (ET == Context.LongDoubleTy)
928 Current = ComplexX87;
929
930 // If this complex type crosses an eightbyte boundary then it
931 // should be split.
932 uint64_t EB_Real = (OffsetBase) / 64;
933 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
934 if (Hi == NoClass && EB_Real != EB_Imag)
935 Hi = Lo;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000936
937 return;
938 }
939
940 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000941 // Arrays are treated like structures.
942
943 uint64_t Size = Context.getTypeSize(Ty);
944
945 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
946 // than two eightbytes, ..., it has class MEMORY.
947 if (Size > 128)
948 return;
949
950 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
951 // fields, it has class MEMORY.
952 //
953 // Only need to check alignment of array base.
954 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
955 return;
956
957 // Otherwise implement simplified merge. We could be smarter about
958 // this, but it isn't worth it and would be harder to verify.
959 Current = NoClass;
960 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
961 uint64_t ArraySize = AT->getSize().getZExtValue();
962 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
963 Class FieldLo, FieldHi;
964 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
965 Lo = merge(Lo, FieldLo);
966 Hi = merge(Hi, FieldHi);
967 if (Lo == Memory || Hi == Memory)
968 break;
969 }
970
971 // Do post merger cleanup (see below). Only case we worry about is Memory.
972 if (Hi == Memory)
973 Lo = Memory;
974 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +0000975 return;
976 }
977
978 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000979 uint64_t Size = Context.getTypeSize(Ty);
980
981 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
982 // than two eightbytes, ..., it has class MEMORY.
983 if (Size > 128)
984 return;
985
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000986 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
987 // copy constructor or a non-trivial destructor, it is passed by invisible
988 // reference.
989 if (hasNonTrivialDestructorOrCopyConstructor(RT))
990 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000991
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000992 const RecordDecl *RD = RT->getDecl();
993
994 // Assume variable sized types are passed in memory.
995 if (RD->hasFlexibleArrayMember())
996 return;
997
998 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
999
1000 // Reset Lo class, this will be recomputed.
1001 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001002
1003 // If this is a C++ record, classify the bases first.
1004 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1005 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1006 e = CXXRD->bases_end(); i != e; ++i) {
1007 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1008 "Unexpected base class!");
1009 const CXXRecordDecl *Base =
1010 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1011
1012 // Classify this field.
1013 //
1014 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1015 // single eightbyte, each is classified separately. Each eightbyte gets
1016 // initialized to class NO_CLASS.
1017 Class FieldLo, FieldHi;
1018 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
1019 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1020 Lo = merge(Lo, FieldLo);
1021 Hi = merge(Hi, FieldHi);
1022 if (Lo == Memory || Hi == Memory)
1023 break;
1024 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +00001025
1026 // If this record has no fields but isn't empty, classify as INTEGER.
1027 if (RD->field_empty() && Size)
1028 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001029 }
1030
1031 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001032 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001033 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1034 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001035 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1036 bool BitField = i->isBitField();
1037
1038 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1039 // fields, it has class MEMORY.
1040 //
1041 // Note, skip this test for bit-fields, see below.
1042 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1043 Lo = Memory;
1044 return;
1045 }
1046
1047 // Classify this field.
1048 //
1049 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1050 // exceeds a single eightbyte, each is classified
1051 // separately. Each eightbyte gets initialized to class
1052 // NO_CLASS.
1053 Class FieldLo, FieldHi;
1054
1055 // Bit-fields require special handling, they do not force the
1056 // structure to be passed in memory even if unaligned, and
1057 // therefore they can straddle an eightbyte.
1058 if (BitField) {
1059 // Ignore padding bit-fields.
1060 if (i->isUnnamedBitfield())
1061 continue;
1062
1063 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1064 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1065
1066 uint64_t EB_Lo = Offset / 64;
1067 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1068 FieldLo = FieldHi = NoClass;
1069 if (EB_Lo) {
1070 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1071 FieldLo = NoClass;
1072 FieldHi = Integer;
1073 } else {
1074 FieldLo = Integer;
1075 FieldHi = EB_Hi ? Integer : NoClass;
1076 }
1077 } else
1078 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1079 Lo = merge(Lo, FieldLo);
1080 Hi = merge(Hi, FieldHi);
1081 if (Lo == Memory || Hi == Memory)
1082 break;
1083 }
1084
1085 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1086 //
1087 // (a) If one of the classes is MEMORY, the whole argument is
1088 // passed in memory.
1089 //
1090 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1091
1092 // The first of these conditions is guaranteed by how we implement
1093 // the merge (just bail).
1094 //
1095 // The second condition occurs in the case of unions; for example
1096 // union { _Complex double; unsigned; }.
1097 if (Hi == Memory)
1098 Lo = Memory;
1099 if (Hi == SSEUp && Lo != SSE)
1100 Hi = SSE;
1101 }
1102}
1103
1104ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1105 const llvm::Type *CoerceTo,
1106 ASTContext &Context) const {
Chris Lattner7f215c12010-06-26 21:52:32 +00001107 if (CoerceTo->isIntegerTy(64)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001108 // Integer and pointer types will end up in a general purpose
1109 // register.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001110
1111 // Treat an enum type as its underlying type.
1112 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1113 Ty = EnumTy->getDecl()->getIntegerType();
1114
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001115 if (Ty->isIntegralOrEnumerationType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001116 return (Ty->isPromotableIntegerType() ?
1117 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001118
Chris Lattner8ff29642010-06-28 21:59:07 +00001119 // If this is a 8/16/32-bit structure that is passed as an int64, then it
1120 // will be passed in the low 8/16/32-bits of a 64-bit GPR, which is the same
1121 // as how an i8/i16/i32 is passed. Coerce to a i8/i16/i32 instead of a i64.
1122 switch (Context.getTypeSizeInChars(Ty).getQuantity()) {
1123 default: break;
1124 case 1: CoerceTo = llvm::Type::getInt8Ty(CoerceTo->getContext()); break;
1125 case 2: CoerceTo = llvm::Type::getInt16Ty(CoerceTo->getContext()); break;
1126 case 4: CoerceTo = llvm::Type::getInt32Ty(CoerceTo->getContext()); break;
1127 }
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001128
Chris Lattner7f215c12010-06-26 21:52:32 +00001129 } else if (CoerceTo->isDoubleTy()) {
John McCall0b0ef0a2010-02-24 07:14:12 +00001130 assert(Ty.isCanonical() && "should always have a canonical type here");
1131 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001132
1133 // Float and double end up in a single SSE reg.
John McCall0b0ef0a2010-02-24 07:14:12 +00001134 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001135 return ABIArgInfo::getDirect();
1136
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001137 // If this is a 32-bit structure that is passed as a double, then it will be
1138 // passed in the low 32-bits of the XMM register, which is the same as how a
1139 // float is passed. Coerce to a float instead of a double.
1140 if (Context.getTypeSizeInChars(Ty).getQuantity() == 4)
1141 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001142 }
1143
1144 return ABIArgInfo::getCoerce(CoerceTo);
1145}
1146
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001147ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1148 ASTContext &Context) const {
1149 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1150 // place naturally.
1151 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1152 // Treat an enum type as its underlying type.
1153 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1154 Ty = EnumTy->getDecl()->getIntegerType();
1155
1156 return (Ty->isPromotableIntegerType() ?
1157 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1158 }
1159
1160 return ABIArgInfo::getIndirect(0);
1161}
1162
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001163ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1164 ASTContext &Context) const {
1165 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1166 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001167 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1168 // Treat an enum type as its underlying type.
1169 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1170 Ty = EnumTy->getDecl()->getIntegerType();
1171
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001172 return (Ty->isPromotableIntegerType() ?
1173 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001174 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001175
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001176 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1177 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001178
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001179 // Compute the byval alignment. We trust the back-end to honor the
1180 // minimum ABI alignment for byval, to make cleaner IR.
1181 const unsigned MinABIAlign = 8;
1182 unsigned Align = Context.getTypeAlign(Ty) / 8;
1183 if (Align > MinABIAlign)
1184 return ABIArgInfo::getIndirect(Align);
1185 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001186}
1187
Chris Lattner1090a9b2010-06-28 21:43:59 +00001188ABIArgInfo X86_64ABIInfo::
1189classifyReturnType(QualType RetTy, ASTContext &Context,
1190 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001191 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1192 // classification algorithm.
1193 X86_64ABIInfo::Class Lo, Hi;
1194 classify(RetTy, Context, 0, Lo, Hi);
1195
1196 // Check some invariants.
1197 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1198 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1199 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1200
1201 const llvm::Type *ResType = 0;
1202 switch (Lo) {
1203 case NoClass:
1204 return ABIArgInfo::getIgnore();
1205
1206 case SSEUp:
1207 case X87Up:
1208 assert(0 && "Invalid classification for lo word.");
1209
1210 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1211 // hidden argument.
1212 case Memory:
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001213 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001214
1215 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1216 // available register of the sequence %rax, %rdx is used.
1217 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001218 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001219
1220 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1221 // available SSE register of the sequence %xmm0, %xmm1 is used.
1222 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001223 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001224
1225 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1226 // returned on the X87 stack in %st0 as 80-bit x87 number.
1227 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001228 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001229
1230 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1231 // part of the value is returned in %st0 and the imaginary part in
1232 // %st1.
1233 case ComplexX87:
1234 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner52d9ae32010-04-06 17:29:22 +00001235 ResType = llvm::StructType::get(VMContext,
1236 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson0032b272009-08-13 21:57:51 +00001237 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001238 NULL);
1239 break;
1240 }
1241
1242 switch (Hi) {
1243 // Memory was handled previously and X87 should
1244 // never occur as a hi class.
1245 case Memory:
1246 case X87:
1247 assert(0 && "Invalid classification for hi word.");
1248
1249 case ComplexX87: // Previously handled.
1250 case NoClass: break;
1251
1252 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001253 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001254 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001255 break;
1256 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001257 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001258 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001259 break;
1260
1261 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1262 // is passed in the upper half of the last used SSE register.
1263 //
1264 // SSEUP should always be preceeded by SSE, just widen.
1265 case SSEUp:
1266 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001267 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001268 break;
1269
1270 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1271 // returned together with the previous X87 value in %st0.
1272 case X87Up:
1273 // If X87Up is preceeded by X87, we don't need to do
1274 // anything. However, in some cases with unions it may not be
1275 // preceeded by X87. In such situations we follow gcc and pass the
1276 // extra bits in an SSE reg.
1277 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001278 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001279 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001280 break;
1281 }
1282
1283 return getCoerceResult(RetTy, ResType, Context);
1284}
1285
1286ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001287 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001288 unsigned &neededInt,
1289 unsigned &neededSSE) const {
1290 X86_64ABIInfo::Class Lo, Hi;
1291 classify(Ty, Context, 0, Lo, Hi);
1292
1293 // Check some invariants.
1294 // FIXME: Enforce these by construction.
1295 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1296 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1297 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1298
1299 neededInt = 0;
1300 neededSSE = 0;
1301 const llvm::Type *ResType = 0;
1302 switch (Lo) {
1303 case NoClass:
1304 return ABIArgInfo::getIgnore();
1305
1306 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1307 // on the stack.
1308 case Memory:
1309
1310 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1311 // COMPLEX_X87, it is passed in memory.
1312 case X87:
1313 case ComplexX87:
1314 return getIndirectResult(Ty, Context);
1315
1316 case SSEUp:
1317 case X87Up:
1318 assert(0 && "Invalid classification for lo word.");
1319
1320 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1321 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1322 // and %r9 is used.
1323 case Integer:
1324 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001325 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001326 break;
1327
1328 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1329 // available SSE register is used, the registers are taken in the
1330 // order from %xmm0 to %xmm7.
1331 case SSE:
1332 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001333 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001334 break;
1335 }
1336
1337 switch (Hi) {
1338 // Memory was handled previously, ComplexX87 and X87 should
1339 // never occur as hi classes, and X87Up must be preceed by X87,
1340 // which is passed in memory.
1341 case Memory:
1342 case X87:
1343 case ComplexX87:
1344 assert(0 && "Invalid classification for hi word.");
1345 break;
1346
1347 case NoClass: break;
1348 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001349 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001350 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001351 ++neededInt;
1352 break;
1353
1354 // X87Up generally doesn't occur here (long double is passed in
1355 // memory), except in situations involving unions.
1356 case X87Up:
1357 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001358 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001359 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001360 ++neededSSE;
1361 break;
1362
1363 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1364 // eightbyte is passed in the upper half of the last used SSE
1365 // register.
1366 case SSEUp:
1367 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001368 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001369 break;
1370 }
1371
1372 return getCoerceResult(Ty, ResType, Context);
1373}
1374
Owen Andersona1cf15f2009-07-14 23:10:40 +00001375void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1376 llvm::LLVMContext &VMContext) const {
1377 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1378 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001379
1380 // Keep track of the number of assigned registers.
1381 unsigned freeIntRegs = 6, freeSSERegs = 8;
1382
1383 // If the return value is indirect, then the hidden argument is consuming one
1384 // integer register.
1385 if (FI.getReturnInfo().isIndirect())
1386 --freeIntRegs;
1387
1388 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1389 // get assigned (in left-to-right order) for passing as follows...
1390 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1391 it != ie; ++it) {
1392 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001393 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001394 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001395
1396 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1397 // eightbyte of an argument, the whole argument is passed on the
1398 // stack. If registers have already been assigned for some
1399 // eightbytes of such an argument, the assignments get reverted.
1400 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1401 freeIntRegs -= neededInt;
1402 freeSSERegs -= neededSSE;
1403 } else {
1404 it->info = getIndirectResult(it->type, Context);
1405 }
1406 }
1407}
1408
1409static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1410 QualType Ty,
1411 CodeGenFunction &CGF) {
1412 llvm::Value *overflow_arg_area_p =
1413 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1414 llvm::Value *overflow_arg_area =
1415 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1416
1417 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1418 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1419 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1420 if (Align > 8) {
1421 // Note that we follow the ABI & gcc here, even though the type
1422 // could in theory have an alignment greater than 16. This case
1423 // shouldn't ever matter in practice.
1424
1425 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001426 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001427 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001428 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1429 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001430 CGF.Int64Ty);
1431 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001432 overflow_arg_area =
1433 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1434 overflow_arg_area->getType(),
1435 "overflow_arg_area.align");
1436 }
1437
1438 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1439 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1440 llvm::Value *Res =
1441 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001442 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001443
1444 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1445 // l->overflow_arg_area + sizeof(type).
1446 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1447 // an 8 byte boundary.
1448
1449 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001450 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001451 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001452 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1453 "overflow_arg_area.next");
1454 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1455
1456 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1457 return Res;
1458}
1459
1460llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1461 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001462 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001464 // Assume that va_list type is correct; should be pointer to LLVM type:
1465 // struct {
1466 // i32 gp_offset;
1467 // i32 fp_offset;
1468 // i8* overflow_arg_area;
1469 // i8* reg_save_area;
1470 // };
1471 unsigned neededInt, neededSSE;
Chris Lattnera14db752010-03-11 18:19:55 +00001472
1473 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001474 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001475 neededInt, neededSSE);
1476
1477 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1478 // in the registers. If not go to step 7.
1479 if (!neededInt && !neededSSE)
1480 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1481
1482 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1483 // general purpose registers needed to pass type and num_fp to hold
1484 // the number of floating point registers needed.
1485
1486 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1487 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1488 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1489 //
1490 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1491 // register save space).
1492
1493 llvm::Value *InRegs = 0;
1494 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1495 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1496 if (neededInt) {
1497 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1498 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001499 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1500 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001501 }
1502
1503 if (neededSSE) {
1504 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1505 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1506 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001507 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1508 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1510 }
1511
1512 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1513 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1514 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1515 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1516
1517 // Emit code to load the value if it was passed in registers.
1518
1519 CGF.EmitBlock(InRegBlock);
1520
1521 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1522 // an offset of l->gp_offset and/or l->fp_offset. This may require
1523 // copying to a temporary location in case the parameter is passed
1524 // in different register classes or requires an alignment greater
1525 // than 8 for general purpose registers and 16 for XMM registers.
1526 //
1527 // FIXME: This really results in shameful code when we end up needing to
1528 // collect arguments from different places; often what should result in a
1529 // simple assembling of a structure from scattered addresses has many more
1530 // loads than necessary. Can we clean this up?
1531 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1532 llvm::Value *RegAddr =
1533 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1534 "reg_save_area");
1535 if (neededInt && neededSSE) {
1536 // FIXME: Cleanup.
1537 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1538 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1539 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1540 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1541 const llvm::Type *TyLo = ST->getElementType(0);
1542 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001543 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001544 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001545 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1546 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1548 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001549 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1550 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001551 llvm::Value *V =
1552 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1553 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1554 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1555 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1556
Owen Andersona1cf15f2009-07-14 23:10:40 +00001557 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001558 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001559 } else if (neededInt) {
1560 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1561 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001562 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001563 } else if (neededSSE == 1) {
1564 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1565 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1566 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001567 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001568 assert(neededSSE == 2 && "Invalid number of needed registers!");
1569 // SSE registers are spaced 16 bytes apart in the register save
1570 // area, we need to collect the two eightbytes together.
1571 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001572 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001573 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1574 const llvm::Type *DblPtrTy =
1575 llvm::PointerType::getUnqual(DoubleTy);
1576 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1577 DoubleTy, NULL);
1578 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1579 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1580 DblPtrTy));
1581 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1582 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1583 DblPtrTy));
1584 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1585 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1586 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001587 }
1588
1589 // AMD64-ABI 3.5.7p5: Step 5. Set:
1590 // l->gp_offset = l->gp_offset + num_gp * 8
1591 // l->fp_offset = l->fp_offset + num_fp * 16.
1592 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001593 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001594 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1595 gp_offset_p);
1596 }
1597 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001598 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001599 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1600 fp_offset_p);
1601 }
1602 CGF.EmitBranch(ContBlock);
1603
1604 // Emit code to load the value if it was passed in memory.
1605
1606 CGF.EmitBlock(InMemBlock);
1607 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1608
1609 // Return the appropriate result.
1610
1611 CGF.EmitBlock(ContBlock);
1612 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1613 "vaarg.addr");
1614 ResAddr->reserveOperandSpace(2);
1615 ResAddr->addIncoming(RegAddr, InRegBlock);
1616 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001617 return ResAddr;
1618}
1619
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001620
1621
1622//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001623// PIC16 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001624//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001625
1626namespace {
1627
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001628class PIC16ABIInfo : public ABIInfo {
1629 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001630 ASTContext &Context,
1631 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001632
1633 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001634 ASTContext &Context,
1635 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001636
Owen Andersona1cf15f2009-07-14 23:10:40 +00001637 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1638 llvm::LLVMContext &VMContext) const {
1639 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1640 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001641 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1642 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001643 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001644 }
1645
1646 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1647 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001648};
1649
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001650class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1651public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001652 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001653};
1654
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001655}
1656
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001657ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001658 ASTContext &Context,
1659 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001660 if (RetTy->isVoidType()) {
1661 return ABIArgInfo::getIgnore();
1662 } else {
1663 return ABIArgInfo::getDirect();
1664 }
1665}
1666
1667ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001668 ASTContext &Context,
1669 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001670 return ABIArgInfo::getDirect();
1671}
1672
1673llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00001674 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00001675 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001676 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1677
1678 CGBuilderTy &Builder = CGF.Builder;
1679 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1680 "ap");
1681 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1682 llvm::Type *PTy =
1683 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1684 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1685
1686 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1687
1688 llvm::Value *NextAddr =
1689 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1690 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1691 "ap.next");
1692 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1693
1694 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001695}
1696
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001697
John McCallec853ba2010-03-11 00:10:12 +00001698// PowerPC-32
1699
1700namespace {
1701class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1702public:
1703 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1704 // This is recovered from gcc output.
1705 return 1; // r1 is the dedicated stack pointer
1706 }
1707
1708 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1709 llvm::Value *Address) const;
1710};
1711
1712}
1713
1714bool
1715PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1716 llvm::Value *Address) const {
1717 // This is calculated from the LLVM and GCC tables and verified
1718 // against gcc output. AFAIK all ABIs use the same encoding.
1719
1720 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1721 llvm::LLVMContext &Context = CGF.getLLVMContext();
1722
1723 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1724 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1725 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1726 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1727
1728 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00001729 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00001730
1731 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00001732 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00001733
1734 // 64-76 are various 4-byte special-purpose registers:
1735 // 64: mq
1736 // 65: lr
1737 // 66: ctr
1738 // 67: ap
1739 // 68-75 cr0-7
1740 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00001741 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00001742
1743 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00001744 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00001745
1746 // 109: vrsave
1747 // 110: vscr
1748 // 111: spe_acc
1749 // 112: spefscr
1750 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00001751 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00001752
1753 return false;
1754}
1755
1756
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001757//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001758// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001759//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001760
1761namespace {
1762
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001763class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001764public:
1765 enum ABIKind {
1766 APCS = 0,
1767 AAPCS = 1,
1768 AAPCS_VFP
1769 };
1770
1771private:
1772 ABIKind Kind;
1773
1774public:
1775 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1776
1777private:
1778 ABIKind getABIKind() const { return Kind; }
1779
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001780 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001781 ASTContext &Context,
1782 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001783
1784 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001785 ASTContext &Context,
1786 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001787
Owen Andersona1cf15f2009-07-14 23:10:40 +00001788 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1789 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001790
1791 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1792 CodeGenFunction &CGF) const;
1793};
1794
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001795class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1796public:
1797 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001798 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCall6374c332010-03-06 00:35:14 +00001799
1800 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1801 return 13;
1802 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001803};
1804
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001805}
1806
Owen Andersona1cf15f2009-07-14 23:10:40 +00001807void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1808 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001809 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001810 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001811 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1812 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001813 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001814 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001815
Rafael Espindola25117ab2010-06-16 16:13:39 +00001816 const llvm::Triple &Triple(Context.Target.getTriple());
1817 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001818 if (Triple.getEnvironmentName() == "gnueabi" ||
1819 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00001820 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001821 else
1822 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00001823
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001824 switch (getABIKind()) {
1825 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001826 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1827 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001828 break;
1829
1830 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001831 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1832 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001833 break;
1834
1835 case AAPCS_VFP:
1836 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1837 break;
1838 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001839}
1840
1841ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001842 ASTContext &Context,
1843 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001844 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1845 // Treat an enum type as its underlying type.
1846 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1847 Ty = EnumTy->getDecl()->getIntegerType();
1848
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001849 return (Ty->isPromotableIntegerType() ?
1850 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001851 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001852
Daniel Dunbar42025572009-09-14 21:54:03 +00001853 // Ignore empty records.
1854 if (isEmptyRecord(Context, Ty, true))
1855 return ABIArgInfo::getIgnore();
1856
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001857 // Structures with either a non-trivial destructor or a non-trivial
1858 // copy constructor are always indirect.
1859 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1860 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1861
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001862 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1863 // backend doesn't support byval.
1864 // FIXME: This doesn't handle alignment > 64 bits.
1865 const llvm::Type* ElemTy;
1866 unsigned SizeRegs;
1867 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001868 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001869 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1870 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001871 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001872 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1873 }
1874 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001875 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001876 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001877 return ABIArgInfo::getCoerce(STy);
1878}
1879
Daniel Dunbar98303b92009-09-13 08:03:58 +00001880static bool isIntegerLikeType(QualType Ty,
1881 ASTContext &Context,
1882 llvm::LLVMContext &VMContext) {
1883 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1884 // is called integer-like if its size is less than or equal to one word, and
1885 // the offset of each of its addressable sub-fields is zero.
1886
1887 uint64_t Size = Context.getTypeSize(Ty);
1888
1889 // Check that the type fits in a word.
1890 if (Size > 32)
1891 return false;
1892
1893 // FIXME: Handle vector types!
1894 if (Ty->isVectorType())
1895 return false;
1896
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001897 // Float types are never treated as "integer like".
1898 if (Ty->isRealFloatingType())
1899 return false;
1900
Daniel Dunbar98303b92009-09-13 08:03:58 +00001901 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001902 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001903 return true;
1904
Daniel Dunbar45815812010-02-01 23:31:26 +00001905 // Small complex integer types are "integer like".
1906 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1907 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001908
1909 // Single element and zero sized arrays should be allowed, by the definition
1910 // above, but they are not.
1911
1912 // Otherwise, it must be a record type.
1913 const RecordType *RT = Ty->getAs<RecordType>();
1914 if (!RT) return false;
1915
1916 // Ignore records with flexible arrays.
1917 const RecordDecl *RD = RT->getDecl();
1918 if (RD->hasFlexibleArrayMember())
1919 return false;
1920
1921 // Check that all sub-fields are at offset 0, and are themselves "integer
1922 // like".
1923 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1924
1925 bool HadField = false;
1926 unsigned idx = 0;
1927 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1928 i != e; ++i, ++idx) {
1929 const FieldDecl *FD = *i;
1930
Daniel Dunbar679855a2010-01-29 03:22:29 +00001931 // Bit-fields are not addressable, we only need to verify they are "integer
1932 // like". We still have to disallow a subsequent non-bitfield, for example:
1933 // struct { int : 0; int x }
1934 // is non-integer like according to gcc.
1935 if (FD->isBitField()) {
1936 if (!RD->isUnion())
1937 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001938
Daniel Dunbar679855a2010-01-29 03:22:29 +00001939 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1940 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001941
Daniel Dunbar679855a2010-01-29 03:22:29 +00001942 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001943 }
1944
Daniel Dunbar679855a2010-01-29 03:22:29 +00001945 // Check if this field is at offset 0.
1946 if (Layout.getFieldOffset(idx) != 0)
1947 return false;
1948
Daniel Dunbar98303b92009-09-13 08:03:58 +00001949 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1950 return false;
1951
Daniel Dunbar679855a2010-01-29 03:22:29 +00001952 // Only allow at most one field in a structure. This doesn't match the
1953 // wording above, but follows gcc in situations with a field following an
1954 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001955 if (!RD->isUnion()) {
1956 if (HadField)
1957 return false;
1958
1959 HadField = true;
1960 }
1961 }
1962
1963 return true;
1964}
1965
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001966ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001967 ASTContext &Context,
1968 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001969 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001970 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001971
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001972 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1973 // Treat an enum type as its underlying type.
1974 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1975 RetTy = EnumTy->getDecl()->getIntegerType();
1976
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001977 return (RetTy->isPromotableIntegerType() ?
1978 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001979 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001980
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001981 // Structures with either a non-trivial destructor or a non-trivial
1982 // copy constructor are always indirect.
1983 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
1984 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1985
Daniel Dunbar98303b92009-09-13 08:03:58 +00001986 // Are we following APCS?
1987 if (getABIKind() == APCS) {
1988 if (isEmptyRecord(Context, RetTy, false))
1989 return ABIArgInfo::getIgnore();
1990
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00001991 // Complex types are all returned as packed integers.
1992 //
1993 // FIXME: Consider using 2 x vector types if the back end handles them
1994 // correctly.
1995 if (RetTy->isAnyComplexType())
1996 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1997 VMContext, Context.getTypeSize(RetTy)));
1998
Daniel Dunbar98303b92009-09-13 08:03:58 +00001999 // Integer like structures are returned in r0.
2000 if (isIntegerLikeType(RetTy, Context, VMContext)) {
2001 // Return in the smallest viable integer type.
2002 uint64_t Size = Context.getTypeSize(RetTy);
2003 if (Size <= 8)
2004 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2005 if (Size <= 16)
2006 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
2007 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
2008 }
2009
2010 // Otherwise return in memory.
2011 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002012 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002013
2014 // Otherwise this is an AAPCS variant.
2015
Daniel Dunbar16a08082009-09-14 00:56:55 +00002016 if (isEmptyRecord(Context, RetTy, true))
2017 return ABIArgInfo::getIgnore();
2018
Daniel Dunbar98303b92009-09-13 08:03:58 +00002019 // Aggregates <= 4 bytes are returned in r0; other aggregates
2020 // are returned indirectly.
2021 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002022 if (Size <= 32) {
2023 // Return in the smallest viable integer type.
2024 if (Size <= 8)
2025 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2026 if (Size <= 16)
2027 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002028 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002029 }
2030
Daniel Dunbar98303b92009-09-13 08:03:58 +00002031 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002032}
2033
2034llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002035 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002036 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002037 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002038 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002039
2040 CGBuilderTy &Builder = CGF.Builder;
2041 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2042 "ap");
2043 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2044 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002045 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002046 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2047
2048 uint64_t Offset =
2049 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2050 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002051 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002052 "ap.next");
2053 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2054
2055 return AddrTyped;
2056}
2057
2058ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00002059 ASTContext &Context,
2060 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002061 if (RetTy->isVoidType()) {
2062 return ABIArgInfo::getIgnore();
2063 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2064 return ABIArgInfo::getIndirect(0);
2065 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002066 // Treat an enum type as its underlying type.
2067 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2068 RetTy = EnumTy->getDecl()->getIntegerType();
2069
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002070 return (RetTy->isPromotableIntegerType() ?
2071 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002072 }
2073}
2074
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002075//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002076// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002077//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002078
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002079namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002080
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002081class SystemZABIInfo : public ABIInfo {
2082 bool isPromotableIntegerType(QualType Ty) const;
2083
2084 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2085 llvm::LLVMContext &VMContext) const;
2086
2087 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2088 llvm::LLVMContext &VMContext) const;
2089
2090 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
2091 llvm::LLVMContext &VMContext) const {
2092 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2093 Context, VMContext);
2094 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2095 it != ie; ++it)
2096 it->info = classifyArgumentType(it->type, Context, VMContext);
2097 }
2098
2099 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2100 CodeGenFunction &CGF) const;
2101};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002102
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002103class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2104public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002105 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002106};
2107
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002108}
2109
2110bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2111 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002112 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002113 switch (BT->getKind()) {
2114 case BuiltinType::Bool:
2115 case BuiltinType::Char_S:
2116 case BuiltinType::Char_U:
2117 case BuiltinType::SChar:
2118 case BuiltinType::UChar:
2119 case BuiltinType::Short:
2120 case BuiltinType::UShort:
2121 case BuiltinType::Int:
2122 case BuiltinType::UInt:
2123 return true;
2124 default:
2125 return false;
2126 }
2127 return false;
2128}
2129
2130llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2131 CodeGenFunction &CGF) const {
2132 // FIXME: Implement
2133 return 0;
2134}
2135
2136
2137ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2138 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002139 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002140 if (RetTy->isVoidType()) {
2141 return ABIArgInfo::getIgnore();
2142 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2143 return ABIArgInfo::getIndirect(0);
2144 } else {
2145 return (isPromotableIntegerType(RetTy) ?
2146 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2147 }
2148}
2149
2150ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2151 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002152 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002153 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2154 return ABIArgInfo::getIndirect(0);
2155 } else {
2156 return (isPromotableIntegerType(Ty) ?
2157 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2158 }
2159}
2160
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002161//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002162// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002163//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002164
2165namespace {
2166
2167class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2168public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002169 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002170 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2171 CodeGen::CodeGenModule &M) const;
2172};
2173
2174}
2175
2176void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2177 llvm::GlobalValue *GV,
2178 CodeGen::CodeGenModule &M) const {
2179 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2180 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2181 // Handle 'interrupt' attribute:
2182 llvm::Function *F = cast<llvm::Function>(GV);
2183
2184 // Step 1: Set ISR calling convention.
2185 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2186
2187 // Step 2: Add attributes goodness.
2188 F->addFnAttr(llvm::Attribute::NoInline);
2189
2190 // Step 3: Emit ISR vector alias.
2191 unsigned Num = attr->getNumber() + 0xffe0;
2192 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2193 "vector_" +
2194 llvm::LowercaseString(llvm::utohexstr(Num)),
2195 GV, &M.getModule());
2196 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002197 }
2198}
2199
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002200//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002201// MIPS ABI Implementation. This works for both little-endian and
2202// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002203//===----------------------------------------------------------------------===//
2204
John McCallaeeb7012010-05-27 06:19:26 +00002205namespace {
2206class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2207public:
2208 MIPSTargetCodeGenInfo(): TargetCodeGenInfo(new DefaultABIInfo()) {}
2209
2210 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2211 return 29;
2212 }
2213
2214 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2215 llvm::Value *Address) const;
2216};
2217}
2218
2219bool
2220MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2221 llvm::Value *Address) const {
2222 // This information comes from gcc's implementation, which seems to
2223 // as canonical as it gets.
2224
2225 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2226 llvm::LLVMContext &Context = CGF.getLLVMContext();
2227
2228 // Everything on MIPS is 4 bytes. Double-precision FP registers
2229 // are aliased to pairs of single-precision FP registers.
2230 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2231 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2232
2233 // 0-31 are the general purpose registers, $0 - $31.
2234 // 32-63 are the floating-point registers, $f0 - $f31.
2235 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2236 // 66 is the (notional, I think) register for signal-handler return.
2237 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2238
2239 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2240 // They are one bit wide and ignored here.
2241
2242 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2243 // (coprocessor 1 is the FP unit)
2244 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2245 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2246 // 176-181 are the DSP accumulator registers.
2247 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2248
2249 return false;
2250}
2251
2252
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002253const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2254 if (TheTargetCodeGenInfo)
2255 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002256
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002257 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2258 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002259
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002260 const llvm::Triple &Triple(getContext().Target.getTriple());
2261 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002262 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002263 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002264
John McCallaeeb7012010-05-27 06:19:26 +00002265 case llvm::Triple::mips:
2266 case llvm::Triple::mipsel:
2267 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo());
2268
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002269 case llvm::Triple::arm:
2270 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002271 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002272 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002273 return *(TheTargetCodeGenInfo =
2274 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002275
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002276 return *(TheTargetCodeGenInfo =
2277 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002278
2279 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002280 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002281
John McCallec853ba2010-03-11 00:10:12 +00002282 case llvm::Triple::ppc:
2283 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2284
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002285 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002286 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2287
2288 case llvm::Triple::msp430:
2289 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002290
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002291 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002292 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002293 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002294 return *(TheTargetCodeGenInfo =
2295 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002296 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002297 case llvm::Triple::MinGW32:
2298 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002299 case llvm::Triple::AuroraUX:
2300 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002301 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002302 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002303 return *(TheTargetCodeGenInfo =
2304 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002305
2306 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002307 return *(TheTargetCodeGenInfo =
2308 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002309 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002310
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002311 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002312 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002313 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002314}