blob: 292f58eef452e9fbdf85f04446f3adfe2f4ab6d8 [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,
Chris Lattner8640cd62010-06-29 01:08:48 +0000283 llvm::LLVMContext &VMContext,
284 const llvm::Type *const *PrefTypes,
285 unsigned NumPrefTypes) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000286 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
287 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000288 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
289 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000290 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000291 }
292
293 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
294 CodeGenFunction &CGF) const;
295};
296
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000297class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
298public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000299 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000300};
301
302llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
303 CodeGenFunction &CGF) const {
304 return 0;
305}
306
307ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
308 ASTContext &Context,
309 llvm::LLVMContext &VMContext) const {
Chris Lattnera14db752010-03-11 18:19:55 +0000310 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000311 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000312
Chris Lattnera14db752010-03-11 18:19:55 +0000313 // Treat an enum type as its underlying type.
314 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
315 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000316
Chris Lattnera14db752010-03-11 18:19:55 +0000317 return (Ty->isPromotableIntegerType() ?
318 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000319}
320
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000321//===----------------------------------------------------------------------===//
322// X86-32 ABI Implementation
323//===----------------------------------------------------------------------===//
324
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000325/// X86_32ABIInfo - The X86-32 ABI information.
326class X86_32ABIInfo : public ABIInfo {
327 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000328 bool IsDarwinVectorABI;
329 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000330
331 static bool isRegisterSize(unsigned Size) {
332 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
333 }
334
335 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
336
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000337 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
338 /// such that the argument will be passed in memory.
339 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
340 bool ByVal = true) const;
341
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000342public:
343 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000344 ASTContext &Context,
345 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000346
347 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000348 ASTContext &Context,
349 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000350
Owen Andersona1cf15f2009-07-14 23:10:40 +0000351 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +0000352 llvm::LLVMContext &VMContext,
353 const llvm::Type *const *PrefTypes,
354 unsigned NumPrefTypes) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000355 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
356 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000357 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
358 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000359 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000360 }
361
362 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
363 CodeGenFunction &CGF) const;
364
David Chisnall1e4249c2009-08-17 23:08:21 +0000365 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000366 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000367 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000368};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000369
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000370class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
371public:
372 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000373 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000374
375 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
376 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000377
378 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
379 // Darwin uses different dwarf register numbers for EH.
380 if (CGM.isTargetDarwin()) return 5;
381
382 return 4;
383 }
384
385 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
386 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000387};
388
389}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000390
391/// shouldReturnTypeInRegister - Determine if the given type should be
392/// passed in a register (for the Darwin ABI).
393bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
394 ASTContext &Context) {
395 uint64_t Size = Context.getTypeSize(Ty);
396
397 // Type must be register sized.
398 if (!isRegisterSize(Size))
399 return false;
400
401 if (Ty->isVectorType()) {
402 // 64- and 128- bit vectors inside structures are not returned in
403 // registers.
404 if (Size == 64 || Size == 128)
405 return false;
406
407 return true;
408 }
409
Daniel Dunbar77115232010-05-15 00:00:30 +0000410 // If this is a builtin, pointer, enum, complex type, member pointer, or
411 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000412 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000413 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000414 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000415 return true;
416
417 // Arrays are treated like records.
418 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
419 return shouldReturnTypeInRegister(AT->getElementType(), Context);
420
421 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000422 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000423 if (!RT) return false;
424
Anders Carlssona8874232010-01-27 03:25:19 +0000425 // FIXME: Traverse bases here too.
426
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000427 // Structure types are passed in register if all fields would be
428 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000429 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
430 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000431 const FieldDecl *FD = *i;
432
433 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000434 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000435 continue;
436
437 // Check fields recursively.
438 if (!shouldReturnTypeInRegister(FD->getType(), Context))
439 return false;
440 }
441
442 return true;
443}
444
445ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000446 ASTContext &Context,
447 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000448 if (RetTy->isVoidType()) {
449 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000450 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000451 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000452 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000453 uint64_t Size = Context.getTypeSize(RetTy);
454
455 // 128-bit vectors are a special case; they are returned in
456 // registers and we need to make sure to pick a type the LLVM
457 // backend will like.
458 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000459 return ABIArgInfo::getCoerce(llvm::VectorType::get(
460 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000461
462 // Always return in register if it fits in a general purpose
463 // register, or if it is 64 bits and has a single element.
464 if ((Size == 8 || Size == 16 || Size == 32) ||
465 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000466 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000467
468 return ABIArgInfo::getIndirect(0);
469 }
470
471 return ABIArgInfo::getDirect();
472 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000473 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000474 // Structures with either a non-trivial destructor or a non-trivial
475 // copy constructor are always indirect.
476 if (hasNonTrivialDestructorOrCopyConstructor(RT))
477 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
478
479 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000480 if (RT->getDecl()->hasFlexibleArrayMember())
481 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000482 }
483
David Chisnall1e4249c2009-08-17 23:08:21 +0000484 // If specified, structs and unions are always indirect.
485 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000486 return ABIArgInfo::getIndirect(0);
487
488 // Classify "single element" structs as their element type.
489 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000490 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000491 if (BT->isIntegerType()) {
492 // We need to use the size of the structure, padding
493 // bit-fields can adjust that to be larger than the single
494 // element type.
495 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000496 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000497 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498 } else if (BT->getKind() == BuiltinType::Float) {
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::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000502 } else if (BT->getKind() == BuiltinType::Double) {
503 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
504 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000505 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000506 }
507 } else if (SeltTy->isPointerType()) {
508 // FIXME: It would be really nice if this could come out as the proper
509 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000510 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000511 return ABIArgInfo::getCoerce(PtrTy);
512 } else if (SeltTy->isVectorType()) {
513 // 64- and 128-bit vectors are never returned in a
514 // register when inside a structure.
515 uint64_t Size = Context.getTypeSize(RetTy);
516 if (Size == 64 || Size == 128)
517 return ABIArgInfo::getIndirect(0);
518
Owen Andersona1cf15f2009-07-14 23:10:40 +0000519 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000520 }
521 }
522
523 // Small structures which are register sized are generally returned
524 // in a register.
525 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
526 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000527 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000528 }
529
530 return ABIArgInfo::getIndirect(0);
531 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000532 // Treat an enum type as its underlying type.
533 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
534 RetTy = EnumTy->getDecl()->getIntegerType();
535
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000536 return (RetTy->isPromotableIntegerType() ?
537 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000538 }
539}
540
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000541ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
542 ASTContext &Context,
543 bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000544 if (!ByVal)
545 return ABIArgInfo::getIndirect(0, false);
546
547 // Compute the byval alignment. We trust the back-end to honor the
548 // minimum ABI alignment for byval, to make cleaner IR.
549 const unsigned MinABIAlign = 4;
550 unsigned Align = Context.getTypeAlign(Ty) / 8;
551 if (Align > MinABIAlign)
552 return ABIArgInfo::getIndirect(Align);
553 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000554}
555
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000556ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000557 ASTContext &Context,
558 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000559 // FIXME: Set alignment on indirect arguments.
560 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
561 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000562 if (const RecordType *RT = Ty->getAs<RecordType>()) {
563 // Structures with either a non-trivial destructor or a non-trivial
564 // copy constructor are always indirect.
565 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000566 return getIndirectResult(Ty, Context, /*ByVal=*/false);
567
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000568 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000569 return getIndirectResult(Ty, Context);
Anders Carlssona8874232010-01-27 03:25:19 +0000570 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571
572 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000573 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000574 return ABIArgInfo::getIgnore();
575
Daniel Dunbar53012f42009-11-09 01:33:53 +0000576 // Expand small (<= 128-bit) record types when we know that the stack layout
577 // of those arguments will match the struct. This is important because the
578 // LLVM backend isn't smart enough to remove byval, which inhibits many
579 // optimizations.
580 if (Context.getTypeSize(Ty) <= 4*32 &&
581 canExpandIndirectArgument(Ty, Context))
582 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000583
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000584 return getIndirectResult(Ty, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000585 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000586 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
587 Ty = EnumTy->getDecl()->getIntegerType();
588
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000589 return (Ty->isPromotableIntegerType() ?
590 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000591 }
592}
593
594llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
595 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000596 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000597 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000598
599 CGBuilderTy &Builder = CGF.Builder;
600 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
601 "ap");
602 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
603 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000604 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000605 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
606
607 uint64_t Offset =
608 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
609 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000610 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000611 "ap.next");
612 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
613
614 return AddrTyped;
615}
616
Charles Davis74f72932010-02-13 15:54:06 +0000617void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
618 llvm::GlobalValue *GV,
619 CodeGen::CodeGenModule &CGM) const {
620 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
621 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
622 // Get the LLVM function.
623 llvm::Function *Fn = cast<llvm::Function>(GV);
624
625 // Now add the 'alignstack' attribute with a value of 16.
626 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
627 }
628 }
629}
630
John McCall6374c332010-03-06 00:35:14 +0000631bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
632 CodeGen::CodeGenFunction &CGF,
633 llvm::Value *Address) const {
634 CodeGen::CGBuilderTy &Builder = CGF.Builder;
635 llvm::LLVMContext &Context = CGF.getLLVMContext();
636
637 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
638 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
639
640 // 0-7 are the eight integer registers; the order is different
641 // on Darwin (for EH), but the range is the same.
642 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000643 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000644
645 if (CGF.CGM.isTargetDarwin()) {
646 // 12-16 are st(0..4). Not sure why we stop at 4.
647 // These have size 16, which is sizeof(long double) on
648 // platforms with 8-byte alignment for that type.
649 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000650 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCall6374c332010-03-06 00:35:14 +0000651
652 } else {
653 // 9 is %eflags, which doesn't get a size on Darwin for some
654 // reason.
655 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
656
657 // 11-16 are st(0..5). Not sure why we stop at 5.
658 // These have size 12, which is sizeof(long double) on
659 // platforms with 4-byte alignment for that type.
660 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000661 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
662 }
John McCall6374c332010-03-06 00:35:14 +0000663
664 return false;
665}
666
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000667//===----------------------------------------------------------------------===//
668// X86-64 ABI Implementation
669//===----------------------------------------------------------------------===//
670
671
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000672namespace {
673/// X86_64ABIInfo - The X86_64 ABI information.
674class X86_64ABIInfo : public ABIInfo {
675 enum Class {
676 Integer = 0,
677 SSE,
678 SSEUp,
679 X87,
680 X87Up,
681 ComplexX87,
682 NoClass,
683 Memory
684 };
685
686 /// merge - Implement the X86_64 ABI merging algorithm.
687 ///
688 /// Merge an accumulating classification \arg Accum with a field
689 /// classification \arg Field.
690 ///
691 /// \param Accum - The accumulating classification. This should
692 /// always be either NoClass or the result of a previous merge
693 /// call. In addition, this should never be Memory (the caller
694 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000695 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000696
697 /// classify - Determine the x86_64 register classes in which the
698 /// given type T should be passed.
699 ///
700 /// \param Lo - The classification for the parts of the type
701 /// residing in the low word of the containing object.
702 ///
703 /// \param Hi - The classification for the parts of the type
704 /// residing in the high word of the containing object.
705 ///
706 /// \param OffsetBase - The bit offset of this type in the
707 /// containing object. Some parameters are classified different
708 /// depending on whether they straddle an eightbyte boundary.
709 ///
710 /// If a word is unused its result will be NoClass; if a type should
711 /// be passed in Memory then at least the classification of \arg Lo
712 /// will be Memory.
713 ///
714 /// The \arg Lo class will be NoClass iff the argument is ignored.
715 ///
716 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
717 /// also be ComplexX87.
718 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
719 Class &Lo, Class &Hi) const;
720
721 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
722 /// to coerce to, chose the best way to pass Ty in the same place
723 /// that \arg CoerceTo would be passed, but while keeping the
724 /// emitted code as simple as possible.
725 ///
726 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
727 /// the ways we might want to pass things, instead of constructing an LLVM
728 /// type. This makes this code more explicit, and it makes it clearer that we
729 /// are also doing this for correctness in the case of passing scalar types.
730 ABIArgInfo getCoerceResult(QualType Ty,
731 const llvm::Type *CoerceTo,
732 ASTContext &Context) const;
733
734 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000735 /// such that the argument will be returned in memory.
736 ABIArgInfo getIndirectReturnResult(QualType Ty, ASTContext &Context) const;
737
738 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000739 /// such that the argument will be passed in memory.
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000740 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000741
742 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000743 ASTContext &Context,
744 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000745
746 ABIArgInfo classifyArgumentType(QualType Ty,
747 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000748 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000749 unsigned &neededInt,
Chris Lattnera159c2e2010-06-29 01:14:09 +0000750 unsigned &neededSSE,
751 const llvm::Type *PrefType) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000752
753public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000754 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +0000755 llvm::LLVMContext &VMContext,
756 const llvm::Type *const *PrefTypes,
757 unsigned NumPrefTypes) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000758
759 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
760 CodeGenFunction &CGF) const;
761};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000762
763class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
764public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000765 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCall6374c332010-03-06 00:35:14 +0000766
767 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
768 return 7;
769 }
770
771 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
772 llvm::Value *Address) const {
773 CodeGen::CGBuilderTy &Builder = CGF.Builder;
774 llvm::LLVMContext &Context = CGF.getLLVMContext();
775
776 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
777 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
778
John McCallaeeb7012010-05-27 06:19:26 +0000779 // 0-15 are the 16 integer registers.
780 // 16 is %rip.
781 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000782
783 return false;
784 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000785};
786
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000787}
788
Chris Lattner1090a9b2010-06-28 21:43:59 +0000789X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000790 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
791 // classified recursively so that always two fields are
792 // considered. The resulting class is calculated according to
793 // the classes of the fields in the eightbyte:
794 //
795 // (a) If both classes are equal, this is the resulting class.
796 //
797 // (b) If one of the classes is NO_CLASS, the resulting class is
798 // the other class.
799 //
800 // (c) If one of the classes is MEMORY, the result is the MEMORY
801 // class.
802 //
803 // (d) If one of the classes is INTEGER, the result is the
804 // INTEGER.
805 //
806 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
807 // MEMORY is used as class.
808 //
809 // (f) Otherwise class SSE is used.
810
811 // Accum should never be memory (we should have returned) or
812 // ComplexX87 (because this cannot be passed in a structure).
813 assert((Accum != Memory && Accum != ComplexX87) &&
814 "Invalid accumulated classification during merge.");
815 if (Accum == Field || Field == NoClass)
816 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000817 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000818 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000819 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000820 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000821 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000822 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000823 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
824 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000825 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000826 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000827}
828
829void X86_64ABIInfo::classify(QualType Ty,
830 ASTContext &Context,
831 uint64_t OffsetBase,
832 Class &Lo, Class &Hi) const {
833 // FIXME: This code can be simplified by introducing a simple value class for
834 // Class pairs with appropriate constructor methods for the various
835 // situations.
836
837 // FIXME: Some of the split computations are wrong; unaligned vectors
838 // shouldn't be passed in registers for example, so there is no chance they
839 // can straddle an eightbyte. Verify & simplify.
840
841 Lo = Hi = NoClass;
842
843 Class &Current = OffsetBase < 64 ? Lo : Hi;
844 Current = Memory;
845
John McCall183700f2009-09-21 23:43:11 +0000846 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000847 BuiltinType::Kind k = BT->getKind();
848
849 if (k == BuiltinType::Void) {
850 Current = NoClass;
851 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
852 Lo = Integer;
853 Hi = Integer;
854 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
855 Current = Integer;
856 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
857 Current = SSE;
858 } else if (k == BuiltinType::LongDouble) {
859 Lo = X87;
860 Hi = X87Up;
861 }
862 // FIXME: _Decimal32 and _Decimal64 are SSE.
863 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000864 return;
865 }
866
867 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000868 // Classify the underlying integer type.
869 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000870 return;
871 }
872
873 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000874 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000875 return;
876 }
877
878 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000879 if (Ty->isMemberFunctionPointerType())
880 Lo = Hi = Integer;
881 else
882 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000883 return;
884 }
885
886 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887 uint64_t Size = Context.getTypeSize(VT);
888 if (Size == 32) {
889 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
890 // float> as integer.
891 Current = Integer;
892
893 // If this type crosses an eightbyte boundary, it should be
894 // split.
895 uint64_t EB_Real = (OffsetBase) / 64;
896 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
897 if (EB_Real != EB_Imag)
898 Hi = Lo;
899 } else if (Size == 64) {
900 // gcc passes <1 x double> in memory. :(
901 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
902 return;
903
904 // gcc passes <1 x long long> as INTEGER.
905 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
906 Current = Integer;
907 else
908 Current = SSE;
909
910 // If this type crosses an eightbyte boundary, it should be
911 // split.
912 if (OffsetBase && OffsetBase != 64)
913 Hi = Lo;
914 } else if (Size == 128) {
915 Lo = SSE;
916 Hi = SSEUp;
917 }
Chris Lattner1090a9b2010-06-28 21:43:59 +0000918 return;
919 }
920
921 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000922 QualType ET = Context.getCanonicalType(CT->getElementType());
923
924 uint64_t Size = Context.getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000925 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000926 if (Size <= 64)
927 Current = Integer;
928 else if (Size <= 128)
929 Lo = Hi = Integer;
930 } else if (ET == Context.FloatTy)
931 Current = SSE;
932 else if (ET == Context.DoubleTy)
933 Lo = Hi = SSE;
934 else if (ET == Context.LongDoubleTy)
935 Current = ComplexX87;
936
937 // If this complex type crosses an eightbyte boundary then it
938 // should be split.
939 uint64_t EB_Real = (OffsetBase) / 64;
940 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
941 if (Hi == NoClass && EB_Real != EB_Imag)
942 Hi = Lo;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000943
944 return;
945 }
946
947 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000948 // Arrays are treated like structures.
949
950 uint64_t Size = Context.getTypeSize(Ty);
951
952 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
953 // than two eightbytes, ..., it has class MEMORY.
954 if (Size > 128)
955 return;
956
957 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
958 // fields, it has class MEMORY.
959 //
960 // Only need to check alignment of array base.
961 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
962 return;
963
964 // Otherwise implement simplified merge. We could be smarter about
965 // this, but it isn't worth it and would be harder to verify.
966 Current = NoClass;
967 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
968 uint64_t ArraySize = AT->getSize().getZExtValue();
969 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
970 Class FieldLo, FieldHi;
971 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
972 Lo = merge(Lo, FieldLo);
973 Hi = merge(Hi, FieldHi);
974 if (Lo == Memory || Hi == Memory)
975 break;
976 }
977
978 // Do post merger cleanup (see below). Only case we worry about is Memory.
979 if (Hi == Memory)
980 Lo = Memory;
981 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +0000982 return;
983 }
984
985 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000986 uint64_t Size = Context.getTypeSize(Ty);
987
988 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
989 // than two eightbytes, ..., it has class MEMORY.
990 if (Size > 128)
991 return;
992
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000993 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
994 // copy constructor or a non-trivial destructor, it is passed by invisible
995 // reference.
996 if (hasNonTrivialDestructorOrCopyConstructor(RT))
997 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000998
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000999 const RecordDecl *RD = RT->getDecl();
1000
1001 // Assume variable sized types are passed in memory.
1002 if (RD->hasFlexibleArrayMember())
1003 return;
1004
1005 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1006
1007 // Reset Lo class, this will be recomputed.
1008 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001009
1010 // If this is a C++ record, classify the bases first.
1011 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1012 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1013 e = CXXRD->bases_end(); i != e; ++i) {
1014 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1015 "Unexpected base class!");
1016 const CXXRecordDecl *Base =
1017 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1018
1019 // Classify this field.
1020 //
1021 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1022 // single eightbyte, each is classified separately. Each eightbyte gets
1023 // initialized to class NO_CLASS.
1024 Class FieldLo, FieldHi;
1025 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
1026 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1027 Lo = merge(Lo, FieldLo);
1028 Hi = merge(Hi, FieldHi);
1029 if (Lo == Memory || Hi == Memory)
1030 break;
1031 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +00001032
1033 // If this record has no fields but isn't empty, classify as INTEGER.
1034 if (RD->field_empty() && Size)
1035 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001036 }
1037
1038 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001039 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001040 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1041 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001042 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1043 bool BitField = i->isBitField();
1044
1045 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1046 // fields, it has class MEMORY.
1047 //
1048 // Note, skip this test for bit-fields, see below.
1049 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1050 Lo = Memory;
1051 return;
1052 }
1053
1054 // Classify this field.
1055 //
1056 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1057 // exceeds a single eightbyte, each is classified
1058 // separately. Each eightbyte gets initialized to class
1059 // NO_CLASS.
1060 Class FieldLo, FieldHi;
1061
1062 // Bit-fields require special handling, they do not force the
1063 // structure to be passed in memory even if unaligned, and
1064 // therefore they can straddle an eightbyte.
1065 if (BitField) {
1066 // Ignore padding bit-fields.
1067 if (i->isUnnamedBitfield())
1068 continue;
1069
1070 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1071 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1072
1073 uint64_t EB_Lo = Offset / 64;
1074 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1075 FieldLo = FieldHi = NoClass;
1076 if (EB_Lo) {
1077 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1078 FieldLo = NoClass;
1079 FieldHi = Integer;
1080 } else {
1081 FieldLo = Integer;
1082 FieldHi = EB_Hi ? Integer : NoClass;
1083 }
1084 } else
1085 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1086 Lo = merge(Lo, FieldLo);
1087 Hi = merge(Hi, FieldHi);
1088 if (Lo == Memory || Hi == Memory)
1089 break;
1090 }
1091
1092 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1093 //
1094 // (a) If one of the classes is MEMORY, the whole argument is
1095 // passed in memory.
1096 //
1097 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1098
1099 // The first of these conditions is guaranteed by how we implement
1100 // the merge (just bail).
1101 //
1102 // The second condition occurs in the case of unions; for example
1103 // union { _Complex double; unsigned; }.
1104 if (Hi == Memory)
1105 Lo = Memory;
1106 if (Hi == SSEUp && Lo != SSE)
1107 Hi = SSE;
1108 }
1109}
1110
1111ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1112 const llvm::Type *CoerceTo,
1113 ASTContext &Context) const {
Chris Lattner7f215c12010-06-26 21:52:32 +00001114 if (CoerceTo->isIntegerTy(64)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001115 // Integer and pointer types will end up in a general purpose
1116 // register.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001117
1118 // Treat an enum type as its underlying type.
1119 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1120 Ty = EnumTy->getDecl()->getIntegerType();
1121
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001122 if (Ty->isIntegralOrEnumerationType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001123 return (Ty->isPromotableIntegerType() ?
1124 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001125
Chris Lattner8ff29642010-06-28 21:59:07 +00001126 // If this is a 8/16/32-bit structure that is passed as an int64, then it
1127 // will be passed in the low 8/16/32-bits of a 64-bit GPR, which is the same
1128 // as how an i8/i16/i32 is passed. Coerce to a i8/i16/i32 instead of a i64.
1129 switch (Context.getTypeSizeInChars(Ty).getQuantity()) {
1130 default: break;
1131 case 1: CoerceTo = llvm::Type::getInt8Ty(CoerceTo->getContext()); break;
1132 case 2: CoerceTo = llvm::Type::getInt16Ty(CoerceTo->getContext()); break;
1133 case 4: CoerceTo = llvm::Type::getInt32Ty(CoerceTo->getContext()); break;
1134 }
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001135
Chris Lattner7f215c12010-06-26 21:52:32 +00001136 } else if (CoerceTo->isDoubleTy()) {
John McCall0b0ef0a2010-02-24 07:14:12 +00001137 assert(Ty.isCanonical() && "should always have a canonical type here");
1138 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001139
1140 // Float and double end up in a single SSE reg.
John McCall0b0ef0a2010-02-24 07:14:12 +00001141 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001142 return ABIArgInfo::getDirect();
1143
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001144 // If this is a 32-bit structure that is passed as a double, then it will be
1145 // passed in the low 32-bits of the XMM register, which is the same as how a
1146 // float is passed. Coerce to a float instead of a double.
1147 if (Context.getTypeSizeInChars(Ty).getQuantity() == 4)
1148 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001149 }
1150
1151 return ABIArgInfo::getCoerce(CoerceTo);
1152}
1153
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001154ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1155 ASTContext &Context) const {
1156 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1157 // place naturally.
1158 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1159 // Treat an enum type as its underlying type.
1160 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1161 Ty = EnumTy->getDecl()->getIntegerType();
1162
1163 return (Ty->isPromotableIntegerType() ?
1164 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1165 }
1166
1167 return ABIArgInfo::getIndirect(0);
1168}
1169
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001170ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1171 ASTContext &Context) const {
1172 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1173 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001174 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1175 // Treat an enum type as its underlying type.
1176 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1177 Ty = EnumTy->getDecl()->getIntegerType();
1178
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001179 return (Ty->isPromotableIntegerType() ?
1180 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001181 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001182
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001183 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1184 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001185
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001186 // Compute the byval alignment. We trust the back-end to honor the
1187 // minimum ABI alignment for byval, to make cleaner IR.
1188 const unsigned MinABIAlign = 8;
1189 unsigned Align = Context.getTypeAlign(Ty) / 8;
1190 if (Align > MinABIAlign)
1191 return ABIArgInfo::getIndirect(Align);
1192 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001193}
1194
Chris Lattner1090a9b2010-06-28 21:43:59 +00001195ABIArgInfo X86_64ABIInfo::
1196classifyReturnType(QualType RetTy, ASTContext &Context,
1197 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001198 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1199 // classification algorithm.
1200 X86_64ABIInfo::Class Lo, Hi;
1201 classify(RetTy, Context, 0, Lo, Hi);
1202
1203 // Check some invariants.
1204 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1205 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1206 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1207
1208 const llvm::Type *ResType = 0;
1209 switch (Lo) {
1210 case NoClass:
1211 return ABIArgInfo::getIgnore();
1212
1213 case SSEUp:
1214 case X87Up:
1215 assert(0 && "Invalid classification for lo word.");
1216
1217 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1218 // hidden argument.
1219 case Memory:
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001220 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001221
1222 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1223 // available register of the sequence %rax, %rdx is used.
1224 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001225 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001226
1227 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1228 // available SSE register of the sequence %xmm0, %xmm1 is used.
1229 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001230 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001231
1232 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1233 // returned on the X87 stack in %st0 as 80-bit x87 number.
1234 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001235 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001236
1237 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1238 // part of the value is returned in %st0 and the imaginary part in
1239 // %st1.
1240 case ComplexX87:
1241 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner52d9ae32010-04-06 17:29:22 +00001242 ResType = llvm::StructType::get(VMContext,
1243 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson0032b272009-08-13 21:57:51 +00001244 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001245 NULL);
1246 break;
1247 }
1248
1249 switch (Hi) {
1250 // Memory was handled previously and X87 should
1251 // never occur as a hi class.
1252 case Memory:
1253 case X87:
1254 assert(0 && "Invalid classification for hi word.");
1255
1256 case ComplexX87: // Previously handled.
1257 case NoClass: break;
1258
1259 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001260 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001261 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001262 break;
1263 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001264 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001265 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001266 break;
1267
1268 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1269 // is passed in the upper half of the last used SSE register.
1270 //
1271 // SSEUP should always be preceeded by SSE, just widen.
1272 case SSEUp:
1273 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001274 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001275 break;
1276
1277 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1278 // returned together with the previous X87 value in %st0.
1279 case X87Up:
1280 // If X87Up is preceeded by X87, we don't need to do
1281 // anything. However, in some cases with unions it may not be
1282 // preceeded by X87. In such situations we follow gcc and pass the
1283 // extra bits in an SSE reg.
1284 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001285 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001286 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001287 break;
1288 }
1289
1290 return getCoerceResult(RetTy, ResType, Context);
1291}
1292
1293ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001294 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001295 unsigned &neededInt,
Chris Lattnera159c2e2010-06-29 01:14:09 +00001296 unsigned &neededSSE,
1297 const llvm::Type *PrefType)const{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001298 X86_64ABIInfo::Class Lo, Hi;
1299 classify(Ty, Context, 0, Lo, Hi);
1300
1301 // Check some invariants.
1302 // FIXME: Enforce these by construction.
1303 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1304 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1305 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1306
1307 neededInt = 0;
1308 neededSSE = 0;
1309 const llvm::Type *ResType = 0;
1310 switch (Lo) {
1311 case NoClass:
1312 return ABIArgInfo::getIgnore();
1313
1314 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1315 // on the stack.
1316 case Memory:
1317
1318 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1319 // COMPLEX_X87, it is passed in memory.
1320 case X87:
1321 case ComplexX87:
1322 return getIndirectResult(Ty, Context);
1323
1324 case SSEUp:
1325 case X87Up:
1326 assert(0 && "Invalid classification for lo word.");
1327
1328 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1329 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1330 // and %r9 is used.
1331 case Integer:
1332 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001333 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001334 break;
1335
1336 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1337 // available SSE register is used, the registers are taken in the
1338 // order from %xmm0 to %xmm7.
1339 case SSE:
1340 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001341 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001342 break;
1343 }
1344
1345 switch (Hi) {
1346 // Memory was handled previously, ComplexX87 and X87 should
1347 // never occur as hi classes, and X87Up must be preceed by X87,
1348 // which is passed in memory.
1349 case Memory:
1350 case X87:
1351 case ComplexX87:
1352 assert(0 && "Invalid classification for hi word.");
1353 break;
1354
1355 case NoClass: break;
1356 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001357 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001358 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001359 ++neededInt;
1360 break;
1361
1362 // X87Up generally doesn't occur here (long double is passed in
1363 // memory), except in situations involving unions.
1364 case X87Up:
1365 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001366 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001367 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001368 ++neededSSE;
1369 break;
1370
1371 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1372 // eightbyte is passed in the upper half of the last used SSE
1373 // register.
1374 case SSEUp:
1375 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001376 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001377 break;
1378 }
1379
1380 return getCoerceResult(Ty, ResType, Context);
1381}
1382
Owen Andersona1cf15f2009-07-14 23:10:40 +00001383void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00001384 llvm::LLVMContext &VMContext,
1385 const llvm::Type *const *PrefTypes,
1386 unsigned NumPrefTypes) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001387 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1388 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001389
1390 // Keep track of the number of assigned registers.
1391 unsigned freeIntRegs = 6, freeSSERegs = 8;
1392
1393 // If the return value is indirect, then the hidden argument is consuming one
1394 // integer register.
1395 if (FI.getReturnInfo().isIndirect())
1396 --freeIntRegs;
1397
1398 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1399 // get assigned (in left-to-right order) for passing as follows...
1400 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1401 it != ie; ++it) {
Chris Lattnera159c2e2010-06-29 01:14:09 +00001402 // If the client specified a preferred IR type to use, pass it down to
1403 // classifyArgumentType.
1404 const llvm::Type *PrefType = 0;
1405 if (NumPrefTypes) {
1406 PrefType = *PrefTypes++;
1407 --NumPrefTypes;
1408 }
1409
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001410 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001411 it->info = classifyArgumentType(it->type, Context, VMContext,
Chris Lattnera159c2e2010-06-29 01:14:09 +00001412 neededInt, neededSSE, PrefType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001413
1414 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1415 // eightbyte of an argument, the whole argument is passed on the
1416 // stack. If registers have already been assigned for some
1417 // eightbytes of such an argument, the assignments get reverted.
1418 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1419 freeIntRegs -= neededInt;
1420 freeSSERegs -= neededSSE;
1421 } else {
1422 it->info = getIndirectResult(it->type, Context);
1423 }
1424 }
1425}
1426
1427static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1428 QualType Ty,
1429 CodeGenFunction &CGF) {
1430 llvm::Value *overflow_arg_area_p =
1431 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1432 llvm::Value *overflow_arg_area =
1433 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1434
1435 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1436 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1437 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1438 if (Align > 8) {
1439 // Note that we follow the ABI & gcc here, even though the type
1440 // could in theory have an alignment greater than 16. This case
1441 // shouldn't ever matter in practice.
1442
1443 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001444 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001445 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001446 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1447 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001448 CGF.Int64Ty);
1449 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001450 overflow_arg_area =
1451 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1452 overflow_arg_area->getType(),
1453 "overflow_arg_area.align");
1454 }
1455
1456 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1457 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1458 llvm::Value *Res =
1459 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001460 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001461
1462 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1463 // l->overflow_arg_area + sizeof(type).
1464 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1465 // an 8 byte boundary.
1466
1467 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001468 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001469 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001470 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1471 "overflow_arg_area.next");
1472 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1473
1474 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1475 return Res;
1476}
1477
1478llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1479 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001480 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482 // Assume that va_list type is correct; should be pointer to LLVM type:
1483 // struct {
1484 // i32 gp_offset;
1485 // i32 fp_offset;
1486 // i8* overflow_arg_area;
1487 // i8* reg_save_area;
1488 // };
1489 unsigned neededInt, neededSSE;
Chris Lattnera14db752010-03-11 18:19:55 +00001490
1491 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001492 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Chris Lattnera159c2e2010-06-29 01:14:09 +00001493 neededInt, neededSSE, 0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001494
1495 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1496 // in the registers. If not go to step 7.
1497 if (!neededInt && !neededSSE)
1498 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1499
1500 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1501 // general purpose registers needed to pass type and num_fp to hold
1502 // the number of floating point registers needed.
1503
1504 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1505 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1506 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1507 //
1508 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1509 // register save space).
1510
1511 llvm::Value *InRegs = 0;
1512 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1513 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1514 if (neededInt) {
1515 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1516 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001517 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1518 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001519 }
1520
1521 if (neededSSE) {
1522 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1523 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1524 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001525 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1526 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001527 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1528 }
1529
1530 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1531 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1532 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1533 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1534
1535 // Emit code to load the value if it was passed in registers.
1536
1537 CGF.EmitBlock(InRegBlock);
1538
1539 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1540 // an offset of l->gp_offset and/or l->fp_offset. This may require
1541 // copying to a temporary location in case the parameter is passed
1542 // in different register classes or requires an alignment greater
1543 // than 8 for general purpose registers and 16 for XMM registers.
1544 //
1545 // FIXME: This really results in shameful code when we end up needing to
1546 // collect arguments from different places; often what should result in a
1547 // simple assembling of a structure from scattered addresses has many more
1548 // loads than necessary. Can we clean this up?
1549 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1550 llvm::Value *RegAddr =
1551 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1552 "reg_save_area");
1553 if (neededInt && neededSSE) {
1554 // FIXME: Cleanup.
1555 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1556 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1557 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1558 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1559 const llvm::Type *TyLo = ST->getElementType(0);
1560 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001561 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001562 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001563 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1564 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001565 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1566 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001567 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1568 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001569 llvm::Value *V =
1570 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1571 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1572 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1573 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1574
Owen Andersona1cf15f2009-07-14 23:10:40 +00001575 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001576 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001577 } else if (neededInt) {
1578 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1579 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001580 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001581 } else if (neededSSE == 1) {
1582 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1583 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1584 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001585 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001586 assert(neededSSE == 2 && "Invalid number of needed registers!");
1587 // SSE registers are spaced 16 bytes apart in the register save
1588 // area, we need to collect the two eightbytes together.
1589 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001590 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001591 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1592 const llvm::Type *DblPtrTy =
1593 llvm::PointerType::getUnqual(DoubleTy);
1594 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1595 DoubleTy, NULL);
1596 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1597 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1598 DblPtrTy));
1599 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1600 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1601 DblPtrTy));
1602 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1603 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1604 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001605 }
1606
1607 // AMD64-ABI 3.5.7p5: Step 5. Set:
1608 // l->gp_offset = l->gp_offset + num_gp * 8
1609 // l->fp_offset = l->fp_offset + num_fp * 16.
1610 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001611 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001612 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1613 gp_offset_p);
1614 }
1615 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001616 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001617 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1618 fp_offset_p);
1619 }
1620 CGF.EmitBranch(ContBlock);
1621
1622 // Emit code to load the value if it was passed in memory.
1623
1624 CGF.EmitBlock(InMemBlock);
1625 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1626
1627 // Return the appropriate result.
1628
1629 CGF.EmitBlock(ContBlock);
1630 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1631 "vaarg.addr");
1632 ResAddr->reserveOperandSpace(2);
1633 ResAddr->addIncoming(RegAddr, InRegBlock);
1634 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001635 return ResAddr;
1636}
1637
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001638
1639
1640//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001641// PIC16 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001642//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001643
1644namespace {
1645
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001646class PIC16ABIInfo : public ABIInfo {
1647 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001648 ASTContext &Context,
1649 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001650
1651 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001652 ASTContext &Context,
1653 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001654
Owen Andersona1cf15f2009-07-14 23:10:40 +00001655 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00001656 llvm::LLVMContext &VMContext,
1657 const llvm::Type *const *PrefTypes,
1658 unsigned NumPrefTypes) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001659 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1660 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001661 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1662 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001663 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001664 }
1665
1666 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1667 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001668};
1669
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001670class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1671public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001672 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001673};
1674
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001675}
1676
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001677ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001678 ASTContext &Context,
1679 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001680 if (RetTy->isVoidType()) {
1681 return ABIArgInfo::getIgnore();
1682 } else {
1683 return ABIArgInfo::getDirect();
1684 }
1685}
1686
1687ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001688 ASTContext &Context,
1689 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001690 return ABIArgInfo::getDirect();
1691}
1692
1693llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00001694 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00001695 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001696 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1697
1698 CGBuilderTy &Builder = CGF.Builder;
1699 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1700 "ap");
1701 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1702 llvm::Type *PTy =
1703 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1704 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1705
1706 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1707
1708 llvm::Value *NextAddr =
1709 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1710 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1711 "ap.next");
1712 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1713
1714 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001715}
1716
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001717
John McCallec853ba2010-03-11 00:10:12 +00001718// PowerPC-32
1719
1720namespace {
1721class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1722public:
1723 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1724 // This is recovered from gcc output.
1725 return 1; // r1 is the dedicated stack pointer
1726 }
1727
1728 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1729 llvm::Value *Address) const;
1730};
1731
1732}
1733
1734bool
1735PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1736 llvm::Value *Address) const {
1737 // This is calculated from the LLVM and GCC tables and verified
1738 // against gcc output. AFAIK all ABIs use the same encoding.
1739
1740 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1741 llvm::LLVMContext &Context = CGF.getLLVMContext();
1742
1743 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1744 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1745 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1746 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1747
1748 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00001749 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00001750
1751 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00001752 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00001753
1754 // 64-76 are various 4-byte special-purpose registers:
1755 // 64: mq
1756 // 65: lr
1757 // 66: ctr
1758 // 67: ap
1759 // 68-75 cr0-7
1760 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00001761 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00001762
1763 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00001764 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00001765
1766 // 109: vrsave
1767 // 110: vscr
1768 // 111: spe_acc
1769 // 112: spefscr
1770 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00001771 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00001772
1773 return false;
1774}
1775
1776
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001777//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001778// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001779//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001780
1781namespace {
1782
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001783class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001784public:
1785 enum ABIKind {
1786 APCS = 0,
1787 AAPCS = 1,
1788 AAPCS_VFP
1789 };
1790
1791private:
1792 ABIKind Kind;
1793
1794public:
1795 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1796
1797private:
1798 ABIKind getABIKind() const { return Kind; }
1799
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001800 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001801 ASTContext &Context,
1802 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001803
1804 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001805 ASTContext &Context,
1806 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001807
Owen Andersona1cf15f2009-07-14 23:10:40 +00001808 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00001809 llvm::LLVMContext &VMContext,
1810 const llvm::Type *const *PrefTypes,
1811 unsigned NumPrefTypes) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001812
1813 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1814 CodeGenFunction &CGF) const;
1815};
1816
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001817class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1818public:
1819 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001820 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCall6374c332010-03-06 00:35:14 +00001821
1822 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1823 return 13;
1824 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001825};
1826
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001827}
1828
Owen Andersona1cf15f2009-07-14 23:10:40 +00001829void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00001830 llvm::LLVMContext &VMContext,
1831 const llvm::Type *const *PrefTypes,
1832 unsigned NumPrefTypes) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001833 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001834 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001835 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1836 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001837 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001838 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001839
Rafael Espindola25117ab2010-06-16 16:13:39 +00001840 const llvm::Triple &Triple(Context.Target.getTriple());
1841 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001842 if (Triple.getEnvironmentName() == "gnueabi" ||
1843 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00001844 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001845 else
1846 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00001847
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001848 switch (getABIKind()) {
1849 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001850 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1851 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001852 break;
1853
1854 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001855 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1856 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001857 break;
1858
1859 case AAPCS_VFP:
1860 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1861 break;
1862 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001863}
1864
1865ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001866 ASTContext &Context,
1867 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001868 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1869 // Treat an enum type as its underlying type.
1870 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1871 Ty = EnumTy->getDecl()->getIntegerType();
1872
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001873 return (Ty->isPromotableIntegerType() ?
1874 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001875 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001876
Daniel Dunbar42025572009-09-14 21:54:03 +00001877 // Ignore empty records.
1878 if (isEmptyRecord(Context, Ty, true))
1879 return ABIArgInfo::getIgnore();
1880
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001881 // Structures with either a non-trivial destructor or a non-trivial
1882 // copy constructor are always indirect.
1883 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1884 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1885
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001886 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1887 // backend doesn't support byval.
1888 // FIXME: This doesn't handle alignment > 64 bits.
1889 const llvm::Type* ElemTy;
1890 unsigned SizeRegs;
1891 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001892 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001893 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1894 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001895 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001896 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1897 }
1898 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001899 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001900 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001901 return ABIArgInfo::getCoerce(STy);
1902}
1903
Daniel Dunbar98303b92009-09-13 08:03:58 +00001904static bool isIntegerLikeType(QualType Ty,
1905 ASTContext &Context,
1906 llvm::LLVMContext &VMContext) {
1907 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1908 // is called integer-like if its size is less than or equal to one word, and
1909 // the offset of each of its addressable sub-fields is zero.
1910
1911 uint64_t Size = Context.getTypeSize(Ty);
1912
1913 // Check that the type fits in a word.
1914 if (Size > 32)
1915 return false;
1916
1917 // FIXME: Handle vector types!
1918 if (Ty->isVectorType())
1919 return false;
1920
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001921 // Float types are never treated as "integer like".
1922 if (Ty->isRealFloatingType())
1923 return false;
1924
Daniel Dunbar98303b92009-09-13 08:03:58 +00001925 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001926 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001927 return true;
1928
Daniel Dunbar45815812010-02-01 23:31:26 +00001929 // Small complex integer types are "integer like".
1930 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1931 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001932
1933 // Single element and zero sized arrays should be allowed, by the definition
1934 // above, but they are not.
1935
1936 // Otherwise, it must be a record type.
1937 const RecordType *RT = Ty->getAs<RecordType>();
1938 if (!RT) return false;
1939
1940 // Ignore records with flexible arrays.
1941 const RecordDecl *RD = RT->getDecl();
1942 if (RD->hasFlexibleArrayMember())
1943 return false;
1944
1945 // Check that all sub-fields are at offset 0, and are themselves "integer
1946 // like".
1947 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1948
1949 bool HadField = false;
1950 unsigned idx = 0;
1951 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1952 i != e; ++i, ++idx) {
1953 const FieldDecl *FD = *i;
1954
Daniel Dunbar679855a2010-01-29 03:22:29 +00001955 // Bit-fields are not addressable, we only need to verify they are "integer
1956 // like". We still have to disallow a subsequent non-bitfield, for example:
1957 // struct { int : 0; int x }
1958 // is non-integer like according to gcc.
1959 if (FD->isBitField()) {
1960 if (!RD->isUnion())
1961 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001962
Daniel Dunbar679855a2010-01-29 03:22:29 +00001963 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1964 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001965
Daniel Dunbar679855a2010-01-29 03:22:29 +00001966 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001967 }
1968
Daniel Dunbar679855a2010-01-29 03:22:29 +00001969 // Check if this field is at offset 0.
1970 if (Layout.getFieldOffset(idx) != 0)
1971 return false;
1972
Daniel Dunbar98303b92009-09-13 08:03:58 +00001973 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1974 return false;
1975
Daniel Dunbar679855a2010-01-29 03:22:29 +00001976 // Only allow at most one field in a structure. This doesn't match the
1977 // wording above, but follows gcc in situations with a field following an
1978 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001979 if (!RD->isUnion()) {
1980 if (HadField)
1981 return false;
1982
1983 HadField = true;
1984 }
1985 }
1986
1987 return true;
1988}
1989
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001990ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001991 ASTContext &Context,
1992 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001993 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001994 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001995
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001996 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1997 // Treat an enum type as its underlying type.
1998 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1999 RetTy = EnumTy->getDecl()->getIntegerType();
2000
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002001 return (RetTy->isPromotableIntegerType() ?
2002 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002003 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002004
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002005 // Structures with either a non-trivial destructor or a non-trivial
2006 // copy constructor are always indirect.
2007 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2008 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2009
Daniel Dunbar98303b92009-09-13 08:03:58 +00002010 // Are we following APCS?
2011 if (getABIKind() == APCS) {
2012 if (isEmptyRecord(Context, RetTy, false))
2013 return ABIArgInfo::getIgnore();
2014
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002015 // Complex types are all returned as packed integers.
2016 //
2017 // FIXME: Consider using 2 x vector types if the back end handles them
2018 // correctly.
2019 if (RetTy->isAnyComplexType())
2020 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
2021 VMContext, Context.getTypeSize(RetTy)));
2022
Daniel Dunbar98303b92009-09-13 08:03:58 +00002023 // Integer like structures are returned in r0.
2024 if (isIntegerLikeType(RetTy, Context, VMContext)) {
2025 // Return in the smallest viable integer type.
2026 uint64_t Size = Context.getTypeSize(RetTy);
2027 if (Size <= 8)
2028 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2029 if (Size <= 16)
2030 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
2031 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
2032 }
2033
2034 // Otherwise return in memory.
2035 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002036 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002037
2038 // Otherwise this is an AAPCS variant.
2039
Daniel Dunbar16a08082009-09-14 00:56:55 +00002040 if (isEmptyRecord(Context, RetTy, true))
2041 return ABIArgInfo::getIgnore();
2042
Daniel Dunbar98303b92009-09-13 08:03:58 +00002043 // Aggregates <= 4 bytes are returned in r0; other aggregates
2044 // are returned indirectly.
2045 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002046 if (Size <= 32) {
2047 // Return in the smallest viable integer type.
2048 if (Size <= 8)
2049 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2050 if (Size <= 16)
2051 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002052 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002053 }
2054
Daniel Dunbar98303b92009-09-13 08:03:58 +00002055 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002056}
2057
2058llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002059 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002060 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002061 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002062 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002063
2064 CGBuilderTy &Builder = CGF.Builder;
2065 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2066 "ap");
2067 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2068 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002069 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002070 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2071
2072 uint64_t Offset =
2073 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2074 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002075 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002076 "ap.next");
2077 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2078
2079 return AddrTyped;
2080}
2081
2082ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00002083 ASTContext &Context,
2084 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002085 if (RetTy->isVoidType()) {
2086 return ABIArgInfo::getIgnore();
2087 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2088 return ABIArgInfo::getIndirect(0);
2089 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002090 // Treat an enum type as its underlying type.
2091 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2092 RetTy = EnumTy->getDecl()->getIntegerType();
2093
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002094 return (RetTy->isPromotableIntegerType() ?
2095 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002096 }
2097}
2098
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002099//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002100// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002101//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002102
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002103namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002104
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002105class SystemZABIInfo : public ABIInfo {
2106 bool isPromotableIntegerType(QualType Ty) const;
2107
2108 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2109 llvm::LLVMContext &VMContext) const;
2110
2111 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2112 llvm::LLVMContext &VMContext) const;
2113
2114 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00002115 llvm::LLVMContext &VMContext,
2116 const llvm::Type *const *PrefTypes,
2117 unsigned NumPrefTypes) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002118 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2119 Context, VMContext);
2120 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2121 it != ie; ++it)
2122 it->info = classifyArgumentType(it->type, Context, VMContext);
2123 }
2124
2125 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2126 CodeGenFunction &CGF) const;
2127};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002128
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002129class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2130public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002131 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002132};
2133
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002134}
2135
2136bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2137 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002138 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002139 switch (BT->getKind()) {
2140 case BuiltinType::Bool:
2141 case BuiltinType::Char_S:
2142 case BuiltinType::Char_U:
2143 case BuiltinType::SChar:
2144 case BuiltinType::UChar:
2145 case BuiltinType::Short:
2146 case BuiltinType::UShort:
2147 case BuiltinType::Int:
2148 case BuiltinType::UInt:
2149 return true;
2150 default:
2151 return false;
2152 }
2153 return false;
2154}
2155
2156llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2157 CodeGenFunction &CGF) const {
2158 // FIXME: Implement
2159 return 0;
2160}
2161
2162
2163ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2164 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002165 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002166 if (RetTy->isVoidType()) {
2167 return ABIArgInfo::getIgnore();
2168 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2169 return ABIArgInfo::getIndirect(0);
2170 } else {
2171 return (isPromotableIntegerType(RetTy) ?
2172 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2173 }
2174}
2175
2176ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2177 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002178 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002179 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2180 return ABIArgInfo::getIndirect(0);
2181 } else {
2182 return (isPromotableIntegerType(Ty) ?
2183 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2184 }
2185}
2186
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002187//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002188// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002189//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002190
2191namespace {
2192
2193class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2194public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002195 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002196 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2197 CodeGen::CodeGenModule &M) const;
2198};
2199
2200}
2201
2202void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2203 llvm::GlobalValue *GV,
2204 CodeGen::CodeGenModule &M) const {
2205 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2206 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2207 // Handle 'interrupt' attribute:
2208 llvm::Function *F = cast<llvm::Function>(GV);
2209
2210 // Step 1: Set ISR calling convention.
2211 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2212
2213 // Step 2: Add attributes goodness.
2214 F->addFnAttr(llvm::Attribute::NoInline);
2215
2216 // Step 3: Emit ISR vector alias.
2217 unsigned Num = attr->getNumber() + 0xffe0;
2218 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2219 "vector_" +
2220 llvm::LowercaseString(llvm::utohexstr(Num)),
2221 GV, &M.getModule());
2222 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002223 }
2224}
2225
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002226//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002227// MIPS ABI Implementation. This works for both little-endian and
2228// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002229//===----------------------------------------------------------------------===//
2230
John McCallaeeb7012010-05-27 06:19:26 +00002231namespace {
2232class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2233public:
2234 MIPSTargetCodeGenInfo(): TargetCodeGenInfo(new DefaultABIInfo()) {}
2235
2236 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2237 return 29;
2238 }
2239
2240 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2241 llvm::Value *Address) const;
2242};
2243}
2244
2245bool
2246MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2247 llvm::Value *Address) const {
2248 // This information comes from gcc's implementation, which seems to
2249 // as canonical as it gets.
2250
2251 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2252 llvm::LLVMContext &Context = CGF.getLLVMContext();
2253
2254 // Everything on MIPS is 4 bytes. Double-precision FP registers
2255 // are aliased to pairs of single-precision FP registers.
2256 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2257 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2258
2259 // 0-31 are the general purpose registers, $0 - $31.
2260 // 32-63 are the floating-point registers, $f0 - $f31.
2261 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2262 // 66 is the (notional, I think) register for signal-handler return.
2263 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2264
2265 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2266 // They are one bit wide and ignored here.
2267
2268 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2269 // (coprocessor 1 is the FP unit)
2270 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2271 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2272 // 176-181 are the DSP accumulator registers.
2273 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2274
2275 return false;
2276}
2277
2278
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002279const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2280 if (TheTargetCodeGenInfo)
2281 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002282
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002283 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2284 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002285
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002286 const llvm::Triple &Triple(getContext().Target.getTriple());
2287 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002288 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002289 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002290
John McCallaeeb7012010-05-27 06:19:26 +00002291 case llvm::Triple::mips:
2292 case llvm::Triple::mipsel:
2293 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo());
2294
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002295 case llvm::Triple::arm:
2296 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002297 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002298 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002299 return *(TheTargetCodeGenInfo =
2300 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002301
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002302 return *(TheTargetCodeGenInfo =
2303 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002304
2305 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002306 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002307
John McCallec853ba2010-03-11 00:10:12 +00002308 case llvm::Triple::ppc:
2309 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2310
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002311 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002312 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2313
2314 case llvm::Triple::msp430:
2315 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002316
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002317 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002318 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002319 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002320 return *(TheTargetCodeGenInfo =
2321 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002322 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002323 case llvm::Triple::MinGW32:
2324 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002325 case llvm::Triple::AuroraUX:
2326 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002327 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002328 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002329 return *(TheTargetCodeGenInfo =
2330 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002331
2332 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002333 return *(TheTargetCodeGenInfo =
2334 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002335 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002336
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002337 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002338 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002339 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002340}