blob: 3bdfbf641c234991aaac7ff226c5085c674836aa [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,
750 unsigned &neededSSE) const;
751
752public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000753 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +0000754 llvm::LLVMContext &VMContext,
755 const llvm::Type *const *PrefTypes,
756 unsigned NumPrefTypes) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000757
758 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
759 CodeGenFunction &CGF) const;
760};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000761
762class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
763public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000764 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCall6374c332010-03-06 00:35:14 +0000765
766 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
767 return 7;
768 }
769
770 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
771 llvm::Value *Address) const {
772 CodeGen::CGBuilderTy &Builder = CGF.Builder;
773 llvm::LLVMContext &Context = CGF.getLLVMContext();
774
775 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
776 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
777
John McCallaeeb7012010-05-27 06:19:26 +0000778 // 0-15 are the 16 integer registers.
779 // 16 is %rip.
780 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000781
782 return false;
783 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000784};
785
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000786}
787
Chris Lattner1090a9b2010-06-28 21:43:59 +0000788X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000789 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
790 // classified recursively so that always two fields are
791 // considered. The resulting class is calculated according to
792 // the classes of the fields in the eightbyte:
793 //
794 // (a) If both classes are equal, this is the resulting class.
795 //
796 // (b) If one of the classes is NO_CLASS, the resulting class is
797 // the other class.
798 //
799 // (c) If one of the classes is MEMORY, the result is the MEMORY
800 // class.
801 //
802 // (d) If one of the classes is INTEGER, the result is the
803 // INTEGER.
804 //
805 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
806 // MEMORY is used as class.
807 //
808 // (f) Otherwise class SSE is used.
809
810 // Accum should never be memory (we should have returned) or
811 // ComplexX87 (because this cannot be passed in a structure).
812 assert((Accum != Memory && Accum != ComplexX87) &&
813 "Invalid accumulated classification during merge.");
814 if (Accum == Field || Field == NoClass)
815 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000816 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000817 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000818 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000819 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000820 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000821 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000822 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
823 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000824 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000825 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000826}
827
828void X86_64ABIInfo::classify(QualType Ty,
829 ASTContext &Context,
830 uint64_t OffsetBase,
831 Class &Lo, Class &Hi) const {
832 // FIXME: This code can be simplified by introducing a simple value class for
833 // Class pairs with appropriate constructor methods for the various
834 // situations.
835
836 // FIXME: Some of the split computations are wrong; unaligned vectors
837 // shouldn't be passed in registers for example, so there is no chance they
838 // can straddle an eightbyte. Verify & simplify.
839
840 Lo = Hi = NoClass;
841
842 Class &Current = OffsetBase < 64 ? Lo : Hi;
843 Current = Memory;
844
John McCall183700f2009-09-21 23:43:11 +0000845 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000846 BuiltinType::Kind k = BT->getKind();
847
848 if (k == BuiltinType::Void) {
849 Current = NoClass;
850 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
851 Lo = Integer;
852 Hi = Integer;
853 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
854 Current = Integer;
855 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
856 Current = SSE;
857 } else if (k == BuiltinType::LongDouble) {
858 Lo = X87;
859 Hi = X87Up;
860 }
861 // FIXME: _Decimal32 and _Decimal64 are SSE.
862 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000863 return;
864 }
865
866 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000867 // Classify the underlying integer type.
868 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000869 return;
870 }
871
872 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000873 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000874 return;
875 }
876
877 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000878 if (Ty->isMemberFunctionPointerType())
879 Lo = Hi = Integer;
880 else
881 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000882 return;
883 }
884
885 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000886 uint64_t Size = Context.getTypeSize(VT);
887 if (Size == 32) {
888 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
889 // float> as integer.
890 Current = Integer;
891
892 // If this type crosses an eightbyte boundary, it should be
893 // split.
894 uint64_t EB_Real = (OffsetBase) / 64;
895 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
896 if (EB_Real != EB_Imag)
897 Hi = Lo;
898 } else if (Size == 64) {
899 // gcc passes <1 x double> in memory. :(
900 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
901 return;
902
903 // gcc passes <1 x long long> as INTEGER.
904 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
905 Current = Integer;
906 else
907 Current = SSE;
908
909 // If this type crosses an eightbyte boundary, it should be
910 // split.
911 if (OffsetBase && OffsetBase != 64)
912 Hi = Lo;
913 } else if (Size == 128) {
914 Lo = SSE;
915 Hi = SSEUp;
916 }
Chris Lattner1090a9b2010-06-28 21:43:59 +0000917 return;
918 }
919
920 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000921 QualType ET = Context.getCanonicalType(CT->getElementType());
922
923 uint64_t Size = Context.getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000924 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000925 if (Size <= 64)
926 Current = Integer;
927 else if (Size <= 128)
928 Lo = Hi = Integer;
929 } else if (ET == Context.FloatTy)
930 Current = SSE;
931 else if (ET == Context.DoubleTy)
932 Lo = Hi = SSE;
933 else if (ET == Context.LongDoubleTy)
934 Current = ComplexX87;
935
936 // If this complex type crosses an eightbyte boundary then it
937 // should be split.
938 uint64_t EB_Real = (OffsetBase) / 64;
939 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
940 if (Hi == NoClass && EB_Real != EB_Imag)
941 Hi = Lo;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000942
943 return;
944 }
945
946 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000947 // Arrays are treated like structures.
948
949 uint64_t Size = Context.getTypeSize(Ty);
950
951 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
952 // than two eightbytes, ..., it has class MEMORY.
953 if (Size > 128)
954 return;
955
956 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
957 // fields, it has class MEMORY.
958 //
959 // Only need to check alignment of array base.
960 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
961 return;
962
963 // Otherwise implement simplified merge. We could be smarter about
964 // this, but it isn't worth it and would be harder to verify.
965 Current = NoClass;
966 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
967 uint64_t ArraySize = AT->getSize().getZExtValue();
968 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
969 Class FieldLo, FieldHi;
970 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
971 Lo = merge(Lo, FieldLo);
972 Hi = merge(Hi, FieldHi);
973 if (Lo == Memory || Hi == Memory)
974 break;
975 }
976
977 // Do post merger cleanup (see below). Only case we worry about is Memory.
978 if (Hi == Memory)
979 Lo = Memory;
980 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +0000981 return;
982 }
983
984 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000985 uint64_t Size = Context.getTypeSize(Ty);
986
987 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
988 // than two eightbytes, ..., it has class MEMORY.
989 if (Size > 128)
990 return;
991
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000992 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
993 // copy constructor or a non-trivial destructor, it is passed by invisible
994 // reference.
995 if (hasNonTrivialDestructorOrCopyConstructor(RT))
996 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000997
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000998 const RecordDecl *RD = RT->getDecl();
999
1000 // Assume variable sized types are passed in memory.
1001 if (RD->hasFlexibleArrayMember())
1002 return;
1003
1004 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1005
1006 // Reset Lo class, this will be recomputed.
1007 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001008
1009 // If this is a C++ record, classify the bases first.
1010 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1011 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1012 e = CXXRD->bases_end(); i != e; ++i) {
1013 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1014 "Unexpected base class!");
1015 const CXXRecordDecl *Base =
1016 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1017
1018 // Classify this field.
1019 //
1020 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1021 // single eightbyte, each is classified separately. Each eightbyte gets
1022 // initialized to class NO_CLASS.
1023 Class FieldLo, FieldHi;
1024 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
1025 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1026 Lo = merge(Lo, FieldLo);
1027 Hi = merge(Hi, FieldHi);
1028 if (Lo == Memory || Hi == Memory)
1029 break;
1030 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +00001031
1032 // If this record has no fields but isn't empty, classify as INTEGER.
1033 if (RD->field_empty() && Size)
1034 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001035 }
1036
1037 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001038 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001039 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1040 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001041 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1042 bool BitField = i->isBitField();
1043
1044 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1045 // fields, it has class MEMORY.
1046 //
1047 // Note, skip this test for bit-fields, see below.
1048 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1049 Lo = Memory;
1050 return;
1051 }
1052
1053 // Classify this field.
1054 //
1055 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1056 // exceeds a single eightbyte, each is classified
1057 // separately. Each eightbyte gets initialized to class
1058 // NO_CLASS.
1059 Class FieldLo, FieldHi;
1060
1061 // Bit-fields require special handling, they do not force the
1062 // structure to be passed in memory even if unaligned, and
1063 // therefore they can straddle an eightbyte.
1064 if (BitField) {
1065 // Ignore padding bit-fields.
1066 if (i->isUnnamedBitfield())
1067 continue;
1068
1069 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1070 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1071
1072 uint64_t EB_Lo = Offset / 64;
1073 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1074 FieldLo = FieldHi = NoClass;
1075 if (EB_Lo) {
1076 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1077 FieldLo = NoClass;
1078 FieldHi = Integer;
1079 } else {
1080 FieldLo = Integer;
1081 FieldHi = EB_Hi ? Integer : NoClass;
1082 }
1083 } else
1084 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1085 Lo = merge(Lo, FieldLo);
1086 Hi = merge(Hi, FieldHi);
1087 if (Lo == Memory || Hi == Memory)
1088 break;
1089 }
1090
1091 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1092 //
1093 // (a) If one of the classes is MEMORY, the whole argument is
1094 // passed in memory.
1095 //
1096 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1097
1098 // The first of these conditions is guaranteed by how we implement
1099 // the merge (just bail).
1100 //
1101 // The second condition occurs in the case of unions; for example
1102 // union { _Complex double; unsigned; }.
1103 if (Hi == Memory)
1104 Lo = Memory;
1105 if (Hi == SSEUp && Lo != SSE)
1106 Hi = SSE;
1107 }
1108}
1109
1110ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1111 const llvm::Type *CoerceTo,
1112 ASTContext &Context) const {
Chris Lattner7f215c12010-06-26 21:52:32 +00001113 if (CoerceTo->isIntegerTy(64)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001114 // Integer and pointer types will end up in a general purpose
1115 // register.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001116
1117 // Treat an enum type as its underlying type.
1118 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1119 Ty = EnumTy->getDecl()->getIntegerType();
1120
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001121 if (Ty->isIntegralOrEnumerationType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001122 return (Ty->isPromotableIntegerType() ?
1123 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001124
Chris Lattner8ff29642010-06-28 21:59:07 +00001125 // If this is a 8/16/32-bit structure that is passed as an int64, then it
1126 // will be passed in the low 8/16/32-bits of a 64-bit GPR, which is the same
1127 // as how an i8/i16/i32 is passed. Coerce to a i8/i16/i32 instead of a i64.
1128 switch (Context.getTypeSizeInChars(Ty).getQuantity()) {
1129 default: break;
1130 case 1: CoerceTo = llvm::Type::getInt8Ty(CoerceTo->getContext()); break;
1131 case 2: CoerceTo = llvm::Type::getInt16Ty(CoerceTo->getContext()); break;
1132 case 4: CoerceTo = llvm::Type::getInt32Ty(CoerceTo->getContext()); break;
1133 }
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001134
Chris Lattner7f215c12010-06-26 21:52:32 +00001135 } else if (CoerceTo->isDoubleTy()) {
John McCall0b0ef0a2010-02-24 07:14:12 +00001136 assert(Ty.isCanonical() && "should always have a canonical type here");
1137 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138
1139 // Float and double end up in a single SSE reg.
John McCall0b0ef0a2010-02-24 07:14:12 +00001140 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141 return ABIArgInfo::getDirect();
1142
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001143 // If this is a 32-bit structure that is passed as a double, then it will be
1144 // passed in the low 32-bits of the XMM register, which is the same as how a
1145 // float is passed. Coerce to a float instead of a double.
1146 if (Context.getTypeSizeInChars(Ty).getQuantity() == 4)
1147 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001148 }
1149
1150 return ABIArgInfo::getCoerce(CoerceTo);
1151}
1152
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001153ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1154 ASTContext &Context) const {
1155 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1156 // place naturally.
1157 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1158 // Treat an enum type as its underlying type.
1159 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1160 Ty = EnumTy->getDecl()->getIntegerType();
1161
1162 return (Ty->isPromotableIntegerType() ?
1163 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1164 }
1165
1166 return ABIArgInfo::getIndirect(0);
1167}
1168
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001169ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1170 ASTContext &Context) const {
1171 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1172 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001173 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1174 // Treat an enum type as its underlying type.
1175 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1176 Ty = EnumTy->getDecl()->getIntegerType();
1177
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001178 return (Ty->isPromotableIntegerType() ?
1179 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001180 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001181
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001182 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1183 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001184
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001185 // Compute the byval alignment. We trust the back-end to honor the
1186 // minimum ABI alignment for byval, to make cleaner IR.
1187 const unsigned MinABIAlign = 8;
1188 unsigned Align = Context.getTypeAlign(Ty) / 8;
1189 if (Align > MinABIAlign)
1190 return ABIArgInfo::getIndirect(Align);
1191 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001192}
1193
Chris Lattner1090a9b2010-06-28 21:43:59 +00001194ABIArgInfo X86_64ABIInfo::
1195classifyReturnType(QualType RetTy, ASTContext &Context,
1196 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001197 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1198 // classification algorithm.
1199 X86_64ABIInfo::Class Lo, Hi;
1200 classify(RetTy, Context, 0, Lo, Hi);
1201
1202 // Check some invariants.
1203 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1204 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1205 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1206
1207 const llvm::Type *ResType = 0;
1208 switch (Lo) {
1209 case NoClass:
1210 return ABIArgInfo::getIgnore();
1211
1212 case SSEUp:
1213 case X87Up:
1214 assert(0 && "Invalid classification for lo word.");
1215
1216 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1217 // hidden argument.
1218 case Memory:
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001219 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001220
1221 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1222 // available register of the sequence %rax, %rdx is used.
1223 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001224 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001225
1226 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1227 // available SSE register of the sequence %xmm0, %xmm1 is used.
1228 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001229 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001230
1231 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1232 // returned on the X87 stack in %st0 as 80-bit x87 number.
1233 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001234 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001235
1236 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1237 // part of the value is returned in %st0 and the imaginary part in
1238 // %st1.
1239 case ComplexX87:
1240 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner52d9ae32010-04-06 17:29:22 +00001241 ResType = llvm::StructType::get(VMContext,
1242 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson0032b272009-08-13 21:57:51 +00001243 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001244 NULL);
1245 break;
1246 }
1247
1248 switch (Hi) {
1249 // Memory was handled previously and X87 should
1250 // never occur as a hi class.
1251 case Memory:
1252 case X87:
1253 assert(0 && "Invalid classification for hi word.");
1254
1255 case ComplexX87: // Previously handled.
1256 case NoClass: break;
1257
1258 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001259 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001260 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001261 break;
1262 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001263 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001264 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001265 break;
1266
1267 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1268 // is passed in the upper half of the last used SSE register.
1269 //
1270 // SSEUP should always be preceeded by SSE, just widen.
1271 case SSEUp:
1272 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001273 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001274 break;
1275
1276 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1277 // returned together with the previous X87 value in %st0.
1278 case X87Up:
1279 // If X87Up is preceeded by X87, we don't need to do
1280 // anything. However, in some cases with unions it may not be
1281 // preceeded by X87. In such situations we follow gcc and pass the
1282 // extra bits in an SSE reg.
1283 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001284 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001285 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001286 break;
1287 }
1288
1289 return getCoerceResult(RetTy, ResType, Context);
1290}
1291
1292ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001293 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001294 unsigned &neededInt,
1295 unsigned &neededSSE) const {
1296 X86_64ABIInfo::Class Lo, Hi;
1297 classify(Ty, Context, 0, Lo, Hi);
1298
1299 // Check some invariants.
1300 // FIXME: Enforce these by construction.
1301 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1302 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1303 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1304
1305 neededInt = 0;
1306 neededSSE = 0;
1307 const llvm::Type *ResType = 0;
1308 switch (Lo) {
1309 case NoClass:
1310 return ABIArgInfo::getIgnore();
1311
1312 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1313 // on the stack.
1314 case Memory:
1315
1316 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1317 // COMPLEX_X87, it is passed in memory.
1318 case X87:
1319 case ComplexX87:
1320 return getIndirectResult(Ty, Context);
1321
1322 case SSEUp:
1323 case X87Up:
1324 assert(0 && "Invalid classification for lo word.");
1325
1326 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1327 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1328 // and %r9 is used.
1329 case Integer:
1330 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001331 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001332 break;
1333
1334 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1335 // available SSE register is used, the registers are taken in the
1336 // order from %xmm0 to %xmm7.
1337 case SSE:
1338 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001339 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340 break;
1341 }
1342
1343 switch (Hi) {
1344 // Memory was handled previously, ComplexX87 and X87 should
1345 // never occur as hi classes, and X87Up must be preceed by X87,
1346 // which is passed in memory.
1347 case Memory:
1348 case X87:
1349 case ComplexX87:
1350 assert(0 && "Invalid classification for hi word.");
1351 break;
1352
1353 case NoClass: break;
1354 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001355 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001356 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001357 ++neededInt;
1358 break;
1359
1360 // X87Up generally doesn't occur here (long double is passed in
1361 // memory), except in situations involving unions.
1362 case X87Up:
1363 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001364 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001365 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001366 ++neededSSE;
1367 break;
1368
1369 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1370 // eightbyte is passed in the upper half of the last used SSE
1371 // register.
1372 case SSEUp:
1373 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001374 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001375 break;
1376 }
1377
1378 return getCoerceResult(Ty, ResType, Context);
1379}
1380
Owen Andersona1cf15f2009-07-14 23:10:40 +00001381void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00001382 llvm::LLVMContext &VMContext,
1383 const llvm::Type *const *PrefTypes,
1384 unsigned NumPrefTypes) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001385 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1386 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001387
1388 // Keep track of the number of assigned registers.
1389 unsigned freeIntRegs = 6, freeSSERegs = 8;
1390
1391 // If the return value is indirect, then the hidden argument is consuming one
1392 // integer register.
1393 if (FI.getReturnInfo().isIndirect())
1394 --freeIntRegs;
1395
1396 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1397 // get assigned (in left-to-right order) for passing as follows...
1398 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1399 it != ie; ++it) {
1400 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001401 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001402 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001403
1404 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1405 // eightbyte of an argument, the whole argument is passed on the
1406 // stack. If registers have already been assigned for some
1407 // eightbytes of such an argument, the assignments get reverted.
1408 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1409 freeIntRegs -= neededInt;
1410 freeSSERegs -= neededSSE;
1411 } else {
1412 it->info = getIndirectResult(it->type, Context);
1413 }
1414 }
1415}
1416
1417static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1418 QualType Ty,
1419 CodeGenFunction &CGF) {
1420 llvm::Value *overflow_arg_area_p =
1421 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1422 llvm::Value *overflow_arg_area =
1423 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1424
1425 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1426 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1427 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1428 if (Align > 8) {
1429 // Note that we follow the ABI & gcc here, even though the type
1430 // could in theory have an alignment greater than 16. This case
1431 // shouldn't ever matter in practice.
1432
1433 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001434 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001435 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001436 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1437 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001438 CGF.Int64Ty);
1439 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001440 overflow_arg_area =
1441 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1442 overflow_arg_area->getType(),
1443 "overflow_arg_area.align");
1444 }
1445
1446 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1447 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1448 llvm::Value *Res =
1449 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001450 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001451
1452 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1453 // l->overflow_arg_area + sizeof(type).
1454 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1455 // an 8 byte boundary.
1456
1457 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001458 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001459 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001460 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1461 "overflow_arg_area.next");
1462 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1463
1464 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1465 return Res;
1466}
1467
1468llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1469 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001470 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001472 // Assume that va_list type is correct; should be pointer to LLVM type:
1473 // struct {
1474 // i32 gp_offset;
1475 // i32 fp_offset;
1476 // i8* overflow_arg_area;
1477 // i8* reg_save_area;
1478 // };
1479 unsigned neededInt, neededSSE;
Chris Lattnera14db752010-03-11 18:19:55 +00001480
1481 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001482 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001483 neededInt, neededSSE);
1484
1485 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1486 // in the registers. If not go to step 7.
1487 if (!neededInt && !neededSSE)
1488 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1489
1490 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1491 // general purpose registers needed to pass type and num_fp to hold
1492 // the number of floating point registers needed.
1493
1494 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1495 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1496 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1497 //
1498 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1499 // register save space).
1500
1501 llvm::Value *InRegs = 0;
1502 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1503 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1504 if (neededInt) {
1505 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1506 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001507 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1508 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 }
1510
1511 if (neededSSE) {
1512 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1513 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1514 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001515 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1516 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001517 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1518 }
1519
1520 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1521 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1522 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1523 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1524
1525 // Emit code to load the value if it was passed in registers.
1526
1527 CGF.EmitBlock(InRegBlock);
1528
1529 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1530 // an offset of l->gp_offset and/or l->fp_offset. This may require
1531 // copying to a temporary location in case the parameter is passed
1532 // in different register classes or requires an alignment greater
1533 // than 8 for general purpose registers and 16 for XMM registers.
1534 //
1535 // FIXME: This really results in shameful code when we end up needing to
1536 // collect arguments from different places; often what should result in a
1537 // simple assembling of a structure from scattered addresses has many more
1538 // loads than necessary. Can we clean this up?
1539 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1540 llvm::Value *RegAddr =
1541 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1542 "reg_save_area");
1543 if (neededInt && neededSSE) {
1544 // FIXME: Cleanup.
1545 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1546 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1547 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1548 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1549 const llvm::Type *TyLo = ST->getElementType(0);
1550 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001551 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001552 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001553 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1554 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001555 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1556 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001557 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1558 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001559 llvm::Value *V =
1560 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1561 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1562 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1563 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1564
Owen Andersona1cf15f2009-07-14 23:10:40 +00001565 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001566 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001567 } else if (neededInt) {
1568 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1569 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001570 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001571 } else if (neededSSE == 1) {
1572 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1573 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1574 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001575 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001576 assert(neededSSE == 2 && "Invalid number of needed registers!");
1577 // SSE registers are spaced 16 bytes apart in the register save
1578 // area, we need to collect the two eightbytes together.
1579 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001580 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001581 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1582 const llvm::Type *DblPtrTy =
1583 llvm::PointerType::getUnqual(DoubleTy);
1584 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1585 DoubleTy, NULL);
1586 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1587 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1588 DblPtrTy));
1589 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1590 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1591 DblPtrTy));
1592 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1593 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1594 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001595 }
1596
1597 // AMD64-ABI 3.5.7p5: Step 5. Set:
1598 // l->gp_offset = l->gp_offset + num_gp * 8
1599 // l->fp_offset = l->fp_offset + num_fp * 16.
1600 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001601 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001602 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1603 gp_offset_p);
1604 }
1605 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001606 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001607 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1608 fp_offset_p);
1609 }
1610 CGF.EmitBranch(ContBlock);
1611
1612 // Emit code to load the value if it was passed in memory.
1613
1614 CGF.EmitBlock(InMemBlock);
1615 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1616
1617 // Return the appropriate result.
1618
1619 CGF.EmitBlock(ContBlock);
1620 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1621 "vaarg.addr");
1622 ResAddr->reserveOperandSpace(2);
1623 ResAddr->addIncoming(RegAddr, InRegBlock);
1624 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001625 return ResAddr;
1626}
1627
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001628
1629
1630//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001631// PIC16 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001632//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001633
1634namespace {
1635
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001636class PIC16ABIInfo : public ABIInfo {
1637 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001638 ASTContext &Context,
1639 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001640
1641 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001642 ASTContext &Context,
1643 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001644
Owen Andersona1cf15f2009-07-14 23:10:40 +00001645 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00001646 llvm::LLVMContext &VMContext,
1647 const llvm::Type *const *PrefTypes,
1648 unsigned NumPrefTypes) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001649 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1650 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001651 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1652 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001653 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001654 }
1655
1656 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1657 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001658};
1659
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001660class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1661public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001662 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001663};
1664
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001665}
1666
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001667ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001668 ASTContext &Context,
1669 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001670 if (RetTy->isVoidType()) {
1671 return ABIArgInfo::getIgnore();
1672 } else {
1673 return ABIArgInfo::getDirect();
1674 }
1675}
1676
1677ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001678 ASTContext &Context,
1679 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001680 return ABIArgInfo::getDirect();
1681}
1682
1683llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00001684 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00001685 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001686 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1687
1688 CGBuilderTy &Builder = CGF.Builder;
1689 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1690 "ap");
1691 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1692 llvm::Type *PTy =
1693 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1694 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1695
1696 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1697
1698 llvm::Value *NextAddr =
1699 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1700 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1701 "ap.next");
1702 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1703
1704 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001705}
1706
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001707
John McCallec853ba2010-03-11 00:10:12 +00001708// PowerPC-32
1709
1710namespace {
1711class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1712public:
1713 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1714 // This is recovered from gcc output.
1715 return 1; // r1 is the dedicated stack pointer
1716 }
1717
1718 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1719 llvm::Value *Address) const;
1720};
1721
1722}
1723
1724bool
1725PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1726 llvm::Value *Address) const {
1727 // This is calculated from the LLVM and GCC tables and verified
1728 // against gcc output. AFAIK all ABIs use the same encoding.
1729
1730 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1731 llvm::LLVMContext &Context = CGF.getLLVMContext();
1732
1733 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1734 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1735 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1736 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1737
1738 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00001739 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00001740
1741 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00001742 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00001743
1744 // 64-76 are various 4-byte special-purpose registers:
1745 // 64: mq
1746 // 65: lr
1747 // 66: ctr
1748 // 67: ap
1749 // 68-75 cr0-7
1750 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00001751 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00001752
1753 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00001754 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00001755
1756 // 109: vrsave
1757 // 110: vscr
1758 // 111: spe_acc
1759 // 112: spefscr
1760 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00001761 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00001762
1763 return false;
1764}
1765
1766
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001767//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001768// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001769//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001770
1771namespace {
1772
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001773class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001774public:
1775 enum ABIKind {
1776 APCS = 0,
1777 AAPCS = 1,
1778 AAPCS_VFP
1779 };
1780
1781private:
1782 ABIKind Kind;
1783
1784public:
1785 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1786
1787private:
1788 ABIKind getABIKind() const { return Kind; }
1789
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001790 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001791 ASTContext &Context,
1792 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001793
1794 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001795 ASTContext &Context,
1796 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001797
Owen Andersona1cf15f2009-07-14 23:10:40 +00001798 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00001799 llvm::LLVMContext &VMContext,
1800 const llvm::Type *const *PrefTypes,
1801 unsigned NumPrefTypes) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001802
1803 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1804 CodeGenFunction &CGF) const;
1805};
1806
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001807class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1808public:
1809 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001810 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCall6374c332010-03-06 00:35:14 +00001811
1812 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1813 return 13;
1814 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001815};
1816
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001817}
1818
Owen Andersona1cf15f2009-07-14 23:10:40 +00001819void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00001820 llvm::LLVMContext &VMContext,
1821 const llvm::Type *const *PrefTypes,
1822 unsigned NumPrefTypes) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001823 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001824 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001825 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1826 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001827 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001828 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001829
Rafael Espindola25117ab2010-06-16 16:13:39 +00001830 const llvm::Triple &Triple(Context.Target.getTriple());
1831 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001832 if (Triple.getEnvironmentName() == "gnueabi" ||
1833 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00001834 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001835 else
1836 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00001837
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001838 switch (getABIKind()) {
1839 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001840 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1841 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001842 break;
1843
1844 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001845 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1846 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001847 break;
1848
1849 case AAPCS_VFP:
1850 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1851 break;
1852 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001853}
1854
1855ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001856 ASTContext &Context,
1857 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001858 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1859 // Treat an enum type as its underlying type.
1860 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1861 Ty = EnumTy->getDecl()->getIntegerType();
1862
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001863 return (Ty->isPromotableIntegerType() ?
1864 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001865 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001866
Daniel Dunbar42025572009-09-14 21:54:03 +00001867 // Ignore empty records.
1868 if (isEmptyRecord(Context, Ty, true))
1869 return ABIArgInfo::getIgnore();
1870
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001871 // Structures with either a non-trivial destructor or a non-trivial
1872 // copy constructor are always indirect.
1873 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1874 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1875
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001876 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1877 // backend doesn't support byval.
1878 // FIXME: This doesn't handle alignment > 64 bits.
1879 const llvm::Type* ElemTy;
1880 unsigned SizeRegs;
1881 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001882 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001883 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1884 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001885 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001886 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1887 }
1888 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001889 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001890 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001891 return ABIArgInfo::getCoerce(STy);
1892}
1893
Daniel Dunbar98303b92009-09-13 08:03:58 +00001894static bool isIntegerLikeType(QualType Ty,
1895 ASTContext &Context,
1896 llvm::LLVMContext &VMContext) {
1897 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1898 // is called integer-like if its size is less than or equal to one word, and
1899 // the offset of each of its addressable sub-fields is zero.
1900
1901 uint64_t Size = Context.getTypeSize(Ty);
1902
1903 // Check that the type fits in a word.
1904 if (Size > 32)
1905 return false;
1906
1907 // FIXME: Handle vector types!
1908 if (Ty->isVectorType())
1909 return false;
1910
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001911 // Float types are never treated as "integer like".
1912 if (Ty->isRealFloatingType())
1913 return false;
1914
Daniel Dunbar98303b92009-09-13 08:03:58 +00001915 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001916 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001917 return true;
1918
Daniel Dunbar45815812010-02-01 23:31:26 +00001919 // Small complex integer types are "integer like".
1920 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1921 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001922
1923 // Single element and zero sized arrays should be allowed, by the definition
1924 // above, but they are not.
1925
1926 // Otherwise, it must be a record type.
1927 const RecordType *RT = Ty->getAs<RecordType>();
1928 if (!RT) return false;
1929
1930 // Ignore records with flexible arrays.
1931 const RecordDecl *RD = RT->getDecl();
1932 if (RD->hasFlexibleArrayMember())
1933 return false;
1934
1935 // Check that all sub-fields are at offset 0, and are themselves "integer
1936 // like".
1937 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1938
1939 bool HadField = false;
1940 unsigned idx = 0;
1941 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1942 i != e; ++i, ++idx) {
1943 const FieldDecl *FD = *i;
1944
Daniel Dunbar679855a2010-01-29 03:22:29 +00001945 // Bit-fields are not addressable, we only need to verify they are "integer
1946 // like". We still have to disallow a subsequent non-bitfield, for example:
1947 // struct { int : 0; int x }
1948 // is non-integer like according to gcc.
1949 if (FD->isBitField()) {
1950 if (!RD->isUnion())
1951 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001952
Daniel Dunbar679855a2010-01-29 03:22:29 +00001953 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1954 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001955
Daniel Dunbar679855a2010-01-29 03:22:29 +00001956 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001957 }
1958
Daniel Dunbar679855a2010-01-29 03:22:29 +00001959 // Check if this field is at offset 0.
1960 if (Layout.getFieldOffset(idx) != 0)
1961 return false;
1962
Daniel Dunbar98303b92009-09-13 08:03:58 +00001963 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1964 return false;
1965
Daniel Dunbar679855a2010-01-29 03:22:29 +00001966 // Only allow at most one field in a structure. This doesn't match the
1967 // wording above, but follows gcc in situations with a field following an
1968 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001969 if (!RD->isUnion()) {
1970 if (HadField)
1971 return false;
1972
1973 HadField = true;
1974 }
1975 }
1976
1977 return true;
1978}
1979
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001980ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001981 ASTContext &Context,
1982 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001983 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001984 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001985
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001986 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1987 // Treat an enum type as its underlying type.
1988 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1989 RetTy = EnumTy->getDecl()->getIntegerType();
1990
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001991 return (RetTy->isPromotableIntegerType() ?
1992 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001993 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001994
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001995 // Structures with either a non-trivial destructor or a non-trivial
1996 // copy constructor are always indirect.
1997 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
1998 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1999
Daniel Dunbar98303b92009-09-13 08:03:58 +00002000 // Are we following APCS?
2001 if (getABIKind() == APCS) {
2002 if (isEmptyRecord(Context, RetTy, false))
2003 return ABIArgInfo::getIgnore();
2004
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002005 // Complex types are all returned as packed integers.
2006 //
2007 // FIXME: Consider using 2 x vector types if the back end handles them
2008 // correctly.
2009 if (RetTy->isAnyComplexType())
2010 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
2011 VMContext, Context.getTypeSize(RetTy)));
2012
Daniel Dunbar98303b92009-09-13 08:03:58 +00002013 // Integer like structures are returned in r0.
2014 if (isIntegerLikeType(RetTy, Context, VMContext)) {
2015 // Return in the smallest viable integer type.
2016 uint64_t Size = Context.getTypeSize(RetTy);
2017 if (Size <= 8)
2018 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2019 if (Size <= 16)
2020 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
2021 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
2022 }
2023
2024 // Otherwise return in memory.
2025 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002026 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002027
2028 // Otherwise this is an AAPCS variant.
2029
Daniel Dunbar16a08082009-09-14 00:56:55 +00002030 if (isEmptyRecord(Context, RetTy, true))
2031 return ABIArgInfo::getIgnore();
2032
Daniel Dunbar98303b92009-09-13 08:03:58 +00002033 // Aggregates <= 4 bytes are returned in r0; other aggregates
2034 // are returned indirectly.
2035 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002036 if (Size <= 32) {
2037 // Return in the smallest viable integer type.
2038 if (Size <= 8)
2039 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2040 if (Size <= 16)
2041 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002042 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002043 }
2044
Daniel Dunbar98303b92009-09-13 08:03:58 +00002045 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002046}
2047
2048llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002049 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002050 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002051 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002052 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002053
2054 CGBuilderTy &Builder = CGF.Builder;
2055 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2056 "ap");
2057 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2058 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002059 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002060 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2061
2062 uint64_t Offset =
2063 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2064 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002065 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002066 "ap.next");
2067 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2068
2069 return AddrTyped;
2070}
2071
2072ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00002073 ASTContext &Context,
2074 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002075 if (RetTy->isVoidType()) {
2076 return ABIArgInfo::getIgnore();
2077 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2078 return ABIArgInfo::getIndirect(0);
2079 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002080 // Treat an enum type as its underlying type.
2081 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2082 RetTy = EnumTy->getDecl()->getIntegerType();
2083
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002084 return (RetTy->isPromotableIntegerType() ?
2085 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002086 }
2087}
2088
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002089//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002090// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002091//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002092
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002093namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002094
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002095class SystemZABIInfo : public ABIInfo {
2096 bool isPromotableIntegerType(QualType Ty) const;
2097
2098 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2099 llvm::LLVMContext &VMContext) const;
2100
2101 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2102 llvm::LLVMContext &VMContext) const;
2103
2104 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner8640cd62010-06-29 01:08:48 +00002105 llvm::LLVMContext &VMContext,
2106 const llvm::Type *const *PrefTypes,
2107 unsigned NumPrefTypes) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002108 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2109 Context, VMContext);
2110 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2111 it != ie; ++it)
2112 it->info = classifyArgumentType(it->type, Context, VMContext);
2113 }
2114
2115 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2116 CodeGenFunction &CGF) const;
2117};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002118
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002119class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2120public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002121 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002122};
2123
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002124}
2125
2126bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2127 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002128 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002129 switch (BT->getKind()) {
2130 case BuiltinType::Bool:
2131 case BuiltinType::Char_S:
2132 case BuiltinType::Char_U:
2133 case BuiltinType::SChar:
2134 case BuiltinType::UChar:
2135 case BuiltinType::Short:
2136 case BuiltinType::UShort:
2137 case BuiltinType::Int:
2138 case BuiltinType::UInt:
2139 return true;
2140 default:
2141 return false;
2142 }
2143 return false;
2144}
2145
2146llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2147 CodeGenFunction &CGF) const {
2148 // FIXME: Implement
2149 return 0;
2150}
2151
2152
2153ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2154 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002155 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002156 if (RetTy->isVoidType()) {
2157 return ABIArgInfo::getIgnore();
2158 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2159 return ABIArgInfo::getIndirect(0);
2160 } else {
2161 return (isPromotableIntegerType(RetTy) ?
2162 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2163 }
2164}
2165
2166ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2167 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002168 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002169 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2170 return ABIArgInfo::getIndirect(0);
2171 } else {
2172 return (isPromotableIntegerType(Ty) ?
2173 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2174 }
2175}
2176
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002177//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002178// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002179//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002180
2181namespace {
2182
2183class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2184public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002185 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002186 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2187 CodeGen::CodeGenModule &M) const;
2188};
2189
2190}
2191
2192void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2193 llvm::GlobalValue *GV,
2194 CodeGen::CodeGenModule &M) const {
2195 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2196 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2197 // Handle 'interrupt' attribute:
2198 llvm::Function *F = cast<llvm::Function>(GV);
2199
2200 // Step 1: Set ISR calling convention.
2201 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2202
2203 // Step 2: Add attributes goodness.
2204 F->addFnAttr(llvm::Attribute::NoInline);
2205
2206 // Step 3: Emit ISR vector alias.
2207 unsigned Num = attr->getNumber() + 0xffe0;
2208 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2209 "vector_" +
2210 llvm::LowercaseString(llvm::utohexstr(Num)),
2211 GV, &M.getModule());
2212 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002213 }
2214}
2215
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002216//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002217// MIPS ABI Implementation. This works for both little-endian and
2218// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002219//===----------------------------------------------------------------------===//
2220
John McCallaeeb7012010-05-27 06:19:26 +00002221namespace {
2222class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2223public:
2224 MIPSTargetCodeGenInfo(): TargetCodeGenInfo(new DefaultABIInfo()) {}
2225
2226 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2227 return 29;
2228 }
2229
2230 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2231 llvm::Value *Address) const;
2232};
2233}
2234
2235bool
2236MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2237 llvm::Value *Address) const {
2238 // This information comes from gcc's implementation, which seems to
2239 // as canonical as it gets.
2240
2241 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2242 llvm::LLVMContext &Context = CGF.getLLVMContext();
2243
2244 // Everything on MIPS is 4 bytes. Double-precision FP registers
2245 // are aliased to pairs of single-precision FP registers.
2246 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2247 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2248
2249 // 0-31 are the general purpose registers, $0 - $31.
2250 // 32-63 are the floating-point registers, $f0 - $f31.
2251 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2252 // 66 is the (notional, I think) register for signal-handler return.
2253 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2254
2255 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2256 // They are one bit wide and ignored here.
2257
2258 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2259 // (coprocessor 1 is the FP unit)
2260 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2261 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2262 // 176-181 are the DSP accumulator registers.
2263 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2264
2265 return false;
2266}
2267
2268
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002269const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2270 if (TheTargetCodeGenInfo)
2271 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002272
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002273 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2274 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002275
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002276 const llvm::Triple &Triple(getContext().Target.getTriple());
2277 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002278 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002279 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002280
John McCallaeeb7012010-05-27 06:19:26 +00002281 case llvm::Triple::mips:
2282 case llvm::Triple::mipsel:
2283 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo());
2284
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002285 case llvm::Triple::arm:
2286 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002287 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002288 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002289 return *(TheTargetCodeGenInfo =
2290 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002291
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002292 return *(TheTargetCodeGenInfo =
2293 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002294
2295 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002296 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002297
John McCallec853ba2010-03-11 00:10:12 +00002298 case llvm::Triple::ppc:
2299 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2300
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002301 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002302 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2303
2304 case llvm::Triple::msp430:
2305 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002306
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002307 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002308 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002309 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002310 return *(TheTargetCodeGenInfo =
2311 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002312 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002313 case llvm::Triple::MinGW32:
2314 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002315 case llvm::Triple::AuroraUX:
2316 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002317 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002318 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002319 return *(TheTargetCodeGenInfo =
2320 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002321
2322 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002323 return *(TheTargetCodeGenInfo =
2324 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002325 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002326
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002327 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002328 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002329 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002330}